@oh-my-pi/omp-stats 8.1.0 → 8.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oh-my-pi/omp-stats",
3
- "version": "8.1.0",
3
+ "version": "8.2.1",
4
4
  "description": "Local observability dashboard for pi AI usage statistics",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -37,9 +37,8 @@
37
37
  ],
38
38
  "scripts": {
39
39
  "dev": "bun run src/index.ts",
40
- "check": "tsgo --noEmit && tsgo --noEmit -p tsconfig.client.json",
41
- "build": "bun run build.ts && tsgo -p tsconfig.build.json",
42
- "prepublishOnly": "cp tsconfig.publish.json tsconfig.json"
40
+ "check": "tsgo -p tsconfig.json && tsgo -p tsconfig.client.json",
41
+ "build": "bun run build.ts"
43
42
  },
44
43
  "author": "Can Bölük",
45
44
  "license": "MIT",
@@ -49,9 +48,9 @@
49
48
  "directory": "packages/stats"
50
49
  },
51
50
  "dependencies": {
52
- "@oh-my-pi/pi-ai": "workspace:*",
51
+ "@oh-my-pi/pi-ai": "8.2.1",
53
52
  "date-fns": "^4.1.0",
54
- "lucide-react": "^0.562.0",
53
+ "lucide-react": "^0.563.0",
55
54
  "react": "^19.2.3",
56
55
  "react-dom": "^19.2.3",
57
56
  "recharts": "^3.6.0"
package/src/aggregator.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { stat } from "node:fs/promises";
1
+ import * as fs from "node:fs/promises";
2
2
  import {
3
3
  getRecentErrors as dbGetRecentErrors,
4
4
  getRecentRequests as dbGetRecentRequests,
@@ -22,9 +22,9 @@ import type { DashboardStats, MessageStats, RequestDetails } from "./types";
22
22
  */
23
23
  async function syncSessionFile(sessionFile: string): Promise<number> {
24
24
  // Get file stats
25
- let fileStats: Awaited<ReturnType<typeof stat>>;
25
+ let fileStats: Awaited<ReturnType<typeof fs.stat>>;
26
26
  try {
27
- fileStats = await stat(sessionFile);
27
+ fileStats = await fs.stat(sessionFile);
28
28
  } catch {
29
29
  return 0;
30
30
  }
package/src/db.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { Database } from "bun:sqlite";
2
- import { mkdir } from "node:fs/promises";
3
- import { homedir } from "node:os";
4
- import { join } from "node:path";
2
+ import * as fs from "node:fs/promises";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
5
  import type { AggregatedStats, FolderStats, MessageStats, ModelStats, TimeSeriesPoint } from "./types";
6
6
 
7
- const DB_PATH = join(homedir(), ".omp", "stats.db");
7
+ const DB_PATH = path.join(os.homedir(), ".omp", "stats.db");
8
8
 
9
9
  let db: Database | null = null;
10
10
 
@@ -15,7 +15,7 @@ export async function initDb(): Promise<Database> {
15
15
  if (db) return db;
16
16
 
17
17
  // Ensure directory exists
18
- await mkdir(join(homedir(), ".omp"), { recursive: true });
18
+ await fs.mkdir(path.join(os.homedir(), ".omp"), { recursive: true });
19
19
 
20
20
  db = new Database(DB_PATH);
21
21
  db.exec("PRAGMA journal_mode = WAL");
@@ -245,7 +245,7 @@ export function getStatsByModel(): ModelStats[] {
245
245
  `);
246
246
 
247
247
  const rows = stmt.all() as any[];
248
- return rows.map((row) => ({
248
+ return rows.map(row => ({
249
249
  model: row.model,
250
250
  provider: row.provider,
251
251
  ...buildAggregatedStats([row]),
@@ -279,7 +279,7 @@ export function getStatsByFolder(): FolderStats[] {
279
279
  `);
280
280
 
281
281
  const rows = stmt.all() as any[];
282
- return rows.map((row) => ({
282
+ return rows.map(row => ({
283
283
  folder: row.folder,
284
284
  ...buildAggregatedStats([row]),
285
285
  }));
@@ -307,7 +307,7 @@ export function getTimeSeries(hours = 24): TimeSeriesPoint[] {
307
307
  `);
308
308
 
309
309
  const rows = stmt.all(cutoff) as any[];
310
- return rows.map((row) => ({
310
+ return rows.map(row => ({
311
311
  timestamp: row.bucket,
312
312
  requests: row.requests,
313
313
  errors: row.errors,
package/src/index.ts CHANGED
@@ -6,14 +6,7 @@ import { closeDb } from "./db";
6
6
  import { startServer } from "./server";
7
7
 
8
8
  export { getDashboardStats, getTotalMessageCount, syncAllSessions } from "./aggregator";
9
- export type {
10
- AggregatedStats,
11
- DashboardStats,
12
- FolderStats,
13
- MessageStats,
14
- ModelStats,
15
- TimeSeriesPoint,
16
- } from "./types";
9
+ export type { AggregatedStats, DashboardStats, FolderStats, MessageStats, ModelStats, TimeSeriesPoint } from "./types";
17
10
 
18
11
  /**
19
12
  * Format a number with appropriate suffix (K, M, etc.)
@@ -145,7 +138,7 @@ Examples:
145
138
 
146
139
  // Start server
147
140
  const port = parseInt(values.port || "3847", 10);
148
- const { port: actualPort } = startServer(port);
141
+ const { port: actualPort } = await startServer(port);
149
142
  console.log(`Dashboard available at: http://localhost:${actualPort}`);
150
143
  console.log("Press Ctrl+C to stop\n");
151
144
 
package/src/parser.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { readdir } from "node:fs/promises";
2
- import { homedir } from "node:os";
3
- import { basename, join } from "node:path";
1
+ import * as fs from "node:fs/promises";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
4
  import type { AssistantMessage } from "@oh-my-pi/pi-ai";
5
5
  import type { MessageStats, SessionEntry, SessionMessageEntry } from "./types";
6
6
 
7
- const SESSIONS_DIR = join(homedir(), ".omp", "agent", "sessions");
7
+ const SESSIONS_DIR = path.join(os.homedir(), ".omp", "agent", "sessions");
8
8
 
9
9
  /**
10
10
  * Extract folder name from session filename.
@@ -12,7 +12,7 @@ const SESSIONS_DIR = join(homedir(), ".omp", "agent", "sessions");
12
12
  * The folder part uses -- as path separator.
13
13
  */
14
14
  function extractFolderFromPath(sessionPath: string): string {
15
- const dir = basename(sessionPath.replace(/\/[^/]+\.jsonl$/, ""));
15
+ const dir = path.basename(sessionPath.replace(/\/[^/]+\.jsonl$/, ""));
16
16
  // Convert --work--pi-- to /work/pi
17
17
  return dir.replace(/^--/, "/").replace(/--/g, "/");
18
18
  }
@@ -105,8 +105,8 @@ export async function parseSessionFile(
105
105
  */
106
106
  export async function listSessionFolders(): Promise<string[]> {
107
107
  try {
108
- const entries = await readdir(SESSIONS_DIR, { withFileTypes: true });
109
- return entries.filter((e) => e.isDirectory()).map((e) => join(SESSIONS_DIR, e.name));
108
+ const entries = await fs.readdir(SESSIONS_DIR, { withFileTypes: true });
109
+ return entries.filter(e => e.isDirectory()).map(e => path.join(SESSIONS_DIR, e.name));
110
110
  } catch {
111
111
  return [];
112
112
  }
@@ -117,8 +117,8 @@ export async function listSessionFolders(): Promise<string[]> {
117
117
  */
118
118
  export async function listSessionFiles(folderPath: string): Promise<string[]> {
119
119
  try {
120
- const entries = await readdir(folderPath, { withFileTypes: true });
121
- return entries.filter((e) => e.isFile() && e.name.endsWith(".jsonl")).map((e) => join(folderPath, e.name));
120
+ const entries = await fs.readdir(folderPath, { withFileTypes: true });
121
+ return entries.filter(e => e.isFile() && e.name.endsWith(".jsonl")).map(e => path.join(folderPath, e.name));
122
122
  } catch {
123
123
  return [];
124
124
  }
package/src/server.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { join } from "node:path";
1
+ import * as fs from "node:fs/promises";
2
+ import * as path from "node:path";
2
3
  import {
3
4
  getDashboardStats,
4
5
  getRecentErrors,
@@ -8,7 +9,68 @@ import {
8
9
  syncAllSessions,
9
10
  } from "./aggregator";
10
11
 
11
- const STATIC_DIR = join(import.meta.dir, "..", "dist", "client");
12
+ const CLIENT_DIR = path.join(import.meta.dir, "client");
13
+ const STATIC_DIR = path.join(import.meta.dir, "..", "dist", "client");
14
+
15
+ const ensureClientBuild = async () => {
16
+ const indexFile = Bun.file(path.join(STATIC_DIR, "index.html"));
17
+ if (await indexFile.exists()) return;
18
+
19
+ await fs.rm(STATIC_DIR, { recursive: true, force: true });
20
+
21
+ const result = await Bun.build({
22
+ entrypoints: [path.join(CLIENT_DIR, "index.tsx")],
23
+ outdir: STATIC_DIR,
24
+ minify: true,
25
+ naming: "[dir]/[name].[ext]",
26
+ });
27
+
28
+ if (!result.success) {
29
+ const errors = result.logs.map(log => log.message).join("\n");
30
+ throw new Error(`Failed to build stats client:\n${errors}`);
31
+ }
32
+
33
+ const indexHtml = `<!DOCTYPE html>
34
+ <html lang="en">
35
+ <head>
36
+ <meta charset="UTF-8">
37
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
38
+ <title>AI Usage Statistics</title>
39
+ <style>
40
+ :root {
41
+ --bg-primary: #1a1a2e;
42
+ --bg-secondary: #16213e;
43
+ --bg-card: #0f3460;
44
+ --text-primary: #eee;
45
+ --text-secondary: #aaa;
46
+ --accent: #e94560;
47
+ --success: #4ade80;
48
+ --error: #f87171;
49
+ --border: #1f2937;
50
+ }
51
+ body {
52
+ margin: 0;
53
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
54
+ background: var(--bg-primary);
55
+ color: var(--text-primary);
56
+ }
57
+ * { box-sizing: border-box; }
58
+ ::-webkit-scrollbar { width: 8px; height: 8px; }
59
+ ::-webkit-scrollbar-track { background: var(--bg-primary); }
60
+ ::-webkit-scrollbar-thumb { background: var(--bg-card); border-radius: 4px; }
61
+ ::-webkit-scrollbar-thumb:hover { background: var(--accent); }
62
+ .spin { animation: spin 1s linear infinite; }
63
+ @keyframes spin { 100% { transform: rotate(360deg); } }
64
+ </style>
65
+ </head>
66
+ <body>
67
+ <div id="root"></div>
68
+ <script src="index.js" type="module"></script>
69
+ </body>
70
+ </html>`;
71
+
72
+ await Bun.write(path.join(STATIC_DIR, "index.html"), indexHtml);
73
+ };
12
74
 
13
75
  /**
14
76
  * Handle API requests.
@@ -72,9 +134,9 @@ async function handleApi(req: Request): Promise<Response> {
72
134
  /**
73
135
  * Handle static file requests.
74
136
  */
75
- async function handleStatic(path: string): Promise<Response> {
76
- const filePath = path === "/" ? "/index.html" : path;
77
- const fullPath = join(STATIC_DIR, filePath);
137
+ async function handleStatic(requestPath: string): Promise<Response> {
138
+ const filePath = requestPath === "/" ? "/index.html" : requestPath;
139
+ const fullPath = path.join(STATIC_DIR, filePath);
78
140
 
79
141
  const file = Bun.file(fullPath);
80
142
  if (await file.exists()) {
@@ -82,7 +144,7 @@ async function handleStatic(path: string): Promise<Response> {
82
144
  }
83
145
 
84
146
  // SPA fallback
85
- const index = Bun.file(join(STATIC_DIR, "index.html"));
147
+ const index = Bun.file(path.join(STATIC_DIR, "index.html"));
86
148
  if (await index.exists()) {
87
149
  return new Response(index);
88
150
  }
@@ -93,7 +155,9 @@ async function handleStatic(path: string): Promise<Response> {
93
155
  /**
94
156
  * Start the HTTP server.
95
157
  */
96
- export function startServer(port = 3847): { port: number; stop: () => void } {
158
+ export async function startServer(port = 3847): Promise<{ port: number; stop: () => void }> {
159
+ await ensureClientBuild();
160
+
97
161
  const server = Bun.serve({
98
162
  port,
99
163
  async fetch(req) {