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

main
ts 587 lines 21.3 KB
Raw
1 // @effect-diagnostics-next-line nodeBuiltinImport:off - Effect's File.Info has no ctime, which the cache fingerprint needs.
2 import * as NodeFSP from "node:fs/promises";
3 import { pipe } from "effect/Function";
4
5 import type { UsageRecord } from "@t3tools/provider-core/server/usage";
6 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
7 import * as Effect from "effect/Effect";
8 import * as Exit from "effect/Exit";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Path from "effect/Path";
11 import * as Schema from "effect/Schema";
12 import * as SqlClient from "effect/sql/SqlClient";
13
14 type FieldValue = number | bigint | Uint8Array;
15 type Fields = Map<number, FieldValue[]>;
16
17 function fields(bytes: Uint8Array): Fields {
18 let offset = 0;
19 const result: Fields = new Map();
20 const varint = () => {
21 let value = 0n;
22 for (let shift = 0n; shift < 70n; shift += 7n) {
23 const byte = bytes[offset++];
24 if (byte === undefined || (shift === 63n && byte > 1)) {
25 throw new Error("Invalid Antigravity protobuf varint");
26 }
27 value |= BigInt(byte & 127) << shift;
28 if (byte < 128) {
29 return value > BigInt(Number.MAX_SAFE_INTEGER) ? value : Number(value);
30 }
31 }
32 throw new Error("Invalid Antigravity protobuf varint");
33 };
34 while (offset < bytes.length) {
35 const tag = varint();
36 if (typeof tag !== "number") throw new Error("Invalid protobuf field");
37 const number = Math.floor(tag / 8);
38 const wire = tag % 8;
39 if (number === 0) throw new Error("Invalid protobuf field");
40 let value: FieldValue;
41 if (wire === 0) {
42 value = varint();
43 } else if (wire === 1 || wire === 5 || wire === 2) {
44 const length = wire === 2 ? varint() : wire === 1 ? 8 : 4;
45 if (typeof length !== "number") throw new Error("Invalid protobuf field length");
46 if (length > bytes.length - offset) throw new Error("Truncated protobuf field");
47 value = bytes.subarray(offset, offset + length);
48 offset += length;
49 if (wire !== 2) continue;
50 } else {
51 throw new Error("Unsupported protobuf wire type");
52 }
53 const entries = result.get(number) ?? [];
54 entries.push(value);
55 result.set(number, entries);
56 }
57 return result;
58 }
59
60 const numberAt = (value: Fields, key: number) => {
61 const entry = value.get(key)?.[0];
62 return typeof entry === "number" ? entry : 0;
63 };
64 const bytesAt = (value: Fields, key: number) => {
65 const entry = value.get(key)?.[0];
66 return entry instanceof Uint8Array ? entry : undefined;
67 };
68 const nested = (value: Fields, key: number) => {
69 const bytes = bytesAt(value, key);
70 return bytes === undefined ? new Map<number, FieldValue[]>() : fields(bytes);
71 };
72 const textAt = (value: Fields, key: number) => {
73 const bytes = bytesAt(value, key);
74 return bytes === undefined ? "" : new TextDecoder("utf-8", { fatal: true }).decode(bytes).trim();
75 };
76 const timestamp = (value: Fields) => {
77 const seconds = numberAt(value, 1);
78 return seconds > 0 ? seconds * 1000 + Math.floor(numberAt(value, 2) / 1_000_000) : null;
79 };
80
81 // Offline fallback for records without names. New names are learned from the scanned metadata.
82 // Cross-checked against ccusage's Antigravity parser and mrexodia/agent-cost-dashboard.
83 const MODEL_IDS: Record<number, string> = {
84 246: "gemini-2.5-pro",
85 312: "gemini-2.5-flash",
86 313: "gemini-2.5-flash-thinking",
87 329: "gemini-2.5-flash-thinking",
88 330: "gemini-2.5-flash-lite",
89 281: "claude-sonnet-4",
90 282: "claude-sonnet-4",
91 290: "claude-opus-4",
92 291: "claude-opus-4",
93 333: "claude-sonnet-4-5",
94 334: "claude-sonnet-4-5",
95 340: "claude-haiku-4-5",
96 341: "claude-haiku-4-5",
97 342: "gpt-oss-120b-medium",
98 1026: "claude-opus-4-6",
99 1035: "claude-sonnet-4-6",
100 1016: "gemini-3.1-pro",
101 1036: "gemini-3.1-pro",
102 1037: "gemini-3.1-pro",
103 1018: "gemini-3-flash-preview",
104 1084: "gemini-3-flash-preview",
105 1047: "gemini-3-flash-preview",
106 1020: "gemini-3.5-flash-medium",
107 1021: "gemini-3.1-flash-image",
108 1050: "gemini-3.1-flash-lite",
109 1071: "gemini-3.6-flash-high",
110 1072: "gemini-3.6-flash-medium",
111 1073: "gemini-3.6-flash-low",
112 1132: "gemini-3.5-flash-high",
113 1133: "gemini-3.5-flash-high",
114 1187: "gemini-3.5-flash-extra-low",
115 1196: "gemini-3.6-flash",
116 1266: "gemini-3.6-flash",
117 1298: "gemini-3.7-flash-high",
118 1299: "gemini-3.7-flash-medium",
119 1300: "gemini-3.7-flash-low",
120 1301: "gemini-3.7-flash-tiered",
121 1318: "gemini-3.8-flash-high",
122 1319: "gemini-3.8-flash-medium",
123 1320: "gemini-3.8-flash-low",
124 1322: "gemini-3.8-flash-tiered",
125 1402: "claude-opus-5-5",
126 1417: "gemini-4-argon",
127 1418: "gemini-4-argon",
128 1419: "gemini-4-argon",
129 };
130
131 // Effort and routing variants share the base model's price.
132 export const antigravityRateModel = (model: string) => {
133 const base = model.replace(/-(?:extra-low|low|medium|high|tiered|control|thinking|agent)$/, "");
134 return /^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(model)
135 ? "gemini-3.5-flash"
136 : model === "gemini-3.7-flash-safety-le"
137 ? "gemini-3.7-flash"
138 : base === "gemini-3-flash"
139 ? "gemini-3-flash-preview"
140 : base;
141 };
142
143 function withRateModel(record: UsageRecord): UsageRecord {
144 const rateModel = antigravityRateModel(record.model);
145 return rateModel === record.model ? record : { ...record, rateModel };
146 }
147
148 function modelName(name: string): string {
149 if (name) {
150 const normalized = name
151 .toLowerCase()
152 .replace(/\s*\(([^)]*)\)\s*$/, "-$1")
153 .replace(/\s+/g, "-");
154 if (normalized.startsWith("claude-")) {
155 return normalized
156 .replace(/^claude-(\d+(?:\.\d+)?)-(sonnet|opus|haiku)/, "claude-$2-$1")
157 .replaceAll(".", "-");
158 }
159 return normalized;
160 }
161 return "";
162 }
163
164 function isConcreteModel(model: string): boolean {
165 return (
166 /^(?:gemini-\d|claude-(?:sonnet|opus|haiku)-\d|gpt-)/.test(model) &&
167 !/^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(model)
168 );
169 }
170
171 function rememberModel(models: Map<number, string | null>, id: number, model: string | null) {
172 const previous = models.get(id);
173 models.set(id, previous === undefined || previous === model ? model : null);
174 }
175
176 interface Metadata {
177 model: string;
178 modelId: number;
179 timestampMs: number | null;
180 usages: Fields[];
181 }
182
183 function metadata(bytes: Uint8Array, step: boolean): Metadata {
184 const root = fields(bytes);
185 if (!step && bytesAt(root, 1) === undefined) {
186 throw new Error("Missing Antigravity generation metadata");
187 }
188 const data = step ? root : nested(root, 1);
189 const model = step ? nested(data, 24) : data;
190 const usage = bytesAt(data, step ? 9 : 4);
191 // Copy the usage bytes: subarrays kept in usages would hold each row's whole blob alive.
192 const primaryUsage = usage === undefined ? undefined : fields(usage.slice());
193 const usages = primaryUsage === undefined ? [] : [primaryUsage];
194 for (const retry of data.get(step ? 28 : 17) ?? []) {
195 if (!(retry instanceof Uint8Array)) throw new Error("Invalid retry metadata");
196 const retryUsage = bytesAt(fields(retry), 2);
197 if (retryUsage !== undefined) usages.push(fields(retryUsage.slice()));
198 }
199 const names = [textAt(model, step ? 12 : 19), textAt(model, step ? 8 : 21)].map(modelName);
200 return {
201 model:
202 names.find(isConcreteModel) ?? names.find((name) => name && !/^model[_-]/.test(name)) ?? "",
203 modelId:
204 numberAt(model, step ? 1 : 3) || (primaryUsage === undefined ? 0 : numberAt(primaryUsage, 1)),
205 timestampMs: step
206 ? (timestamp(nested(data, 8)) ?? timestamp(nested(data, 1)))
207 : timestamp(nested(nested(data, 9), 4)),
208 usages,
209 };
210 }
211
212 function blob(value: unknown): Uint8Array {
213 if (!(value instanceof Uint8Array)) throw new Error("Invalid Antigravity metadata blob");
214 return value;
215 }
216
217 interface UsageCandidate {
218 record: UsageRecord;
219 modelId: number;
220 modelQuality: number;
221 keys: readonly string[];
222 timestampQuality: number;
223 }
224
225 class AntigravityDatabaseDecodeError extends Schema.TaggedError<AntigravityDatabaseDecodeError>()(
226 "AntigravityDatabaseDecodeError",
227 { path: Schema.String, cause: Schema.optional(Schema.Defect()) },
228 ) {
229 override get message(): string {
230 return `Antigravity usage could not be decoded from ${this.path}.`;
231 }
232 }
233
234 /** Generation rows can hold megabytes of context; read one at a time to bound blocking statement.all(). */
235 const GENERATION_PAGE_ROWS = 1;
236 const STEP_PAGE_ROWS = 64;
237
238 const readDatabase = Effect.fn("readDatabase")(function* (
239 path: string,
240 sessionId: string,
241 fallbackTimestamp: number,
242 ) {
243 const sql = yield* SqlClient.SqlClient;
244 const decode = <A>(evaluate: () => A) =>
245 Effect.try({
246 try: evaluate,
247 catch: (cause) => new AntigravityDatabaseDecodeError({ path, cause }),
248 });
249 yield* sql.unsafe("PRAGMA busy_timeout = 100");
250 return yield* Effect.gen(function* () {
251 const tables = new Set(
252 (yield* sql.unsafe<{ readonly name: unknown }>(
253 "SELECT name FROM sqlite_master WHERE type = 'table'",
254 )).map((row) => row.name),
255 );
256 // Not a conversation store, e.g. agy's conversation_summaries.db index.
257 if (!tables.has("gen_metadata") && !tables.has("steps")) return null;
258 // Seek by rowid regardless of indexes; idx normally aliases rowid, but sort for layouts where it does not.
259 const readMetadata = Effect.fnUntraced(function* (
260 query: string,
261 pageRows: number,
262 step: boolean,
263 ) {
264 const entries: Array<{ idx: number; entry: Metadata }> = [];
265 let after = -Infinity;
266 while (true) {
267 const rows = yield* sql
268 .unsafe<{
269 readonly row_id: number;
270 readonly idx: unknown;
271 readonly data: unknown;
272 }>(query, [after, pageRows])
273 .pipe(Effect.withTracerEnabled(false));
274 for (const row of rows) {
275 // Filter outside SQL to bound scans to one page, including rows without metadata.
276 if (step && row.data === null) continue;
277 const idx = row.idx;
278 if (typeof idx !== "number") {
279 return yield* new AntigravityDatabaseDecodeError({ path });
280 }
281 entries.push({ idx, entry: yield* decode(() => metadata(blob(row.data), step)) });
282 }
283 if (rows.length < pageRows) break;
284 after = rows[rows.length - 1]!.row_id;
285 yield* Effect.yieldNow;
286 }
287 return entries.sort((a, b) => a.idx - b.idx);
288 });
289 const generations = tables.has("gen_metadata")
290 ? yield* readMetadata(
291 "SELECT rowid AS row_id, idx, data FROM gen_metadata WHERE rowid > ? ORDER BY rowid LIMIT ?",
292 GENERATION_PAGE_ROWS,
293 false,
294 )
295 : [];
296 let trajectoryTimestamp: number | null = null;
297 if (tables.has("trajectory_metadata_blob")) {
298 for (const row of yield* sql.unsafe<{ readonly data: unknown }>(
299 "SELECT data FROM trajectory_metadata_blob",
300 )) {
301 trajectoryTimestamp ??= yield* decode(() =>
302 pipe(blob(row.data), fields, (message) => nested(message, 2), timestamp),
303 );
304 }
305 }
306 const steps = tables.has("steps")
307 ? yield* readMetadata(
308 "SELECT rowid AS row_id, idx, metadata AS data FROM steps WHERE rowid > ? ORDER BY rowid LIMIT ?",
309 STEP_PAGE_ROWS,
310 true,
311 )
312 : [];
313 return yield* decode(() => {
314 const models = new Map<number, string | null>();
315 for (const { entry } of [...generations, ...steps]) {
316 if (entry.modelId > 0 && isConcreteModel(entry.model))
317 rememberModel(models, entry.modelId, entry.model);
318 }
319 return {
320 candidates: usageCandidates(
321 sessionId,
322 generations,
323 steps,
324 trajectoryTimestamp,
325 fallbackTimestamp,
326 ),
327 models,
328 };
329 });
330 }).pipe(sql.withTransaction);
331 });
332
333 function usageCandidates(
334 sessionId: string,
335 generations: ReadonlyArray<{ idx: number; entry: Metadata }>,
336 steps: ReadonlyArray<{ idx: number; entry: Metadata }>,
337 trajectoryTimestamp: number | null,
338 fallbackTimestamp: number,
339 ): UsageCandidate[] {
340 const records: UsageCandidate[] = [];
341 const generationModels = new Map(generations.map(({ idx, entry }) => [idx, entry]));
342 for (const [source, entries] of [
343 ["step", steps],
344 ["generation", generations],
345 ] as const) {
346 for (const [index, { idx, entry }] of entries.entries()) {
347 for (const [usageIndex, usage] of entry.usages.entries()) {
348 const modelId = numberAt(usage, 1) || entry.modelId;
349 const generation = source === "step" ? generationModels.get(idx) : undefined;
350 // A retry can use a different model than its enclosing generation or step.
351 const matches = (metadata: Metadata) => modelId === 0 || metadata.modelId === modelId;
352 const model =
353 matches(entry) && entry.model
354 ? entry.model
355 : generation && matches(generation)
356 ? generation.model
357 : "";
358 const outputTokens = Math.max(numberAt(usage, 3), numberAt(usage, 9) + numberAt(usage, 10));
359 const totals = {
360 uncachedInputTokens: numberAt(usage, 2),
361 cachedInputTokens: numberAt(usage, 5),
362 cacheCreationTokens: numberAt(usage, 4),
363 outputTokens,
364 reasoningTokens: Math.min(outputTokens, numberAt(usage, 9)),
365 };
366 if (
367 totals.uncachedInputTokens +
368 totals.cachedInputTokens +
369 totals.cacheCreationTokens +
370 outputTokens ===
371 0
372 )
373 continue;
374 const keys = ([11, 12, 7] as const).flatMap((key) => {
375 const id = textAt(usage, key);
376 return id ? [`antigravity:${key}:${id}`] : [];
377 });
378 const record: UsageRecord = {
379 provider: "antigravity",
380 sessionId,
381 timestampMs: entry.timestampMs ?? trajectoryTimestamp ?? fallbackTimestamp,
382 model,
383 totals,
384 reportedCostUsd: null,
385 speed: "standard",
386 dedupeKey: keys[0] ?? `antigravity:${sessionId}:${source}:${index}:${usageIndex}`,
387 };
388 records.push({
389 record,
390 modelId,
391 modelQuality: isConcreteModel(model) ? 4 : model ? 1 : 0,
392 keys,
393 timestampQuality: entry.timestampMs !== null ? 2 : trajectoryTimestamp !== null ? 1 : 0,
394 });
395 }
396 }
397 }
398 return records;
399 }
400
401 interface CachedDatabase {
402 readonly fingerprint: string;
403 readonly data: Effect.Success<ReturnType<typeof readDatabase>>;
404 }
405
406 /** Fingerprint canonical DB/WAL paths by size, mtime and ctime; WAL holds new rows, and ctime catches restored mtimes. */
407 export const makeAntigravityUsageCache = () => new Map<string, CachedDatabase>();
408
409 export const readAntigravityUsage = Effect.fn("readAntigravityUsage")(function* (
410 conversationsDirectories: string | readonly string[],
411 sinceMs: number,
412 cache?: Map<string, CachedDatabase>,
413 ) {
414 const fileSystem = yield* FileSystem.FileSystem;
415 const pathService = yield* Path.Path;
416 const roots =
417 typeof conversationsDirectories === "string"
418 ? [conversationsDirectories]
419 : conversationsDirectories;
420 const files: Array<{ root: string; path: string; records: UsageRecord[] }> = [];
421 const errors: string[] = [];
422 const models = new Map<number, string | null>();
423 const pending: Array<{ data: NonNullable<CachedDatabase["data"]>; fileIndex: number }> = [];
424 const identities = new Map<string, number>();
425 const groups: Array<
426 UsageCandidate & { parent: number; size: number; owner: number; fileIndex: number }
427 > = [];
428 const find = (index: number): number => {
429 let root = index;
430 while (groups[root]!.parent !== root) root = groups[root]!.parent;
431 while (index !== root) {
432 const parent = groups[index]!.parent;
433 groups[index]!.parent = root;
434 index = parent;
435 }
436 return root;
437 };
438 const merge = (left: number, right: number): number => {
439 let a = find(left);
440 let b = find(right);
441 if (a === b) return a;
442 if (groups[a]!.size < groups[b]!.size) [a, b] = [b, a];
443 const target = groups[a]!;
444 const source = groups[b]!;
445 const first = target.owner < source.owner ? target : source;
446 const bestModel =
447 source.modelQuality === target.modelQuality
448 ? first
449 : source.modelQuality > target.modelQuality
450 ? source
451 : target;
452 const bestTime =
453 source.timestampQuality > target.timestampQuality ||
454 (source.timestampQuality === target.timestampQuality &&
455 source.record.timestampMs < target.record.timestampMs)
456 ? source
457 : target;
458 const x = target.record.totals;
459 const y = source.record.totals;
460 target.record = {
461 ...first.record,
462 model: bestModel.record.model,
463 timestampMs: bestTime.record.timestampMs,
464 totals: {
465 uncachedInputTokens: Math.max(x.uncachedInputTokens, y.uncachedInputTokens),
466 cachedInputTokens: Math.max(x.cachedInputTokens, y.cachedInputTokens),
467 cacheCreationTokens: Math.max(x.cacheCreationTokens, y.cacheCreationTokens),
468 outputTokens: Math.max(x.outputTokens, y.outputTokens),
469 reasoningTokens: Math.max(x.reasoningTokens, y.reasoningTokens),
470 },
471 };
472 target.timestampQuality = bestTime.timestampQuality;
473 target.modelQuality = bestModel.modelQuality;
474 target.owner = first.owner;
475 target.fileIndex = first.fileIndex;
476 target.size += source.size;
477 source.parent = a;
478 return a;
479 };
480 const append = (candidate: UsageCandidate, fileIndex: number) => {
481 const index = groups.length;
482 groups.push({ ...candidate, parent: index, size: 1, owner: index, fileIndex });
483 for (const key of candidate.keys) {
484 const existing = identities.get(key);
485 if (existing !== undefined) merge(index, existing);
486 identities.set(key, index);
487 }
488 };
489 const visited = new Set<string>();
490 const readFile = Effect.fn("readAntigravityUsage.readFile")(function* (path: string) {
491 const canonical = yield* fileSystem.realPath(path);
492 if (visited.has(canonical)) return null;
493 visited.add(canonical);
494 const fingerprintOf = (file: string) =>
495 Effect.tryPromise({
496 try: () => NodeFSP.stat(file),
497 catch: (cause) => new AntigravityDatabaseDecodeError({ path: file, cause }),
498 }).pipe(
499 Effect.map((stat) => ({ stat, part: `${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}` })),
500 );
501 const { stat, part } = yield* fingerprintOf(path);
502 const wal = yield* fingerprintOf(`${path}-wal`).pipe(Effect.option);
503 const fingerprint = `${part}/${wal._tag === "Some" ? wal.value.part : "-"}`;
504 const cached = cache?.get(canonical);
505 if (cached?.fingerprint === fingerprint) return cached.data;
506 const data = yield* readDatabase(path, pathService.basename(path, ".db"), stat.mtimeMs).pipe(
507 Effect.provide(NodeSqliteClient.layer({ filename: path, readonly: true })),
508 );
509 cache?.set(canonical, { fingerprint, data });
510 return data;
511 });
512 const walk = Effect.fn("antigravityUsageReader.walk")(function* (
513 directory: string,
514 root: string,
515 ): Effect.fn.Return<void> {
516 const names = yield* fileSystem.readDirectory(directory).pipe(
517 Effect.catchTags({
518 PlatformError: (cause) => {
519 if (cause.reason._tag !== "NotFound") errors.push(directory);
520 return Effect.succeed(null);
521 },
522 }),
523 );
524 if (names === null) return;
525 names.sort((a, b) => a.localeCompare(b));
526 for (const name of names) {
527 const path = pathService.join(directory, name);
528 // Like a directory listing's entry types: symlinks are neither files nor directories.
529 const isLink = Exit.isSuccess(yield* Effect.exit(fileSystem.readLink(path)));
530 const type = isLink
531 ? "SymbolicLink"
532 : yield* fileSystem.stat(path).pipe(
533 Effect.map((info) => info.type),
534 Effect.orElseSucceed(() => null),
535 );
536 if (type === "Directory") {
537 yield* walk(path, root);
538 } else if ((type === "File" || type === null) && name.endsWith(".db")) {
539 const data = yield* readFile(path).pipe(
540 Effect.catch(() => {
541 errors.push(path);
542 return Effect.succeed(null);
543 }),
544 );
545 if (data === null) continue;
546 for (const [id, model] of data.models) {
547 rememberModel(models, id, model);
548 }
549 const fileIndex = files.length;
550 files.push({ root, path, records: [] });
551 pending.push({ data, fileIndex });
552 }
553 }
554 });
555 for (const root of roots) yield* walk(root, root);
556 if (cache !== undefined) {
557 for (const key of cache.keys()) if (!visited.has(key)) cache.delete(key);
558 }
559 for (const { data, fileIndex } of pending) {
560 for (const [index, candidate] of data.candidates.entries()) {
561 const learned = models.get(candidate.modelId);
562 const fallback = learned === null ? undefined : MODEL_IDS[candidate.modelId];
563 const model =
564 (candidate.modelQuality === 4 ? candidate.record.model : "") ||
565 learned ||
566 fallback ||
567 candidate.record.model ||
568 (candidate.modelId > 0 ? `antigravity-model-${candidate.modelId}` : "antigravity-unknown");
569 append(
570 {
571 ...candidate,
572 record: { ...candidate.record, model },
573 modelQuality:
574 candidate.modelQuality === 4 ? 4 : learned ? 3 : fallback ? 2 : candidate.modelQuality,
575 },
576 fileIndex,
577 );
578 if (index % 256 === 255) yield* Effect.yieldNow;
579 }
580 }
581 for (const [index, group] of groups.entries()) {
582 if (group.parent === index && group.record.timestampMs >= sinceMs) {
583 files[group.fileIndex]!.records.push(withRateModel(group.record));
584 }
585 }
586 return { files, errors };
587 });