db

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdminWorkspaceInfo added in v0.18.3

type AdminWorkspaceInfo struct {
	Workspace
	OwnerID      *string
	OwnerEmail   *string
	OwnerName    *string
	OwnerPicture *string
	SandboxCount int
}

AdminWorkspaceInfo holds enriched workspace data for the admin panel.

type AgentInfo added in v0.16.0

type AgentInfo struct {
	SandboxID       string          `json:"sandbox_id"`
	Hostname        string          `json:"hostname"`
	OS              string          `json:"os"`
	Platform        string          `json:"platform"`
	PlatformVersion string          `json:"platform_version"`
	KernelArch      string          `json:"kernel_arch"`
	CPUModelName    string          `json:"cpu_model_name"`
	CPUCountLogical int             `json:"cpu_count_logical"`
	MemoryTotal     int64           `json:"memory_total"`
	DiskTotal       int64           `json:"disk_total"`
	DiskFree        int64           `json:"disk_free"`
	AgentVersion    string          `json:"agent_version"`
	OpencodeVersion string          `json:"opencode_version"`
	Workdir         string          `json:"workdir"`
	HostInfo        json.RawMessage `json:"host_info"`
	CPUInfo         json.RawMessage `json:"cpu_info"`
	MemoryInfo      json.RawMessage `json:"memory_info"`
	DiskInfo        json.RawMessage `json:"disk_info"`
	UpdatedAt       time.Time       `json:"updated_at"`
}

AgentInfo holds system information reported by a local agent.

type AgentRegistrationCode

type AgentRegistrationCode struct {
	Code        string
	UserID      string
	WorkspaceID string
	CreatedAt   time.Time
	ExpiresAt   time.Time
	Used        bool
}

type DB

type DB struct {
	*sql.DB
}

DB wraps a *sql.DB with migration support.

func Open

func Open(databaseURL string) (*DB, error)

Open connects to PostgreSQL and runs migrations.

func (*DB) AddWorkspaceMember

func (db *DB) AddWorkspaceMember(workspaceID, userID, role string) error

func (*DB) AddWorkspaceVolume added in v0.9.0

func (db *DB) AddWorkspaceVolume(id, workspaceID, pvcName, mountPath string) error

func (*DB) ConsumeAgentRegistrationCode

func (db *DB) ConsumeAgentRegistrationCode(code string) (*AgentRegistrationCode, error)

ConsumeAgentRegistrationCode atomically validates and marks a code as used. Returns the code record if valid, nil if not found/expired/used.

func (*DB) CountSandboxesByWorkspace added in v0.7.0

func (db *DB) CountSandboxesByWorkspace(workspaceID string) (int, error)

func (*DB) CountUsers added in v0.6.1

func (db *DB) CountUsers() (int, error)

func (*DB) CountWorkspacesOwnedByUser added in v0.7.0

func (db *DB) CountWorkspacesOwnedByUser(userID string) (int, error)

func (*DB) CreateAgentRegistrationCode

func (db *DB) CreateAgentRegistrationCode(code, userID, workspaceID string, expiresAt time.Time) error

CreateAgentRegistrationCode inserts a new one-time registration code.

func (*DB) CreateLocalSandbox

func (db *DB) CreateLocalSandbox(id, workspaceID, name, sandboxType, opencodeToken, proxyToken, tunnelToken, shortID string) error

CreateLocalSandbox inserts a local agent sandbox with is_local=true.

func (*DB) CreateOIDCIdentity

func (db *DB) CreateOIDCIdentity(provider, subject, userID string, email *string) error

func (*DB) CreateSandbox

func (db *DB) CreateSandbox(id, workspaceID, name, sandboxType, sandboxName, opencodeToken, proxyToken, openclawToken, shortID string, cpu int, memory int64, idleTimeout *int) error

func (*DB) CreateToken

func (db *DB) CreateToken(token, userID string, expiresAt time.Time) error

func (*DB) CreateUser

func (db *DB) CreateUser(id, email, passwordHash string) error

func (*DB) CreateUserWithEmail

func (db *DB) CreateUserWithEmail(id string, passwordHash *string, email string) error

func (*DB) CreateWeixinBinding added in v0.22.0

func (db *DB) CreateWeixinBinding(sandboxID, botID, userID string) error

CreateWeixinBinding inserts a new binding record after a successful QR login.

func (*DB) CreateWorkspace

func (db *DB) CreateWorkspace(id, name string) error

func (*DB) DeleteExpiredTokens

func (db *DB) DeleteExpiredTokens() error

func (*DB) DeleteModelserverConnection added in v0.22.0

func (db *DB) DeleteModelserverConnection(workspaceID string) error

func (*DB) DeleteSandbox

func (db *DB) DeleteSandbox(id string) error

func (*DB) DeleteUserQuota added in v0.7.0

