pi-model-manager 0.2.0 → 0.3.1

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/tui/ui-helpers.ts CHANGED
@@ -9,8 +9,9 @@
9
9
  // - contextWindow 用 K/M 简写
10
10
 
11
11
  import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
12
+ import { t } from "../i18n.ts";
12
13
  import { getApiKeyEnvVarName, getAuthStatusText, getProviderDisplayName } from "../state-document.ts";
13
- import { CLIENT_HEADER_PROFILE_LABELS, getClientHeaderProfileDisplay, resolveClientHeaderProfile } from "../presets/client-headers.ts";
14
+ import { getClientHeaderProfileDisplay, getClientHeaderProfileLabel, resolveClientHeaderProfile } from "../presets/client-headers.ts";
14
15
  import { redactUrlForDisplay } from "../sensitive-redaction.ts";
15
16
  import type {
16
17
  ApiKind,
@@ -20,9 +21,10 @@ import type {
20
21
  StoredProvider,
21
22
  StoredRequestHeaderProfile,
22
23
  } from "../types.ts";
24
+ import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
23
25
 
24
26
  export function maskSecret(value: string | undefined): string {
25
- if (!value) return "<未填写>";
27
+ if (!value) return t("<未填写>");
26
28
  if (getApiKeyEnvVarName(value) || value.startsWith("!")) return value;
27
29
  return "********";
28
30
  }
@@ -39,6 +41,7 @@ interface FixedColumn {
39
41
  text: string;
40
42
  width: number;
41
43
  align?: ColumnAlign;
44
+ color?: ThemeColor;
42
45
  }
43
46
 
44
47
  export function fitColumn(text: string, columns: number, align: ColumnAlign = "left"): string {
@@ -47,8 +50,15 @@ export function fitColumn(text: string, columns: number, align: ColumnAlign = "l
47
50
  return align === "right" ? pad + clipped : clipped + pad;
48
51
  }
49
52
 
50
- function joinFixedColumns(columns: readonly FixedColumn[], gap = " "): string {
51
- return columns.map((column) => fitColumn(column.text, column.width, column.align)).join(gap).trimEnd();
53
+ // [喵喵喵]: 先按纯文本对齐再着色;反过来会让列宽计算受 ANSI 序列干扰。
54
+ function joinFixedColumns(columns: readonly FixedColumn[], theme?: Theme, gap = " "): string {
55
+ return columns
56
+ .map((column) => {
57
+ const cell = fitColumn(column.text, column.width, column.align);
58
+ return theme && column.color ? theme.fg(column.color, cell) : cell;
59
+ })
60
+ .join(gap)
61
+ .trimEnd();
52
62
  }
53
63
 
54
64
  function formatTableHeader(line: string): string {
@@ -87,7 +97,7 @@ function formatProviderHeaderProfile(
87
97
  ): string {
88
98
  if (provider.clientHeaderProfile === "recommended") {
89
99
  const resolved = resolveClientHeaderProfile(provider.clientHeaderProfile, provider.api);
90
- return `Auto→${CLIENT_HEADER_PROFILE_LABELS[resolved]}`;
100
+ return `Auto→${getClientHeaderProfileLabel(resolved)}`;
91
101
  }
92
102
  if (provider.clientHeaderProfile === "disabled") return "Off";
93
103
  if (provider.clientHeaderProfile === "custom") {
@@ -98,7 +108,7 @@ function formatProviderHeaderProfile(
98
108
  const inlineCount = Object.keys(provider.customClientHeaders ?? {}).length;
99
109
  return inlineCount > 0 ? `Inline(${inlineCount})` : "Custom?";
100
110
  }
101
- return CLIENT_HEADER_PROFILE_LABELS[provider.clientHeaderProfile];
111
+ return getClientHeaderProfileLabel(provider.clientHeaderProfile);
102
112
  }
103
113
 
104
114
  interface ProviderConsoleCells {
@@ -111,89 +121,99 @@ interface ProviderConsoleCells {
111
121
  status: string;
112
122
  }
113
123
 
114
- function getProviderConsoleColumns(cells: ProviderConsoleCells, availableWidth: number): FixedColumn[] {
115
- if (availableWidth >= 81) {
116
- return [
117
- { text: cells.provider, width: 22 },
118
- { text: cells.api, width: 9 },
119
- { text: cells.models, width: 6, align: "right" },
120
- { text: cells.headers, width: 15 },
121
- { text: cells.proxy, width: 6 },
122
- { text: cells.auth, width: 6 },
123
- { text: cells.status, width: 5 },
124
- ];
125
- }
126
- if (availableWidth >= 64) {
127
- return [
128
- { text: cells.provider, width: 20 },
129
- { text: cells.api, width: 9 },
130
- { text: cells.models, width: 6, align: "right" },
131
- { text: cells.proxy, width: 6 },
132
- { text: cells.auth, width: 6 },
133
- { text: cells.status, width: 5 },
134
- ];
135
- }
136
- if (availableWidth >= 48) {
137
- return [
138
- { text: cells.provider, width: 20 },
139
- { text: cells.api, width: 9 },
140
- { text: cells.models, width: 6, align: "right" },
141
- { text: cells.status, width: 5 },
142
- ];
143
- }
144
- return [
145
- { text: cells.provider, width: Math.max(10, availableWidth - 26) },
146
- { text: cells.api, width: 9 },
147
- { text: cells.models, width: 6, align: "right" },
148
- { text: cells.status, width: 5 },
149
- ];
124
+ export interface ProviderTableOptions {
125
+ requestHeaderProfiles?: Record<string, StoredRequestHeaderProfile>;
126
+ theme?: Theme;
127
+ // 按当前数据收敛后的接入列宽;表头与数据行必须传同一个值才能对齐。
128
+ nameWidth?: number;
129
+ }
130
+
131
+ const PROVIDER_NAME_COLUMN_LIMIT = 22;
132
+ const PROVIDER_NAME_COLUMN_MIN = 12;
133
+
134
+ export function getProviderDisplayLabel(providerId: string, provider: StoredProvider): string {
135
+ const name = getProviderDisplayName(providerId, provider);
136
+ return name === providerId ? providerId : `${name} (${providerId})`;
137
+ }
138
+
139
+ export function getProviderNameColumnWidth(displayLabels: readonly string[]): number {
140
+ const longest = displayLabels.reduce((max, label) => Math.max(max, visibleWidth(label)), 0);
141
+ return Math.min(PROVIDER_NAME_COLUMN_LIMIT, Math.max(PROVIDER_NAME_COLUMN_MIN, longest));
150
142
  }
151
143
 
152
- export function formatProviderConsoleHeader(menuWidth: number): string {
144
+ function getAuthColor(auth: string): ThemeColor | undefined {
145
+ return auth === "miss" || auth === "auth?" ? "warning" : undefined;
146
+ }
147
+
148
+ function getProviderConsoleColumns(cells: ProviderConsoleCells, availableWidth: number, nameWidth: number): FixedColumn[] {
149
+ const provider: FixedColumn = { text: cells.provider, width: Math.min(nameWidth, PROVIDER_NAME_COLUMN_LIMIT) };
150
+ const api: FixedColumn = { text: cells.api, width: 9 };
151
+ const models: FixedColumn = { text: cells.models, width: 6, align: "right" };
152
+ const headers: FixedColumn = { text: cells.headers, width: 15, color: cells.headers === "Off" ? "dim" : undefined };
153
+ const proxy: FixedColumn = { text: cells.proxy, width: 6, color: cells.proxy === "proxy" ? "accent" : "dim" };
154
+ const auth: FixedColumn = { text: cells.auth, width: 6, color: getAuthColor(cells.auth) };
155
+ const status: FixedColumn = { text: cells.status, width: 5, color: cells.status === "ready" ? "success" : "warning" };
156
+ if (availableWidth >= 81) return [provider, api, models, headers, proxy, auth, status];
157
+ if (availableWidth >= 64) return [provider, api, models, proxy, auth, status];
158
+ if (availableWidth >= 48) return [provider, api, models, status];
159
+ return [{ ...provider, width: Math.max(10, availableWidth - 26) }, api, models, status];
160
+ }
161
+
162
+ export function formatProviderConsoleHeader(menuWidth: number, options: ProviderTableOptions = {}): string {
153
163
  return formatTableHeader(joinFixedColumns(getProviderConsoleColumns({
154
- provider: "接入",
164
+ provider: t("接入"),
155
165
  api: "API",
156
- models: "模型",
157
- headers: "请求头",
158
- proxy: "代理",
159
- auth: "认证",
160
- status: "状态",
161
- }, Math.max(0, menuWidth - 2))));
166
+ models: t("模型"),
167
+ headers: t("请求头"),
168
+ proxy: t("代理"),
169
+ auth: t("认证"),
170
+ status: t("状态"),
171
+ }, Math.max(0, menuWidth - 2), options.nameWidth ?? PROVIDER_NAME_COLUMN_LIMIT)));
162
172
  }
163
173
 
164
174
  export function formatProviderConsoleRow(
165
175
  providerId: string,
166
176
  provider: StoredProvider,
167
- requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
168
177
  availableWidth = 81,
178
+ options: ProviderTableOptions = {},
169
179
  ): string {
170
- const name = getProviderDisplayName(providerId, provider);
171
- const displayName = name === providerId ? providerId : `${name} (${providerId})`;
172
180
  return joinFixedColumns(getProviderConsoleColumns({
173
- provider: displayName,
181
+ provider: getProviderDisplayLabel(providerId, provider),
174
182
  api: formatApiShort(provider.api),
175
183
  models: String(provider.models.length),
176
- headers: formatProviderHeaderProfile(provider, requestHeaderProfiles),
184
+ headers: formatProviderHeaderProfile(provider, options.requestHeaderProfiles ?? {}),
177
185
  proxy: getProviderProxyText(provider),
178
186
  auth: getAuthKind(provider.apiKey),
179
187
  status: getProviderStatus(provider),
180
- }, availableWidth));
188
+ }, availableWidth, options.nameWidth ?? PROVIDER_NAME_COLUMN_LIMIT), options.theme);
189
+ }
190
+
191
+ // [喵喵喵]: 详情区分三级(标题/字段名/值),否则整块同一灰度无法扫读;
192
+ // 缩进与标签列宽必须在所有面板一致,否则切面板时详情区会跳动。
193
+ const DETAIL_LABEL_WIDTH = 10;
194
+
195
+ export function formatDetailTitle(title: string, theme?: Theme): string {
196
+ return theme ? theme.fg("text", title) : title;
197
+ }
198
+
199
+ export function formatDetailField(label: string, value: string, theme?: Theme): string {
200
+ const labelCell = ` ${fitColumn(label, DETAIL_LABEL_WIDTH)}`;
201
+ return theme ? `${theme.fg("dim", labelCell)}${theme.fg("text", value)}` : `${labelCell}${value}`;
181
202
  }
182
203
 
183
204
  export function formatProviderDetailLines(
184
205
  providerId: string,
185
206
  provider: StoredProvider,
186
- requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
207
+ options: ProviderTableOptions = {},
187
208
  ): string[] {
188
- const name = getProviderDisplayName(providerId, provider);
189
- const title = name === providerId ? providerId : `${name} (${providerId})`;
190
- const modelIds = provider.models.map((model) => model.id).join(", ") || "<无模型>";
209
+ const { theme, requestHeaderProfiles = {} } = options;
210
+ const modelIds = provider.models.map((model) => model.id).join(", ") || t("<无模型>");
191
211
  return [
192
- title,
193
- ` endpoint ${redactUrlForDisplay(provider.baseUrl)}`,
194
- ` proxy ${provider.httpProxyEnabled ? redactUrlForDisplay(provider.httpProxyUrl ?? "http://127.0.0.1:7890") : "direct"}`,
195
- ` api ${formatApiShort(provider.api)} · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · auth ${getAuthKind(provider.apiKey)}`,
196
- ` models ${modelIds}`,
212
+ formatDetailTitle(getProviderDisplayLabel(providerId, provider), theme),
213
+ formatDetailField("endpoint", redactUrlForDisplay(provider.baseUrl), theme),
214
+ formatDetailField("proxy", provider.httpProxyEnabled ? redactUrlForDisplay(provider.httpProxyUrl ?? "http://127.0.0.1:7890") : "direct", theme),
215
+ formatDetailField("api", `${formatApiShort(provider.api)} · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · auth ${getAuthKind(provider.apiKey)}`, theme),
216
+ formatDetailField("models", modelIds, theme),
197
217
  ];
198
218
  }
199
219
 
@@ -201,7 +221,7 @@ export function formatProviderSummaryLine(
201
221
  provider: StoredProvider,
202
222
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
203
223
  ): string {
204
- return `${formatApiShort(provider.api)} · ${provider.models.length} 模型 · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · proxy ${getProviderProxyText(provider)} · auth ${getAuthKind(provider.apiKey)}`;
224
+ return `${formatApiShort(provider.api)} · ${provider.models.length} ${t("模型")} · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · proxy ${getProviderProxyText(provider)} · auth ${getAuthKind(provider.apiKey)}`;
205
225
  }
206
226
 
207
227
  export function formatProviderEndpointLine(provider: StoredProvider): string {
@@ -210,15 +230,15 @@ export function formatProviderEndpointLine(provider: StoredProvider): string {
210
230
  }
211
231
 
212
232
  function formatModelNameCell(model: StoredModel): string {
213
- return model.name && model.name !== model.id ? model.name : "默认";
233
+ return model.name && model.name !== model.id ? model.name : t("默认");
214
234
  }
215
235
 
216
236
  function formatModelInputCell(model: StoredModel): string {
217
- return model.input.includes("image") ? "文本,视觉" : "文本";
237
+ return model.input.includes("image") ? t("文本,视觉") : t("文本");
218
238
  }
219
239
 
220
240
  function formatModelThinkingCell(model: StoredModel): string {
221
- return model.reasoning ? "开" : "关";
241
+ return model.reasoning ? t("开") : t("关");
222
242
  }
223
243
 
224
244
  interface ModelListCells {
@@ -231,42 +251,73 @@ interface ModelListCells {
231
251
  fast: string;
232
252
  }
233
253
 
234
- function getModelListColumns(provider: StoredProvider, cells: ModelListCells, availableWidth: number): FixedColumn[] {
254
+ export interface ModelTableOptions {
255
+ theme?: Theme;
256
+ // 按当前数据收敛后的列宽;表头与数据行必须传同一组值才能对齐。
257
+ modelIdWidth?: number;
258
+ nameWidth?: number;
259
+ }
260
+
261
+ const MODEL_ID_COLUMN_LIMIT = 30;
262
+ const MODEL_ID_COLUMN_MIN = 14;
263
+ const MODEL_NAME_COLUMN_LIMIT = 16;
264
+ const MODEL_NAME_COLUMN_MIN = 8;
265
+
266
+ export function getModelColumnWidths(models: readonly StoredModel[]): Required<Pick<ModelTableOptions, "modelIdWidth" | "nameWidth">> {
267
+ let longestId = 0;
268
+ let longestName = 0;
269
+ for (const model of models) {
270
+ longestId = Math.max(longestId, visibleWidth(model.id));
271
+ longestName = Math.max(longestName, visibleWidth(formatModelNameCell(model)));
272
+ }
273
+ return {
274
+ modelIdWidth: Math.min(MODEL_ID_COLUMN_LIMIT, Math.max(MODEL_ID_COLUMN_MIN, longestId)),
275
+ nameWidth: Math.min(MODEL_NAME_COLUMN_LIMIT, Math.max(MODEL_NAME_COLUMN_MIN, longestName)),
276
+ };
277
+ }
278
+
279
+ function getModelListColumns(
280
+ provider: StoredProvider,
281
+ cells: ModelListCells,
282
+ availableWidth: number,
283
+ options: ModelTableOptions,
284
+ ): FixedColumn[] {
285
+ const modelIdWidth = options.modelIdWidth ?? MODEL_ID_COLUMN_LIMIT;
286
+ const nameWidth = options.nameWidth ?? MODEL_NAME_COLUMN_LIMIT;
287
+ const name: FixedColumn = { text: cells.name, width: nameWidth, color: cells.name === t("默认") ? "dim" : undefined };
288
+ const input: FixedColumn = { text: cells.input, width: 10, color: cells.input === t("文本,视觉") ? "accent" : undefined };
289
+ const thinking: FixedColumn = { text: cells.thinking, width: 8, color: cells.thinking === t("开") ? "accent" : cells.thinking === t("关") ? "dim" : undefined };
290
+ const context: FixedColumn = { text: cells.context, width: 7, align: "right" };
291
+ const output: FixedColumn = { text: cells.output, width: 7, align: "right" };
292
+ // [喵喵喵]: priority 会消耗 Fast 额度,用 warning 提醒而不是当普通开关。
293
+ const fast: FixedColumn = { text: cells.fast, width: 8, color: cells.fast === "priority" ? "warning" : cells.fast === "off" ? "dim" : undefined };
294
+
235
295
  const fullWidth = provider.api === "openai-responses" ? 98 : 88;
236
296
  if (availableWidth >= fullWidth) {
237
297
  const columns: FixedColumn[] = [
238
- { text: cells.modelId, width: 30 },
239
- { text: cells.name, width: 16 },
240
- { text: cells.input, width: 10 },
241
- { text: cells.thinking, width: 8 },
242
- { text: cells.context, width: 7, align: "right" },
243
- { text: cells.output, width: 7, align: "right" },
298
+ { text: cells.modelId, width: Math.min(modelIdWidth, 30) },
299
+ { ...name, width: Math.min(nameWidth, 16) },
300
+ input,
301
+ thinking,
302
+ context,
303
+ output,
244
304
  ];
245
- if (provider.api === "openai-responses") columns.push({ text: cells.fast, width: 8 });
305
+ if (provider.api === "openai-responses") columns.push(fast);
246
306
  return columns;
247
307
  }
248
308
  if (availableWidth >= 75) {
249
309
  return [
250
- { text: cells.modelId, width: 28 },
251
- { text: cells.name, width: 14 },
252
- { text: cells.input, width: 10 },
253
- { text: cells.thinking, width: 8 },
254
- { text: cells.context, width: 7, align: "right" },
310
+ { text: cells.modelId, width: Math.min(modelIdWidth, 28) },
311
+ { ...name, width: Math.min(nameWidth, 14) },
312
+ input,
313
+ thinking,
314
+ context,
255
315
  ];
256
316
  }
257
317
  if (availableWidth >= 57) {
258
- return [
259
- { text: cells.modelId, width: 26 },
260
- { text: cells.input, width: 10 },
261
- { text: cells.thinking, width: 8 },
262
- { text: cells.context, width: 7, align: "right" },
263
- ];
318
+ return [{ text: cells.modelId, width: Math.min(modelIdWidth, 26) }, input, thinking, context];
264
319
  }
265
- return [
266
- { text: cells.modelId, width: Math.max(10, availableWidth - 22) },
267
- { text: cells.input, width: 10 },
268
- { text: cells.thinking, width: 8 },
269
- ];
320
+ return [{ text: cells.modelId, width: Math.max(10, availableWidth - 22) }, input, thinking];
270
321
  }
271
322
 
272
323
  function getModelListCells(model?: StoredModel): ModelListCells {
@@ -281,37 +332,46 @@ function getModelListCells(model?: StoredModel): ModelListCells {
281
332
  fast: model.openAIServiceTier === "priority" ? "priority" : "off",
282
333
  }
283
334
  : {
284
- modelId: "模型 ID",
285
- name: "显示名",
286
- input: "输入",
335
+ modelId: t("模型 ID"),
336
+ name: t("显示名"),
337
+ input: t("输入"),
287
338
  thinking: "Thinking",
288
- context: "上下文",
289
- output: "输出",
339
+ context: t("上下文"),
340
+ output: t("输出"),
290
341
  fast: "Fast",
291
342
  };
292
343
  }
293
344
 
294
- export function formatModelListHeader(provider: StoredProvider, menuWidth = 100): string {
295
- return formatTableHeader(joinFixedColumns(getModelListColumns(provider, getModelListCells(), Math.max(0, menuWidth - 2))));
345
+ export function formatModelListHeader(provider: StoredProvider, menuWidth = 100, options: ModelTableOptions = {}): string {
346
+ return formatTableHeader(joinFixedColumns(getModelListColumns(provider, getModelListCells(), Math.max(0, menuWidth - 2), options)));
296
347
  }
297
348
 
298
- export function formatModelListRow(provider: StoredProvider, model: StoredModel, availableWidth = 98): string {
299
- return joinFixedColumns(getModelListColumns(provider, getModelListCells(model), availableWidth));
349
+ export function formatModelListRow(
350
+ provider: StoredProvider,
351
+ model: StoredModel,
352
+ availableWidth = 98,
353
+ options: ModelTableOptions = {},
354
+ ): string {
355
+ return joinFixedColumns(getModelListColumns(provider, getModelListCells(model), availableWidth, options), options.theme);
300
356
  }
301
357
 
302
- export const API_CHOICES: { id: ApiKind; label: string }[] = [
303
- { id: "openai-responses", label: "OpenAI Responses · 标准 instructions/input wire" },
304
- { id: "openai-completions", label: "OpenAI Chat · 传统 chat/completions 兼容" },
305
- { id: "anthropic-messages", label: "Anthropic Messages · Claude / Claude Code 兼容" },
306
- { id: "google-generative-ai", label: "Google Gemini · Gemini 原生 API" },
307
- ];
358
+ export function getApiChoices(): { id: ApiKind; label: string }[] {
359
+ return [
360
+ { id: "openai-responses", label: t("OpenAI Responses · 标准 instructions/input wire") },
361
+ { id: "openai-completions", label: t("OpenAI Chat · 传统 chat/completions 兼容") },
362
+ { id: "anthropic-messages", label: t("Anthropic Messages · Claude / Claude Code 兼容") },
363
+ { id: "google-generative-ai", label: t("Google Gemini · Gemini 原生 API") },
364
+ ];
365
+ }
308
366
 
309
- export const BUILT_IN_PROFILE_CHOICES: { id: Exclude<ClientHeaderProfileId, "custom">; label: string }[] = [
310
- { id: "recommended", label: "自动推荐" },
311
- { id: "disabled", label: "不添加" },
312
- { id: "claude-code", label: "ClaudeCode" },
313
- { id: "codex-cli", label: "Codex" },
314
- ];
367
+ export function getBuiltInProfileChoices(): { id: Exclude<ClientHeaderProfileId, "custom">; label: string }[] {
368
+ return [
369
+ { id: "recommended", label: t("自动推荐") },
370
+ { id: "disabled", label: t("不添加") },
371
+ { id: "claude-code", label: "ClaudeCode" },
372
+ { id: "codex-cli", label: "Codex" },
373
+ ];
374
+ }
315
375
 
316
376
  export function describeProfile(
317
377
  profile: ClientHeaderProfileId,
@@ -320,20 +380,23 @@ export function describeProfile(
320
380
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
321
381
  ): string {
322
382
  if (profile !== "custom") return getClientHeaderProfileDisplay(profile, api);
323
- if (!requestHeaderProfileId) return "自定义请求头(未选择)";
383
+ if (!requestHeaderProfileId) return t("自定义请求头(未选择)");
324
384
  const selected = requestHeaderProfiles[requestHeaderProfileId];
325
- return selected ? `${selected.name} (${requestHeaderProfileId})` : `自定义请求头缺失:${requestHeaderProfileId}`;
385
+ return selected ? `${selected.name} (${requestHeaderProfileId})` : t("自定义请求头缺失:{id}", { id: requestHeaderProfileId });
326
386
  }
327
387
 
328
- export const VISION_INPUT_CHOICES: { enabled: boolean; kinds: ModelInputKind[]; label: string }[] = [
329
- { enabled: false, kinds: ["text"], label: "关闭 — 仅文本输入" },
330
- { enabled: true, kinds: ["text", "image"], label: "开启 — 支持视觉(文本 + 图片)" },
331
- ];
388
+ export function getVisionInputChoices(): { enabled: boolean; kinds: ModelInputKind[]; label: string }[] {
389
+ return [
390
+ { enabled: false, kinds: ["text"], label: t("关闭 — 仅文本输入") },
391
+ { enabled: true, kinds: ["text", "image"], label: t("开启 — 文本 + 图片输入") },
392
+ ];
393
+ }
332
394
 
333
395
  export function supportsVisionInput(kinds: ModelInputKind[]): boolean {
334
396
  return kinds.includes("image");
335
397
  }
336
398
 
399
+ // 字段名已经说明了含义,值只需要跟 Thinking / Fast mode 一样给出开关状态。
337
400
  export function describeVisionInput(kinds: ModelInputKind[]): string {
338
- return supportsVisionInput(kinds) ? "开启 · 支持视觉" : "关闭 · 仅文本";
401
+ return supportsVisionInput(kinds) ? t("开启") : t("关闭");
339
402
  }
@@ -0,0 +1,47 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { atomicWriteText } from "./atomic-write.ts";
4
+ import { DEFAULT_UI_LANGUAGE, isUiLanguage, t, type UiLanguage } from "./i18n.ts";
5
+ import { STATE_DIR } from "./state-metadata-store.ts";
6
+
7
+ export const UI_LANGUAGE_SETTINGS_PATH = join(STATE_DIR, "ui-settings.json");
8
+
9
+ interface UiLanguageSettingsDocument {
10
+ version: 1;
11
+ language: UiLanguage;
12
+ }
13
+
14
+ function getErrorCode(error: unknown): string | undefined {
15
+ return error instanceof Error && "code" in error
16
+ ? String((error as NodeJS.ErrnoException).code)
17
+ : undefined;
18
+ }
19
+
20
+ export function parseUiLanguageSettings(source: string): UiLanguage {
21
+ const parsed = JSON.parse(source) as unknown;
22
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
23
+ throw new Error(t("语言设置根节点必须是对象"));
24
+ }
25
+ const settings = parsed as { version?: unknown; language?: unknown };
26
+ if (settings.version !== 1) {
27
+ throw new Error(t("不支持的语言设置版本:{version}", { version: String(settings.version) }));
28
+ }
29
+ if (!isUiLanguage(settings.language)) {
30
+ throw new Error(t("不支持的界面语言:{language}", { language: String(settings.language) }));
31
+ }
32
+ return settings.language;
33
+ }
34
+
35
+ export async function readUiLanguage(path = UI_LANGUAGE_SETTINGS_PATH): Promise<UiLanguage> {
36
+ try {
37
+ return parseUiLanguageSettings(await readFile(path, "utf8"));
38
+ } catch (error) {
39
+ if (getErrorCode(error) === "ENOENT") return DEFAULT_UI_LANGUAGE;
40
+ throw error;
41
+ }
42
+ }
43
+
44
+ export async function writeUiLanguage(language: UiLanguage, path = UI_LANGUAGE_SETTINGS_PATH): Promise<void> {
45
+ const document: UiLanguageSettingsDocument = { version: 1, language };
46
+ await atomicWriteText(path, `${JSON.stringify(document, null, 2)}\n`);
47
+ }