db

package
v1.12.0 Latest Latest
Warning

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

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

Documentation

Overview

Package db provides database access for sessions, messages, and files. It supports both SQLite and MySQL providers with automatic migrations.

Index

Constants

This section is empty.

Variables

Functions

func Connect

func Connect(ctx context.Context) (*sql.DB, error)

Connect creates a new database connection based on the current configuration. It sets up the provider, connects to the database, runs migrations, and backfills project IDs for existing sessions.

func GetProjectID

func GetProjectID(workingDir string) string

GetProjectID determines the project ID for the given working directory. It first attempts to use the Git repository origin URL, falling back to the directory name if Git is not available or configured. Results are cached per working directory.

Types

type CreateFileParams

type CreateFileParams struct {
	ID        string `json:"id"`
	SessionID string `json:"session_id"`
	Path      string `json:"path"`
	Content   string `json:"content"`
	Version   string `json:"version"`
}

type CreateFlowStateParams added in v1.7.0

type CreateFlowStateParams struct {
	SessionID      string         `json:"session_id"`
	RootSessionID  sql.NullString `json:"root_session_id"`
	FlowID         string         `json:"flow_id"`
	StepID         string         `json:"step_id"`
	Status         string         `json:"status"`
	Args           sql.NullString `json:"args"`
	Output         sql.NullString `json:"output"`
	IsStructOutput bool           `json:"is_struct_output"`
}

type CreateMessageParams

type CreateMessageParams struct {
	ID        string         `json:"id"`
	SessionID string         `json:"session_id"`
	Role      string         `json:"role"`
	Parts     string         `json:"parts"`
	Model     sql.NullString `json:"model"`
	Seq       sql.NullInt64  `json:"seq"`
}

type CreateSessionParams

type CreateSessionParams struct {
	ID               string         `json:"id"`
	ProjectID        sql.NullString `json:"project_id"`
	ParentSessionID  sql.NullString `json:"parent_session_id"`
	RootSessionID    sql.NullString `json:"root_session_id"`
	Title            string         `json:"title"`
	MessageCount     int64          `json:"message_count"`
	PromptTokens     int64          `json:"prompt_tokens"`
	CompletionTokens int64          `json:"completion_tokens"`
	Cost             float64        `json:"cost"`
}

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

type DeleteFlowStatesByRootSessionParams added in v1.11.0

type DeleteFlowStatesByRootSessionParams struct {
	RootSessionID sql.NullString `json:"root_session_id"`
	Column2       interface{}    `json:"column_2"`
}

type File

type File struct {
	ID        string `json:"id"`
	SessionID string `json:"session_id"`
	Path      string `json:"path"`
	Content   string `json:"content"`
	Version   string `json:"version"`
	CreatedAt int64  `json:"created_at"`
	UpdatedAt int64  `json:"updated_at"`
}

type FlowState added in v1.7.0

type FlowState struct {
	SessionID      string         `json:"session_id"`
	RootSessionID  sql.NullString `json:"root_session_id"`
	FlowID         string         `json:"flow_id"`
	StepID         string         `json:"step_id"`
	Status         string         `json:"status"`
	Args           sql.NullString `json:"args"`
	Output         sql.NullString `json:"output"`
	IsStructOutput bool           `json:"is_struct_output"`
	CreatedAt      int64          `json:"created_at"`
	UpdatedAt      int64          `json:"updated_at"`
}

type GetFileByPathAndSessionParams

type GetFileByPathAndSessionParams struct {
	Path      string `json:"path"`
	SessionID string `json:"session_id"`
}

type ListFlowStatesByRootSessionParams added in v1.11.0

type ListFlowStatesByRootSessionParams struct {
	RootSessionID sql.NullString `json:"root_session_id"`
	Column2       interface{}    `json:"column_2"`
}

type Message

type Message struct {
	ID         string         `json:"id"`
	SessionID  string         `json:"session_id"`
	Role       string         `json:"role"`
	Parts      string         `json:"parts"`
	Model      sql.NullString `json:"model"`
	CreatedAt  int64          `json:"created_at"`
	UpdatedAt  int64          `json:"updated_at"`
	FinishedAt sql.NullInt64  `json:"finished_at"`
	Seq        sql.NullInt64  `json:"seq"`
}

type MySQLProvider

type MySQLProvider struct {
	// contains filtered or unexported fields
}

MySQLProvider implements the Provider interface for MySQL databases.

func NewMySQLProvider