func (db *DB) DeleteUserQuota(userID string) error

func (*DB) DeleteWorkspace

func (db *DB) DeleteWorkspace(id string) error

func (*DB) DeleteWorkspaceLLMConfig added in v0.22.0

func (db *DB) DeleteWorkspaceLLMConfig(workspaceID string) error

func (*DB) DeleteWorkspaceQuota added in v0.9.0

func (db *DB) DeleteWorkspaceQuota(workspaceID string) error

func (*DB) GetAgentInfo added in v0.16.0

func (db *DB) GetAgentInfo(sandboxID string) (*AgentInfo, error)

GetAgentInfo returns agent info for a sandbox, or nil,nil if not found.

func (*DB) GetAllWorkspaceNamespaces

func (db *DB) GetAllWorkspaceNamespaces() ([]string, error)

func (*DB) GetBindingsWithBotToken added in v0.26.0

func (db *DB) GetBindingsWithBotToken() ([]*WeixinBinding, error)

GetBindingsWithBotToken returns all nanoclaw bindings that have a bot_token set, for starting long-poll goroutines. Only returns bindings for running nanoclaw sandboxes.

func (*DB) GetBotCredentials added in v0.26.0

func (db *DB) GetBotCredentials(sandboxID, botID string) (botToken, baseURL string, err error)

GetBotCredentials returns the bot_token and ilink_base_url for a specific binding. Used by the outbound bridge endpoint to send messages via iLink.

func (*DB) GetContextToken added in v0.26.0

func (db *DB) GetContextToken(sandboxID, botID, userID string) (string, error)

GetContextToken retrieves the cached context_token for a user.

func (*DB) GetModelserverConnection added in v0.22.0

func (db *DB) GetModelserverConnection(workspaceID string) (*ModelserverConnection, error)

func (*DB) GetOIDCIdentity

func (db *DB) GetOIDCIdentity(provider, subject string) (*OIDCIdentity, error)

func (*DB) GetPasswordHash added in v0.9.0

func (db *DB) GetPasswordHash(userID string) (*string, error)

GetPasswordHash returns the bcrypt hash for the given user, or nil if no credential exists.

func (*DB) GetSandbox

func (db *DB) GetSandbox(id string) (*Sandbox, error)

func (*DB) GetSandboxByBotID added in v0.26.0

func (db *DB) GetSandboxByBotID(botID string) (string, error)

GetSandboxByBotID returns the sandbox_id for a given WeChat bot_id. Used for routing inbound iLink messages to the correct NanoClaw sandbox.

func (*DB) GetSandboxByProxyToken

func (db *DB) GetSandboxByProxyToken(proxyToken string) (*Sandbox, error)

func (*DB) GetSandboxByShortID

func (db *DB) GetSandboxByShortID(shortID string) (*Sandbox, error)

func (*DB) GetSandboxByTunnelToken

func (db *DB) GetSandboxByTunnelToken(sandboxID, tunnelToken string) (*Sandbox, error)

GetSandboxByTunnelToken finds a local sandbox by its tunnel token.

func (*DB) GetSystemSetting added in v0.7.0

func (db *DB) GetSystemSetting(key string) (string, error)

func (*DB) GetUserByEmail

func (db *DB) GetUserByEmail(email string) (*User, error)

func (*DB) GetUserByID

func (db *DB) GetUserByID(id string) (*User, error)

func (*DB) GetUserQuota added in v0.7.0

func (db *DB) GetUserQuota(userID string) (*UserQuota, error)

func (*DB) GetWorkspace

func (db *DB) GetWorkspace(id string) (*Workspace, error)

func (*DB) GetWorkspaceLLMConfig added in v0.22.0

func (db *DB) GetWorkspaceLLMConfig(workspaceID string) (*WorkspaceLLMConfig, error)

func (*DB) GetWorkspaceMember

func (db *DB) GetWorkspaceMember(workspaceID, userID string) (*WorkspaceMember, error)

func (*DB) GetWorkspaceMemberRole

func (db *DB) GetWorkspaceMemberRole(workspaceID, userID string) (string, error)

func (*DB) GetWorkspaceQuota added in v0.9.0

func (db *DB) GetWorkspaceQuota(workspaceID string) (*WorkspaceQuota, error)

func (*DB) HasModelserverConnection added in v0.22.0

func (db *DB) HasModelserverConnection(workspaceID string) (bool, error)

func (*DB) IsWorkspaceMember

func (db *DB) IsWorkspaceMember(workspaceID, userID string) (bool, error)

func (*DB) ListAllActiveSandboxNames

func (db *DB) ListAllActiveSandboxNames() ([]string, error)

func (*DB) ListAllSandboxes added in v0.6.1

func (db *DB) ListAllSandboxes() ([]*Sandbox, error)

