The best way to control Claude Code, Codex and more from any computer or phone

main
ts 518 lines 20.7 KB
Raw
1 import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
2 import {
3 totalTokens,
4 type ProviderUsageReader,
5 type UsageRecord,
6 } from "@t3tools/provider-core/server/usage";
7 import * as HostProcess from "@t3tools/shared/HostProcess";
8 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Option from "effect/Option";
12 import * as Path from "effect/Path";
13 import type * as PlatformError from "effect/PlatformError";
14 import * as Semaphore from "effect/Semaphore";
15 import * as SqlClient from "effect/sql/SqlClient";
16
17 import type { OpenCodeSettings } from "../settings.ts";
18
19 function object(value: unknown): Record<string, unknown> {
20 return typeof value === "object" && value !== null && !Array.isArray(value)
21 ? (value as Record<string, unknown>)
22 : {};
23 }
24
25 function tokens(value: unknown): number {
26 return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
27 }
28
29 function text(value: unknown): string {
30 return typeof value === "string" ? value : "";
31 }
32
33 // OpenCode provider IDs whose bare model IDs LiteLLM prices under its own prefix.
34 const LITELLM_PREFIXES = new Map([["opencode-go", "opencode_go"]]);
35 const OPENCODE_GATEWAYS = new Set(["opencode", "opencode-go"]);
36
37 function parseOpenCodeMessage(
38 source: string,
39 fallback: {
40 readonly id?: string;
41 readonly sessionId?: string;
42 readonly timestampMs?: number;
43 } = {},
44 ): UsageRecord | null {
45 let parsed: unknown;
46 try {
47 parsed = JSON.parse(source);
48 } catch {
49 return null;
50 }
51 const message = object(parsed);
52 if (message.role !== undefined && message.role !== "assistant") return null;
53 const usage = object(message.tokens);
54 const cache = object(usage.cache);
55 const modelReference = object(message.model);
56 const model = text(modelReference.id) || text(modelReference.modelID) || text(message.modelID);
57 const providerId = text(modelReference.providerID) || text(message.providerID);
58 const timestampMs = object(message.time).created ?? fallback.timestampMs;
59 if (!model || typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return null;
60 const reasoningTokens = tokens(usage.reasoning);
61 const totals = {
62 uncachedInputTokens: tokens(usage.input),
63 cachedInputTokens: tokens(cache.read),
64 cacheCreationTokens: tokens(cache.write),
65 outputTokens: tokens(usage.output) + reasoningTokens,
66 reasoningTokens,
67 };
68 if (totalTokens(totals) === 0) return null;
69 const id = fallback.id || text(message.id);
70 const cost = message.cost;
71 const prefix = LITELLM_PREFIXES.get(providerId);
72 const free = OPENCODE_GATEWAYS.has(providerId) && model.endsWith("-free");
73 return {
74 provider: "opencode",
75 timestampMs,
76 model,
77 ...(prefix !== undefined && !model.includes("/") ? { rateModel: `${prefix}/${model}` } : {}),
78 sessionId: fallback.sessionId || text(message.sessionID),
79 totals,
80 // OpenCode records zero for unknown rates and paid subscriptions alike, so only its own
81 // gateways' free models report zero; shared pricing estimates every other zero.
82 reportedCostUsd:
83 typeof cost === "number" && Number.isFinite(cost) && (cost > 0 || (free && cost === 0))
84 ? cost
85 : null,
86 speed: "standard",
87 dedupeKey: id ? `opencode:${id}` : null,
88 };
89 }
90
91 export interface OpenCodeUsageReadResult {
92 readonly files: readonly { readonly path: string; readonly records: readonly UsageRecord[] }[];
93 readonly missing: boolean;
94 readonly error: boolean;
95 }
96
97 interface CachedRow {
98 readonly updated: unknown;
99 readonly size: unknown;
100 readonly created: unknown;
101 readonly record: UsageRecord | null;
102 }
103
104 // Parsed rows by `${databasePath}\0${table}`, then rowid, so unchanged rows are not read again.
105 const rowCache = new Map<string, { version: string; rows: Map<number, CachedRow> }>();
106 // Concurrent scans of the same database would each read every uncached row.
107 const scanLock = Semaphore.makeUnsafe(1);
108
109 const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound";
110 const MESSAGE_PAGE_SIZE = 64;
111
112 const isSymbolicLink = Effect.fnUntraced(function* (path: string) {
113 const fileSystem = yield* FileSystem.FileSystem;
114 return yield* Effect.isSuccess(fileSystem.readLink(path));
115 });
116
117 const entryInfo = Effect.fn("entryInfo")(function* (path: string) {
118 const fileSystem = yield* FileSystem.FileSystem;
119 if (yield* isSymbolicLink(path))
120 return { type: "SymbolicLink" as const, mtime: Option.none<Date>() };
121 return yield* fileSystem.stat(path);
122 });
123
124 const LEGACY_STAT_CONCURRENCY = 4;
125
126 // Batch stats so huge directories do not stay in memory.
127 const LEGACY_STAT_BATCH = 256;
128
129 export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
130 root: string,
131 sinceMs: number,
132 retentionCutoffMs = sinceMs,
133 ): Effect.fn.Return<OpenCodeUsageReadResult, never, FileSystem.FileSystem | Path.Path> {
134 const fileSystem = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
136 const files: { path: string; records: UsageRecord[] }[] = [];
137 const seen = new Set<string>();
138 let found = false;
139 let error = false;
140 let databaseRows = 0;
141 let databasePages = 0;
142 const append = (records: UsageRecord[], record: UsageRecord | null) => {
143 if (record === null || record.timestampMs < sinceMs) return;
144 if (record.dedupeKey !== null) {
145 if (seen.has(record.dedupeKey)) return;
146 seen.add(record.dedupeKey);
147 }
148 records.push(record);
149 };
150
151 const databases = yield* fileSystem.readDirectory(root).pipe(
152 Effect.flatMap((names) =>
153 Effect.filter(
154 names.filter((name) => /^opencode(?:-[a-zA-Z0-9_-]+)?\.db$/.test(name)),
155 (name) =>
156 entryInfo(path.join(root, name)).pipe(
157 Effect.map((info) => info.type === "File"),
158 Effect.catchTags({
159 PlatformError: (cause) =>
160 isNotFound(cause) ? Effect.succeed(false) : Effect.fail(cause),
161 }),
162 ),
163 ),
164 ),
165 Effect.map((names) =>
166 names.sort((a, b) =>
167 a === "opencode.db" ? -1 : b === "opencode.db" ? 1 : a.localeCompare(b),
168 ),
169 ),
170 Effect.catchTags({
171 PlatformError: (cause) => {
172 if (!isNotFound(cause)) error = true;
173 return Effect.succeed([]);
174 },
175 }),
176 );
177 for (const name of databases) {
178 found = true;
179 const file = { path: path.join(root, name), records: [] as UsageRecord[] };
180 files.push(file);
181 // Taken before the snapshot opens, so a write that races the scan invalidates its rows.
182 const [main, wal] = yield* Effect.forEach([file.path, `${file.path}-wal`], (target) =>
183 Effect.option(fileSystem.stat(target)),
184 );
185 const stamp = (info: Option.Option<FileSystem.File.Info>) =>
186 Option.match(info, {
187 onNone: () => "none",
188 onSome: (stat) => `${stat.size}:${Option.getOrNull(stat.mtime)?.getTime()}`,
189 });
190 // A replaced or restored database can repeat rowids and change markers, so every key includes
191 // the main file; WAL commits leave it alone until a checkpoint.
192 const identity = Option.match(main!, {
193 onNone: () => "none",
194 onSome: (stat) =>
195 `${stat.dev}:${Option.getOrNull(stat.ino)}:${Option.getOrNull(stat.birthtime)?.getTime()}/${stamp(main!)}`,
196 });
197 yield* Effect.gen(function* () {
198 const sql = yield* SqlClient.SqlClient;
199 // Fail promptly on live-writer locks instead of stalling the server.
200 yield* sql.unsafe("PRAGMA busy_timeout = 100");
201 const tables = new Set(
202 (yield* sql.unsafe<{ readonly name: unknown }>(
203 "SELECT name FROM sqlite_master WHERE type = 'table'",
204 )).map((row) => row.name),
205 );
206 if (!tables.has("message") && !tables.has("session_message")) error = true;
207 const columnsOf = (table: string) =>
208 sql
209 .unsafe<{ readonly name: unknown }>(`PRAGMA table_info(${table})`)
210 .pipe(Effect.map((rows) => new Set(rows.map((row) => row.name))));
211 // A fork copies its parent's messages through the boundary under new ids, keeping
212 // seq, creation time and data. Resolve each fork to its last copied seq so a copy
213 // names its original as (session, seq). A fork whose boundary is gone keeps its usage.
214 const forks = new Map<
215 string,
216 { readonly parent: string; readonly lastCopiedSeq: number; readonly created: number }
217 >();
218 if (
219 tables.has("session_v2") &&
220 tables.has("session_message") &&
221 (yield* columnsOf("session_message")).has("seq") &&
222 (yield* columnsOf("session_v2")).has("fork_boundary")
223 ) {
224 const resolved = yield* sql.unsafe<{
225 readonly id: unknown;
226 readonly parent: unknown;
227 readonly created: unknown;
228 readonly last_copied_seq: unknown;
229 }>(
230 `SELECT fork.id, fork.fork_session_id AS parent, fork.time_created AS created,
231 (SELECT MAX(copied.seq) FROM session_message AS copied
232 WHERE copied.session_id = fork.fork_session_id
233 AND CASE json_extract(fork.fork_boundary, '$.type')
234 WHEN 'through' THEN copied.seq <= boundary.seq
235 WHEN 'before' THEN copied.seq < boundary.seq
236 END) AS last_copied_seq
237 FROM session_v2 AS fork
238 JOIN session_message AS boundary
239 ON boundary.session_id = fork.fork_session_id
240 AND boundary.id = json_extract(fork.fork_boundary, '$.messageID')
241 WHERE fork.fork_session_id IS NOT NULL AND json_valid(fork.fork_boundary)`,
242 );
243 for (const row of resolved) {
244 if (typeof row.last_copied_seq !== "number" || typeof row.created !== "number") continue;
245 forks.set(text(row.id), {
246 parent: text(row.parent),
247 lastCopiedSeq: row.last_copied_seq,
248 created: row.created,
249 });
250 }
251 }
252 const original = (sessionId: string, seq: number, created: number) => {
253 const visited = new Set<string>();
254 let session = sessionId;
255 for (
256 let fork = forks.get(session);
257 fork !== undefined &&
258 seq <= fork.lastCopiedSeq &&
259 created < fork.created &&
260 !visited.has(session);
261 fork = forks.get(session)
262 ) {
263 visited.add(session);
264 session = fork.parent;
265 }
266 return `${session}:${seq}`;
267 };
268 const originals = new Set<string>();
269 for (const table of ["message", "session_message"] as const) {
270 if (!tables.has(table)) continue;
271 const columns = yield* columnsOf(table);
272 const timestamp = columns.has("time_created") ? "time_created" : "NULL";
273 const updated = columns.has("time_updated") ? "time_updated" : "NULL";
274 const lineage =
275 table === "session_message" && forks.size > 0 && columns.has("seq") ? "seq" : "NULL";
276 // Without time_updated an in-place edit can keep the length, so any database write counts.
277 const version = updated === "NULL" ? `${identity}/${stamp(wal!)}` : identity;
278 // OpenCode 2 records a compaction request's spend on its compaction message.
279 const predicates =
280 table === "session_message" ? ["type IN ('assistant', 'compaction')"] : [];
281 if (timestamp !== "NULL") predicates.push("time_created >= ?");
282 // List rowids and change markers so SQLite can use time indexes without reading message
283 // bodies; octet_length comes from the record header.
284 const listed = (yield* sql.unsafe<{
285 readonly row_id: number;
286 readonly updated: unknown;
287 readonly size: unknown;
288 readonly created: unknown;
289 readonly seq: unknown;
290 }>(
291 `SELECT rowid AS row_id, ${updated} AS updated, octet_length(data) AS size,
292 ${timestamp} AS created, ${lineage} AS seq
293 FROM ${table}
294 ${predicates.length ? `WHERE ${predicates.join(" AND ")}` : ""}`,
295 timestamp === "NULL" ? [] : [sinceMs],
296 )).toSorted((a, b) => a.row_id - b.row_id);
297 databaseRows += listed.length;
298 const key = `${file.path}\0${table}`;
299 const previous = rowCache.get(key);
300 const reusable = previous?.version === version ? previous.rows : undefined;
301 const cached = new Map<number, CachedRow>();
302 const misses: typeof listed = [];
303 for (const row of listed) {
304 const hit = reusable?.get(row.row_id);
305 if (hit && hit.updated === row.updated && hit.size === row.size)
306 cached.set(row.row_id, hit);
307 else misses.push(row);
308 }
309 // Filter and project in SQLite: user diffs and assistant bodies can be hundreds of MB.
310 const validAssistant = `CASE WHEN typeof(data) = 'text' AND json_valid(data)
311 THEN json_type(data, '$.role') IS NULL OR data ->> '$.role' = 'assistant' END`;
312 // Omit per-page spans; the enclosing scan records row and page counts.
313 for (let start = 0; start < misses.length; start += MESSAGE_PAGE_SIZE) {
314 const page = misses.slice(start, start + MESSAGE_PAGE_SIZE);
315 databasePages++;
316 const rows = yield* sql
317 .unsafe<{
318 readonly row_id: number;
319 readonly id: unknown;
320 readonly session_id: unknown;
321 readonly data: unknown;
322 readonly created: unknown;
323 }>(
324 `SELECT rowid AS row_id, id, session_id, ${timestamp} AS created, json_object(
325 'tokens', data -> '$.tokens', 'cost', data -> '$.cost',
326 'model', data -> '$.model', 'modelID', data -> '$.modelID',
327 'providerID', data -> '$.providerID',
328 'time', data -> '$.time', 'id', data -> '$.id', 'sessionID', data -> '$.sessionID'
329 ) AS data
330 FROM ${table}
331 WHERE rowid IN (${page.map(() => "?").join(", ")}) AND ${validAssistant}
332 ORDER BY rowid`,
333 page.map((row) => row.row_id),
334 )
335 .pipe(Effect.withTracerEnabled(false));
336 const parsed = new Map<number, UsageRecord | null>();
337 for (const row of rows) {
338 parsed.set(
339 row.row_id,
340 parseOpenCodeMessage(text(row.data), {
341 id: text(row.id),
342 sessionId: text(row.session_id),
343 ...(typeof row.created === "number" ? { timestampMs: row.created } : {}),
344 }),
345 );
346 }
347 // Rows the SQL filter dropped are cached as empty so they are not read again either.
348 for (const row of page) {
349 cached.set(row.row_id, {
350 updated: row.updated,
351 size: row.size,
352 created: row.created,
353 record: parsed.get(row.row_id) ?? null,
354 });
355 }
356 yield* Effect.yieldNow;
357 }
358 for (const row of listed) {
359 const record = cached.get(row.row_id)!.record;
360 if (record !== null && typeof row.seq === "number") {
361 const copyOf = original(record.sessionId, row.seq, record.timestampMs);
362 if (originals.has(copyOf)) continue;
363 originals.add(copyOf);
364 }
365 append(file.records, record);
366 }
367 // Rows before this window were not listed; keep them for a wider window until retention.
368 for (const [rowId, entry] of reusable ?? []) {
369 const created = entry.created;
370 if (typeof created === "number" && created < sinceMs && created >= retentionCutoffMs)
371 cached.set(rowId, entry);
372 }
373 rowCache.set(key, { version, rows: cached });
374 }
375 }).pipe(
376 (read) => Effect.flatMap(SqlClient.SqlClient, (sql) => sql.withTransaction(read)),
377 // Run the scan on a reader worker so large databases do not block the server's event loop.
378 NodeSqliteClient.readOnly,
379 Effect.provide(
380 NodeSqliteClient.layer({ filename: file.path, readonly: true, readerWorker: true }),
381 ),
382 Effect.catchTags({
383 SqlError: () => {
384 error = true;
385 return Effect.void;
386 },
387 }),
388 );
389 }
390
391 const directories = [path.join(root, "storage", "message")];
392 while (directories.length > 0) {
393 const directory = directories.pop()!;
394 yield* Effect.gen(function* () {
395 const names = yield* fileSystem.readDirectory(directory);
396 for (let start = 0; start < names.length; start += LEGACY_STAT_BATCH) {
397 const batch = names.slice(start, start + LEGACY_STAT_BATCH);
398 const entries = batch.map((name) => path.join(directory, name));
399 // stat follows symlinks; avoid expensive readLink failures unless link identity matters.
400 const infos = yield* Effect.forEach(
401 entries,
402 (entry) =>
403 fileSystem.stat(entry).pipe(
404 Effect.catchTags({
405 PlatformError: (cause) =>
406 isNotFound(cause)
407 ? Effect.succeed(null)
408 : isSymbolicLink(entry).pipe(
409 Effect.flatMap((link) =>
410 link ? Effect.succeed(null) : Effect.fail(cause),
411 ),
412 ),
413 }),
414 Effect.exit,
415 ),
416 { concurrency: LEGACY_STAT_CONCURRENCY },
417 );
418 for (const [index, name] of batch.entries()) {
419 const entry = entries[index]!;
420 const info = yield* infos[index]!;
421 if (info?.type === "Directory") {
422 if (!(yield* isSymbolicLink(entry))) directories.push(entry);
423 } else if (info?.type === "File" && name.endsWith(".json")) {
424 const id = name.slice(0, -5);
425 // Messages cannot postdate file mtime; older files hold no in-window usage.
426 const skip =
427 seen.has(`opencode:${id}`) ||
428 Option.exists(info.mtime, (mtime) => mtime.getTime() < sinceMs);
429 if (found && skip) continue;
430 if (yield* isSymbolicLink(entry)) continue;
431 found = true;
432 if (skip) continue;
433 const file = { path: entry, records: [] as UsageRecord[] };
434 files.push(file);
435 yield* fileSystem.readFileString(entry).pipe(
436 Effect.map((source) => append(file.records, parseOpenCodeMessage(source, { id }))),
437 Effect.catchTags({
438 PlatformError: (cause) => {
439 if (!isNotFound(cause)) error = true;
440 return Effect.void;
441 },
442 }),
443 );
444 }
445 }
446 }
447 }).pipe(
448 Effect.catchTags({
449 PlatformError: (cause) => {
450 if (!isNotFound(cause)) error = true;
451 return Effect.void;
452 },
453 }),
454 );
455 }
456 yield* Effect.annotateCurrentSpan({
457 "usage.database_rows": databaseRows,
458 "usage.database_pages": databasePages,
459 });
460 return { files, missing: !found && !error, error };
461 }, scanLock.withPermit);
462
463 const resolveOpenCodeDataDirs = Effect.fn("resolveOpenCodeDataDirs")(function* (
464 environment: NodeJS.ProcessEnv,
465 ) {
466 const fileSystem = yield* FileSystem.FileSystem;
467 const path = yield* Path.Path;
468 const homeDirectory = yield* HostProcess.HomeDirectory;
469 const roots = environment["OPENCODE_DATA_DIR"]
470 ?.split(",")
471 .map((value) => value.trim())
472 .filter(Boolean);
473 const dataHome = environment["XDG_DATA_HOME"]?.trim();
474 const defaults = [
475 path.join(
476 dataHome && path.isAbsolute(dataHome)
477 ? dataHome
478 : path.join(homeDirectory, ".local", "share"),
479 "opencode",
480 ),
481 ];
482 const canonical = new Set<string>();
483 for (const root of roots?.length ? roots : defaults) {
484 const resolved = path.resolve(expandHomePath(root, homeDirectory));
485 canonical.add(yield* fileSystem.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved)));
486 }
487 return [...canonical];
488 });
489
490 export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path;
491
492 export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCodeUsageReaderEnv> = {
493 kind: "scan",
494 provider: "opencode",
495 scan: Effect.fn("openCodeUsageReader.scan")(function* ({
496 instances,
497 windowStartMs,
498 retentionCutoffMs,
499 }) {
500 const roots = new Set<string>();
501 for (const instance of instances) {
502 for (const root of yield* resolveOpenCodeDataDirs(instance.environment)) roots.add(root);
503 }
504 return yield* Effect.forEach(
505 roots,
506 (dir) =>
507 readOpenCodeUsage(dir, windowStartMs, retentionCutoffMs).pipe(
508 Effect.map((result) => ({
509 dir,
510 files: result.missing && !result.error ? null : result.files,
511 status: result.error ? ("partial" as const) : ("ok" as const),
512 ...(result.error ? { message: "Some OpenCode history could not be read." } : {}),
513 })),
514 ),
515 { concurrency: "unbounded" },
516 );
517 }),
518 };