dsh-thinking-lang 1.0.6 → 1.0.7

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/client.js CHANGED
@@ -42,11 +42,18 @@ window.__ModuleLoader__.load({
42
42
  const AUTO = "auto";
43
43
  /** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
44
44
  const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
45
- /** The one field this plugin stores. / 本插件唯一保存的字段。 */
45
+ /** The language field this plugin stores. / 本插件保存的语言字段。 */
46
46
  const FIELD_LANGUAGE = "language";
47
+ /** The reminder field this plugin stores. / 本插件保存的提醒字段。 */
48
+ const FIELD_REMINDER = "reminderMinutes";
49
+ /** Largest accepted reminder interval. / 允许的最大提醒间隔(分钟)。 */
50
+ const MAX_REMINDER_MINUTES = 1440;
47
51
  CATALOGUE.map((language) => language.tag);
48
- /** Configuration applied when the field is absent. / 字段缺失时采用的配置。 */
49
- const DEFAULTS = { language: AUTO };
52
+ /** Configuration applied when the fields are absent. / 字段缺失时采用的配置。 */
53
+ const DEFAULTS = {
54
+ language: AUTO,
55
+ reminderMinutes: 30
56
+ };
50
57
  /** Catalogue lookup keyed by lower-cased tag. / 以小写标签为键的目录索引。 */
51
58
  const BY_TAG = new Map(CATALOGUE.map((language) => [language.tag.toLowerCase(), language]));
52
59
  /**
@@ -68,38 +75,68 @@ window.__ModuleLoader__.load({
68
75
  * Coerce an untrusted settings section into a complete configuration.
69
76
  * 把不可信的设置段强制转换为完整配置。
70
77
  */
78
+ /** Clamp one untrusted reminder value into the accepted range; any negative value disables it. / 把不可信的提醒值收敛到允许范围;负数一律视为关闭。 */
79
+ function normalizeReminder(raw) {
80
+ const numeric = typeof raw === "number" ? raw : typeof raw === "string" && raw.trim() !== "" ? Number(raw) : NaN;
81
+ if (!Number.isFinite(numeric)) return 30;
82
+ const rounded = Math.round(numeric);
83
+ if (rounded < 0) return -1;
84
+ return rounded > 1440 ? MAX_REMINDER_MINUTES : rounded;
85
+ }
71
86
  function normalizeSettings(raw) {
72
- const stored = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw[FIELD_LANGUAGE] : void 0;
73
- return { language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language };
87
+ const section = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw : void 0;
88
+ const stored = section?.[FIELD_LANGUAGE];
89
+ return {
90
+ language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language,
91
+ reminderMinutes: normalizeReminder(section?.[FIELD_REMINDER])
92
+ };
74
93
  }
75
94
  /** Dictionaries keyed by UI locale, handed to the host locale service. / 以界面语言为键、交给宿主 locale 服务的字典。 */
76
95
  const dictionaries = {
77
96
  zh: {
78
97
  heading: "思考语言",
79
98
  description: "内部推理与规划使用的语言。「自动」优先跟随操作系统语言,其次是界面语言。",
80
- autoLabel: "自动"
99
+ autoLabel: "自动",
100
+ reminderHeading: "提醒间隔",
101
+ reminderDescription: "两次提醒之间的最小间隔;关闭后只保留系统提示里的规则。",
102
+ reminderOff: "关闭",
103
+ reminderTurn: "每轮",
104
+ reminderCustom: "自定义",
105
+ reminderUnit: "分钟",
106
+ reminderCustomHint: "1–1440 分钟"
81
107
  },
82
108
  en: {
83
109
  heading: "Thinking language",
84
110
  description: "Language used for internal reasoning and planning. Automatic follows the operating-system language first, then the UI language.",
85
- autoLabel: "Automatic"
111
+ autoLabel: "Automatic",
112
+ reminderHeading: "Reminder interval",
113
+ reminderDescription: "Minimum gap between reminders. Off keeps only the system-prompt rule.",
114
+ reminderOff: "Off",
115
+ reminderTurn: "Every turn",
116
+ reminderCustom: "Custom",
117
+ reminderUnit: "min",
118
+ reminderCustomHint: "1–1440 minutes"
86
119
  }
87
120
  };
88
121
  //#endregion
89
122
  //#region src/client/panel.ts
90
123
  /**
91
- * The settings row: one language picker bound to the plugin's settings document.
92
- * 设置行:一个绑定到本插件设置文档的语言选择器。
124
+ * Settings UI: the General row and the plugin's configuration card.
125
+ * 设置界面:「通用」页的设置行,以及插件详情页的配置卡片。
93
126
  *
94
- * It is registered into the General settings page (see `./index.ts`) rather than
95
- * as a section of its own, so it sits directly under the host's language row.
96
- * 它注册进「通用」设置页(见 `./index.ts`)而不是自成菜单,因此紧邻宿主自身的
97
- * 语言设置项下方。
127
+ * Both surfaces bind to the same host settings document, so a change in either
128
+ * place is visible in the other. / 两个入口绑定同一份宿主设置文档,任一处修改
129
+ * 另一处都会看到。
98
130
  *
99
- * The row owns no state of its own: it reads the host's settings document,
100
- * subscribes to it, and writes the single field back.
101
- * 本行不持有自己的状态:它读取宿主的设置文档、订阅其变化,并回写那唯一一个字段。
131
+ * The row owns no state of its own: it reads the document, subscribes to it, and
132
+ * writes the fields back. / 控件不持有自己的状态:读取文档、订阅变化、回写字段。
102
133
  */
134
+ /** Preset reminder intervals in minutes. / 预设提醒间隔(分钟)。 */
135
+ const PRESET_MINUTES = [
136
+ 10,
137
+ 30,
138
+ 60
139
+ ];
103
140
  /**
104
141
  * Read and normalize the current settings, falling back to defaults on refusal.
105
142
  * 读取并归一化当前设置;被拒绝时回退到默认值。
@@ -112,12 +149,8 @@ window.__ModuleLoader__.load({
112
149
  }
113
150
  }
114
151
  /**
115
- * Write one field back, swallowing a refusal so a read-only host cannot break the row.
116
- * 回写单个字段,并吞掉拒绝异常,使只读宿主不会让这一行崩溃。
117
- *
118
- * A host may return a promise instead of a value; an unhandled rejection from a
119
- * read-only document is not worth surfacing, so it is discarded here.
120
- * 宿主可能返回 promise 而不是值;只读文档产生的未处理拒绝不值得上抛,因此在此丢弃。
152
+ * Write one field back, swallowing a refusal so a read-only host cannot break the UI.
153
+ * 回写单个字段,并吞掉拒绝异常,使只读宿主不会让控件崩溃。
121
154
  */
122
155
  function writeField(doc, field, value) {
123
156
  try {
@@ -125,24 +158,26 @@ window.__ModuleLoader__.load({
125
158
  if (typeof outcome?.then === "function") outcome.then(void 0, () => {});
126
159
  } catch {}
127
160
  }
161
+ /** Subscribe one component to the document and re-read it on every revision. / 订阅设置文档,并在每次修订时重新读取。 */
162
+ function useSettings(doc) {
163
+ const [settings, setSettings] = (0, react.useState)(() => readSettings(doc));
164
+ (0, react.useEffect)(() => {
165
+ const sync = () => setSettings(readSettings(doc));
166
+ sync();
167
+ return doc.subscribe(sync);
168
+ }, [doc]);
169
+ return settings;
170
+ }
128
171
  /**
129
- * Build the row component bound to the host's UI primitives.
130
- * 构建绑定到宿主 UI 原语的行组件。
172
+ * Build the language picker bound to the host's UI primitives.
173
+ * 构建绑定到宿主 UI 原语的语言选择器。
131
174
  */
132
- function createRow(primitives) {
175
+ function createLanguagePicker(primitives) {
133
176
  const Menu = primitives?.Menu;
134
177
  const Chevron = primitives?.IconChevronDownOutlineRegular ?? primitives?.IconChevronDownOutline14 ?? primitives?.IconChevronDownOutlineMedium;
135
- return function ThinkingLanguageRow(props) {
136
- const { doc, t: hostT } = props;
137
- const [settings, setSettings] = (0, react.useState)(() => readSettings(doc));
178
+ return function LanguagePicker(props) {
179
+ const { doc, t, settings } = props;
138
180
  const [open, setOpen] = (0, react.useState)(false);
139
- settings.language;
140
- const t = (key) => hostT(key);
141
- (0, react.useEffect)(() => {
142
- const sync = () => setSettings(readSettings(doc));
143
- sync();
144
- return doc.subscribe(sync);
145
- }, [doc]);
146
181
  const items = [{
147
182
  id: AUTO,
148
183
  label: t("autoLabel")
@@ -152,7 +187,6 @@ window.__ModuleLoader__.load({
152
187
  }))];
153
188
  const activeLabel = settings.language === "auto" ? t("autoLabel") : nativeNameFor(settings.language) ?? settings.language;
154
189
  const close = () => setOpen(false);
155
- const toggle = () => setOpen((value) => !value);
156
190
  const choose = (id) => {
157
191
  writeField(doc, FIELD_LANGUAGE, id);
158
192
  close();
@@ -162,9 +196,10 @@ window.__ModuleLoader__.load({
162
196
  className: "dsh_desk_lang_picker",
163
197
  "aria-haspopup": "menu",
164
198
  "aria-expanded": open,
165
- onClick: toggle
199
+ onClick: () => setOpen((value) => !value)
166
200
  }, activeLabel, Chevron === void 0 ? null : (0, react.createElement)(Chevron, { className: "dsh_desk_lang_chevron" }));
167
- const control = Menu ? (0, react.createElement)(Menu, {
201
+ if (!Menu) return picker;
202
+ return (0, react.createElement)(Menu, {
168
203
  open,
169
204
  anchor: picker,
170
205
  items,
@@ -173,8 +208,142 @@ window.__ModuleLoader__.load({
173
208
  portal: true,
174
209
  onClose: close,
175
210
  onSelect: choose
211
+ });
212
+ };
213
+ }
214
+ /** Map a stored minute count onto the picker id it should show. / 把存储的分钟数映射成选择器应显示的选项 id。 */
215
+ function derivedPreset(minutes) {
216
+ if (minutes < 0) return "off";
217
+ if (minutes === 0) return "turn";
218
+ const preset = PRESET_MINUTES.find((candidate) => candidate === minutes);
219
+ return preset === void 0 ? "custom" : String(preset);
220
+ }
221
+ /**
222
+ * Build the reminder picker: presets, every turn, off, and a custom minute count.
223
+ * 构建提醒选择器:预设、每轮、关闭,以及自定义分钟数。
224
+ *
225
+ * It uses the host Menu like the language picker, so the two controls match;
226
+ * a native `<select>` renders its own list chrome and breaks the row style.
227
+ * 与语言选择器一样使用宿主 Menu,两个控件因此外观一致;原生 `<select>` 的下拉列表
228
+ * 自带浏览器样式,会破坏这一行。
229
+ */
230
+ function createReminderPicker(primitives) {
231
+ const Menu = primitives?.Menu;
232
+ const Chevron = primitives?.IconChevronDownOutlineRegular ?? primitives?.IconChevronDownOutline14 ?? primitives?.IconChevronDownOutlineMedium;
233
+ return function ReminderPicker(props) {
234
+ const { doc, t, settings } = props;
235
+ const derived = derivedPreset(settings.reminderMinutes);
236
+ const current = settings.reminderMinutes > 0 ? settings.reminderMinutes : 30;
237
+ const [selected, setSelected] = (0, react.useState)(derived);
238
+ const [open, setOpen] = (0, react.useState)(false);
239
+ const [draft, setDraft] = (0, react.useState)(String(current));
240
+ (0, react.useEffect)(() => {
241
+ setSelected(derived);
242
+ }, [derived]);
243
+ (0, react.useEffect)(() => {
244
+ setDraft(String(current));
245
+ }, [current]);
246
+ const labelOf = (id) => {
247
+ if (id === "off") return t("reminderOff");
248
+ if (id === "turn") return t("reminderTurn");
249
+ if (id === "custom") return t("reminderCustom");
250
+ return `${id} ${t("reminderUnit")}`;
251
+ };
252
+ const items = [
253
+ "off",
254
+ "turn",
255
+ ...PRESET_MINUTES.map(String),
256
+ "custom"
257
+ ].map((id) => ({
258
+ id,
259
+ label: labelOf(id)
260
+ }));
261
+ const choose = (id) => {
262
+ setOpen(false);
263
+ setSelected(id);
264
+ if (id === "off") writeField(doc, FIELD_REMINDER, -1);
265
+ else if (id === "turn") writeField(doc, FIELD_REMINDER, 0);
266
+ else if (id !== "custom") writeField(doc, FIELD_REMINDER, Number(id));
267
+ };
268
+ const commit = () => {
269
+ const minutes = Math.round(Number(draft));
270
+ if (!Number.isFinite(minutes) || draft.trim() === "") {
271
+ setDraft(String(current));
272
+ return;
273
+ }
274
+ const clamped = Math.min(MAX_REMINDER_MINUTES, Math.max(1, minutes));
275
+ writeField(doc, FIELD_REMINDER, clamped);
276
+ setDraft(String(clamped));
277
+ };
278
+ const picker = (0, react.createElement)("button", {
279
+ type: "button",
280
+ className: "dsh_desk_lang_picker",
281
+ "aria-haspopup": "menu",
282
+ "aria-expanded": open,
283
+ onClick: () => setOpen((value) => !value)
284
+ }, labelOf(selected), Chevron === void 0 ? null : (0, react.createElement)(Chevron, { className: "dsh_desk_lang_chevron" }));
285
+ const control = Menu ? (0, react.createElement)(Menu, {
286
+ open,
287
+ anchor: picker,
288
+ items,
289
+ selectedId: selected,
290
+ align: "end",
291
+ portal: true,
292
+ onClose: () => setOpen(false),
293
+ onSelect: choose
176
294
  }) : picker;
177
- return (0, react.createElement)("div", { className: "dsh_desk_lang_row" }, (0, react.createElement)("div", { className: "dsh_desk_lang_text" }, (0, react.createElement)("div", { className: "dsh_desk_lang_title" }, t("heading")), (0, react.createElement)("div", { className: "dsh_desk_lang_desc" }, t("description"))), control);
295
+ if (selected !== "custom") return control;
296
+ return (0, react.createElement)("div", { className: "dsh_desk_lang_control" }, control, (0, react.createElement)("input", {
297
+ type: "number",
298
+ className: "dsh_desk_lang_input",
299
+ min: 1,
300
+ max: MAX_REMINDER_MINUTES,
301
+ placeholder: String(settings.reminderMinutes > 0 ? settings.reminderMinutes : 30),
302
+ value: draft,
303
+ onChange: (event) => setDraft(event.currentTarget.value),
304
+ onBlur: commit,
305
+ onKeyDown: (event) => {
306
+ if (event.key === "Enter") commit();
307
+ }
308
+ }), (0, react.createElement)("span", { className: "dsh_desk_lang_hint" }, t("reminderCustomHint")));
309
+ };
310
+ }
311
+ /**
312
+ * Build the General settings row: heading, description and the language picker.
313
+ * 构建「通用」设置行:标题、说明与语言选择器。
314
+ */
315
+ function createRow(primitives) {
316
+ const LanguagePicker = createLanguagePicker(primitives);
317
+ return function ThinkingLanguageRow(props) {
318
+ const settings = useSettings(props.doc);
319
+ return (0, react.createElement)("div", { className: "dsh_desk_lang_row" }, (0, react.createElement)("div", { className: "dsh_desk_lang_text" }, (0, react.createElement)("div", { className: "dsh_desk_lang_title" }, props.t("heading")), (0, react.createElement)("div", { className: "dsh_desk_lang_desc" }, props.t("description"))), (0, react.createElement)(LanguagePicker, {
320
+ doc: props.doc,
321
+ t: props.t,
322
+ settings
323
+ }));
324
+ };
325
+ }
326
+ /**
327
+ * Build the plugin configuration card: language and reminder interval.
328
+ * 构建插件配置卡片:思考语言与提醒间隔。
329
+ *
330
+ * The host renders this inside the plugin's detail page; `view: 'summary'` is the
331
+ * one-line form a card may ask for, which this plugin leaves empty.
332
+ * 宿主把它渲染在插件详情页里;`view: 'summary'` 是卡片可能请求的单行形式,本插件
333
+ * 留空。
334
+ */
335
+ function createConfigCard(primitives) {
336
+ const LanguagePicker = createLanguagePicker(primitives);
337
+ const ReminderPicker = createReminderPicker(primitives);
338
+ return function ThinkingLanguageCard(props) {
339
+ const settings = useSettings(props.doc);
340
+ if (props.view === "summary") return null;
341
+ const binding = {
342
+ doc: props.doc,
343
+ t: props.t,
344
+ settings
345
+ };
346
+ return (0, react.createElement)("div", null, (0, react.createElement)("div", { className: "dsh_desk_lang_row" }, (0, react.createElement)("div", { className: "dsh_desk_lang_text" }, (0, react.createElement)("div", { className: "dsh_desk_lang_title" }, props.t("heading")), (0, react.createElement)("div", { className: "dsh_desk_lang_desc" }, props.t("description"))), (0, react.createElement)(LanguagePicker, binding)), (0, react.createElement)("div", { className: "dsh_desk_lang_row" }, (0, react.createElement)("div", { className: "dsh_desk_lang_text" }, (0, react.createElement)("div", { className: "dsh_desk_lang_title" }, props.t("reminderHeading")), (0, react.createElement)("div", { className: "dsh_desk_lang_desc" }, props.t("reminderDescription"))), (0, react.createElement)(ReminderPicker, binding)));
178
347
  };
179
348
  }
180
349
  const rowCss = [
@@ -184,22 +353,16 @@ window.__ModuleLoader__.load({
184
353
  ".dsh_desk_lang_desc{color:var(--dsw-alias-label-tertiary);font-size:12px;font-weight:400;line-height:18px}",
185
354
  ".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}",
186
355
  ".dsh_desk_lang_picker:hover{background:var(--dsw-alias-interactive-bg-hover)}",
187
- ".dsh_desk_lang_chevron{flex:none}"
356
+ ".dsh_desk_lang_chevron{flex:none}",
357
+ ".dsh_desk_lang_control{align-items:center;gap:8px;display:flex;flex:none}",
358
+ ".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}",
359
+ ".dsh_desk_lang_hint{color:var(--dsw-alias-label-tertiary);font-size:12px;white-space:nowrap}"
188
360
  ].join("\n");
189
361
  /** Marks the injected stylesheet, so a second mount can detect the first. / 标记已注入的样式表,使第二次挂载能发现第一次。 */
190
362
  const STYLE_MARKER = "dsh-thinking-lang";
191
363
  /**
192
- * Inject the row stylesheet once per document, replacing a stale one.
193
- * 每份文档只注入一次行样式表,已过期的那张会被替换。
194
- *
195
- * The guard reads the document rather than a module-level flag: a client plugin
196
- * can be mounted more than once against the same page, and only the DOM knows
197
- * whether this sheet is already there. Content is compared as well, because a
198
- * hot-reloaded bundle keeps its marker while its CSS changes; skipping on the
199
- * marker alone would leave the row styled by class names that no longer exist.
200
- * 判重依据是文档本身而不是模块级标志:客户端插件可能被重复挂载到同一页面,只有 DOM
201
- * 知道这张样式表是否已经存在。同时比对内容:热重载的 bundle 标记不变而 CSS 已变,
202
- * 只看标记就跳过会让这一行按已不存在的类名取样式。
364
+ * Inject the settings stylesheet once per document, replacing a stale one.
365
+ * 每份文档只注入一次设置样式表,已过期的那张会被替换。
203
366
  */
204
367
  function mountRowStyles() {
205
368
  if (typeof document === "undefined") return;
@@ -228,6 +391,8 @@ window.__ModuleLoader__.load({
228
391
  */
229
392
  /** Stable Cordis plugin name. / 稳定的 Cordis 插件名。 */
230
393
  const name = "ay-dsh-tk-lang";
394
+ /** Package name; the key Plugins page uses to find a bundle's configuration page. / 包名;插件页用它查找某个 bundle 的配置页。 */
395
+ const PACKAGE_NAME = "dsh-thinking-lang";
231
396
  /** The one client service this plugin requires. / 本插件唯一需要的客户端服务。 */
232
397
  const inject = ["slots"];
233
398
  /** Locale namespace holding the row copy. / 保存行文案的 locale 命名空间。 */
@@ -298,6 +463,21 @@ window.__ModuleLoader__.load({
298
463
  })
299
464
  }, Row);
300
465
  });
466
+ const whileServed = typeof forms.whileServed === "function" ? forms.whileServed.bind(forms) : void 0;
467
+ if (whileServed === void 0) {
468
+ report(uiCtx, "configForms.whileServed unavailable; plugin configuration page disabled");
469
+ return;
470
+ }
471
+ const Card = createConfigCard(primitives);
472
+ uiCtx.effect(() => whileServed([SETTINGS_NAMESPACE], () => uiCtx.slots.inject("plugins.bundle.config", () => uiCtx.slots.register({
473
+ name: "plugins.bundle.config",
474
+ key: PACKAGE_NAME,
475
+ locale: LOCALE_NS,
476
+ inject: () => ({
477
+ doc,
478
+ t
479
+ })
480
+ }, Card))), "dsh-thinking-lang: plugin configuration page");
301
481
  });
302
482
  }
303
483
  //#endregion