api.services.ai.store

Persistence of AI usage: every call, feedback on its results, and the AI settings. SQLite on the API’s persistent volume (shared by the api and worker containers; WAL handles their concurrent writes).

Attributes

Classes

Module Contents

api.services.ai.store.SCHEMA = Multiline-String
Show Value
"""
CREATE TABLE IF NOT EXISTS ai_calls (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    created_at TEXT NOT NULL,
    feature TEXT NOT NULL,
    provider TEXT,
    profile TEXT,
    model TEXT NOT NULL,
    served_model TEXT,
    status TEXT NOT NULL,
    stop_reason TEXT,
    input_tokens INTEGER NOT NULL DEFAULT 0,
    output_tokens INTEGER NOT NULL DEFAULT 0,
    cache_read_tokens INTEGER NOT NULL DEFAULT 0,
    cache_write_tokens INTEGER NOT NULL DEFAULT 0,
    web_searches INTEGER NOT NULL DEFAULT 0,
    images INTEGER NOT NULL DEFAULT 0,
    cost_usd REAL NOT NULL DEFAULT 0,
    duration_ms INTEGER NOT NULL DEFAULT 0,
    subject TEXT,
    request_id TEXT,
    error TEXT
);
CREATE INDEX IF NOT EXISTS ai_calls_feature_created ON ai_calls (feature, created_at);
CREATE TABLE IF NOT EXISTS ai_feedback (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    call_id INTEGER NOT NULL REFERENCES ai_calls (id),
    created_at TEXT NOT NULL,
    outcome TEXT NOT NULL,
    detail TEXT
);
CREATE INDEX IF NOT EXISTS ai_feedback_call ON ai_feedback (call_id);
CREATE TABLE IF NOT EXISTS ai_settings (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL,
    updated_at TEXT NOT NULL
);
"""
api.services.ai.store.OUTCOMES = ('accepted', 'edited', 'rejected')
class api.services.ai.store.AIStore(path: str | None = None)
path: str = ''
record_call(**values: Any) → int
calls_since(since: datetime.datetime, *, feature: str | None = None, provider: str | None = None, profile: str | None = None) → int

Calls actually made (blocked attempts excluded) since since.

cost_since(since: datetime.datetime, *, provider: str | None = None, profile: str | None = None) → float
last_rate_limit(profile: str, since: datetime.datetime) → datetime.datetime | None

When this AI model was last rate-limited (any request, any process).

recent_calls(limit: int = 50, feature: str | None = None) → list[dict[str, Any]]
usage_summary(days: int = 30) → dict[str, Any]

Per feature over the period: calls, errors, tokens, cost, and how often its results were accepted / edited / rejected.

add_feedback(call_id: int, outcome: str, detail: dict[str, Any] | None = None) → int
load_settings() → dict[str, Any] | None
save_settings(value: dict[str, Any]) → None