@antonkw/opencode-minimax-quota 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +132 -2
- package/dist/format.d.ts +4 -0
- package/dist/format.js +30 -0
- package/dist/poll.d.ts +9 -0
- package/dist/poll.js +13 -0
- package/dist/quota.d.ts +26 -0
- package/dist/quota.js +108 -0
- package/dist/tui.d.ts +4 -0
- package/dist/tui.js +396 -0
- package/package.json +58 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KVAnton-WEB
|
|
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,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @antonkw/opencode-minimax-quota
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenCode TUI plugin showing MiniMax (coding plan) quota in sidebar and
|
|
4
|
+
compact status line, plus `/quota-m` slash command for detail view.
|
|
5
|
+
|
|
6
|
+
**OpenCode V2 only** — the V1 plugin API is not supported.
|
|
7
|
+
|
|
8
|
+
## Screenshots
|
|
9
|
+
|
|
10
|
+
Sidebar:
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
Status line:
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+
|
|
18
|
+
`/quota-m` dialog:
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
opencode plugin add @antonkw/opencode-minimax-quota
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The plugin is TUI-only, so it is configured in `cli.json`
|
|
29
|
+
(`~/.config/opencode/cli.json`), not in `opencode.json`. To add it manually:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"$schema": "https://opencode.ai/v2/cli.json",
|
|
34
|
+
"plugins": ["@antonkw/opencode-minimax-quota"]
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Requires `MINIMAX_CODING_PLAN_API_KEY` in the environment where OpenCode
|
|
39
|
+
is launched.
|
|
40
|
+
|
|
41
|
+
### From source
|
|
42
|
+
|
|
43
|
+
OpenCode V2 discovers plugins under the global config directory. Create a
|
|
44
|
+
one-line shim that re-exports the build:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# <global-config>/plugins/<id>/ — same directory your other plugins live in
|
|
48
|
+
mkdir -p ~/.config/opencode/plugins/minimax-quota
|
|
49
|
+
echo 'export { default } from "/absolute/path/to/opencode-minimax-quota/dist/tui.js"' \
|
|
50
|
+
> ~/.config/opencode/plugins/minimax-quota/tui.js
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The whole `dist/` directory has to stay where it is — `dist/tui.js` imports
|
|
54
|
+
`./quota.js` next to it.
|
|
55
|
+
|
|
56
|
+
shortcut: `cli.json` -> `plugins` accepts a package name, a directory or a
|
|
57
|
+
`file://` path, but the target must look like a plugin: `tui.ts`/`index.ts` or a
|
|
58
|
+
package manifest. A bare `dist/tui.js` or a directory without an entrypoint is
|
|
59
|
+
skipped without a warning in V2.0.28, which is why the shim above is used.
|
|
60
|
+
|
|
61
|
+
## Build
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install
|
|
65
|
+
npm run build # babel → dist/*.js, tsc → dist/*.d.ts (fails on type errors)
|
|
66
|
+
npm test
|
|
67
|
+
npm run check:dist # rebuild and verify the committed dist/ matches src/
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`dist/` is committed: rebuild and commit it after changing `src/`, otherwise users get a stale build.
|
|
71
|
+
|
|
72
|
+
## What it shows
|
|
73
|
+
|
|
74
|
+
- **Sidebar** (`sidebar.content`, appended): quota for the `general`
|
|
75
|
+
model — 5h and 7d weekly windows with progress bars, remaining
|
|
76
|
+
percent, time until reset, plus current session token counts
|
|
77
|
+
(input ↑ / output ↓).
|
|
78
|
+
- **Compact line** (`prompt.footer.status`): `↑N ↓M quota N%` badge
|
|
79
|
+
colored by remaining percent through the theme's
|
|
80
|
+
`success` / `warning` / `error` feedback tokens. The footer slot
|
|
81
|
+
has no session outside a session, so the counters show `0` there.
|
|
82
|
+
- **`/quota-m`** slash command: detailed quota snapshot + session
|
|
83
|
+
tokens in a toast / dialog. The dialog opens immediately with the
|
|
84
|
+
cached snapshot and updates itself when the refresh finishes, so a
|
|
85
|
+
slow network never leaves the UI hanging.
|
|
86
|
+
|
|
87
|
+
### Token counters
|
|
88
|
+
|
|
89
|
+
`SessionInfo.tokens` counts every token once: `input` already excludes the
|
|
90
|
+
cache, so the plugin adds `cache.read` + `cache.write` to it, and adds
|
|
91
|
+
`reasoning` to `output` (MiniMax bills reasoning tokens as output).
|
|
92
|
+
|
|
93
|
+
### Errors
|
|
94
|
+
|
|
95
|
+
The last successful snapshot is kept: a network failure shows the error next
|
|
96
|
+
to the still-valid quota instead of wiping the panel. Retries back off from
|
|
97
|
+
1 min to 10 min. Polling stops entirely (until `/quota-m` is run again) only
|
|
98
|
+
where a retry cannot help: no API key, HTTP 401/403, or MiniMax API error
|
|
99
|
+
1004 (invalid key). Transient API errors (1002 rate limit, 1013 internal
|
|
100
|
+
error, 1039 TPM limit) are retried with the same backoff.
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
shortcut: a slot `render` runs **once and untracked** — the host calls it
|
|
104
|
+
through Solid's `createComponent`, which drops the tracking context. A
|
|
105
|
+
signal read into a local variable (`const s = snapshot()`) therefore stays
|
|
106
|
+
frozen at its mount value. Read reactive values inside the JSX instead
|
|
107
|
+
(`{quota().ok}`, `<For each={quota().models}>`), where Solid compiles the
|
|
108
|
+
read into a tracked effect.
|
|
109
|
+
|
|
110
|
+
## API
|
|
111
|
+
|
|
112
|
+
Reads `https://www.minimax.io/v1/token_plan/remains` with
|
|
113
|
+
`Authorization: Bearer $MINIMAX_CODING_PLAN_API_KEY`. Nothing else leaves the machine.
|
|
114
|
+
|
|
115
|
+
## References
|
|
116
|
+
|
|
117
|
+
Docs that helped build this plugin:
|
|
118
|
+
|
|
119
|
+
- **[CLI plugins guide](https://opencode.ai/v2/docs/build/plugins/cli)** —
|
|
120
|
+
the V2 CLI plugin API: `Plugin.define({ id, setup })`, `context.ui.slot`,
|
|
121
|
+
`context.keymap.layer`, `context.data.on/listen`, dialogs, toasts, storage.
|
|
122
|
+
- **[CLI plugins config](https://opencode.ai/v2/docs/cli/plugins)** — where
|
|
123
|
+
`cli.json` entries and the `<global-config>/plugins/<id>/tui.ts` discovery
|
|
124
|
+
path live.
|
|
125
|
+
- **[V1 → V2 migration](https://opencode.ai/v2/docs/migrate-v1)** — why the
|
|
126
|
+
V1 plugin entrypoint (`export default { id, tui }`) no longer loads.
|
|
127
|
+
- **`@opencode/plugin/tui` type declarations** — `node_modules/@opencode/plugin/dist/tui/*.d.ts`
|
|
128
|
+
is the source of truth for `Context`, `SlotMap`, `Data`, `UI` and `Keymap`.
|
|
129
|
+
Read them when the guides are silent.
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT — see [LICENSE](./LICENSE).
|
package/dist/format.d.ts
ADDED
package/dist/format.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function fmtDuration(ms) {
|
|
2
|
+
if (ms <= 0) return "now";
|
|
3
|
+
const sec = Math.floor(ms / 1000);
|
|
4
|
+
const days = Math.floor(sec / 86400);
|
|
5
|
+
const hours = Math.floor(sec % 86400 / 3600);
|
|
6
|
+
const mins = Math.floor(sec % 3600 / 60);
|
|
7
|
+
if (days > 0) return `${days}d ${hours}h`;
|
|
8
|
+
if (hours > 0) return `${hours}h ${mins}m`;
|
|
9
|
+
return `${mins}m`;
|
|
10
|
+
}
|
|
11
|
+
export function fmtAge(ts, now) {
|
|
12
|
+
if (!ts) return "—";
|
|
13
|
+
const sec = Math.max(0, Math.floor((now - ts) / 1000));
|
|
14
|
+
if (sec < 60) return `${sec}s ago`;
|
|
15
|
+
if (sec < 3600) return `${Math.floor(sec / 60)}m ago`;
|
|
16
|
+
return `${Math.floor(sec / 3600)}h ago`;
|
|
17
|
+
}
|
|
18
|
+
export function fmtTokens(n) {
|
|
19
|
+
if (n < 1000) return String(n);
|
|
20
|
+
if (n < 1_000_000) {
|
|
21
|
+
const k = Math.round(n / 100) / 10;
|
|
22
|
+
return (k < 10 ? k.toFixed(1) : String(Math.round(k))) + "k";
|
|
23
|
+
}
|
|
24
|
+
const m = n / 1_000_000;
|
|
25
|
+
return (m < 10 ? m.toFixed(1) : String(Math.round(m))) + "M";
|
|
26
|
+
}
|
|
27
|
+
export function progressBar(p, width = 16) {
|
|
28
|
+
const filled = Math.max(0, Math.min(width, Math.round(p / 100 * width)));
|
|
29
|
+
return "█".repeat(filled) + "░".repeat(width - filled);
|
|
30
|
+
}
|
package/dist/poll.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Интервал успешного опроса. */
|
|
2
|
+
export declare const CACHE_MS = 60000;
|
|
3
|
+
/** Потолок отсрочки между неудачными попытками. */
|
|
4
|
+
export declare const MAX_BACKOFF_MS: number;
|
|
5
|
+
/**
|
|
6
|
+
* Задержка до следующей попытки после `failures` подряд неудачных запросов:
|
|
7
|
+
* 1 мин, 2, 4, 8 и дальше до 10 мин.
|
|
8
|
+
*/
|
|
9
|
+
export declare function backoffDelay(failures: number): number;
|
package/dist/poll.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Интервал успешного опроса. */
|
|
2
|
+
export const CACHE_MS = 60_000;
|
|
3
|
+
|
|
4
|
+
/** Потолок отсрочки между неудачными попытками. */
|
|
5
|
+
export const MAX_BACKOFF_MS = 10 * 60_000;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Задержка до следующей попытки после `failures` подряд неудачных запросов:
|
|
9
|
+
* 1 мин, 2, 4, 8 и дальше до 10 мин.
|
|
10
|
+
*/
|
|
11
|
+
export function backoffDelay(failures) {
|
|
12
|
+
return Math.min(CACHE_MS * 2 ** Math.max(0, failures - 1), MAX_BACKOFF_MS);
|
|
13
|
+
}
|
package/dist/quota.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `base_resp.status_code` MiniMax: 1004 — неверный ключ (постоянная ошибка,
|
|
3
|
+
* повтор не поможет). Остальные коды транзиентные — 1002 rate limit, 1013
|
|
4
|
+
* internal error, 1039 TPM limit — их надо повторять с backoff.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isFatalApiCode(code: unknown): boolean;
|
|
7
|
+
export type QuotaWindow = {
|
|
8
|
+
label: string;
|
|
9
|
+
remainingPercent: number;
|
|
10
|
+
endTimeMs: number;
|
|
11
|
+
};
|
|
12
|
+
export type ModelQuota = {
|
|
13
|
+
modelName: string;
|
|
14
|
+
fiveHour: QuotaWindow | null;
|
|
15
|
+
weekly: QuotaWindow | null;
|
|
16
|
+
};
|
|
17
|
+
export type Snapshot = {
|
|
18
|
+
ok: boolean;
|
|
19
|
+
fetchedAt: number;
|
|
20
|
+
models: ModelQuota[];
|
|
21
|
+
error?: string;
|
|
22
|
+
/** Повтор не поможет (нет ключа, ключ невалиден, доступ запрещён) — опрос надо прекратить. */
|
|
23
|
+
fatal?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export declare function parseQuota(json: any): Snapshot;
|
|
26
|
+
export declare function fetchQuota(): Promise<Snapshot>;
|
package/dist/quota.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const ENDPOINT = "https://www.minimax.io/v1/token_plan/remains";
|
|
2
|
+
const PREFERRED_MODEL = "general";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `base_resp.status_code` MiniMax: 1004 — неверный ключ (постоянная ошибка,
|
|
6
|
+
* повтор не поможет). Остальные коды транзиентные — 1002 rate limit, 1013
|
|
7
|
+
* internal error, 1039 TPM limit — их надо повторять с backoff.
|
|
8
|
+
*/
|
|
9
|
+
export function isFatalApiCode(code) {
|
|
10
|
+
return Number(code) === 1004;
|
|
11
|
+
}
|
|
12
|
+
function num(x) {
|
|
13
|
+
if (typeof x === "number" && Number.isFinite(x)) return x;
|
|
14
|
+
if (typeof x === "string") {
|
|
15
|
+
const n = Number(x);
|
|
16
|
+
return Number.isFinite(n) ? n : undefined;
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
function buildWindow(prefix, entry) {
|
|
21
|
+
const pct = num(entry[`${prefix}_remaining_percent`]);
|
|
22
|
+
if (pct === undefined) return null;
|
|
23
|
+
return {
|
|
24
|
+
label: prefix === "current_interval" ? "5h" : "7d",
|
|
25
|
+
remainingPercent: pct,
|
|
26
|
+
endTimeMs: num(entry[prefix === "current_interval" ? "end_time" : "weekly_end_time"]) ?? 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function parseQuota(json) {
|
|
30
|
+
const baseResp = json?.base_resp;
|
|
31
|
+
if (baseResp && baseResp.status_code !== undefined && baseResp.status_code !== 0) {
|
|
32
|
+
// Повтор с тем же ключом поможет только для транзиентных кодов (rate limit и т.п.).
|
|
33
|
+
return {
|
|
34
|
+
ok: false,
|
|
35
|
+
fetchedAt: Date.now(),
|
|
36
|
+
models: [],
|
|
37
|
+
error: baseResp.status_msg || `code ${baseResp.status_code}`,
|
|
38
|
+
fatal: isFatalApiCode(baseResp.status_code)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const list = json?.model_remains ?? json?.data ?? json?.result;
|
|
42
|
+
if (!Array.isArray(list)) {
|
|
43
|
+
return {
|
|
44
|
+
ok: false,
|
|
45
|
+
fetchedAt: Date.now(),
|
|
46
|
+
models: [],
|
|
47
|
+
error: "unexpected response shape"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const all = list.map(entry => ({
|
|
51
|
+
modelName: String(entry?.model_name ?? "unknown"),
|
|
52
|
+
fiveHour: buildWindow("current_interval", entry),
|
|
53
|
+
weekly: buildWindow("current_weekly", entry)
|
|
54
|
+
})).filter(m => m.fiveHour || m.weekly);
|
|
55
|
+
const preferred = all.find(m => m.modelName === PREFERRED_MODEL);
|
|
56
|
+
const models = preferred ? [preferred] : all.slice(0, 1);
|
|
57
|
+
if (!models.length) {
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
fetchedAt: Date.now(),
|
|
61
|
+
models: [],
|
|
62
|
+
error: "no quota windows in response"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
fetchedAt: Date.now(),
|
|
68
|
+
models
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export async function fetchQuota() {
|
|
72
|
+
const key = process.env.MINIMAX_CODING_PLAN_API_KEY;
|
|
73
|
+
if (!key) {
|
|
74
|
+
return {
|
|
75
|
+
ok: false,
|
|
76
|
+
fetchedAt: Date.now(),
|
|
77
|
+
models: [],
|
|
78
|
+
error: "MINIMAX_CODING_PLAN_API_KEY not set",
|
|
79
|
+
fatal: true
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const res = await fetch(ENDPOINT, {
|
|
84
|
+
headers: {
|
|
85
|
+
Authorization: `Bearer ${key}`,
|
|
86
|
+
"Content-Type": "application/json"
|
|
87
|
+
},
|
|
88
|
+
signal: AbortSignal.timeout(10_000)
|
|
89
|
+
});
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
return {
|
|
92
|
+
ok: false,
|
|
93
|
+
fetchedAt: Date.now(),
|
|
94
|
+
models: [],
|
|
95
|
+
error: `HTTP ${res.status}`,
|
|
96
|
+
fatal: res.status === 401 || res.status === 403
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return parseQuota(await res.json());
|
|
100
|
+
} catch (e) {
|
|
101
|
+
return {
|
|
102
|
+
ok: false,
|
|
103
|
+
fetchedAt: Date.now(),
|
|
104
|
+
models: [],
|
|
105
|
+
error: String(e?.message ?? e)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
package/dist/tui.d.ts
ADDED
package/dist/tui.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { memo as _$memo } from "@opentui/solid";
|
|
2
|
+
import { createComponent as _$createComponent } from "@opentui/solid";
|
|
3
|
+
import { effect as _$effect } from "@opentui/solid";
|
|
4
|
+
import { createTextNode as _$createTextNode } from "@opentui/solid";
|
|
5
|
+
import { insertNode as _$insertNode } from "@opentui/solid";
|
|
6
|
+
import { insert as _$insert } from "@opentui/solid";
|
|
7
|
+
import { setProp as _$setProp } from "@opentui/solid";
|
|
8
|
+
import { createElement as _$createElement } from "@opentui/solid";
|
|
9
|
+
/** @jsxImportSource @opentui/solid */
|
|
10
|
+
import { Plugin } from "@opencode/plugin/tui";
|
|
11
|
+
import { Show, For, createSignal } from "solid-js";
|
|
12
|
+
import { fetchQuota } from "./quota.js";
|
|
13
|
+
import { fmtAge, fmtDuration, fmtTokens, progressBar } from "./format.js";
|
|
14
|
+
import { CACHE_MS, backoffDelay } from "./poll.js";
|
|
15
|
+
const TICK_MS = 30_000;
|
|
16
|
+
const ZERO_TOKENS = {
|
|
17
|
+
input: 0,
|
|
18
|
+
output: 0
|
|
19
|
+
};
|
|
20
|
+
function quotaColor(theme, p) {
|
|
21
|
+
if (p >= 50) return theme.text.feedback.success.base;
|
|
22
|
+
if (p >= 20) return theme.text.feedback.warning.base;
|
|
23
|
+
return theme.text.feedback.error.base;
|
|
24
|
+
}
|
|
25
|
+
function compactLine(theme, q, err) {
|
|
26
|
+
if (!q) return {
|
|
27
|
+
text: `quota: ${err ?? "loading…"}`,
|
|
28
|
+
color: theme.text.feedback.error.base
|
|
29
|
+
};
|
|
30
|
+
const model = q.models[0];
|
|
31
|
+
const w = model?.fiveHour ?? model?.weekly;
|
|
32
|
+
if (!w) return {
|
|
33
|
+
text: "quota: ?",
|
|
34
|
+
color: theme.text.feedback.warning.base
|
|
35
|
+
};
|
|
36
|
+
// Данные могут быть устаревшими после ошибки сети — подсвечиваем предупреждением.
|
|
37
|
+
const text = `quota ${w.remainingPercent}%`;
|
|
38
|
+
if (err) return {
|
|
39
|
+
text,
|
|
40
|
+
color: theme.text.feedback.warning.base
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
text,
|
|
44
|
+
color: quotaColor(theme, w.remainingPercent)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function renderWindowBlock(theme, label, w, now) {
|
|
48
|
+
const reset = w.endTimeMs ? `reset in ${fmtDuration(w.endTimeMs - now)}` : "reset time unknown";
|
|
49
|
+
return (() => {
|
|
50
|
+
var _el$ = _$createElement("box"),
|
|
51
|
+
_el$2 = _$createElement("text"),
|
|
52
|
+
_el$3 = _$createElement("b"),
|
|
53
|
+
_el$4 = _$createTextNode(` (`),
|
|
54
|
+
_el$5 = _$createElement("span"),
|
|
55
|
+
_el$6 = _$createTextNode(`)`),
|
|
56
|
+
_el$7 = _$createElement("text"),
|
|
57
|
+
_el$8 = _$createTextNode(` `),
|
|
58
|
+
_el$9 = _$createTextNode(`%`);
|
|
59
|
+
_$insertNode(_el$, _el$2);
|
|
60
|
+
_$insertNode(_el$, _el$7);
|
|
61
|
+
_$setProp(_el$, "flexDirection", "column");
|
|
62
|
+
_$setProp(_el$, "gap", 0);
|
|
63
|
+
_$insertNode(_el$2, _el$3);
|
|
64
|
+
_$insertNode(_el$2, _el$4);
|
|
65
|
+
_$insertNode(_el$2, _el$5);
|
|
66
|
+
_$insertNode(_el$2, _el$6);
|
|
67
|
+
_$insert(_el$3, label);
|
|
68
|
+
_$insert(_el$5, reset);
|
|
69
|
+
_$insertNode(_el$7, _el$8);
|
|
70
|
+
_$insertNode(_el$7, _el$9);
|
|
71
|
+
_$insert(_el$7, () => progressBar(w.remainingPercent), _el$8);
|
|
72
|
+
_$insert(_el$7, () => w.remainingPercent, _el$9);
|
|
73
|
+
_$effect(_p$ => {
|
|
74
|
+
var _v$ = theme.text.muted,
|
|
75
|
+
_v$2 = {
|
|
76
|
+
fg: theme.text.base
|
|
77
|
+
},
|
|
78
|
+
_v$3 = quotaColor(theme, w.remainingPercent);
|
|
79
|
+
_v$ !== _p$.e && (_p$.e = _$setProp(_el$2, "fg", _v$, _p$.e));
|
|
80
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp(_el$5, "style", _v$2, _p$.t));
|
|
81
|
+
_v$3 !== _p$.a && (_p$.a = _$setProp(_el$7, "fg", _v$3, _p$.a));
|
|
82
|
+
return _p$;
|
|
83
|
+
}, {
|
|
84
|
+
e: undefined,
|
|
85
|
+
t: undefined,
|
|
86
|
+
a: undefined
|
|
87
|
+
});
|
|
88
|
+
return _el$;
|
|
89
|
+
})();
|
|
90
|
+
}
|
|
91
|
+
function detailBody(q, err, t, now) {
|
|
92
|
+
const out = ["MiniMax Coding Plan", "─".repeat(32)];
|
|
93
|
+
if (!q) {
|
|
94
|
+
out.push(`error: ${err ?? "loading…"}`);
|
|
95
|
+
} else {
|
|
96
|
+
for (const m of q.models) {
|
|
97
|
+
out.push(m.modelName);
|
|
98
|
+
for (const w of [m.fiveHour, m.weekly]) {
|
|
99
|
+
if (!w) continue;
|
|
100
|
+
out.push(` ${w.label}`);
|
|
101
|
+
out.push(` ${progressBar(w.remainingPercent, 24)} ${w.remainingPercent}%`);
|
|
102
|
+
out.push(w.endTimeMs ? ` reset in ${fmtDuration(w.endTimeMs - now)} (${new Date(w.endTimeMs).toISOString()})` : " reset time unknown");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (q && err) out.push(`last error: ${err}`);
|
|
107
|
+
out.push("─".repeat(32));
|
|
108
|
+
out.push(`Session tokens: ↑ ${t.input} ↓ ${t.output}`);
|
|
109
|
+
if (q) out.push(`fetched: ${fmtAge(q.fetchedAt, now)}`);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
export default Plugin.define({
|
|
113
|
+
id: "@antonkw/opencode-minimax-quota",
|
|
114
|
+
setup(context) {
|
|
115
|
+
const theme = context.theme;
|
|
116
|
+
// Последний успешный снапшот живёт отдельно от ошибки: сетевой сбой не должен стирать квоту.
|
|
117
|
+
const [quota, setQuota] = createSignal(null);
|
|
118
|
+
const [error, setError] = createSignal(undefined);
|
|
119
|
+
const [now, setNow] = createSignal(Date.now());
|
|
120
|
+
const [detail, setDetail] = createSignal([]);
|
|
121
|
+
let stopped = false;
|
|
122
|
+
let disposed = false;
|
|
123
|
+
let failures = 0;
|
|
124
|
+
let delay = CACHE_MS;
|
|
125
|
+
let lastFetch = Date.now();
|
|
126
|
+
let pending;
|
|
127
|
+
const refresh = async () => {
|
|
128
|
+
// Параллельный вызов ждёт уже идущий запрос, а не возвращает старые данные.
|
|
129
|
+
if (pending) return pending;
|
|
130
|
+
lastFetch = Date.now();
|
|
131
|
+
const p = fetchQuota().then(s => {
|
|
132
|
+
if (s.ok) {
|
|
133
|
+
setQuota(s);
|
|
134
|
+
setError(undefined);
|
|
135
|
+
stopped = false;
|
|
136
|
+
failures = 0;
|
|
137
|
+
delay = CACHE_MS;
|
|
138
|
+
} else {
|
|
139
|
+
setError(s.error);
|
|
140
|
+
if (s.fatal) stopped = true;else {
|
|
141
|
+
failures++;
|
|
142
|
+
delay = backoffDelay(failures);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return s;
|
|
146
|
+
});
|
|
147
|
+
pending = p;
|
|
148
|
+
try {
|
|
149
|
+
return await p;
|
|
150
|
+
} finally {
|
|
151
|
+
if (pending === p) pending = undefined;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// SessionInfo.tokens агрегирует всю сессию; input не включает cache, reasoning отдельно.
|
|
156
|
+
const tokensFor = sessionID => {
|
|
157
|
+
const t = sessionID ? context.data.session.get(sessionID)?.tokens : undefined;
|
|
158
|
+
if (!t) return ZERO_TOKENS;
|
|
159
|
+
return {
|
|
160
|
+
input: t.input + t.cache.read + t.cache.write,
|
|
161
|
+
output: t.output + t.reasoning
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
let timer;
|
|
165
|
+
const tick = async () => {
|
|
166
|
+
const t = Date.now();
|
|
167
|
+
setNow(t);
|
|
168
|
+
if (!stopped && t - lastFetch >= delay) {
|
|
169
|
+
await refresh().catch(() => undefined);
|
|
170
|
+
}
|
|
171
|
+
// Teardown мог прийти во время запроса — тогда таймер больше не перевзводим.
|
|
172
|
+
if (disposed) return;
|
|
173
|
+
timer = setTimeout(tick, TICK_MS);
|
|
174
|
+
};
|
|
175
|
+
void refresh();
|
|
176
|
+
timer = setTimeout(tick, TICK_MS);
|
|
177
|
+
|
|
178
|
+
// shortcut: the host applies `session.usage.updated` to its session cache
|
|
179
|
+
// itself, so no explicit sync() — add `context.data.session.sync(id)` on
|
|
180
|
+
// that event if the token counts ever look stale.
|
|
181
|
+
|
|
182
|
+
// The host calls a slot render once and untracked, so every reactive read
|
|
183
|
+
// has to live inside a JSX expression — a value hoisted into a local
|
|
184
|
+
// variable stays frozen at the value it had while mounting.
|
|
185
|
+
const unregisterSidebar = context.ui.slot({
|
|
186
|
+
append: "sidebar.content",
|
|
187
|
+
render: props => {
|
|
188
|
+
const tokens = () => tokensFor(props.sessionID);
|
|
189
|
+
return (() => {
|
|
190
|
+
var _el$0 = _$createElement("box"),
|
|
191
|
+
_el$1 = _$createElement("text"),
|
|
192
|
+
_el$10 = _$createElement("b");
|
|
193
|
+
_$insertNode(_el$0, _el$1);
|
|
194
|
+
_$setProp(_el$0, "flexDirection", "column");
|
|
195
|
+
_$setProp(_el$0, "paddingLeft", 1);
|
|
196
|
+
_$setProp(_el$0, "paddingRight", 1);
|
|
197
|
+
_$setProp(_el$0, "gap", 0);
|
|
198
|
+
_$insertNode(_el$1, _el$10);
|
|
199
|
+
_$insertNode(_el$10, _$createTextNode(`MiniMax Quota`));
|
|
200
|
+
_$insert(_el$0, _$createComponent(Show, {
|
|
201
|
+
get when() {
|
|
202
|
+
return quota();
|
|
203
|
+
},
|
|
204
|
+
get fallback() {
|
|
205
|
+
return (() => {
|
|
206
|
+
var _el$12 = _$createElement("text");
|
|
207
|
+
_$insert(_el$12, () => error() ?? "loading…");
|
|
208
|
+
_$effect(_$p => _$setProp(_el$12, "fg", theme.text.feedback.error.base, _$p));
|
|
209
|
+
return _el$12;
|
|
210
|
+
})();
|
|
211
|
+
},
|
|
212
|
+
children: q => (() => {
|
|
213
|
+
var _el$13 = _$createElement("box"),
|
|
214
|
+
_el$14 = _$createElement("box"),
|
|
215
|
+
_el$15 = _$createElement("box"),
|
|
216
|
+
_el$16 = _$createElement("text"),
|
|
217
|
+
_el$17 = _$createElement("b"),
|
|
218
|
+
_el$19 = _$createElement("text"),
|
|
219
|
+
_el$20 = _$createTextNode(`↑ `),
|
|
220
|
+
_el$21 = _$createTextNode(` ↓ `),
|
|
221
|
+
_el$22 = _$createElement("text"),
|
|
222
|
+
_el$23 = _$createTextNode(`updated `);
|
|
223
|
+
_$insertNode(_el$13, _el$14);
|
|
224
|
+
_$insertNode(_el$13, _el$15);
|
|
225
|
+
_$insertNode(_el$13, _el$22);
|
|
226
|
+
_$setProp(_el$13, "flexDirection", "column");
|
|
227
|
+
_$setProp(_el$13, "gap", 0);
|
|
228
|
+
_$insert(_el$13, _$createComponent(For, {
|
|
229
|
+
get each() {
|
|
230
|
+
return q().models;
|
|
231
|
+
},
|
|
232
|
+
children: model => (() => {
|
|
233
|
+
var _el$24 = _$createElement("box");
|
|
234
|
+
_$setProp(_el$24, "flexDirection", "column");
|
|
235
|
+
_$setProp(_el$24, "gap", 0);
|
|
236
|
+
_$insert(_el$24, _$createComponent(Show, {
|
|
237
|
+
get when() {
|
|
238
|
+
return model.fiveHour;
|
|
239
|
+
},
|
|
240
|
+
children: w => renderWindowBlock(theme, "5h", w(), now())
|
|
241
|
+
}), null);
|
|
242
|
+
_$insert(_el$24, _$createComponent(Show, {
|
|
243
|
+
get when() {
|
|
244
|
+
return _$memo(() => !!model.fiveHour)() && model.weekly;
|
|
245
|
+
},
|
|
246
|
+
get children() {
|
|
247
|
+
var _el$25 = _$createElement("box");
|
|
248
|
+
_$setProp(_el$25, "height", 1);
|
|
249
|
+
return _el$25;
|
|
250
|
+
}
|
|
251
|
+
}), null);
|
|
252
|
+
_$insert(_el$24, _$createComponent(Show, {
|
|
253
|
+
get when() {
|
|
254
|
+
return model.weekly;
|
|
255
|
+
},
|
|
256
|
+
children: w => renderWindowBlock(theme, "7d", w(), now())
|
|
257
|
+
}), null);
|
|
258
|
+
return _el$24;
|
|
259
|
+
})()
|
|
260
|
+
}), _el$14);
|
|
261
|
+
_$setProp(_el$14, "height", 1);
|
|
262
|
+
_$insertNode(_el$15, _el$16);
|
|
263
|
+
_$insertNode(_el$15, _el$19);
|
|
264
|
+
_$setProp(_el$15, "flexDirection", "column");
|
|
265
|
+
_$setProp(_el$15, "gap", 0);
|
|
266
|
+
_$insertNode(_el$16, _el$17);
|
|
267
|
+
_$insertNode(_el$17, _$createTextNode(`Session tokens`));
|
|
268
|
+
_$insertNode(_el$19, _el$20);
|
|
269
|
+
_$insertNode(_el$19, _el$21);
|
|
270
|
+
_$insert(_el$19, () => fmtTokens(tokens().input), _el$21);
|
|
271
|
+
_$insert(_el$19, () => fmtTokens(tokens().output), null);
|
|
272
|
+
_$insertNode(_el$22, _el$23);
|
|
273
|
+
_$insert(_el$22, () => fmtAge(q().fetchedAt, now()), null);
|
|
274
|
+
_$insert(_el$13, _$createComponent(Show, {
|
|
275
|
+
get when() {
|
|
276
|
+
return error();
|
|
277
|
+
},
|
|
278
|
+
children: e => (() => {
|
|
279
|
+
var _el$26 = _$createElement("text");
|
|
280
|
+
_$insert(_el$26, e);
|
|
281
|
+
_$effect(_$p => _$setProp(_el$26, "fg", theme.text.feedback.warning.base, _$p));
|
|
282
|
+
return _el$26;
|
|
283
|
+
})()
|
|
284
|
+
}), null);
|
|
285
|
+
_$effect(_p$ => {
|
|
286
|
+
var _v$4 = theme.text.muted,
|
|
287
|
+
_v$5 = theme.text.base,
|
|
288
|
+
_v$6 = theme.text.muted;
|
|
289
|
+
_v$4 !== _p$.e && (_p$.e = _$setProp(_el$16, "fg", _v$4, _p$.e));
|
|
290
|
+
_v$5 !== _p$.t && (_p$.t = _$setProp(_el$19, "fg", _v$5, _p$.t));
|
|
291
|
+
_v$6 !== _p$.a && (_p$.a = _$setProp(_el$22, "fg", _v$6, _p$.a));
|
|
292
|
+
return _p$;
|
|
293
|
+
}, {
|
|
294
|
+
e: undefined,
|
|
295
|
+
t: undefined,
|
|
296
|
+
a: undefined
|
|
297
|
+
});
|
|
298
|
+
return _el$13;
|
|
299
|
+
})()
|
|
300
|
+
}), null);
|
|
301
|
+
_$effect(_$p => _$setProp(_el$1, "fg", theme.text.base, _$p));
|
|
302
|
+
return _el$0;
|
|
303
|
+
})();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
const unregisterFooter = context.ui.slot({
|
|
307
|
+
append: "prompt.footer.status",
|
|
308
|
+
render: props => {
|
|
309
|
+
const tokens = () => tokensFor(props.sessionID);
|
|
310
|
+
const line = () => compactLine(theme, quota(), error());
|
|
311
|
+
return (() => {
|
|
312
|
+
var _el$27 = _$createElement("box"),
|
|
313
|
+
_el$28 = _$createElement("text"),
|
|
314
|
+
_el$29 = _$createTextNode(`↑`),
|
|
315
|
+
_el$30 = _$createTextNode(` ↓`),
|
|
316
|
+
_el$31 = _$createElement("text");
|
|
317
|
+
_$insertNode(_el$27, _el$28);
|
|
318
|
+
_$insertNode(_el$27, _el$31);
|
|
319
|
+
_$setProp(_el$27, "flexDirection", "row");
|
|
320
|
+
_$setProp(_el$27, "gap", 2);
|
|
321
|
+
_$insertNode(_el$28, _el$29);
|
|
322
|
+
_$insertNode(_el$28, _el$30);
|
|
323
|
+
_$insert(_el$28, () => fmtTokens(tokens().input), _el$30);
|
|
324
|
+
_$insert(_el$28, () => fmtTokens(tokens().output), null);
|
|
325
|
+
_$insert(_el$31, () => line().text);
|
|
326
|
+
_$effect(_p$ => {
|
|
327
|
+
var _v$7 = theme.text.muted,
|
|
328
|
+
_v$8 = line().color;
|
|
329
|
+
_v$7 !== _p$.e && (_p$.e = _$setProp(_el$28, "fg", _v$7, _p$.e));
|
|
330
|
+
_v$8 !== _p$.t && (_p$.t = _$setProp(_el$31, "fg", _v$8, _p$.t));
|
|
331
|
+
return _p$;
|
|
332
|
+
}, {
|
|
333
|
+
e: undefined,
|
|
334
|
+
t: undefined
|
|
335
|
+
});
|
|
336
|
+
return _el$27;
|
|
337
|
+
})();
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
context.keymap.layer(() => ({
|
|
341
|
+
mode: "global",
|
|
342
|
+
commands: [{
|
|
343
|
+
id: "minimax-quota.show",
|
|
344
|
+
title: "MiniMax quota",
|
|
345
|
+
description: "Show MiniMax coding plan quota (refresh + detail)",
|
|
346
|
+
group: "MiniMax",
|
|
347
|
+
palette: true,
|
|
348
|
+
slash: {
|
|
349
|
+
name: "quota-m"
|
|
350
|
+
},
|
|
351
|
+
async run() {
|
|
352
|
+
const route = context.ui.router.current();
|
|
353
|
+
const sessionID = route.type === "session" ? route.sessionID : undefined;
|
|
354
|
+
// Диалог открывается сразу по кэшу, свежие данные подставляются ниже.
|
|
355
|
+
setDetail(detailBody(quota(), error(), tokensFor(sessionID), Date.now()));
|
|
356
|
+
context.ui.dialog.show(() => (() => {
|
|
357
|
+
var _el$32 = _$createElement("box");
|
|
358
|
+
_$setProp(_el$32, "flexDirection", "column");
|
|
359
|
+
_$setProp(_el$32, "paddingLeft", 1);
|
|
360
|
+
_$setProp(_el$32, "paddingRight", 1);
|
|
361
|
+
_$insert(_el$32, _$createComponent(For, {
|
|
362
|
+
get each() {
|
|
363
|
+
return detail();
|
|
364
|
+
},
|
|
365
|
+
children: line => (() => {
|
|
366
|
+
var _el$33 = _$createElement("text");
|
|
367
|
+
_$insert(_el$33, line);
|
|
368
|
+
_$effect(_$p => _$setProp(_el$33, "fg", theme.text.base, _$p));
|
|
369
|
+
return _el$33;
|
|
370
|
+
})()
|
|
371
|
+
}));
|
|
372
|
+
return _el$32;
|
|
373
|
+
})());
|
|
374
|
+
// `show()` resets the dialog to medium size, so the size has to be
|
|
375
|
+
// applied after it.
|
|
376
|
+
context.ui.dialog.set({
|
|
377
|
+
size: "large",
|
|
378
|
+
centered: true
|
|
379
|
+
});
|
|
380
|
+
const s = await refresh();
|
|
381
|
+
setDetail(detailBody(s.ok ? s : quota(), error(), tokensFor(sessionID), Date.now()));
|
|
382
|
+
context.ui.toast.show({
|
|
383
|
+
variant: s.ok ? "info" : "error",
|
|
384
|
+
message: s.ok ? "MiniMax quota refreshed" : `MiniMax quota: ${s.error}`
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}]
|
|
388
|
+
}));
|
|
389
|
+
return () => {
|
|
390
|
+
disposed = true;
|
|
391
|
+
clearTimeout(timer);
|
|
392
|
+
unregisterSidebar();
|
|
393
|
+
unregisterFooter();
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antonkw/opencode-minimax-quota",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "MiniMax coding plan quota in OpenCode TUI sidebar + status line",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "KVAnton-WEB <kvanton.web@gmail.com>",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/KVAnton-WEB/opencode-minimax-quota.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"opencode",
|
|
14
|
+
"opencode-plugin",
|
|
15
|
+
"minimax",
|
|
16
|
+
"quota",
|
|
17
|
+
"tui"
|
|
18
|
+
],
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/tui.js",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./tui": {
|
|
28
|
+
"types": "./dist/tui.d.ts",
|
|
29
|
+
"default": "./dist/tui.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"opencode": "^2.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "node scripts/build.mjs",
|
|
37
|
+
"check:dist": "node scripts/build.mjs && git diff --exit-code HEAD -- dist",
|
|
38
|
+
"watch": "node scripts/build.mjs --watch",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "node --test \"test/**/*.test.ts\""
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@opencode/plugin": "^2.0.0",
|
|
44
|
+
"@opentui/core": ">=0.5.17",
|
|
45
|
+
"@opentui/solid": ">=0.5.17",
|
|
46
|
+
"solid-js": ">=1.9.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@babel/core": "^7.28.0",
|
|
50
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
51
|
+
"@opencode/plugin": "^2.0.28",
|
|
52
|
+
"@opencode/theme": "^2.0.28",
|
|
53
|
+
"@opentui/core": "^0.5.17",
|
|
54
|
+
"@opentui/solid": "^0.5.17",
|
|
55
|
+
"@types/node": "^22.0.0",
|
|
56
|
+
"babel-preset-solid": "1.9.12",
|
|
57
|
+
"solid-js": "1.9.12",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
59
|
+
}
|
|
60
|
+
}
|