pi-model-manager 0.3.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 +8 -6
- package/README.md +7 -5
- package/atomic-write.ts +4 -2
- package/common.ts +3 -2
- package/configuration-lock.ts +3 -2
- package/configuration-persistence.ts +13 -12
- package/file-snapshot.ts +2 -1
- package/header-profile-mutations.ts +4 -3
- package/i18n.ts +403 -0
- package/index.ts +20 -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 +4 -2
- package/presets/client-headers.ts +9 -9
- package/presets/providers.ts +2 -2
- package/provider-registrar.ts +4 -3
- package/request-pipeline.ts +3 -2
- package/rescue.ts +4 -3
- package/state-document.ts +35 -34
- package/state-metadata-store.ts +38 -37
- package/state-store.ts +2 -1
- package/tui/dashboard.ts +75 -44
- package/tui/editor-model.ts +36 -35
- package/tui/editor-provider.ts +35 -34
- package/tui/header-profiles-panel.ts +69 -62
- package/tui/model-list-fetch.ts +9 -8
- package/tui/model-list-validation.ts +8 -7
- package/tui/model-picker.ts +13 -8
- package/tui/persistent-menu.ts +12 -11
- package/tui/ui-helpers.ts +49 -42
- package/ui-language-settings.ts +47 -0
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();
|
package/tui/dashboard.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
13
13
|
import { formatUnknownError } from "../common.ts";
|
|
14
|
+
import { getUiLanguage, setUiLanguage, t, type UiLanguage } from "../i18n.ts";
|
|
15
|
+
import { writeUiLanguage } from "../ui-language-settings.ts";
|
|
14
16
|
import {
|
|
15
17
|
deleteModelConfiguration,
|
|
16
18
|
deleteProviderConfiguration,
|
|
@@ -35,7 +37,7 @@ import { getBuiltinProviderIds } from "../builtin-model-catalog.ts";
|
|
|
35
37
|
import type { StateDocument, StoredModel, StoredProvider } from "../types.ts";
|
|
36
38
|
import { editModel } from "./editor-model.ts";
|
|
37
39
|
import { editProvider } from "./editor-provider.ts";
|
|
38
|
-
import { showPersistentShortcutMenu, type MenuCursor } from "./persistent-menu.ts";
|
|
40
|
+
import { showOptionPicker, showPersistentShortcutMenu, type MenuCursor } from "./persistent-menu.ts";
|
|
39
41
|
import {
|
|
40
42
|
formatModelListHeader,
|
|
41
43
|
formatModelListRow,
|
|
@@ -85,12 +87,33 @@ function buildProviderMenuRows(provider: StoredProvider): ProviderMenuRow[] {
|
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
function notifyValidationErrors(ctx: ExtensionCommandContext, title: string, errors: string[]): void {
|
|
88
|
-
ctx.ui.notify(
|
|
90
|
+
ctx.ui.notify(t("{title}:\n{errors}", { title, errors: errors.map((error) => `- ${error}`).join("\n") }), "warning");
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
async function confirmWarnings(ctx: ExtensionCommandContext, warnings: string[]): Promise<boolean> {
|
|
92
94
|
if (warnings.length === 0) return true;
|
|
93
|
-
return ctx.ui.confirm("请确认改动",
|
|
95
|
+
return ctx.ui.confirm(t("请确认改动"), t("{warnings}\n\n继续保存?", { warnings: warnings.join("\n") }));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function selectUiLanguage(ctx: ExtensionCommandContext): Promise<void> {
|
|
99
|
+
const currentLanguage = getUiLanguage();
|
|
100
|
+
const choice = await showOptionPicker<{ id: UiLanguage; label: string }>(
|
|
101
|
+
ctx,
|
|
102
|
+
"语言 / Language",
|
|
103
|
+
[
|
|
104
|
+
{ id: "zh-CN", label: "简体中文 / Simplified Chinese" },
|
|
105
|
+
{ id: "en", label: "English" },
|
|
106
|
+
],
|
|
107
|
+
currentLanguage,
|
|
108
|
+
);
|
|
109
|
+
if (!choice || choice.id === currentLanguage) return;
|
|
110
|
+
try {
|
|
111
|
+
await writeUiLanguage(choice.id);
|
|
112
|
+
setUiLanguage(choice.id);
|
|
113
|
+
ctx.ui.notify(t("语言已切换为 {language}", { language: choice.id === "en" ? "English" : "简体中文" }), "info");
|
|
114
|
+
} catch (error) {
|
|
115
|
+
ctx.ui.notify(t("语言设置保存失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
116
|
+
}
|
|
94
117
|
}
|
|
95
118
|
|
|
96
119
|
|
|
@@ -106,7 +129,7 @@ async function saveProviderDraft(
|
|
|
106
129
|
const builtInIds = await getBuiltinProviderIds();
|
|
107
130
|
const errors = validateProviderDraft(draft, state, builtInIds, oldProviderId);
|
|
108
131
|
if (errors.length > 0) {
|
|
109
|
-
notifyValidationErrors(ctx, "接入配置无效", errors);
|
|
132
|
+
notifyValidationErrors(ctx, t("接入配置无效"), errors);
|
|
110
133
|
return false;
|
|
111
134
|
}
|
|
112
135
|
const current = oldProviderId ? state.providers[oldProviderId] : state.providers[draft.providerId];
|
|
@@ -116,7 +139,7 @@ async function saveProviderDraft(
|
|
|
116
139
|
await saveProviderConfiguration(pi, ctx, state, draft, oldProviderId);
|
|
117
140
|
return true;
|
|
118
141
|
} catch (error) {
|
|
119
|
-
ctx.ui.notify(
|
|
142
|
+
ctx.ui.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
120
143
|
return false;
|
|
121
144
|
}
|
|
122
145
|
}
|
|
@@ -130,14 +153,14 @@ async function saveModelDraft(
|
|
|
130
153
|
const state = await readState();
|
|
131
154
|
const errors = validateModelDraft(draft, state, replacedModelId);
|
|
132
155
|
if (errors.length > 0) {
|
|
133
|
-
notifyValidationErrors(ctx, "模型配置无效", errors);
|
|
156
|
+
notifyValidationErrors(ctx, t("模型配置无效"), errors);
|
|
134
157
|
return false;
|
|
135
158
|
}
|
|
136
159
|
try {
|
|
137
160
|
await saveModelConfiguration(pi, ctx, state, draft, replacedModelId);
|
|
138
161
|
return true;
|
|
139
162
|
} catch (error) {
|
|
140
|
-
ctx.ui.notify(
|
|
163
|
+
ctx.ui.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
141
164
|
return false;
|
|
142
165
|
}
|
|
143
166
|
}
|
|
@@ -152,20 +175,20 @@ async function saveNewProviderAndModelDraft(
|
|
|
152
175
|
const builtInIds = await getBuiltinProviderIds();
|
|
153
176
|
const providerErrors = validateProviderDraft(providerDraft, state, builtInIds, undefined);
|
|
154
177
|
if (providerErrors.length > 0) {
|
|
155
|
-
notifyValidationErrors(ctx, "接入配置无效", providerErrors);
|
|
178
|
+
notifyValidationErrors(ctx, t("接入配置无效"), providerErrors);
|
|
156
179
|
return false;
|
|
157
180
|
}
|
|
158
181
|
const providerState = upsertProviderInDocument(state, undefined, providerDraft);
|
|
159
182
|
const modelErrors = validateModelDraft(modelDraft, providerState, undefined);
|
|
160
183
|
if (modelErrors.length > 0) {
|
|
161
|
-
notifyValidationErrors(ctx, "模型配置无效", modelErrors);
|
|
184
|
+
notifyValidationErrors(ctx, t("模型配置无效"), modelErrors);
|
|
162
185
|
return false;
|
|
163
186
|
}
|
|
164
187
|
try {
|
|
165
188
|
await saveNewProviderWithModelConfiguration(pi, ctx, providerState, providerDraft, modelDraft);
|
|
166
189
|
return true;
|
|
167
190
|
} catch (error) {
|
|
168
|
-
ctx.ui.notify(
|
|
191
|
+
ctx.ui.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
169
192
|
return false;
|
|
170
193
|
}
|
|
171
194
|
}
|
|
@@ -174,15 +197,15 @@ async function saveNewProviderAndModelDraft(
|
|
|
174
197
|
|
|
175
198
|
async function deleteProvider(pi: ExtensionAPI, ctx: ExtensionCommandContext, providerId: string, provider: StoredProvider): Promise<boolean> {
|
|
176
199
|
const ok = await ctx.ui.confirm(
|
|
177
|
-
|
|
178
|
-
|
|
200
|
+
t("删除接入 {providerId}", { providerId }),
|
|
201
|
+
t("将删除 {name} 下全部 {count} 个模型。", { name: getProviderDisplayName(providerId, provider), count: provider.models.length }),
|
|
179
202
|
);
|
|
180
203
|
if (!ok) return false;
|
|
181
204
|
try {
|
|
182
205
|
await deleteProviderConfiguration(pi, ctx, providerId, provider);
|
|
183
206
|
return true;
|
|
184
207
|
} catch (error) {
|
|
185
|
-
ctx.ui.notify(
|
|
208
|
+
ctx.ui.notify(t("删除失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
186
209
|
return false;
|
|
187
210
|
}
|
|
188
211
|
}
|
|
@@ -193,15 +216,15 @@ async function deleteModel(pi: ExtensionAPI, ctx: ExtensionCommandContext, provi
|
|
|
193
216
|
const currentProvider = stateForPrompt.providers[providerId];
|
|
194
217
|
const removesLastModel = (currentProvider?.models.length ?? 0) <= 1;
|
|
195
218
|
const ok = await ctx.ui.confirm(
|
|
196
|
-
|
|
197
|
-
removesLastModel ? "这是该接入下最后一个模型;删除后接入也会从 models.json 中移除。" : "只删除该模型,接入保留。",
|
|
219
|
+
t("删除模型 {fullId}", { fullId }),
|
|
220
|
+
removesLastModel ? t("这是该接入下最后一个模型;删除后接入也会从 models.json 中移除。") : t("只删除该模型,接入保留。"),
|
|
198
221
|
);
|
|
199
222
|
if (!ok) return false;
|
|
200
223
|
try {
|
|
201
224
|
await deleteModelConfiguration(pi, ctx, providerId, modelId);
|
|
202
225
|
return true;
|
|
203
226
|
} catch (error) {
|
|
204
|
-
ctx.ui.notify(
|
|
227
|
+
ctx.ui.notify(t("删除失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
205
228
|
return false;
|
|
206
229
|
}
|
|
207
230
|
}
|
|
@@ -221,11 +244,11 @@ async function editStoredModel(
|
|
|
221
244
|
): Promise<boolean> {
|
|
222
245
|
const model = provider.models.find((m) => m.id === modelId);
|
|
223
246
|
if (!model) {
|
|
224
|
-
ctx.ui.notify(
|
|
247
|
+
ctx.ui.notify(t("模型不存在:{fullId}", { fullId: getModelFullId(providerId, modelId) }), "error");
|
|
225
248
|
return false;
|
|
226
249
|
}
|
|
227
250
|
const draft = createModelDraftFromStoredModel(providerId, provider, model);
|
|
228
|
-
const outcome = await editModel(ctx, draft,
|
|
251
|
+
const outcome = await editModel(ctx, draft, t("编辑模型 {fullId}", { fullId: getModelFullId(providerId, modelId) }), requestHeaderProfiles, clientHeaderCaptures);
|
|
229
252
|
if (outcome.action !== "save") return false;
|
|
230
253
|
return saveModelDraft(pi, ctx, outcome.draft, modelId);
|
|
231
254
|
}
|
|
@@ -236,7 +259,7 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
236
259
|
const state = await readState();
|
|
237
260
|
const currentProvider = state.providers[providerId];
|
|
238
261
|
if (!currentProvider) {
|
|
239
|
-
ctx.ui.notify(
|
|
262
|
+
ctx.ui.notify(t("接入配置已不存在:{providerId}", { providerId }), "warning");
|
|
240
263
|
return true;
|
|
241
264
|
}
|
|
242
265
|
const rows = buildProviderMenuRows(currentProvider);
|
|
@@ -264,27 +287,27 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
264
287
|
},
|
|
265
288
|
visibleRows: Math.min(12, Math.max(1, rows.length)),
|
|
266
289
|
hints: [
|
|
267
|
-
{ key: "↑↓", label: "选择" },
|
|
268
|
-
{ key: "Enter", label: "编辑模型" },
|
|
269
|
-
{ key: "A", label: "添加模型" },
|
|
270
|
-
{ key: "E", label: "编辑接入" },
|
|
271
|
-
{ key: "D", label: "删除模型" },
|
|
272
|
-
{ key: "Esc", label: "返回" },
|
|
290
|
+
{ key: "↑↓", label: t("选择") },
|
|
291
|
+
{ key: "Enter", label: t("编辑模型") },
|
|
292
|
+
{ key: "A", label: t("添加模型") },
|
|
293
|
+
{ key: "E", label: t("编辑接入") },
|
|
294
|
+
{ key: "D", label: t("删除模型") },
|
|
295
|
+
{ key: "Esc", label: t("返回") },
|
|
273
296
|
],
|
|
274
|
-
emptyLabel: "暂无模型;按 a 添加第一个模型",
|
|
297
|
+
emptyLabel: t("暂无模型;按 a 添加第一个模型"),
|
|
275
298
|
},
|
|
276
299
|
);
|
|
277
300
|
if (action.type === "cancel") return false;
|
|
278
301
|
if (action.type === "shortcut") {
|
|
279
302
|
if (action.shortcut === "add-model") {
|
|
280
303
|
const draft = createModelDraftForStoredProvider(providerId, currentProvider);
|
|
281
|
-
const outcome = await editModel(ctx, draft,
|
|
304
|
+
const outcome = await editModel(ctx, draft, t("添加模型到 {providerId}", { providerId }), state.requestHeaderProfiles, state.clientHeaderCaptures);
|
|
282
305
|
if (outcome.action === "save") await saveModelDraft(pi, ctx, outcome.draft, undefined);
|
|
283
306
|
continue;
|
|
284
307
|
}
|
|
285
308
|
if (action.shortcut === "edit-provider") {
|
|
286
309
|
const draft = createProviderDraftFromStored(providerId, currentProvider);
|
|
287
|
-
const outcome = await editProvider(ctx, draft,
|
|
310
|
+
const outcome = await editProvider(ctx, draft, t("编辑接入 {providerId}", { providerId }), state.requestHeaderProfiles);
|
|
288
311
|
if (outcome.action === "save") {
|
|
289
312
|
await saveProviderDraft(pi, ctx, outcome.draft, providerId);
|
|
290
313
|
if (outcome.draft.providerId !== providerId) return true;
|
|
@@ -293,7 +316,7 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
293
316
|
}
|
|
294
317
|
const selectedModelId = rows[cursor.index]?.modelId;
|
|
295
318
|
if (!selectedModelId) {
|
|
296
|
-
ctx.ui.notify("没有可删除的模型;删除接入请返回上一级按 d。", "info");
|
|
319
|
+
ctx.ui.notify(t("没有可删除的模型;删除接入请返回上一级按 d。"), "info");
|
|
297
320
|
continue;
|
|
298
321
|
}
|
|
299
322
|
await deleteModel(pi, ctx, providerId, selectedModelId);
|
|
@@ -314,21 +337,21 @@ async function createProviderAndModel(pi: ExtensionAPI, ctx: ExtensionCommandCon
|
|
|
314
337
|
const providerOutcome = await editProvider(
|
|
315
338
|
ctx,
|
|
316
339
|
providerDraft,
|
|
317
|
-
"新建接入配置",
|
|
340
|
+
t("新建接入配置"),
|
|
318
341
|
stateBeforeEdit.requestHeaderProfiles,
|
|
319
342
|
);
|
|
320
343
|
if (providerOutcome.action !== "save") return false;
|
|
321
344
|
const providerErrors = validateProviderDraft(providerOutcome.draft, stateBeforeEdit, builtInIds, undefined);
|
|
322
345
|
if (providerErrors.length > 0) {
|
|
323
|
-
notifyValidationErrors(ctx, "接入配置无效", providerErrors);
|
|
346
|
+
notifyValidationErrors(ctx, t("接入配置无效"), providerErrors);
|
|
324
347
|
continue;
|
|
325
348
|
}
|
|
326
349
|
|
|
327
350
|
const stagedState = upsertProviderInDocument(stateBeforeEdit, undefined, providerOutcome.draft);
|
|
328
351
|
const stored = stagedState.providers[providerOutcome.draft.providerId]!;
|
|
329
|
-
ctx.ui.notify("请继续添加第一个模型;取消模型编辑则不会创建该接入。", "info");
|
|
352
|
+
ctx.ui.notify(t("请继续添加第一个模型;取消模型编辑则不会创建该接入。"), "info");
|
|
330
353
|
const modelDraft = createModelDraftForStoredProvider(providerOutcome.draft.providerId, stored);
|
|
331
|
-
const modelOutcome = await editModel(ctx, modelDraft,
|
|
354
|
+
const modelOutcome = await editModel(ctx, modelDraft, t("添加模型到 {providerId}", { providerId: providerOutcome.draft.providerId }), stagedState.requestHeaderProfiles, stagedState.clientHeaderCaptures);
|
|
332
355
|
if (modelOutcome.action !== "save") return false;
|
|
333
356
|
return saveNewProviderAndModelDraft(pi, ctx, providerOutcome.draft, modelOutcome.draft);
|
|
334
357
|
}
|
|
@@ -336,7 +359,7 @@ async function createProviderAndModel(pi: ExtensionAPI, ctx: ExtensionCommandCon
|
|
|
336
359
|
|
|
337
360
|
// ========== 入口 ==========
|
|
338
361
|
|
|
339
|
-
type DashboardShortcut = "new-provider" | "header-profiles" | "delete-provider";
|
|
362
|
+
type DashboardShortcut = "new-provider" | "header-profiles" | "delete-provider" | "language";
|
|
340
363
|
|
|
341
364
|
export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContext): Promise<void> {
|
|
342
365
|
const cursor: MenuCursor = { index: 0 };
|
|
@@ -361,9 +384,15 @@ export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContex
|
|
|
361
384
|
{ input: "n", shortcut: "new-provider" },
|
|
362
385
|
{ input: "h", shortcut: "header-profiles" },
|
|
363
386
|
{ input: "d", shortcut: "delete-provider" },
|
|
387
|
+
{ input: "l", shortcut: "language" },
|
|
364
388
|
],
|
|
365
389
|
{
|
|
366
|
-
summaryLines: [
|
|
390
|
+
summaryLines: [t("{providers} 接入 · {models} 模型 · {captures} 内置抓包 · {profiles} 自定义请求头", {
|
|
391
|
+
providers: rows.length,
|
|
392
|
+
models: modelCount,
|
|
393
|
+
captures: captureCount,
|
|
394
|
+
profiles: profileCount,
|
|
395
|
+
})],
|
|
367
396
|
tableHeader: (width) => formatProviderConsoleHeader(width, { nameWidth }),
|
|
368
397
|
formatRow: (menuRow, width, theme) => {
|
|
369
398
|
const row = rows[Number.parseInt(menuRow.id, 10)];
|
|
@@ -380,25 +409,27 @@ export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContex
|
|
|
380
409
|
: [];
|
|
381
410
|
},
|
|
382
411
|
hints: [
|
|
383
|
-
{ key: "↑↓", label: "选择" },
|
|
384
|
-
{ key: "Enter", label: "进入" },
|
|
385
|
-
{ key: "N", label: "新建接入" },
|
|
386
|
-
{ key: "D", label: "删除接入" },
|
|
387
|
-
{ key: "H", label: "
|
|
388
|
-
{ key: "
|
|
412
|
+
{ key: "↑↓", label: t("选择") },
|
|
413
|
+
{ key: "Enter", label: t("进入") },
|
|
414
|
+
{ key: "N", label: t("新建接入") },
|
|
415
|
+
{ key: "D", label: t("删除接入") },
|
|
416
|
+
{ key: "H", label: t("请求头管理") },
|
|
417
|
+
{ key: "L", label: "语言 / Language" },
|
|
418
|
+
{ key: "Esc", label: t("退出") },
|
|
389
419
|
],
|
|
390
|
-
emptyLabel: "暂无自定义接入;按 n 新建",
|
|
420
|
+
emptyLabel: t("暂无自定义接入;按 n 新建"),
|
|
391
421
|
},
|
|
392
422
|
);
|
|
393
423
|
if (action.type === "cancel") return;
|
|
394
424
|
if (action.type === "shortcut") {
|
|
395
425
|
if (action.shortcut === "new-provider") await createProviderAndModel(pi, ctx);
|
|
396
426
|
if (action.shortcut === "header-profiles") await runHeaderProfilesPanel(pi, ctx);
|
|
427
|
+
if (action.shortcut === "language") await selectUiLanguage(ctx);
|
|
397
428
|
if (action.shortcut === "delete-provider") {
|
|
398
429
|
const selectedProviderId = rows[cursor.index]?.providerId;
|
|
399
430
|
const selectedProvider = selectedProviderId ? state.providers[selectedProviderId] : undefined;
|
|
400
431
|
if (selectedProviderId && selectedProvider) await deleteProvider(pi, ctx, selectedProviderId, selectedProvider);
|
|
401
|
-
else ctx.ui.notify("没有可删除的接入。", "info");
|
|
432
|
+
else ctx.ui.notify(t("没有可删除的接入。"), "info");
|
|
402
433
|
}
|
|
403
434
|
continue;
|
|
404
435
|
}
|
|
@@ -407,7 +438,7 @@ export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContex
|
|
|
407
438
|
const row = rows[index]!;
|
|
408
439
|
const provider = state.providers[row.providerId];
|
|
409
440
|
if (!provider) {
|
|
410
|
-
ctx.ui.notify(
|
|
441
|
+
ctx.ui.notify(t("接入配置已不存在:{providerId}", { providerId: row.providerId }), "warning");
|
|
411
442
|
continue;
|
|
412
443
|
}
|
|
413
444
|
await showProviderMenu(pi, ctx, row.providerId);
|