func NewMySQLProvider(cfg config.MySQLConfig) *MySQLProvider

NewMySQLProvider creates a new MySQL provider instance.

func (*MySQLProvider) Connect

func (p *MySQLProvider) Connect() (*sql.DB, error)

Connect establishes a connection to the MySQL database.

func (*MySQLProvider) Dialect

func (p *MySQLProvider) Dialect() string

Dialect returns the SQL dialect name for migrations.

func (*MySQLProvider) Type

func (p *MySQLProvider) Type() config.ProviderType

Type returns the provider type.

type MySQLQuerier

type MySQLQuerier struct {
	*Queries
	// contains filtered or unexported fields
}

MySQLQuerier wraps the MySQL-generated queries and implements the Querier interface

func NewMySQLQuerier

func NewMySQLQuerier(database *sql.DB) *MySQLQuerier

NewMySQLQuerier creates a new MySQL querier wrapper

func (*MySQLQuerier) CreateFile

func (q *MySQLQuerier) CreateFile(ctx context.Context, arg CreateFileParams) (File, error)

CreateFile creates a file and returns it

func (*MySQLQuerier) CreateFlowState added in v1.7.0

func (q *MySQLQuerier) CreateFlowState(ctx context.Context, arg CreateFlowStateParams) (FlowState, error)

CreateFlowState creates a flow state and returns it

func (*MySQLQuerier) CreateMessage

func (q *MySQLQuerier) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)

CreateMessage creates a message and returns it

func (*MySQLQuerier) CreateSession

func (q *MySQLQuerier) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)

CreateSession creates a session and returns it

func (*MySQLQuerier) DeleteFile

func (q *MySQLQuerier) DeleteFile(ctx context.Context, id string) error

DeleteFile deletes a file

func (*MySQLQuerier) DeleteFlowStatesByRootSession added in v1.7.0

func (q *MySQLQuerier) DeleteFlowStatesByRootSession(ctx context.Context, arg DeleteFlowStatesByRootSessionParams) error

DeleteFlowStatesByRootSession deletes all flow states for a root session

func (*MySQLQuerier) DeleteMessage

func (q *MySQLQuerier) DeleteMessage(ctx context.Context, id string) error

DeleteMessage deletes a message

func (*MySQLQuerier) DeleteSession

func (q *MySQLQuerier) DeleteSession(ctx context.Context, id string) error

DeleteSession deletes a session

func (*MySQLQuerier) DeleteSessionFiles

func (q *MySQLQuerier) DeleteSessionFiles(ctx context.Context, sessionID string) error

DeleteSessionFiles deletes all files for a session

func (*MySQLQuerier) DeleteSessionMessages

func (q *MySQLQuerier) DeleteSessionMessages(ctx context.Context, sessionID string) error

DeleteSessionMessages deletes all messages for a session

func (*MySQLQuerier) GetFile

func (q *MySQLQuerier) GetFile(ctx context.Context, id string) (File, error)

GetFile gets a file by ID

func (*MySQLQuerier) GetFileByPathAndSession

func (q *MySQLQuerier) GetFileByPathAndSession(ctx context.Context, arg GetFileByPathAndSessionParams) (File, error)

GetFileByPathAndSession gets a file by path and session

func (*MySQLQuerier) GetFlowState added in v1.7.0

func (q *MySQLQuerier) GetFlowState(ctx context.Context, sessionID string) (FlowState, error)

GetFlowState gets a flow state by session ID

func (*MySQLQuerier) GetMaxSeqBySession added in v1.10.0

func (q *MySQLQuerier) GetMaxSeqBySession(ctx context.Context, sessionID string) (int64, error)

GetMaxSeqBySession returns the maximum seq value for a session

func (*MySQLQuerier) GetMessage

func (q *MySQLQuerier) GetMessage(ctx context.Context, id string) (Message, error)

GetMessage gets a message by ID

func (*MySQLQuerier) GetSessionByID

func (q *MySQLQuerier) GetSessionByID(ctx context.Context, id string) (Session, error)

GetSessionByID gets a session by ID

func (*MySQLQuerier) ListChildSessions

func (q *MySQLQuerier) ListChildSessions(ctx context.Context, rootSessionID sql.NullString) ([]Session, error)

ListChildSessions lists child sessions by root session ID

func (*MySQLQuerier) ListFilesByPath

func (q *MySQLQuerier) ListFilesByPath(ctx context.Context, path string) ([]File, error)

ListFilesByPath lists files by path

