dsh-thinking-lang 0.0.0-stage → 1.0.5
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,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings row: one language picker bound to the plugin's settings document.
|
|
3
|
+
* 设置行:一个绑定到本插件设置文档的语言选择器。
|
|
4
|
+
*
|
|
5
|
+
* It is registered into the General settings page (see `./index.ts`) rather than
|
|
6
|
+
* as a section of its own, so it sits directly under the host's language row.
|
|
7
|
+
* 它注册进「通用」设置页(见 `./index.ts`)而不是自成菜单,因此紧邻宿主自身的
|
|
8
|
+
* 语言设置项下方。
|
|
9
|
+
*
|
|
10
|
+
* The row owns no state of its own: it reads the host's settings document,
|
|
11
|
+
* subscribes to it, and writes the single field back.
|
|
12
|
+
* 本行不持有自己的状态:它读取宿主的设置文档、订阅其变化,并回写那唯一一个字段。
|
|
13
|
+
*/
|
|
14
|
+
import { createElement as h, useEffect, useState } from "react";
|
|
15
|
+
import {
|
|
16
|
+
AUTO,
|
|
17
|
+
CATALOGUE,
|
|
18
|
+
DEFAULTS,
|
|
19
|
+
FIELD_LANGUAGE,
|
|
20
|
+
type ThinkingSettings,
|
|
21
|
+
nativeNameFor,
|
|
22
|
+
normalizeSettings
|
|
23
|
+
} from "../core.js";
|
|
24
|
+
import { dictionaryFor, type Dict } from "./locales.js";
|
|
25
|
+
|
|
26
|
+
/** The settings document handle the host hands to a client plugin. / 宿主交给客户端插件的设置文档句柄。 */
|
|
27
|
+
export interface SettingsDoc {
|
|
28
|
+
getSnapshot(): { value?: unknown; revision?: number };
|
|
29
|
+
subscribe(listener: () => void): () => void;
|
|
30
|
+
set(field: string, value: unknown): unknown;
|
|
31
|
+
unset(field: string): unknown;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** UI primitives this row can use when the host provides them. / 宿主提供时本行可用的 UI 原语。 */
|
|
35
|
+
export interface UiPrimitives {
|
|
36
|
+
Menu?: unknown;
|
|
37
|
+
IconChevronDownOutlineRegular?: unknown;
|
|
38
|
+
IconChevronDownOutline14?: unknown;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What the slot hands to the row component. / 插槽交给行组件的东西。 */
|
|
43
|
+
interface RowBinding {
|
|
44
|
+
/** Live settings document for the plugin namespace. / 插件命名空间的实时设置文档。 */
|
|
45
|
+
doc: SettingsDoc;
|
|
46
|
+
/** Bound translator for the host locale namespace (the UI language). / 绑定到宿主 locale 命名空间(界面语言)的翻译函数。 */
|
|
47
|
+
t: (key: keyof Dict) => string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Read and normalize the current settings, falling back to defaults on refusal.
|
|
52
|
+
* 读取并归一化当前设置;被拒绝时回退到默认值。
|
|
53
|
+
*/
|
|
54
|
+
function readSettings(doc: SettingsDoc): ThinkingSettings {
|
|
55
|
+
try {
|
|
56
|
+
return normalizeSettings(doc.getSnapshot()?.value);
|
|
57
|
+
} catch {
|
|
58
|
+
return DEFAULTS;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Write one field back, swallowing a refusal so a read-only host cannot break the row.
|
|
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
|
+
*/
|
|
70
|
+
function writeField(doc: SettingsDoc, field: string, value: unknown): void {
|
|
71
|
+
try {
|
|
72
|
+
const outcome = doc.set(field, value) as { then?: unknown } | null | undefined;
|
|
73
|
+
if (typeof outcome?.then === "function") {
|
|
74
|
+
void (outcome as Promise<unknown>).then(undefined, () => {});
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
/* a read-only settings document is not an error worth surfacing here */
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Build the row component bound to the host's UI primitives.
|
|
83
|
+
* 构建绑定到宿主 UI 原语的行组件。
|
|
84
|
+
*/
|
|
85
|
+
export function createRow(primitives: UiPrimitives | undefined): (props: RowBinding) => unknown {
|
|
86
|
+
const Menu = primitives?.Menu;
|
|
87
|
+
const Chevron =
|
|
88
|
+
primitives?.IconChevronDownOutlineRegular ??
|
|
89
|
+
primitives?.IconChevronDownOutline14 ??
|
|
90
|
+
primitives?.IconChevronDownOutlineMedium;
|
|
91
|
+
|
|
92
|
+
return function ThinkingLanguageRow(props: RowBinding): unknown {
|
|
93
|
+
const { doc, t: hostT } = props;
|
|
94
|
+
const [settings, setSettings] = useState<ThinkingSettings>(() => readSettings(doc));
|
|
95
|
+
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
|
+
const items = [
|
|
115
|
+
{ id: AUTO, label: t("autoLabel") },
|
|
116
|
+
...CATALOGUE.map((language) => ({ id: language.tag, label: language.nativeName }))
|
|
117
|
+
];
|
|
118
|
+
const activeLabel =
|
|
119
|
+
settings.language === AUTO ? t("autoLabel") : nativeNameFor(settings.language) ?? settings.language;
|
|
120
|
+
|
|
121
|
+
const close = (): void => setOpen(false);
|
|
122
|
+
const toggle = (): void => setOpen((value: boolean) => !value);
|
|
123
|
+
const choose = (id: string): void => {
|
|
124
|
+
writeField(doc, FIELD_LANGUAGE, id);
|
|
125
|
+
close();
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const picker = h(
|
|
129
|
+
"button",
|
|
130
|
+
{
|
|
131
|
+
type: "button",
|
|
132
|
+
className: "dsh_desk_lang_picker",
|
|
133
|
+
"aria-haspopup": "menu",
|
|
134
|
+
"aria-expanded": open,
|
|
135
|
+
onClick: toggle
|
|
136
|
+
},
|
|
137
|
+
activeLabel,
|
|
138
|
+
Chevron === undefined ? null : h(Chevron, { className: "dsh_desk_lang_chevron" })
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const control = Menu
|
|
142
|
+
? h(Menu, {
|
|
143
|
+
open,
|
|
144
|
+
anchor: picker,
|
|
145
|
+
items,
|
|
146
|
+
selectedId: settings.language,
|
|
147
|
+
align: "end",
|
|
148
|
+
portal: true,
|
|
149
|
+
onClose: close,
|
|
150
|
+
onSelect: choose
|
|
151
|
+
})
|
|
152
|
+
: picker;
|
|
153
|
+
|
|
154
|
+
return h(
|
|
155
|
+
"div",
|
|
156
|
+
{ className: "dsh_desk_lang_row" },
|
|
157
|
+
h(
|
|
158
|
+
"div",
|
|
159
|
+
{ 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"))
|
|
162
|
+
),
|
|
163
|
+
control
|
|
164
|
+
);
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Declaration order follows the host's own PreferenceRow verbatim —
|
|
169
|
+
// 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, so
|
|
171
|
+
// the two can be diffed property by property whenever the host styles move.
|
|
172
|
+
// Class names differ (the host's are CSS-Modules hashes) and the picker carries a
|
|
173
|
+
// trailing `flex:none` the host does not need; every other character matches.
|
|
174
|
+
// 声明顺序逐字照宿主自己的 PreferenceRow——
|
|
175
|
+
// packages/client/ui-chat/src/client/settings/PreferenceRow.module.css,MIT,
|
|
176
|
+
// Copyright (c) 2026 DeepSeek——因为那就是本行紧邻的邻居,宿主样式变动时可以逐属性
|
|
177
|
+
// 对照。类名不同(宿主的是 CSS Modules 哈希),且选择器末尾多一个宿主不需要的
|
|
178
|
+
// `flex:none`;除此之外逐字符一致。
|
|
179
|
+
const rowCss = [
|
|
180
|
+
".dsh_desk_lang_row{border-bottom:.5px solid var(--dsw-alias-border-l2);align-items:center;gap:8px;padding:16px 0;display:flex}",
|
|
181
|
+
".dsh_desk_lang_text{flex-direction:column;flex:1;gap:4px;min-width:0;padding-right:48px;display:flex}",
|
|
182
|
+
".dsh_desk_lang_title{color:var(--dsw-alias-label-primary);font-size:14px;font-weight:400;line-height:22px}",
|
|
183
|
+
".dsh_desk_lang_desc{color:var(--dsw-alias-label-tertiary);font-size:12px;font-weight:400;line-height:18px}",
|
|
184
|
+
".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
|
+
".dsh_desk_lang_picker:hover{background:var(--dsw-alias-interactive-bg-hover)}",
|
|
186
|
+
".dsh_desk_lang_chevron{flex:none}"
|
|
187
|
+
].join("\n");
|
|
188
|
+
|
|
189
|
+
/** Marks the injected stylesheet, so a second mount can detect the first. / 标记已注入的样式表,使第二次挂载能发现第一次。 */
|
|
190
|
+
const STYLE_MARKER = "dsh-thinking-lang";
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Inject the row stylesheet once per document, replacing a stale one.
|
|
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
|
+
* 只看标记就跳过会让这一行按已不存在的类名取样式。
|
|
204
|
+
*/
|
|
205
|
+
export function mountRowStyles(): void {
|
|
206
|
+
if (typeof document === "undefined") return;
|
|
207
|
+
const existing = document.head.querySelector<HTMLStyleElement>(`style[data-plugin=${JSON.stringify(STYLE_MARKER)}]`);
|
|
208
|
+
if (existing !== null) {
|
|
209
|
+
if (existing.textContent === rowCss) return;
|
|
210
|
+
existing.remove();
|
|
211
|
+
}
|
|
212
|
+
const style = document.createElement("style");
|
|
213
|
+
style.dataset.plugin = STYLE_MARKER;
|
|
214
|
+
style.dataset.pluginCss = `${STYLE_MARKER}/row.css`;
|
|
215
|
+
style.textContent = rowCss;
|
|
216
|
+
document.head.append(style);
|
|
217
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal ambient types for the host-provided React runtime.
|
|
3
|
+
* 为宿主提供的 React 运行时声明最小环境类型。
|
|
4
|
+
*
|
|
5
|
+
* The plugin never bundles React — the DSH client module loader injects it — and
|
|
6
|
+
* this package deliberately does not depend on `@types/react`, so only the three
|
|
7
|
+
* APIs the panel actually calls are declared here.
|
|
8
|
+
* 本插件不打包 React(由 DSH 客户端模块加载器注入),且刻意不依赖
|
|
9
|
+
* `@types/react`,因此这里只声明面板真正调用的三个 API。
|
|
10
|
+
*/
|
|
11
|
+
declare module "react" {
|
|
12
|
+
/**
|
|
13
|
+
* Create one element of any component type.
|
|
14
|
+
* 创建任意组件类型的一个元素。
|
|
15
|
+
*/
|
|
16
|
+
export function createElement(
|
|
17
|
+
type: unknown,
|
|
18
|
+
props: Record<string, unknown> | null,
|
|
19
|
+
...children: unknown[]
|
|
20
|
+
): unknown;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Hold one piece of component state.
|
|
24
|
+
* 保存一份组件状态。
|
|
25
|
+
*/
|
|
26
|
+
export function useState<T>(initial: T | (() => T)): [T, (next: T | ((prev: T) => T)) => void];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Run a side effect after render, optionally cleaning it up on unmount.
|
|
30
|
+
* 在渲染后运行副作用,并可选地在卸载时清理。
|
|
31
|
+
*/
|
|
32
|
+
export function useEffect(effect: () => void | (() => void), deps?: unknown[]): void;
|
|
33
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
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
|
+
/** One selectable reasoning language. / 一种可选的推理语言。 */
|
|
30
|
+
export interface Language {
|
|
31
|
+
/** BCP-47 tag; also the value persisted in settings. / BCP-47 标签;同时也是存入设置的值。 */
|
|
32
|
+
tag: string;
|
|
33
|
+
/** Native name shown in the settings picker. / 设置选择器里显示的本族语名。 */
|
|
34
|
+
nativeName: string;
|
|
35
|
+
/** The rule sentence written in this language. / 用本语言写成的规则句。 */
|
|
36
|
+
section: string;
|
|
37
|
+
/** The short per-turn reminder written in this language. / 用本语言写成的每轮短提醒。 */
|
|
38
|
+
reminder: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The catalogue, in picker order.
|
|
43
|
+
* 语言目录,按选择器顺序排列。
|
|
44
|
+
*
|
|
45
|
+
* A tag that carries no region (`en`) is reachable through its bare primary
|
|
46
|
+
* subtag as well; one that does carry a region (`zh-CN`, `zh-TW`) is not. That
|
|
47
|
+
* asymmetry is what the resolution order in {@link tagForLocale} relies on, so
|
|
48
|
+
* it needs no extra flag on the entries.
|
|
49
|
+
* 不含地区的标签(`en`)也可以由裸主语言标签命中,含地区的(`zh-CN`、`zh-TW`)则
|
|
50
|
+
* 不能。{@link tagForLocale} 的解析顺序正是建立在这一不对称之上,因此条目本身不必
|
|
51
|
+
* 额外带标记。
|
|
52
|
+
*/
|
|
53
|
+
export const CATALOGUE: readonly Language[] = [
|
|
54
|
+
{
|
|
55
|
+
tag: "zh-CN",
|
|
56
|
+
nativeName: "简体中文",
|
|
57
|
+
section: "推理规则:思考、推理与规划一律使用简体中文。回复用户时使用用户所用的语言。",
|
|
58
|
+
reminder: "继续用简体中文思考、推理与规划。"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
tag: "zh-TW",
|
|
62
|
+
nativeName: "繁體中文",
|
|
63
|
+
section: "推理規則:思考、推理與規劃一律使用繁體中文。回覆使用者時使用使用者所用的語言。",
|
|
64
|
+
reminder: "繼續用繁體中文思考、推理與規劃。"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
tag: "en",
|
|
68
|
+
nativeName: "English",
|
|
69
|
+
section: "Reasoning rules: think, reason, and plan in English. Reply to the user in the language they use.",
|
|
70
|
+
reminder: "Keep thinking, reasoning, and planning in English."
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
/** The sentinel meaning "follow the host locale". / 表示「跟随宿主语言」的哨兵值。 */
|
|
75
|
+
export const AUTO = "auto";
|
|
76
|
+
|
|
77
|
+
/** Language used when no other source resolves. / 其它来源都解析不出时的兜底语言。 */
|
|
78
|
+
const FALLBACK_TAG = "zh-CN";
|
|
79
|
+
|
|
80
|
+
/** Settings namespace owned by this plugin. / 本插件拥有的设置命名空间。 */
|
|
81
|
+
export const SETTINGS_NAMESPACE = "ay-dsh-tk-lang";
|
|
82
|
+
|
|
83
|
+
/** The one field this plugin stores. / 本插件唯一保存的字段。 */
|
|
84
|
+
export const FIELD_LANGUAGE = "language";
|
|
85
|
+
|
|
86
|
+
/** Every value the picker offers besides {@link AUTO}. / 选择器在 {@link AUTO} 之外提供的全部取值。 */
|
|
87
|
+
export const SELECTABLE_TAGS: readonly string[] = CATALOGUE.map((language) => language.tag);
|
|
88
|
+
|
|
89
|
+
/** The resolved configuration this plugin acts on. / 本插件实际作用的已解析配置。 */
|
|
90
|
+
export interface ThinkingSettings {
|
|
91
|
+
/** Selected language tag, or {@link AUTO}. / 所选语言标签,或 {@link AUTO}。 */
|
|
92
|
+
language: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Configuration applied when the field is absent. / 字段缺失时采用的配置。 */
|
|
96
|
+
export const DEFAULTS: ThinkingSettings = { language: AUTO };
|
|
97
|
+
|
|
98
|
+
/** A minimal read handle over the host settings service. / 宿主设置服务的最小读取句柄。 */
|
|
99
|
+
export interface Reader {
|
|
100
|
+
get(ns?: string): unknown;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Catalogue lookup keyed by lower-cased tag. / 以小写标签为键的目录索引。 */
|
|
104
|
+
const BY_TAG = new Map<string, Language>(CATALOGUE.map((language) => [language.tag.toLowerCase(), language]));
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The two Chinese tags, which the script — not the region — decides between.
|
|
108
|
+
* 两个中文标签;区分它们的是文字,而不是地区。
|
|
109
|
+
*/
|
|
110
|
+
const CHINESE = { simplified: "zh-CN", traditional: "zh-TW" } as const;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Look up a catalogue entry by language tag.
|
|
114
|
+
* 按语言标签查找目录条目。
|
|
115
|
+
*/
|
|
116
|
+
function languageOf(tag: string): Language | undefined {
|
|
117
|
+
if (tag === "" || tag === AUTO) return undefined;
|
|
118
|
+
return BY_TAG.get(tag.toLowerCase());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Return the native name of a language tag, for display in the picker.
|
|
123
|
+
* 返回语言标签的本族语名,用于选择器展示。
|
|
124
|
+
*/
|
|
125
|
+
export function nativeNameFor(tag: string): string | undefined {
|
|
126
|
+
return languageOf(tag)?.nativeName;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Parse an untrusted string into a modern `Intl.Locale`, or nothing.
|
|
131
|
+
* 把不可信的字符串解析成现代 `Intl.Locale`;解析不出则为空。
|
|
132
|
+
*
|
|
133
|
+
* POSIX spellings such as `zh_CN.UTF-8` or `sr_RS@latin` are not BCP-47, so the
|
|
134
|
+
* encoding and modifier halves are dropped and the separator is normalized
|
|
135
|
+
* before the tag reaches `Intl`. A tag `Intl` rejects is not an error here: the
|
|
136
|
+
* caller simply moves on to the next locale source.
|
|
137
|
+
* POSIX 写法(如 `zh_CN.UTF-8`、`sr_RS@latin`)不是 BCP-47,因此先去掉了编码与修饰
|
|
138
|
+
* 符部分、归一化分隔符,再把标签交给 `Intl`。被 `Intl` 拒绝的标签在这里不算错误,
|
|
139
|
+
* 调用方继续尝试下一个语言来源即可。
|
|
140
|
+
*/
|
|
141
|
+
function parseLocale(raw: unknown): Intl.Locale | undefined {
|
|
142
|
+
if (typeof raw !== "string") return undefined;
|
|
143
|
+
const tag = raw.trim().split(/[.@]/)[0]?.replace(/_/g, "-") ?? "";
|
|
144
|
+
if (tag === "") return undefined;
|
|
145
|
+
try {
|
|
146
|
+
return new Intl.Locale(tag);
|
|
147
|
+
} catch {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* List the tags worth looking up for one parsed locale, most specific first.
|
|
154
|
+
* 为一个已解析的语言环境列出值得查找的标签,从最具体开始。
|
|
155
|
+
*
|
|
156
|
+
* `zh-Hant-TW` yields `zh-Hant-TW`, `zh-Hant`, `zh-TW`, `zh`; `en-US` yields
|
|
157
|
+
* `en-US`, `en`.
|
|
158
|
+
* `zh-Hant-TW` 会得到 `zh-Hant-TW`、`zh-Hant`、`zh-TW`、`zh`;`en-US` 会得到
|
|
159
|
+
* `en-US`、`en`。
|
|
160
|
+
*/
|
|
161
|
+
function candidatesOf(locale: Intl.Locale): string[] {
|
|
162
|
+
const { language, script, region } = locale;
|
|
163
|
+
const candidates: string[] = [];
|
|
164
|
+
if (script !== undefined && region !== undefined) candidates.push(`${language}-${script}-${region}`);
|
|
165
|
+
if (script !== undefined) candidates.push(`${language}-${script}`);
|
|
166
|
+
if (region !== undefined) candidates.push(`${language}-${region}`);
|
|
167
|
+
candidates.push(language);
|
|
168
|
+
return candidates;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Decide which Chinese tag a script-ambiguous locale means.
|
|
173
|
+
* 判定一个文字不明确的中文语言环境对应哪个标签。
|
|
174
|
+
*
|
|
175
|
+
* `zh`, `zh-SG` and `zh-Hans` all mean Simplified, while `zh-TW`, `zh-HK` and
|
|
176
|
+
* `zh-Hant` mean Traditional. CLDR already encodes that mapping, so `maximize()`
|
|
177
|
+
* supplies the script a short tag omitted instead of a hand-written region table.
|
|
178
|
+
* `zh`、`zh-SG`、`zh-Hans` 都指简体,而 `zh-TW`、`zh-HK`、`zh-Hant` 都指繁体。
|
|
179
|
+
* CLDR 已经把这个映射编码好了,因此用 `maximize()` 补全短标签省略的文字,而不必
|
|
180
|
+
* 手写地区对照表。
|
|
181
|
+
*/
|
|
182
|
+
function chineseTag(locale: Intl.Locale): string {
|
|
183
|
+
const maximized = typeof locale.maximize === "function" ? locale.maximize() : undefined;
|
|
184
|
+
return maximized?.script === "Hant" ? CHINESE.traditional : CHINESE.simplified;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Map a locale onto a catalogue language tag.
|
|
189
|
+
* 把语言环境映射到目录里的语言标签。
|
|
190
|
+
*/
|
|
191
|
+
function tagForLocale(raw: unknown): string | undefined {
|
|
192
|
+
const locale = parseLocale(raw);
|
|
193
|
+
if (locale === undefined) return undefined;
|
|
194
|
+
for (const candidate of candidatesOf(locale)) {
|
|
195
|
+
const match = BY_TAG.get(candidate.toLowerCase());
|
|
196
|
+
if (match !== undefined) return match.tag;
|
|
197
|
+
}
|
|
198
|
+
return locale.language === "zh" ? chineseTag(locale) : undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Coerce an untrusted settings section into a complete configuration.
|
|
203
|
+
* 把不可信的设置段强制转换为完整配置。
|
|
204
|
+
*/
|
|
205
|
+
export function normalizeSettings(raw: unknown): ThinkingSettings {
|
|
206
|
+
const isSection = raw !== null && typeof raw === "object" && !Array.isArray(raw);
|
|
207
|
+
const stored = isSection ? (raw as Record<string, unknown>)[FIELD_LANGUAGE] : undefined;
|
|
208
|
+
return { language: typeof stored === "string" && stored !== "" ? stored : DEFAULTS.language };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Resolve the effective language for `auto`, most authoritative source first.
|
|
213
|
+
* 解析 `auto` 下的生效语言,来源按权威性排序。
|
|
214
|
+
*
|
|
215
|
+
* Sources are tried in order: the operating-system locale, then the desktop UI
|
|
216
|
+
* locale, then the fallback. The first one that maps onto the catalogue wins, so
|
|
217
|
+
* a machine whose system language is Chinese keeps the agent thinking in Chinese
|
|
218
|
+
* even when the DSH interface itself is English.
|
|
219
|
+
* 来源按顺序尝试:操作系统语言 → 桌面界面语言 → 兜底。第一个能映射到语言目录的胜出
|
|
220
|
+
* ——因此系统语言为中文时,即使 DSH 界面是英文,智能体仍会用中文思考。
|
|
221
|
+
*
|
|
222
|
+
* Returns an empty string when the stored tag is neither `auto` nor known, which
|
|
223
|
+
* callers read as "inject nothing".
|
|
224
|
+
* 当存储的标签既不是 `auto` 也不是已知语言时返回空串,调用方据此判定「不注入」。
|
|
225
|
+
*/
|
|
226
|
+
export function effectiveCode(settings: ThinkingSettings, osLocale?: string, desktopLocale?: string): string {
|
|
227
|
+
if (settings.language !== AUTO) return languageOf(settings.language)?.tag ?? "";
|
|
228
|
+
for (const source of [osLocale, desktopLocale, FALLBACK_TAG]) {
|
|
229
|
+
const resolved = tagForLocale(source);
|
|
230
|
+
if (resolved !== undefined) return resolved;
|
|
231
|
+
}
|
|
232
|
+
return FALLBACK_TAG;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Build the system-prompt rule sentence in the selected language.
|
|
237
|
+
* 用所选语言生成系统提示词规则句。
|
|
238
|
+
*
|
|
239
|
+
* The text varies with the selection — one variant per language.
|
|
240
|
+
* 文本随所选语言变化,每种语言一个版本。
|
|
241
|
+
*/
|
|
242
|
+
export function sectionText(code: string): string {
|
|
243
|
+
return languageOf(code)?.section ?? "";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Build the short per-turn reminder in the selected language.
|
|
248
|
+
* 用所选语言生成每轮短提醒。
|
|
249
|
+
*/
|
|
250
|
+
export function reminderText(code: string): string {
|
|
251
|
+
return languageOf(code)?.reminder ?? "";
|
|
252
|
+
}
|