@leo000001/opencode-quota-sidebar 1.4.0 → 1.7.0
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/format.js
CHANGED
|
@@ -127,9 +127,22 @@ function fitLine(value, width) {
|
|
|
127
127
|
return truncateToCellWidth(safe, width);
|
|
128
128
|
return `${head}~`;
|
|
129
129
|
}
|
|
130
|
-
function
|
|
130
|
+
function formatCurrency(value, currency) {
|
|
131
131
|
const safe = Number.isFinite(value) && value > 0 ? value : 0;
|
|
132
|
-
|
|
132
|
+
const prefix = typeof currency === 'string' && currency ? currency : '$';
|
|
133
|
+
if (safe === 0)
|
|
134
|
+
return `${prefix}0.00`;
|
|
135
|
+
if (safe < 10)
|
|
136
|
+
return `${prefix}${safe.toFixed(2)}`;
|
|
137
|
+
const one = safe.toFixed(1);
|
|
138
|
+
const trimmed = one.endsWith('.0') ? one.slice(0, -2) : one;
|
|
139
|
+
return `${prefix}${trimmed}`;
|
|
140
|
+
}
|
|
141
|
+
function formatUsd(value) {
|
|
142
|
+
return formatCurrency(value, '$');
|
|
143
|
+
}
|
|
144
|
+
function formatApiCostValue(value) {
|
|
145
|
+
return formatUsd(value);
|
|
133
146
|
}
|
|
134
147
|
function formatApiCostLine(value) {
|
|
135
148
|
return `${formatApiCostValue(value)} as API cost`;
|
|
@@ -237,7 +250,7 @@ function compactQuotaWide(quota, labelWidth = 0, options) {
|
|
|
237
250
|
if (quota.status !== 'ok')
|
|
238
251
|
return [];
|
|
239
252
|
const balanceText = quota.balance
|
|
240
|
-
? `Balance ${quota.balance.
|
|
253
|
+
? `Balance ${formatCurrency(quota.balance.amount, quota.balance.currency)}`
|
|
241
254
|
: undefined;
|
|
242
255
|
const renderWindow = (win) => {
|
|
243
256
|
const showPercent = win.showPercent !== false;
|
|
@@ -359,7 +372,7 @@ export function renderMarkdownReport(period, usage, quotas, options) {
|
|
|
359
372
|
rightCodeSubscriptionProviderIDs.has(providerID);
|
|
360
373
|
if (isSubscription)
|
|
361
374
|
return '-';
|
|
362
|
-
return
|
|
375
|
+
return formatUsd(cost);
|
|
363
376
|
};
|
|
364
377
|
const isSubscriptionMeasuredProvider = (providerID) => {
|
|
365
378
|
const canonical = canonicalProviderID(providerID);
|
|
@@ -371,18 +384,16 @@ export function renderMarkdownReport(period, usage, quotas, options) {
|
|
|
371
384
|
const canonical = canonicalProviderID(providerID);
|
|
372
385
|
if (canonical === 'github-copilot')
|
|
373
386
|
return '-';
|
|
374
|
-
|
|
375
|
-
return '$0.00';
|
|
376
|
-
return `$${apiCost.toFixed(2)}`;
|
|
387
|
+
return formatUsd(apiCost);
|
|
377
388
|
};
|
|
378
389
|
const measuredCostSummaryValue = () => {
|
|
379
390
|
const providers = Object.values(usage.providers);
|
|
380
391
|
if (providers.length === 0)
|
|
381
|
-
return
|
|
392
|
+
return formatUsd(usage.cost);
|
|
382
393
|
const hasNonSubscription = providers.some((provider) => !isSubscriptionMeasuredProvider(provider.providerID));
|
|
383
394
|
if (!hasNonSubscription)
|
|
384
395
|
return '-';
|
|
385
|
-
return
|
|
396
|
+
return formatUsd(usage.cost);
|
|
386
397
|
};
|
|
387
398
|
const apiCostSummaryValue = () => {
|
|
388
399
|
const providers = Object.values(usage.providers);
|
|
@@ -418,7 +429,7 @@ export function renderMarkdownReport(period, usage, quotas, options) {
|
|
|
418
429
|
}
|
|
419
430
|
if (quota.status === 'ok' && quota.balance) {
|
|
420
431
|
return [
|
|
421
|
-
mdCell(`- ${quota.label}: ${quota.status} | balance ${quota.balance.
|
|
432
|
+
mdCell(`- ${quota.label}: ${quota.status} | balance ${formatCurrency(quota.balance.amount, quota.balance.currency)}`),
|
|
422
433
|
];
|
|
423
434
|
}
|
|
424
435
|
const remaining = quota.remainingPercent === undefined
|
|
@@ -497,7 +508,7 @@ export function renderToastMessage(period, usage, quotas, options) {
|
|
|
497
508
|
.sort((left, right) => right.apiCost - left.apiCost)
|
|
498
509
|
.map((provider) => ({
|
|
499
510
|
label: displayShortLabel(provider.providerID),
|
|
500
|
-
value:
|
|
511
|
+
value: formatUsd(provider.apiCost),
|
|
501
512
|
}));
|
|
502
513
|
lines.push('');
|
|
503
514
|
lines.push(fitLine('Cost as API', width));
|
|
@@ -531,7 +542,7 @@ export function renderToastMessage(period, usage, quotas, options) {
|
|
|
531
542
|
if (item.balance) {
|
|
532
543
|
pairs.push({
|
|
533
544
|
label: '',
|
|
534
|
-
value: `Balance ${item.balance.
|
|
545
|
+
value: `Balance ${formatCurrency(item.balance.amount, item.balance.currency)}`,
|
|
535
546
|
});
|
|
536
547
|
}
|
|
537
548
|
return pairs;
|
|
@@ -540,7 +551,7 @@ export function renderToastMessage(period, usage, quotas, options) {
|
|
|
540
551
|
return [
|
|
541
552
|
{
|
|
542
553
|
label: quotaDisplayLabel(item),
|
|
543
|
-
value: `Balance ${item.balance.
|
|
554
|
+
value: `Balance ${formatCurrency(item.balance.amount, item.balance.currency)}`,
|
|
544
555
|
},
|
|
545
556
|
];
|
|
546
557
|
}
|
|
@@ -51,8 +51,11 @@ function matchesSubscriptionPrefix(providerPrefixes, availablePrefixes) {
|
|
|
51
51
|
function formatQuotaValue(value) {
|
|
52
52
|
if (!Number.isFinite(value))
|
|
53
53
|
return '0';
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
if (Math.abs(value) >= 10) {
|
|
55
|
+
const one = value.toFixed(1);
|
|
56
|
+
return one.endsWith('.0') ? one.slice(0, -2) : one;
|
|
57
|
+
}
|
|
58
|
+
return value.toFixed(2);
|
|
56
59
|
}
|
|
57
60
|
function parseSubscription(value) {
|
|
58
61
|
const total = asNumber(value.total_quota);
|