opencode-statboard 2.10.13 → 2.10.15

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/fix-i18n.js ADDED
@@ -0,0 +1,161 @@
1
+ const fs = require('fs');
2
+ const c = fs.readFileSync('tui-dashboard.js', 'utf8');
3
+
4
+ const zhOld = ` zh: {
5
+ title: 'opencode-statboard v{version} TUI',
6
+ keys: '[1]總覽 [2]專案 [3]模型 [4]趨勢 [5]熱力 [W]網頁 [R]重整 [Q]離開 [L]語言',
7
+ footer: '上次更新: {time} | 視圖: {view} | W:網頁 R:重整 Q:離開',
8
+ loading: '載入資料中...',
9
+ error: '錯誤: {error}',
10
+ overview: '總覽',
11
+ projects: '專案',
12
+ models: '模型',
13
+ trend: '趨勢 (90 天)',
14
+ heatmap: '熱力圖',
15
+ noData: '無資料',
16
+ sessions: '會話',
17
+ main: '主',
18
+ total: '總計',
19
+ projectsLabel: '專案數',
20
+ days: '天數',
21
+ messages: '訊息',
22
+ tokens: 'Token',
23
+ input: '輸入',
24
+ output: '輸出',
25
+ reasoning: '推理',
26
+ cacheRead: '快取讀取',
27
+ cacheWrite: '快取寫入',
28
+ total: '總計',
29
+ cost: '成本',
30
+ marketValue: '市場價值',
31
+ saved: '節省',
32
+ avgCostDay: '日均成本',
33
+ avgTokensDay: '日均Token',
34
+ projectHeader: '{專案:<45} {會話:>10} {Token:>18} {成本:>14}',
35
+ modelHeader: '{模型:<40} {供應商:<14} {Token:>18} {成本:>12} {速度:>10}',
36
+ unknown: '未知',
37
+ today: '今天',
38
+ version: '版本',
39
+ sun: '日', mon: '一', tue: '二', wed: '三', thu: '四', fri: '五', sat: '六'
40
+ }`;
41
+
42
+ const zhNew = ` zh: {
43
+ title: 'opencode-statboard v{version} TUI',
44
+ keys: '[1]總覽 [2]專案 [3]模型 [4]趨勢 [5]熱力 [W]網頁 [R]重整 [Q]離開 [L]語言',
45
+ footer: '上次更新: {time} | 視圖: {view} | W:網頁 R:重整 Q:離開',
46
+ loading: '載入資料中...',
47
+ error: '錯誤: {error}',
48
+ overview: '總覽',
49
+ projects: '專案',
50
+ models: '模型',
51
+ trend: '趨勢 (90 天)',
52
+ heatmap: '熱力圖',
53
+ noData: '無資料',
54
+ sessions: '會話',
55
+ main: '主',
56
+ total: '總計',
57
+ projectsLabel: '專案數',
58
+ days: '天數',
59
+ messages: '訊息',
60
+ tokens: 'Token',
61
+ input: '輸入',
62
+ output: '輸出',
63
+ reasoning: '推理',
64
+ cacheRead: '快取讀取',
65
+ cacheWrite: '快取寫入',
66
+ total: '總計',
67
+ cost: '成本',
68
+ marketValue: '市場價值',
69
+ saved: '節省',
70
+ avgCostDay: '日均成本',
71
+ avgTokensDay: '日均Token',
72
+ projectHeader: '{專案:<45} {會話:>10} {Token:>18} {成本:>14}',
73
+ modelHeader: '{模型:<40} {供應商:<14} {Token:>18} {成本:>12} {速度:>10}',
74
+ unknown: '未知',
75
+ today: '今天',
76
+ version: '版本',
77
+ sun: '日', mon: '一', tue: '二', wed: '三', thu: '四', fri: '五', sat: '六',
78
+ day: '日', week: '周', month: '月', quarter: '季', all: '全',
79
+ range: '範圍'
80
+ }`;
81
+
82
+ const enOld = ` en: {
83
+ title: 'opencode-statboard v{version} TUI',
84
+ keys: '[1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit',
85
+ footer: 'Last update: {time} | View: {view} | W:Web R:Refresh Q:Quit',
86
+ loading: 'Loading data...',
87
+ error: 'Error: {error}',
88
+ overview: 'OVERVIEW',
89
+ projects: 'PROJECTS',
90
+ models: 'MODELS',
91
+ trend: 'TREND (90 days)',
92
+ heatmap: 'HEATMAP',
93
+ noData: 'No data',
94
+ sessions: 'Sessions',
95
+ main: 'main',
96
+ total: 'total',
97
+ projectsLabel: 'Projects',
98
+ days: 'Days',
99
+ messages: 'Messages',
100
+ tokens: 'Tokens',
101
+ input: 'Input',
102
+ output: 'Output',
103
+ reasoning: 'Reasoning',
104
+ cacheRead: 'Cache Read',
105
+ cacheWrite: 'Cache Write',
106
+ total: 'Total',
107
+ cost: 'Cost',
108
+ marketValue: 'Market Value',
109
+ saved: 'Saved',
110
+ avgCostDay: 'Avg Cost/Day',
111
+ avgTokensDay: 'Avg Tokens/Day',
112
+ projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
113
+ modelHeader: '{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}',
114
+ unknown: 'unknown',
115
+ today: 'Today',
116
+ version: 'Version'
117
+ }`;
118
+
119
+ const enNew = ` en: {
120
+ title: 'opencode-statboard v{version} TUI',
121
+ keys: '[1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit [L]Lang',
122
+ footer: 'Last update: {time} | View: {view} | W:Web R:Refresh Q:Quit',
123
+ loading: 'Loading data...',
124
+ error: 'Error: {error}',
125
+ overview: 'OVERVIEW',
126
+ projects: 'PROJECTS',
127
+ models: 'MODELS',
128
+ trend: 'TREND (90 days)',
129
+ heatmap: 'HEATMAP',
130
+ noData: 'No data',
131
+ sessions: 'Sessions',
132
+ main: 'main',
133
+ total: 'total',
134
+ projectsLabel: 'Projects',
135
+ days: 'Days',
136
+ messages: 'Messages',
137
+ tokens: 'Tokens',
138
+ input: 'Input',
139
+ output: 'Output',
140
+ reasoning: 'Reasoning',
141
+ cacheRead: 'Cache Read',
142
+ cacheWrite: 'Cache Write',
143
+ total: 'Total',
144
+ cost: 'Cost',
145
+ marketValue: 'Market Value',
146
+ saved: 'Saved',
147
+ avgCostDay: 'Avg Cost/Day',
148
+ avgTokensDay: 'Avg Tokens/Day',
149
+ projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
150
+ modelHeader: '{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}',
151
+ unknown: 'unknown',
152
+ today: 'Today',
153
+ version: 'Version',
154
+ day: 'Day', week: 'Week', month: 'Month', quarter: 'Quarter', all: 'All',
155
+ range: 'Range'
156
+ }`;
157
+
158
+ let newContent = c.replace(zhOld, zhNew).replace(enOld, enNew);
159
+
160
+ fs.writeFileSync('tui-dashboard.js', newContent);
161
+ console.log('Done');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.10.13",
3
+ "version": "2.10.15",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/test-open.js ADDED
@@ -0,0 +1,4 @@
1
+ const { spawn } = require('child_process');
2
+ const url = 'http://127.0.0.1:4868';
3
+ spawn('cmd', ['/c', 'start', '""', url], { detached: true, stdio: 'ignore' }).unref();
4
+ console.log('Opened');
package/tui-dashboard.js CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  const blessed = require('blessed');
8
8
  const http = require('http');
