diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index b63c58a2..5f0b556d 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -85,6 +85,7 @@ import { SqliteResourceStore } from "./store/sqliteResourceStore.ts"; import { SqliteRunStore } from "./store/sqliteRunStore.ts"; import { SqliteSessionStore } from "./store/sqliteSessionStore.ts"; import { SqliteSharedFolderStore } from "./store/sqliteSharedFolderStore.ts"; +import { SqliteUsageStore } from "./store/sqliteUsageStore.ts"; // Opening the DB applies pending drizzle-kit migrations on startup. The single // shared connection backs every store: session metadata for the REST routes and @@ -217,10 +218,14 @@ reactors.useSuperviseCitation(async (sessionId, cause, supervisorId) => { // previous process are settled once here: nothing can run before we start. // Constructed before the router so a correlation can be keyed by its asker's // open run. -const runs = new RunRegistry(new SqliteRunStore(db)); +const runStore = new SqliteRunStore(db); +const runs = new RunRegistry(runStore); void runs.failStaleRuns().then((failed) => { if (failed > 0) console.log(`[runs] settled ${failed} stale run(s)`); }); +// Per-message token/cost accounting, appended as each turn finalizes and rolled +// up by the usage route into per-session / per-agent / per-run totals. +const usage = new SqliteUsageStore(db); // Holds the outstanding request/reply correlations posted Messages open and // resolve, so "who is blocked on whom" is a listable fact and a timeout is // engine policy rather than a connector's private table. Empty until a Message @@ -308,6 +313,7 @@ const pi = new PiAgentManager( agentHandlers, memoryResolver, runs, + usage, hostResourcePreamble, (sessionId) => store.selectorsOf(sessionId), ); @@ -479,6 +485,8 @@ app.use( provokes: false, }); }, + usage, + runStore, bundleKb, ), ); diff --git a/apps/server/src/pi/piAgentManager.test.ts b/apps/server/src/pi/piAgentManager.test.ts index f82ac908..7a8d5f1c 100644 --- a/apps/server/src/pi/piAgentManager.test.ts +++ b/apps/server/src/pi/piAgentManager.test.ts @@ -23,6 +23,7 @@ import type { MemoryResolver, MemorySpawnPlan } from "../memory/resolver.ts"; import type { AgentRole } from "../memory/types.ts"; import { RunRegistry } from "../runs/runRegistry.ts"; import { InMemoryRunStore } from "../store/inMemoryRunStore.ts"; +import { InMemoryUsageStore } from "../store/inMemoryUsageStore.ts"; import type { SessionAgent } from "../store/sessionStore.ts"; import type { ResolvedSessionConfig } from "./agentConfig.ts"; import { @@ -145,6 +146,7 @@ function makeManager(options?: { statuses: SessionStatusPayload[]; runs: RunRegistry; runStore: InMemoryRunStore; + usageStore: InMemoryUsageStore; } { const spawns: SpawnRecord[] = []; const rosterUpdates: { id: string; status: string }[] = []; @@ -177,16 +179,27 @@ function makeManager(options?: { const runStore = new InMemoryRunStore(); const runs = new RunRegistry(runStore); + const usageStore = new InMemoryUsageStore(); const pi = new PiAgentManager( handlers, memory, runs, + usageStore, undefined, () => options?.selectors ?? [], fakeSpawn, ); runs.useOnSettled((run, settlement) => pi.recordSettled(run, settlement)); - return { pi, spawns, rosterUpdates, events, statuses, runs, runStore }; + return { + pi, + spawns, + rosterUpdates, + events, + statuses, + runs, + runStore, + usageStore, + }; } /** A persisted roster row with sane defaults, overridable per field. */ @@ -456,6 +469,68 @@ function flush(): Promise { return new Promise((resolve) => setImmediate(resolve)); } +/** Drives a turn whose assistant `message_end` carries token/cost usage. */ +function runTurnWithUsage( + child: FakeChild, + text: string, + usage: Record, +): void { + emitPi(child, { type: "agent_start" }); + emitPi(child, { type: "message_start", message: { role: "assistant" } }); + emitPi(child, { + type: "message_update", + assistantMessageEvent: { type: "text_delta", delta: text }, + }); + emitPi(child, { + type: "message_end", + message: { role: "assistant", content: text, usage }, + }); + emitPi(child, { type: "agent_end" }); +} + +test("an assistant turn's usage is recorded against its agent and run", async () => { + const h = makeManager(); + await h.pi.ensure("s1", "/tmp/s1"); + + h.pi.sendToAgent({ sessionId: "s1", agentId: PRIME_AGENT_ID, text: "hi" }); + const runId = h.runs.current("s1", PRIME_AGENT_ID)?.id; + + runTurnWithUsage(h.spawns[0].child, "done", { + input: 100, + output: 20, + cacheRead: 5, + cacheWrite: 0, + totalTokens: 125, + cost: { total: 0.0021 }, + }); + await flush(); + + const summary = await h.usageStore.summarize("s1"); + assert.equal(summary.total.totalTokens, 125); + assert.equal(summary.total.inputTokens, 100); + assert.equal(summary.total.cacheReadTokens, 5); + assert.equal(summary.total.costUsd, 0.0021); + assert.deepEqual( + summary.byAgent.map((agent) => agent.agentId), + [PRIME_AGENT_ID], + ); + assert.equal(summary.byRun[0]?.runId, runId); + assert.equal(summary.byRun[0]?.totals.totalTokens, 125); +}); + +test("a turn whose message carries no usage records nothing", async () => { + const h = makeManager(); + await h.pi.ensure("s1", "/tmp/s1"); + + h.pi.sendToAgent({ sessionId: "s1", agentId: PRIME_AGENT_ID, text: "hi" }); + runTurn(h.spawns[0].child, "no usage here"); + await flush(); + + const summary = await h.usageStore.summarize("s1"); + assert.equal(summary.total.totalTokens, 0); + assert.equal(summary.byAgent.length, 0); +}); + test("a prompt opens one run and every event in the turn carries its id", async () => { const h = makeManager(); await h.pi.ensure("s1", "/tmp/s1"); diff --git a/apps/server/src/pi/piAgentManager.ts b/apps/server/src/pi/piAgentManager.ts index 1f7ffee3..1df47d8b 100644 --- a/apps/server/src/pi/piAgentManager.ts +++ b/apps/server/src/pi/piAgentManager.ts @@ -41,6 +41,7 @@ import type { Settlement, } from "../runs/runRegistry.ts"; import type { SessionAgent } from "../store/sessionStore.ts"; +import type { UsageStore } from "../store/usageStore.ts"; import { type AgentConfig, getPrimeAgentConfig, @@ -86,6 +87,7 @@ import { toolActivityLabel, toSubagentInfo, TRIGGERS_EXTENSION, + usageFromMessage, } from "./utils.ts"; // Re-exported so existing consumers (sockets, routes, index) keep importing @@ -1041,6 +1043,11 @@ export class PiAgentManager { * settles them rather than having them inferred from the event stream. */ private readonly runs: RunRegistry; + /** + * Sink for per-message token/cost accounting, written as each assistant turn + * finalizes. The authority that feeds the usage route's rollups. + */ + private readonly usage: UsageStore; /** * Spawn-time projection of each session's host resources, appended to every * agent's preamble. Optional so a bare manager (e.g. a test) skips it. @@ -1082,6 +1089,7 @@ export class PiAgentManager { handlers: ConversationEventSink, memory: MemoryResolver, runs: RunRegistry, + usage: UsageStore, hostResources?: HostResourcePreamble, selectorsOf?: SessionSelectors, spawnProcess: typeof spawn = spawn, @@ -1090,6 +1098,7 @@ export class PiAgentManager { this.handlers = handlers; this.memory = memory; this.runs = runs; + this.usage = usage; this.hostResources = hostResources; this.selectorsOf = selectorsOf; } @@ -2370,6 +2379,8 @@ export class PiAgentManager { assistantTextFromMessage(event.message) || agent.accum || ""; if (content.trim()) agent.lastFinalContent = content; + this.recordUsage(sessionId, agent, event); + this.emit(sessionId, descriptor, { type: "end", messageId: agent.currentMessageId as string, @@ -2379,6 +2390,32 @@ export class PiAgentManager { }); } + /** + * Appends the finalized message's token/cost accounting, attributed to the + * agent and whatever Run it is working under. Fire-and-forget: a usage write + * must never block or fail the turn it measures. A message Pi sent with no + * `usage` (an older build, or a provider that omits it) records nothing. + */ + private recordUsage( + sessionId: string, + agent: AgentProcess, + event: PiStdoutEvent, + ): void { + const usage = usageFromMessage(event.message); + if (!usage) return; + void this.usage + .record({ + sessionId, + agentId: agent.agentId, + runId: this.runs.current(sessionId, agent.agentId)?.id, + model: agent.config.model, + usage, + }) + .catch((err: unknown) => { + console.error(`[pi:${sessionId}:${agent.agentId}] usage write`, err); + }); + } + /** * Surfaces a starting tool call as the ephemeral activity indicator. The * descriptive label deliberately persists past the tool's `tool_execution_end` diff --git a/apps/server/src/pi/types.ts b/apps/server/src/pi/types.ts index 59221eb2..a0c3f337 100644 --- a/apps/server/src/pi/types.ts +++ b/apps/server/src/pi/types.ts @@ -240,6 +240,28 @@ export interface AssistantDelta { delta?: string; } +/** + * The token/cost accounting Pi attaches to a finalized assistant message. Token + * counts are that model call's own; `cost` is already in US dollars, computed by + * Pi from its per-model rate table. Flat and all-optional because it comes + * straight from JSON and an older Pi (or a provider that omits a field) may not + * carry every key. + */ +export interface MessageUsage { + input?: number; + output?: number; + cacheRead?: number; + cacheWrite?: number; + totalTokens?: number; + cost?: { + input?: number; + output?: number; + cacheRead?: number; + cacheWrite?: number; + total?: number; + }; +} + /** * A parsed line from a Pi process's stdout (JSONL RPC stream). Kept as a flat * shape (rather than a discriminated union) because the `type` field comes @@ -254,7 +276,12 @@ export interface PiStdoutEvent { * Present on `message_start` / `message_update` / `message_end` events. An * assistant message's `stopReason` is `error` when the model call failed. */ - message?: { role?: string; content?: unknown; stopReason?: string }; + message?: { + role?: string; + content?: unknown; + stopReason?: string; + usage?: MessageUsage; + }; /** Present on `tool_execution_*` events. */ toolName?: string; /** Present on `tool_execution_start` / `tool_execution_update` events. */ diff --git a/apps/server/src/pi/utils.test.ts b/apps/server/src/pi/utils.test.ts new file mode 100644 index 00000000..a1f9e48d --- /dev/null +++ b/apps/server/src/pi/utils.test.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { usageFromMessage } from "./utils.ts"; + +test("usageFromMessage lifts a full usage object onto the wire totals", () => { + const usage = usageFromMessage({ + usage: { + input: 100, + output: 20, + cacheRead: 5, + cacheWrite: 2, + totalTokens: 127, + cost: { input: 0.1, output: 0.2, total: 0.0031 }, + }, + }); + assert.deepEqual(usage, { + inputTokens: 100, + outputTokens: 20, + cacheReadTokens: 5, + cacheWriteTokens: 2, + totalTokens: 127, + costUsd: 0.0031, + }); +}); + +test("usageFromMessage returns undefined when the message carries no usage", () => { + assert.equal(usageFromMessage(undefined), undefined); + assert.equal(usageFromMessage({}), undefined); +}); + +test("usageFromMessage tolerates partial usage, defaulting missing fields to 0", () => { + const usage = usageFromMessage({ usage: { input: 10, output: 5 } }); + assert.deepEqual(usage, { + inputTokens: 10, + outputTokens: 5, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalTokens: 15, + costUsd: 0, + }); +}); + +test("usageFromMessage falls back to summing tokens when totalTokens is absent", () => { + const usage = usageFromMessage({ + usage: { input: 100, output: 20, cacheRead: 5, cacheWrite: 5 }, + }); + assert.equal(usage?.totalTokens, 130); +}); diff --git a/apps/server/src/pi/utils.ts b/apps/server/src/pi/utils.ts index b6b8bd4b..c5f6e4b8 100644 --- a/apps/server/src/pi/utils.ts +++ b/apps/server/src/pi/utils.ts @@ -1,12 +1,17 @@ import path from "node:path"; import { StringDecoder } from "node:string_decoder"; -import { connectorFor, type SubagentInfo } from "@tangent/shared/contracts.ts"; +import { + connectorFor, + type SubagentInfo, + type UsageTotals, +} from "@tangent/shared/contracts.ts"; import type { AgentDescriptor, AgentProcess, AssistantDelta, + MessageUsage, PiStdoutEvent, } from "./types.ts"; @@ -233,6 +238,38 @@ export function assistantTextFromMessage(message: unknown): string | undefined { return assistantText(message); } +/** A finite number, or 0 — so a missing/NaN field never poisons a sum. */ +function num(value: unknown): number { + return typeof value === "number" && Number.isFinite(value) ? value : 0; +} + +/** + * Lifts the token/cost accounting off a finalized assistant message into the + * wire {@link UsageTotals}. Returns `undefined` when the message carries no + * `usage` at all (the signal not to record anything), but tolerates partial + * usage — any absent field reads as 0. + */ +export function usageFromMessage( + message: { usage?: MessageUsage } | undefined, +): UsageTotals | undefined { + const usage = message?.usage; + if (!usage || typeof usage !== "object") return undefined; + const input = num(usage.input); + const output = num(usage.output); + const cacheRead = num(usage.cacheRead); + const cacheWrite = num(usage.cacheWrite); + const totalTokens = + num(usage.totalTokens) || input + output + cacheRead + cacheWrite; + return { + inputTokens: input, + outputTokens: output, + cacheReadTokens: cacheRead, + cacheWriteTokens: cacheWrite, + totalTokens, + costUsd: num(usage.cost?.total), + }; +} + /** Max length of an arg hint before it is ellipsized in the activity label. */ const MAX_HINT_LENGTH = 80; diff --git a/apps/server/src/routes/sessions/handlers.ts b/apps/server/src/routes/sessions/handlers.ts index 2c6af2c4..1331cda5 100644 --- a/apps/server/src/routes/sessions/handlers.ts +++ b/apps/server/src/routes/sessions/handlers.ts @@ -3,12 +3,16 @@ import fs from "node:fs"; import path from "node:path"; import type { + AgentUsage, Attachment, HostResourceInput, MemoryBinding, + Run, + RunUsage, Session, SessionActivity, SessionConfigMeta, + SessionUsage, UploadFilesResponse, UserIdentity, } from "@tangent/shared/contracts.ts"; @@ -42,8 +46,14 @@ import type { PiAgentManager } from "../../pi/piAgentManager.ts"; import type { TriggerEngine } from "../../pi/triggers/triggerEngine.ts"; import type { AgentBundleStore } from "../../store/agentBundleStore.ts"; import { readActivity } from "../../store/chatLog.ts"; -import type { SessionStore } from "../../store/sessionStore.ts"; +import type { RunStore } from "../../store/runStore.ts"; +import type { SessionAgent, SessionStore } from "../../store/sessionStore.ts"; import type { SharedFolderStore } from "../../store/sharedFolderStore.ts"; +import type { + AgentUsageTotals, + RunUsageTotals, + UsageStore, +} from "../../store/usageStore.ts"; import { injectPageBridge } from "./pageBridge.ts"; import type { CreateSessionInput, @@ -524,6 +534,98 @@ async function applyArchiveState( deps.triggerEngine.disarm(session.id); } +/** Builds one agent's row: its roster metadata, or id-only defaults. */ +function agentUsage( + { agentId, totals }: AgentUsageTotals, + agent: SessionAgent | undefined, +): AgentUsage { + if (!agent) { + return { + agentId, + name: agentId, + role: "subagent", + status: "completed", + totals, + }; + } + return { + agentId, + name: agent.name, + role: agent.role, + purpose: agent.purpose, + status: agent.status, + totals, + }; +} + +/** Enriches a store's per-agent totals with each agent's roster metadata. */ +function enrichAgents( + byAgent: AgentUsageTotals[], + agents: SessionAgent[], +): AgentUsage[] { + const roster = new Map(agents.map((agent) => [agent.id, agent])); + return byAgent.map((row) => agentUsage(row, roster.get(row.agentId))); +} + +/** Builds one run's row: its ingress/status, or defaults for an unknown run. */ +function runUsage( + { runId, totals }: RunUsageTotals, + run: Run | undefined, +): RunUsage { + if (!run) { + return { + runId, + participantId: "", + ingress: "reaction", + status: "completed", + totals, + }; + } + return { + runId, + participantId: run.participantId, + ingress: run.ingress, + status: run.status, + totals, + }; +} + +/** Enriches a store's per-run totals with each run's ingress and status. */ +function enrichRuns(byRun: RunUsageTotals[], runs: Run[]): RunUsage[] { + const index = new Map(runs.map((run) => [run.id, run])); + return byRun.map((row) => runUsage(row, index.get(row.runId))); +} + +/** + * Handles `GET /api/sessions/:id/usage`: the session's token/cost total plus + * the per-agent and per-run (task) breakdowns. The store rolls up the raw ids; + * this enriches them with roster and run metadata the store does not hold. + */ +export async function handleGetSessionUsage( + store: SessionStore, + usageStore: UsageStore, + runStore: RunStore, + id: string, + res: Response, +): Promise { + const session = await loadSession(store, res, id); + if (!session) return; + + const [summary, agents, runs] = await Promise.all([ + usageStore.summarize(id), + store.listAgents(id), + runStore.listRuns(id), + ]); + + const usage: SessionUsage = { + sessionId: id, + total: summary.total, + byAgent: enrichAgents(summary.byAgent, agents), + byRun: enrichRuns(summary.byRun, runs), + }; + res.json({ usage }); +} + /** Handles `PATCH /api/sessions/:id`. */ export async function handleUpdateSession( deps: SessionUpdateDeps, diff --git a/apps/server/src/routes/sessions/index.ts b/apps/server/src/routes/sessions/index.ts index 5987154b..c01cedcf 100644 --- a/apps/server/src/routes/sessions/index.ts +++ b/apps/server/src/routes/sessions/index.ts @@ -11,8 +11,10 @@ import type { TriggerEngine } from "../../pi/triggers/triggerEngine.ts"; import type { TriggerManager } from "../../pi/triggers/triggerManager.ts"; import type { UiCommandEmitter } from "../../sockets/sessionRoster.ts"; import type { AgentBundleStore } from "../../store/agentBundleStore.ts"; +import type { RunStore } from "../../store/runStore.ts"; import type { SessionStore } from "../../store/sessionStore.ts"; import type { SharedFolderStore } from "../../store/sharedFolderStore.ts"; +import type { UsageStore } from "../../store/usageStore.ts"; import { registerBundleRoutes, type SystemNotePoster, @@ -26,6 +28,7 @@ import { handleCreateSession, handleDeleteSession, handleGetSession, + handleGetSessionUsage, handleListSessions, handleMarkSessionViewed, handleUpdateSession, @@ -71,6 +74,27 @@ function registerSessionCollectionRoutes( ); } +/** Registers `GET /:id/usage`: a session's token/cost totals and breakdowns. */ +function registerSessionUsageRoute( + router: Router, + store: SessionStore, + usage: UsageStore, + runStore: RunStore, +): void { + router.get( + "/:id/usage", + validate({ params: sessionParamsSchema }), + (req: Request, res: Response) => + handleGetSessionUsage( + store, + usage, + runStore, + getValidated(req).params.id, + res, + ), + ); +} + /** Registers the single-session item routes (read/update/delete/upload). */ function registerSessionItemRoutes( router: Router, @@ -163,6 +187,8 @@ export function createSessionsRouter( environments: EnvironmentLister, emitUiCommand: UiCommandEmitter, postSystemNote: SystemNotePoster, + usage: UsageStore, + runStore: RunStore, bundleKb?: BundleKbRegistry, ): Router { const router = Router(); @@ -182,6 +208,7 @@ export function createSessionsRouter( registerSessionCollectionRoutes(router, createDeps); registerSessionItemRoutes(router, store, pi, triggerEngine); + registerSessionUsageRoute(router, store, usage, runStore); registerSessionActivityRoutes(router, store); registerBundleRoutes(router, { store, diff --git a/apps/server/src/routes/sessions/usage.test.ts b/apps/server/src/routes/sessions/usage.test.ts new file mode 100644 index 00000000..fab8dd04 --- /dev/null +++ b/apps/server/src/routes/sessions/usage.test.ts @@ -0,0 +1,132 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import type { SessionUsage, UsageTotals } from "@tangent/shared/contracts.ts"; +import type { Response } from "express"; + +import { InMemoryRunStore } from "../../store/inMemoryRunStore.ts"; +import { InMemorySessionStore } from "../../store/inMemorySessionStore.ts"; +import { InMemoryUsageStore } from "../../store/inMemoryUsageStore.ts"; +import { handleGetSessionUsage } from "./handlers.ts"; + +class TestResponse { + statusCode = 200; + body: unknown; + + status(code: number): this { + this.statusCode = code; + return this; + } + + json(body: unknown): this { + this.body = body; + return this; + } +} + +function totals(overrides: Partial): UsageTotals { + return { + inputTokens: 0, + outputTokens: 0, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalTokens: 0, + costUsd: 0, + ...overrides, + }; +} + +/** Seeds a session with two agents, one run, and usage against each. */ +async function seedSession() { + const store = new InMemorySessionStore(); + const runStore = new InMemoryRunStore(); + const usageStore = new InMemoryUsageStore(); + + const session = await store.createSession({ name: "S" }); + await store.recordAgent(session.id, { + id: "prime", + role: "prime", + name: "Prime", + }); + await store.recordAgent(session.id, { + id: "worker-1", + role: "subagent", + name: "Worker", + purpose: "Crunch the CSV", + }); + await runStore.createRun({ + id: "run-a", + sessionId: session.id, + participantId: "worker-1", + homeConversationId: "worker-1", + ingress: "tool", + }); + await usageStore.record({ + sessionId: session.id, + agentId: "prime", + usage: totals({ inputTokens: 100, totalTokens: 100, costUsd: 0.01 }), + }); + await usageStore.record({ + sessionId: session.id, + agentId: "worker-1", + runId: "run-a", + usage: totals({ outputTokens: 40, totalTokens: 40, costUsd: 0.04 }), + }); + return { store, runStore, usageStore, sessionId: session.id }; +} + +/** Resolves the `SessionUsage` the endpoint writes for `sessionId`. */ +async function fetchUsage(deps: Awaited>) { + const res = new TestResponse(); + await handleGetSessionUsage( + deps.store, + deps.usageStore, + deps.runStore, + deps.sessionId, + res as unknown as Response, + ); + return (res.body as { usage: SessionUsage }).usage; +} + +test("the usage endpoint totals a session and names each agent", async () => { + const deps = await seedSession(); + const usage = await fetchUsage(deps); + + assert.equal(usage.sessionId, deps.sessionId); + assert.equal(usage.total.totalTokens, 140); + assert.equal(Number(usage.total.costUsd.toFixed(4)), 0.05); + + const worker = usage.byAgent.find((agent) => agent.agentId === "worker-1"); + assert.ok(worker, "the worker appears in the per-agent breakdown"); + assert.equal(worker.name, "Worker"); + assert.equal(worker.purpose, "Crunch the CSV"); + assert.equal(worker.role, "subagent"); + assert.equal(worker.totals.totalTokens, 40); +}); + +test("the usage endpoint breaks spend down by run", async () => { + const deps = await seedSession(); + const usage = await fetchUsage(deps); + + assert.equal(usage.byRun.length, 1); + const [run] = usage.byRun; + assert.ok(run, "the run appears in the per-run breakdown"); + assert.equal(run.runId, "run-a"); + assert.equal(run.ingress, "tool"); + assert.equal(run.participantId, "worker-1"); +}); + +test("the usage endpoint 404s for an unknown session", async () => { + const store = new InMemorySessionStore(); + const res = new TestResponse(); + + await handleGetSessionUsage( + store, + new InMemoryUsageStore(), + new InMemoryRunStore(), + "nope", + res as unknown as Response, + ); + + assert.equal(res.statusCode, 404); +}); diff --git a/apps/server/src/store/db/migrations/0027_flat_dragon_man.sql b/apps/server/src/store/db/migrations/0027_flat_dragon_man.sql new file mode 100644 index 00000000..abe34b54 --- /dev/null +++ b/apps/server/src/store/db/migrations/0027_flat_dragon_man.sql @@ -0,0 +1,19 @@ +CREATE TABLE `usage_events` ( + `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, + `session_id` text NOT NULL, + `agent_id` text NOT NULL, + `run_id` text, + `model` text, + `input_tokens` integer DEFAULT 0 NOT NULL, + `output_tokens` integer DEFAULT 0 NOT NULL, + `cache_read_tokens` integer DEFAULT 0 NOT NULL, + `cache_write_tokens` integer DEFAULT 0 NOT NULL, + `total_tokens` integer DEFAULT 0 NOT NULL, + `cost_usd` real DEFAULT 0 NOT NULL, + `created_at` text NOT NULL, + FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE cascade +); +--> statement-breakpoint +CREATE INDEX `usage_events_session_idx` ON `usage_events` (`session_id`);--> statement-breakpoint +CREATE INDEX `usage_events_session_agent_idx` ON `usage_events` (`session_id`,`agent_id`);--> statement-breakpoint +CREATE INDEX `usage_events_session_run_idx` ON `usage_events` (`session_id`,`run_id`); \ No newline at end of file diff --git a/apps/server/src/store/db/migrations/meta/0027_snapshot.json b/apps/server/src/store/db/migrations/meta/0027_snapshot.json new file mode 100644 index 00000000..8cb53ba9 --- /dev/null +++ b/apps/server/src/store/db/migrations/meta/0027_snapshot.json @@ -0,0 +1,1175 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "00e72db8-ba76-45db-8f60-a3461b3e5e26", + "prevId": "ef44c691-6163-4834-bb62-5c5dee6fbc3e", + "tables": { + "bundle_editors": { + "name": "bundle_editors", + "columns": { + "email": { + "name": "email", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "added_by": { + "name": "added_by", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "conversations": { + "name": "conversations", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "next_seq": { + "name": "next_seq", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 1 + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "conversations_session_idx": { + "name": "conversations_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "conversations_session_agent_idx": { + "name": "conversations_session_agent_idx", + "columns": ["session_id", "agent_id"], + "isUnique": false + }, + "conversations_session_id": { + "name": "conversations_session_id", + "columns": ["session_id", "id"], + "isUnique": true + } + }, + "foreignKeys": { + "conversations_session_id_sessions_id_fk": { + "name": "conversations_session_id_sessions_id_fk", + "tableFrom": "conversations", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "memberships": { + "name": "memberships", + "columns": { + "participant_id": { + "name": "participant_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "conversation_id": { + "name": "conversation_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reaction": { + "name": "reaction", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'never'" + }, + "muted": { + "name": "muted", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "ingress": { + "name": "ingress", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'reaction'" + }, + "admission": { + "name": "admission", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'queue'" + }, + "transcript_visibility": { + "name": "transcript_visibility", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'shared'" + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "memberships_session_conversation_idx": { + "name": "memberships_session_conversation_idx", + "columns": ["session_id", "conversation_id"], + "isUnique": false + }, + "memberships_session_conversation_participant": { + "name": "memberships_session_conversation_participant", + "columns": ["session_id", "conversation_id", "participant_id"], + "isUnique": true + } + }, + "foreignKeys": { + "memberships_session_id_sessions_id_fk": { + "name": "memberships_session_id_sessions_id_fk", + "tableFrom": "memberships", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "participants": { + "name": "participants", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "kind": { + "name": "kind", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "display_name": { + "name": "display_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "capabilities": { + "name": "capabilities", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'[]'" + }, + "presence": { + "name": "presence", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'connected'" + }, + "connector_kind": { + "name": "connector_kind", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "connector_lifecycle": { + "name": "connector_lifecycle", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "connector_environment_id": { + "name": "connector_environment_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "connector_endpoint_url": { + "name": "connector_endpoint_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "agent_payload": { + "name": "agent_payload", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "revoked_at": { + "name": "revoked_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "participants_session_idx": { + "name": "participants_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "participants_session_id": { + "name": "participants_session_id", + "columns": ["session_id", "id"], + "isUnique": true + } + }, + "foreignKeys": { + "participants_session_id_sessions_id_fk": { + "name": "participants_session_id_sessions_id_fk", + "tableFrom": "participants", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "reactor_state": { + "name": "reactor_state", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "participant_id": { + "name": "participant_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "home_conversation_id": { + "name": "home_conversation_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "scope_key": { + "name": "scope_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "spec": { + "name": "spec", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "reactor_state_session_idx": { + "name": "reactor_state_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "reactor_state_session_participant_idx": { + "name": "reactor_state_session_participant_idx", + "columns": ["session_id", "participant_id"], + "isUnique": false + }, + "reactor_state_session_participant_scope": { + "name": "reactor_state_session_participant_scope", + "columns": ["session_id", "participant_id", "scope_key"], + "isUnique": true + } + }, + "foreignKeys": { + "reactor_state_session_id_sessions_id_fk": { + "name": "reactor_state_session_id_sessions_id_fk", + "tableFrom": "reactor_state", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "resource_grants": { + "name": "resource_grants", + "columns": { + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "conversation_id": { + "name": "conversation_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "participant_id": { + "name": "participant_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "resource_id": { + "name": "resource_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "resource_grants_conversation_participant_idx": { + "name": "resource_grants_conversation_participant_idx", + "columns": ["session_id", "conversation_id", "participant_id"], + "isUnique": false + }, + "resource_grants_conversation_participant_resource": { + "name": "resource_grants_conversation_participant_resource", + "columns": ["conversation_id", "participant_id", "resource_id"], + "isUnique": true + } + }, + "foreignKeys": { + "resource_grants_session_id_sessions_id_fk": { + "name": "resource_grants_session_id_sessions_id_fk", + "tableFrom": "resource_grants", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "resource_grants_resource_id_resources_id_fk": { + "name": "resource_grants_resource_id_resources_id_fk", + "tableFrom": "resource_grants", + "tableTo": "resources", + "columnsFrom": ["resource_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "resource_references": { + "name": "resource_references", + "columns": { + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "conversation_id": { + "name": "conversation_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "resource_id": { + "name": "resource_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "resource_references_conversation_idx": { + "name": "resource_references_conversation_idx", + "columns": ["session_id", "conversation_id"], + "isUnique": false + }, + "resource_references_conversation_resource": { + "name": "resource_references_conversation_resource", + "columns": ["conversation_id", "resource_id"], + "isUnique": true + } + }, + "foreignKeys": { + "resource_references_session_id_sessions_id_fk": { + "name": "resource_references_session_id_sessions_id_fk", + "tableFrom": "resource_references", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "resource_references_resource_id_resources_id_fk": { + "name": "resource_references_resource_id_resources_id_fk", + "tableFrom": "resource_references", + "tableTo": "resources", + "columnsFrom": ["resource_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "resources": { + "name": "resources", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "kind": { + "name": "kind", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "uri": { + "name": "uri", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "author_participant_id": { + "name": "author_participant_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "meta": { + "name": "meta", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "resources_session_idx": { + "name": "resources_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "resources_session_uri": { + "name": "resources_session_uri", + "columns": ["session_id", "uri"], + "isUnique": true + } + }, + "foreignKeys": { + "resources_session_id_sessions_id_fk": { + "name": "resources_session_id_sessions_id_fk", + "tableFrom": "resources", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "runs": { + "name": "runs", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "participant_id": { + "name": "participant_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "home_conversation_id": { + "name": "home_conversation_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'running'" + }, + "ingress": { + "name": "ingress", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "external_id": { + "name": "external_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cursor": { + "name": "cursor", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ended_at": { + "name": "ended_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "runs_session_idx": { + "name": "runs_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "runs_session_participant_idx": { + "name": "runs_session_participant_idx", + "columns": ["session_id", "participant_id"], + "isUnique": false + } + }, + "foreignKeys": { + "runs_session_id_sessions_id_fk": { + "name": "runs_session_id_sessions_id_fk", + "tableFrom": "runs", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "session_assets": { + "name": "session_assets", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "path": { + "name": "path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "pinned_at": { + "name": "pinned_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "session_assets_session_idx": { + "name": "session_assets_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "session_assets_session_path": { + "name": "session_assets_session_path", + "columns": ["session_id", "path"], + "isUnique": true + } + }, + "foreignKeys": { + "session_assets_session_id_sessions_id_fk": { + "name": "session_assets_session_id_sessions_id_fk", + "tableFrom": "session_assets", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "session_views": { + "name": "session_views", + "columns": { + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "user_key": { + "name": "user_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "last_viewed_at": { + "name": "last_viewed_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "session_views_session_idx": { + "name": "session_views_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "session_views_session_user": { + "name": "session_views_session_user", + "columns": ["session_id", "user_key"], + "isUnique": true + } + }, + "foreignKeys": { + "session_views_session_id_sessions_id_fk": { + "name": "session_views_session_id_sessions_id_fk", + "tableFrom": "session_views", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "sessions": { + "name": "sessions", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "root_path": { + "name": "root_path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'created'" + }, + "config": { + "name": "config", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "user_identity": { + "name": "user_identity", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "env": { + "name": "env", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "memory": { + "name": "memory", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tags": { + "name": "tags", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "archived": { + "name": "archived", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "access_policy": { + "name": "access_policy", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'open'" + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "shared_folders": { + "name": "shared_folders", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "user_identity": { + "name": "user_identity", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "usage_events": { + "name": "usage_events", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "run_id": { + "name": "run_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "model": { + "name": "model", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "input_tokens": { + "name": "input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "output_tokens": { + "name": "output_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "cache_read_tokens": { + "name": "cache_read_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "cache_write_tokens": { + "name": "cache_write_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "total_tokens": { + "name": "total_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "cost_usd": { + "name": "cost_usd", + "type": "real", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "usage_events_session_idx": { + "name": "usage_events_session_idx", + "columns": ["session_id"], + "isUnique": false + }, + "usage_events_session_agent_idx": { + "name": "usage_events_session_agent_idx", + "columns": ["session_id", "agent_id"], + "isUnique": false + }, + "usage_events_session_run_idx": { + "name": "usage_events_session_run_idx", + "columns": ["session_id", "run_id"], + "isUnique": false + } + }, + "foreignKeys": { + "usage_events_session_id_sessions_id_fk": { + "name": "usage_events_session_id_sessions_id_fk", + "tableFrom": "usage_events", + "tableTo": "sessions", + "columnsFrom": ["session_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} diff --git a/apps/server/src/store/db/migrations/meta/_journal.json b/apps/server/src/store/db/migrations/meta/_journal.json index 6a53b1a6..f7d8e910 100644 --- a/apps/server/src/store/db/migrations/meta/_journal.json +++ b/apps/server/src/store/db/migrations/meta/_journal.json @@ -190,6 +190,13 @@ "when": 1793422241629, "tag": "0026_lazy_captain_cross", "breakpoints": true + }, + { + "idx": 27, + "version": "6", + "when": 1794353181678, + "tag": "0027_flat_dragon_man", + "breakpoints": true } ] } diff --git a/apps/server/src/store/db/schema.ts b/apps/server/src/store/db/schema.ts index 01c5d456..0c09e80d 100644 --- a/apps/server/src/store/db/schema.ts +++ b/apps/server/src/store/db/schema.ts @@ -1,6 +1,7 @@ import { index, integer, + real, sqliteTable, text, unique, @@ -123,6 +124,43 @@ export const runs = sqliteTable( ], ); +/** + * One assistant message's token/cost accounting, appended as the message + * finalizes. Append-only (one row per model call) so the per-agent and per-run + * breakdowns are a `GROUP BY` rather than a running counter that a crash could + * leave half-updated. `cost_usd` is the dollar figure the agent runtime already + * computed; `run_id` is nullable because a connector can stream a turn the + * server opened no Run for. + */ +export const usageEvents = sqliteTable( + "usage_events", + { + id: integer("id").primaryKey({ autoIncrement: true }), + sessionId: text("session_id") + .notNull() + .references(() => sessions.id, { onDelete: "cascade" }), + /** The agent that produced the message (`prime` or a sub-agent id). */ + agentId: text("agent_id").notNull(), + /** The Run the turn was attributed to, when one was open. */ + runId: text("run_id"), + /** The model that served the call, for later per-model rollups. */ + model: text("model"), + inputTokens: integer("input_tokens").notNull().default(0), + outputTokens: integer("output_tokens").notNull().default(0), + cacheReadTokens: integer("cache_read_tokens").notNull().default(0), + cacheWriteTokens: integer("cache_write_tokens").notNull().default(0), + totalTokens: integer("total_tokens").notNull().default(0), + /** Combined cost in US dollars. */ + costUsd: real("cost_usd").notNull().default(0), + createdAt: text("created_at").notNull(), + }, + (table) => [ + index("usage_events_session_idx").on(table.sessionId), + index("usage_events_session_agent_idx").on(table.sessionId, table.agentId), + index("usage_events_session_run_idx").on(table.sessionId, table.runId), + ], +); + /** * Per-conversation `seq` counter: the write authority that gives a Conversation * an order rather than a race. Rows are created on first allocation, seeded @@ -463,6 +501,7 @@ export type SessionRow = typeof sessions.$inferSelect; export type SharedFolderRow = typeof sharedFolders.$inferSelect; export type SessionAssetRow = typeof sessionAssets.$inferSelect; export type RunRow = typeof runs.$inferSelect; +export type UsageEventRow = typeof usageEvents.$inferSelect; export type ConversationRow = typeof conversations.$inferSelect; export type MembershipRow = typeof memberships.$inferSelect; export type ParticipantRow = typeof participants.$inferSelect; diff --git a/apps/server/src/store/inMemoryUsageStore.ts b/apps/server/src/store/inMemoryUsageStore.ts new file mode 100644 index 00000000..489512c2 --- /dev/null +++ b/apps/server/src/store/inMemoryUsageStore.ts @@ -0,0 +1,80 @@ +import type { RunId, UsageTotals } from "@tangent/shared/contracts.ts"; + +import { + type AgentUsageTotals, + emptyTotals, + type RecordUsageInput, + type RunUsageTotals, + type UsageStore, + type UsageSummary, +} from "./usageStore.ts"; + +/** One appended accounting event, as held in memory. */ +interface UsageEvent { + sessionId: string; + agentId: string; + runId?: RunId; + usage: UsageTotals; +} + +/** Folds one event's totals into an accumulator in place. */ +function add(into: UsageTotals, usage: UsageTotals): void { + into.inputTokens += usage.inputTokens; + into.outputTokens += usage.outputTokens; + into.cacheReadTokens += usage.cacheReadTokens; + into.cacheWriteTokens += usage.cacheWriteTokens; + into.totalTokens += usage.totalTokens; + into.costUsd += usage.costUsd; +} + +/** Returns the accumulator for `key`, creating a zero one on first sight. */ +function bucket(map: Map, key: string): UsageTotals { + const existing = map.get(key); + if (existing) return existing; + const fresh = emptyTotals(); + map.set(key, fresh); + return fresh; +} + +/** + * Process-local {@link UsageStore}, mirroring {@link + * import("./inMemoryRunStore.ts").InMemoryRunStore}. For tests and for wiring a + * usage sink that has no DB to write to. + */ +export class InMemoryUsageStore implements UsageStore { + private readonly events: UsageEvent[] = []; + + async record(input: RecordUsageInput): Promise { + this.events.push({ + sessionId: input.sessionId, + agentId: input.agentId, + runId: input.runId, + usage: input.usage, + }); + } + + async summarize(sessionId: string): Promise { + const total = emptyTotals(); + const agents = new Map(); + const runs = new Map(); + + for (const event of this.events) { + if (event.sessionId !== sessionId) continue; + add(total, event.usage); + add(bucket(agents, event.agentId), event.usage); + if (event.runId) add(bucket(runs, event.runId), event.usage); + } + + const byAgent: AgentUsageTotals[] = [...agents].map( + ([agentId, totals]) => ({ + agentId, + totals, + }), + ); + const byRun: RunUsageTotals[] = [...runs].map(([runId, totals]) => ({ + runId, + totals, + })); + return { total, byAgent, byRun }; + } +} diff --git a/apps/server/src/store/sqliteUsageStore.test.ts b/apps/server/src/store/sqliteUsageStore.test.ts new file mode 100644 index 00000000..78e14308 --- /dev/null +++ b/apps/server/src/store/sqliteUsageStore.test.ts @@ -0,0 +1,113 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { after, test } from "node:test"; + +import type { UsageTotals } from "@tangent/shared/contracts.ts"; + +// Point the session root at a throwaway dir before importing modules that read +// config at load time. +const ROOT = mkdtempSync(path.join(tmpdir(), "usage-store-")); +process.env.SESSIONS_ROOT = ROOT; + +const { openDb } = await import("./db/client.ts"); +const { SqliteSessionStore } = await import("./sqliteSessionStore.ts"); +const { SqliteUsageStore } = await import("./sqliteUsageStore.ts"); + +after(() => rmSync(ROOT, { recursive: true, force: true })); + +/** A usage store over a fresh db, plus the session its rows reference. */ +async function newStore() { + const db = openDb(":memory:"); + const sessions = new SqliteSessionStore(db); + const session = await sessions.createSession({ name: "S" }); + return { store: new SqliteUsageStore(db), sessionId: session.id }; +} + +/** Token totals with sane defaults, overridable per field. */ +function totals(overrides: Partial): UsageTotals { + return { + inputTokens: 0, + outputTokens: 0, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalTokens: 0, + costUsd: 0, + ...overrides, + }; +} + +test("summarize sums every event into the session total", async () => { + const { store, sessionId } = await newStore(); + await store.record({ + sessionId, + agentId: "prime", + usage: totals({ inputTokens: 100, totalTokens: 100, costUsd: 0.01 }), + }); + await store.record({ + sessionId, + agentId: "prime", + usage: totals({ outputTokens: 50, totalTokens: 50, costUsd: 0.02 }), + }); + + const { total } = await store.summarize(sessionId); + assert.equal(total.inputTokens, 100); + assert.equal(total.outputTokens, 50); + assert.equal(total.totalTokens, 150); + assert.equal(Number(total.costUsd.toFixed(4)), 0.03); +}); + +test("summarize groups by agent and by run", async () => { + const { store, sessionId } = await newStore(); + await store.record({ + sessionId, + agentId: "prime", + runId: "run-a", + usage: totals({ totalTokens: 10, costUsd: 0.1 }), + }); + await store.record({ + sessionId, + agentId: "worker-1", + runId: "run-b", + usage: totals({ totalTokens: 30, costUsd: 0.3 }), + }); + await store.record({ + sessionId, + agentId: "worker-1", + runId: "run-b", + usage: totals({ totalTokens: 5, costUsd: 0.05 }), + }); + + const { byAgent, byRun } = await store.summarize(sessionId); + + const agents = new Map(byAgent.map((row) => [row.agentId, row.totals])); + assert.equal(agents.get("prime")?.totalTokens, 10); + assert.equal(agents.get("worker-1")?.totalTokens, 35); + + const runs = new Map(byRun.map((row) => [row.runId, row.totals])); + assert.equal(runs.get("run-a")?.totalTokens, 10); + assert.equal(runs.get("run-b")?.totalTokens, 35); +}); + +test("a run-less event joins the total and agent, not any run bucket", async () => { + const { store, sessionId } = await newStore(); + await store.record({ + sessionId, + agentId: "prime", + usage: totals({ totalTokens: 7, costUsd: 0.07 }), + }); + + const { total, byAgent, byRun } = await store.summarize(sessionId); + assert.equal(total.totalTokens, 7); + assert.equal(byAgent[0]?.totals.totalTokens, 7); + assert.deepEqual(byRun, []); +}); + +test("summarize of a session with no events is all zeros", async () => { + const { store, sessionId } = await newStore(); + const { total, byAgent, byRun } = await store.summarize(sessionId); + assert.deepEqual(total, totals({})); + assert.deepEqual(byAgent, []); + assert.deepEqual(byRun, []); +}); diff --git a/apps/server/src/store/sqliteUsageStore.ts b/apps/server/src/store/sqliteUsageStore.ts new file mode 100644 index 00000000..20226679 --- /dev/null +++ b/apps/server/src/store/sqliteUsageStore.ts @@ -0,0 +1,121 @@ +import type { RunId, UsageTotals } from "@tangent/shared/contracts.ts"; +import { and, eq, isNotNull, type SQL, sql } from "drizzle-orm"; + +import type { Db } from "./db/client.ts"; +import { usageEvents } from "./db/schema.ts"; +import type { + AgentUsageTotals, + RecordUsageInput, + RunUsageTotals, + UsageStore, + UsageSummary, +} from "./usageStore.ts"; + +/** The summed columns shared by the total and the grouped queries. */ +const totalsColumns = { + inputTokens: sql`coalesce(sum(${usageEvents.inputTokens}), 0)`, + outputTokens: sql`coalesce(sum(${usageEvents.outputTokens}), 0)`, + cacheReadTokens: sql`coalesce(sum(${usageEvents.cacheReadTokens}), 0)`, + cacheWriteTokens: sql`coalesce(sum(${usageEvents.cacheWriteTokens}), 0)`, + totalTokens: sql`coalesce(sum(${usageEvents.totalTokens}), 0)`, + costUsd: sql`coalesce(sum(${usageEvents.costUsd}), 0)`, +}; + +/** Maps a summed-columns row onto the wire {@link UsageTotals}. */ +function toTotals( + row: Record, +): UsageTotals { + return { + inputTokens: row.inputTokens, + outputTokens: row.outputTokens, + cacheReadTokens: row.cacheReadTokens, + cacheWriteTokens: row.cacheWriteTokens, + totalTokens: row.totalTokens, + costUsd: row.costUsd, + }; +} + +/** SQLite-backed {@link UsageStore} over the shared session metadata DB. */ +export class SqliteUsageStore implements UsageStore { + private readonly db: Db; + + constructor(db: Db) { + this.db = db; + } + + async record(input: RecordUsageInput): Promise { + const { usage } = input; + this.db + .insert(usageEvents) + .values({ + sessionId: input.sessionId, + agentId: input.agentId, + runId: input.runId ?? null, + model: input.model ?? null, + inputTokens: usage.inputTokens, + outputTokens: usage.outputTokens, + cacheReadTokens: usage.cacheReadTokens, + cacheWriteTokens: usage.cacheWriteTokens, + totalTokens: usage.totalTokens, + costUsd: usage.costUsd, + createdAt: new Date().toISOString(), + }) + .run(); + } + + async summarize(sessionId: string): Promise { + return { + total: this.totalFor(sessionId), + byAgent: this.byAgentFor(sessionId), + byRun: this.byRunFor(sessionId), + }; + } + + private totalFor(sessionId: string): UsageTotals { + const row = this.db + .select(totalsColumns) + .from(usageEvents) + .where(eq(usageEvents.sessionId, sessionId)) + .get(); + return toTotals(row ?? zeroRow()); + } + + private byAgentFor(sessionId: string): AgentUsageTotals[] { + const rows = this.db + .select({ agentId: usageEvents.agentId, ...totalsColumns }) + .from(usageEvents) + .where(eq(usageEvents.sessionId, sessionId)) + .groupBy(usageEvents.agentId) + .all(); + return rows.map((row) => ({ agentId: row.agentId, totals: toTotals(row) })); + } + + private byRunFor(sessionId: string): RunUsageTotals[] { + const scoped: SQL = and( + eq(usageEvents.sessionId, sessionId), + isNotNull(usageEvents.runId), + )!; + const rows = this.db + .select({ runId: usageEvents.runId, ...totalsColumns }) + .from(usageEvents) + .where(scoped) + .groupBy(usageEvents.runId) + .all(); + return rows.map((row) => ({ + runId: row.runId as RunId, + totals: toTotals(row), + })); + } +} + +/** The zero aggregate, for a session the `sum()` query found no rows for. */ +function zeroRow(): Record { + return { + inputTokens: 0, + outputTokens: 0, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalTokens: 0, + costUsd: 0, + }; +} diff --git a/apps/server/src/store/usageStore.ts b/apps/server/src/store/usageStore.ts new file mode 100644 index 00000000..bdc135f0 --- /dev/null +++ b/apps/server/src/store/usageStore.ts @@ -0,0 +1,61 @@ +import type { RunId, UsageTotals } from "@tangent/shared/contracts.ts"; + +/** The accounting a caller appends as one assistant message finalizes. */ +export interface RecordUsageInput { + sessionId: string; + /** The agent that produced the message (`prime` or a sub-agent id). */ + agentId: string; + /** The Run the turn was attributed to, when one was open. */ + runId?: RunId; + /** The model that served the call, when known. */ + model?: string; + usage: UsageTotals; +} + +/** One agent's summed spend, before name/purpose enrichment. */ +export interface AgentUsageTotals { + agentId: string; + totals: UsageTotals; +} + +/** One Run's summed spend, before ingress/status enrichment. */ +export interface RunUsageTotals { + runId: RunId; + totals: UsageTotals; +} + +/** + * A session's spend already rolled up by the store: the grand total plus the + * per-agent and per-Run groupings. The route handler enriches these raw ids + * with roster/run metadata; the store stays unaware of either. + */ +export interface UsageSummary { + total: UsageTotals; + byAgent: AgentUsageTotals[]; + byRun: RunUsageTotals[]; +} + +/** + * Durable home of per-message token/cost accounting. Written on the streaming + * hot path as a turn finalizes and read by the usage route. Kept apart from + * {@link import("./sessionStore.ts").SessionStore} for the same reason as + * {@link import("./runStore.ts").RunStore}: it is not session metadata. + */ +export interface UsageStore { + /** Appends one assistant message's accounting. */ + record(input: RecordUsageInput): Promise; + /** Rolls a session's events up into a {@link UsageSummary}. */ + summarize(sessionId: string): Promise; +} + +/** The zero {@link UsageTotals}, the identity for a session with no spend. */ +export function emptyTotals(): UsageTotals { + return { + inputTokens: 0, + outputTokens: 0, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalTokens: 0, + costUsd: 0, + }; +} diff --git a/docs/server/index.md b/docs/server/index.md index 6f2630fb..b5464b79 100644 --- a/docs/server/index.md +++ b/docs/server/index.md @@ -67,7 +67,7 @@ flowchart TB Runs["RunRegistry"] subgraph stores [SQLite stores + disk] - DB["tangent.db (sessions, agents, participants,
memberships, runs, resources)"] + DB["tangent.db (sessions, agents, participants,
memberships, runs, usage_events, resources)"] JSONL[".tangent/chats/*.jsonl transcripts"] end end @@ -143,10 +143,14 @@ SqliteResourceStore(db)`, `membershipStore = new SqliteMembershipStore(db)` — that relays a participant's streaming events, roster changes, and posted messages the same way whether it runs locally, remotely, or entirely outside Tangent. -- `runs = new RunRegistry(new SqliteRunStore(db))` — tracks which participant is - working under which run id; settles stale runs left by a previous process. -- `pi = new PiAgentManager(agentHandlers, memory, runs, hostResourcePreamble)` — - the roster of Pi processes per session (the local connector). +- `runStore = new SqliteRunStore(db)`, `runs = new RunRegistry(runStore)` — tracks + which participant is working under which run id; settles stale runs left by a + previous process. +- `usage = new SqliteUsageStore(db)` — append-only per-message token/cost + accounting, rolled up by `GET /api/sessions/:id/usage`. +- `pi = new PiAgentManager(agentHandlers, memory, runs, usage, hostResourcePreamble)` + — the roster of Pi processes per session (the local connector); records each + finalized turn's usage as it streams. - `remoteGateway`, `externalGateway`, `a2aGateway` — the other three connector gateways, all sharing `agentHandlers` and `runs`. - `connectors = createConnectorRegistry(pi, remoteGateway, externalGateway, @@ -162,7 +166,7 @@ participantService)` — arms schedule timers and posts firings into the target' The Express app mounts: - `GET /api/health` -- `/api/sessions` -> `createSessionsRouter(store, pi, triggers, triggerEngine, agentBundleStore, participantService, resourceCatalog, sharedFolderStore, memory, hostResourcePreamble, emitResourcesUpdated, …)` +- `/api/sessions` -> `createSessionsRouter(store, pi, triggers, triggerEngine, agentBundleStore, participantService, resourceCatalog, sharedFolderStore, memory, hostResourcePreamble, emitResourcesUpdated, …, usage, runStore, …)` (adds `GET /:id/usage`) - `/api/agent-bundles` -> `createAgentBundlesRouter(agentBundleStore)` - `/api/shared-folders` -> `createSharedFoldersRouter(sharedFolderStore)` (cross-session folders a session mounts into its root) - `/api/global-memory` -> `createGlobalMemoryRouter(memory)` diff --git a/docs/server/orchestrator.md b/docs/server/orchestrator.md index 3f68fe4f..f63675fb 100644 --- a/docs/server/orchestrator.md +++ b/docs/server/orchestrator.md @@ -44,9 +44,11 @@ higher-level choreography between the human, Prime, and sub-agents, see or `null`; retained in memory so a client joining mid-run can replay it. The manager is constructed with a shared `ConversationEventSink` (the same sink -every connector uses), the `MemoryManager`, the `RunRegistry`, and the host -resource preamble. The sink's handlers bridge Pi's stream into the conversation -layer rather than emitting to a room directly: +every connector uses), the `MemoryManager`, the `RunRegistry`, the `UsageStore` +(per-message token/cost accounting, written as each turn finalizes — see +[sessions-and-storage.md](./sessions-and-storage.md#usage-and-cost-accounting)), +and the host resource preamble. The sink's handlers bridge Pi's stream into the +conversation layer rather than emitting to a room directly: - `onAgentEvent(sessionId, descriptor, event)` — streaming + terminal events; the handler routes finalized content through the `ConversationRouter` (which @@ -147,7 +149,7 @@ parsed and routed through `eventHandlers`, keyed by the Pi RPC `type`: | `agent_start` | `onAgentStart` | Reset run state; emit `activity: "Thinking..."`. | | `message_start` | `onMessageStart` | Open a fresh in-flight assistant message (ignore non-assistant). | | `message_update` | `onMessageDelta` | Accumulate + relay a text/`thinking` delta; lazily emit `start` on the first one. | -| `message_end` | `onMessageEnd` | Finalize the message into its own bubble; relay sub-agent replies to Prime. | +| `message_end` | `onMessageEnd` | Finalize the message into its own bubble; record its token/cost `usage`; relay sub-agent replies to Prime. | | `tool_execution_start` | `onToolExecutionStart` | Emit a descriptive `tool` activity label. | | `queue_update` | `onQueueUpdate` | Relay the pending steer/follow-up queue as a `queue` event (drives the composer's queued-nudge indicator). | | `agent_end` | `onAgentEnd` | Clear `busy` + activity; reset run state. | @@ -206,6 +208,9 @@ Notes: `Spawning sub-agent ...`). - An empty (tool-only) assistant message that never emitted `start` finalizes silently. +- `finalizeMessage` also records the message's token/cost `usage` to the + `UsageStore`, attributed to the agent and its open Run, when Pi reported any + (see [sessions-and-storage.md](./sessions-and-storage.md#usage-and-cost-accounting)). - `emitActivity` records the value on the producing `AgentProcess` (`lastActivity`) in addition to relaying it. This is in-memory only (never persisted to disk), but it lets `handleChatJoin` replay the current activity to diff --git a/docs/server/sessions-and-storage.md b/docs/server/sessions-and-storage.md index 6f56d84a..1eb496c1 100644 --- a/docs/server/sessions-and-storage.md +++ b/docs/server/sessions-and-storage.md @@ -64,6 +64,7 @@ changes go exclusively through drizzle migrations; never alter tables ad-hoc. | `conversations` | per-Conversation `seq` counter and its owning agent; maps a Conversation id to its transcript. | | `memberships` | a `(participant, conversation)` attachment: reaction spec, ingress, transcript visibility. | | `runs` | one unit of work by one participant: status, ingress, home conversation, external id, cursor. | +| `usage_events` | append-only token/cost accounting, one row per finalized assistant message: `agent_id`, nullable `run_id`, `model`, the token columns, and `cost_usd`. Rolled up by the usage route (see [Usage and cost accounting](#usage-and-cost-accounting)). | | `resources` | the catalog: `file` / `memory` / `attachment` / `artifact` / `host` / `mount`, pointing at bytes by `uri`. | | `resource_references` | a resource surfaced into a Conversation (surfacing + citation, not a filesystem gate). | | `resource_grants` | per-Membership refinement of a reference; default-permissive (an empty table changes nothing). | @@ -78,6 +79,53 @@ facet column, defaulting to `pi-stdio` when unset. --- +## Usage and cost accounting + +Every finalized assistant turn carries the token counts and the dollar cost the +`pi` runtime computed for that model call (from its per-model rate table). The +local connector lifts that off each `message_end` message and appends it, so a +session's spend is a durable, queryable fact rather than a derived guess. + +- **Source.** Pi attaches a `usage` object to the assistant message on + `message_end`: `{ input, output, cacheRead, cacheWrite, totalTokens, cost: { total, … } }`, + `cost` already in USD. + [`PiAgentManager.finalizeMessage`](../../apps/server/src/pi/piAgentManager.ts) + reads it (via `usageFromMessage` in + [pi/utils.ts](../../apps/server/src/pi/utils.ts)) and records one row, + attributed to the producing agent and whatever [Run](./connectors.md) it was + working under. A message with no `usage` records nothing; the write is + fire-and-forget so it never blocks or fails the turn. +- **Store.** [`UsageStore`](../../apps/server/src/store/usageStore.ts) + (`SqliteUsageStore` in production, `InMemoryUsageStore` for tests) owns the + append-only `usage_events` table. `record()` appends; `summarize(sessionId)` + rolls every row up with SQL `GROUP BY` into a session total plus per-agent and + per-run groupings (a run-less turn counts toward the total and its agent but no + run bucket). +- **Endpoint.** `GET /api/sessions/:id/usage` returns the wire `SessionUsage` + ([packages/shared/src/contracts.ts](../../packages/shared/src/contracts.ts)): + + ```jsonc + { + "usage": { + "sessionId": "…", + "total": { "inputTokens": 140, "outputTokens": 40, "cacheReadTokens": 0, "cacheWriteTokens": 0, "totalTokens": 180, "costUsd": 0.05 }, + "byAgent": [{ "agentId": "worker-1", "name": "Worker", "role": "subagent", "purpose": "…", "status": "active", "totals": { … } }], + "byRun": [{ "runId": "…", "participantId": "worker-1", "ingress": "tool", "status": "completed", "totals": { … } }] + } + } + ``` + + [`handleGetSessionUsage`](../../apps/server/src/routes/sessions/handlers.ts) + enriches the store's raw ids with roster metadata (name/role/purpose/status + from `listAgents`) and run metadata (ingress/status from the `RunStore`); the + store itself stays unaware of either. + +> **Scope.** Only locally spawned pi agents (Prime + local sub-agents) are +> metered today. A sub-agent hosted in a remote environment would need its +> `usage` threaded through `RemoteAgentEvent` before it shows up here. + +--- + ## The per-session root folder Each session owns `SESSIONS_ROOT//`. After a bundle install and some agent diff --git a/docs/server/ui-server-protocol.md b/docs/server/ui-server-protocol.md index c9a8b76c..4eadde25 100644 --- a/docs/server/ui-server-protocol.md +++ b/docs/server/ui-server-protocol.md @@ -38,6 +38,7 @@ implemented under [server/src/routes/](../../server/src/routes). | `DELETE /api/sessions/:id` | Delete a session; disposes its `pi` processes and triggers. | | `POST /api/sessions/:id/viewed` | Record that the current user opened the session. | | `POST /api/sessions/:id/files` | Upload chat attachments into the session's `uploads/`. | +| `GET /api/sessions/:id/usage` | Token/cost totals for the session, with per-agent and per-run breakdowns (`SessionUsage`). | | `GET /api/sessions/:id/triggers` | List the session's triggers. | | `POST /api/sessions/:id/triggers` | Create a runtime trigger. | | `PATCH /api/sessions/:id/triggers/:triggerId` | Update a mutable trigger field. | diff --git a/packages/shared/src/contracts.ts b/packages/shared/src/contracts.ts index 2146df13..df2a1deb 100644 --- a/packages/shared/src/contracts.ts +++ b/packages/shared/src/contracts.ts @@ -896,6 +896,59 @@ export interface Run { endedAt?: string; } +/** + * Aggregated model spend for some scope (a whole session, one agent, or one + * {@link Run}). Token counts are summed across every model call; `costUsd` is + * the dollar figure the agent runtime computed from its per-model rate table, + * so no pricing lives server-side. + */ +export interface UsageTotals { + /** Non-cached input (prompt) tokens. */ + inputTokens: number; + /** Output (completion) tokens. */ + outputTokens: number; + /** Input tokens served from the prompt cache. */ + cacheReadTokens: number; + /** Input tokens written to the prompt cache. */ + cacheWriteTokens: number; + /** All token kinds combined. */ + totalTokens: number; + /** Combined cost in US dollars. */ + costUsd: number; +} + +/** One agent's share of a session's spend, in the per-agent breakdown. */ +export interface AgentUsage { + agentId: string; + name: string; + role: AgentRole; + /** The agent's task/description, when known. */ + purpose?: string; + status: SubagentStatus; + totals: UsageTotals; +} + +/** One {@link Run}'s share of a session's spend, the per-task breakdown. */ +export interface RunUsage { + runId: RunId; + participantId: string; + ingress: RunIngress; + status: RunStatus; + totals: UsageTotals; +} + +/** + * A session's model spend: the grand {@link UsageTotals} plus the per-agent and + * per-{@link Run} (task) breakdowns. The shape returned by + * `GET /api/sessions/:id/usage`. + */ +export interface SessionUsage { + sessionId: string; + total: UsageTotals; + byAgent: AgentUsage[]; + byRun: RunUsage[]; +} + /** * A request awaiting its answer (unified-model §9.4). A Message carrying a * `correlationId` opens one; a later Message whose `inReplyTo` names it resolves