mslxdff 0.1.231 → 0.1.233
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/bin/mslxdff.js +3 -3
- package/package.json +1 -1
- package/src/auto.js +264 -254
- package/src/bench/cline-bench.js +42 -42
- package/src/bench/probe.js +70 -70
- package/src/bench/report.js +162 -162
- package/src/bench/runner.js +77 -77
- package/src/bench/via-probe.js +124 -124
- package/src/bench/via-routes.js +87 -87
- package/src/bench/workbuddy-bench.js +54 -54
- package/src/chat/engine.js +160 -160
- package/src/chat/gateway.js +163 -163
- package/src/chat/orchestrator.js +234 -234
- package/src/chat/prompt.js +70 -70
- package/src/chat/repl.js +88 -88
- package/src/chat/terminal.js +135 -135
- package/src/chat/tools.js +306 -306
- package/src/chat-pipeline/index.js +155 -145
- package/src/chat-pipeline/policy.js +84 -84
- package/src/cli/commands/group.js +266 -266
- package/src/cli/commands/model/picks.js +50 -50
- package/src/cli/commands/provider/bench-via.js +247 -247
- package/src/cli/commands/provider/bench.js +141 -141
- package/src/cli/commands/provider/index.js +139 -139
- package/src/cli/commands/provider/models.js +149 -149
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/provider/raccoon-quota.js +156 -156
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +246 -246
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/parse.js +70 -70
- package/src/model-trace.js +10 -2
- package/src/models.js +292 -225
- package/src/peers.js +345 -345
- package/src/providers/agentrouter.js +238 -238
- package/src/providers/base.js +164 -164
- package/src/providers/cline/auth.js +228 -228
- package/src/providers/cline/chat.js +307 -307
- package/src/providers/cline.js +2 -2
- package/src/providers/codearts/chat.js +207 -207
- package/src/providers/dispatcher.js +147 -147
- package/src/providers/globalqwenwork/cosy.js +123 -123
- package/src/providers/globalqwenwork/crypto.js +218 -218
- package/src/providers/globalqwenwork/http.js +20 -20
- package/src/providers/globalqwenwork/rsa.js +54 -54
- package/src/providers/globalqwenwork/sse.js +268 -268
- package/src/providers/globalqwenwork/stream.js +130 -130
- package/src/providers/globalqwenwork/upstream.js +122 -122
- package/src/providers/keyring.js +60 -60
- package/src/providers/qoder/account-store.js +18 -0
- package/src/providers/qoder/chat.js +292 -203
- package/src/providers/qoder/checkin.js +25 -12
- package/src/providers/qoder/index.js +311 -249
- package/src/providers/qoder/models.js +64 -2
- package/src/providers/qoder/oauth.js +42 -1
- package/src/providers/qoder/payload.js +116 -80
- package/src/providers/qoder/request.js +7 -2
- package/src/providers/qoder/session.js +26 -10
- package/src/providers/qoder/sse.js +233 -145
- package/src/providers/qoder/sticky.js +5 -2
- package/src/providers/qoder/wire-messages.js +242 -0
- package/src/providers/qwenwork/account-store.js +133 -133
- package/src/providers/qwenwork/constants.js +67 -67
- package/src/providers/qwenwork/cosy.js +120 -120
- package/src/providers/qwenwork/crypto.js +218 -218
- package/src/providers/qwenwork/http.js +20 -20
- package/src/providers/qwenwork/index.js +337 -337
- package/src/providers/qwenwork/payload.js +144 -144
- package/src/providers/qwenwork/rsa.js +54 -54
- package/src/providers/qwenwork/sse.js +268 -268
- package/src/providers/qwenwork/stream.js +130 -130
- package/src/providers/qwenwork/upstream.js +120 -120
- package/src/providers/qwenwork.js +1 -1
- package/src/providers/raccoon/chat.js +1 -1
- package/src/providers/raccoon/const.js +219 -202
- package/src/providers/raccoon/credits.js +285 -285
- package/src/providers/raccoon/index.js +15 -2
- package/src/providers/raccoon/models.js +68 -17
- package/src/providers/registry.js +89 -89
- package/src/providers/workbuddy/auth.js +16 -0
- package/src/providers/workbuddy/chat.js +298 -264
- package/src/providers/workbuddy/client-headers.js +148 -0
- package/src/providers/workbuddy/desktop-info.js +17 -1
- package/src/providers/workbuddy/models.js +8 -20
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy.js +2 -2
- package/src/reasoning.js +32 -32
- package/src/routes/chat/broadband-handler.js +80 -80
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +282 -282
- package/src/routes/chat/via-route-handler.js +156 -156
- package/src/routes/hedge.js +260 -260
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +373 -373
- package/src/routes/stream.js +364 -347
- package/src/runtime/bootstrap.js +75 -75
- package/src/runtime/keep-warm.js +135 -135
- package/src/runtime/provider-gate.js +75 -75
- package/src/runtime/providers-setup.js +187 -187
- package/src/runtime/raccoon-checkin.js +175 -175
- package/src/server.js +85 -85
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/transport/index.js +244 -244
- package/src/transport/pool.js +56 -56
- package/src/transport/retry.js +24 -24
- package/src/transport/sse.js +93 -93
- package/src/upstream-probe/display.js +52 -52
- package/src/upstream-probe/probe.js +49 -49
- package/src/upstream-probe/rotate.js +110 -110
- package/src/upstream-probe/start.js +45 -45
- package/src/upstream.js +355 -355
- package/docs/cli_help_mini.md +0 -148
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
// vendor 自 src/providers/qwenwork/sse.js 的 transform(真流式:上游 SSE → OpenAI chunk,边收边吐)。
|
|
2
|
-
// 唯一一处**有意偏离**原作者实现:把 chunk 里的 `model` 回写成客户端请求的模型名。
|
|
3
|
-
// 上游实际返回的是池名(实测 `qwork-openai-chat-mode-pool`),直接透传会让客户端按模型名匹配失败,
|
|
4
|
-
// 违反本仓契约 1「model 原样透传」。原作者的非流式路径同样做了 `completion.model = wantModel`。
|
|
5
|
-
import { handleLine } from "./sse.js";
|
|
6
|
-
|
|
7
|
-
function rewriteModel(frameText, model) {
|
|
8
|
-
if (!model) return frameText;
|
|
9
|
-
const body = frameText.slice(6, -2);
|
|
10
|
-
let obj;
|
|
11
|
-
try {
|
|
12
|
-
obj = JSON.parse(body);
|
|
13
|
-
} catch {
|
|
14
|
-
return frameText;
|
|
15
|
-
}
|
|
16
|
-
if (!obj || typeof obj !== "object") return frameText;
|
|
17
|
-
obj.model = model;
|
|
18
|
-
return `data: ${JSON.stringify(obj)}\n\n`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function transformStream(upstreamBody, model, onUsage, { heartbeatMs = 15000 } = {}) {
|
|
22
|
-
const reader = upstreamBody.getReader();
|
|
23
|
-
const decoder = new TextDecoder();
|
|
24
|
-
const encoder = new TextEncoder();
|
|
25
|
-
let buffer = "";
|
|
26
|
-
let notified = false;
|
|
27
|
-
let heartbeat = null;
|
|
28
|
-
const state = { usage: null, error: null };
|
|
29
|
-
|
|
30
|
-
const notify = () => {
|
|
31
|
-
if (notified) return;
|
|
32
|
-
notified = true;
|
|
33
|
-
if (onUsage) onUsage(state.usage);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const emitError = (controller, message) => {
|
|
37
|
-
try {
|
|
38
|
-
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: { message, type: "upstream_error" } })}\n\n`));
|
|
39
|
-
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
40
|
-
controller.close();
|
|
41
|
-
} catch {
|
|
42
|
-
/* 客户端已断开 */
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const stopHeartbeat = () => {
|
|
47
|
-
if (heartbeat) {
|
|
48
|
-
clearInterval(heartbeat);
|
|
49
|
-
heartbeat = null;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const finish = () => {
|
|
54
|
-
stopHeartbeat();
|
|
55
|
-
notify();
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
return new ReadableStream({
|
|
59
|
-
start(controller) {
|
|
60
|
-
if (heartbeatMs > 0) {
|
|
61
|
-
heartbeat = setInterval(() => {
|
|
62
|
-
try {
|
|
63
|
-
controller.enqueue(encoder.encode(": ping\n\n"));
|
|
64
|
-
} catch {
|
|
65
|
-
/* 已关闭 */
|
|
66
|
-
}
|
|
67
|
-
}, heartbeatMs);
|
|
68
|
-
if (heartbeat.unref) heartbeat.unref();
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
async pull(controller) {
|
|
72
|
-
try {
|
|
73
|
-
for (;;) {
|
|
74
|
-
let read;
|
|
75
|
-
try {
|
|
76
|
-
read = await reader.read();
|
|
77
|
-
} catch (err) {
|
|
78
|
-
const message = `upstream stream read error: ${err && err.message ? err.message : err}`;
|
|
79
|
-
stopHeartbeat();
|
|
80
|
-
emitError(controller, message);
|
|
81
|
-
notify();
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
if (read.done) {
|
|
85
|
-
stopHeartbeat();
|
|
86
|
-
if (buffer) {
|
|
87
|
-
const out = handleLine(buffer, state);
|
|
88
|
-
if (out) controller.enqueue(encoder.encode(rewriteModel(out, model)));
|
|
89
|
-
buffer = "";
|
|
90
|
-
}
|
|
91
|
-
if (state.error) emitError(controller, state.error);
|
|
92
|
-
else {
|
|
93
|
-
try {
|
|
94
|
-
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
95
|
-
controller.close();
|
|
96
|
-
} catch {
|
|
97
|
-
/* 客户端已断开 */
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
finish();
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
buffer += decoder.decode(read.value, { stream: true });
|
|
104
|
-
const lines = buffer.split("\n");
|
|
105
|
-
buffer = lines.pop() || "";
|
|
106
|
-
let out = "";
|
|
107
|
-
for (const line of lines) {
|
|
108
|
-
const frame = handleLine(line, state);
|
|
109
|
-
if (frame) out += rewriteModel(frame, model);
|
|
110
|
-
}
|
|
111
|
-
if (out) {
|
|
112
|
-
controller.enqueue(encoder.encode(out));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
} catch (err) {
|
|
117
|
-
finish();
|
|
118
|
-
try {
|
|
119
|
-
emitError(controller, `stream error: ${err && err.message ? err.message : err}`);
|
|
120
|
-
} catch {
|
|
121
|
-
/* 已关闭 */
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
cancel(reason) {
|
|
126
|
-
finish();
|
|
127
|
-
if (reader.cancel) reader.cancel(reason);
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
}
|
|
1
|
+
// vendor 自 src/providers/qwenwork/sse.js 的 transform(真流式:上游 SSE → OpenAI chunk,边收边吐)。
|
|
2
|
+
// 唯一一处**有意偏离**原作者实现:把 chunk 里的 `model` 回写成客户端请求的模型名。
|
|
3
|
+
// 上游实际返回的是池名(实测 `qwork-openai-chat-mode-pool`),直接透传会让客户端按模型名匹配失败,
|
|
4
|
+
// 违反本仓契约 1「model 原样透传」。原作者的非流式路径同样做了 `completion.model = wantModel`。
|
|
5
|
+
import { handleLine } from "./sse.js";
|
|
6
|
+
|
|
7
|
+
function rewriteModel(frameText, model) {
|
|
8
|
+
if (!model) return frameText;
|
|
9
|
+
const body = frameText.slice(6, -2);
|
|
10
|
+
let obj;
|
|
11
|
+
try {
|
|
12
|
+
obj = JSON.parse(body);
|
|
13
|
+
} catch {
|
|
14
|
+
return frameText;
|
|
15
|
+
}
|
|
16
|
+
if (!obj || typeof obj !== "object") return frameText;
|
|
17
|
+
obj.model = model;
|
|
18
|
+
return `data: ${JSON.stringify(obj)}\n\n`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function transformStream(upstreamBody, model, onUsage, { heartbeatMs = 15000 } = {}) {
|
|
22
|
+
const reader = upstreamBody.getReader();
|
|
23
|
+
const decoder = new TextDecoder();
|
|
24
|
+
const encoder = new TextEncoder();
|
|
25
|
+
let buffer = "";
|
|
26
|
+
let notified = false;
|
|
27
|
+
let heartbeat = null;
|
|
28
|
+
const state = { usage: null, error: null };
|
|
29
|
+
|
|
30
|
+
const notify = () => {
|
|
31
|
+
if (notified) return;
|
|
32
|
+
notified = true;
|
|
33
|
+
if (onUsage) onUsage(state.usage);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const emitError = (controller, message) => {
|
|
37
|
+
try {
|
|
38
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: { message, type: "upstream_error" } })}\n\n`));
|
|
39
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
40
|
+
controller.close();
|
|
41
|
+
} catch {
|
|
42
|
+
/* 客户端已断开 */
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const stopHeartbeat = () => {
|
|
47
|
+
if (heartbeat) {
|
|
48
|
+
clearInterval(heartbeat);
|
|
49
|
+
heartbeat = null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const finish = () => {
|
|
54
|
+
stopHeartbeat();
|
|
55
|
+
notify();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return new ReadableStream({
|
|
59
|
+
start(controller) {
|
|
60
|
+
if (heartbeatMs > 0) {
|
|
61
|
+
heartbeat = setInterval(() => {
|
|
62
|
+
try {
|
|
63
|
+
controller.enqueue(encoder.encode(": ping\n\n"));
|
|
64
|
+
} catch {
|
|
65
|
+
/* 已关闭 */
|
|
66
|
+
}
|
|
67
|
+
}, heartbeatMs);
|
|
68
|
+
if (heartbeat.unref) heartbeat.unref();
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
async pull(controller) {
|
|
72
|
+
try {
|
|
73
|
+
for (;;) {
|
|
74
|
+
let read;
|
|
75
|
+
try {
|
|
76
|
+
read = await reader.read();
|
|
77
|
+
} catch (err) {
|
|
78
|
+
const message = `upstream stream read error: ${err && err.message ? err.message : err}`;
|
|
79
|
+
stopHeartbeat();
|
|
80
|
+
emitError(controller, message);
|
|
81
|
+
notify();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (read.done) {
|
|
85
|
+
stopHeartbeat();
|
|
86
|
+
if (buffer) {
|
|
87
|
+
const out = handleLine(buffer, state);
|
|
88
|
+
if (out) controller.enqueue(encoder.encode(rewriteModel(out, model)));
|
|
89
|
+
buffer = "";
|
|
90
|
+
}
|
|
91
|
+
if (state.error) emitError(controller, state.error);
|
|
92
|
+
else {
|
|
93
|
+
try {
|
|
94
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
95
|
+
controller.close();
|
|
96
|
+
} catch {
|
|
97
|
+
/* 客户端已断开 */
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
finish();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
buffer += decoder.decode(read.value, { stream: true });
|
|
104
|
+
const lines = buffer.split("\n");
|
|
105
|
+
buffer = lines.pop() || "";
|
|
106
|
+
let out = "";
|
|
107
|
+
for (const line of lines) {
|
|
108
|
+
const frame = handleLine(line, state);
|
|
109
|
+
if (frame) out += rewriteModel(frame, model);
|
|
110
|
+
}
|
|
111
|
+
if (out) {
|
|
112
|
+
controller.enqueue(encoder.encode(out));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} catch (err) {
|
|
117
|
+
finish();
|
|
118
|
+
try {
|
|
119
|
+
emitError(controller, `stream error: ${err && err.message ? err.message : err}`);
|
|
120
|
+
} catch {
|
|
121
|
+
/* 已关闭 */
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
cancel(reason) {
|
|
126
|
+
finish();
|
|
127
|
+
if (reader.cancel) reader.cancel(reason);
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
131
|
|
|
132
132
|
// globalqwenwork 变体:国际站 gateway.qwenwork.ai,协议与 cn 逐字同构(2026-10-01 现网取证),仅命名空间与常量不同 — 见 docs/adr/0041-globalqwenwork-international-provider.md
|
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
// upstream.js(签名请求装配 + OAuth 设备流)— vendor 自 src/providers/qwenwork/upstream.js 逐字搬;
|
|
2
|
-
// URL 全部来自本目录 constants.js(BASE=.ai),client_id/redirect_uri 自动取国际站值。
|
|
3
|
-
import { compatFetch } from "../../compat.js";
|
|
4
|
-
import { uuid, b64url, utf8 } from "./crypto.js";
|
|
5
|
-
import { signHeaders } from "./cosy.js";
|
|
6
|
-
import {
|
|
7
|
-
UA, CLIENT_ID, REDIRECT_URI, DEVICE_AUTH_URL, CHAT_URL, MODELS_URL,
|
|
8
|
-
USERINFO_URL, ACCOUNT_CONTEXT_URL, POLL_URL, REFRESH_URL, MODEL_SLICE,
|
|
9
|
-
} from "./constants.js";
|
|
10
|
-
|
|
11
|
-
function plainHeaders() {
|
|
12
|
-
return {
|
|
13
|
-
accept: "application/json, text/plain, */*",
|
|
14
|
-
"content-type": "application/json",
|
|
15
|
-
"user-agent": UA,
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function chatRequest(account, modelKey, bodyStr, signal, fetchImpl = compatFetch) {
|
|
20
|
-
const headers = signHeaders(account, bodyStr, CHAT_URL, modelKey, "text/event-stream");
|
|
21
|
-
return fetchImpl(CHAT_URL, { method: "POST", headers, body: bodyStr, signal });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function modelList(account, fetchImpl = compatFetch) {
|
|
25
|
-
const headers = signHeaders(account, "", MODELS_URL, "", "application/json");
|
|
26
|
-
const res = await fetchImpl(MODELS_URL, { headers });
|
|
27
|
-
if (!res.ok) throw new Error(`models http ${res.status}`);
|
|
28
|
-
const data = await res.json();
|
|
29
|
-
// 实测 11 个根切片只有 `qwork` 非空(其余 n=0);切片名见 constants.MODEL_SLICE
|
|
30
|
-
const list = Array.isArray(data && data[MODEL_SLICE]) ? data[MODEL_SLICE] : [];
|
|
31
|
-
return list
|
|
32
|
-
.filter((m) => m && m.enable !== false && m.key)
|
|
33
|
-
.map((m) => ({
|
|
34
|
-
key: m.key,
|
|
35
|
-
name: m.display_name || m.key,
|
|
36
|
-
isReasoning: !!m.is_reasoning,
|
|
37
|
-
isVL: !!m.is_vl,
|
|
38
|
-
maxInputTokens: m.max_input_tokens || 0,
|
|
39
|
-
maxOutputTokens: m.max_output_tokens || 0,
|
|
40
|
-
priceFactor: typeof m.price_factor === "number" ? m.price_factor : 0,
|
|
41
|
-
description: typeof m.description === "string" ? m.description : "",
|
|
42
|
-
}));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function userInfo(accessToken, fetchImpl = compatFetch) {
|
|
46
|
-
const res = await fetchImpl(USERINFO_URL, {
|
|
47
|
-
headers: { ...plainHeaders(), authorization: `Bearer ${accessToken}` },
|
|
48
|
-
});
|
|
49
|
-
if (!res.ok) throw new Error(`userinfo http ${res.status}`);
|
|
50
|
-
return res.json();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 额度/套餐:data.{user,plan,quota};quota.remaining 即积分余量。 */
|
|
54
|
-
export async function accountContext(accessToken, fetchImpl = compatFetch) {
|
|
55
|
-
const res = await fetchImpl(ACCOUNT_CONTEXT_URL, {
|
|
56
|
-
headers: { ...plainHeaders(), authorization: `Bearer ${accessToken}` },
|
|
57
|
-
});
|
|
58
|
-
if (!res.ok) throw new Error(`account-context http ${res.status}`);
|
|
59
|
-
const body = await res.json();
|
|
60
|
-
if (!body || (body.code && body.code !== "ok")) throw new Error(`account-context code=${body && body.code}`);
|
|
61
|
-
return (body && body.data) || {};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export async function refreshDeviceToken(refreshToken, fetchImpl = compatFetch) {
|
|
65
|
-
const res = await fetchImpl(REFRESH_URL, {
|
|
66
|
-
method: "POST",
|
|
67
|
-
headers: plainHeaders(),
|
|
68
|
-
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
69
|
-
});
|
|
70
|
-
if (!res.ok) throw new Error(`refresh http ${res.status}`);
|
|
71
|
-
const data = await res.json();
|
|
72
|
-
const access = data && (data.token || data.device_token);
|
|
73
|
-
if (!access || !data.refresh_token) throw new Error("refresh: incomplete token pair");
|
|
74
|
-
return data;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export async function pollGrant(nonce, verifier, fetchImpl = compatFetch) {
|
|
78
|
-
const q = new URLSearchParams({ nonce, verifier, challenge_method: "S256" });
|
|
79
|
-
const res = await fetchImpl(`${POLL_URL}?${q.toString()}`, {
|
|
80
|
-
headers: { accept: "application/json", "user-agent": UA },
|
|
81
|
-
});
|
|
82
|
-
if (res.status === 404 || res.status === 202) return { pending: true };
|
|
83
|
-
if (!res.ok) throw new Error(`poll http ${res.status}`);
|
|
84
|
-
const data = await res.json();
|
|
85
|
-
if (!data || !(data.token || data.device_token)) return { pending: true };
|
|
86
|
-
return { pending: false, grant: data };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export async function newDeviceFlow() {
|
|
90
|
-
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
91
|
-
const raw = new Uint8Array(64);
|
|
92
|
-
crypto.getRandomValues(raw);
|
|
93
|
-
let verifier = "";
|
|
94
|
-
for (let i = 0; i < raw.length; i++) verifier += alphabet[raw[i] % alphabet.length];
|
|
95
|
-
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(verifier)));
|
|
96
|
-
const challenge = b64url(digest);
|
|
97
|
-
const nonce = uuid();
|
|
98
|
-
const machineID = uuid();
|
|
99
|
-
const q = new URLSearchParams({
|
|
100
|
-
challenge,
|
|
101
|
-
challenge_method: "S256",
|
|
102
|
-
nonce,
|
|
103
|
-
machine_id: machineID,
|
|
104
|
-
client_id: CLIENT_ID,
|
|
105
|
-
redirect_uri: REDIRECT_URI,
|
|
106
|
-
});
|
|
107
|
-
return { verifier, nonce, url: `${DEVICE_AUTH_URL}?${q.toString()}` };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function expiryUnix(grant) {
|
|
111
|
-
if (grant && grant.expires_at) {
|
|
112
|
-
const t = Date.parse(grant.expires_at);
|
|
113
|
-
if (!Number.isNaN(t)) return Math.floor(t / 1000);
|
|
114
|
-
}
|
|
115
|
-
if (grant && grant.expires_in > 0) {
|
|
116
|
-
const ms = grant.expires_in > 10000000 ? grant.expires_in : grant.expires_in * 1000;
|
|
117
|
-
return Math.floor((Date.now() + ms) / 1000);
|
|
118
|
-
}
|
|
119
|
-
return Math.floor((Date.now() + 30 * 24 * 3600 * 1000) / 1000);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// globalqwenwork 变体(国际站 gateway.qwenwork.ai):协议与 cn 逐字同构,2026-10-01 现网实测签名/请求 200 — 见 docs/adr/0041-globalqwenwork-international-provider.md
|
|
1
|
+
// upstream.js(签名请求装配 + OAuth 设备流)— vendor 自 src/providers/qwenwork/upstream.js 逐字搬;
|
|
2
|
+
// URL 全部来自本目录 constants.js(BASE=.ai),client_id/redirect_uri 自动取国际站值。
|
|
3
|
+
import { compatFetch } from "../../compat.js";
|
|
4
|
+
import { uuid, b64url, utf8 } from "./crypto.js";
|
|
5
|
+
import { signHeaders } from "./cosy.js";
|
|
6
|
+
import {
|
|
7
|
+
UA, CLIENT_ID, REDIRECT_URI, DEVICE_AUTH_URL, CHAT_URL, MODELS_URL,
|
|
8
|
+
USERINFO_URL, ACCOUNT_CONTEXT_URL, POLL_URL, REFRESH_URL, MODEL_SLICE,
|
|
9
|
+
} from "./constants.js";
|
|
10
|
+
|
|
11
|
+
function plainHeaders() {
|
|
12
|
+
return {
|
|
13
|
+
accept: "application/json, text/plain, */*",
|
|
14
|
+
"content-type": "application/json",
|
|
15
|
+
"user-agent": UA,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function chatRequest(account, modelKey, bodyStr, signal, fetchImpl = compatFetch) {
|
|
20
|
+
const headers = signHeaders(account, bodyStr, CHAT_URL, modelKey, "text/event-stream");
|
|
21
|
+
return fetchImpl(CHAT_URL, { method: "POST", headers, body: bodyStr, signal });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function modelList(account, fetchImpl = compatFetch) {
|
|
25
|
+
const headers = signHeaders(account, "", MODELS_URL, "", "application/json");
|
|
26
|
+
const res = await fetchImpl(MODELS_URL, { headers });
|
|
27
|
+
if (!res.ok) throw new Error(`models http ${res.status}`);
|
|
28
|
+
const data = await res.json();
|
|
29
|
+
// 实测 11 个根切片只有 `qwork` 非空(其余 n=0);切片名见 constants.MODEL_SLICE
|
|
30
|
+
const list = Array.isArray(data && data[MODEL_SLICE]) ? data[MODEL_SLICE] : [];
|
|
31
|
+
return list
|
|
32
|
+
.filter((m) => m && m.enable !== false && m.key)
|
|
33
|
+
.map((m) => ({
|
|
34
|
+
key: m.key,
|
|
35
|
+
name: m.display_name || m.key,
|
|
36
|
+
isReasoning: !!m.is_reasoning,
|
|
37
|
+
isVL: !!m.is_vl,
|
|
38
|
+
maxInputTokens: m.max_input_tokens || 0,
|
|
39
|
+
maxOutputTokens: m.max_output_tokens || 0,
|
|
40
|
+
priceFactor: typeof m.price_factor === "number" ? m.price_factor : 0,
|
|
41
|
+
description: typeof m.description === "string" ? m.description : "",
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function userInfo(accessToken, fetchImpl = compatFetch) {
|
|
46
|
+
const res = await fetchImpl(USERINFO_URL, {
|
|
47
|
+
headers: { ...plainHeaders(), authorization: `Bearer ${accessToken}` },
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok) throw new Error(`userinfo http ${res.status}`);
|
|
50
|
+
return res.json();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 额度/套餐:data.{user,plan,quota};quota.remaining 即积分余量。 */
|
|
54
|
+
export async function accountContext(accessToken, fetchImpl = compatFetch) {
|
|
55
|
+
const res = await fetchImpl(ACCOUNT_CONTEXT_URL, {
|
|
56
|
+
headers: { ...plainHeaders(), authorization: `Bearer ${accessToken}` },
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok) throw new Error(`account-context http ${res.status}`);
|
|
59
|
+
const body = await res.json();
|
|
60
|
+
if (!body || (body.code && body.code !== "ok")) throw new Error(`account-context code=${body && body.code}`);
|
|
61
|
+
return (body && body.data) || {};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function refreshDeviceToken(refreshToken, fetchImpl = compatFetch) {
|
|
65
|
+
const res = await fetchImpl(REFRESH_URL, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: plainHeaders(),
|
|
68
|
+
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
69
|
+
});
|
|
70
|
+
if (!res.ok) throw new Error(`refresh http ${res.status}`);
|
|
71
|
+
const data = await res.json();
|
|
72
|
+
const access = data && (data.token || data.device_token);
|
|
73
|
+
if (!access || !data.refresh_token) throw new Error("refresh: incomplete token pair");
|
|
74
|
+
return data;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function pollGrant(nonce, verifier, fetchImpl = compatFetch) {
|
|
78
|
+
const q = new URLSearchParams({ nonce, verifier, challenge_method: "S256" });
|
|
79
|
+
const res = await fetchImpl(`${POLL_URL}?${q.toString()}`, {
|
|
80
|
+
headers: { accept: "application/json", "user-agent": UA },
|
|
81
|
+
});
|
|
82
|
+
if (res.status === 404 || res.status === 202) return { pending: true };
|
|
83
|
+
if (!res.ok) throw new Error(`poll http ${res.status}`);
|
|
84
|
+
const data = await res.json();
|
|
85
|
+
if (!data || !(data.token || data.device_token)) return { pending: true };
|
|
86
|
+
return { pending: false, grant: data };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function newDeviceFlow() {
|
|
90
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
91
|
+
const raw = new Uint8Array(64);
|
|
92
|
+
crypto.getRandomValues(raw);
|
|
93
|
+
let verifier = "";
|
|
94
|
+
for (let i = 0; i < raw.length; i++) verifier += alphabet[raw[i] % alphabet.length];
|
|
95
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(verifier)));
|
|
96
|
+
const challenge = b64url(digest);
|
|
97
|
+
const nonce = uuid();
|
|
98
|
+
const machineID = uuid();
|
|
99
|
+
const q = new URLSearchParams({
|
|
100
|
+
challenge,
|
|
101
|
+
challenge_method: "S256",
|
|
102
|
+
nonce,
|
|
103
|
+
machine_id: machineID,
|
|
104
|
+
client_id: CLIENT_ID,
|
|
105
|
+
redirect_uri: REDIRECT_URI,
|
|
106
|
+
});
|
|
107
|
+
return { verifier, nonce, url: `${DEVICE_AUTH_URL}?${q.toString()}` };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function expiryUnix(grant) {
|
|
111
|
+
if (grant && grant.expires_at) {
|
|
112
|
+
const t = Date.parse(grant.expires_at);
|
|
113
|
+
if (!Number.isNaN(t)) return Math.floor(t / 1000);
|
|
114
|
+
}
|
|
115
|
+
if (grant && grant.expires_in > 0) {
|
|
116
|
+
const ms = grant.expires_in > 10000000 ? grant.expires_in : grant.expires_in * 1000;
|
|
117
|
+
return Math.floor((Date.now() + ms) / 1000);
|
|
118
|
+
}
|
|
119
|
+
return Math.floor((Date.now() + 30 * 24 * 3600 * 1000) / 1000);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// globalqwenwork 变体(国际站 gateway.qwenwork.ai):协议与 cn 逐字同构,2026-10-01 现网实测签名/请求 200 — 见 docs/adr/0041-globalqwenwork-international-provider.md
|
package/src/providers/keyring.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
// 多 key 轮转器:round-robin 取键 + 出错冷却隔离。
|
|
2
|
-
// 语义:某个 key 出错后 cooldownMs 内不再使用;全部 key 都在冷却 → next() 返回 null
|
|
3
|
-
// (调用方应视为该供应商暂时失效,直接报错而非降级为单 key)。
|
|
4
|
-
export const DEFAULT_COOLDOWN_MS = 30_000;
|
|
5
|
-
|
|
6
|
-
export function createKeyRing(keys = [], { cooldownMs = DEFAULT_COOLDOWN_MS, now = Date.now } = {}) {
|
|
7
|
-
const list = [...new Set(keys.filter((k) => typeof k === "string" && k.trim().length))];
|
|
8
|
-
const errAt = new Map();
|
|
9
|
-
let cursor = 0;
|
|
10
|
-
|
|
11
|
-
// errAt 存「冷却到期时间戳」:onError(key, durationMs?) 可自定义时长(额度错长冷却用),
|
|
12
|
-
// 缺省用构造时的 cooldownMs —— 旧的 onError(key) 调用方行为不变。
|
|
13
|
-
function isCooling(key) {
|
|
14
|
-
const e = errAt.get(key);
|
|
15
|
-
return e != null && now() < e;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function next() {
|
|
19
|
-
if (!list.length) return null;
|
|
20
|
-
for (let i = 0; i < list.length; i++) {
|
|
21
|
-
const idx = (cursor + i) % list.length;
|
|
22
|
-
const key = list[idx];
|
|
23
|
-
if (!isCooling(key)) {
|
|
24
|
-
cursor = (idx + 1) % list.length;
|
|
25
|
-
return key;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function onError(key, durationMs) {
|
|
32
|
-
if (!list.includes(key)) return;
|
|
33
|
-
const dur = Number.isFinite(durationMs) && durationMs >= 0 ? durationMs : cooldownMs;
|
|
34
|
-
errAt.set(key, now() + dur);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function replace(oldKey, newKey) {
|
|
38
|
-
const idx = list.indexOf(oldKey);
|
|
39
|
-
if (idx < 0) {
|
|
40
|
-
if (newKey && !list.includes(newKey)) list.push(newKey);
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
if (newKey && oldKey !== newKey) {
|
|
44
|
-
list[idx] = newKey;
|
|
45
|
-
// 迁移冷却状态:旧 key 的冷却移到新 key,避免新 token 立即被误判可用
|
|
46
|
-
if (errAt.has(oldKey)) {
|
|
47
|
-
const t = errAt.get(oldKey);
|
|
48
|
-
errAt.delete(oldKey);
|
|
49
|
-
// 新 token 刚刷新,不应继承旧冷却,直接清掉
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function available() {
|
|
56
|
-
return list.filter((k) => !isCooling(k)).length;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// isCooling 对外暴露:粘号选择器要判断"上次这个号还在冷却吗"(决定是否必须换号)
|
|
60
|
-
return { next, onError, replace, available, isCooling, cooldownMs, get size() { return list.length; }, get keys() { return [...list]; } }; // ⚠ size/keys 必须是 getter:replace() 换号后构造期快照会停在已废弃的旧 token
|
|
1
|
+
// 多 key 轮转器:round-robin 取键 + 出错冷却隔离。
|
|
2
|
+
// 语义:某个 key 出错后 cooldownMs 内不再使用;全部 key 都在冷却 → next() 返回 null
|
|
3
|
+
// (调用方应视为该供应商暂时失效,直接报错而非降级为单 key)。
|
|
4
|
+
export const DEFAULT_COOLDOWN_MS = 30_000;
|
|
5
|
+
|
|
6
|
+
export function createKeyRing(keys = [], { cooldownMs = DEFAULT_COOLDOWN_MS, now = Date.now } = {}) {
|
|
7
|
+
const list = [...new Set(keys.filter((k) => typeof k === "string" && k.trim().length))];
|
|
8
|
+
const errAt = new Map();
|
|
9
|
+
let cursor = 0;
|
|
10
|
+
|
|
11
|
+
// errAt 存「冷却到期时间戳」:onError(key, durationMs?) 可自定义时长(额度错长冷却用),
|
|
12
|
+
// 缺省用构造时的 cooldownMs —— 旧的 onError(key) 调用方行为不变。
|
|
13
|
+
function isCooling(key) {
|
|
14
|
+
const e = errAt.get(key);
|
|
15
|
+
return e != null && now() < e;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function next() {
|
|
19
|
+
if (!list.length) return null;
|
|
20
|
+
for (let i = 0; i < list.length; i++) {
|
|
21
|
+
const idx = (cursor + i) % list.length;
|
|
22
|
+
const key = list[idx];
|
|
23
|
+
if (!isCooling(key)) {
|
|
24
|
+
cursor = (idx + 1) % list.length;
|
|
25
|
+
return key;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function onError(key, durationMs) {
|
|
32
|
+
if (!list.includes(key)) return;
|
|
33
|
+
const dur = Number.isFinite(durationMs) && durationMs >= 0 ? durationMs : cooldownMs;
|
|
34
|
+
errAt.set(key, now() + dur);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function replace(oldKey, newKey) {
|
|
38
|
+
const idx = list.indexOf(oldKey);
|
|
39
|
+
if (idx < 0) {
|
|
40
|
+
if (newKey && !list.includes(newKey)) list.push(newKey);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (newKey && oldKey !== newKey) {
|
|
44
|
+
list[idx] = newKey;
|
|
45
|
+
// 迁移冷却状态:旧 key 的冷却移到新 key,避免新 token 立即被误判可用
|
|
46
|
+
if (errAt.has(oldKey)) {
|
|
47
|
+
const t = errAt.get(oldKey);
|
|
48
|
+
errAt.delete(oldKey);
|
|
49
|
+
// 新 token 刚刷新,不应继承旧冷却,直接清掉
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function available() {
|
|
56
|
+
return list.filter((k) => !isCooling(k)).length;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// isCooling 对外暴露:粘号选择器要判断"上次这个号还在冷却吗"(决定是否必须换号)
|
|
60
|
+
return { next, onError, replace, available, isCooling, cooldownMs, get size() { return list.length; }, get keys() { return [...list]; } }; // ⚠ size/keys 必须是 getter:replace() 换号后构造期快照会停在已废弃的旧 token
|
|
61
61
|
}
|
|
@@ -96,6 +96,24 @@ export async function saveQoderAccount({ uid, deviceToken, refreshToken = "", re
|
|
|
96
96
|
return { file: fp, accounts: st.keys.length, updated: st.updated > 0 };
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
// 刷新成功后按 uid 原位更新令牌对(deviceToken 时效短,105 过期即刷)。
|
|
100
|
+
// auth 文档保留原 region/name;state 行按 uid 覆盖(persistAccounts 命中既有 uid 行即替换)。
|
|
101
|
+
export async function updateQoderAccountTokens({ uid, deviceToken, refreshToken, file } = {}) {
|
|
102
|
+
if (!uid || !deviceToken) throw new Error("updateQoderAccountTokens: 缺少 uid/deviceToken");
|
|
103
|
+
const docs = listAccountDocs();
|
|
104
|
+
const found = docs.find((d) => String(d.uid) === String(uid));
|
|
105
|
+
const a = found?.doc?.auth || {};
|
|
106
|
+
const acc = found?.doc?.account || {};
|
|
107
|
+
writeAccountFile({
|
|
108
|
+
uid, deviceToken, refreshToken,
|
|
109
|
+
region: a.region || "global",
|
|
110
|
+
name: acc.name || "", email: acc.email || "", userType: acc.userType || "personal_standard",
|
|
111
|
+
organizationId: acc.organizationId || "", organizationName: acc.organizationName || "",
|
|
112
|
+
});
|
|
113
|
+
await persistAccounts([{ uid, deviceToken, refreshToken, region: a.region || "global", name: acc.name || "", domain: "qoder.com" }], file);
|
|
114
|
+
return { updated: Boolean(found) };
|
|
115
|
+
}
|
|
116
|
+
|
|
99
117
|
// 重建 provider 的行级 auth:由 keys[i] blob + auths[i] 还原 deviceToken。
|
|
100
118
|
export function accountFromBlob(blob) {
|
|
101
119
|
try {
|