opencode-glm-plugin 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +98 -2
- package/dist/gui-experimental.cjs +1 -0
- package/package.json +54 -4
- package/src/gui/bigmodel.ts +328 -0
- package/src/gui/contract.ts +17 -0
- package/src/gui/definition.ts +11 -0
- package/src/gui/i18n/en.ts +41 -0
- package/src/gui/i18n/zh.ts +39 -0
- package/src/gui/main.ts +36 -0
- package/src/gui/panel.css +331 -0
- package/src/gui/panel.tsx +318 -0
- package/src/gui/state.ts +47 -0
- package/src/gui/view.ts +78 -0
- package/src/gui/window.tsx +93 -0
- package/src/gui.tsx +10 -0
- package/src/index.ts +10 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { For, Show, createSignal, onCleanup, onMount } from "solid-js"
|
|
2
|
+
import { usePlugin } from "@opencode/plugin/gui"
|
|
3
|
+
import type { definition } from "./definition"
|
|
4
|
+
import type { QuotaLimit, UsageBucket, UsageRange } from "./bigmodel"
|
|
5
|
+
import { quotaState } from "./state"
|
|
6
|
+
import { PANEL_WIDTH, formatCountdown, formatTokens, isLimited, tierColor, tierOf } from "./view"
|
|
7
|
+
|
|
8
|
+
/** The right-side "GLM 额度" panel: plan limits, limit status, 7/30-day usage. */
|
|
9
|
+
|
|
10
|
+
const STALE_MS = 60_000
|
|
11
|
+
|
|
12
|
+
export function QuotaPanel() {
|
|
13
|
+
const ctx = usePlugin<typeof definition>()
|
|
14
|
+
const zh = () => ctx.locale.locale().startsWith("zh")
|
|
15
|
+
|
|
16
|
+
/** Pin the compact width; the host's stored value (e.g. recorded from a drag)
|
|
17
|
+
* wins over `Panel.width`, so correct it whenever it is missing or larger. */
|
|
18
|
+
const ensureWidth = () => {
|
|
19
|
+
const stored = ctx.layout.side.width("quota")
|
|
20
|
+
if (stored === undefined || stored > PANEL_WIDTH) ctx.layout.side.resize("quota", PANEL_WIDTH)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Ticks every second so reset countdowns stay live while the panel shows.
|
|
24
|
+
const [now, setNow] = createSignal(Date.now())
|
|
25
|
+
onMount(() => {
|
|
26
|
+
const tick = setInterval(() => setNow(Date.now()), 1000)
|
|
27
|
+
onCleanup(() => clearInterval(tick))
|
|
28
|
+
|
|
29
|
+
ensureWidth()
|
|
30
|
+
|
|
31
|
+
// A snapshot opened after a while on screen refreshes right away.
|
|
32
|
+
const snap = quotaState.snapshot()
|
|
33
|
+
if (!snap || Date.now() - snap.fetchedAt > STALE_MS) quotaState.pull(true)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const snapshot = () => quotaState.snapshot()
|
|
37
|
+
const limited = () => isLimited(snapshot())
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<div class="glmq">
|
|
41
|
+
<header class="glmq-head">
|
|
42
|
+
<span class="glmq-title">{ctx.t("panel.title")}</span>
|
|
43
|
+
<Show when={snapshot()?.level}>
|
|
44
|
+
<span class="glmq-level">{snapshot()!.level}</span>
|
|
45
|
+
</Show>
|
|
46
|
+
<Show when={limited()}>
|
|
47
|
+
<span class="glmq-flag">{ctx.t("state.limited")}</span>
|
|
48
|
+
</Show>
|
|
49
|
+
<button
|
|
50
|
+
type="button"
|
|
51
|
+
class="glmq-refresh"
|
|
52
|
+
title={ctx.t("panel.refresh")}
|
|
53
|
+
aria-label={ctx.t("panel.refresh")}
|
|
54
|
+
disabled={quotaState.busy()}
|
|
55
|
+
onClick={() => quotaState.pull(true)}
|
|
56
|
+
>
|
|
57
|
+
<ctx.ui.Icon name="refresh" size="small" />
|
|
58
|
+
</button>
|
|
59
|
+
</header>
|
|
60
|
+
|
|
61
|
+
<Show when={snapshot()} fallback={<StatusCard kind="loading" />}>
|
|
62
|
+
{(snap) => (
|
|
63
|
+
<Show when={snap().status === "ok"} fallback={<StatusCard kind={snap().status} message={snap().message} />}>
|
|
64
|
+
<div class="glmq-body">
|
|
65
|
+
<section class="glmq-card">
|
|
66
|
+
<div class="glmq-card-title">{ctx.t("section.quota")}</div>
|
|
67
|
+
<For each={snap().limits}>{(limit) => <LimitRow limit={limit} now={now()} zh={zh()} />}</For>
|
|
68
|
+
</section>
|
|
69
|
+
|
|
70
|
+
<UsageSection
|
|
71
|
+
title={ctx.t("section.today")}
|
|
72
|
+
range={snap().usageToday}
|
|
73
|
+
zh={zh()}
|
|
74
|
+
chart={{ minSpan: 24, hourTicks: true }}
|
|
75
|
+
/>
|
|
76
|
+
<UsageSection title={ctx.t("section.7d")} range={snap().usage7d} zh={zh()} />
|
|
77
|
+
<UsageSection title={ctx.t("section.30d")} range={snap().usage30d} zh={zh()} />
|
|
78
|
+
|
|
79
|
+
<footer class="glmq-foot">
|
|
80
|
+
{ctx.t("foot.updated", { time: formatClock(snap().fetchedAt) })} · {ctx.t("foot.auto")}
|
|
81
|
+
</footer>
|
|
82
|
+
</div>
|
|
83
|
+
</Show>
|
|
84
|
+
)}
|
|
85
|
+
</Show>
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function StatusCard(props: { kind: string; message?: string }) {
|
|
91
|
+
const ctx = usePlugin<typeof definition>()
|
|
92
|
+
const hint = () => {
|
|
93
|
+
if (props.kind === "no-key") return ctx.t("state.noKeyHint")
|
|
94
|
+
if (props.kind === "empty") return ctx.t("state.emptyHint")
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
const text = () => {
|
|
98
|
+
if (props.kind === "loading") return ctx.t("state.loading")
|
|
99
|
+
if (props.kind === "no-key") return ctx.t("state.noKey")
|
|
100
|
+
if (props.kind === "empty") return ctx.t("state.empty")
|
|
101
|
+
return ctx.t("state.error")
|
|
102
|
+
}
|
|
103
|
+
return (
|
|
104
|
+
<div class="glmq-card">
|
|
105
|
+
<div class="glmq-status" classList={{ "glmq-status-error": props.kind === "error" }}>
|
|
106
|
+
<div class="glmq-status-title">{text()}</div>
|
|
107
|
+
<Show when={props.message}>
|
|
108
|
+
<div class="glmq-status-detail">{props.message}</div>
|
|
109
|
+
</Show>
|
|
110
|
+
<Show when={hint()}>
|
|
111
|
+
<div class="glmq-status-detail">{hint()}</div>
|
|
112
|
+
</Show>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function LimitRow(props: { limit: QuotaLimit; now: number; zh: boolean }) {
|
|
119
|
+
const ctx = usePlugin<typeof definition>()
|
|
120
|
+
const pct = () => props.limit.percentage
|
|
121
|
+
const tier = () => tierOf(pct())
|
|
122
|
+
const color = () => tierColor(tier()) ?? "var(--v2-icon-icon-accent)"
|
|
123
|
+
const reset = () => {
|
|
124
|
+
const at = props.limit.nextResetTime
|
|
125
|
+
if (!at) return undefined
|
|
126
|
+
return ctx.t("state.resetIn", { time: formatCountdown(at - props.now, props.zh) })
|
|
127
|
+
}
|
|
128
|
+
const amounts = () => {
|
|
129
|
+
const used = props.limit.currentValue
|
|
130
|
+
const total = props.limit.usage
|
|
131
|
+
const left = props.limit.remaining
|
|
132
|
+
const parts: string[] = []
|
|
133
|
+
if (used !== undefined && total !== undefined && total > 0)
|
|
134
|
+
parts.push(`${ctx.t("state.used")} ${formatTokens(used, props.zh)} / ${formatTokens(total, props.zh)}`)
|
|
135
|
+
else if (left !== undefined && total !== undefined && total > 0)
|
|
136
|
+
parts.push(`${ctx.t("state.remaining")} ${formatTokens(left, props.zh)}`)
|
|
137
|
+
else if (pct() !== undefined) parts.push(ctx.t("state.freePct", { pct: 100 - Math.round(pct()!) }))
|
|
138
|
+
return parts.join(" · ")
|
|
139
|
+
}
|
|
140
|
+
const sub = amounts
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<div class="glmq-limit">
|
|
144
|
+
<div class="glmq-limit-head">
|
|
145
|
+
<span class="glmq-limit-name">{ctx.t(`limit.${props.limit.kind}`)}</span>
|
|
146
|
+
<span class="glmq-limit-pct" style={{ color: color() }}>
|
|
147
|
+
{pct() !== undefined ? ctx.t("state.usedPct", { pct: Math.round(pct()!) }) : "—"}
|
|
148
|
+
</span>
|
|
149
|
+
</div>
|
|
150
|
+
<div class="glmq-bar" role="progressbar" aria-valuenow={pct() !== undefined ? Math.round(pct()!) : undefined}>
|
|
151
|
+
<div
|
|
152
|
+
class="glmq-fill"
|
|
153
|
+
style={{ width: `${Math.min(100, Math.max(0, pct() ?? 0))}%`, background: color() }}
|
|
154
|
+
/>
|
|
155
|
+
</div>
|
|
156
|
+
<div class="glmq-sub">
|
|
157
|
+
<span>{sub() || " "}</span>
|
|
158
|
+
<Show when={reset()}>
|
|
159
|
+
<span>{reset()}</span>
|
|
160
|
+
</Show>
|
|
161
|
+
</div>
|
|
162
|
+
</div>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function UsageSection(props: {
|
|
167
|
+
title: string
|
|
168
|
+
range: UsageRange | undefined
|
|
169
|
+
zh: boolean
|
|
170
|
+
chart?: { minSpan?: number; hourTicks?: boolean }
|
|
171
|
+
}) {
|
|
172
|
+
const ctx = usePlugin<typeof definition>()
|
|
173
|
+
const has = () => !!props.range && (props.range.totalTokens > 0 || props.range.totalCalls > 0)
|
|
174
|
+
const byDay = () => props.range?.granularity === "daily"
|
|
175
|
+
return (
|
|
176
|
+
<section class="glmq-card">
|
|
177
|
+
<div class="glmq-card-row">
|
|
178
|
+
<div class="glmq-card-title">{props.title}</div>
|
|
179
|
+
<Show when={has()}>
|
|
180
|
+
<span class="glmq-gran">{byDay() ? ctx.t("chart.byDay") : ctx.t("chart.byHour")}</span>
|
|
181
|
+
</Show>
|
|
182
|
+
</div>
|
|
183
|
+
<Show
|
|
184
|
+
when={has()}
|
|
185
|
+
fallback={<div class="glmq-none">{ctx.t("usage.empty")}</div>}
|
|
186
|
+
>
|
|
187
|
+
<div class="glmq-totals">
|
|
188
|
+
<div class="glmq-total">
|
|
189
|
+
<b>{formatTokens(props.range!.totalTokens, props.zh)}</b>
|
|
190
|
+
<span>{ctx.t("usage.tokens")}</span>
|
|
191
|
+
</div>
|
|
192
|
+
<div class="glmq-total">
|
|
193
|
+
<b>{formatCount(props.range!.totalCalls)}</b>
|
|
194
|
+
<span>{ctx.t("usage.calls")}</span>
|
|
195
|
+
</div>
|
|
196
|
+
</div>
|
|
197
|
+
<Chart buckets={props.range!.buckets} zh={props.zh} span={props.chart} />
|
|
198
|
+
<Show when={props.range!.models.length > 0}>
|
|
199
|
+
<div class="glmq-models">
|
|
200
|
+
<div class="glmq-models-title">{ctx.t("usage.models")}</div>
|
|
201
|
+
<For each={props.range!.models.slice(0, 5)}>
|
|
202
|
+
{(model) => (
|
|
203
|
+
<ModelRow name={model.name} tokens={model.tokens} total={props.range!.totalTokens} zh={props.zh} />
|
|
204
|
+
)}
|
|
205
|
+
</For>
|
|
206
|
+
</div>
|
|
207
|
+
</Show>
|
|
208
|
+
</Show>
|
|
209
|
+
</section>
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Bars with axes: horizontal gridlines carry y tick labels (peak, mid, 0), the
|
|
215
|
+
* row under the plot carries x time ticks, so the magnitude is readable.
|
|
216
|
+
*/
|
|
217
|
+
function Chart(props: {
|
|
218
|
+
buckets: readonly UsageBucket[]
|
|
219
|
+
zh: boolean
|
|
220
|
+
span?: { minSpan?: number; hourTicks?: boolean }
|
|
221
|
+
}) {
|
|
222
|
+
const count = () => props.buckets.length
|
|
223
|
+
const max = () => Math.max(1, ...props.buckets.map((bucket) => bucket.tokens))
|
|
224
|
+
const viewWidth = () => Math.max(count(), props.span?.minSpan ?? 1)
|
|
225
|
+
|
|
226
|
+
const yTicks = () => [
|
|
227
|
+
{ top: "0%", label: formatTokens(max(), props.zh) },
|
|
228
|
+
{ top: "50%", label: formatTokens(max() / 2, props.zh) },
|
|
229
|
+
{ top: "100%", label: "0" },
|
|
230
|
+
]
|
|
231
|
+
|
|
232
|
+
const xTicks = () => {
|
|
233
|
+
const n = count()
|
|
234
|
+
if (n === 0) return []
|
|
235
|
+
const positions =
|
|
236
|
+
n < 8 ? (n === 1 ? [0] : [0, n - 1]) : [0, Math.round((n - 1) / 3), Math.round(((n - 1) * 2) / 3), n - 1]
|
|
237
|
+
return [...new Set(positions)].map((index) => ({
|
|
238
|
+
index,
|
|
239
|
+
label: tickLabel(props.buckets[index]!.time, props.span?.hourTicks),
|
|
240
|
+
}))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<div class="glmq-chart">
|
|
245
|
+
<div class="glmq-plot">
|
|
246
|
+
<div class="glmq-gutter">
|
|
247
|
+
<For each={yTicks()}>
|
|
248
|
+
{(tick) => (
|
|
249
|
+
<span class="glmq-ylabel" style={{ top: tick.top }}>
|
|
250
|
+
{tick.label}
|
|
251
|
+
</span>
|
|
252
|
+
)}
|
|
253
|
+
</For>
|
|
254
|
+
</div>
|
|
255
|
+
<div class="glmq-area">
|
|
256
|
+
<For each={yTicks()}>
|
|
257
|
+
{(tick) => <span class="glmq-gridline" style={{ top: tick.top }} />}
|
|
258
|
+
</For>
|
|
259
|
+
<svg class="glmq-bars" viewBox={`0 0 ${viewWidth()} 100`} preserveAspectRatio="none" aria-hidden="true">
|
|
260
|
+
<For each={props.buckets}>
|
|
261
|
+
{(bucket, index) => {
|
|
262
|
+
const height = () => Math.max(bucket.tokens > 0 ? 1.5 : 0, (bucket.tokens / max()) * 97)
|
|
263
|
+
return (
|
|
264
|
+
<rect x={index() + 0.09} y={100 - height()} width={0.82} height={height()} rx={0.25} fill="currentColor">
|
|
265
|
+
<title>{`${bucket.time} · ${formatTokens(bucket.tokens, props.zh)}`}</title>
|
|
266
|
+
</rect>
|
|
267
|
+
)
|
|
268
|
+
}}
|
|
269
|
+
</For>
|
|
270
|
+
</svg>
|
|
271
|
+
</div>
|
|
272
|
+
</div>
|
|
273
|
+
<div class="glmq-xlabels">
|
|
274
|
+
<For each={xTicks()}>{(tick) => <span>{tick.label}</span>}</For>
|
|
275
|
+
</div>
|
|
276
|
+
</div>
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Hour ticks read `"2026-11-22 13:00"` as `"13:00"`; day ticks as `"11/22"`. */
|
|
281
|
+
function tickLabel(time: string, hourTicks: boolean | undefined): string {
|
|
282
|
+
if (hourTicks) {
|
|
283
|
+
const hour = time.slice(11, 16)
|
|
284
|
+
return hour || shortDate(time)
|
|
285
|
+
}
|
|
286
|
+
return shortDate(time)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function ModelRow(props: { name: string; tokens: number; total: number; zh: boolean }) {
|
|
290
|
+
const fill = () => (props.total > 0 ? Math.max(2, (props.tokens / props.total) * 100) : 0)
|
|
291
|
+
return (
|
|
292
|
+
<div class="glmq-model">
|
|
293
|
+
<div class="glmq-model-head">
|
|
294
|
+
<span class="glmq-model-name" title={props.name}>
|
|
295
|
+
{props.name}
|
|
296
|
+
</span>
|
|
297
|
+
<span class="glmq-model-tokens">{formatTokens(props.tokens, props.zh)}</span>
|
|
298
|
+
</div>
|
|
299
|
+
<span class="glmq-model-bar">
|
|
300
|
+
<span class="glmq-model-fill" style={{ width: `${fill()}%` }} />
|
|
301
|
+
</span>
|
|
302
|
+
</div>
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** `"2026-11-16 00:00"` → `"11/16"`. */
|
|
307
|
+
function shortDate(time: string): string {
|
|
308
|
+
const day = time.slice(5, 10)
|
|
309
|
+
return day.includes("-") ? day.replace("-", "/") : day
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function formatCount(value: number): string {
|
|
313
|
+
return new Intl.NumberFormat(undefined, { notation: value >= 100000 ? "compact" : "standard" }).format(value)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function formatClock(at: number): string {
|
|
317
|
+
return new Date(at).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" })
|
|
318
|
+
}
|
package/src/gui/state.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createSignal } from "solid-js"
|
|
2
|
+
import type { Snapshot } from "./bigmodel"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The window-side snapshot store, shared by the panel, the + menu item and the
|
|
6
|
+
* titlebar pill. Module-scope Solid signals: one store per desktop window.
|
|
7
|
+
* The main process evaluates this module too, but only window code touches it —
|
|
8
|
+
* there solid-js is an inert placeholder, and these calls stay harmless.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const [snapshot, setSnapshot] = createSignal<Snapshot | undefined>(undefined)
|
|
12
|
+
const [busy, setBusy] = createSignal(false)
|
|
13
|
+
|
|
14
|
+
let client: { snapshot(): Promise<unknown>; refresh(): Promise<unknown> } | undefined
|
|
15
|
+
let timer: ReturnType<typeof setInterval> | undefined
|
|
16
|
+
|
|
17
|
+
export const quotaState = {
|
|
18
|
+
snapshot,
|
|
19
|
+
busy,
|
|
20
|
+
|
|
21
|
+
/** Wires the store to the Ipc client; call once from the window setup. */
|
|
22
|
+
attach(next: { snapshot(): Promise<unknown>; refresh(): Promise<unknown> }) {
|
|
23
|
+
client = next
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
/** Fetches through main; `force` bypasses the throttle in main. */
|
|
27
|
+
pull(force: boolean) {
|
|
28
|
+
if (!client || busy()) return
|
|
29
|
+
setBusy(true)
|
|
30
|
+
const call = force ? client.refresh() : client.snapshot()
|
|
31
|
+
call
|
|
32
|
+
.then((value) => setSnapshot(value as Snapshot))
|
|
33
|
+
.catch(() => {})
|
|
34
|
+
.finally(() => setBusy(false))
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
/** Initial pull plus the auto-refresh loop. */
|
|
38
|
+
start(intervalMs: number) {
|
|
39
|
+
this.pull(false)
|
|
40
|
+
if (!timer) timer = setInterval(() => this.pull(false), intervalMs)
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
stop() {
|
|
44
|
+
if (timer) clearInterval(timer)
|
|
45
|
+
timer = undefined
|
|
46
|
+
},
|
|
47
|
+
}
|
package/src/gui/view.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { QuotaLimit, Snapshot, UsageRange } from "./bigmodel"
|
|
2
|
+
|
|
3
|
+
/** Pure view helpers shared by the window shell and the panel. */
|
|
4
|
+
|
|
5
|
+
/** The panel's compact width, in CSS pixels. */
|
|
6
|
+
export const PANEL_WIDTH = 340
|
|
7
|
+
|
|
8
|
+
/** The tier of a used percentage, for colors and wording. */
|
|
9
|
+
export type Tier = "ok" | "warn" | "danger"
|
|
10
|
+
|
|
11
|
+
export function tierOf(percentage: number | undefined): Tier | undefined {
|
|
12
|
+
if (percentage === undefined) return undefined
|
|
13
|
+
if (percentage >= 95) return "danger"
|
|
14
|
+
if (percentage >= 80) return "warn"
|
|
15
|
+
return "ok"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** CSS color for a tier, from the app's own status tokens. */
|
|
19
|
+
export function tierColor(tier: Tier | undefined): string | undefined {
|
|
20
|
+
if (tier === "danger") return "var(--icon-critical-base, #e5484d)"
|
|
21
|
+
if (tier === "warn") return "var(--icon-warning-base, #f5a524)"
|
|
22
|
+
if (tier === "ok") return "var(--icon-success-base, #46a758)"
|
|
23
|
+
return undefined
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The headline percentage: the first limit that reports one, preferring the
|
|
28
|
+
* 5-hour token window, then credit windows, then anything else.
|
|
29
|
+
*/
|
|
30
|
+
export function primaryPercentage(snapshot: Snapshot | undefined): number | undefined {
|
|
31
|
+
if (!snapshot || snapshot.status !== "ok") return undefined
|
|
32
|
+
const order: QuotaLimit["kind"][] = ["tokens-5h", "credit-5h", "credit-week", "credit-month", "time-month", "other"]
|
|
33
|
+
for (const kind of order) {
|
|
34
|
+
const limit = snapshot.limits.find((entry) => entry.kind === kind)
|
|
35
|
+
if (limit?.percentage !== undefined) return limit.percentage
|
|
36
|
+
}
|
|
37
|
+
return snapshot.limits.find((limit) => limit.percentage !== undefined)?.percentage
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether any limit has run out. */
|
|
41
|
+
export function isLimited(snapshot: Snapshot | undefined): boolean {
|
|
42
|
+
if (!snapshot) return false
|
|
43
|
+
return snapshot.limits.some((limit) => (limit.percentage ?? 0) >= 100)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Humanizes token counts per locale: 万/亿 for zh, K/M/B otherwise. */
|
|
47
|
+
export function formatTokens(value: number, zh: boolean): string {
|
|
48
|
+
if (!Number.isFinite(value)) return "0"
|
|
49
|
+
if (zh) {
|
|
50
|
+
if (value >= 1e8) return `${strip(value / 1e8)} 亿`
|
|
51
|
+
if (value >= 1e4) return `${strip(value / 1e4)} 万`
|
|
52
|
+
return String(Math.round(value))
|
|
53
|
+
}
|
|
54
|
+
if (value >= 1e9) return `${strip(value / 1e9)}B`
|
|
55
|
+
if (value >= 1e6) return `${strip(value / 1e6)}M`
|
|
56
|
+
if (value >= 1e3) return `${strip(value / 1e3)}K`
|
|
57
|
+
return String(Math.round(value))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function strip(value: number): string {
|
|
61
|
+
return value >= 100 ? String(Math.round(value)) : value.toFixed(1).replace(/\.0$/, "")
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Humanizes a duration for the reset countdown, in whole units. */
|
|
65
|
+
export function formatCountdown(ms: number, zh: boolean): string {
|
|
66
|
+
if (ms <= 0) return zh ? "即将重置" : "any moment"
|
|
67
|
+
const minutes = Math.round(ms / 60_000)
|
|
68
|
+
if (minutes < 60) return zh ? `${Math.max(1, minutes)} 分钟` : `${Math.max(1, minutes)} min`
|
|
69
|
+
const hours = Math.round(minutes / 60)
|
|
70
|
+
if (hours < 48) return zh ? `${hours} 小时` : `${hours} h`
|
|
71
|
+
const days = Math.round(hours / 24)
|
|
72
|
+
return zh ? `${days} 天` : `${days} d`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The chart buckets of a range, already downsampled by main. */
|
|
76
|
+
export function bucketsOf(range: UsageRange | undefined): UsageRange["buckets"] {
|
|
77
|
+
return range?.buckets ?? []
|
|
78
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { onCleanup } from "solid-js"
|
|
2
|
+
import { Panel, Style, TitlebarItem, MenuItem, type PanelTab, type Plugin } from "@opencode/plugin/gui"
|
|
3
|
+
import type { definition } from "./definition"
|
|
4
|
+
import { GlmQuota } from "./contract"
|
|
5
|
+
import { quotaState } from "./state"
|
|
6
|
+
import { QuotaPanel } from "./panel"
|
|
7
|
+
import { PANEL_WIDTH, primaryPercentage } from "./view"
|
|
8
|
+
import css from "./panel.css?inline"
|
|
9
|
+
|
|
10
|
+
/** The window half: the side panel, the + menu item and the titlebar pill. */
|
|
11
|
+
|
|
12
|
+
const REFRESH_MS = 5 * 60_000
|
|
13
|
+
const TAB_ID = "quota"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The panel keeps a compact width. A stored width wins in the host — including
|
|
17
|
+
* one the host recorded for us while the divider was dragged — so correct any
|
|
18
|
+
* missing or larger value; a user's narrower drag stays.
|
|
19
|
+
*/
|
|
20
|
+
function ensureWidth(ctx: { layout: { side: { width(panel: string): number | undefined; resize(panel: string, width: number): void } } }) {
|
|
21
|
+
const stored = ctx.layout.side.width(TAB_ID)
|
|
22
|
+
if (stored === undefined || stored > PANEL_WIDTH) ctx.layout.side.resize(TAB_ID, PANEL_WIDTH)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const setup: Plugin.Setup<typeof definition> = (ctx) => {
|
|
26
|
+
if (!ctx.desktop) return
|
|
27
|
+
|
|
28
|
+
quotaState.attach(ctx.main(GlmQuota))
|
|
29
|
+
quotaState.start(REFRESH_MS)
|
|
30
|
+
onCleanup(() => quotaState.stop())
|
|
31
|
+
|
|
32
|
+
ensureWidth(ctx)
|
|
33
|
+
|
|
34
|
+
ctx.add(Style, css)
|
|
35
|
+
|
|
36
|
+
const openPanel = () => {
|
|
37
|
+
const session = ctx.sessions.current()
|
|
38
|
+
if (session) ctx.layout.open(`${ctx.id}:${TAB_ID}`, session)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const tab: PanelTab = {
|
|
42
|
+
id: TAB_ID,
|
|
43
|
+
get title() {
|
|
44
|
+
return ctx.t("panel.title")
|
|
45
|
+
},
|
|
46
|
+
closable: "compact",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
ctx.add(Panel, {
|
|
50
|
+
id: TAB_ID,
|
|
51
|
+
region: "side",
|
|
52
|
+
width: PANEL_WIDTH,
|
|
53
|
+
list: (input) => (input.open.includes(TAB_ID) ? [tab] : []),
|
|
54
|
+
// The reliable moment to correct the width: the tab is on screen. The
|
|
55
|
+
// panel's own mount does the same, for a reload with the tab already open.
|
|
56
|
+
focus: () => ensureWidth(ctx),
|
|
57
|
+
render: () => <QuotaPanel />,
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
ctx.add(MenuItem, (): MenuItem => {
|
|
61
|
+
const session = ctx.sessions.current()
|
|
62
|
+
const pct = primaryPercentage(quotaState.snapshot())
|
|
63
|
+
return {
|
|
64
|
+
menu: "session.panel",
|
|
65
|
+
id: "open",
|
|
66
|
+
get title() {
|
|
67
|
+
return ctx.t("menu.open")
|
|
68
|
+
},
|
|
69
|
+
icon: "status",
|
|
70
|
+
...(pct !== undefined ? { description: ctx.t("state.usedPct", { pct: Math.round(pct) }) } : {}),
|
|
71
|
+
opened: !!session && ctx.layout.state(`${ctx.id}:${TAB_ID}`, session) !== "closed",
|
|
72
|
+
run: openPanel,
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
ctx.add(TitlebarItem, (): TitlebarItem | undefined => {
|
|
77
|
+
const snap = quotaState.snapshot()
|
|
78
|
+
const pct = primaryPercentage(snap)
|
|
79
|
+
if (pct === undefined) {
|
|
80
|
+
// A spinner while the very first fetch runs; nothing when it failed.
|
|
81
|
+
return quotaState.busy() && !snap
|
|
82
|
+
? { id: "quota", label: ctx.t("pill.loading"), icon: "status", busy: true, run: openPanel }
|
|
83
|
+
: undefined
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
id: "quota",
|
|
87
|
+
label: ctx.t("pill.label", { pct: Math.round(pct) }),
|
|
88
|
+
icon: "status",
|
|
89
|
+
busy: quotaState.busy(),
|
|
90
|
+
run: openPanel,
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
package/src/gui.tsx
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin/gui"
|
|
2
|
+
import { definition } from "./gui/definition"
|
|
3
|
+
import { main } from "./gui/main"
|
|
4
|
+
import { setup } from "./gui/window"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `./gui-experimental` entry. Both processes evaluate this file: windows run
|
|
8
|
+
* `setup`, the desktop app's main process runs `main`.
|
|
9
|
+
*/
|
|
10
|
+
export default Plugin.define({ ...definition, setup, main })
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The server half of the plugin. It carries the plugin id the desktop GUI takes,
|
|
5
|
+
* and nothing else: all the work happens in `./gui-experimental`.
|
|
6
|
+
*/
|
|
7
|
+
export default Plugin.define({
|
|
8
|
+
id: "opencode-glm-plugin",
|
|
9
|
+
setup() {},
|
|
10
|
+
})
|