dsh-unified-router 0.6.2 → 0.6.4
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/lib/client.js +19 -1
- package/lib/web-host.js +3 -1
- package/lib/workbuddy.js +207 -11
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -662,7 +662,7 @@ function projectApiKeyRateLimit(value) {
|
|
|
662
662
|
|
|
663
663
|
const MODEL_PAGE_SIZE = 15
|
|
664
664
|
const PLAN_METHODS = new Set(['plan.snapshot', 'plan.setEnabled', 'plan.setKey', 'plan.setModelEnabled', 'plan.setModelParams', 'plan.resetModels', 'plan.consoleLogin'])
|
|
665
|
-
const WORKBUDDY_METHODS = new Set(['workbuddy.snapshot', 'workbuddy.login', 'workbuddy.logout', 'workbuddy.checkin', 'workbuddy.models', 'workbuddy.setModelEnabled', 'workbuddy.setModelParams', 'workbuddy.resetModels'])
|
|
665
|
+
const WORKBUDDY_METHODS = new Set(['workbuddy.snapshot', 'workbuddy.login', 'workbuddy.logout', 'workbuddy.checkin', 'workbuddy.setAccountEnabled', 'workbuddy.models', 'workbuddy.setModelEnabled', 'workbuddy.setModelParams', 'workbuddy.resetModels'])
|
|
666
666
|
// API Key 渠道(Agnes、云知声)共用的宿主方法;渠道 id 在 payload.provider 里带。
|
|
667
667
|
const APIKEY_METHODS = new Set(['apikey.snapshot', 'apikey.setKey', 'apikey.setEnabled', 'apikey.setModelEnabled', 'apikey.setModelParams', 'apikey.resetModels', 'apikey.setToolsEnabled', 'apikey.setConsoleSession', 'apikey.consoleLogin', 'apikey.clearConsoleSession'])
|
|
668
668
|
// 自管多账号渠道(AStudio / CodeArts)共用的宿主方法。
|
|
@@ -1110,6 +1110,8 @@ function workbuddyAccountFrom(value) {
|
|
|
1110
1110
|
const rawError = text(value.error, 40)
|
|
1111
1111
|
return {
|
|
1112
1112
|
id, name: text(value.name, 80) || id, uid: text(value.uid, 120),
|
|
1113
|
+
// 账号开关(缺省启用):与宿主投影、索引三处同一判据。
|
|
1114
|
+
enabled: value.enabled !== false,
|
|
1113
1115
|
signedIn: value.signedIn === true, plan: text(value.plan, 160),
|
|
1114
1116
|
signIn: text(value.signIn, 20) === 'renewed' ? 'renewed' : 'kept',
|
|
1115
1117
|
windows: Array.isArray(value.windows) ? value.windows.slice(0, 16).map(workbuddyWindowFrom).filter(Boolean) : [],
|
|
@@ -4796,8 +4798,12 @@ const workbuddy = {
|
|
|
4796
4798
|
const checkin = checkinEntry(state, workbuddy.id, account.id)
|
|
4797
4799
|
return {
|
|
4798
4800
|
id: account.id, name, updatedAt: account.updatedAt,
|
|
4801
|
+
// 账号开关:停用账号不参与选号与 429 轮换。
|
|
4802
|
+
enabled: account.enabled !== false,
|
|
4803
|
+
toggle: ['workbuddySetAccountEnabled', account.id, account.enabled === false],
|
|
4799
4804
|
tags: [
|
|
4800
4805
|
account.plan && [account.plan, 'info'],
|
|
4806
|
+
account.enabled === false && ['已停用'],
|
|
4801
4807
|
!account.signedIn && ['待登录', 'warn'],
|
|
4802
4808
|
(account.error || state.workbuddyStale) && [account.windows.length ? '上次额度' : '额度读取失败', 'warn'],
|
|
4803
4809
|
checkin?.tag,
|
|
@@ -4921,6 +4927,18 @@ function createWorkBuddyActions({ getState, isDisposed, hasSnapshots, request, p
|
|
|
4921
4927
|
if (typeof accountId !== 'string' || !accountId) return Promise.resolve(false)
|
|
4922
4928
|
return mutate('workbuddy.logout', { accountId }, { message: '已退出该 WorkBuddy 账号。' })
|
|
4923
4929
|
},
|
|
4930
|
+
/**
|
|
4931
|
+
* 账号开关:停用后该账号不参与选号与 429 轮换(凭据保留,重新启用即可用)。
|
|
4932
|
+
* WorkBuddy 每个账号的 DS-V4.1-Flash 额度用尽后上游回 429,此开关用于
|
|
4933
|
+
* 主动把耗尽的账号摘出轮换池。
|
|
4934
|
+
*/
|
|
4935
|
+
workbuddySetAccountEnabled(accountId, enabled) {
|
|
4936
|
+
if (typeof accountId !== 'string' || !accountId) return Promise.resolve(false)
|
|
4937
|
+
if (typeof enabled !== 'boolean') return Promise.resolve(false)
|
|
4938
|
+
return mutate('workbuddy.setAccountEnabled', { accountId, enabled }, {
|
|
4939
|
+
message: enabled ? '已启用该 WorkBuddy 账号。' : '已停用该 WorkBuddy 账号(不再参与选号)。',
|
|
4940
|
+
})
|
|
4941
|
+
},
|
|
4924
4942
|
workbuddyCheckin(accountId) {
|
|
4925
4943
|
if (typeof accountId !== 'string' || !accountId) return Promise.resolve(false)
|
|
4926
4944
|
return mutate('workbuddy.checkin', { accountId }, { refresh: false, progress: '正在签到领取当日积分…',
|
package/lib/web-host.js
CHANGED
|
@@ -28,7 +28,7 @@ const planWriteMethods = PLAN_METHODS
|
|
|
28
28
|
// 登录会写凭据、开关会改注册状态:这些写操作要求同源 + 主机授权。
|
|
29
29
|
// workbuddy.snapshot 虽名为读,但 token 临期续期会经 client.auth.set 落盘新凭据,
|
|
30
30
|
// 同样按写操作门禁(客户端补读是同源请求,不受影响)。
|
|
31
|
-
const workbuddyWriteMethods = new Set(['workbuddy.snapshot', 'workbuddy.login', 'workbuddy.logout', 'workbuddy.checkin', ...WORKBUDDY_MODEL_WRITES])
|
|
31
|
+
const workbuddyWriteMethods = new Set(['workbuddy.snapshot', 'workbuddy.login', 'workbuddy.logout', 'workbuddy.checkin', 'workbuddy.setAccountEnabled', ...WORKBUDDY_MODEL_WRITES])
|
|
32
32
|
// apikey.snapshot 不进门禁:它是纯读。五个写方法会改凭据 / 注册状态 / 模型偏好。
|
|
33
33
|
const apikeyWriteMethods = new Set(['apikey.setKey', 'apikey.setEnabled', 'apikey.setModelEnabled', 'apikey.setModelParams', 'apikey.resetModels', 'apikey.setToolsEnabled', 'apikey.setConsoleSession', 'apikey.consoleLogin', 'apikey.clearConsoleSession'])
|
|
34
34
|
const automationWriteMethods = new Set(['automation.setConfig', 'automation.run', 'automation.query'])
|
|
@@ -212,6 +212,8 @@ export function publicResult(method, value) {
|
|
|
212
212
|
if (!id) return null
|
|
213
213
|
return {
|
|
214
214
|
id, name: text(a.name, 80), uid: text(a.uid, 120),
|
|
215
|
+
// 账号开关(缺省启用):客户端据此渲染开关并显示「已停用」。
|
|
216
|
+
enabled: a.enabled !== false,
|
|
215
217
|
signedIn: a.signedIn === true, plan: text(a.plan, 160),
|
|
216
218
|
signIn: text(a.signIn, 20) === 'renewed' ? 'renewed' : 'kept',
|
|
217
219
|
windows: (Array.isArray(a.windows) ? a.windows : []).slice(0, 16).map(wbWindow).filter(Boolean),
|
package/lib/workbuddy.js
CHANGED
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
// SAFE_ERROR_CODES 里的固定码;窗口/模型/签到结果走白名单字段投影。
|
|
25
25
|
// 账号展示名(auth.accountId)与 uid 属账号标识,按契约出现在快照的 name/uid 字段。
|
|
26
26
|
import { randomBytes } from 'node:crypto'
|
|
27
|
+
import { readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
28
|
+
import { dirname, join } from 'node:path'
|
|
27
29
|
import { WorkBuddyAuthPlugin, _internal } from '../vendor/workbuddy/index.mjs'
|
|
28
30
|
import { createWorkBuddyModelSettings, WORKBUDDY_MODEL_WRITES } from './workbuddy-models.js'
|
|
29
31
|
import { readableModelName } from './model-catalog.js'
|
|
@@ -36,6 +38,7 @@ export const WORKBUDDY_METHODS = Object.freeze(new Set([
|
|
|
36
38
|
'workbuddy.login',
|
|
37
39
|
'workbuddy.logout',
|
|
38
40
|
'workbuddy.checkin',
|
|
41
|
+
'workbuddy.setAccountEnabled',
|
|
39
42
|
'workbuddy.models',
|
|
40
43
|
...WORKBUDDY_MODEL_WRITES,
|
|
41
44
|
]))
|
|
@@ -125,6 +128,9 @@ function sanitizeEntry(value) {
|
|
|
125
128
|
name: text(row.name, 80),
|
|
126
129
|
uid: text(row.uid, 120),
|
|
127
130
|
createdAt: finite(row.createdAt) ?? Date.now(),
|
|
131
|
+
// 账号开关:缺省视为启用(旧索引没有该字段)。只在显式关闭时落盘,
|
|
132
|
+
// 避免给既有条目凭空加上字段。
|
|
133
|
+
...(row.enabled === false ? { enabled: false } : {}),
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
136
|
|
|
@@ -232,6 +238,73 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
232
238
|
// 的默认实例(登录流程与未登录时的站点目录兜底用,不会触发续期回写)。
|
|
233
239
|
const pluginCache = new Map()
|
|
234
240
|
const modelSettings = createWorkBuddyModelSettings(rawConfig.modelSettingsFile, { defaultMaxOutput: rawConfig.defaultMaxOutput })
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 429 限流记账:账号 → 「被拒到什么时候」。
|
|
244
|
+
*
|
|
245
|
+
* ## 为什么需要它
|
|
246
|
+
*
|
|
247
|
+
* WorkBuddy 的免费模型(`deepseek-v4.1-flash`,中国版)有**独立于 credits** 的
|
|
248
|
+
* 每账号 24 小时 token 窗口(约 2e8)。撞墙时上游回 429 并在文案里给出重置时刻:
|
|
249
|
+
* `... your usage will reset at 2026-10-20 19:47:41 UTC+8, ...`。
|
|
250
|
+
*
|
|
251
|
+
* vendor 自带的 token 窗口计算**只读 Magpie 的 usage.jsonl**,DSH 的调用不写那里,
|
|
252
|
+
* 所以在纯 DSH 环境该窗口永远不会亮。这里退一步:只记**被拒信号本身**,用于在
|
|
253
|
+
* 额度面板如实显示「已满 · 重置于 X」——不需要 token 统计就能回答「还能不能用」。
|
|
254
|
+
*
|
|
255
|
+
* ⚠️ 记账必须发生在**换号之前**:换号成功后上层只看到 200,429 信号会丢失。
|
|
256
|
+
*/
|
|
257
|
+
// 路径未知时置空:记账退化为不持久化(快照里也就不显示该窗口),不影响推理。
|
|
258
|
+
const limitsFile = typeof rawConfig.limitsFile === 'string' && rawConfig.limitsFile
|
|
259
|
+
? rawConfig.limitsFile
|
|
260
|
+
: typeof rawConfig.modelSettingsFile === 'string' && rawConfig.modelSettingsFile
|
|
261
|
+
? join(dirname(rawConfig.modelSettingsFile), 'workbuddy-limits.json')
|
|
262
|
+
: ''
|
|
263
|
+
|
|
264
|
+
const readLimits = () => {
|
|
265
|
+
if (!limitsFile) return {}
|
|
266
|
+
try {
|
|
267
|
+
const parsed = JSON.parse(readFileSync(limitsFile, 'utf8'))
|
|
268
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}
|
|
269
|
+
} catch { return {} }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const writeLimits = next => {
|
|
273
|
+
if (!limitsFile) return
|
|
274
|
+
try {
|
|
275
|
+
const temporary = `${limitsFile}.tmp`
|
|
276
|
+
writeFileSync(temporary, JSON.stringify(next))
|
|
277
|
+
renameSync(temporary, limitsFile)
|
|
278
|
+
} catch { /* 记账失败不得影响推理 */ }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 从 429 文案里取重置时刻(毫秒)。取不到返回 null(不猜)。
|
|
283
|
+
* 上游按 UTC+8 报时且文案自带 `UTC+8`,因此固定按 +0800 解释。
|
|
284
|
+
*/
|
|
285
|
+
const parseResetAt = text => {
|
|
286
|
+
const match = /reset at\s+(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2})/i.exec(String(text ?? ''))
|
|
287
|
+
if (!match) return null
|
|
288
|
+
const at = Date.parse(`${match[1]}T${match[2]}+08:00`)
|
|
289
|
+
return Number.isFinite(at) ? at : null
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const noteRateLimited = (accountId, until) => {
|
|
293
|
+
if (!accountId || !Number.isFinite(until)) return
|
|
294
|
+
const limits = readLimits()
|
|
295
|
+
// 同一账号只保留更晚的重置时刻(并发请求可能先后撞墙)。
|
|
296
|
+
const previous = finite(limits[accountId])
|
|
297
|
+
if (previous !== undefined && previous >= until) return
|
|
298
|
+
writeLimits({ ...limits, [accountId]: until })
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const clearRateLimited = accountId => {
|
|
302
|
+
const limits = readLimits()
|
|
303
|
+
if (!(accountId in limits)) return
|
|
304
|
+
const next = { ...limits }
|
|
305
|
+
delete next[accountId]
|
|
306
|
+
writeLimits(next)
|
|
307
|
+
}
|
|
235
308
|
const site = _internal.SITES[WORKBUDDY_PROVIDER_ID]
|
|
236
309
|
let catalog = []
|
|
237
310
|
let catalogAccount = null
|
|
@@ -287,7 +360,11 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
287
360
|
}
|
|
288
361
|
|
|
289
362
|
const writeIndex = async entries => {
|
|
290
|
-
|
|
363
|
+
// ⚠️ 必须透传 `enabled`:早期这里按字段白名单重建条目,新增字段会被静默丢弃。
|
|
364
|
+
const rows = entries.slice(0, WORKBUDDY_MAX_ACCOUNTS).map(({ id, credentialRef, name, uid, createdAt, enabled }) => ({
|
|
365
|
+
id, credentialRef, name, uid, createdAt,
|
|
366
|
+
...(enabled === false ? { enabled: false } : {}),
|
|
367
|
+
}))
|
|
291
368
|
await writeCredential(WORKBUDDY_ACCOUNTS_REF, JSON.stringify(rows))
|
|
292
369
|
}
|
|
293
370
|
|
|
@@ -387,24 +464,85 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
387
464
|
// 续期结果经 client.auth.set 回写本账号的凭据引用。
|
|
388
465
|
const getAuthFor = entry => async () => (await readAccountAuth(entry)).auth
|
|
389
466
|
|
|
467
|
+
/** 参与选号/轮换的账号:`enabled !== false` 且凭据可用,保持索引顺序。 */
|
|
468
|
+
const activeAccounts = async () => (await accounts()).filter(entry => entry.enabled !== false)
|
|
469
|
+
|
|
390
470
|
const firstReadyAccount = async () => {
|
|
391
|
-
for (const entry of await
|
|
471
|
+
for (const entry of await activeAccounts()) {
|
|
392
472
|
const { auth } = await readAccountAuth(entry)
|
|
393
473
|
if (auth) return { entry, auth }
|
|
394
474
|
}
|
|
395
475
|
return null
|
|
396
476
|
}
|
|
397
477
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
const
|
|
402
|
-
const transport = await plugin.auth.loader(getAuthFor(account))
|
|
478
|
+
/** 取某账号的推理 transport(凭据过期由上游 hook 自行续期并回写本账号引用)。 */
|
|
479
|
+
const transportFor = async entry => {
|
|
480
|
+
const plugin = await pluginFor(entry.id)
|
|
481
|
+
const transport = await plugin.auth.loader(getAuthFor(entry))
|
|
403
482
|
if (typeof transport?.fetch !== 'function') throw new Error('请重新登录 WorkBuddy')
|
|
404
|
-
testDeps.noteQuotaAccount?.(WORKBUDDY_PROVIDER_ID, account.id)
|
|
405
483
|
return transport.fetch
|
|
406
484
|
}
|
|
407
485
|
|
|
486
|
+
/**
|
|
487
|
+
* 推理请求的 fetch:**按账号顺序尝试,429 时自动切到下一个账号**。
|
|
488
|
+
*
|
|
489
|
+
* ## 为什么需要它
|
|
490
|
+
*
|
|
491
|
+
* WorkBuddy 的 `DeepSeek-V4.1-Flash` 每账号有约 2 亿 token 的额度,**用尽后上游
|
|
492
|
+
* 直接回 429**,而早期实现把推理固定绑在「目录来源账号」上:该账号耗尽后,
|
|
493
|
+
* 整个 provider 一起 429,其它已登录账号完全用不上。
|
|
494
|
+
*
|
|
495
|
+
* ## 边界
|
|
496
|
+
*
|
|
497
|
+
* - 只对 429 换号;其它错误(401/5xx/网络)原样返回,交给上层重试语义。
|
|
498
|
+
* - 请求体必须是**可重放**的(JSON 字符串 / Uint8Array)。pi-ai 的
|
|
499
|
+
* openai-completions / anthropic-messages 都是这种形态;若是流式 body 就不重试。
|
|
500
|
+
* - 轮换只在**本次请求内**按顺序试一遍,不做跨请求的粘滞记账:下一次请求仍从
|
|
501
|
+
* 首选账号开始(首选账号恢复额度后自然继续用它)。
|
|
502
|
+
*/
|
|
503
|
+
const getInferenceFetch = async preferredAccountId => {
|
|
504
|
+
if (disposed) throw new Error('请刷新 WorkBuddy 目录或重新登录')
|
|
505
|
+
const entries = await activeAccounts()
|
|
506
|
+
if (entries.length === 0) throw new Error('没有启用的 WorkBuddy 账号,请在设置中启用账号')
|
|
507
|
+
// 首选账号置前,其余按索引顺序;同 id 只出现一次。
|
|
508
|
+
const ordered = []
|
|
509
|
+
for (const entry of entries) {
|
|
510
|
+
if (entry.id === preferredAccountId) ordered.unshift(entry)
|
|
511
|
+
else ordered.push(entry)
|
|
512
|
+
}
|
|
513
|
+
const replayable = body => body === undefined || body === null
|
|
514
|
+
|| typeof body === 'string' || body instanceof Uint8Array || body instanceof ArrayBuffer
|
|
515
|
+
|
|
516
|
+
return async (input, init) => {
|
|
517
|
+
let lastResponse
|
|
518
|
+
for (let index = 0; index < ordered.length; index += 1) {
|
|
519
|
+
const entry = ordered[index]
|
|
520
|
+
const fetch = await transportFor(entry)
|
|
521
|
+
// Request 需要克隆:一旦被消费就无法用于下一个账号。
|
|
522
|
+
const attemptInput = input instanceof Request ? input.clone() : input
|
|
523
|
+
const response = await fetch(attemptInput, init)
|
|
524
|
+
if (response.status !== 429) {
|
|
525
|
+
// 该账号又能用了:清掉上一次的限流记账,面板随之恢复。
|
|
526
|
+
clearRateLimited(entry.id)
|
|
527
|
+
testDeps.noteQuotaAccount?.(WORKBUDDY_PROVIDER_ID, entry.id)
|
|
528
|
+
return response
|
|
529
|
+
}
|
|
530
|
+
// ⚠️ 必须在换号之前记账:换到下个账号后上层只看到 200,429 会丢失。
|
|
531
|
+
// body 只能读一次,用 clone 读文案、原响应继续向下传。
|
|
532
|
+
try {
|
|
533
|
+
const said = await response.clone().text()
|
|
534
|
+
noteRateLimited(entry.id, parseResetAt(said))
|
|
535
|
+
} catch { /* 取不到文案就不记 */ }
|
|
536
|
+
lastResponse = response
|
|
537
|
+
// 已耗尽:换下一个账号。body 不可重放时不再尝试(避免发出空请求)。
|
|
538
|
+
if (index < ordered.length - 1 && !replayable(init?.body)) break
|
|
539
|
+
}
|
|
540
|
+
// 全部 429:如实返回最后一次响应,由上层按限流处理。
|
|
541
|
+
if (lastResponse) return lastResponse
|
|
542
|
+
throw new Error('WorkBuddy 请求失败')
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
408
546
|
function refreshRouting() {
|
|
409
547
|
const run = routingJob.then(async () => {
|
|
410
548
|
const llm = serviceOf(ctx, 'llm')
|
|
@@ -466,7 +604,12 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
466
604
|
|
|
467
605
|
// ---- 快照 ---------------------------------------------------------------
|
|
468
606
|
const readAccount = async (entry, signal) => {
|
|
469
|
-
const base = {
|
|
607
|
+
const base = {
|
|
608
|
+
id: entry.id, name: entry.name, uid: entry.uid,
|
|
609
|
+
// 账号开关(缺省启用):客户端据此渲染开关,选号/轮换按同一判据过滤。
|
|
610
|
+
enabled: entry.enabled !== false,
|
|
611
|
+
signedIn: false, plan: '', windows: [], signIn: 'kept', updatedAt: Date.now(), error: '',
|
|
612
|
+
}
|
|
470
613
|
const { auth, corrupt } = await readAccountAuth(entry)
|
|
471
614
|
if (!auth) return { ...base, error: corrupt ? 'credential_error' : 'not_configured' }
|
|
472
615
|
// 索引里的展示名可能为空(老索引/手改过):回退到凭据里的 accountId。
|
|
@@ -500,15 +643,44 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
500
643
|
|
|
501
644
|
// 快照逐账号读额度(Promise.all):单个账号失败只污染自己那一行,
|
|
502
645
|
// 不影响其他账号的读数。
|
|
646
|
+
/**
|
|
647
|
+
* 给撞过 429 的账号追加一条「已满」窗口。
|
|
648
|
+
*
|
|
649
|
+
* 只表达**被拒信号**,不估算已用 token:vendor 的精确窗口依赖 Magpie 的
|
|
650
|
+
* usage.jsonl(DSH 不写那里),故这里用 `estimate: true` 明确标记为估算,
|
|
651
|
+
* 并在重置时刻过去后自动失效。
|
|
652
|
+
*/
|
|
653
|
+
const withRateLimitWindow = row => {
|
|
654
|
+
const until = finite(readLimits()[row.id])
|
|
655
|
+
if (until === undefined || until <= Date.now()) return row
|
|
656
|
+
return {
|
|
657
|
+
...row,
|
|
658
|
+
windows: [...(Array.isArray(row.windows) ? row.windows : []), {
|
|
659
|
+
name: '免费额度(token 窗口)',
|
|
660
|
+
used: 100,
|
|
661
|
+
limit: 100,
|
|
662
|
+
balance: 0,
|
|
663
|
+
unit: '',
|
|
664
|
+
display: '已满 · 待重置',
|
|
665
|
+
resetsAt: new Date(until).toISOString(),
|
|
666
|
+
estimate: true,
|
|
667
|
+
aside: false,
|
|
668
|
+
}],
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
503
672
|
const snapshot = async signal => {
|
|
504
673
|
signal?.throwIfAborted()
|
|
505
674
|
if (disposed) throw new Error('请求已结束')
|
|
506
675
|
const entries = await accounts()
|
|
507
676
|
signal?.throwIfAborted()
|
|
508
|
-
const rows = await Promise.all(entries.map(entry => readAccount(entry, signal).catch(error => {
|
|
677
|
+
const rows = (await Promise.all(entries.map(entry => readAccount(entry, signal).catch(error => {
|
|
509
678
|
if (signal?.aborted) throw error
|
|
510
679
|
return { id: entry.id, name: entry.name, uid: entry.uid, signedIn: false, plan: '', windows: [], signIn: 'kept', updatedAt: Date.now(), error: 'upstream_error' }
|
|
511
|
-
})))
|
|
680
|
+
}))))
|
|
681
|
+
// 撞过 429 的账号追加一条「已满」窗口:上游的免费模型 token 窗口不看 credits,
|
|
682
|
+
// 面板上那三个 credits 窗口无法回答「这个号还能不能继续用」。
|
|
683
|
+
.map(row => withRateLimitWindow(row))
|
|
512
684
|
signal?.throwIfAborted()
|
|
513
685
|
return { accounts: rows, loginUrl: pendingLoginUrl }
|
|
514
686
|
}
|
|
@@ -582,6 +754,29 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
582
754
|
return { signedIn: false }
|
|
583
755
|
}
|
|
584
756
|
|
|
757
|
+
/**
|
|
758
|
+
* 账号开关:启用 / 停用某个 WorkBuddy 账号。
|
|
759
|
+
*
|
|
760
|
+
* 停用只改索引条目的 `enabled`,**不动凭据**——重新启用后无需再次登录。
|
|
761
|
+
* 选号与 429 轮换都按同一判据过滤(`enabled !== false`),所以停用账号
|
|
762
|
+
* 既不会被选中,也不会成为轮换目标。
|
|
763
|
+
*/
|
|
764
|
+
const setAccountEnabled = async (payload, signal) => {
|
|
765
|
+
assertProvider(payload)
|
|
766
|
+
if (typeof payload.enabled !== 'boolean') throw new Error('账号开关无效')
|
|
767
|
+
signal?.throwIfAborted()
|
|
768
|
+
if (disposed) throw new Error('请求已结束')
|
|
769
|
+
const entries = await accounts()
|
|
770
|
+
const entry = resolveAccount(payload, entries)
|
|
771
|
+
await writeIndex(entries.map(row => row.id === entry.id
|
|
772
|
+
? { ...row, enabled: payload.enabled }
|
|
773
|
+
: row))
|
|
774
|
+
signal?.throwIfAborted()
|
|
775
|
+
// 目录账号可能被停用:重算一次路由,避免继续把请求派给已停用账号。
|
|
776
|
+
await refreshRouting()
|
|
777
|
+
return { id: entry.id, enabled: payload.enabled }
|
|
778
|
+
}
|
|
779
|
+
|
|
585
780
|
const checkin = async (payload, signal, statusOnly = false) => {
|
|
586
781
|
assertProvider(payload)
|
|
587
782
|
signal?.throwIfAborted()
|
|
@@ -678,6 +873,7 @@ export function createWorkBuddyManager(ctx, rawConfig = {}, testDeps = {}) {
|
|
|
678
873
|
if (method === 'workbuddy.login') return login(payload, signal)
|
|
679
874
|
if (method === 'workbuddy.logout') return logout(payload, signal)
|
|
680
875
|
if (method === 'workbuddy.checkin') return checkin(payload, signal)
|
|
876
|
+
if (method === 'workbuddy.setAccountEnabled') return setAccountEnabled(payload, signal)
|
|
681
877
|
if (method === 'workbuddy.models') return models(payload, signal)
|
|
682
878
|
if (WORKBUDDY_MODEL_WRITES.includes(method)) {
|
|
683
879
|
if (!catalog.length) await models(payload, signal)
|