opencode-statboard 2.0.6 → 2.0.8

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 (3) hide show
  1. package/AGENTS.md +77 -0
  2. package/package.json +1 -1
  3. package/server.js +10 -5
package/AGENTS.md ADDED
@@ -0,0 +1,77 @@
1
+ # oc-stats (opencode-statboard)
2
+
3
+ Dashboard for opencode token usage and cost statistics. Published as `opencode-statboard` on npm.
4
+
5
+ ## Quick Start
6
+
7
+ ```bash
8
+ npm install -g opencode-statboard
9
+ opencode-dashboard
10
+ # Open http://127.0.0.1:4868
11
+ ```
12
+
13
+ ## Architecture
14
+
15
+ - `server.js` — Node.js HTTP server (zero frameworks, pure `http` module)
16
+ - `public/index.html` — Single-file frontend (vanilla JS/CSS/HTML, no build step)
17
+ - `backups/` — Gzipped DB backups (auto-rotated, `OPENCODE_BACKUP_RETENTION`)
18
+
19
+ ## Key Patterns
20
+
21
+ ### Data Source
22
+
23
+ All data comes from `opencode.db` (read-only via `better-sqlite3` with WAL mode). The DB is opened once and health-checked before each query (`SELECT 1`); if the connection is stale, it re-opens automatically.
24
+
25
+ ### Hybrid Attribution (`attributeUsage`)
26
+
27
+ Token/cost attribution uses a hybrid model:
28
+ 1. **Message-level** — Each assistant message's `tokens` and `cost` from `message.data` JSON are attributed to its `time_created` local day
29
+ 2. **Session residual** — `session.tokens_* - sum(message.tokens_*)` is spread across the session's full local-day span, weighted by per-day message volume
30
+
31
+ This ensures multi-day sessions (e.g. a session created Monday, updated Thursday) don't dump all tokens onto Thursday.
32
+
33
+ ### API Endpoints
34
+
35
+ | Endpoint | Description | Cache TTL |
36
+ |----------|-------------|-----------|
37
+ | `GET /api/stats?days=7` | Overview + trend data | 60s (trend) |
38
+ | `GET /api/report?days=1\|7\|30\|-1` | Usage report (Daily/Weekly/Monthly/All) | 10min |
39
+ | `GET /api/backup` | Manual backup trigger | — |
40
+ | `GET /api/backups` | List backups | — |
41
+ | `GET /api/vacuum` | DB vacuum (skips if freelist <64MB) | — |
42
+ | `GET /api/db-info` | DB file sizes + pragma | — |
43
+
44
+ ### Environment Variables
45
+
46
+ - `OC_PORT` — Listening port (default 4868, auto-fallbacks +5)
47
+ - `OPENCODE_DATA` — Custom path to `opencode.db`
48
+ - `OPENCODE_BACKUP_RETENTION` — Max backup dirs (default 3)
49
+
50
+ ## Verification
51
+
52
+ After any change, run:
53
+ ```bash
54
+ node check-stats-verify.js
55
+ node check-trend-verify.js 7
56
+ node check-report-verify.js
57
+ ```
58
+
59
+ ## Publishing
60
+
61
+ ```bash
62
+ npm set //registry.npmjs.org/:_authToken "$NPM_TOKEN"
63
+ npm version patch --no-git-tag-version
64
+ npm publish
65
+ npm install -g opencode-statboard # auto-install after publish
66
+ npm config delete //registry.npmjs.org/:_authToken
67
+ ```
68
+
69
+ NPM token stored in user env var `NPM_TOKEN` and `oc-stats/.env` (gitignored).
70
+
71
+ ## Known Constraints
72
+
73
+ - `better-sqlite3` is a native addon; must match Node.js ABI
74
+ - `attributeUsage` does a full DB scan (~22k messages); cached 300s via `getAttrAll()`
75
+ - Trend and report share the same `getAttrAll()` cache so daily values are consistent
76
+ - `gzip` compression on all `/api/` responses (>512B, level 1 for speed)
77
+ - Backup is deferred 30s after startup — heavy disk I/O (500MB copy+compress) during startup causes Windows to terminate the process
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -797,10 +797,11 @@ function gracefulShutdown() {
797
797
 
798
798
  process.on('SIGINT', gracefulShutdown);
799
799
  process.on('SIGTERM', gracefulShutdown);
800
- process.on('uncaughtException', (e) => { console.error('uncaught:', e); });
801
- process.on('unhandledRejection', (e) => { console.error('unhandled:', e); });
800
+ process.on('uncaughtException', (e) => { console.error('uncaught:', e.message || e); });
801
+ process.on('unhandledRejection', (e) => { console.error('unhandled:', e && e.message ? e.message : e); });
802
802
 
803
803
  // Keep process alive even if all timers are cleared
804
+ process.title = 'opencode-dashboard';
804
805
  process.stdin.resume();
805
806
 
806
807
  setInterval(() => {
@@ -814,7 +815,7 @@ function tryListen(port, maxPort) {
814
815
  server.once('error', (e) => {
815
816
  if (e.code === 'EADDRINUSE') {
816
817
  console.log(`port ${port} in use, trying ${port + 1}...`);
817
- server.removeAllListeners();
818
+ server.removeAllListeners('error');
818
819
  if (port < (maxPort || port + 5)) {
819
820
  resolve(tryListen(port + 1, maxPort));
820
821
  } else {
@@ -852,8 +853,12 @@ function tryListen(port, maxPort) {
852
853
  if (t) trendCache[String(d)] = t;
853
854
  } catch (_) {}
854
855
  }
855
- await autoBackupIfDue();
856
+ // Defer backup to avoid heavy disk I/O during startup
857
+ // (Windows may kill process for excessive I/O on large DB files).
858
+ setTimeout(() => {
859
+ autoBackupIfDue().catch((_) => {});
860
+ }, 30000);
856
861
  } catch (e) {
857
- console.error('startup error:', e);
862
+ console.error('startup error:', e.message || e);
858
863
  }
859
864
  })();