func (*MySQLQuerier) ListFilesBySession

func (q *MySQLQuerier) ListFilesBySession(ctx context.Context, sessionID string) ([]File, error)

ListFilesBySession lists files by session

func (*MySQLQuerier) ListFilesBySessionTree

func (q *MySQLQuerier) ListFilesBySessionTree(ctx context.Context, rootSessionID sql.NullString) ([]File, error)

ListFilesBySessionTree lists files by session tree

func (*MySQLQuerier) ListFlowStatesByFlowID added in v1.7.0

func (q *MySQLQuerier) ListFlowStatesByFlowID(ctx context.Context, flowID string) ([]FlowState, error)

ListFlowStatesByFlowID lists flow states by flow ID

func (*MySQLQuerier) ListFlowStatesByRootSession added in v1.7.0

func (q *MySQLQuerier) ListFlowStatesByRootSession(ctx context.Context, arg ListFlowStatesByRootSessionParams) ([]FlowState, error)

ListFlowStatesByRootSession lists flow states by root session ID

func (*MySQLQuerier) ListLatestSessionFiles

func (q *MySQLQuerier) ListLatestSessionFiles(ctx context.Context, sessionID string) ([]File, error)

ListLatestSessionFiles lists the latest files for a session

func (*MySQLQuerier) ListLatestSessionTreeFiles

func (q *MySQLQuerier) ListLatestSessionTreeFiles(ctx context.Context, rootSessionID sql.NullString) ([]File, error)

ListLatestSessionTreeFiles lists the latest files for a session tree

func (*MySQLQuerier) ListMessagesBySession

func (q *MySQLQuerier) ListMessagesBySession(ctx context.Context, sessionID string) ([]Message, error)

ListMessagesBySession lists messages by session

func (*MySQLQuerier) ListSessions

func (q *MySQLQuerier) ListSessions(ctx context.Context, projectID sql.NullString) ([]Session, error)

ListSessions lists sessions

func (*MySQLQuerier) UpdateFile

func (q *MySQLQuerier) UpdateFile(ctx context.Context, arg UpdateFileParams) (File, error)

UpdateFile updates a file and returns it

func (*MySQLQuerier) UpdateFlowState added in v1.7.0

func (q *MySQLQuerier) UpdateFlowState(ctx context.Context, arg UpdateFlowStateParams) (FlowState, error)

UpdateFlowState updates a flow state and returns it

func (*MySQLQuerier) UpdateMessage

func (q *MySQLQuerier) UpdateMessage(ctx context.Context, arg UpdateMessageParams) error

UpdateMessage updates a message

func (*MySQLQuerier) UpdateSession

func (q *MySQLQuerier) UpdateSession(ctx context.Context, arg UpdateSessionParams) (Session, error)

UpdateSession updates a session and returns it

func (*MySQLQuerier) WithTx

func (q *MySQLQuerier) WithTx(tx *sql.Tx) *MySQLQuerier

WithTx creates a new MySQLQuerier with a transaction

type Provider

type Provider interface {
	// Connect establishes a connection to the database and returns a *sql.DB instance.
	// It should handle provider-specific connection setup, including:
	// - Connection string/DSN building
	// - Connection pooling configuration
	// - Provider-specific optimizations (pragmas, settings, etc.)
	// - Connection verification (ping)
	Connect() (*sql.DB, error)

	// Type returns the provider type (sqlite or mysql).
	Type() config.ProviderType

	// Dialect returns the SQL dialect name for migration purposes.
	Dialect() string
}

Provider defines the interface for database connection providers.

func NewProvider

func NewProvider(cfg *config.Config) (Provider, error)

NewProvider creates a new database provider based on the configuration.

type Querier

