@1930dev/opencode-usage 0.1.3 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Agu Rodríguez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -3,6 +3,26 @@
3
3
  Usage tracking, budget percentages and model ranking for [opencode](https://opencode.ai):
4
4
  a CLI, and a `/usage` slash command for the TUI.
5
5
 
6
+ Every connected provider in one table, with each one's remaining budget as a
7
+ comparable percentage — whether the provider bills in tokens, requests, credits or
8
+ neurons.
9
+
10
+ <!-- Absolute URL so the image also renders on npm, which resolves nothing relative. -->
11
+ ![The /usage dialog](https://raw.githubusercontent.com/1930-dev/opencode-usage/main/docs/demo.gif)
12
+
13
+ [![npm](https://img.shields.io/npm/v/@1930dev/opencode-usage)](https://www.npmjs.com/package/@1930dev/opencode-usage)
14
+ [![license](https://img.shields.io/npm/l/@1930dev/opencode-usage)](./LICENSE)
15
+
16
+ ## Why
17
+
18
+ opencode talks to many providers at once, and each one meters differently: a weekly
19
+ window here, premium requests there, credits, neurons, tokens per day. Nothing tells
20
+ you which one you are about to exhaust.
21
+
22
+ This reads opencode's own SQLite and auth store — no configuration, no account — and
23
+ normalizes every quota into one percentage, so the answer to "which provider still has
24
+ room" is one glance.
25
+
6
26
  ## Install
7
27
 
8
28
  ```bash
@@ -45,7 +65,22 @@ opencode-usage top --limit 10 --json
45
65
  ## Plugin for opencode
46
66
 
47
67
  The package also ships a `/usage` slash command for the opencode TUI. It shows the
48
- same table as the CLI, with the % budget column drawn as a progress bar.
68
+ same table as the CLI, with the % budget column drawn as a progress bar:
69
+
70
+ ```
71
+ Usage — today esc
72
+
73
+ PROVIDER MSGS TOK IN TOK OUT COST BUDGET
74
+ opencode-go 7 0 0 $0.00 ████████████ 100% weekly
75
+ nvidia 98 10.9M 19.9K $0.00
76
+ cloudflare-workers-ai 0 0 0 $0.00
77
+ github-copilot 1 4.2K 830 $12.35 █████░░░░░░░ 42% 1M tok/day
78
+
79
+ TOTAL 106 $12.35
80
+ ```
81
+
82
+ The dialog sizes itself to the terminal: a wide frame spells the budget labels out,
83
+ a narrow one shortens them to the window (`wk`, `mo`, `d`).
49
84
 
50
85
  ```bash
51
86
  opencode plugin @1930dev/opencode-usage
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, startOfMonthMs2) {
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(startOfMonthMs2);
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 snapshot2 = await getUsageSnapshot(sinceMs, groupBy, withPct);
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: snapshot2.rows,
849
- totals: snapshot2.totals,
850
- pct: snapshot2.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(snapshot2.rows, snapshot2.totals, label, groupBy, snapshot2.pct ? new Map(Object.entries(snapshot2.pct)) : undefined));
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 db2 = openDb();
860
+ const db = openDb();
861
861
  try {
862
- const local = localUsageByProvider(db2, Date.now() - parseDuration("7d"));
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
- db2.close();
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, startOfMonthMs2) {
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(startOfMonthMs2);
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 budget2 = Math.max(14, inner - used);
552
- return { cols: [...base, { title: "BUDGET", width: budget2, align: "left" }], bar: wide ? WIDE_BAR : COMPACT_BAR };
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 budget2 = props.snapshot.pct?.[r.provider];
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
- budget2 ? /* @__PURE__ */ jsx(Budget, {
738
- pct: budget2.pct,
739
- label: budget2.label ?? budget2.source,
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 snapshot2;
781
+ let snapshot;
782
782
  try {
783
- snapshot2 = await getUsageSnapshot(startOfDayMs(), "provider", true);
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: snapshot2
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
- tui,
830
- tui_default as default,
829
+ Message,
831
830
  Table,
832
- Message
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",
3
+ "version": "0.1.5",
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": {
@@ -45,6 +45,7 @@
45
45
  },
46
46
  "files": [
47
47
  "dist",
48
+ "LICENSE",
48
49
  "README.md"
49
50
  ],
50
51
  "publishConfig": {