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/local-proxy-service.ts
CHANGED
|
@@ -8,6 +8,7 @@ import http, { type IncomingHttpHeaders, type IncomingMessage, type ServerRespon
|
|
|
8
8
|
import https from "node:https";
|
|
9
9
|
import { HttpProxyAgent } from "http-proxy-agent";
|
|
10
10
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
11
|
+
import { t } from "./i18n.ts";
|
|
11
12
|
import { redactSensitiveText, redactUrlForDisplay } from "./sensitive-redaction.ts";
|
|
12
13
|
import type { StoredProvider } from "./types.ts";
|
|
13
14
|
import { DEFAULT_PROVIDER_HTTP_PROXY_URL } from "./types.ts";
|
|
@@ -54,7 +55,7 @@ function parseHttpProxyUrl(proxyUrl: string | undefined): URL {
|
|
|
54
55
|
try {
|
|
55
56
|
parsed = new URL(normalized);
|
|
56
57
|
} catch {
|
|
57
|
-
throw new Error(
|
|
58
|
+
throw new Error(t("代理地址无效:{url}", { url: redactUrlForDisplay(normalized) }));
|
|
58
59
|
}
|
|
59
60
|
ensureSupportedProxyProtocol(parsed);
|
|
60
61
|
return parsed;
|
|
@@ -88,7 +89,7 @@ function getHostHeader(url: URL): string {
|
|
|
88
89
|
|
|
89
90
|
function ensureSupportedProxyProtocol(proxy: URL): void {
|
|
90
91
|
if (proxy.protocol !== "http:" && proxy.protocol !== "https:") {
|
|
91
|
-
throw new Error("代理地址目前只支持 http:// 或 https:// 代理");
|
|
92
|
+
throw new Error(t("代理地址目前只支持 http:// 或 https:// 代理"));
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
|
|
@@ -111,11 +112,11 @@ function formatProxyError(error: unknown): string {
|
|
|
111
112
|
if (!message) {
|
|
112
113
|
const code = (error as Error & { code?: string }).code;
|
|
113
114
|
message = code
|
|
114
|
-
?
|
|
115
|
-
: error.stack?.trim().split("\n", 1)[0] ?? `${error.name || "Error"}
|
|
115
|
+
? t("代理请求失败:{code}", { code })
|
|
116
|
+
: error.stack?.trim().split("\n", 1)[0] ?? `${error.name || "Error"} (${t("无错误消息")})`;
|
|
116
117
|
}
|
|
117
118
|
} else {
|
|
118
|
-
message = String(error).trim() || "未知代理错误";
|
|
119
|
+
message = String(error).trim() || t("未知代理错误");
|
|
119
120
|
}
|
|
120
121
|
return redactSensitiveText(message);
|
|
121
122
|
}
|
|
@@ -209,7 +210,7 @@ function forwardRequest(route: ProviderProxyRoute, request: IncomingMessage, res
|
|
|
209
210
|
forwardHttps(target, proxy, request, response);
|
|
210
211
|
return;
|
|
211
212
|
}
|
|
212
|
-
throw new Error(
|
|
213
|
+
throw new Error(t("不支持的上游协议:{protocol}", { protocol: target.protocol }));
|
|
213
214
|
}
|
|
214
215
|
|
|
215
216
|
function handleRequest(request: IncomingMessage, response: ServerResponse): void {
|
|
@@ -245,7 +246,7 @@ function clearServerState(expected: http.Server): void {
|
|
|
245
246
|
}
|
|
246
247
|
|
|
247
248
|
async function ensureServer(): Promise<number> {
|
|
248
|
-
if (closingPromise) throw new Error("本地代理服务正在关闭,请稍后重试");
|
|
249
|
+
if (closingPromise) throw new Error(t("本地代理服务正在关闭,请稍后重试"));
|
|
249
250
|
if (listenPort !== undefined) return listenPort;
|
|
250
251
|
if (listenPromise) return listenPromise;
|
|
251
252
|
|
|
@@ -257,7 +258,7 @@ async function ensureServer(): Promise<number> {
|
|
|
257
258
|
const address = nextServer.address();
|
|
258
259
|
if (!address || typeof address === "string") {
|
|
259
260
|
nextServer.off("error", rejectListen);
|
|
260
|
-
reject(new Error("本地代理转发服务监听地址异常"));
|
|
261
|
+
reject(new Error(t("本地代理转发服务监听地址异常")));
|
|
261
262
|
return;
|
|
262
263
|
}
|
|
263
264
|
nextServer.off("error", rejectListen);
|
|
@@ -280,7 +281,7 @@ async function ensureServer(): Promise<number> {
|
|
|
280
281
|
|
|
281
282
|
async function ensureServerForRoute(): Promise<number> {
|
|
282
283
|
const port = await ensureServer();
|
|
283
|
-
if (closingPromise) throw new Error("本地代理服务正在关闭,请稍后重试");
|
|
284
|
+
if (closingPromise) throw new Error(t("本地代理服务正在关闭,请稍后重试"));
|
|
284
285
|
return port;
|
|
285
286
|
}
|
|
286
287
|
|
package/model-mutations.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { formatUnknownError } from "./common.ts";
|
|
8
8
|
import { isBuiltinProviderId } from "./builtin-model-catalog.ts";
|
|
9
|
+
import { t } from "./i18n.ts";
|
|
9
10
|
import {
|
|
10
11
|
persistManagedConfiguration,
|
|
11
12
|
persistModelConfiguration,
|
|
@@ -31,14 +32,14 @@ type ModelDraft = ReturnType<typeof createModelDraftForStoredProvider>;
|
|
|
31
32
|
|
|
32
33
|
async function assertProviderIsEditable(providerId: string): Promise<void> {
|
|
33
34
|
if (await isBuiltinProviderId(providerId)) {
|
|
34
|
-
throw new Error(
|
|
35
|
+
throw new Error(t("内置接入 {providerId} 不允许通过 /model-manager 编辑或删除。", { providerId }));
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
function formatEnableNote(mode: "all-enabled" | "updated" | "unchanged", scope?: "global" | "project"): string {
|
|
39
|
-
if (mode === "all-enabled") return "当前未限制 enabledModels,重启后默认可选";
|
|
40
|
-
if (mode === "updated") return
|
|
41
|
-
return "已在 enabledModels 中";
|
|
40
|
+
if (mode === "all-enabled") return t("当前未限制 enabledModels,重启后默认可选");
|
|
41
|
+
if (mode === "updated") return scope === "project" ? t("已写入项目 enabledModels") : t("已写入全局 enabledModels");
|
|
42
|
+
return t("已在 enabledModels 中");
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
async function notifyModelAvailability(
|
|
@@ -46,7 +47,7 @@ async function notifyModelAvailability(
|
|
|
46
47
|
providerId: string,
|
|
47
48
|
modelId: string,
|
|
48
49
|
replacedFullId?: string,
|
|
49
|
-
messagePrefix = "已保存并启用模型",
|
|
50
|
+
messagePrefix = t("已保存并启用模型"),
|
|
50
51
|
): Promise<void> {
|
|
51
52
|
const fullId = getModelFullId(providerId, modelId);
|
|
52
53
|
try {
|
|
@@ -54,12 +55,12 @@ async function notifyModelAvailability(
|
|
|
54
55
|
const verification = await verifyNativeModelAvailable(ctx, providerId, modelId);
|
|
55
56
|
const enableNote = formatEnableNote(enableOutcome.mode, enableOutcome.scope);
|
|
56
57
|
if (verification.ok) {
|
|
57
|
-
ctx.ui.notify(
|
|
58
|
+
ctx.ui.notify(t("{messagePrefix} {fullId}({availability})", { messagePrefix, fullId, availability: enableNote }), "info");
|
|
58
59
|
} else {
|
|
59
|
-
ctx.ui.notify(
|
|
60
|
+
ctx.ui.notify(t("已保存模型 {fullId},但启用校验有警告:\n- {warnings}", { fullId, warnings: verification.warnings.join("\n- ") }), "warning");
|
|
60
61
|
}
|
|
61
62
|
} catch (error) {
|
|
62
|
-
ctx.ui.notify(
|
|
63
|
+
ctx.ui.notify(t("已保存模型 {fullId},但启用同步/校验失败:{error}", { fullId, error: formatUnknownError(error) }), "warning");
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
|
|
@@ -72,7 +73,7 @@ async function reconcilePersistedProviderRuntime(
|
|
|
72
73
|
try {
|
|
73
74
|
await reconcileProvider(pi, providerId, provider, document.requestHeaderProfiles, document.clientHeaderCaptures);
|
|
74
75
|
} catch (error) {
|
|
75
|
-
throw new Error(
|
|
76
|
+
throw new Error(t("models.json/state.json 已保存,但当前会话接入刷新失败:{error}。可执行 /reload 重试。", { error: formatUnknownError(error) }));
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -102,7 +103,7 @@ export async function saveProviderConfiguration(
|
|
|
102
103
|
await reconcilePersistedProviderRuntime(pi, draft.providerId, stored, nextState);
|
|
103
104
|
if (oldProviderId && oldProviderId !== draft.providerId) unregisterManagedProvider(pi, oldProviderId);
|
|
104
105
|
|
|
105
|
-
ctx.ui.notify(
|
|
106
|
+
ctx.ui.notify(t("已保存接入 {providerId}", { providerId: draft.providerId }), "info");
|
|
106
107
|
|
|
107
108
|
if (oldProviderId && oldProviderId !== draft.providerId) {
|
|
108
109
|
try {
|
|
@@ -112,13 +113,15 @@ export async function saveProviderConfiguration(
|
|
|
112
113
|
draft.providerId,
|
|
113
114
|
);
|
|
114
115
|
if (outcome.mode === "updated") {
|
|
115
|
-
ctx.ui.notify(
|
|
116
|
+
ctx.ui.notify(outcome.scope === "project"
|
|
117
|
+
? t("已同步项目 enabledModels 中的接入重命名")
|
|
118
|
+
: t("已同步全局 enabledModels 中的接入重命名"), "info");
|
|
116
119
|
}
|
|
117
120
|
} catch (error) {
|
|
118
|
-
ctx.ui.notify(
|
|
121
|
+
ctx.ui.notify(t("接入已保存,但 enabledModels 同步失败:{error}", { error: formatUnknownError(error) }), "warning");
|
|
119
122
|
}
|
|
120
123
|
await withModelRescue(ctx, pi, { providerId: oldProviderId }, {
|
|
121
|
-
reason:
|
|
124
|
+
reason: t("接入 {oldProviderId} 已重命名为 {providerId}", { oldProviderId, providerId: draft.providerId }),
|
|
122
125
|
preferred: renamedCurrentModelId
|
|
123
126
|
? { providerId: draft.providerId, modelId: renamedCurrentModelId }
|
|
124
127
|
: undefined,
|
|
@@ -156,7 +159,7 @@ export async function saveModelConfiguration(
|
|
|
156
159
|
pi,
|
|
157
160
|
{ providerId: draft.providerId, modelId: replacedModelId },
|
|
158
161
|
{
|
|
159
|
-
reason:
|
|
162
|
+
reason: t("模型 {oldFullId} 已重命名为 {newFullId}", { oldFullId, newFullId }),
|
|
160
163
|
preferred: { providerId: draft.providerId, modelId: draft.modelId.trim() },
|
|
161
164
|
},
|
|
162
165
|
);
|
|
@@ -179,7 +182,7 @@ export async function saveNewProviderWithModelConfiguration(
|
|
|
179
182
|
const nextState = await persistManagedConfiguration(ctx, prepare);
|
|
180
183
|
const stored = nextState.providers[providerDraft.providerId]!;
|
|
181
184
|
await reconcilePersistedProviderRuntime(pi, providerDraft.providerId, stored, nextState);
|
|
182
|
-
await notifyModelAvailability(ctx, providerDraft.providerId, modelDraft.modelId.trim(), undefined, "已创建并启用模型");
|
|
185
|
+
await notifyModelAvailability(ctx, providerDraft.providerId, modelDraft.modelId.trim(), undefined, t("已创建并启用模型"));
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
export async function deleteProviderConfiguration(
|
|
@@ -197,11 +200,11 @@ export async function deleteProviderConfiguration(
|
|
|
197
200
|
try {
|
|
198
201
|
await removeProviderFromNextPiStart(ctx.cwd, providerId);
|
|
199
202
|
} catch (error) {
|
|
200
|
-
ctx.ui.notify(
|
|
203
|
+
ctx.ui.notify(t("接入已删除,但 enabledModels 清理失败:{error}", { error: formatUnknownError(error) }), "warning");
|
|
201
204
|
}
|
|
202
205
|
unregisterManagedProvider(pi, providerId);
|
|
203
|
-
ctx.ui.notify(
|
|
204
|
-
await withModelRescue(ctx, pi, { providerId }, { reason:
|
|
206
|
+
ctx.ui.notify(t("已删除接入 {providerId}", { providerId }), "info");
|
|
207
|
+
await withModelRescue(ctx, pi, { providerId }, { reason: t("接入 {providerId} 已删除", { providerId }) });
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
export async function deleteModelConfiguration(
|
|
@@ -227,10 +230,10 @@ export async function deleteModelConfiguration(
|
|
|
227
230
|
try {
|
|
228
231
|
await removeModelFromNextPiStart(ctx.cwd, fullId);
|
|
229
232
|
} catch (error) {
|
|
230
|
-
ctx.ui.notify(
|
|
233
|
+
ctx.ui.notify(t("模型已删除,但 enabledModels 清理失败:{error}", { error: formatUnknownError(error) }), "warning");
|
|
231
234
|
}
|
|
232
235
|
if (stored) await reconcilePersistedProviderRuntime(pi, providerId, stored, nextState);
|
|
233
236
|
else unregisterManagedProvider(pi, providerId);
|
|
234
|
-
ctx.ui.notify(
|
|
235
|
-
await withModelRescue(ctx, pi, { providerId, modelId }, { reason:
|
|
237
|
+
ctx.ui.notify(t("已删除模型 {fullId}", { fullId }), "info");
|
|
238
|
+
await withModelRescue(ctx, pi, { providerId, modelId }, { reason: t("模型 {fullId} 已删除", { fullId }) });
|
|
236
239
|
}
|
package/models-json-manager.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
|
9
9
|
import { join } from "node:path";
|
|
10
10
|
import { cloneJson, isObjectRecord, stripJsonNoise } from "./common.ts";
|
|
11
11
|
import { readStableTextFileSnapshot, type FileSignature } from "./file-snapshot.ts";
|
|
12
|
+
import { t } from "./i18n.ts";
|
|
12
13
|
import type { TokenCost } from "./types.ts";
|
|
13
14
|
|
|
14
15
|
export const MODELS_JSON_PATH = join(getAgentDir(), "models.json");
|
|
@@ -96,7 +97,7 @@ function createEmpty(): ModelsJsonDocument {
|
|
|
96
97
|
|
|
97
98
|
function parseModelsJson(source: string): ModelsJsonDocument {
|
|
98
99
|
const parsed = JSON.parse(stripJsonNoise(source));
|
|
99
|
-
if (!isObjectRecord(parsed)) throw new Error("models.json 根节点必须是对象");
|
|
100
|
+
if (!isObjectRecord(parsed)) throw new Error(t("models.json 根节点必须是对象"));
|
|
100
101
|
const providers = isObjectRecord(parsed.providers) ? parsed.providers : {};
|
|
101
102
|
return { ...parsed, providers: providers as Record<string, ModelsJsonProviderEntry> };
|
|
102
103
|
}
|
|
@@ -141,8 +142,8 @@ export function renameProviderInDoc(
|
|
|
141
142
|
const next = cloneJson(doc);
|
|
142
143
|
if (oldProviderId === newProviderId) return next;
|
|
143
144
|
const source = next.providers[oldProviderId];
|
|
144
|
-
if (!source) throw new Error(
|
|
145
|
-
if (next.providers[newProviderId]) throw new Error(
|
|
145
|
+
if (!source) throw new Error(t("models.json 中不存在待重命名接入:{providerId}", { providerId: oldProviderId }));
|
|
146
|
+
if (next.providers[newProviderId]) throw new Error(t("models.json 中已存在接入:{providerId}", { providerId: newProviderId }));
|
|
146
147
|
next.providers[newProviderId] = source;
|
|
147
148
|
delete next.providers[oldProviderId];
|
|
148
149
|
return next;
|
|
@@ -156,7 +157,7 @@ export function setModelInDoc(
|
|
|
156
157
|
): ModelsJsonDocument {
|
|
157
158
|
const next = cloneJson(doc);
|
|
158
159
|
const provider = next.providers[providerId];
|
|
159
|
-
if (!provider) throw new Error(
|
|
160
|
+
if (!provider) throw new Error(t("models.json 中不存在接入:{providerId}", { providerId }));
|
|
160
161
|
let replaced = false;
|
|
161
162
|
const models = (provider.models ?? []).flatMap((current) => {
|
|
162
163
|
if (current.id !== model.id && current.id !== replacedModelId) return [current];
|
|
@@ -178,12 +179,12 @@ export function renameModelInDoc(
|
|
|
178
179
|
const next = cloneJson(doc);
|
|
179
180
|
if (oldModelId === newModelId) return next;
|
|
180
181
|
const provider = next.providers[providerId];
|
|
181
|
-
if (!provider) throw new Error(
|
|
182
|
+
if (!provider) throw new Error(t("models.json 中不存在接入:{providerId}", { providerId }));
|
|
182
183
|
const models = provider.models ?? [];
|
|
183
184
|
const source = models.find((model) => model.id === oldModelId);
|
|
184
|
-
if (!source) throw new Error(
|
|
185
|
+
if (!source) throw new Error(t("models.json 中不存在待重命名模型:{fullId}", { fullId: `${providerId}/${oldModelId}` }));
|
|
185
186
|
if (models.some((model) => model.id === newModelId)) {
|
|
186
|
-
throw new Error(
|
|
187
|
+
throw new Error(t("models.json 中已存在模型:{fullId}", { fullId: `${providerId}/${newModelId}` }));
|
|
187
188
|
}
|
|
188
189
|
source.id = newModelId;
|
|
189
190
|
return next;
|
package/models-json-sync.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { atomicWriteText } from "./atomic-write.ts";
|
|
|
18
18
|
import { cloneJson, formatUnknownError, isObjectRecord, stringifyJson, stripJsonNoise } from "./common.ts";
|
|
19
19
|
import { withConfigurationLock } from "./configuration-lock.ts";
|
|
20
20
|
import { readStableTextFileSnapshot } from "./file-snapshot.ts";
|
|
21
|
+
import { t } from "./i18n.ts";
|
|
21
22
|
import {
|
|
22
23
|
deleteModelInDoc,
|
|
23
24
|
deleteProviderInDoc,
|
|
@@ -169,9 +170,9 @@ export function buildModelsDocumentWithSynchronizedModel(
|
|
|
169
170
|
replacedModelId?: string,
|
|
170
171
|
): ModelsJsonDocument {
|
|
171
172
|
const provider = document.providers[providerId];
|
|
172
|
-
if (!provider) throw new Error(
|
|
173
|
+
if (!provider) throw new Error(t("接入不存在:{providerId}", { providerId }));
|
|
173
174
|
const model = provider.models.find((candidate) => candidate.id === modelId);
|
|
174
|
-
if (!model) throw new Error(
|
|
175
|
+
if (!model) throw new Error(t("模型不存在:{fullId}", { fullId: `${providerId}/${modelId}` }));
|
|
175
176
|
const sourceModel = sourceDocument.providers[providerId]?.models?.find(
|
|
176
177
|
(candidate) => candidate.id === modelId || candidate.id === replacedModelId,
|
|
177
178
|
);
|
|
@@ -281,7 +282,7 @@ async function updateLockedSettings(
|
|
|
281
282
|
try {
|
|
282
283
|
const snapshot = await readStableTextFileSnapshot(path);
|
|
283
284
|
const settings = snapshot.source === undefined ? {} : JSON.parse(stripJsonNoise(snapshot.source));
|
|
284
|
-
if (!isObjectRecord(settings)) throw new Error(
|
|
285
|
+
if (!isObjectRecord(settings)) throw new Error(t("{path} 根节点必须是对象", { path }));
|
|
285
286
|
const current = settings.enabledModels;
|
|
286
287
|
if (!Array.isArray(current)) return { configured: false, outcome: { mode: "all-enabled" } };
|
|
287
288
|
const patterns = current.filter((item): item is string => typeof item === "string");
|
|
@@ -292,7 +293,7 @@ async function updateLockedSettings(
|
|
|
292
293
|
}
|
|
293
294
|
const currentSnapshot = await readStableTextFileSnapshot(path);
|
|
294
295
|
if (currentSnapshot.contentHash !== snapshot.contentHash) {
|
|
295
|
-
throw new Error(
|
|
296
|
+
throw new Error(t("{path} 已被未遵守 Pi 文件锁的编辑器修改;已取消 enabledModels 同步,请重试。", { path }));
|
|
296
297
|
}
|
|
297
298
|
settings.enabledModels = next;
|
|
298
299
|
await atomicWriteText(path, stringifyJson(settings));
|
|
@@ -374,15 +375,15 @@ async function verifyRegistryModel(
|
|
|
374
375
|
const warnings: string[] = [];
|
|
375
376
|
const model = registry.find(providerId, modelId);
|
|
376
377
|
if (!model) {
|
|
377
|
-
warnings.push(
|
|
378
|
+
warnings.push(t("{label} 未找到模型 {fullId}", { label, fullId: `${providerId}/${modelId}` }));
|
|
378
379
|
return warnings;
|
|
379
380
|
}
|
|
380
381
|
if (!registry.hasConfiguredAuth(model)) {
|
|
381
|
-
warnings.push(
|
|
382
|
+
warnings.push(t("{label} 找到模型 {fullId},但 API key 未配置或不可解析", { label, fullId: `${providerId}/${modelId}` }));
|
|
382
383
|
return warnings;
|
|
383
384
|
}
|
|
384
385
|
const auth = await registry.getApiKeyAndHeaders(model);
|
|
385
|
-
if (!auth.ok) warnings.push(
|
|
386
|
+
if (!auth.ok) warnings.push(t("{label} 请求认证解析失败:{error}", { label, error: auth.error }));
|
|
386
387
|
return warnings;
|
|
387
388
|
}
|
|
388
389
|
|
|
@@ -393,7 +394,7 @@ export async function verifyNativeModelAvailable(
|
|
|
393
394
|
modelId: string,
|
|
394
395
|
): Promise<NativeModelVerification> {
|
|
395
396
|
const warnings: string[] = [];
|
|
396
|
-
warnings.push(...await verifyRegistryModel("当前会话 registry", ctx.modelRegistry, providerId, modelId));
|
|
397
|
+
warnings.push(...await verifyRegistryModel(t("当前会话 registry"), ctx.modelRegistry, providerId, modelId));
|
|
397
398
|
try {
|
|
398
399
|
const agentDir = getAgentDir();
|
|
399
400
|
const runtime = await ModelRuntime.create({
|
|
@@ -402,9 +403,9 @@ export async function verifyNativeModelAvailable(
|
|
|
402
403
|
allowModelNetwork: false,
|
|
403
404
|
});
|
|
404
405
|
const registry = new ModelRegistry(runtime);
|
|
405
|
-
warnings.push(...await verifyRegistryModel("原生 models.json registry", registry, providerId, modelId));
|
|
406
|
+
warnings.push(...await verifyRegistryModel(t("原生 models.json registry"), registry, providerId, modelId));
|
|
406
407
|
} catch (error) {
|
|
407
|
-
warnings.push(
|
|
408
|
+
warnings.push(t("原生 models.json registry 校验失败:{error}", { error: formatUnknownError(error) }));
|
|
408
409
|
}
|
|
409
410
|
return { ok: warnings.length === 0, warnings };
|
|
410
411
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-model-manager",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A TUI model and provider manager for Pi with native models.json persistence, request-header profiles, proxy routing, and protocol compatibility.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.ts",
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"NOTICE",
|
|
12
12
|
"README.en.md",
|
|
13
13
|
"index.ts",
|
|
14
|
+
"i18n.ts",
|
|
15
|
+
"ui-language-settings.ts",
|
|
14
16
|
"state-store.ts",
|
|
15
17
|
"state-metadata-store.ts",
|
|
16
18
|
"state-cache.ts",
|
|
@@ -64,11 +66,10 @@
|
|
|
64
66
|
"pi": {
|
|
65
67
|
"extensions": [
|
|
66
68
|
"./index.ts"
|
|
67
|
-
]
|
|
68
|
-
"image": "https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/pi-model-manager-preview.png"
|
|
69
|
+
]
|
|
69
70
|
},
|
|
70
71
|
"peerDependencies": {
|
|
71
|
-
"@earendil-works/pi-coding-agent": ">=0.
|
|
72
|
+
"@earendil-works/pi-coding-agent": ">=0.84.2",
|
|
72
73
|
"@earendil-works/pi-tui": ">=0.75.0"
|
|
73
74
|
},
|
|
74
75
|
"dependencies": {
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
// 由私有 request-capture-service 根据真实客户端请求生成;公开插件仅消费这些脱敏后的内置值。
|
|
2
2
|
|
|
3
3
|
export const CLAUDE_CODE_CLIENT_HEADERS: Record<string, string> = {
|
|
4
|
-
"
|
|
4
|
+
"anthropic-beta": "claude-code-20250219,context-1m-2025-08-07,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,effort-2025-11-24",
|
|
5
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
6
|
+
"anthropic-version": "2023-06-01",
|
|
7
|
+
"user-agent": "claude-cli/2.1.232 (external, sdk-cli)",
|
|
8
|
+
"x-app": "cli",
|
|
5
9
|
"x-stainless-arch": "x64",
|
|
6
10
|
"x-stainless-lang": "js",
|
|
7
11
|
"x-stainless-os": "Windows",
|
|
8
|
-
"x-stainless-package-version": "0.
|
|
12
|
+
"x-stainless-package-version": "0.112.1",
|
|
9
13
|
"x-stainless-retry-count": "0",
|
|
10
14
|
"x-stainless-runtime": "node",
|
|
11
15
|
"x-stainless-runtime-version": "v26.3.0",
|
|
12
16
|
"x-stainless-timeout": "300",
|
|
13
|
-
"anthropic-beta": "claude-code-20250219,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,effort-2025-11-24",
|
|
14
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
15
|
-
"anthropic-version": "2023-06-01",
|
|
16
|
-
"x-app": "cli",
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
export const CODEX_CLI_CLIENT_HEADERS: Record<string, string> = {
|
|
20
|
-
"user-agent": "codex-tui/0.145.0 (Windows 10.0.26200; x86_64) WindowsTerminal (codex-tui; 0.145.0)",
|
|
21
20
|
"originator": "codex-tui",
|
|
21
|
+
"user-agent": "codex-tui/0.147.0 (Windows 10.0.26200; x86_64) unknown (codex-tui; 0.147.0)",
|
|
22
22
|
"x-codex-beta-features": "remote_compaction_v2",
|
|
23
23
|
};
|
|
@@ -4,19 +4,19 @@
|
|
|
4
4
|
// [喵喵喵]: 内置值由私有抓包工具生成,公开插件不携带捕获能力 (2026-07-26)
|
|
5
5
|
|
|
6
6
|
import { cloneStringRecord, hasStringRecordEntries } from "../common.ts";
|
|
7
|
+
import { t } from "../i18n.ts";
|
|
7
8
|
import type { ApiKind, BuiltInClientHeaderProfileId, ClientHeaderProfileId, CompatSettings, StoredClientHeaderCapture } from "../types.ts";
|
|
8
9
|
import { CLAUDE_CODE_CLIENT_HEADERS, CODEX_CLI_CLIENT_HEADERS } from "./builtin-client-headers.ts";
|
|
9
10
|
|
|
10
11
|
const ANTHROPIC_INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
|
|
11
12
|
|
|
12
13
|
|
|
13
|
-
export
|
|
14
|
-
recommended
|
|
15
|
-
disabled
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
};
|
|
14
|
+
export function getClientHeaderProfileLabel(profile: ClientHeaderProfileId): string {
|
|
15
|
+
if (profile === "recommended") return t("自动推荐");
|
|
16
|
+
if (profile === "disabled") return t("不添加");
|
|
17
|
+
if (profile === "custom") return t("自定义请求头");
|
|
18
|
+
return profile === "claude-code" ? "ClaudeCode" : "Codex";
|
|
19
|
+
}
|
|
20
20
|
|
|
21
21
|
function removeAnthropicBetaFeature(headers: Record<string, string>, feature: string): void {
|
|
22
22
|
const betaHeaderKey = Object.keys(headers).find((name) => name.toLowerCase() === "anthropic-beta");
|
|
@@ -107,7 +107,7 @@ export function getClientHeadersForProfile(
|
|
|
107
107
|
export function getClientHeaderProfileDisplay(profile: ClientHeaderProfileId, api: ApiKind): string {
|
|
108
108
|
const resolved = resolveClientHeaderProfile(profile, api);
|
|
109
109
|
if (profile === "recommended") {
|
|
110
|
-
return `${
|
|
110
|
+
return `${getClientHeaderProfileLabel("recommended")} → ${getClientHeaderProfileLabel(resolved)}`;
|
|
111
111
|
}
|
|
112
|
-
return
|
|
112
|
+
return getClientHeaderProfileLabel(profile);
|
|
113
113
|
}
|
package/presets/providers.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// 4 种 API 协议的默认预设。新建接入/模型时用,便于一键填好常用字段。
|
|
2
2
|
|
|
3
3
|
import type { ApiKind, ModelInputKind, ProviderDraft } from "../types.ts";
|
|
4
|
+
import { resolveRuntimeBaseUrl } from "../runtime-base-url.ts";
|
|
4
5
|
|
|
5
6
|
export interface ProviderPreset {
|
|
6
7
|
api: ApiKind;
|
|
@@ -19,7 +20,7 @@ export interface ProviderPreset {
|
|
|
19
20
|
const PROVIDER_PRESETS: ProviderPreset[] = [
|
|
20
21
|
{
|
|
21
22
|
api: "openai-responses",
|
|
22
|
-
label: "OpenAI Responses —
|
|
23
|
+
label: "OpenAI Responses — instructions / input",
|
|
23
24
|
shortLabel: "OpenAI Responses",
|
|
24
25
|
defaultProviderName: "openai-responses",
|
|
25
26
|
baseUrl: "https://api.openai.com/v1",
|
|
@@ -32,7 +33,7 @@ const PROVIDER_PRESETS: ProviderPreset[] = [
|
|
|
32
33
|
},
|
|
33
34
|
{
|
|
34
35
|
api: "openai-completions",
|
|
35
|
-
label: "OpenAI Chat Completions —
|
|
36
|
+
label: "OpenAI Chat Completions — OpenAI-compatible",
|
|
36
37
|
shortLabel: "OpenAI Chat",
|
|
37
38
|
defaultProviderName: "openai-chat",
|
|
38
39
|
baseUrl: "https://api.openai.com/v1",
|
|
@@ -98,6 +99,8 @@ export function switchProviderDraftApiPreset(draft: ProviderDraft, nextApi: ApiK
|
|
|
98
99
|
const replaceBaseUrl = stillUsesPresetUrl(draft.baseUrl, previousPreset.baseUrl);
|
|
99
100
|
const replaceApiKey = draft.apiKey === previousPreset.apiKey;
|
|
100
101
|
draft.api = nextApi;
|
|
101
|
-
|
|
102
|
+
// [喵喵喵]: 版本路径规则随协议而变(OpenAI 要 /v1、Anthropic 不要),
|
|
103
|
+
// 保留的自定义地址必须按新协议重新归一化,否则切完协议就指向错误端点。
|
|
104
|
+
draft.baseUrl = replaceBaseUrl ? nextPreset.baseUrl : resolveRuntimeBaseUrl(nextApi, draft.baseUrl);
|
|
102
105
|
if (replaceApiKey) draft.apiKey = nextPreset.apiKey;
|
|
103
106
|
}
|
package/provider-registrar.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
11
11
|
import { isBuiltinProviderId } from "./builtin-model-catalog.ts";
|
|
12
12
|
import { mergeCompatSettings } from "./compat-settings.ts";
|
|
13
13
|
import { formatUnknownError } from "./common.ts";
|
|
14
|
+
import { t } from "./i18n.ts";
|
|
14
15
|
import {
|
|
15
16
|
getLocalProxyBaseUrl,
|
|
16
17
|
getProviderHttpProxyUrl,
|
|
@@ -197,10 +198,10 @@ function replaceManagedProviderConfig(
|
|
|
197
198
|
rollbackError = restoreError;
|
|
198
199
|
}
|
|
199
200
|
const rollbackNote = rollbackError
|
|
200
|
-
?
|
|
201
|
+
? t(";恢复上一版 runtime 也失败:{error}", { error: formatUnknownError(rollbackError) })
|
|
201
202
|
: previousConfig
|
|
202
|
-
? ";已恢复上一版 runtime"
|
|
203
|
-
: ";新配置未注册到当前 runtime";
|
|
203
|
+
? t(";已恢复上一版 runtime")
|
|
204
|
+
: t(";新配置未注册到当前 runtime");
|
|
204
205
|
throw new Error(`${formatUnknownError(error)}${rollbackNote}`);
|
|
205
206
|
}
|
|
206
207
|
}
|
package/request-pipeline.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { injectClaudeCodeMetadata } from "./claude-code-compat.ts";
|
|
8
8
|
import { formatUnknownError } from "./common.ts";
|
|
9
|
+
import { t, type MessageKey } from "./i18n.ts";
|
|
9
10
|
import { normalizeManagedOpenAIResponsesPayload } from "./openai-responses-payload.ts";
|
|
10
11
|
import { injectOpenAIServiceTier } from "./openai-service-tier.ts";
|
|
11
12
|
import { readCachedState } from "./state-cache.ts";
|
|
@@ -15,7 +16,7 @@ type StateLoader = () => Promise<StateDocument>;
|
|
|
15
16
|
|
|
16
17
|
type RequestTransform = {
|
|
17
18
|
id: string;
|
|
18
|
-
warning:
|
|
19
|
+
warning: MessageKey;
|
|
19
20
|
run(payload: unknown, ctx: ExtensionContext, loadState: StateLoader): Promise<unknown | undefined> | unknown | undefined;
|
|
20
21
|
};
|
|
21
22
|
|
|
@@ -77,7 +78,7 @@ export function createRequestPipeline(): RequestPipeline {
|
|
|
77
78
|
} catch (error) {
|
|
78
79
|
if (!notifiedTransformErrors.has(transform.id) && ctx.hasUI) {
|
|
79
80
|
notifiedTransformErrors.add(transform.id);
|
|
80
|
-
ctx.ui.notify(`[pi-model-manager] ${transform.warning}
|
|
81
|
+
ctx.ui.notify(`[pi-model-manager] ${t(transform.warning)}: ${formatUnknownError(error)}`, "warning");
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
84
|
}
|
package/rescue.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
// 调用方负责先同步 registry/runtime,避免救援阶段再次触发全目录刷新。
|
|
14
14
|
|
|
15
15
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import { t } from "./i18n.ts";
|
|
16
17
|
|
|
17
18
|
interface AffectedRef {
|
|
18
19
|
providerId: string;
|
|
@@ -45,7 +46,7 @@ export async function withModelRescue(
|
|
|
45
46
|
if (replacement && ctx.modelRegistry.hasConfiguredAuth(replacement)) {
|
|
46
47
|
if (await pi.setModel(replacement)) {
|
|
47
48
|
ctx.ui.notify(
|
|
48
|
-
|
|
49
|
+
t("{reason},已切换到 {fullId}", { reason: options.reason, fullId: `${options.preferred.providerId}/${options.preferred.modelId}` }),
|
|
49
50
|
"info",
|
|
50
51
|
);
|
|
51
52
|
return;
|
|
@@ -56,14 +57,14 @@ export async function withModelRescue(
|
|
|
56
57
|
const fallback = ctx.modelRegistry.getAvailable()[0];
|
|
57
58
|
if (fallback && await pi.setModel(fallback)) {
|
|
58
59
|
ctx.ui.notify(
|
|
59
|
-
|
|
60
|
+
t("{reason},已自动切换到 {fullId}", { reason: options.reason, fullId: `${fallback.provider}/${fallback.id}` }),
|
|
60
61
|
"info",
|
|
61
62
|
);
|
|
62
63
|
return;
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
ctx.ui.notify(
|
|
66
|
-
|
|
67
|
+
t("{reason},但当前没有其它可用模型。请使用 /model-manager 添加模型或 /login 配置认证。", { reason: options.reason }),
|
|
67
68
|
"warning",
|
|
68
69
|
);
|
|
69
70
|
}
|
package/runtime-base-url.ts
CHANGED
|
@@ -71,9 +71,15 @@ function appendGoogleGenerativeApiVersionForRootUrl(baseUrl: string): string {
|
|
|
71
71
|
return parsed.toString();
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
// [喵喵喵]: URL.toString() 对根路径会补出尾斜杠,归一化结果现在会落盘到 models.json,
|
|
75
|
+
// 多一个斜杠会让本来正确的配置被判定为需要迁移,白白改写用户配置。
|
|
76
|
+
function stripRootTrailingSlash(url: string): string {
|
|
77
|
+
return url.replace(/^(https?:\/\/[^/?#]+)\/(?=$|[?#])/i, "$1");
|
|
78
|
+
}
|
|
79
|
+
|
|
74
80
|
export function resolveRuntimeBaseUrl(api: ApiKind, baseUrl: string): string {
|
|
75
|
-
if (api === "anthropic-messages") return stripTrailingV1(baseUrl);
|
|
76
|
-
if (api === "openai-completions" || api === "openai-responses") return appendV1ForRootUrl(baseUrl);
|
|
77
|
-
if (api === "google-generative-ai") return appendGoogleGenerativeApiVersionForRootUrl(baseUrl);
|
|
81
|
+
if (api === "anthropic-messages") return stripRootTrailingSlash(stripTrailingV1(baseUrl));
|
|
82
|
+
if (api === "openai-completions" || api === "openai-responses") return stripRootTrailingSlash(appendV1ForRootUrl(baseUrl));
|
|
83
|
+
if (api === "google-generative-ai") return stripRootTrailingSlash(appendGoogleGenerativeApiVersionForRootUrl(baseUrl));
|
|
78
84
|
return trimTrailingSlashes(baseUrl);
|
|
79
85
|
}
|