func (*DB) ListAllUsers added in v0.6.1

func (db *DB) ListAllUsers() ([]*User, error)

func (*DB) ListAllWorkspaces added in v0.6.1

func (db *DB) ListAllWorkspaces() ([]*Workspace, error)

func (*DB) ListAllWorkspacesAdmin added in v0.18.3

func (db *DB) ListAllWorkspacesAdmin() ([]*AdminWorkspaceInfo, error)

func (*DB) ListIdleSandboxes

func (db *DB) ListIdleSandboxes(defaultTimeoutSeconds int) ([]*Sandbox, error)

func (*DB) ListSandboxesByWorkspace

func (db *DB) ListSandboxesByWorkspace(workspaceID string) ([]*Sandbox, error)

func (*DB) ListWeixinBindings added in v0.22.0

func (db *DB) ListWeixinBindings(sandboxID string) ([]*WeixinBinding, error)

ListWeixinBindings returns all binding records for a sandbox, most recent first.

func (*DB) ListWorkspaceMembers

func (db *DB) ListWorkspaceMembers(workspaceID string) ([]*WorkspaceMember, error)

func (*DB) ListWorkspaceVolumes added in v0.9.0

func (db *DB) ListWorkspaceVolumes(workspaceID string) ([]WorkspaceVolume, error)

func (*DB) ListWorkspacesByUser

func (db *DB) ListWorkspacesByUser(userID string) ([]*Workspace, error)

func (*DB) ListWorkspacesWithoutNamespace

func (db *DB) ListWorkspacesWithoutNamespace() ([]*Workspace, error)

func (*DB) RemoveWorkspaceMember

func (db *DB) RemoveWorkspaceMember(workspaceID, userID string) error

func (*DB) SaveBotCredentials added in v0.26.0

func (db *DB) SaveBotCredentials(sandboxID, botID, botToken, baseURL string) error

SaveBotCredentials stores iLink bot credentials for bridge-mode messaging. Used by nanoclaw sandboxes where agentserver holds the credentials.

func (*DB) SetModelserverConnection added in v0.22.0

func (db *DB) SetModelserverConnection(c *ModelserverConnection) error

func (*DB) SetPasswordHash added in v0.9.0

func (db *DB) SetPasswordHash(userID, hash string) error

SetPasswordHash upserts a password hash for the given user.

func (*DB) SetSystemSetting added in v0.7.0

func (db *DB) SetSystemSetting(key, value string) error

func (*DB) SetUserQuota added in v0.7.0

func (db *DB) SetUserQuota(userID string, maxWorkspaces *int) error

func (*DB) SetWorkspaceLLMConfig added in v0.22.0

func (db *DB) SetWorkspaceLLMConfig(workspaceID, baseURL, apiKey string, models []LLMModel) error

func (*DB) SetWorkspaceNamespace

func (db *DB) SetWorkspaceNamespace(id, namespace string) error

func (*DB) SetWorkspaceQuota added in v0.9.0

func (db *DB) SetWorkspaceQuota(workspaceID string, maxSandboxes *int,
	maxSandboxCPU *int, maxSandboxMemory *int64, maxIdleTimeout *int, maxTotalCPU *int, maxTotalMemory *int64, maxDriveSize *int64) error

func (*DB) SumWorkspaceSandboxResources added in v0.7.0

func (db *DB) SumWorkspaceSandboxResources(workspaceID string) (cpuMillis int64, memBytes int64, err error)

SumWorkspaceSandboxResources returns the total CPU (millicores) and memory (bytes) allocated by non-offline sandboxes in a workspace.

func (*DB) UpdateGetUpdatesBuf added in v0.26.0

func (db *DB) UpdateGetUpdatesBuf(sandboxID, botID, buf string) error

UpdateGetUpdatesBuf persists the long-poll cursor for a binding.

func (*DB) UpdateModelserverTokens added in v0.22.0

func (db *DB) UpdateModelserverTokens(workspaceID, accessToken, refreshToken string, expiresAt time.Time) error

func (*DB) UpdateOIDCIdentityEmail added in v0.9.0

func (db *DB) UpdateOIDCIdentityEmail(provider, subject, email string) error

func (*DB) UpdateSandboxActivity

func (db *DB) UpdateSandboxActivity(id string) error

func (*DB) UpdateSandboxHeartbeat

func (db *DB) UpdateSandboxHeartbeat(id string) error

UpdateSandboxHeartbeat updates the last_heartbeat_at timestamp.

func (*DB) UpdateSandboxName added in v0.16.0

func (db *DB) UpdateSandboxName(id, name string) error

func (*DB) UpdateSandboxNanoclawBridgeSecret added in v0.26.0

func (db *DB) UpdateSandboxNanoclawBridgeSecret(id, secret string) error

UpdateSandboxNanoclawBridgeSecret stores the bridge secret for a nanoclaw sandbox.

