@leo000001/opencode-quota-sidebar 4.0.9 → 4.0.11
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/README.md +504 -446
- package/README.zh-CN.md +516 -458
- package/dist/cli.d.ts +31 -0
- package/dist/cli.js +95 -40
- package/dist/cli_render.d.ts +4 -4
- package/dist/cli_render.js +74 -74
- package/dist/cost.d.ts +21 -4
- package/dist/cost.js +493 -264
- package/dist/format.d.ts +5 -5
- package/dist/format.js +288 -287
- package/dist/history_usage.d.ts +15 -9
- package/dist/history_usage.js +28 -22
- package/dist/index.d.ts +3 -3
- package/dist/index.js +35 -34
- package/dist/models_dev_pricing.d.ts +6 -0
- package/dist/models_dev_pricing.js +226 -0
- package/dist/opencode_pricing.d.ts +14 -0
- package/dist/opencode_pricing.js +273 -0
- package/dist/storage.d.ts +3 -3
- package/dist/storage.js +27 -28
- package/dist/storage_parse.d.ts +1 -1
- package/dist/storage_parse.js +51 -45
- package/dist/storage_paths.d.ts +1 -0
- package/dist/storage_paths.js +26 -11
- package/dist/title_apply.d.ts +5 -22
- package/dist/title_apply.js +19 -61
- package/dist/tui.d.ts +1 -1
- package/dist/tui.tsx +481 -471
- package/dist/tui_helpers.d.ts +5 -3
- package/dist/tui_helpers.js +62 -34
- package/dist/types.d.ts +8 -10
- package/dist/usage.d.ts +9 -6
- package/dist/usage.js +27 -21
- package/dist/usage_service.d.ts +8 -7
- package/dist/usage_service.js +261 -150
- package/package.json +1 -1
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
|
@@ -15,6 +15,42 @@ import { filterHistoryProvidersForDisplay, filterUsageProvidersForDisplay, listC
|
|
|
15
15
|
import { createUsageService } from './usage_service.js';
|
|
16
16
|
const DEFAULT_OPENCODE_BASE_URL = 'http://localhost:4096';
|
|
17
17
|
const CLI_SERVER_TIMEOUT_MS = 10_000;
|
|
18
|
+
const CLI_FORCE_EXIT_DELAY_MS = 100;
|
|
19
|
+
export function releaseCliServerProcess(proc) {
|
|
20
|
+
// The CLI only needs the child pipes until the server prints its listen URL.
|
|
21
|
+
// After that, unref/destroy them so the parent process can exit cleanly.
|
|
22
|
+
proc.stdin?.destroy();
|
|
23
|
+
proc.stdout?.destroy();
|
|
24
|
+
proc.stderr?.destroy();
|
|
25
|
+
proc.unref();
|
|
26
|
+
}
|
|
27
|
+
export function terminateCliServerProcess(proc, options) {
|
|
28
|
+
releaseCliServerProcess(proc);
|
|
29
|
+
if (proc.killed)
|
|
30
|
+
return;
|
|
31
|
+
const platform = options?.platform ?? process.platform;
|
|
32
|
+
const killProcess = options?.killProcess ?? process.kill;
|
|
33
|
+
if (platform !== 'win32' && typeof proc.pid === 'number' && proc.pid > 0) {
|
|
34
|
+
try {
|
|
35
|
+
killProcess(-proc.pid, 'SIGTERM');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Fall back to the direct child pid when group termination is unavailable.
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
proc.kill('SIGTERM');
|
|
43
|
+
}
|
|
44
|
+
export function extractCliServerUrl(output) {
|
|
45
|
+
for (const line of output.split('\n')) {
|
|
46
|
+
if (!line.startsWith('opencode server listening'))
|
|
47
|
+
continue;
|
|
48
|
+
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
|
|
49
|
+
if (match)
|
|
50
|
+
return match[1];
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
18
54
|
const HELP_TEXT = `opencode-quota
|
|
19
55
|
|
|
20
56
|
Usage:
|
|
@@ -172,6 +208,7 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
|
|
|
172
208
|
try {
|
|
173
209
|
proc = spawnProcess(candidate.command, candidate.args, {
|
|
174
210
|
env: process.env,
|
|
211
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
175
212
|
shell: candidate.shell ?? false,
|
|
176
213
|
detached: true,
|
|
177
214
|
windowsHide: true,
|
|
@@ -189,6 +226,8 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
|
|
|
189
226
|
let inspect;
|
|
190
227
|
let onError;
|
|
191
228
|
let onExit;
|
|
229
|
+
let output = '';
|
|
230
|
+
let settled = false;
|
|
192
231
|
const id = setTimeout(() => {
|
|
193
232
|
if (settled)
|
|
194
233
|
return;
|
|
@@ -197,54 +236,47 @@ export async function tryStartCliOpencodeServer(candidate, spawnProcess = spawn,
|
|
|
197
236
|
closeProcess(proc);
|
|
198
237
|
reject(new Error(`Timeout waiting for OpenCode server to start after ${CLI_SERVER_TIMEOUT_MS}ms`));
|
|
199
238
|
}, CLI_SERVER_TIMEOUT_MS);
|
|
200
|
-
|
|
201
|
-
|
|
239
|
+
id.unref?.();
|
|
240
|
+
const fail = (failure) => {
|
|
241
|
+
if (settled)
|
|
242
|
+
return;
|
|
243
|
+
settled = true;
|
|
244
|
+
clearTimeout(id);
|
|
245
|
+
releaseCliServerPipes(proc, inspect, onError, onExit);
|
|
246
|
+
reject(failure);
|
|
247
|
+
};
|
|
202
248
|
inspect = (chunk) => {
|
|
203
249
|
output += chunk.toString();
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
if (!line.startsWith('opencode server listening'))
|
|
207
|
-
continue;
|
|
208
|
-
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
|
|
209
|
-
if (!match)
|
|
210
|
-
continue;
|
|
250
|
+
const url = extractCliServerUrl(output);
|
|
251
|
+
if (url) {
|
|
211
252
|
clearTimeout(id);
|
|
212
253
|
settled = true;
|
|
213
254
|
releaseCliServerPipes(proc, inspect, onError, onExit);
|
|
214
255
|
// The CLI only needs the startup line; after that the detached server
|
|
215
256
|
// must not keep the parent process alive.
|
|
257
|
+
proc.stdin?.destroy();
|
|
216
258
|
proc.unref();
|
|
217
|
-
resolve(
|
|
259
|
+
resolve(url);
|
|
218
260
|
return;
|
|
219
261
|
}
|
|
220
262
|
};
|
|
221
263
|
proc.stdout?.on('data', inspect);
|
|
222
264
|
proc.stderr?.on('data', inspect);
|
|
223
265
|
onError = (error) => {
|
|
224
|
-
if (settled)
|
|
225
|
-
return;
|
|
226
|
-
settled = true;
|
|
227
|
-
clearTimeout(id);
|
|
228
|
-
releaseCliServerPipes(proc, inspect, onError, onExit);
|
|
229
266
|
const code = error.code;
|
|
230
|
-
|
|
267
|
+
fail({
|
|
231
268
|
error,
|
|
232
269
|
output,
|
|
233
270
|
recoverable: code === 'ENOENT' || code === 'EINVAL',
|
|
234
271
|
});
|
|
235
272
|
};
|
|
236
273
|
onExit = (code) => {
|
|
237
|
-
if (settled)
|
|
238
|
-
return;
|
|
239
|
-
settled = true;
|
|
240
|
-
clearTimeout(id);
|
|
241
|
-
releaseCliServerPipes(proc, inspect, onError, onExit);
|
|
242
274
|
let message = `OpenCode server exited with code ${code}`;
|
|
243
275
|
if (output.trim())
|
|
244
276
|
message += `\n${output}`;
|
|
245
277
|
const recoverable = /not recognized as an internal or external command/i.test(output) ||
|
|
246
278
|
/command not found/i.test(output);
|
|
247
|
-
|
|
279
|
+
fail({ error: new Error(message), output, recoverable });
|
|
248
280
|
};
|
|
249
281
|
proc.on('error', onError);
|
|
250
282
|
proc.on('exit', onExit);
|
|
@@ -305,23 +337,40 @@ async function resolvePathInfo(directory) {
|
|
|
305
337
|
throw new Error(`Failed to connect to OpenCode API at ${cliBaseUrl()}: ${error instanceof Error ? error.message : String(error)}`);
|
|
306
338
|
}
|
|
307
339
|
const server = await startCliOpencodeServer();
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
340
|
+
try {
|
|
341
|
+
const client = createOpencodeClient({
|
|
342
|
+
directory,
|
|
343
|
+
baseUrl: server.url,
|
|
344
|
+
});
|
|
345
|
+
const response = await client.path.get({
|
|
346
|
+
query: { directory },
|
|
347
|
+
throwOnError: true,
|
|
348
|
+
});
|
|
349
|
+
const data = response.data;
|
|
350
|
+
return {
|
|
351
|
+
client,
|
|
352
|
+
worktree: data.worktree || directory,
|
|
353
|
+
directory: data.directory || directory,
|
|
354
|
+
close: () => server.close(),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
catch (innerError) {
|
|
358
|
+
server.close();
|
|
359
|
+
throw innerError;
|
|
360
|
+
}
|
|
323
361
|
}
|
|
324
362
|
}
|
|
363
|
+
function writeCliLine(stream, value) {
|
|
364
|
+
return new Promise((resolve, reject) => {
|
|
365
|
+
stream.write(`${value}\n`, (error) => {
|
|
366
|
+
if (error) {
|
|
367
|
+
reject(error);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
resolve();
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
}
|
|
325
374
|
export async function runCli(argv) {
|
|
326
375
|
const command = parseCliArgs(argv);
|
|
327
376
|
const cwd = process.cwd();
|
|
@@ -348,6 +397,7 @@ export async function runCli(argv) {
|
|
|
348
397
|
statePath,
|
|
349
398
|
client: client,
|
|
350
399
|
directory,
|
|
400
|
+
worktree,
|
|
351
401
|
persistence: {
|
|
352
402
|
markDirty: () => { },
|
|
353
403
|
scheduleSave: () => { },
|
|
@@ -406,16 +456,21 @@ export function cliShouldRunMain(argv1 = process.argv[1], modulePath = fileURLTo
|
|
|
406
456
|
return resolvePath(modulePath) === resolvePath(argv1);
|
|
407
457
|
}
|
|
408
458
|
async function main() {
|
|
459
|
+
let exitCode = 0;
|
|
409
460
|
try {
|
|
410
461
|
const output = await runCli(process.argv.slice(2));
|
|
411
|
-
process.stdout
|
|
462
|
+
await writeCliLine(process.stdout, output);
|
|
412
463
|
}
|
|
413
464
|
catch (error) {
|
|
414
465
|
const message = error instanceof Error ? error.message : String(error);
|
|
415
|
-
|
|
466
|
+
exitCode = cliExitCodeForError(message);
|
|
416
467
|
const stream = exitCode === 0 ? process.stdout : process.stderr;
|
|
417
|
-
stream
|
|
468
|
+
await writeCliLine(stream, message);
|
|
469
|
+
}
|
|
470
|
+
finally {
|
|
418
471
|
process.exitCode = exitCode;
|
|
472
|
+
const forceExit = setTimeout(() => process.exit(exitCode), CLI_FORCE_EXIT_DELAY_MS);
|
|
473
|
+
forceExit.unref?.();
|
|
419
474
|
}
|
|
420
475
|
}
|
|
421
476
|
if (cliShouldRunMain()) {
|
package/dist/cli_render.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { QuotaSnapshot } from
|
|
2
|
-
import { type UsageSummary } from
|
|
3
|
-
import type { HistoryUsageResult } from
|
|
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:
|
|
17
|
+
export declare function cliCurrentLabel(period: "day" | "week" | "month"): "Today" | "This Week" | "This Month";
|
package/dist/cli_render.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { canonicalProviderID, collapseQuotaSnapshots, quotaDisplayLabel, } from
|
|
2
|
-
import { getCacheCoverageMetrics, getProviderCacheCoverageMetrics, } from
|
|
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
|
|
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 ===
|
|
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 ?
|
|
24
|
-
const rounded = Math.abs(safe).toFixed(1).replace(/\.0$/,
|
|
25
|
-
return `${safe < 0 ?
|
|
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+$/,
|
|
33
|
+
return `${pct.replace(/\.0+$/, "").replace(/(\.\d*[1-9])0+$/, "$1")}%`;
|
|
34
34
|
}
|
|
35
35
|
function compactCountdown(iso) {
|
|
36
36
|
if (!iso)
|
|
37
|
-
return
|
|
37
|
+
return "n/a";
|
|
38
38
|
const timestamp = Date.parse(iso);
|
|
39
39
|
if (Number.isNaN(timestamp))
|
|
40
|
-
return
|
|
40
|
+
return "n/a";
|
|
41
41
|
const remainingMs = timestamp - Date.now();
|
|
42
42
|
if (!Number.isFinite(remainingMs))
|
|
43
|
-
return
|
|
43
|
+
return "n/a";
|
|
44
44
|
if (remainingMs <= 0)
|
|
45
|
-
return
|
|
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,
|
|
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,
|
|
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 `${
|
|
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 `${
|
|
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 ?
|
|
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 ?
|
|
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 `${
|
|
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 =
|
|
97
|
+
const rule = "─".repeat(inner);
|
|
98
98
|
const body = lines.map((line) => clip(line, inner));
|
|
99
|
-
return [top, rule, ...body, rule].join(
|
|
99
|
+
return [top, rule, ...body, rule].join("\n");
|
|
100
100
|
}
|
|
101
101
|
function currentLabel(period) {
|
|
102
|
-
if (period ===
|
|
103
|
-
return
|
|
104
|
-
if (period ===
|
|
105
|
-
return
|
|
106
|
-
return
|
|
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 ===
|
|
109
|
+
if (result.period === "day")
|
|
110
110
|
return `Daily since ${result.since.raw}`;
|
|
111
|
-
if (result.period ===
|
|
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 ===
|
|
116
|
+
const visible = collapseQuotaSnapshots(quotas).filter((item) => item.status === "ok" || item.status === "error");
|
|
117
117
|
if (visible.length === 0)
|
|
118
|
-
return [
|
|
118
|
+
return ["no provider quota data available"];
|
|
119
119
|
return visible.flatMap((quota) => {
|
|
120
|
-
const label = quotaDisplayLabel(quota).padEnd(11,
|
|
121
|
-
if (quota.status ===
|
|
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 ||
|
|
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(
|
|
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(
|
|
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 [
|
|
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:
|
|
154
|
-
const apiCost = canonicalProviderID(provider.providerID) ===
|
|
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,
|
|
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) !==
|
|
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
|
|
181
|
+
return "░".repeat(width);
|
|
182
182
|
}
|
|
183
183
|
const filled = Math.max(1, Math.round((value / maxValue) * width));
|
|
184
|
-
return `${
|
|
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
|
-
|
|
207
|
+
"QUOTA",
|
|
208
208
|
...quotaRows(input.quotas),
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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
|
-
|
|
270
|
+
"QUOTA",
|
|
271
271
|
...quotaRows(input.quotas),
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
282
|
+
"",
|
|
283
|
+
"PROVIDERS",
|
|
284
284
|
...providerRows(input.result.total, showCost),
|
|
285
|
-
|
|
286
|
-
|
|
285
|
+
"",
|
|
286
|
+
"TREND",
|
|
287
287
|
...trendBlocks,
|
|
288
288
|
], width);
|
|
289
289
|
}
|
package/dist/cost.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { AssistantMessage } from
|
|
2
|
-
import type {
|
|
1
|
+
import type { AssistantMessage } from "@opencode-ai/sdk";
|
|
2
|
+
import type { OpenCodePricingModel } from "./opencode_pricing.js";
|
|
3
|
+
import type { CacheCoverageMode } from "./types.js";
|
|
3
4
|
export declare const API_COST_ENABLED_PROVIDERS: Set<string>;
|
|
4
|
-
export
|
|
5
|
+
export declare const API_COST_RULES_VERSION = 2;
|
|
6
|
+
export type CanonicalPriceSource = "official-doc" | "runtime";
|
|
5
7
|
export declare function canonicalPricingProviderID(providerID: string): string;
|
|
6
8
|
export declare function canonicalApiCostProviderID(providerID: string): string;
|
|
7
9
|
export type ModelCostRates = {
|
|
@@ -26,6 +28,21 @@ export type CanonicalPriceEntry = {
|
|
|
26
28
|
};
|
|
27
29
|
export declare function modelCostKey(providerID: string, modelID: string): string;
|
|
28
30
|
export declare function modelCostLookupKeys(providerID: string, modelID: string): string[];
|
|
31
|
+
export declare function derivedTierBaseModelID(model: OpenCodePricingModel): string | undefined;
|
|
32
|
+
export declare function explicitModelCostMap(models: OpenCodePricingModel[]): Record<string, ModelCostRates>;
|
|
33
|
+
export declare function applyDerivedTierRatesFromSource(baseMap: Record<string, ModelCostRates>, metadataModels: OpenCodePricingModel[], sourceRates: Record<string, ModelCostRates>, options?: {
|
|
34
|
+
skipExplicitRates?: Record<string, ModelCostRates>;
|
|
35
|
+
}): {
|
|
36
|
+
[x: string]: ModelCostRates;
|
|
37
|
+
};
|
|
38
|
+
export declare function applyExplicitRatesFromSource(baseMap: Record<string, ModelCostRates>, metadataModels: OpenCodePricingModel[], sourceRates: Record<string, ModelCostRates>, options?: {
|
|
39
|
+
skipExplicitRates?: Record<string, ModelCostRates>;
|
|
40
|
+
}): {
|
|
41
|
+
[x: string]: ModelCostRates;
|
|
42
|
+
};
|
|
43
|
+
export declare function mergeModelCostSource(baseMap: Record<string, ModelCostRates>, models: OpenCodePricingModel[]): {
|
|
44
|
+
[x: string]: ModelCostRates;
|
|
45
|
+
};
|
|
29
46
|
export declare function getBundledModelCostMap(): {
|
|
30
47
|
[x: string]: ModelCostRates;
|
|
31
48
|
};
|
|
@@ -48,7 +65,7 @@ export declare function getBundledCanonicalPriceEntries(): {
|
|
|
48
65
|
sourceURL?: string;
|
|
49
66
|
updatedAt?: string;
|
|
50
67
|
}[];
|
|
68
|
+
export declare function normalizeModelCostRates(rates: ModelCostRates): ModelCostRates;
|
|
51
69
|
export declare function parseModelCostRates(value: unknown): ModelCostRates | undefined;
|
|
52
|
-
export declare function guessModelCostDivisor(rates: ModelCostRates): 1 | 1000000;
|
|
53
70
|
export declare function cacheCoverageModeFromRates(rates: ModelCostRates | undefined): CacheCoverageMode;
|
|
54
71
|
export declare function calcEquivalentApiCostForMessage(message: AssistantMessage, rates: ModelCostRates): number;
|