pi-model-manager 0.3.0 → 0.3.2

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,
@@ -23,7 +24,7 @@ import type {
23
24
  import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
24
25
 
25
26
  export function maskSecret(value: string | undefined): string {
26
- if (!value) return "<未填写>";
27
+ if (!value) return t("<未填写>");
27
28
  if (getApiKeyEnvVarName(value) || value.startsWith("!")) return value;
28
29
  return "********";
29
30
  }
@@ -96,7 +97,7 @@ function formatProviderHeaderProfile(
96
97
  ): string {
97
98
  if (provider.clientHeaderProfile === "recommended") {
98
99
  const resolved = resolveClientHeaderProfile(provider.clientHeaderProfile, provider.api);
99
- return `Auto→${CLIENT_HEADER_PROFILE_LABELS[resolved]}`;
100
+ return `Auto→${getClientHeaderProfileLabel(resolved)}`;
100
101
  }
101
102
  if (provider.clientHeaderProfile === "disabled") return "Off";
102
103
  if (provider.clientHeaderProfile === "custom") {
@@ -107,7 +108,7 @@ function formatProviderHeaderProfile(
107
108
  const inlineCount = Object.keys(provider.customClientHeaders ?? {}).length;
108
109
  return inlineCount > 0 ? `Inline(${inlineCount})` : "Custom?";
109
110
  }
110
- return CLIENT_HEADER_PROFILE_LABELS[provider.clientHeaderProfile];
111
+ return getClientHeaderProfileLabel(provider.clientHeaderProfile);
111
112
  }
112
113
 
113
114
  interface ProviderConsoleCells {
@@ -160,13 +161,13 @@ function getProviderConsoleColumns(cells: ProviderConsoleCells, availableWidth:
160
161
 
161
162
  export function formatProviderConsoleHeader(menuWidth: number, options: ProviderTableOptions = {}): string {
162
163
  return formatTableHeader(joinFixedColumns(getProviderConsoleColumns({
163
- provider: "接入",
164
+ provider: t("接入"),
164
165
  api: "API",
165
- models: "模型",
166
- headers: "请求头",
167
- proxy: "代理",
168
- auth: "认证",
169
- status: "状态",
166
+ models: t("模型"),
167
+ headers: t("请求头"),
168
+ proxy: t("代理"),
169
+ auth: t("认证"),
170
+ status: t("状态"),
170
171
  }, Math.max(0, menuWidth - 2), options.nameWidth ?? PROVIDER_NAME_COLUMN_LIMIT)));
171
172
  }
172
173
 
@@ -206,7 +207,7 @@ export function formatProviderDetailLines(
206
207
  options: ProviderTableOptions = {},
207
208
  ): string[] {
208
209
  const { theme, requestHeaderProfiles = {} } = options;
209
- const modelIds = provider.models.map((model) => model.id).join(", ") || "<无模型>";
210
+ const modelIds = provider.models.map((model) => model.id).join(", ") || t("<无模型>");
210
211
  return [
211
212
  formatDetailTitle(getProviderDisplayLabel(providerId, provider), theme),
212
213
  formatDetailField("endpoint", redactUrlForDisplay(provider.baseUrl), theme),
@@ -220,7 +221,7 @@ export function formatProviderSummaryLine(
220
221
  provider: StoredProvider,
221
222
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
222
223
  ): string {
223
- 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)}`;
224
225
  }
225
226
 
226
227
  export function formatProviderEndpointLine(provider: StoredProvider): string {
@@ -229,15 +230,15 @@ export function formatProviderEndpointLine(provider: StoredProvider): string {
229
230
  }
230
231
 
231
232
  function formatModelNameCell(model: StoredModel): string {
232
- return model.name && model.name !== model.id ? model.name : "默认";
233
+ return model.name && model.name !== model.id ? model.name : t("默认");
233
234
  }
234
235
 
235
236
  function formatModelInputCell(model: StoredModel): string {
236
- return model.input.includes("image") ? "文本,视觉" : "文本";
237
+ return model.input.includes("image") ? t("文本,视觉") : t("文本");
237
238
  }
238
239
 
239
240
  function formatModelThinkingCell(model: StoredModel): string {
240
- return model.reasoning ? "开" : "关";
241
+ return model.reasoning ? t("开") : t("关");
241
242
  }
242
243
 
243
244
  interface ModelListCells {
@@ -283,9 +284,9 @@ function getModelListColumns(
283
284
  ): FixedColumn[] {
284
285
  const modelIdWidth = options.modelIdWidth ?? MODEL_ID_COLUMN_LIMIT;
285
286
  const nameWidth = options.nameWidth ?? MODEL_NAME_COLUMN_LIMIT;
286
- const name: FixedColumn = { text: cells.name, width: nameWidth, color: cells.name === "默认" ? "dim" : undefined };
287
- const input: FixedColumn = { text: cells.input, width: 10, color: cells.input.includes("视觉") ? "accent" : undefined };
288
- const thinking: FixedColumn = { text: cells.thinking, width: 8, color: cells.thinking === "开" ? "accent" : cells.thinking === "关" ? "dim" : undefined };
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 };
289
290
  const context: FixedColumn = { text: cells.context, width: 7, align: "right" };
290
291
  const output: FixedColumn = { text: cells.output, width: 7, align: "right" };
291
292
  // [喵喵喵]: priority 会消耗 Fast 额度,用 warning 提醒而不是当普通开关。
@@ -331,12 +332,12 @@ function getModelListCells(model?: StoredModel): ModelListCells {
331
332
  fast: model.openAIServiceTier === "priority" ? "priority" : "off",
332
333
  }
333
334
  : {
334
- modelId: "模型 ID",
335
- name: "显示名",
336
- input: "输入",
335
+ modelId: t("模型 ID"),
336
+ name: t("显示名"),
337
+ input: t("输入"),
337
338
  thinking: "Thinking",
338
- context: "上下文",
339
- output: "输出",
339
+ context: t("上下文"),
340
+ output: t("输出"),
340
341
  fast: "Fast",
341
342
  };
342
343
  }
@@ -354,19 +355,23 @@ export function formatModelListRow(
354
355
  return joinFixedColumns(getModelListColumns(provider, getModelListCells(model), availableWidth, options), options.theme);
355
356
  }
356
357
 
357
- export const API_CHOICES: { id: ApiKind; label: string }[] = [
358
- { id: "openai-responses", label: "OpenAI Responses · 标准 instructions/input wire" },
359
- { id: "openai-completions", label: "OpenAI Chat · 传统 chat/completions 兼容" },
360
- { id: "anthropic-messages", label: "Anthropic Messages · Claude / Claude Code 兼容" },
361
- { id: "google-generative-ai", label: "Google Gemini · Gemini 原生 API" },
362
- ];
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
+ }
363
366
 
364
- export const BUILT_IN_PROFILE_CHOICES: { id: Exclude<ClientHeaderProfileId, "custom">; label: string }[] = [
365
- { id: "recommended", label: "自动推荐" },
366
- { id: "disabled", label: "不添加" },
367
- { id: "claude-code", label: "ClaudeCode" },
368
- { id: "codex-cli", label: "Codex" },
369
- ];
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
+ }
370
375
 
371
376
  export function describeProfile(
372
377
  profile: ClientHeaderProfileId,
@@ -375,15 +380,17 @@ export function describeProfile(
375
380
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
376
381
  ): string {
377
382
  if (profile !== "custom") return getClientHeaderProfileDisplay(profile, api);
378
- if (!requestHeaderProfileId) return "自定义请求头(未选择)";
383
+ if (!requestHeaderProfileId) return t("自定义请求头(未选择)");
379
384
  const selected = requestHeaderProfiles[requestHeaderProfileId];
380
- return selected ? `${selected.name} (${requestHeaderProfileId})` : `自定义请求头缺失:${requestHeaderProfileId}`;
385
+ return selected ? `${selected.name} (${requestHeaderProfileId})` : t("自定义请求头缺失:{id}", { id: requestHeaderProfileId });
381
386
  }
382
387
 
383
- export const VISION_INPUT_CHOICES: { enabled: boolean; kinds: ModelInputKind[]; label: string }[] = [
384
- { enabled: false, kinds: ["text"], label: "关闭 — 仅文本输入" },
385
- { enabled: true, kinds: ["text", "image"], label: "开启 — 文本 + 图片输入" },
386
- ];
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
+ }
387
394
 
388
395
  export function supportsVisionInput(kinds: ModelInputKind[]): boolean {
389
396
  return kinds.includes("image");
@@ -391,5 +398,5 @@ export function supportsVisionInput(kinds: ModelInputKind[]): boolean {
391
398
 
392
399
  // 字段名已经说明了含义,值只需要跟 Thinking / Fast mode 一样给出开关状态。
393
400
  export function describeVisionInput(kinds: ModelInputKind[]): string {
394
- return supportsVisionInput(kinds) ? "开启" : "关闭";
401
+ return supportsVisionInput(kinds) ? t("开启") : t("关闭");
395
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
+ }