@@ -17,7 +17,7 @@ import * as NodeFS from "node:fs";
1717import * as NodeFSP from "node:fs/promises" ;
1818import * as NodePath from "node:path" ;
1919import * as NodeReadline from "node:readline" ;
20- import * as NodeSqlite from "node:sqlite " ;
20+ import * as NodeWorkerThreads from "node:worker_threads " ;
2121
2222import type { UsageProviderKind } from "@t3tools/contracts" ;
2323
@@ -237,6 +237,99 @@ const OPEN_CODE_TABLE_QUERIES = {
237237
238238type OpenCodeMessageTable = keyof typeof OPEN_CODE_TABLE_QUERIES ;
239239
240+ interface OpenCodeWorkerRows {
241+ readonly table : OpenCodeMessageTable ;
242+ readonly rows : readonly OpenCodeUsageRow [ ] ;
243+ }
244+
245+ type OpenCodeWorkerResult =
246+ | { readonly status : "ok" ; readonly groups : readonly OpenCodeWorkerRows [ ] }
247+ | { readonly status : "failed" } ;
248+
249+ /**
250+ * Kept inline so server bundles do not need a second worker entrypoint. Only
251+ * usage scalars cross back to the main thread; message content stays in SQLite.
252+ */
253+ const OPEN_CODE_WORKER_SOURCE = String . raw `
254+ const { parentPort, workerData } = require("node:worker_threads");
255+ const { DatabaseSync } = require("node:sqlite");
256+
257+ let database;
258+ let result = { status: "failed" };
259+ try {
260+ database = new DatabaseSync(workerData.dbPath, {
261+ readOnly: true,
262+ timeout: workerData.busyTimeoutMs,
263+ });
264+
265+ const tables = new Set();
266+ for (const row of database.prepare(workerData.tablesQuery).all()) {
267+ if (row.name === "session_message" || row.name === "message") tables.add(row.name);
268+ }
269+ if (tables.size > 0) {
270+ const groups = [];
271+ for (const table of ["session_message", "message"]) {
272+ if (!tables.has(table)) continue;
273+ const rows = database
274+ .prepare(workerData.tableQueries[table])
275+ .all(workerData.sinceMs)
276+ .map((row) => ({ ...row }));
277+ groups.push({ table, rows });
278+ }
279+ result = { status: "ok", groups };
280+ }
281+ } catch {
282+ result = { status: "failed" };
283+ } finally {
284+ try {
285+ database?.close();
286+ } catch {}
287+ }
288+
289+ parentPort.postMessage(result);
290+ ` ;
291+
292+ function readOpenCodeRows (
293+ dbPath : string ,
294+ sinceMs : number ,
295+ ) : Promise < readonly OpenCodeWorkerRows [ ] | null > {
296+ return new Promise ( ( resolve ) => {
297+ const worker = ( ( ) => {
298+ try {
299+ return new NodeWorkerThreads . Worker ( OPEN_CODE_WORKER_SOURCE , {
300+ eval : true ,
301+ workerData : {
302+ dbPath,
303+ sinceMs,
304+ busyTimeoutMs : OPEN_CODE_SQLITE_BUSY_TIMEOUT_MS ,
305+ tablesQuery : OPEN_CODE_MESSAGE_TABLES_QUERY ,
306+ tableQueries : OPEN_CODE_TABLE_QUERIES ,
307+ } ,
308+ } ) ;
309+ } catch {
310+ return null ;
311+ }
312+ } ) ( ) ;
313+ if ( worker === null ) {
314+ resolve ( null ) ;
315+ return ;
316+ }
317+
318+ let settled = false ;
319+ const finish = ( value : readonly OpenCodeWorkerRows [ ] | null ) => {
320+ if ( settled ) return ;
321+ settled = true ;
322+ resolve ( value ) ;
323+ } ;
324+
325+ worker . once ( "message" , ( message : OpenCodeWorkerResult ) => {
326+ finish ( message . status === "ok" ? message . groups : null ) ;
327+ } ) ;
328+ worker . once ( "error" , ( ) => finish ( null ) ) ;
329+ worker . once ( "exit" , ( ) => finish ( null ) ) ;
330+ } ) ;
331+ }
332+
240333/**
241334 * Reads usage records from OpenCode's SQLite transcript store.
242335 *
@@ -254,47 +347,25 @@ export async function readOpenCodeRecords(
254347 dbPath : string ,
255348 sinceMs : number ,
256349) : Promise < readonly UsageRecord [ ] | null > {
257- let database : NodeSqlite . DatabaseSync ;
258- try {
259- database = new NodeSqlite . DatabaseSync ( dbPath , {
260- readOnly : true ,
261- timeout : OPEN_CODE_SQLITE_BUSY_TIMEOUT_MS ,
262- } ) ;
263- } catch {
264- return null ;
265- }
266-
267- try {
268- const tables = new Set < OpenCodeMessageTable > ( ) ;
269- for ( const row of database . prepare ( OPEN_CODE_MESSAGE_TABLES_QUERY ) . all ( ) ) {
270- const name = ( row as Record < string , unknown > ) [ "name" ] ;
271- if ( name === "session_message" || name === "message" ) tables . add ( name ) ;
272- }
273- if ( tables . size === 0 ) return null ;
350+ const groups = await readOpenCodeRows ( dbPath , sinceMs ) ;
351+ if ( groups === null ) return null ;
274352
275- const recordsByKey = new Map < string , UsageRecord > ( ) ;
276- let anonymous = 0 ;
277- for ( const table of [ "session_message" , "message" ] as const ) {
278- if ( ! tables . has ( table ) ) continue ;
279- const rows = database . prepare ( OPEN_CODE_TABLE_QUERIES [ table ] ) . all ( sinceMs ) ;
280- for ( const row of rows ) {
281- const record = parseOpenCodeUsageRow ( row as OpenCodeUsageRow ) ;
282- if ( record === null ) continue ;
283- if ( record . dedupeKey === null ) {
284- // An anonymous record can still be unique; it just cannot dedupe
285- // across the two projections.
286- recordsByKey . set ( `${ table } #${ anonymous ++ } ` , record ) ;
287- continue ;
288- }
289- if ( ! recordsByKey . has ( record . dedupeKey ) ) {
290- recordsByKey . set ( record . dedupeKey , record ) ;
291- }
353+ const recordsByKey = new Map < string , UsageRecord > ( ) ;
354+ let anonymous = 0 ;
355+ for ( const { table, rows } of groups ) {
356+ for ( const row of rows ) {
357+ const record = parseOpenCodeUsageRow ( row ) ;
358+ if ( record === null ) continue ;
359+ if ( record . dedupeKey === null ) {
360+ // An anonymous record can still be unique; it just cannot dedupe
361+ // across the two projections.
362+ recordsByKey . set ( `${ table } #${ anonymous ++ } ` , record ) ;
363+ continue ;
364+ }
365+ if ( ! recordsByKey . has ( record . dedupeKey ) ) {
366+ recordsByKey . set ( record . dedupeKey , record ) ;
292367 }
293368 }
294- return [ ...recordsByKey . values ( ) ] ;
295- } catch {
296- return null ;
297- } finally {
298- database . close ( ) ;
299369 }
370+ return [ ...recordsByKey . values ( ) ] ;
300371}
0 commit comments