@gajae-code/stats 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +82 -0
  2. package/build.ts +84 -0
  3. package/dist/client/index.css +1 -0
  4. package/dist/client/index.html +13 -0
  5. package/dist/client/index.js +257 -0
  6. package/dist/client/styles.css +1159 -0
  7. package/dist/types/aggregator.d.ts +65 -0
  8. package/dist/types/client/App.d.ts +1 -0
  9. package/dist/types/client/api.d.ts +10 -0
  10. package/dist/types/client/components/BehaviorChart.d.ts +6 -0
  11. package/dist/types/client/components/BehaviorModelsTable.d.ts +7 -0
  12. package/dist/types/client/components/BehaviorSummary.d.ts +7 -0
  13. package/dist/types/client/components/ChartsContainer.d.ts +7 -0
  14. package/dist/types/client/components/CostChart.d.ts +6 -0
  15. package/dist/types/client/components/CostSummary.d.ts +6 -0
  16. package/dist/types/client/components/Header.d.ts +12 -0
  17. package/dist/types/client/components/ModelsTable.d.ts +8 -0
  18. package/dist/types/client/components/RequestDetail.d.ts +6 -0
  19. package/dist/types/client/components/RequestList.d.ts +8 -0
  20. package/dist/types/client/components/StatsGrid.d.ts +6 -0
  21. package/dist/types/client/components/chart-shared.d.ts +187 -0
  22. package/dist/types/client/components/models-table-shared.d.ts +195 -0
  23. package/dist/types/client/components/range-meta.d.ts +21 -0
  24. package/dist/types/client/index.d.ts +1 -0
  25. package/dist/types/client/types.d.ts +62 -0
  26. package/dist/types/client/useSystemTheme.d.ts +2 -0
  27. package/dist/types/db.d.ts +93 -0
  28. package/dist/types/index.d.ts +5 -0
  29. package/dist/types/parser.d.ts +40 -0
  30. package/dist/types/server.d.ts +7 -0
  31. package/dist/types/shared-types.d.ts +192 -0
  32. package/dist/types/sync-worker.d.ts +31 -0
  33. package/dist/types/types.d.ts +120 -0
  34. package/dist/types/user-metrics.d.ts +72 -0
  35. package/package.json +91 -0
  36. package/src/aggregator.ts +454 -0
  37. package/src/client/App.tsx +221 -0
  38. package/src/client/api.ts +65 -0
  39. package/src/client/components/BehaviorChart.tsx +189 -0
  40. package/src/client/components/BehaviorModelsTable.tsx +342 -0
  41. package/src/client/components/BehaviorSummary.tsx +95 -0
  42. package/src/client/components/ChartsContainer.tsx +221 -0
  43. package/src/client/components/CostChart.tsx +171 -0
  44. package/src/client/components/CostSummary.tsx +53 -0
  45. package/src/client/components/Header.tsx +72 -0
  46. package/src/client/components/ModelsTable.tsx +265 -0
  47. package/src/client/components/RequestDetail.tsx +172 -0
  48. package/src/client/components/RequestList.tsx +73 -0
  49. package/src/client/components/StatsGrid.tsx +135 -0
  50. package/src/client/components/chart-shared.tsx +320 -0
  51. package/src/client/components/models-table-shared.tsx +275 -0
  52. package/src/client/components/range-meta.ts +72 -0
  53. package/src/client/css.d.ts +1 -0
  54. package/src/client/index.tsx +6 -0
  55. package/src/client/styles.css +306 -0
  56. package/src/client/types.ts +78 -0
  57. package/src/client/useSystemTheme.ts +31 -0
  58. package/src/db.ts +1100 -0
  59. package/src/embedded-client.generated.txt +7 -0
  60. package/src/index.ts +182 -0
  61. package/src/parser.ts +334 -0
  62. package/src/server.ts +325 -0
  63. package/src/shared-types.ts +204 -0
  64. package/src/sync-worker.ts +40 -0
  65. package/src/types.ts +125 -0
  66. package/src/user-metrics.ts +686 -0
  67. package/tailwind.config.js +40 -0
