@daoninc/opencode-copilot-usage 0.1.0 → 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/README.md +5 -2
- package/dist/source.js +30 -0
- package/dist/tui.js +224 -0
- package/dist/usage.js +46 -0
- package/package.json +4 -4
- package/source.ts +0 -26
- package/tui.tsx +0 -110
- package/usage.ts +0 -67
package/README.md
CHANGED
|
@@ -8,11 +8,11 @@ shows only the percentage beside the title.
|
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Add the fixed `0.1.1` release to the existing
|
|
12
12
|
`plugins` array in `~/.config/opencode/cli.json`:
|
|
13
13
|
|
|
14
14
|
```json
|
|
15
|
-
{ "plugins": ["@daoninc/opencode-copilot-usage@0.1.
|
|
15
|
+
{ "plugins": ["@daoninc/opencode-copilot-usage@0.1.1"] }
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
Requires OpenCode V2 and a locally installed, authenticated GitHub CLI:
|
|
@@ -39,6 +39,9 @@ OpenCode server, usage still belongs to the local GitHub CLI account.
|
|
|
39
39
|
- A zero GitHub quota-reset timestamp is ignored; the returned reset date is used.
|
|
40
40
|
- This is the account's plan meter, not a reconstruction from OpenCode token usage.
|
|
41
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
|
+
|
|
42
45
|
This package is TUI-only; configure it in `cli.json`, not `opencode.json`. Remove
|
|
43
46
|
the old local plugin entry before enabling the npm copy. It appends to the normal
|
|
44
47
|
sidebar slot; hot reload may affect its order relative to other contributions.
|
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,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daoninc/opencode-copilot-usage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Live GitHub Copilot plan usage and reset countdown in the OpenCode sidebar",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"exports": { "./tui": "./tui.
|
|
8
|
-
"files": ["
|
|
9
|
-
"scripts": { "test": "bun test", "prepublishOnly": "bun run ../../scripts/check-release.ts ." },
|
|
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
10
|
"dependencies": { "@opencode/plugin": "^2.3.1" },
|
|
11
11
|
"peerDependencies": { "@opentui/core": ">=0.8.4 <0.9", "@opentui/solid": ">=0.8.4 <0.9", "solid-js": "^1.9.0" },
|
|
12
12
|
"publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" },
|
package/source.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process"
|
|
2
|
-
import { promisify } from "node:util"
|
|
3
|
-
import { parseUsage } from "./usage"
|
|
4
|
-
|
|
5
|
-
const exec = promisify(execFile)
|
|
6
|
-
|
|
7
|
-
export async function fetchUsage(signal?: AbortSignal) {
|
|
8
|
-
let stdout: string
|
|
9
|
-
try {
|
|
10
|
-
const result = await exec("gh", ["api", "--hostname", "github.com", "/copilot_internal/user"], {
|
|
11
|
-
timeout: 15_000,
|
|
12
|
-
maxBuffer: 1_048_576,
|
|
13
|
-
encoding: "utf8",
|
|
14
|
-
signal,
|
|
15
|
-
env: { ...process.env, GH_PROMPT_DISABLED: "1" },
|
|
16
|
-
})
|
|
17
|
-
stdout = result.stdout
|
|
18
|
-
} catch (error) {
|
|
19
|
-
// Never surface raw CLI output: it can contain account or authentication details.
|
|
20
|
-
const code = (error as NodeJS.ErrnoException).code
|
|
21
|
-
throw new Error(code === "ENOENT" ? "Install GitHub CLI (gh) to load usage" : "Usage unavailable; check gh auth status")
|
|
22
|
-
}
|
|
23
|
-
let data: unknown
|
|
24
|
-
try { data = JSON.parse(stdout) } catch { throw new Error("GitHub returned an invalid usage response") }
|
|
25
|
-
return parseUsage(data)
|
|
26
|
-
}
|
package/tui.tsx
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/** @jsxImportSource @opentui/solid */
|
|
2
|
-
import { Plugin } from "@opencode/plugin/tui"
|
|
3
|
-
import { createSignal, Show } from "solid-js"
|
|
4
|
-
import { fetchUsage } from "./source"
|
|
5
|
-
import { daysToReset, progressBar, type Usage } from "./usage"
|
|
6
|
-
|
|
7
|
-
export default Plugin.define({
|
|
8
|
-
id: "local.copilot-usage",
|
|
9
|
-
setup(context) {
|
|
10
|
-
const [usage, setUsage] = createSignal<Usage>()
|
|
11
|
-
const [error, setError] = createSignal("")
|
|
12
|
-
const [loading, setLoading] = createSignal(false)
|
|
13
|
-
const [updated, setUpdated] = createSignal<Date>()
|
|
14
|
-
const [now, setNow] = createSignal(new Date())
|
|
15
|
-
const [section, updateSection] = context.storage.memory("copilot-usage-section", {
|
|
16
|
-
initial: { expanded: true },
|
|
17
|
-
})
|
|
18
|
-
const abort = new AbortController()
|
|
19
|
-
const format = (value: number | undefined) => value === undefined ? "—" : value.toLocaleString("en-US")
|
|
20
|
-
let disposed = false
|
|
21
|
-
let inFlight = false
|
|
22
|
-
|
|
23
|
-
async function refresh() {
|
|
24
|
-
if (disposed || inFlight) return
|
|
25
|
-
inFlight = true
|
|
26
|
-
setLoading(true)
|
|
27
|
-
try {
|
|
28
|
-
const next = await fetchUsage(abort.signal)
|
|
29
|
-
if (disposed) return
|
|
30
|
-
setUsage(next)
|
|
31
|
-
setError("")
|
|
32
|
-
setUpdated(new Date())
|
|
33
|
-
} catch (cause) {
|
|
34
|
-
if (!disposed) setError(cause instanceof Error ? cause.message : "Copilot usage unavailable")
|
|
35
|
-
} finally {
|
|
36
|
-
inFlight = false
|
|
37
|
-
if (!disposed) setLoading(false)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
context.keymap.layer(() => ({
|
|
42
|
-
commands: [{
|
|
43
|
-
id: "local.copilot-usage.refresh",
|
|
44
|
-
title: "Refresh Copilot usage",
|
|
45
|
-
group: "Copilot",
|
|
46
|
-
palette: true,
|
|
47
|
-
slash: { name: "copilot-usage" },
|
|
48
|
-
run: refresh,
|
|
49
|
-
}],
|
|
50
|
-
}))
|
|
51
|
-
|
|
52
|
-
const removeSlot = context.ui.slot({
|
|
53
|
-
append: "sidebar.content",
|
|
54
|
-
render: () => {
|
|
55
|
-
const days = () => daysToReset(usage()?.reset, now())
|
|
56
|
-
const percent = () => usage()?.percentUsed
|
|
57
|
-
return <box flexDirection="column" flexShrink={0} gap={0}>
|
|
58
|
-
<box flexDirection="row" justifyContent="space-between" gap={1}>
|
|
59
|
-
<text fg={context.theme.text.base} selectable={false} onMouseUp={(event) => {
|
|
60
|
-
if (event.button !== 0) return
|
|
61
|
-
event.stopPropagation()
|
|
62
|
-
updateSection((draft) => { draft.expanded = !draft.expanded })
|
|
63
|
-
}}>
|
|
64
|
-
<span style={{ fg: context.theme.text.muted }}>{section.expanded ? "▼ " : "▶ "}</span><b>Copilot usage</b>
|
|
65
|
-
</text>
|
|
66
|
-
<Show when={section.expanded} fallback={
|
|
67
|
-
<text fg={context.theme.text.base}>
|
|
68
|
-
{usage()?.unlimited ? "Unlimited" : percent() === undefined ? "—" : `${percent()!.toFixed(1)}%`}
|
|
69
|
-
</text>
|
|
70
|
-
}>
|
|
71
|
-
<text fg={context.theme.text.muted} selectable={false} onMouseUp={(event) => {
|
|
72
|
-
if (event.button !== 0) return
|
|
73
|
-
event.stopPropagation()
|
|
74
|
-
void refresh()
|
|
75
|
-
}}>{loading() ? "…" : "↻"}</text>
|
|
76
|
-
</Show>
|
|
77
|
-
</box>
|
|
78
|
-
<Show when={section.expanded}>
|
|
79
|
-
<Show when={usage()} fallback={<text fg={context.theme.text.muted} wrapMode="word">{error() || "Loading allowance…"}</text>}>
|
|
80
|
-
<text fg={context.theme.text.base}>
|
|
81
|
-
{usage()?.unlimited ? "Unlimited" : percent() === undefined ? "Usage available" : `${percent()!.toFixed(1)}% used`}
|
|
82
|
-
</text>
|
|
83
|
-
<Show when={!usage()?.unlimited}>
|
|
84
|
-
<text fg={context.theme.text.muted}>{progressBar(percent())}</text>
|
|
85
|
-
<text fg={context.theme.text.base} wrapMode="word">{format(usage()?.used)} / {format(usage()?.allowance)} {usage()?.unit}</text>
|
|
86
|
-
</Show>
|
|
87
|
-
<text fg={context.theme.text.base} wrapMode="word">
|
|
88
|
-
{days() === undefined ? "Reset date unavailable" : days() === 0 ? "Resets today · refresh pending" : `Resets in ${days()} ${days() === 1 ? "day" : "days"}`}
|
|
89
|
-
</text>
|
|
90
|
-
<text fg={context.theme.text.muted} wrapMode="word">
|
|
91
|
-
{error() ? `Stale · ${error()}` : `Updated ${updated()?.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) ?? "—"}`}
|
|
92
|
-
</text>
|
|
93
|
-
</Show>
|
|
94
|
-
</Show>
|
|
95
|
-
</box>
|
|
96
|
-
},
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
const poll = setInterval(() => { void refresh() }, 300_000)
|
|
100
|
-
const clock = setInterval(() => setNow(new Date()), 60_000)
|
|
101
|
-
void refresh()
|
|
102
|
-
return () => {
|
|
103
|
-
disposed = true
|
|
104
|
-
abort.abort()
|
|
105
|
-
clearInterval(poll)
|
|
106
|
-
clearInterval(clock)
|
|
107
|
-
removeSlot()
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
})
|
package/usage.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export type Usage = {
|
|
2
|
-
plan: string
|
|
3
|
-
unlimited: boolean
|
|
4
|
-
used?: number
|
|
5
|
-
remaining?: number
|
|
6
|
-
allowance?: number
|
|
7
|
-
percentUsed?: number
|
|
8
|
-
reset?: string
|
|
9
|
-
unit: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function record(value: unknown): Record<string, unknown> {
|
|
13
|
-
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
14
|
-
? value as Record<string, unknown>
|
|
15
|
-
: {}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function number(value: unknown): number | undefined {
|
|
19
|
-
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function resetDate(value: unknown): string | undefined {
|
|
23
|
-
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}(?:T.*)?$/.test(value)) return
|
|
24
|
-
return Number.isFinite(Date.parse(value)) ? value : undefined
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function parseUsage(value: unknown): Usage {
|
|
28
|
-
const data = record(value)
|
|
29
|
-
const quota = record(record(data.quota_snapshots).premium_interactions)
|
|
30
|
-
if (Object.keys(quota).length === 0) throw new Error("Copilot allowance unavailable for this account")
|
|
31
|
-
const allowance = number(quota.entitlement)
|
|
32
|
-
const remaining = number(quota.quota_remaining) ?? number(quota.remaining)
|
|
33
|
-
// Match GitHub's plan meter. credits_used can lag behind quota_remaining.
|
|
34
|
-
const used = allowance !== undefined && remaining !== undefined
|
|
35
|
-
? Math.max(0, allowance - remaining)
|
|
36
|
-
: number(quota.credits_used)
|
|
37
|
-
const percentRemaining = number(quota.percent_remaining)
|
|
38
|
-
const percentUsed = allowance && used !== undefined
|
|
39
|
-
? used / allowance * 100
|
|
40
|
-
: percentRemaining !== undefined ? Math.max(0, 100 - percentRemaining) : undefined
|
|
41
|
-
const resetAt = number(quota.quota_reset_at)
|
|
42
|
-
const reset = resetDate(data.quota_reset_date) ?? resetDate(data.quota_reset_date_utc) ??
|
|
43
|
-
(resetAt && resetAt < 8.64e12 ? new Date(resetAt * 1000).toISOString() : undefined)
|
|
44
|
-
const unlimited = quota.unlimited === true
|
|
45
|
-
if (!unlimited && used === undefined && percentUsed === undefined)
|
|
46
|
-
throw new Error("GitHub returned no Copilot usage figures")
|
|
47
|
-
return {
|
|
48
|
-
plan: typeof data.copilot_plan === "string" ? data.copilot_plan : "Copilot",
|
|
49
|
-
unlimited, used, remaining, allowance, percentUsed, reset,
|
|
50
|
-
unit: quota.token_based_billing === true ? "AI credits" : "premium requests",
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function daysToReset(reset: string | undefined, now = new Date()): number | undefined {
|
|
55
|
-
if (!reset) return
|
|
56
|
-
const date = resetDate(reset)
|
|
57
|
-
if (!date) return
|
|
58
|
-
// Compare calendar days, not 24-hour periods (DST and time of day must not add a day).
|
|
59
|
-
const target = new Date(date.length === 10 ? `${date}T00:00:00` : date)
|
|
60
|
-
const day = (d: Date) => Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())
|
|
61
|
-
return Math.max(0, Math.round((day(target) - day(now)) / 86_400_000))
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function progressBar(percent: number | undefined, width = 20): string {
|
|
65
|
-
const filled = Math.round(Math.max(0, Math.min(100, percent ?? 0)) / 100 * width)
|
|
66
|
-
return "━".repeat(filled) + "─".repeat(width - filled)
|
|
67
|
-
}
|