sqlite

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sqlite implements the reference SQLite store for the open-source core: the request log, budget ledger, settings, and a static model-price book. It deliberately migrates only the core tables — request_logs, model_prices, settings — never any enterprise table (tenants, api_keys, users, incidents, alert configs). Those belong to the enterprise module, which keeps its own handle and adapts to the core seams.

GORM and the pure-Go glebarez/sqlite driver are confined to this package (and its tests) per the core dependency rule.

Index

Constants

View Source
const (
	PriceSourceSeed   = "seed"
	PriceSourceManual = "manual"
	PriceSourceSync   = "sync"
)

Source values for the model_prices.source column.

View Source
const DefaultLoopThreshold = 2

DefaultLoopThreshold mirrors the detector's repeat-threshold default.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB wraps the gorm handle and serializes access. Writes and reads are serialized through a single mutex, and the SQLite handle is pinned to one connection so the connection-scoped WAL pragmas apply to every query.

func Open

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

Open opens (creating if needed) the SQLite database at path, applies WAL pragmas, and migrates only the core tables. It creates the parent directory if it does not exist.

func (*DB) Close

func (d *DB) Close() error

Close closes the underlying database. Idempotent.

func (*DB) GetSettings

func (d *DB) GetSettings(ctx context.Context) (proxy.SettingsRow, error)

GetSettings implements proxy.SettingsStore, seeding the defaults on first access so the table always holds exactly one row.

func (*DB) ListModelPrices

func (d *DB) ListModelPrices(ctx context.Context) ([]PriceRow, error)

ListModelPrices returns every price row, ordered by model case-insensitively.

func (*DB) LogRequest

func (d *DB) LogRequest(ctx context.Context, r proxy.RequestRecord) error

LogRequest implements proxy.RequestLogStore by persisting one request row.

func (*DB) SeedModelPrices

func (d *DB) SeedModelPrices(ctx context.Context, rows []PriceRow) error

SeedModelPrices inserts the static price rows with INSERT OR IGNORE semantics on the model key: an existing row is never overwritten. It is the core equivalent of the enterprise boot seeding, fed from the static config/pricing.json table.

func (*DB) SumActualCostForKeySince

func (d *DB) SumActualCostForKeySince(ctx context.Context, keyID string, since int64) (int64, error)

SumActualCostForKeySince implements proxy.BudgetLedger: the sum of actual cost for one caller's requests at or after since. The empty keyID selects the anonymous/legacy bucket.

func (*DB) SumActualCostSince

func (d *DB) SumActualCostSince(ctx context.Context, since int64) (int64, error)

SumActualCostSince implements proxy.BudgetLedger: the sum of actual (non-loop-blocked) cost at or after since, across every caller.

func (*DB) UpdateSettings

func (d *DB) UpdateSettings(ctx context.Context, s proxy.SettingsRow) error

UpdateSettings implements proxy.SettingsStore by upserting the single row onto id = 1.

type PriceRow

type PriceRow struct {
	Model          string
	Provider       string
	InputMicroUSD  int64 // µUSD per 1M input (prompt) tokens
	OutputMicroUSD int64 // µUSD per 1M output (completion) tokens
	Source         string
}

PriceRow is the public form of one model_prices row, for seeding from the static config/pricing.json table and for listing.

Jump to

Keyboard shortcuts

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