/** * Summarize token usage and estimated cost: every agent's own session records, * PAL's inference log, and rtk's savings. */ import { spawnSync } from "node:child_process"; import { resolve } from "node:path"; import { AGENT_REGISTRY, type AgentName } from "../hooks/lib/agent-registry"; import { eventsSince } from "../hooks/lib/events"; import { platform } from "../hooks/lib/paths"; import { transcriptArchive } from "../hooks/lib/transcript-preserve"; import { findBinaryOnPath } from "../hooks/lib/which"; import { opencodeDatabase, readCodex, readCopilot, readOpencode, } from "./lib/agent-usage"; import { type AgentReport, parseRtkSummary, type RtkGain, type UsageData, } from "./lib/token-report"; import { readClaudeCode, readPalInference } from "./lib/usage-buckets"; const UNTRACKED_AGENTS: AgentName[] = ["cursor", "antigravity"]; function agentReports(project: string | undefined): AgentReport[] { const report = (agent: AgentName, read: () => AgentReport["usage"]) => ({ label: AGENT_REGISTRY[agent].label, usage: read(), }); return [ report("claude", () => readClaudeCode( resolve(platform.claudeDir(), "projects"), project, new Date(), transcriptArchive() ) ), report("codex", () => readCodex(platform.codexDir(), project)), report("opencode", () => readOpencode(opencodeDatabase(), project)), report("copilot", () => readCopilot(platform.copilotDir(), project)), ]; } const installedUntracked = () => UNTRACKED_AGENTS.filter((agent) => findBinaryOnPath(AGENT_REGISTRY[agent].binary)).map( (agent) => AGENT_REGISTRY[agent].label ); function rtkGain(): RtkGain { const rtk = findBinaryOnPath("rtk"); if (!rtk) return { installed: false, summary: null }; const result = spawnSync(rtk, ["gain", "--format", "json"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], }); return { installed: true, summary: parseRtkSummary(result.status, result.stdout ?? ""), }; } export function collectUsage(project: string | undefined): UsageData { return { agents: agentReports(project), pal: readPalInference(eventsSince("inference_call", new Date(0))), rtk: rtkGain(), untracked: installedUntracked(), }; }