dsh-sensenova-pool 0.2.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -4
- package/lib/catalog.d.ts +128 -0
- package/lib/catalog.js +229 -3
- package/lib/client.js +185 -22
- package/lib/index.js +90 -29
- package/lib/routes.d.ts +14 -1
- package/lib/routes.js +71 -0
- package/lib/sanitize.d.ts +51 -0
- package/lib/sanitize.js +218 -0
- package/lib/shim.d.ts +8 -0
- package/lib/shim.js +17 -1
- package/package.json +79 -79
package/lib/client.js
CHANGED
|
@@ -87,6 +87,46 @@ window.__ModuleLoader__.load({
|
|
|
87
87
|
fontWeight: 500,
|
|
88
88
|
};
|
|
89
89
|
const smallButtonStyle = { ...buttonStyle, minHeight: 26, padding: '2px 10px', fontSize: 12 };
|
|
90
|
+
/** 模型能力编辑面板:展开在模型条目下方,点「保存」才生效。 */
|
|
91
|
+
const limitEditorStyle = {
|
|
92
|
+
display: 'grid',
|
|
93
|
+
gap: 4,
|
|
94
|
+
width: '100%',
|
|
95
|
+
paddingTop: 10,
|
|
96
|
+
marginTop: 2,
|
|
97
|
+
borderTop: `1px solid ${borderL2}`,
|
|
98
|
+
};
|
|
99
|
+
/** 字段名:参考宿主模型配置页的写法,label 在上、输入框在下。 */
|
|
100
|
+
const fieldLabelStyle = { fontSize: 12, color: labelSecondary, lineHeight: '18px' };
|
|
101
|
+
const fieldHintStyle = { margin: '0 0 6px', fontSize: 12, color: labelTertiary, lineHeight: '16px' };
|
|
102
|
+
const limitInputStyle = {
|
|
103
|
+
boxSizing: 'border-box',
|
|
104
|
+
width: '100%',
|
|
105
|
+
padding: '6px 8px',
|
|
106
|
+
border: `1px solid ${borderL2}`,
|
|
107
|
+
borderRadius: 8,
|
|
108
|
+
background: bgLayer1,
|
|
109
|
+
color: labelPrimary,
|
|
110
|
+
font: 'inherit',
|
|
111
|
+
fontSize: 13,
|
|
112
|
+
lineHeight: '20px',
|
|
113
|
+
outline: 'none',
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** 把 token 数写成紧凑形式(1048576 → 1M、131072 → 128K),与宿主风格一致。 */
|
|
117
|
+
const formatTokens = (value) => {
|
|
118
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) return '—';
|
|
119
|
+
if (value >= 1048576 && value % 1048576 === 0) return `${value / 1048576}M`;
|
|
120
|
+
if (value >= 1024 && value % 1024 === 0) return `${value / 1024}K`;
|
|
121
|
+
return String(value);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** 提示用的默认值描述:`1M (1048576)`;紧凑写法已经等于原值时只写一个。 */
|
|
125
|
+
const describeTokens = (value) => {
|
|
126
|
+
const compact = formatTokens(value);
|
|
127
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return compact;
|
|
128
|
+
return compact === String(value) ? compact : `${compact} (${value})`;
|
|
129
|
+
};
|
|
90
130
|
const rowStyle = {
|
|
91
131
|
display: 'flex',
|
|
92
132
|
alignItems: 'center',
|
|
@@ -225,6 +265,10 @@ window.__ModuleLoader__.load({
|
|
|
225
265
|
const [adminKey, setAdminKey] = React.useState(undefined);
|
|
226
266
|
const [tested, setTested] = React.useState({});
|
|
227
267
|
const [copied, setCopied] = React.useState('');
|
|
268
|
+
// 正在编辑窗口/输出上限的模型 id 与草稿值(点「保存」才写回)。
|
|
269
|
+
const [limitEditing, setLimitEditing] = React.useState('');
|
|
270
|
+
const [limitWindow, setLimitWindow] = React.useState('');
|
|
271
|
+
const [limitOutput, setLimitOutput] = React.useState('');
|
|
228
272
|
|
|
229
273
|
const load = React.useCallback(async () => {
|
|
230
274
|
try {
|
|
@@ -305,6 +349,42 @@ window.__ModuleLoader__.load({
|
|
|
305
349
|
return enabled ? t('modelShown', { id }) : t('modelHidden', { id });
|
|
306
350
|
});
|
|
307
351
|
|
|
352
|
+
// 模型窗口/输出上限:点「设置」展开草稿,点「保存」才写回。
|
|
353
|
+
// 不做逐键提交 —— 数字输入的中间态(先删空再输入)不该打请求。
|
|
354
|
+
// 打开面板时用紧凑写法(1M / 128K),与宿主模型配置页一致;
|
|
355
|
+
// 用户也可以直接写精确数字,服务端两种都认。
|
|
356
|
+
const openLimits = (model) => {
|
|
357
|
+
setLimitEditing(model.id);
|
|
358
|
+
setLimitWindow(formatTokens(model.contextWindow));
|
|
359
|
+
setLimitOutput(formatTokens(model.maxTokens));
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const closeLimits = () => {
|
|
363
|
+
setLimitEditing('');
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const limitsPayload = (model, contextWindow, maxTokens) => ({
|
|
367
|
+
action: 'set-model-limits',
|
|
368
|
+
id: model.id,
|
|
369
|
+
contextWindow: String(contextWindow).trim(),
|
|
370
|
+
maxTokens: String(maxTokens).trim(),
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const submitLimits = (model) =>
|
|
374
|
+
run('limits', async () => {
|
|
375
|
+
await postJson(`${BASE}/keys`, limitsPayload(model, limitWindow, limitOutput), adminKey);
|
|
376
|
+
closeLimits();
|
|
377
|
+
return t('limitsSaved', { id: model.id });
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
const resetLimits = (model) =>
|
|
381
|
+
run('limits', async () => {
|
|
382
|
+
// 两个字段传空 = 恢复默认(服务端会归一成「不存覆盖」)。
|
|
383
|
+
await postJson(`${BASE}/keys`, limitsPayload(model, '', ''), adminKey);
|
|
384
|
+
closeLimits();
|
|
385
|
+
return t('limitsReset', { id: model.id });
|
|
386
|
+
});
|
|
387
|
+
|
|
308
388
|
const copy = async (field, text) => {
|
|
309
389
|
const ok = await copyText(text);
|
|
310
390
|
setCopied(ok ? field : '');
|
|
@@ -463,26 +543,88 @@ window.__ModuleLoader__.load({
|
|
|
463
543
|
models.map((model) =>
|
|
464
544
|
h(
|
|
465
545
|
'li',
|
|
466
|
-
{ key: model.id, style: { ...rowStyle, padding: '6px 10px' } },
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
546
|
+
{ key: model.id, style: { ...rowStyle, padding: '6px 10px', flexWrap: 'wrap', rowGap: 6 } },
|
|
547
|
+
[
|
|
548
|
+
h(
|
|
549
|
+
'div',
|
|
550
|
+
{ key: 'main', style: { display: 'flex', alignItems: 'center', gap: 10, width: '100%' } },
|
|
551
|
+
[
|
|
552
|
+
h(
|
|
553
|
+
'label',
|
|
554
|
+
{ key: 'label', style: { display: 'flex', alignItems: 'center', gap: 10, flex: '1 1 auto', minWidth: 0, cursor: 'pointer' } },
|
|
555
|
+
[
|
|
556
|
+
h('input', {
|
|
557
|
+
key: 'box',
|
|
558
|
+
type: 'checkbox',
|
|
559
|
+
checked: model.enabled === true,
|
|
560
|
+
disabled: busy,
|
|
561
|
+
onChange: (event) => toggleModel(model.id, event.target.checked),
|
|
562
|
+
}),
|
|
563
|
+
h(
|
|
564
|
+
'span',
|
|
565
|
+
{ key: 'name', style: { fontSize: 13, color: model.enabled ? labelPrimary : labelTertiary, flex: '0 0 auto' } },
|
|
566
|
+
model.name,
|
|
567
|
+
),
|
|
568
|
+
h('span', { key: 'id', style: { ...codeStyle, flex: '1 1 auto', wordBreak: 'break-all' } }, model.id),
|
|
569
|
+
],
|
|
570
|
+
),
|
|
571
|
+
model.overridden === true
|
|
572
|
+
? h('span', { key: 'badge', style: { ...hintStyle, color: labelSecondary, flex: '0 0 auto' } }, t('limitsCustom'))
|
|
573
|
+
: null,
|
|
574
|
+
// 窗口/最大输出不进主行:点「设置」展开面板,保存后才生效(issue #4)。
|
|
575
|
+
h(
|
|
576
|
+
'button',
|
|
577
|
+
{
|
|
578
|
+
key: 'edit',
|
|
579
|
+
type: 'button',
|
|
580
|
+
style: { ...smallButtonStyle, flex: '0 0 auto' },
|
|
581
|
+
disabled: busy,
|
|
582
|
+
onClick: () => (limitEditing === model.id ? closeLimits() : openLimits(model)),
|
|
583
|
+
},
|
|
584
|
+
t('limitsEdit'),
|
|
585
|
+
),
|
|
586
|
+
],
|
|
587
|
+
),
|
|
588
|
+
limitEditing === model.id
|
|
589
|
+
? h(
|
|
590
|
+
'div',
|
|
591
|
+
{ key: 'editor', style: limitEditorStyle },
|
|
592
|
+
[
|
|
593
|
+
h('label', { key: 'cwLabel', style: fieldLabelStyle }, t('limitsWindow')),
|
|
594
|
+
h('input', {
|
|
595
|
+
key: 'cw',
|
|
596
|
+
type: 'text',
|
|
597
|
+
inputMode: 'numeric',
|
|
598
|
+
value: limitWindow,
|
|
599
|
+
placeholder: formatTokens(model.defaults?.contextWindow),
|
|
600
|
+
disabled: busy,
|
|
601
|
+
style: limitInputStyle,
|
|
602
|
+
onChange: (event) => setLimitWindow(event.target.value),
|
|
603
|
+
}),
|
|
604
|
+
h('p', { key: 'cwHint', style: fieldHintStyle },
|
|
605
|
+
t('limitsDefaultHint', { value: describeTokens(model.defaults?.contextWindow) })),
|
|
606
|
+
h('label', { key: 'mtLabel', style: fieldLabelStyle }, t('limitsOutput')),
|
|
607
|
+
h('input', {
|
|
608
|
+
key: 'mt',
|
|
609
|
+
type: 'text',
|
|
610
|
+
inputMode: 'numeric',
|
|
611
|
+
value: limitOutput,
|
|
612
|
+
placeholder: formatTokens(model.defaults?.maxTokens),
|
|
613
|
+
disabled: busy,
|
|
614
|
+
style: limitInputStyle,
|
|
615
|
+
onChange: (event) => setLimitOutput(event.target.value),
|
|
616
|
+
}),
|
|
617
|
+
h('p', { key: 'mtHint', style: fieldHintStyle },
|
|
618
|
+
t('limitsDefaultHint', { value: describeTokens(model.defaults?.maxTokens) })),
|
|
619
|
+
h('div', { key: 'actions', style: { display: 'flex', gap: 8, justifyContent: 'flex-end', marginTop: 2 } }, [
|
|
620
|
+
h('button', { key: 'reset', type: 'button', style: smallButtonStyle, disabled: busy, onClick: () => resetLimits(model) }, t('limitsResetButton')),
|
|
621
|
+
h('button', { key: 'cancel', type: 'button', style: smallButtonStyle, disabled: busy, onClick: closeLimits }, t('cancel')),
|
|
622
|
+
h('button', { key: 'save', type: 'button', style: { ...buttonPrimaryStyle, minHeight: 26, padding: '2px 14px', fontSize: 12 }, disabled: busy, onClick: () => submitLimits(model) }, t('limitsSave')),
|
|
623
|
+
]),
|
|
624
|
+
],
|
|
625
|
+
)
|
|
626
|
+
: null,
|
|
627
|
+
],
|
|
486
628
|
),
|
|
487
629
|
),
|
|
488
630
|
),
|
|
@@ -533,11 +675,21 @@ window.__ModuleLoader__.load({
|
|
|
533
675
|
copyFailed: 'Copy failed — select the text and copy manually.',
|
|
534
676
|
keysTitle: 'Key pool',
|
|
535
677
|
modelsTitle: 'Models in the picker',
|
|
536
|
-
modelsHint: '{count} of {total} models are shown
|
|
678
|
+
modelsHint: '{count} of {total} models are shown. Context window and max output are editable per model — clear a field to restore the default.',
|
|
537
679
|
modelsAllHidden: 'Every model is hidden — the picker will be empty until you re-enable one.',
|
|
538
680
|
modelsEmpty: 'No models yet. Add a key and refresh the list.',
|
|
539
681
|
modelShown: '{id} is now shown',
|
|
540
682
|
modelHidden: '{id} is hidden',
|
|
683
|
+
limitsEdit: 'Settings',
|
|
684
|
+
limitsWindow: 'Context window',
|
|
685
|
+
limitsOutput: 'Max output tokens',
|
|
686
|
+
limitsDefaultHint: 'Default {value}. Accepts 1M / 128K; clear the field to restore it.',
|
|
687
|
+
limitsSave: 'Save',
|
|
688
|
+
limitsResetButton: 'Restore default',
|
|
689
|
+
limitsReset: 'Restored default limits for {id}',
|
|
690
|
+
limitsSaved: 'Saved limits for {id}',
|
|
691
|
+
limitsCustom: '(custom)',
|
|
692
|
+
cancel: 'Cancel',
|
|
541
693
|
};
|
|
542
694
|
|
|
543
695
|
const zh = {
|
|
@@ -578,9 +730,20 @@ window.__ModuleLoader__.load({
|
|
|
578
730
|
copyFailed: '复制失败,请手动选中文本复制。',
|
|
579
731
|
keysTitle: 'Key 池',
|
|
580
732
|
modelsTitle: '下拉中的模型',
|
|
581
|
-
modelsHint: '共 {total} 个模型,当前展示 {count}
|
|
733
|
+
modelsHint: '共 {total} 个模型,当前展示 {count} 个。窗口与最大输出可逐模型修改(清空输入框即恢复默认)。',
|
|
582
734
|
modelsAllHidden: '所有模型都被隐藏了,模型选择器会是空的 —— 至少勾回一个。',
|
|
583
735
|
modelsEmpty: '还没有模型,请先添加 key 并刷新列表。',
|
|
736
|
+
limitsEdit: '设置',
|
|
737
|
+
limitsWindow: '上下文窗口',
|
|
738
|
+
limitsOutput: '最大输出 token 数',
|
|
739
|
+
|
|
740
|
+
limitsDefaultHint: '默认 {value},支持 1M / 128K 写法,留空即恢复默认。',
|
|
741
|
+
limitsSave: '保存',
|
|
742
|
+
limitsResetButton: '恢复默认',
|
|
743
|
+
limitsReset: '已恢复 {id} 的默认窗口',
|
|
744
|
+
limitsSaved: '已保存 {id} 的窗口设置',
|
|
745
|
+
limitsCustom: '(自定义)',
|
|
746
|
+
cancel: '取消',
|
|
584
747
|
modelShown: '已展示 {id}',
|
|
585
748
|
modelHidden: '已隐藏 {id}',
|
|
586
749
|
};
|
package/lib/index.js
CHANGED
|
@@ -21,7 +21,7 @@ import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
|
|
|
21
21
|
import { createProvider } from '@earendil-works/pi-ai';
|
|
22
22
|
import { openAICompletionsApi } from '@earendil-works/pi-ai/api/openai-completions.lazy';
|
|
23
23
|
import z from '@deepseek-ai/schemastery';
|
|
24
|
-
import { DEFAULT_BASE_URL } from './catalog.js';
|
|
24
|
+
import { DEFAULT_BASE_URL, thinkingLevelMapFor } from './catalog.js';
|
|
25
25
|
import { KeyPool, keyIdOf, maskKey } from './keypool.js';
|
|
26
26
|
import { ModelCatalog, createShim } from './shim.js';
|
|
27
27
|
import { registerKeyRoutes, registerSettingsSection } from './routes.js';
|
|
@@ -132,6 +132,29 @@ export function apply(ctx, config) {
|
|
|
132
132
|
* `buildModels()`,因此不会出现在模型选择器里(上游仍然可用,只是不展示)。
|
|
133
133
|
*/
|
|
134
134
|
const disabledModels = new Set();
|
|
135
|
+
/**
|
|
136
|
+
* 用户为单个模型自定义的窗口 / 输出上限。
|
|
137
|
+
*
|
|
138
|
+
* 为什么要让用户改:上游 `/v1/models` 的 `context_length` 不一定可信(issue #4
|
|
139
|
+
* 实测到 `sensenova-6.8-flash` 报小 4 倍),基线只能覆盖我们已经实测过的模型;
|
|
140
|
+
* 剩下那些「上游报了但我们没实测」的,交给用户自己的判断。
|
|
141
|
+
*
|
|
142
|
+
* 优先级:用户覆盖 > 目录值(目录值可能已被基线覆盖,见 catalog.ts)。
|
|
143
|
+
* 逐字段回退:只改窗口时,输出上限仍取目录值。
|
|
144
|
+
*/
|
|
145
|
+
const modelLimits = new Map();
|
|
146
|
+
/** 正整数校验(窗口/输出上限只接受正整数)。 */
|
|
147
|
+
function isPositiveInteger(value) {
|
|
148
|
+
return typeof value === 'number' && Number.isInteger(value) && value > 0;
|
|
149
|
+
}
|
|
150
|
+
/** 该模型最终生效的窗口 / 输出上限。 */
|
|
151
|
+
function limitsFor(model) {
|
|
152
|
+
const override = modelLimits.get(model.id);
|
|
153
|
+
return {
|
|
154
|
+
contextWindow: override?.contextWindow ?? model.contextWindow,
|
|
155
|
+
maxTokens: override?.maxTokens ?? model.maxTokens,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
135
158
|
/**
|
|
136
159
|
* 对外暴露的 API Key(别的服务用它访问本地 shim)。
|
|
137
160
|
*
|
|
@@ -174,13 +197,20 @@ export function apply(ctx, config) {
|
|
|
174
197
|
return { baseUrl: '', port: 0, apiKey: shim.token() };
|
|
175
198
|
}
|
|
176
199
|
},
|
|
177
|
-
models: () => catalog.current().map((model) =>
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
200
|
+
models: () => catalog.current().map((model) => {
|
|
201
|
+
const limits = limitsFor(model);
|
|
202
|
+
return {
|
|
203
|
+
id: model.id,
|
|
204
|
+
name: model.name,
|
|
205
|
+
// 生效值(含用户覆盖),界面显示与模型注册都用它。
|
|
206
|
+
contextWindow: limits.contextWindow,
|
|
207
|
+
maxTokens: limits.maxTokens,
|
|
208
|
+
// 目录给出的默认值(可能已被基线覆盖上游),供「恢复默认」与占位提示。
|
|
209
|
+
defaults: { contextWindow: model.contextWindow, maxTokens: model.maxTokens },
|
|
210
|
+
overridden: modelLimits.has(model.id),
|
|
211
|
+
enabled: !disabledModels.has(model.id),
|
|
212
|
+
};
|
|
213
|
+
}),
|
|
184
214
|
providerId: PROVIDER_ID,
|
|
185
215
|
providerName: PROVIDER_NAME,
|
|
186
216
|
baseUrl: resolved.baseUrl,
|
|
@@ -316,6 +346,24 @@ export function apply(ctx, config) {
|
|
|
316
346
|
disabledModels.add(id);
|
|
317
347
|
}
|
|
318
348
|
}
|
|
349
|
+
// 模型窗口/输出上限的用户覆盖:坏条目跳过,不影响其它模型。
|
|
350
|
+
modelLimits.clear();
|
|
351
|
+
if (payload.modelLimits !== null && typeof payload.modelLimits === 'object') {
|
|
352
|
+
for (const [id, value] of Object.entries(payload.modelLimits)) {
|
|
353
|
+
if (typeof id !== 'string' || id.length === 0)
|
|
354
|
+
continue;
|
|
355
|
+
if (value === null || typeof value !== 'object')
|
|
356
|
+
continue;
|
|
357
|
+
const record = value;
|
|
358
|
+
const limits = {};
|
|
359
|
+
if (isPositiveInteger(record.contextWindow))
|
|
360
|
+
limits.contextWindow = record.contextWindow;
|
|
361
|
+
if (isPositiveInteger(record.maxTokens))
|
|
362
|
+
limits.maxTokens = record.maxTokens;
|
|
363
|
+
if (limits.contextWindow !== undefined || limits.maxTokens !== undefined)
|
|
364
|
+
modelLimits.set(id, limits);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
319
367
|
// 对外密钥:拿到持久化的那把后立刻换上,DSH 与外部服务从此用同一个。
|
|
320
368
|
if (typeof payload.externalKey === 'string' && payload.externalKey.length >= 16) {
|
|
321
369
|
externalKey = payload.externalKey;
|
|
@@ -366,6 +414,8 @@ export function apply(ctx, config) {
|
|
|
366
414
|
payload: {
|
|
367
415
|
keys: pool.keys(),
|
|
368
416
|
disabledModels: [...disabledModels],
|
|
417
|
+
// 只存用户真正改过的模型;没有覆盖的模型不出现在这里。
|
|
418
|
+
modelLimits: Object.fromEntries(modelLimits),
|
|
369
419
|
externalKey,
|
|
370
420
|
},
|
|
371
421
|
}));
|
|
@@ -397,30 +447,21 @@ export function apply(ctx, config) {
|
|
|
397
447
|
provider: PROVIDER_ID,
|
|
398
448
|
baseUrl,
|
|
399
449
|
input: model.supportsImages ? ['text', 'image'] : ['text'],
|
|
400
|
-
//
|
|
450
|
+
// 商汤的思考等级 —— **每个模型的合法词表不同**(issue #3):`glm-5.3`
|
|
451
|
+
// 只认 low / high / max(真机发 xhigh、none 都是 400),而 `glm-5.2`、
|
|
452
|
+
// `deepseek-v4-flash` 七档全收。
|
|
401
453
|
//
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
//
|
|
405
|
-
//
|
|
406
|
-
//
|
|
454
|
+
// 这里只把**实测支持**的档位给 pi-ai,不支持的写 null —— 于是模型选择器里
|
|
455
|
+
// glm-5.3 的思考档只剩 Low / High / Max,声明什么就发什么。缺档的兜底(例如
|
|
456
|
+
// 别的客户端发来 xhigh)由 shim 清洗层就近归位,不在这里做。
|
|
457
|
+
//
|
|
458
|
+
// 全局表的教训:把 `max` 统一翻成 `xhigh` 会让 glm-5.3 的每次调用都
|
|
459
|
+
// 400,而不翻又会让七档全收的模型浪费掉 `max` —— 怎么调都会踩到某一头。
|
|
407
460
|
reasoning: model.reasoning,
|
|
408
|
-
...(model.reasoning
|
|
409
|
-
? {
|
|
410
|
-
thinkingLevelMap: {
|
|
411
|
-
off: 'none',
|
|
412
|
-
minimal: null,
|
|
413
|
-
low: 'low',
|
|
414
|
-
medium: 'medium',
|
|
415
|
-
high: 'high',
|
|
416
|
-
xhigh: 'xhigh',
|
|
417
|
-
max: 'xhigh',
|
|
418
|
-
},
|
|
419
|
-
}
|
|
420
|
-
: {}),
|
|
461
|
+
...(model.reasoning ? { thinkingLevelMap: thinkingLevelMapFor(model.id) } : {}),
|
|
421
462
|
cost: NO_COST,
|
|
422
|
-
|
|
423
|
-
|
|
463
|
+
// 用户在设置页里的自定义值优先于目录值(issue #4:上游的 context_length 不一定可信)。
|
|
464
|
+
...limitsFor(model),
|
|
424
465
|
compat: { maxTokensField: 'max_tokens' },
|
|
425
466
|
}));
|
|
426
467
|
}
|
|
@@ -680,6 +721,25 @@ export function apply(ctx, config) {
|
|
|
680
721
|
logger.warn(`模型可见性落盘失败(本次会话已生效):${error instanceof Error ? error.message : String(error)}`);
|
|
681
722
|
}
|
|
682
723
|
};
|
|
724
|
+
/**
|
|
725
|
+
* 应用「某个模型的窗口 / 输出上限」的用户覆盖。
|
|
726
|
+
*
|
|
727
|
+
* 两个字段都缺省表示恢复默认(删除这条覆盖)。落盘失败不算失败:内存里已经
|
|
728
|
+
* 生效,下次写入会一并带上(与模型可见性同样的策略)。
|
|
729
|
+
*/
|
|
730
|
+
const handleModelLimitsChanged = async (id, limits) => {
|
|
731
|
+
if (limits.contextWindow === undefined && limits.maxTokens === undefined)
|
|
732
|
+
modelLimits.delete(id);
|
|
733
|
+
else
|
|
734
|
+
modelLimits.set(id, { ...limits });
|
|
735
|
+
refreshCatalog();
|
|
736
|
+
try {
|
|
737
|
+
await saveKeys();
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
logger.warn(`模型窗口设置落盘失败(本次会话已生效):${error instanceof Error ? error.message : String(error)}`);
|
|
741
|
+
}
|
|
742
|
+
};
|
|
683
743
|
// 设置页:填 key(用户明确要求只在设置页填)。
|
|
684
744
|
try {
|
|
685
745
|
registerSettingsSection(ctx, {
|
|
@@ -700,6 +760,7 @@ export function apply(ctx, config) {
|
|
|
700
760
|
logger,
|
|
701
761
|
onKeysChanged: handleKeysChanged,
|
|
702
762
|
onModelsChanged: handleModelsChanged,
|
|
763
|
+
onModelLimitsChanged: handleModelLimitsChanged,
|
|
703
764
|
reload: async () => {
|
|
704
765
|
// 手动重载:走同一套「装载并宣告」逻辑,因此在启动早期点它也不会把
|
|
705
766
|
// 「还没读到」误当成「真的没有 key」而清空权威装载信号。
|
package/lib/routes.d.ts
CHANGED
|
@@ -46,12 +46,20 @@ export interface StatusProvider {
|
|
|
46
46
|
port: number;
|
|
47
47
|
apiKey: string;
|
|
48
48
|
};
|
|
49
|
-
/** 目录里的全部模型 +
|
|
49
|
+
/** 目录里的全部模型 + 是否在下拉中展示 + 生效的窗口/输出上限。 */
|
|
50
50
|
models(): Array<{
|
|
51
51
|
id: string;
|
|
52
52
|
name: string;
|
|
53
|
+
/** 生效值(含用户在设置页里的覆盖)。 */
|
|
53
54
|
contextWindow: number;
|
|
54
55
|
maxTokens: number;
|
|
56
|
+
/** 目录给出的默认值(可能已被基线覆盖上游),供「恢复默认」与占位提示。 */
|
|
57
|
+
defaults: {
|
|
58
|
+
contextWindow: number;
|
|
59
|
+
maxTokens: number;
|
|
60
|
+
};
|
|
61
|
+
/** 是否被用户自定义覆盖。 */
|
|
62
|
+
overridden: boolean;
|
|
55
63
|
enabled: boolean;
|
|
56
64
|
}>;
|
|
57
65
|
providerId: string;
|
|
@@ -71,6 +79,11 @@ export interface RouteDeps {
|
|
|
71
79
|
onKeysChanged(): Promise<void>;
|
|
72
80
|
/** 模型可见性发生变更后回调(收到完整的「被隐藏」列表)。 */
|
|
73
81
|
onModelsChanged(disabled: string[]): Promise<void>;
|
|
82
|
+
/** 某个模型的窗口/输出上限发生变更;两字段都缺省表示恢复默认。 */
|
|
83
|
+
onModelLimitsChanged(id: string, limits: {
|
|
84
|
+
contextWindow?: number;
|
|
85
|
+
maxTokens?: number;
|
|
86
|
+
}): Promise<void>;
|
|
74
87
|
/** 重新从凭据库装载。 */
|
|
75
88
|
reload(): Promise<void>;
|
|
76
89
|
/** 主动拉取上游模型目录。 */
|
package/lib/routes.js
CHANGED
|
@@ -40,6 +40,37 @@ function loopbackRequest(req) {
|
|
|
40
40
|
return false;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
/** 简写单位:K = 1024、M = 1024²(与宿主模型配置页的 `1M` / `128K` 一致)。 */
|
|
44
|
+
const SIZE_UNITS = { k: 1024, m: 1048576 };
|
|
45
|
+
/**
|
|
46
|
+
* 解析界面传来的窗口/输出上限。
|
|
47
|
+
*
|
|
48
|
+
* 接受纯数字,也接受 `1M` / `128K` 这种简写:大小写与空格随意(`1M`、`1m`、
|
|
49
|
+
* ` 128 K `),支持小数(`1.5M` = 1,572,864)。空值表示「恢复默认」。
|
|
50
|
+
* 其余一律非法 —— 宁可让界面收到 400,也不要把一个奇怪的值写进记录。
|
|
51
|
+
*/
|
|
52
|
+
function parseLimit(raw) {
|
|
53
|
+
if (raw === undefined || raw === null)
|
|
54
|
+
return undefined;
|
|
55
|
+
if (typeof raw === 'number') {
|
|
56
|
+
return Number.isFinite(raw) && Number.isInteger(raw) && raw > 0 ? raw : 'invalid';
|
|
57
|
+
}
|
|
58
|
+
if (typeof raw !== 'string')
|
|
59
|
+
return 'invalid';
|
|
60
|
+
const text = raw.trim();
|
|
61
|
+
if (text === '')
|
|
62
|
+
return undefined;
|
|
63
|
+
const match = /^(\d+(?:\.\d+)?)\s*([kKmM]?)$/.exec(text);
|
|
64
|
+
if (match === null)
|
|
65
|
+
return 'invalid';
|
|
66
|
+
const base = Number(match[1]);
|
|
67
|
+
const unit = (match[2] ?? '').toLowerCase();
|
|
68
|
+
const multiplier = unit === '' ? 1 : SIZE_UNITS[unit];
|
|
69
|
+
if (!Number.isFinite(base) || base <= 0 || multiplier === undefined)
|
|
70
|
+
return 'invalid';
|
|
71
|
+
const value = Math.round(base * multiplier);
|
|
72
|
+
return Number.isInteger(value) && value > 0 ? value : 'invalid';
|
|
73
|
+
}
|
|
43
74
|
function sendJson(res, status, payload) {
|
|
44
75
|
const text = JSON.stringify(payload);
|
|
45
76
|
try {
|
|
@@ -240,6 +271,46 @@ export function registerKeyRoutes(ctx, deps) {
|
|
|
240
271
|
sendJson(res, 200, { id, enabled, models: deps.status.models() });
|
|
241
272
|
return;
|
|
242
273
|
}
|
|
274
|
+
// ---- 模型的窗口 / 输出上限:让用户能覆盖上游的元数据 -------------
|
|
275
|
+
//
|
|
276
|
+
// 上游 /v1/models 的 context_length 不一定可信(issue #4 实测到把
|
|
277
|
+
// sensenova-6.8-flash 报小 4 倍)。基线只能覆盖我们实测过的模型,剩下的
|
|
278
|
+
// 交给用户自己的判断:这里保存「每个模型的窗口与输出上限」。
|
|
279
|
+
//
|
|
280
|
+
// 归一规则:字段留空、或与目录默认值相同 → 视为恢复默认(不存覆盖)。
|
|
281
|
+
if (action === 'set-model-limits') {
|
|
282
|
+
const id = typeof body?.id === 'string' ? body.id : '';
|
|
283
|
+
if (!id) {
|
|
284
|
+
sendJson(res, 400, { error: '缺少模型 id' });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const target = deps.status.models().find((model) => model.id === id);
|
|
288
|
+
if (target === undefined) {
|
|
289
|
+
sendJson(res, 404, { error: `未知模型:${id}` });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const contextWindow = parseLimit(body?.contextWindow);
|
|
293
|
+
const maxTokens = parseLimit(body?.maxTokens);
|
|
294
|
+
if (contextWindow === 'invalid' || maxTokens === 'invalid') {
|
|
295
|
+
sendJson(res, 400, { error: '上下文窗口与最大输出必须是正整数,可写 1M / 128K(留空表示恢复默认)' });
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (contextWindow !== undefined && contextWindow < 1024) {
|
|
299
|
+
sendJson(res, 400, { error: '上下文窗口至少 1024' });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const effectiveWindow = contextWindow ?? target.defaults.contextWindow;
|
|
303
|
+
if (maxTokens !== undefined && maxTokens >= effectiveWindow) {
|
|
304
|
+
sendJson(res, 400, { error: '最大输出必须小于上下文窗口' });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
await deps.onModelLimitsChanged(id, {
|
|
308
|
+
contextWindow: contextWindow === target.defaults.contextWindow ? undefined : contextWindow,
|
|
309
|
+
maxTokens: maxTokens === target.defaults.maxTokens ? undefined : maxTokens,
|
|
310
|
+
});
|
|
311
|
+
sendJson(res, 200, { id, models: deps.status.models() });
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
243
314
|
if (action === 'revive') {
|
|
244
315
|
// 手动复活被误判失效的 key。
|
|
245
316
|
const revived = deps.pool.reviveInvalid();
|
|
@@ -0,0 +1,51 @@
|
|
|
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" ← 商汤拼作 none
|
|
27
|
+
*
|
|
28
|
+
* 规范成等价安全写法后实测全部 200。
|
|
29
|
+
*
|
|
30
|
+
* 注意 `max` / `minimal` **本来就是合法值**,不能映射掉(issue #2 的结论);
|
|
31
|
+
* 它们之所以仍可能 400,是因为**每个模型接受的词表不同**(issue #3):
|
|
32
|
+
* `glm-5.3` 只认 `low / high / max`,收到 `xhigh` 一样 400。词表与就近归位
|
|
33
|
+
* 规则集中在 catalog.ts,本文件只负责在转发前按词表把值改合法。
|
|
34
|
+
*
|
|
35
|
+
* 刻意**不代改**的三类(改了是把错误往后推,甚至篡改用户意图):
|
|
36
|
+
* * `user.content` 为 null/"":确实是空用户轮,上游报 `user content required`;
|
|
37
|
+
* * 数值越界(temperature=3、frequency_penalty=5):上游报错信息已足够明确;
|
|
38
|
+
* * 未知 role(除 developer 外):猜错会掩盖真正的调用错误。
|
|
39
|
+
*/
|
|
40
|
+
export interface SanitizeResult<T> {
|
|
41
|
+
body: T;
|
|
42
|
+
/** 实际做过的改动,供日志/UI 展示(空数组表示原样放行)。 */
|
|
43
|
+
changes: string[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 清洗一份 OpenAI chat 请求体。
|
|
47
|
+
*
|
|
48
|
+
* 只做**语义等价**的改写,不改变请求意图;返回改动清单便于排查。
|
|
49
|
+
* 非对象输入原样返回(交给上游如实报错)。
|
|
50
|
+
*/
|
|
51
|
+
export declare function sanitizeChatRequest<T>(input: T): SanitizeResult<T>;
|