@polderlabs/bizar 4.4.13 → 4.5.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.
Files changed (90) hide show
  1. package/bizar-dash/CHANGELOG.md +37 -276
  2. package/bizar-dash/dist/assets/main-CDFKHzBg.css +1 -0
  3. package/bizar-dash/dist/assets/main-NYFpS2wY.js +312 -0
  4. package/bizar-dash/dist/assets/main-NYFpS2wY.js.map +1 -0
  5. package/bizar-dash/dist/assets/{mobile-DSb-t42Y.js → mobile--0FBIKX3.js} +2 -2
  6. package/bizar-dash/dist/assets/{mobile-DSb-t42Y.js.map → mobile--0FBIKX3.js.map} +1 -1
  7. package/bizar-dash/dist/assets/mobile-OgRp8VIb.js +352 -0
  8. package/bizar-dash/dist/assets/mobile-OgRp8VIb.js.map +1 -0
  9. package/bizar-dash/dist/index.html +3 -3
  10. package/bizar-dash/dist/mobile.html +2 -2
  11. package/bizar-dash/skills/agent-baseline/SKILL.md +80 -0
  12. package/bizar-dash/skills/bizar/SKILL.md +96 -0
  13. package/bizar-dash/skills/chat/SKILL.md +74 -0
  14. package/bizar-dash/skills/lightrag/SKILL.md +75 -0
  15. package/bizar-dash/skills/minimax/SKILL.md +80 -0
  16. package/bizar-dash/skills/obsidian/SKILL.md +55 -0
  17. package/bizar-dash/skills/providers/SKILL.md +75 -0
  18. package/bizar-dash/skills/sdk/SKILL.md +138 -0
  19. package/bizar-dash/skills/self-improvement/SKILL.md +53 -0
  20. package/bizar-dash/skills/skills-cli/SKILL.md +94 -0
  21. package/bizar-dash/skills/usage/SKILL.md +62 -0
  22. package/bizar-dash/src/server/api.mjs +12 -0
  23. package/bizar-dash/src/server/memory-lightrag.mjs +5 -2
  24. package/bizar-dash/src/server/memory-store.mjs +38 -0
  25. package/bizar-dash/src/server/minimax-usage-store.mjs +372 -0
  26. package/bizar-dash/src/server/minimax.mjs +196 -5
  27. package/bizar-dash/src/server/providers-store.mjs +956 -0
  28. package/bizar-dash/src/server/routes/config.mjs +52 -1
  29. package/bizar-dash/src/server/routes/env-vars.mjs +165 -0
  30. package/bizar-dash/src/server/routes/lightrag.mjs +154 -0
  31. package/bizar-dash/src/server/routes/memory.mjs +241 -1
  32. package/bizar-dash/src/server/routes/opencode-session-detail.mjs +14 -29
  33. package/bizar-dash/src/server/routes/opencode-sessions.mjs +205 -3
  34. package/bizar-dash/src/server/routes/providers.mjs +266 -5
  35. package/bizar-dash/src/server/routes/skills.mjs +32 -43
  36. package/bizar-dash/src/server/routes/update.mjs +340 -0
  37. package/bizar-dash/src/server/routes/usage.mjs +136 -0
  38. package/bizar-dash/src/server/serve-info.mjs +135 -4
  39. package/bizar-dash/src/server/server.mjs +4 -0
  40. package/bizar-dash/src/server/skills-store.mjs +152 -262
  41. package/bizar-dash/src/web/App.tsx +118 -29
  42. package/bizar-dash/src/web/components/EnvVarManager.tsx +247 -0
  43. package/bizar-dash/src/web/components/SettingsSearch.tsx +213 -0
  44. package/bizar-dash/src/web/components/Topbar.tsx +0 -1
  45. package/bizar-dash/src/web/components/UsageChart.tsx +250 -0
  46. package/bizar-dash/src/web/components/UsageTable.tsx +90 -0
  47. package/bizar-dash/src/web/components/chat/ChatComposer.tsx +21 -25
  48. package/bizar-dash/src/web/components/chat/ChatInfoPanel.tsx +199 -37
  49. package/bizar-dash/src/web/components/chat/ChatThread.tsx +29 -17
  50. package/bizar-dash/src/web/components/chat/FloatingComposer.tsx +7 -1
  51. package/bizar-dash/src/web/components/chat/InfoPanel.tsx +71 -6
  52. package/bizar-dash/src/web/components/chat/useChat.ts +751 -257
  53. package/bizar-dash/src/web/lib/api.ts +43 -0
  54. package/bizar-dash/src/web/main.tsx +1 -0
  55. package/bizar-dash/src/web/mobile/views/MobileChat.tsx +110 -35
  56. package/bizar-dash/src/web/styles/chat.css +135 -1
  57. package/bizar-dash/src/web/styles/main.css +46 -0
  58. package/bizar-dash/src/web/styles/minimax-usage.css +335 -0
  59. package/bizar-dash/src/web/styles/settings.css +418 -0
  60. package/bizar-dash/src/web/styles/skills.css +302 -0
  61. package/bizar-dash/src/web/styles/tasks.css +288 -0
  62. package/bizar-dash/src/web/views/Chat.tsx +276 -48
  63. package/bizar-dash/src/web/views/Config.tsx +3 -2065
  64. package/bizar-dash/src/web/views/MiniMaxUsage.tsx +476 -461
  65. package/bizar-dash/src/web/views/Settings.tsx +6 -0
  66. package/bizar-dash/src/web/views/Skills.tsx +208 -260
  67. package/bizar-dash/src/web/views/Tasks.tsx +348 -1119
  68. package/bizar-dash/tests/chat-session-create.test.mjs +391 -0
  69. package/bizar-dash/tests/chat-session-stream.test.mjs +308 -0
  70. package/bizar-dash/tests/env-vars-store.test.mjs +216 -0
  71. package/bizar-dash/tests/lightrag-defaults.node.test.mjs +118 -0
  72. package/bizar-dash/tests/minimax-chat-usage.test.mjs +178 -0
  73. package/bizar-dash/tests/minimax-usage-store.node.test.mjs +293 -0
  74. package/bizar-dash/tests/opencode-sessions-detail.test.mjs +12 -9
  75. package/bizar-dash/tests/providers-store-backup-keys.node.test.mjs +479 -3
  76. package/bizar-dash/tests/providers-store-search.node.test.mjs +166 -0
  77. package/bizar-dash/tests/skills-list.test.mjs +232 -0
  78. package/bizar-dash/tests/skills-search.test.mjs +222 -0
  79. package/bizar-dash/tests/tasks-create.test.mjs +187 -0
  80. package/bizar-dash/tests/update-check.test.mjs +127 -0
  81. package/bizar-dash/tests/update-run.test.mjs +266 -0
  82. package/cli/bin.mjs +82 -1
  83. package/cli/provision.mjs +118 -4
  84. package/config/agents/_shared/SKILLS.md +109 -0
  85. package/package.json +1 -1
  86. package/bizar-dash/dist/assets/main-BB5mJurD.js +0 -352
  87. package/bizar-dash/dist/assets/main-BB5mJurD.js.map +0 -1
  88. package/bizar-dash/dist/assets/main-BsnQLXdh.css +0 -1
  89. package/bizar-dash/dist/assets/mobile-Dl1q7Cyq.js +0 -354
  90. package/bizar-dash/dist/assets/mobile-Dl1q7Cyq.js.map +0 -1