9
+ const zlib = require('zlib');
9
10
  const { spawn } = require('child_process');
10
11
  const path = require('path');
11
12
  const pkg = require('./package.json');
@@ -13,7 +14,7 @@ const pkg = require('./package.json');
13
14
  const I18N = {
14
15
  en: {
15
16
  title: 'opencode-statboard v{version} TUI',
16
- keys: '[1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit',
17
+ keys: '[1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit [L]Lang',
17
18
  footer: 'Last update: {time} | View: {view} | W:Web R:Refresh Q:Quit',
18
19
  loading: 'Loading data...',
19
20
  error: 'Error: {error}',
@@ -45,7 +46,9 @@ const I18N = {
45
46
  modelHeader: '{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}',
46
47
  unknown: 'unknown',
47
48
  today: 'Today',
48
- version: 'Version'
49
+ version: 'Version',
50
+ day: 'Day', week: 'Week', month: 'Month', quarter: 'Quarter', all: 'All',
51
+ range: 'Range'
49
52
  },
50
53
  zh: {
51
54
  title: 'opencode-statboard v{version} TUI',
@@ -82,7 +85,9 @@ const I18N = {
82
85
  unknown: '未知',
83
86
  today: '今天',
84
87
  version: '版本',
85
- sun: '日', mon: '一', tue: '二', wed: '三', thu: '四', fri: '五', sat: '六'
88
+ sun: '日', mon: '一', tue: '二', wed: '三', thu: '四', fri: '五', sat: '六',
89
+ day: '日', week: '周', month: '月', quarter: '季', all: '全',
90
+ range: '範圍'
86
91
  },
87
92
  en: {
88
93
  title: 'opencode-statboard v{version} TUI',
@@ -144,11 +149,28 @@ class TUIServer {
144
149
  this.lastUpdate = null;
145
150
  this._earlyLogs = [];
146
151
  this.lang = 'zh'; // Default to Chinese
152
+ this.timeRange = 7; // Default to 7 days (week)
153
+ this.timeRanges = [
154
+ { key: 'd', days: 1, label: { en: 'Day', zh: '日' } },
155
+ { key: 'w', days: 7, label: { en: 'Week', zh: '周' } },
156
+ { key: 'm', days: 30, label: { en: 'Month', zh: '月' } },
157
+ { key: 'q', days: 90, label: { en: 'Quarter', zh: '季' } },
158
+ { key: 'a', days: -1, label: { en: 'All', zh: '全' } }
159
+ ];
147
160
  }
148
161
 
149
162
  async start() {
150
163
  this.startServer();
151
- await this.sleep(1500);
164
+ await this.sleep(3000);
165
+ // Health check: wait for server to be ready
166
+ for (let i = 0; i < 10; i++) {
167
+ try {
168
+ await this.fetchData('/api/stats?days=7');
169
+ break;
170
+ } catch {
171
+ await this.sleep(500);
172
+ }
173
+ }
152
174
  this.initTUI();
153
175
  this.startPolling();
154
176
  process.on('SIGINT', () => this.shutdown());
@@ -179,10 +201,15 @@ class TUIServer {
179
201
 
180
202
  async fetchData(endpoint) {
181
203
  return new Promise((resolve, reject) => {
182
- http.get('http://' + this.host + ':' + this.port + endpoint, (res) => {
204
+ const req = http.get('http://' + this.host + ':' + this.port + endpoint, (res) => {
205
+ const encoding = res.headers['content-encoding'];
206
+ let stream = res;
207
+ if (encoding && encoding.includes('gzip')) {
208
+ stream = res.pipe(zlib.createGunzip());
209
+ }
183
210
  let data = '';
184
- res.on('data', chunk => data += chunk);
185
- res.on('end', () => {
211
+ stream.on('data', chunk => data += chunk);
212
+ stream.on('end', () => {
186
213
  try {
187
214
  const raw = res.headers['content-type']?.includes('utf-16')
188
215
  ? Buffer.from(data, 'utf16le').toString('utf8')
@@ -192,17 +219,22 @@ class TUIServer {
192
219
  reject(e);
193
220
  }
194
221
  });
195
- }).on('error', reject);
222
+ stream.on('error', reject);
223
+ });
224
+ req.on('error', reject);
225
+ req.setTimeout(10000, () => req.destroy(new Error('Request timeout')));
196
226
  });
197
227
  }
198
228
 
199
229
  async pollData() {
200
230
  try {
231
+ const days = this.timeRange;
232
+ const trendDays = days === -1 ? 90 : days; // trend uses 90 for "all"
201
233
  const [stats, report, models, trend] = await Promise.all([
202
- this.fetchData('/api/stats?days=7'),
203
- this.fetchData('/api/report?days=7'),
204
- this.fetchData('/api/stats?days=7').then(r => r.models || []),
205
- this.fetchData('/api/stats?days=90')
234
+ this.fetchData('/api/stats?days=' + days),
235
+ this.fetchData('/api/report?days=' + days),
236
+ this.fetchData('/api/stats?days=' + days).then(r => r.models || []),
237
+ this.fetchData('/api/stats?days=' + trendDays)
206
238
  ]);
207
239
 
208
240
  // Parse stats response
@@ -299,6 +331,11 @@ class TUIServer {
299
331
  this.screen.key(['w'], () => this.openWeb());
300
332
  this.screen.key(['l'], () => this.toggleLang());
301
333
 
334
+ // Time range keys
335
+ this.timeRanges.forEach(tr => {
336
+ this.screen.key([tr.key], () => this.setTimeRange(tr.days));
337
+ });
338
+
302
339
  this.createLayout();
303
340
  this.render();
304
341
  }
@@ -309,6 +346,18 @@ class TUIServer {
309
346
  this.render();
310
347
  }
311
348
 
349
+ setTimeRange(days) {
350
+ this.timeRange = days;
351
+ this.pollData();
352
+ this.createLayout();
353
+ this.render();
354
+ }
355
+
356
+ getTimeRangeLabel() {
357
+ const tr = this.timeRanges.find(r => r.days === this.timeRange);
358
+ return tr ? tr.label[this.lang] : String(this.timeRange);
359
+ }
360
+
312
361
  t(key) {
313
362
  return I18N[this.lang][key] || key;
314
363
  }
@@ -329,7 +378,7 @@ class TUIServer {
329
378
  tags: true,
330
379
  border: { type: 'line' },
331
380
  style: { border: { fg: 'cyan' } },
332
- content: '{center}{bold}' + t('title').replace('{version}', pkg.version) + '{/bold} | ' + t('keys') + ' [L]Lang{/center}'
381
+ content: '{center}{bold}' + t('title').replace('{version}', pkg.version) + '{/bold} | ' + t('keys') + ' [L]Lang | ' + t('range') + ': ' + this.getTimeRangeLabel() + '{/center}'
333
382
  });
334
383
 
335
384
  this.layout.main = blessed.box({
@@ -524,7 +573,7 @@ class TUIServer {
524
573
 
525
574
  for (const m of models.slice(0, 30)) {
526
575
  const name = m.name.length > 38 ? m.name.slice(0, 35) + '...' : m.name;
527
- const provider = (m.provider || 'unknown').slice(0, 12);
576
+ const provider = (m.provider || (m.name.includes('/') ? m.name.split('/')[0] : 'unknown')).slice(0, 12);
528
577
  const tokens = this.formatNumber(m.tokens?.total || m.tokens || 0);
529
578
  const cost = this.formatCost(m.cost || 0);
530
579
  const speed = m.speed ? m.speed.toFixed(1) + ' tok/s' : '\u2014';