dsh-unified-router 0.8.13 → 0.8.14
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/backend.js
CHANGED
|
@@ -10990,6 +10990,42 @@ function registerChannelPackEndpoints(ctx, pool, qoder, qoderCn, trae, raccoon,
|
|
|
10990
10990
|
return member;
|
|
10991
10991
|
};
|
|
10992
10992
|
const pendingLoginFailures = /* @__PURE__ */ new Map();
|
|
10993
|
+
const raccoonPlanCheckedAt = /* @__PURE__ */ new Map();
|
|
10994
|
+
let raccoonPlanJob;
|
|
10995
|
+
function refreshRaccoonPlans(accounts, force, signal) {
|
|
10996
|
+
return raccoonPlanJob ??= collectRaccoonPlans(accounts, force, signal).finally(() => {
|
|
10997
|
+
raccoonPlanJob = void 0;
|
|
10998
|
+
});
|
|
10999
|
+
}
|
|
11000
|
+
async function collectRaccoonPlans(accounts, force, signal) {
|
|
11001
|
+
const present = new Set(accounts.map((account) => account.id));
|
|
11002
|
+
for (const id of raccoonPlanCheckedAt.keys()) if (!present.has(id)) raccoonPlanCheckedAt.delete(id);
|
|
11003
|
+
const changes = await Promise.all(accounts.map(async (account) => {
|
|
11004
|
+
if (account.enabled === false || account.expiresAt != null && account.expiresAt <= Date.now()) return false;
|
|
11005
|
+
if (!force && Date.now() - (raccoonPlanCheckedAt.get(account.id) ?? 0) < 45e3) return false;
|
|
11006
|
+
try {
|
|
11007
|
+
const credential = await pool.resolveCredentialForAccount(account.id);
|
|
11008
|
+
if (!credential || signal?.aborted) return false;
|
|
11009
|
+
const plan = await requireRaccoonFamily(RACCOON.id).auth.fetchPlan(credential, signal);
|
|
11010
|
+
if (signal?.aborted) return false;
|
|
11011
|
+
raccoonPlanCheckedAt.set(account.id, Date.now());
|
|
11012
|
+
const current = pool.findAccount(account.id);
|
|
11013
|
+
if (plan === "unknown" || current?.credentialRef !== account.credentialRef || current.plan === plan) return false;
|
|
11014
|
+
await pool.updateAccount(account.id, { plan });
|
|
11015
|
+
return true;
|
|
11016
|
+
} catch {
|
|
11017
|
+
return false;
|
|
11018
|
+
}
|
|
11019
|
+
}));
|
|
11020
|
+
if (changes.some(Boolean)) {
|
|
11021
|
+
broadcastCatalogChanged(ctx);
|
|
11022
|
+
await Promise.allSettled(raccoonFamily.map(async ({ product }) => {
|
|
11023
|
+
await modelAdapters?.[product.id]?.refreshRemoteModels?.();
|
|
11024
|
+
}));
|
|
11025
|
+
broadcastCatalogChanged(ctx);
|
|
11026
|
+
}
|
|
11027
|
+
return changes.some(Boolean);
|
|
11028
|
+
}
|
|
10993
11029
|
const badgePreferences = createBadgePreferenceStore(ctx);
|
|
10994
11030
|
const autoCheckin = createAutoCheckin({
|
|
10995
11031
|
store: createAutoCheckinStore(ctx),
|
|
@@ -11062,7 +11098,10 @@ function registerChannelPackEndpoints(ctx, pool, qoder, qoderCn, trae, raccoon,
|
|
|
11062
11098
|
switch (method) {
|
|
11063
11099
|
case "account.list": {
|
|
11064
11100
|
const req = payload;
|
|
11065
|
-
|
|
11101
|
+
let accounts = await pool.listAccounts(req.provider);
|
|
11102
|
+
if (req.provider === RACCOON.id && req.refreshPlan === true && await refreshRaccoonPlans(accounts, req.forcePlanRefresh === true, signal)) {
|
|
11103
|
+
accounts = await pool.listAccounts(req.provider);
|
|
11104
|
+
}
|
|
11066
11105
|
if (req.provider === TRAE.id) {
|
|
11067
11106
|
for (const account2 of accounts) {
|
|
11068
11107
|
if (account2.enabled === false) continue;
|
|
@@ -12349,12 +12388,16 @@ async function requestJson(url, credential, product, init, fetcher, signal) {
|
|
|
12349
12388
|
const timeout = AbortSignal.timeout(RACCOON_REQUEST_TIMEOUT_MS);
|
|
12350
12389
|
let receivedResponse = false;
|
|
12351
12390
|
try {
|
|
12391
|
+
const headers = raccoonHeaders(credential, {
|
|
12392
|
+
platform: product.clientPlatform,
|
|
12393
|
+
version: product.clientVersion
|
|
12394
|
+
});
|
|
12395
|
+
if (url.startsWith(product.apiBase + RACCOON_POINTS_PREFIX + "/") && (!credential.office_identity || credential.office_identity === "personal")) {
|
|
12396
|
+
delete headers["X-Org-Code"];
|
|
12397
|
+
}
|
|
12352
12398
|
const response = await fetcher(url, {
|
|
12353
12399
|
method: init.method,
|
|
12354
|
-
headers
|
|
12355
|
-
platform: product.clientPlatform,
|
|
12356
|
-
version: product.clientVersion
|
|
12357
|
-
}),
|
|
12400
|
+
headers,
|
|
12358
12401
|
...init.body === void 0 ? {} : { body: init.body },
|
|
12359
12402
|
signal: signal ? AbortSignal.any([signal, timeout]) : timeout
|
|
12360
12403
|
});
|
|
@@ -12453,7 +12496,9 @@ function isDailyGrantBill(item, today) {
|
|
|
12453
12496
|
if (points === void 0 || points <= 0) return false;
|
|
12454
12497
|
if (String(item.created_at ?? "").slice(0, 10) !== today) return false;
|
|
12455
12498
|
const biz = String(item.biz_type ?? "");
|
|
12456
|
-
|
|
12499
|
+
const name = typeof item.event_name === "string" ? item.event_name.trim() : "";
|
|
12500
|
+
if (name === RACCOON_LOGIN_REWARD_EVENT_NAME) return false;
|
|
12501
|
+
return biz === "daily_grant" || (biz === "" || biz === "reward_grant") && ["\u6BCF\u65E5\u79EF\u5206\u53D1\u653E", "\u6BCF\u65E5\u7B7E\u5230\u5956\u52B1", "\u8FDE\u7EED\u7B7E\u5230\u5956\u52B1"].includes(name);
|
|
12457
12502
|
}
|
|
12458
12503
|
async function readBills(product, credential, fetcher) {
|
|
12459
12504
|
const envelope = await requestJson(
|
|
@@ -12464,7 +12509,8 @@ async function readBills(product, credential, fetcher) {
|
|
|
12464
12509
|
fetcher
|
|
12465
12510
|
);
|
|
12466
12511
|
if (!envelope.ok || envelope.data === void 0) return null;
|
|
12467
|
-
|
|
12512
|
+
if (!Array.isArray(envelope.data.items)) return null;
|
|
12513
|
+
const items = envelope.data.items;
|
|
12468
12514
|
return items.filter(
|
|
12469
12515
|
(raw) => typeof raw === "object" && raw !== null && !Array.isArray(raw)
|
|
12470
12516
|
);
|
|
@@ -12498,7 +12544,8 @@ async function checkInRaccoon(product, credential, fetcher = fetch) {
|
|
|
12498
12544
|
for (const item of after) {
|
|
12499
12545
|
if (!isDailyGrantBill(item, today)) continue;
|
|
12500
12546
|
if (beforeKeys.has(billKey(item))) continue;
|
|
12501
|
-
|
|
12547
|
+
beforeKeys.add(billKey(item));
|
|
12548
|
+
credit += readNumber3(item.points) ?? 0;
|
|
12502
12549
|
const event = String(item.event_name ?? "");
|
|
12503
12550
|
if (event.length > 0 && !names.includes(event)) names.push(event);
|
|
12504
12551
|
}
|
|
@@ -13739,7 +13786,10 @@ function isPaidPlanType(planType) {
|
|
|
13739
13786
|
function parseRaccoonPlan(payload) {
|
|
13740
13787
|
const root = asRecord(payload);
|
|
13741
13788
|
if (root === void 0) return "unknown";
|
|
13742
|
-
|
|
13789
|
+
if ((typeof root.code === "number" || typeof root.code === "string") && root.code !== 0 && root.code !== "0") return "unknown";
|
|
13790
|
+
const containers = [root, root.data, root.result, root.code].map(asRecord);
|
|
13791
|
+
const office = containers.map((block) => asRecord(block?.office)).filter(Boolean);
|
|
13792
|
+
const blocks = (office.length ? office : containers).map(asRecord).filter((block) => block !== void 0);
|
|
13743
13793
|
let explicitFree = false;
|
|
13744
13794
|
for (const block of blocks) {
|
|
13745
13795
|
if (block.pro_enable === true) return "pro";
|
|
@@ -14027,7 +14077,7 @@ var RaccoonAuth = class extends Service {
|
|
|
14027
14077
|
* (登录成功后才探测),探测失败不能让用户看到「登录失败」——那会让一个
|
|
14028
14078
|
* 只是拿不到套餐信息的账号被反复重登。调用方把 `'unknown'` 归到免费侧。
|
|
14029
14079
|
*/
|
|
14030
|
-
async fetchPlan(credential) {
|
|
14080
|
+
async fetchPlan(credential, signal) {
|
|
14031
14081
|
if (typeof credential?.access_token !== "string" || credential.access_token.length === 0) return "unknown";
|
|
14032
14082
|
try {
|
|
14033
14083
|
const response = await this.fetchImpl(
|
|
@@ -14037,10 +14087,11 @@ var RaccoonAuth = class extends Service {
|
|
|
14037
14087
|
headers: {
|
|
14038
14088
|
Accept: "application/json",
|
|
14039
14089
|
Authorization: `Bearer ${credential.access_token}`,
|
|
14040
|
-
"X-Org-Code": credential.office_identity
|
|
14090
|
+
...credential.office_identity && credential.office_identity !== "personal" ? { "X-Org-Code": credential.office_identity } : {},
|
|
14041
14091
|
"X-Raccoon-Language": "zh"
|
|
14042
14092
|
},
|
|
14043
|
-
|
|
14093
|
+
redirect: "error",
|
|
14094
|
+
signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(6e3)]) : AbortSignal.timeout(6e3)
|
|
14044
14095
|
}
|
|
14045
14096
|
);
|
|
14046
14097
|
if (!response.ok) return "unknown";
|
|
@@ -187,7 +187,7 @@ export function parseAStudioCatalog(json) {
|
|
|
187
187
|
out.push({
|
|
188
188
|
id,
|
|
189
189
|
name: typeof item.display_name === 'string' && item.display_name.trim() ? item.display_name.trim() : id,
|
|
190
|
-
context:
|
|
190
|
+
context: [item.context_window, item.max_context_window].find(value => Number.isSafeInteger(value) && value > 0) ?? 262_144,
|
|
191
191
|
output: [item.max_output_tokens, item.max_tokens].find(value => Number.isSafeInteger(value) && value > 0) ?? 100_000,
|
|
192
192
|
attachment: modalities.includes('image'),
|
|
193
193
|
efforts,
|
|
@@ -62,9 +62,11 @@ function parseContext(row) {
|
|
|
62
62
|
const marked = entries.find(entry => entry?.isDefault === true || entry?.is_default === true)
|
|
63
63
|
const preferred = tokens(row.defaultContextWindow ?? row.default_context_window) ??
|
|
64
64
|
tokens(marked?.token_count ?? marked?.tokenCount)
|
|
65
|
-
const context = preferred !== null && (cap === null || preferred <= cap) ? preferred : cap
|
|
66
65
|
const options = [...new Set(values.map(value => tokens(value?.tokenCount ?? value?.token_count ?? value))
|
|
67
66
|
.filter(value => value !== null && (cap === null || value <= cap)))].sort((a, b) => a - b)
|
|
67
|
+
// A ceiling only validates the offered settings; it is not itself a
|
|
68
|
+
// selectable profile. Some catalogs declare only a window list.
|
|
69
|
+
const context = preferred !== null && (cap === null || preferred <= cap) ? preferred : options.at(-1) ?? cap
|
|
68
70
|
return { context, ...(options.length && context ? { contextOptions: [...new Set([...options, context])].sort((a, b) => a - b) } : {}) }
|
|
69
71
|
}
|
|
70
72
|
|
|
@@ -44,22 +44,25 @@ function canonicalArguments(parts) {
|
|
|
44
44
|
|
|
45
45
|
const INVALID_TOOLS = '千问办公返回了冲突或损坏的工具参数;本轮工具未执行,请重试。'
|
|
46
46
|
const MAX_TOOL_BYTES = 2 * 1024 * 1024
|
|
47
|
+
const completeArguments = item => item.canonical !== undefined
|
|
48
|
+
? item.canonical : (item.canonical = canonicalArguments(item.parts))
|
|
47
49
|
|
|
48
50
|
async function* unwrapStream(body, signal) {
|
|
49
51
|
let sawChoice = false
|
|
50
52
|
let finished = false
|
|
51
53
|
let toolBytes = 0
|
|
52
54
|
const calls = new Map()
|
|
53
|
-
const ids = new
|
|
55
|
+
const ids = new Map()
|
|
54
56
|
const closed = new Set()
|
|
55
57
|
const flush = choice => {
|
|
56
|
-
const
|
|
57
|
-
if (!
|
|
58
|
+
const state = calls.get(choice.index)
|
|
59
|
+
if (!state) return []
|
|
58
60
|
if (choice.finish_reason !== 'tool_calls') return null
|
|
59
61
|
const tools = []
|
|
60
|
-
for (const
|
|
61
|
-
const args =
|
|
62
|
+
for (const item of state.ordered) {
|
|
63
|
+
const args = completeArguments(item)
|
|
62
64
|
if (args === null || !item.id || !item.name) return null
|
|
65
|
+
const index = state.reusedIndex ? tools.length : item.index
|
|
63
66
|
tools.push({ index, id: item.id, type: 'function', function: { name: item.name, arguments: args } })
|
|
64
67
|
}
|
|
65
68
|
calls.delete(choice.index)
|
|
@@ -78,25 +81,53 @@ async function* unwrapStream(body, signal) {
|
|
|
78
81
|
const delta = choice.delta
|
|
79
82
|
if (delta?.tool_calls != null) {
|
|
80
83
|
if (!Number.isSafeInteger(choice.index) || !Array.isArray(delta.tool_calls) || closed.has(choice.index)) { invalid = true; break }
|
|
81
|
-
const
|
|
84
|
+
const state = calls.get(choice.index) ?? { slots: new Map(), ordered: [], reusedIndex: false }
|
|
82
85
|
for (const tool of delta.tool_calls) {
|
|
83
86
|
if (!Number.isSafeInteger(tool?.index) || tool.index < 0 || tool.index > 127 || tool.type != null && tool.type !== 'function') { invalid = true; break }
|
|
84
|
-
const item = items.get(tool.index) ?? { id: '', name: '', parts: [] }
|
|
85
87
|
const next = tool.function ?? {}
|
|
86
|
-
if (tool.id != null &&
|
|
87
|
-
next.name != null &&
|
|
88
|
+
if (tool.id != null && typeof tool.id !== 'string' ||
|
|
89
|
+
next.name != null && typeof next.name !== 'string' ||
|
|
88
90
|
next.arguments != null && typeof next.arguments !== 'string') { invalid = true; break }
|
|
89
|
-
|
|
91
|
+
let slot = state.slots.get(tool.index)
|
|
92
|
+
if (!slot) {
|
|
93
|
+
slot = { active: null, previous: [] }
|
|
94
|
+
state.slots.set(tool.index, slot)
|
|
95
|
+
}
|
|
96
|
+
const known = tool.id ? ids.get(tool.id) : undefined
|
|
97
|
+
if (known && known.slot !== slot) { invalid = true; break }
|
|
98
|
+
let item = known ?? slot.active
|
|
99
|
+
if (!item || !known && tool.id && item.id) {
|
|
100
|
+
if (state.ordered.length >= 128) { invalid = true; break }
|
|
101
|
+
if (item) {
|
|
102
|
+
slot.previous.push(item)
|
|
103
|
+
state.reusedIndex = true
|
|
104
|
+
}
|
|
105
|
+
item = { index: tool.index, slot, id: '', name: '', parts: [] }
|
|
106
|
+
state.ordered.push(item)
|
|
107
|
+
slot.active = item
|
|
108
|
+
}
|
|
109
|
+
// QwenWork may reuse index 0 for distinct calls. An explicit ID
|
|
110
|
+
// always owns its fragment. Without an ID, a reused slot has only
|
|
111
|
+
// one eligible receiver if all older calls already have complete
|
|
112
|
+
// arguments. Do not infer ownership from a later successful parse.
|
|
113
|
+
if (!tool.id && (next.arguments || next.name) && slot.previous.length) {
|
|
114
|
+
if (slot.previous.some(previous => {
|
|
115
|
+
const args = previous.name && completeArguments(previous)
|
|
116
|
+
return !args || args === next.arguments
|
|
117
|
+
})) { invalid = true; break }
|
|
118
|
+
}
|
|
119
|
+
if (item.name && next.name && item.name !== next.name) { invalid = true; break }
|
|
120
|
+
if (tool.id && !item.id) { item.id = tool.id; ids.set(tool.id, item) }
|
|
90
121
|
if (next.name) item.name = next.name
|
|
91
122
|
if (next.arguments) {
|
|
92
123
|
toolBytes += encoder.encode(next.arguments).byteLength
|
|
93
124
|
if (toolBytes > MAX_TOOL_BYTES) { invalid = true; break }
|
|
94
125
|
item.parts.push(next.arguments)
|
|
126
|
+
item.canonical = undefined
|
|
95
127
|
}
|
|
96
|
-
items.set(tool.index, item)
|
|
97
128
|
}
|
|
98
129
|
if (invalid) break
|
|
99
|
-
if (
|
|
130
|
+
if (state.ordered.length) calls.set(choice.index, state)
|
|
100
131
|
}
|
|
101
132
|
if (typeof choice.finish_reason === 'string' && choice.finish_reason !== '') {
|
|
102
133
|
const tools = flush(choice)
|
|
@@ -78,7 +78,7 @@ export function settingsResources(read, publicResult, { managedIds = MANAGED_PRO
|
|
|
78
78
|
register('model.list', provider.id)
|
|
79
79
|
register('model.params', provider.id)
|
|
80
80
|
if (provider.accountPool === false) continue
|
|
81
|
-
register('account.list', provider.id, { project: value => publicResult('account.list', {
|
|
81
|
+
register('account.list', provider.id, { payload: { provider: provider.id, ...(provider.id === RACCOON_CHECKIN_PROVIDER_ID ? { refreshPlan: true } : {}) }, project: value => publicResult('account.list', {
|
|
82
82
|
accounts: value?.accounts?.filter(account => account.provider === provider.id),
|
|
83
83
|
}) })
|
|
84
84
|
register('credits.balances', provider.id, { merge: mergeBalances })
|
package/lib/web-host.js
CHANGED
|
@@ -741,7 +741,10 @@ ${String(error?.stack ?? error)}
|
|
|
741
741
|
adapters: checkinAdapters,
|
|
742
742
|
isEnabled: provider => automationSettings.isAutoCheckinEnabled(provider),
|
|
743
743
|
logger: host.logger,
|
|
744
|
-
|
|
744
|
+
// 小浣熊总余额会因登录奖励、会员或充值增加,也可能因消费抵消签到;
|
|
745
|
+
// 只用每日发放账单确认签到,不用余额差推算金额。
|
|
746
|
+
readBalance: (provider, accountId) => provider === 'raccoon' || provider === 'raccoon-pro'
|
|
747
|
+
? null : readBalance(provider, accountId),
|
|
745
748
|
async onChange(provider, outcome) {
|
|
746
749
|
await snapshots.changed(checkinKey)
|
|
747
750
|
if (outcome.kind === 'claimed') {
|
|
@@ -991,7 +994,13 @@ ${String(error?.stack ?? error)}
|
|
|
991
994
|
} else if (managedManagers.has(provider)) {
|
|
992
995
|
await managedRouter.invalidate(provider)
|
|
993
996
|
}
|
|
994
|
-
|
|
997
|
+
if (provider === 'raccoon' && wait && config.backend === 'standalone') {
|
|
998
|
+
job = rpc('account.list', { provider, refreshPlan: true, forcePlanRefresh: true }, request.signal)
|
|
999
|
+
.catch(() => {}).then(() => snapshots.changed(settingsResourceKey('account.list', provider)))
|
|
1000
|
+
.then(() => snapshots.refresh(['provider.status', checkinKey, automationKey, ...keys]))
|
|
1001
|
+
} else {
|
|
1002
|
+
job = snapshots.refresh(['provider.status', checkinKey, automationKey, ...keys])
|
|
1003
|
+
}
|
|
995
1004
|
if (wait) await job
|
|
996
1005
|
return reply(rpcId, { ok: true, value: null })
|
|
997
1006
|
}
|