type Querier interface {
	CreateFile(ctx context.Context, arg CreateFileParams) (File, error)
	CreateFlowState(ctx context.Context, arg CreateFlowStateParams) (FlowState, error)
	CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)
	CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
	DeleteFile(ctx context.Context, id string) error
	DeleteFlowStatesByRootSession(ctx context.Context, arg DeleteFlowStatesByRootSessionParams) error
	DeleteMessage(ctx context.Context, id string) error
	DeleteSession(ctx context.Context, id string) error
	DeleteSessionFiles(ctx context.Context, sessionID string) error
	DeleteSessionMessages(ctx context.Context, sessionID string) error
	GetFile(ctx context.Context, id string) (File, error)
	GetFileByPathAndSession(ctx context.Context, arg GetFileByPathAndSessionParams) (File, error)
	GetFlowState(ctx context.Context, sessionID string) (FlowState, error)
	GetMaxSeqBySession(ctx context.Context, sessionID string) (int64, error)
	GetMessage(ctx context.Context, id string) (Message, error)
	GetSessionByID(ctx context.Context, id string) (Session, error)
	ListChildSessions(ctx context.Context, rootSessionID sql.NullString) ([]Session, error)
	ListFilesByPath(ctx context.Context, path string) ([]File, error)
	ListFilesBySession(ctx context.Context, sessionID string) ([]File, error)
	ListFilesBySessionTree(ctx context.Context, rootSessionID sql.NullString) ([]File, error)
	ListFlowStatesByFlowID(ctx context.Context, flowID string) ([]FlowState, error)
	ListFlowStatesByRootSession(ctx context.Context, arg ListFlowStatesByRootSessionParams) ([]FlowState, error)
	ListLatestSessionFiles(ctx context.Context, sessionID string) ([]File, error)
	ListLatestSessionTreeFiles(ctx context.Context, rootSessionID sql.NullString) ([]File, error)
	ListMessagesBySession(ctx context.Context, sessionID string) ([]Message, error)
	ListSessions(ctx context.Context, projectID sql.NullString) ([]Session, error)
	UpdateFile(ctx context.Context, arg UpdateFileParams) (File, error)
	UpdateFlowState(ctx context.Context, arg UpdateFlowStateParams) (FlowState, error)
	UpdateMessage(ctx context.Context, arg UpdateMessageParams) error
	UpdateSession(ctx context.Context, arg UpdateSessionParams) (Session, error)
}

type QuerierWithTx

type QuerierWithTx interface {
	Querier
	WithTx(tx *sql.Tx) QuerierWithTx
}

QuerierWithTx extends the Querier interface with transaction support

func NewQuerier

func NewQuerier(db *sql.DB) QuerierWithTx

NewQuerier creates a new Querier based on the configured provider type

type Queries

type Queries struct {
	// contains filtered or unexported fields
}

func New

func New(db DBTX) *Queries

func Prepare

func Prepare(ctx context.Context, db DBTX) (*Queries, error)

func (*Queries) Close

func (q *Queries) Close() error

func (*Queries) CreateFile

func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, error)

func (*Queries) CreateFlowState added in v1.7.0

func (q *Queries) CreateFlowState(ctx context.Context, arg CreateFlowStateParams) (FlowState, error)

func (*Queries) CreateMessage

func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)

func (*Queries) CreateSession

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)

func (*Queries) DeleteFile

func (q *Queries) DeleteFile(ctx context.Context, id string) error

func (*Queries) DeleteFlowStatesByRootSession added in v1.7.0

func (q *Queries) DeleteFlowStatesByRootSession(ctx context.Context, arg DeleteFlowStatesByRootSessionParams) error

func (*Queries) DeleteMessage

func (q *Queries) DeleteMessage(ctx context.Context, id string) error

func (*Queries) DeleteSession

func (q *Queries) DeleteSession(ctx context.Context, id string) error

func (*Queries) DeleteSessionFiles

func (q *Queries) DeleteSessionFiles(ctx context.Context, sessionID string) error

func (*Queries) DeleteSessionMessages

func (q *Queries) DeleteSessionMessages(ctx context.Context, sessionID string) error

func (*Queries) GetFile

func (q *Queries) GetFile(ctx context.Context, id string) (File, error)

func (*Queries) GetFileByPathAndSession

func (q *Queries) GetFileByPathAndSession(ctx context.Context, arg GetFileByPathAndSessionParams) (File, error)

func (*Queries) GetFlowState added in v1.7.0

func (q *Queries) GetFlowState(ctx context.Context, sessionID string) (FlowState, error)

func (*Queries) GetMaxSeqBySession added in v1.10.0

func (q *Queries) GetMaxSeqBySession(ctx context.Context, sessionID string) (int64, error)

func (*Queries) GetMessage

func (q *Queries) GetMessage(ctx context.Context, id string) (Message, error)

func (*Queries) GetSessionByID

func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error)

func (*Queries) ListChildSessions

func (q *Queries) ListChildSessions(ctx context.Context, rootSessionID sql.NullString) ([]Session, error)

func (*Queries) ListFilesByPath

func (q *Queries) ListFilesByPath(ctx context.Context, path string) ([]File, error)

func (*Queries) ListFilesBySession

