@leo000001/opencode-quota-sidebar 4.0.9 → 4.0.12

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/dist/cli.d.ts CHANGED
@@ -12,6 +12,37 @@ type CliServerCommand = {
12
12
  shell?: boolean;
13
13
  };
14
14
  type SpawnedCliServerProcess = ReturnType<typeof spawn>;
15
+ export declare function releaseCliServerProcess(proc: {
16
+ stdin?: {
17
+ destroy: () => void;
18
+ } | null;
19
+ stdout?: {
20
+ destroy: () => void;
21
+ } | null;
22
+ stderr?: {
23
+ destroy: () => void;
24
+ } | null;
25
+ unref: () => void;
26
+ }): void;
27
+ export declare function terminateCliServerProcess(proc: {
28
+ pid?: number;
29
+ killed?: boolean;
30
+ kill: (signal?: NodeJS.Signals) => boolean;
31
+ stdin?: {
32
+ destroy: () => void;
33
+ } | null;
34
+ stdout?: {
35
+ destroy: () => void;
36
+ } | null;
37
+ stderr?: {
38
+ destroy: () => void;
39
+ } | null;
40
+ unref: () => void;
41
+ }, options?: {
42
+ platform?: NodeJS.Platform;
43
+ killProcess?: typeof process.kill;
44
+ }): void;
45
+ export declare function extractCliServerUrl(output: string): string | undefined;
15
46
  export declare function parseCliArgs(argv: string[]): CliCommand;
16
47
  export declare function cliBaseUrl(): string;
17
48
  export declare function cliServerCommandCandidates(platform?: NodeJS.Platform): CliServerCommand[];
package/dist/cli.js CHANGED
@@ -13,8 +13,68 @@ import { authFilePath, loadConfig, loadState,
13
13
  quotaConfigPaths, resolveOpencodeDataDir, stateFilePath, } from './storage.js';
14
14
  import { filterHistoryProvidersForDisplay, filterUsageProvidersForDisplay, listCurrentProviderIDs, } from './provider_catalog.js';
15
15
  import { createUsageService } from './usage_service.js';
16
+ function emptyProviderUsage(usage) {
17
+ return {
18
+ ...usage,
19
+ providers: {},
20
+ };
21
+ }
22
+ function strictFilterUsageProviders(usage, allowedProviderIDs) {
23
+ if (allowedProviderIDs.size === 0)
24
+ return emptyProviderUsage(usage);
25
+ return filterUsageProvidersForDisplay(usage, allowedProviderIDs);
26
+ }
27
+ function strictFilterHistoryProviders(history, allowedProviderIDs) {
28
+ if (allowedProviderIDs.size === 0) {
29
+ return {
30
+ ...history,
31
+ rows: history.rows.map((row) => ({
32
+ ...row,
33
+ usage: emptyProviderUsage(row.usage),
34
+ })),
35
+ total: emptyProviderUsage(history.total),
36
+ };
37
+ }
38
+ return filterHistoryProvidersForDisplay(history, allowedProviderIDs);
39
+ }
16
40
  const DEFAULT_OPENCODE_BASE_URL = 'http://localhost:4096';
17
41
  const CLI_SERVER_TIMEOUT_MS = 10_000;
