@daoninc/opencode-copilot-usage 0.0.0-stage → 0.1.1

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 Daon Inc.
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
@@ -1,3 +1,52 @@
1
- # Temporary Holding Version
1
+ # Copilot usage for OpenCode
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Live GitHub Copilot plan allowance in the right sidebar of **OpenCode V2**.
4
+ Shows percentage used, a progress bar, used/total AI credits (or premium requests
5
+ for older plans), and days until reset. It does not show remaining credits or
6
+ the calendar reset date. Click **▼ / ▶** to collapse/expand; the collapsed header
7
+ shows only the percentage beside the title.
8
+
9
+ ## Install
10
+
11
+ Add the fixed `0.1.1` release to the existing
12
+ `plugins` array in `~/.config/opencode/cli.json`:
13
+
14
+ ```json
15
+ { "plugins": ["@daoninc/opencode-copilot-usage@0.1.1"] }
16
+ ```
17
+
18
+ Requires OpenCode V2 and a locally installed, authenticated GitHub CLI:
19
+
20
+ ```sh
21
+ gh auth login --hostname github.com
22
+ ```
23
+
24
+ Uses the GitHub account selected by `gh` for github.com, which may differ from
25
+ OpenCode's Copilot login. It does not read or store authentication tokens and
26
+ does not collect session prompts or token-usage estimates. Against a remote
27
+ OpenCode server, usage still belongs to the local GitHub CLI account.
28
+
29
+ ## Refresh and limitations
30
+
31
+ - Refreshes on load and every five minutes. Click **↻**, run `/copilot-usage`, or
32
+ choose **Refresh Copilot usage** from the command palette to refresh manually.
33
+ - Shows an explicit unavailable state on failure. If a previous lookup succeeded,
34
+ it retains that snapshot with a **Stale** warning (visible when expanded).
35
+ - Reset countdown uses local calendar days. No reset date is guessed when GitHub
36
+ omits it. The collapse preference survives plugin reloads for the TUI lifetime.
37
+ - Reads `/copilot_internal/user` using `gh api`. This is an **undocumented GitHub
38
+ endpoint** and may change or become unavailable. No browser is required.
39
+ - A zero GitHub quota-reset timestamp is ignored; the returned reset date is used.
40
+ - This is the account's plan meter, not a reconstruction from OpenCode token usage.
41
+
42
+ Version `0.1.1` ships Solid-compiled JavaScript and uses the `daoninc.copilot-usage`
43
+ runtime ID. Do not use the raw-TSX `0.1.0` build with OpenCode 2.3.1.
44
+
45
+ This package is TUI-only; configure it in `cli.json`, not `opencode.json`. Remove
46
+ the old local plugin entry before enabling the npm copy. It appends to the normal
47
+ sidebar slot; hot reload may affect its order relative to other contributions.
48
+
49
+ For maintainers: run `bun test` in this directory, or the workspace's `bun run
50
+ check` for isolated tarball installation checks.
51
+
52
+ MIT licensed; see `LICENSE`.
package/dist/source.js ADDED
@@ -0,0 +1,30 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ import { parseUsage } from "./usage.js";
4
+ const exec = promisify(execFile);
5
+ export async function fetchUsage(signal) {
6
+ let stdout;
7
+ try {
8
+ const result = await exec("gh", ["api", "--hostname", "github.com", "/copilot_internal/user"], {
9
+ timeout: 15_000,
10
+ maxBuffer: 1_048_576,
11
+ encoding: "utf8",
12
+ signal,
13
+ env: {
14
+ ...process.env,
15
+ GH_PROMPT_DISABLED: "1"
16
+ }
17
+ });
18
+ stdout = result.stdout;
19
+ } catch (error) {
20
+ const code = error.code;
21
+ throw new Error(code === "ENOENT" ? "Install GitHub CLI (gh) to load usage" : "Usage unavailable; check gh auth status");
22
+ }
23
+ let data;
24
+ try {
25
+ data = JSON.parse(stdout);
26
+ } catch {
27
+ throw new Error("GitHub returned an invalid usage response");
28
+ }
29
+ return parseUsage(data);
30
+ }
package/dist/tui.js ADDED
@@ -0,0 +1,224 @@
1
+ import { createComponent as _$createComponent } from "@opentui/solid";
2
+ import { effect as _$effect } from "@opentui/solid";
3
+ import { createTextNode as _$createTextNode } from "@opentui/solid";
4
+ import { insertNode as _$insertNode } from "@opentui/solid";
5
+ import { insert as _$insert } from "@opentui/solid";
6
+ import { memo as _$memo } from "@opentui/solid";
7
+ import { setProp as _$setProp } from "@opentui/solid";
8
+ import { createElement as _$createElement } from "@opentui/solid";
9
+ import { Plugin } from "@opencode/plugin/tui";
10
+ import { createSignal, Show } from "solid-js";
11
+ import { fetchUsage } from "./source.js";
12
+ import { daysToReset, progressBar } from "./usage.js";
13
+ export default Plugin.define({
14
+ id: "daoninc.copilot-usage",
15
+ setup(context) {
16
+ const [usage, setUsage] = createSignal();
17
+ const [error, setError] = createSignal("");
18
+ const [loading, setLoading] = createSignal(false);
19
+ const [updated, setUpdated] = createSignal();
20
+ const [now, setNow] = createSignal(new Date());
21
+ const [section, updateSection] = context.storage.memory("copilot-usage-section", {
22
+ initial: {
23
+ expanded: true
24
+ }
25
+ });
26
+ const abort = new AbortController();
27
+ const format = value => value === undefined ? "—" : value.toLocaleString("en-US");
28
+ let disposed = false;
29
+ let inFlight = false;
30
+ async function refresh() {
31
+ if (disposed || inFlight) return;
32
+ inFlight = true;
33
+ setLoading(true);
34
+ try {
35
+ const next = await fetchUsage(abort.signal);
36
+ if (disposed) return;
37
+ setUsage(next);
38
+ setError("");
39
+ setUpdated(new Date());
40
+ } catch (cause) {
41
+ if (!disposed) setError(cause instanceof Error ? cause.message : "Copilot usage unavailable");
42
+ } finally {
43
+ inFlight = false;
44
+ if (!disposed) setLoading(false);
45
+ }
46
+ }
47
+ context.keymap.layer(() => ({
48
+ commands: [{
49
+ id: "daoninc.copilot-usage.refresh",
50
+ title: "Refresh Copilot usage",
51
+ group: "Copilot",
52
+ palette: true,
53
+ slash: {
54
+ name: "copilot-usage"
55
+ },
56
+ run: refresh
57
+ }]
58
+ }));
59
+ const removeSlot = context.ui.slot({
60
+ append: "sidebar.content",
61
+ render: () => {
62
+ const days = () => daysToReset(usage()?.reset, now());
63
+ const percent = () => usage()?.percentUsed;
64
+ return (() => {
65
+ var _el$ = _$createElement("box"),
66
+ _el$2 = _$createElement("box"),
67
+ _el$3 = _$createElement("text"),
68
+ _el$4 = _$createElement("span"),
69
+ _el$5 = _$createElement("b");
70
+ _$insertNode(_el$, _el$2);
71
+ _$setProp(_el$, "flexDirection", "column");
72
+ _$setProp(_el$, "flexShrink", 0);
73
+ _$setProp(_el$, "gap", 0);
74
+ _$insertNode(_el$2, _el$3);
75
+ _$setProp(_el$2, "flexDirection", "row");
76
+ _$setProp(_el$2, "justifyContent", "space-between");
77
+ _$setProp(_el$2, "gap", 1);
78
+ _$insertNode(_el$3, _el$4);
79
+ _$insertNode(_el$3, _el$5);
80
+ _$setProp(_el$3, "selectable", false);
81
+ _$setProp(_el$3, "onMouseUp", event => {
82
+ if (event.button !== 0) return;
83
+ event.stopPropagation();
84
+ updateSection(draft => {
85
+ draft.expanded = !draft.expanded;
86
+ });
87
+ });
88
+ _$insert(_el$4, () => section.expanded ? "▼ " : "▶ ");
89
+ _$insertNode(_el$5, _$createTextNode(`Copilot usage`));
90
+ _$insert(_el$2, _$createComponent(Show, {
91
+ get when() {
92
+ return section.expanded;
93
+ },
94
+ get fallback() {
95
+ return (() => {
96
+ var _el$13 = _$createElement("text");
97
+ _$insert(_el$13, (() => {
98
+ var _c$4 = _$memo(() => !!usage()?.unlimited);
99
+ return () => _c$4() ? "Unlimited" : _$memo(() => percent() === undefined)() ? "—" : `${percent().toFixed(1)}%`;
100
+ })());
101
+ _$effect(_$p => _$setProp(_el$13, "fg", context.theme.text.base, _$p));
102
+ return _el$13;
103
+ })();
104
+ },
105
+ get children() {
106
+ var _el$7 = _$createElement("text");
107
+ _$setProp(_el$7, "selectable", false);
108
+ _$setProp(_el$7, "onMouseUp", event => {
109
+ if (event.button !== 0) return;
110
+ event.stopPropagation();
111
+ void refresh();
112
+ });
113
+ _$insert(_el$7, () => loading() ? "…" : "↻");
114
+ _$effect(_$p => _$setProp(_el$7, "fg", context.theme.text.muted, _$p));
115
+ return _el$7;
116
+ }
117
+ }), null);
118
+ _$insert(_el$, _$createComponent(Show, {
119
+ get when() {
120
+ return section.expanded;
121
+ },
122
+ get children() {
123
+ return _$createComponent(Show, {
124
+ get when() {
125
+ return usage();
126
+ },
127
+ get fallback() {
128
+ return (() => {
129
+ var _el$14 = _$createElement("text");
130
+ _$setProp(_el$14, "wrapMode", "word");
131
+ _$insert(_el$14, () => error() || "Loading allowance…");
132
+ _$effect(_$p => _$setProp(_el$14, "fg", context.theme.text.muted, _$p));
133
+ return _el$14;
134
+ })();
135
+ },
136
+ get children() {
137
+ return [(() => {
138
+ var _el$8 = _$createElement("text");
139
+ _$insert(_el$8, (() => {
140
+ var _c$ = _$memo(() => !!usage()?.unlimited);
141
+ return () => _c$() ? "Unlimited" : _$memo(() => percent() === undefined)() ? "Usage available" : `${percent().toFixed(1)}% used`;
142
+ })());
143
+ _$effect(_$p => _$setProp(_el$8, "fg", context.theme.text.base, _$p));
144
+ return _el$8;
145
+ })(), _$createComponent(Show, {
146
+ get when() {
147
+ return !usage()?.unlimited;
148
+ },
149
+ get children() {
150
+ return [(() => {
151
+ var _el$9 = _$createElement("text");
152
+ _$insert(_el$9, () => progressBar(percent()));
153
+ _$effect(_$p => _$setProp(_el$9, "fg", context.theme.text.muted, _$p));
154
+ return _el$9;
155
+ })(), (() => {
156
+ var _el$0 = _$createElement("text"),
157
+ _el$1 = _$createTextNode(` / `),
158
+ _el$10 = _$createTextNode(` `);
159
+ _$insertNode(_el$0, _el$1);
160
+ _$insertNode(_el$0, _el$10);
161
+ _$setProp(_el$0, "wrapMode", "word");
162
+ _$insert(_el$0, () => format(usage()?.used), _el$1);
163
+ _$insert(_el$0, () => format(usage()?.allowance), _el$10);
164
+ _$insert(_el$0, () => usage()?.unit, null);
165
+ _$effect(_$p => _$setProp(_el$0, "fg", context.theme.text.base, _$p));
166
+ return _el$0;
167
+ })()];
168
+ }
169
+ }), (() => {
170
+ var _el$11 = _$createElement("text");
171
+ _$setProp(_el$11, "wrapMode", "word");
172
+ _$insert(_el$11, (() => {
173
+ var _c$2 = _$memo(() => days() === undefined);
174
+ return () => _c$2() ? "Reset date unavailable" : _$memo(() => days() === 0)() ? "Resets today · refresh pending" : `Resets in ${days()} ${days() === 1 ? "day" : "days"}`;
175
+ })());
176
+ _$effect(_$p => _$setProp(_el$11, "fg", context.theme.text.base, _$p));
177
+ return _el$11;
178
+ })(), (() => {
179
+ var _el$12 = _$createElement("text");
180
+ _$setProp(_el$12, "wrapMode", "word");
181
+ _$insert(_el$12, (() => {
182
+ var _c$3 = _$memo(() => !!error());
183
+ return () => _c$3() ? `Stale · ${error()}` : `Updated ${updated()?.toLocaleTimeString([], {
184
+ hour: "2-digit",
185
+ minute: "2-digit"
186
+ }) ?? "—"}`;
187
+ })());
188
+ _$effect(_$p => _$setProp(_el$12, "fg", context.theme.text.muted, _$p));
189
+ return _el$12;
190
+ })()];
191
+ }
192
+ });
193
+ }
194
+ }), null);
195
+ _$effect(_p$ => {
196
+ var _v$ = context.theme.text.base,
197
+ _v$2 = {
198
+ fg: context.theme.text.muted
199
+ };
200
+ _v$ !== _p$.e && (_p$.e = _$setProp(_el$3, "fg", _v$, _p$.e));
201
+ _v$2 !== _p$.t && (_p$.t = _$setProp(_el$4, "style", _v$2, _p$.t));
202
+ return _p$;
203
+ }, {
204
+ e: undefined,
205
+ t: undefined
206
+ });
207
+ return _el$;
208
+ })();
209
+ }
210
+ });
211
+ const poll = setInterval(() => {
212
+ void refresh();
213
+ }, 300_000);
214
+ const clock = setInterval(() => setNow(new Date()), 60_000);
215
+ void refresh();
216
+ return () => {
217
+ disposed = true;
218
+ abort.abort();
219
+ clearInterval(poll);
220
+ clearInterval(clock);
221
+ removeSlot();
222
+ };
223
+ }
224
+ });
package/dist/usage.js ADDED
@@ -0,0 +1,46 @@
1
+ function record(value) {
2
+ return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
3
+ }
4
+ function number(value) {
5
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
6
+ }
7
+ function resetDate(value) {
8
+ if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}(?:T.*)?$/.test(value)) return;
9
+ return Number.isFinite(Date.parse(value)) ? value : undefined;
10
+ }
11
+ export function parseUsage(value) {
12
+ const data = record(value);
13
+ const quota = record(record(data.quota_snapshots).premium_interactions);
14
+ if (Object.keys(quota).length === 0) throw new Error("Copilot allowance unavailable for this account");
15
+ const allowance = number(quota.entitlement);
16
+ const remaining = number(quota.quota_remaining) ?? number(quota.remaining);
17
+ const used = allowance !== undefined && remaining !== undefined ? Math.max(0, allowance - remaining) : number(quota.credits_used);
18
+ const percentRemaining = number(quota.percent_remaining);
19
+ const percentUsed = allowance && used !== undefined ? used / allowance * 100 : percentRemaining !== undefined ? Math.max(0, 100 - percentRemaining) : undefined;
20
+ const resetAt = number(quota.quota_reset_at);
21
+ const reset = resetDate(data.quota_reset_date) ?? resetDate(data.quota_reset_date_utc) ?? (resetAt && resetAt < 8.64e12 ? new Date(resetAt * 1000).toISOString() : undefined);
22
+ const unlimited = quota.unlimited === true;
23
+ if (!unlimited && used === undefined && percentUsed === undefined) throw new Error("GitHub returned no Copilot usage figures");
24
+ return {
25
+ plan: typeof data.copilot_plan === "string" ? data.copilot_plan : "Copilot",
26
+ unlimited,
27
+ used,
28
+ remaining,
29
+ allowance,
30
+ percentUsed,
31
+ reset,
32
+ unit: quota.token_based_billing === true ? "AI credits" : "premium requests"
33
+ };
34
+ }
35
+ export function daysToReset(reset, now = new Date()) {
36
+ if (!reset) return;
37
+ const date = resetDate(reset);
38
+ if (!date) return;
39
+ const target = new Date(date.length === 10 ? `${date}T00:00:00` : date);
40
+ const day = d => Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
41
+ return Math.max(0, Math.round((day(target) - day(now)) / 86_400_000));
42
+ }
43
+ export function progressBar(percent, width = 20) {
44
+ const filled = Math.round(Math.max(0, Math.min(100, percent ?? 0)) / 100 * width);
45
+ return "━".repeat(filled) + "─".repeat(width - filled);
46
+ }
package/package.json CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@daoninc/opencode-copilot-usage",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Live GitHub Copilot plan usage and reset countdown in the OpenCode sidebar",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": { "./tui": "./dist/tui.js" },
8
+ "files": ["dist", "README.md", "LICENSE"],
9
+ "scripts": { "test": "bun test", "prepack": "bun run ../../scripts/build.ts .", "prepublishOnly": "bun run ../../scripts/build.ts . && bun run ../../scripts/check-release.ts ." },
10
+ "dependencies": { "@opencode/plugin": "^2.3.1" },
11
+ "peerDependencies": { "@opentui/core": ">=0.8.4 <0.9", "@opentui/solid": ">=0.8.4 <0.9", "solid-js": "^1.9.0" },
12
+ "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" },
13
+ "keywords": ["opencode", "opencode-plugin", "tui", "github-copilot"],
14
+ "repository": { "type": "git", "url": "git+https://github.com/daon-private/sdk-tools.git", "directory": "AI/opencode/plugins/copilot-usage" }
15
+ }