dsh-sensenova-pool 0.2.0 → 0.2.1
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/README.md +37 -3
- package/lib/sanitize.d.ts +46 -0
- package/lib/sanitize.js +210 -0
- package/lib/shim.d.ts +8 -0
- package/lib/shim.js +17 -1
- package/package.json +79 -79
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
- 池子真的耗尽时,回报 `503 + Retry-After`(而不是 429),交由 DSH 自行退避重试
|
|
46
46
|
- 429 计数与冷却状态在卡片上实时可见
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
截图里的 `2/3 成功,1 次限流` 是真实发生的记录:这把 Key 触发过 1 次 429,被池子内部消化掉,上层没有收到过任何 429。
|
|
49
49
|
|
|
50
50
|
### 🧠 一次接入全部模型
|
|
51
51
|
|
|
@@ -75,7 +75,7 @@ Key 存在 DSH 的凭据库里,不写进任何配置文件。填入即生效
|
|
|
75
75
|
|
|
76
76
|
### 🔌 提供对外的 OpenAI 兼容接口
|
|
77
77
|
|
|
78
|
-
插件在 127.0.0.1 上起一个 OpenAI
|
|
78
|
+
插件在 127.0.0.1 上起一个 OpenAI 兼容服务。设置页显示接口地址和 API Key,复制后填进其他 Agent 或客户端即可使用;请求共用同一个 Key 池,遇到限流同样自动换 Key。
|
|
79
79
|
|
|
80
80
|
```bash
|
|
81
81
|
curl http://127.0.0.1:51234/v1/chat/completions \
|
|
@@ -91,6 +91,8 @@ curl http://127.0.0.1:51234/v1/chat/completions \
|
|
|
91
91
|
|
|
92
92
|
设置页列出目录里的全部模型。取消勾选的模型不再注册给 provider,也就不会出现在模型选择器里,这个选择重启后保留。套餐里不含某些模型时,可以在这里把它们关掉。
|
|
93
93
|
|
|
94
|
+

|
|
95
|
+
|
|
94
96
|
---
|
|
95
97
|
|
|
96
98
|
## 安装
|
|
@@ -139,7 +141,7 @@ pnpm --dir "$DSH_PROFILE_DIR" add "link:$PWD"
|
|
|
139
141
|
|
|
140
142
|
改完 `package.json` 后**重启 DeepSeek Harness**。确认安装成功:插件页的「已安装」列表里会出现本插件。
|
|
141
143
|
|
|
142
|
-

|
|
143
145
|
|
|
144
146
|
---
|
|
145
147
|
|
|
@@ -262,6 +264,38 @@ records:
|
|
|
262
264
|
如果你还在旧版本上遇到,升级即可,不需要重新填 Key(Key 一直好好地在
|
|
263
265
|
`$DSH_HOME/.credentials.yaml` 里)。临时办法是在插件卡片上点一次「刷新/重载」。
|
|
264
266
|
|
|
267
|
+
**报 `400 inference request is invalid` / `400 invalid arguments`?**
|
|
268
|
+
|
|
269
|
+
这是 **商汤网关的 schema 校验**比 OpenAI 官方严导致的,不是 Key 无效,也不是限流——
|
|
270
|
+
换 Key 完全没用。Agent(DSH 的 pi-ai 层)会按 OpenAI 规范发出一些「官方接受、商汤不认」
|
|
271
|
+
的写法:
|
|
272
|
+
|
|
273
|
+
| 请求体写法 | 商汤 `deepseek-v4-flash` | 说明 |
|
|
274
|
+
|---|---|---|
|
|
275
|
+
| `{"role":"developer","content":"…"}` | ❌ 400 | OpenAI 新规范里替代 `system` 的角色,商汤只认 `system` |
|
|
276
|
+
| `{"role":"assistant","content":null}`(无 `tool_calls`) | ❌ 400 | 表示「这一轮只有工具调用」的常规写法 |
|
|
277
|
+
| `{"role":"system","content":null}` / `{"role":"tool","content":null}` | ❌ 400 | 工具结果为空时很常见 |
|
|
278
|
+
| `tool_calls[*].function.arguments` 传**对象** | ❌ 400 `invalid arguments` | 必须是 JSON 字符串 |
|
|
279
|
+
| `content` 传数字(`123`) | ❌ 400 `invalid arguments` | 必须是字符串或数组 |
|
|
280
|
+
| `temperature` 等传字符串(`"0.7"`) | ❌ 400 `invalid arguments` | 必须是数字 |
|
|
281
|
+
| `logprobs` / `top_logprobs` / `metadata` | ❌ 400 | 商汤不支持这些字段 |
|
|
282
|
+
| `reasoning_effort` 传 `off` | ❌ 400 `field ReasoningEffort invalid` | 合法值只有 `none`/`minimal`/`low`/`medium`/`high`/`xhigh`/`max`(`off` 要翻成 `none`) |
|
|
283
|
+
|
|
284
|
+
**0.2.1 已修复**:shim 在转发前会把这些写法**语义等价**地规范成商汤接受的形式
|
|
285
|
+
(`developer`→`system`、`null`/缺失的 content→`""`、`arguments` 对象→JSON 字符串、
|
|
286
|
+
数值字符串→数字、`off`→`none`、摘掉不支持字段),
|
|
287
|
+
**你不需要改任何配置**。带 `tool_calls` 的 `content: null` 会**保持原样**(商汤接受,动它反而有风险)。
|
|
288
|
+
|
|
289
|
+
> 特别说明:`max` 与 `minimal` **本来就是合法档位**(上游报错原文列全了它们),
|
|
290
|
+
> 所以不会被改动或降级 —— 否则会悄悄改掉你要求的推理强度。
|
|
291
|
+
|
|
292
|
+
刻意不代改的三类(改了只会把错误往后推):`user.content` 为空、数值**越界**
|
|
293
|
+
(如 `temperature: 3`)、未知 role。
|
|
294
|
+
|
|
295
|
+
> 💡 如果升级后仍报 400,说明是清洗层还没覆盖的新写法。此时把
|
|
296
|
+
> `debugRequestOn400` 打开,插件的 400 日志里会**直接打出实际发出的请求体**,
|
|
297
|
+
> 一眼就能定位是哪个字段——不用再猜。相关讨论见 [issue #2](https://github.com/Phoeky/dsh-sensenova-pool/issues/2)。
|
|
298
|
+
|
|
265
299
|
**Key 会泄漏吗?**
|
|
266
300
|
|
|
267
301
|
不会。界面只显示掩码;明文只存在 DSH 凭据库(`$DSH_HOME/.credentials.yaml`)中,不会写入任何配置文件,也不会出现在日志里。
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求体清洗:把「Agent 合法、商汤网关不认」的写法规范成语义等价的安全写法。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要这一层
|
|
5
|
+
* ----------------
|
|
6
|
+
* shim 的职责是**透明转发**,但商汤的 schema 校验比 OpenAI 官方严得多,
|
|
7
|
+
* 一批「OpenAI/DeepSeek 直连都接受」的写法会被它判成 400。而 pi-ai 发请求时
|
|
8
|
+
* 完全按 OpenAI 规范构造,撞上这些点就会让用户看到:
|
|
9
|
+
*
|
|
10
|
+
* 400 {"message":"inference request is invalid",
|
|
11
|
+
* "type":"invalid_request_error","code":"invalid_parameter_error"}
|
|
12
|
+
* 400 {"message":"invalid arguments", … ,"code":"3"}
|
|
13
|
+
*
|
|
14
|
+
* 实测(2026-10-30,deepseek-v4-flash,逐例对真实上游验证)下列写法全部 400:
|
|
15
|
+
*
|
|
16
|
+
* {"role":"developer","content":"…"} ← OpenAI 新规范,商汤只认 system
|
|
17
|
+
* {"role":"assistant","content":null} ← 无 tool_calls 时
|
|
18
|
+
* {"role":"assistant"} ← 连字段都没有
|
|
19
|
+
* {"role":"system","content":null}
|
|
20
|
+
* {"role":"tool","content":null} ← 工具结果为空
|
|
21
|
+
* {"role":"tool"}
|
|
22
|
+
* "arguments": {"a":1} ← 必须是 JSON **字符串**
|
|
23
|
+
* "content": 123 ← 必须是字符串或数组
|
|
24
|
+
* "temperature": "0.7" ← 必须是数字
|
|
25
|
+
* "logprobs" / "top_logprobs" / "metadata"
|
|
26
|
+
* "reasoning_effort": "off" / "max" / "minimal"
|
|
27
|
+
*
|
|
28
|
+
* 规范成等价安全写法后实测全部 200。
|
|
29
|
+
*
|
|
30
|
+
* 刻意**不代改**的三类(改了是把错误往后推,甚至篡改用户意图):
|
|
31
|
+
* * `user.content` 为 null/"":确实是空用户轮,上游报 `user content required`;
|
|
32
|
+
* * 数值越界(temperature=3、frequency_penalty=5):上游报错信息已足够明确;
|
|
33
|
+
* * 未知 role(除 developer 外):猜错会掩盖真正的调用错误。
|
|
34
|
+
*/
|
|
35
|
+
export interface SanitizeResult<T> {
|
|
36
|
+
body: T;
|
|
37
|
+
/** 实际做过的改动,供日志/UI 展示(空数组表示原样放行)。 */
|
|
38
|
+
changes: string[];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 清洗一份 OpenAI chat 请求体。
|
|
42
|
+
*
|
|
43
|
+
* 只做**语义等价**的改写,不改变请求意图;返回改动清单便于排查。
|
|
44
|
+
* 非对象输入原样返回(交给上游如实报错)。
|
|
45
|
+
*/
|
|
46
|
+
export declare function sanitizeChatRequest<T>(input: T): SanitizeResult<T>;
|
package/lib/sanitize.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求体清洗:把「Agent 合法、商汤网关不认」的写法规范成语义等价的安全写法。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要这一层
|
|
5
|
+
* ----------------
|
|
6
|
+
* shim 的职责是**透明转发**,但商汤的 schema 校验比 OpenAI 官方严得多,
|
|
7
|
+
* 一批「OpenAI/DeepSeek 直连都接受」的写法会被它判成 400。而 pi-ai 发请求时
|
|
8
|
+
* 完全按 OpenAI 规范构造,撞上这些点就会让用户看到:
|
|
9
|
+
*
|
|
10
|
+
* 400 {"message":"inference request is invalid",
|
|
11
|
+
* "type":"invalid_request_error","code":"invalid_parameter_error"}
|
|
12
|
+
* 400 {"message":"invalid arguments", … ,"code":"3"}
|
|
13
|
+
*
|
|
14
|
+
* 实测(2026-10-30,deepseek-v4-flash,逐例对真实上游验证)下列写法全部 400:
|
|
15
|
+
*
|
|
16
|
+
* {"role":"developer","content":"…"} ← OpenAI 新规范,商汤只认 system
|
|
17
|
+
* {"role":"assistant","content":null} ← 无 tool_calls 时
|
|
18
|
+
* {"role":"assistant"} ← 连字段都没有
|
|
19
|
+
* {"role":"system","content":null}
|
|
20
|
+
* {"role":"tool","content":null} ← 工具结果为空
|
|
21
|
+
* {"role":"tool"}
|
|
22
|
+
* "arguments": {"a":1} ← 必须是 JSON **字符串**
|
|
23
|
+
* "content": 123 ← 必须是字符串或数组
|
|
24
|
+
* "temperature": "0.7" ← 必须是数字
|
|
25
|
+
* "logprobs" / "top_logprobs" / "metadata"
|
|
26
|
+
* "reasoning_effort": "off" / "max" / "minimal"
|
|
27
|
+
*
|
|
28
|
+
* 规范成等价安全写法后实测全部 200。
|
|
29
|
+
*
|
|
30
|
+
* 刻意**不代改**的三类(改了是把错误往后推,甚至篡改用户意图):
|
|
31
|
+
* * `user.content` 为 null/"":确实是空用户轮,上游报 `user content required`;
|
|
32
|
+
* * 数值越界(temperature=3、frequency_penalty=5):上游报错信息已足够明确;
|
|
33
|
+
* * 未知 role(除 developer 外):猜错会掩盖真正的调用错误。
|
|
34
|
+
*/
|
|
35
|
+
/** 商汤不认识的角色名 → 映射成等价角色。`developer` 是 OpenAI 新规范里替代 system 的角色。 */
|
|
36
|
+
const ROLE_ALIASES = { developer: 'system' };
|
|
37
|
+
/** 允许 `content` 缺失/null 的角色(带 tool_calls 的 assistant 例外,见下)。 */
|
|
38
|
+
const NULLABLE_CONTENT_ROLES = new Set(['assistant', 'system', 'tool']);
|
|
39
|
+
/** 商汤拒收的顶层字段。 */
|
|
40
|
+
const UNSUPPORTED_FIELDS = ['logprobs', 'top_logprobs', 'metadata'];
|
|
41
|
+
/** 必须是数字的采样参数(pi-ai 偶尔会传字符串)。 */
|
|
42
|
+
const NUMERIC_PARAMS = ['temperature', 'top_p', 'frequency_penalty', 'presence_penalty'];
|
|
43
|
+
/**
|
|
44
|
+
* `reasoning_effort` 的合法取值。
|
|
45
|
+
*
|
|
46
|
+
* 实测(2026-10-30,deepseek-v4-flash)上游报错原文写得很明确:
|
|
47
|
+
* `field ReasoningEffort invalid, should be one of:
|
|
48
|
+
* none, minimal, low, medium, high, xhigh, max`
|
|
49
|
+
*
|
|
50
|
+
* 注意 **`minimal` 与 `max` 都是合法的** —— 不能像有些资料说的那样把它们
|
|
51
|
+
* 当成非法值映射掉,那会悄悄改掉用户要求的推理等级(`max` 尤其明显)。
|
|
52
|
+
* 唯一需要翻译的是 DSH 侧的 `off`,商汤拼作 `none`。
|
|
53
|
+
*/
|
|
54
|
+
const REASONING_EFFORT_ALIASES = { off: 'none' };
|
|
55
|
+
const REASONING_EFFORT_ALLOWED = new Set([
|
|
56
|
+
'none',
|
|
57
|
+
'minimal',
|
|
58
|
+
'low',
|
|
59
|
+
'medium',
|
|
60
|
+
'high',
|
|
61
|
+
'xhigh',
|
|
62
|
+
'max',
|
|
63
|
+
]);
|
|
64
|
+
function isRecord(value) {
|
|
65
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
66
|
+
}
|
|
67
|
+
/** 把 content 规范成商汤能接受的形态;没改动返回 `undefined`。 */
|
|
68
|
+
function normalizeContent(message) {
|
|
69
|
+
const role = message.role;
|
|
70
|
+
const hasContent = 'content' in message;
|
|
71
|
+
const content = message.content;
|
|
72
|
+
if (!hasContent || content === null || content === undefined) {
|
|
73
|
+
// assistant 带 tool_calls 时商汤接受 content:null,保持原样(动它反而有风险)
|
|
74
|
+
if (role === 'assistant' && Array.isArray(message.tool_calls) && message.tool_calls.length > 0) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
if (typeof role === 'string' && NULLABLE_CONTENT_ROLES.has(role)) {
|
|
78
|
+
return { ...message, content: '' };
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (typeof content === 'string' || Array.isArray(content))
|
|
83
|
+
return undefined;
|
|
84
|
+
if (typeof content === 'number' || typeof content === 'boolean') {
|
|
85
|
+
return { ...message, content: String(content) };
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/** 角色名映射;没改动返回 `undefined`。 */
|
|
90
|
+
function normalizeRole(message) {
|
|
91
|
+
const role = message.role;
|
|
92
|
+
if (typeof role !== 'string')
|
|
93
|
+
return undefined;
|
|
94
|
+
const mapped = ROLE_ALIASES[role.trim().toLowerCase()];
|
|
95
|
+
if (mapped === undefined || mapped === role)
|
|
96
|
+
return undefined;
|
|
97
|
+
return { ...message, role: mapped };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `tool_calls[*].function.arguments` 必须是 JSON 字符串:
|
|
101
|
+
* 传了对象/数字就序列化过去(OpenAI 规范本身也要求字符串)。
|
|
102
|
+
*/
|
|
103
|
+
function normalizeToolCalls(message) {
|
|
104
|
+
const calls = message.tool_calls;
|
|
105
|
+
if (!Array.isArray(calls) || calls.length === 0)
|
|
106
|
+
return undefined;
|
|
107
|
+
let changed = false;
|
|
108
|
+
const next = calls.map((call) => {
|
|
109
|
+
if (!isRecord(call))
|
|
110
|
+
return call;
|
|
111
|
+
const func = call.function;
|
|
112
|
+
if (!isRecord(func))
|
|
113
|
+
return call;
|
|
114
|
+
const args = func.arguments;
|
|
115
|
+
if (typeof args === 'string')
|
|
116
|
+
return call;
|
|
117
|
+
changed = true;
|
|
118
|
+
return {
|
|
119
|
+
...call,
|
|
120
|
+
function: { ...func, arguments: JSON.stringify(args ?? {}) },
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
return changed ? { ...message, tool_calls: next } : undefined;
|
|
124
|
+
}
|
|
125
|
+
/** 清洗 messages 数组,返回改动说明。 */
|
|
126
|
+
function sanitizeMessages(messages, changes) {
|
|
127
|
+
if (!Array.isArray(messages))
|
|
128
|
+
return messages;
|
|
129
|
+
return messages.map((message, index) => {
|
|
130
|
+
if (!isRecord(message))
|
|
131
|
+
return message;
|
|
132
|
+
let current = message;
|
|
133
|
+
const steps = [
|
|
134
|
+
['role', normalizeRole],
|
|
135
|
+
['content', normalizeContent],
|
|
136
|
+
['arguments', normalizeToolCalls],
|
|
137
|
+
];
|
|
138
|
+
for (const [label, step] of steps) {
|
|
139
|
+
const result = step(current);
|
|
140
|
+
if (result !== undefined) {
|
|
141
|
+
current = result;
|
|
142
|
+
changes.push(`messages[${index}].${label}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return current;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** 归一 `reasoning_effort`;无法识别时直接摘掉该字段(比猜一个值更安全)。 */
|
|
149
|
+
function sanitizeReasoningEffort(body, changes) {
|
|
150
|
+
if (!('reasoning_effort' in body))
|
|
151
|
+
return;
|
|
152
|
+
const raw = body.reasoning_effort;
|
|
153
|
+
if (typeof raw !== 'string') {
|
|
154
|
+
delete body.reasoning_effort;
|
|
155
|
+
changes.push('reasoning_effort');
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const lowered = raw.trim().toLowerCase();
|
|
159
|
+
if (REASONING_EFFORT_ALLOWED.has(lowered)) {
|
|
160
|
+
if (lowered !== raw) {
|
|
161
|
+
body.reasoning_effort = lowered;
|
|
162
|
+
changes.push('reasoning_effort');
|
|
163
|
+
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const mapped = REASONING_EFFORT_ALIASES[lowered];
|
|
167
|
+
if (mapped !== undefined) {
|
|
168
|
+
body.reasoning_effort = mapped;
|
|
169
|
+
changes.push('reasoning_effort');
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
// 未知取值:删掉比瞎映射更安全(关掉这一轮思考,而不是发出非法值撞 400)。
|
|
173
|
+
delete body.reasoning_effort;
|
|
174
|
+
changes.push('reasoning_effort');
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 清洗一份 OpenAI chat 请求体。
|
|
178
|
+
*
|
|
179
|
+
* 只做**语义等价**的改写,不改变请求意图;返回改动清单便于排查。
|
|
180
|
+
* 非对象输入原样返回(交给上游如实报错)。
|
|
181
|
+
*/
|
|
182
|
+
export function sanitizeChatRequest(input) {
|
|
183
|
+
const changes = [];
|
|
184
|
+
if (!isRecord(input))
|
|
185
|
+
return { body: input, changes };
|
|
186
|
+
const body = { ...input };
|
|
187
|
+
for (const field of UNSUPPORTED_FIELDS) {
|
|
188
|
+
if (field in body) {
|
|
189
|
+
delete body[field];
|
|
190
|
+
changes.push(field);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const field of NUMERIC_PARAMS) {
|
|
194
|
+
const value = body[field];
|
|
195
|
+
if (typeof value === 'string') {
|
|
196
|
+
const parsed = Number(value);
|
|
197
|
+
if (Number.isFinite(parsed)) {
|
|
198
|
+
body[field] = parsed;
|
|
199
|
+
changes.push(field);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
sanitizeReasoningEffort(body, changes);
|
|
204
|
+
if ('messages' in body) {
|
|
205
|
+
const next = sanitizeMessages(body.messages, changes);
|
|
206
|
+
if (next !== body.messages)
|
|
207
|
+
body.messages = next;
|
|
208
|
+
}
|
|
209
|
+
return { body: changes.length > 0 ? body : input, changes };
|
|
210
|
+
}
|
package/lib/shim.d.ts
CHANGED
|
@@ -52,6 +52,14 @@ export interface ShimOptions {
|
|
|
52
52
|
};
|
|
53
53
|
/** 每次因 429/网络原因切换 key 时回调,供 UI 展示。 */
|
|
54
54
|
onAttemptFailed?: (log: AttemptLog) => void;
|
|
55
|
+
/**
|
|
56
|
+
* 上游返回 400 时,把**实际发出去的请求体**记进日志。
|
|
57
|
+
*
|
|
58
|
+
* 商汤的 400 只回一句 `inference request is invalid`,不告诉你是哪个字段、
|
|
59
|
+
* 也不回显请求体;历史上排查这类问题只能靠猜字段,效率极低。记录原始请求体后
|
|
60
|
+
* 一眼就能定位。默认关闭(体积大且可能含用户内容),由 e2e / 排障时显式打开。
|
|
61
|
+
*/
|
|
62
|
+
debugRequestOn400?: boolean;
|
|
55
63
|
}
|
|
56
64
|
export interface Shim {
|
|
57
65
|
ready: Promise<void>;
|
package/lib/shim.js
CHANGED
|
@@ -21,6 +21,7 @@ import { createServer } from 'node:http';
|
|
|
21
21
|
import { Readable } from 'node:stream';
|
|
22
22
|
import { BUILTIN_MODELS, DEFAULT_BASE_URL, parseModelCatalog } from './catalog.js';
|
|
23
23
|
import { DEFAULT_ROTATION, requestWithRotation } from './rotate.js';
|
|
24
|
+
import { sanitizeChatRequest } from './sanitize.js';
|
|
24
25
|
// --------------------------------------------------------------------- 目录
|
|
25
26
|
/**
|
|
26
27
|
* 模型目录缓存。
|
|
@@ -140,6 +141,15 @@ export function createShim(options) {
|
|
|
140
141
|
writeError(res, 400, 'invalid_request_error', 'request body must be JSON');
|
|
141
142
|
return;
|
|
142
143
|
}
|
|
144
|
+
// 关键:转发前把「Agent 合法、商汤不认」的写法规范成等价安全写法。
|
|
145
|
+
// pi-ai 完全按 OpenAI 规范构造请求,其中一批写法(developer 角色、
|
|
146
|
+
// content:null、arguments 传对象、reasoning_effort 的 off/max…)会在商汤
|
|
147
|
+
// 网关撞 400,而换 key 是解不开的 —— 必须在这里就地修好再发。
|
|
148
|
+
const { body: sanitized, changes } = sanitizeChatRequest(body);
|
|
149
|
+
const forwardBody = changes.length > 0 ? JSON.stringify(sanitized) : raw;
|
|
150
|
+
if (changes.length > 0) {
|
|
151
|
+
options.logger?.info(`sensenova-pool: 已规范化请求体字段 ${changes.join(', ')}`);
|
|
152
|
+
}
|
|
143
153
|
const wantsStream = body.stream === true;
|
|
144
154
|
const upstreamUrl = `${options.baseUrl.replace(/\/+$/, '')}/chat/completions`;
|
|
145
155
|
// 关键:轮换发生在「拿到首个响应」之前,因此对上层完全透明。
|
|
@@ -152,7 +162,7 @@ export function createShim(options) {
|
|
|
152
162
|
authorization: `Bearer ${apiKey}`,
|
|
153
163
|
accept: wantsStream ? 'text/event-stream' : 'application/json',
|
|
154
164
|
},
|
|
155
|
-
body:
|
|
165
|
+
body: forwardBody,
|
|
156
166
|
signal,
|
|
157
167
|
}), {
|
|
158
168
|
onAttemptFailed: (log) => {
|
|
@@ -169,6 +179,12 @@ export function createShim(options) {
|
|
|
169
179
|
res.end();
|
|
170
180
|
return;
|
|
171
181
|
}
|
|
182
|
+
// 400 是「请求体」问题,换 key 无解,也最难排查:商汤不回显请求体、
|
|
183
|
+
// 只回一句 inference request is invalid。排障时打开 debugRequestOn400,
|
|
184
|
+
// 直接把实际发出去的 body 落日志,省掉一轮轮猜字段。
|
|
185
|
+
if (options.debugRequestOn400 && result.error.status === 400) {
|
|
186
|
+
options.logger?.warn(`sensenova-pool: 上游 400(${result.error.message}),实际发出的请求体:${forwardBody}`);
|
|
187
|
+
}
|
|
172
188
|
writeError(res, outwardStatus(result.error), result.error.kind, summarizeFailure(result.error.message, result.attempts), result.error.kind === 'rate_limit' ? retryAfterSeconds(pool) : undefined);
|
|
173
189
|
return;
|
|
174
190
|
}
|
package/package.json
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-sensenova-pool",
|
|
3
|
-
"displayName": "DSH SenseNova Key Pool",
|
|
4
|
-
"description": "商汤日日新(SenseNova)多 Key 轮换池 — 填入多把 Key 即可零配置接入 DeepSeek Harness,遇 429 限流自动切换下一把 Key。",
|
|
5
|
-
"version": "0.2.
|
|
6
|
-
"author": "Phoeky",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"dsh-plugin",
|
|
9
|
-
"deepseek-harness",
|
|
10
|
-
"deepseek",
|
|
11
|
-
"sensenova",
|
|
12
|
-
"日日新",
|
|
13
|
-
"key-rotation",
|
|
14
|
-
"key-pool",
|
|
15
|
-
"rate-limit",
|
|
16
|
-
"429",
|
|
17
|
-
"model-provider"
|
|
18
|
-
],
|
|
19
|
-
"type": "module",
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": "^22.19.0 || >=24.0.0"
|
|
22
|
-
},
|
|
23
|
-
"main": "lib/index.js",
|
|
24
|
-
"types": "lib/index.d.ts",
|
|
25
|
-
"exports": {
|
|
26
|
-
".": {
|
|
27
|
-
"types": "./lib/index.d.ts",
|
|
28
|
-
"default": "./lib/index.js"
|
|
29
|
-
},
|
|
30
|
-
"./client": "./lib/client.js",
|
|
31
|
-
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
32
|
-
"./package.json": "./package.json"
|
|
33
|
-
},
|
|
34
|
-
"files": [
|
|
35
|
-
"lib",
|
|
36
|
-
"cordis.patch.yml",
|
|
37
|
-
"README.md",
|
|
38
|
-
"LICENSE"
|
|
39
|
-
],
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/Phoeky/dsh-sensenova-pool.git"
|
|
44
|
-
},
|
|
45
|
-
"homepage": "https://github.com/Phoeky/dsh-sensenova-pool#readme",
|
|
46
|
-
"bugs": "https://github.com/Phoeky/dsh-sensenova-pool/issues",
|
|
47
|
-
"scripts": {
|
|
48
|
-
"prepack": "npm run build",
|
|
49
|
-
"prepare": "npm run build",
|
|
50
|
-
"build": "tsc -p tsconfig.json && node tools/build-client.mjs",
|
|
51
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
52
|
-
"test": "node tools/test-all.mjs"
|
|
53
|
-
},
|
|
54
|
-
"dsh": {
|
|
55
|
-
"bundle": {
|
|
56
|
-
"patch": "./cordis.patch.yml"
|
|
57
|
-
},
|
|
58
|
-
"client": {
|
|
59
|
-
"inject": [
|
|
60
|
-
"@deepseek-ai/dsh-client-ui-plugin-manager",
|
|
61
|
-
"@deepseek-ai/dsh-client-ui-settings",
|
|
62
|
-
"@deepseek-ai/dsh-client-ui-settings-plugins",
|
|
63
|
-
"@deepseek-ai/dsh-client-locale"
|
|
64
|
-
],
|
|
65
|
-
"platform": "web"
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
"peerDependencies": {
|
|
69
|
-
"@deepseek-ai/cordis": "^4.0.4 || >=4.0.4-0 <5.0.0-0",
|
|
70
|
-
"@deepseek-ai/schemastery": "~3.18.4 || >=3.18.4-0 <3.19.0-0",
|
|
71
|
-
"@deepseek-ai/dsh-llm": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
72
|
-
"@deepseek-ai/dsh-llm-pi-ai": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
73
|
-
"@earendil-works/pi-ai": "^0.87.1 || >=0.87.1-0 <0.88.0-0"
|
|
74
|
-
},
|
|
75
|
-
"devDependencies": {
|
|
76
|
-
"@types/node": "22.19.19",
|
|
77
|
-
"typescript": "5.9.3"
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-sensenova-pool",
|
|
3
|
+
"displayName": "DSH SenseNova Key Pool",
|
|
4
|
+
"description": "商汤日日新(SenseNova)多 Key 轮换池 — 填入多把 Key 即可零配置接入 DeepSeek Harness,遇 429 限流自动切换下一把 Key。",
|
|
5
|
+
"version": "0.2.1",
|
|
6
|
+
"author": "Phoeky",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"deepseek",
|
|
11
|
+
"sensenova",
|
|
12
|
+
"日日新",
|
|
13
|
+
"key-rotation",
|
|
14
|
+
"key-pool",
|
|
15
|
+
"rate-limit",
|
|
16
|
+
"429",
|
|
17
|
+
"model-provider"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
22
|
+
},
|
|
23
|
+
"main": "lib/index.js",
|
|
24
|
+
"types": "lib/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./lib/index.d.ts",
|
|
28
|
+
"default": "./lib/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./client": "./lib/client.js",
|
|
31
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"lib",
|
|
36
|
+
"cordis.patch.yml",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/Phoeky/dsh-sensenova-pool.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/Phoeky/dsh-sensenova-pool#readme",
|
|
46
|
+
"bugs": "https://github.com/Phoeky/dsh-sensenova-pool/issues",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"prepack": "npm run build",
|
|
49
|
+
"prepare": "npm run build",
|
|
50
|
+
"build": "tsc -p tsconfig.json && node tools/build-client.mjs",
|
|
51
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
52
|
+
"test": "node tools/test-all.mjs"
|
|
53
|
+
},
|
|
54
|
+
"dsh": {
|
|
55
|
+
"bundle": {
|
|
56
|
+
"patch": "./cordis.patch.yml"
|
|
57
|
+
},
|
|
58
|
+
"client": {
|
|
59
|
+
"inject": [
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-plugin-manager",
|
|
61
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
62
|
+
"@deepseek-ai/dsh-client-ui-settings-plugins",
|
|
63
|
+
"@deepseek-ai/dsh-client-locale"
|
|
64
|
+
],
|
|
65
|
+
"platform": "web"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@deepseek-ai/cordis": "^4.0.4 || >=4.0.4-0 <5.0.0-0",
|
|
70
|
+
"@deepseek-ai/schemastery": "~3.18.4 || >=3.18.4-0 <3.19.0-0",
|
|
71
|
+
"@deepseek-ai/dsh-llm": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
72
|
+
"@deepseek-ai/dsh-llm-pi-ai": "0.2.0-rc.2 || >=0.2.0-rc.2 <0.3.0-0",
|
|
73
|
+
"@earendil-works/pi-ai": "^0.87.1 || >=0.87.1-0 <0.88.0-0"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/node": "22.19.19",
|
|
77
|
+
"typescript": "5.9.3"
|
|
78
|
+
}
|
|
79
|
+
}
|