opencode-usage-insights 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +204 -67
- package/dist/core.d.ts +19 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +558 -154
- package/dist/index.js.map +9 -8
- package/dist/line.d.ts +15 -5
- package/dist/panel-build.d.ts +2 -0
- package/dist/panel-data.d.ts +15 -6
- package/dist/panel.d.ts +8 -0
- package/dist/pricing.d.ts +72 -0
- package/dist/recorder.d.ts +10 -0
- package/dist/stats/aggregates.d.ts +14 -3
- package/dist/stats/db.d.ts +34 -3
- package/dist/stats/maintenance.d.ts +19 -0
- package/dist/stats/queries.d.ts +50 -5
- package/dist/tui.d.ts +7 -1
- package/dist/tui.js +924 -278
- package/dist/tui.js.map +15 -14
- package/package.json +5 -3
package/dist/index.js.map
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts", "../src/stats/db.ts", "../src/stats/paths.ts", "../src/text.ts", "../src/core.ts", "../src/stats/queries.ts", "../src/recorder.ts"],
|
|
3
|
+
"sources": ["../src/index.ts", "../src/pricing.ts", "../src/stats/db.ts", "../src/stats/paths.ts", "../src/text.ts", "../src/core.ts", "../src/stats/queries.ts", "../src/recorder.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { Plugin } from \"@opencode/plugin\"\nimport type { Database } from \"bun:sqlite\"\nimport { createRecorder, type RecorderEvent } from \"./recorder\"\nimport { openStatsDb } from \"./stats/db\"\nimport { resolveDbPath } from \"./stats/paths\"\n\n/**\n * Server-side recorder. Subscribes to the session event stream and persists\n * every assistant step, prompt and lifecycle event into the stats database;\n * the TUI entry reads the same database for its live line and stats panel.\n *\n * Everything the recorder stores comes from the event stream — the provider\n * and model a step ran as, the tokens and the timing. The plugin is\n * observational: it never rewrites prompts, requests or responses, registers\n * no hook on the request path, and a failure inside the recorder is swallowed\n * so a session can never be interrupted by its own telemetry.\n */\n\nexport type Options = {\n /** Turn recording off: no event subscription, no database. */\n enabled?: boolean | undefined\n /** SQLite database path override (defaults to the per-OS state directory). */\n db?: string | undefined\n}\n\nfunction tryOpen(path: string): Database | undefined {\n try {\n return openStatsDb(path)\n } catch {\n return undefined\n }\n}\n\nexport default Plugin.define({\n id: \"usage-insights\",\n async setup(ctx) {\n const options = (ctx.options ?? {}) as Options\n if (options.enabled === false) return\n\n const db = tryOpen(resolveDbPath(options.db))\n if (db === undefined) return\n\n // The recorder's connection runs on the agent's own event path, so it waits\n // at most a second for another writer and then defers the write: a stalled\n // event handler is worse than a record that lands on the next event.\n try {\n db.exec(\"PRAGMA busy_timeout = 1000\")\n } catch {\n // A database that cannot take the pragma is still usable.\n }\n\n const recorder = createRecorder(db)\n const abort = new AbortController()\n const stream = (async () => {\n try {\n for await (const event of ctx.event.subscribe({ signal: abort.signal })) {\n try {\n recorder.handleEvent(event as RecorderEvent)\n } catch {\n // A malformed event must never break the stream.\n }\n }\n } catch {\n // Subscription closed (shutdown or transport error): nothing to do.\n }\n })()\n\n return async () => {\n abort.abort()\n await stream\n db.close()\n }\n },\n})\n",
|
|
6
|
-
"import { Database } from \"bun:sqlite\"\nimport { chmodSync, mkdirSync } from \"node:fs\"\nimport { dirname } from \"node:path\"\nimport { ensureRegularFile } from \"./paths\"\nimport { isToken, sanitizeLabel, TEXT_LIMIT } from \"../text\"\n\n/**\n * Persistent usage store. One SQLite database holds every recorded step,\n * prompt, event and session row — and nothing else: the recorder only writes\n * what a view reads.\n *\n * The database is opened in WAL mode with a busy timeout so the server\n * recorder process and the TUI reader process can share it safely. The two\n * sides open it differently on purpose:\n *\n * - `openStatsDb` writes: schema, migrations and recording (the server).\n * - `openStatsDbReadOnly` only reads (the TUI), so no view can change the file\n * and no reader pays for a schema write.\n * - `openStatsDbWrite` is a short-lived maintenance connection (prune, vacuum)\n * that gives up quickly instead of holding the recorder up.\n */\n\nexport const SCHEMA_VERSION = 9\n\n/** Longest title or path kept: a label on a line, not a document. */\nconst LABEL_LIMIT = 4 * TEXT_LIMIT\n\nconst SCHEMA = `\nCREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS sessions (\n session_id TEXT PRIMARY KEY,\n parent_id TEXT,\n directory TEXT,\n title TEXT,\n started_at INTEGER,\n updated_at INTEGER\n);\n\nCREATE TABLE IF NOT EXISTS prompts (\n session_id TEXT NOT NULL,\n message_id TEXT NOT NULL,\n created_at INTEGER,\n PRIMARY KEY (session_id, message_id)\n);\nCREATE INDEX IF NOT EXISTS prompts_created ON prompts(created_at);\n\nCREATE TABLE IF NOT EXISTS steps (\n message_id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n parent_id TEXT,\n provider_id TEXT,\n model_id TEXT,\n variant TEXT,\n input_tokens INTEGER NOT NULL DEFAULT 0,\n output_tokens INTEGER NOT NULL DEFAULT 0,\n reasoning_tokens INTEGER NOT NULL DEFAULT 0,\n cache_read INTEGER NOT NULL DEFAULT 0,\n cache_write INTEGER NOT NULL DEFAULT 0,\n cost REAL NOT NULL DEFAULT 0,\n created_at INTEGER,\n streamed_at INTEGER,\n completed_at INTEGER,\n finish TEXT,\n recorded_at INTEGER NOT NULL\n);\nCREATE INDEX IF NOT EXISTS steps_session ON steps(session_id, created_at);\nCREATE INDEX IF NOT EXISTS steps_model ON steps(provider_id, model_id, variant);\nCREATE INDEX IF NOT EXISTS steps_completed ON steps(completed_at);\n-- Every range filter and every ordering compares the step's own clock, which\n-- falls back to the recording time for a step that was already in flight when\n-- the plugin loaded. The index has to cover that expression, or every ranged\n-- view is a full table scan.\nCREATE INDEX IF NOT EXISTS steps_at ON steps(COALESCE(created_at, recorded_at));\nCREATE INDEX IF NOT EXISTS steps_session_at ON steps(session_id, COALESCE(created_at, recorded_at));\n\nCREATE TABLE IF NOT EXISTS events (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n session_id TEXT NOT NULL,\n kind TEXT NOT NULL,\n message_id TEXT,\n at INTEGER,\n data TEXT\n);\nCREATE INDEX IF NOT EXISTS events_session ON events(session_id, at);\nCREATE INDEX IF NOT EXISTS events_kind ON events(kind, at);\n`\n\n/** Tables carried by `exportNdjson` / `importNdjson`, in import-safe order. */\nexport const TABLES = [\"sessions\", \"prompts\", \"events\", \"steps\"] as const\n\nexport type TableName = (typeof TABLES)[number]\n\nexport type StepRecord = {\n messageID: string\n sessionID: string\n parentID?: string | undefined\n /** The provider the model ran through, as OpenCode configured it (`opencode-go`). */\n providerID?: string | undefined\n modelID?: string | undefined\n variant?: string | undefined\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cacheRead?: number | undefined\n cacheWrite?: number | undefined\n cost?: number | undefined\n createdAt?: number | undefined\n streamedAt?: number | undefined\n completedAt?: number | undefined\n finish?: string | undefined\n /** Clock of the recording itself, defaults to `Date.now()`. */\n recordedAt?: number | undefined\n}\n\nexport type PromptRecord = {\n sessionID: string\n messageID: string\n createdAt?: number | undefined\n}\n\nexport type EventRecord = {\n sessionID: string\n kind: string\n messageID?: string | undefined\n at?: number | undefined\n data?: unknown\n}\n\nexport type SessionRecord = {\n sessionID: string\n parentID?: string | undefined\n directory?: string | undefined\n title?: string | undefined\n startedAt?: number | undefined\n updatedAt?: number | undefined\n}\n\nfunction jsonText(value: unknown): string | null {\n if (value === undefined || value === null) return null\n if (typeof value === \"string\") return value\n try {\n return JSON.stringify(value)\n } catch {\n return null\n }\n}\n\n/**\n * Columns and tables the recorder used to write and no view ever read. They are\n * dropped from an existing database, together with the rows of the events that\n * followed the same fate, so a long-lived file shrinks instead of carrying dead\n * weight forever.\n *\n * `router` is here because the gateway that named the routing endpoint in a\n * response header (`x-opencode-endpoint-id`) stopped naming one: the identity a\n * step is grouped by is the provider the model ran through, and the endpoint a\n * gateway happened to route to is no longer observable at all.\n */\nconst REMOVED_COLUMNS = [\"provider_state\", \"upstream\", \"upstream_source\", \"upstream_model\", \"tps\", \"router\"] as const\nconst REMOVED_EVENT_KINDS = [\"execution\", \"step-failed\", \"model-selected\", \"usage-recorded\"] as const\n\nfunction dropUnused(db: Database): void {\n const objects = db.query(\"SELECT name, type FROM sqlite_master WHERE type IN ('table', 'index')\").all() as {\n name: string\n type: string\n }[]\n const tables = new Set(objects.filter((row) => row.type === \"table\").map((row) => row.name))\n const indexes = new Set(objects.filter((row) => row.type === \"index\").map((row) => row.name))\n\n if (tables.has(\"http_responses\")) db.exec(\"DROP TABLE http_responses\")\n // These indexes read a column that is about to go: SQLite refuses to drop an\n // indexed column, so the index goes first.\n if (indexes.has(\"steps_upstream\")) db.exec(\"DROP INDEX steps_upstream\")\n if (indexes.has(\"steps_router\")) db.exec(\"DROP INDEX steps_router\")\n\n const columns = new Set((db.query(\"PRAGMA table_info(steps)\").all() as { name: string }[]).map((column) => column.name))\n for (const name of REMOVED_COLUMNS) {\n if (columns.has(name)) db.exec(`ALTER TABLE steps DROP COLUMN ${name}`)\n }\n\n if (tables.has(\"events\")) {\n const kinds = REMOVED_EVENT_KINDS.map((kind) => `'${kind}'`).join(\", \")\n db.exec(`DELETE FROM events WHERE kind IN (${kinds})`)\n }\n}\n\n/**\n * Bring an existing file up to date. There is no version ladder: a column the\n * schema carries is created with the table, and a column the recorder stopped\n * writing is dropped by `dropUnused`, which is idempotent and also heals a\n * database a half-applied run left behind.\n */\nfunction migrate(db: Database): void {\n dropUnused(db)\n}\n\n/**\n * The file holds session titles, project directories and usage history, so it\n * is created for the user alone. Windows takes its access from the parent\n * directory's ACL, which is not something a plugin should rewrite.\n */\nconst POSIX = process.platform !== \"win32\"\n\nfunction hardenDirectory(path: string): void {\n if (!POSIX) return\n try {\n chmodSync(path, 0o700)\n } catch {\n // A shared or read-only directory: the database is still usable.\n }\n}\n\nfunction hardenFiles(path: string): void {\n if (!POSIX || path === \":memory:\") return\n for (const file of [path, `${path}-wal`, `${path}-shm`]) {\n try {\n chmodSync(file, 0o600)\n } catch {\n // The WAL and the shared-memory file are created on the first write and\n // removed on the last close, so one of them is often simply absent.\n }\n }\n}\n\n/** Directory for a database path, created private on POSIX. */\nfunction prepareDirectory(path: string): void {\n if (path === \":memory:\") return\n mkdirSync(dirname(path), { recursive: true, mode: 0o700 })\n hardenDirectory(dirname(path))\n}\n\n/** Open (creating when needed) the stats database and apply the schema. */\nexport function openStatsDb(path: string): Database {\n if (path !== \":memory:\") ensureRegularFile(path)\n prepareDirectory(path)\n const db = new Database(path, { create: true })\n db.exec(\"PRAGMA journal_mode = WAL\")\n db.exec(\"PRAGMA busy_timeout = 5000\")\n db.exec(\"PRAGMA synchronous = NORMAL\")\n db.exec(SCHEMA)\n migrate(db)\n db.query(\"INSERT INTO meta (key, value) VALUES ('schema_version', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value\").run(\n String(SCHEMA_VERSION),\n )\n // The schema write is what creates the WAL and its shared-memory file.\n hardenFiles(path)\n return db\n}\n\n/**\n * Reader connection for the TUI: no schema, no migration, no write. It throws\n * when the database is absent or was never written by the recorder, so the\n * caller can say so instead of showing an empty panel.\n */\nexport function openStatsDbReadOnly(path: string): Database {\n ensureRegularFile(path)\n const db = new Database(path, { readonly: true })\n try {\n db.query(\"SELECT 1 FROM steps LIMIT 1\").get()\n } catch (error) {\n db.close()\n throw error\n }\n return db\n}\n\n/**\n * Short-lived write connection for maintenance. The busy timeout is short on\n * purpose: a prune or a vacuum that cannot take the lock must fail in front of\n * the user, not hold the recorder's response path for seconds.\n */\nexport function openStatsDbWrite(path: string): Database {\n if (path !== \":memory:\") ensureRegularFile(path)\n prepareDirectory(path)\n const db = new Database(path, { create: true })\n db.exec(\"PRAGMA busy_timeout = 250\")\n db.exec(\"PRAGMA synchronous = NORMAL\")\n hardenFiles(path)\n return db\n}\n\n/**\n * Insert or refresh an assistant step. Idempotent by message ID so replayed\n * events, restarts and NDJSON imports never duplicate usage. A non-null field\n * (the provider, the model, the finish reason) survives a re-record with `NULL`\n * through COALESCE.\n *\n * The provider is the identity every view groups by, so it is validated as a\n * token before it is stored: a value that is not a provider id is recorded as\n * absent instead of becoming a grouping key.\n */\nexport function recordStep(db: Database, record: StepRecord): void {\n db.query(\n `INSERT INTO steps (\n message_id, session_id, parent_id, provider_id, model_id, variant,\n input_tokens, output_tokens, reasoning_tokens, cache_read, cache_write, cost,\n created_at, streamed_at, completed_at, finish, recorded_at\n ) VALUES (\n $messageID, $sessionID, $parentID, $providerID, $modelID, $variant,\n $input, $output, $reasoning, $cacheRead, $cacheWrite, $cost,\n $createdAt, $streamedAt, $completedAt, $finish, $recordedAt\n )\n ON CONFLICT(message_id) DO UPDATE SET\n session_id = excluded.session_id,\n parent_id = COALESCE(excluded.parent_id, steps.parent_id),\n provider_id = COALESCE(excluded.provider_id, steps.provider_id),\n model_id = COALESCE(excluded.model_id, steps.model_id),\n variant = COALESCE(excluded.variant, steps.variant),\n input_tokens = excluded.input_tokens,\n output_tokens = excluded.output_tokens,\n reasoning_tokens = excluded.reasoning_tokens,\n cache_read = excluded.cache_read,\n cache_write = excluded.cache_write,\n cost = excluded.cost,\n created_at = COALESCE(excluded.created_at, steps.created_at),\n streamed_at = COALESCE(excluded.streamed_at, steps.streamed_at),\n completed_at = COALESCE(excluded.completed_at, steps.completed_at),\n finish = COALESCE(excluded.finish, steps.finish),\n recorded_at = excluded.recorded_at`,\n ).run({\n $messageID: record.messageID,\n $sessionID: record.sessionID,\n $parentID: record.parentID ?? null,\n $providerID: record.providerID !== undefined && isToken(record.providerID) ? record.providerID : null,\n $modelID: record.modelID ?? null,\n $variant: record.variant ?? null,\n $input: record.input ?? 0,\n $output: record.output ?? 0,\n $reasoning: record.reasoning ?? 0,\n $cacheRead: record.cacheRead ?? 0,\n $cacheWrite: record.cacheWrite ?? 0,\n $cost: record.cost ?? 0,\n $createdAt: record.createdAt ?? null,\n $streamedAt: record.streamedAt ?? null,\n $completedAt: record.completedAt ?? null,\n $finish: record.finish === undefined ? null : sanitizeLabel(record.finish, 64),\n $recordedAt: record.recordedAt ?? Date.now(),\n })\n}\n\nexport function recordPrompt(db: Database, record: PromptRecord): void {\n db.query(\n `INSERT INTO prompts (session_id, message_id, created_at)\n VALUES ($sessionID, $messageID, $createdAt)\n ON CONFLICT(session_id, message_id) DO UPDATE SET created_at = COALESCE(excluded.created_at, prompts.created_at)`,\n ).run({\n $sessionID: record.sessionID,\n $messageID: record.messageID,\n $createdAt: record.createdAt ?? null,\n })\n}\n\nexport function recordEvent(db: Database, record: EventRecord): void {\n db.query(\n `INSERT INTO events (session_id, kind, message_id, at, data)\n VALUES ($sessionID, $kind, $messageID, $at, $data)`,\n ).run({\n $sessionID: record.sessionID,\n $kind: sanitizeLabel(record.kind, 64),\n $messageID: record.messageID ?? null,\n $at: record.at ?? Date.now(),\n $data: jsonText(record.data),\n })\n}\n\nexport function upsertSession(db: Database, record: SessionRecord): void {\n db.query(\n `INSERT INTO sessions (session_id, parent_id, directory, title, started_at, updated_at)\n VALUES ($sessionID, $parentID, $directory, $title, $startedAt, $updatedAt)\n ON CONFLICT(session_id) DO UPDATE SET\n parent_id = COALESCE(excluded.parent_id, sessions.parent_id),\n directory = COALESCE(excluded.directory, sessions.directory),\n title = COALESCE(excluded.title, sessions.title),\n started_at = COALESCE(sessions.started_at, excluded.started_at),\n updated_at = COALESCE(excluded.updated_at, sessions.updated_at)`,\n ).run({\n $sessionID: record.sessionID,\n $parentID: record.parentID ?? null,\n // Titles are written by a model and directories come from the host: both\n // are displayed, so neither may carry control characters into a terminal.\n $directory: record.directory === undefined ? null : sanitizeLabel(record.directory, LABEL_LIMIT),\n $title: record.title === undefined ? null : sanitizeLabel(record.title, LABEL_LIMIT),\n $startedAt: record.startedAt ?? null,\n $updatedAt: record.updatedAt ?? null,\n })\n}\n\n/** Dump every table as NDJSON (`{\"table\": \"...\", \"row\": {...}}` per line). */\nexport function exportNdjson(db: Database): string {\n const lines: string[] = []\n for (const table of TABLES) {\n const rows = db.query(`SELECT * FROM ${table}`).all() as Record<string, unknown>[]\n for (const row of rows) lines.push(JSON.stringify({ table, row }))\n }\n return lines.length > 0 ? `${lines.join(\"\\n\")}\\n` : \"\"\n}\n\n/** Column shape an import has to respect: the type and the NOT NULL rules. */\ntype ImportColumn = {\n name: string\n /** Declared affinity: `INTEGER`, `REAL`, `TEXT`, `BLOB` or empty. */\n type: string\n notNull: boolean\n primaryKey: boolean\n /** A column with a default may be left out of the import entirely. */\n hasDefault: boolean\n}\n\nfunction numericAffinity(type: string): boolean {\n const upper = type.toUpperCase()\n return upper.includes(\"INT\") || upper.includes(\"REAL\") || upper.includes(\"NUM\") || upper.includes(\"DEC\") || upper.includes(\"DOUB\")\n}\n\n/**\n * One imported value, forced into the shape of its column. Text is sanitized\n * and capped like everything else that is displayed; a number that is not a\n * finite number becomes NULL, so a crafted dump cannot store `NaN` or a\n * string where a view does arithmetic.\n */\nfunction importValue(value: unknown, column: ImportColumn): string | number | null {\n if (value === null || value === undefined) return null\n if (numericAffinity(column.type)) {\n const parsed = typeof value === \"number\" ? value : Number(value)\n return Number.isFinite(parsed) ? parsed : null\n }\n if (typeof value === \"number\") return Number.isFinite(value) ? value : null\n if (typeof value === \"boolean\") return value ? 1 : 0\n if (typeof value === \"object\") return sanitizeLabel(JSON.stringify(value) ?? \"\", LABEL_LIMIT)\n return sanitizeLabel(String(value), LABEL_LIMIT)\n}\n\n/**\n * Load an `exportNdjson` dump back in. Rows are inserted with `OR IGNORE`, so\n * importing the same dump twice is harmless. Unknown tables and rows that are\n * not objects are rejected; a row that cannot fill a required column is\n * skipped rather than aborting the rest of the import. A column the row does\n * not carry is left to its default, the same way a hand-written insert is.\n */\nexport function importNdjson(db: Database, text: string): number {\n const columns = new Map<TableName, ImportColumn[]>()\n for (const table of TABLES) {\n const info = db.query(`PRAGMA table_info(${table})`).all() as {\n name: string\n type: string\n notnull: number\n pk: number\n dflt_value: string | null\n }[]\n columns.set(\n table,\n info.map((column) => ({\n name: column.name,\n type: column.type,\n notNull: column.notnull === 1,\n primaryKey: column.pk > 0,\n hasDefault: column.dflt_value !== null,\n })),\n )\n }\n\n let imported = 0\n for (const line of text.split(\"\\n\")) {\n const trimmed = line.trim()\n if (!trimmed) continue\n let parsed: { table?: string; row?: Record<string, unknown> }\n try {\n parsed = JSON.parse(trimmed) as { table?: string; row?: Record<string, unknown> }\n } catch {\n continue\n }\n const table = parsed.table as TableName | undefined\n const row = parsed.row\n if (!table || !row || !(TABLES as readonly string[]).includes(table)) continue\n const shape = columns.get(table) ?? []\n if (shape.length === 0) continue\n const names: string[] = []\n const values: (string | number)[] = []\n let importable = true\n for (const column of shape) {\n const value = importValue(row[column.name], column)\n if (value !== null) {\n names.push(column.name)\n values.push(value)\n continue\n }\n // NULL for a column that cannot hold one has no default to fall back on,\n // and a NULL in a text primary key is a key the database cannot use; an\n // INTEGER PRIMARY KEY is a rowid, so leaving it out assigns one.\n const required = (column.notNull || (column.primaryKey && !numericAffinity(column.type))) && !column.hasDefault\n if (required) {\n importable = false\n break\n }\n }\n if (!importable) continue\n const placeholders = names.map(() => \"?\").join(\", \")\n db.query(`INSERT OR IGNORE INTO ${table} (${names.join(\", \")}) VALUES (${placeholders})`).run(...values)\n imported += 1\n }\n return imported\n}\n",
|
|
5
|
+
"import { Plugin } from \"@opencode/plugin\"\nimport type { Database } from \"bun:sqlite\"\nimport { costOf, priceTable, type PriceFn, type PriceTable } from \"./pricing\"\nimport { createRecorder, type RecorderEvent } from \"./recorder\"\nimport { openStatsDb } from \"./stats/db\"\nimport { resolveDbPath } from \"./stats/paths\"\n\n/**\n * Server-side recorder. Subscribes to the session event stream and persists\n * every assistant step, prompt and lifecycle event into the stats database;\n * the TUI entry reads the same database for its live line and stats panel.\n *\n * Everything the recorder stores comes from the event stream — the provider\n * and model a step ran as, the tokens and the timing. The plugin is\n * observational: it never rewrites prompts, requests or responses, registers\n * no hook on the request path, and a failure inside the recorder is swallowed\n * so a session can never be interrupted by its own telemetry.\n */\n\nexport type Options = {\n /** Turn recording off: no event subscription, no database. */\n enabled?: boolean | undefined\n /** SQLite database path override (defaults to the per-OS state directory). */\n db?: string | undefined\n /**\n * List-price estimates for steps the host reports as costing nothing.\n * `off` records charged money only; the TUI hides any stored estimates.\n */\n pricing?: \"auto\" | \"off\" | undefined\n}\n\n/** How long a loaded catalog is trusted before a background refresh. */\nconst PRICE_TTL_MS = 5 * 60 * 1000\n/** Retry delay after a failed catalog load. */\nconst PRICE_RETRY_MS = 30 * 1000\n\n/**\n * Catalog prices for the recorder, loaded from the host and refreshed in the\n * background. The recorder never waits for the catalog: until it lands, steps\n * are recorded exactly as before and the maintenance action can fill them in.\n */\nfunction createPriceSource(ctx: { model: { list: () => Promise<unknown> } }): {\n price: PriceFn\n refresh: (force?: boolean) => void\n} {\n let table: PriceTable | undefined\n let refreshing = false\n let lastAttempt = 0\n\n function refresh(force = false): void {\n if (refreshing) return\n const now = Date.now()\n const wait = table === undefined ? PRICE_RETRY_MS : PRICE_TTL_MS\n if (!force && now - lastAttempt < wait) return\n lastAttempt = now\n refreshing = true\n void (async () => {\n try {\n const payload = (await ctx.model.list()) as { data?: unknown } | unknown\n const models = Array.isArray(payload) ? payload : (payload as { data?: unknown } | null | undefined)?.data\n // An empty list is not a catalog: keep the previous table and retry.\n if (Array.isArray(models) && models.length > 0) table = priceTable(models as never)\n } catch {\n // A catalog that cannot be read keeps the previous table: recording is\n // more important than the estimate.\n } finally {\n refreshing = false\n }\n })()\n }\n\n return {\n price: (ref, tokens) => {\n refresh()\n return table === undefined ? undefined : costOf(table, ref, tokens)\n },\n refresh,\n }\n}\n\nfunction tryOpen(path: string): Database | undefined {\n try {\n return openStatsDb(path)\n } catch {\n return undefined\n }\n}\n\nexport default Plugin.define({\n id: \"usage-insights\",\n async setup(ctx) {\n const options = (ctx.options ?? {}) as Options\n if (options.enabled === false) return\n\n const db = tryOpen(resolveDbPath(options.db))\n if (db === undefined) return\n\n // The recorder's connection runs on the agent's own event path, so it waits\n // at most a second for another writer and then defers the write: a stalled\n // event handler is worse than a record that lands on the next event.\n try {\n db.exec(\"PRAGMA busy_timeout = 1000\")\n } catch {\n // A database that cannot take the pragma is still usable.\n }\n\n const pricing = options.pricing !== \"off\"\n const source = pricing ? createPriceSource(ctx) : undefined\n // Load the catalog without delaying the recorder; the first steps may be\n // unpriced and the maintenance action can fill them in later.\n source?.refresh(true)\n\n const recorder = createRecorder(db, source === undefined ? undefined : { price: source.price })\n const abort = new AbortController()\n const stream = (async () => {\n try {\n for await (const event of ctx.event.subscribe({ signal: abort.signal })) {\n try {\n // The event carries no model details: a catalog refresh is the only\n // way to pick up a price change.\n if (event.type === \"model.updated\") source?.refresh(true)\n recorder.handleEvent(event as RecorderEvent)\n } catch {\n // A malformed event must never break the stream.\n }\n }\n } catch {\n // Subscription closed (shutdown or transport error): nothing to do.\n }\n })()\n\n return async () => {\n abort.abort()\n await stream\n db.close()\n }\n },\n})\n",
|
|
6
|
+
"/**\n * List-price arithmetic for models the host reported as costing nothing.\n *\n * Prices come from the running OpenCode catalog (`ModelInfo.cost`) and are\n * expressed in USD per 1,000,000 tokens. This module mirrors the host's own\n * fallback exactly, so an estimate for a step reproduces what OpenCode would\n * have recorded before a provider started reporting `cost: 0`.\n *\n * Everything here takes plain objects: no host imports, so the rule can be\n * tested and reused by the recorder, the TUI line and the maintenance action.\n */\n\nexport type CostLike = {\n tier?: { type?: string | undefined; size?: number | undefined } | undefined\n input?: number | undefined\n output?: number | undefined\n cache?: { read?: number | undefined; write?: number | undefined } | undefined\n}\n\n/** The part of `ModelInfo` pricing needs, structurally. */\nexport type ModelInfoLike = {\n providerID?: string | undefined\n modelID?: string | undefined\n id?: string | undefined\n cost?: readonly CostLike[] | undefined\n}\n\nexport type PriceTier = {\n /** Present only on a context-tiered price; an untiered price has no tier. */\n tier?: { type?: string | undefined; size: number } | undefined\n input: number\n output: number\n cacheRead: number\n cacheWrite: number\n}\n\nexport type ModelPrice = {\n tiers: PriceTier[]\n}\n\n/** `providerID/modelID` to its prices. */\nexport type PriceTable = Map<string, ModelPrice>\n\nexport type PriceRef = {\n providerID?: string | undefined\n modelID?: string | undefined\n}\n\nexport type PriceTokens = {\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cacheRead?: number | undefined\n cacheWrite?: number | undefined\n}\n\n/** The estimate callback the recorder and the live line share. */\nexport type PriceFn = (ref: PriceRef, tokens: PriceTokens) => number | undefined\n\nfunction money(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) ? Math.max(0, value) : 0\n}\n\nfunction tokens(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) ? Math.max(0, value) : 0\n}\n\n/** Build the price table from the catalog the host exposes. */\nexport function priceTable(models: readonly ModelInfoLike[] | undefined): PriceTable {\n const table: PriceTable = new Map()\n if (!Array.isArray(models)) return table\n\n for (const model of models) {\n if (model === null || model === undefined) continue\n const providerID = model.providerID\n const modelID = model.modelID ?? model.id\n if (typeof providerID !== \"string\" || providerID === \"\") continue\n if (typeof modelID !== \"string\" || modelID === \"\") continue\n if (!Array.isArray(model.cost) || model.cost.length === 0) continue\n\n const tiers: PriceTier[] = []\n for (const cost of model.cost) {\n if (cost === null || cost === undefined) continue\n let tier: PriceTier[\"tier\"]\n if (cost.tier !== undefined) {\n const size = cost.tier.size\n // A tier without a usable size is not a base price: the host finds no\n // applicable entry for it and records zero. Treating it as the untiered\n // fallback would invent a price. Only an entry with no `tier` at all is\n // the base price. A negative size is kept: the host compares against it\n // like any other number, so mirroring the comparison is the honest\n // behaviour even though a real catalog never produces one.\n if (typeof size !== \"number\" || !Number.isFinite(size)) continue\n tier = { ...(cost.tier.type !== undefined ? { type: cost.tier.type } : {}), size }\n }\n tiers.push({\n ...(tier !== undefined ? { tier } : {}),\n input: money(cost.input),\n output: money(cost.output),\n cacheRead: money(cost.cache?.read),\n cacheWrite: money(cost.cache?.write),\n })\n }\n if (tiers.length > 0) table.set(`${providerID}/${modelID}`, { tiers })\n }\n return table\n}\n\nexport function priceOf(table: PriceTable, ref: PriceRef): ModelPrice | undefined {\n if (ref.providerID === undefined || ref.providerID === \"\") return undefined\n if (ref.modelID === undefined || ref.modelID === \"\") return undefined\n return table.get(`${ref.providerID}/${ref.modelID}`)\n}\n\n/**\n * The list price of one step, or undefined when there is nothing to show.\n *\n * The rule is the host's `cost` fallback: the prompt is fresh input plus cache\n * reads and writes; the tier that applies is the largest context tier the\n * prompt is strictly above; otherwise the untiered price. A known model whose\n * applicable prices are all zero returns `0` (free, not an estimate); an\n * unknown model or no applicable tier returns `undefined`.\n */\nexport function costOf(table: PriceTable, ref: PriceRef, usage: PriceTokens): number | undefined {\n const price = priceOf(table, ref)\n if (price === undefined) return undefined\n\n const input = tokens(usage.input)\n const output = tokens(usage.output)\n const reasoning = tokens(usage.reasoning)\n const cacheRead = tokens(usage.cacheRead)\n const cacheWrite = tokens(usage.cacheWrite)\n const prompt = input + cacheRead + cacheWrite\n\n const selected =\n price.tiers\n .filter((tier) => tier.tier?.type === \"context\" && prompt > tier.tier.size)\n .sort((left, right) => (right.tier?.size ?? 0) - (left.tier?.size ?? 0))[0] ??\n price.tiers.find((tier) => tier.tier === undefined)\n if (selected === undefined) return undefined\n\n // A model known to be free is not an unknown price: storing 0 keeps it out\n // of the backfill's \"needs an estimate\" set without inventing money.\n if (selected.input === 0 && selected.output === 0 && selected.cacheRead === 0 && selected.cacheWrite === 0) return 0\n\n const billed =\n input * selected.input +\n (output + reasoning) * selected.output +\n cacheRead * selected.cacheRead +\n cacheWrite * selected.cacheWrite\n if (!Number.isFinite(billed) || billed <= 0) return undefined\n return billed / 1_000_000\n}\n",
|
|
7
|
+
"import { Database } from \"bun:sqlite\"\nimport { chmodSync, mkdirSync } from \"node:fs\"\nimport { dirname } from \"node:path\"\nimport { ensureRegularFile } from \"./paths\"\nimport { isToken, sanitizeLabel, TEXT_LIMIT } from \"../text\"\n\n/**\n * Persistent usage store. One SQLite database holds every recorded step,\n * prompt, event and session row — and nothing else: the recorder only writes\n * what a view reads.\n *\n * The database is opened in WAL mode with a busy timeout so the server\n * recorder process and the TUI reader process can share it safely. The two\n * sides open it differently on purpose:\n *\n * - `openStatsDb` writes: schema, migrations and recording (the server).\n * - `openStatsDbReadOnly` only reads (the TUI), so no view can change the file\n * and no reader pays for a schema write.\n * - `openStatsDbWrite` is a short-lived maintenance connection (prune, vacuum)\n * that gives up quickly instead of holding the recorder up.\n */\n\nexport const SCHEMA_VERSION = 11\n\n/** Longest title or path kept: a label on a line, not a document. */\nconst LABEL_LIMIT = 4 * TEXT_LIMIT\n\nconst SCHEMA = `\nCREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS sessions (\n session_id TEXT PRIMARY KEY,\n parent_id TEXT,\n directory TEXT,\n title TEXT,\n started_at INTEGER,\n updated_at INTEGER\n);\n\nCREATE TABLE IF NOT EXISTS prompts (\n session_id TEXT NOT NULL,\n message_id TEXT NOT NULL,\n created_at INTEGER,\n PRIMARY KEY (session_id, message_id)\n);\nCREATE INDEX IF NOT EXISTS prompts_created ON prompts(created_at);\n\nCREATE TABLE IF NOT EXISTS steps (\n message_id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n parent_id TEXT,\n provider_id TEXT,\n model_id TEXT,\n variant TEXT,\n input_tokens INTEGER NOT NULL DEFAULT 0,\n output_tokens INTEGER NOT NULL DEFAULT 0,\n reasoning_tokens INTEGER NOT NULL DEFAULT 0,\n cache_read INTEGER NOT NULL DEFAULT 0,\n cache_write INTEGER NOT NULL DEFAULT 0,\n cost REAL NOT NULL DEFAULT 0,\n list_cost REAL,\n created_at INTEGER,\n streamed_at INTEGER,\n completed_at INTEGER,\n finish TEXT,\n recorded_at INTEGER NOT NULL\n);\nCREATE INDEX IF NOT EXISTS steps_session ON steps(session_id, created_at);\nCREATE INDEX IF NOT EXISTS steps_model ON steps(provider_id, model_id, variant);\nCREATE INDEX IF NOT EXISTS steps_completed ON steps(completed_at);\n-- Every range filter and every ordering compares the step's own clock, which\n-- falls back to the recording time for a step that was already in flight when\n-- the plugin loaded. The index has to cover that expression, or every ranged\n-- view is a full table scan.\nCREATE INDEX IF NOT EXISTS steps_at ON steps(COALESCE(created_at, recorded_at));\nCREATE INDEX IF NOT EXISTS steps_session_at ON steps(session_id, COALESCE(created_at, recorded_at));\n\nCREATE TABLE IF NOT EXISTS events (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n session_id TEXT NOT NULL,\n kind TEXT NOT NULL,\n message_id TEXT,\n at INTEGER,\n data TEXT\n);\nCREATE INDEX IF NOT EXISTS events_session ON events(session_id, at);\nCREATE INDEX IF NOT EXISTS events_kind ON events(kind, at);\n\n-- Usage the host reports with no assistant step of its own: the compaction\n-- summarisation and the session-title generation. The host publishes it as\n-- session.usage.recorded with a source other than generate, and includes it in\n-- the session's own cost/token totals. Primary key is the host event ID, so a\n-- replayed event never counts twice.\nCREATE TABLE IF NOT EXISTS usage (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n source TEXT NOT NULL,\n at INTEGER,\n cost REAL NOT NULL DEFAULT 0,\n input_tokens INTEGER NOT NULL DEFAULT 0,\n output_tokens INTEGER NOT NULL DEFAULT 0,\n reasoning_tokens INTEGER NOT NULL DEFAULT 0,\n cache_read INTEGER NOT NULL DEFAULT 0,\n cache_write INTEGER NOT NULL DEFAULT 0\n);\nCREATE INDEX IF NOT EXISTS usage_session_at ON usage(session_id, at);\nCREATE INDEX IF NOT EXISTS usage_at ON usage(at);\n`\n\n/** Tables carried by `exportNdjson` / `importNdjson`, in import-safe order. */\nexport const TABLES = [\"sessions\", \"prompts\", \"events\", \"steps\", \"usage\"] as const\n\nexport type TableName = (typeof TABLES)[number]\n\nexport type StepRecord = {\n messageID: string\n sessionID: string\n parentID?: string | undefined\n /** The provider the model ran through, as OpenCode configured it (`opencode-go`). */\n providerID?: string | undefined\n modelID?: string | undefined\n variant?: string | undefined\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cacheRead?: number | undefined\n cacheWrite?: number | undefined\n cost?: number | undefined\n /**\n * List-price estimate for a step the host reported as costing nothing.\n * `0` means a known free model; `NULL` means charged money or an unknown\n * price.\n */\n listCost?: number | undefined\n createdAt?: number | undefined\n streamedAt?: number | undefined\n completedAt?: number | undefined\n finish?: string | undefined\n /** Clock of the recording itself, defaults to `Date.now()`. */\n recordedAt?: number | undefined\n}\n\nexport type PromptRecord = {\n sessionID: string\n messageID: string\n createdAt?: number | undefined\n}\n\nexport type EventRecord = {\n sessionID: string\n kind: string\n messageID?: string | undefined\n at?: number | undefined\n data?: unknown\n}\n\n/**\n * Usage with no assistant step behind it (a compaction or a title generation).\n * `id` is the host event ID: it is the primary key, so a replay is ignored\n * instead of counted again.\n */\nexport type UsageRecord = {\n id: string\n sessionID: string\n source: string\n at?: number | undefined\n cost?: number | undefined\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cacheRead?: number | undefined\n cacheWrite?: number | undefined\n}\n\nexport type SessionRecord = {\n sessionID: string\n parentID?: string | undefined\n directory?: string | undefined\n title?: string | undefined\n startedAt?: number | undefined\n updatedAt?: number | undefined\n}\n\nfunction jsonText(value: unknown): string | null {\n if (value === undefined || value === null) return null\n if (typeof value === \"string\") return value\n try {\n return JSON.stringify(value)\n } catch {\n return null\n }\n}\n\n/**\n * Numeric fields arrive from the host event stream, which the plugin treats as\n * untrusted: a single `NaN` bound to a NOT NULL column makes SQLite reject the\n * whole statement, and the recorder's defensive catch would then silently lose\n * the step. Every number is therefore forced into a finite, non-negative value\n * before it reaches the database.\n */\nfunction finite(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) ? value : 0\n}\n\nfunction tokenCount(value: unknown): number {\n return Math.max(0, Math.floor(finite(value)))\n}\n\nfunction timestamp(value: unknown): number | null {\n return typeof value === \"number\" && Number.isFinite(value) ? value : null\n}\n\n/**\n * Columns and tables the recorder used to write and no view ever read. They are\n * dropped from an existing database, together with the rows of the events that\n * followed the same fate, so a long-lived file shrinks instead of carrying dead\n * weight forever.\n *\n * `router` is here because the gateway that named the routing endpoint in a\n * response header (`x-opencode-endpoint-id`) stopped naming one: the identity a\n * step is grouped by is the provider the model ran through, and the endpoint a\n * gateway happened to route to is no longer observable at all.\n */\nconst REMOVED_COLUMNS = [\"provider_state\", \"upstream\", \"upstream_source\", \"upstream_model\", \"tps\", \"router\"] as const\nconst REMOVED_EVENT_KINDS = [\"execution\", \"step-failed\", \"model-selected\", \"usage-recorded\"] as const\n\nfunction dropUnused(db: Database): void {\n const objects = db.query(\"SELECT name, type FROM sqlite_master WHERE type IN ('table', 'index')\").all() as {\n name: string\n type: string\n }[]\n const tables = new Set(objects.filter((row) => row.type === \"table\").map((row) => row.name))\n const indexes = new Set(objects.filter((row) => row.type === \"index\").map((row) => row.name))\n\n if (tables.has(\"http_responses\")) db.exec(\"DROP TABLE http_responses\")\n // These indexes read a column that is about to go: SQLite refuses to drop an\n // indexed column, so the index goes first.\n if (indexes.has(\"steps_upstream\")) db.exec(\"DROP INDEX steps_upstream\")\n if (indexes.has(\"steps_router\")) db.exec(\"DROP INDEX steps_router\")\n\n const columns = new Set((db.query(\"PRAGMA table_info(steps)\").all() as { name: string }[]).map((column) => column.name))\n for (const name of REMOVED_COLUMNS) {\n if (columns.has(name)) db.exec(`ALTER TABLE steps DROP COLUMN ${name}`)\n }\n\n if (tables.has(\"events\")) {\n const kinds = REMOVED_EVENT_KINDS.map((kind) => `'${kind}'`).join(\", \")\n db.exec(`DELETE FROM events WHERE kind IN (${kinds})`)\n }\n}\n\n/**\n * Add the columns a newer recorder writes. `CREATE TABLE IF NOT EXISTS` does\n * not touch a table that already exists, so a column added to `SCHEMA` after a\n * database was created has to be added here too. The check is idempotent and\n * also heals a database a half-applied run left behind.\n */\nfunction addMissing(db: Database): void {\n const columns = new Set((db.query(\"PRAGMA table_info(steps)\").all() as { name: string }[]).map((column) => column.name))\n if (columns.size > 0 && !columns.has(\"list_cost\")) {\n db.exec(\"ALTER TABLE steps ADD COLUMN list_cost REAL\")\n }\n}\n\n/**\n * Older recorders inserted a compaction event on every delivery, and the host\n * replays events on reconnect: the same compaction can be stored several times.\n * Keep one row per exact duplicate before the unique key is installed, so the\n * event count and any future cost aggregation stay honest.\n */\nfunction dedupeEvents(db: Database): void {\n const table = db.query(\"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'events'\").get()\n if (table === null) return\n db.exec(`DELETE FROM events\n WHERE kind = 'compaction' AND message_id IS NULL\n AND rowid NOT IN (\n SELECT MIN(rowid) FROM events\n WHERE kind = 'compaction' AND message_id IS NULL\n GROUP BY session_id, at, data\n )`)\n db.exec(`DELETE FROM events\n WHERE message_id IS NOT NULL\n AND rowid NOT IN (\n SELECT MIN(rowid) FROM events\n WHERE message_id IS NOT NULL\n GROUP BY kind, message_id\n )`)\n}\n\n/**\n * A host event ID identifies one event: a replay must not insert a second row.\n * The index is partial because historical rows carry no ID.\n */\nfunction addEventKey(db: Database): void {\n const table = db.query(\"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'events'\").get()\n if (table === null) return\n db.exec(\"CREATE UNIQUE INDEX IF NOT EXISTS events_kind_message ON events(kind, message_id) WHERE message_id IS NOT NULL\")\n}\n\n/**\n * Older recorders kept only the compaction event, never `session.usage.recorded`.\n * The event's payload already carries the cost and tokens, so recovering it\n * makes the fix retroactive for compactions. A row gets a stable synthetic ID;\n * `INSERT OR IGNORE` makes the recovery idempotent. Only historical rows\n * (`message_id IS NULL`) are considered: from this version on the recorder\n * stores the usage event itself, and backfilling again would count it twice.\n */\nfunction backfillCompactionUsage(db: Database): void {\n const tables = new Set(\n (db.query(\"SELECT name FROM sqlite_master WHERE type = 'table'\").all() as { name: string }[]).map((row) => row.name),\n )\n if (!tables.has(\"events\") || !tables.has(\"usage\")) return\n db.exec(`INSERT OR IGNORE INTO usage (\n id, session_id, source, at, cost,\n input_tokens, output_tokens, reasoning_tokens, cache_read, cache_write\n )\n SELECT 'compaction:' || rowid, session_id, 'compaction', at,\n MAX(0, COALESCE(CAST(json_extract(data, '$.cost') AS REAL), 0)),\n MAX(0, CAST(COALESCE(json_extract(data, '$.tokens.input'), 0) AS INTEGER)),\n MAX(0, CAST(COALESCE(json_extract(data, '$.tokens.output'), 0) AS INTEGER)),\n MAX(0, CAST(COALESCE(json_extract(data, '$.tokens.reasoning'), 0) AS INTEGER)),\n MAX(0, CAST(COALESCE(json_extract(data, '$.tokens.cache.read'), 0) AS INTEGER)),\n MAX(0, CAST(COALESCE(json_extract(data, '$.tokens.cache.write'), 0) AS INTEGER))\n FROM events\n WHERE kind = 'compaction' AND message_id IS NULL AND at IS NOT NULL AND data IS NOT NULL\n AND json_valid(data)`)\n}\n\n/**\n * Bring an existing file up to date: drop what no view reads, add what a newer\n * recorder writes, repair the event log a replayed write duplicated, and\n * recover the compaction usage older versions left in the event payload. Every\n * step is idempotent.\n */\nfunction migrate(db: Database): void {\n dropUnused(db)\n addMissing(db)\n dedupeEvents(db)\n backfillCompactionUsage(db)\n addEventKey(db)\n}\n\n/**\n * The file holds session titles, project directories and usage history, so it\n * is created for the user alone. Windows takes its access from the parent\n * directory's ACL, which is not something a plugin should rewrite.\n */\nconst POSIX = process.platform !== \"win32\"\n\nfunction hardenDirectory(path: string): void {\n if (!POSIX) return\n try {\n chmodSync(path, 0o700)\n } catch {\n // A shared or read-only directory: the database is still usable.\n }\n}\n\nfunction hardenFiles(path: string): void {\n if (!POSIX || path === \":memory:\") return\n for (const file of [path, `${path}-wal`, `${path}-shm`]) {\n try {\n chmodSync(file, 0o600)\n } catch {\n // The WAL and the shared-memory file are created on the first write and\n // removed on the last close, so one of them is often simply absent.\n }\n }\n}\n\n/** Directory for a database path, created private on POSIX. */\nfunction prepareDirectory(path: string): void {\n if (path === \":memory:\") return\n mkdirSync(dirname(path), { recursive: true, mode: 0o700 })\n hardenDirectory(dirname(path))\n}\n\n/** Open (creating when needed) the stats database and apply the schema. */\nexport function openStatsDb(path: string): Database {\n if (path !== \":memory:\") ensureRegularFile(path)\n prepareDirectory(path)\n const db = new Database(path, { create: true })\n db.exec(\"PRAGMA journal_mode = WAL\")\n db.exec(\"PRAGMA busy_timeout = 5000\")\n db.exec(\"PRAGMA synchronous = NORMAL\")\n db.exec(SCHEMA)\n migrate(db)\n db.query(\"INSERT INTO meta (key, value) VALUES ('schema_version', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value\").run(\n String(SCHEMA_VERSION),\n )\n // The schema write is what creates the WAL and its shared-memory file.\n hardenFiles(path)\n return db\n}\n\n/**\n * Reader connection for the TUI: no schema, no migration, no write. It throws\n * when the database is absent or was never written by the recorder, so the\n * caller can say so instead of showing an empty panel.\n */\nexport function openStatsDbReadOnly(path: string): Database {\n ensureRegularFile(path)\n const db = new Database(path, { readonly: true })\n try {\n db.query(\"SELECT 1 FROM steps LIMIT 1\").get()\n } catch (error) {\n db.close()\n throw error\n }\n return db\n}\n\n/**\n * Short-lived write connection for maintenance. The busy timeout is short on\n * purpose: a prune or a vacuum that cannot take the lock must fail in front of\n * the user, not hold the recorder's response path for seconds.\n */\nexport function openStatsDbWrite(path: string): Database {\n if (path !== \":memory:\") ensureRegularFile(path)\n prepareDirectory(path)\n const db = new Database(path, { create: true })\n db.exec(\"PRAGMA busy_timeout = 250\")\n db.exec(\"PRAGMA synchronous = NORMAL\")\n hardenFiles(path)\n return db\n}\n\n/**\n * Insert or refresh an assistant step. Idempotent by message ID so replayed\n * events, restarts and NDJSON imports never duplicate usage. A non-null field\n * (the provider, the model, the finish reason) survives a re-record with `NULL`\n * through COALESCE. `list_cost` follows the opposite rule: a step that becomes\n * charged loses its estimate, and an estimate survives a replay that carries\n * no price table.\n *\n * The provider is the identity every view groups by, so it is validated as a\n * token before it is stored: a value that is not a provider id is recorded as\n * absent instead of becoming a grouping key.\n */\nexport function recordStep(db: Database, record: StepRecord): void {\n const cost = Math.max(0, finite(record.cost))\n const listCost =\n cost <= 0 && typeof record.listCost === \"number\" && Number.isFinite(record.listCost) && record.listCost >= 0\n ? record.listCost\n : null\n db.query(\n `INSERT INTO steps (\n message_id, session_id, parent_id, provider_id, model_id, variant,\n input_tokens, output_tokens, reasoning_tokens, cache_read, cache_write, cost, list_cost,\n created_at, streamed_at, completed_at, finish, recorded_at\n ) VALUES (\n $messageID, $sessionID, $parentID, $providerID, $modelID, $variant,\n $input, $output, $reasoning, $cacheRead, $cacheWrite, $cost, $listCost,\n $createdAt, $streamedAt, $completedAt, $finish, $recordedAt\n )\n ON CONFLICT(message_id) DO UPDATE SET\n session_id = excluded.session_id,\n parent_id = COALESCE(excluded.parent_id, steps.parent_id),\n provider_id = COALESCE(excluded.provider_id, steps.provider_id),\n model_id = COALESCE(excluded.model_id, steps.model_id),\n variant = COALESCE(excluded.variant, steps.variant),\n input_tokens = excluded.input_tokens,\n output_tokens = excluded.output_tokens,\n reasoning_tokens = excluded.reasoning_tokens,\n cache_read = excluded.cache_read,\n cache_write = excluded.cache_write,\n cost = excluded.cost,\n -- A step that starts being charged loses its estimate; an estimate is\n -- kept when a replayed event arrives without a price table.\n list_cost = CASE WHEN excluded.cost > 0 THEN NULL ELSE COALESCE(excluded.list_cost, steps.list_cost) END,\n created_at = COALESCE(excluded.created_at, steps.created_at),\n streamed_at = COALESCE(excluded.streamed_at, steps.streamed_at),\n completed_at = COALESCE(excluded.completed_at, steps.completed_at),\n finish = COALESCE(excluded.finish, steps.finish),\n recorded_at = excluded.recorded_at`,\n ).run({\n $messageID: record.messageID,\n $sessionID: record.sessionID,\n $parentID: record.parentID ?? null,\n $providerID: record.providerID !== undefined && isToken(record.providerID) ? record.providerID : null,\n $modelID: record.modelID ?? null,\n $variant: record.variant ?? null,\n $input: tokenCount(record.input),\n $output: tokenCount(record.output),\n $reasoning: tokenCount(record.reasoning),\n $cacheRead: tokenCount(record.cacheRead),\n $cacheWrite: tokenCount(record.cacheWrite),\n $cost: cost,\n $listCost: listCost,\n $createdAt: timestamp(record.createdAt),\n $streamedAt: timestamp(record.streamedAt),\n $completedAt: timestamp(record.completedAt),\n $finish: record.finish === undefined ? null : sanitizeLabel(record.finish, 64),\n $recordedAt: timestamp(record.recordedAt) ?? Date.now(),\n })\n}\n\n/**\n * Insert usage that has no assistant step of its own. The host event ID is the\n * primary key: a replayed event is ignored, so the totals never double count a\n * compaction or a title generation delivered more than once.\n */\nexport function recordUsage(db: Database, record: UsageRecord): void {\n if (typeof record.id !== \"string\" || record.id === \"\") return\n if (typeof record.sessionID !== \"string\" || record.sessionID === \"\") return\n db.query(\n `INSERT INTO usage (\n id, session_id, source, at, cost, input_tokens, output_tokens, reasoning_tokens, cache_read, cache_write\n ) VALUES (\n $id, $sessionID, $source, $at, $cost, $input, $output, $reasoning, $cacheRead, $cacheWrite\n ) ON CONFLICT(id) DO NOTHING`,\n ).run({\n $id: record.id,\n $sessionID: record.sessionID,\n $source: sanitizeLabel(record.source, 64),\n $at: timestamp(record.at) ?? Date.now(),\n $cost: Math.max(0, finite(record.cost)),\n $input: tokenCount(record.input),\n $output: tokenCount(record.output),\n $reasoning: tokenCount(record.reasoning),\n $cacheRead: tokenCount(record.cacheRead),\n $cacheWrite: tokenCount(record.cacheWrite),\n })\n}\n\nexport function recordPrompt(db: Database, record: PromptRecord): void {\n db.query(\n `INSERT INTO prompts (session_id, message_id, created_at)\n VALUES ($sessionID, $messageID, $createdAt)\n ON CONFLICT(session_id, message_id) DO UPDATE SET created_at = COALESCE(excluded.created_at, prompts.created_at)`,\n ).run({\n $sessionID: record.sessionID,\n $messageID: record.messageID,\n $createdAt: timestamp(record.createdAt),\n })\n}\n\nexport function recordEvent(db: Database, record: EventRecord): void {\n db.query(\n `INSERT OR IGNORE INTO events (session_id, kind, message_id, at, data)\n VALUES ($sessionID, $kind, $messageID, $at, $data)`,\n ).run({\n $sessionID: record.sessionID,\n $kind: sanitizeLabel(record.kind, 64),\n $messageID: record.messageID ?? null,\n $at: timestamp(record.at) ?? Date.now(),\n $data: jsonText(record.data),\n })\n}\n\nexport function upsertSession(db: Database, record: SessionRecord): void {\n db.query(\n `INSERT INTO sessions (session_id, parent_id, directory, title, started_at, updated_at)\n VALUES ($sessionID, $parentID, $directory, $title, $startedAt, $updatedAt)\n ON CONFLICT(session_id) DO UPDATE SET\n parent_id = COALESCE(excluded.parent_id, sessions.parent_id),\n directory = COALESCE(excluded.directory, sessions.directory),\n title = COALESCE(excluded.title, sessions.title),\n started_at = COALESCE(sessions.started_at, excluded.started_at),\n updated_at = COALESCE(excluded.updated_at, sessions.updated_at)`,\n ).run({\n $sessionID: record.sessionID,\n $parentID: record.parentID ?? null,\n // Titles are written by a model and directories come from the host: both\n // are displayed, so neither may carry control characters into a terminal.\n $directory: record.directory === undefined ? null : sanitizeLabel(record.directory, LABEL_LIMIT),\n $title: record.title === undefined ? null : sanitizeLabel(record.title, LABEL_LIMIT),\n $startedAt: timestamp(record.startedAt),\n $updatedAt: timestamp(record.updatedAt),\n })\n}\n\n/** Dump every table as NDJSON (`{\"table\": \"...\", \"row\": {...}}` per line). */\nexport function exportNdjson(db: Database): string {\n const lines: string[] = []\n for (const table of TABLES) {\n const rows = db.query(`SELECT * FROM ${table}`).all() as Record<string, unknown>[]\n for (const row of rows) lines.push(JSON.stringify({ table, row }))\n }\n return lines.length > 0 ? `${lines.join(\"\\n\")}\\n` : \"\"\n}\n\n/** Column shape an import has to respect: the type and the NOT NULL rules. */\ntype ImportColumn = {\n name: string\n /** Declared affinity: `INTEGER`, `REAL`, `TEXT`, `BLOB` or empty. */\n type: string\n notNull: boolean\n primaryKey: boolean\n /** A column with a default may be left out of the import entirely. */\n hasDefault: boolean\n}\n\nfunction numericAffinity(type: string): boolean {\n const upper = type.toUpperCase()\n return upper.includes(\"INT\") || upper.includes(\"REAL\") || upper.includes(\"NUM\") || upper.includes(\"DEC\") || upper.includes(\"DOUB\")\n}\n\n/**\n * One imported value, forced into the shape of its column. Text is sanitized\n * and capped like everything else that is displayed; a number that is not a\n * finite number becomes NULL, so a crafted dump cannot store `NaN` or a\n * string where a view does arithmetic.\n */\nfunction importValue(value: unknown, column: ImportColumn): string | number | null {\n if (value === null || value === undefined) return null\n if (numericAffinity(column.type)) {\n const parsed = typeof value === \"number\" ? value : Number(value)\n return Number.isFinite(parsed) ? parsed : null\n }\n if (typeof value === \"number\") return Number.isFinite(value) ? value : null\n if (typeof value === \"boolean\") return value ? 1 : 0\n if (typeof value === \"object\") return sanitizeLabel(JSON.stringify(value) ?? \"\", LABEL_LIMIT)\n return sanitizeLabel(String(value), LABEL_LIMIT)\n}\n\n/**\n * Load an `exportNdjson` dump back in. Rows are inserted with `OR IGNORE`, so\n * importing the same dump twice is harmless. Unknown tables and rows that are\n * not objects are rejected; a row that cannot fill a required column is\n * skipped rather than aborting the rest of the import. A column the row does\n * not carry is left to its default, the same way a hand-written insert is.\n */\nexport function importNdjson(db: Database, text: string): number {\n const columns = new Map<TableName, ImportColumn[]>()\n for (const table of TABLES) {\n const info = db.query(`PRAGMA table_info(${table})`).all() as {\n name: string\n type: string\n notnull: number\n pk: number\n dflt_value: string | null\n }[]\n columns.set(\n table,\n info.map((column) => ({\n name: column.name,\n type: column.type,\n notNull: column.notnull === 1,\n primaryKey: column.pk > 0,\n hasDefault: column.dflt_value !== null,\n })),\n )\n }\n\n let imported = 0\n for (const line of text.split(\"\\n\")) {\n const trimmed = line.trim()\n if (!trimmed) continue\n let parsed: { table?: string; row?: Record<string, unknown> }\n try {\n parsed = JSON.parse(trimmed) as { table?: string; row?: Record<string, unknown> }\n } catch {\n continue\n }\n const table = parsed.table as TableName | undefined\n const row = parsed.row\n if (!table || !row || !(TABLES as readonly string[]).includes(table)) continue\n const shape = columns.get(table) ?? []\n if (shape.length === 0) continue\n const names: string[] = []\n const values: (string | number)[] = []\n let importable = true\n for (const column of shape) {\n const value = importValue(row[column.name], column)\n if (value !== null) {\n names.push(column.name)\n values.push(value)\n continue\n }\n // NULL for a column that cannot hold one has no default to fall back on,\n // and a NULL in a text primary key is a key the database cannot use; an\n // INTEGER PRIMARY KEY is a rowid, so leaving it out assigns one.\n const required = (column.notNull || (column.primaryKey && !numericAffinity(column.type))) && !column.hasDefault\n if (required) {\n importable = false\n break\n }\n }\n if (!importable) continue\n // The database's money rule is an invariant, not a convention: a step that\n // was charged has no list estimate. A hand-written or foreign dump may\n // carry both, which every cost total would then add twice. Negative token\n // counts and costs are nonsense and would poison the sums.\n const tokenNames = [\"input_tokens\", \"output_tokens\", \"reasoning_tokens\", \"cache_read\", \"cache_write\"]\n for (const name of tokenNames) {\n const index = names.indexOf(name)\n if (index !== -1 && typeof values[index] === \"number\") values[index] = Math.max(0, Math.floor(values[index] as number))\n }\n const costIndex = names.indexOf(\"cost\")\n if (costIndex !== -1 && typeof values[costIndex] === \"number\") {\n const cost = Math.max(0, values[costIndex] as number)\n values[costIndex] = cost\n const listIndex = names.indexOf(\"list_cost\")\n if (table === \"steps\" && cost > 0 && listIndex !== -1) {\n names.splice(listIndex, 1)\n values.splice(listIndex, 1)\n }\n }\n const placeholders = names.map(() => \"?\").join(\", \")\n db.query(`INSERT OR IGNORE INTO ${table} (${names.join(\", \")}) VALUES (${placeholders})`).run(...values)\n imported += 1\n }\n return imported\n}\n",
|
|
7
8
|
"import { homedir } from \"node:os\"\nimport { join } from \"node:path\"\nimport { statSync } from \"node:fs\"\n\n/**\n * SQLite database used when no `db` option is configured: the per-user state\n * directory of the current OS, never a shared temp directory.\n *\n * The subdirectory is the plugin's package name, never a generic one: the\n * `opencode` tree is shared with OpenCode itself and with every other plugin,\n * so a `stats/` of our own would be a collision waiting to happen.\n *\n * - Windows: `%LOCALAPPDATA%\\opencode\\opencode-usage-insights\\usage.db`\n * - macOS: `~/Library/Application Support/opencode/opencode-usage-insights/usage.db`\n * - Linux/other: `$XDG_STATE_HOME/opencode/opencode-usage-insights/usage.db`,\n * falling back to `~/.local/state/opencode/opencode-usage-insights/usage.db`\n *\n * Pure by design so every OS can be asserted from any machine.\n */\nexport function defaultDbPath(env: Record<string, string | undefined> = process.env, platform: string = process.platform): string {\n const name = join(\"opencode\", \"opencode-usage-insights\", \"usage.db\")\n if (platform === \"win32\") {\n return join(env.LOCALAPPDATA ?? join(homedir(), \"AppData\", \"Local\"), name)\n }\n if (platform === \"darwin\") {\n return join(homedir(), \"Library\", \"Application Support\", name)\n }\n return join(env.XDG_STATE_HOME ?? join(homedir(), \".local\", \"state\"), name)\n}\n\n/**\n * Expand a leading `~` (or `~/`), so a configured path reads the way every\n * shell does. Windows accepts the backslash form too.\n */\nexport function expandHome(path: string, home: string = homedir()): string {\n if (path === \"~\") return home\n if (path.startsWith(\"~/\") || path.startsWith(\"~\\\\\")) return join(home, path.slice(2))\n return path\n}\n\n/**\n * Resolve the effective database path: explicit plugin option first, then the\n * `OPENCODE_USAGE_INSIGHTS_DB` escape hatch (handy for dev and tests), then\n * the per-OS state directory. A leading `~` is expanded in all three.\n */\nexport function resolveDbPath(\n configured?: string | undefined,\n env: Record<string, string | undefined> = process.env,\n platform: string = process.platform,\n): string {\n const home = env.HOME ?? env.USERPROFILE ?? homedir()\n if (configured) return expandHome(configured, home)\n if (env.OPENCODE_USAGE_INSIGHTS_DB) return expandHome(env.OPENCODE_USAGE_INSIGHTS_DB, home)\n return defaultDbPath(env, platform)\n}\n\n/**\n * A configured path must not be a directory, a device or a FIFO: SQLite would\n * fail obscurely, or write where nobody expects it. Symlinks to a regular file\n * are followed (`statSync`, not `lstat`) because moving the state directory to\n * another disk is a legitimate setup. A missing file is fine — the plugin\n * creates it.\n */\nexport function ensureRegularFile(path: string): void {\n let stats\n try {\n stats = statSync(path)\n } catch {\n return\n }\n if (!stats.isFile()) throw new Error(`not a regular file: ${path}`)\n}\n",
|
|
8
9
|
"/**\n * Text hygiene for everything that reaches a terminal.\n *\n * Some of what the plugin displays is not written by the user: a model writes\n * a session title, an NDJSON export can come from anywhere. A control\n * character in any of those is interpreted by the terminal that renders it\n * (title changes, cursor moves, colours), so every such string is stripped of\n * control characters before it is stored or drawn.\n *\n * The rule is deliberately blunt: drop C0 controls, DEL and the C1 block, keep\n * everything else (the text is otherwise arbitrary Unicode), and cap the\n * length so a hostile value cannot bloat the database or the frame. Layout is\n * never touched: whitespace is preserved, because callers build indentation\n * into the strings they truncate.\n */\n\n/** Longest value kept by default: a title, an id or a path, not a document. */\nexport const TEXT_LIMIT = 512\n\n/**\n * Strip control characters and cap the length. Newlines and tabs go too: a\n * line is one line, and the renderers lay out their own spacing.\n */\nexport function sanitizeText(value: string, limit = TEXT_LIMIT): string {\n if (limit <= 0) return \"\"\n let cleaned = \"\"\n for (const character of value) {\n const code = character.codePointAt(0) ?? 0\n if (code <= 0x1f || code === 0x7f || (code >= 0x80 && code <= 0x9f)) continue\n cleaned += character\n if (cleaned.length >= limit) break\n }\n return cleaned\n}\n\n/** `sanitizeText` without the layout, for a stored label: trimmed as well. */\nexport function sanitizeLabel(value: string, limit = TEXT_LIMIT): string {\n return sanitizeText(value, limit).trim()\n}\n\n/**\n * A single identifier token: the shape a provider id or a model id has. Used\n * to validate the values that become grouping keys in the database, so a\n * configured provider cannot inject anything into an identity a view groups\n * by. Case is left alone: the provider is shown as it is configured, and\n * folding it would report a provider the user never chose.\n */\nconst TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/\n\nexport function isToken(value: string, limit = 64): boolean {\n return value.length > 0 && value.length <= limit && TOKEN.test(value)\n}\n",
|
|
9
|
-
"/**\n * Pure turn accounting shared by the TUI line, the server recorder and the\n * stats engine.\n *\n * Everything here operates on structural shapes that mirror the session\n * messages published by OpenCode, so the logic stays testable without a host\n * runtime and without the effect-based schema types.\n */\n\nexport type ModelRefLike = {\n providerID: string\n id: string\n variant?: string | undefined\n}\n\nexport type AssistantMessage = {\n type: \"assistant\"\n id: string\n time?: { created?: number | undefined; streamed?: number | undefined; completed?: number | undefined } | undefined\n model?: ModelRefLike | undefined\n tokens?:\n | {\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cache?: { read?: number | undefined; write?: number | undefined } | undefined\n }\n | undefined\n cost?: number | undefined\n finish?: string | null | undefined\n rawFinish?: string | null | undefined\n}\n\nexport type PromptMessage = {\n type: \"user\" | \"synthetic\" | \"system\"\n id?: string | undefined\n time?: { created?: number | undefined } | undefined\n}\n\nexport type TranscriptMessage =\n | AssistantMessage\n | PromptMessage\n | { type: string; id?: string | undefined; time?: { created?: number | undefined } | undefined }\n\n/** Accumulated token usage for a group of assistant steps. */\nexport type Totals = {\n input: number\n cached: number\n output: number\n reasoning: number\n cacheWrite: number\n cost: number\n steps: number\n}\n\nexport type CacheDrop = {\n messageID: string\n /** Tokens that were expected to hit the prompt cache but did not. */\n tokens: number\n /** `cache.read` of the step that was used as the reuse reference. */\n prevCached: number\n /** Time between the reference step's completion and this step's request. */\n gapMs: number | undefined\n}\n\nexport type TurnStats = {\n /** Wall clock time of the last step's completion. */\n endAt: number | undefined\n /** Creation time of the first settled step of the turn. */\n startAt: number | undefined\n /** Creation time of the prompt that opened this turn. */\n promptAt: number | undefined\n /** Time between the previous prompt and this turn's prompt. */\n promptGapMs: number | undefined\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n /** Provider ID differs from the previous turn in the same transcript. */\n providerChanged: boolean\n /** Provider is the same but the model or variant differs. */\n modelChanged: boolean\n totals: Totals\n /** Passed through from the caller (session-wide accumulated usage). */\n session: Totals | undefined\n /**\n * Prompt-side usage of the last finished step: the context the turn ended\n * with. Summing `input`/`cached` over the steps of a turn counts the same\n * re-sent prompt once per step, which is not what a context view means.\n */\n latest: Totals\n drops: CacheDrop[]\n cacheDropTokens: number\n cacheExpired: boolean\n /** Assistant steps in the turn. */\n steps: number\n}\n\nexport type TurnSegment = {\n assistants: AssistantMessage[]\n prompt: TranscriptMessage | undefined\n}\n\n/** Message types that open a turn: a run of assistants belongs to the last one. */\nexport const PROMPT_BOUNDARIES: ReadonlySet<string> = new Set([\"user\", \"synthetic\", \"system\"])\n\n/**\n * Split a transcript into turns. A turn is a run of assistant steps opened by\n * a prompt boundary (`user`, `synthetic`, `system`). Idle notices, info\n * messages, compactions and shell entries keep the current turn open so a\n * single execution is never split in two.\n */\nexport function transcriptSegments(messages: readonly TranscriptMessage[]): TurnSegment[] {\n const segments: TurnSegment[] = []\n let current: AssistantMessage[] = []\n let prompt: TranscriptMessage | undefined\n\n for (const message of messages) {\n if (message.type === \"assistant\") {\n current.push(message as AssistantMessage)\n continue\n }\n if (!PROMPT_BOUNDARIES.has(message.type)) continue\n if (current.length > 0) {\n segments.push({ assistants: current, prompt })\n current = []\n }\n prompt = message\n }\n if (current.length > 0) segments.push({ assistants: current, prompt })\n return segments\n}\n\n/**\n * The last `count` segments, found by walking the transcript backwards. The\n * live line only needs the turn that just ended and the one before it, and a\n * long session holds tens of thousands of messages: the tail walk stops as\n * soon as it has them, instead of building a segment for every turn.\n */\nexport function transcriptTail(messages: readonly TranscriptMessage[], count: number): TurnSegment[] {\n const segments: TurnSegment[] = []\n let current: AssistantMessage[] = []\n\n for (let index = messages.length - 1; index >= 0; index -= 1) {\n const message = messages[index]!\n if (message.type === \"assistant\") {\n current.unshift(message as AssistantMessage)\n continue\n }\n if (!PROMPT_BOUNDARIES.has(message.type)) continue\n if (current.length === 0) continue\n // The boundary closes the run collected after it.\n segments.unshift({ assistants: current, prompt: message })\n if (segments.length >= count) return segments\n current = []\n }\n // A leading run with no prompt before it is a segment of its own.\n if (current.length > 0 && segments.length < count) segments.unshift({ assistants: current, prompt: undefined })\n return segments\n}\n\nexport function tokenCounts(message: AssistantMessage): { input: number; output: number; reasoning: number; read: number; write: number } {\n const tokens = message.tokens\n return {\n input: tokens?.input ?? 0,\n output: tokens?.output ?? 0,\n reasoning: tokens?.reasoning ?? 0,\n read: tokens?.cache?.read ?? 0,\n write: tokens?.cache?.write ?? 0,\n }\n}\n\nexport function totalize(steps: readonly AssistantMessage[]): Totals {\n const totals: Totals = { input: 0, cached: 0, output: 0, reasoning: 0, cacheWrite: 0, cost: 0, steps: steps.length }\n for (const step of steps) {\n const counts = tokenCounts(step)\n totals.input += counts.input\n totals.cached += counts.read\n totals.output += counts.output\n totals.reasoning += counts.reasoning\n totals.cacheWrite += counts.write\n totals.cost += step.cost ?? 0\n }\n return totals\n}\n\n/**\n * The identity of a model reference: the provider it runs through, the model\n * and its variant. Two steps with the same key ran the same thing the same way,\n * which is what `modelChanged` compares.\n */\nexport function identityKey(model: ModelRefLike | undefined): string | undefined {\n if (!model) return undefined\n return `${model.providerID}/${model.id}${model.variant ? `#${model.variant}` : \"\"}`\n}\n\nexport function cacheRead(message: AssistantMessage | undefined): number {\n return message?.tokens?.cache?.read ?? 0\n}\n\nexport function stepCreated(message: AssistantMessage | undefined): number | undefined {\n return message?.time?.created\n}\n\nexport function stepEnd(message: AssistantMessage | undefined): number | undefined {\n const time = message?.time\n if (!time) return undefined\n return time.completed ?? time.streamed ?? time.created\n}\n\n/**\n * Which identity the two steps must share for a reuse loss to be measurable.\n * `provider` is the strict one: two providers are two caches, so a switch\n * between them is not a loss but a different cache. `model` drops that test,\n * for the reports that measure what an identity change costs: forfeiting the\n * previous provider's cache is exactly the cost they report.\n */\nexport type CacheDropScope = \"provider\" | \"model\"\n\n/**\n * Cache reuse loss against a reference step, mirroring OpenCode's own\n * `turn-usage` heuristic: only comparable when the model is identical, and\n * OpenAI's API reports small reads that are not worth flagging.\n */\nexport function cacheDrop(reference: AssistantMessage | undefined, current: AssistantMessage, scope: CacheDropScope = \"provider\"): number | undefined {\n if (!reference) return undefined\n const before = reference.model\n const after = current.model\n if (!before || !after) return undefined\n if (scope === \"provider\" && before.providerID !== after.providerID) return undefined\n if (before.id !== after.id || (before.variant ?? \"\") !== (after.variant ?? \"\")) {\n return undefined\n }\n const drop = cacheRead(reference) - cacheRead(current)\n if (isNoiseDrop(after.providerID, drop)) return undefined\n return drop > 0 ? drop : undefined\n}\n\n/** Milliseconds between a reference step's completion and the next request. */\nexport function stepGap(reference: AssistantMessage | undefined, current: AssistantMessage): number | undefined {\n const ended = stepEnd(reference)\n const started = stepCreated(current)\n if (ended === undefined || started === undefined) return undefined\n return Math.max(0, started - ended)\n}\n\n/**\n * Streaming duration of a step: request start to the last streamed chunk. The\n * host's `streamed` stamp marks the end of streaming, so this is the window a\n * generation rate belongs to — not a time to first token, which the session\n * messages do not record.\n */\nexport function stepStreamMs(message: AssistantMessage): number | undefined {\n const created = message.time?.created\n const streamed = message.time?.streamed\n if (created === undefined || streamed === undefined || streamed <= created) return undefined\n return streamed - created\n}\n\n/**\n * Tokens produced per second over a window. For a step the window is the\n * streaming one: request start to the last streamed chunk (`created` →\n * `streamed`) — `completed` marks the end of the step, which for a tool-call\n * step happens after the tool calls were handled. Returns undefined when the\n * window or the produced tokens are unknown. Single definition, shared by the\n * recorder (which stores it per step) and the latency view.\n */\nexport function tokensPerSecond(produced: number, from: number | undefined, to: number | undefined): number | undefined {\n if (from === undefined || to === undefined || to <= from) return undefined\n if (produced <= 0) return undefined\n return (produced / (to - from)) * 1000\n}\n\n\n/** Output-plus-reasoning tokens per second of a step. */\nexport function stepTps(message: AssistantMessage): number | undefined {\n const counts = tokenCounts(message)\n return tokensPerSecond(counts.output + counts.reasoning, message.time?.created, message.time?.streamed)\n}\n\nconst DROP_EXEMPT_PROVIDERS = new Set([\"openai\"])\n\n/**\n * True when a step has final numbers. A step that is still streaming reports\n * partial tokens (usually zero) and no cache read yet, so reading it would\n * render an empty line and invent a cache-expiry warning against it.\n */\nexport function isSettledStep(message: AssistantMessage): boolean {\n if (message.time?.completed !== undefined) return true\n return message.finish !== undefined && message.finish !== null\n}\n\n/**\n * Compute the stats of the most recent turn in a transcript, including the\n * comparison against the previous turn (a provider or model switch, and cache\n * reuse).\n *\n * Returns undefined while the newest turn is still being generated: the line\n * appears when a turn ends, never with the zeroes of an in-flight response.\n */\nexport function computeTurn(messages: readonly TranscriptMessage[], session?: Totals): TurnStats | undefined {\n const segments = transcriptTail(messages, 2)\n const last = segments[segments.length - 1]\n if (!last) return undefined\n const previous = segments[segments.length - 2]\n const lastStep = last.assistants[last.assistants.length - 1]\n if (!lastStep) return undefined\n if (!isSettledStep(lastStep)) return undefined\n const previousStep = previous?.assistants[previous.assistants.length - 1]\n // Defensive: a step that never settled mid-turn carries no usable numbers,\n // so it is neither a cache reference nor a reuse candidate.\n const settled = last.assistants.filter(isSettledStep)\n\n const drops: CacheDrop[] = []\n let reference: AssistantMessage | undefined = previousStep !== undefined && isSettledStep(previousStep) ? previousStep : undefined\n for (const step of settled) {\n const drop = cacheDrop(reference, step)\n if (drop !== undefined) {\n drops.push({\n messageID: step.id,\n tokens: drop,\n prevCached: cacheRead(reference),\n gapMs: stepGap(reference, step),\n })\n }\n reference = step\n }\n\n const provider = lastStep.model?.providerID\n const previousProvider = previousStep?.model?.providerID\n const key = identityKey(lastStep.model)\n const previousKey = identityKey(previousStep?.model)\n\n return {\n endAt: stepEnd(lastStep),\n startAt: stepCreated(settled[0] ?? lastStep),\n promptAt: last.prompt?.time?.created,\n promptGapMs: promptGap(last.prompt, previous?.prompt),\n providerID: provider,\n modelID: lastStep.model?.id,\n variant: lastStep.model?.variant,\n providerChanged: previousProvider !== undefined && provider !== undefined && previousProvider !== provider,\n modelChanged: previousKey !== undefined && key !== undefined && previousKey !== key && previousProvider === provider,\n totals: totalize(settled),\n latest: totalize([lastStep]),\n session,\n drops,\n cacheDropTokens: drops.reduce((sum, drop) => sum + drop.tokens, 0),\n cacheExpired: drops.length > 0,\n steps: settled.length,\n }\n}\n\nfunction promptGap(current: TranscriptMessage | undefined, previous: TranscriptMessage | undefined): number | undefined {\n const now = current?.time?.created\n const before = previous?.time?.created\n if (now === undefined || before === undefined) return undefined\n return Math.max(0, now - before)\n}\n\n/** True when a cache drop is small enough to be provider noise. */\nexport function isNoiseDrop(providerID: string | undefined, drop: number): boolean {\n if (providerID === undefined) return false\n return DROP_EXEMPT_PROVIDERS.has(providerID) && drop >= 1024 && drop <= 2048\n}\n\n// ---------------------------------------------------------------------------\n// Formatting\n// ---------------------------------------------------------------------------\n\nexport type LineTone = \"base\" | \"muted\" | \"warning\"\n\nexport type LineSegment = { text: string; tone: LineTone }\n\nexport type LineLabels = {\n delta: string\n turn: string\n session: string\n step: string\n steps: string\n input: string\n cached: string\n output: string\n reasoning: string\n cost: string\n cacheExpired: string\n providerChanged: string\n modelChanged: string\n}\n\n/**\n * How the turn segment counts prompt usage. `latest` shows the prompt size the\n * turn ended with — the context view, and what OpenCode's own turn-token debug\n * line reports. `sum` adds every step of the turn: the billing view, where a\n * tool-call loop re-sends (and re-reads) the same context once per step.\n */\nexport type TurnTotalsView = \"latest\" | \"sum\"\n\nexport type LineFields = {\n clock: boolean\n delta: boolean\n model: boolean\n variant: boolean\n turn: boolean\n session: boolean\n reasoning: boolean\n cost: boolean\n cacheExpiry: boolean\n}\n\nexport const DEFAULT_LABELS: LineLabels = {\n delta: \"Δ\",\n // The turn segment is unlabelled by default to keep the line short; set it\n // back to \"turn\" (or anything else) through `labels.turn`.\n turn: \"\",\n session: \"sess\",\n step: \"step\",\n steps: \"steps\",\n input: \"in\",\n cached: \"cached\",\n output: \"out\",\n reasoning: \"reason\",\n cost: \"cost\",\n cacheExpired: \"cache expired\",\n providerChanged: \"changed\",\n modelChanged: \"model changed\",\n}\n\nexport const DEFAULT_FIELDS: LineFields = {\n clock: true,\n delta: true,\n model: true,\n variant: true,\n turn: true,\n session: true,\n reasoning: false,\n // The turn's and the session's cost are on by default: what a session costs\n // is the question the tokens only half answer. A zero is left out, so a free\n // model reads as free instead of repeating `$0` on every line.\n cost: true,\n cacheExpiry: true,\n}\n\nexport function formatClock(ms: number): string {\n const date = new Date(ms)\n const pad = (value: number) => String(value).padStart(2, \"0\")\n return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`\n}\n\nexport function formatDuration(ms: number): string {\n const totalSeconds = Math.max(0, Math.floor(ms / 1000))\n const seconds = totalSeconds % 60\n const minutes = Math.floor(totalSeconds / 60) % 60\n const hours = Math.floor(totalSeconds / 3600) % 24\n const days = Math.floor(totalSeconds / 86400)\n if (days > 0) return `${days}d${hours}h`\n if (hours > 0) return `${hours}h${String(minutes).padStart(2, \"0\")}m`\n if (minutes > 0) return `${minutes}m${String(seconds).padStart(2, \"0\")}s`\n return `${seconds}s`\n}\n\nexport function formatTokens(value: number): string {\n const n = Math.max(0, Math.round(value))\n if (n < 1_000) return String(n)\n if (n < 999_500) {\n const k = n / 1_000\n return `${trimZeros(k.toFixed(k < 10 ? 2 : 1))}k`\n }\n if (n < 999_500_000) {\n const m = n / 1_000_000\n return `${trimZeros(m.toFixed(m < 10 ? 2 : 1))}M`\n }\n return `${Math.round(n / 1_000_000)}M`\n}\n\nfunction trimZeros(value: string): string {\n return value.includes(\".\") ? value.replace(/0+$/, \"\").replace(/\\.$/, \"\") : value\n}\n\nexport function formatCost(value: number): string {\n if (value <= 0) return \"$0\"\n if (value < 1) return `$${value.toFixed(4)}`\n return `$${value.toFixed(2)}`\n}\n\n/**\n * The buckets the line displays, derived from a totals group. `input` is the\n * *uncached* part of the prompt (fresh input plus cache writes, i.e. what the\n * debug line's `new` counts on the prompt side), `prompt` is the whole prompt\n * of the request and `output` is everything the model generated (visible output\n * + reasoning). OpenCode's own debug line reconciles with these:\n * `new = in + out` and `total = prompt + out`, with `prompt = in + cached`.\n * The stored buckets stay raw; only the display folds.\n */\nexport type DisplayTotals = Totals & {\n /** Whole prompt of the request: fresh input + cached reads + cache writes. */\n prompt: number\n}\n\nexport function displayTotals(totals: Totals): DisplayTotals {\n const prompt = totals.input + totals.cached + totals.cacheWrite\n return {\n input: prompt - totals.cached,\n cached: totals.cached,\n cacheWrite: totals.cacheWrite,\n output: totals.output + totals.reasoning,\n reasoning: totals.reasoning,\n cost: totals.cost,\n steps: totals.steps,\n prompt,\n }\n}\n\n/**\n * The numbers of one totals group. `cost` is passed explicitly because the two\n * callers mean different money: a turn's cost is the sum over its steps\n * whatever view its tokens follow, and money never comes from a \"latest\" step.\n * A zero cost is left out of the line.\n */\nfunction totalsText(prefix: string, totals: Totals, labels: LineLabels, fields: LineFields, stepCount = 0, cost = totals.cost): string {\n const display = displayTotals(totals)\n // The parenthesised value is the whole prompt, shown only when it differs\n // from the uncached part the label leads with.\n const input =\n display.prompt === display.input\n ? `${labels.input} ${formatTokens(display.input)}`\n : `${labels.input} ${formatTokens(display.input)} (${formatTokens(display.prompt)})`\n const parts = [\n input,\n `${labels.cached} ${formatTokens(display.cached)}`,\n `${labels.output} ${formatTokens(display.output)}`,\n ]\n if (fields.reasoning) parts.push(`${labels.reasoning} ${formatTokens(display.reasoning)}`)\n if (fields.cost && cost > 0) parts.push(`${labels.cost} ${formatCost(cost)}`)\n // An empty label (the default for the turn) leaves the numbers unlabelled; a\n // multi-step turn still says how many requests it took.\n const steps = stepCount > 1 ? `${stepCount} ${stepCount === 1 ? labels.step : labels.steps}` : \"\"\n const head = [prefix, steps].filter((piece) => piece !== \"\").join(\" \")\n const numbers = parts.join(\", \")\n if (head === \"\") return numbers\n return stepCount > 1 ? `${head} · ${numbers}` : `${head} ${numbers}`\n}\n\n/**\n * Render the telemetry line as toned segments. The caller maps tones onto\n * theme colors; `formatLine` joins the segments as plain text.\n */\nexport function formatLineSegments(\n stats: TurnStats,\n options?: { labels?: Partial<LineLabels>; fields?: Partial<LineFields>; turnTotals?: TurnTotalsView },\n): LineSegment[] {\n const labels = { ...DEFAULT_LABELS, ...options?.labels }\n const fields = { ...DEFAULT_FIELDS, ...options?.fields }\n const turnTotals = options?.turnTotals ?? \"latest\"\n const segments: LineSegment[] = []\n\n if (fields.clock && stats.endAt !== undefined) {\n segments.push({ text: formatClock(stats.endAt), tone: \"muted\" })\n }\n if (fields.delta && stats.promptGapMs !== undefined) {\n segments.push({ text: `${labels.delta} ${formatDuration(stats.promptGapMs)}`, tone: \"muted\" })\n }\n // The turn's identity is the provider the model ran through and the model\n // itself: the same model can come from different providers, so both are\n // shown, and a provider switch is the change the line warns about.\n if (stats.providerID !== undefined) {\n let text = stats.providerID\n if (fields.model && stats.modelID !== undefined) text += `/${stats.modelID}`\n if (fields.variant && stats.variant) text += ` ${stats.variant}`\n segments.push({ text, tone: stats.providerChanged ? \"warning\" : \"base\" })\n if (stats.providerChanged) segments.push({ text: labels.providerChanged, tone: \"warning\" })\n else if (stats.modelChanged) segments.push({ text: labels.modelChanged, tone: \"muted\" })\n }\n if (fields.turn && stats.totals.steps > 0) {\n const totals = turnTotals === \"sum\" ? stats.totals : stats.latest\n // The tokens follow the view — `latest` is the context the turn ended with\n // — but money is paid per step: the turn's cost is the sum over its steps,\n // never the last step's share of it.\n segments.push({\n text: totalsText(labels.turn, totals, labels, fields, turnTotals === \"sum\" ? stats.totals.steps : stats.steps, stats.totals.cost),\n tone: \"base\",\n })\n }\n if (fields.session && stats.session) {\n segments.push({ text: totalsText(labels.session, stats.session, labels, fields, 0, stats.session.cost), tone: \"muted\" })\n }\n if (fields.cacheExpiry && stats.cacheExpired) {\n const count = stats.drops.length > 1 ? `×${stats.drops.length}` : \"\"\n segments.push({ text: `⚠ ${labels.cacheExpired} (−${formatTokens(stats.cacheDropTokens)}${count})`, tone: \"warning\" })\n }\n return segments\n}\n\nexport function formatLine(\n stats: TurnStats,\n options?: { labels?: Partial<LineLabels>; fields?: Partial<LineFields>; turnTotals?: TurnTotalsView },\n): string {\n return formatLineSegments(stats, options)\n .map((segment) => segment.text)\n .join(\" · \")\n}\n",
|
|
10
|
-
"import type { Database } from \"bun:sqlite\"\nimport { cacheDrop, stepGap, tokensPerSecond, type AssistantMessage } from \"../core\"\n\n/**\n * Read side of the stats engine: every function takes an open database and\n * returns plain rows for the TUI panel, the report script and the NDJSON\n * exports. Aggregations stay in SQL; comparisons and percentiles that would\n * need window functions or custom SQLite builds are computed in JS.\n */\n\n/** Event kind written by the recorder when a compaction completes. */\nexport const COMPACTION_KIND = \"compaction\"\n\n/**\n * Idle gap above which a cache reuse loss is attributed to provider-side\n * expiry rather than to a mid-session event. Heuristic: the empirical TTL\n * evidence view narrows this down with real observations.\n */\nexport const DEFAULT_TTL_HINT_MS = 10 * 60 * 1000\n\nexport type Range = {\n from?: number | undefined\n to?: number | undefined\n}\n\n/**\n * Panel filters. They narrow every aggregation to one model, one provider or\n * one session, and are applied with the same `WHERE` builder the range uses so\n * a filtered view and a drill-down agree.\n */\nexport type Filters = {\n /** Model id, variant excluded. */\n modelID?: string | undefined\n /**\n * One model *and* its variant, as `modelKey` builds it (`m1#max`). A variant\n * is a different model to the provider, so a drill-down into one variant must\n * not pick up its family.\n */\n modelKey?: string | undefined\n /**\n * The provider the model ran through, as OpenCode configured it\n * (`opencode-go`, `deepseek`, `deepinfra`). It is the identity every view\n * groups by: a gateway may re-route the request, but the upstream that\n * actually ran it is not observable.\n */\n provider?: string | undefined\n /**\n * One session, exactly. Used by the `session` range and by the overview's\n * \"this session\" block, which must not pick up the steps of every other\n * session that happened to run after it started.\n */\n sessionID?: string | undefined\n}\n\nexport type Usage = {\n steps: number\n input: number\n cached: number\n cacheWrite: number\n output: number\n reasoning: number\n cost: number\n}\n\ntype UsageRow = {\n steps: number | null\n input: number | null\n cached: number | null\n cacheWrite: number | null\n output: number | null\n reasoning: number | null\n cost: number | null\n}\n\nexport const STEP_TIME = \"COALESCE(created_at, recorded_at)\"\n\n/**\n * The identity of a model *and* its variant, in SQL: `modelKey` says a variant\n * is a different model to the provider, so a variant never shares a row with\n * its family. The JS twin is `modelKey` below; both have to agree.\n */\nexport const MODEL_KEY_SQL = \"COALESCE(model_id, '(unknown)') || '#' || COALESCE(variant, '')\"\n\n/** `modelKey(modelID, variant)`: the family, with the variant as a suffix. */\nexport function modelKey(modelID: string, variant: string | undefined): string {\n return `${modelID}#${variant ?? \"\"}`\n}\n\n/** Human label of a model key or a model id and variant. */\nexport function modelLabel(modelID: string, variant: string | undefined): string {\n return variant === undefined ? modelID : `${modelID} ${variant}`\n}\nconst USAGE_SELECT = `COUNT(*) AS steps,\n COALESCE(SUM(input_tokens), 0) AS input,\n COALESCE(SUM(cache_read), 0) AS cached,\n COALESCE(SUM(cache_write), 0) AS cacheWrite,\n COALESCE(SUM(output_tokens), 0) AS output,\n COALESCE(SUM(reasoning_tokens), 0) AS reasoning,\n COALESCE(SUM(cost), 0) AS cost`\n\nfunction usageFrom(row: UsageRow): Usage {\n return {\n steps: row.steps ?? 0,\n input: row.input ?? 0,\n cached: row.cached ?? 0,\n cacheWrite: row.cacheWrite ?? 0,\n output: row.output ?? 0,\n reasoning: row.reasoning ?? 0,\n cost: row.cost ?? 0,\n }\n}\n\nfunction rangeWhere(range: Range | undefined, column = STEP_TIME): { where: string; params: number[] } {\n const clauses: string[] = []\n const params: number[] = []\n if (range?.from !== undefined) {\n clauses.push(`${column} >= ?`)\n params.push(range.from)\n }\n if (range?.to !== undefined) {\n clauses.push(`${column} < ?`)\n params.push(range.to)\n }\n return { where: clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\", params }\n}\n\n/**\n * `WHERE` for a range plus the panel filters. Shared with the analysis layer so\n * a filtered view and a drill-down agree on what is in scope.\n */\nexport function buildWhere(\n range: Range | undefined,\n filters?: Filters | undefined,\n column = STEP_TIME,\n): { where: string; params: (string | number)[] } {\n const rangeClause = rangeWhere(range, column)\n const clauses = rangeClause.where === \"\" ? [] : [rangeClause.where.slice(\"WHERE \".length)]\n const params: (string | number)[] = [...rangeClause.params]\n if (filters?.modelID !== undefined && filters.modelID !== \"\") {\n clauses.push(\"model_id = ?\")\n params.push(filters.modelID)\n }\n if (filters?.modelKey !== undefined && filters.modelKey !== \"\") {\n clauses.push(`${MODEL_KEY_SQL} = ?`)\n params.push(filters.modelKey)\n }\n if (filters?.provider !== undefined && filters.provider !== \"\") {\n clauses.push(\"provider_id = ?\")\n params.push(filters.provider)\n }\n if (filters?.sessionID !== undefined && filters.sessionID !== \"\") {\n clauses.push(\"session_id = ?\")\n params.push(filters.sessionID)\n }\n return { where: clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\", params }\n}\n\n/**\n * Share of the whole prompt that came from the cache. The denominator is what\n * the provider saw: cache reads, fresh input and cache writes.\n */\nexport function cacheHitRatio(usage: Pick<Usage, \"input\" | \"cached\" | \"cacheWrite\">): number | undefined {\n const total = usage.cached + usage.input + usage.cacheWrite\n return total > 0 ? usage.cached / total : undefined\n}\n\nexport type Overview = Usage & {\n sessions: number\n prompts: number\n firstAt: number | undefined\n lastAt: number | undefined\n cacheHitRatio: number | undefined\n /**\n * Tokens generated per second of streaming, over the whole selection: the\n * produced tokens divided by the summed streaming windows. A weighted average,\n * not the mean of the per-step rates the latency view reports as p50.\n */\n tps: number | undefined\n}\n\nexport function overview(db: Database, range?: Range, filters?: Filters): Overview {\n const steps = buildWhere(range, filters)\n const row = db\n .query(\n `SELECT ${USAGE_SELECT},\n COUNT(DISTINCT session_id) AS sessions,\n MIN(${STEP_TIME}) AS firstAt,\n MAX(COALESCE(completed_at, ${STEP_TIME})) AS lastAt,\n COALESCE(SUM(CASE WHEN streamed_at > created_at THEN output_tokens + reasoning_tokens ELSE 0 END), 0) AS produced,\n COALESCE(SUM(CASE WHEN streamed_at > created_at THEN streamed_at - created_at ELSE 0 END), 0) AS window\n FROM steps ${steps.where}`,\n )\n .get(...steps.params) as\n | (UsageRow & { sessions: number | null; firstAt: number | null; lastAt: number | null; produced: number | null; window: number | null })\n | null\n\n const prompts = rangeWhere(range, \"created_at\")\n const promptRow = db.query(`SELECT COUNT(*) AS prompts FROM prompts ${prompts.where}`).get(...prompts.params) as { prompts: number } | null\n\n const usage = usageFrom(row ?? { steps: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0 })\n return {\n ...usage,\n sessions: row?.sessions ?? 0,\n prompts: promptRow?.prompts ?? 0,\n firstAt: row?.firstAt ?? undefined,\n lastAt: row?.lastAt ?? undefined,\n cacheHitRatio: cacheHitRatio(usage),\n tps: row === null ? undefined : tokensPerSecond(row.produced ?? 0, 0, row.window ?? 0),\n }\n}\n\nexport type ProviderUsage = Usage & {\n providerID: string\n firstAt: number | undefined\n lastAt: number | undefined\n}\n\nexport type ModelUsage = {\n modelID: string\n variant: string | undefined\n providers: ProviderUsage[]\n totals: Usage\n}\n\ntype ModelRow = UsageRow & {\n provider_id: string | null\n model_id: string | null\n variant: string | null\n first_at: number | null\n last_at: number | null\n}\n\n/**\n * Usage per model, each with the providers that served it. This is the view\n * that answers \"through which provider is this model actually coming, over the\n * selected period\".\n */\nexport function models(db: Database, range?: Range, filters?: Filters): ModelUsage[] {\n const filter = buildWhere(range, filters)\n const rows = db\n .query(\n `SELECT provider_id, model_id, variant,\n ${USAGE_SELECT},\n MIN(${STEP_TIME}) AS first_at,\n MAX(COALESCE(completed_at, recorded_at)) AS last_at\n FROM steps ${filter.where}\n GROUP BY provider_id, model_id, variant\n ORDER BY steps DESC, cached DESC`,\n )\n .all(...filter.params) as ModelRow[]\n\n const models = new Map<string, ModelUsage>()\n for (const row of rows) {\n const modelID = row.model_id ?? \"(unknown)\"\n const variant = row.variant ?? undefined\n const key = `${modelID}#${variant ?? \"\"}`\n let model = models.get(key)\n if (!model) {\n model = { modelID, variant, providers: [], totals: emptyUsage() }\n models.set(key, model)\n }\n const usage = usageFrom(row)\n model.providers.push({\n ...usage,\n providerID: row.provider_id ?? \"(unknown)\",\n firstAt: row.first_at ?? undefined,\n lastAt: row.last_at ?? undefined,\n })\n addUsage(model.totals, usage)\n }\n return [...models.values()].sort((a, b) => billedTokens(b.totals) - billedTokens(a.totals))\n}\n\nfunction emptyUsage(): Usage {\n return { steps: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0 }\n}\n\nfunction addUsage(target: Usage, source: Usage): void {\n target.steps += source.steps\n target.input += source.input\n target.cached += source.cached\n target.cacheWrite += source.cacheWrite\n target.output += source.output\n target.reasoning += source.reasoning\n target.cost += source.cost\n}\n\nfunction billedTokens(usage: Pick<Usage, \"input\" | \"cached\" | \"output\">): number {\n return usage.input + usage.cached + usage.output\n}\n\nexport type TimelineBucket = { at: number } & Usage\n\n/** Usage aggregated into fixed time buckets (for example one hour or day). */\nexport function timeline(db: Database, range: Range | undefined, bucketMs: number): TimelineBucket[] {\n const filter = rangeWhere(range)\n const rows = db\n .query(\n `SELECT CAST(${STEP_TIME} / ? AS INTEGER) * ? AS bucket, ${USAGE_SELECT}\n FROM steps ${filter.where}\n GROUP BY bucket\n ORDER BY bucket`,\n )\n .all(bucketMs, bucketMs, ...filter.params) as (UsageRow & { bucket: number })[]\n return rows.map((row) => ({ at: row.bucket, ...usageFrom(row) }))\n}\n\nexport type DropReason = \"compaction\" | \"expired\" | \"unknown\"\n\nexport type CacheDropRow = {\n sessionID: string\n messageID: string\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n at: number\n tokens: number\n gapMs: number | undefined\n prevCached: number\n reason: DropReason\n}\n\nexport type TtlEvidenceRow = {\n providerID: string\n modelID: string\n samples: number\n /** Largest idle gap that still hit the cache. */\n maxHitGapMs: number | undefined\n /** Smallest idle gap after which reuse was lost. */\n minDropGapMs: number | undefined\n}\n\nexport type LatencyStats = {\n count: number\n p50: number | undefined\n p90: number | undefined\n p99: number | undefined\n}\n\nexport type LatencyReport = {\n /** Milliseconds from request to completed response. */\n wall: LatencyStats\n /** Milliseconds from request to the last streamed chunk (the streaming window). */\n stream: LatencyStats\n /** Output-plus-reasoning tokens per second over that window. */\n tps: LatencyStats\n}\n\n/** Nearest-rank percentile over an unsorted sample. */\nexport function percentile(values: readonly number[], p: number): number | undefined {\n if (values.length === 0) return undefined\n const sorted = [...values].sort((a, b) => a - b)\n const rank = Math.ceil((p / 100) * sorted.length)\n const index = Math.min(sorted.length - 1, Math.max(0, rank - 1))\n return sorted[index]\n}\n\nexport function latencyStats(values: number[]): LatencyStats {\n return {\n count: values.length,\n p50: percentile(values, 50),\n p90: percentile(values, 90),\n p99: percentile(values, 99),\n }\n}\n\nconst statsFor = latencyStats\n\nexport type SessionUsage = Usage & {\n sessionID: string\n parentID: string | undefined\n title: string | undefined\n directory: string | undefined\n providers: string[]\n models: string[]\n prompts: number\n firstAt: number | undefined\n lastAt: number | undefined\n cacheHitRatio: number | undefined\n}\n\nexport function sessions(\n db: Database,\n options?: { range?: Range; filters?: Filters; limit?: number; sessionIDs?: readonly string[] },\n): SessionUsage[] {\n if (options?.sessionIDs !== undefined && options.sessionIDs.length === 0) return []\n const range = options?.range\n const filter = buildWhere(range, options?.filters, \"COALESCE(st.created_at, st.recorded_at)\")\n const limit = options?.limit ?? 20\n const clauses = filter.where === \"\" ? [] : [filter.where.slice(\"WHERE \".length)]\n const params: (number | string)[] = [...filter.params]\n if (options?.sessionIDs !== undefined) {\n // A specific set of sessions (the subagents of one session): the group by\n // then reads one index range per session instead of every step recorded.\n const ids = options.sessionIDs.slice(0, 500)\n clauses.push(`st.session_id IN (${ids.map(() => \"?\").join(\", \")})`)\n params.push(...ids)\n }\n const rows = db\n .query(\n `SELECT st.session_id, s.title, s.directory, s.parent_id,\n GROUP_CONCAT(DISTINCT st.provider_id) AS providers,\n GROUP_CONCAT(DISTINCT st.model_id) AS models,\n (SELECT COUNT(*) FROM prompts p WHERE p.session_id = st.session_id) AS prompts,\n COUNT(*) AS steps,\n COALESCE(SUM(st.input_tokens), 0) AS input,\n COALESCE(SUM(st.cache_read), 0) AS cached,\n COALESCE(SUM(st.cache_write), 0) AS cacheWrite,\n COALESCE(SUM(st.output_tokens), 0) AS output,\n COALESCE(SUM(st.reasoning_tokens), 0) AS reasoning,\n COALESCE(SUM(st.cost), 0) AS cost,\n MIN(COALESCE(st.created_at, st.recorded_at)) AS first_at,\n MAX(COALESCE(st.completed_at, st.created_at, st.recorded_at)) AS last_at\n FROM steps st\n LEFT JOIN sessions s ON s.session_id = st.session_id\n ${clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\"}\n GROUP BY st.session_id\n ORDER BY last_at DESC\n LIMIT ?`,\n )\n .all(\n ...params,\n limit,\n ) as (UsageRow & {\n session_id: string\n title: string | null\n directory: string | null\n parent_id: string | null\n providers: string | null\n models: string | null\n prompts: number | null\n first_at: number | null\n last_at: number | null\n })[]\n\n return rows.map((row) => {\n const usage = usageFrom(row)\n return {\n ...usage,\n sessionID: row.session_id,\n parentID: row.parent_id ?? undefined,\n title: row.title ?? undefined,\n directory: row.directory ?? undefined,\n providers: row.providers ? row.providers.split(\",\").filter(Boolean) : [],\n models: row.models ? row.models.split(\",\").filter(Boolean) : [],\n prompts: row.prompts ?? 0,\n firstAt: row.first_at ?? undefined,\n lastAt: row.last_at ?? undefined,\n cacheHitRatio: cacheHitRatio(usage),\n }\n })\n}\n\n/** Sessions whose parent is `parentID`: the subagents of one session. */\nexport function childSessionIDs(db: Database, parentID: string): string[] {\n return (db.query(\"SELECT session_id FROM sessions WHERE parent_id = ? ORDER BY session_id\").all(parentID) as { session_id: string }[]).map(\n (row) => row.session_id,\n )\n}\n\n/** Session usage rows for a specific set of sessions (subagent rollups). */\nexport function sessionUsageOf(db: Database, sessionIDs: readonly string[]): SessionUsage[] {\n return sessions(db, { sessionIDs, limit: sessionIDs.length })\n}\n\nexport type TurnDrop = {\n messageID: string\n tokens: number\n gapMs: number | undefined\n}\n\n/**\n * One finished turn of a session, built from the recorded steps and prompts. It\n * carries the same buckets the telemetry line uses, so the panel can render the\n * per-prompt history with the line's own formatter.\n */\nexport type TurnUsage = {\n sessionID: string\n /** Creation time of the prompt that opened the turn, when it was recorded. */\n promptAt: number | undefined\n endAt: number | undefined\n /** Time between the previous prompt of the session and this turn's prompt. */\n promptGapMs: number | undefined\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n providerChanged: boolean\n modelChanged: boolean\n steps: number\n /** Raw buckets of the last step: the prompt the turn ended with. */\n latest: Usage\n /** Raw buckets summed over every step of the turn. */\n totals: Usage\n drops: TurnDrop[]\n /** Finish reason of the turn's last step. */\n finish: string | undefined\n /**\n * Tokens generated per second of streaming over the turn's steps: produced\n * tokens divided by their summed streaming windows. For a one-step turn it is\n * that step's rate.\n */\n tps: number | undefined\n}\n\ntype TurnStepRow = {\n message_id: string\n session_id: string\n provider_id: string | null\n model_id: string | null\n variant: string | null\n input_tokens: number | null\n cache_read: number | null\n cache_write: number | null\n output_tokens: number | null\n reasoning_tokens: number | null\n cost: number | null\n created_at: number | null\n streamed_at: number | null\n recorded_at: number\n completed_at: number | null\n finish: string | null\n}\n\n/** A turn is finished when its last step ended without asking for more tools. */\nfunction turnFinished(finish: string | null): boolean {\n return finish !== null && finish !== \"\" && finish !== \"tool-calls\"\n}\n\nfunction stepUsage(row: TurnStepRow): Usage {\n return {\n steps: 1,\n input: row.input_tokens ?? 0,\n cached: row.cache_read ?? 0,\n cacheWrite: row.cache_write ?? 0,\n output: row.output_tokens ?? 0,\n reasoning: row.reasoning_tokens ?? 0,\n cost: row.cost ?? 0,\n }\n}\n\n/** Structural view of a recorded step, so the shared cache-drop rule applies. */\nfunction stepMessage(row: TurnStepRow): AssistantMessage {\n return {\n type: \"assistant\",\n id: row.message_id,\n time: { created: row.created_at ?? row.recorded_at, completed: row.completed_at ?? undefined },\n model:\n row.provider_id !== null && row.model_id !== null\n ? { providerID: row.provider_id, id: row.model_id, variant: row.variant ?? undefined }\n : undefined,\n tokens: {\n input: row.input_tokens ?? 0,\n output: row.output_tokens ?? 0,\n reasoning: row.reasoning_tokens ?? 0,\n cache: { read: row.cache_read ?? 0, write: row.cache_write ?? 0 },\n },\n finish: row.finish ?? undefined,\n }\n}\n\n/**\n * Finished turns, newest first. Steps belong to the newest prompt recorded at or\n * before them; a turn whose last step asked for tools is dropped, because it is\n * still running — the same rule the telemetry line follows.\n *\n * For one session the read is bounded by a window: only the steps at or after\n * the `limit`-th newest prompt are loaded, so a session with tens of thousands\n * of steps costs the same as a short one. The extra prompt is kept as the\n * reference the oldest returned turn measures its gap against.\n */\nexport function turns(\n db: Database,\n options?: { sessionID?: string | undefined; range?: Range | undefined; filters?: Filters | undefined; limit?: number | undefined },\n): TurnUsage[] {\n const limit = options?.limit ?? 50\n const filter = buildWhere(options?.range, options?.filters)\n const clauses = filter.where === \"\" ? [] : [filter.where.slice(\"WHERE \".length)]\n const params: (number | string)[] = [...filter.params]\n if (options?.sessionID !== undefined) {\n clauses.push(\"session_id = ?\")\n params.push(options.sessionID)\n }\n const floor = options?.sessionID === undefined ? undefined : promptFloor(db, options.sessionID, limit + 1)\n if (floor !== undefined) {\n clauses.push(`${STEP_TIME} >= ?`)\n params.push(floor)\n }\n const steps = db\n .query(\n `SELECT message_id, session_id, provider_id, model_id, variant,\n input_tokens, cache_read, cache_write, output_tokens, reasoning_tokens, cost,\n created_at, streamed_at, recorded_at, completed_at, finish\n FROM steps ${clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\"}\n ORDER BY session_id, ${STEP_TIME}`,\n )\n .all(...params) as TurnStepRow[]\n\n const promptClause = options?.sessionID === undefined ? \"\" : `WHERE session_id = ?${floor === undefined ? \"\" : \" AND created_at >= ?\"}`\n const promptParams = options?.sessionID === undefined ? [] : floor === undefined ? [options.sessionID] : [options.sessionID, floor]\n const promptRows = db\n .query(`SELECT session_id, created_at FROM prompts ${promptClause} ORDER BY session_id, created_at`)\n .all(...promptParams) as { session_id: string; created_at: number | null }[]\n\n const promptsBySession = new Map<string, number[]>()\n for (const row of promptRows) {\n if (row.created_at === null) continue\n const list = promptsBySession.get(row.session_id) ?? []\n list.push(row.created_at)\n promptsBySession.set(row.session_id, list)\n }\n\n const result: TurnUsage[] = []\n let sessionID: string | undefined\n let bucket: TurnStepRow[] = []\n let promptIndex = -2\n let promptAt: number | undefined\n let previousPromptAt: number | undefined\n\n function close(): void {\n const last = bucket[bucket.length - 1]\n if (last === undefined || !turnFinished(last.finish)) {\n bucket = []\n return\n }\n const totals = emptyUsage()\n for (const row of bucket) addUsage(totals, stepUsage(row))\n const drops: TurnDrop[] = []\n let reference: AssistantMessage | undefined\n for (const row of bucket) {\n const message = stepMessage(row)\n const drop = cacheDrop(reference, message)\n if (drop !== undefined) {\n drops.push({ messageID: row.message_id, tokens: drop, gapMs: stepGap(reference, message) })\n }\n reference = message\n }\n const providerID = last.provider_id ?? undefined\n const modelID = last.model_id ?? undefined\n const variant = last.variant ?? undefined\n let produced = 0\n let window = 0\n for (const row of bucket) {\n if (row.created_at === null || row.streamed_at === null || row.streamed_at <= row.created_at) continue\n produced += (row.output_tokens ?? 0) + (row.reasoning_tokens ?? 0)\n window += row.streamed_at - row.created_at\n }\n const previous = result.length > 0 && result[result.length - 1]!.sessionID === last.session_id ? result[result.length - 1] : undefined\n result.push({\n sessionID: last.session_id,\n promptAt,\n endAt: last.completed_at ?? last.created_at ?? last.recorded_at,\n promptGapMs: promptAt !== undefined && previousPromptAt !== undefined ? Math.max(0, promptAt - previousPromptAt) : undefined,\n providerID,\n modelID,\n variant,\n providerChanged: previous?.providerID !== undefined && providerID !== undefined && previous.providerID !== providerID,\n modelChanged:\n previous !== undefined &&\n previous.providerID === providerID &&\n `${previous.modelID ?? \"\"}#${previous.variant ?? \"\"}` !== `${modelID ?? \"\"}#${variant ?? \"\"}`,\n steps: bucket.length,\n latest: stepUsage(last),\n totals,\n drops,\n finish: last.finish ?? undefined,\n tps: tokensPerSecond(produced, 0, window),\n })\n bucket = []\n }\n\n for (const row of steps) {\n if (row.session_id !== sessionID) {\n close()\n sessionID = row.session_id\n promptIndex = -2\n promptAt = undefined\n previousPromptAt = undefined\n }\n const time = row.created_at ?? row.recorded_at\n const prompts = promptsBySession.get(row.session_id) ?? []\n let index = -1\n for (let candidate = prompts.length - 1; candidate >= 0; candidate -= 1) {\n if ((prompts[candidate] ?? Number.POSITIVE_INFINITY) <= time) {\n index = candidate\n break\n }\n }\n if (index !== promptIndex) {\n close()\n promptIndex = index\n promptAt = index >= 0 ? prompts[index] : undefined\n previousPromptAt = index >= 1 ? prompts[index - 1] : undefined\n }\n bucket.push(row)\n }\n close()\n\n return result.reverse().slice(0, limit)\n}\n\n/**\n * Creation time of the `count`-th newest prompt of a session, or undefined\n * when it has fewer. Used to bound the steps a `turns` read has to load.\n */\nfunction promptFloor(db: Database, sessionID: string, count: number): number | undefined {\n const rows = db\n .query(\"SELECT created_at FROM prompts WHERE session_id = ? AND created_at IS NOT NULL ORDER BY created_at DESC LIMIT ?\")\n .all(sessionID, count) as { created_at: number }[]\n return rows.length === count ? rows[count - 1]!.created_at : undefined\n}\n\n// ---------------------------------------------------------------------------\n// Filter pickers\n// ---------------------------------------------------------------------------\n\nexport type FilterOption = { value: string; steps: number }\n\n/** Distinct models and providers, most used first. */\nexport function filterOptions(db: Database): { models: FilterOption[]; providers: FilterOption[] } {\n const pick = (column: string): FilterOption[] =>\n (\n db\n .query(`SELECT ${column} AS value, COUNT(*) AS steps FROM steps WHERE ${column} IS NOT NULL GROUP BY ${column} ORDER BY steps DESC`)\n .all() as { value: string; steps: number }[]\n ).map((row) => ({ value: row.value, steps: row.steps }))\n return { models: pick(\"model_id\"), providers: pick(\"provider_id\") }\n}\n",
|
|
11
|
-
"import type { Database } from \"bun:sqlite\"\nimport { recordEvent, recordPrompt, recordStep, upsertSession } from \"./stats/db\"\nimport { COMPACTION_KIND } from \"./stats/queries\"\n\n/**\n * Event-driven recorder. Every handler is idempotent and defensive: the\n * plugin must never break a session because an event arrived in an\n * unexpected shape, so unknown events are ignored and malformed payloads are\n * dropped rather than thrown.\n *\n * Everything written comes from the event stream: the step's provider, model\n * and variant, its tokens, its timing and its outcome. Pure with respect to\n * its inputs (database plus events) so the whole recording policy can be\n * unit-tested without the plugin runtime.\n */\n\nexport type SessionModelRef = {\n id: string\n providerID: string\n variant?: string | undefined\n}\n\nexport type TokenUsageLike = {\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cache?: { read?: number | undefined; write?: number | undefined } | undefined\n}\n\nexport type RecorderEventData = {\n sessionID?: string | undefined\n assistantMessageID?: string | undefined\n inboxID?: string | undefined\n item?: { type?: string | undefined } | undefined\n started?: number | undefined\n model?: SessionModelRef | undefined\n previous?: SessionModelRef | undefined\n tokens?: TokenUsageLike | undefined\n cost?: number | undefined\n finish?: string | undefined\n rawFinish?: string | undefined\n reason?: string | undefined\n location?: { directory?: string | undefined } | undefined\n parentID?: string | undefined\n title?: string | undefined\n}\n\nexport type RecorderEvent = {\n type: string\n created?: number | undefined\n data?: RecorderEventData | undefined\n}\n\nexport type PendingStep = {\n messageID: string\n sessionID: string\n started: number | undefined\n streamed: number | undefined\n model: SessionModelRef | undefined\n}\n\nexport type RecorderOptions = {\n /** Clock override, for tests. */\n now?: (() => number) | undefined\n}\n\nexport type Recorder = {\n /** Apply one session event. Unknown or malformed events are ignored. */\n handleEvent(event: RecorderEvent): void\n pendingFor(sessionID: string): PendingStep | undefined\n}\n\nexport function createRecorder(db: Database, options?: RecorderOptions): Recorder {\n /**\n * Ceiling on the per-session maps. A session that ends takes its entries\n * with it; a host running for weeks has thousands of sessions, and a map that\n * only ever grows is a leak with a nice name.\n */\n const MAX_TRACKED_SESSIONS = 512\n /** Ceiling on in-flight steps; a step that never ends is a leak too. */\n const MAX_PENDING = 256\n\n /** Steps in flight between `step.started` and `step.ended`. */\n const pending = new Map<string, PendingStep>()\n /** Last model selected per session, used when a step starts unobserved. */\n const lastModel = new Map<string, SessionModelRef>()\n const now = options?.now ?? (() => Date.now())\n\n /**\n * Ceiling on the writes kept while the database is busy. A maintenance\n * operation (a prune, a vacuum) holds the write lock; waiting for it would\n * stall the agent, and dropping the record would lose usage. The recorder\n * defers instead, retrying on the next event, and only a lock held for longer\n * than this many records takes a record with it.\n */\n const MAX_DEFERRED = 256\n const deferred: Array<() => void> = []\n\n /** True when the database was locked rather than the statement being wrong. */\n function isBusy(error: unknown): boolean {\n const code = (error as { code?: unknown } | null | undefined)?.code\n return code === \"SQLITE_BUSY\" || code === \"SQLITE_LOCKED\"\n }\n\n function enqueue(run: () => void): void {\n if (deferred.length >= MAX_DEFERRED) deferred.shift()\n deferred.push(run)\n }\n\n /** Retry what a busy database postponed, oldest first, stopping at the first failure. */\n function flushDeferred(): void {\n while (deferred.length > 0) {\n const next = deferred[0]!\n try {\n next()\n } catch {\n return\n }\n deferred.shift()\n }\n }\n\n /**\n * Run one write. On a busy database the work is kept for the next event\n * instead of waiting for the lock: the recorder is on the agent's path, and\n * the record is worth more late than the agent is worth stalled.\n */\n function write(run: () => void): void {\n if (deferred.length > 0) {\n flushDeferred()\n // Still locked: this write has to wait behind the queued ones.\n if (deferred.length > 0) {\n enqueue(run)\n return\n }\n }\n try {\n run()\n } catch (error) {\n if (isBusy(error)) enqueue(run)\n // Anything else is swallowed, as before: telemetry never breaks a session.\n }\n }\n\n /** Insertion-ordered map with a size ceiling: the oldest entry goes first. */\n function remember<Value>(map: Map<string, Value>, key: string, value: Value, limit: number): void {\n map.delete(key)\n map.set(key, value)\n while (map.size > limit) {\n const oldest = map.keys().next()\n if (oldest.done === true) break\n map.delete(oldest.value)\n }\n }\n\n /** Drop everything remembered about a session that is gone. */\n function forget(sessionID: string): void {\n lastModel.delete(sessionID)\n for (const [messageID, step] of pending) {\n if (step.sessionID === sessionID) pending.delete(messageID)\n }\n }\n\n function recordPendingStep(messageID: string, data: RecorderEventData, finish: string | undefined, at: number): void {\n const sessionID = data.sessionID\n if (typeof sessionID !== \"string\") return\n const info = pending.get(messageID)\n pending.delete(messageID)\n const model = info?.model ?? lastModel.get(sessionID)\n write(() =>\n recordStep(db, {\n messageID,\n sessionID,\n providerID: model?.providerID,\n modelID: model?.id,\n variant: model?.variant,\n input: data.tokens?.input,\n output: data.tokens?.output,\n reasoning: data.tokens?.reasoning,\n cacheRead: data.tokens?.cache?.read,\n cacheWrite: data.tokens?.cache?.write,\n cost: data.cost,\n createdAt: info?.started,\n streamedAt: info?.streamed,\n completedAt: at,\n finish,\n recordedAt: now(),\n }),\n )\n }\n\n function handleEvent(event: RecorderEvent): void {\n // A previous write may have been postponed: this is the cheapest moment to\n // land it, and the check costs nothing while the queue is empty.\n if (deferred.length > 0) flushDeferred()\n const data = event.data ?? {}\n const at = typeof event.created === \"number\" ? event.created : now()\n const sessionID = typeof data.sessionID === \"string\" ? data.sessionID : undefined\n\n switch (event.type) {\n case \"session.created\": {\n if (sessionID === undefined) return\n write(() =>\n upsertSession(db, {\n sessionID,\n parentID: data.parentID,\n directory: data.location?.directory,\n title: data.title,\n startedAt: at,\n updatedAt: at,\n }),\n )\n if (data.model !== undefined) remember(lastModel, sessionID, data.model, MAX_TRACKED_SESSIONS)\n return\n }\n case \"session.deleted\": {\n // The session is gone: its in-flight steps will never end and nothing\n // else about it will be needed again.\n if (sessionID !== undefined) forget(sessionID)\n return\n }\n case \"session.renamed\": {\n if (sessionID === undefined) return\n write(() => upsertSession(db, { sessionID, title: data.title, updatedAt: at }))\n return\n }\n case \"session.moved\": {\n if (sessionID === undefined) return\n write(() => upsertSession(db, { sessionID, directory: data.location?.directory, updatedAt: at }))\n return\n }\n case \"session.inbox.enqueued\": {\n if (sessionID === undefined) return\n if (data.item?.type !== \"user\" && data.item?.type !== \"synthetic\") return\n // Narrowed into a local: a closure does not keep a property's narrowing.\n const inboxID = data.inboxID\n if (typeof inboxID !== \"string\") return\n write(() => recordPrompt(db, { sessionID, messageID: inboxID, createdAt: at }))\n return\n }\n case \"session.model.selected\": {\n // The session's current model, remembered so a step that started before\n // the plugin loaded is still attributed. Nothing is written for it: no\n // view reads a model-selection event.\n if (sessionID !== undefined && data.model !== undefined) remember(lastModel, sessionID, data.model, MAX_TRACKED_SESSIONS)\n return\n }\n case \"session.step.started\": {\n if (sessionID === undefined || typeof data.assistantMessageID !== \"string\") return\n remember(\n pending,\n data.assistantMessageID,\n {\n messageID: data.assistantMessageID,\n sessionID,\n started: typeof data.started === \"number\" ? data.started : undefined,\n streamed: undefined,\n model: data.model,\n },\n MAX_PENDING,\n )\n return\n }\n case \"session.step.streamed\": {\n if (typeof data.assistantMessageID !== \"string\") return\n const info = pending.get(data.assistantMessageID)\n if (info !== undefined) info.streamed = at\n return\n }\n case \"session.step.ended\": {\n if (typeof data.assistantMessageID !== \"string\") return\n recordPendingStep(data.assistantMessageID, data, data.finish ?? \"unknown\", at)\n return\n }\n case \"session.step.failed\": {\n if (typeof data.assistantMessageID !== \"string\") return\n // The step row keeps the outcome through `finish`; the error event is\n // not written because no view reads it.\n recordPendingStep(data.assistantMessageID, data, data.finish ?? \"error\", at)\n return\n }\n case \"session.compaction.ended\": {\n if (sessionID === undefined) return\n write(() =>\n recordEvent(db, {\n sessionID,\n kind: COMPACTION_KIND,\n at,\n data: { reason: data.reason, model: data.model, tokens: data.tokens, cost: data.cost },\n }),\n )\n return\n }\n default:\n return\n }\n }\n\n function pendingFor(sessionID: string): PendingStep | undefined {\n let found: PendingStep | undefined\n for (const item of pending.values()) {\n if (item.sessionID !== sessionID) continue\n if (found === undefined || (item.started ?? 0) >= (found.started ?? 0)) found = item\n }\n return found\n }\n\n return { handleEvent, pendingFor }\n}\n"
|
|
10
|
+
"/**\n * Pure turn accounting shared by the TUI line, the server recorder and the\n * stats engine.\n *\n * Everything here operates on structural shapes that mirror the session\n * messages published by OpenCode, so the logic stays testable without a host\n * runtime and without the effect-based schema types.\n */\n\nexport type ModelRefLike = {\n providerID: string\n id: string\n variant?: string | undefined\n}\n\nexport type AssistantMessage = {\n type: \"assistant\"\n id: string\n time?: { created?: number | undefined; streamed?: number | undefined; completed?: number | undefined } | undefined\n model?: ModelRefLike | undefined\n tokens?:\n | {\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cache?: { read?: number | undefined; write?: number | undefined } | undefined\n }\n | undefined\n cost?: number | undefined\n /** List-price estimate for a step the host reported as costing nothing. */\n listCost?: number | undefined\n finish?: string | null | undefined\n rawFinish?: string | null | undefined\n}\n\nexport type PromptMessage = {\n type: \"user\" | \"synthetic\" | \"system\"\n id?: string | undefined\n time?: { created?: number | undefined } | undefined\n}\n\nexport type TranscriptMessage =\n | AssistantMessage\n | PromptMessage\n | { type: string; id?: string | undefined; time?: { created?: number | undefined } | undefined }\n\n/** Accumulated token usage for a group of assistant steps. */\nexport type Totals = {\n input: number\n cached: number\n output: number\n reasoning: number\n cacheWrite: number\n cost: number\n /** Sum of list-price estimates for steps the host reported as $0. */\n listCost: number\n steps: number\n}\n\nexport type CacheDrop = {\n messageID: string\n /** Tokens that were expected to hit the prompt cache but did not. */\n tokens: number\n /** `cache.read` of the step that was used as the reuse reference. */\n prevCached: number\n /** Time between the reference step's completion and this step's request. */\n gapMs: number | undefined\n}\n\nexport type TurnStats = {\n /** Wall clock time of the last step's completion. */\n endAt: number | undefined\n /** Creation time of the first settled step of the turn. */\n startAt: number | undefined\n /** Creation time of the prompt that opened this turn. */\n promptAt: number | undefined\n /** Time between the previous prompt and this turn's prompt. */\n promptGapMs: number | undefined\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n /** Provider ID differs from the previous turn in the same transcript. */\n providerChanged: boolean\n /** Provider is the same but the model or variant differs. */\n modelChanged: boolean\n totals: Totals\n /** Passed through from the caller (session-wide accumulated usage). */\n session: Totals | undefined\n /**\n * Prompt-side usage of the last finished step: the context the turn ended\n * with. Summing `input`/`cached` over the steps of a turn counts the same\n * re-sent prompt once per step, which is not what a context view means.\n */\n latest: Totals\n drops: CacheDrop[]\n cacheDropTokens: number\n cacheExpired: boolean\n /** Assistant steps in the turn. */\n steps: number\n}\n\nexport type TurnSegment = {\n assistants: AssistantMessage[]\n prompt: TranscriptMessage | undefined\n}\n\n/** Message types that open a turn: a run of assistants belongs to the last one. */\nexport const PROMPT_BOUNDARIES: ReadonlySet<string> = new Set([\"user\", \"synthetic\", \"system\"])\n\n/**\n * Split a transcript into turns. A turn is a run of assistant steps opened by\n * a prompt boundary (`user`, `synthetic`, `system`). Idle notices, info\n * messages, compactions and shell entries keep the current turn open so a\n * single execution is never split in two.\n */\nexport function transcriptSegments(messages: readonly TranscriptMessage[]): TurnSegment[] {\n const segments: TurnSegment[] = []\n let current: AssistantMessage[] = []\n let prompt: TranscriptMessage | undefined\n\n for (const message of messages) {\n if (message.type === \"assistant\") {\n current.push(message as AssistantMessage)\n continue\n }\n if (!PROMPT_BOUNDARIES.has(message.type)) continue\n if (current.length > 0) {\n segments.push({ assistants: current, prompt })\n current = []\n }\n prompt = message\n }\n if (current.length > 0) segments.push({ assistants: current, prompt })\n return segments\n}\n\n/**\n * The last `count` segments, found by walking the transcript backwards. The\n * live line only needs the turn that just ended and the one before it, and a\n * long session holds tens of thousands of messages: the tail walk stops as\n * soon as it has them, instead of building a segment for every turn.\n */\nexport function transcriptTail(messages: readonly TranscriptMessage[], count: number): TurnSegment[] {\n const segments: TurnSegment[] = []\n let current: AssistantMessage[] = []\n\n for (let index = messages.length - 1; index >= 0; index -= 1) {\n const message = messages[index]!\n if (message.type === \"assistant\") {\n current.unshift(message as AssistantMessage)\n continue\n }\n if (!PROMPT_BOUNDARIES.has(message.type)) continue\n if (current.length === 0) continue\n // The boundary closes the run collected after it.\n segments.unshift({ assistants: current, prompt: message })\n if (segments.length >= count) return segments\n current = []\n }\n // A leading run with no prompt before it is a segment of its own.\n if (current.length > 0 && segments.length < count) segments.unshift({ assistants: current, prompt: undefined })\n return segments\n}\n\n/**\n * A token count or a timestamp read from a host message: `undefined` for a\n * timestamp that is not a finite number, zero for a count that is not. The\n * recorder applies the same rule before SQLite; doing it here keeps the live\n * line on the same footing when a payload is malformed.\n */\nfunction countOf(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0\n}\n\nfunction amountOf(value: unknown): number {\n return typeof value === \"number\" && Number.isFinite(value) ? Math.max(0, value) : 0\n}\n\nfunction stampOf(value: unknown): number | undefined {\n return typeof value === \"number\" && Number.isFinite(value) ? value : undefined\n}\n\nexport function tokenCounts(message: AssistantMessage): { input: number; output: number; reasoning: number; read: number; write: number } {\n const tokens = message.tokens\n return {\n input: countOf(tokens?.input),\n output: countOf(tokens?.output),\n reasoning: countOf(tokens?.reasoning),\n read: countOf(tokens?.cache?.read),\n write: countOf(tokens?.cache?.write),\n }\n}\n\nexport function totalize(steps: readonly AssistantMessage[]): Totals {\n const totals: Totals = { input: 0, cached: 0, output: 0, reasoning: 0, cacheWrite: 0, cost: 0, listCost: 0, steps: steps.length }\n for (const step of steps) {\n const counts = tokenCounts(step)\n totals.input += counts.input\n totals.cached += counts.read\n totals.output += counts.output\n totals.reasoning += counts.reasoning\n totals.cacheWrite += counts.write\n totals.cost += amountOf(step.cost)\n totals.listCost += amountOf(step.listCost)\n }\n return totals\n}\n\n/**\n * The identity of a model reference: the provider it runs through, the model\n * and its variant. Two steps with the same key ran the same thing the same way,\n * which is what `modelChanged` compares.\n */\nexport function identityKey(model: ModelRefLike | undefined): string | undefined {\n if (!model) return undefined\n return `${model.providerID}/${model.id}${model.variant ? `#${model.variant}` : \"\"}`\n}\n\nexport function cacheRead(message: AssistantMessage | undefined): number {\n return countOf(message?.tokens?.cache?.read)\n}\n\nexport function stepCreated(message: AssistantMessage | undefined): number | undefined {\n return stampOf(message?.time?.created)\n}\n\nexport function stepEnd(message: AssistantMessage | undefined): number | undefined {\n const time = message?.time\n if (!time) return undefined\n return stampOf(time.completed) ?? stampOf(time.streamed) ?? stampOf(time.created)\n}\n\n/**\n * Which identity the two steps must share for a reuse loss to be measurable.\n * `provider` is the strict one: two providers are two caches, so a switch\n * between them is not a loss but a different cache. `model` drops that test,\n * for the reports that measure what an identity change costs: forfeiting the\n * previous provider's cache is exactly the cost they report.\n */\nexport type CacheDropScope = \"provider\" | \"model\"\n\n/**\n * Cache reuse loss against a reference step, mirroring OpenCode's own\n * `turn-usage` heuristic: only comparable when the model is identical, and\n * OpenAI's API reports small reads that are not worth flagging.\n */\nexport function cacheDrop(reference: AssistantMessage | undefined, current: AssistantMessage, scope: CacheDropScope = \"provider\"): number | undefined {\n if (!reference) return undefined\n const before = reference.model\n const after = current.model\n if (!before || !after) return undefined\n if (scope === \"provider\" && before.providerID !== after.providerID) return undefined\n if (before.id !== after.id || (before.variant ?? \"\") !== (after.variant ?? \"\")) {\n return undefined\n }\n const drop = cacheRead(reference) - cacheRead(current)\n if (isNoiseDrop(after.providerID, drop)) return undefined\n return drop > 0 ? drop : undefined\n}\n\n/** Milliseconds between a reference step's completion and the next request. */\nexport function stepGap(reference: AssistantMessage | undefined, current: AssistantMessage): number | undefined {\n const ended = stepEnd(reference)\n const started = stepCreated(current)\n if (ended === undefined || started === undefined) return undefined\n return Math.max(0, started - ended)\n}\n\n/**\n * Streaming duration of a step: request start to the last streamed chunk. The\n * host's `streamed` stamp marks the end of streaming, so this is the window a\n * generation rate belongs to — not a time to first token, which the session\n * messages do not record.\n */\nexport function stepStreamMs(message: AssistantMessage): number | undefined {\n const created = stampOf(message.time?.created)\n const streamed = stampOf(message.time?.streamed)\n if (created === undefined || streamed === undefined || streamed <= created) return undefined\n return streamed - created\n}\n\n/**\n * Tokens produced per second over a window. For a step the window is the\n * streaming one: request start to the last streamed chunk (`created` →\n * `streamed`) — `completed` marks the end of the step, which for a tool-call\n * step happens after the tool calls were handled. Returns undefined when the\n * window or the produced tokens are unknown. Single definition, shared by the\n * recorder (which stores it per step) and the latency view.\n */\nexport function tokensPerSecond(produced: number, from: number | undefined, to: number | undefined): number | undefined {\n if (from === undefined || to === undefined || !Number.isFinite(from) || !Number.isFinite(to) || to <= from) return undefined\n if (!Number.isFinite(produced) || produced <= 0) return undefined\n return (produced / (to - from)) * 1000\n}\n\n\n/** Output-plus-reasoning tokens per second of a step. */\nexport function stepTps(message: AssistantMessage): number | undefined {\n const counts = tokenCounts(message)\n return tokensPerSecond(counts.output + counts.reasoning, message.time?.created, message.time?.streamed)\n}\n\nconst DROP_EXEMPT_PROVIDERS = new Set([\"openai\"])\n\n/**\n * True when a step has final numbers. A step that is still streaming reports\n * partial tokens (usually zero) and no cache read yet, so reading it would\n * render an empty line and invent a cache-expiry warning against it.\n */\nexport function isSettledStep(message: AssistantMessage): boolean {\n if (message.time?.completed !== undefined) return true\n return message.finish !== undefined && message.finish !== null\n}\n\n/**\n * Compute the stats of the most recent turn in a transcript, including the\n * comparison against the previous turn (a provider or model switch, and cache\n * reuse).\n *\n * Returns undefined while the newest turn is still being generated: the line\n * appears when a turn ends, never with the zeroes of an in-flight response.\n */\nexport function computeTurn(messages: readonly TranscriptMessage[], session?: Totals): TurnStats | undefined {\n const segments = transcriptTail(messages, 2)\n const last = segments[segments.length - 1]\n if (!last) return undefined\n const previous = segments[segments.length - 2]\n const lastStep = last.assistants[last.assistants.length - 1]\n if (!lastStep) return undefined\n if (!isSettledStep(lastStep)) return undefined\n const previousStep = previous?.assistants[previous.assistants.length - 1]\n // Defensive: a step that never settled mid-turn carries no usable numbers,\n // so it is neither a cache reference nor a reuse candidate.\n const settled = last.assistants.filter(isSettledStep)\n\n const drops: CacheDrop[] = []\n let reference: AssistantMessage | undefined = previousStep !== undefined && isSettledStep(previousStep) ? previousStep : undefined\n for (const step of settled) {\n const drop = cacheDrop(reference, step)\n if (drop !== undefined) {\n drops.push({\n messageID: step.id,\n tokens: drop,\n prevCached: cacheRead(reference),\n gapMs: stepGap(reference, step),\n })\n }\n reference = step\n }\n\n const provider = lastStep.model?.providerID\n const previousProvider = previousStep?.model?.providerID\n const key = identityKey(lastStep.model)\n const previousKey = identityKey(previousStep?.model)\n\n return {\n endAt: stepEnd(lastStep),\n startAt: stepCreated(settled[0] ?? lastStep),\n promptAt: stampOf(last.prompt?.time?.created),\n promptGapMs: promptGap(last.prompt, previous?.prompt),\n providerID: provider,\n modelID: lastStep.model?.id,\n variant: lastStep.model?.variant,\n providerChanged: previousProvider !== undefined && provider !== undefined && previousProvider !== provider,\n modelChanged: previousKey !== undefined && key !== undefined && previousKey !== key && previousProvider === provider,\n totals: totalize(settled),\n latest: totalize([lastStep]),\n session,\n drops,\n cacheDropTokens: drops.reduce((sum, drop) => sum + drop.tokens, 0),\n cacheExpired: drops.length > 0,\n steps: settled.length,\n }\n}\n\nfunction promptGap(current: TranscriptMessage | undefined, previous: TranscriptMessage | undefined): number | undefined {\n const now = stampOf(current?.time?.created)\n const before = stampOf(previous?.time?.created)\n if (now === undefined || before === undefined) return undefined\n return Math.max(0, now - before)\n}\n\n/** True when a cache drop is small enough to be provider noise. */\nexport function isNoiseDrop(providerID: string | undefined, drop: number): boolean {\n if (providerID === undefined) return false\n return DROP_EXEMPT_PROVIDERS.has(providerID) && drop >= 1024 && drop <= 2048\n}\n\n// ---------------------------------------------------------------------------\n// Formatting\n// ---------------------------------------------------------------------------\n\nexport type LineTone = \"base\" | \"muted\" | \"warning\"\n\nexport type LineSegment = { text: string; tone: LineTone }\n\nexport type LineLabels = {\n delta: string\n turn: string\n session: string\n step: string\n steps: string\n input: string\n cached: string\n output: string\n reasoning: string\n cost: string\n /** Prefix for a list-price estimate, e.g. `cost ≈ $0.03`. */\n estimate: string\n cacheExpired: string\n providerChanged: string\n modelChanged: string\n}\n\n/**\n * How the turn segment counts prompt usage. `latest` shows the prompt size the\n * turn ended with — the context view, and what OpenCode's own turn-token debug\n * line reports. `sum` adds every step of the turn: the billing view, where a\n * tool-call loop re-sends (and re-reads) the same context once per step.\n */\nexport type TurnTotalsView = \"latest\" | \"sum\"\n\nexport type LineFields = {\n clock: boolean\n delta: boolean\n model: boolean\n variant: boolean\n turn: boolean\n session: boolean\n reasoning: boolean\n cost: boolean\n cacheExpiry: boolean\n}\n\nexport const DEFAULT_LABELS: LineLabels = {\n delta: \"Δ\",\n // The turn segment is unlabelled by default to keep the line short; set it\n // back to \"turn\" (or anything else) through `labels.turn`.\n turn: \"\",\n session: \"sess\",\n step: \"step\",\n steps: \"steps\",\n input: \"in\",\n cached: \"cached\",\n output: \"out\",\n reasoning: \"reason\",\n cost: \"cost\",\n estimate: \"≈\",\n cacheExpired: \"cache expired\",\n providerChanged: \"changed\",\n modelChanged: \"model changed\",\n}\n\nexport const DEFAULT_FIELDS: LineFields = {\n clock: true,\n delta: true,\n model: true,\n variant: true,\n turn: true,\n session: true,\n reasoning: false,\n // The turn's and the session's cost are on by default: what a session costs\n // is the question the tokens only half answer. A zero is left out, so a free\n // model reads as free instead of repeating `$0` on every line.\n cost: true,\n cacheExpiry: true,\n}\n\nexport function formatClock(ms: number): string {\n if (!Number.isFinite(ms)) return \"--:--\"\n const date = new Date(ms)\n const pad = (value: number) => String(value).padStart(2, \"0\")\n return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`\n}\n\nexport function formatDuration(ms: number): string {\n if (!Number.isFinite(ms)) return \"–\"\n const totalSeconds = Math.max(0, Math.floor(ms / 1000))\n const seconds = totalSeconds % 60\n const minutes = Math.floor(totalSeconds / 60) % 60\n const hours = Math.floor(totalSeconds / 3600) % 24\n const days = Math.floor(totalSeconds / 86400)\n if (days > 0) return `${days}d${hours}h`\n if (hours > 0) return `${hours}h${String(minutes).padStart(2, \"0\")}m`\n if (minutes > 0) return `${minutes}m${String(seconds).padStart(2, \"0\")}s`\n return `${seconds}s`\n}\n\nexport function formatTokens(value: number): string {\n const n = Number.isFinite(value) ? Math.max(0, Math.round(value)) : 0\n if (n < 1_000) return String(n)\n if (n < 999_500) {\n const k = n / 1_000\n return `${trimZeros(k.toFixed(k < 10 ? 2 : 1))}k`\n }\n if (n < 999_500_000) {\n const m = n / 1_000_000\n return `${trimZeros(m.toFixed(m < 10 ? 2 : 1))}M`\n }\n return `${Math.round(n / 1_000_000)}M`\n}\n\nfunction trimZeros(value: string): string {\n return value.includes(\".\") ? value.replace(/0+$/, \"\").replace(/\\.$/, \"\") : value\n}\n\nexport function formatCost(value: number): string {\n if (!Number.isFinite(value) || value <= 0) return \"$0\"\n // Four decimals is the display precision below a dollar; a real, positive\n // amount that rounds to `$0.0000` would read as free, so it is shown as a\n // bound instead. This keeps a tiny list estimate distinguishable from zero.\n if (value < 0.00005) return \"<$0.0001\"\n if (value < 1) return `$${value.toFixed(4)}`\n return `$${value.toFixed(2)}`\n}\n\n/**\n * Charged and estimated money as one string. A mixed total keeps both parts\n * visible so list price is never mistaken for money that was charged:\n * `$0.12 + ≈ $3.40`. Returns undefined when there is no money to show.\n */\nexport function formatMoney(charged: number, estimated: number, marker = \"≈\"): string | undefined {\n const paid = Number.isFinite(charged) && charged > 0 ? formatCost(charged) : undefined\n const estimate =\n Number.isFinite(estimated) && estimated > 0\n ? `${marker.trim() === \"\" ? \"\" : `${marker.trim()} `}${formatCost(estimated)}`\n : undefined\n if (paid !== undefined && estimate !== undefined) return `${paid} + ${estimate}`\n return paid ?? estimate\n}\n\n/**\n * The list-equivalent total for a compact table cell: charged money plus the\n * estimates, marked with `≈` when any part is estimated.\n */\nexport function formatSpend(charged: number, estimated: number, marker = \"≈\"): string {\n const paid = Number.isFinite(charged) ? Math.max(0, charged) : 0\n const estimate = Number.isFinite(estimated) ? Math.max(0, estimated) : 0\n if (estimate > 0) return `${marker.trim()}${formatCost(paid + estimate)}`\n return formatCost(paid)\n}\n\n/**\n * The buckets the line displays, derived from a totals group. `input` is the\n * *uncached* part of the prompt (fresh input plus cache writes, i.e. what the\n * debug line's `new` counts on the prompt side), `prompt` is the whole prompt\n * of the request and `output` is everything the model generated (visible output\n * + reasoning). OpenCode's own debug line reconciles with these:\n * `new = in + out` and `total = prompt + out`, with `prompt = in + cached`.\n * The stored buckets stay raw; only the display folds.\n */\nexport type DisplayTotals = Totals & {\n /** Whole prompt of the request: fresh input + cached reads + cache writes. */\n prompt: number\n}\n\nexport function displayTotals(totals: Totals): DisplayTotals {\n const prompt = totals.input + totals.cached + totals.cacheWrite\n return {\n input: prompt - totals.cached,\n cached: totals.cached,\n cacheWrite: totals.cacheWrite,\n output: totals.output + totals.reasoning,\n reasoning: totals.reasoning,\n cost: totals.cost,\n listCost: totals.listCost,\n steps: totals.steps,\n prompt,\n }\n}\n\n/**\n * The numbers of one totals group. `cost` is passed explicitly because the two\n * callers mean different money: a turn's cost is the sum over its steps\n * whatever view its tokens follow, and money never comes from a \"latest\" step.\n * A charged cost of zero is left out; a list-price estimate is shown with `≈`.\n */\nfunction totalsText(\n prefix: string,\n totals: Totals,\n labels: LineLabels,\n fields: LineFields,\n stepCount = 0,\n cost = totals.cost,\n listCost = totals.listCost,\n estimates = true,\n): string {\n const display = displayTotals(totals)\n // The parenthesised value is the whole prompt, shown only when it differs\n // from the uncached part the label leads with.\n const input =\n display.prompt === display.input\n ? `${labels.input} ${formatTokens(display.input)}`\n : `${labels.input} ${formatTokens(display.input)} (${formatTokens(display.prompt)})`\n const parts = [\n input,\n `${labels.cached} ${formatTokens(display.cached)}`,\n `${labels.output} ${formatTokens(display.output)}`,\n ]\n if (fields.reasoning) parts.push(`${labels.reasoning} ${formatTokens(display.reasoning)}`)\n const money = fields.cost ? formatMoney(cost, estimates ? listCost : 0, labels.estimate) : undefined\n if (money !== undefined) parts.push(`${labels.cost} ${money}`)\n // An empty label (the default for the turn) leaves the numbers unlabelled; a\n // multi-step turn still says how many requests it took.\n const steps = stepCount > 1 ? `${stepCount} ${stepCount === 1 ? labels.step : labels.steps}` : \"\"\n const head = [prefix, steps].filter((piece) => piece !== \"\").join(\" \")\n const numbers = parts.join(\", \")\n if (head === \"\") return numbers\n return stepCount > 1 ? `${head} · ${numbers}` : `${head} ${numbers}`\n}\n\n/**\n * Render the telemetry line as toned segments. The caller maps tones onto\n * theme colors; `formatLine` joins the segments as plain text.\n */\nexport function formatLineSegments(\n stats: TurnStats,\n options?: { labels?: Partial<LineLabels>; fields?: Partial<LineFields>; turnTotals?: TurnTotalsView; estimates?: boolean },\n): LineSegment[] {\n const labels = { ...DEFAULT_LABELS, ...options?.labels }\n const fields = { ...DEFAULT_FIELDS, ...options?.fields }\n const turnTotals = options?.turnTotals ?? \"latest\"\n const estimates = options?.estimates !== false\n const segments: LineSegment[] = []\n\n if (fields.clock && stats.endAt !== undefined) {\n segments.push({ text: formatClock(stats.endAt), tone: \"muted\" })\n }\n if (fields.delta && stats.promptGapMs !== undefined) {\n segments.push({ text: `${labels.delta} ${formatDuration(stats.promptGapMs)}`, tone: \"muted\" })\n }\n // The turn's identity is the provider the model ran through and the model\n // itself: the same model can come from different providers, so both are\n // shown, and a provider switch is the change the line warns about.\n if (stats.providerID !== undefined) {\n let text = stats.providerID\n if (fields.model && stats.modelID !== undefined) text += `/${stats.modelID}`\n if (fields.variant && stats.variant) text += ` ${stats.variant}`\n segments.push({ text, tone: stats.providerChanged ? \"warning\" : \"base\" })\n if (stats.providerChanged) segments.push({ text: labels.providerChanged, tone: \"warning\" })\n else if (stats.modelChanged) segments.push({ text: labels.modelChanged, tone: \"muted\" })\n }\n if (fields.turn && stats.totals.steps > 0) {\n const totals = turnTotals === \"sum\" ? stats.totals : stats.latest\n // The tokens follow the view — `latest` is the context the turn ended with\n // — but money is paid per step: the turn's cost is the sum over its steps,\n // never the last step's share of it.\n segments.push({\n text: totalsText(\n labels.turn,\n totals,\n labels,\n fields,\n turnTotals === \"sum\" ? stats.totals.steps : stats.steps,\n stats.totals.cost,\n stats.totals.listCost,\n estimates,\n ),\n tone: \"base\",\n })\n }\n if (fields.session && stats.session) {\n segments.push({\n text: totalsText(labels.session, stats.session, labels, fields, 0, stats.session.cost, stats.session.listCost, estimates),\n tone: \"muted\",\n })\n }\n if (fields.cacheExpiry && stats.cacheExpired) {\n const count = stats.drops.length > 1 ? `×${stats.drops.length}` : \"\"\n segments.push({ text: `⚠ ${labels.cacheExpired} (−${formatTokens(stats.cacheDropTokens)}${count})`, tone: \"warning\" })\n }\n return segments\n}\n\nexport function formatLine(\n stats: TurnStats,\n options?: { labels?: Partial<LineLabels>; fields?: Partial<LineFields>; turnTotals?: TurnTotalsView; estimates?: boolean },\n): string {\n return formatLineSegments(stats, options)\n .map((segment) => segment.text)\n .join(\" · \")\n}\n",
|
|
11
|
+
"import type { Database } from \"bun:sqlite\"\nimport { cacheDrop, stepGap, tokensPerSecond, type AssistantMessage } from \"../core\"\n\n/**\n * Read side of the stats engine: every function takes an open database and\n * returns plain rows for the TUI panel, the report script and the NDJSON\n * exports. Aggregations stay in SQL; comparisons and percentiles that would\n * need window functions or custom SQLite builds are computed in JS.\n */\n\n/** Event kind written by the recorder when a compaction completes. */\nexport const COMPACTION_KIND = \"compaction\"\n\n/**\n * Idle gap above which a cache reuse loss is attributed to provider-side\n * expiry rather than to a mid-session event. Heuristic: the empirical TTL\n * evidence view narrows this down with real observations.\n */\nexport const DEFAULT_TTL_HINT_MS = 10 * 60 * 1000\n\nexport type Range = {\n from?: number | undefined\n to?: number | undefined\n}\n\n/**\n * Panel filters. They narrow every aggregation to one model, one provider or\n * one session, and are applied with the same `WHERE` builder the range uses so\n * a filtered view and a drill-down agree.\n */\nexport type Filters = {\n /** Model id, variant excluded. */\n modelID?: string | undefined\n /**\n * One model *and* its variant, as `modelKey` builds it (`m1#max`). A variant\n * is a different model to the provider, so a drill-down into one variant must\n * not pick up its family.\n */\n modelKey?: string | undefined\n /**\n * The provider the model ran through, as OpenCode configured it\n * (`opencode-go`, `deepseek`, `deepinfra`). It is the identity every view\n * groups by: a gateway may re-route the request, but the upstream that\n * actually ran it is not observable.\n */\n provider?: string | undefined\n /**\n * One session, exactly. Used by the `session` range and by the overview's\n * \"this session\" block, which must not pick up the steps of every other\n * session that happened to run after it started.\n */\n sessionID?: string | undefined\n}\n\nexport type Usage = {\n steps: number\n input: number\n cached: number\n cacheWrite: number\n output: number\n reasoning: number\n cost: number\n /** List-price estimate for steps the host reported as costing nothing. */\n listCost: number\n}\n\n/**\n * What the selection costs: charged money plus the list-price estimate of the\n * steps the host reported as free. Display code marks the estimated part.\n */\nexport function spend(usage: Pick<Usage, \"cost\" | \"listCost\">): number {\n return usage.cost + usage.listCost\n}\n\n/** True when at least one step in the aggregate is an estimate. */\nexport function hasEstimate(usage: Pick<Usage, \"listCost\">): boolean {\n return usage.listCost > 0\n}\n\n/**\n * The list-price estimate of one session, from the steps the plugin recorded.\n * The live line uses it for the `sess` segment; a session with no recorded\n * estimate (or a database without the column) answers 0.\n */\nexport function sessionListCost(db: Database, sessionID: string): number {\n const row = db\n .query(\"SELECT COALESCE(SUM(list_cost), 0) AS listCost FROM steps WHERE session_id = ?\")\n .get(sessionID) as { listCost: number | null } | null\n return row?.listCost ?? 0\n}\n\n/**\n * Money the host reported with no assistant step behind it: a compaction\n * summary or a generated title. It is part of the session's own cost, so the\n * totals that claim to describe a session include it.\n */\nexport type OtherUsage = {\n /** Host usage records behind the number. */\n entries: number\n cost: number\n}\n\nexport const EMPTY_OTHER: OtherUsage = { entries: 0, cost: 0 }\n\n/** True when the database has the table the non-step usage lives in. */\nfunction hasUsageTable(db: Database): boolean {\n try {\n return db.query(\"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'usage'\").get() !== null\n } catch {\n return false\n }\n}\n\n/**\n * `WHERE` for non-step usage. A model or provider filter cannot be honored:\n * the host usage record carries neither, so including it under a filter would\n * attribute money to a model that did not earn it. A session filter can: the\n * row is the session's.\n */\nfunction otherWhere(\n range: Range | undefined,\n filters?: Filters | undefined,\n): { where: string; params: (string | number)[] } | undefined {\n if (filters?.modelID !== undefined && filters.modelID !== \"\") return undefined\n if (filters?.modelKey !== undefined && filters.modelKey !== \"\") return undefined\n if (filters?.provider !== undefined && filters.provider !== \"\") return undefined\n const span = rangeWhere(range, \"at\")\n const clauses = span.where === \"\" ? [] : [span.where.slice(\"WHERE \".length)]\n const params: (string | number)[] = [...span.params]\n if (filters?.sessionID !== undefined && filters.sessionID !== \"\") {\n clauses.push(\"session_id = ?\")\n params.push(filters.sessionID)\n }\n return { where: clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\", params }\n}\n\n/** Non-step usage in the selection. */\nexport function otherUsage(db: Database, range?: Range | undefined, filters?: Filters | undefined): OtherUsage {\n const filter = otherWhere(range, filters)\n if (filter === undefined || !hasUsageTable(db)) return { ...EMPTY_OTHER }\n try {\n const row = db\n .query(`SELECT COUNT(*) AS entries, COALESCE(SUM(cost), 0) AS cost FROM usage ${filter.where}`)\n .get(...filter.params) as { entries: number | null; cost: number | null } | null\n return { entries: row?.entries ?? 0, cost: row?.cost ?? 0 }\n } catch {\n return { ...EMPTY_OTHER }\n }\n}\n\n/** Non-step usage per session, for the session rows. */\nexport function otherUsageBySession(db: Database, range?: Range | undefined, filters?: Filters | undefined): Map<string, OtherUsage> {\n const out = new Map<string, OtherUsage>()\n const filter = otherWhere(range, filters)\n if (filter === undefined || !hasUsageTable(db)) return out\n try {\n const rows = db\n .query(\n `SELECT session_id AS sessionID, COUNT(*) AS entries, COALESCE(SUM(cost), 0) AS cost\n FROM usage ${filter.where}\n GROUP BY session_id`,\n )\n .all(...filter.params) as Array<{ sessionID: string; entries: number | null; cost: number | null }>\n for (const row of rows) out.set(row.sessionID, { entries: row.entries ?? 0, cost: row.cost ?? 0 })\n } catch {\n // An older database has no usage table: step totals alone are still valid.\n }\n return out\n}\n\nexport type OtherUsageRow = {\n sessionID: string\n at: number\n cost: number\n}\n\n/**\n * Non-step usage as one row per host record, so calendar buckets can fold it\n * into their periods with the same local-time rule the steps use.\n */\nexport function otherUsageRows(db: Database, range?: Range | undefined, filters?: Filters | undefined): OtherUsageRow[] {\n const filter = otherWhere(range, filters)\n if (filter === undefined || !hasUsageTable(db)) return []\n const where = filter.where === \"\" ? \"WHERE at IS NOT NULL\" : `${filter.where} AND at IS NOT NULL`\n try {\n return db\n .query(`SELECT session_id AS sessionID, at, cost FROM usage ${where} ORDER BY at`)\n .all(...filter.params) as OtherUsageRow[]\n } catch {\n return []\n }\n}\n\ntype UsageRow = {\n steps: number | null\n input: number | null\n cached: number | null\n cacheWrite: number | null\n output: number | null\n reasoning: number | null\n cost: number | null\n listCost: number | null\n}\n\nexport const STEP_TIME = \"COALESCE(created_at, recorded_at)\"\n\n/**\n * The identity of a model *and* its variant, in SQL: `modelKey` says a variant\n * is a different model to the provider, so a variant never shares a row with\n * its family. The JS twin is `modelKey` below; both have to agree.\n */\nexport const MODEL_KEY_SQL = \"COALESCE(model_id, '(unknown)') || '#' || COALESCE(variant, '')\"\n\n/** `modelKey(modelID, variant)`: the family, with the variant as a suffix. */\nexport function modelKey(modelID: string, variant: string | undefined): string {\n return `${modelID}#${variant ?? \"\"}`\n}\n\n/** Human label of a model key or a model id and variant. */\nexport function modelLabel(modelID: string, variant: string | undefined): string {\n return variant === undefined ? modelID : `${modelID} ${variant}`\n}\nconst USAGE_SELECT = `COUNT(*) AS steps,\n COALESCE(SUM(input_tokens), 0) AS input,\n COALESCE(SUM(cache_read), 0) AS cached,\n COALESCE(SUM(cache_write), 0) AS cacheWrite,\n COALESCE(SUM(output_tokens), 0) AS output,\n COALESCE(SUM(reasoning_tokens), 0) AS reasoning,\n COALESCE(SUM(cost), 0) AS cost,\n COALESCE(SUM(list_cost), 0) AS listCost`\n\nfunction usageFrom(row: UsageRow): Usage {\n return {\n steps: row.steps ?? 0,\n input: row.input ?? 0,\n cached: row.cached ?? 0,\n cacheWrite: row.cacheWrite ?? 0,\n output: row.output ?? 0,\n reasoning: row.reasoning ?? 0,\n cost: row.cost ?? 0,\n listCost: row.listCost ?? 0,\n }\n}\n\nfunction rangeWhere(range: Range | undefined, column = STEP_TIME): { where: string; params: number[] } {\n const clauses: string[] = []\n const params: number[] = []\n if (range?.from !== undefined) {\n clauses.push(`${column} >= ?`)\n params.push(range.from)\n }\n if (range?.to !== undefined) {\n clauses.push(`${column} < ?`)\n params.push(range.to)\n }\n return { where: clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\", params }\n}\n\n/**\n * `WHERE` for a range plus the panel filters. Shared with the analysis layer so\n * a filtered view and a drill-down agree on what is in scope.\n */\nexport function buildWhere(\n range: Range | undefined,\n filters?: Filters | undefined,\n column = STEP_TIME,\n): { where: string; params: (string | number)[] } {\n const rangeClause = rangeWhere(range, column)\n const clauses = rangeClause.where === \"\" ? [] : [rangeClause.where.slice(\"WHERE \".length)]\n const params: (string | number)[] = [...rangeClause.params]\n if (filters?.modelID !== undefined && filters.modelID !== \"\") {\n clauses.push(\"model_id = ?\")\n params.push(filters.modelID)\n }\n if (filters?.modelKey !== undefined && filters.modelKey !== \"\") {\n clauses.push(`${MODEL_KEY_SQL} = ?`)\n params.push(filters.modelKey)\n }\n if (filters?.provider !== undefined && filters.provider !== \"\") {\n clauses.push(\"provider_id = ?\")\n params.push(filters.provider)\n }\n if (filters?.sessionID !== undefined && filters.sessionID !== \"\") {\n clauses.push(\"session_id = ?\")\n params.push(filters.sessionID)\n }\n return { where: clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\", params }\n}\n\n/**\n * Share of the whole prompt that came from the cache. The denominator is what\n * the provider saw: cache reads, fresh input and cache writes.\n */\nexport function cacheHitRatio(usage: Pick<Usage, \"input\" | \"cached\" | \"cacheWrite\">): number | undefined {\n const total = usage.cached + usage.input + usage.cacheWrite\n return total > 0 ? usage.cached / total : undefined\n}\n\nexport type Overview = Usage & {\n sessions: number\n prompts: number\n /** Compaction/title money included in `cost`, when the range has any. */\n otherCost?: number | undefined\n firstAt: number | undefined\n lastAt: number | undefined\n cacheHitRatio: number | undefined\n /**\n * Tokens generated per second of streaming, over the whole selection: the\n * produced tokens divided by the summed streaming windows. A weighted average,\n * not the mean of the per-step rates the latency view reports as p50.\n */\n tps: number | undefined\n}\n\nexport function overview(db: Database, range?: Range, filters?: Filters): Overview {\n const steps = buildWhere(range, filters)\n const row = db\n .query(\n `SELECT ${USAGE_SELECT},\n COUNT(DISTINCT session_id) AS sessions,\n MIN(${STEP_TIME}) AS firstAt,\n MAX(COALESCE(completed_at, ${STEP_TIME})) AS lastAt,\n COALESCE(SUM(CASE WHEN streamed_at > created_at THEN output_tokens + reasoning_tokens ELSE 0 END), 0) AS produced,\n COALESCE(SUM(CASE WHEN streamed_at > created_at THEN streamed_at - created_at ELSE 0 END), 0) AS window\n FROM steps ${steps.where}`,\n )\n .get(...steps.params) as\n | (UsageRow & { sessions: number | null; firstAt: number | null; lastAt: number | null; produced: number | null; window: number | null })\n | null\n\n const prompts = rangeWhere(range, \"created_at\")\n const promptClauses = prompts.where === \"\" ? [] : [prompts.where.slice(\"WHERE \".length)]\n const promptParams: (string | number)[] = [...prompts.params]\n // The session block asks for one session: a prompt has no model, so the\n // session filter is the one that can be honored here. Ignoring it counted\n // every prompt in the range, including those of other sessions.\n if (filters?.sessionID !== undefined && filters.sessionID !== \"\") {\n promptClauses.push(\"session_id = ?\")\n promptParams.push(filters.sessionID)\n }\n const promptWhere = promptClauses.length > 0 ? `WHERE ${promptClauses.join(\" AND \")}` : \"\"\n const promptRow = db.query(`SELECT COUNT(*) AS prompts FROM prompts ${promptWhere}`).get(...promptParams) as {\n prompts: number\n } | null\n\n const usage = usageFrom(row ?? { steps: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0, listCost: 0 })\n const other = otherUsage(db, range, filters)\n return {\n ...usage,\n // The host's session cost includes compactions and titles; a total that\n // claims to describe the selection has to include them too. Tokens stay\n // step-based so the cache views keep comparing like with like.\n cost: usage.cost + other.cost,\n sessions: row?.sessions ?? 0,\n prompts: promptRow?.prompts ?? 0,\n otherCost: other.cost,\n firstAt: row?.firstAt ?? undefined,\n lastAt: row?.lastAt ?? undefined,\n cacheHitRatio: cacheHitRatio(usage),\n tps: row === null ? undefined : tokensPerSecond(row.produced ?? 0, 0, row.window ?? 0),\n }\n}\n\nexport type ProviderUsage = Usage & {\n providerID: string\n firstAt: number | undefined\n lastAt: number | undefined\n}\n\nexport type ModelUsage = {\n modelID: string\n variant: string | undefined\n providers: ProviderUsage[]\n totals: Usage\n}\n\ntype ModelRow = UsageRow & {\n provider_id: string | null\n model_id: string | null\n variant: string | null\n first_at: number | null\n last_at: number | null\n}\n\n/**\n * Usage per model, each with the providers that served it. This is the view\n * that answers \"through which provider is this model actually coming, over the\n * selected period\".\n */\nexport function models(db: Database, range?: Range, filters?: Filters): ModelUsage[] {\n const filter = buildWhere(range, filters)\n const rows = db\n .query(\n `SELECT provider_id, model_id, variant,\n ${USAGE_SELECT},\n MIN(${STEP_TIME}) AS first_at,\n MAX(COALESCE(completed_at, recorded_at)) AS last_at\n FROM steps ${filter.where}\n GROUP BY provider_id, model_id, variant\n ORDER BY steps DESC, cached DESC`,\n )\n .all(...filter.params) as ModelRow[]\n\n const models = new Map<string, ModelUsage>()\n for (const row of rows) {\n const modelID = row.model_id ?? \"(unknown)\"\n const variant = row.variant ?? undefined\n const key = `${modelID}#${variant ?? \"\"}`\n let model = models.get(key)\n if (!model) {\n model = { modelID, variant, providers: [], totals: emptyUsage() }\n models.set(key, model)\n }\n const usage = usageFrom(row)\n model.providers.push({\n ...usage,\n providerID: row.provider_id ?? \"(unknown)\",\n firstAt: row.first_at ?? undefined,\n lastAt: row.last_at ?? undefined,\n })\n addUsage(model.totals, usage)\n }\n return [...models.values()].sort((a, b) => billedTokens(b.totals) - billedTokens(a.totals))\n}\n\nfunction emptyUsage(): Usage {\n return { steps: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0, listCost: 0 }\n}\n\nfunction addUsage(target: Usage, source: Usage): void {\n target.steps += source.steps\n target.input += source.input\n target.cached += source.cached\n target.cacheWrite += source.cacheWrite\n target.output += source.output\n target.reasoning += source.reasoning\n target.cost += source.cost\n target.listCost += source.listCost\n}\n\n/**\n * Every bucket a provider bills for: the whole prompt (fresh input, cache\n * reads and writes) plus visible output and reasoning. Leaving `cacheWrite`\n * or `reasoning` out ranked a model with heavy cache writes or thinking below\n * one with more visible output, which is not what \"most tokens\" means.\n */\nfunction billedTokens(usage: Pick<Usage, \"input\" | \"cached\" | \"cacheWrite\" | \"output\" | \"reasoning\">): number {\n return usage.input + usage.cached + usage.cacheWrite + usage.output + usage.reasoning\n}\n\nexport type TimelineBucket = { at: number } & Usage\n\n/** Usage aggregated into fixed time buckets (for example one hour or day). */\nexport function timeline(db: Database, range: Range | undefined, bucketMs: number): TimelineBucket[] {\n const filter = rangeWhere(range)\n const rows = db\n .query(\n `SELECT CAST(${STEP_TIME} / ? AS INTEGER) * ? AS bucket, ${USAGE_SELECT}\n FROM steps ${filter.where}\n GROUP BY bucket\n ORDER BY bucket`,\n )\n .all(bucketMs, bucketMs, ...filter.params) as (UsageRow & { bucket: number })[]\n return rows.map((row) => ({ at: row.bucket, ...usageFrom(row) }))\n}\n\nexport type DropReason = \"compaction\" | \"expired\" | \"unknown\"\n\nexport type CacheDropRow = {\n sessionID: string\n messageID: string\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n at: number\n tokens: number\n gapMs: number | undefined\n prevCached: number\n reason: DropReason\n}\n\nexport type TtlEvidenceRow = {\n providerID: string\n modelID: string\n samples: number\n /** Largest idle gap that still hit the cache. */\n maxHitGapMs: number | undefined\n /** Smallest idle gap after which reuse was lost. */\n minDropGapMs: number | undefined\n}\n\nexport type LatencyStats = {\n count: number\n p50: number | undefined\n p90: number | undefined\n p99: number | undefined\n}\n\nexport type LatencyReport = {\n /** Milliseconds from request to completed response. */\n wall: LatencyStats\n /** Milliseconds from request to the last streamed chunk (the streaming window). */\n stream: LatencyStats\n /** Output-plus-reasoning tokens per second over that window. */\n tps: LatencyStats\n}\n\n/** Nearest-rank percentile over an unsorted sample. */\nexport function percentile(values: readonly number[], p: number): number | undefined {\n if (values.length === 0) return undefined\n const sorted = [...values].sort((a, b) => a - b)\n const rank = Math.ceil((p / 100) * sorted.length)\n const index = Math.min(sorted.length - 1, Math.max(0, rank - 1))\n return sorted[index]\n}\n\nexport function latencyStats(values: number[]): LatencyStats {\n return {\n count: values.length,\n p50: percentile(values, 50),\n p90: percentile(values, 90),\n p99: percentile(values, 99),\n }\n}\n\nconst statsFor = latencyStats\n\nexport type SessionUsage = Usage & {\n sessionID: string\n parentID: string | undefined\n title: string | undefined\n directory: string | undefined\n providers: string[]\n models: string[]\n prompts: number\n /** Compaction/title money included in `cost`, when the session has any. */\n otherCost?: number | undefined\n firstAt: number | undefined\n lastAt: number | undefined\n cacheHitRatio: number | undefined\n}\n\nexport function sessions(\n db: Database,\n options?: { range?: Range; filters?: Filters; limit?: number; sessionIDs?: readonly string[] },\n): SessionUsage[] {\n if (options?.sessionIDs !== undefined && options.sessionIDs.length === 0) return []\n const range = options?.range\n const filter = buildWhere(range, options?.filters, \"COALESCE(st.created_at, st.recorded_at)\")\n const limit = options?.limit ?? 20\n const clauses = filter.where === \"\" ? [] : [filter.where.slice(\"WHERE \".length)]\n const params: (number | string)[] = [...filter.params]\n // The session row is for a range, so its prompt count has to be for that\n // range too: a 7-day row must not show the prompts of the session's whole\n // history. The placeholders sit in the SELECT list, before the WHERE ones.\n const promptRange: string[] = []\n const promptParams: number[] = []\n if (range?.from !== undefined) {\n promptRange.push(\"p.created_at >= ?\")\n promptParams.push(range.from)\n }\n if (range?.to !== undefined) {\n promptRange.push(\"p.created_at < ?\")\n promptParams.push(range.to)\n }\n const promptClause = promptRange.length > 0 ? ` AND ${promptRange.join(\" AND \")}` : \"\"\n if (options?.sessionIDs !== undefined) {\n // A specific set of sessions (the subagents of one session): the group by\n // then reads one index range per session instead of every step recorded.\n const ids = options.sessionIDs.slice(0, 500)\n clauses.push(`st.session_id IN (${ids.map(() => \"?\").join(\", \")})`)\n params.push(...ids)\n }\n const rows = db\n .query(\n `SELECT st.session_id, s.title, s.directory, s.parent_id,\n GROUP_CONCAT(DISTINCT st.provider_id) AS providers,\n GROUP_CONCAT(DISTINCT st.model_id) AS models,\n (SELECT COUNT(*) FROM prompts p WHERE p.session_id = st.session_id${promptClause}) AS prompts,\n COUNT(*) AS steps,\n COALESCE(SUM(st.input_tokens), 0) AS input,\n COALESCE(SUM(st.cache_read), 0) AS cached,\n COALESCE(SUM(st.cache_write), 0) AS cacheWrite,\n COALESCE(SUM(st.output_tokens), 0) AS output,\n COALESCE(SUM(st.reasoning_tokens), 0) AS reasoning,\n COALESCE(SUM(st.cost), 0) AS cost,\n COALESCE(SUM(st.list_cost), 0) AS listCost,\n MIN(COALESCE(st.created_at, st.recorded_at)) AS first_at,\n MAX(COALESCE(st.completed_at, st.created_at, st.recorded_at)) AS last_at\n FROM steps st\n LEFT JOIN sessions s ON s.session_id = st.session_id\n ${clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\"}\n GROUP BY st.session_id\n ORDER BY last_at DESC\n LIMIT ?`,\n )\n .all(\n ...promptParams,\n ...params,\n limit,\n ) as (UsageRow & {\n session_id: string\n title: string | null\n directory: string | null\n parent_id: string | null\n providers: string | null\n models: string | null\n prompts: number | null\n first_at: number | null\n last_at: number | null\n })[]\n\n // The host's session cost includes compactions and titles; the session row\n // has to include them too, or it disagrees with the line's `sess` segment.\n const other = otherUsageBySession(db, range, options?.filters)\n return rows.map((row) => {\n const usage = usageFrom(row)\n const extra = other.get(row.session_id)?.cost ?? 0\n return {\n ...usage,\n cost: usage.cost + extra,\n sessionID: row.session_id,\n parentID: row.parent_id ?? undefined,\n title: row.title ?? undefined,\n directory: row.directory ?? undefined,\n providers: row.providers ? row.providers.split(\",\").filter(Boolean) : [],\n models: row.models ? row.models.split(\",\").filter(Boolean) : [],\n prompts: row.prompts ?? 0,\n otherCost: extra,\n firstAt: row.first_at ?? undefined,\n lastAt: row.last_at ?? undefined,\n cacheHitRatio: cacheHitRatio(usage),\n }\n })\n}\n\n/** Sessions whose parent is `parentID`: the subagents of one session. */\nexport function childSessionIDs(db: Database, parentID: string): string[] {\n return (db.query(\"SELECT session_id FROM sessions WHERE parent_id = ? ORDER BY session_id\").all(parentID) as { session_id: string }[]).map(\n (row) => row.session_id,\n )\n}\n\n/** Session usage rows for a specific set of sessions (subagent rollups). */\nexport function sessionUsageOf(db: Database, sessionIDs: readonly string[]): SessionUsage[] {\n return sessions(db, { sessionIDs, limit: sessionIDs.length })\n}\n\nexport type TurnDrop = {\n messageID: string\n tokens: number\n gapMs: number | undefined\n}\n\n/**\n * One finished turn of a session, built from the recorded steps and prompts. It\n * carries the same buckets the telemetry line uses, so the panel can render the\n * per-prompt history with the line's own formatter.\n */\nexport type TurnUsage = {\n sessionID: string\n /** Creation time of the prompt that opened the turn, when it was recorded. */\n promptAt: number | undefined\n endAt: number | undefined\n /** Time between the previous finished turn's prompt and this turn's prompt. */\n promptGapMs: number | undefined\n providerID: string | undefined\n modelID: string | undefined\n variant: string | undefined\n providerChanged: boolean\n modelChanged: boolean\n steps: number\n /** Raw buckets of the last step: the prompt the turn ended with. */\n latest: Usage\n /** Raw buckets summed over every step of the turn. */\n totals: Usage\n drops: TurnDrop[]\n /** Finish reason of the turn's last step. */\n finish: string | undefined\n /**\n * Tokens generated per second of streaming over the turn's steps: produced\n * tokens divided by their summed streaming windows. For a one-step turn it is\n * that step's rate.\n */\n tps: number | undefined\n}\n\ntype TurnStepRow = {\n message_id: string\n session_id: string\n provider_id: string | null\n model_id: string | null\n variant: string | null\n input_tokens: number | null\n cache_read: number | null\n cache_write: number | null\n output_tokens: number | null\n reasoning_tokens: number | null\n cost: number | null\n list_cost: number | null\n created_at: number | null\n streamed_at: number | null\n recorded_at: number\n completed_at: number | null\n finish: string | null\n}\n\n/** A turn is finished when its last step ended without asking for more tools. */\nfunction turnFinished(finish: string | null): boolean {\n return finish !== null && finish !== \"\" && finish !== \"tool-calls\"\n}\n\nfunction stepUsage(row: TurnStepRow): Usage {\n return {\n steps: 1,\n input: row.input_tokens ?? 0,\n cached: row.cache_read ?? 0,\n cacheWrite: row.cache_write ?? 0,\n output: row.output_tokens ?? 0,\n reasoning: row.reasoning_tokens ?? 0,\n cost: row.cost ?? 0,\n listCost: row.list_cost ?? 0,\n }\n}\n\n/** Structural view of a recorded step, so the shared cache-drop rule applies. */\nfunction stepMessage(row: TurnStepRow): AssistantMessage {\n return {\n type: \"assistant\",\n id: row.message_id,\n time: { created: row.created_at ?? row.recorded_at, completed: row.completed_at ?? undefined },\n model:\n row.provider_id !== null && row.model_id !== null\n ? { providerID: row.provider_id, id: row.model_id, variant: row.variant ?? undefined }\n : undefined,\n tokens: {\n input: row.input_tokens ?? 0,\n output: row.output_tokens ?? 0,\n reasoning: row.reasoning_tokens ?? 0,\n cache: { read: row.cache_read ?? 0, write: row.cache_write ?? 0 },\n },\n finish: row.finish ?? undefined,\n }\n}\n\n/**\n * Finished turns, newest first. Steps belong to the newest prompt recorded at or\n * before them; a turn whose last step asked for tools is dropped, because it is\n * still running — the same rule the telemetry line follows.\n *\n * For one session the read is bounded by a window of the newest prompts. The\n * window grows until it holds `limit` finished turns: a prompt can be empty\n * (the host records a synthetic system-reminder next to the user prompt) and\n * the newest turn can still be running, so a fixed one-prompt margin is not\n * enough. `complete` means the window covers every prompt of the session.\n */\nexport function turns(\n db: Database,\n options?: { sessionID?: string | undefined; range?: Range | undefined; filters?: Filters | undefined; limit?: number | undefined },\n): TurnUsage[] {\n const requested = options?.limit ?? 50\n const limit = Number.isFinite(requested) ? Math.max(0, Math.floor(requested)) : 50\n const filter = buildWhere(options?.range, options?.filters)\n const baseClauses = filter.where === \"\" ? [] : [filter.where.slice(\"WHERE \".length)]\n const baseParams: (number | string)[] = [...filter.params]\n if (options?.sessionID !== undefined) {\n baseClauses.push(\"session_id = ?\")\n baseParams.push(options.sessionID)\n }\n\n /** Read the turns inside the given prompt window, newest first. */\n function read(promptCount: number): { rows: TurnUsage[]; complete: boolean } {\n const clauses = [...baseClauses]\n const params = [...baseParams]\n const floor = options?.sessionID === undefined ? undefined : promptFloor(db, options.sessionID, promptCount)\n if (floor !== undefined) {\n clauses.push(`${STEP_TIME} >= ?`)\n params.push(floor)\n }\n const steps = db\n .query(\n `SELECT message_id, session_id, provider_id, model_id, variant,\n input_tokens, cache_read, cache_write, output_tokens, reasoning_tokens, cost, list_cost,\n created_at, streamed_at, recorded_at, completed_at, finish\n FROM steps ${clauses.length > 0 ? `WHERE ${clauses.join(\" AND \")}` : \"\"}\n ORDER BY session_id, ${STEP_TIME}, message_id`,\n )\n .all(...params) as TurnStepRow[]\n\n const promptClause = options?.sessionID === undefined ? \"\" : `WHERE session_id = ?${floor === undefined ? \"\" : \" AND created_at >= ?\"}`\n const promptParams = options?.sessionID === undefined ? [] : floor === undefined ? [options.sessionID] : [options.sessionID, floor]\n const promptRows = db\n .query(`SELECT session_id, created_at FROM prompts ${promptClause} ORDER BY session_id, created_at`)\n .all(...promptParams) as { session_id: string; created_at: number | null }[]\n\n const promptsBySession = new Map<string, number[]>()\n for (const row of promptRows) {\n if (row.created_at === null) continue\n const list = promptsBySession.get(row.session_id) ?? []\n list.push(row.created_at)\n promptsBySession.set(row.session_id, list)\n }\n\n const result: TurnUsage[] = []\n let sessionID: string | undefined\n let bucket: TurnStepRow[] = []\n let promptIndex = -2\n let promptAt: number | undefined\n\n function close(): void {\n const last = bucket[bucket.length - 1]\n if (last === undefined || !turnFinished(last.finish)) {\n bucket = []\n return\n }\n const totals = emptyUsage()\n for (const row of bucket) addUsage(totals, stepUsage(row))\n const drops: TurnDrop[] = []\n let reference: AssistantMessage | undefined\n for (const row of bucket) {\n const message = stepMessage(row)\n const drop = cacheDrop(reference, message)\n if (drop !== undefined) {\n drops.push({ messageID: row.message_id, tokens: drop, gapMs: stepGap(reference, message) })\n }\n reference = message\n }\n const providerID = last.provider_id ?? undefined\n const modelID = last.model_id ?? undefined\n const variant = last.variant ?? undefined\n let produced = 0\n let window = 0\n for (const row of bucket) {\n if (row.created_at === null || row.streamed_at === null || row.streamed_at <= row.created_at) continue\n produced += (row.output_tokens ?? 0) + (row.reasoning_tokens ?? 0)\n window += row.streamed_at - row.created_at\n }\n const previous = result.length > 0 && result[result.length - 1]!.sessionID === last.session_id ? result[result.length - 1] : undefined\n result.push({\n sessionID: last.session_id,\n promptAt,\n endAt: last.completed_at ?? last.created_at ?? last.recorded_at,\n // The gap is against the previous *turn*, not the previous prompt row:\n // a synthetic system-reminder prompt can sit between two user turns.\n promptGapMs: promptAt !== undefined && previous?.promptAt !== undefined ? Math.max(0, promptAt - previous.promptAt) : undefined,\n providerID,\n modelID,\n variant,\n providerChanged: previous?.providerID !== undefined && providerID !== undefined && previous.providerID !== providerID,\n modelChanged:\n previous !== undefined &&\n previous.providerID === providerID &&\n `${previous.modelID ?? \"\"}#${previous.variant ?? \"\"}` !== `${modelID ?? \"\"}#${variant ?? \"\"}`,\n steps: bucket.length,\n latest: stepUsage(last),\n totals,\n drops,\n finish: last.finish ?? undefined,\n tps: tokensPerSecond(produced, 0, window),\n })\n bucket = []\n }\n\n for (const row of steps) {\n if (row.session_id !== sessionID) {\n close()\n sessionID = row.session_id\n promptIndex = -2\n promptAt = undefined\n }\n const time = row.created_at ?? row.recorded_at\n const prompts = promptsBySession.get(row.session_id) ?? []\n let index = -1\n for (let candidate = prompts.length - 1; candidate >= 0; candidate -= 1) {\n if ((prompts[candidate] ?? Number.POSITIVE_INFINITY) <= time) {\n index = candidate\n break\n }\n }\n if (index !== promptIndex) {\n close()\n promptIndex = index\n promptAt = index >= 0 ? prompts[index] : undefined\n }\n bucket.push(row)\n }\n close()\n\n // Once the window reaches a prompt at or before the range's start, no older\n // prompt can be the newest one before a step inside the range: the window\n // already covers every turn the range can contain.\n const reachedRangeStart = options?.range?.from !== undefined && floor !== undefined && floor <= options.range.from\n return { rows: result.reverse(), complete: floor === undefined || reachedRangeStart }\n }\n\n let promptCount = Math.max(1, limit + 1)\n for (;;) {\n const { rows, complete } = read(promptCount)\n if (rows.length >= limit || complete) return rows.slice(0, limit)\n const next = promptCount * 2 + 1\n if (!Number.isFinite(next) || next <= promptCount) return rows.slice(0, limit)\n promptCount = next\n }\n}\n\n/**\n * Creation time of the `count`-th newest prompt of a session, or undefined\n * when it has fewer. Used to bound the steps a `turns` read has to load.\n */\nfunction promptFloor(db: Database, sessionID: string, count: number): number | undefined {\n const rows = db\n .query(\"SELECT created_at FROM prompts WHERE session_id = ? AND created_at IS NOT NULL ORDER BY created_at DESC LIMIT ?\")\n .all(sessionID, count) as { created_at: number }[]\n return rows.length === count ? rows[count - 1]!.created_at : undefined\n}\n\n// ---------------------------------------------------------------------------\n// Filter pickers\n// ---------------------------------------------------------------------------\n\nexport type FilterOption = { value: string; steps: number }\n\n/** Distinct models and providers, most used first. */\nexport function filterOptions(db: Database): { models: FilterOption[]; providers: FilterOption[] } {\n const pick = (column: string): FilterOption[] =>\n (\n db\n .query(`SELECT ${column} AS value, COUNT(*) AS steps FROM steps WHERE ${column} IS NOT NULL GROUP BY ${column} ORDER BY steps DESC`)\n .all() as { value: string; steps: number }[]\n ).map((row) => ({ value: row.value, steps: row.steps }))\n return { models: pick(\"model_id\"), providers: pick(\"provider_id\") }\n}\n",
|
|
12
|
+
"import type { Database } from \"bun:sqlite\"\nimport { recordEvent, recordPrompt, recordStep, recordUsage, upsertSession } from \"./stats/db\"\nimport { COMPACTION_KIND } from \"./stats/queries\"\nimport type { PriceFn } from \"./pricing\"\n\n/**\n * Event-driven recorder. Every handler is idempotent and defensive: the\n * plugin must never break a session because an event arrived in an\n * unexpected shape, so unknown events are ignored and malformed payloads are\n * dropped rather than thrown.\n *\n * Everything written comes from the event stream: the step's provider, model\n * and variant, its tokens, its timing and its outcome. Pure with respect to\n * its inputs (database plus events) so the whole recording policy can be\n * unit-tested without the plugin runtime.\n */\n\nexport type SessionModelRef = {\n id: string\n providerID: string\n variant?: string | undefined\n}\n\nexport type TokenUsageLike = {\n input?: number | undefined\n output?: number | undefined\n reasoning?: number | undefined\n cache?: { read?: number | undefined; write?: number | undefined } | undefined\n}\n\nexport type RecorderEventData = {\n sessionID?: string | undefined\n assistantMessageID?: string | undefined\n /** Host usage source: `generate`, `compaction` or `title`. */\n source?: string | undefined\n inboxID?: string | undefined\n item?: { type?: string | undefined } | undefined\n started?: number | undefined\n model?: SessionModelRef | undefined\n previous?: SessionModelRef | undefined\n tokens?: TokenUsageLike | undefined\n cost?: number | undefined\n finish?: string | undefined\n rawFinish?: string | undefined\n reason?: string | undefined\n location?: { directory?: string | undefined } | undefined\n parentID?: string | undefined\n title?: string | undefined\n}\n\nexport type RecorderEvent = {\n /** Host event ID: the idempotency key for usage and compaction rows. */\n id?: string | undefined\n type: string\n created?: number | undefined\n data?: RecorderEventData | undefined\n}\n\nexport type PendingStep = {\n messageID: string\n sessionID: string\n started: number | undefined\n streamed: number | undefined\n model: SessionModelRef | undefined\n}\n\nexport type RecorderOptions = {\n /** Clock override, for tests. */\n now?: (() => number) | undefined\n /**\n * List-price estimate for a step the host reported as costing nothing.\n * Undefined keeps the recorder on charged money alone.\n */\n price?: PriceFn | undefined\n}\n\nexport type Recorder = {\n /** Apply one session event. Unknown or malformed events are ignored. */\n handleEvent(event: RecorderEvent): void\n pendingFor(sessionID: string): PendingStep | undefined\n}\n\nexport function createRecorder(db: Database, options?: RecorderOptions): Recorder {\n /**\n * Ceiling on the per-session maps. A session that ends takes its entries\n * with it; a host running for weeks has thousands of sessions, and a map that\n * only ever grows is a leak with a nice name.\n */\n const MAX_TRACKED_SESSIONS = 512\n /** Ceiling on in-flight steps; a step that never ends is a leak too. */\n const MAX_PENDING = 256\n\n /** Steps in flight between `step.started` and `step.ended`. */\n const pending = new Map<string, PendingStep>()\n /** Last model selected per session, used when a step starts unobserved. */\n const lastModel = new Map<string, SessionModelRef>()\n const now = options?.now ?? (() => Date.now())\n\n /**\n * Ceiling on the writes kept while the database is busy. A maintenance\n * operation (a prune, a vacuum) holds the write lock; waiting for it would\n * stall the agent, and dropping the record would lose usage. The recorder\n * defers instead, retrying on the next event, and only a lock held for longer\n * than this many records takes a record with it.\n */\n const MAX_DEFERRED = 256\n const deferred: Array<() => void> = []\n\n /** True when the database was locked rather than the statement being wrong. */\n function isBusy(error: unknown): boolean {\n const code = (error as { code?: unknown } | null | undefined)?.code\n return code === \"SQLITE_BUSY\" || code === \"SQLITE_LOCKED\"\n }\n\n function enqueue(run: () => void): void {\n if (deferred.length >= MAX_DEFERRED) deferred.shift()\n deferred.push(run)\n }\n\n /** Retry what a busy database postponed, oldest first, stopping at the first failure. */\n function flushDeferred(): void {\n while (deferred.length > 0) {\n const next = deferred[0]!\n try {\n next()\n } catch {\n return\n }\n deferred.shift()\n }\n }\n\n /**\n * Run one write. On a busy database the work is kept for the next event\n * instead of waiting for the lock: the recorder is on the agent's path, and\n * the record is worth more late than the agent is worth stalled.\n */\n function write(run: () => void): void {\n if (deferred.length > 0) {\n flushDeferred()\n // Still locked: this write has to wait behind the queued ones.\n if (deferred.length > 0) {\n enqueue(run)\n return\n }\n }\n try {\n run()\n } catch (error) {\n if (isBusy(error)) enqueue(run)\n // Anything else is swallowed, as before: telemetry never breaks a session.\n }\n }\n\n /** Insertion-ordered map with a size ceiling: the oldest entry goes first. */\n function remember<Value>(map: Map<string, Value>, key: string, value: Value, limit: number): void {\n map.delete(key)\n map.set(key, value)\n while (map.size > limit) {\n const oldest = map.keys().next()\n if (oldest.done === true) break\n map.delete(oldest.value)\n }\n }\n\n /** Drop everything remembered about a session that is gone. */\n function forget(sessionID: string): void {\n lastModel.delete(sessionID)\n for (const [messageID, step] of pending) {\n if (step.sessionID === sessionID) pending.delete(messageID)\n }\n }\n\n function recordPendingStep(messageID: string, data: RecorderEventData, finish: string | undefined, at: number): void {\n const sessionID = data.sessionID\n if (typeof sessionID !== \"string\") return\n const info = pending.get(messageID)\n pending.delete(messageID)\n const model = info?.model ?? lastModel.get(sessionID)\n // The host's reported cost wins when it charged something. A reported zero\n // (Go's subscription) or a missing value gets the catalog estimate.\n const charged = data.cost\n const estimate =\n charged !== undefined && charged > 0\n ? undefined\n : options?.price?.(\n { providerID: model?.providerID, modelID: model?.id },\n {\n input: data.tokens?.input,\n output: data.tokens?.output,\n reasoning: data.tokens?.reasoning,\n cacheRead: data.tokens?.cache?.read,\n cacheWrite: data.tokens?.cache?.write,\n },\n )\n write(() =>\n recordStep(db, {\n messageID,\n sessionID,\n providerID: model?.providerID,\n modelID: model?.id,\n variant: model?.variant,\n input: data.tokens?.input,\n output: data.tokens?.output,\n reasoning: data.tokens?.reasoning,\n cacheRead: data.tokens?.cache?.read,\n cacheWrite: data.tokens?.cache?.write,\n cost: data.cost,\n listCost: estimate !== undefined && Number.isFinite(estimate) && estimate >= 0 ? estimate : undefined,\n createdAt: info?.started,\n streamedAt: info?.streamed,\n completedAt: at,\n finish,\n recordedAt: now(),\n }),\n )\n }\n\n function handleEvent(event: RecorderEvent): void {\n // A previous write may have been postponed: this is the cheapest moment to\n // land it, and the check costs nothing while the queue is empty.\n if (deferred.length > 0) flushDeferred()\n const data = event.data ?? {}\n const at = typeof event.created === \"number\" ? event.created : now()\n const sessionID = typeof data.sessionID === \"string\" ? data.sessionID : undefined\n\n switch (event.type) {\n case \"session.created\": {\n if (sessionID === undefined) return\n write(() =>\n upsertSession(db, {\n sessionID,\n parentID: data.parentID,\n directory: data.location?.directory,\n title: data.title,\n startedAt: at,\n updatedAt: at,\n }),\n )\n if (data.model !== undefined) remember(lastModel, sessionID, data.model, MAX_TRACKED_SESSIONS)\n return\n }\n case \"session.deleted\": {\n // The session is gone: its in-flight steps will never end and nothing\n // else about it will be needed again.\n if (sessionID !== undefined) forget(sessionID)\n return\n }\n case \"session.renamed\": {\n if (sessionID === undefined) return\n write(() => upsertSession(db, { sessionID, title: data.title, updatedAt: at }))\n return\n }\n case \"session.moved\": {\n if (sessionID === undefined) return\n write(() => upsertSession(db, { sessionID, directory: data.location?.directory, updatedAt: at }))\n return\n }\n case \"session.inbox.enqueued\": {\n if (sessionID === undefined) return\n if (data.item?.type !== \"user\" && data.item?.type !== \"synthetic\") return\n // Narrowed into a local: a closure does not keep a property's narrowing.\n const inboxID = data.inboxID\n if (typeof inboxID !== \"string\") return\n write(() => recordPrompt(db, { sessionID, messageID: inboxID, createdAt: at }))\n return\n }\n case \"session.model.selected\": {\n // The session's current model, remembered so a step that started before\n // the plugin loaded is still attributed. Nothing is written for it: no\n // view reads a model-selection event.\n if (sessionID !== undefined && data.model !== undefined) remember(lastModel, sessionID, data.model, MAX_TRACKED_SESSIONS)\n return\n }\n case \"session.step.started\": {\n if (sessionID === undefined || typeof data.assistantMessageID !== \"string\") return\n remember(\n pending,\n data.assistantMessageID,\n {\n messageID: data.assistantMessageID,\n sessionID,\n started: typeof data.started === \"number\" ? data.started : undefined,\n streamed: undefined,\n model: data.model,\n },\n MAX_PENDING,\n )\n return\n }\n case \"session.step.streamed\": {\n if (typeof data.assistantMessageID !== \"string\") return\n const info = pending.get(data.assistantMessageID)\n if (info !== undefined) info.streamed = at\n return\n }\n case \"session.step.ended\": {\n if (typeof data.assistantMessageID !== \"string\") return\n recordPendingStep(data.assistantMessageID, data, data.finish ?? \"unknown\", at)\n return\n }\n case \"session.step.failed\": {\n if (typeof data.assistantMessageID !== \"string\") return\n // The step row keeps the outcome through `finish`; the error event is\n // not written because no view reads it.\n recordPendingStep(data.assistantMessageID, data, data.finish ?? \"error\", at)\n return\n }\n case \"session.usage.recorded\": {\n // A `generate` usage is the assistant step this recorder already stores\n // from `session.step.ended`; recording it again would double count it.\n // Compaction and title generations have no step of their own, and the\n // host includes them in the session's own cost, so they are kept apart\n // for the views that report a complete total.\n if (sessionID === undefined) return\n const source = typeof data.source === \"string\" ? data.source : undefined\n if (source === undefined || source === \"generate\") return\n const id =\n typeof event.id === \"string\" && event.id !== \"\" ? event.id : `${sessionID}:${source}:${at}:${data.cost ?? 0}`\n write(() =>\n recordUsage(db, {\n id,\n sessionID,\n source,\n at,\n cost: data.cost,\n input: data.tokens?.input,\n output: data.tokens?.output,\n reasoning: data.tokens?.reasoning,\n cacheRead: data.tokens?.cache?.read,\n cacheWrite: data.tokens?.cache?.write,\n }),\n )\n return\n }\n case \"session.compaction.ended\": {\n if (sessionID === undefined) return\n // The host event ID makes a replay a no-op. Older recorders left it\n // NULL, which is why the migration dedupes historical rows separately.\n const messageID =\n typeof event.id === \"string\" && event.id !== \"\" ? event.id : `compaction:${sessionID}:${at}`\n write(() =>\n recordEvent(db, {\n sessionID,\n kind: COMPACTION_KIND,\n messageID,\n at,\n data: { reason: data.reason, model: data.model, tokens: data.tokens, cost: data.cost },\n }),\n )\n return\n }\n default:\n return\n }\n }\n\n function pendingFor(sessionID: string): PendingStep | undefined {\n let found: PendingStep | undefined\n for (const item of pending.values()) {\n if (item.sessionID !== sessionID) continue\n if (found === undefined || (item.started ?? 0) >= (found.started ?? 0)) found = item\n }\n return found\n }\n\n return { handleEvent, pendingFor }\n}\n"
|
|
12
13
|
],
|
|
13
|
-
"mappings": ";;AAAA;;;ACAA;AACA;AACA;;;ACFA;AACA;AACA;AAiBO,SAAS,aAAa,CAAC,MAA0C,QAAQ,KAAK,WAAmB,QAAQ,UAAkB;AAAA,EAChI,MAAM,OAAO,KAAK,YAAY,2BAA2B,UAAU;AAAA,EACnE,IAAI,aAAa,SAAS;AAAA,IACxB,OAAO,KAAK,IAAI,gBAAgB,KAAK,QAAQ,GAAG,WAAW,OAAO,GAAG,IAAI;AAAA,EAC3E;AAAA,EACA,I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