sqlite

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlite holds a complete Storage over SQLite (§3.2): every table the platform needs, created on construction. A working agent platform with no infrastructure to stand up first.

The package never imports a driver itself (§3.4). Hand it any *sql.DB opened with a SQLite driver, or call Open, which picks whichever database/sql driver the process has registered ("sqlite" from modernc.org/sqlite, or "sqlite3" from github.com/mattn/go-sqlite3).

The schema is the same one the TypeScript SqliteStorage creates, so both runtimes can read one file.

Index

Constants

This section is empty.

Variables

View Source
var KvSchema = []string{
	`CREATE TABLE IF NOT EXISTS kv (
	  key TEXT PRIMARY KEY, value TEXT NOT NULL, expiresAt INTEGER)`,
}

KvSchema is the DDL for the Kv table.

View Source
var Schema = []string{
	`CREATE TABLE IF NOT EXISTS threads (
	  id TEXT PRIMARY KEY, title TEXT NOT NULL DEFAULT 'New Thread',
	  state TEXT NOT NULL DEFAULT 'IDLE', model TEXT NOT NULL DEFAULT 'gpt-4o',
	  createdAt INTEGER NOT NULL, updatedAt INTEGER NOT NULL)`,
	`CREATE TABLE IF NOT EXISTS messages (
	  id TEXT PRIMARY KEY, threadId TEXT NOT NULL, agentId TEXT,
	  role TEXT NOT NULL, content TEXT NOT NULL, createdAt INTEGER NOT NULL,
	  seq INTEGER NOT NULL)`,
	`CREATE INDEX IF NOT EXISTS messages_thread ON messages(threadId, seq)`,
	`CREATE TABLE IF NOT EXISTS events (
	  id TEXT PRIMARY KEY, threadId TEXT NOT NULL, seq INTEGER NOT NULL,
	  type TEXT NOT NULL, payload TEXT, createdAt INTEGER NOT NULL)`,
	`CREATE INDEX IF NOT EXISTS events_thread_seq ON events(threadId, seq)`,
	`CREATE INDEX IF NOT EXISTS events_thread_type ON events(threadId, type, seq)`,

	`CREATE TABLE IF NOT EXISTS usage (
	  id TEXT PRIMARY KEY, threadId TEXT NOT NULL, runId TEXT, agentId TEXT, agentName TEXT,
	  kind TEXT NOT NULL DEFAULT 'step', step INTEGER NOT NULL DEFAULT 0,
	  model TEXT, modelId TEXT,
	  inputTokens INTEGER NOT NULL, cachedInputTokens INTEGER NOT NULL,
	  cacheWriteInputTokens INTEGER NOT NULL DEFAULT 0,
	  outputTokens INTEGER NOT NULL, reasoningTokens INTEGER NOT NULL DEFAULT 0,
	  totalTokens INTEGER NOT NULL,
	  outcome TEXT NOT NULL DEFAULT 'finished', estimated INTEGER NOT NULL DEFAULT 0,
	  providerMetadata TEXT,
	  costMicros INTEGER, costCurrency TEXT, costSource TEXT,
	  createdAt INTEGER NOT NULL)`,
	`CREATE INDEX IF NOT EXISTS usage_thread ON usage(threadId)`,
	`CREATE INDEX IF NOT EXISTS usage_run ON usage(runId, createdAt)`,
}

Schema is the DDL, split per statement.

Functions

func Open

func Open(filename string) (*sql.DB, error)

Open opens a SQLite file with whichever driver the process has. Pass ":memory:" for a database that lives only as long as the process.

func Tune added in v0.4.0

func Tune(db *sql.DB) error

addMissing adds any of cols the table does not have yet. Tune sets a file up for more than one process (§3.4): WAL lets readers run beside the one writer, and busy_timeout makes a writer wait up to five seconds for the lock rather than fail at once with SQLITE_BUSY. Open calls it; call it yourself on a handle you opened some other way. An in-memory database keeps its own journal mode, which is fine.

busy_timeout goes FIRST: switching a new file to WAL takes a lock too. And SQLite does not always wait on the busy timeout for that switch: with several processes opening one fresh file at once it can give up at once with "database is locked". So the switch is tried again for up to five seconds, like any other write. The TS runtime's tuneSqlite does the same.

Types

type Kv

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

Kv is a Kv over SQLite, for a local setup whose storage is durable too.

The event sequence counter (§3.4) lives in the Kv. With an in-memory Kv beside a SQLite Storage, a restart resets the counter while the log keeps its numbers, so new events reuse old sequence numbers and every client's cursor drops them. Keeping the Kv in the same file as the log removes that failure entirely: one file, one lifetime.

Expiry is enforced on read. Incr and SET NX are single statements, so concurrent callers never collide on a counter or a lock.

func NewKv

func NewKv(db *sql.DB) (*Kv, error)

NewKv creates the table if missing and returns the Kv.

func (*Kv) Del

func (k *Kv) Del(ctx context.Context, key string) error

func (*Kv) DelIfValue added in v0.3.0

func (k *Kv) DelIfValue(ctx context.Context, key, expected string) (bool, error)

DelIfValue deletes only while the row still holds expected.

func (*Kv) Get

func (k *Kv) Get(ctx context.Context, key string) (string, bool, error)

func (*Kv) Incr

func (k *Kv) Incr(ctx context.Context, key string) (int64, error)

func (*Kv) IncrWithExpiry added in v0.3.0

func (k *Kv) IncrWithExpiry(ctx context.Context, key string, ttl time.Duration) (int64, error)

IncrWithExpiry is Incr that stamps a new (or expired) counter with an expiry; a live counter keeps the one it has.

func (*Kv) Set

func (k *Kv) Set(ctx context.Context, key, value string, opts ports.SetOptions) (bool, error)

func (*Kv) SetIfValue added in v0.3.0

func (k *Kv) SetIfValue(ctx context.Context, key, expected, value string, ttl time.Duration) (bool, error)

SetIfValue writes only while the row still holds expected and is live.

type RunStreams added in v0.4.0

type RunStreams struct{ *sqlstreams.Streams }

RunStreams is RunStreams over SQLite: the run_streams and run_stream_events tables, the same ones the TS adapter uses. Readers in this process wake on its own appends; one in another process sees them on its next poll.

func NewRunStreams added in v0.4.0

func NewRunStreams(ctx context.Context, db *sql.DB, poll time.Duration) (*RunStreams, error)

NewRunStreams creates the tables if they are missing. A zero poll means 250ms.

type Storage

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

Storage is a Storage over SQLite.

func New

func New(db *sql.DB) (*Storage, error)

New creates the tables if missing and returns the storage.

func (*Storage) Events

func (s *Storage) Events() ports.EventStore

func (*Storage) Messages

func (s *Storage) Messages() ports.MessageStore

func (*Storage) Threads

func (s *Storage) Threads() ports.ThreadStore

func (*Storage) Usage

func (s *Storage) Usage() ports.UsageStore

Jump to

Keyboard shortcuts

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