42
+ const CLI_FORCE_EXIT_DELAY_MS = 100;
43
+ export function releaseCliServerProcess(proc) {
44
+ // The CLI only needs the child pipes until the server prints its listen URL.
45
+ // After that, unref/destroy them so the parent process can exit cleanly.
46
+ proc.stdin?.destroy();
47
+ proc.stdout?.destroy();
48
+ proc.stderr?.destroy();
49
+ proc.unref();
50
+ }
51
+ export function terminateCliServerProcess(proc, options) {
52
+ releaseCliServerProcess(proc);
53
+ if (proc.killed)
54
+ return;
55
+ const platform = options?.platform ?? process.platform;
56
+ const killProcess = options?.killProcess ?? process.kill;
57
+ if (platform !== 'win32' && typeof proc.pid === 'number' && proc.pid > 0) {
58
+ try {
59
+ killProcess(-proc.pid, 'SIGTERM');
60
+ return;
61
+ }
62
+ catch {
63
+ // Fall back to the direct child pid when group termination is unavailable.
64
+ }
65
+ }
66
+ proc.kill('SIGTERM');
67
+ }
68
+ export function extractCliServerUrl(output) {
69
+ for (const line of output.split('\n')) {
70
+ if (!line.startsWith('opencode server listening'))
71
+ continue;
72
+ const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
73
+ if (match)
74
+ return match[1];
75
+ }
76
+ return undefined;
77
+ }
18
78
  const HELP_TEXT = `opencode-quota
19
79
 
20
80
  Usage:
@@ -172,6 +232,7 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
172
232
  try {
173
233
  proc = spawnProcess(candidate.command, candidate.args, {
174
234
  env: process.env,
235
+ stdio: ['pipe', 'pipe', 'pipe'],
175
236
  shell: candidate.shell ?? false,
176
237
  detached: true,
177
238
  windowsHide: true,
@@ -189,6 +250,8 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
189
250
  let inspect;
190
251
  let onError;
191
252
  let onExit;
253
+ let output = '';
254
+ let settled = false;
192
255
  const id = setTimeout(() => {
193
256
  if (settled)
194
257
  return;
@@ -197,54 +260,47 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
197
260
  closeProcess(proc);
198
261
  reject(new Error(`Timeout waiting for OpenCode server to start after ${CLI_SERVER_TIMEOUT_MS}ms`));
199
262
  }, CLI_SERVER_TIMEOUT_MS);
200
- let output = '';
201
- let settled = false;
263
+ id.unref?.();
264
+ const fail = (failure) => {
265
+ if (settled)
266
+ return;
267
+ settled = true;
268
+ clearTimeout(id);
269
+ releaseCliServerPipes(proc, inspect, onError, onExit);
270
+ reject(failure);
271
+ };
202
272
  inspect = (chunk) => {
203
273
  output += chunk.toString();
204
- const lines = output.split('\n');
205
- for (const line of lines) {
206
- if (!line.startsWith('opencode server listening'))
207
- continue;
208
- const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
209
- if (!match)
210
- continue;
274
+ const url = extractCliServerUrl(output);
275
+ if (url) {
211
276
  clearTimeout(id);
212
277
  settled = true;
213
278
  releaseCliServerPipes(proc, inspect, onError, onExit);
214
279
  // The CLI only needs the startup line; after that the detached server
215
280
  // must not keep the parent process alive.
281
+ proc.stdin?.destroy();
216
282
  proc.unref();
217
- resolve(match[1]);
283
+ resolve(url);
218
284
  return;
219
285
  }
220
286
  };
221
287
  proc.stdout?.on('data', inspect);
222
288
  proc.stderr?.on('data', inspect);
223
289
  onError = (error) => {
224
- if (settled)
225
- return;
226
- settled = true;
227
- clearTimeout(id);
228
- releaseCliServerPipes(proc, inspect, onError, onExit);
229
290
  const code = error.code;
230
- reject({
291
+ fail({
231
292
  error,
232
293
  output,
233
294
  recoverable: code === 'ENOENT' || code === 'EINVAL',
234
295
  });
235
296
  };
236
297
  onExit = (code) => {
237
- if (settled)
238
- return;
239
- settled = true;
240
- clearTimeout(id);
241
- releaseCliServerPipes(proc, inspect, onError, onExit);
242
298
  let message = `OpenCode server exited with code ${code}`;
243
299
  if (output.trim())
244
300
  message += `\n${output}`;
245
301
  const recoverable = /not recognized as an internal or external command/i.test(output) ||
246
302
  /command not found/i.test(output);
247
- reject({ error: new Error(message), output, recoverable });
303
+ fail({ error: new Error(message), output, recoverable });
248
304
  };
249
305
  proc.on('error', onError);
250
306
  proc.on('exit', onExit);
@@ -305,23 +361,40 @@ async function resolvePathInfo(directory) {
305
361
  throw new Error(`Failed to connect to OpenCode API at ${cliBaseUrl()}: ${error instanceof Error ? error.message : String(error)}`);
306
362
  }
307
363
  const server = await startCliOpencodeServer();
308
- const client = createOpencodeClient({
309
- directory,
310
- baseUrl: server.url,
311
- });
312
- const response = await client.path.get({
313
- query: { directory },
314
- throwOnError: true,
315
- });
316
- const data = response.data;
317
- return {
318
- client,
319
- worktree: data.worktree || directory,
320
- directory: data.directory || directory,
321
- close: () => server.close(),
322
- };
364
+ try {
365
+ const client = createOpencodeClient({
366
+ directory,
367
+ baseUrl: server.url,
368
+ });
369
+ const response = await client.path.get({
370
+ query: { directory },
371
+ throwOnError: true,
372
+ });
373
+ const data = response.data;
374
+ return {
375
+ client,
376
+ worktree: data.worktree || directory,
377
+ directory: data.directory || directory,
378
+ close: () => server.close(),
379
+ };
380
+ }
381
+ catch (innerError) {
382
+ server.close();
383
+ throw innerError;
384
+ }
323
385
  }
324
386
  }
387
+ function writeCliLine(stream, value) {
388
+ return new Promise((resolve, reject) => {
389
+ stream.write(`${value}\n`, (error) => {
390
+ if (error) {
391
+ reject(error);
392
+ return;
393
+ }
394
+ resolve();
395
+ });
396
+ });
397
+ }
325
398
  export async function runCli(argv) {
326
399
  const command = parseCliArgs(argv);
327
400
  const cwd = process.cwd();
@@ -348,6 +421,7 @@ export async function runCli(argv) {
348
421
  statePath,
349
422
  client: client,
350
423
  directory,
424
+ worktree,
351
425
  persistence: {
352
426
  markDirty: () => { },
353
427
  scheduleSave: () => { },
@@ -357,17 +431,15 @@ export async function runCli(argv) {
357
431
  listDescendantSessionIDs: async () => [],
358
432
  },
359
433
  });
360
- const quotas = await quotaService.getQuotaSnapshots([], {
361
- allowDefault: true,
362
- });
363
434
  const allowedProviderIDs = await listCurrentProviderIDs({
364
435
  client,
365
436
  directory,
366
- }).catch(() => new Set());
437
+ });
438
+ const quotas = await quotaService.getQuotaSnapshots([...allowedProviderIDs]);
367
439
  if (command.since || command.last !== undefined) {
368
440
  const resolvedSince = command.since || sinceFromLast(command.period, command.last);
369
441
  const historyRaw = await usageService.summarizeHistoryUsage(command.period, resolvedSince);
370
- const history = filterHistoryProvidersForDisplay(historyRaw, allowedProviderIDs);
442
+ const history = strictFilterHistoryProviders(historyRaw, allowedProviderIDs);
371
443
  return renderCliHistoryDashboard({
372
444
  result: history,
373
445
  quotas,
@@ -376,7 +448,7 @@ export async function runCli(argv) {
376
448
  });
377
449
  }
378
450
  const usageRaw = await usageService.summarizeForTool(command.period, '', false);
379
- const usage = filterUsageProvidersForDisplay(usageRaw, allowedProviderIDs);
451
+ const usage = strictFilterUsageProviders(usageRaw, allowedProviderIDs);
380
452
  return renderCliDashboard({
381
453
  label: cliCurrentLabel(command.period),
382
454
  usage,
@@ -406,16 +478,21 @@ export function cliShouldRunMain(argv1 = process.argv[1], modulePath = fileURLTo
406
478
  return resolvePath(modulePath) === resolvePath(argv1);
407
479
  }
408
480
  async function main() {
481
+ let exitCode = 0;
409
482
  try {
410
483
  const output = await runCli(process.argv.slice(2));
411
- process.stdout.write(`${output}\n`);
484
+ await writeCliLine(process.stdout, output);
412
485
  }
413
486
  catch (error) {
414
487
  const message = error instanceof Error ? error.message : String(error);
415
- const exitCode = cliExitCodeForError(message);
488
+ exitCode = cliExitCodeForError(message);
416
489
  const stream = exitCode === 0 ? process.stdout : process.stderr;
417
- stream.write(`${message}\n`);
490
+ await writeCliLine(stream, message);
491
+ }
492
+ finally {
418
493
  process.exitCode = exitCode;
494
+ const forceExit = setTimeout(() => process.exit(exitCode), CLI_FORCE_EXIT_DELAY_MS);
495
+ forceExit.unref?.();
419
496
  }
420
497
  }
421
498
  if (cliShouldRunMain()) {
@@ -1,6 +1,6 @@
1
- import type { QuotaSnapshot } from './types.js';
2
- import { type UsageSummary } from './usage.js';
3
- import type { HistoryUsageResult } from './usage_service.js';
1
+ import type { QuotaSnapshot } from "./types.js";
2
+ import { type UsageSummary } from "./usage.js";
3
+ import type { HistoryUsageResult } from "./usage_service.js";
4
4
  export declare function renderCliDashboard(input: {
5
5
  label: string;
6
6
  usage: UsageSummary;
@@ -14,4 +14,4 @@ export declare function renderCliHistoryDashboard(input: {
14
14
  width?: number;
15
15
  showCost?: boolean;
16
16
  }): string;
17
- export declare function cliCurrentLabel(period: 'day' | 'week' | 'month'): "Today" | "This Week" | "This Month";
17
+ export declare function cliCurrentLabel(period: "day" | "week" | "month"): "Today" | "This Week" | "This Month";
@@ -1,8 +1,8 @@
1
- import { canonicalProviderID, collapseQuotaSnapshots, quotaDisplayLabel, } from './quota_render.js';
2
- import { getCacheCoverageMetrics, getProviderCacheCoverageMetrics, } from './usage.js';
1
+ import { canonicalProviderID, collapseQuotaSnapshots, quotaDisplayLabel, } from "./quota_render.js";
2
+ import { getCacheCoverageMetrics, getProviderCacheCoverageMetrics, } from "./usage.js";
3
3
  function shortNumber(value, decimals = 1) {
4
4
  if (!Number.isFinite(value) || value < 0)
5
- return '0';
5
+ return "0";
6
6
  if (value >= 1_000_000)
7
7
  return `${(value / 1_000_000).toFixed(decimals)}m`;
8
8
  if (value >= 1000) {
@@ -16,51 +16,51 @@ function shortNumber(value, decimals = 1) {
16
16
  }
17
17
  function formatCurrency(value, currency) {
18
18
  const safe = Number.isFinite(value) ? value : 0;
19
- const prefix = typeof currency === 'string' && currency ? currency : '$';
19
+ const prefix = typeof currency === "string" && currency ? currency : "$";
20
20
  if (safe === 0)
21
21
  return `${prefix}0.00`;
22
22
  if (safe < 10 && safe > -10)
23
- return `${safe < 0 ? '-' : ''}${prefix}${Math.abs(safe).toFixed(2)}`;
24
- const rounded = Math.abs(safe).toFixed(1).replace(/\.0$/, '');
25
- return `${safe < 0 ? '-' : ''}${prefix}${rounded}`;
23
+ return `${safe < 0 ? "-" : ""}${prefix}${Math.abs(safe).toFixed(2)}`;
24
+ const rounded = Math.abs(safe).toFixed(1).replace(/\.0$/, "");
25
+ return `${safe < 0 ? "-" : ""}${prefix}${rounded}`;
26
26
  }
27
27
  function formatApiCost(value) {
28
- return formatCurrency(value, '$');
28
+ return formatCurrency(value, "$");
29
29
  }
30
30
  function formatPercent(value, decimals = 1) {
31
31
  const safe = Number.isFinite(value) && value >= 0 ? value : 0;
32
32
  const pct = (safe * 100).toFixed(decimals);
33
- return `${pct.replace(/\.0+$/, '').replace(/(\.\d*[1-9])0+$/, '$1')}%`;
33
+ return `${pct.replace(/\.0+$/, "").replace(/(\.\d*[1-9])0+$/, "$1")}%`;
34
34
  }
35
35
  function compactCountdown(iso) {
36
36
  if (!iso)
37
- return 'n/a';
37
+ return "n/a";
38
38
  const timestamp = Date.parse(iso);
39
39
  if (Number.isNaN(timestamp))
40
- return 'n/a';
40
+ return "n/a";
41
41
  const remainingMs = timestamp - Date.now();
42
42
  if (!Number.isFinite(remainingMs))
43
- return 'n/a';
43
+ return "n/a";
44
44
  if (remainingMs <= 0)
45
- return '0m';
45
+ return "0m";
46
46
  const totalMinutes = Math.max(1, Math.floor(remainingMs / 60_000));
47
47
  if (totalMinutes < 60)
48
48
  return `${totalMinutes}m`;
49
49
  if (totalMinutes < 24 * 60) {
50
50
  const hours = Math.floor(totalMinutes / 60);
51
51
  const minutes = totalMinutes % 60;
52
- return `${hours}h${`${minutes}`.padStart(2, '0')}m`;
52
+ return `${hours}h${`${minutes}`.padStart(2, "0")}m`;
53
53
  }
54
54
  const days = Math.floor(totalMinutes / (24 * 60));
55
55
  const hours = Math.floor((totalMinutes % (24 * 60)) / 60);
56
- return `${days}D${`${hours}`.padStart(2, '0')}h`;
56
+ return `${days}D${`${hours}`.padStart(2, "0")}h`;
57
57
  }
58
58
  function gauge(value, width = 10) {
59
59
  if (value === undefined || !Number.isFinite(value))
60
- return `${''.repeat(width)} n/a`;
60
+ return `${"".repeat(width)} n/a`;
61
61
  const ratio = Math.max(0, Math.min(1, value / 100));
62
62
  const filled = Math.max(value > 0 ? 1 : 0, Math.round(ratio * width));
63
- return `${''.repeat(filled)}${''.repeat(width - filled)} ${`${Math.round(value)}`.padStart(3, ' ')}%`;
63
+ return `${"".repeat(filled)}${"".repeat(width - filled)} ${`${Math.round(value)}`.padStart(3, " ")}%`;
64
64
  }
65
65
  function formatDelta(current, previous, format) {
66
66
  if (previous === undefined)
@@ -68,11 +68,11 @@ function formatDelta(current, previous, format) {
68
68
  if (!Number.isFinite(previous) || previous < 0)
69
69
  return `${format(current)} now`;
70
70
  if (previous === 0)
71
- return `${format(current)} now, ${current === 0 ? 'flat' : 'new'}`;
71
+ return `${format(current)} now, ${current === 0 ? "flat" : "new"}`;
72
72
  const delta = ((current - previous) / previous) * 100;
73
73
  const rounded = Math.abs(delta) >= 10 ? delta.toFixed(0) : delta.toFixed(1);
74
- const normalized = rounded.replace(/\.0$/, '');
75
- return `${format(current)} now, ${delta > 0 ? '+' : ''}${normalized}%`;
74
+ const normalized = rounded.replace(/\.0$/, "");
75
+ return `${format(current)} now, ${delta > 0 ? "+" : ""}${normalized}%`;
76
76
  }
77
77
  function clip(value, width) {
78
78
  return value.length <= width
@@ -85,58 +85,58 @@ function centerLine(value, width) {
85
85
  return clipped;
86
86
  const left = Math.floor((width - clipped.length) / 2);
87
87
  const right = width - clipped.length - left;
88
- return `${' '.repeat(left)}${clipped}${' '.repeat(right)}`;
88
+ return `${" ".repeat(left)}${clipped}${" ".repeat(right)}`;
89
89
  }
90
90
  function padRight(value, width) {
91
- return clip(value, width).padEnd(width, ' ');
91
+ return clip(value, width).padEnd(width, " ");
92
92
  }
93
93
  function box(title, lines, width = 78) {
94
94
  const longestLine = lines.reduce((max, line) => Math.max(max, line.length), 0);
95
95
  const inner = Math.max(48, width, title.length, longestLine);
96
96
  const top = centerLine(title, inner);
97
- const rule = ''.repeat(inner);
97
+ const rule = "".repeat(inner);
98
98
  const body = lines.map((line) => clip(line, inner));
99
- return [top, rule, ...body, rule].join('\n');
99
+ return [top, rule, ...body, rule].join("\n");
100
100
  }
101
101
  function currentLabel(period) {
102
- if (period === 'day')
103
- return 'Today';
104
- if (period === 'week')
105
- return 'This Week';
106
- return 'This Month';
102
+ if (period === "day")
103
+ return "Today";
104
+ if (period === "week")
105
+ return "This Week";
106
+ return "This Month";
107
107
  }
108
108
  function historyLabel(result) {
109
- if (result.period === 'day')
109
+ if (result.period === "day")
110
110
  return `Daily since ${result.since.raw}`;
111
- if (result.period === 'week')
111
+ if (result.period === "week")
112
112
  return `Weekly since ${result.since.raw}`;
113
113
  return `Monthly since ${result.since.raw}`;
114
114
  }
115
115
  function quotaRows(quotas) {
116
- const visible = collapseQuotaSnapshots(quotas).filter((item) => item.status === 'ok' || item.status === 'error');
116
+ const visible = collapseQuotaSnapshots(quotas).filter((item) => item.status === "ok" || item.status === "error");
117
117
  if (visible.length === 0)
118
- return ['no provider quota data available'];
118
+ return ["no provider quota data available"];
119
119
  return visible.flatMap((quota) => {
120
- const label = quotaDisplayLabel(quota).padEnd(11, ' ');
121
- if (quota.status === 'error') {
122
- return [`${label} error${quota.note ? ` · ${quota.note}` : ''}`];
120
+ const label = quotaDisplayLabel(quota).padEnd(11, " ");
121
+ if (quota.status === "error") {
122
+ return [`${label} error${quota.note ? ` · ${quota.note}` : ""}`];
123
123
  }
124
124
  if (quota.windows && quota.windows.length > 0) {
125
125
  const lines = quota.windows.map((win) => {
126
- const detail = padRight(win.label || 'quota', 18);
126
+ const detail = padRight(win.label || "quota", 18);
127
127
  if (win.showPercent === false) {
128
128
  return `${label}${detail} ${compactCountdown(win.resetAt)}`;
129
129
  }
130
130
  return `${label}${detail} [${gauge(win.remainingPercent)}] ${compactCountdown(win.resetAt)}`;
131
131
  });
132
132
  if (quota.balance) {
133
- lines.push(`${label}${padRight('balance', 18)} ${formatCurrency(quota.balance.amount, quota.balance.currency)}`);
133
+ lines.push(`${label}${padRight("balance", 18)} ${formatCurrency(quota.balance.amount, quota.balance.currency)}`);
134
134
  }
135
135
  return lines;
136
136
  }
137
137
  if (quota.balance) {
138
138
  return [
139
- `${label}${padRight('balance', 18)} ${formatCurrency(quota.balance.amount, quota.balance.currency)}`,
139
+ `${label}${padRight("balance", 18)} ${formatCurrency(quota.balance.amount, quota.balance.currency)}`,
140
140
  ];
141
141
  }
142
142
  return [
@@ -147,48 +147,48 @@ function quotaRows(quotas) {
147
147
  function providerRows(usage, showCost) {
148
148
  const providers = Object.values(usage.providers).sort((a, b) => b.total - a.total);
149
149
  if (providers.length === 0)
150
- return ['no provider activity'];
150
+ return ["no provider activity"];
151
151
  return providers.map((provider) => {
152
152
  const cache = getProviderCacheCoverageMetrics(provider).cachedRatio;
153
- const base = `${quotaDisplayLabel({ providerID: provider.providerID, label: provider.providerID, status: 'ok', checkedAt: 0 }).padEnd(10, ' ')} ${shortNumber(provider.assistantMessages).padStart(4, ' ')} req ${shortNumber(provider.total).padStart(7, ' ')} tok ${(cache !== undefined ? formatPercent(cache, 0) : '-').padStart(4, ' ')} cache`;
154
- const apiCost = canonicalProviderID(provider.providerID) === 'github-copilot'
155
- ? '-'
153
+ const base = `${quotaDisplayLabel({ providerID: provider.providerID, label: provider.providerID, status: "ok", checkedAt: 0 }).padEnd(10, " ")} ${shortNumber(provider.assistantMessages).padStart(4, " ")} req ${shortNumber(provider.total).padStart(7, " ")} tok ${(cache !== undefined ? formatPercent(cache, 0) : "-").padStart(4, " ")} cache`;
154
+ const apiCost = canonicalProviderID(provider.providerID) === "github-copilot"
155
+ ? "-"
156
156
  : formatApiCost(provider.apiCost);
157
- return showCost ? `${base} ${apiCost.padStart(7, ' ')}` : base;
157
+ return showCost ? `${base} ${apiCost.padStart(7, " ")}` : base;
158
158
  });
159
159
  }
160
160
  function cliApiCostSummary(usage) {
161
161
  const providers = Object.values(usage.providers);
162
162
  if (providers.length === 0)
163
163
  return formatApiCost(usage.apiCost);
164
- const hasNonCopilot = providers.some((provider) => canonicalProviderID(provider.providerID) !== 'github-copilot');
165
- return hasNonCopilot ? formatApiCost(usage.apiCost) : '-';
164
+ const hasNonCopilot = providers.some((provider) => canonicalProviderID(provider.providerID) !== "github-copilot");
165
+ return hasNonCopilot ? formatApiCost(usage.apiCost) : "-";
166
166
  }
167
167
  function totalsRows(input) {
168
168
  const left = [`Requests ${input.requests}`, `Tokens ${input.tokens}`];
169
169
  const right = [
170
- ...(input.cost ? [`Cost ${input.cost}`] : []),
170
+ ...(input.cost ? [`API Cost ${input.cost}`] : []),
171
171
  ...(input.cache ? [`Cache ${input.cache}`] : []),
172
172
  ];
173
173
  const metaLeft = input.periods ? `Periods ${input.periods}` : undefined;
174
174
  const metaRight = input.current ? `Current ${input.current}` : undefined;
175
- const row1 = [left[0], left[1], ...right].join(' ');
176
- const row2 = [metaLeft, metaRight].filter(Boolean).join(' ');
175
+ const row1 = [left[0], left[1], ...right].join(" ");
176
+ const row2 = [metaLeft, metaRight].filter(Boolean).join(" ");
177
177
  return [row1, ...(row2 ? [row2] : [])];
178
178
  }
179
179
  function trendBar(value, maxValue, width = 20) {
180
180
  if (!Number.isFinite(value) || value <= 0 || maxValue <= 0) {
181
- return ''.repeat(width);
181
+ return "".repeat(width);
182
182
  }
183
183
  const filled = Math.max(1, Math.round((value / maxValue) * width));
184
- return `${''.repeat(filled)}${''.repeat(width - filled)}`;
184
+ return `${"".repeat(filled)}${"".repeat(width - filled)}`;
185
185
  }
186
186
  function trendMetricBlock(input) {
187
187
  const visibleRows = input.rows.slice(-Math.min(8, input.rows.length));
188
188
  const values = visibleRows.map(input.pick);
189
189
  const maxValue = Math.max(...values, 0);
190
190
  const currentValue = input.current ? input.pick(input.current) : 0;
191
- const displayLabels = visibleRows.map((row) => `${row.range.shortLabel}${row.range.isCurrent ? '*' : ''}`);
191
+ const displayLabels = visibleRows.map((row) => `${row.range.shortLabel}${row.range.isCurrent ? "*" : ""}`);
192
192
  const labelWidth = Math.max(8, Math.min(28, Math.max(...displayLabels.map((label) => label.length), 8)));
193
193
  return [
194
194
  `${input.label} ${input.format(currentValue)}`,
@@ -204,20 +204,20 @@ export function renderCliDashboard(input) {
204
204
  const showCost = input.showCost !== false;
205
205
  const cache = getCacheCoverageMetrics(input.usage).cachedRatio;
206
206
  return box(`opencode-quota · ${input.label}`, [
207
- 'QUOTA',
207
+ "QUOTA",
208
208
  ...quotaRows(input.quotas),
209
- '',
210
- 'TOTALS',
209
+ "",
210
+ "TOTALS",
211
211
  ...totalsRows({
212
212
  requests: shortNumber(input.usage.assistantMessages),
213
213
  tokens: shortNumber(input.usage.total),
214
214
  ...(showCost ? { cost: cliApiCostSummary(input.usage) } : {}),
215
- cache: cache !== undefined ? formatPercent(cache, 1) : '-',
215
+ cache: cache !== undefined ? formatPercent(cache, 1) : "-",
216
216
  periods: `${input.usage.sessionCount}`,
217
217
  }),
218
218
  `Input ${shortNumber(input.usage.input)} Output ${shortNumber(input.usage.output)}`,
219
- '',
220
- 'PROVIDERS',
219
+ "",
220
+ "PROVIDERS",
221
221
  ...providerRows(input.usage, showCost),
222
222
  ], width);
223
223
  }
@@ -231,23 +231,23 @@ export function renderCliHistoryDashboard(input) {
231
231
  const cache = getCacheCoverageMetrics(input.result.total).cachedRatio;
232
232
  const trendBlocks = [
233
233
  ...trendMetricBlock({
234
- label: 'Requests',
234
+ label: "Requests",
235
235
  rows,
236
236
  current,
237
237
  pick: (row) => row.usage.assistantMessages,
238
238
  format: (value) => shortNumber(value),
239
239
  }),
240
- '',
240
+ "",
241
241
  ...trendMetricBlock({
242
- label: 'Tokens',
242
+ label: "Tokens",
243
243
  rows,
244
244
  current,
245
245
  pick: (row) => row.usage.total,
246
246
  format: (value) => shortNumber(value),
247
247
  }),
248
- '',
248
+ "",
249
249
  ...trendMetricBlock({
250
- label: 'Cache',
250
+ label: "Cache",
251
251
  rows,
252
252
  current,
253
253
  pick: (row) => getCacheCoverageMetrics(row.usage).cachedRatio ?? 0,
@@ -255,9 +255,9 @@ export function renderCliHistoryDashboard(input) {
255
255
  }),
256
256
  ...(showCost
257
257
  ? [
258
- '',
258
+ "",
259
259
  ...trendMetricBlock({
260
- label: 'Cost',
260
+ label: "API Cost",
261
261
  rows,
262
262
  current,
263
263
  pick: (row) => row.usage.apiCost,
@@ -267,23 +267,23 @@ export function renderCliHistoryDashboard(input) {
267
267
  : []),
268
268
  ];
269
269
  return box(`opencode-quota · ${historyLabel(input.result)}`, [
270
- 'QUOTA',
270
+ "QUOTA",
271
271
  ...quotaRows(input.quotas),
272
- '',
273
- 'TOTALS',
272
+ "",
273
+ "TOTALS",
274
274
  ...totalsRows({
275
275
  requests: shortNumber(input.result.total.assistantMessages),
276
276
  tokens: shortNumber(input.result.total.total),
277
277
  ...(showCost ? { cost: cliApiCostSummary(input.result.total) } : {}),
278
- cache: cache !== undefined ? formatPercent(cache, 1) : '-',
278
+ cache: cache !== undefined ? formatPercent(cache, 1) : "-",
279
279
  periods: `${rows.length}`,
280
- current: current?.range.shortLabel || '-',
280
+ current: current?.range.shortLabel || "-",
281
281
  }),
282
- '',
283
- 'PROVIDERS',
282
+ "",
283
+ "PROVIDERS",
284
284
  ...providerRows(input.result.total, showCost),
285
- '',
286
- 'TREND',
285
+ "",
286
+ "TREND",
287
287
  ...trendBlocks,
288
288
  ], width);
289
289
  }