dsh-thinking-lang 1.0.6 → 1.0.8
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/DEVELOPMENT.md +177 -0
- package/README.md +34 -174
- package/assets/plugin-config.png +0 -0
- package/client/client.js +305 -54
- package/client/client.raw.cjs +305 -54
- package/lib/core.d.ts +41 -5
- package/lib/core.js +67 -9
- package/lib/index.d.ts +1 -0
- package/lib/index.js +89 -20
- package/package.json +2 -1
- package/src/client/index.ts +32 -1
- package/src/client/locales.ts +54 -61
- package/src/client/panel.ts +303 -72
- package/src/core.ts +89 -10
- package/src/index.ts +96 -20
package/src/client/panel.ts
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Settings UI: the General row and the plugin's configuration card.
|
|
3
|
+
* 设置界面:「通用」页的设置行,以及插件详情页的配置卡片。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* 语言设置项下方。
|
|
5
|
+
* Both surfaces bind to the same host settings document, so a change in either
|
|
6
|
+
* place is visible in the other. / 两个入口绑定同一份宿主设置文档,任一处修改
|
|
7
|
+
* 另一处都会看到。
|
|
9
8
|
*
|
|
10
|
-
* The row owns no state of its own: it reads the
|
|
11
|
-
*
|
|
12
|
-
* 本行不持有自己的状态:它读取宿主的设置文档、订阅其变化,并回写那唯一一个字段。
|
|
9
|
+
* The row owns no state of its own: it reads the document, subscribes to it, and
|
|
10
|
+
* writes the fields back. / 控件不持有自己的状态:读取文档、订阅变化、回写字段。
|
|
13
11
|
*/
|
|
14
12
|
import { createElement as h, useEffect, useState } from "react";
|
|
15
13
|
import {
|
|
16
14
|
AUTO,
|
|
17
15
|
CATALOGUE,
|
|
18
16
|
DEFAULTS,
|
|
17
|
+
FIELD_EXTRA,
|
|
19
18
|
FIELD_LANGUAGE,
|
|
19
|
+
FIELD_REMINDER,
|
|
20
|
+
MAX_REMINDER_MINUTES,
|
|
21
|
+
REMINDER_EVERY_TURN,
|
|
22
|
+
REMINDER_OFF,
|
|
20
23
|
type ThinkingSettings,
|
|
21
24
|
nativeNameFor,
|
|
22
25
|
normalizeSettings
|
|
23
26
|
} from "../core.js";
|
|
24
|
-
import {
|
|
27
|
+
import type { Dict } from "./locales.js";
|
|
25
28
|
|
|
26
29
|
/** The settings document handle the host hands to a client plugin. / 宿主交给客户端插件的设置文档句柄。 */
|
|
27
30
|
export interface SettingsDoc {
|
|
@@ -31,7 +34,7 @@ export interface SettingsDoc {
|
|
|
31
34
|
unset(field: string): unknown;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
/** UI primitives this
|
|
37
|
+
/** UI primitives this plugin can use when the host provides them. / 宿主提供时本插件可用的 UI 原语。 */
|
|
35
38
|
export interface UiPrimitives {
|
|
36
39
|
Menu?: unknown;
|
|
37
40
|
IconChevronDownOutlineRegular?: unknown;
|
|
@@ -39,14 +42,17 @@ export interface UiPrimitives {
|
|
|
39
42
|
[key: string]: unknown;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
/** What
|
|
43
|
-
interface
|
|
45
|
+
/** What both surfaces receive from their slot. / 两个界面从插槽收到的内容。 */
|
|
46
|
+
interface Binding {
|
|
44
47
|
/** Live settings document for the plugin namespace. / 插件命名空间的实时设置文档。 */
|
|
45
48
|
doc: SettingsDoc;
|
|
46
|
-
/** Bound translator for the host locale namespace
|
|
49
|
+
/** Bound translator for the host locale namespace. / 绑定到宿主 locale 命名空间的翻译函数。 */
|
|
47
50
|
t: (key: keyof Dict) => string;
|
|
48
51
|
}
|
|
49
52
|
|
|
53
|
+
/** Preset reminder intervals in minutes. / 预设提醒间隔(分钟)。 */
|
|
54
|
+
const PRESET_MINUTES = [10, 30, 60] as const;
|
|
55
|
+
|
|
50
56
|
/**
|
|
51
57
|
* Read and normalize the current settings, falling back to defaults on refusal.
|
|
52
58
|
* 读取并归一化当前设置;被拒绝时回退到默认值。
|
|
@@ -60,12 +66,8 @@ function readSettings(doc: SettingsDoc): ThinkingSettings {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
63
|
-
* Write one field back, swallowing a refusal so a read-only host cannot break the
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* A host may return a promise instead of a value; an unhandled rejection from a
|
|
67
|
-
* read-only document is not worth surfacing, so it is discarded here.
|
|
68
|
-
* 宿主可能返回 promise 而不是值;只读文档产生的未处理拒绝不值得上抛,因此在此丢弃。
|
|
69
|
+
* Write one field back, swallowing a refusal so a read-only host cannot break the UI.
|
|
70
|
+
* 回写单个字段,并吞掉拒绝异常,使只读宿主不会让控件崩溃。
|
|
69
71
|
*/
|
|
70
72
|
function writeField(doc: SettingsDoc, field: string, value: unknown): void {
|
|
71
73
|
try {
|
|
@@ -78,53 +80,42 @@ function writeField(doc: SettingsDoc, field: string, value: unknown): void {
|
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
/** Subscribe one component to the document and re-read it on every revision. / 订阅设置文档,并在每次修订时重新读取。 */
|
|
84
|
+
function useSettings(doc: SettingsDoc): ThinkingSettings {
|
|
85
|
+
const [settings, setSettings] = useState<ThinkingSettings>(() => readSettings(doc));
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const sync = (): void => setSettings(readSettings(doc));
|
|
88
|
+
sync();
|
|
89
|
+
return doc.subscribe(sync);
|
|
90
|
+
}, [doc]);
|
|
91
|
+
return settings;
|
|
92
|
+
}
|
|
93
|
+
|
|
81
94
|
/**
|
|
82
|
-
* Build the
|
|
83
|
-
* 构建绑定到宿主 UI
|
|
95
|
+
* Build the language picker bound to the host's UI primitives.
|
|
96
|
+
* 构建绑定到宿主 UI 原语的语言选择器。
|
|
84
97
|
*/
|
|
85
|
-
|
|
98
|
+
function createLanguagePicker(primitives: UiPrimitives | undefined) {
|
|
86
99
|
const Menu = primitives?.Menu;
|
|
87
100
|
const Chevron =
|
|
88
101
|
primitives?.IconChevronDownOutlineRegular ??
|
|
89
102
|
primitives?.IconChevronDownOutline14 ??
|
|
90
103
|
primitives?.IconChevronDownOutlineMedium;
|
|
91
104
|
|
|
92
|
-
return function
|
|
93
|
-
const { doc, t
|
|
94
|
-
const [settings, setSettings] = useState<ThinkingSettings>(() => readSettings(doc));
|
|
105
|
+
return function LanguagePicker(props: Binding & { settings: ThinkingSettings }): unknown {
|
|
106
|
+
const { doc, t, settings } = props;
|
|
95
107
|
const [open, setOpen] = useState<boolean>(false);
|
|
96
|
-
|
|
97
|
-
// Copy follows the THINKING language only while the feature flag is on;
|
|
98
|
-
// otherwise the host dictionary — resolved against the interface language —
|
|
99
|
-
// is used throughout. `auto` always defers to the host.
|
|
100
|
-
// 只有功能开关打开时,文案才跟随**思考语言**;否则一律使用宿主字典(按界面
|
|
101
|
-
// 语言解析)。`auto` 始终交给宿主。
|
|
102
|
-
const dict = dictionaryFor(settings.language, AUTO);
|
|
103
|
-
const t = (key: keyof Dict): string => (dict ? dict[key] : hostT(key));
|
|
104
|
-
|
|
105
|
-
// Re-read on every document revision; the initial call covers the gap
|
|
106
|
-
// between the first render and the subscription.
|
|
107
|
-
// 每次文档修订都重新读取;首次调用用于覆盖「首次渲染」到「订阅生效」之间的空档。
|
|
108
|
-
useEffect(() => {
|
|
109
|
-
const sync = () => setSettings(readSettings(doc));
|
|
110
|
-
sync();
|
|
111
|
-
return doc.subscribe(sync);
|
|
112
|
-
}, [doc]);
|
|
113
|
-
|
|
114
108
|
const items = [
|
|
115
109
|
{ id: AUTO, label: t("autoLabel") },
|
|
116
110
|
...CATALOGUE.map((language) => ({ id: language.tag, label: language.nativeName }))
|
|
117
111
|
];
|
|
118
112
|
const activeLabel =
|
|
119
113
|
settings.language === AUTO ? t("autoLabel") : nativeNameFor(settings.language) ?? settings.language;
|
|
120
|
-
|
|
121
114
|
const close = (): void => setOpen(false);
|
|
122
|
-
const toggle = (): void => setOpen((value: boolean) => !value);
|
|
123
115
|
const choose = (id: string): void => {
|
|
124
116
|
writeField(doc, FIELD_LANGUAGE, id);
|
|
125
117
|
close();
|
|
126
118
|
};
|
|
127
|
-
|
|
128
119
|
const picker = h(
|
|
129
120
|
"button",
|
|
130
121
|
{
|
|
@@ -132,75 +123,315 @@ export function createRow(primitives: UiPrimitives | undefined): (props: RowBind
|
|
|
132
123
|
className: "dsh_desk_lang_picker",
|
|
133
124
|
"aria-haspopup": "menu",
|
|
134
125
|
"aria-expanded": open,
|
|
135
|
-
onClick:
|
|
126
|
+
onClick: (): void => setOpen((value: boolean) => !value)
|
|
136
127
|
},
|
|
137
128
|
activeLabel,
|
|
138
129
|
Chevron === undefined ? null : h(Chevron, { className: "dsh_desk_lang_chevron" })
|
|
139
130
|
);
|
|
131
|
+
if (!Menu) return picker;
|
|
132
|
+
return h(Menu, {
|
|
133
|
+
open,
|
|
134
|
+
anchor: picker,
|
|
135
|
+
items,
|
|
136
|
+
selectedId: settings.language,
|
|
137
|
+
align: "end",
|
|
138
|
+
portal: true,
|
|
139
|
+
onClose: close,
|
|
140
|
+
onSelect: choose
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Map a stored minute count onto the picker id it should show. / 把存储的分钟数映射成选择器应显示的选项 id。 */
|
|
146
|
+
function derivedPreset(minutes: number): string {
|
|
147
|
+
if (minutes < 0) return "off";
|
|
148
|
+
if (minutes === REMINDER_EVERY_TURN) return "turn";
|
|
149
|
+
const preset = PRESET_MINUTES.find((candidate) => candidate === minutes);
|
|
150
|
+
return preset === undefined ? "custom" : String(preset);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Build the reminder picker: presets, every turn, off, and a custom minute count.
|
|
155
|
+
* 构建提醒选择器:预设、每轮、关闭,以及自定义分钟数。
|
|
156
|
+
*
|
|
157
|
+
* It uses the host Menu like the language picker, so the two controls match;
|
|
158
|
+
* a native `<select>` renders its own list chrome and breaks the row style.
|
|
159
|
+
* 与语言选择器一样使用宿主 Menu,两个控件因此外观一致;原生 `<select>` 的下拉列表
|
|
160
|
+
* 自带浏览器样式,会破坏这一行。
|
|
161
|
+
*/
|
|
162
|
+
function createReminderPicker(primitives: UiPrimitives | undefined) {
|
|
163
|
+
const Menu = primitives?.Menu;
|
|
164
|
+
const Chevron =
|
|
165
|
+
primitives?.IconChevronDownOutlineRegular ??
|
|
166
|
+
primitives?.IconChevronDownOutline14 ??
|
|
167
|
+
primitives?.IconChevronDownOutlineMedium;
|
|
168
|
+
|
|
169
|
+
return function ReminderPicker(props: Binding & { settings: ThinkingSettings }): unknown {
|
|
170
|
+
const { doc, t, settings } = props;
|
|
171
|
+
const derived = derivedPreset(settings.reminderMinutes);
|
|
172
|
+
const current = settings.reminderMinutes > 0 ? settings.reminderMinutes : 30;
|
|
173
|
+
const [selected, setSelected] = useState<string>(derived);
|
|
174
|
+
const [open, setOpen] = useState<boolean>(false);
|
|
175
|
+
const [draft, setDraft] = useState<string>(String(current));
|
|
176
|
+
|
|
177
|
+
// An external change (the other surface, or a re-read) wins over a local
|
|
178
|
+
// selection; a local "custom" survives because it leaves `derived` alone.
|
|
179
|
+
// 外部改动(另一入口或重新读取)优先于本地选择;本地的「自定义」不会改变
|
|
180
|
+
// `derived`,因此能保持选中。
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
setSelected(derived);
|
|
183
|
+
}, [derived]);
|
|
184
|
+
|
|
185
|
+
// Keep the input in step with the stored value, so an accepted edit never
|
|
186
|
+
// falls back to the previous number while the document update lands.
|
|
187
|
+
// 让输入框跟随存储值,避免写入落盘前先闪回上一个数字。
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
setDraft(String(current));
|
|
190
|
+
}, [current]);
|
|
191
|
+
|
|
192
|
+
const labelOf = (id: string): string => {
|
|
193
|
+
if (id === "off") return t("reminderOff");
|
|
194
|
+
if (id === "turn") return t("reminderTurn");
|
|
195
|
+
if (id === "custom") return t("reminderCustom");
|
|
196
|
+
return `${id} ${t("reminderUnit")}`;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const items = ["off", "turn", ...PRESET_MINUTES.map(String), "custom"].map((id) => ({
|
|
200
|
+
id,
|
|
201
|
+
label: labelOf(id)
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
const choose = (id: string): void => {
|
|
205
|
+
setOpen(false);
|
|
206
|
+
setSelected(id);
|
|
207
|
+
if (id === "off") writeField(doc, FIELD_REMINDER, REMINDER_OFF);
|
|
208
|
+
else if (id === "turn") writeField(doc, FIELD_REMINDER, REMINDER_EVERY_TURN);
|
|
209
|
+
else if (id !== "custom") writeField(doc, FIELD_REMINDER, Number(id));
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const commit = (): void => {
|
|
213
|
+
const minutes = Math.round(Number(draft));
|
|
214
|
+
if (!Number.isFinite(minutes) || draft.trim() === "") {
|
|
215
|
+
setDraft(String(current));
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const clamped = Math.min(MAX_REMINDER_MINUTES, Math.max(1, minutes));
|
|
219
|
+
writeField(doc, FIELD_REMINDER, clamped);
|
|
220
|
+
setDraft(String(clamped));
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const picker = h(
|
|
224
|
+
"button",
|
|
225
|
+
{
|
|
226
|
+
type: "button",
|
|
227
|
+
className: "dsh_desk_lang_picker",
|
|
228
|
+
"aria-haspopup": "menu",
|
|
229
|
+
"aria-expanded": open,
|
|
230
|
+
onClick: (): void => setOpen((value: boolean) => !value)
|
|
231
|
+
},
|
|
232
|
+
labelOf(selected),
|
|
233
|
+
Chevron === undefined ? null : h(Chevron, { className: "dsh_desk_lang_chevron" })
|
|
234
|
+
);
|
|
140
235
|
|
|
141
236
|
const control = Menu
|
|
142
237
|
? h(Menu, {
|
|
143
238
|
open,
|
|
144
239
|
anchor: picker,
|
|
145
240
|
items,
|
|
146
|
-
selectedId:
|
|
241
|
+
selectedId: selected,
|
|
147
242
|
align: "end",
|
|
148
243
|
portal: true,
|
|
149
|
-
onClose:
|
|
244
|
+
onClose: (): void => setOpen(false),
|
|
150
245
|
onSelect: choose
|
|
151
246
|
})
|
|
152
247
|
: picker;
|
|
153
248
|
|
|
249
|
+
if (selected !== "custom") return control;
|
|
250
|
+
return h(
|
|
251
|
+
"div",
|
|
252
|
+
{ className: "dsh_desk_lang_control" },
|
|
253
|
+
control,
|
|
254
|
+
h("input", {
|
|
255
|
+
type: "number",
|
|
256
|
+
className: "dsh_desk_lang_input",
|
|
257
|
+
min: 1,
|
|
258
|
+
max: MAX_REMINDER_MINUTES,
|
|
259
|
+
placeholder: String(settings.reminderMinutes > 0 ? settings.reminderMinutes : 30),
|
|
260
|
+
value: draft,
|
|
261
|
+
onChange: (event: { currentTarget: { value: string } }): void => setDraft(event.currentTarget.value),
|
|
262
|
+
onBlur: commit,
|
|
263
|
+
onKeyDown: (event: { key: string }): void => {
|
|
264
|
+
if (event.key === "Enter") commit();
|
|
265
|
+
}
|
|
266
|
+
}),
|
|
267
|
+
h("span", { className: "dsh_desk_lang_hint" }, t("reminderCustomHint"))
|
|
268
|
+
);
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Build the extra-injection picker: on or off.
|
|
274
|
+
* 构建额外注入选择器:开启或关闭。
|
|
275
|
+
*
|
|
276
|
+
* It shares the host Menu with the other controls, so the three read as one column.
|
|
277
|
+
* 与另外两个控件共用宿主 Menu,三者在视觉上成一列。
|
|
278
|
+
*/
|
|
279
|
+
function createExtraPicker(primitives: UiPrimitives | undefined) {
|
|
280
|
+
const Menu = primitives?.Menu;
|
|
281
|
+
const Chevron =
|
|
282
|
+
primitives?.IconChevronDownOutlineRegular ??
|
|
283
|
+
primitives?.IconChevronDownOutline14 ??
|
|
284
|
+
primitives?.IconChevronDownOutlineMedium;
|
|
285
|
+
|
|
286
|
+
return function ExtraPicker(props: Binding & { settings: ThinkingSettings; disabled?: boolean }): unknown {
|
|
287
|
+
const { doc, t, settings, disabled = false } = props;
|
|
288
|
+
const [open, setOpen] = useState<boolean>(false);
|
|
289
|
+
const items = [
|
|
290
|
+
{ id: "on", label: t("extraOn") },
|
|
291
|
+
{ id: "off", label: t("extraOff") }
|
|
292
|
+
];
|
|
293
|
+
const close = (): void => setOpen(false);
|
|
294
|
+
const choose = (id: string): void => {
|
|
295
|
+
writeField(doc, FIELD_EXTRA, id === "on");
|
|
296
|
+
close();
|
|
297
|
+
};
|
|
298
|
+
const picker = h(
|
|
299
|
+
"button",
|
|
300
|
+
{
|
|
301
|
+
type: "button",
|
|
302
|
+
className: "dsh_desk_lang_picker",
|
|
303
|
+
"aria-haspopup": "menu",
|
|
304
|
+
"aria-expanded": open,
|
|
305
|
+
disabled,
|
|
306
|
+
onClick: (): void => setOpen((value: boolean) => !value)
|
|
307
|
+
},
|
|
308
|
+
settings.extraInjection ? t("extraOn") : t("extraOff"),
|
|
309
|
+
Chevron === undefined ? null : h(Chevron, { className: "dsh_desk_lang_chevron" })
|
|
310
|
+
);
|
|
311
|
+
if (!Menu) return picker;
|
|
312
|
+
return h(Menu, {
|
|
313
|
+
open,
|
|
314
|
+
anchor: picker,
|
|
315
|
+
items,
|
|
316
|
+
selectedId: settings.extraInjection ? "on" : "off",
|
|
317
|
+
align: "end",
|
|
318
|
+
portal: true,
|
|
319
|
+
onClose: close,
|
|
320
|
+
onSelect: choose
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Build the General settings row: heading, description and the language picker.
|
|
327
|
+
* 构建「通用」设置行:标题、说明与语言选择器。
|
|
328
|
+
*/
|
|
329
|
+
export function createRow(primitives: UiPrimitives | undefined): (props: Binding) => unknown {
|
|
330
|
+
const LanguagePicker = createLanguagePicker(primitives);
|
|
331
|
+
return function ThinkingLanguageRow(props: Binding): unknown {
|
|
332
|
+
const settings = useSettings(props.doc);
|
|
154
333
|
return h(
|
|
155
334
|
"div",
|
|
156
335
|
{ className: "dsh_desk_lang_row" },
|
|
157
336
|
h(
|
|
158
337
|
"div",
|
|
159
338
|
{ className: "dsh_desk_lang_text" },
|
|
160
|
-
h("div", { className: "dsh_desk_lang_title" }, t("heading")),
|
|
161
|
-
h("div", { className: "dsh_desk_lang_desc" }, t("description"))
|
|
339
|
+
h("div", { className: "dsh_desk_lang_title" }, props.t("heading")),
|
|
340
|
+
h("div", { className: "dsh_desk_lang_desc" }, props.t("description"))
|
|
162
341
|
),
|
|
163
|
-
|
|
342
|
+
h(LanguagePicker, { doc: props.doc, t: props.t, settings })
|
|
343
|
+
);
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Build the plugin configuration card: language and reminder interval.
|
|
349
|
+
* 构建插件配置卡片:思考语言与提醒间隔。
|
|
350
|
+
*
|
|
351
|
+
* The host renders this inside the plugin's detail page; `view: 'summary'` is the
|
|
352
|
+
* one-line form a card may ask for, which this plugin leaves empty.
|
|
353
|
+
* 宿主把它渲染在插件详情页里;`view: 'summary'` 是卡片可能请求的单行形式,本插件
|
|
354
|
+
* 留空。
|
|
355
|
+
*/
|
|
356
|
+
export function createConfigCard(primitives: UiPrimitives | undefined): (props: Binding & { view?: string }) => unknown {
|
|
357
|
+
const LanguagePicker = createLanguagePicker(primitives);
|
|
358
|
+
const ReminderPicker = createReminderPicker(primitives);
|
|
359
|
+
const ExtraPicker = createExtraPicker(primitives);
|
|
360
|
+
return function ThinkingLanguageCard(props: Binding & { view?: string }): unknown {
|
|
361
|
+
const settings = useSettings(props.doc);
|
|
362
|
+
if (props.view === "summary") return null;
|
|
363
|
+
const binding = { doc: props.doc, t: props.t, settings };
|
|
364
|
+
// Turning the reminder off stops both injections, so the extra-injection
|
|
365
|
+
// control greys out instead of accepting a value that cannot take effect.
|
|
366
|
+
// 提醒关闭时两处注入都会停,因此额外注入控件置灰,而不是让一个不生效的取值可选。
|
|
367
|
+
const reminderOff = settings.reminderMinutes < 0;
|
|
368
|
+
return h(
|
|
369
|
+
"div",
|
|
370
|
+
null,
|
|
371
|
+
h(
|
|
372
|
+
"div",
|
|
373
|
+
{ className: "dsh_desk_lang_row" },
|
|
374
|
+
h(
|
|
375
|
+
"div",
|
|
376
|
+
{ className: "dsh_desk_lang_text" },
|
|
377
|
+
h("div", { className: "dsh_desk_lang_title" }, props.t("heading")),
|
|
378
|
+
h("div", { className: "dsh_desk_lang_desc" }, props.t("description"))
|
|
379
|
+
),
|
|
380
|
+
h(LanguagePicker, binding)
|
|
381
|
+
),
|
|
382
|
+
h(
|
|
383
|
+
"div",
|
|
384
|
+
{ className: "dsh_desk_lang_row" },
|
|
385
|
+
h(
|
|
386
|
+
"div",
|
|
387
|
+
{ className: "dsh_desk_lang_text" },
|
|
388
|
+
h("div", { className: "dsh_desk_lang_title" }, props.t("reminderHeading")),
|
|
389
|
+
h("div", { className: "dsh_desk_lang_desc" }, props.t("reminderDescription"))
|
|
390
|
+
),
|
|
391
|
+
h(ReminderPicker, binding)
|
|
392
|
+
),
|
|
393
|
+
h(
|
|
394
|
+
"div",
|
|
395
|
+
{ className: reminderOff ? "dsh_desk_lang_row dsh_desk_lang_row_disabled" : "dsh_desk_lang_row" },
|
|
396
|
+
h(
|
|
397
|
+
"div",
|
|
398
|
+
{ className: "dsh_desk_lang_text" },
|
|
399
|
+
h("div", { className: "dsh_desk_lang_title" }, props.t("extraHeading")),
|
|
400
|
+
h("div", { className: "dsh_desk_lang_desc" }, props.t("extraDescription"))
|
|
401
|
+
),
|
|
402
|
+
h(ExtraPicker, { ...binding, disabled: reminderOff })
|
|
403
|
+
)
|
|
164
404
|
);
|
|
165
405
|
};
|
|
166
406
|
}
|
|
167
407
|
|
|
168
408
|
// Declaration order follows the host's own PreferenceRow verbatim —
|
|
169
409
|
// packages/client/ui-chat/src/client/settings/PreferenceRow.module.css, MIT,
|
|
170
|
-
// Copyright (c) 2026 DeepSeek — because that is the row this one sits beside
|
|
171
|
-
// the two can be diffed property by property whenever the host styles move.
|
|
410
|
+
// Copyright (c) 2026 DeepSeek — because that is the row this one sits beside.
|
|
172
411
|
// Class names differ (the host's are CSS-Modules hashes) and the picker carries a
|
|
173
412
|
// trailing `flex:none` the host does not need; every other character matches.
|
|
174
|
-
// 声明顺序逐字照宿主自己的 PreferenceRow
|
|
175
|
-
//
|
|
176
|
-
// Copyright (c) 2026 DeepSeek——因为那就是本行紧邻的邻居,宿主样式变动时可以逐属性
|
|
177
|
-
// 对照。类名不同(宿主的是 CSS Modules 哈希),且选择器末尾多一个宿主不需要的
|
|
178
|
-
// `flex:none`;除此之外逐字符一致。
|
|
413
|
+
// 声明顺序逐字照宿主自己的 PreferenceRow——因为那就是本行紧邻的邻居。类名不同,
|
|
414
|
+
// 选择器末尾多一个宿主不需要的 `flex:none`;除此之外逐字符一致。
|
|
179
415
|
const rowCss = [
|
|
180
416
|
".dsh_desk_lang_row{border-bottom:.5px solid var(--dsw-alias-border-l2);align-items:center;gap:8px;padding:16px 0;display:flex}",
|
|
417
|
+
".dsh_desk_lang_row_disabled{opacity:.45;pointer-events:none}",
|
|
181
418
|
".dsh_desk_lang_text{flex-direction:column;flex:1;gap:4px;min-width:0;padding-right:48px;display:flex}",
|
|
182
419
|
".dsh_desk_lang_title{color:var(--dsw-alias-label-primary);font-size:14px;font-weight:400;line-height:22px}",
|
|
183
420
|
".dsh_desk_lang_desc{color:var(--dsw-alias-label-tertiary);font-size:12px;font-weight:400;line-height:18px}",
|
|
184
421
|
".dsh_desk_lang_picker{border-radius:var(--dsw-radius-md);background:var(--dsw-alias-bg-module-platform);height:36px;font:inherit;color:var(--dsw-alias-label-primary);cursor:pointer;border:none;align-items:center;gap:12px;padding:0 14px;font-size:14px;line-height:22px;display:inline-flex;flex:none}",
|
|
185
422
|
".dsh_desk_lang_picker:hover{background:var(--dsw-alias-interactive-bg-hover)}",
|
|
186
|
-
".dsh_desk_lang_chevron{flex:none}"
|
|
423
|
+
".dsh_desk_lang_chevron{flex:none}",
|
|
424
|
+
".dsh_desk_lang_control{align-items:center;gap:8px;display:flex;flex:none}",
|
|
425
|
+
".dsh_desk_lang_input{border-radius:var(--dsw-radius-md);background:var(--dsw-alias-bg-module-platform);height:36px;width:88px;font:inherit;color:var(--dsw-alias-label-primary);border:none;padding:0 12px;font-size:14px}",
|
|
426
|
+
".dsh_desk_lang_hint{color:var(--dsw-alias-label-tertiary);font-size:12px;white-space:nowrap}"
|
|
187
427
|
].join("\n");
|
|
188
428
|
|
|
189
429
|
/** Marks the injected stylesheet, so a second mount can detect the first. / 标记已注入的样式表,使第二次挂载能发现第一次。 */
|
|
190
430
|
const STYLE_MARKER = "dsh-thinking-lang";
|
|
191
431
|
|
|
192
432
|
/**
|
|
193
|
-
* Inject the
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* The guard reads the document rather than a module-level flag: a client plugin
|
|
197
|
-
* can be mounted more than once against the same page, and only the DOM knows
|
|
198
|
-
* whether this sheet is already there. Content is compared as well, because a
|
|
199
|
-
* hot-reloaded bundle keeps its marker while its CSS changes; skipping on the
|
|
200
|
-
* marker alone would leave the row styled by class names that no longer exist.
|
|
201
|
-
* 判重依据是文档本身而不是模块级标志:客户端插件可能被重复挂载到同一页面,只有 DOM
|
|
202
|
-
* 知道这张样式表是否已经存在。同时比对内容:热重载的 bundle 标记不变而 CSS 已变,
|
|
203
|
-
* 只看标记就跳过会让这一行按已不存在的类名取样式。
|
|
433
|
+
* Inject the settings stylesheet once per document, replacing a stale one.
|
|
434
|
+
* 每份文档只注入一次设置样式表,已过期的那张会被替换。
|
|
204
435
|
*/
|
|
205
436
|
export function mountRowStyles(): void {
|
|
206
437
|
if (typeof document === "undefined") return;
|
package/src/core.ts
CHANGED
|
@@ -34,8 +34,10 @@ export interface Language {
|
|
|
34
34
|
nativeName: string;
|
|
35
35
|
/** The rule sentence written in this language. / 用本语言写成的规则句。 */
|
|
36
36
|
section: string;
|
|
37
|
-
/** The short
|
|
37
|
+
/** The short reminder written in this language for the head of the batch. / 写在批次开头的短提醒。 */
|
|
38
38
|
reminder: string;
|
|
39
|
+
/** The short reminder written for the last message of the request. / 写在请求末尾的短提醒。 */
|
|
40
|
+
tail: string;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
/**
|
|
@@ -55,19 +57,22 @@ export const CATALOGUE: readonly Language[] = [
|
|
|
55
57
|
tag: "zh-CN",
|
|
56
58
|
nativeName: "简体中文",
|
|
57
59
|
section: "推理规则:思考、推理与规划一律使用简体中文。回复用户时使用用户所用的语言。",
|
|
58
|
-
reminder: "
|
|
60
|
+
reminder: "注意:思考、推理与规划用简体中文,即使上文不是",
|
|
61
|
+
tail: "下文请使用简体中文思考"
|
|
59
62
|
},
|
|
60
63
|
{
|
|
61
64
|
tag: "zh-TW",
|
|
62
65
|
nativeName: "繁體中文",
|
|
63
66
|
section: "推理規則:思考、推理與規劃一律使用繁體中文。回覆使用者時使用使用者所用的語言。",
|
|
64
|
-
reminder: "
|
|
67
|
+
reminder: "注意:思考、推理與規劃用繁體中文,即使上文不是",
|
|
68
|
+
tail: "下文請使用繁體中文思考"
|
|
65
69
|
},
|
|
66
70
|
{
|
|
67
71
|
tag: "en",
|
|
68
72
|
nativeName: "English",
|
|
69
73
|
section: "Reasoning rules: think, reason, and plan in English. Reply to the user in the language they use.",
|
|
70
|
-
reminder: "
|
|
74
|
+
reminder: "Note: think, reason, and plan in English, even if the text above is not",
|
|
75
|
+
tail: "Think in English from here on"
|
|
71
76
|
}
|
|
72
77
|
];
|
|
73
78
|
|
|
@@ -80,9 +85,27 @@ const FALLBACK_TAG = "zh-CN";
|
|
|
80
85
|
/** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
|
|
81
86
|
export const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
|
|
82
87
|
|
|
83
|
-
/** The
|
|
88
|
+
/** The language field this plugin stores. / 本插件保存的语言字段。 */
|
|
84
89
|
export const FIELD_LANGUAGE = "language";
|
|
85
90
|
|
|
91
|
+
/** The reminder field this plugin stores. / 本插件保存的提醒字段。 */
|
|
92
|
+
export const FIELD_REMINDER = "reminderMinutes";
|
|
93
|
+
|
|
94
|
+
/** The extra-injection field this plugin stores. / 本插件保存的额外注入字段。 */
|
|
95
|
+
export const FIELD_EXTRA = "extraInjection";
|
|
96
|
+
|
|
97
|
+
/** Reminder value that disables the per-turn reminder. / 关闭每轮提醒的取值。 */
|
|
98
|
+
export const REMINDER_OFF = -1;
|
|
99
|
+
|
|
100
|
+
/** Reminder value that injects on every turn. / 每轮都注入的取值。 */
|
|
101
|
+
export const REMINDER_EVERY_TURN = 0;
|
|
102
|
+
|
|
103
|
+
/** Reminder interval applied when the field is absent. / 字段缺失时的提醒间隔(分钟)。 */
|
|
104
|
+
export const DEFAULT_REMINDER_MINUTES = 30;
|
|
105
|
+
|
|
106
|
+
/** Largest accepted reminder interval. / 允许的最大提醒间隔(分钟)。 */
|
|
107
|
+
export const MAX_REMINDER_MINUTES = 1440;
|
|
108
|
+
|
|
86
109
|
/** Every value the picker offers besides {@link AUTO}. / 选择器在 {@link AUTO} 之外提供的全部取值。 */
|
|
87
110
|
export const SELECTABLE_TAGS: readonly string[] = CATALOGUE.map((language) => language.tag);
|
|
88
111
|
|
|
@@ -90,10 +113,27 @@ export const SELECTABLE_TAGS: readonly string[] = CATALOGUE.map((language) => la
|
|
|
90
113
|
export interface ThinkingSettings {
|
|
91
114
|
/** Selected language tag, or {@link AUTO}. / 所选语言标签,或 {@link AUTO}。 */
|
|
92
115
|
language: string;
|
|
116
|
+
/**
|
|
117
|
+
* Minutes between reminders: {@link REMINDER_OFF} disables them,
|
|
118
|
+
* {@link REMINDER_EVERY_TURN} injects on every turn, and 1–1440 is a minimum gap.
|
|
119
|
+
* 提醒间隔(分钟):{@link REMINDER_OFF} 关闭,{@link REMINDER_EVERY_TURN} 每轮
|
|
120
|
+
* 注入,1–1440 为最小间隔。
|
|
121
|
+
*/
|
|
122
|
+
reminderMinutes: number;
|
|
123
|
+
/**
|
|
124
|
+
* Whether the closing rule is also folded into the skill catalogue. It only
|
|
125
|
+
* applies while the reminder is on.
|
|
126
|
+
* 是否把收尾规则同时折进技能目录;仅在提醒开启时生效。
|
|
127
|
+
*/
|
|
128
|
+
extraInjection: boolean;
|
|
93
129
|
}
|
|
94
130
|
|
|
95
|
-
/** Configuration applied when the
|
|
96
|
-
export const DEFAULTS: ThinkingSettings = {
|
|
131
|
+
/** Configuration applied when the fields are absent. / 字段缺失时采用的配置。 */
|
|
132
|
+
export const DEFAULTS: ThinkingSettings = {
|
|
133
|
+
language: AUTO,
|
|
134
|
+
reminderMinutes: DEFAULT_REMINDER_MINUTES,
|
|
135
|
+
extraInjection: true
|
|
136
|
+
};
|
|
97
137
|
|
|
98
138
|
/** A minimal read handle over the host settings service. / 宿主设置服务的最小读取句柄。 */
|
|
99
139
|
export interface Reader {
|
|
@@ -202,10 +242,41 @@ function tagForLocale(raw: unknown): string | undefined {
|
|
|
202
242
|
* Coerce an untrusted settings section into a complete configuration.
|
|
203
243
|
* 把不可信的设置段强制转换为完整配置。
|
|
204
244
|
*/
|
|
245
|
+
/** Clamp one untrusted reminder value into the accepted range; any negative value disables it. / 把不可信的提醒值收敛到允许范围;负数一律视为关闭。 */
|
|
246
|
+
function normalizeReminder(raw: unknown): number {
|
|
247
|
+
const numeric = typeof raw === "number" ? raw : typeof raw === "string" && raw.trim() !== "" ? Number(raw) : NaN;
|
|
248
|
+
if (!Number.isFinite(numeric)) return DEFAULT_REMINDER_MINUTES;
|
|
249
|
+
const rounded = Math.round(numeric);
|
|
250
|
+
if (rounded < 0) return REMINDER_OFF;
|
|
251
|
+
return rounded > MAX_REMINDER_MINUTES ? MAX_REMINDER_MINUTES : rounded;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Coerce one untrusted extra-injection value; only an explicit false turns it off. / 把不可信的额外注入值收敛为布尔;只有明确的 false 视为关闭。 */
|
|
255
|
+
function normalizeExtra(raw: unknown): boolean {
|
|
256
|
+
return raw !== false && raw !== "false";
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Convert stored minutes into the minimum gap between reminders.
|
|
261
|
+
* 把存储的分钟数换算成两次提醒之间的最小间隔。
|
|
262
|
+
*
|
|
263
|
+
* Returns {@link REMINDER_OFF} when the reminder is disabled and `0` when it runs
|
|
264
|
+
* on every turn. / 关闭时返回 {@link REMINDER_OFF},每轮注入时返回 `0`。
|
|
265
|
+
*/
|
|
266
|
+
export function reminderDelayMs(minutes: number): number {
|
|
267
|
+
if (minutes < 0) return REMINDER_OFF;
|
|
268
|
+
if (minutes === REMINDER_EVERY_TURN) return 0;
|
|
269
|
+
return minutes * 60_000;
|
|
270
|
+
}
|
|
271
|
+
|
|
205
272
|
export function normalizeSettings(raw: unknown): ThinkingSettings {
|
|
206
|
-
const
|
|
207
|
-
const stored =
|
|
208
|
-
return {
|
|
273
|
+
const section = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? (raw as Record<string, unknown>) : undefined;
|
|
274
|
+
const stored = section?.[FIELD_LANGUAGE];
|
|
275
|
+
return {
|
|
276
|
+
language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language,
|
|
277
|
+
reminderMinutes: normalizeReminder(section?.[FIELD_REMINDER]),
|
|
278
|
+
extraInjection: normalizeExtra(section?.[FIELD_EXTRA])
|
|
279
|
+
};
|
|
209
280
|
}
|
|
210
281
|
|
|
211
282
|
/**
|
|
@@ -250,3 +321,11 @@ export function sectionText(code: string): string {
|
|
|
250
321
|
export function reminderText(code: string): string {
|
|
251
322
|
return languageOf(code)?.reminder ?? "";
|
|
252
323
|
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Build the closing reminder appended as the request's last message.
|
|
327
|
+
* 生成追加在请求最后一条消息里的收尾提醒。
|
|
328
|
+
*/
|
|
329
|
+
export function tailText(code: string): string {
|
|
330
|
+
return languageOf(code)?.tail ?? "";
|
|
331
|
+
}
|