dsh-model-allowance 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 +147 -2
- package/README.zh.md +128 -0
- package/client.js +1897 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +7 -0
- package/index.js +457 -0
- package/lib/balance.js +413 -0
- package/lib/credentials.js +174 -0
- package/lib/debug.js +70 -0
- package/lib/providers/deepseek.js +408 -0
- package/lib/providers/kimi.js +474 -0
- package/lib/providers/minimax.js +383 -0
- package/lib/providers/opencode.js +209 -0
- package/lib/providers/schedule.js +153 -0
- package/lib/providers/shared.js +324 -0
- package/lib/providers/status.js +133 -0
- package/lib/providers/zhipu.js +185 -0
- package/lib/providers.js +112 -0
- package/lib/remote.js +214 -0
- package/lib/status.js +196 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +82 -4
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepSeek adapters.
|
|
3
|
+
*
|
|
4
|
+
* Two routes exist because two authorizations exist. The signed-in account
|
|
5
|
+
* route forwards to the Host's own `deepseekAccount` service, which already
|
|
6
|
+
* owns that query — re-implementing the Platform call here would be a second
|
|
7
|
+
* source of truth. The API-key route asks the same public endpoint the key
|
|
8
|
+
* itself can read.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { defineSchedule } from './schedule.js'
|
|
12
|
+
import { asObject, defineAdapter, KIND_AMOUNT, parseAmount } from './shared.js'
|
|
13
|
+
import {
|
|
14
|
+
normalizeState,
|
|
15
|
+
STATUS_DEGRADED,
|
|
16
|
+
STATUS_MAINTENANCE,
|
|
17
|
+
STATUS_OPERATIONAL,
|
|
18
|
+
worstState,
|
|
19
|
+
} from './status.js'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Where DeepSeek's own console shows the same wallet this plugin reads.
|
|
23
|
+
* Verified 2026-11-06.
|
|
24
|
+
*/
|
|
25
|
+
const CONSOLE = 'https://platform.deepseek.com/top_up'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* DeepSeek publishes its live platform status on a FlashCat status page.
|
|
29
|
+
*
|
|
30
|
+
* The page is a Next.js server-rendered document, so the machine-readable
|
|
31
|
+
* answer is the flight payload embedded in the HTML rather than a JSON
|
|
32
|
+
* endpoint: `initialData.active_changes` lists every change in progress, and an
|
|
33
|
+
* empty list is what the page renders as "All Systems Operational". The parser
|
|
34
|
+
* below decodes exactly those two facts and nothing else; anything it cannot
|
|
35
|
+
* decode reads as unknown rather than as a guess.
|
|
36
|
+
*
|
|
37
|
+
* @type {{endpoint: string, url: string, parse: (env: {text?: string}) => {state: string, summary?: string}}}
|
|
38
|
+
*/
|
|
39
|
+
const STATUS = Object.freeze({
|
|
40
|
+
endpoint: 'https://status.deepseek.com/',
|
|
41
|
+
url: 'https://status.deepseek.com',
|
|
42
|
+
parse: parseStatusPage,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* DeepSeek prices by wall clock: Monday to Friday, 09:00–12:00 and
|
|
47
|
+
* 14:00–18:00 Beijing time are peak, and off-peak prices are half. Chinese
|
|
48
|
+
* statutory holidays are off-peak all day; a working weekend stays a weekend
|
|
49
|
+
* under the rule, which names Monday to Friday.
|
|
50
|
+
*
|
|
51
|
+
* The holiday list is the State Council's 2026 arrangement (国办发明电
|
|
52
|
+
* 〔2025〕7号). Extend it when the next year's notice is published.
|
|
53
|
+
*/
|
|
54
|
+
const SCHEDULE = defineSchedule({
|
|
55
|
+
timezone: 'Asia/Shanghai',
|
|
56
|
+
timezoneLabel: { zh: '北京时间', en: 'Beijing time' },
|
|
57
|
+
days: [1, 2, 3, 4, 5],
|
|
58
|
+
windows: [
|
|
59
|
+
{ from: '09:00', to: '12:00' },
|
|
60
|
+
{ from: '14:00', to: '18:00' },
|
|
61
|
+
],
|
|
62
|
+
holidays: [
|
|
63
|
+
// 元旦
|
|
64
|
+
'2026-01-01',
|
|
65
|
+
'2026-01-02',
|
|
66
|
+
'2026-01-03',
|
|
67
|
+
// 春节
|
|
68
|
+
'2026-02-15',
|
|
69
|
+
'2026-02-16',
|
|
70
|
+
'2026-02-17',
|
|
71
|
+
'2026-02-18',
|
|
72
|
+
'2026-02-19',
|
|
73
|
+
'2026-02-20',
|
|
74
|
+
'2026-02-21',
|
|
75
|
+
'2026-02-22',
|
|
76
|
+
'2026-02-23',
|
|
77
|
+
// 清明节
|
|
78
|
+
'2026-04-04',
|
|
79
|
+
'2026-04-05',
|
|
80
|
+
'2026-04-06',
|
|
81
|
+
// 劳动节
|
|
82
|
+
'2026-05-01',
|
|
83
|
+
'2026-05-02',
|
|
84
|
+
'2026-05-03',
|
|
85
|
+
'2026-05-04',
|
|
86
|
+
'2026-05-05',
|
|
87
|
+
// 端午节
|
|
88
|
+
'2026-06-19',
|
|
89
|
+
'2026-06-20',
|
|
90
|
+
'2026-06-21',
|
|
91
|
+
// 中秋节
|
|
92
|
+
'2026-09-25',
|
|
93
|
+
'2026-09-26',
|
|
94
|
+
'2026-09-27',
|
|
95
|
+
// 国庆节
|
|
96
|
+
'2026-10-01',
|
|
97
|
+
'2026-10-02',
|
|
98
|
+
'2026-10-03',
|
|
99
|
+
'2026-10-04',
|
|
100
|
+
'2026-10-05',
|
|
101
|
+
'2026-10-06',
|
|
102
|
+
'2026-10-07',
|
|
103
|
+
],
|
|
104
|
+
note: { zh: '低谷时段 API 价格减半', en: 'API prices are halved off-peak' },
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Decode the flight payload a Next.js server-rendered page embeds.
|
|
109
|
+
*
|
|
110
|
+
* Each `self.__next_f.push([1, "..."])` chunk carries one JSON string; the
|
|
111
|
+
* concatenation of those strings is the payload the page rendered from. A
|
|
112
|
+
* chunk that is not such a pair (the bootstrap markers) carries no page data.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} html Page source.
|
|
115
|
+
* @returns {string} The decoded payload text.
|
|
116
|
+
*/
|
|
117
|
+
function flightText(html) {
|
|
118
|
+
let text = ''
|
|
119
|
+
for (const match of html.matchAll(/self\.__next_f\.push\((\[[\s\S]*?\])\)/g)) {
|
|
120
|
+
try {
|
|
121
|
+
const chunk = JSON.parse(match[1])
|
|
122
|
+
if (Array.isArray(chunk) && chunk[0] === 1 && typeof chunk[1] === 'string') {
|
|
123
|
+
text += chunk[1]
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
// Not a [1, text] pair; nothing to add.
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return text
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Find the page-level `initialData` object inside a flight payload.
|
|
134
|
+
* @param {unknown} node Parsed payload node.
|
|
135
|
+
* @param {number} depth Recursion depth.
|
|
136
|
+
* @returns {Record<string, unknown> | undefined} The page data, when found.
|
|
137
|
+
*/
|
|
138
|
+
function findPageData(node, depth) {
|
|
139
|
+
if (depth > 10 || node === null || typeof node !== 'object') return undefined
|
|
140
|
+
const record = /** @type {Record<string, unknown>} */ (node)
|
|
141
|
+
const initial = /** @type {Record<string, unknown> | undefined} */ (record.initialData)
|
|
142
|
+
if (!Array.isArray(node) && initial && Array.isArray(initial.active_changes)) {
|
|
143
|
+
return initial
|
|
144
|
+
}
|
|
145
|
+
for (const value of Array.isArray(node) ? node : Object.values(record)) {
|
|
146
|
+
const found = findPageData(value, depth + 1)
|
|
147
|
+
if (found !== undefined) return found
|
|
148
|
+
}
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Read the page-level data row out of one decoded flight payload.
|
|
154
|
+
* @param {string} payload Decoded payload text.
|
|
155
|
+
* @returns {Record<string, unknown> | undefined} The page data, when present.
|
|
156
|
+
*/
|
|
157
|
+
function pageData(payload) {
|
|
158
|
+
for (const line of payload.split('\n')) {
|
|
159
|
+
const separator = line.indexOf(':')
|
|
160
|
+
if (separator <= 0) continue
|
|
161
|
+
const json = line.slice(separator + 1)
|
|
162
|
+
if (!json.startsWith('[')) continue
|
|
163
|
+
let node
|
|
164
|
+
try {
|
|
165
|
+
node = JSON.parse(json)
|
|
166
|
+
} catch {
|
|
167
|
+
// A payload row that is not JSON is not a page row.
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
const found = findPageData(node, 0)
|
|
171
|
+
if (found !== undefined) return found
|
|
172
|
+
}
|
|
173
|
+
return undefined
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The state one active change implies.
|
|
178
|
+
*
|
|
179
|
+
* A change carries its impact either directly (`status`/`impact`) or on the
|
|
180
|
+
* entries beside it (`impacts[]`, `affected_components[]`); the response phase
|
|
181
|
+
* (`investigating`, `monitoring`) describes the handling, not the service, so
|
|
182
|
+
* it is never read as a state. A change this function cannot decode still
|
|
183
|
+
* means something is in progress, and a maintenance window is the mildest
|
|
184
|
+
* thing that could be.
|
|
185
|
+
*
|
|
186
|
+
* @param {unknown} change One entry of `active_changes`.
|
|
187
|
+
* @returns {string} The implied state.
|
|
188
|
+
*/
|
|
189
|
+
function changeState(change) {
|
|
190
|
+
const record = asObject(change)
|
|
191
|
+
const direct = normalizeState(record.status) ?? normalizeState(record.impact)
|
|
192
|
+
if (direct !== undefined) return direct
|
|
193
|
+
const impacts = []
|
|
194
|
+
for (const key of ['impacts', 'affected_components', 'sections']) {
|
|
195
|
+
const list = Array.isArray(record[key]) ? record[key] : []
|
|
196
|
+
for (const entry of list) impacts.push(asObject(entry).status)
|
|
197
|
+
}
|
|
198
|
+
const worst = worstState(impacts)
|
|
199
|
+
if (worst !== undefined) return worst
|
|
200
|
+
if (record.type === 'maintenance') return STATUS_MAINTENANCE
|
|
201
|
+
return STATUS_DEGRADED
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Project one DeepSeek status page.
|
|
206
|
+
* @param {{text?: string}} env Source payload.
|
|
207
|
+
* @returns {{state: string, summary?: string}} The projected reading.
|
|
208
|
+
*/
|
|
209
|
+
function parseStatusPage({ text }) {
|
|
210
|
+
const data = pageData(flightText(typeof text === 'string' ? text : ''))
|
|
211
|
+
if (data === undefined) throw new Error('no status payload in page')
|
|
212
|
+
const changes = Array.isArray(data.active_changes) ? data.active_changes : []
|
|
213
|
+
if (changes.length === 0) return { state: STATUS_OPERATIONAL }
|
|
214
|
+
/** @type {string[]} */
|
|
215
|
+
const titles = []
|
|
216
|
+
/** @type {string[]} */
|
|
217
|
+
const states = []
|
|
218
|
+
for (const change of changes) {
|
|
219
|
+
const record = asObject(change)
|
|
220
|
+
const title = typeof record.title === 'string' ? record.title.trim() : ''
|
|
221
|
+
if (title !== '') titles.push(title)
|
|
222
|
+
states.push(changeState(record))
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
state: worstState(states) ?? STATUS_DEGRADED,
|
|
226
|
+
...(titles.length === 0 ? {} : { summary: titles.join('; ') }),
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Report the same wallet balances the Account settings page shows, through
|
|
232
|
+
* the Host service that already owns that query.
|
|
233
|
+
*
|
|
234
|
+
* `deepseekAccount` is an optional service: a composition without it simply
|
|
235
|
+
* has no account route.
|
|
236
|
+
*
|
|
237
|
+
* @param {import('../providers.js').AdapterEnv} env Adapter environment.
|
|
238
|
+
* @returns {Promise<import('../providers.js').BalanceView>} The reading.
|
|
239
|
+
*/
|
|
240
|
+
async function readAccountBalance(env) {
|
|
241
|
+
const { route, ctx } = env
|
|
242
|
+
const account = typeof ctx.get === 'function' ? ctx.get('deepseekAccount') : undefined
|
|
243
|
+
if (!account || typeof account.getBalance !== 'function') {
|
|
244
|
+
return { provider: route, status: 'unsupported', kind: KIND_AMOUNT }
|
|
245
|
+
}
|
|
246
|
+
const details = await account.getBalance({
|
|
247
|
+
version: '1.0.0',
|
|
248
|
+
locale: 'en',
|
|
249
|
+
timezoneOffsetSeconds: -new Date().getTimezoneOffset() * 60,
|
|
250
|
+
})
|
|
251
|
+
if (!details) {
|
|
252
|
+
return { provider: route, status: 'unconfigured', kind: KIND_AMOUNT }
|
|
253
|
+
}
|
|
254
|
+
if (details.status !== 'ready') {
|
|
255
|
+
return { provider: route, status: 'failed', kind: KIND_AMOUNT, error: 'account balance failed' }
|
|
256
|
+
}
|
|
257
|
+
const wallets = [...(details.value ?? []), ...(details.bonusWallets ?? [])]
|
|
258
|
+
if (wallets.length === 0) {
|
|
259
|
+
return { provider: route, status: 'failed', kind: KIND_AMOUNT, error: 'no wallet in response' }
|
|
260
|
+
}
|
|
261
|
+
// The reading is one figure, so sum wallets that share the primary currency.
|
|
262
|
+
const primary = asObject(wallets[0])
|
|
263
|
+
const currency = typeof primary.currency === 'string' ? primary.currency : 'CNY'
|
|
264
|
+
let amount = 0
|
|
265
|
+
for (const wallet of wallets) {
|
|
266
|
+
const record = asObject(wallet)
|
|
267
|
+
if (record.currency !== currency) continue
|
|
268
|
+
amount += parseAmount(record.balance) ?? 0
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
provider: route,
|
|
272
|
+
status: 'ready',
|
|
273
|
+
kind: KIND_AMOUNT,
|
|
274
|
+
amount,
|
|
275
|
+
currency,
|
|
276
|
+
consoleUrl: CONSOLE,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Project the API-key route's `/user/balance` body.
|
|
282
|
+
*
|
|
283
|
+
* The endpoint reports one entry per currency. A reading is one figure, so it
|
|
284
|
+
* takes the first reported currency as the account's primary wallet and sums
|
|
285
|
+
* only entries in that currency. Summing across currencies would add CNY to
|
|
286
|
+
* USD and report a number that means nothing.
|
|
287
|
+
*
|
|
288
|
+
* @param {{route: string, body: Record<string, unknown>}} env Projection context.
|
|
289
|
+
* @returns {import('../providers.js').BalanceView} The reading.
|
|
290
|
+
*/
|
|
291
|
+
function readApiKeyBalance({ route, body }) {
|
|
292
|
+
const infos = Array.isArray(body.balance_infos) ? body.balance_infos : []
|
|
293
|
+
if (infos.length === 0) {
|
|
294
|
+
return { provider: route, status: 'failed', kind: KIND_AMOUNT, error: 'no balance in response' }
|
|
295
|
+
}
|
|
296
|
+
const primary = asObject(infos[0])
|
|
297
|
+
const currency = typeof primary.currency === 'string' ? primary.currency : 'CNY'
|
|
298
|
+
let amount = 0
|
|
299
|
+
for (const info of infos) {
|
|
300
|
+
const record = asObject(info)
|
|
301
|
+
if (record.currency !== currency) continue
|
|
302
|
+
amount += parseAmount(record.total_balance) ?? 0
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
provider: route,
|
|
306
|
+
status: 'ready',
|
|
307
|
+
kind: KIND_AMOUNT,
|
|
308
|
+
amount,
|
|
309
|
+
currency,
|
|
310
|
+
consoleUrl: CONSOLE,
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** DeepSeek's two routes. */
|
|
315
|
+
export const deepseekAdapters = [
|
|
316
|
+
defineAdapter({
|
|
317
|
+
id: 'deepseek-account',
|
|
318
|
+
routes: ['deepseek-account'],
|
|
319
|
+
kind: KIND_AMOUNT,
|
|
320
|
+
service: 'deepseekAccount',
|
|
321
|
+
readWithEnv: readAccountBalance,
|
|
322
|
+
consoleUrl: CONSOLE,
|
|
323
|
+
status: STATUS,
|
|
324
|
+
schedule: SCHEDULE,
|
|
325
|
+
display: {
|
|
326
|
+
name: { zh: 'DeepSeek 账号', en: 'DeepSeek account' },
|
|
327
|
+
mark: { icon: 'deepseek', color: '#4d6bfe' },
|
|
328
|
+
},
|
|
329
|
+
fixtures: [
|
|
330
|
+
{
|
|
331
|
+
name: 'wallet plus bonus wallet',
|
|
332
|
+
services: {
|
|
333
|
+
deepseekAccount: {
|
|
334
|
+
getBalance: async () => ({
|
|
335
|
+
status: 'ready',
|
|
336
|
+
value: [{ currency: 'CNY', balance: '40.00' }],
|
|
337
|
+
bonusWallets: [{ currency: 'CNY', balance: '6.71' }],
|
|
338
|
+
}),
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
expect: { status: 'ready', kind: KIND_AMOUNT, amount: 46.71, currency: 'CNY' },
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
name: 'service absent',
|
|
345
|
+
services: {},
|
|
346
|
+
expect: { status: 'unsupported', kind: KIND_AMOUNT },
|
|
347
|
+
},
|
|
348
|
+
],
|
|
349
|
+
}),
|
|
350
|
+
defineAdapter({
|
|
351
|
+
id: 'deepseek-api-key',
|
|
352
|
+
routes: ['deepseek'],
|
|
353
|
+
kind: KIND_AMOUNT,
|
|
354
|
+
endpoint: 'https://api.deepseek.com/user/balance',
|
|
355
|
+
auth: 'bearer',
|
|
356
|
+
credentialRefs: ['DEEPSEEK_API_KEY'],
|
|
357
|
+
read: readApiKeyBalance,
|
|
358
|
+
consoleUrl: CONSOLE,
|
|
359
|
+
status: STATUS,
|
|
360
|
+
schedule: SCHEDULE,
|
|
361
|
+
display: {
|
|
362
|
+
name: { zh: 'DeepSeek API', en: 'DeepSeek API' },
|
|
363
|
+
mark: { icon: 'deepseek', color: '#4d6bfe' },
|
|
364
|
+
},
|
|
365
|
+
fixtures: [
|
|
366
|
+
{
|
|
367
|
+
name: 'recorded balance',
|
|
368
|
+
response: {
|
|
369
|
+
is_available: true,
|
|
370
|
+
balance_infos: [
|
|
371
|
+
{
|
|
372
|
+
currency: 'CNY',
|
|
373
|
+
total_balance: '46.71',
|
|
374
|
+
granted_balance: '0.00',
|
|
375
|
+
topped_up_balance: '46.71',
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
},
|
|
379
|
+
apiKey: 'sk-test',
|
|
380
|
+
expect: { status: 'ready', kind: KIND_AMOUNT, amount: 46.71, currency: 'CNY' },
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
name: 'multiple currencies keep only the primary wallet',
|
|
384
|
+
response: {
|
|
385
|
+
is_available: true,
|
|
386
|
+
balance_infos: [
|
|
387
|
+
{ currency: 'CNY', total_balance: '40.00' },
|
|
388
|
+
{ currency: 'USD', total_balance: '10.00' },
|
|
389
|
+
{ currency: 'CNY', total_balance: '6.71' },
|
|
390
|
+
],
|
|
391
|
+
},
|
|
392
|
+
apiKey: 'sk-test',
|
|
393
|
+
// 40.00 + 6.71 in the primary currency; the USD wallet is not added.
|
|
394
|
+
expect: { status: 'ready', kind: KIND_AMOUNT, amount: 46.71, currency: 'CNY' },
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
name: 'empty balance list',
|
|
398
|
+
response: { is_available: false, balance_infos: [] },
|
|
399
|
+
expect: { status: 'failed', error: 'no balance in response' },
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
name: 'key rejected',
|
|
403
|
+
httpStatus: 401,
|
|
404
|
+
expect: { status: 'failed', error: 'HTTP 401' },
|
|
405
|
+
},
|
|
406
|
+
],
|
|
407
|
+
}),
|
|
408
|
+
]
|