package/src/server.ts ADDED
@@ -0,0 +1,325 @@
1
+ import * as fs from "node:fs/promises";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import { $ } from "bun";
5
+ import {
6
+ getBehaviorDashboardStats,
7
+ getCostDashboardStats,
8
+ getDashboardStats,
9
+ getModelDashboardStats,
10
+ getOverviewStats,
11
+ getRecentErrors,
12
+ getRecentRequests,
13
+ getRequestDetails,
14
+ getTotalMessageCount,
15
+ syncAllSessions,
16
+ } from "./aggregator";
17
+ import embeddedClientArchiveTxt from "./embedded-client.generated.txt";
18
+
19
+ const getEmbeddedClientArchive = (() => {
20
+ const txt = embeddedClientArchiveTxt.replaceAll(/[\s\r\n]/g, "").trim();
21
+ if (!txt) return null;
22
+ return () => Buffer.from(txt, "base64");
23
+ })();
24
+
25
+ const CLIENT_DIR = path.join(import.meta.dir, "client");
26
+ const STATIC_DIR = path.join(import.meta.dir, "..", "dist", "client");
27
+ const IS_BUN_COMPILED =
28
+ Bun.env.PI_COMPILED ||
29
+ import.meta.url.includes("$bunfs") ||
30
+ import.meta.url.includes("~BUN") ||
31
+ import.meta.url.includes("%7EBUN");
32
+
33
+ const COMPILED_CLIENT_DIR_ROOT = path.join(os.tmpdir(), "gjc-stats-client");
34
+ let compiledClientDirPromise: Promise<string> | null = null;
35
+
36
+ function sanitizeArchivePath(archivePath: string): string | null {
37
+ const normalized = archivePath.replaceAll("\\", "/").replace(/^\.\//, "");
38
+ if (!normalized || normalized === ".") return null;
39
+ if (normalized.includes("..") || path.isAbsolute(normalized)) return null;
40
+ return normalized;
41
+ }
42
+
43
+ async function extractEmbeddedClientArchive(archiveBytes: Buffer, outputDir: string): Promise<void> {
44
+ const archive = new Bun.Archive(archiveBytes);
45
+ const files = await archive.files();
46
+ const extractRoot = path.resolve(outputDir);
47
+
48
+ for (const [archivePath, file] of files) {
49
+ const sanitizedPath = sanitizeArchivePath(archivePath);
50
+ if (!sanitizedPath) continue;
51
+ const destinationPath = path.resolve(extractRoot, sanitizedPath);
52
+ if (!destinationPath.startsWith(extractRoot + path.sep)) {
53
+ throw new Error(`Archive entry escapes extraction directory: ${archivePath}`);
54
+ }
55
+ await Bun.write(destinationPath, file);
56
+ }
57
+ }
58
+
59
+ async function getCompiledClientDir(): Promise<string> {
60
+ if (!IS_BUN_COMPILED) return STATIC_DIR;
61
+ if (compiledClientDirPromise) return compiledClientDirPromise;
62
+
63
+ const archiveBytes = getEmbeddedClientArchive?.();
64
+ if (!archiveBytes) {
65
+ throw new Error("Compiled stats client bundle missing. Rebuild binary with embedded stats assets.");
66
+ }
67
+
68
+ compiledClientDirPromise = (async () => {
69
+ const bundleHash = Bun.hash(archiveBytes).toString(16);
70
+ const outputDir = path.join(COMPILED_CLIENT_DIR_ROOT, bundleHash);
71
+ const markerPath = path.join(outputDir, "index.html");
72
+ try {
73
+ const marker = await fs.stat(markerPath);
74
+ if (marker.isFile()) return outputDir;
75
+ } catch {}
76
+
77
+ await fs.rm(outputDir, { recursive: true, force: true });
78
+ await fs.mkdir(outputDir, { recursive: true });
79
+ await extractEmbeddedClientArchive(archiveBytes, outputDir);
80
+ return outputDir;
81
+ })();
82
+
83
+ return compiledClientDirPromise;
84
+ }
85
+
86
+ async function getLatestMtime(dir: string): Promise<number> {
87
+ const entries = await fs.readdir(dir, { withFileTypes: true });
88
+
89
+ const promises = [];
90
+ for (const entry of entries) {
91
+ const fullPath = path.join(dir, entry.name);
92
+ if (entry.isDirectory()) {
93
+ promises.push(getLatestMtime(fullPath));
94
+ } else if (entry.isFile()) {
95
+ promises.push(fs.stat(fullPath).then(stats => stats.mtimeMs));
96
+ }
97
+ }
98
+
99
+ let latest = 0;
100
+ await Promise.allSettled(promises).then(results => {
101
+ for (const result of results) {
102
+ if (result.status === "fulfilled") {
103
+ latest = Math.max(latest, result.value);
104
+ }
105
+ }
106
+ });
107
+ return latest;
108
+ }
109
+
110
+ const ensureClientBuild = async () => {
111
+ if (IS_BUN_COMPILED) return;
112
+ const indexPath = path.join(STATIC_DIR, "index.html");
113
+ const cssPath = path.join(STATIC_DIR, "styles.css");
114
+ const clientSourceMtime = await getLatestMtime(CLIENT_DIR);
115
+ const tailwindConfigPath = path.join(import.meta.dir, "..", "tailwind.config.js");
116
+ let tailwindConfigMtime = 0;
117
+ try {
118
+ const tailwindConfigStats = await fs.stat(tailwindConfigPath);
119
+ tailwindConfigMtime = tailwindConfigStats.mtimeMs;
120
+ } catch {}
121
+ const sourceMtime = Math.max(clientSourceMtime, tailwindConfigMtime);
122
+ let shouldBuild = true;
123
+ try {
124
+ const [indexStats, cssStats] = await Promise.all([fs.stat(indexPath), fs.stat(cssPath)]);
125
+ if (
126
+ indexStats.isFile() &&
127
+ cssStats.isFile() &&
128
+ indexStats.mtimeMs >= sourceMtime &&
129
+ cssStats.mtimeMs >= sourceMtime
130
+ ) {
131
+ shouldBuild = false;
132
+ }
133
+ } catch {
134
+ shouldBuild = true;
135
+ }
136
+
137
+ if (!shouldBuild) return;
138
+
139
+ await fs.rm(STATIC_DIR, { recursive: true, force: true });
140
+
141
+ console.log("Building stats client...");
142
+ const packageRoot = path.join(import.meta.dir, "..");
143
+ const buildResult = await $`bun run build.ts`.cwd(packageRoot).quiet().nothrow();
144
+ if (buildResult.exitCode !== 0) {
145
+ const output = buildResult.text().trim();
146
+ const details = output ? `\n${output}` : "";
147
+ throw new Error(`Failed to build stats client (exit ${buildResult.exitCode})${details}`);
148
+ }
149
+
150
+ const indexHtml = `<!DOCTYPE html>
151
+ <html lang="en">
152
+ <head>
153
+ <meta charset="UTF-8">
154
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
155
+ <title>AI Usage Statistics</title>
156
+ <link rel="stylesheet" href="styles.css">
157
+ </head>
158
+ <body>
159
+ <div id="root"></div>
160
+ <script src="index.js" type="module"></script>
161
+ </body>
162
+ </html>`;
163
+
164
+ await Bun.write(path.join(STATIC_DIR, "index.html"), indexHtml);
165
+ };
166
+
167
+ /**
168
+ * Handle API requests.
169
+ */
170
+ async function handleApi(req: Request): Promise<Response> {
171
+ const url = new URL(req.url);
172
+ const path = url.pathname;
173
+
174
+ // Stats reads are DB-only; explicit /api/sync does the expensive session scan.
175
+ const range = url.searchParams.get("range");
176
+
177
+ if (path === "/api/stats") {
178
+ const stats = await getDashboardStats(range);
179
+ return Response.json(stats);
180
+ }
181
+
182
+ if (path === "/api/stats/overview") {
183
+ const stats = await getOverviewStats(range);
184
+ return Response.json(stats);
185
+ }
186
+
187
+ if (path === "/api/stats/model-dashboard") {
188
+ const stats = await getModelDashboardStats(range);
189
+ return Response.json(stats);
190
+ }
191
+
192
+ if (path === "/api/stats/costs") {
193
+ const stats = await getCostDashboardStats(range);
194
+ return Response.json(stats);
195
+ }
196
+
197
+ if (path === "/api/stats/behavior") {
198
+ const stats = await getBehaviorDashboardStats(range);
199
+ return Response.json(stats);
200
+ }
201
+
202
+ if (path === "/api/stats/recent") {
203
+ const limit = url.searchParams.get("limit");
204
+ const stats = await getRecentRequests(limit ? parseInt(limit, 10) : undefined);
205
+ return Response.json(stats);
206
+ }
207
+
208
+ if (path === "/api/stats/errors") {
209
+ const limit = url.searchParams.get("limit");
210
+ const stats = await getRecentErrors(limit ? parseInt(limit, 10) : undefined);
211
+ return Response.json(stats);
212
+ }
213
+
214
+ if (path === "/api/stats/models") {
215
+ const stats = await getDashboardStats(range);
216
+ return Response.json(stats.byModel);
217
+ }
218
+
219
+ if (path === "/api/stats/folders") {
220
+ const stats = await getDashboardStats(range);
221
+ return Response.json(stats.byFolder);
222
+ }
223
+
224
+ if (path === "/api/stats/timeseries") {
225
+ const stats = await getDashboardStats(range);
226
+ return Response.json(stats.timeSeries);
227
+ }
228
+
229
+ if (path.startsWith("/api/request/")) {
230
+ const id = path.split("/").pop();
231
+ if (!id) return new Response("Bad Request", { status: 400 });
232
+ const details = await getRequestDetails(parseInt(id, 10));
233
+ if (!details) return new Response("Not Found", { status: 404 });
234
+ return Response.json(details);
235
+ }
236
+
237
+ if (path === "/api/sync") {
238
+ const result = await syncAllSessions();
239
+ const count = await getTotalMessageCount();
240
+ return Response.json({ ...result, totalMessages: count });
241
+ }
242
+
243
+ return new Response("Not Found", { status: 404 });
244
+ }
245
+
246
+ /**
247
+ * Handle static file requests.
248
+ */
249
+ async function handleStatic(requestPath: string): Promise<Response> {
250
+ const staticDir = IS_BUN_COMPILED ? await getCompiledClientDir() : STATIC_DIR;
251
+ const filePath = requestPath === "/" ? "/index.html" : requestPath;
252
+ const fullPath = path.join(staticDir, filePath);
253
+
254
+ const file = Bun.file(fullPath);
255
+ if (await file.exists()) {
256
+ return new Response(file);
257
+ }
258
+
259
+ // SPA fallback
260
+ const index = Bun.file(path.join(staticDir, "index.html"));
261
+ if (await index.exists()) {
262
+ return new Response(index);
263
+ }
264
+
265
+ return new Response("Not Found", { status: 404 });
266
+ }
267
+
268
+ /**
269
+ * Start the HTTP server.
270
+ */
271
+ export async function startServer(port = 3847): Promise<{ port: number; stop: () => void }> {
272
+ await ensureClientBuild();
273
+
274
+ const server = Bun.serve({
275
+ hostname: "127.0.0.1",
276
+ port,
277
+ async fetch(req) {
278
+ const url = new URL(req.url);
279
+ const path = url.pathname;
280
+
281
+ // CORS headers for local development
282
+ const corsHeaders = {
283
+ "Access-Control-Allow-Origin": "*",
284
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
285
+ "Access-Control-Allow-Headers": "Content-Type",
286
+ };
287
+
288
+ if (req.method === "OPTIONS") {
289
+ return new Response(null, { headers: corsHeaders });
290
+ }
291
+
292
+ try {
293
+ let response: Response;
294
+
295
+ if (path.startsWith("/api/")) {
296
+ response = await handleApi(req);
297
+ } else {
298
+ response = await handleStatic(path);
299
+ }
300
+
301
+ // Add CORS headers to all responses
302
+ const headers = new Headers(response.headers);
303
+ for (const [key, value] of Object.entries(corsHeaders)) {
304
+ headers.set(key, value);
305
+ }
306
+
307
+ return new Response(response.body, {
308
+ status: response.status,
309
+ headers,
310
+ });
311
+ } catch (error) {
312
+ console.error("Server error:", error);
313
+ return Response.json(
314
+ { error: error instanceof Error ? error.message : "Unknown error" },
315
+ { status: 500, headers: corsHeaders },
316
+ );
317
+ }
318
+ },
319
+ });
320
+
321
+ return {
322
+ port: server.port ?? port,
323
+ stop: () => server.stop(),
324
+ };
325
+ }
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Shared type definitions consumed by both the server-side stats code and the
3
+ * standalone client bundle. Keep this file free of any imports from server-only
4
+ * packages (e.g. `@gajae-code/ai`, `bun:sqlite`) so the client can import it
5
+ * without dragging server dependencies into its bundle.
6
+ */
7
+
8
+ /**
9
+ * Aggregated stats for a model or folder.
10
+ */
11
+ export interface AggregatedStats {
12
+ /** Total number of requests */
13
+ totalRequests: number;
14
+ /** Number of successful requests */
15
+ successfulRequests: number;
16
+ /** Number of failed requests */
17
+ failedRequests: number;
18
+ /** Error rate (0-1) */
19
+ errorRate: number;
20
+ /** Total input tokens */
21
+ totalInputTokens: number;
22
+ /** Total output tokens */
23
+ totalOutputTokens: number;
24
+ /** Total cache read tokens */
25
+ totalCacheReadTokens: number;
26
+ /** Total cache write tokens */
27
+ totalCacheWriteTokens: number;
28
+ /** Cache hit rate (0-1) */
29
+ cacheRate: number;
30
+ /** Total cost */
31
+ totalCost: number;
32
+ /** Total premium requests */
33
+ totalPremiumRequests: number;
34
+ /** Average duration in ms */
35
+ avgDuration: number | null;
36
+ /** Average TTFT in ms */
37
+ avgTtft: number | null;
38
+ /** Average tokens per second (output tokens / duration) */
39
+ avgTokensPerSecond: number | null;
40
+ /** Time range */
41
+ firstTimestamp: number;
42
+ lastTimestamp: number;
43
+ }
44
+
45
+ /**
46
+ * Stats grouped by model.
47
+ */
48
+ export interface ModelStats extends AggregatedStats {
49
+ model: string;
50
+ provider: string;
51
+ }
52
+
53
+ /**
54
+ * Stats grouped by folder.
55
+ */
56
+ export interface FolderStats extends AggregatedStats {
57
+ folder: string;
58
+ }
59
+
60
+ /**
61
+ * Time series data point.
62
+ */
63
+ export interface TimeSeriesPoint {
64
+ /** Bucket timestamp (start of hour/day) */
65
+ timestamp: number;
66
+ /** Request count */
67
+ requests: number;
68
+ /** Error count */
69
+ errors: number;
70
+ /** Total tokens */
71
+ tokens: number;
72
+ /** Total cost */
73
+ cost: number;
74
+ }
75
+
76
+ /**
77
+ * Model usage time series data point (daily buckets).
78
+ */
79
+ export interface ModelTimeSeriesPoint {
80
+ /** Bucket timestamp (start of day) */
81
+ timestamp: number;
82
+ /** Model name */
83
+ model: string;
84
+ /** Provider name */
85
+ provider: string;
86
+ /** Request count */
87
+ requests: number;
88
+ }
89
+
90
+ /**
91
+ * Model performance time series data point (daily buckets).
92
+ */
93
+ export interface ModelPerformancePoint {
94
+ /** Bucket timestamp (start of day) */
95
+ timestamp: number;
96
+ /** Model name */
97
+ model: string;
98
+ /** Provider name */
99
+ provider: string;
100
+ /** Request count */
101
+ requests: number;
102
+ /** Average TTFT in ms */
103
+ avgTtft: number | null;
104
+ /** Average tokens per second */
105
+ avgTokensPerSecond: number | null;
106
+ }
107
+
108
+ /**
109
+ * Cost time series data point (daily buckets).
110
+ */
111
+ export interface CostTimeSeriesPoint {
112
+ /** Bucket timestamp (start of day) */
113
+ timestamp: number;
114
+ /** Model name */
115
+ model: string;
116
+ /** Provider name */
117
+ provider: string;
118
+ /** Total cost for this bucket */
119
+ cost: number;
120
+ /** Cost breakdown */
121
+ costInput: number;
122
+ costOutput: number;
123
+ costCacheRead: number;
124
+ costCacheWrite: number;
125
+ /** Request count */
126
+ requests: number;
127
+ }
128
+
129
+ /**
130
+ * Overall dashboard stats.
131
+ */
132
+ export interface DashboardStats {
133
+ overall: AggregatedStats;
134
+ byModel: ModelStats[];
135
+ byFolder: FolderStats[];
136
+ timeSeries: TimeSeriesPoint[];
137
+ modelSeries: ModelTimeSeriesPoint[];
138
+ modelPerformanceSeries: ModelPerformancePoint[];
139
+ costSeries: CostTimeSeriesPoint[];
140
+ }
141
+
142
+ /**
143
+ * Behavior time-series point (daily bucket, per responding model).
144
+ */
145
+ export interface BehaviorTimeSeriesPoint {
146
+ /** Bucket timestamp (start of day) */
147
+ timestamp: number;
148
+ /** Responding model ("unknown" if user msg never got a reply) */
149
+ model: string;
150
+ /** Responding provider */
151
+ provider: string;
152
+ /** Number of user messages in bucket */
153
+ messages: number;
154
+ /** Total yelling sentences in bucket */
155
+ yelling: number;
156
+ /** Total profanity hits in bucket */
157
+ profanity: number;
158
+ /** Total anguish signal in bucket */
159
+ anguish: number;
160
+ /** Total corrective-negation hits in bucket */
161
+ negation: number;
162
+ /** Total user-repeating-themselves hits in bucket */
163
+ repetition: number;
164
+ /** Total second-person blame hits in bucket */
165
+ blame: number;
166
+ /** Total characters in bucket */
167
+ chars: number;
168
+ }
169
+
170
+ export interface BehaviorOverallStats {
171
+ totalMessages: number;
172
+ totalYelling: number;
173
+ totalProfanity: number;
174
+ totalAnguish: number;
175
+ totalNegation: number;
176
+ totalRepetition: number;
177
+ totalBlame: number;
178
+ totalChars: number;
179
+ firstTimestamp: number;
180
+ lastTimestamp: number;
181
+ }
182
+
183
+ /**
184
+ * Per-model behavioral aggregate over the active range.
185
+ */
186
+ export interface BehaviorModelStats {
187
+ model: string;
188
+ provider: string;
189
+ totalMessages: number;
190
+ totalYelling: number;
191
+ totalProfanity: number;
192
+ totalAnguish: number;
193
+ totalNegation: number;
194
+ totalRepetition: number;
195
+ totalBlame: number;
196
+ totalChars: number;
197
+ lastTimestamp: number;
198
+ }
199
+
200
+ export interface BehaviorDashboardStats {
201
+ overall: BehaviorOverallStats;
202
+ byModel: BehaviorModelStats[];
203
+ behaviorSeries: BehaviorTimeSeriesPoint[];
204
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Stateless parse worker for `syncAllSessions`. The main thread owns the
3
+ * SQLite handle; workers receive `{ sessionFile, fromOffset }`, run
4
+ * `parseSessionFile` (which is pure I/O + CPU, no DB), and post the
5
+ * structured-clone-safe result back. One in-flight request per worker so
6
+ * the main thread can fan jobs out 1:1 with the pool size.
7
+ *
8
+ * A `{ kind: "ping" }` request is also accepted and replies with
9
+ * `{ ok: true, kind: "pong" }` — used by `smokeTestSyncWorker` to prove the
10
+ * worker actually spawns and runs in compiled binaries (regression coverage
11
+ * for issue #1011 / PR #1027, where the worker silently failed to load).
12
+ */
13
+
14
+ import { type ParseSessionResult, parseSessionFile } from "./parser";
15
+
16
+ export type SyncWorkerRequest = { kind?: "parse"; sessionFile: string; fromOffset: number } | { kind: "ping" };
17
+
18
+ export type SyncWorkerResponse =
19
+ | { ok: true; kind?: "parse"; result: ParseSessionResult }
20
+ | { ok: true; kind: "pong" }
21
+ | { ok: false; error: string };
22
+
23
+ declare const self: Worker & {
24
+ onmessage: ((event: MessageEvent<SyncWorkerRequest>) => void) | null;
25
+ };
26
+
27
+ self.onmessage = async event => {
28
+ const request = event.data;
29
+ try {
30
+ if (request.kind === "ping") {
31
+ self.postMessage({ ok: true, kind: "pong" } satisfies SyncWorkerResponse);
32
+ return;
33
+ }
34
+ const result = await parseSessionFile(request.sessionFile, request.fromOffset);
35
+ self.postMessage({ ok: true, result } satisfies SyncWorkerResponse);
36
+ } catch (err) {
37
+ const error = err instanceof Error ? (err.stack ?? err.message) : String(err);
38
+ self.postMessage({ ok: false, error } satisfies SyncWorkerResponse);
39
+ }
40
+ };
package/src/types.ts ADDED
@@ -0,0 +1,125 @@
1
+ import type { AssistantMessage, ServiceTier, StopReason, Usage } from "@gajae-code/ai";
2
+
3
+ export * from "./shared-types";
4
+
5
+ /**
6
+ * Extracted stats from an assistant message.
7
+ */
8
+ export interface MessageStats {
9
+ /** Database ID */
10
+ id?: number;
11
+ /** Session file path */
12
+ sessionFile: string;
13
+ /** Entry ID within the session */
14
+ entryId: string;
15
+ /** Folder/project path (extracted from session filename) */
16
+ folder: string;
17
+ /** Model ID */
18
+ model: string;
19
+ /** Provider name */
20
+ provider: string;
21
+ /** API type */
22
+ api: string;
23
+ /** Unix timestamp in milliseconds */
24
+ timestamp: number;
25
+ /** Request duration in milliseconds */
26
+ duration: number | null;
27
+ /** Time to first token in milliseconds */
28
+ ttft: number | null;
29
+ /** Stop reason */
30
+ stopReason: StopReason;
31
+ /** Error message if stopReason is error */
32
+ errorMessage: string | null;
33
+ /** Token usage */
34
+ usage: Usage;
35
+ }
36
+
37
+ /**
38
+ * Full details of a request, including content.
39
+ */
40
+ export interface RequestDetails extends MessageStats {
41
+ /** The full conversation history or just the last turn. */
42
+ messages: unknown[];
43
+ /** The model's response. */
44
+ output: unknown;
45
+ }
46
+
47
+ /**
48
+ * Session log entry types.
49
+ */
50
+ export interface SessionHeader {
51
+ type: "session";
52
+ version: number;
53
+ id: string;
54
+ timestamp: string;
55
+ cwd: string;
56
+ title?: string;
57
+ }
58
+
59
+ export interface SessionMessageEntry {
60
+ type: "message";
61
+ id: string;
62
+ parentId: string | null;
63
+ timestamp: string;
64
+ message: AssistantMessage | { role: "user" | "toolResult" };
65
+ }
66
+
67
+ export interface SessionServiceTierChangeEntry {
68
+ type: "service_tier_change";
69
+ id: string;
70
+ parentId?: string | null;
71
+ timestamp: string;
72
+ serviceTier: ServiceTier | null;
73
+ }
74
+
75
+ export type SessionEntry = SessionHeader | SessionMessageEntry | SessionServiceTierChangeEntry | { type: string };
76
+
77
+ /**
78
+ * Behavioral stats extracted from a single user message.
79
+ */
80
+ export interface UserMessageStats {
81
+ /** Database ID */
82
+ id?: number;
83
+ /** Session file path */
84
+ sessionFile: string;
85
+ /** Entry ID within the session */
86
+ entryId: string;
87
+ /** Folder/project path */
88
+ folder: string;
89
+ /** Unix timestamp in ms */
90
+ timestamp: number;
91
+ /** Model that responded to this user message, if linked */
92
+ model: string | null;
93
+ /** Provider that responded to this user message, if linked */
94
+ provider: string | null;
95
+ /** Total characters of message text */
96
+ chars: number;
97
+ /** Whitespace-delimited word count */
98
+ words: number;
99
+ /** Yelling sentences (> 50% uppercase letters) */
100
+ yelling: number;
101
+ /** Profanity hits */
102
+ profanity: number;
103
+ /** Catch-all upset signal: drama runs + `noooo`/`ughh`/... + `dude` + `..` */
104
+ anguish: number;
105
+ /** Corrective negation ("no", "nope", "thats not what i meant") */
106
+ negation: number;
107
+ /** User repeating themselves ("i meant", "still doesnt work", "like i said") */
108
+ repetition: number;
109
+ /** Second-person reproach ("you didnt", "you broke", "stop X-ing") */
110
+ blame: number;
111
+ }
112
+
113
+ /**
114
+ * Pair emitted by the parser when it sees an assistant message whose
115
+ * `parentId` points to a user message that wasn't parsed in the same pass
116
+ * (e.g. user prompt landed in an earlier incremental sync). The aggregator
117
+ * applies the link to the persisted `user_messages` row so it stops showing
118
+ * up in the "unknown" model bucket.
119
+ */
120
+ export interface UserMessageLink {
121
+ sessionFile: string;
122
+ entryId: string;
123
+ model: string;
124
+ provider: string;
125
+ }