func (q *Queries) ListFilesBySession(ctx context.Context, sessionID string) ([]File, error)

func (*Queries) ListFilesBySessionTree

func (q *Queries) ListFilesBySessionTree(ctx context.Context, rootSessionID sql.NullString) ([]File, error)

func (*Queries) ListFlowStatesByFlowID added in v1.7.0

func (q *Queries) ListFlowStatesByFlowID(ctx context.Context, flowID string) ([]FlowState, error)

func (*Queries) ListFlowStatesByRootSession added in v1.7.0

func (q *Queries) ListFlowStatesByRootSession(ctx context.Context, arg ListFlowStatesByRootSessionParams) ([]FlowState, error)

func (*Queries) ListLatestSessionFiles

func (q *Queries) ListLatestSessionFiles(ctx context.Context, sessionID string) ([]File, error)

func (*Queries) ListLatestSessionTreeFiles

func (q *Queries) ListLatestSessionTreeFiles(ctx context.Context, rootSessionID sql.NullString) ([]File, error)

func (*Queries) ListMessagesBySession

func (q *Queries) ListMessagesBySession(ctx context.Context, sessionID string) ([]Message, error)

func (*Queries) ListSessions

func (q *Queries) ListSessions(ctx context.Context, projectID sql.NullString) ([]Session, error)

func (*Queries) UpdateFile

func (q *Queries) UpdateFile(ctx context.Context, arg UpdateFileParams) (File, error)

func (*Queries) UpdateFlowState added in v1.7.0

func (q *Queries) UpdateFlowState(ctx context.Context, arg UpdateFlowStateParams) (FlowState, error)

func (*Queries) UpdateMessage

func (q *Queries) UpdateMessage(ctx context.Context, arg UpdateMessageParams) error

func (*Queries) UpdateSession

func (q *Queries) UpdateSession(ctx context.Context, arg UpdateSessionParams) (Session, error)

func (*Queries) WithTx

func (q *Queries) WithTx(tx *sql.Tx) *Queries

type SQLiteProvider

type SQLiteProvider struct {
	// contains filtered or unexported fields
}

SQLiteProvider implements the Provider interface for SQLite databases.

func NewSQLiteProvider

func NewSQLiteProvider(dataDir string) *SQLiteProvider

NewSQLiteProvider creates a new SQLite provider instance.

func (*SQLiteProvider) Connect

func (p *SQLiteProvider) Connect() (*sql.DB, error)

Connect establishes a connection to the SQLite database.

func (*SQLiteProvider) Dialect

func (p *SQLiteProvider) Dialect() string

Dialect returns the SQL dialect name for migrations.

func (*SQLiteProvider) Type

Type returns the provider type.

type Session

type Session struct {
	ID               string         `json:"id"`
	ParentSessionID  sql.NullString `json:"parent_session_id"`
	Title            string         `json:"title"`
	MessageCount     int64          `json:"message_count"`
	PromptTokens     int64          `json:"prompt_tokens"`
	CompletionTokens int64          `json:"completion_tokens"`
	Cost             float64        `json:"cost"`
	UpdatedAt        int64          `json:"updated_at"`
	CreatedAt        int64          `json:"created_at"`
	SummaryMessageID sql.NullString `json:"summary_message_id"`
	ProjectID        sql.NullString `json:"project_id"`
	RootSessionID    sql.NullString `json:"root_session_id"`
}

type UpdateFileParams

type UpdateFileParams struct {
	Content string `json:"content"`
	Version string `json:"version"`
	ID      string `json:"id"`
}

type UpdateFlowStateParams added in v1.7.0

type UpdateFlowStateParams struct {
	Status         string         `json:"status"`
	Args           sql.NullString `json:"args"`
	Output         sql.NullString `json:"output"`
	IsStructOutput bool           `json:"is_struct_output"`
	SessionID      string         `json:"session_id"`
}

type UpdateMessageParams

type UpdateMessageParams struct {
	Parts      string        `json:"parts"`
	FinishedAt sql.NullInt64 `json:"finished_at"`
	ID         string        `json:"id"`
}

type UpdateSessionParams

type UpdateSessionParams struct {
	Title            string         `json:"title"`
	PromptTokens     int64          `json:"prompt_tokens"`
	CompletionTokens int64          `json:"completion_tokens"`
	SummaryMessageID sql.NullString `json:"summary_message_id"`
	Cost             float64        `json:"cost"`
	ID               string         `json:"id"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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