Open History to see the last commit
1import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
2import {
3 totalTokens,
4 type ProviderUsageReader,
5 type UsageRecord,
6} from "@t3tools/provider-core/server/usage";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Option from "effect/Option";
12import * as Path from "effect/Path";
13import type * as PlatformError from "effect/PlatformError";
14import * as SqlClient from "effect/sql/SqlClient";
15
16import type { OpenCodeSettings } from "../settings.ts";
17
18function object(value: unknown): Record<string, unknown> {
19 return typeof value === "object" && value !== null && !Array.isArray(value)
20 ? (value as Record<string, unknown>)
21 : {};
22}
23
24function tokens(value: unknown): number {
25 return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0;
26}
27
28function text(value: unknown): string {
29 return typeof value === "string" ? value : "";
30}
31
32// OpenCode provider IDs whose bare model IDs LiteLLM prices under its own prefix.
33const LITELLM_PREFIXES = new Map([["opencode-go", "opencode_go"]]);
34const OPENCODE_GATEWAYS = new Set(["opencode", "opencode-go"]);
35
36function parseOpenCodeMessage(
37 source: string,
38 fallback: {
39 readonly id?: string;
40 readonly sessionId?: string;
41 readonly timestampMs?: number;
42 } = {},
43): UsageRecord | null {
44 let parsed: unknown;
45 try {
46 parsed = JSON.parse(source);
47 } catch {
48 return null;
49 }
50 const message = object(parsed);
51 if (message.role !== undefined && message.role !== "assistant") return null;
52 const usage = object(message.tokens);
53 const cache = object(usage.cache);
54 const modelReference = object(message.model);
55 const model = text(modelReference.id) || text(modelReference.modelID) || text(message.modelID);
56 const providerId = text(modelReference.providerID) || text(message.providerID);
57 const timestampMs = object(message.time).created ?? fallback.timestampMs;
58 if (!model || typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return null;
59 const reasoningTokens = tokens(usage.reasoning);
60 const totals = {
61 uncachedInputTokens: tokens(usage.input),
62 cachedInputTokens: tokens(cache.read),
63 cacheCreationTokens: tokens(cache.write),
64 outputTokens: tokens(usage.output) + reasoningTokens,
65 reasoningTokens,
66 };
67 if (totalTokens(totals) === 0) return null;
68 const id = fallback.id || text(message.id);
69 const cost = message.cost;
70 const prefix = LITELLM_PREFIXES.get(providerId);
71 const free = OPENCODE_GATEWAYS.has(providerId) && model.endsWith("-free");
72 return {
73 provider: "opencode",
74 timestampMs,
75 model,
76 ...(prefix !== undefined && !model.includes("/") ? { rateModel: `${prefix}/${model}` } : {}),
77 sessionId: fallback.sessionId || text(message.sessionID),
78 totals,
79 // OpenCode records zero for unknown rates and paid subscriptions alike, so only its own
80 // gateways' free models report zero; shared pricing estimates every other zero.
81 reportedCostUsd:
82 typeof cost === "number" && Number.isFinite(cost) && (cost > 0 || (free && cost === 0))
83 ? cost
84 : null,
85 speed: "standard",
86 dedupeKey: id ? `opencode:${id}` : null,
87 };
88}
89
90export interface OpenCodeUsageReadResult {
91 readonly files: readonly { readonly path: string; readonly records: readonly UsageRecord[] }[];
92 readonly missing: boolean;
93 readonly error: boolean;
94}
95
96const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound";
97const MESSAGE_PAGE_SIZE = 64;
98
99const isSymbolicLink = Effect.fnUntraced(function* (path: string) {
100 const fileSystem = yield* FileSystem.FileSystem;
101 return yield* Effect.isSuccess(fileSystem.readLink(path));
102});
103
104const entryInfo = Effect.fn("entryInfo")(function* (path: string) {
105 const fileSystem = yield* FileSystem.FileSystem;
106 if (yield* isSymbolicLink(path))
107 return { type: "SymbolicLink" as const, mtime: Option.none<Date>() };
108 return yield* fileSystem.stat(path);
109});
110
111const LEGACY_STAT_CONCURRENCY = 4;
112
113// Batch stats so huge directories do not stay in memory.
114const LEGACY_STAT_BATCH = 256;
115
116export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
117 root: string,
118 sinceMs: number,
119): Effect.fn.Return<OpenCodeUsageReadResult, never, FileSystem.FileSystem | Path.Path> {
120 const fileSystem = yield* FileSystem.FileSystem;
121 const path = yield* Path.Path;
122 const files: { path: string; records: UsageRecord[] }[] = [];
123 const seen = new Set<string>();
124 let found = false;
125 let error = false;
126 let databaseRows = 0;
127 let databasePages = 0;
128 const append = (records: UsageRecord[], record: UsageRecord | null) => {
129 if (record === null || record.timestampMs < sinceMs) return;
130 if (record.dedupeKey !== null) {
131 if (seen.has(record.dedupeKey)) return;
132 seen.add(record.dedupeKey);
133 }
134 records.push(record);
135 };
136
137 const databases = yield* fileSystem.readDirectory(root).pipe(
138 Effect.flatMap((names) =>
139 Effect.filter(
140 names.filter((name) => /^opencode(?:-[a-zA-Z0-9_-]+)?\.db$/.test(name)),
141 (name) =>
142 entryInfo(path.join(root, name)).pipe(
143 Effect.map((info) => info.type === "File"),
144 Effect.catchTags({
145 PlatformError: (cause) =>
146 isNotFound(cause) ? Effect.succeed(false) : Effect.fail(cause),
147 }),
148 ),
149 ),
150 ),
151 Effect.map((names) =>
152 names.sort((a, b) =>
153 a === "opencode.db" ? -1 : b === "opencode.db" ? 1 : a.localeCompare(b),
154 ),
155 ),
156 Effect.catchTags({
157 PlatformError: (cause) => {
158 if (!isNotFound(cause)) error = true;
159 return Effect.succeed([]);
160 },
161 }),
162 );
163 for (const name of databases) {
164 found = true;
165 const file = { path: path.join(root, name), records: [] as UsageRecord[] };
166 files.push(file);
167 yield* Effect.gen(function* () {
168 const sql = yield* SqlClient.SqlClient;
169 // Fail promptly on live-writer locks instead of stalling the server.
170 yield* sql.unsafe("PRAGMA busy_timeout = 100");
171 const tables = new Set(
172 (yield* sql.unsafe<{ readonly name: unknown }>(
173 "SELECT name FROM sqlite_master WHERE type = 'table'",
174 )).map((row) => row.name),
175 );
176 if (!tables.has("message") && !tables.has("session_message")) error = true;
177 for (const table of ["message", "session_message"] as const) {
178 if (!tables.has(table)) continue;
179 const columns = new Set(
180 (yield* sql.unsafe<{ readonly name: unknown }>(`PRAGMA table_info(${table})`)).map(
181 (row) => row.name,
182 ),
183 );
184 const timestamp = columns.has("time_created") ? "time_created" : "NULL";
185 const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
186 if (timestamp !== "NULL") predicates.push("time_created >= ?");
187 // List only rowids so SQLite can use time indexes without reading message bodies.
188 const listed = (yield* sql.unsafe<{ readonly row_id: number }>(
189 `SELECT rowid AS row_id FROM ${table}
190 ${predicates.length ? `WHERE ${predicates.join(" AND ")}` : ""}`,
191 timestamp === "NULL" ? [] : [sinceMs],
192 )).toSorted((a, b) => a.row_id - b.row_id);
193 databaseRows += listed.length;
194 // Filter and project in SQLite: user diffs and assistant bodies can be hundreds of MB.
195 const validAssistant = `CASE WHEN typeof(data) = 'text' AND json_valid(data)
196 THEN json_type(data, '$.role') IS NULL OR data ->> '$.role' = 'assistant' END`;
197 // Omit per-page spans; the enclosing scan records row and page counts.
198 for (let start = 0; start < listed.length; start += MESSAGE_PAGE_SIZE) {
199 const page = listed.slice(start, start + MESSAGE_PAGE_SIZE);
200 databasePages++;
201 const rows = yield* sql
202 .unsafe<{
203 readonly id: unknown;
204 readonly session_id: unknown;
205 readonly data: unknown;
206 readonly created: unknown;
207 }>(
208 `SELECT id, session_id, ${timestamp} AS created, json_object(
209 'tokens', data -> '$.tokens', 'cost', data -> '$.cost',
210 'model', data -> '$.model', 'modelID', data -> '$.modelID',
211 'providerID', data -> '$.providerID',
212 'time', data -> '$.time', 'id', data -> '$.id', 'sessionID', data -> '$.sessionID'
213 ) AS data
214 FROM ${table}
215 WHERE rowid IN (${page.map(() => "?").join(", ")}) AND ${validAssistant}
216 ORDER BY rowid`,
217 page.map((row) => row.row_id),
218 )
219 .pipe(Effect.withTracerEnabled(false));
220 for (const row of rows) {
221 append(
222 file.records,
223 parseOpenCodeMessage(text(row.data), {
224 id: text(row.id),
225 sessionId: text(row.session_id),
226 ...(typeof row.created === "number" ? { timestampMs: row.created } : {}),
227 }),
228 );
229 }
230 yield* Effect.yieldNow;
231 }
232 }
233 }).pipe(
234 (read) => Effect.flatMap(SqlClient.SqlClient, (sql) => sql.withTransaction(read)),
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 const directories = [path.join(root, "storage", "message")];
246 while (directories.length > 0) {
247 const directory = directories.pop()!;
248 yield* Effect.gen(function* () {
249 const names = yield* fileSystem.readDirectory(directory);
250 for (let start = 0; start < names.length; start += LEGACY_STAT_BATCH) {
251 const batch = names.slice(start, start + LEGACY_STAT_BATCH);
252 const entries = batch.map((name) => path.join(directory, name));
253 // stat follows symlinks; avoid expensive readLink failures unless link identity matters.
254 const infos = yield* Effect.forEach(
255 entries,
256 (entry) =>
257 fileSystem.stat(entry).pipe(
258 Effect.catchTags({
259 PlatformError: (cause) =>
260 isNotFound(cause)
261 ? Effect.succeed(null)
262 : isSymbolicLink(entry).pipe(
263 Effect.flatMap((link) =>
264 link ? Effect.succeed(null) : Effect.fail(cause),
265 ),
266 ),
267 }),
268 Effect.exit,
269 ),
270 { concurrency: LEGACY_STAT_CONCURRENCY },
271 );
272 for (const [index, name] of batch.entries()) {
273 const entry = entries[index]!;
274 const info = yield* infos[index]!;
275 if (info?.type === "Directory") {
276 if (!(yield* isSymbolicLink(entry))) directories.push(entry);
277 } else if (info?.type === "File" && name.endsWith(".json")) {
278 const id = name.slice(0, -5);
279 // Messages cannot postdate file mtime; older files hold no in-window usage.
280 const skip =
281 seen.has(`opencode:${id}`) ||
282 Option.exists(info.mtime, (mtime) => mtime.getTime() < sinceMs);
283 if (found && skip) continue;
284 if (yield* isSymbolicLink(entry)) continue;
285 found = true;
286 if (skip) continue;
287 const file = { path: entry, records: [] as UsageRecord[] };
288 files.push(file);
289 yield* fileSystem.readFileString(entry).pipe(
290 Effect.map((source) => append(file.records, parseOpenCodeMessage(source, { id }))),
291 Effect.catchTags({
292 PlatformError: (cause) => {
293 if (!isNotFound(cause)) error = true;
294 return Effect.void;
295 },
296 }),
297 );
298 }
299 }
300 }
301 }).pipe(
302 Effect.catchTags({
303 PlatformError: (cause) => {
304 if (!isNotFound(cause)) error = true;
305 return Effect.void;
306 },
307 }),
308 );
309 }
310 yield* Effect.annotateCurrentSpan({
311 "usage.database_rows": databaseRows,
312 "usage.database_pages": databasePages,
313 });
314 return { files, missing: !found && !error, error };
315});
316
317const resolveOpenCodeDataDirs = Effect.fn("resolveOpenCodeDataDirs")(function* (
318 environment: NodeJS.ProcessEnv,
319) {
320 const fileSystem = yield* FileSystem.FileSystem;
321 const path = yield* Path.Path;
322 const homeDirectory = yield* HostProcess.HomeDirectory;
323 const roots = environment["OPENCODE_DATA_DIR"]
324 ?.split(",")
325 .map((value) => value.trim())
326 .filter(Boolean);
327 const dataHome = environment["XDG_DATA_HOME"]?.trim();
328 const defaults = [
329 path.join(
330 dataHome && path.isAbsolute(dataHome)
331 ? dataHome
332 : path.join(homeDirectory, ".local", "share"),
333 "opencode",
334 ),
335 ];
336 const canonical = new Set<string>();
337 for (const root of roots?.length ? roots : defaults) {
338 const resolved = path.resolve(expandHomePath(root, homeDirectory));
339 canonical.add(yield* fileSystem.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved)));
340 }
341 return [...canonical];
342});
343
344export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path;
345
346export const openCodeUsageReader: ProviderUsageReader<OpenCodeSettings, OpenCodeUsageReaderEnv> = {
347 kind: "scan",
348 provider: "opencode",
349 scan: Effect.fn("openCodeUsageReader.scan")(function* ({ instances, windowStartMs }) {
350 const roots = new Set<string>();
351 for (const instance of instances) {
352 for (const root of yield* resolveOpenCodeDataDirs(instance.environment)) roots.add(root);
353 }
354 return yield* Effect.forEach(
355 roots,
356 (dir) =>
357 readOpenCodeUsage(dir, windowStartMs).pipe(
358 Effect.map((result) => ({
359 dir,
360 files: result.missing && !result.error ? null : result.files,
361 status: result.error ? ("partial" as const) : ("ok" as const),
362 ...(result.error ? { message: "Some OpenCode history could not be read." } : {}),
363 })),
364 ),
365 { concurrency: "unbounded" },
366 );
367 }),
368};