storage

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package storage provides a SQLite persistence layer using modernc.org/sqlite (pure Go, no CGO). The DB type wraps sql.DB and opens the database with WAL journal mode and a 5-second busy timeout. Schema migrations run automatically on first open, creating the execution_logs, chat_sessions, and chat_messages tables with appropriate indexes. CRUD operations cover execution log recording and querying, chat session lifecycle management, and chat message logging.

File models.go defines the data models for the storage layer: ExecutionLog (job execution record with token usage, cost, and status), UsageSummary (aggregated run statistics), ChatSession (Telegram user-to-Claude session mapping with model, effort, and working directory), and ChatMessage (logged chat exchange for terminal echo and log viewing).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatMessage

type ChatMessage struct {
	ID        int64     `json:"id"`
	SessionID string    `json:"session_id"`
	Role      string    `json:"role"` // "user" or "assistant"
	Content   string    `json:"content"`
	CostUSD   float64   `json:"cost_usd"`
	Tokens    int       `json:"tokens"`
	CreatedAt time.Time `json:"created_at"`
}

ChatMessage represents a logged chat message for visibility.

type ChatSession

type ChatSession struct {
	ID         string    `json:"id"`      // UUID, also used as claude --session-id
	UserID     int64     `json:"user_id"` // Telegram user ID
	ChatID     int64     `json:"chat_id"` // Telegram chat ID
	Model      string    `json:"model"`   // sonnet, opus, haiku
	Effort     string    `json:"effort"`  // low, medium, high, max
	WorkingDir string    `json:"working_dir"`
	Active     bool      `json:"active"`
	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
}

ChatSession represents a chat session between a user and Claude.

type DB

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

DB wraps the SQLite database connection.

func Open

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

Open opens or creates the SQLite database at the given path.

func (*DB) AddChatLog

func (db *DB) AddChatLog(sessionID, role, content string, costUSD float64, tokens int) error

AddChatLog inserts a chat message log entry.

func (*DB) Close

func (db *DB) Close() error

Close closes the database connection.

func (*DB) CreateSession

func (db *DB) CreateSession(session *ChatSession) error

CreateSession inserts a new chat session.

func (*DB) DeactivateStaleSessions

func (db *DB) DeactivateStaleSessions(maxIdle time.Duration) (int64, error)

DeactivateStaleSessions deactivates sessions idle for more than the given duration.

func (*DB) DeactivateUserSessions

func (db *DB) DeactivateUserSessions(userID int64) error

DeactivateUserSessions marks all sessions for a user as inactive.

func (*DB) GetActiveSession

func (db *DB) GetActiveSession(userID int64) (*ChatSession, error)

GetActiveSession returns the active chat session for a user, or nil if none.

func (*DB) GetChatLogs

func (db *DB) GetChatLogs(sessionID string, limit int) ([]ChatMessage, error)

GetChatLogs returns chat messages for a session, ordered by creation time.

func (*DB) GetLogsByJobName

func (db *DB) GetLogsByJobName(jobName string, limit int) ([]ExecutionLog, error)

GetLogsByJobName returns execution logs for a specific job, most recent first.

func (*DB) GetRecentLogs

func (db *DB) GetRecentLogs(limit int) ([]ExecutionLog, error)

GetRecentLogs returns recent execution logs across all jobs.

func (*DB) GetTotalUsage

func (db *DB) GetTotalUsage() (*UsageSummary, error)

GetTotalUsage returns aggregated usage stats across all jobs.

func (*DB) GetUsageByJobName

func (db *DB) GetUsageByJobName(jobName string) (*UsageSummary, error)

GetUsageByJobName returns aggregated usage stats for a specific job.

func (*DB) InsertLog

func (db *DB) InsertLog(entry *ExecutionLog) (int64, error)

InsertLog creates a new execution log entry and returns its ID.

func (*DB) TouchSession

func (db *DB) TouchSession(sessionID string) error

TouchSession updates the session's updated_at timestamp.

func (*DB) UpdateLog

func (db *DB) UpdateLog(id int64, finishedAt time.Time, exitCode int, stdoutPath, stderrPath string, costUSD float64, inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens int, status, errorMsg string) error

UpdateLog updates an existing execution log entry with results and usage data.

func (*DB) UpdateSessionEffort

func (db *DB) UpdateSessionEffort(sessionID, effort string) error

UpdateSessionEffort updates the effort for a session.

func (*DB) UpdateSessionModel

func (db *DB) UpdateSessionModel(sessionID, model string) error

UpdateSessionModel updates the model for a session.

type ExecutionLog

type ExecutionLog struct {
	ID                  int64      `json:"id"`
	JobID               string     `json:"job_id"`
	JobName             string     `json:"job_name"`
	StartedAt           time.Time  `json:"started_at"`
	FinishedAt          *time.Time `json:"finished_at,omitempty"`
	ExitCode            *int       `json:"exit_code,omitempty"`
	StdoutPath          string     `json:"stdout_path"`
	StderrPath          string     `json:"stderr_path"`
	CostUSD             *float64   `json:"cost_usd,omitempty"`
	InputTokens         *int       `json:"input_tokens,omitempty"`
	OutputTokens        *int       `json:"output_tokens,omitempty"`
	CacheReadTokens     *int       `json:"cache_read_tokens,omitempty"`
	CacheCreationTokens *int       `json:"cache_creation_tokens,omitempty"`
	Status              string     `json:"status"`
	TriggerType         string     `json:"trigger_type"`
	ErrorMsg            string     `json:"error_msg,omitempty"`
}

ExecutionLog represents a single job execution record.

type UsageSummary

type UsageSummary struct {
	TotalRuns          int     `json:"total_runs"`
	TotalCostUSD       float64 `json:"total_cost_usd"`
	TotalInputTokens   int     `json:"total_input_tokens"`
	TotalOutputTokens  int     `json:"total_output_tokens"`
	TotalCacheRead     int     `json:"total_cache_read_tokens"`
	TotalCacheCreation int     `json:"total_cache_creation_tokens"`
}

UsageSummary holds aggregated usage stats.

Jump to

Keyboard shortcuts

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