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/README.en.md +118 -23
- package/README.md +118 -23
- package/atomic-write.ts +4 -2
- package/common.ts +3 -2
- package/configuration-lock.ts +3 -2
- package/configuration-persistence.ts +24 -19
- package/file-snapshot.ts +2 -1
- package/header-profile-mutations.ts +4 -3
- package/i18n.ts +403 -0
- package/index.ts +39 -9
- package/local-proxy-service.ts +10 -9
- package/model-mutations.ts +24 -21
- package/models-json-manager.ts +8 -7
- package/models-json-sync.ts +11 -10
- package/package.json +5 -4
- package/presets/builtin-client-headers.ts +7 -7
- package/presets/client-headers.ts +9 -9
- package/presets/providers.ts +6 -3
- package/provider-registrar.ts +4 -3
- package/request-pipeline.ts +3 -2
- package/rescue.ts +4 -3
- package/runtime-base-url.ts +9 -3
- package/state-document.ts +58 -34
- package/state-metadata-store.ts +38 -37
- package/state-store.ts +2 -1
- package/tui/dashboard.ts +110 -53
- package/tui/editor-model.ts +78 -116
- package/tui/editor-provider.ts +77 -76
- package/tui/header-profiles-panel.ts +100 -76
- package/tui/model-list-fetch.ts +9 -8
- package/tui/model-list-validation.ts +8 -7
- package/tui/model-picker.ts +18 -11
- package/tui/persistent-menu.ts +231 -87
- package/tui/ui-helpers.ts +184 -121
- package/ui-language-settings.ts +47 -0
package/state-document.ts
CHANGED
|
@@ -15,9 +15,11 @@ import {
|
|
|
15
15
|
getSingleConfigValueEnvVarName,
|
|
16
16
|
isCommandConfigValue,
|
|
17
17
|
} from "./config-value-reference.ts";
|
|
18
|
+
import { joinLocalizedList, t } from "./i18n.ts";
|
|
18
19
|
import { findPresetForApi } from "./presets/providers.ts";
|
|
19
20
|
import { normalizeThinkingLevelMap } from "./presets/thinking.ts";
|
|
20
21
|
import { isSensitiveHeaderName } from "./sensitive-redaction.ts";
|
|
22
|
+
import { resolveRuntimeBaseUrl } from "./runtime-base-url.ts";
|
|
21
23
|
import type {
|
|
22
24
|
ApiKind,
|
|
23
25
|
CompatSettings,
|
|
@@ -180,20 +182,20 @@ function validateUrl(url: string, label: string, errors: string[]): void {
|
|
|
180
182
|
try {
|
|
181
183
|
const parsed = new URL(url);
|
|
182
184
|
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
183
|
-
errors.push(
|
|
185
|
+
errors.push(t("{label} 必须使用 http 或 https", { label }));
|
|
184
186
|
}
|
|
185
187
|
} catch {
|
|
186
|
-
errors.push(
|
|
188
|
+
errors.push(t("{label} 不是有效 URL", { label }));
|
|
187
189
|
}
|
|
188
190
|
}
|
|
189
191
|
|
|
190
192
|
function validateHttpProxyUrl(url: string, errors: string[]): void {
|
|
191
193
|
try {
|
|
192
194
|
const parsed = new URL(url);
|
|
193
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") errors.push("代理地址目前只支持 http:// 或 https:// 代理");
|
|
194
|
-
if (!parsed.hostname) errors.push("代理地址必须包含主机名");
|
|
195
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") errors.push(t("代理地址目前只支持 http:// 或 https:// 代理"));
|
|
196
|
+
if (!parsed.hostname) errors.push(t("代理地址必须包含主机名"));
|
|
195
197
|
} catch {
|
|
196
|
-
errors.push("代理地址不是有效 URL");
|
|
198
|
+
errors.push(t("代理地址不是有效 URL"));
|
|
197
199
|
}
|
|
198
200
|
}
|
|
199
201
|
|
|
@@ -206,25 +208,25 @@ export function validateProviderDraft(
|
|
|
206
208
|
const errors: string[] = [];
|
|
207
209
|
const id = draft.providerId.trim();
|
|
208
210
|
|
|
209
|
-
if (!id) errors.push("接入 ID 不能为空");
|
|
210
|
-
else if (!ID_PATTERN.test(id)) errors.push("接入 ID 只能包含字母、数字、点、下划线和连字符");
|
|
211
|
-
else if (id.length > MAX_PROVIDER_ID_LENGTH) errors.push(
|
|
211
|
+
if (!id) errors.push(t("接入 ID 不能为空"));
|
|
212
|
+
else if (!ID_PATTERN.test(id)) errors.push(t("接入 ID 只能包含字母、数字、点、下划线和连字符"));
|
|
213
|
+
else if (id.length > MAX_PROVIDER_ID_LENGTH) errors.push(t("接入 ID 最多 {count} 个字符", { count: MAX_PROVIDER_ID_LENGTH }));
|
|
212
214
|
|
|
213
215
|
if (id && oldProviderId !== id && document.providers[id]) {
|
|
214
|
-
errors.push(
|
|
216
|
+
errors.push(t("接入 ID 已存在:{id}", { id }));
|
|
215
217
|
}
|
|
216
218
|
if (id && builtInProviderIds.has(id)) {
|
|
217
|
-
errors.push(
|
|
219
|
+
errors.push(t("接入 ID 与 pi 内置接入冲突:{id}(请改用其它 ID)", { id }));
|
|
218
220
|
}
|
|
219
221
|
|
|
220
|
-
if (!draft.baseUrl.trim()) errors.push("Base URL 不能为空");
|
|
222
|
+
if (!draft.baseUrl.trim()) errors.push(t("Base URL 不能为空"));
|
|
221
223
|
else validateUrl(draft.baseUrl.trim(), "Base URL", errors);
|
|
222
224
|
|
|
223
225
|
|
|
224
226
|
if (draft.clientHeaderProfile === "custom") {
|
|
225
227
|
const profileId = draft.requestHeaderProfileId?.trim();
|
|
226
|
-
if (!profileId && !hasStringRecordEntries(draft.customClientHeaders)) errors.push("请选择一个自定义请求头");
|
|
227
|
-
else if (profileId && !document.requestHeaderProfiles[profileId]) errors.push(
|
|
228
|
+
if (!profileId && !hasStringRecordEntries(draft.customClientHeaders)) errors.push(t("请选择一个自定义请求头"));
|
|
229
|
+
else if (profileId && !document.requestHeaderProfiles[profileId]) errors.push(t("自定义请求头不存在:{profileId}", { profileId }));
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
if (draft.httpProxyEnabled) {
|
|
@@ -241,24 +243,24 @@ export function validateModelDraft(
|
|
|
241
243
|
replacedModelId?: string,
|
|
242
244
|
): string[] {
|
|
243
245
|
const errors: string[] = [];
|
|
244
|
-
if (!draft.providerId.trim()) errors.push("接入 ID 不能为空");
|
|
245
|
-
if (!draft.modelId.trim()) errors.push("模型 ID 不能为空");
|
|
246
|
+
if (!draft.providerId.trim()) errors.push(t("接入 ID 不能为空"));
|
|
247
|
+
if (!draft.modelId.trim()) errors.push(t("模型 ID 不能为空"));
|
|
246
248
|
|
|
247
249
|
const provider = document.providers[draft.providerId];
|
|
248
|
-
if (!provider) errors.push(
|
|
250
|
+
if (!provider) errors.push(t("接入配置不存在:{providerId}", { providerId: draft.providerId }));
|
|
249
251
|
|
|
250
|
-
if (!draft.baseUrl.trim()) errors.push("Base URL 不能为空");
|
|
252
|
+
if (!draft.baseUrl.trim()) errors.push(t("Base URL 不能为空"));
|
|
251
253
|
else validateUrl(draft.baseUrl.trim(), "Base URL", errors);
|
|
252
254
|
|
|
253
255
|
|
|
254
256
|
if (!Number.isInteger(draft.contextWindow) || draft.contextWindow <= 0)
|
|
255
|
-
errors.push("上下文窗口必须是正整数");
|
|
257
|
+
errors.push(t("上下文窗口必须是正整数"));
|
|
256
258
|
if (!Number.isInteger(draft.maxTokens) || draft.maxTokens <= 0)
|
|
257
|
-
errors.push("最大输出必须是正整数");
|
|
259
|
+
errors.push(t("最大输出必须是正整数"));
|
|
258
260
|
|
|
259
261
|
const duplicate = (provider?.models ?? []).find((m) => m.id === draft.modelId.trim());
|
|
260
262
|
if (duplicate && duplicate.id !== replacedModelId)
|
|
261
|
-
errors.push(
|
|
263
|
+
errors.push(t("模型已存在:{modelId}", { modelId: draft.modelId.trim() }));
|
|
262
264
|
|
|
263
265
|
return errors;
|
|
264
266
|
}
|
|
@@ -273,17 +275,17 @@ export function getProviderChangeWarnings(
|
|
|
273
275
|
const changes: string[] = [];
|
|
274
276
|
if ((current.baseUrl ?? "") !== draft.baseUrl.trim()) changes.push("Base URL");
|
|
275
277
|
if ((current.apiKey ?? "") !== draft.apiKey.trim()) changes.push("API key");
|
|
276
|
-
if ((current.api ?? "") !== draft.api) changes.push("API 协议");
|
|
277
|
-
if ((current.authHeader ?? false) !== draft.authHeader) changes.push("认证头");
|
|
278
|
+
if ((current.api ?? "") !== draft.api) changes.push(t("API 协议"));
|
|
279
|
+
if ((current.authHeader ?? false) !== draft.authHeader) changes.push(t("认证头"));
|
|
278
280
|
if ((current.clientHeaderProfile ?? "recommended") !== draft.clientHeaderProfile
|
|
279
281
|
|| (current.requestHeaderProfileId ?? "") !== (draft.requestHeaderProfileId ?? ""))
|
|
280
|
-
changes.push("请求头");
|
|
282
|
+
changes.push(t("请求头"));
|
|
281
283
|
if ((current.httpProxyEnabled ?? false) !== draft.httpProxyEnabled
|
|
282
284
|
|| (current.httpProxyUrl?.trim() || DEFAULT_PROVIDER_HTTP_PROXY_URL) !== (draft.httpProxyUrl.trim() || DEFAULT_PROVIDER_HTTP_PROXY_URL)) {
|
|
283
|
-
changes.push("本机代理");
|
|
285
|
+
changes.push(t("本机代理"));
|
|
284
286
|
}
|
|
285
287
|
if (changes.length === 0) return [];
|
|
286
|
-
return [
|
|
288
|
+
return [t("将修改接入级配置:{changes},会影响该接入下 {count} 个模型。", { changes: joinLocalizedList(changes), count: affected })];
|
|
287
289
|
}
|
|
288
290
|
|
|
289
291
|
// ========== build:draft → stored ==========
|
|
@@ -409,7 +411,7 @@ export function buildModelFromDraft(
|
|
|
409
411
|
export function createRequestHeaderProfileDraft(): RequestHeaderProfileDraft {
|
|
410
412
|
return {
|
|
411
413
|
profileId: "custom-headers",
|
|
412
|
-
profileName: "自定义请求头",
|
|
414
|
+
profileName: t("自定义请求头"),
|
|
413
415
|
headers: {},
|
|
414
416
|
selectedIndex: 0,
|
|
415
417
|
};
|
|
@@ -445,17 +447,17 @@ export function validateRequestHeaderProfileDraft(
|
|
|
445
447
|
): string[] {
|
|
446
448
|
const errors: string[] = [];
|
|
447
449
|
const profileId = draft.profileId.trim();
|
|
448
|
-
if (!profileId) errors.push("请求头 ID 不能为空");
|
|
449
|
-
else if (!ID_PATTERN.test(profileId)) errors.push("请求头 ID 只能包含字母、数字、点、下划线和连字符");
|
|
450
|
-
else if (RESERVED_REQUEST_HEADER_PROFILE_IDS.has(profileId)) errors.push(
|
|
450
|
+
if (!profileId) errors.push(t("请求头 ID 不能为空"));
|
|
451
|
+
else if (!ID_PATTERN.test(profileId)) errors.push(t("请求头 ID 只能包含字母、数字、点、下划线和连字符"));
|
|
452
|
+
else if (RESERVED_REQUEST_HEADER_PROFILE_IDS.has(profileId)) errors.push(t("请求头 ID 是插件内置保留名:{profileId}", { profileId }));
|
|
451
453
|
if (profileId && oldProfileId !== profileId && document.requestHeaderProfiles[profileId]) {
|
|
452
|
-
errors.push(
|
|
454
|
+
errors.push(t("请求头 ID 已存在:{profileId}", { profileId }));
|
|
453
455
|
}
|
|
454
|
-
if (!draft.profileName.trim()) errors.push("请求头名称不能为空");
|
|
455
|
-
if (!hasStringRecordEntries(draft.headers)) errors.push("请求头至少需要 1 项");
|
|
456
|
+
if (!draft.profileName.trim()) errors.push(t("请求头名称不能为空"));
|
|
457
|
+
if (!hasStringRecordEntries(draft.headers)) errors.push(t("请求头至少需要 1 项"));
|
|
456
458
|
const sensitiveHeaders = Object.keys(draft.headers).filter(isSensitiveHeaderName);
|
|
457
459
|
if (sensitiveHeaders.length > 0) {
|
|
458
|
-
errors.push(
|
|
460
|
+
errors.push(t("请求头包含会明文落盘的敏感字段:{headers}(请改用 provider API key / 环境变量 / !command)", { headers: joinLocalizedList(sensitiveHeaders) }));
|
|
459
461
|
}
|
|
460
462
|
return errors;
|
|
461
463
|
}
|
|
@@ -483,6 +485,28 @@ export function upsertRequestHeaderProfileInDocument(
|
|
|
483
485
|
};
|
|
484
486
|
return next;
|
|
485
487
|
}
|
|
488
|
+
// [喵喵喵]: 早期版本只在注册时补全 baseUrl,models.json 里留下的是半成品(如 openai 缺 /v1)。
|
|
489
|
+
// 插件未加载时 pi 会回落到 models.json 直接请求,因此需要把存量值一次性归一化。
|
|
490
|
+
// 只处理受管理接入;原生接入的 baseUrl 不是本插件写的,不能代为改写。
|
|
491
|
+
export function findProvidersNeedingBaseUrlNormalization(document: StateDocument): string[] {
|
|
492
|
+
return Object.entries(document.providers)
|
|
493
|
+
.filter(([, provider]) => provider.managed && provider.baseUrl)
|
|
494
|
+
.filter(([, provider]) => resolveRuntimeBaseUrl(provider.api, provider.baseUrl) !== provider.baseUrl)
|
|
495
|
+
.map(([providerId]) => providerId);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export function normalizeProviderBaseUrlsInDocument(
|
|
499
|
+
document: StateDocument,
|
|
500
|
+
providerIds: readonly string[],
|
|
501
|
+
): StateDocument {
|
|
502
|
+
const next: StateDocument = cloneJson(document);
|
|
503
|
+
for (const providerId of providerIds) {
|
|
504
|
+
const provider = next.providers[providerId];
|
|
505
|
+
if (!provider?.managed || !provider.baseUrl) continue;
|
|
506
|
+
provider.baseUrl = resolveRuntimeBaseUrl(provider.api, provider.baseUrl);
|
|
507
|
+
}
|
|
508
|
+
return next;
|
|
509
|
+
}
|
|
486
510
|
|
|
487
511
|
export function deleteRequestHeaderProfileFromDocument(document: StateDocument, profileId: string): StateDocument {
|
|
488
512
|
const next: StateDocument = cloneJson(document);
|
|
@@ -537,7 +561,7 @@ export function upsertModelInDocument(
|
|
|
537
561
|
): StateDocument {
|
|
538
562
|
const next: StateDocument = cloneJson(document);
|
|
539
563
|
const provider = next.providers[draft.providerId];
|
|
540
|
-
if (!provider) throw new Error(
|
|
564
|
+
if (!provider) throw new Error(t("upsertModel:接入不存在:{providerId}", { providerId: draft.providerId }));
|
|
541
565
|
const existing = (provider.models ?? []).find(
|
|
542
566
|
(m) => m.id === options.replacedModelId || m.id === draft.modelId.trim(),
|
|
543
567
|
);
|
package/state-metadata-store.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import { cloneJson, isObjectRecord, stringifyJson, stripJsonNoise } from "./common.ts";
|
|
10
10
|
import { readStableTextFileSnapshot, type FileSignature } from "./file-snapshot.ts";
|
|
11
|
+
import { t } from "./i18n.ts";
|
|
11
12
|
import type {
|
|
12
13
|
ApiKind,
|
|
13
14
|
BuiltInClientHeaderProfileId,
|
|
@@ -74,40 +75,40 @@ export function createEmptyMetadata(): MetadataDocument {
|
|
|
74
75
|
return { version: 4, managedProviderIds: [], providers: {}, models: {}, requestHeaderProfiles: {}, clientHeaderCaptures: {} };
|
|
75
76
|
}
|
|
76
77
|
function fail(path: string, message: string): never {
|
|
77
|
-
throw new Error(
|
|
78
|
+
throw new Error(t("state.json {path}: {message}", { path, message }));
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
function readRequiredString(record: Record<string, unknown>, key: string, path: string): string {
|
|
81
82
|
const value = record[key];
|
|
82
|
-
if (typeof value !== "string" || !value.trim()) fail(`${path}.${key}`, "必须是非空字符串");
|
|
83
|
+
if (typeof value !== "string" || !value.trim()) fail(`${path}.${key}`, t("必须是非空字符串"));
|
|
83
84
|
return value;
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
function readOptionalString(record: Record<string, unknown>, key: string, path: string): string | undefined {
|
|
87
88
|
const value = record[key];
|
|
88
89
|
if (value === undefined) return undefined;
|
|
89
|
-
if (typeof value !== "string") fail(`${path}.${key}`, "必须是字符串");
|
|
90
|
+
if (typeof value !== "string") fail(`${path}.${key}`, t("必须是字符串"));
|
|
90
91
|
return value;
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
function readOptionalBoolean(record: Record<string, unknown>, key: string, path: string): boolean | undefined {
|
|
94
95
|
const value = record[key];
|
|
95
96
|
if (value === undefined) return undefined;
|
|
96
|
-
if (typeof value !== "boolean") fail(`${path}.${key}`, "必须是 boolean");
|
|
97
|
+
if (typeof value !== "boolean") fail(`${path}.${key}`, t("必须是 boolean"));
|
|
97
98
|
return value;
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
function readPositiveInteger(record: Record<string, unknown>, key: string, path: string): number {
|
|
101
102
|
const value = record[key];
|
|
102
|
-
if (!Number.isInteger(value) || (value as number) <= 0) fail(`${path}.${key}`, "必须是正整数");
|
|
103
|
+
if (!Number.isInteger(value) || (value as number) <= 0) fail(`${path}.${key}`, t("必须是正整数"));
|
|
103
104
|
return value as number;
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
function readStringRecord(value: unknown, path: string): Record<string, string> {
|
|
107
|
-
if (!isObjectRecord(value)) fail(path, "必须是对象");
|
|
108
|
+
if (!isObjectRecord(value)) fail(path, t("必须是对象"));
|
|
108
109
|
const record: Record<string, string> = {};
|
|
109
110
|
for (const [key, entry] of Object.entries(value)) {
|
|
110
|
-
if (typeof entry !== "string") fail(`${path}.${key}`, "值必须是字符串");
|
|
111
|
+
if (typeof entry !== "string") fail(`${path}.${key}`, t("值必须是字符串"));
|
|
111
112
|
record[key] = entry;
|
|
112
113
|
}
|
|
113
114
|
return record;
|
|
@@ -120,9 +121,9 @@ function readOptionalStringRecord(record: Record<string, unknown>, key: string,
|
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
function readProviderIdList(value: unknown, path: string): string[] {
|
|
123
|
-
if (!Array.isArray(value)) fail(path, "必须是字符串数组");
|
|
124
|
+
if (!Array.isArray(value)) fail(path, t("必须是字符串数组"));
|
|
124
125
|
return [...new Set(value.map((item, index) => {
|
|
125
|
-
if (typeof item !== "string" || !item.trim()) fail(`${path}[${index}]`, "必须是非空字符串");
|
|
126
|
+
if (typeof item !== "string" || !item.trim()) fail(`${path}[${index}]`, t("必须是非空字符串"));
|
|
126
127
|
return item;
|
|
127
128
|
}))];
|
|
128
129
|
}
|
|
@@ -130,35 +131,35 @@ function readProviderIdList(value: unknown, path: string): string[] {
|
|
|
130
131
|
function readObjectRecord(record: Record<string, unknown>, key: string, path: string): Record<string, unknown> | undefined {
|
|
131
132
|
const value = record[key];
|
|
132
133
|
if (value === undefined) return undefined;
|
|
133
|
-
if (!isObjectRecord(value)) fail(`${path}.${key}`, "必须是对象");
|
|
134
|
+
if (!isObjectRecord(value)) fail(`${path}.${key}`, t("必须是对象"));
|
|
134
135
|
return cloneJson(value);
|
|
135
136
|
}
|
|
136
137
|
|
|
137
138
|
function readApiKind(record: Record<string, unknown>, key: string, path: string): ApiKind {
|
|
138
139
|
const value = readRequiredString(record, key, path);
|
|
139
|
-
if (!API_KINDS.has(value as ApiKind)) fail(`${path}.${key}`,
|
|
140
|
+
if (!API_KINDS.has(value as ApiKind)) fail(`${path}.${key}`, t("未知 API 协议:{value}", { value }));
|
|
140
141
|
return value as ApiKind;
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
function readOptionalClientHeaderProfile(record: Record<string, unknown>, key: string, path: string): ClientHeaderProfileId | undefined {
|
|
144
145
|
const value = readOptionalString(record, key, path);
|
|
145
146
|
if (value === undefined) return undefined;
|
|
146
|
-
if (!CLIENT_HEADER_PROFILE_IDS.has(value as ClientHeaderProfileId)) fail(`${path}.${key}`,
|
|
147
|
+
if (!CLIENT_HEADER_PROFILE_IDS.has(value as ClientHeaderProfileId)) fail(`${path}.${key}`, t("未知请求头:{value}", { value }));
|
|
147
148
|
return value as ClientHeaderProfileId;
|
|
148
149
|
}
|
|
149
150
|
|
|
150
151
|
function readOptionalOpenAIServiceTier(record: Record<string, unknown>, key: string, path: string): OpenAIServiceTier | undefined {
|
|
151
152
|
const value = readOptionalString(record, key, path);
|
|
152
153
|
if (value === undefined) return undefined;
|
|
153
|
-
if (!OPENAI_SERVICE_TIERS.has(value as OpenAIServiceTier)) fail(`${path}.${key}`,
|
|
154
|
+
if (!OPENAI_SERVICE_TIERS.has(value as OpenAIServiceTier)) fail(`${path}.${key}`, t("未知 OpenAI service tier:{value}", { value }));
|
|
154
155
|
return value as OpenAIServiceTier;
|
|
155
156
|
}
|
|
156
157
|
|
|
157
158
|
function readInputKinds(record: Record<string, unknown>, key: string, path: string): ModelInputKind[] {
|
|
158
159
|
const value = record[key];
|
|
159
|
-
if (!Array.isArray(value) || value.length === 0) fail(`${path}.${key}`, "必须是非空数组");
|
|
160
|
+
if (!Array.isArray(value) || value.length === 0) fail(`${path}.${key}`, t("必须是非空数组"));
|
|
160
161
|
const kinds = value.map((item, index) => {
|
|
161
|
-
if (typeof item !== "string" || !INPUT_KINDS.has(item as ModelInputKind)) fail(`${path}.${key}[${index}]`, "必须是 text 或 image");
|
|
162
|
+
if (typeof item !== "string" || !INPUT_KINDS.has(item as ModelInputKind)) fail(`${path}.${key}[${index}]`, t("必须是 text 或 image"));
|
|
162
163
|
return item as ModelInputKind;
|
|
163
164
|
});
|
|
164
165
|
return [...new Set(kinds)];
|
|
@@ -166,11 +167,11 @@ function readInputKinds(record: Record<string, unknown>, key: string, path: stri
|
|
|
166
167
|
|
|
167
168
|
function readCost(record: Record<string, unknown>, key: string, path: string): TokenCost {
|
|
168
169
|
const value = record[key];
|
|
169
|
-
if (!isObjectRecord(value)) fail(`${path}.${key}`, "必须是对象");
|
|
170
|
+
if (!isObjectRecord(value)) fail(`${path}.${key}`, t("必须是对象"));
|
|
170
171
|
const cost: Partial<TokenCost> = {};
|
|
171
172
|
for (const field of ["input", "output", "cacheRead", "cacheWrite"] as const) {
|
|
172
173
|
const amount = value[field];
|
|
173
|
-
if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) fail(`${path}.${key}.${field}`, "必须是非负数字");
|
|
174
|
+
if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) fail(`${path}.${key}.${field}`, t("必须是非负数字"));
|
|
174
175
|
cost[field] = amount;
|
|
175
176
|
}
|
|
176
177
|
return cost as TokenCost;
|
|
@@ -179,20 +180,20 @@ function readCost(record: Record<string, unknown>, key: string, path: string): T
|
|
|
179
180
|
function readThinkingLevelMap(record: Record<string, unknown>, key: string, path: string): ThinkingLevelMap | undefined {
|
|
180
181
|
const value = record[key];
|
|
181
182
|
if (value === undefined) return undefined;
|
|
182
|
-
if (!isObjectRecord(value)) fail(`${path}.${key}`, "必须是对象");
|
|
183
|
+
if (!isObjectRecord(value)) fail(`${path}.${key}`, t("必须是对象"));
|
|
183
184
|
const map: ThinkingLevelMap = {};
|
|
184
185
|
for (const [level, mapped] of Object.entries(value)) {
|
|
185
|
-
if (!THINKING_LEVELS.has(level)) fail(`${path}.${key}.${level}`, "未知 thinking level");
|
|
186
|
-
if (typeof mapped !== "string" && mapped !== null) fail(`${path}.${key}.${level}`, "必须是字符串或 null");
|
|
186
|
+
if (!THINKING_LEVELS.has(level)) fail(`${path}.${key}.${level}`, t("未知 thinking level"));
|
|
187
|
+
if (typeof mapped !== "string" && mapped !== null) fail(`${path}.${key}.${level}`, t("必须是字符串或 null"));
|
|
187
188
|
map[level as keyof ThinkingLevelMap] = mapped;
|
|
188
189
|
}
|
|
189
190
|
return map;
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
function readStoredModel(raw: unknown, path: string): StoredModel {
|
|
193
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
194
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
194
195
|
const reasoning = raw.reasoning;
|
|
195
|
-
if (typeof reasoning !== "boolean") fail(`${path}.reasoning`, "必须是 boolean");
|
|
196
|
+
if (typeof reasoning !== "boolean") fail(`${path}.reasoning`, t("必须是 boolean"));
|
|
196
197
|
|
|
197
198
|
const model: StoredModel = {
|
|
198
199
|
id: readRequiredString(raw, "id", path),
|
|
@@ -221,7 +222,7 @@ function readStoredModel(raw: unknown, path: string): StoredModel {
|
|
|
221
222
|
}
|
|
222
223
|
|
|
223
224
|
function readProviderMetadata(raw: unknown, path: string): ProviderMetadata {
|
|
224
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
225
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
225
226
|
const metadata: ProviderMetadata = {};
|
|
226
227
|
const clientHeaderProfile = readOptionalClientHeaderProfile(raw, "clientHeaderProfile", path);
|
|
227
228
|
if (clientHeaderProfile) metadata.clientHeaderProfile = clientHeaderProfile;
|
|
@@ -237,7 +238,7 @@ function readProviderMetadata(raw: unknown, path: string): ProviderMetadata {
|
|
|
237
238
|
}
|
|
238
239
|
|
|
239
240
|
function readModelMetadata(raw: unknown, path: string): ModelMetadata {
|
|
240
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
241
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
241
242
|
const metadata: ModelMetadata = {};
|
|
242
243
|
const openAIServiceTier = readOptionalOpenAIServiceTier(raw, "openAIServiceTier", path);
|
|
243
244
|
if (openAIServiceTier) metadata.openAIServiceTier = openAIServiceTier;
|
|
@@ -270,9 +271,9 @@ function inferProviderHeaderProfile(models: StoredModel[]): HeaderProfileSelecti
|
|
|
270
271
|
}
|
|
271
272
|
|
|
272
273
|
function readStoredProvider(raw: unknown, path: string, managed: boolean): StoredProvider {
|
|
273
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
274
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
274
275
|
const models = raw.models;
|
|
275
|
-
if (!Array.isArray(models)) fail(`${path}.models`, "必须是数组");
|
|
276
|
+
if (!Array.isArray(models)) fail(`${path}.models`, t("必须是数组"));
|
|
276
277
|
const storedModels = models.map((model, index) => readStoredModel(model, `${path}.models[${index}]`));
|
|
277
278
|
const inferredProfile = inferProviderHeaderProfile(storedModels);
|
|
278
279
|
const clientHeaderProfile = managed
|
|
@@ -304,7 +305,7 @@ function readStoredProvider(raw: unknown, path: string, managed: boolean): Store
|
|
|
304
305
|
}
|
|
305
306
|
|
|
306
307
|
function readRequestHeaderProfile(raw: unknown, path: string): StoredRequestHeaderProfile {
|
|
307
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
308
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
308
309
|
return {
|
|
309
310
|
name: readRequiredString(raw, "name", path),
|
|
310
311
|
headers: readStringRecord(raw.headers, `${path}.headers`),
|
|
@@ -312,7 +313,7 @@ function readRequestHeaderProfile(raw: unknown, path: string): StoredRequestHead
|
|
|
312
313
|
}
|
|
313
314
|
|
|
314
315
|
function readClientHeaderCapture(raw: unknown, path: string): StoredClientHeaderCapture {
|
|
315
|
-
if (!isObjectRecord(raw)) fail(path, "必须是对象");
|
|
316
|
+
if (!isObjectRecord(raw)) fail(path, t("必须是对象"));
|
|
316
317
|
return {
|
|
317
318
|
capturedAt: readRequiredString(raw, "capturedAt", path),
|
|
318
319
|
headers: readStringRecord(raw.headers, `${path}.headers`),
|
|
@@ -323,10 +324,10 @@ function readCommonMetadataFields(parsed: Record<string, unknown>, base: Metadat
|
|
|
323
324
|
const requestHeaderProfiles: MetadataDocument["requestHeaderProfiles"] = {};
|
|
324
325
|
const rawProfiles = parsed.requestHeaderProfiles;
|
|
325
326
|
if (rawProfiles !== undefined) {
|
|
326
|
-
if (!isObjectRecord(rawProfiles)) fail(".requestHeaderProfiles", "必须是对象");
|
|
327
|
+
if (!isObjectRecord(rawProfiles)) fail(".requestHeaderProfiles", t("必须是对象"));
|
|
327
328
|
for (const [profileId, profile] of Object.entries(rawProfiles)) {
|
|
328
329
|
if (RESERVED_REQUEST_HEADER_PROFILE_IDS.has(profileId)) {
|
|
329
|
-
fail(`.requestHeaderProfiles.${profileId}`, "不能使用插件内置请求头保留名");
|
|
330
|
+
fail(`.requestHeaderProfiles.${profileId}`, t("不能使用插件内置请求头保留名"));
|
|
330
331
|
}
|
|
331
332
|
requestHeaderProfiles[profileId] = readRequestHeaderProfile(profile, `.requestHeaderProfiles.${profileId}`);
|
|
332
333
|
}
|
|
@@ -335,10 +336,10 @@ function readCommonMetadataFields(parsed: Record<string, unknown>, base: Metadat
|
|
|
335
336
|
const clientHeaderCaptures: MetadataDocument["clientHeaderCaptures"] = {};
|
|
336
337
|
const rawCaptures = parsed.clientHeaderCaptures;
|
|
337
338
|
if (rawCaptures !== undefined) {
|
|
338
|
-
if (!isObjectRecord(rawCaptures)) fail(".clientHeaderCaptures", "必须是对象");
|
|
339
|
+
if (!isObjectRecord(rawCaptures)) fail(".clientHeaderCaptures", t("必须是对象"));
|
|
339
340
|
for (const [profileId, capture] of Object.entries(rawCaptures)) {
|
|
340
341
|
if (!BUILT_IN_CLIENT_HEADER_PROFILE_IDS.has(profileId as BuiltInClientHeaderProfileId)) {
|
|
341
|
-
fail(`.clientHeaderCaptures.${profileId}`, "只能保存 ClaudeCode/Codex 内置 profile 的抓包数据");
|
|
342
|
+
fail(`.clientHeaderCaptures.${profileId}`, t("只能保存 ClaudeCode/Codex 内置 profile 的抓包数据"));
|
|
342
343
|
}
|
|
343
344
|
clientHeaderCaptures[profileId as BuiltInClientHeaderProfileId] = readClientHeaderCapture(
|
|
344
345
|
capture,
|
|
@@ -400,7 +401,7 @@ function parseLegacyState(parsed: Record<string, unknown>): ParsedMetadataStateF
|
|
|
400
401
|
const providers: StateDocument["providers"] = {};
|
|
401
402
|
const rawProviders = parsed.providers;
|
|
402
403
|
if (rawProviders !== undefined) {
|
|
403
|
-
if (!isObjectRecord(rawProviders)) fail(".providers", "必须是对象");
|
|
404
|
+
if (!isObjectRecord(rawProviders)) fail(".providers", t("必须是对象"));
|
|
404
405
|
for (const [providerId, provider] of Object.entries(rawProviders)) {
|
|
405
406
|
providers[providerId] = readStoredProvider(provider, `.providers.${providerId}`, true);
|
|
406
407
|
}
|
|
@@ -418,14 +419,14 @@ function parseMetadataState(parsed: Record<string, unknown>): ParsedMetadataStat
|
|
|
418
419
|
const metadata = readCommonMetadataFields(parsed, createEmptyMetadata());
|
|
419
420
|
const rawProviders = parsed.providers;
|
|
420
421
|
if (rawProviders !== undefined) {
|
|
421
|
-
if (!isObjectRecord(rawProviders)) fail(".providers", "必须是对象");
|
|
422
|
+
if (!isObjectRecord(rawProviders)) fail(".providers", t("必须是对象"));
|
|
422
423
|
for (const [providerId, provider] of Object.entries(rawProviders)) {
|
|
423
424
|
metadata.providers[providerId] = readProviderMetadata(provider, `.providers.${providerId}`);
|
|
424
425
|
}
|
|
425
426
|
}
|
|
426
427
|
const rawModels = parsed.models;
|
|
427
428
|
if (rawModels !== undefined) {
|
|
428
|
-
if (!isObjectRecord(rawModels)) fail(".models", "必须是对象");
|
|
429
|
+
if (!isObjectRecord(rawModels)) fail(".models", t("必须是对象"));
|
|
429
430
|
for (const [fullModelId, model] of Object.entries(rawModels)) {
|
|
430
431
|
metadata.models[fullModelId] = readModelMetadata(model, `.models.${fullModelId}`);
|
|
431
432
|
}
|
|
@@ -448,10 +449,10 @@ function parseMetadataState(parsed: Record<string, unknown>): ParsedMetadataStat
|
|
|
448
449
|
|
|
449
450
|
function parseStateFile(source: string): ParsedMetadataStateFile {
|
|
450
451
|
const parsed = JSON.parse(stripJsonNoise(source));
|
|
451
|
-
if (!isObjectRecord(parsed)) fail("", "根节点必须是对象");
|
|
452
|
+
if (!isObjectRecord(parsed)) fail("", t("根节点必须是对象"));
|
|
452
453
|
if (parsed.version === 4 || parsed.version === 3 || parsed.version === 2) return parseMetadataState(parsed);
|
|
453
454
|
if (parsed.version === undefined || parsed.version === 1) return parseLegacyState(parsed);
|
|
454
|
-
fail(".version",
|
|
455
|
+
fail(".version", t("不支持的版本:{version}", { version: String(parsed.version) }));
|
|
455
456
|
}
|
|
456
457
|
|
|
457
458
|
export async function readMetadataStateSnapshot(): Promise<MetadataStateSnapshot> {
|
package/state-store.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { getBuiltinProviderDefaults } from "./builtin-model-catalog.ts";
|
|
|
8
8
|
import { mergeCompatSettings } from "./compat-settings.ts";
|
|
9
9
|
import { cloneJson, cloneStringRecord, hasStringRecordEntries, isObjectRecord } from "./common.ts";
|
|
10
10
|
import { readStableTextFileSnapshot } from "./file-snapshot.ts";
|
|
11
|
+
import { t } from "./i18n.ts";
|
|
11
12
|
import {
|
|
12
13
|
getModelsJsonPath,
|
|
13
14
|
hasPluginManagedProviderMarker,
|
|
@@ -281,7 +282,7 @@ export async function readState(): Promise<StateDocument> {
|
|
|
281
282
|
}
|
|
282
283
|
if (attempt < 2) await delay(10);
|
|
283
284
|
}
|
|
284
|
-
throw new Error("models.json/state.json 正在事务更新或持续变化;请稍后重试。");
|
|
285
|
+
throw new Error(t("models.json/state.json 正在事务更新或持续变化;请稍后重试。"));
|
|
285
286
|
}
|
|
286
287
|
export function getStatePath(): string {
|
|
287
288
|
return getMetadataStatePath();
|