@@ -0,0 +1,372 @@
1
+ /**
2
+ * src/server/minimax-usage-store.mjs
3
+ *
4
+ * JSONL append-only usage log at ~/.local/share/bizar/usage.jsonl.
5
+ *
6
+ * Records every MiniMax API call (chatCompletion, fetchRemains) so that:
7
+ * - The Usage view can show per-model/per-key analytics over 24h/7d/30d
8
+ * - Agents can read their own rolling usage totals via getUsageLimitsForAgent()
9
+ * to avoid burning through quota mid-session
10
+ * - Approximate USD cost estimates can be surfaced
11
+ *
12
+ * Record shape:
13
+ * {
14
+ * ts: 1234567890, // unix ms
15
+ * providerId: "minimax",
16
+ * modelId: "MiniMax-M3",
17
+ * endpoint: "chat" | "remains" | "test",
18
+ * requestId: "msg_abc",
19
+ * promptTokens: 100,
20
+ * completionTokens: 200,
21
+ * totalTokens: 300,
22
+ * cachedTokens: 0,
23
+ * reasoningTokens: 50,
24
+ * latencyMs: 1234,
25
+ * finishReason: "stop",
26
+ * error: null | {code: "...", message: "..."},
27
+ * keyEnvVar: "BIZAR_MINIMAX_KEY", // which env var / key slot was used
28
+ * isBackup: false,
29
+ * cached: false // true = returned from cache; NOT counted in cost
30
+ * }
31
+ *
32
+ * Exports:
33
+ * recordUsage(record) — append JSON line
34
+ * queryUsage({range, from, to, providerId, modelId})
35
+ * getUsageSummary(providerId) — last-5-min rolling totals
36
+ * getUsageLimitsForAgent(providerId) — agent-awareness compact summary
37
+ * pruneUsage({olderThanMs}) — remove old records
38
+ * __resetStoreForTests() — wipe the JSONL (tests only)
39
+ */
40
+
41
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, unlinkSync } from 'node:fs';
42
+ import { join } from 'node:path';
43
+ import { homedir } from 'node:os';
44
+
45
+ // ─── Price map (USD per 1M tokens) ────────────────────────────────────────
46
+
47
+ /** @type {Record<string, {in: number, out: number}>} */
48
+ export const PRICE_PER_MTOK = {
49
+ 'minimax/MiniMax-M3': { in: 1.00, out: 3.00 },
50
+ 'minimax/MiniMax-M2.7': { in: 0.86, out: 2.59 },
51
+ 'minimax/MiniMax-M2.7-highspeed':{ in: 1.20, out: 3.60 },
52
+ 'minimax/MiniMax-M2.5': { in: 0.59, out: 1.77 },
53
+ 'minimax/MiniMax-M2.5-highspeed':{ in: 0.86, out: 2.59 },
54
+ 'minimax/MiniMax-M2.1': { in: 0.40, out: 1.20 },
55
+ 'minimax/MiniMax-M2.1-highspeed': { in: 0.58, out: 1.74 },
56
+ 'minimax/MiniMax-M2': { in: 0.20, out: 0.60 },
57
+ };
58
+
59
+ // ─── Store path ─────────────────────────────────────────────────────────────
60
+
61
+ // Allow test override without patching process.env.HOME (which can be racy with
62
+ // ESM module-level evaluation order in the Node test runner).
63
+ const STORE_HOME = process.env.BIZAR_STORE_HOME
64
+ ? process.env.BIZAR_STORE_HOME
65
+ : join(homedir(), '.local', 'share', 'bizar');
66
+ const STORE_DIR = STORE_HOME;
67
+ const STORE_FILE = join(STORE_DIR, 'usage.jsonl');
68
+
69
+ function ensureStoreDir() {
70
+ // mode 0o700 — Node.js v24.16.0 has a bug where recursive+mode:0o600 fails with
71
+ // EACCES in /tmp temp dirs even with umask 0o022; 0o700 is fine here.
72
+ try { mkdirSync(STORE_DIR, { recursive: true, mode: 0o700 }); } catch { /* already exists */ }
73
+ }
74
+
75
+ // ─── Cost estimation ─────────────────────────────────────────────────────────
76
+
77
+ /** Approximate USD cost for a usage record. Returns 0 if model is unknown. */
78
+ function estimateCost(record) {
79
+ if (record.cached || record.error) return 0;
80
+ const key = `minimax/${record.modelId}`;
81
+ const price = PRICE_PER_MTOK[key];
82
+ if (!price) return 0;
83
+ const p = record.promptTokens ?? 0;
84
+ const c = record.completionTokens ?? 0;
85
+ return (p / 1_000_000) * price.in + (c / 1_000_000) * price.out;
86
+ }
87
+
88
+ // ─── Record ─────────────────────────────────────────────────────────────────
89
+
90
+ /**
91
+ * Append a usage record to the JSONL store.
92
+ * Auto-creates the parent directory on first call.
93
+ * @param {object} record
94
+ */
95
+ export function recordUsage(record) {
96
+ ensureStoreDir();
97
+ const line = JSON.stringify(record) + '\n';
98
+ appendFileSync(STORE_FILE, line, 'utf8');
99
+ }
100
+
101
+ // ─── Parse helpers ───────────────────────────────────────────────────────────
102
+
103
+ function parseLine(line) {
104
+ const trimmed = line.trim();
105
+ if (!trimmed || trimmed.startsWith('#')) return null;
106
+ try { return JSON.parse(trimmed); } catch { return null; }
107
+ }
108
+
109
+ /** @returns {object[]} */
110
+ export function readAllRecords() {
111
+ if (!existsSync(STORE_FILE)) return [];
112
+ try {
113
+ const raw = readFileSync(STORE_FILE, 'utf8');
114
+ return raw.split('\n').map(parseLine).filter(r => r !== null);
115
+ } catch { return []; }
116
+ }
117
+
118
+ // ─── Range helpers ───────────────────────────────────────────────────────────
119
+
120
+ function msForRange(range) {
121
+ const map = { '24h': 86_400_000, '7d': 604_800_000, '30d': 2_592_600_000 };
122
+ return map[range] ?? 86_400_000;
123
+ }
124
+
125
+ function dateStr(ts) {
126
+ const d = new Date(ts);
127
+ return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
128
+ }
129
+
130
+ // ─── Aggregation helpers ─────────────────────────────────────────────────────
131
+
132
+ function computeTotals(records) {
133
+ if (records.length === 0) {
134
+ return { requests: 0, errors: 0, promptTokens: 0, completionTokens: 0,
135
+ totalTokens: 0, cachedTokens: 0, reasoningTokens: 0,
136
+ avgLatencyMs: 0, p95LatencyMs: 0, costEstimate: 0 };
137
+ }
138
+ const errors = records.filter(r => r.error !== null).length;
139
+ const latencies = records.map(r => r.latencyMs).filter(l => l >= 0).sort((a, b) => a - b);
140
+ const avg = latencies.length ? latencies.reduce((a, b) => a + b, 0) / latencies.length : 0;
141
+ const p95Idx = Math.floor(latencies.length * 0.95);
142
+ const p95 = latencies[p95Idx] ?? 0;
143
+ const cost = records.reduce((sum, r) => sum + estimateCost(r), 0);
144
+ return {
145
+ requests: records.length,
146
+ errors,
147
+ promptTokens: records.reduce((s, r) => s + (r.promptTokens ?? 0), 0),
148
+ completionTokens: records.reduce((s, r) => s + (r.completionTokens ?? 0), 0),
149
+ totalTokens: records.reduce((s, r) => s + (r.totalTokens ?? 0), 0),
150
+ cachedTokens: records.reduce((s, r) => s + (r.cachedTokens ?? 0), 0),
151
+ reasoningTokens: records.reduce((s, r) => s + (r.reasoningTokens ?? 0), 0),
152
+ avgLatencyMs: Math.round(avg),
153
+ p95LatencyMs: Math.round(p95),
154
+ costEstimate: Math.round(cost * 100_000) / 100_000,
155
+ };
156
+ }
157
+
158
+ function computeDaily(records) {
159
+ const byDate = new Map();
160
+ for (const r of records) {
161
+ const d = dateStr(r.ts);
162
+ if (!byDate.has(d)) byDate.set(d, []);
163
+ byDate.get(d).push(r);
164
+ }
165
+ return Array.from(byDate.entries())
166
+ .sort(([a], [b]) => a.localeCompare(b))
167
+ .map(([date, recs]) => {
168
+ const t = computeTotals(recs);
169
+ return { date, requests: t.requests, totalTokens: t.totalTokens,
170
+ promptTokens: t.promptTokens, completionTokens: t.completionTokens,
171
+ errors: t.errors, avgLatencyMs: t.avgLatencyMs };
172
+ });
173
+ }
174
+
175
+ function computePerModel(records) {
176
+ const byModel = new Map();
177
+ for (const r of records) {
178
+ const key = `${r.providerId}::${r.modelId}`;
179
+ if (!byModel.has(key)) byModel.set(key, []);
180
+ byModel.get(key).push(r);
181
+ }
182
+ return Array.from(byModel.entries())
183
+ .map(([key, recs]) => {
184
+ const [providerId, modelId] = key.split('::');
185
+ const t = computeTotals(recs);
186
+ return { providerId, modelId, requests: t.requests, totalTokens: t.totalTokens,
187
+ promptTokens: t.promptTokens, completionTokens: t.completionTokens,
188
+ errors: t.errors, avgLatencyMs: t.avgLatencyMs };
189
+ })
190
+ .sort((a, b) => b.requests - a.requests);
191
+ }
192
+
193
+ function computePerKey(records) {
194
+ const byKey = new Map();
195
+ for (const r of records) {
196
+ const key = `${r.keyEnvVar}::${r.isBackup}`;
197
+ if (!byKey.has(key)) byKey.set(key, []);
198
+ byKey.get(key).push(r);
199
+ }
200
+ return Array.from(byKey.entries())
201
+ .map(([key, recs]) => {
202
+ const [keyEnvVar, isBackupStr] = key.split('::');
203
+ const isBackup = isBackupStr === 'true';
204
+ return {
205
+ keyEnvVar,
206
+ isBackup,
207
+ requests: recs.length,
208
+ errors: recs.filter(r => r.error !== null).length,
209
+ lastUsed: recs.length ? Math.max(...recs.map(r => r.ts)) : null,
210
+ status: isBackup ? 'backup' : 'active',
211
+ };
212
+ })
213
+ .sort((a, b) => (b.lastUsed ?? 0) - (a.lastUsed ?? 0));
214
+ }
215
+
216
+ function computeErrors(records) {
217
+ const byError = new Map();
218
+ for (const r of records) {
219
+ if (!r.error) continue;
220
+ const key = `${r.error.code}::${r.error.message}`;
221
+ if (!byError.has(key)) byError.set(key, { code: r.error.code, message: r.error.message, recs: [] });
222
+ byError.get(key).recs.push(r);
223
+ }
224
+ return Array.from(byError.values())
225
+ .map(({ code, message, recs }) => ({
226
+ code, message,
227
+ count: recs.length,
228
+ lastOccurred: recs.length ? Math.max(...recs.map(r => r.ts)) : null,
229
+ }))
230
+ .sort((a, b) => b.count - a.count);
231
+ }
232
+
233
+ // ─── Query ──────────────────────────────────────────────────────────────────
234
+
235
+ /**
236
+ * @param {object} opts
237
+ * @param {'24h'|'7d'|'30d'|'custom'} [opts.range='24h']
238
+ * @param {number} [opts.from] — unix ms, required when range === 'custom'
239
+ * @param {number} [opts.to] — unix ms, required when range === 'custom'
240
+ * @param {string} [opts.providerId]
241
+ * @param {string} [opts.modelId]
242
+ */
243
+ export function queryUsage(opts = {}) {
244
+ const { range = '24h', from, to, providerId, modelId } = opts;
245
+
246
+ let fromMs;
247
+ let toMs;
248
+ if (range === 'custom') {
249
+ if (typeof from !== 'number' || typeof to !== 'number') {
250
+ throw new Error('range=custom requires `from` and `to` (unix ms)');
251
+ }
252
+ fromMs = from;
253
+ toMs = to;
254
+ } else {
255
+ toMs = Date.now();
256
+ fromMs = toMs - msForRange(range);
257
+ }
258
+
259
+ let records = readAllRecords()
260
+ .filter(r => r.ts >= fromMs && r.ts <= toMs);
261
+
262
+ if (providerId) records = records.filter(r => r.providerId === providerId);
263
+ if (modelId) records = records.filter(r => r.modelId === modelId);
264
+
265
+ return {
266
+ totals: computeTotals(records),
267
+ daily: computeDaily(records),
268
+ perModel: computePerModel(records),
269
+ perKey: computePerKey(records),
270
+ errors: computeErrors(records),
271
+ };
272
+ }
273
+
274
+ // ─── Rolling summary (for agents) ──────────────────────────────────────────
275
+
276
+ /** Last-5-minute rolling window totals — used for "agents know their limits". */
277
+ export function getUsageSummary(providerId = 'minimax') {
278
+ const cutoff = Date.now() - 5 * 60 * 1000;
279
+ const records = readAllRecords()
280
+ .filter(r => r.providerId === providerId && r.ts >= cutoff);
281
+ const t = computeTotals(records);
282
+ return { requests: t.requests, tokens: t.totalTokens, errors: t.errors, avgLatencyMs: t.avgLatencyMs };
283
+ }
284
+
285
+ // ─── Agent awareness ─────────────────────────────────────────────────────────
286
+
287
+ /**
288
+ * Compact usage summary for injection into an agent's system prompt context.
289
+ * @param {string} [providerId='minimax']
290
+ */
291
+ export async function getUsageLimitsForAgent(providerId = 'minimax') {
292
+ const now = Date.now();
293
+ const cutoff5m = now - 5 * 60 * 1000;
294
+ const cutoff24h = now - 86_400_000;
295
+
296
+ const all = readAllRecords().filter(r => r.providerId === providerId);
297
+
298
+ const last5min = all.filter(r => r.ts >= cutoff5m);
299
+ const last24h = all.filter(r => r.ts >= cutoff24h);
300
+
301
+ const t5 = computeTotals(last5min);
302
+ const t24 = computeTotals(last24h);
303
+
304
+ // Heuristic limits: 1000 requests / 1M tokens per 24h for the free-ish tier.
305
+ const limits = { dailyRequests: 1000, dailyTokens: 1_000_000 };
306
+
307
+ const percentUsed24h = limits.dailyTokens > 0
308
+ ? Math.round((t24.totalTokens / limits.dailyTokens) * 1000) / 10
309
+ : 0;
310
+
311
+ /** @type {null|string} */
312
+ let warning = null;
313
+ if (percentUsed24h >= 80) warning = 'approaching_daily_limit';
314
+ if (last24h.filter(r => r.isBackup).length > last24h.length * 0.5) {
315
+ warning = 'key_cycling';
316
+ }
317
+
318
+ // Estimate time until midnight UTC reset.
319
+ const midnightUtc = new Date();
320
+ midnightUtc.setUTCHours(0, 0, 0, 0);
321
+ midnightUtc.setUTCDate(midnightUtc.getUTCDate() + 1);
322
+ const msUntilReset = midnightUtc.getTime() - now;
323
+ const hr = Math.floor(msUntilReset / 3_600_000);
324
+ const min = Math.floor((msUntilReset % 3_600_000) / 60_000);
325
+ const estimatedTimeUntilReset = msUntilReset > 0 ? `${hr}h ${min}m` : null;
326
+
327
+ return {
328
+ provider: providerId,
329
+ requestsLast5min: t5.requests,
330
+ tokensLast5min: t5.totalTokens,
331
+ requestsLast24h: t24.requests,
332
+ tokensLast24h: t24.totalTokens,
333
+ limits,
334
+ percentUsed24h,
335
+ estimatedTimeUntilReset,
336
+ warning,
337
+ };
338
+ }
339
+
340
+ // ─── Prune ──────────────────────────────────────────────────────────────────
341
+
342
+ /**
343
+ * Remove records older than `olderThanMs` from the JSONL.
344
+ * @param {{olderThanMs?: number}} [opts]
345
+ */
346
+ export function pruneUsage(opts = {}) {
347
+ const { olderThanMs = Infinity } = opts;
348
+ if (olderThanMs === Infinity) return 0;
349
+ const cutoff = Date.now() - olderThanMs;
350
+ const all = readAllRecords();
351
+ const keep = all.filter(r => r.ts >= cutoff);
352
+ const removed = all.length - keep.length;
353
+ if (removed === 0) return 0;
354
+ // Rewrite without the pruned records.
355
+ ensureStoreDir();
356
+ const tmp = STORE_FILE + '.tmp';
357
+ const lines = keep.map(r => JSON.stringify(r)).join('\n') + '\n';
358
+ writeFileSync(tmp, lines, 'utf8');
359
+ try { unlinkSync(STORE_FILE); } catch { /* ignore */ }
360
+ try { require('node:fs').renameSync(tmp, STORE_FILE); } catch {
361
+ writeFileSync(STORE_FILE, lines, 'utf8');
362
+ try { unlinkSync(tmp); } catch { /* ignore */ }
363
+ }
364
+ return removed;
365
+ }
366
+
367
+ // ─── Test reset ─────────────────────────────────────────────────────────────
368
+
369
+ /** Wipes the entire JSONL. For tests only. */
370
+ export function __resetStoreForTests() {
371
+ try { unlinkSync(STORE_FILE); } catch { /* ignore */ }
372
+ }
@@ -308,11 +308,31 @@ export async function fetchRemains({ force = false } = {}) {
308
308
  if (!force) {
309
309
  const cached = readCachedRemains();
310
310
  if (cached && cached.apiKeyHint === maskKey(resolved.key) && (Date.now() - cached.fetchedAt) < 60_000) {
311
+ // Cached — still record it so usage totals stay honest.
312
+ await recordUsageDynamic({
313
+ providerId: 'minimax',
314
+ modelId: 'unknown',
315
+ endpoint: 'remains',
316
+ requestId: `rem_${Math.random().toString(36).slice(2, 9)}`,
317
+ promptTokens: 0,
318
+ completionTokens: 0,
319
+ totalTokens: 0,
320
+ cachedTokens: 0,
321
+ reasoningTokens: 0,
322
+ latencyMs: 0,
323
+ finishReason: null,
324
+ error: null,
325
+ keyEnvVar: resolved.source,
326
+ isBackup: false,
327
+ cached: true,
328
+ });
311
329
  return { ok: true, cached: true, ...cached };
312
330
  }
313
331
  }
314
332
  const urls = resolveBaseUrls();
315
333
  const url = `${urls.tokenBase.replace(/\/$/, '')}/v1/token_plan/remains?group_id=${encodeURIComponent(resolved.groupId)}`;
334
+ const requestId = `rem_${Math.random().toString(36).slice(2, 9)}`;
335
+ const startMs = Date.now();
316
336
  let resp;
317
337
  try {
318
338
  resp = await fetchWithTimeout(url, {
@@ -324,28 +344,102 @@ export async function fetchRemains({ force = false } = {}) {
324
344
  },
325
345
  });
326
346
  } catch (err) {
347
+ const latencyMs = Date.now() - startMs;
348
+ await recordUsageDynamic({
349
+ providerId: 'minimax',
350
+ modelId: 'unknown',
351
+ endpoint: 'remains',
352
+ requestId,
353
+ promptTokens: 0,
354
+ completionTokens: 0,
355
+ totalTokens: 0,
356
+ cachedTokens: 0,
357
+ reasoningTokens: 0,
358
+ latencyMs,
359
+ finishReason: null,
360
+ error: { code: 'network_error', message: err.message || 'fetch failed' },
361
+ keyEnvVar: resolved.source,
362
+ isBackup: false,
363
+ cached: false,
364
+ });
327
365
  return { ok: false, error: 'network_error', message: err.message || 'fetch failed' };
328
366
  }
367
+ const latencyMs = Date.now() - startMs;
329
368
  const text = await resp.text();
330
369
  let body = null;
331
370
  try { body = text ? JSON.parse(text) : null; } catch { /* keep as null */ }
332
371
  if (!resp.ok) {
372
+ const errMsg = body?.base_resp?.status_msg || resp.statusText || 'request failed';
373
+ await recordUsageDynamic({
374
+ providerId: 'minimax',
375
+ modelId: 'unknown',
376
+ endpoint: 'remains',
377
+ requestId,
378
+ promptTokens: 0,
379
+ completionTokens: 0,
380
+ totalTokens: 0,
381
+ cachedTokens: 0,
382
+ reasoningTokens: 0,
383
+ latencyMs,
384
+ finishReason: null,
385
+ error: { code: `http_${resp.status}`, message: errMsg },
386
+ keyEnvVar: resolved.source,
387
+ isBackup: false,
388
+ cached: false,
389
+ });
333
390
  return {
334
391
  ok: false,
335
392
  error: `http_${resp.status}`,
336
- message: body?.base_resp?.status_msg || resp.statusText || 'request failed',
393
+ message: errMsg,
337
394
  status: resp.status,
338
395
  };
339
396
  }
340
397
  if (!body || body.base_resp?.status_code !== 0) {
398
+ const errMsg = body?.base_resp?.status_msg || 'unknown error';
399
+ await recordUsageDynamic({
400
+ providerId: 'minimax',
401
+ modelId: 'unknown',
402
+ endpoint: 'remains',
403
+ requestId,
404
+ promptTokens: 0,
405
+ completionTokens: 0,
406
+ totalTokens: 0,
407
+ cachedTokens: 0,
408
+ reasoningTokens: 0,
409
+ latencyMs,
410
+ finishReason: null,
411
+ error: { code: 'api_error', message: errMsg },
412
+ keyEnvVar: resolved.source,
413
+ isBackup: false,
414
+ cached: false,
415
+ });
341
416
  return {
342
417
  ok: false,
343
418
  error: 'api_error',
344
- message: body?.base_resp?.status_msg || 'unknown error',
419
+ message: errMsg,
345
420
  raw: body,
346
421
  };
347
422
  }
348
423
 
424
+ // Record success for remains (no token metering — it's a quota check).
425
+ await recordUsageDynamic({
426
+ providerId: 'minimax',
427
+ modelId: 'unknown',
428
+ endpoint: 'remains',
429
+ requestId,
430
+ promptTokens: 0,
431
+ completionTokens: 0,
432
+ totalTokens: 0,
433
+ cachedTokens: 0,
434
+ reasoningTokens: 0,
435
+ latencyMs,
436
+ finishReason: null,
437
+ error: null,
438
+ keyEnvVar: resolved.source,
439
+ isBackup: false,
440
+ cached: false,
441
+ });
442
+
349
443
  // Augment with ISO timestamps + human labels for the UI.
350
444
  const models = (body.model_remains || []).map((m) => ({
351
445
  ...m,
@@ -403,6 +497,8 @@ export async function chatCompletion({
403
497
  max_completion_tokens: maxTokens,
404
498
  stream: false,
405
499
  });
500
+ const requestId = `msg_${Math.random().toString(36).slice(2, 9)}`;
501
+ const startMs = Date.now();
406
502
  let resp;
407
503
  try {
408
504
  resp = await fetchWithTimeout(url, {
@@ -415,41 +511,136 @@ export async function chatCompletion({
415
511
  body,
416
512
  });
417
513
  } catch (err) {
514
+ // Record error usage.
515
+ await recordUsageDynamic({
516
+ providerId: 'minimax',
517
+ modelId: model,
518
+ endpoint: 'chat',
519
+ requestId,
520
+ promptTokens: 0,
521
+ completionTokens: 0,
522
+ totalTokens: 0,
523
+ cachedTokens: 0,
524
+ reasoningTokens: 0,
525
+ latencyMs: Date.now() - startMs,
526
+ finishReason: null,
527
+ error: { code: 'network_error', message: err.message || 'fetch failed' },
528
+ keyEnvVar: resolved.source,
529
+ isBackup: false,
530
+ cached: false,
531
+ });
418
532
  return { ok: false, error: 'network_error', message: err.message || 'fetch failed' };
419
533
  }
534
+ const latencyMs = Date.now() - startMs;
420
535
  const text = await resp.text();
421
536
  let data = null;
422
537
  try { data = text ? JSON.parse(text) : null; } catch { /* ignore */ }
423
538
  if (!resp.ok) {
539
+ const errMsg = data?.base_resp?.status_msg || resp.statusText || 'request failed';
540
+ // Record error usage.
541
+ await recordUsageDynamic({
542
+ providerId: 'minimax',
543
+ modelId: model,
544
+ endpoint: 'chat',
545
+ requestId,
546
+ promptTokens: 0,
547
+ completionTokens: 0,
548
+ totalTokens: 0,
549
+ cachedTokens: 0,
550
+ reasoningTokens: 0,
551
+ latencyMs,
552
+ finishReason: null,
553
+ error: { code: `http_${resp.status}`, message: errMsg },
554
+ keyEnvVar: resolved.source,
555
+ isBackup: false,
556
+ cached: false,
557
+ });
424
558
  return {
425
559
  ok: false,
426
560
  error: `http_${resp.status}`,
427
- message: data?.base_resp?.status_msg || resp.statusText || 'request failed',
561
+ message: errMsg,
428
562
  status: resp.status,
429
563
  raw: data,
430
564
  };
431
565
  }
432
566
  if (!data || data.base_resp?.status_code !== 0) {
567
+ const errMsg = data?.base_resp?.status_msg || 'unknown error';
568
+ // Record error usage.
569
+ await recordUsageDynamic({
570
+ providerId: 'minimax',
571
+ modelId: model,
572
+ endpoint: 'chat',
573
+ requestId,
574
+ promptTokens: 0,
575
+ completionTokens: 0,
576
+ totalTokens: 0,
577
+ cachedTokens: 0,
578
+ reasoningTokens: 0,
579
+ latencyMs,
580
+ finishReason: null,
581
+ error: { code: 'api_error', message: errMsg },
582
+ keyEnvVar: resolved.source,
583
+ isBackup: false,
584
+ cached: false,
585
+ });
433
586
  return {
434
587
  ok: false,
435
588
  error: 'api_error',
436
- message: data?.base_resp?.status_msg || 'unknown error',
589
+ message: errMsg,
437
590
  raw: data,
438
591
  };
439
592
  }
440
593
  const choice = (data.choices || [])[0] || {};
441
594
  const content = choice?.message?.content || '';
595
+ const usage = data.usage || null;
596
+ const promptTokens = usage?.prompt_tokens ?? 0;
597
+ const completionTokens = usage?.completion_tokens ?? 0;
598
+ const totalTokens = usage?.total_tokens ?? promptTokens + completionTokens;
599
+ const cachedTokens = usage?.prompt_tokens_details?.cached_tokens ?? 0;
600
+ const reasoningTokens = usage?.completion_tokens_details?.reasoning_tokens ?? 0;
601
+
602
+ // Record success usage.
603
+ await recordUsageDynamic({
604
+ providerId: 'minimax',
605
+ modelId: data.model || model,
606
+ endpoint: 'chat',
607
+ requestId,
608
+ promptTokens,
609
+ completionTokens,
610
+ totalTokens,
611
+ cachedTokens,
612
+ reasoningTokens,
613
+ latencyMs,
614
+ finishReason: choice?.finish_reason || null,
615
+ error: null,
616
+ keyEnvVar: resolved.source,
617
+ isBackup: false,
618
+ cached: false,
619
+ });
620
+
442
621
  return {
443
622
  ok: true,
444
623
  model: data.model || model,
445
624
  content,
446
625
  reasoning: choice?.message?.reasoning_content || null,
447
626
  finishReason: choice?.finish_reason || null,
448
- usage: data.usage || null,
627
+ usage,
449
628
  baseResp: data.base_resp,
450
629
  };
451
630
  }
452
631
 
632
+ /**
633
+ * Dynamically import the usage store and record a usage entry.
634
+ * Separated from chatCompletion() to avoid a circular import.
635
+ * @param {object} record
636
+ */
637
+ async function recordUsageDynamic(record) {
638
+ try {
639
+ const { recordUsage } = await import('./minimax-usage-store.mjs');
640
+ recordUsage(record);
641
+ } catch { /* best-effort — usage recording must never break the API */ }
642
+ }
643
+
453
644
  // ─── Helpers ────────────────────────────────────────────────────────────────
454
645
 
455
646
  /** Mask the API key for safe logging / display. Shows last 4 chars. */