dsh-pin-default-model 0.0.0-stage → 1.3.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.
@@ -0,0 +1,244 @@
1
+ # 需求分析:默认推理等级锁定
2
+
3
+ - 插件:`dsh-pin-default-model`
4
+ - 当前版本:1.2.0
5
+ - 目标版本:1.3.0
6
+ - 日期:2026-11-18
7
+
8
+ ---
9
+
10
+ ## 1. 需求描述
11
+
12
+ 在现有「默认模型锁定」基础上,增加**默认推理等级(reasoning effort)锁定**:
13
+
14
+ - 新建会话使用锁定的推理等级
15
+ - 默认值为 `high`
16
+ - 会话内临时切换推理等级仍然只对该会话生效,不改写锁定值
17
+
18
+ ---
19
+
20
+ ## 2. 已核实的现状(源码级)
21
+
22
+ ### 2.1 数据链路
23
+
24
+ ```
25
+ agentDefaultModel.currentSelection() → { provider, model, reasoningEffort? }
26
+ │ (三个字段都是 Volatile 配置引用)
27
+ ├─→ AgentOptions.reasoningEffort (getter,实时读取)
28
+ │ └─→ 新会话的 Agent 用它作为初始推理等级
29
+ │
30
+ └─→ saveSelection(next) → configEditor.edit(entry, () => config)
31
+ └─→ 写入 profile 的 agent-default-model 条目
32
+ ```
33
+
34
+ 会话内切换走 `sessionController.selectModel(request)`(`dsh-api-session-controller/src/commands.ts:166`):
35
+
36
+ ```ts
37
+ agent.model.select(selected); // 会话内立即生效
38
+ void this.ctx.agentDefaultModel.saveSelection(selected); // 顺手改写全局默认值 ← 要拦截的就是这条
39
+ ```
40
+
41
+ `selected` 是三元组 `{ provider, model, reasoningEffort? }`,**推理等级本来就在被改写的范围内**。
42
+
43
+ ### 2.2 现有拦截逻辑已天然覆盖 effort
44
+
45
+ `lib/index.js` 的 `keyOf()` 已经按三元组比较:
46
+
47
+ ```js
48
+ [provider, model, reasoningEffort].join('\u0000')
49
+ ```
50
+
51
+ 所以只要 `pin` 里带上 `reasoningEffort`:
52
+
53
+ - 会话内只切 effort 不切 model → `keyOf(next) !== pinKey` → **被拒绝**,默认值不被改写 ✓
54
+ - `hold()` 漂移修复也按三元组比对 → effort 一起恢复 ✓
55
+
56
+ **host 端拦截逻辑无需重构,只需让 pin 携带 effort。**
57
+
58
+ ### 2.3 effort 是 model-specific 的(关键约束)
59
+
60
+ `dsh-llm/src/index.ts:913` `resolveCallConfig()` 是**硬校验**:
61
+
62
+ | 情况 | 行为 |
63
+ | --- | --- |
64
+ | 模型无 reasoning,但请求了 effort | 抛 `UNSUPPORTED_REASONING_EFFORT` |
65
+ | 请求的 effort 不在该模型的 efforts 列表里 | 抛 `UNSUPPORTED_REASONING_EFFORT` |
66
+
67
+ 这意味着 **锁定的 effort 不被当前模型支持时,新会话会直接失败**,不是降级。
68
+
69
+ ### 2.4 实测模型目录(当前 profile,2026-11-18)
70
+
71
+ 当前锁定:`magpie/group/x-call` → efforts `[high]`,无 defaultEffort。
72
+
73
+ 各模型的 effort 覆盖面差异极大:
74
+
75
+ | 模型 | efforts | default |
76
+ |---|---|---|
77
+ | `magpie/group/x-call` | `high` | — |
78
+ | `magpie/workbuddy/space-bunny` | `low,medium,high,xhigh,max` | — |
79
+ | `magpie/workbuddy/minimax-m3.1-flash` | `low,medium,high,xhigh,max` | — |
80
+ | `magpie/openrouter/liquid/lfm-3-1.7b:free` | **(无 reasoning)** | — |
81
+ | `magpie/kilo/kilo-auto/free` | **(无 reasoning)** | — |
82
+ | `magpie-anthropic/tokenhub/minimax-m3` | **(无 reasoning)** | — |
83
+ | `deepseek-account/deepseek-flash` | `off,low,high,max` | `high` |
84
+
85
+ 注意:
86
+
87
+ - 很多模型支持 `off`(关闭推理)
88
+ - **约 10 个模型完全没有 reasoning**,这类模型**任何 effort 都不能锁**,连 `off` 也不行
89
+ - `high` 是所有支持 reasoning 的模型都有的档位,覆盖面最好
90
+
91
+ ### 2.5 顺带发现的既有 Bug(需一并修)
92
+
93
+ `client/client.js` 当前把 `efforts` 当**对象**处理:
94
+
95
+ ```js
96
+ const efforts = model?.reasoning?.efforts;
97
+ return Object.keys(efforts).filter((k) => efforts[k] !== false); // ❌
98
+ ```
99
+
100
+ 但实际类型是**数组**(`dsh-api-session-controller/src/types.ts:106`):
101
+
102
+ ```ts
103
+ readonly efforts: readonly ModelReasoningEffort[] // [{ id, name, description? }]
104
+ readonly defaultEffort?: string
105
+ ```
106
+
107
+ 数组走 `Object.keys` 会得到 `["0","1","2"]`,下拉框会显示 `0`/`1` 这种垃圾值。
108
+
109
+ 现在没暴露,纯粹是因为当前模型 `magpie/group/x-call` 的 reasoning 元数据在界面里没被正确读取到 effort 下拉。**加了 effort 锁定后这个 bug 必然暴露,必须同时修。**
110
+
111
+ ---
112
+
113
+ ## 3. 功能需求
114
+
115
+ ### F1 设置页:推理等级下拉
116
+
117
+ - 在模型下拉框旁增加「推理等级」下拉
118
+ - 选项来自**当前选中模型**的 `reasoning.efforts`,显示 `name`,取值 `id`
119
+ - 提供「跟随模型默认」选项(值为空)
120
+ - 模型切换时,effort 选项列表跟着换
121
+ - 若当前锁定的 effort 不在新模型的列表里,给出明确提示
122
+
123
+ ### F2 锁定语义
124
+
125
+ - `pin` 扩展为 `{ provider, model, reasoningEffort? }`
126
+ - `reasoningEffort` 缺省时:
127
+ - **跟随模型默认**(不写入 effort,等于 DSH 原生行为)
128
+ - 界面上默认值呈现为 `high`(当模型支持时)
129
+
130
+ ### F3 默认值 high
131
+
132
+ 插件配置 `reasoningEffort` 未设置时按 `high` 处理。
133
+
134
+ > ⚠️ 此项与 F2 的「跟随模型默认」有冲突,见下方决策点 D1。
135
+
136
+ ### F4 持久化
137
+
138
+ - 锁定值写入 profile,重启后保持
139
+ - `saveSelection` 写入 `agent-default-model` 条目时带 `reasoningEffort`
140
+ - 已核实:`provider`/`model` 同为 Volatile 字段却确实写进了 profile,Volatile 不阻止持久化
141
+
142
+ ### F5 会话内行为不变
143
+
144
+ - 会话内切模型/切 effort → 该会话生效,默认值不变
145
+ - 自动化任务创建的新会话 → 使用锁定的模型 + effort
146
+
147
+ ---
148
+
149
+ ## 4. 风险
150
+
151
+ | 级别 | 风险 | 说明 |
152
+ | --- | --- | --- |
153
+ | **高** | effort 与模型不兼容导致会话创建失败 | `resolveCallConfig` 硬校验,抛 `UNSUPPORTED_REASONING_EFFORT` |
154
+ | **高** | 锁定 `high` 但切到无 reasoning 的模型 | 该模型任何 effort 都不支持,会话直接失败 |
155
+ | **中** | 模型切换后锁定值失效 | 换模型后原 effort 可能不在新列表里 |
156
+ | **中** | `off` 语义歧义 | `off` 表示关闭推理,但仍属 effort 列表项,不能当「跟随」用 |
157
+ | 低 | 客户端 efforts 数组处理错误 | 见 2.5,必须修 |
158
+ | 低 | effort 值随 provider 变化 | 不同 provider 的档位命名不统一(`xhigh` / `minimal` / `medium`) |
159
+
160
+ ---
161
+
162
+ ## 5. 待决策项
163
+
164
+ ### D1 「默认 high」的确切语义
165
+
166
+ | 方案 | 描述 | 优点 | 缺点 |
167
+ | --- | --- | --- | --- |
168
+ | **A(推荐)** | 配置缺省 = **跟随模型默认**;UI 默认展示并推荐 `high` | 对无 reasoning 的模型天然安全,不会把会话搞挂 | 不完全等于「强制 high」 |
169
+ | B | 配置缺省 = 强制 `high` | 严格符合字面需求 | 切到无 reasoning 模型会**直接失败** |
170
+ | C | 缺省 = 强制 `high`,但**运行时校验**:模型不支持时自动落到该模型的 `defaultEffort` 或最高档 | 兼顾字面需求与安全 | 行为不完全可预测,需要额外提示 |
171
+
172
+ > 当前锁定的 `magpie/group/x-call` 只支持 `high`,所以 A/B/C 在**当前配置下行为完全一致**。差异只在主人日后换到别的模型时才显现。
173
+
174
+ ### D2 模型不支持锁定 effort 时的处理
175
+
176
+ | 方案 | 描述 |
177
+ | --- | --- |
178
+ | **A(推荐)** | 设置页明确警告 + 提供「一键跟随模型默认」;不做自动降级 |
179
+ | B | 运行时自动降级到 `defaultEffort`(无则最高档),日志记录 |
180
+ | C | 保持失败,让主人自己发现 |
181
+
182
+ > 选 A 需要在设置页做到「当前模型不支持此 effort」的提示足够醒目。
183
+
184
+ ### D3 是否允许「跟随模型默认」作为一个可选锁定值
185
+
186
+ 需要锁定值本身能表达「不锁 effort」。当前 `cleanSelection` 已支持省略 effort,但要确认这在语义上与「锁定 effort」区分得开(我倾向用**字段缺省**表达跟随,不引入哨兵值)。
187
+
188
+ ---
189
+
190
+ ## 6. 实现方案概要
191
+
192
+ ### 6.1 host 端(`lib/index.js`)
193
+
194
+ 改动不大:
195
+
196
+ 1. `FALLBACK_PIN` 增加 `reasoningEffort`(按 D1 结论定)
197
+ 2. `readPin` / config 读取:处理 effort 缺省与校验
198
+ 3. `/dsh-pin-model/set` 路由:接受并保存 `reasoningEffort`
199
+ 4. `/dsh-pin-model/view` 路由:返回锁定值(含 effort)
200
+ 5. `hold()` 漂移修复:三元组已覆盖,无需改
201
+
202
+ **校验点**:写 pin 时,若锁定模型不支持该 effort,host 端应**拒绝并返回明确错误**(而不是等到会话创建时才炸)。
203
+
204
+ ### 6.2 客户端(`client/client.js`)
205
+
206
+ 1. **修 efforts 数组 bug**:`model.reasoning.efforts.map(e => e.id)`,显示 `e.name`
207
+ 2. 新增 effort 下拉,选项随当前模型联动
208
+ 3. 「跟随模型默认」选项
209
+ 4. 当前锁定展示加上 effort
210
+ 5. 不兼容提示(按 D2 结论)
211
+
212
+ ### 6.3 配置与文档
213
+
214
+ - `cordis.patch.yml` 增加 `reasoningEffort` 示例
215
+ - README 中英文补充 effort 锁定说明与「模型必须支持该档位」的警告
216
+ - 版本号 → 1.3.0
217
+ - 重新生成 preview 配图(含 effort 控件)
218
+
219
+ ---
220
+
221
+ ## 7. 验收标准
222
+
223
+ 1. 设置页出现「推理等级」下拉,选项为当前模型支持的档位(含显示名)
224
+ 2. 选择 `high` 保存后,重启 DSH 锁定值仍在
225
+ 3. 新建会话使用锁定模型 + `high`
226
+ 4. 会话内切换 effort → 该会话生效,默认锁定值不变
227
+ 5. 会话内切换模型 → 默认锁定值不变
228
+ 6. 切换到不支持 `high` 的模型 → 设置页给出明确警告,不静默失败
229
+ 7. 自动化任务创建的新会话同样使用锁定模型 + effort
230
+ 8. 现有测试全部通过,新增 effort 相关测试
231
+
232
+ ---
233
+
234
+ ## 8. 参考源码位置
235
+
236
+ | 事实 | 位置 |
237
+ | --- | --- |
238
+ | `saveSelection` / `currentSelection` | `@deepseek-ai/dsh-agent-default-model/src/index.ts:67,82` |
239
+ | Volatile 配置定义 | 同上 `:51-55` |
240
+ | `selectModel` 双写 | `@deepseek-ai/dsh-api-session-controller/src/commands.ts:166` |
241
+ | `AgentOptions` getter | `@deepseek-ai/dsh-api-session-controller/src/agent.ts:453` |
242
+ | effort 硬校验 | `@deepseek-ai/dsh-llm/src/index.ts:913` |
243
+ | `ModelReasoning` 类型 | `@deepseek-ai/dsh-api-session-controller/src/types.ts:104` |
244
+ | catalog 投影 | `@deepseek-ai/dsh-api-session-controller/src/catalog.ts:30` |
package/icon.svg ADDED
@@ -0,0 +1,8 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36" fill="none" role="img" aria-label="Default model pin">
2
+ <g stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
3
+ <path d="M18 4.8v10.4"/>
4
+ <path d="M12.4 15.2h11.2"/>
5
+ <path d="M18 15.2v7.4"/>
6
+ </g>
7
+ <circle cx="18" cy="27.4" r="3.1" fill="currentColor" fill-opacity="0.18" stroke="currentColor" stroke-width="2"/>
8
+ </svg>
package/lib/index.js ADDED
@@ -0,0 +1,361 @@
1
+ /**
2
+ * dsh-pin-default-model — keeps the Host default model fixed.
3
+ *
4
+ * A model picked inside a session is applied to that session by
5
+ * `agent.model.select()`, but `sessionController.selectModel()` also calls
6
+ * `agentDefaultModel.saveSelection()`. That second call rewrites the profile
7
+ * patch, so every later new session — hand-made or created by a scheduled
8
+ * task — inherits whatever was picked last instead of a fixed default.
9
+ *
10
+ * This plugin replaces `saveSelection` on the live service object with one that
11
+ * refuses to overwrite the pinned selection. The pin itself is changed only
12
+ * through this plugin's own host route, so a session switch can never move it.
13
+ *
14
+ * The pin carries a routing triple: provider, model, and an optional
15
+ * reasoning effort. When the effort is omitted the plugin does not interfere
16
+ * with the effort at all and the provider default applies — that is the safe
17
+ * default, because DSH rejects a request whose effort the model does not
18
+ * support (`UNSUPPORTED_REASONING_EFFORT`).
19
+ *
20
+ * The browser half uses plain fetch against those routes, which avoids any
21
+ * dependency on the settings-client services and keeps loading order simple.
22
+ */
23
+
24
+ /** Cordis plugin identity (the name the profile patch inserts). */
25
+ export const name = 'dsh-pin-default-model';
26
+
27
+ /** Services this plugin uses on the host side. */
28
+ export const inject = ['agentDefaultModel', 'webServer', 'sessionController'];
29
+
30
+ /**
31
+ * Pin used when the live service has no selection yet.
32
+ * No reasoning effort: the provider default applies until the user locks one.
33
+ */
34
+ const FALLBACK_PIN = { provider: 'magpie', model: 'group/x-call' };
35
+
36
+ /** How long to wait before the first drift check, letting the profile settle. */
37
+ const FIRST_CHECK_MS = 1500;
38
+
39
+ /** How often the live default is compared against the pin. */
40
+ const WATCH_INTERVAL_MS = 2000;
41
+
42
+ /**
43
+ * Build a clean selection value.
44
+ * @param value - a candidate selection from a request or the service.
45
+ * @returns a normalized selection, or null when provider/model are missing.
46
+ */
47
+ function cleanSelection(value) {
48
+ const provider = typeof value?.provider === 'string' ? value.provider.trim() : '';
49
+ const model = typeof value?.model === 'string' ? value.model.trim() : '';
50
+ if (provider === '' || model === '') return null;
51
+ const effort = value?.reasoningEffort;
52
+ return {
53
+ provider,
54
+ model,
55
+ ...(typeof effort === 'string' && effort.trim() !== '' ? { reasoningEffort: effort.trim() } : {}),
56
+ };
57
+ }
58
+
59
+ /**
60
+ * Decide whether a live selection satisfies the pin.
61
+ *
62
+ * The effort only participates when the pin locks one. A pin without an effort
63
+ * means "follow the provider default", so a session that changes only its
64
+ * effort is left alone while a session that changes the route is still refused.
65
+ * @param pin - the pinned selection.
66
+ * @param live - the selection to test.
67
+ * @returns true when the live selection already satisfies the pin.
68
+ */
69
+ function satisfies(pin, live) {
70
+ if (pin === undefined || live === undefined) return false;
71
+ if (pin.provider !== live.provider || pin.model !== live.model) return false;
72
+ if (pin.reasoningEffort === undefined) return true;
73
+ return pin.reasoningEffort === live.reasoningEffort;
74
+ }
75
+
76
+ /**
77
+ * Find one model in a provider-grouped catalog.
78
+ * @param groups - provider groups from the model catalog.
79
+ * @param provider - provider id.
80
+ * @param model - model id.
81
+ * @returns the catalog entry, or undefined when the route is not listed.
82
+ */
83
+ function findModel(groups, provider, model) {
84
+ const group = (groups ?? []).find((candidate) => candidate?.id === provider);
85
+ if (group === undefined) return undefined;
86
+ return (group.models ?? []).find((candidate) => candidate?.id === model);
87
+ }
88
+
89
+
90
+ /** Send a JSON response with a no-store cache policy. */
91
+ function sendJson(response, status, body) {
92
+ const payload = JSON.stringify(body);
93
+ response.writeHead(status, {
94
+ 'content-type': 'application/json; charset=utf-8',
95
+ 'cache-control': 'no-store',
96
+ 'content-length': Buffer.byteLength(payload),
97
+ });
98
+ response.end(payload);
99
+ }
100
+
101
+ /** Reject cross-origin writes to the local routes. */
102
+ function isSameOrigin(request) {
103
+ const origin = request.headers?.origin;
104
+ if (origin === undefined) return true;
105
+ try {
106
+ return new URL(origin).host === request.headers?.host;
107
+ }
108
+ catch {
109
+ return false;
110
+ }
111
+ }
112
+
113
+ /** Read and parse a bounded JSON request body. */
114
+ function readJsonBody(request, maxBytes = 65536) {
115
+ return new Promise((resolve, reject) => {
116
+ const chunks = [];
117
+ let size = 0;
118
+ let done = false;
119
+ request.on('data', (chunk) => {
120
+ if (done) return;
121
+ size += chunk.length;
122
+ if (size > maxBytes) {
123
+ done = true;
124
+ reject(new Error('request body too large'));
125
+ return;
126
+ }
127
+ chunks.push(chunk);
128
+ });
129
+ request.on('end', () => {
130
+ if (done) return;
131
+ done = true;
132
+ const text = Buffer.concat(chunks).toString('utf8');
133
+ if (text.trim() === '') return resolve({});
134
+ try { resolve(JSON.parse(text)); }
135
+ catch (error) { reject(new Error(`invalid json: ${error.message}`)); }
136
+ });
137
+ request.on('error', reject);
138
+ });
139
+ }
140
+
141
+ /**
142
+ * Pin the default model on the live service and expose management routes.
143
+ * @param ctx - Host context carrying the injected services.
144
+ * @param config - Plugin Config; its `provider`/`model` seed the first pin.
145
+ */
146
+ export function apply(ctx, config) {
147
+ const log = (level, message) => {
148
+ const logger = ctx.logger;
149
+ if (logger === undefined) return;
150
+ const write = logger[level] ?? logger.info;
151
+ if (typeof write === 'function') write.call(logger, message);
152
+ };
153
+
154
+ ctx.effect(() => {
155
+ const service = ctx.agentDefaultModel;
156
+ if (service === undefined) {
157
+ log('warn', 'pin-default-model: agentDefaultModel is unavailable; nothing to pin');
158
+ return;
159
+ }
160
+ const originalSave = service.saveSelection;
161
+ if (typeof originalSave !== 'function') {
162
+ log('warn', 'pin-default-model: saveSelection is missing; nothing to pin');
163
+ return;
164
+ }
165
+
166
+ const originalOwn = Object.prototype.hasOwnProperty.call(service, 'saveSelection');
167
+
168
+ // The live pin. `currentPin` is the single source of truth for what the
169
+ // default is allowed to be; it changes only via this plugin's set route.
170
+ let currentPin = cleanSelection({
171
+ provider: config?.provider?.get?.(),
172
+ model: config?.model?.get?.(),
173
+ reasoningEffort: config?.reasoningEffort?.get?.(),
174
+ }) ?? cleanSelection(service.currentSelection?.()) ?? { ...FALLBACK_PIN };
175
+ let restoring = false;
176
+ let settingFromRoute = false;
177
+
178
+ const pinnedSave = function pinnedSaveSelection(next) {
179
+ // A write issued by our own route or by the restore path is allowed.
180
+ if (settingFromRoute || restoring || satisfies(currentPin, next)) {
181
+ return Promise.resolve(originalSave.call(service, next));
182
+ }
183
+ log(
184
+ 'info',
185
+ `pin-default-model: ignored a default-model change to ${String(next?.provider)}/${String(next?.model)}`
186
+ + `${next?.reasoningEffort === undefined ? '' : ` (${String(next.reasoningEffort)})`};`
187
+ + ` the default stays ${currentPin.provider}/${currentPin.model}`
188
+ + `${currentPin.reasoningEffort === undefined ? '' : ` (${currentPin.reasoningEffort})`}`,
189
+ );
190
+ queueMicrotask(() => { void hold(); });
191
+ return Promise.resolve();
192
+ };
193
+
194
+ try {
195
+ service.saveSelection = pinnedSave;
196
+ }
197
+ catch (error) {
198
+ log('warn', `pin-default-model: could not replace saveSelection: ${String(error)}`);
199
+ return;
200
+ }
201
+
202
+ /** Repair the live default if anything moved it away from the pin. */
203
+ const hold = async () => {
204
+ let live;
205
+ try {
206
+ live = service.currentSelection?.();
207
+ }
208
+ catch (error) {
209
+ log('warn', `pin-default-model: cannot read the default selection: ${String(error)}`);
210
+ return;
211
+ }
212
+ if (satisfies(currentPin, live)) return;
213
+ log(
214
+ 'info',
215
+ `pin-default-model: default drifted to ${String(live?.provider)}/${String(live?.model)}`
216
+ + `${live?.reasoningEffort === undefined ? '' : ` (${String(live.reasoningEffort)})`};`
217
+ + ` restoring ${currentPin.provider}/${currentPin.model}`
218
+ + `${currentPin.reasoningEffort === undefined ? '' : ` (${currentPin.reasoningEffort})`}`,
219
+ );
220
+ restoring = true;
221
+ try {
222
+ await originalSave.call(service, currentPin);
223
+ }
224
+ catch (error) {
225
+ log('warn', `pin-default-model: restore failed: ${String(error)}`);
226
+ }
227
+ finally {
228
+ restoring = false;
229
+ }
230
+ };
231
+
232
+ /** Build the routable model catalog for the settings dropdown. */
233
+ const catalog = async () => {
234
+ const controller = ctx.sessionController;
235
+ if (typeof controller?.modelCatalog !== 'function') return { groups: [] };
236
+ try {
237
+ const result = await controller.modelCatalog();
238
+ return { groups: Array.isArray(result?.groups) ? result.groups : [] };
239
+ }
240
+ catch (error) {
241
+ log('warn', `pin-default-model: catalog failed: ${String(error)}`);
242
+ return { groups: [] };
243
+ }
244
+ };
245
+
246
+ // --- host routes -----------------------------------------------------
247
+ const routes = [];
248
+ if (typeof ctx.webServer?.register === 'function') {
249
+ routes.push(ctx.webServer.register({
250
+ kind: 'exact',
251
+ path: '/dsh-pin-model/view',
252
+ handler: async (request, response) => {
253
+ if (request.method !== 'GET') {
254
+ response.writeHead(405, { allow: 'GET' });
255
+ response.end();
256
+ return;
257
+ }
258
+ try {
259
+ const groups = await catalog();
260
+ sendJson(response, 200, { ok: true, selection: currentPin, groups: groups.groups });
261
+ }
262
+ catch (error) {
263
+ sendJson(response, 500, { ok: false, error: String(error) });
264
+ }
265
+ },
266
+ }));
267
+ routes.push(ctx.webServer.register({
268
+ kind: 'exact',
269
+ path: '/dsh-pin-model/set',
270
+ handler: async (request, response) => {
271
+ if (request.method !== 'POST') {
272
+ response.writeHead(405, { allow: 'POST' });
273
+ response.end();
274
+ return;
275
+ }
276
+ if (!isSameOrigin(request)) {
277
+ sendJson(response, 403, { ok: false, error: 'untrusted origin' });
278
+ return;
279
+ }
280
+ try {
281
+ const body = await readJsonBody(request);
282
+ const next = cleanSelection(body);
283
+ if (next === null) {
284
+ sendJson(response, 400, { ok: false, error: 'provider and model are required' });
285
+ return;
286
+ }
287
+ // DSH rejects a call whose effort the model does not support, so a
288
+ // pin that could never be used must not be stored. The route is
289
+ // checked here rather than left to fail at session creation.
290
+ if (next.reasoningEffort !== undefined) {
291
+ const groups = (await catalog()).groups;
292
+ const entry = findModel(groups, next.provider, next.model);
293
+ if (entry === undefined) {
294
+ sendJson(response, 400, {
295
+ ok: false,
296
+ error: `model "${next.provider}/${next.model}" is not in the catalog; cannot verify the reasoning effort`,
297
+ });
298
+ return;
299
+ }
300
+ const supported = (entry.reasoning?.efforts ?? []).map((effort) => effort.id);
301
+ if (!supported.includes(next.reasoningEffort)) {
302
+ sendJson(response, 400, {
303
+ ok: false,
304
+ error: `model "${next.provider}/${next.model}" does not support reasoning effort "${next.reasoningEffort}"`
305
+ + `${supported.length === 0 ? ' (it offers no reasoning effort)' : ` (supported: ${supported.join(', ')})`}`,
306
+ });
307
+ return;
308
+ }
309
+ }
310
+ currentPin = next;
311
+ settingFromRoute = true;
312
+ try {
313
+ await originalSave.call(service, next);
314
+ }
315
+ finally {
316
+ settingFromRoute = false;
317
+ }
318
+ log(
319
+ 'info',
320
+ `pin-default-model: pin changed to ${next.provider}/${next.model}`
321
+ + `${next.reasoningEffort === undefined ? '' : ` (${next.reasoningEffort})`}`,
322
+ );
323
+ sendJson(response, 200, { ok: true, selection: currentPin });
324
+ }
325
+ catch (error) {
326
+ sendJson(response, 500, { ok: false, error: String(error) });
327
+ }
328
+ },
329
+ }));
330
+ }
331
+ else {
332
+ log('warn', 'pin-default-model: webServer unavailable; settings page will be read-only');
333
+ }
334
+
335
+ const timer = setInterval(() => { void hold(); }, WATCH_INTERVAL_MS);
336
+ if (typeof timer?.unref === 'function') timer.unref();
337
+
338
+ const first = setTimeout(() => { void hold(); }, FIRST_CHECK_MS);
339
+ if (typeof first?.unref === 'function') first.unref();
340
+
341
+ log(
342
+ 'info',
343
+ `pin-default-model: holding the default at ${currentPin.provider}/${currentPin.model}`
344
+ + `${currentPin.reasoningEffort === undefined ? ' (reasoning effort follows the provider default)' : ` (${currentPin.reasoningEffort})`}`,
345
+ );
346
+
347
+ return () => {
348
+ clearInterval(timer);
349
+ clearTimeout(first);
350
+ for (const dispose of routes) dispose?.();
351
+ try {
352
+ if (originalOwn) service.saveSelection = originalSave;
353
+ else delete service.saveSelection;
354
+ }
355
+ catch (error) {
356
+ log('warn', `pin-default-model: could not restore saveSelection: ${String(error)}`);
357
+ }
358
+ log('info', 'pin-default-model: pin released');
359
+ };
360
+ }, 'dsh-pin-default-model: pinned default model');
361
+ }
package/locale/en.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "Default model pin",
4
+ "description": "Keeps the DeepSeek Harness default model fixed. Switching models inside a session still works for that session, but it no longer rewrites the default, so every newly created conversation — including one created by a scheduled task — starts on the pinned model. Change the pin in Settings → Default model pin.",
5
+ "detail": "docs/DETAIL.md"
6
+ }
7
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "默认模型锁定",
4
+ "description": "锁定 DeepSeek Harness 的默认模型。在会话内临时切换模型依然只对那个会话生效,不会改写默认值,因此每个新建对话——包括自动化任务创建的——都以锁定的模型启动。在设置 → 默认模型锁定里修改锁定值。",
5
+ "detail": "docs/DETAIL.zh.md"
6
+ }
7
+ }