db

package
v0.27.3 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 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) BindSandboxToChannel added in v0.27.0

func (db *DB) BindSandboxToChannel(sandboxID, channelID string) error

BindSandboxToChannel binds a sandbox to a workspace IM channel. Any other sandbox previously bound to this channel is unbound first.

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) CreateIMBinding added in v0.27.0

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

CreateIMBinding inserts or updates an IM binding record. On conflict (same sandbox+provider+bot), updates user_id and bound_at.

func (*DB) CreateIMChannel added in v0.27.0

func (db *DB) CreateIMChannel(workspaceID, provider, botID, userID string) (string, error)

CreateIMChannel inserts or updates a workspace IM channel record. On conflict (same workspace+provider+bot), updates bound_at. Returns the channel ID.

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) CreateWorkspace

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

func (*DB) DeleteExpiredTokens

func (db *DB) DeleteExpiredTokens() error

func (*DB) DeleteIMBinding added in v0.27.0

func (db *DB) DeleteIMBinding(sandboxID, provider, botID string) error

DeleteIMBinding deletes an IM binding by sandbox, provider, and bot ID.

func (*DB) DeleteIMChannel added in v0.27.0

func (db *DB) DeleteIMChannel(channelID string) error

DeleteIMChannel deletes a workspace IM channel by ID.

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) GetActiveBindings added in v0.27.0

func (db *DB) GetActiveBindings(provider string) ([]*IMBinding, error)

GetActiveBindings returns all bindings with credentials for a given provider, filtered to sandboxes of type 'nanoclaw' with status 'running'.

func (*DB) GetActiveBindingsForSandbox added in v0.27.0

func (db *DB) GetActiveBindingsForSandbox(sandboxID string) ([]*IMBinding, error)

GetActiveBindingsForSandbox returns all bindings with credentials for a specific sandbox.

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) GetAllChannelMeta added in v0.27.0

func (db *DB) GetAllChannelMeta(channelID, userID string) (map[string]string, error)

GetAllChannelMeta retrieves all metadata entries for a user on a channel.

func (*DB) GetAllProviderMeta added in v0.27.0

func (db *DB) GetAllProviderMeta(sandboxID, provider, botID, userID string) (map[string]string, error)

GetAllProviderMeta retrieves all metadata entries for a user.

func (*DB) GetAllWorkspaceNamespaces

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

func (*DB) GetChannelMeta added in v0.27.0

func (db *DB) GetChannelMeta(channelID, userID, key string) (string, error)

GetChannelMeta retrieves a channel-specific metadata value.

func (*DB) GetChannelRequireMention added in v0.27.0

func (db *DB) GetChannelRequireMention(channelID string) (bool, error)

GetChannelRequireMention returns the require_mention flag for a channel.

func (*DB) GetIMChannel added in v0.27.0

func (db *DB) GetIMChannel(channelID string) (*IMChannel, error)

GetIMChannel retrieves a single workspace IM channel by ID.

func (*DB) GetIMChannelForSandbox added in v0.27.0

func (db *DB) GetIMChannelForSandbox(sandboxID string) (*IMChannel, error)

GetIMChannelForSandbox returns the IM channel bound to a sandbox, if any. Returns sql.ErrNoRows if the sandbox has no channel bound.

func (*DB) GetIMCredentials added in v0.27.0

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

GetIMCredentials retrieves bot credentials for an IM binding.

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) GetProviderMeta added in v0.27.0

func (db *DB) GetProviderMeta(sandboxID, provider, botID, userID, key string) (string, error)

GetProviderMeta retrieves a provider-specific metadata value.

func (*DB) GetSandbox

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

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) GetSandboxForChannel added in v0.27.0

func (db *DB) GetSandboxForChannel(channelID string) (sandboxID, podIP, bridgeSecret string, err error)

GetSandboxForChannel returns the running sandbox bound to a channel. Returns sql.ErrNoRows if no sandbox is bound or none is running.

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) ListAllActiveChannels added in v0.27.0

func (db *DB) ListAllActiveChannels(provider string) ([]IMChannel, error)

ListAllActiveChannels returns all IM channels with credentials for a given provider. Used by RestoreIMBridgePollers.

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) ListIMBindings added in v0.27.0

func (db *DB) ListIMBindings(sandboxID, provider string) ([]*IMBinding, error)

ListIMBindings returns all IM bindings for a sandbox, optionally filtered by provider. If provider is empty, all bindings are returned.

func (*DB) ListIMChannels added in v0.27.0

func (db *DB) ListIMChannels(workspaceID string) ([]IMChannel, error)

ListIMChannels returns all IM channels for a workspace.

func (*DB) ListIdleSandboxes

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

func (*DB) ListSandboxesByWorkspace

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

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) SaveIMChannelCredentials added in v0.27.0

func (db *DB) SaveIMChannelCredentials(channelID, botToken, baseURL string) error

SaveIMChannelCredentials stores bot credentials for a workspace IM channel.

func (*DB) SaveIMCredentials added in v0.27.0

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

SaveIMCredentials stores bot credentials for an IM binding.

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) UnbindSandboxFromChannel added in v0.27.0

func (db *DB) UnbindSandboxFromChannel(sandboxID string) error

UnbindSandboxFromChannel removes the IM channel binding from a sandbox.

func (*DB) UpdateCursor added in v0.27.0

func (db *DB) UpdateCursor(sandboxID, provider, botID, cursor string) error

UpdateCursor persists the long-poll cursor for an IM binding.

func (*DB) UpdateIMChannelCursor added in v0.27.0

func (db *DB) UpdateIMChannelCursor(channelID, cursor string) error

UpdateIMChannelCursor persists the long-poll cursor for an IM channel.

func (*DB) UpdateIMChannelSettings added in v0.27.0

func (db *DB) UpdateIMChannelSettings(channelID string, requireMention bool) error

UpdateIMChannelSettings updates channel settings.

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) UpsertChannelMeta added in v0.27.0

func (db *DB) UpsertChannelMeta(channelID, userID, key, value string) error

UpsertChannelMeta inserts or updates a channel-specific metadata entry.

func (*DB) UpsertProviderMeta added in v0.27.0

func (db *DB) UpsertProviderMeta(sandboxID, provider, botID, userID, key, value string) error

UpsertProviderMeta inserts or updates a provider-specific metadata entry.

func (*DB) ValidateToken

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

type IMBinding added in v0.27.0

type IMBinding struct {
	ID        int
	SandboxID string
	Provider  string
	BotID     string
	UserID    string
	BotToken  string
	BaseURL   string
	Cursor    string
	BoundAt   time.Time
}

IMBinding represents a row in the sandbox_im_bindings table.

type IMChannel added in v0.27.0

type IMChannel struct {
	ID             string
	WorkspaceID    string
	Provider       string
	BotID          string
	UserID         string
	BotToken       string
	BaseURL        string
	Cursor         string
	RequireMention bool
	BoundAt        time.Time
}

IMChannel represents a row in the workspace_im_channels table.

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 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