func (*DB) UpdateSandboxPodIP

func (db *DB) UpdateSandboxPodIP(id, podIP string) error

func (*DB) UpdateSandboxSandboxName

func (db *DB) UpdateSandboxSandboxName(id, sandboxName string) error

func (*DB) UpdateSandboxStatus

func (db *DB) UpdateSandboxStatus(id, status string) error

func (*DB) UpdateUserEmail

func (db *DB) UpdateUserEmail(userID, email string) error

func (*DB) UpdateUserName added in v0.9.0

func (db *DB) UpdateUserName(userID, name string) error

func (*DB) UpdateUserPicture added in v0.9.0

func (db *DB) UpdateUserPicture(userID, picture string) error

func (*DB) UpdateUserRole added in v0.6.1

func (db *DB) UpdateUserRole(userID, role string) error

func (*DB) UpdateWorkspaceMemberRole

func (db *DB) UpdateWorkspaceMemberRole(workspaceID, userID, role string) error

func (*DB) UpdateWorkspaceName added in v0.16.0

func (db *DB) UpdateWorkspaceName(id, name string) error

func (*DB) UpsertAgentInfo added in v0.16.0

func (db *DB) UpsertAgentInfo(info *AgentInfo) error

UpsertAgentInfo inserts or updates agent info for a sandbox.

func (*DB) UpsertContextToken added in v0.26.0

func (db *DB) UpsertContextToken(sandboxID, botID, userID, contextToken string) error

UpsertContextToken stores or updates the context_token for a user conversation.

func (*DB) ValidateToken

func (db *DB) ValidateToken(token string) (string, error)

type LLMModel added in v0.22.0

type LLMModel struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type ModelserverConnection added in v0.22.0

type ModelserverConnection struct {
	WorkspaceID    string     `json:"workspace_id"`
	ProjectID      string     `json:"project_id"`
	ProjectName    string     `json:"project_name"`
	UserID         string     `json:"user_id"`
	AccessToken    string     `json:"-"`
	RefreshToken   string     `json:"-"`
	TokenExpiresAt time.Time  `json:"token_expires_at"`
	Models         []LLMModel `json:"models"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
}

type OIDCIdentity

type OIDCIdentity struct {
	Provider  string
	Subject   string
	UserID    string
	Email     *string
	CreatedAt time.Time
}

type Sandbox

type Sandbox struct {
	ID                   string
	WorkspaceID          string
	Name                 string
	Type                 string
	Status               string
	IsLocal              bool
	ShortID              sql.NullString
	SandboxName          sql.NullString
	PodIP                sql.NullString
	ProxyToken           sql.NullString
	OpencodeToken        sql.NullString
	OpenclawToken        sql.NullString
	TunnelToken          sql.NullString
	NanoclawBridgeSecret sql.NullString
	LastActivityAt       sql.NullTime
	CreatedAt            time.Time
	PausedAt             sql.NullTime
	LastHeartbeatAt      sql.NullTime
	CPU                  *int
	Memory               *int64
	IdleTimeout          *int
}

type User

type User struct {
	ID        string
	Email     string
	Name      *string
	Picture   *string
	Role      string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type UserQuota added in v0.7.0

type UserQuota struct {
	UserID        string
	MaxWorkspaces *int
	UpdatedAt     time.Time
}

type WeixinBinding added in v0.22.0

type WeixinBinding struct {
	ID            int
	SandboxID     string
	BotID         string
	UserID        string
	BoundAt       time.Time
	BotToken      string
	ILinkBaseURL  string
	GetUpdatesBuf string
}

WeixinBinding records a WeChat QR scan binding for a sandbox.

type Workspace

type Workspace struct {
	ID           string
	Name         string
	K8sNamespace sql.NullString
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

type WorkspaceLLMConfig added in v0.22.0

type WorkspaceLLMConfig struct {
	WorkspaceID string
	BaseURL     string
	APIKey      string
	Models      []LLMModel
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

type WorkspaceMember

type WorkspaceMember struct {
	WorkspaceID string
	UserID      string
	Role        string
	CreatedAt   time.Time
}

type WorkspaceQuota added in v0.9.0

type WorkspaceQuota struct {
	WorkspaceID      string
	MaxSandboxes     *int
	MaxSandboxCPU    *int   // millicores
	MaxSandboxMemory *int64 // bytes
	MaxIdleTimeout   *int   // seconds
	MaxTotalCPU      *int   // millicores
	MaxTotalMemory   *int64 // bytes
	MaxDriveSize     *int64 // bytes
	UpdatedAt        time.Time
}

type WorkspaceVolume added in v0.9.0

type WorkspaceVolume struct {
	ID          string
	WorkspaceID string
	PVCName     string
	MountPath   string
	CreatedAt   time.Time
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL