dsh-thinking-lang 0.0.0-stage → 1.0.4
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/LICENSE +21 -0
- package/PROVENANCE.md +40 -0
- package/README.md +227 -2
- package/client/client.js +309 -0
- package/client/client.raw.cjs +301 -0
- package/cordis.patch.yml +14 -0
- package/lib/core.d.ts +109 -0
- package/lib/core.js +221 -0
- package/lib/index.d.ts +45 -0
- package/lib/index.js +281 -0
- package/package.json +89 -4
- package/src/client/index.ts +130 -0
- package/src/client/locales.ts +88 -0
- package/src/client/panel.ts +217 -0
- package/src/client/react.d.ts +33 -0
- package/src/core.ts +252 -0
- package/src/index.ts +353 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/core.ts
|
|
4
|
+
/**
|
|
5
|
+
* The catalogue, in picker order.
|
|
6
|
+
* 语言目录,按选择器顺序排列。
|
|
7
|
+
*
|
|
8
|
+
* A tag that carries no region (`en`) is reachable through its bare primary
|
|
9
|
+
* subtag as well; one that does carry a region (`zh-CN`, `zh-TW`) is not. That
|
|
10
|
+
* asymmetry is what the resolution order in {@link tagForLocale} relies on, so
|
|
11
|
+
* it needs no extra flag on the entries.
|
|
12
|
+
* 不含地区的标签(`en`)也可以由裸主语言标签命中,含地区的(`zh-CN`、`zh-TW`)则
|
|
13
|
+
* 不能。{@link tagForLocale} 的解析顺序正是建立在这一不对称之上,因此条目本身不必
|
|
14
|
+
* 额外带标记。
|
|
15
|
+
*/
|
|
16
|
+
const CATALOGUE = [
|
|
17
|
+
{
|
|
18
|
+
tag: "zh-CN",
|
|
19
|
+
nativeName: "简体中文",
|
|
20
|
+
section: "推理规则:思考、推理与规划一律使用简体中文。回复用户时使用用户所用的语言。",
|
|
21
|
+
reminder: "继续用简体中文思考、推理与规划。"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
tag: "zh-TW",
|
|
25
|
+
nativeName: "繁體中文",
|
|
26
|
+
section: "推理規則:思考、推理與規劃一律使用繁體中文。回覆使用者時使用使用者所用的語言。",
|
|
27
|
+
reminder: "繼續用繁體中文思考、推理與規劃。"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
tag: "en",
|
|
31
|
+
nativeName: "English",
|
|
32
|
+
section: "Reasoning rules: think, reason, and plan in English. Reply to the user in the language they use.",
|
|
33
|
+
reminder: "Keep thinking, reasoning, and planning in English."
|
|
34
|
+
}
|
|
35
|
+
];
|
|
36
|
+
/** The sentinel meaning "follow the host locale". / 表示「跟随宿主语言」的哨兵值。 */
|
|
37
|
+
const AUTO = "auto";
|
|
38
|
+
/** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
|
|
39
|
+
const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
|
|
40
|
+
/** The one field this plugin stores. / 本插件唯一保存的字段。 */
|
|
41
|
+
const FIELD_LANGUAGE = "language";
|
|
42
|
+
CATALOGUE.map((language) => language.tag);
|
|
43
|
+
/** Configuration applied when the field is absent. / 字段缺失时采用的配置。 */
|
|
44
|
+
const DEFAULTS = { language: AUTO };
|
|
45
|
+
/** Catalogue lookup keyed by lower-cased tag. / 以小写标签为键的目录索引。 */
|
|
46
|
+
const BY_TAG = new Map(CATALOGUE.map((language) => [language.tag.toLowerCase(), language]));
|
|
47
|
+
/**
|
|
48
|
+
* Look up a catalogue entry by language tag.
|
|
49
|
+
* 按语言标签查找目录条目。
|
|
50
|
+
*/
|
|
51
|
+
function languageOf(tag) {
|
|
52
|
+
if (tag === "" || tag === "auto") return void 0;
|
|
53
|
+
return BY_TAG.get(tag.toLowerCase());
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Return the native name of a language tag, for display in the picker.
|
|
57
|
+
* 返回语言标签的本族语名,用于选择器展示。
|
|
58
|
+
*/
|
|
59
|
+
function nativeNameFor(tag) {
|
|
60
|
+
return languageOf(tag)?.nativeName;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Coerce an untrusted settings section into a complete configuration.
|
|
64
|
+
* 把不可信的设置段强制转换为完整配置。
|
|
65
|
+
*/
|
|
66
|
+
function normalizeSettings(raw) {
|
|
67
|
+
const stored = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw[FIELD_LANGUAGE] : void 0;
|
|
68
|
+
return { language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language };
|
|
69
|
+
}
|
|
70
|
+
/** Dictionaries keyed by UI locale, handed to the host locale service. / 以界面语言为键、交给宿主 locale 服务的字典。 */
|
|
71
|
+
const dictionaries = {
|
|
72
|
+
zh: {
|
|
73
|
+
heading: "思考语言",
|
|
74
|
+
description: "内部推理与规划使用的语言。「自动」优先跟随操作系统语言,其次是界面语言。",
|
|
75
|
+
autoLabel: "自动"
|
|
76
|
+
},
|
|
77
|
+
en: {
|
|
78
|
+
heading: "Thinking language",
|
|
79
|
+
description: "Language used for internal reasoning and planning. Automatic follows the operating-system language first, then the UI language.",
|
|
80
|
+
autoLabel: "Automatic"
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/client/panel.ts
|
|
85
|
+
/**
|
|
86
|
+
* The settings row: one language picker bound to the plugin's settings document.
|
|
87
|
+
* 设置行:一个绑定到本插件设置文档的语言选择器。
|
|
88
|
+
*
|
|
89
|
+
* It is registered into the General settings page (see `./index.ts`) rather than
|
|
90
|
+
* as a section of its own, so it sits directly under the host's language row.
|
|
91
|
+
* 它注册进「通用」设置页(见 `./index.ts`)而不是自成菜单,因此紧邻宿主自身的
|
|
92
|
+
* 语言设置项下方。
|
|
93
|
+
*
|
|
94
|
+
* The row owns no state of its own: it reads the host's settings document,
|
|
95
|
+
* subscribes to it, and writes the single field back.
|
|
96
|
+
* 本行不持有自己的状态:它读取宿主的设置文档、订阅其变化,并回写那唯一一个字段。
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* Read and normalize the current settings, falling back to defaults on refusal.
|
|
100
|
+
* 读取并归一化当前设置;被拒绝时回退到默认值。
|
|
101
|
+
*/
|
|
102
|
+
function readSettings(doc) {
|
|
103
|
+
try {
|
|
104
|
+
return normalizeSettings(doc.getSnapshot()?.value);
|
|
105
|
+
} catch {
|
|
106
|
+
return DEFAULTS;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Write one field back, swallowing a refusal so a read-only host cannot break the row.
|
|
111
|
+
* 回写单个字段,并吞掉拒绝异常,使只读宿主不会让这一行崩溃。
|
|
112
|
+
*
|
|
113
|
+
* A host may return a promise instead of a value; an unhandled rejection from a
|
|
114
|
+
* read-only document is not worth surfacing, so it is discarded here.
|
|
115
|
+
* 宿主可能返回 promise 而不是值;只读文档产生的未处理拒绝不值得上抛,因此在此丢弃。
|
|
116
|
+
*/
|
|
117
|
+
function writeField(doc, field, value) {
|
|
118
|
+
try {
|
|
119
|
+
const outcome = doc.set(field, value);
|
|
120
|
+
if (typeof outcome?.then === "function") outcome.then(void 0, () => {});
|
|
121
|
+
} catch {}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Build the row component bound to the host's UI primitives.
|
|
125
|
+
* 构建绑定到宿主 UI 原语的行组件。
|
|
126
|
+
*/
|
|
127
|
+
function createRow(primitives) {
|
|
128
|
+
const Menu = primitives?.Menu;
|
|
129
|
+
const Chevron = primitives?.IconChevronDownOutlineRegular ?? primitives?.IconChevronDownOutline14 ?? primitives?.IconChevronDownOutlineMedium;
|
|
130
|
+
return function ThinkingLanguageRow(props) {
|
|
131
|
+
const { doc, t: hostT } = props;
|
|
132
|
+
const [settings, setSettings] = (0, react.useState)(() => readSettings(doc));
|
|
133
|
+
const [open, setOpen] = (0, react.useState)(false);
|
|
134
|
+
settings.language;
|
|
135
|
+
const t = (key) => hostT(key);
|
|
136
|
+
(0, react.useEffect)(() => {
|
|
137
|
+
const sync = () => setSettings(readSettings(doc));
|
|
138
|
+
sync();
|
|
139
|
+
return doc.subscribe(sync);
|
|
140
|
+
}, [doc]);
|
|
141
|
+
const items = [{
|
|
142
|
+
id: AUTO,
|
|
143
|
+
label: t("autoLabel")
|
|
144
|
+
}, ...CATALOGUE.map((language) => ({
|
|
145
|
+
id: language.tag,
|
|
146
|
+
label: language.nativeName
|
|
147
|
+
}))];
|
|
148
|
+
const activeLabel = settings.language === "auto" ? t("autoLabel") : nativeNameFor(settings.language) ?? settings.language;
|
|
149
|
+
const close = () => setOpen(false);
|
|
150
|
+
const toggle = () => setOpen((value) => !value);
|
|
151
|
+
const choose = (id) => {
|
|
152
|
+
writeField(doc, FIELD_LANGUAGE, id);
|
|
153
|
+
close();
|
|
154
|
+
};
|
|
155
|
+
const picker = (0, react.createElement)("button", {
|
|
156
|
+
type: "button",
|
|
157
|
+
className: "dsh_desk_lang_picker",
|
|
158
|
+
"aria-haspopup": "menu",
|
|
159
|
+
"aria-expanded": open,
|
|
160
|
+
onClick: toggle
|
|
161
|
+
}, activeLabel, Chevron === void 0 ? null : (0, react.createElement)(Chevron, { className: "dsh_desk_lang_chevron" }));
|
|
162
|
+
const control = Menu ? (0, react.createElement)(Menu, {
|
|
163
|
+
open,
|
|
164
|
+
anchor: picker,
|
|
165
|
+
items,
|
|
166
|
+
selectedId: settings.language,
|
|
167
|
+
align: "end",
|
|
168
|
+
portal: true,
|
|
169
|
+
onClose: close,
|
|
170
|
+
onSelect: choose
|
|
171
|
+
}) : picker;
|
|
172
|
+
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);
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const rowCss = [
|
|
176
|
+
".dsh_desk_lang_row{border-bottom:.5px solid var(--dsw-alias-border-l2);align-items:center;gap:8px;padding:16px 0;display:flex}",
|
|
177
|
+
".dsh_desk_lang_text{flex-direction:column;flex:1;gap:4px;min-width:0;padding-right:48px;display:flex}",
|
|
178
|
+
".dsh_desk_lang_title{color:var(--dsw-alias-label-primary);font-size:14px;font-weight:400;line-height:22px}",
|
|
179
|
+
".dsh_desk_lang_desc{color:var(--dsw-alias-label-tertiary);font-size:12px;font-weight:400;line-height:18px}",
|
|
180
|
+
".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}",
|
|
181
|
+
".dsh_desk_lang_picker:hover{background:var(--dsw-alias-interactive-bg-hover)}",
|
|
182
|
+
".dsh_desk_lang_chevron{flex:none}"
|
|
183
|
+
].join("\n");
|
|
184
|
+
/** Marks the injected stylesheet, so a second mount can detect the first. / 标记已注入的样式表,使第二次挂载能发现第一次。 */
|
|
185
|
+
const STYLE_MARKER = "dsh-thinking-lang";
|
|
186
|
+
/**
|
|
187
|
+
* Inject the row stylesheet once per document, replacing a stale one.
|
|
188
|
+
* 每份文档只注入一次行样式表,已过期的那张会被替换。
|
|
189
|
+
*
|
|
190
|
+
* The guard reads the document rather than a module-level flag: a client plugin
|
|
191
|
+
* can be mounted more than once against the same page, and only the DOM knows
|
|
192
|
+
* whether this sheet is already there. Content is compared as well, because a
|
|
193
|
+
* hot-reloaded bundle keeps its marker while its CSS changes; skipping on the
|
|
194
|
+
* marker alone would leave the row styled by class names that no longer exist.
|
|
195
|
+
* 判重依据是文档本身而不是模块级标志:客户端插件可能被重复挂载到同一页面,只有 DOM
|
|
196
|
+
* 知道这张样式表是否已经存在。同时比对内容:热重载的 bundle 标记不变而 CSS 已变,
|
|
197
|
+
* 只看标记就跳过会让这一行按已不存在的类名取样式。
|
|
198
|
+
*/
|
|
199
|
+
function mountRowStyles() {
|
|
200
|
+
if (typeof document === "undefined") return;
|
|
201
|
+
const existing = document.head.querySelector(`style[data-plugin=${JSON.stringify(STYLE_MARKER)}]`);
|
|
202
|
+
if (existing !== null) {
|
|
203
|
+
if (existing.textContent === rowCss) return;
|
|
204
|
+
existing.remove();
|
|
205
|
+
}
|
|
206
|
+
const style = document.createElement("style");
|
|
207
|
+
style.dataset.plugin = STYLE_MARKER;
|
|
208
|
+
style.dataset.pluginCss = `${STYLE_MARKER}/row.css`;
|
|
209
|
+
style.textContent = rowCss;
|
|
210
|
+
document.head.append(style);
|
|
211
|
+
}
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/client/index.ts
|
|
214
|
+
/**
|
|
215
|
+
* Client entry: registers the plugin's settings row in General settings.
|
|
216
|
+
* 客户端入口:在「通用」设置页注册本插件的设置行。
|
|
217
|
+
*
|
|
218
|
+
* The row is registered through the `settings.general.item` slot, which the
|
|
219
|
+
* settings shell renders as one item on the General page — directly under the
|
|
220
|
+
* host's own language row.
|
|
221
|
+
* 该行通过 `settings.general.item` 插槽注册;设置外壳把它渲染成「通用」页上的
|
|
222
|
+
* 一个条目——紧邻宿主自身的语言设置项下方。
|
|
223
|
+
*/
|
|
224
|
+
/** Stable Cordis plugin name. / 稳定的 Cordis 插件名。 */
|
|
225
|
+
const name = "ay-dsh-tk-lang";
|
|
226
|
+
/** The one client service this plugin requires. / 本插件唯一需要的客户端服务。 */
|
|
227
|
+
const inject = ["slots"];
|
|
228
|
+
/** Locale namespace holding the row copy. / 保存行文案的 locale 命名空间。 */
|
|
229
|
+
const LOCALE_NS = "ay-dsh-tk-lang";
|
|
230
|
+
/**
|
|
231
|
+
* Position on the General page, sorted ascending. The host's own language row
|
|
232
|
+
* registers at `order: 0` (dsh-client-locale) and the next known row — chat's
|
|
233
|
+
* "link opening" — sits at 17, so 1 lands directly beneath the language row.
|
|
234
|
+
* 在「通用」页中的位置,按 order 升序排列。宿主自己的语言行注册在 `order: 0`
|
|
235
|
+
* (dsh-client-locale),下一个已知项——对话的「链接打开方式」——是 17,因此取
|
|
236
|
+
* 1 即可紧贴在语言行下方。
|
|
237
|
+
*/
|
|
238
|
+
const ROW_ORDER = 1;
|
|
239
|
+
/**
|
|
240
|
+
* Require one of several module ids, returning the first that resolves.
|
|
241
|
+
* 依次尝试 require 若干模块 id,返回第一个成功解析的结果。
|
|
242
|
+
*
|
|
243
|
+
* The host decides which optional modules exist, so a miss is expected rather
|
|
244
|
+
* than exceptional and simply moves on to the next candidate.
|
|
245
|
+
* 可选模块是否存在由宿主决定,因此未命中属于预期情况而非异常,直接尝试下一个即可。
|
|
246
|
+
*/
|
|
247
|
+
function tryRequire(...names) {
|
|
248
|
+
for (const name of names) try {
|
|
249
|
+
const mod = require(name);
|
|
250
|
+
if (mod != null) return mod;
|
|
251
|
+
} catch {}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Report a client-side problem through the host logger when one exists.
|
|
255
|
+
* 在宿主提供 logger 时,通过它报告客户端问题。
|
|
256
|
+
*/
|
|
257
|
+
function report(ctx, message, error) {
|
|
258
|
+
const logger = ctx.get("logger");
|
|
259
|
+
const line = `dsh-thinking-lang: ${message}`;
|
|
260
|
+
(typeof logger?.warn === "function" ? logger.warn.bind(logger) : console.warn)(line, ...error === void 0 ? [] : [error]);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Register the dictionaries, the settings document binding, and the General row.
|
|
264
|
+
* 注册字典、绑定设置文档,并登记「通用」页上的设置行。
|
|
265
|
+
*/
|
|
266
|
+
function apply(ctx) {
|
|
267
|
+
ctx.inject(["configForms", "locale"], (uiCtx) => {
|
|
268
|
+
const forms = uiCtx.get("configForms");
|
|
269
|
+
const locale = uiCtx.get("locale");
|
|
270
|
+
if (!forms || typeof forms.get !== "function") {
|
|
271
|
+
report(uiCtx, "configForms service unavailable; settings row disabled");
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (!locale || typeof locale.register !== "function" || typeof locale.bind !== "function") {
|
|
275
|
+
report(uiCtx, "locale service unavailable; settings row disabled");
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
uiCtx.effect(() => locale.register(LOCALE_NS, dictionaries), "dsh-thinking-lang: dictionaries");
|
|
279
|
+
const t = locale.bind(LOCALE_NS);
|
|
280
|
+
const doc = forms.get(SETTINGS_NAMESPACE);
|
|
281
|
+
const primitives = tryRequire("@deepseek-ai/dsh-client-ui-primitives");
|
|
282
|
+
mountRowStyles();
|
|
283
|
+
const Row = createRow(primitives);
|
|
284
|
+
uiCtx.slots.inject("settings.general.item", function* registerRow() {
|
|
285
|
+
yield uiCtx.slots.register({
|
|
286
|
+
name: "settings.general.item",
|
|
287
|
+
id: "ay-dsh-tk-lang",
|
|
288
|
+
order: ROW_ORDER,
|
|
289
|
+
locale: LOCALE_NS,
|
|
290
|
+
inject: () => ({
|
|
291
|
+
doc,
|
|
292
|
+
t
|
|
293
|
+
})
|
|
294
|
+
}, Row);
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
exports.apply = apply;
|
|
300
|
+
exports.inject = inject;
|
|
301
|
+
exports.name = name;
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# dsh-thinking-lang bundle patch.
|
|
2
|
+
#
|
|
3
|
+
# Registers the host entry: the `ay-dsh-tk-lang` settings namespace, the
|
|
4
|
+
# stable system-prompt section, and the per-step context line.
|
|
5
|
+
# 注册宿主入口:`ay-dsh-tk-lang` 设置命名空间、稳定的系统提示词段、
|
|
6
|
+
# 以及每一步注入的上下文行。
|
|
7
|
+
#
|
|
8
|
+
# The browser half is discovered automatically through the package's
|
|
9
|
+
# `dsh.client` declaration — the settings section needs no entry here.
|
|
10
|
+
# 浏览器端由包内的 `dsh.client` 声明自动发现,设置页无需在此登记。
|
|
11
|
+
|
|
12
|
+
- insert:
|
|
13
|
+
- id: ay-dsh-tk-lang
|
|
14
|
+
name: dsh-thinking-lang
|
package/lib/core.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language catalogue, settings normalization, and the two halves of the prompt.
|
|
3
|
+
* 语言目录、设置归一化,以及提示词的两个半边。
|
|
4
|
+
*
|
|
5
|
+
* Both halves state the rule in the selected language itself, so the prompt
|
|
6
|
+
* carries a matching language signal instead of an English language name:
|
|
7
|
+
* 两半都用所选语言本身写规则,提示词因此带有同语言信号,而不是英文语言名:
|
|
8
|
+
*
|
|
9
|
+
* - {@link sectionText} goes in as a `systemPrompt.section`: the authoritative
|
|
10
|
+
* rule, re-evaluated per assembly and committed when its text changes.
|
|
11
|
+
* - {@link reminderText} goes in through the per-turn pre-step reminder:
|
|
12
|
+
* re-evaluated on every model call and injected at most once per turn.
|
|
13
|
+
*
|
|
14
|
+
* - {@link sectionText} 作为 `systemPrompt.section` 注入:权威规则,每次组装
|
|
15
|
+
* 重新求值,文本变化时才提交。
|
|
16
|
+
* - {@link reminderText} 由每轮一次的 pre-step 提醒注入:每次模型调用重新求值,
|
|
17
|
+
* 每轮最多注入一次。
|
|
18
|
+
*
|
|
19
|
+
* Locale resolution is delegated to the platform's own `Intl.Locale` rather than
|
|
20
|
+
* to a hand-rolled BCP-47 parser: the runtime already knows how to split a tag
|
|
21
|
+
* into language, script and region, and CLDR already knows which script a region
|
|
22
|
+
* implies. Neither the tag grammar nor the Simplified/Traditional split needs a
|
|
23
|
+
* table of its own here.
|
|
24
|
+
* 语言解析交给平台自带的 `Intl.Locale`,而不是自己写 BCP-47 解析器:运行时本来就
|
|
25
|
+
* 会把标签拆成语言、文字、地区,CLDR 本来就知道某个地区隐含哪种文字。因此这里既
|
|
26
|
+
* 不需要标签文法,也不需要简繁对照表。
|
|
27
|
+
*/
|
|
28
|
+
/** One selectable reasoning language. / 一种可选的推理语言。 */
|
|
29
|
+
export interface Language {
|
|
30
|
+
/** BCP-47 tag; also the value persisted in settings. / BCP-47 标签;同时也是存入设置的值。 */
|
|
31
|
+
tag: string;
|
|
32
|
+
/** Native name shown in the settings picker. / 设置选择器里显示的本族语名。 */
|
|
33
|
+
nativeName: string;
|
|
34
|
+
/** The rule sentence written in this language. / 用本语言写成的规则句。 */
|
|
35
|
+
section: string;
|
|
36
|
+
/** The short per-turn reminder written in this language. / 用本语言写成的每轮短提醒。 */
|
|
37
|
+
reminder: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The catalogue, in picker order.
|
|
41
|
+
* 语言目录,按选择器顺序排列。
|
|
42
|
+
*
|
|
43
|
+
* A tag that carries no region (`en`) is reachable through its bare primary
|
|
44
|
+
* subtag as well; one that does carry a region (`zh-CN`, `zh-TW`) is not. That
|
|
45
|
+
* asymmetry is what the resolution order in {@link tagForLocale} relies on, so
|
|
46
|
+
* it needs no extra flag on the entries.
|
|
47
|
+
* 不含地区的标签(`en`)也可以由裸主语言标签命中,含地区的(`zh-CN`、`zh-TW`)则
|
|
48
|
+
* 不能。{@link tagForLocale} 的解析顺序正是建立在这一不对称之上,因此条目本身不必
|
|
49
|
+
* 额外带标记。
|
|
50
|
+
*/
|
|
51
|
+
export declare const CATALOGUE: readonly Language[];
|
|
52
|
+
/** The sentinel meaning "follow the host locale". / 表示「跟随宿主语言」的哨兵值。 */
|
|
53
|
+
export declare const AUTO = "auto";
|
|
54
|
+
/** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
|
|
55
|
+
export declare const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
|
|
56
|
+
/** The one field this plugin stores. / 本插件唯一保存的字段。 */
|
|
57
|
+
export declare const FIELD_LANGUAGE = "language";
|
|
58
|
+
/** Every value the picker offers besides {@link AUTO}. / 选择器在 {@link AUTO} 之外提供的全部取值。 */
|
|
59
|
+
export declare const SELECTABLE_TAGS: readonly string[];
|
|
60
|
+
/** The resolved configuration this plugin acts on. / 本插件实际作用的已解析配置。 */
|
|
61
|
+
export interface ThinkingSettings {
|
|
62
|
+
/** Selected language tag, or {@link AUTO}. / 所选语言标签,或 {@link AUTO}。 */
|
|
63
|
+
language: string;
|
|
64
|
+
}
|
|
65
|
+
/** Configuration applied when the field is absent. / 字段缺失时采用的配置。 */
|
|
66
|
+
export declare const DEFAULTS: ThinkingSettings;
|
|
67
|
+
/** A minimal read handle over the host settings service. / 宿主设置服务的最小读取句柄。 */
|
|
68
|
+
export interface Reader {
|
|
69
|
+
get(ns?: string): unknown;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Return the native name of a language tag, for display in the picker.
|
|
73
|
+
* 返回语言标签的本族语名,用于选择器展示。
|
|
74
|
+
*/
|
|
75
|
+
export declare function nativeNameFor(tag: string): string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Coerce an untrusted settings section into a complete configuration.
|
|
78
|
+
* 把不可信的设置段强制转换为完整配置。
|
|
79
|
+
*/
|
|
80
|
+
export declare function normalizeSettings(raw: unknown): ThinkingSettings;
|
|
81
|
+
/**
|
|
82
|
+
* Resolve the effective language for `auto`, most authoritative source first.
|
|
83
|
+
* 解析 `auto` 下的生效语言,来源按权威性排序。
|
|
84
|
+
*
|
|
85
|
+
* Sources are tried in order: the operating-system locale, then the desktop UI
|
|
86
|
+
* locale, then the fallback. The first one that maps onto the catalogue wins, so
|
|
87
|
+
* a machine whose system language is Chinese keeps the agent thinking in Chinese
|
|
88
|
+
* even when the DSH interface itself is English.
|
|
89
|
+
* 来源按顺序尝试:操作系统语言 → 桌面界面语言 → 兜底。第一个能映射到语言目录的胜出
|
|
90
|
+
* ——因此系统语言为中文时,即使 DSH 界面是英文,智能体仍会用中文思考。
|
|
91
|
+
*
|
|
92
|
+
* Returns an empty string when the stored tag is neither `auto` nor known, which
|
|
93
|
+
* callers read as "inject nothing".
|
|
94
|
+
* 当存储的标签既不是 `auto` 也不是已知语言时返回空串,调用方据此判定「不注入」。
|
|
95
|
+
*/
|
|
96
|
+
export declare function effectiveCode(settings: ThinkingSettings, osLocale?: string, desktopLocale?: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* Build the system-prompt rule sentence in the selected language.
|
|
99
|
+
* 用所选语言生成系统提示词规则句。
|
|
100
|
+
*
|
|
101
|
+
* The text varies with the selection — one variant per language.
|
|
102
|
+
* 文本随所选语言变化,每种语言一个版本。
|
|
103
|
+
*/
|
|
104
|
+
export declare function sectionText(code: string): string;
|
|
105
|
+
/**
|
|
106
|
+
* Build the short per-turn reminder in the selected language.
|
|
107
|
+
* 用所选语言生成每轮短提醒。
|
|
108
|
+
*/
|
|
109
|
+
export declare function reminderText(code: string): string;
|
package/lib/core.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language catalogue, settings normalization, and the two halves of the prompt.
|
|
3
|
+
* 语言目录、设置归一化,以及提示词的两个半边。
|
|
4
|
+
*
|
|
5
|
+
* Both halves state the rule in the selected language itself, so the prompt
|
|
6
|
+
* carries a matching language signal instead of an English language name:
|
|
7
|
+
* 两半都用所选语言本身写规则,提示词因此带有同语言信号,而不是英文语言名:
|
|
8
|
+
*
|
|
9
|
+
* - {@link sectionText} goes in as a `systemPrompt.section`: the authoritative
|
|
10
|
+
* rule, re-evaluated per assembly and committed when its text changes.
|
|
11
|
+
* - {@link reminderText} goes in through the per-turn pre-step reminder:
|
|
12
|
+
* re-evaluated on every model call and injected at most once per turn.
|
|
13
|
+
*
|
|
14
|
+
* - {@link sectionText} 作为 `systemPrompt.section` 注入:权威规则,每次组装
|
|
15
|
+
* 重新求值,文本变化时才提交。
|
|
16
|
+
* - {@link reminderText} 由每轮一次的 pre-step 提醒注入:每次模型调用重新求值,
|
|
17
|
+
* 每轮最多注入一次。
|
|
18
|
+
*
|
|
19
|
+
* Locale resolution is delegated to the platform's own `Intl.Locale` rather than
|
|
20
|
+
* to a hand-rolled BCP-47 parser: the runtime already knows how to split a tag
|
|
21
|
+
* into language, script and region, and CLDR already knows which script a region
|
|
22
|
+
* implies. Neither the tag grammar nor the Simplified/Traditional split needs a
|
|
23
|
+
* table of its own here.
|
|
24
|
+
* 语言解析交给平台自带的 `Intl.Locale`,而不是自己写 BCP-47 解析器:运行时本来就
|
|
25
|
+
* 会把标签拆成语言、文字、地区,CLDR 本来就知道某个地区隐含哪种文字。因此这里既
|
|
26
|
+
* 不需要标签文法,也不需要简繁对照表。
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* The catalogue, in picker order.
|
|
30
|
+
* 语言目录,按选择器顺序排列。
|
|
31
|
+
*
|
|
32
|
+
* A tag that carries no region (`en`) is reachable through its bare primary
|
|
33
|
+
* subtag as well; one that does carry a region (`zh-CN`, `zh-TW`) is not. That
|
|
34
|
+
* asymmetry is what the resolution order in {@link tagForLocale} relies on, so
|
|
35
|
+
* it needs no extra flag on the entries.
|
|
36
|
+
* 不含地区的标签(`en`)也可以由裸主语言标签命中,含地区的(`zh-CN`、`zh-TW`)则
|
|
37
|
+
* 不能。{@link tagForLocale} 的解析顺序正是建立在这一不对称之上,因此条目本身不必
|
|
38
|
+
* 额外带标记。
|
|
39
|
+
*/
|
|
40
|
+
export const CATALOGUE = [
|
|
41
|
+
{
|
|
42
|
+
tag: "zh-CN",
|
|
43
|
+
nativeName: "简体中文",
|
|
44
|
+
section: "推理规则:思考、推理与规划一律使用简体中文。回复用户时使用用户所用的语言。",
|
|
45
|
+
reminder: "继续用简体中文思考、推理与规划。"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
tag: "zh-TW",
|
|
49
|
+
nativeName: "繁體中文",
|
|
50
|
+
section: "推理規則:思考、推理與規劃一律使用繁體中文。回覆使用者時使用使用者所用的語言。",
|
|
51
|
+
reminder: "繼續用繁體中文思考、推理與規劃。"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
tag: "en",
|
|
55
|
+
nativeName: "English",
|
|
56
|
+
section: "Reasoning rules: think, reason, and plan in English. Reply to the user in the language they use.",
|
|
57
|
+
reminder: "Keep thinking, reasoning, and planning in English."
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
/** The sentinel meaning "follow the host locale". / 表示「跟随宿主语言」的哨兵值。 */
|
|
61
|
+
export const AUTO = "auto";
|
|
62
|
+
/** Language used when no other source resolves. / 其它来源都解析不出时的兜底语言。 */
|
|
63
|
+
const FALLBACK_TAG = "zh-CN";
|
|
64
|
+
/** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
|
|
65
|
+
export const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
|
|
66
|
+
/** The one field this plugin stores. / 本插件唯一保存的字段。 */
|
|
67
|
+
export const FIELD_LANGUAGE = "language";
|
|
68
|
+
/** Every value the picker offers besides {@link AUTO}. / 选择器在 {@link AUTO} 之外提供的全部取值。 */
|
|
69
|
+
export const SELECTABLE_TAGS = CATALOGUE.map((language) => language.tag);
|
|
70
|
+
/** Configuration applied when the field is absent. / 字段缺失时采用的配置。 */
|
|
71
|
+
export const DEFAULTS = { language: AUTO };
|
|
72
|
+
/** Catalogue lookup keyed by lower-cased tag. / 以小写标签为键的目录索引。 */
|
|
73
|
+
const BY_TAG = new Map(CATALOGUE.map((language) => [language.tag.toLowerCase(), language]));
|
|
74
|
+
/**
|
|
75
|
+
* The two Chinese tags, which the script — not the region — decides between.
|
|
76
|
+
* 两个中文标签;区分它们的是文字,而不是地区。
|
|
77
|
+
*/
|
|
78
|
+
const CHINESE = { simplified: "zh-CN", traditional: "zh-TW" };
|
|
79
|
+
/**
|
|
80
|
+
* Look up a catalogue entry by language tag.
|
|
81
|
+
* 按语言标签查找目录条目。
|
|
82
|
+
*/
|
|
83
|
+
function languageOf(tag) {
|
|
84
|
+
if (tag === "" || tag === AUTO)
|
|
85
|
+
return undefined;
|
|
86
|
+
return BY_TAG.get(tag.toLowerCase());
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Return the native name of a language tag, for display in the picker.
|
|
90
|
+
* 返回语言标签的本族语名,用于选择器展示。
|
|
91
|
+
*/
|
|
92
|
+
export function nativeNameFor(tag) {
|
|
93
|
+
return languageOf(tag)?.nativeName;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse an untrusted string into a modern `Intl.Locale`, or nothing.
|
|
97
|
+
* 把不可信的字符串解析成现代 `Intl.Locale`;解析不出则为空。
|
|
98
|
+
*
|
|
99
|
+
* POSIX spellings such as `zh_CN.UTF-8` or `sr_RS@latin` are not BCP-47, so the
|
|
100
|
+
* encoding and modifier halves are dropped and the separator is normalized
|
|
101
|
+
* before the tag reaches `Intl`. A tag `Intl` rejects is not an error here: the
|
|
102
|
+
* caller simply moves on to the next locale source.
|
|
103
|
+
* POSIX 写法(如 `zh_CN.UTF-8`、`sr_RS@latin`)不是 BCP-47,因此先去掉了编码与修饰
|
|
104
|
+
* 符部分、归一化分隔符,再把标签交给 `Intl`。被 `Intl` 拒绝的标签在这里不算错误,
|
|
105
|
+
* 调用方继续尝试下一个语言来源即可。
|
|
106
|
+
*/
|
|
107
|
+
function parseLocale(raw) {
|
|
108
|
+
if (typeof raw !== "string")
|
|
109
|
+
return undefined;
|
|
110
|
+
const tag = raw.trim().split(/[.@]/)[0]?.replace(/_/g, "-") ?? "";
|
|
111
|
+
if (tag === "")
|
|
112
|
+
return undefined;
|
|
113
|
+
try {
|
|
114
|
+
return new Intl.Locale(tag);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* List the tags worth looking up for one parsed locale, most specific first.
|
|
122
|
+
* 为一个已解析的语言环境列出值得查找的标签,从最具体开始。
|
|
123
|
+
*
|
|
124
|
+
* `zh-Hant-TW` yields `zh-Hant-TW`, `zh-Hant`, `zh-TW`, `zh`; `en-US` yields
|
|
125
|
+
* `en-US`, `en`.
|
|
126
|
+
* `zh-Hant-TW` 会得到 `zh-Hant-TW`、`zh-Hant`、`zh-TW`、`zh`;`en-US` 会得到
|
|
127
|
+
* `en-US`、`en`。
|
|
128
|
+
*/
|
|
129
|
+
function candidatesOf(locale) {
|
|
130
|
+
const { language, script, region } = locale;
|
|
131
|
+
const candidates = [];
|
|
132
|
+
if (script !== undefined && region !== undefined)
|
|
133
|
+
candidates.push(`${language}-${script}-${region}`);
|
|
134
|
+
if (script !== undefined)
|
|
135
|
+
candidates.push(`${language}-${script}`);
|
|
136
|
+
if (region !== undefined)
|
|
137
|
+
candidates.push(`${language}-${region}`);
|
|
138
|
+
candidates.push(language);
|
|
139
|
+
return candidates;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Decide which Chinese tag a script-ambiguous locale means.
|
|
143
|
+
* 判定一个文字不明确的中文语言环境对应哪个标签。
|
|
144
|
+
*
|
|
145
|
+
* `zh`, `zh-SG` and `zh-Hans` all mean Simplified, while `zh-TW`, `zh-HK` and
|
|
146
|
+
* `zh-Hant` mean Traditional. CLDR already encodes that mapping, so `maximize()`
|
|
147
|
+
* supplies the script a short tag omitted instead of a hand-written region table.
|
|
148
|
+
* `zh`、`zh-SG`、`zh-Hans` 都指简体,而 `zh-TW`、`zh-HK`、`zh-Hant` 都指繁体。
|
|
149
|
+
* CLDR 已经把这个映射编码好了,因此用 `maximize()` 补全短标签省略的文字,而不必
|
|
150
|
+
* 手写地区对照表。
|
|
151
|
+
*/
|
|
152
|
+
function chineseTag(locale) {
|
|
153
|
+
const maximized = typeof locale.maximize === "function" ? locale.maximize() : undefined;
|
|
154
|
+
return maximized?.script === "Hant" ? CHINESE.traditional : CHINESE.simplified;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Map a locale onto a catalogue language tag.
|
|
158
|
+
* 把语言环境映射到目录里的语言标签。
|
|
159
|
+
*/
|
|
160
|
+
function tagForLocale(raw) {
|
|
161
|
+
const locale = parseLocale(raw);
|
|
162
|
+
if (locale === undefined)
|
|
163
|
+
return undefined;
|
|
164
|
+
for (const candidate of candidatesOf(locale)) {
|
|
165
|
+
const match = BY_TAG.get(candidate.toLowerCase());
|
|
166
|
+
if (match !== undefined)
|
|
167
|
+
return match.tag;
|
|
168
|
+
}
|
|
169
|
+
return locale.language === "zh" ? chineseTag(locale) : undefined;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Coerce an untrusted settings section into a complete configuration.
|
|
173
|
+
* 把不可信的设置段强制转换为完整配置。
|
|
174
|
+
*/
|
|
175
|
+
export function normalizeSettings(raw) {
|
|
176
|
+
const isSection = raw !== null && typeof raw === "object" && !Array.isArray(raw);
|
|
177
|
+
const stored = isSection ? raw[FIELD_LANGUAGE] : undefined;
|
|
178
|
+
return { language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Resolve the effective language for `auto`, most authoritative source first.
|
|
182
|
+
* 解析 `auto` 下的生效语言,来源按权威性排序。
|
|
183
|
+
*
|
|
184
|
+
* Sources are tried in order: the operating-system locale, then the desktop UI
|
|
185
|
+
* locale, then the fallback. The first one that maps onto the catalogue wins, so
|
|
186
|
+
* a machine whose system language is Chinese keeps the agent thinking in Chinese
|
|
187
|
+
* even when the DSH interface itself is English.
|
|
188
|
+
* 来源按顺序尝试:操作系统语言 → 桌面界面语言 → 兜底。第一个能映射到语言目录的胜出
|
|
189
|
+
* ——因此系统语言为中文时,即使 DSH 界面是英文,智能体仍会用中文思考。
|
|
190
|
+
*
|
|
191
|
+
* Returns an empty string when the stored tag is neither `auto` nor known, which
|
|
192
|
+
* callers read as "inject nothing".
|
|
193
|
+
* 当存储的标签既不是 `auto` 也不是已知语言时返回空串,调用方据此判定「不注入」。
|
|
194
|
+
*/
|
|
195
|
+
export function effectiveCode(settings, osLocale, desktopLocale) {
|
|
196
|
+
if (settings.language !== AUTO)
|
|
197
|
+
return languageOf(settings.language)?.tag ?? "";
|
|
198
|
+
for (const source of [osLocale, desktopLocale, FALLBACK_TAG]) {
|
|
199
|
+
const resolved = tagForLocale(source);
|
|
200
|
+
if (resolved !== undefined)
|
|
201
|
+
return resolved;
|
|
202
|
+
}
|
|
203
|
+
return FALLBACK_TAG;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Build the system-prompt rule sentence in the selected language.
|
|
207
|
+
* 用所选语言生成系统提示词规则句。
|
|
208
|
+
*
|
|
209
|
+
* The text varies with the selection — one variant per language.
|
|
210
|
+
* 文本随所选语言变化,每种语言一个版本。
|
|
211
|
+
*/
|
|
212
|
+
export function sectionText(code) {
|
|
213
|
+
return languageOf(code)?.section ?? "";
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Build the short per-turn reminder in the selected language.
|
|
217
|
+
* 用所选语言生成每轮短提醒。
|
|
218
|
+
*/
|
|
219
|
+
export function reminderText(code) {
|
|
220
|
+
return languageOf(code)?.reminder ?? "";
|
|
221
|
+
}
|