dsh-think-effort 0.2.1 → 0.4.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/client.js ADDED
@@ -0,0 +1,736 @@
1
+ /**
2
+ * Browser half of `dsh-think-effort`: the visual settings page for one row.
3
+ *
4
+ * The page renders in two places, from one form:
5
+ *
6
+ * - the Plugins page's `plugins.row.config` slot, keyed by
7
+ * `<package name>#<row id>`, so the bundle's row gains a Configure control;
8
+ * - the Settings panel's `settings.section` slot, so the same options are
9
+ * reachable where a person looks for settings instead of only behind a
10
+ * bundle row.
11
+ *
12
+ * The page owner supplies `form`: the accepted Host values of this row plus one
13
+ * revision-fenced `mutate`. Every field is volatile, so saving writes the
14
+ * profile patch and the running policy follows without a restart. The Settings
15
+ * entry reads the row's settings namespace through `configForms` and exists
16
+ * only while the Host serves it, so a deployment without this row shows no
17
+ * trace of the page.
18
+ *
19
+ * The layout follows the desktop reference implementation: purpose rows
20
+ * (ordinary step, subagent, cheap calls) over a shared effort vocabulary, with
21
+ * capability declaration in an advanced block. It is plain hyperscript with
22
+ * theme tokens; nothing here imports another plugin's runtime.
23
+ */
24
+
25
+ window.__ModuleLoader__.load({
26
+ id: 'dsh-think-effort',
27
+ factory(require) {
28
+ const React = require('react');
29
+ const h = React.createElement;
30
+
31
+ const NS = 'think-effort';
32
+ /** The bundle row this page belongs to; the row id is also the Host settings namespace. */
33
+ const ROW_ID = 'think-effort';
34
+ const ROW_KEY = `dsh-think-effort#${ROW_ID}`;
35
+ /** Settings navigation section id; the shell pairs it with the icon of the same id. */
36
+ const SECTION_ID = 'think-effort';
37
+ /** Right after the built-in Plugins section (order 15): the plugin's own options. */
38
+ const SECTION_ORDER = 16;
39
+ const EFFORT_ORDER = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'];
40
+ const KNOWN_PURPOSES = ['session-title', 'compaction', 'auto-review'];
41
+
42
+ const zh = {
43
+ nav: '自动思考档位',
44
+ intro: '按“用途”分配推理强度:普通步骤、子代理、Harness 自己的廉价调用。改动即时生效,不需要重启。',
45
+ loading: '正在读取插件配置…',
46
+ unavailable: '这个部署没有提供可编辑的插件配置(设置服务未挂载,或该行没有可实时编辑的字段)。',
47
+ noForm: '这里没有拿到这个插件的配置表单;请在「插件」页打开本插件行的「配置」。',
48
+ save: '保存',
49
+ saving: '保存中…',
50
+ saved: '已保存,立即生效。',
51
+ reset: '恢复默认',
52
+ resetConfirm: '把本插件所有被覆盖的字段恢复为默认值?',
53
+ saveFailed: '保存被拒绝:可能被更高层配置覆盖,或值未通过校验。',
54
+ resetFailed: '恢复默认被拒绝。',
55
+ noChanges: '没有需要保存的改动。',
56
+ unsaved: '有未保存的改动。',
57
+ invalidPattern: '模型 ID 正则需要是合法的 JavaScript 正则。',
58
+ invalidEfforts: '至少保留一个档位。',
59
+ effortPicker: '自动思考档位',
60
+ sectionPurpose: '档位分配',
61
+ sectionPurposeHint: '每一类请求各自决定用什么强度;显式选择的具体档位不会被“普通步骤”覆盖。',
62
+ autoRow: '普通步骤 · Auto 落到',
63
+ autoRowHint: '模型选择器里的 Auto 在普通对话步骤上使用的档位。',
64
+ defaultRow: '模型默认档',
65
+ defaultRowHint: '路由声明的默认档;Auto 选“跟随默认档”时使用它,也是廉价调用降档的参照。',
66
+ autoScopeRow: '哪些模型显示 Auto',
67
+ autoScopeHint: 'all:所有模型;declared:只在本插件声明过能力的模型;off:完全不注入 Auto。',
68
+ subagentRow: '子代理 · 推理强度',
69
+ subagentRowHint: 'DSH 的子代理默认继承父代理的模型与档位。这里可以给子代理统一指定强度;“不干预”保持原生行为。恢复默认值也是“不干预”。',
70
+ sectionAux: '廉价调用',
71
+ auxSwitch: '把 Harness 自己的廉价调用降到低档',
72
+ auxSwitchHint: '会话标题、压缩、自动复核这类有界的小请求,不应该继承会话的深度。',
73
+ auxLevelRow: '廉价调用使用的档位',
74
+ auxPurposesRow: '视为廉价调用的 purpose',
75
+ auxPurposesHint: '按 LLM 请求的 purpose 匹配;勾选常见项,或在“其他”里补充。',
76
+ auxOther: '其他(逗号分隔)',
77
+ sectionCapability: '能力声明',
78
+ capabilitySwitch: '为没有推理元数据的 DeepSeek 模型声明档位',
79
+ capabilitySwitchHint: '写入 llm-pi-ai 的 reasoningEfforts 与路由默认档;已有声明绝不覆盖。',
80
+ effortsRow: '声明的档位(按强度排序)',
81
+ effortsHint: 'DeepSeek V4 原生为 low / high / max;取消勾选可移除不支持的档位。',
82
+ hostsRow: 'DeepSeek 端点后缀',
83
+ hostsHint: '逗号分隔;默认 deepseek.com。只做 host 后缀匹配,子域名也算。',
84
+ patternRow: '模型 ID 正则',
85
+ patternHint: '网关别名用,例如 ^deepseek;不区分大小写。',
86
+ declareModelsRow: '额外声明',
87
+ declareModelsHint: '每行一个 provider/model,或一个模型 ID 正则;用于没有匹配到上面两条规则的网关。',
88
+ advanced: '高级',
89
+ traceRow: '把每次决策写到 stderr',
90
+ traceHint: '排查用;默认关闭。',
91
+ enabledRow: '启用插件',
92
+ enabledHint: '总开关;关闭后不注入 Auto、不改写任何请求。',
93
+ summary: 'Auto → {auto} · 子代理 → {sub} · 廉价调用 → {aux}',
94
+ inherit: '不干预(原生行为)',
95
+ auto: 'Auto(按默认档)',
96
+ followDefault: '跟随默认档',
97
+ on: '开',
98
+ off: '关',
99
+ all: '所有模型',
100
+ declared: '仅声明过的模型',
101
+ disabled: '已关闭',
102
+ custom: '自定义',
103
+ otherLevel: '其他档位',
104
+ };
105
+
106
+ const en = {
107
+ nav: 'Automatic thinking depth',
108
+ intro: 'Assign thinking depth by purpose: ordinary steps, subagents, and the Harness\'s own cheap calls. Changes apply immediately, without a restart.',
109
+ loading: 'Reading plugin configuration…',
110
+ unavailable: 'This deployment exposes no editable plugin configuration (the settings service is not mounted, or this row has no live fields).',
111
+ noForm: 'The configuration form for this row is not available here. Open the plugin row\'s Configure control on the Plugins page.',
112
+ save: 'Save',
113
+ saving: 'Saving…',
114
+ saved: 'Saved. It applies immediately.',
115
+ reset: 'Restore defaults',
116
+ resetConfirm: 'Restore every overridden field of this plugin to its default?',
117
+ saveFailed: 'The save was refused: a higher configuration layer may override this row, or a value failed validation.',
118
+ resetFailed: 'Restoring defaults was refused.',
119
+ noChanges: 'Nothing to save.',
120
+ unsaved: 'Unsaved changes.',
121
+ invalidPattern: 'The model-id pattern must be a valid JavaScript regular expression.',
122
+ invalidEfforts: 'Keep at least one depth.',
123
+ effortPicker: 'Automatic thinking depth',
124
+ sectionPurpose: 'Depth assignment',
125
+ sectionPurposeHint: 'Each purpose chooses its own depth; an explicit pick is never overridden by the ordinary-step rule.',
126
+ autoRow: 'Ordinary step · Auto resolves to',
127
+ autoRowHint: 'The depth the model selector\'s Auto uses for ordinary conversation steps.',
128
+ defaultRow: 'Model default depth',
129
+ defaultRowHint: 'The route\'s declared default. Used when Auto follows the default, and as the reference for cheap-call downgrades.',
130
+ autoScopeRow: 'Which models offer Auto',
131
+ autoScopeHint: 'all: every model; declared: only models this plugin declared; off: do not inject Auto at all.',
132
+ subagentRow: 'Subagent · reasoning effort',
133
+ subagentRowHint: 'DSH subagents inherit the parent model and depth by default. Pin a depth for them here; "Do not intervene" keeps native behavior, and so does restoring this field.',
134
+ sectionAux: 'Cheap calls',
135
+ auxSwitch: 'Drop the Harness\'s own cheap calls to a low depth',
136
+ auxSwitchHint: 'Session titles, compaction, and auto-review are bounded auxiliary requests and should not inherit the conversation\'s depth.',
137
+ auxLevelRow: 'Depth used by cheap calls',
138
+ auxPurposesRow: 'Purposes treated as cheap calls',
139
+ auxPurposesHint: 'Matched against the LLM request purpose. Tick the common ones, or add more under "Other".',
140
+ auxOther: 'Other (comma separated)',
141
+ sectionCapability: 'Capability declaration',
142
+ capabilitySwitch: 'Declare depths for DeepSeek models that carry no reasoning metadata',
143
+ capabilitySwitchHint: 'Writes reasoningEfforts and the route default into llm-pi-ai. An existing declaration is never overwritten.',
144
+ effortsRow: 'Declared depths (escalation order)',
145
+ effortsHint: 'DeepSeek V4 declares low / high / max; untick a depth the endpoint does not support.',
146
+ hostsRow: 'DeepSeek endpoint suffixes',
147
+ hostsHint: 'Comma separated; deepseek.com by default. Matching is by host suffix, so subdomains count.',
148
+ patternRow: 'Model-id pattern',
149
+ patternHint: 'For gateway aliases, such as ^deepseek; case insensitive.',
150
+ declareModelsRow: 'Extra declarations',
151
+ declareModelsHint: 'One provider/model per line, or a model-id regular expression, for gateways the rules above miss.',
152
+ advanced: 'Advanced',
153
+ traceRow: 'Write every decision to stderr',
154
+ traceHint: 'Diagnostics only; off by default.',
155
+ enabledRow: 'Enable this plugin',
156
+ enabledHint: 'Master switch; when off, no Auto is injected and no request is rewritten.',
157
+ summary: 'Auto → {auto} · Subagent → {sub} · Cheap calls → {aux}',
158
+ inherit: 'Do not intervene (native)',
159
+ auto: 'Auto (default depth)',
160
+ followDefault: 'Follow the model default',
161
+ on: 'On',
162
+ off: 'Off',
163
+ all: 'All models',
164
+ declared: 'Only declared models',
165
+ disabled: 'Disabled',
166
+ custom: 'Custom',
167
+ otherLevel: 'Other depth',
168
+ };
169
+
170
+ const LEVEL_LABELS = {
171
+ off: 'Off', minimal: 'Minimal', low: 'Low', medium: 'Medium',
172
+ high: 'High', xhigh: 'Extra high', max: 'Max',
173
+ };
174
+
175
+ /** All fields the page edits, in display order. */
176
+ const FIELDS = [
177
+ 'enabled', 'declareCapability', 'deepseekHosts', 'deepseekModelPattern',
178
+ 'declareModels', 'efforts', 'defaultEffort', 'autoLevel', 'autoScope',
179
+ 'auxDowngrade', 'auxPurposes', 'auxLevel', 'subagentLevel', 'trace',
180
+ ];
181
+
182
+ function equal(a, b) {
183
+ return JSON.stringify(a) === JSON.stringify(b);
184
+ }
185
+
186
+ function asArray(value) {
187
+ return Array.isArray(value) ? value : [];
188
+ }
189
+
190
+ function splitList(text) {
191
+ return String(text ?? '')
192
+ .split(/[\n,,;;]+/)
193
+ .map((item) => item.trim())
194
+ .filter((item, index, all) => item !== '' && all.indexOf(item) === index);
195
+ }
196
+
197
+ /** Whether a string is a usable regular expression for model-id matching. */
198
+ function validPattern(text) {
199
+ try {
200
+ new RegExp(String(text ?? ''));
201
+ return true;
202
+ } catch {
203
+ return false;
204
+ }
205
+ }
206
+
207
+ /** Depth vocabulary of a draft, escalation-ordered, without auto/off. */
208
+ function depthLevels(draft) {
209
+ const declared = asArray(draft?.efforts).filter((id) => typeof id === 'string' && id !== 'auto' && id !== 'off');
210
+ const extra = declared.filter((id) => !EFFORT_ORDER.includes(id));
211
+ return [...EFFORT_ORDER.filter((id) => declared.includes(id)), ...extra];
212
+ }
213
+
214
+ /** Depth Auto resolves to for the draft. */
215
+ function autoDepth(draft) {
216
+ if (!draft || draft.autoLevel === 'model-default' || draft.autoLevel === undefined) return draft?.defaultEffort;
217
+ return draft.autoLevel;
218
+ }
219
+
220
+ function levelLabel(level) {
221
+ if (level === 'inherit') return null;
222
+ return LEVEL_LABELS[level] ?? level;
223
+ }
224
+
225
+ function styleText() {
226
+ return `
227
+ .dte-page { display: flex; flex-direction: column; gap: 18px; color: var(--dsw-alias-label-primary); font-size: 13px; line-height: 1.55; }
228
+ .dte-intro { margin: 0; color: var(--dsw-alias-label-secondary); max-width: 72ch; }
229
+ .dte-summary { display: flex; flex-wrap: wrap; gap: 8px; }
230
+ .dte-pill { border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-2); border-radius: 999px; padding: 3px 10px; font-size: 12px; color: var(--dsw-alias-label-secondary); }
231
+ .dte-pill b { color: var(--dsw-alias-label-primary); font-weight: 600; }
232
+ .dte-section { border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-1); border-radius: 10px; overflow: hidden; }
233
+ .dte-section-head { padding: 12px 14px; border-bottom: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-2); }
234
+ .dte-section-title { margin: 0; font-size: 13px; font-weight: 600; }
235
+ .dte-section-hint { margin: 3px 0 0; font-size: 12px; color: var(--dsw-alias-label-secondary); }
236
+ .dte-row { display: grid; grid-template-columns: minmax(160px, 220px) minmax(0, 1fr); gap: 12px 18px; padding: 12px 14px; border-top: 1px solid var(--dsw-alias-border-l1); align-items: start; }
237
+ .dte-row:first-child { border-top: 0; }
238
+ .dte-row-label { padding-top: 5px; font-weight: 500; }
239
+ .dte-row-hint { grid-column: 2; margin: -4px 0 0; font-size: 12px; color: var(--dsw-alias-label-secondary); }
240
+ .dte-control { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
241
+ .dte-inline { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
242
+ .dte-input, .dte-select, .dte-textarea {
243
+ box-sizing: border-box; width: 100%; max-width: 520px; padding: 6px 9px;
244
+ border: 1px solid var(--dsw-alias-border-l1); border-radius: 7px;
245
+ background: var(--dsw-alias-bg-base); color: var(--dsw-alias-label-primary);
246
+ font: inherit; outline: none;
247
+ }
248
+ .dte-input:focus, .dte-select:focus, .dte-textarea:focus { border-color: var(--dsw-alias-brand-primary); }
249
+ .dte-textarea { min-height: 68px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
250
+ .dte-chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--dsw-alias-border-l1); border-radius: 999px; padding: 3px 10px; cursor: pointer; user-select: none; background: var(--dsw-alias-bg-base); }
251
+ .dte-chip-on { border-color: var(--dsw-alias-brand-primary); background: color-mix(in srgb, var(--dsw-alias-brand-primary) 12%, transparent); }
252
+ .dte-chip input { margin: 0; }
253
+ .dte-switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
254
+ .dte-switch input { width: 15px; height: 15px; margin: 0; accent-color: var(--dsw-alias-brand-primary); }
255
+ .dte-footer { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; position: sticky; bottom: 0; margin-top: 2px; padding: 12px 0 4px; border-top: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-base); }
256
+ .dte-btn { border: 1px solid var(--dsw-alias-border-l1); border-radius: 8px; padding: 6px 16px; font: inherit; cursor: pointer; background: var(--dsw-alias-bg-layer-1); color: var(--dsw-alias-label-primary); }
257
+ .dte-btn:disabled { opacity: .55; cursor: default; }
258
+ .dte-btn-primary { border-color: var(--dsw-alias-brand-primary); background: var(--dsw-alias-brand-primary); color: #fff; }
259
+ .dte-status { font-size: 12px; }
260
+ .dte-status-ok { color: var(--dsw-alias-state-success-primary); }
261
+ .dte-status-error { color: var(--dsw-alias-state-error-primary); }
262
+ .dte-status-muted { color: var(--dsw-alias-label-secondary); }
263
+ .dte-details { border: 1px solid var(--dsw-alias-border-l1); border-radius: 10px; background: var(--dsw-alias-bg-layer-1); }
264
+ .dte-details > summary { cursor: pointer; padding: 11px 14px; font-weight: 600; }
265
+ .dte-details[open] > summary { border-bottom: 1px solid var(--dsw-alias-border-l1); }
266
+ .dte-details .dte-row:first-of-type { border-top: 0; }
267
+ @media (max-width: 720px) {
268
+ .dte-row { grid-template-columns: minmax(0, 1fr); gap: 6px; }
269
+ .dte-row-hint { grid-column: 1; }
270
+ .dte-row-label { padding-top: 0; }
271
+ }
272
+ `;
273
+ }
274
+
275
+ function Section({ title, hint, children }) {
276
+ return h('section', { className: 'dte-section' },
277
+ h('div', { className: 'dte-section-head' },
278
+ h('h3', { className: 'dte-section-title' }, title),
279
+ hint ? h('p', { className: 'dte-section-hint' }, hint) : null),
280
+ children);
281
+ }
282
+
283
+ function Row({ key, label, hint, children }) {
284
+ return h('div', { key, className: 'dte-row' },
285
+ h('div', { className: 'dte-row-label' }, label),
286
+ h('div', { className: 'dte-control' }, children),
287
+ hint ? h('p', { className: 'dte-row-hint' }, hint) : null);
288
+ }
289
+
290
+ function Switch({ checked, onChange, label, disabled }) {
291
+ return h('label', { className: 'dte-switch' },
292
+ h('input', { type: 'checkbox', checked: checked === true, disabled, onChange: (event) => onChange(event.target.checked) }),
293
+ h('span', null, label));
294
+ }
295
+
296
+ function levelOptions(draft, includeAuto) {
297
+ const levels = depthLevels(draft);
298
+ return levels.map((id) => ({ value: id, label: LEVEL_LABELS[id] ?? id }));
299
+ }
300
+
301
+ function Select({ value, options, onChange, disabled }) {
302
+ const known = options.some((option) => option.value === value);
303
+ return h('select', { className: 'dte-select', value: value ?? '', disabled, onChange: (event) => onChange(event.target.value) },
304
+ !known && value !== undefined && value !== '' ? h('option', { value, disabled: true }, String(value)) : null,
305
+ options.map((option) => h('option', { key: option.value, value: option.value, disabled: option.disabled }, option.label)));
306
+ }
307
+
308
+ function Page({ form, t }) {
309
+ const state = form?.state;
310
+ const value = state?.value ?? {};
311
+ const [draft, setDraft] = React.useState(null);
312
+ const [dirty, setDirty] = React.useState(false);
313
+ const [busy, setBusy] = React.useState(false);
314
+ const [message, setMessage] = React.useState(null);
315
+ const serialized = JSON.stringify(value);
316
+
317
+ React.useEffect(() => {
318
+ if (dirty) return;
319
+ setDraft(JSON.parse(serialized));
320
+ }, [serialized, dirty]);
321
+
322
+ const current = draft ?? value;
323
+ const levels = depthLevels(current);
324
+ const levelChoices = levels.length > 0 ? levels : ['low', 'high', 'max'];
325
+ const auto = autoDepth(current) ?? '(adapter default)';
326
+ const sub = current.subagentLevel === 'inherit' || current.subagentLevel === undefined
327
+ ? t('inherit')
328
+ : current.subagentLevel === 'auto' ? t('auto') : levelLabel(current.subagentLevel);
329
+ const aux = current.auxDowngrade === false ? t('off') : (levelLabel(current.auxLevel) ?? current.auxLevel);
330
+
331
+ if (form === undefined) {
332
+ return h('div', { className: 'dte-page' }, h('style', null, styleText()),
333
+ h('p', { className: 'dte-intro' }, t('noForm')));
334
+ }
335
+ if (state?.status === 'loading') {
336
+ return h('div', { className: 'dte-page' }, h('style', null, styleText()),
337
+ h('p', { className: 'dte-intro' }, t('loading')));
338
+ }
339
+ if (state?.status === 'unavailable') {
340
+ return h('div', { className: 'dte-page' }, h('style', null, styleText()),
341
+ h('p', { className: 'dte-intro' }, t('unavailable')));
342
+ }
343
+
344
+ const patch = (field, next) => {
345
+ setDirty(true);
346
+ setMessage(null);
347
+ setDraft({ ...(draft ?? value), [field]: next });
348
+ };
349
+
350
+ const validate = () => {
351
+ if (asArray(current.efforts).length === 0) return t('invalidEfforts');
352
+ if (!validPattern(current.deepseekModelPattern)) return t('invalidPattern');
353
+ for (const entry of asArray(current.declareModels)) {
354
+ if (entry.includes('/')) continue;
355
+ if (!validPattern(entry)) return t('invalidPattern');
356
+ }
357
+ return null;
358
+ };
359
+
360
+ const save = async () => {
361
+ const invalid = validate();
362
+ if (invalid !== null) {
363
+ setMessage({ kind: 'error', text: invalid });
364
+ return;
365
+ }
366
+ const ops = [];
367
+ for (const field of FIELDS) {
368
+ if (!equal(current[field], value[field])) {
369
+ ops.push({ op: 'set', path: [field], value: current[field] });
370
+ }
371
+ }
372
+ if (ops.length === 0) {
373
+ setMessage({ kind: 'muted', text: t('noChanges') });
374
+ return;
375
+ }
376
+ setBusy(true);
377
+ try {
378
+ const accepted = await form.mutate(ops, state.revision);
379
+ if (accepted) {
380
+ setDirty(false);
381
+ setDraft(null);
382
+ setMessage({ kind: 'ok', text: t('saved') });
383
+ } else {
384
+ setMessage({ kind: 'error', text: t('saveFailed') });
385
+ }
386
+ } catch (error) {
387
+ setMessage({ kind: 'error', text: `${t('saveFailed')} ${error instanceof Error ? error.message : String(error)}` });
388
+ } finally {
389
+ setBusy(false);
390
+ }
391
+ };
392
+
393
+ const reset = async () => {
394
+ if (typeof window !== 'undefined' && typeof window.confirm === 'function' && !window.confirm(t('resetConfirm'))) return;
395
+ const user = state.user !== null && typeof state.user === 'object' ? state.user : {};
396
+ const ops = FIELDS.filter((field) => Object.prototype.hasOwnProperty.call(user, field))
397
+ .map((field) => ({ op: 'unset', path: [field] }));
398
+ if (ops.length === 0) {
399
+ setMessage({ kind: 'muted', text: t('noChanges') });
400
+ return;
401
+ }
402
+ setBusy(true);
403
+ try {
404
+ const accepted = await form.mutate(ops, state.revision);
405
+ if (accepted) {
406
+ setDirty(false);
407
+ setDraft(null);
408
+ setMessage({ kind: 'ok', text: t('saved') });
409
+ } else {
410
+ setMessage({ kind: 'error', text: t('resetFailed') });
411
+ }
412
+ } catch (error) {
413
+ setMessage({ kind: 'error', text: `${t('resetFailed')} ${error instanceof Error ? error.message : String(error)}` });
414
+ } finally {
415
+ setBusy(false);
416
+ }
417
+ };
418
+
419
+ const toggleEffort = (level) => {
420
+ const declared = asArray(current.efforts);
421
+ const next = declared.includes(level)
422
+ ? declared.filter((id) => id !== level)
423
+ : [...declared, level].sort((a, b) => {
424
+ const ia = EFFORT_ORDER.indexOf(a);
425
+ const ib = EFFORT_ORDER.indexOf(b);
426
+ return (ia < 0 ? 99 : ia) - (ib < 0 ? 99 : ib);
427
+ });
428
+ patch('efforts', next);
429
+ };
430
+
431
+ const togglePurpose = (purpose) => {
432
+ const purposes = asArray(current.auxPurposes);
433
+ patch('auxPurposes', purposes.includes(purpose)
434
+ ? purposes.filter((item) => item !== purpose)
435
+ : [...purposes, purpose]);
436
+ };
437
+
438
+ const customPurposes = asArray(current.auxPurposes).filter((purpose) => !KNOWN_PURPOSES.includes(purpose));
439
+
440
+ const autoOptions = [
441
+ { value: 'model-default', label: t('followDefault') },
442
+ ...levelChoices.map((id) => ({ value: id, label: LEVEL_LABELS[id] ?? id })),
443
+ ];
444
+ const subagentOptions = [
445
+ { value: 'inherit', label: t('inherit') },
446
+ { value: 'auto', label: t('auto') },
447
+ ...levelChoices.map((id) => ({ value: id, label: LEVEL_LABELS[id] ?? id })),
448
+ ];
449
+ const scopeOptions = [
450
+ { value: 'all', label: t('all') },
451
+ { value: 'declared', label: t('declared') },
452
+ { value: 'off', label: t('off') },
453
+ ];
454
+ const levelSelect = (field) => Select({
455
+ value: current[field],
456
+ options: levelChoices.map((id) => ({ value: id, label: LEVEL_LABELS[id] ?? id })),
457
+ onChange: (next) => patch(field, next),
458
+ disabled: busy,
459
+ });
460
+
461
+ // DeepSeek V4's native vocabulary first, then any extra level this row
462
+ // already declares; never offer a standard level the endpoint lacks.
463
+ const declaredEfforts = [...new Set(['low', 'high', 'max', ...asArray(current.efforts)])];
464
+
465
+ return h('div', { className: 'dte-page' },
466
+ h('style', null, styleText()),
467
+ h('p', { className: 'dte-intro' }, t('intro')),
468
+ h('div', { className: 'dte-summary' },
469
+ h('span', { className: 'dte-pill' }, t('autoRow'), ' ', h('b', null, auto)),
470
+ h('span', { className: 'dte-pill' }, t('subagentRow'), ' ', h('b', null, sub)),
471
+ h('span', { className: 'dte-pill' }, t('sectionAux'), ' ', h('b', null, aux))),
472
+ Section({
473
+ title: t('sectionPurpose'),
474
+ hint: t('sectionPurposeHint'),
475
+ children: [
476
+ Row({
477
+ key: 'enabled',
478
+ label: t('enabledRow'),
479
+ hint: t('enabledHint'),
480
+ children: Switch({ checked: current.enabled !== false, disabled: busy, label: current.enabled === false ? t('off') : t('on'), onChange: (next) => patch('enabled', next) }),
481
+ }),
482
+ Row({
483
+ key: 'auto',
484
+ label: t('autoRow'),
485
+ hint: t('autoRowHint'),
486
+ children: Select({ value: current.autoLevel ?? 'model-default', options: autoOptions, disabled: busy, onChange: (next) => patch('autoLevel', next) }),
487
+ }),
488
+ Row({
489
+ key: 'default',
490
+ label: t('defaultRow'),
491
+ hint: t('defaultRowHint'),
492
+ children: Select({ value: current.defaultEffort, options: levelChoices.map((id) => ({ value: id, label: LEVEL_LABELS[id] ?? id })), disabled: busy, onChange: (next) => patch('defaultEffort', next) }),
493
+ }),
494
+ Row({
495
+ key: 'subagent',
496
+ label: t('subagentRow'),
497
+ hint: t('subagentRowHint'),
498
+ children: Select({ value: current.subagentLevel ?? 'inherit', options: subagentOptions, disabled: busy, onChange: (next) => patch('subagentLevel', next) }),
499
+ }),
500
+ Row({
501
+ key: 'scope',
502
+ label: t('autoScopeRow'),
503
+ hint: t('autoScopeHint'),
504
+ children: Select({ value: current.autoScope ?? 'all', options: scopeOptions, disabled: busy, onChange: (next) => patch('autoScope', next) }),
505
+ }),
506
+ ],
507
+ }),
508
+ Section({
509
+ title: t('sectionAux'),
510
+ children: [
511
+ Row({
512
+ key: 'auxSwitch',
513
+ label: t('auxSwitch'),
514
+ hint: t('auxSwitchHint'),
515
+ children: Switch({ checked: current.auxDowngrade !== false, disabled: busy, label: current.auxDowngrade === false ? t('off') : t('on'), onChange: (next) => patch('auxDowngrade', next) }),
516
+ }),
517
+ Row({
518
+ key: 'auxLevel',
519
+ label: t('auxLevelRow'),
520
+ children: levelSelect('auxLevel'),
521
+ }),
522
+ Row({
523
+ key: 'auxPurposes',
524
+ label: t('auxPurposesRow'),
525
+ hint: t('auxPurposesHint'),
526
+ children: [
527
+ h('div', { key: 'known', className: 'dte-inline' },
528
+ KNOWN_PURPOSES.map((purpose) => h('label', {
529
+ key: purpose,
530
+ className: 'dte-chip' + (asArray(current.auxPurposes).includes(purpose) ? ' dte-chip-on' : ''),
531
+ },
532
+ h('input', {
533
+ type: 'checkbox',
534
+ checked: asArray(current.auxPurposes).includes(purpose),
535
+ disabled: busy,
536
+ onChange: () => togglePurpose(purpose),
537
+ }),
538
+ h('code', null, purpose)))),
539
+ h('input', {
540
+ key: 'other',
541
+ className: 'dte-input',
542
+ placeholder: t('auxOther'),
543
+ value: customPurposes.join(', '),
544
+ disabled: busy,
545
+ onChange: (event) => {
546
+ const known = asArray(current.auxPurposes).filter((purpose) => KNOWN_PURPOSES.includes(purpose));
547
+ patch('auxPurposes', [...known, ...splitList(event.target.value)]);
548
+ },
549
+ }),
550
+ ],
551
+ }),
552
+ ],
553
+ }),
554
+ Section({
555
+ title: t('sectionCapability'),
556
+ children: [
557
+ Row({
558
+ key: 'declare',
559
+ label: t('capabilitySwitch'),
560
+ hint: t('capabilitySwitchHint'),
561
+ children: Switch({ checked: current.declareCapability !== false, disabled: busy, label: current.declareCapability === false ? t('off') : t('on'), onChange: (next) => patch('declareCapability', next) }),
562
+ }),
563
+ Row({
564
+ key: 'efforts',
565
+ label: t('effortsRow'),
566
+ hint: t('effortsHint'),
567
+ children: h('div', { className: 'dte-inline' },
568
+ declaredEfforts.map((level) => h('label', {
569
+ key: level,
570
+ className: 'dte-chip' + (asArray(current.efforts).includes(level) ? ' dte-chip-on' : ''),
571
+ },
572
+ h('input', {
573
+ type: 'checkbox',
574
+ checked: asArray(current.efforts).includes(level),
575
+ disabled: busy,
576
+ onChange: () => toggleEffort(level),
577
+ }),
578
+ LEVEL_LABELS[level] ?? level))),
579
+ }),
580
+ Row({
581
+ key: 'hosts',
582
+ label: t('hostsRow'),
583
+ hint: t('hostsHint'),
584
+ children: h('input', {
585
+ className: 'dte-input',
586
+ value: asArray(current.deepseekHosts).join(', '),
587
+ disabled: busy,
588
+ placeholder: 'deepseek.com',
589
+ onChange: (event) => patch('deepseekHosts', splitList(event.target.value)),
590
+ }),
591
+ }),
592
+ Row({
593
+ key: 'pattern',
594
+ label: t('patternRow'),
595
+ hint: t('patternHint'),
596
+ children: h('input', {
597
+ className: 'dte-input',
598
+ value: current.deepseekModelPattern ?? '',
599
+ disabled: busy,
600
+ placeholder: '^deepseek',
601
+ onChange: (event) => patch('deepseekModelPattern', event.target.value),
602
+ }),
603
+ }),
604
+ Row({
605
+ key: 'declareModels',
606
+ label: t('declareModelsRow'),
607
+ hint: t('declareModelsHint'),
608
+ children: h('textarea', {
609
+ className: 'dte-textarea',
610
+ value: asArray(current.declareModels).join('\n'),
611
+ disabled: busy,
612
+ placeholder: 'my-gateway/deepseek-v4.1-flash-1017\n^deepseek-',
613
+ onChange: (event) => patch('declareModels', splitList(event.target.value)),
614
+ }),
615
+ }),
616
+ ],
617
+ }),
618
+ h('details', { className: 'dte-details' },
619
+ h('summary', null, t('advanced')),
620
+ Row({
621
+ key: 'trace',
622
+ label: t('traceRow'),
623
+ hint: t('traceHint'),
624
+ children: Switch({ checked: current.trace === true, disabled: busy, label: current.trace === true ? t('on') : t('off'), onChange: (next) => patch('trace', next) }),
625
+ })),
626
+ h('div', { className: 'dte-footer' },
627
+ h('button', { className: 'dte-btn dte-btn-primary', disabled: busy || !dirty, onClick: () => void save() }, busy ? t('saving') : t('save')),
628
+ h('button', { className: 'dte-btn', disabled: busy, onClick: () => void reset() }, t('reset')),
629
+ message !== null
630
+ ? h('span', { className: `dte-status dte-status-${message.kind}` }, message.text)
631
+ : h('span', { className: 'dte-status dte-status-muted' }, dirty ? t('unsaved') : '')));
632
+ }
633
+
634
+ function Summary({ form, t }) {
635
+ const value = form?.state?.value ?? {};
636
+ const auto = autoDepth(value) ?? '—';
637
+ const sub = value.subagentLevel === 'inherit' || value.subagentLevel === undefined
638
+ ? t('inherit')
639
+ : value.subagentLevel === 'auto' ? t('auto') : levelLabel(value.subagentLevel);
640
+ const aux = value.auxDowngrade === false ? t('off') : (levelLabel(value.auxLevel) ?? value.auxLevel ?? '—');
641
+ return t('summary', { auto, sub, aux });
642
+ }
643
+
644
+ /**
645
+ * The Settings panel's copy of the page.
646
+ *
647
+ * The Plugins page receives `form` from its row owner; a Settings section
648
+ * is owned by the shell and receives only `close`, so this copy reads the
649
+ * row's settings form from the `configForms` service. Both surfaces share
650
+ * that one form, so an edit belongs to the same profile write either way.
651
+ */
652
+ function SettingsSection({ useConfig, mutate, t }) {
653
+ const state = useConfig((snapshot) => snapshot);
654
+ const form = React.useMemo(() => ({ state, mutate }), [state, mutate]);
655
+ return Page({ form, t });
656
+ }
657
+
658
+ /** Settings navigation glyph: a gauge arc with a needle, matching the bundle icon's motif. */
659
+ function SectionIcon({ size }) {
660
+ return h('svg', {
661
+ viewBox: '0 0 24 24',
662
+ width: size,
663
+ height: size,
664
+ fill: 'none',
665
+ stroke: 'currentColor',
666
+ strokeWidth: 1.8,
667
+ strokeLinecap: 'round',
668
+ 'aria-hidden': 'true',
669
+ },
670
+ h('path', { d: 'M4.5 16.5a7.5 7.5 0 1 1 15 0' }),
671
+ h('path', { d: 'M12 16.5 16 11.5' }),
672
+ h('circle', { cx: 12, cy: 16.5, r: 1.6, fill: 'currentColor', stroke: 'none' }));
673
+ }
674
+
675
+ function apply(ctx) {
676
+ const locale = ctx.get('locale');
677
+ if (locale !== undefined) {
678
+ ctx.effect(() => {
679
+ const disposers = [];
680
+ try {
681
+ disposers.push(locale.register(NS, 'zh', zh), locale.register(NS, 'en', en));
682
+ } catch {
683
+ /* a locale registration failure must not hide the page */
684
+ }
685
+ return () => {
686
+ for (const dispose of disposers) dispose();
687
+ };
688
+ }, 'think-effort: dictionaries');
689
+ }
690
+ const t = locale?.bind(NS) ?? ((key) => key);
691
+ const slots = ctx.get('slots');
692
+ if (slots === undefined) return;
693
+ slots.inject('plugins.row.config', () => slots.register({
694
+ name: 'plugins.row.config',
695
+ key: ROW_KEY,
696
+ }, (props) => props.view === 'summary'
697
+ ? Summary({ form: props.form, t })
698
+ : Page({ form: props.form, t })));
699
+ // The same form under Settings: a bundle row alone is too hidden a home
700
+ // for a plugin's options. The entry exists only while the Host serves
701
+ // this row's settings namespace, so a profile without the row (or one
702
+ // whose row exposes no volatile fields) shows no trace of the page.
703
+ ctx.effect(() => ctx.configForms.whileServed([ROW_ID], () => {
704
+ const form = ctx.configForms.get(ROW_ID);
705
+ const offSection = slots.inject('settings.section', () => slots.register({
706
+ name: 'settings.section',
707
+ id: SECTION_ID,
708
+ order: SECTION_ORDER,
709
+ label: () => t('nav'),
710
+ locale: NS,
711
+ inject: () => ({
712
+ mutate: (ops, revision) => form.mutate(ops, revision),
713
+ hooks: {
714
+ config: {
715
+ getSnapshot: () => form.getSnapshot(),
716
+ subscribe: (listener) => form.subscribe(listener),
717
+ },
718
+ },
719
+ t,
720
+ }),
721
+ }, SettingsSection));
722
+ const offIcon = slots.inject('settings.section.icon', () => slots.register({
723
+ name: 'settings.section.icon',
724
+ id: SECTION_ID,
725
+ locale: NS,
726
+ }, SectionIcon));
727
+ return () => {
728
+ offSection();
729
+ offIcon();
730
+ };
731
+ }), 'think-effort: settings entry');
732
+ }
733
+
734
+ return { inject: ['slots', 'locale', 'configForms'], apply };
735
+ },
736
+ });