Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usage.ts

371 lines14.0 KB

1/**
2 * Usage history for OpenCode, read from its SQLite databases and legacy JSON
3 * message store under each data directory.
4 *
5 * @module provider-opencode/server/usage
6 */
7​
8import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
9import {
10 totalTokens,
11 type ProviderUsageReader,
12 type UsageRecord,
13} from "@t3tools/provider-core/server/usage";
14import * as HostProcess from "@t3tools/shared/HostProcess";
15import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
16import * as Effect from "effect/Effect";
17import * as FileSystem from "effect/FileSystem";
18import * as Option from "effect/Option";
19import * as Path from "effect/Path";
20import type * as PlatformError from "effect/PlatformError";
21import * as SqlClient from "effect/sql/SqlClient";
22​
23import type { OpenCodeSettings } from "../settings.ts";
24​
25function object(value: unknown): Record<string, unknown> {
26 return typeof value === "object" && value !== null && !Array.isArray(value)
27 ? (value as Record<string, unknown>)
28 : {};
30​
31function tokens(value: unknown): number {
32 return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
34​
35function text(value: unknown): string {
36 return typeof value === "string" ? value : "";
38​
39/** OpenCode stores uncached input and reasoning separately from input/output. */
40function parseOpenCodeMessage(
41 source: string,
42 fallback: {
43 readonly id?: string;
44 readonly sessionId?: string;
45 readonly timestampMs?: number;
46 } = {},
47): UsageRecord | null {
48 let parsed: unknown;
49 try {
50 parsed = JSON.parse(source);
51 } catch {
52 return null;
53 }
54 const message = object(parsed);
55 if (message.role !== undefined && message.role !== "assistant") return null;
56 const usage = object(message.tokens);
57 const cache = object(usage.cache);
58 const modelReference = object(message.model);
59 const model = text(modelReference.id) || text(modelReference.modelID) || text(message.modelID);
60 const timestampMs = object(message.time).created ?? fallback.timestampMs;
61 if (!model || typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return null;
62 const reasoningTokens = tokens(usage.reasoning);
63 const totals = {
64 uncachedInputTokens: tokens(usage.input),
65 cachedInputTokens: tokens(cache.read),
66 cacheCreationTokens: tokens(cache.write),
67 outputTokens: tokens(usage.output) + reasoningTokens,
68 reasoningTokens,
69 };
70 if (totalTokens(totals) === 0) return null;
71 const id = fallback.id || text(message.id);
72 const cost = message.cost;
73 return {
74 provider: "opencode",
75 timestampMs,
76 model,
77 sessionId: fallback.sessionId || text(message.sessionID),
78 totals,
79 // OpenCode writes zero for models without a known rate, including paid
80 // subscription models. Let the shared price table estimate those records.
81 reportedCostUsd: typeof cost === "number" && Number.isFinite(cost) && cost > 0 ? cost : null,
82 speed: "standard",
83 dedupeKey: id ? `opencode:${id}` : null,
84 };
86​
87export interface OpenCodeUsageReadResult {
88 readonly files: readonly { readonly path: string; readonly records: readonly UsageRecord[] }[];
89 readonly missing: boolean;
90 readonly error: boolean;
92​
93const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound";
94const MESSAGE_PAGE_SIZE = 64;
95​
96/** Whether the path is itself a symlink, without following it. */
97const isSymbolicLink = Effect.fnUntraced(function* (path: string) {
98 const fileSystem = yield* FileSystem.FileSystem;
99 return yield* Effect.isSuccess(fileSystem.readLink(path));
100});
101​
102/** Stat of a regular file or directory; a symlink reports as itself and is never followed. */
103const entryInfo = Effect.fn("entryInfo")(function* (path: string) {
104 const fileSystem = yield* FileSystem.FileSystem;
105 if (yield* isSymbolicLink(path))
106 return { type: "SymbolicLink" as const, mtime: Option.none<Date>() };
107 return yield* fileSystem.stat(path);
108});
109​
110/**
111 * Stats run in the libuv thread pool, so a few at a time cut the walk's wall
112 * time without lengthening any event-loop turn.
113 */
114const LEGACY_STAT_CONCURRENCY = 4;
115​
116/** Entries stat'd before any is processed, so one huge directory is not held in memory at once. */
117const LEGACY_STAT_BATCH = 256;
118​
119/** Reads current SQLite and pre-migration JSON stores without modifying either. */
120export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
121 root: string,
122 sinceMs: number,
123): Effect.fn.Return<OpenCodeUsageReadResult, never, FileSystem.FileSystem | Path.Path> {
124 const fileSystem = yield* FileSystem.FileSystem;
125 const path = yield* Path.Path;
126 const files: { path: string; records: UsageRecord[] }[] = [];
127 const seen = new Set<string>();
128 let found = false;
129 let error = false;
130 const append = (records: UsageRecord[], record: UsageRecord | null) => {
131 if (record === null || record.timestampMs < sinceMs) return;
132 if (record.dedupeKey !== null) {
133 if (seen.has(record.dedupeKey)) return;
134 seen.add(record.dedupeKey);
135 }
136 records.push(record);
137 };
138​
139 const databases = yield* fileSystem.readDirectory(root).pipe(
140 Effect.flatMap((names) =>
141 Effect.filter(
142 names.filter((name) => /^opencode(?:-[a-zA-Z0-9_-]+)?\.db$/.test(name)),
143 // One database removed between listing and stat must not hide the rest.
144 (name) =>
145 entryInfo(path.join(root, name)).pipe(
146 Effect.map((info) => info.type === "File"),
147 Effect.catchTags({
148 PlatformError: (cause) =>
149 isNotFound(cause) ? Effect.succeed(false) : Effect.fail(cause),
150 }),
151 ),
152 ),
153 ),
154 Effect.map((names) =>
155 names.sort((a, b) =>
156 a === "opencode.db" ? -1 : b === "opencode.db" ? 1 : a.localeCompare(b),
157 ),
158 ),
159 Effect.catchTags({
160 PlatformError: (cause) => {
161 if (!isNotFound(cause)) error = true;
162 return Effect.succeed([]);
163 },
164 }),
165 );
166 for (const name of databases) {
167 found = true;
168 const file = { path: path.join(root, name), records: [] as UsageRecord[] };
169 files.push(file);
170 yield* Effect.gen(function* () {
171 const sql = yield* SqlClient.SqlClient;
172 // A busy live provider should fail this source promptly rather than
173 // stalling the server while SQLite waits for its writer.
174 yield* sql.unsafe("PRAGMA busy_timeout = 100");
175 const tables = new Set(
176 (yield* sql.unsafe<{ readonly name: unknown }>(
177 "SELECT name FROM sqlite_master WHERE type = 'table'",
178 )).map((row) => row.name),
179 );
180 if (!tables.has("message") && !tables.has("session_message")) error = true;
181 for (const table of ["message", "session_message"] as const) {
182 if (!tables.has(table)) continue;
183 const columns = new Set(
184 (yield* sql.unsafe<{ readonly name: unknown }>(`PRAGMA table_info(${table})`)).map(
185 (row) => row.name,
186 ),
187 );
188 const timestamp = columns.has("time_created") ? "time_created" : "NULL";
189 const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
190 if (timestamp !== "NULL") predicates.push("time_created >= ?");
191 const lastRowId = (yield* sql.unsafe<{ readonly last_row_id: number | null }>(
192 `SELECT MAX(rowid) AS last_row_id FROM ${table}`,
193 ))[0]?.last_row_id;
194 if (lastRowId === null || lastRowId === undefined) continue;
195 let afterRowId: number | undefined;
196 // SqlClient materializes each result with StatementSync.all(). Keep
197 // message bodies in small pages, and exclude rows appended mid-scan.
198 while (true) {
199 const pagePredicates = [
200 ...predicates,
201 "rowid <= ?",
202 ...(afterRowId === undefined ? [] : ["rowid > ?"]),
203 ];
204 const rows = yield* sql.unsafe<{
205 readonly row_id: number;
206 readonly id: unknown;
207 readonly session_id: unknown;
208 readonly data: unknown;
209 readonly created: unknown;
210 }>(
211 `SELECT rowid AS row_id, id, session_id, data, ${timestamp} AS created
212 FROM ${table} WHERE ${pagePredicates.join(" AND ")} ORDER BY rowid LIMIT ${MESSAGE_PAGE_SIZE}`,
213 [
214 ...(timestamp === "NULL" ? [] : [sinceMs]),
215 lastRowId,
216 ...(afterRowId === undefined ? [] : [afterRowId]),
217 ],
218 );
219 for (const row of rows) {
220 append(
221 file.records,
222 parseOpenCodeMessage(text(row.data), {
223 id: text(row.id),
224 sessionId: text(row.session_id),
225 ...(typeof row.created === "number" ? { timestampMs: row.created } : {}),
226 }),
227 );
228 }
229 if (rows.length < MESSAGE_PAGE_SIZE) break;
230 afterRowId = rows[rows.length - 1]!.row_id;
231 yield* Effect.yieldNow;
232 }
233 }
234 }).pipe(
235 Effect.provide(NodeSqliteClient.layer({ filename: file.path, readonly: true })),
236 Effect.catchTags({
237 SqlError: () => {
238 error = true;
239 return Effect.void;
240 },
241 }),
242 );
243 }
244​
245 // Do not follow symlinks, including cycles. Database records win over their
246 // old JSON copies when OpenCode has migrated a store in place.
247 const directories = [path.join(root, "storage", "message")];
248 while (directories.length > 0) {
249 const directory = directories.pop()!;
250 yield* Effect.gen(function* () {
251 const names = yield* fileSystem.readDirectory(directory);
252 for (let start = 0; start < names.length; start += LEGACY_STAT_BATCH) {
253 const batch = names.slice(start, start + LEGACY_STAT_BATCH);
254 const entries = batch.map((name) => path.join(directory, name));
255 // `stat` follows symlinks and `readLink` fails, expensively, for
256 // everything else. Stat a batch, then ask whether an entry is a symlink
257 // only where the answer changes the result.
258 const infos = yield* Effect.forEach(
259 entries,
260 (entry) =>
261 fileSystem.stat(entry).pipe(
262 Effect.catchTags({
263 // A symlink whose target cannot be stat'd is skipped, not an error.
264 PlatformError: (cause) =>
265 isNotFound(cause)
266 ? Effect.succeed(null)
267 : isSymbolicLink(entry).pipe(
268 Effect.flatMap((link) =>
269 link ? Effect.succeed(null) : Effect.fail(cause),
270 ),
271 ),
272 }),
273 Effect.exit,
274 ),
275 { concurrency: LEGACY_STAT_CONCURRENCY },
276 );
277 for (const [index, name] of batch.entries()) {
278 const entry = entries[index]!;
279 const info = yield* infos[index]!;
280 if (info?.type === "Directory") {
281 if (!(yield* isSymbolicLink(entry))) directories.push(entry);
282 } else if (info?.type === "File" && name.endsWith(".json")) {
283 const id = name.slice(0, -5);
284 // A message cannot be created after its file was last written, so a
285 // file untouched since the window opened holds nothing in range.
286 const skip =
287 seen.has(`opencode:${id}`) ||
288 Option.exists(info.mtime, (mtime) => mtime.getTime() < sinceMs);
289 // A skipped file only matters as proof that the store exists.
290 if (found && skip) continue;
291 if (yield* isSymbolicLink(entry)) continue;
292 found = true;
293 if (skip) continue;
294 const file = { path: entry, records: [] as UsageRecord[] };
295 files.push(file);
296 yield* fileSystem.readFileString(entry).pipe(
297 Effect.map((source) => append(file.records, parseOpenCodeMessage(source, { id }))),
298 Effect.catchTags({
299 PlatformError: (cause) => {
300 if (!isNotFound(cause)) error = true;
301 return Effect.void;
302 },
303 }),
304 );
305 }
306 }
307 }
308 }).pipe(
309 Effect.catchTags({
310 PlatformError: (cause) => {
311 if (!isNotFound(cause)) error = true;
312 return Effect.void;
313 },
314 }),
315 );
316 }
317 return { files, missing: !found && !error, error };
318});
319​
320/**
321 * The data directories to read: `OPENCODE_DATA_DIR` (comma-separated) or the
322 * XDG default, canonicalized so aliases count once.
323 */
324const resolveOpenCodeDataDirs = Effect.fn("resolveOpenCodeDataDirs")(function* () {
325 const fileSystem = yield* FileSystem.FileSystem;
326 const path = yield* Path.Path;
327 const environment = yield* HostProcess.Environment;
328 const homeDirectory = yield* HostProcess.HomeDirectory;
329 const roots = environment["OPENCODE_DATA_DIR"]
330 ?.split(",")
331 .map((value) => value.trim())
332 .filter(Boolean);
333 const dataHome = environment["XDG_DATA_HOME"]?.trim();
334 const defaults = [
335 path.join(
336 dataHome && path.isAbsolute(dataHome)
337 ? dataHome
338 : path.join(homeDirectory, ".local", "share"),
339 "opencode",
340 ),
341 ];
342 const canonical = new Set<string>();
343 for (const root of roots?.length ? roots : defaults) {
344 const resolved = path.resolve(expandHomePath(root, homeDirectory));
345 canonical.add(yield* fileSystem.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved)));
346 }
347 return [...canonical];
348});
349​
350export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path;
351​
352export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCodeUsageReaderEnv> = {
353 kind: "scan",
354 provider: "opencode",
355 scan: Effect.fn("openCodeUsageReader.scan")(function* ({ windowStartMs }) {
356 const roots = yield* resolveOpenCodeDataDirs();
357 return yield* Effect.forEach(
358 roots,
359 (dir) =>
360 readOpenCodeUsage(dir, windowStartMs).pipe(
361 Effect.map((result) => ({
362 dir,
363 files: result.missing && !result.error ? null : result.files,
364 status: result.error ? ("partial" as const) : ("ok" as const),
365 ...(result.error ? { message: "Some OpenCode history could not be read." } : {}),
366 })),
367 ),
368 { concurrency: "unbounded" },
369 );
370 }),
371};