opencode-usage-limits 0.3.0 → 0.3.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/CHANGELOG.en.md +12 -0
- package/CHANGELOG.md +11 -0
- package/README.en.md +22 -6
- package/README.md +19 -5
- package/core.ts +72 -3
- package/package.json +1 -1
- package/tui.tsx +43 -7
package/CHANGELOG.en.md
CHANGED
|
@@ -6,6 +6,18 @@ All notable changes to `usage-limits` are documented here. The format is based o
|
|
|
6
6
|
|
|
7
7
|
[简体中文](./CHANGELOG.md) · [English](./CHANGELOG.en.md)
|
|
8
8
|
|
|
9
|
+
## 0.3.1 - 2026-10-26
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- **The panel broke once OpenCode v2.1 moved credentials**: v2 migrated login credentials from
|
|
14
|
+
`auth.json` to the `credential` table in `opencode.db`, but the plugin still read `auth.json`, so
|
|
15
|
+
selecting OpenCode Go always showed `API key not found`. Keys are now read through
|
|
16
|
+
`context.client.credential.list()`, with `auth.json` as a fallback only.
|
|
17
|
+
- **Prefer the host usage endpoint**: when the host exposes `GET /api/experimental/usage/limits`,
|
|
18
|
+
OpenCode Go uses that payload directly (no key at all); older releases fall back to the Zen endpoint.
|
|
19
|
+
- `preview.mjs` now reads credentials from `opencode.db` too, so the panel can be verified without the TUI.
|
|
20
|
+
|
|
9
21
|
## 0.3.0 - 2026-10-13
|
|
10
22
|
|
|
11
23
|
### Changed
|
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
[简体中文](./CHANGELOG.md) · [English](./CHANGELOG.en.md)
|
|
6
6
|
|
|
7
|
+
## 0.3.1 - 2026-10-26
|
|
8
|
+
|
|
9
|
+
### 修复
|
|
10
|
+
|
|
11
|
+
- **OpenCode v2.1+ 凭据迁移导致面板失效**:v2 把登录凭据从 `auth.json` 迁移到 `opencode.db`
|
|
12
|
+
的 `credential` 表,插件仍只读 `auth.json`,因此选中 OpenCode Go 时固定显示
|
|
13
|
+
`API key not found`。现在优先通过 `context.client.credential.list()` 读取,`auth.json` 仅作兜底。
|
|
14
|
+
- **优先使用宿主用量接口**:宿主提供 `GET /api/experimental/usage/limits` 时,OpenCode Go 直接使用
|
|
15
|
+
该接口(无需密钥);旧版本自动回退到 Zen 用量接口。
|
|
16
|
+
- `preview.mjs` 同步支持从 `opencode.db` 读取凭据,便于在不启动 TUI 的情况下验证。
|
|
17
|
+
|
|
7
18
|
## 0.3.0 - 2026-10-13
|
|
8
19
|
|
|
9
20
|
### 变更
|
package/README.en.md
CHANGED
|
@@ -41,7 +41,8 @@ Monthly 60%
|
|
|
41
41
|
- **Rate-limit backoff**: 429 / 5xx / network failures back off exponentially (capped at 5 minutes) and show `retry in Ns`.
|
|
42
42
|
- **Stale-data warning**: once a reading is older than ~75 seconds (2.5 polling intervals) the title row
|
|
43
43
|
shows its age in yellow; while the data is fresh nothing is shown, so there is no constant noise.
|
|
44
|
-
- **Key safety**: the API key is read live
|
|
44
|
+
- **Key safety**: the API key is read live through OpenCode's credential API (v2 keeps credentials in
|
|
45
|
+
its database), with `auth.json` as a fallback; the plugin stores nothing.
|
|
45
46
|
|
|
46
47
|
## Supported sources
|
|
47
48
|
|
|
@@ -86,8 +87,8 @@ opencode plugin add github:GamingNowEdward/opencode-usage-limits
|
|
|
86
87
|
}
|
|
87
88
|
```
|
|
88
89
|
|
|
89
|
-
CLI plugins run inside the local TUI process, so they can
|
|
90
|
-
directly, and they stay active even when the CLI is connected to a remote server. See the
|
|
90
|
+
CLI plugins run inside the local TUI process, so they can call OpenCode's credential API and reach
|
|
91
|
+
the network directly, and they stay active even when the CLI is connected to a remote server. See the
|
|
91
92
|
[CLI plugin documentation](https://opencode.ai/v2/docs/build/plugins/cli).
|
|
92
93
|
|
|
93
94
|
## Usage
|
|
@@ -111,10 +112,16 @@ In `setup()` the plugin does three things:
|
|
|
111
112
|
|
|
112
113
|
1. **Registers a sidebar slot**: `context.ui.slot({ prepend: "sidebar.content", ... })`.
|
|
113
114
|
2. **Reads the current model**: `context.ui.model.current()?.providerID` (reactive).
|
|
114
|
-
3. **Fetches usage per provider**: resolves the key
|
|
115
|
-
|
|
115
|
+
3. **Fetches usage per provider**: resolves the key through `context.client.credential.list()`
|
|
116
|
+
(OpenCode v2's authoritative store), calls the usage endpoint with `Authorization: Bearer`,
|
|
117
|
+
polls every 30 seconds and ticks the countdown every second.
|
|
116
118
|
|
|
117
|
-
Key resolution order: the `<PROVIDER>_API_KEY` environment variable →
|
|
119
|
+
Key resolution order: the `<PROVIDER>_API_KEY` environment variable →
|
|
120
|
+
`context.client.credential.list()` → `~/.local/share/opencode/auth.json`. OpenCode v2 keeps login
|
|
121
|
+
credentials in the `credential` table of `opencode.db`; `auth.json` is the legacy path only.
|
|
122
|
+
|
|
123
|
+
When the host exposes `GET /api/experimental/usage/limits`, OpenCode Go uses that payload directly
|
|
124
|
+
and needs no key at all.
|
|
118
125
|
|
|
119
126
|
## File layout
|
|
120
127
|
|
|
@@ -142,6 +149,15 @@ node preview.mjs cline-pass # a single source
|
|
|
142
149
|
1. Make sure the current model belongs to `opencode-go` or `cline-pass`.
|
|
143
150
|
2. Plugin files **hot reload on save** — no restart needed. Only adding or removing an imported file requires restarting the TUI.
|
|
144
151
|
3. Check the log: `~/.local/share/opencode/log/opencode.log`, filter by `role=cli` and `plugin`.
|
|
152
|
+
4. If the panel shows **API key not found**, check the login first:
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
opencode auth list
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
When it says `stored` but the panel still fails, the host release is too old to expose the
|
|
159
|
+
credential API. As a workaround, set the `<PROVIDER>_API_KEY` environment variable (for example
|
|
160
|
+
`OPENCODE_GO_API_KEY`) and restart the TUI.
|
|
145
161
|
|
|
146
162
|
## See also
|
|
147
163
|
|
package/README.md
CHANGED
|
@@ -38,7 +38,8 @@ Monthly 60%
|
|
|
38
38
|
- **不打扰**:选中其他 provider 时**完全不渲染**,不占空间。
|
|
39
39
|
- **限流退避**:遇 429 / 5xx / 网络失败自动指数退避(最长 5 分钟),并显示 `retry in Ns`。
|
|
40
40
|
- **数据过期提示**:数据变旧(超过约 75 秒,即 2.5 个轮询间隔)时,标题行才显示年龄并变黄;平时不显示,不产生噪音。
|
|
41
|
-
-
|
|
41
|
+
- **密钥安全**:通过 OpenCode 的凭据 API 实时读取(v2 把凭据存在自己的数据库中),
|
|
42
|
+
`auth.json` 仅作兜底;插件不存储任何凭据。
|
|
42
43
|
|
|
43
44
|
## 支持的数据源
|
|
44
45
|
|
|
@@ -82,7 +83,7 @@ opencode plugin add github:GamingNowEdward/opencode-usage-limits
|
|
|
82
83
|
}
|
|
83
84
|
```
|
|
84
85
|
|
|
85
|
-
CLI 插件在本地 TUI
|
|
86
|
+
CLI 插件在本地 TUI 进程内运行,因此可以调用 OpenCode 的凭据 API 与访问网络;它也会在连接
|
|
86
87
|
远程 server 时保持可用。详见 [CLI 插件文档](https://opencode.ai/v2/docs/build/plugins/cli)。
|
|
87
88
|
|
|
88
89
|
## 使用
|
|
@@ -106,10 +107,15 @@ CLI 插件在本地 TUI 进程内运行,因此可以直接读取 `auth.json`
|
|
|
106
107
|
|
|
107
108
|
1. **注册侧边栏 slot**:`context.ui.slot({ prepend: "sidebar.content", ... })`。
|
|
108
109
|
2. **读取当前模型**:`context.ui.model.current()?.providerID`(响应式)。
|
|
109
|
-
3. **按 provider
|
|
110
|
-
30 秒轮询一次,1 秒刷新倒计时。
|
|
110
|
+
3. **按 provider 拉取用量**:通过 `context.client.credential.list()` 取密钥(OpenCode v2 的
|
|
111
|
+
权威来源),带 `Authorization: Bearer` 请求用量接口,30 秒轮询一次,1 秒刷新倒计时。
|
|
111
112
|
|
|
112
|
-
密钥解析顺序:环境变量 `<PROVIDER>_API_KEY` →
|
|
113
|
+
密钥解析顺序:环境变量 `<PROVIDER>_API_KEY` → `context.client.credential.list()` →
|
|
114
|
+
`~/.local/share/opencode/auth.json`。OpenCode v2 把登录凭据存在 `opencode.db` 的 `credential`
|
|
115
|
+
表中,`auth.json` 只是旧版兼容路径。
|
|
116
|
+
|
|
117
|
+
若宿主版本提供 `GET /api/experimental/usage/limits`,OpenCode Go 会直接使用该接口返回的用量,
|
|
118
|
+
完全不需要密钥。
|
|
113
119
|
|
|
114
120
|
## 文件结构
|
|
115
121
|
|
|
@@ -137,6 +143,14 @@ node preview.mjs cline-pass # 只预览一个
|
|
|
137
143
|
1. 确认当前模型属于 `opencode-go` 或 `cline-pass`。
|
|
138
144
|
2. 插件文件**保存即热重载**,无需重启;只有新增/删除被 import 的文件时才需退出 TUI 重进。
|
|
139
145
|
3. 查看日志:`~/.local/share/opencode/log/opencode.log`,筛 `role=cli` 与 `plugin`。
|
|
146
|
+
4. 面板显示 **API key not found** 时,先确认登录状态:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
opencode auth list
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
若已显示 `stored` 但面板仍报错,说明宿主版本过旧,无法通过凭据 API 读取密钥;
|
|
153
|
+
可临时设置环境变量 `<PROVIDER>_API_KEY`(如 `OPENCODE_GO_API_KEY`)后重启 TUI。
|
|
140
154
|
|
|
141
155
|
## 相关
|
|
142
156
|
|
package/core.ts
CHANGED
|
@@ -132,7 +132,7 @@ function authPaths(): string[] {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
|
-
* Pull an API key out of
|
|
135
|
+
* Pull an API key out of a credential entry. An entry may hold a single
|
|
136
136
|
* credential object, or an array of them when several accounts/connections
|
|
137
137
|
* exist for one provider; in that case the first usable key wins.
|
|
138
138
|
*/
|
|
@@ -153,12 +153,47 @@ function extractKey(entry: any): string | undefined {
|
|
|
153
153
|
return undefined
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
/**
|
|
157
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Pick the credential belonging to `providerID` out of the list returned by
|
|
158
|
+
* OpenCode's `credential.list()` API. OpenCode v2 stores credentials in its
|
|
159
|
+
* database rather than in `auth.json`, so this is the authoritative source.
|
|
160
|
+
* The active credential wins; otherwise the first match is used.
|
|
161
|
+
*/
|
|
162
|
+
export function keyFromCredentials(entries: unknown, providerID: string): string | undefined {
|
|
163
|
+
const list = Array.isArray(entries)
|
|
164
|
+
? entries
|
|
165
|
+
: Array.isArray((entries as any)?.data)
|
|
166
|
+
? (entries as any).data
|
|
167
|
+
: []
|
|
168
|
+
const matches = list.filter((entry: any) => entry?.integrationID === providerID)
|
|
169
|
+
const preferred = matches.find((entry: any) => entry?.active === true) ?? matches[0]
|
|
170
|
+
return extractKey(preferred?.value)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Reads a provider key through the host's OpenCode client. */
|
|
174
|
+
export type KeyResolver = (providerID: string) => Promise<string | undefined>
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Resolve the provider API key from the env, the host credential store, or
|
|
178
|
+
* `auth.json`. The host store comes first because OpenCode v2 keeps credentials
|
|
179
|
+
* in its database; `auth.json` remains as a fallback for older installs and for
|
|
180
|
+
* the offline preview script.
|
|
181
|
+
*/
|
|
182
|
+
export async function resolveKey(providerID: string, fromHost?: KeyResolver): Promise<string | undefined> {
|
|
158
183
|
const envName = `${providerID.toUpperCase().replace(/-/g, "_")}_API_KEY`
|
|
159
184
|
const fromEnv = process.env[envName]
|
|
160
185
|
if (fromEnv) return fromEnv
|
|
161
186
|
|
|
187
|
+
if (fromHost) {
|
|
188
|
+
try {
|
|
189
|
+
const key = await fromHost(providerID)
|
|
190
|
+
if (key) return key
|
|
191
|
+
} catch {
|
|
192
|
+
// Host credential API unavailable (older OpenCode, remote server): fall
|
|
193
|
+
// through to the legacy auth store.
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
162
197
|
for (const path of authPaths()) {
|
|
163
198
|
try {
|
|
164
199
|
if (!existsSync(path)) continue
|
|
@@ -175,6 +210,9 @@ export async function resolveKey(providerID: string): Promise<string | undefined
|
|
|
175
210
|
// ---- API fetching -------------------------------------------------------
|
|
176
211
|
|
|
177
212
|
function toMs(value: unknown): number | null {
|
|
213
|
+
// Provider endpoints return ISO strings; OpenCode's own usage API returns
|
|
214
|
+
// epoch milliseconds.
|
|
215
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null
|
|
178
216
|
if (typeof value !== "string") return null
|
|
179
217
|
const ms = new Date(value).getTime()
|
|
180
218
|
return Number.isFinite(ms) ? ms : null
|
|
@@ -317,6 +355,37 @@ export async function fetchUsage(config: ProviderConfig, key: string, timeoutMs
|
|
|
317
355
|
return windows
|
|
318
356
|
}
|
|
319
357
|
|
|
358
|
+
/**
|
|
359
|
+
* Map the OpenCode host's own usage payload (`GET /api/experimental/usage/limits`)
|
|
360
|
+
* onto the panel's windows. Newer OpenCode releases expose the Go meters there,
|
|
361
|
+
* which removes the need for a key entirely; older ones do not, so the caller
|
|
362
|
+
* falls back to the provider endpoint above.
|
|
363
|
+
*/
|
|
364
|
+
export function goWindowsFromLimits(info: any): UsageWindow[] | undefined {
|
|
365
|
+
const go = info?.go ?? info?.data?.go
|
|
366
|
+
if (!go) return undefined
|
|
367
|
+
const meters: Record<WindowKind, any> = {
|
|
368
|
+
hour5: go.rolling,
|
|
369
|
+
weekly: go.weekly,
|
|
370
|
+
monthly: go.monthly,
|
|
371
|
+
}
|
|
372
|
+
const windows: UsageWindow[] = []
|
|
373
|
+
for (const kind of WINDOW_ORDER) {
|
|
374
|
+
const meter = meters[kind]
|
|
375
|
+
if (!meter) continue
|
|
376
|
+
windows.push(
|
|
377
|
+
buildWindow(
|
|
378
|
+
kind,
|
|
379
|
+
Number(meter.percent) || 0,
|
|
380
|
+
toMs(meter.resetsAt),
|
|
381
|
+
undefined,
|
|
382
|
+
meter.limited === true ? "limited" : "ok",
|
|
383
|
+
),
|
|
384
|
+
)
|
|
385
|
+
}
|
|
386
|
+
return windows.length > 0 ? windows : undefined
|
|
387
|
+
}
|
|
388
|
+
|
|
320
389
|
// ---- Formatting ---------------------------------------------------------
|
|
321
390
|
|
|
322
391
|
const MINUTE = 60_000
|
package/package.json
CHANGED
package/tui.tsx
CHANGED
|
@@ -7,8 +7,10 @@ import {
|
|
|
7
7
|
clampPercent,
|
|
8
8
|
fetchUsage,
|
|
9
9
|
formatUsd,
|
|
10
|
+
goWindowsFromLimits,
|
|
10
11
|
isTransient,
|
|
11
12
|
isWindowLimited,
|
|
13
|
+
keyFromCredentials,
|
|
12
14
|
modelAllowance,
|
|
13
15
|
resetText,
|
|
14
16
|
resolveKey,
|
|
@@ -139,6 +141,36 @@ function UsagePanel(props: { context: any; panel: any; toggle: () => void }) {
|
|
|
139
141
|
const baseMs = Number(props.context?.options?.refreshMs) || REFRESH_MS
|
|
140
142
|
let pollMs = baseMs
|
|
141
143
|
|
|
144
|
+
// OpenCode v2 keeps credentials in its database, so read them through the
|
|
145
|
+
// host API first and only fall back to auth.json. The list is briefly cached:
|
|
146
|
+
// polling runs every 30s and a key rotation need not be picked up instantly.
|
|
147
|
+
let credentialCache: { at: number; entries: unknown } | undefined
|
|
148
|
+
const readCredentials = async () => {
|
|
149
|
+
const client = ctx.client as any
|
|
150
|
+
if (typeof client?.credential?.list !== "function") return undefined
|
|
151
|
+
if (credentialCache && Date.now() - credentialCache.at < 60_000) return credentialCache.entries
|
|
152
|
+
const entries = await client.credential.list()
|
|
153
|
+
credentialCache = { at: Date.now(), entries }
|
|
154
|
+
return entries
|
|
155
|
+
}
|
|
156
|
+
const keyFromHost = async (id: string) => keyFromCredentials(await readCredentials(), id)
|
|
157
|
+
|
|
158
|
+
// Newer OpenCode releases expose the Go meters themselves. Use them when the
|
|
159
|
+
// host has the endpoint, so this provider needs no key at all; older releases
|
|
160
|
+
// return undefined and fall back to the provider endpoint below.
|
|
161
|
+
const goFromHost = async (): Promise<UsageWindow[] | undefined> => {
|
|
162
|
+
const client = ctx.client as any
|
|
163
|
+
const limits = client?.experimental?.usage?.limits
|
|
164
|
+
if (typeof limits !== "function") return undefined
|
|
165
|
+
try {
|
|
166
|
+
const location = ctx.location ?? ctx.data?.location?.default?.()
|
|
167
|
+
const result = await limits.call(client.experimental.usage, location ? { location } : undefined)
|
|
168
|
+
return goWindowsFromLimits(result)
|
|
169
|
+
} catch {
|
|
170
|
+
return undefined
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
142
174
|
// Guard per provider: if the user switches providers while a request is
|
|
143
175
|
// still in flight, the new provider's request must fire immediately instead
|
|
144
176
|
// of waiting for the next poll. Stale responses are dropped by the
|
|
@@ -150,14 +182,18 @@ function UsagePanel(props: { context: any; panel: any; toggle: () => void }) {
|
|
|
150
182
|
inflight.add(cfg.id)
|
|
151
183
|
setLoading(true)
|
|
152
184
|
try {
|
|
153
|
-
|
|
154
|
-
if (!
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
185
|
+
let next = cfg.id === "opencode-go" ? await goFromHost() : undefined
|
|
186
|
+
if (!next) {
|
|
187
|
+
const key = await resolveKey(cfg.id, keyFromHost)
|
|
188
|
+
if (!key) {
|
|
189
|
+
setWindows([])
|
|
190
|
+
setError("API key not found")
|
|
191
|
+
setNextRetryAt(undefined)
|
|
192
|
+
pollMs = baseMs
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
next = await fetchUsage(cfg, key)
|
|
159
196
|
}
|
|
160
|
-
const next = await fetchUsage(cfg, key)
|
|
161
197
|
if (config()?.id === cfg.id) {
|
|
162
198
|
setWindows(next)
|
|
163
199
|
setError("")
|