opencode-statboard 2.9.6 → 2.10.5
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-port-final.js +86 -0
- package/fix-port-final10.js +79 -0
- package/fix-port-final11.js +79 -0
- package/fix-port-final12.js +79 -0
- package/fix-port-final13.js +79 -0
- package/fix-port-final14.js +79 -0
- package/fix-port-final15.js +79 -0
- package/fix-port-final16.js +79 -0
- package/fix-port-final17.js +79 -0
- package/fix-port-final18.js +79 -0
- package/fix-port-final19.js +79 -0
- package/fix-port-final2.js +79 -0
- package/fix-port-final20.js +79 -0
- package/fix-port-final21.js +79 -0
- package/fix-port-final22.js +79 -0
- package/fix-port-final23.js +79 -0
- package/fix-port-final3.js +79 -0
- package/fix-port-final5.js +79 -0
- package/fix-port-final7.js +79 -0
- package/fix-port-final8.js +79 -0
- package/fix-port-final9.js +79 -0
- package/fix-port-v2.js +58 -0
- package/fix-port-v3.js +79 -0
- package/fix-port.js +32 -0
- package/package.json +11 -5
- package/server.js +9 -20
- package/tui-dashboard.js +740 -0
package/tui-dashboard.js
ADDED
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* TUI Dashboard for opencode-statboard
|
|
4
|
+
* Terminal-based dashboard using blessed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const blessed = require('blessed');
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const { spawn } = require('child_process');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const pkg = require('./package.json');
|
|
12
|
+
|
|
13
|
+
const I18N = {
|
|
14
|
+
en: {
|
|
15
|
+
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
|
+
footer: 'Last update: {time} | View: {view} | W:Web R:Refresh Q:Quit',
|
|
18
|
+
loading: 'Loading data...',
|
|
19
|
+
error: 'Error: {error}',
|
|
20
|
+
overview: 'OVERVIEW',
|
|
21
|
+
projects: 'PROJECTS',
|
|
22
|
+
models: 'MODELS',
|
|
23
|
+
trend: 'TREND (90 days)',
|
|
24
|
+
heatmap: 'HEATMAP',
|
|
25
|
+
noData: 'No data',
|
|
26
|
+
sessions: 'Sessions',
|
|
27
|
+
main: 'main',
|
|
28
|
+
total: 'total',
|
|
29
|
+
projectsLabel: 'Projects',
|
|
30
|
+
days: 'Days',
|
|
31
|
+
messages: 'Messages',
|
|
32
|
+
tokens: 'Tokens',
|
|
33
|
+
input: 'Input',
|
|
34
|
+
output: 'Output',
|
|
35
|
+
reasoning: 'Reasoning',
|
|
36
|
+
cacheRead: 'Cache Read',
|
|
37
|
+
cacheWrite: 'Cache Write',
|
|
38
|
+
total: 'Total',
|
|
39
|
+
cost: 'Cost',
|
|
40
|
+
marketValue: 'Market Value',
|
|
41
|
+
saved: 'Saved',
|
|
42
|
+
avgCostDay: 'Avg Cost/Day',
|
|
43
|
+
avgTokensDay: 'Avg Tokens/Day',
|
|
44
|
+
projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
|
|
45
|
+
modelHeader: '{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}',
|
|
46
|
+
unknown: 'unknown',
|
|
47
|
+
today: 'Today',
|
|
48
|
+
version: 'Version'
|
|
49
|
+
},
|
|
50
|
+
zh: {
|
|
51
|
+
title: 'opencode-statboard v{version} TUI',
|
|
52
|
+
keys: '[1]總覽 [2]專案 [3]模型 [4]趨勢 [5]熱力 [W]網頁 [R]重整 [Q]離開 [L]語言',
|
|
53
|
+
footer: '上次更新: {time} | 視圖: {view} | W:網頁 R:重整 Q:離開',
|
|
54
|
+
loading: '載入資料中...',
|
|
55
|
+
error: '錯誤: {error}',
|
|
56
|
+
overview: '總覽',
|
|
57
|
+
projects: '專案',
|
|
58
|
+
models: '模型',
|
|
59
|
+
trend: '趨勢 (90 天)',
|
|
60
|
+
heatmap: '熱力圖',
|
|
61
|
+
noData: '無資料',
|
|
62
|
+
sessions: '會話',
|
|
63
|
+
main: '主',
|
|
64
|
+
total: '總計',
|
|
65
|
+
projectsLabel: '專案數',
|
|
66
|
+
days: '天數',
|
|
67
|
+
messages: '訊息',
|
|
68
|
+
tokens: 'Token',
|
|
69
|
+
input: '輸入',
|
|
70
|
+
output: '輸出',
|
|
71
|
+
reasoning: '推理',
|
|
72
|
+
cacheRead: '快取讀取',
|
|
73
|
+
cacheWrite: '快取寫入',
|
|
74
|
+
total: '總計',
|
|
75
|
+
cost: '成本',
|
|
76
|
+
marketValue: '市場價值',
|
|
77
|
+
saved: '節省',
|
|
78
|
+
avgCostDay: '日均成本',
|
|
79
|
+
avgTokensDay: '日均Token',
|
|
80
|
+
projectHeader: '{專案:<45} {會話:>10} {Token:>18} {成本:>14}',
|
|
81
|
+
modelHeader: '{模型:<40} {供應商:<14} {Token:>18} {成本:>12} {速度:>10}',
|
|
82
|
+
unknown: '未知',
|
|
83
|
+
today: '今天',
|
|
84
|
+
version: '版本',
|
|
85
|
+
sun: '日', mon: '一', tue: '二', wed: '三', thu: '四', fri: '五', sat: '六'
|
|
86
|
+
},
|
|
87
|
+
en: {
|
|
88
|
+
title: 'opencode-statboard v{version} TUI',
|
|
89
|
+
keys: '[1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit [L]Lang',
|
|
90
|
+
footer: 'Last update: {time} | View: {view} | W:Web R:Refresh Q:Quit',
|
|
91
|
+
loading: 'Loading data...',
|
|
92
|
+
error: 'Error: {error}',
|
|
93
|
+
overview: 'OVERVIEW',
|
|
94
|
+
projects: 'PROJECTS',
|
|
95
|
+
models: 'MODELS',
|
|
96
|
+
trend: 'TREND (90 days)',
|
|
97
|
+
heatmap: 'HEATMAP',
|
|
98
|
+
noData: 'No data',
|
|
99
|
+
sessions: 'Sessions',
|
|
100
|
+
main: 'main',
|
|
101
|
+
total: 'total',
|
|
102
|
+
projectsLabel: 'Projects',
|
|
103
|
+
days: 'Days',
|
|
104
|
+
messages: 'Messages',
|
|
105
|
+
tokens: 'Tokens',
|
|
106
|
+
input: 'Input',
|
|
107
|
+
output: 'Output',
|
|
108
|
+
reasoning: 'Reasoning',
|
|
109
|
+
cacheRead: 'Cache Read',
|
|
110
|
+
cacheWrite: 'Cache Write',
|
|
111
|
+
total: 'Total',
|
|
112
|
+
cost: 'Cost',
|
|
113
|
+
marketValue: 'Market Value',
|
|
114
|
+
saved: 'Saved',
|
|
115
|
+
avgCostDay: 'Avg Cost/Day',
|
|
116
|
+
avgTokensDay: 'Avg Tokens/Day',
|
|
117
|
+
projectHeader: '{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}',
|
|
118
|
+
modelHeader: '{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}',
|
|
119
|
+
unknown: 'unknown',
|
|
120
|
+
today: 'Today',
|
|
121
|
+
version: 'Version',
|
|
122
|
+
sun: 'Sun', mon: 'Mon', tue: 'Tue', wed: 'Wed', thu: 'Thu', fri: 'Fri', sat: 'Sat'
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
class TUIServer {
|
|
127
|
+
constructor(options = {}) {
|
|
128
|
+
this.port = options.port || 4868;
|
|
129
|
+
this.host = options.host || '127.0.0.1';
|
|
130
|
+
this.serverProcess = null;
|
|
131
|
+
this.screen = null;
|
|
132
|
+
this.layout = {};
|
|
133
|
+
this.data = {
|
|
134
|
+
overview: null,
|
|
135
|
+
projects: [],
|
|
136
|
+
models: [],
|
|
137
|
+
trend: null,
|
|
138
|
+
loading: true,
|
|
139
|
+
error: null
|
|
140
|
+
};
|
|
141
|
+
this.currentView = 'overview';
|
|
142
|
+
this.views = ['overview', 'projects', 'models', 'trend', 'heatmap'];
|
|
143
|
+
this.pollInterval = null;
|
|
144
|
+
this.lastUpdate = null;
|
|
145
|
+
this._earlyLogs = [];
|
|
146
|
+
this.lang = 'zh'; // Default to Chinese
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async start() {
|
|
150
|
+
this.startServer();
|
|
151
|
+
await this.sleep(1500);
|
|
152
|
+
this.initTUI();
|
|
153
|
+
this.startPolling();
|
|
154
|
+
process.on('SIGINT', () => this.shutdown());
|
|
155
|
+
process.on('SIGTERM', () => this.shutdown());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
startServer() {
|
|
159
|
+
const serverPath = path.join(__dirname, 'server.js');
|
|
160
|
+
this.serverProcess = spawn('node', [serverPath], {
|
|
161
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
162
|
+
env: { ...process.env, OC_PORT: String(this.port) }
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
this.serverProcess.stdout.on('data', (data) => {
|
|
166
|
+
const msg = data.toString().trim();
|
|
167
|
+
if (msg) this.log('[SERVER] ' + msg);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
this.serverProcess.stderr.on('data', (data) => {
|
|
171
|
+
const msg = data.toString().trim();
|
|
172
|
+
if (msg) this.log('[SERVER ERR] ' + msg);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
this.serverProcess.on('close', (code) => {
|
|
176
|
+
this.log('Server exited with code ' + code);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async fetchData(endpoint) {
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
http.get('http://' + this.host + ':' + this.port + endpoint, (res) => {
|
|
183
|
+
let data = '';
|
|
184
|
+
res.on('data', chunk => data += chunk);
|
|
185
|
+
res.on('end', () => {
|
|
186
|
+
try {
|
|
187
|
+
const raw = res.headers['content-type']?.includes('utf-16')
|
|
188
|
+
? Buffer.from(data, 'utf16le').toString('utf8')
|
|
189
|
+
: data;
|
|
190
|
+
resolve(JSON.parse(raw));
|
|
191
|
+
} catch (e) {
|
|
192
|
+
reject(e);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}).on('error', reject);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async pollData() {
|
|
200
|
+
try {
|
|
201
|
+
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')
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
// Parse stats response
|
|
209
|
+
const statsData = stats.data || stats;
|
|
210
|
+
const overview = statsData.overview || {};
|
|
211
|
+
const cost = statsData.cost || {};
|
|
212
|
+
const tools = statsData.tools || [];
|
|
213
|
+
|
|
214
|
+
// Parse report for projects (from agents)
|
|
215
|
+
const reportData = report.report || report;
|
|
216
|
+
const agents = reportData.agents || [];
|
|
217
|
+
const projects = (agents || [])
|
|
218
|
+
.filter(a => a.sessions > 0)
|
|
219
|
+
.map(a => ({
|
|
220
|
+
name: a.agent,
|
|
221
|
+
mainSessionCount: a.sessions,
|
|
222
|
+
sessionCount: a.sessions,
|
|
223
|
+
tokens: { total: (a.tok_in || 0) + (a.tok_out || 0) + (a.tok_reasoning || 0) + (a.cache_read || 0) },
|
|
224
|
+
cost: a.cost || 0
|
|
225
|
+
}));
|
|
226
|
+
|
|
227
|
+
// Parse trend
|
|
228
|
+
const trendData = trend.trend || trend;
|
|
229
|
+
|
|
230
|
+
// Handle overview.sessions being a number or object
|
|
231
|
+
const sessionsValue = overview.sessions;
|
|
232
|
+
const sessionsObj = typeof sessionsValue === 'number'
|
|
233
|
+
? { main: sessionsValue, total: sessionsValue }
|
|
234
|
+
: (sessionsValue || { main: 0, total: 0 });
|
|
235
|
+
// Projects count from agents in report
|
|
236
|
+
const projectsCount = projects.length;
|
|
237
|
+
|
|
238
|
+
this.data = {
|
|
239
|
+
overview: {
|
|
240
|
+
sessions: sessionsObj,
|
|
241
|
+
projects: projectsCount,
|
|
242
|
+
days: overview.days || 0,
|
|
243
|
+
messages: overview.messages || 0
|
|
244
|
+
},
|
|
245
|
+
projects: Array.isArray(projects) ? projects : [],
|
|
246
|
+
models: Array.isArray(models) ? models : [],
|
|
247
|
+
trend: trendData,
|
|
248
|
+
tokens: {
|
|
249
|
+
input: cost.input || 0,
|
|
250
|
+
output: cost.output || 0,
|
|
251
|
+
reasoning: cost.reasoning || 0,
|
|
252
|
+
cacheRead: cost.cacheRead || 0,
|
|
253
|
+
cacheWrite: cost.cacheWrite || 0,
|
|
254
|
+
total: (cost.input || 0) + (cost.output || 0) + (cost.reasoning || 0) + (cost.cacheRead || 0) + (cost.cacheWrite || 0)
|
|
255
|
+
},
|
|
256
|
+
cost: {
|
|
257
|
+
total: cost.total || 0,
|
|
258
|
+
marketValue: cost.marketTotal || 0,
|
|
259
|
+
saved: cost.savedTotal || 0,
|
|
260
|
+
savedPct: cost.proxyShare || 0,
|
|
261
|
+
avgCostPerDay: cost.avgDay || 0,
|
|
262
|
+
avgTokensPerDay: cost.avgTokensDay || 0
|
|
263
|
+
},
|
|
264
|
+
loading: false,
|
|
265
|
+
error: null,
|
|
266
|
+
lastUpdate: new Date()
|
|
267
|
+
};
|
|
268
|
+
this.lastUpdate = new Date();
|
|
269
|
+
this.render();
|
|
270
|
+
} catch (e) {
|
|
271
|
+
this.data.error = e.message;
|
|
272
|
+
this.data.loading = false;
|
|
273
|
+
this.render();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
startPolling() {
|
|
278
|
+
this.pollData();
|
|
279
|
+
this.pollInterval = setInterval(() => this.pollData(), 15000);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
initTUI() {
|
|
283
|
+
this.screen = blessed.screen({
|
|
284
|
+
smartCSR: true,
|
|
285
|
+
title: 'opencode-statboard TUI v' + pkg.version,
|
|
286
|
+
fullUnicode: true,
|
|
287
|
+
autoPadding: false
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
this.screen.key(['q', 'C-c', 'escape'], () => this.shutdown());
|
|
291
|
+
this.screen.key(['1'], () => this.switchView('overview'));
|
|
292
|
+
this.screen.key(['2'], () => this.switchView('projects'));
|
|
293
|
+
this.screen.key(['3'], () => this.switchView('models'));
|
|
294
|
+
this.screen.key(['4'], () => this.switchView('trend'));
|
|
295
|
+
this.screen.key(['5'], () => this.switchView('heatmap'));
|
|
296
|
+
this.screen.key(['tab'], () => this.nextView());
|
|
297
|
+
this.screen.key(['S-tab'], () => this.prevView());
|
|
298
|
+
this.screen.key(['r'], () => this.pollData());
|
|
299
|
+
this.screen.key(['w'], () => this.openWeb());
|
|
300
|
+
this.screen.key(['l'], () => this.toggleLang());
|
|
301
|
+
|
|
302
|
+
this.createLayout();
|
|
303
|
+
this.render();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
toggleLang() {
|
|
307
|
+
this.lang = this.lang === 'zh' ? 'en' : 'zh';
|
|
308
|
+
this.createLayout();
|
|
309
|
+
this.render();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
t(key) {
|
|
313
|
+
return I18N[this.lang][key] || key;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
createLayout() {
|
|
317
|
+
// Remove old layout if exists
|
|
318
|
+
if (this.layout.header) this.screen.remove(this.layout.header);
|
|
319
|
+
if (this.layout.main) this.screen.remove(this.layout.main);
|
|
320
|
+
if (this.layout.footer) this.screen.remove(this.layout.footer);
|
|
321
|
+
|
|
322
|
+
const t = this.t.bind(this);
|
|
323
|
+
|
|
324
|
+
this.layout.header = blessed.box({
|
|
325
|
+
top: 0,
|
|
326
|
+
left: 0,
|
|
327
|
+
width: '100%',
|
|
328
|
+
height: 2,
|
|
329
|
+
tags: true,
|
|
330
|
+
border: { type: 'line' },
|
|
331
|
+
style: { border: { fg: 'cyan' } },
|
|
332
|
+
content: '{center}{bold}' + t('title').replace('{version}', pkg.version) + '{/bold} | ' + t('keys') + ' [L]Lang{/center}'
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
this.layout.main = blessed.box({
|
|
336
|
+
top: 2,
|
|
337
|
+
bottom: 2,
|
|
338
|
+
left: 0,
|
|
339
|
+
width: '100%',
|
|
340
|
+
tags: true,
|
|
341
|
+
border: { type: 'line' },
|
|
342
|
+
style: { border: { fg: 'blue' } },
|
|
343
|
+
scrollable: true,
|
|
344
|
+
alwaysScroll: true,
|
|
345
|
+
keys: true,
|
|
346
|
+
vi: true,
|
|
347
|
+
mouse: true
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
this.layout.footer = blessed.box({
|
|
351
|
+
bottom: 0,
|
|
352
|
+
left: 0,
|
|
353
|
+
width: '100%',
|
|
354
|
+
height: 2,
|
|
355
|
+
tags: true,
|
|
356
|
+
border: { type: 'line' },
|
|
357
|
+
style: { border: { fg: 'green' } },
|
|
358
|
+
content: '{center}' + t('footer').replace('{time}', this.lastUpdate ? this.lastUpdate.toLocaleTimeString() : 'never').replace('{view}', t(this.currentView)) + '{/center}'
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
this.screen.append(this.layout.header);
|
|
362
|
+
this.screen.append(this.layout.main);
|
|
363
|
+
this.screen.append(this.layout.footer);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
switchView(view) {
|
|
367
|
+
if (this.views.includes(view)) {
|
|
368
|
+
this.currentView = view;
|
|
369
|
+
this.selectedIndex = 0;
|
|
370
|
+
this.updateFooter();
|
|
371
|
+
this.render();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
nextView() {
|
|
376
|
+
const idx = this.views.indexOf(this.currentView);
|
|
377
|
+
this.currentView = this.views[(idx + 1) % this.views.length];
|
|
378
|
+
this.selectedIndex = 0;
|
|
379
|
+
this.updateFooter();
|
|
380
|
+
this.render();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
prevView() {
|
|
384
|
+
const idx = this.views.indexOf(this.currentView);
|
|
385
|
+
this.currentView = this.views[(idx - 1 + this.views.length) % this.views.length];
|
|
386
|
+
this.selectedIndex = 0;
|
|
387
|
+
this.updateFooter();
|
|
388
|
+
this.render();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
updateFooter() {
|
|
392
|
+
const t = this.t.bind(this);
|
|
393
|
+
const time = this.lastUpdate ? this.lastUpdate.toLocaleTimeString() : 'never';
|
|
394
|
+
this.layout.footer.setContent(
|
|
395
|
+
'{center}' + t('footer').replace('{time}', time).replace('{view}', t(this.currentView)) + '{/center}'
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
openWeb() {
|
|
400
|
+
const url = 'http://' + this.host + ':' + this.port;
|
|
401
|
+
const opener = process.platform === 'win32' ? 'start' : process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
402
|
+
spawn(opener, [url], { detached: true, stdio: 'ignore' }).unref();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
getContentWidth() {
|
|
406
|
+
const mainWidth = this.layout.main.width;
|
|
407
|
+
return typeof mainWidth === 'number' ? mainWidth - 2 : this.screen.width - 4;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
getContentHeight() {
|
|
411
|
+
const mainHeight = this.layout.main.height;
|
|
412
|
+
if (typeof mainHeight === 'number') return mainHeight - 2;
|
|
413
|
+
return this.screen.height - 6; // header(2) + footer(2) + borders(2)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
render() {
|
|
417
|
+
if (this.data.loading) {
|
|
418
|
+
this.layout.main.setContent('{center}{bold}Loading data...{/bold}{/center}');
|
|
419
|
+
} else if (this.data.error) {
|
|
420
|
+
this.layout.main.setContent('{center}{red-fg}{bold}Error: ' + this.escape(this.data.error) + '{/bold}{/red-fg}{/center}');
|
|
421
|
+
} else {
|
|
422
|
+
this.layout.main.setContent(this.renderCurrentView());
|
|
423
|
+
}
|
|
424
|
+
this.updateFooter();
|
|
425
|
+
this.screen.render();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
renderCurrentView() {
|
|
429
|
+
switch (this.currentView) {
|
|
430
|
+
case 'overview': return this.renderOverview();
|
|
431
|
+
case 'projects': return this.renderProjects();
|
|
432
|
+
case 'models': return this.renderModels();
|
|
433
|
+
case 'trend': return this.renderTrend();
|
|
434
|
+
case 'heatmap': return this.renderHeatmap();
|
|
435
|
+
default: return '{center}Unknown view{/center}';
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
renderOverview() {
|
|
440
|
+
const ov = this.data;
|
|
441
|
+
const t = this.t.bind(this);
|
|
442
|
+
if (!ov || ov.loading) return '{center}{bold}' + t('loading') + '{/bold}{/center}';
|
|
443
|
+
|
|
444
|
+
const tokens = ov.tokens || {};
|
|
445
|
+
const cost = ov.cost || {};
|
|
446
|
+
|
|
447
|
+
const lines = [
|
|
448
|
+
'{center}{bold}\u2501\u2501\u2501 ' + t('overview') + ' \u2501\u2501\u2501{/bold}{/center}',
|
|
449
|
+
'',
|
|
450
|
+
'{bold}' + t('sessions') + ':{/bold} ' + (ov.overview?.sessions?.main || 0) + ' ' + t('main') + ' / ' + (ov.overview?.sessions?.total || 0) + ' ' + t('total'),
|
|
451
|
+
'{bold}' + t('projectsLabel') + ':{/bold} ' + (ov.overview?.projects || 0),
|
|
452
|
+
'{bold}' + t('days') + ':{/bold} ' + (ov.overview?.days || 0),
|
|
453
|
+
'{bold}' + t('messages') + ':{/bold} ' + (ov.overview?.messages || 0),
|
|
454
|
+
'',
|
|
455
|
+
'{bold}' + t('tokens') + ':{/bold}',
|
|
456
|
+
' ' + t('input') + ': ' + this.formatNumber(tokens.input || 0),
|
|
457
|
+
' ' + t('output') + ': ' + this.formatNumber(tokens.output || 0),
|
|
458
|
+
' ' + t('reasoning') + ': ' + this.formatNumber(tokens.reasoning || 0),
|
|
459
|
+
' ' + t('cacheRead') + ': ' + this.formatNumber(tokens.cacheRead || 0),
|
|
460
|
+
' ' + t('cacheWrite') + ': ' + this.formatNumber(tokens.cacheWrite || 0),
|
|
461
|
+
' {bold}' + t('total') + ': ' + this.formatNumber(tokens.total || 0) + '{/bold}',
|
|
462
|
+
'',
|
|
463
|
+
'{bold}' + t('cost') + ':{/bold} ' + this.formatCost(cost.total || 0),
|
|
464
|
+
'{bold}' + t('marketValue') + ':{/bold} ' + this.formatCost(cost.marketValue || 0),
|
|
465
|
+
'{bold}' + t('saved') + ':{/bold} ' + this.formatCost(cost.saved || 0) + (cost.savedPct ? ' (' + cost.savedPct + '%)' : ''),
|
|
466
|
+
'',
|
|
467
|
+
'{bold}' + t('avgCostDay') + ':{/bold} ' + this.formatCost(cost.avgCostPerDay || 0),
|
|
468
|
+
'{bold}' + t('avgTokensDay') + ':{/bold} ' + this.formatNumber(cost.avgTokensPerDay || 0)
|
|
469
|
+
];
|
|
470
|
+
return lines.join('\n');
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
renderProjects() {
|
|
474
|
+
const projects = this.data.projects || [];
|
|
475
|
+
const t = this.t.bind(this);
|
|
476
|
+
if (projects.length === 0) return '{center}' + t('noData') + '{/center}';
|
|
477
|
+
|
|
478
|
+
const lines = ['{center}{bold}\u2501\u2501\u2501 ' + t('projects') + ' \u2501\u2501\u2501{/bold}{/center}', ''];
|
|
479
|
+
lines.push('{bold}' + t('projectHeader') + '{/bold}');
|
|
480
|
+
lines.push('{gray}\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500{/gray}');
|
|
481
|
+
|
|
482
|
+
for (const p of projects.slice(0, 50)) {
|
|
483
|
+
const name = p.name.length > 43 ? p.name.slice(0, 40) + '...' : p.name;
|
|
484
|
+
const tokens = this.formatNumber(p.tokens?.total || 0);
|
|
485
|
+
const cost = this.formatCost(p.cost || 0);
|
|
486
|
+
const sessions = (p.mainSessionCount || 0) + '/' + (p.sessionCount || 0);
|
|
487
|
+
const namePad = name.padEnd(45);
|
|
488
|
+
const sessPad = sessions.padStart(10);
|
|
489
|
+
const tokPad = this.formatNumber(p.tokens?.total || 0).padStart(18);
|
|
490
|
+
const costPad = this.formatCost(p.cost || 0).padStart(14);
|
|
491
|
+
lines.push(namePad + ' ' + sessPad + ' ' + tokPad + ' ' + costPad);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (projects.length > 50) {
|
|
495
|
+
lines.push('');
|
|
496
|
+
lines.push('{gray}... ' + t('noData') + ' ' + (projects.length - 50) + ' ' + t('projects') + '{/gray}');
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return lines.join('\n');
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
renderModels() {
|
|
503
|
+
const models = this.data.models || [];
|
|
504
|
+
const t = this.t.bind(this);
|
|
505
|
+
if (models.length === 0) return '{center}' + t('noData') + '{/center}';
|
|
506
|
+
|
|
507
|
+
const lines = ['{center}{bold}\u2501\u2501\u2501 ' + t('models') + ' \u2501\u2501\u2501{/bold}{/center}', ''];
|
|
508
|
+
lines.push('{bold}' + t('modelHeader') + '{/bold}');
|
|
509
|
+
lines.push('{gray}\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500{/gray}');
|
|
510
|
+
|
|
511
|
+
for (const m of models.slice(0, 30)) {
|
|
512
|
+
const name = m.name.length > 38 ? m.name.slice(0, 35) + '...' : m.name;
|
|
513
|
+
const provider = (m.provider || 'unknown').slice(0, 12);
|
|
514
|
+
const tokens = this.formatNumber(m.tokens?.total || m.tokens || 0);
|
|
515
|
+
const cost = this.formatCost(m.cost || 0);
|
|
516
|
+
const speed = m.speed ? m.speed.toFixed(1) + ' tok/s' : '\u2014';
|
|
517
|
+
const namePad = name.padEnd(40);
|
|
518
|
+
const provPad = provider.padEnd(14);
|
|
519
|
+
const tokPad = tokens.padStart(18);
|
|
520
|
+
const costPad = cost.padStart(12);
|
|
521
|
+
const speedPad = speed.padStart(10);
|
|
522
|
+
lines.push(namePad + ' ' + provPad + ' ' + tokPad + ' ' + costPad + ' ' + speedPad);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return lines.join('\n');
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
renderTrend() {
|
|
529
|
+
const trend = this.data.trend;
|
|
530
|
+
const t = this.t.bind(this);
|
|
531
|
+
if (!trend || !trend.labels || !trend.labels.length) {
|
|
532
|
+
return '{center}' + t('noData') + '{/center}';
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const labels = trend.labels;
|
|
536
|
+
const input = trend.input || [];
|
|
537
|
+
const output = trend.output || [];
|
|
538
|
+
const cost = trend.cost || [];
|
|
539
|
+
|
|
540
|
+
const maxVal = Math.max(...input, ...output, 1);
|
|
541
|
+
const height = this.getContentHeight() - 4;
|
|
542
|
+
const width = this.getContentWidth();
|
|
543
|
+
|
|
544
|
+
let out = '{center}{bold}\u2501\u2501\u2501 ' + t('trend') + ' \u2501\u2501\u2501{/bold}{/center}\n\n';
|
|
545
|
+
out += '{bold}Input (cyan) / Output (green) / Cost (yellow){/bold}\n\n';
|
|
546
|
+
|
|
547
|
+
for (let row = height - 1; row >= 0; row--) {
|
|
548
|
+
let line = '';
|
|
549
|
+
const threshold = (row / height) * maxVal;
|
|
550
|
+
for (let i = 0; i < width; i++) {
|
|
551
|
+
const idx = Math.floor(i * labels.length / width);
|
|
552
|
+
const inVal = input[idx] || 0;
|
|
553
|
+
const outVal = output[idx] || 0;
|
|
554
|
+
if (inVal >= threshold && outVal >= threshold) line += '{magenta-fg}\u2588{/magenta-fg}';
|
|
555
|
+
else if (inVal >= threshold) line += '{cyan-fg}\u2588{/cyan-fg}';
|
|
556
|
+
else if (outVal >= threshold) line += '{green-fg}\u2588{/green-fg}';
|
|
557
|
+
else line += ' ';
|
|
558
|
+
}
|
|
559
|
+
out += line + '\n';
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
let xAxis = '';
|
|
563
|
+
const labelStep = Math.max(1, Math.ceil(width / 20));
|
|
564
|
+
for (let i = 0; i < width; i++) {
|
|
565
|
+
if (i % Math.ceil(width / 20) === 0) {
|
|
566
|
+
const idx = Math.floor(i * labels.length / width);
|
|
567
|
+
xAxis += (labels[idx] || ' ').padEnd(Math.ceil(width / 20), ' ').slice(0, Math.ceil(width / 20));
|
|
568
|
+
} else {
|
|
569
|
+
xAxis += ' ';
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
out += '\n{gray}' + xAxis + '{/gray}\n';
|
|
573
|
+
|
|
574
|
+
const lastIdx = labels.length - 1;
|
|
575
|
+
out += '\n{bold}' + t('today') + ':{/bold} Input: ' + this.formatNumber(input[lastIdx] || 0) + ' | Output: ' + this.formatNumber(output[lastIdx] || 0) + ' | Cost: ' + this.formatCost(cost[lastIdx] || 0);
|
|
576
|
+
|
|
577
|
+
return out;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
renderHeatmap() {
|
|
581
|
+
const trend = this.data.trend;
|
|
582
|
+
const t = this.t.bind(this);
|
|
583
|
+
if (!trend || !trend.dates || !trend.dates.length) {
|
|
584
|
+
return '{center}' + t('noData') + '{/center}';
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const dates = trend.dates;
|
|
588
|
+
const input = trend.input || [];
|
|
589
|
+
const cache = trend.cacheRead || [];
|
|
590
|
+
const vals = dates.map((d, i) => (input[i] || 0) + (cache[i] || 0));
|
|
591
|
+
const max = Math.max(1, ...vals);
|
|
592
|
+
|
|
593
|
+
const firstDate = new Date(dates[0]);
|
|
594
|
+
const lead = firstDate.getDay();
|
|
595
|
+
const cells = Array(lead).fill(null);
|
|
596
|
+
dates.forEach((d, i) => cells.push({ date: d, value: vals[i] }));
|
|
597
|
+
|
|
598
|
+
const weeks = [];
|
|
599
|
+
for (let i = 0; i < cells.length; i += 7) weeks.push(cells.slice(i, i + 7));
|
|
600
|
+
while (weeks[weeks.length - 1]?.length < 7) weeks[weeks.length - 1].push(null);
|
|
601
|
+
|
|
602
|
+
const monthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
|
|
603
|
+
|
|
604
|
+
let out = '{center}{bold}\u2501\u2501\u2501 ' + t('heatmap') + ' \u2501\u2501\u2501{/bold}{/center}\n\n';
|
|
605
|
+
|
|
606
|
+
let monthHeader = ' ';
|
|
607
|
+
let lastMonth = -1;
|
|
608
|
+
for (let w = 0; w < weeks.length; w++) {
|
|
609
|
+
const firstCell = weeks[w].find(c => c);
|
|
610
|
+
if (firstCell) {
|
|
611
|
+
const m = new Date(firstCell.date).getMonth();
|
|
612
|
+
if (m !== lastMonth) {
|
|
613
|
+
monthHeader += monthNames[m].padStart(7, ' ');
|
|
614
|
+
lastMonth = m;
|
|
615
|
+
} else {
|
|
616
|
+
monthHeader += ' ';
|
|
617
|
+
}
|
|
618
|
+
} else {
|
|
619
|
+
monthHeader += ' ';
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
out = '{gray}' + monthHeader + '{/gray}\n';
|
|
623
|
+
|
|
624
|
+
const dow = [t('sun') || 'Sun', t('mon') || 'Mon', t('tue') || 'Tue', t('wed') || 'Wed', t('thu') || 'Thu', t('fri') || 'Fri', t('sat') || 'Sat'];
|
|
625
|
+
for (let d = 0; d < 7; d++) {
|
|
626
|
+
let line = '{bold}' + dow[d].padEnd(4) + '{/bold} ';
|
|
627
|
+
for (let w = 0; w < weeks.length; w++) {
|
|
628
|
+
const cell = weeks[w][d];
|
|
629
|
+
if (!cell) {
|
|
630
|
+
line += ' \u00B7 ';
|
|
631
|
+
} else {
|
|
632
|
+
const level = this.getHeatLevel(cell.value, max);
|
|
633
|
+
const color = this.getHeatColor(level);
|
|
634
|
+
const char = level === 0 ? '\u00B7' : level <= 4 ? '\u2591' : level <= 8 ? '\u2592' : level <= 12 ? '\u2593' : '\u2588';
|
|
635
|
+
line += '{' + color + '-fg}' + char + ' {/}';
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
out += line + '\n';
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
out += '\n{gray}Less \u2591\u2591\u2592\u2592\u2593\u2593\u2588\u2588 More (max: ' + this.formatNumber(max) + '){/gray}';
|
|
642
|
+
return out;
|
|
643
|
+
return out;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
getHeatLevel(value, max) {
|
|
647
|
+
if (!value || value <= 0) return 0;
|
|
648
|
+
const r = value / max;
|
|
649
|
+
if (r >= 0.95) return 20;
|
|
650
|
+
if (r >= 0.90) return 19;
|
|
651
|
+
if (r >= 0.85) return 18;
|
|
652
|
+
if (r >= 0.80) return 17;
|
|
653
|
+
if (r >= 0.75) return 16;
|
|
654
|
+
if (r >= 0.70) return 15;
|
|
655
|
+
if (r >= 0.65) return 14;
|
|
656
|
+
if (r >= 0.60) return 13;
|
|
657
|
+
if (r >= 0.55) return 12;
|
|
658
|
+
if (r >= 0.50) return 11;
|
|
659
|
+
if (r >= 0.45) return 10;
|
|
660
|
+
if (r >= 0.40) return 9;
|
|
661
|
+
if (r >= 0.35) return 9;
|
|
662
|
+
if (r >= 0.30) return 8;
|
|
663
|
+
if (r >= 0.25) return 7;
|
|
664
|
+
if (r >= 0.20) return 6;
|
|
665
|
+
if (r >= 0.15) return 5;
|
|
666
|
+
if (r >= 0.10) return 4;
|
|
667
|
+
if (r >= 0.05) return 3;
|
|
668
|
+
if (r >= 0.02) return 2;
|
|
669
|
+
return 1;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
getHeatColor(level) {
|
|
673
|
+
if (level === 0) return 'gray';
|
|
674
|
+
if (level <= 4) return 'blue';
|
|
675
|
+
if (level <= 8) return 'cyan';
|
|
676
|
+
if (level <= 12) return 'green';
|
|
677
|
+
if (level <= 16) return 'yellow';
|
|
678
|
+
return 'red';
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
formatNumber(n) {
|
|
682
|
+
if (n >= 1e12) return (n / 1e12).toFixed(2) + 'T';
|
|
683
|
+
if (n >= 1e9) return (n / 1e9).toFixed(2) + 'B';
|
|
684
|
+
if (n >= 1e6) return (n / 1e6).toFixed(2) + 'M';
|
|
685
|
+
if (n >= 1e3) return (n / 1e3).toFixed(2) + 'K';
|
|
686
|
+
return String(n);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
formatCost(c) {
|
|
690
|
+
if (c == null) return '\u2014';
|
|
691
|
+
if (c >= 1e6) return '$' + (c / 1e6).toFixed(2) + 'M';
|
|
692
|
+
if (c >= 1e3) return '$' + (c / 1e3).toFixed(2) + 'K';
|
|
693
|
+
return '$' + c.toFixed(2);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
escape(str) {
|
|
697
|
+
return String(str).replace(/[{}]/g, '');
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
log(msg) {
|
|
701
|
+
const time = new Date().toLocaleTimeString();
|
|
702
|
+
console.log('[' + time + '] ' + this.escape(msg));
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
getContentWidth() {
|
|
706
|
+
const mainWidth = this.layout.main.width;
|
|
707
|
+
return typeof mainWidth === 'number' ? mainWidth - 2 : this.screen.width - 4;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
sleep(ms) {
|
|
711
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
shutdown() {
|
|
715
|
+
if (this.pollInterval) clearInterval(this.pollInterval);
|
|
716
|
+
if (this.serverProcess) this.serverProcess.kill();
|
|
717
|
+
this.screen.destroy();
|
|
718
|
+
process.exit(0);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
if (require.main === module) {
|
|
723
|
+
const args = process.argv.slice(2);
|
|
724
|
+
const port = parseInt(process.env.OC_PORT || '4868', 10);
|
|
725
|
+
|
|
726
|
+
const webOnly = args.includes('--web') || args.includes('-w');
|
|
727
|
+
const bothMode = args.includes('--both') || args.includes('-b');
|
|
728
|
+
|
|
729
|
+
if (webOnly) {
|
|
730
|
+
require('./server.js');
|
|
731
|
+
} else if (bothMode) {
|
|
732
|
+
const tui = new TUIServer({ port });
|
|
733
|
+
tui.start();
|
|
734
|
+
} else {
|
|
735
|
+
const tui = new TUIServer({ port });
|
|
736
|
+
tui.start();
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
module.exports = { TUIServer };
|