@1930dev/opencode-usage 0.1.2 → 0.1.4
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.js +10 -10
- package/dist/tui.js +14 -14
- package/package.json +7 -3
package/dist/cli.js
CHANGED
|
@@ -134,13 +134,13 @@ function localUsageByProvider(db, sinceMs) {
|
|
|
134
134
|
}
|
|
135
135
|
return map;
|
|
136
136
|
}
|
|
137
|
-
function monthlyUsageByProvider(db,
|
|
137
|
+
function monthlyUsageByProvider(db, startOfMonthMs) {
|
|
138
138
|
const rows = db.query(`SELECT COALESCE(json_extract(data,'$.providerID'),'?') AS provider,
|
|
139
139
|
COALESCE(SUM(CAST(json_extract(data,'$.cost') AS REAL)),0) AS cost
|
|
140
140
|
FROM message
|
|
141
141
|
WHERE json_extract(data,'$.role')='assistant'
|
|
142
142
|
AND CAST(json_extract(data,'$.time.created') AS INTEGER) >= ?
|
|
143
|
-
GROUP BY provider`).all(
|
|
143
|
+
GROUP BY provider`).all(startOfMonthMs);
|
|
144
144
|
const map = new Map;
|
|
145
145
|
for (const r of rows) {
|
|
146
146
|
map.set(String(r.provider), Number(r.cost));
|
|
@@ -840,33 +840,33 @@ async function cmdUsage(flags, json) {
|
|
|
840
840
|
const withPct = flags.has("--pct");
|
|
841
841
|
if (withPct && groupBy !== "provider")
|
|
842
842
|
fail("--pct only supported with --by provider");
|
|
843
|
-
const
|
|
843
|
+
const snapshot = await getUsageSnapshot(sinceMs, groupBy, withPct);
|
|
844
844
|
if (json) {
|
|
845
845
|
const out = {
|
|
846
846
|
since: new Date(sinceMs).toISOString(),
|
|
847
847
|
groupBy,
|
|
848
|
-
rows:
|
|
849
|
-
totals:
|
|
850
|
-
pct:
|
|
848
|
+
rows: snapshot.rows,
|
|
849
|
+
totals: snapshot.totals,
|
|
850
|
+
pct: snapshot.pct
|
|
851
851
|
};
|
|
852
852
|
console.log(JSON.stringify(out, null, 2));
|
|
853
853
|
} else {
|
|
854
854
|
const label = flags.has("--today") ? "today" : flags.get("--since") ?? "7d";
|
|
855
|
-
console.log(usageTable(
|
|
855
|
+
console.log(usageTable(snapshot.rows, snapshot.totals, label, groupBy, snapshot.pct ? new Map(Object.entries(snapshot.pct)) : undefined));
|
|
856
856
|
}
|
|
857
857
|
}
|
|
858
858
|
async function cmdProviders(flags, json) {
|
|
859
859
|
const statuses = await providerStatuses({ noNet: flags.has("--no-net") });
|
|
860
|
-
const
|
|
860
|
+
const db = openDb();
|
|
861
861
|
try {
|
|
862
|
-
const local = localUsageByProvider(
|
|
862
|
+
const local = localUsageByProvider(db, Date.now() - parseDuration("7d"));
|
|
863
863
|
if (json) {
|
|
864
864
|
console.log(JSON.stringify({ statuses, local: Object.fromEntries(local) }, null, 2));
|
|
865
865
|
} else {
|
|
866
866
|
console.log(providersTable(statuses, local));
|
|
867
867
|
}
|
|
868
868
|
} finally {
|
|
869
|
-
|
|
869
|
+
db.close();
|
|
870
870
|
}
|
|
871
871
|
}
|
|
872
872
|
async function cmdTop(flags, json) {
|
package/dist/tui.js
CHANGED
|
@@ -102,13 +102,13 @@ function usageTotals(db, sinceMs) {
|
|
|
102
102
|
tokensOutput: Number(r?.tokensOutput ?? 0)
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
|
-
function monthlyUsageByProvider(db,
|
|
105
|
+
function monthlyUsageByProvider(db, startOfMonthMs) {
|
|
106
106
|
const rows = db.query(`SELECT COALESCE(json_extract(data,'$.providerID'),'?') AS provider,
|
|
107
107
|
COALESCE(SUM(CAST(json_extract(data,'$.cost') AS REAL)),0) AS cost
|
|
108
108
|
FROM message
|
|
109
109
|
WHERE json_extract(data,'$.role')='assistant'
|
|
110
110
|
AND CAST(json_extract(data,'$.time.created') AS INTEGER) >= ?
|
|
111
|
-
GROUP BY provider`).all(
|
|
111
|
+
GROUP BY provider`).all(startOfMonthMs);
|
|
112
112
|
const map = new Map;
|
|
113
113
|
for (const r of rows) {
|
|
114
114
|
map.set(String(r.provider), Number(r.cost));
|
|
@@ -548,8 +548,8 @@ function columnsFor(inner) {
|
|
|
548
548
|
{ title: "COST", width: 8, align: "right" }
|
|
549
549
|
];
|
|
550
550
|
const used = base.reduce((a, c) => a + c.width, 0) + base.length;
|
|
551
|
-
const
|
|
552
|
-
return { cols: [...base, { title: "BUDGET", width:
|
|
551
|
+
const budget = Math.max(14, inner - used);
|
|
552
|
+
return { cols: [...base, { title: "BUDGET", width: budget, align: "left" }], bar: wide ? WIDE_BAR : COMPACT_BAR };
|
|
553
553
|
}
|
|
554
554
|
function fmtTokens(n) {
|
|
555
555
|
if (n >= 1e6)
|
|
@@ -725,7 +725,7 @@ function Table(props) {
|
|
|
725
725
|
children: row(layout().cols, layout().cols.map((c) => c.title))
|
|
726
726
|
}),
|
|
727
727
|
props.snapshot.rows.slice(0, 20).map((r) => {
|
|
728
|
-
const
|
|
728
|
+
const budget = props.snapshot.pct?.[r.provider];
|
|
729
729
|
return /* @__PURE__ */ jsxs("box", {
|
|
730
730
|
flexDirection: "row",
|
|
731
731
|
children: [
|
|
@@ -734,9 +734,9 @@ function Table(props) {
|
|
|
734
734
|
wrapMode: "none",
|
|
735
735
|
children: lead(layout().cols, r)
|
|
736
736
|
}),
|
|
737
|
-
|
|
738
|
-
pct:
|
|
739
|
-
label:
|
|
737
|
+
budget ? /* @__PURE__ */ jsx(Budget, {
|
|
738
|
+
pct: budget.pct,
|
|
739
|
+
label: budget.label ?? budget.source,
|
|
740
740
|
palette: p,
|
|
741
741
|
col: layout().cols[layout().cols.length - 1],
|
|
742
742
|
bar: layout().bar
|
|
@@ -778,9 +778,9 @@ async function show(api, dialog) {
|
|
|
778
778
|
api,
|
|
779
779
|
text: "Loading usage\u2026"
|
|
780
780
|
}));
|
|
781
|
-
let
|
|
781
|
+
let snapshot;
|
|
782
782
|
try {
|
|
783
|
-
|
|
783
|
+
snapshot = await getUsageSnapshot(startOfDayMs(), "provider", true);
|
|
784
784
|
} catch (err) {
|
|
785
785
|
const message = err instanceof Error ? err.message : String(err);
|
|
786
786
|
dialog.replace(() => /* @__PURE__ */ jsx(Message, {
|
|
@@ -792,7 +792,7 @@ async function show(api, dialog) {
|
|
|
792
792
|
}
|
|
793
793
|
dialog.replace(() => /* @__PURE__ */ jsx(Table, {
|
|
794
794
|
api,
|
|
795
|
-
snapshot
|
|
795
|
+
snapshot
|
|
796
796
|
}));
|
|
797
797
|
}
|
|
798
798
|
var tui = async (api) => {
|
|
@@ -826,8 +826,8 @@ var tui = async (api) => {
|
|
|
826
826
|
};
|
|
827
827
|
var tui_default = { id: "opencode-usage", tui };
|
|
828
828
|
export {
|
|
829
|
-
|
|
830
|
-
tui_default as default,
|
|
829
|
+
Message,
|
|
831
830
|
Table,
|
|
832
|
-
|
|
831
|
+
tui_default as default,
|
|
832
|
+
tui
|
|
833
833
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1930dev/opencode-usage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Usage tracking, budget percentages and model ranking for opencode \u2014 CLI plus a /usage TUI plugin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "git+https://github.com/
|
|
18
|
+
"url": "git+https://github.com/1930-dev/opencode-usage.git"
|
|
19
19
|
},
|
|
20
20
|
"keywords": [
|
|
21
21
|
"opencode",
|
|
@@ -54,5 +54,9 @@
|
|
|
54
54
|
"@opencode-ai/plugin": ">=1.18",
|
|
55
55
|
"@opentui/core": ">=0.5",
|
|
56
56
|
"@opentui/solid": ">=0.5"
|
|
57
|
-
}
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/1930-dev/opencode-usage/issues"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/1930-dev/opencode-usage#readme"
|
|
58
62
|
}
|