pi-model-manager 0.4.8 → 0.4.10
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 +47 -129
- package/README.md +47 -129
- package/common.ts +6 -3
- package/configuration-persistence.ts +39 -52
- package/header-profile-mutations.ts +13 -38
- package/i18n.ts +11 -130
- package/index.ts +13 -11
- package/model-mutations.ts +75 -125
- package/model-request-headers.ts +54 -0
- package/models-json-sync.ts +13 -13
- package/package.json +2 -1
- package/presets/builtin-client-headers.ts +9 -4
- package/presets/client-headers.ts +9 -18
- package/presets/providers.ts +0 -10
- package/presets/thinking.ts +5 -52
- package/provider-registrar.ts +11 -59
- package/provider-transport.ts +42 -6
- package/request-pipeline.ts +10 -0
- package/rescue.ts +10 -16
- package/state-document.ts +26 -31
- package/state-metadata-store.ts +23 -58
- package/state-store.ts +8 -16
- package/tui/model-list-fetch.ts +2 -6
- package/tui/model-search.ts +45 -0
- package/tui/model-values.ts +24 -0
- package/tui/ui-helpers.ts +68 -157
- package/tui/workbench-fields.ts +127 -0
- package/tui/workbench-headers.ts +80 -0
- package/tui/workbench-types.ts +67 -0
- package/tui/workbench-window.ts +439 -0
- package/tui/workbench.ts +385 -0
- package/types.ts +9 -14
- package/tui/dashboard.ts +0 -463
- package/tui/editor-model.ts +0 -391
- package/tui/editor-provider.ts +0 -368
- package/tui/header-profiles-panel.ts +0 -407
- package/tui/model-picker.ts +0 -273
- package/tui/persistent-menu.ts +0 -590
package/tui/workbench.ts
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { getBuiltinProviderIds } from "../builtin-model-catalog.ts";
|
|
3
|
+
import { formatUnknownError } from "../common.ts";
|
|
4
|
+
import { getUiLanguage, setUiLanguage, t } from "../i18n.ts";
|
|
5
|
+
import { writeUiLanguage } from "../ui-language-settings.ts";
|
|
6
|
+
import { deleteModelConfiguration, deleteProviderConfiguration, saveModelConfiguration, saveProviderConfiguration } from "../model-mutations.ts";
|
|
7
|
+
import { deleteRequestHeaderProfileConfiguration, saveRequestHeaderProfileConfiguration } from "../header-profile-mutations.ts";
|
|
8
|
+
import { ANTIGRAVITY_CLI_CLIENT_HEADERS, CODEX_CLI_CLIENT_HEADERS, CLAUDE_CODE_CLIENT_HEADERS } from "../presets/builtin-client-headers.ts";
|
|
9
|
+
import { getClientHeaderProfileLabel, resolveClientHeaderProfile, resolveProviderCustomHeaders } from "../presets/client-headers.ts";
|
|
10
|
+
import { findRequestHeaderProfileProviders, createModelDraftForStoredProvider, createModelDraftFromStoredModel, createProviderDraft, createProviderDraftFromStored, createRequestHeaderProfileDraft, createRequestHeaderProfileDraftFromStored, getProviderChangeWarnings, getProviderDisplayName, getAuthStatusText, validateProviderDraft, validateModelDraft, validateRequestHeaderProfileDraft } from "../state-document.ts";
|
|
11
|
+
import { readState } from "../state-store.ts";
|
|
12
|
+
import type { BuiltInClientHeaderProfileId, ConfigurationChangeResult, StateDocument, ProviderDraft, ModelDraft, RequestHeaderProfileDraft } from "../types.ts";
|
|
13
|
+
import { modelFields, providerFields } from "./workbench-fields.ts";
|
|
14
|
+
import { headerFields, headerJsonField, NEW_HEADER_ROW } from "./workbench-headers.ts";
|
|
15
|
+
import { ModelManagerWindow } from "./workbench-window.ts";
|
|
16
|
+
import { ui, type WorkbenchPage, type WorkbenchView } from "./workbench-types.ts";
|
|
17
|
+
import { formatApiShort, formatModelListHeader, formatModelListRow, formatModelSummaryLine, formatProviderListHeader, formatProviderListRow, formatProviderSummaryLine, formatProviderEndpointLine, getModelColumnWidths, formatHeaderProfileListHeader, formatHeaderProfileListRow } from "./ui-helpers.ts";
|
|
18
|
+
import { fetchModelIds } from "./model-list-fetch.ts";
|
|
19
|
+
import { sortModelIdsForSearch } from "./model-search.ts";
|
|
20
|
+
|
|
21
|
+
const BUILTIN_HEADERS = { "claude-code": CLAUDE_CODE_CLIENT_HEADERS, "codex-cli": CODEX_CLI_CLIENT_HEADERS, "antigravity-cli": ANTIGRAVITY_CLI_CLIENT_HEADERS };
|
|
22
|
+
|
|
23
|
+
type Edit =
|
|
24
|
+
| { kind: "provider"; draft: ProviderDraft; originalId?: string; baseline: string; keyVisible: boolean }
|
|
25
|
+
| { kind: "model"; draft: ModelDraft; originalId?: string; baseline: string }
|
|
26
|
+
| { kind: "header"; draft: RequestHeaderProfileDraft; originalId?: string; baseline: string };
|
|
27
|
+
|
|
28
|
+
class WorkbenchController {
|
|
29
|
+
window!: ModelManagerWindow;
|
|
30
|
+
private section: "providers" | "models" | "headers" = "providers";
|
|
31
|
+
private providerId = "";
|
|
32
|
+
private edit?: Edit;
|
|
33
|
+
private builtinHeader?: BuiltInClientHeaderProfileId;
|
|
34
|
+
private profileUsage = new Map<string, string[]>();
|
|
35
|
+
private pi: ExtensionAPI;
|
|
36
|
+
private ctx: ExtensionCommandContext;
|
|
37
|
+
private state: StateDocument;
|
|
38
|
+
private builtins: ReadonlySet<string>;
|
|
39
|
+
|
|
40
|
+
constructor(pi: ExtensionAPI, ctx: ExtensionCommandContext, state: StateDocument, builtins: ReadonlySet<string>) {
|
|
41
|
+
this.pi = pi; this.ctx = ctx; this.state = state; this.builtins = builtins;
|
|
42
|
+
this.countProfileUsage();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private countProfileUsage(): void {
|
|
46
|
+
this.profileUsage = new Map(Object.keys(this.state.requestHeaderProfiles).map((id) => [id, findRequestHeaderProfileProviders(this.state, id)]));
|
|
47
|
+
for (const id of Object.keys(BUILTIN_HEADERS)) {
|
|
48
|
+
this.profileUsage.set(`builtin:${id}`, Object.entries(this.state.providers).filter(([, provider]) => provider.managed
|
|
49
|
+
&& (provider.models.length ? provider.models.map((model) => model.api ?? provider.api) : [provider.api])
|
|
50
|
+
.some((api) => resolveClientHeaderProfile(provider.clientHeaderProfile, api as ProviderDraft["api"]) === id)).map(([providerId]) => providerId));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
private providerIds(): string[] { return Object.keys(this.state.providers).filter((id) => !this.builtins.has(id)).sort((a, b) => a.localeCompare(b)); }
|
|
54
|
+
private dirty(): boolean { return !!this.edit && JSON.stringify(this.edit.draft) !== this.edit.baseline; }
|
|
55
|
+
private markBaseline(): void { if (this.edit) this.edit.baseline = JSON.stringify(this.edit.draft); }
|
|
56
|
+
|
|
57
|
+
private async refreshState(): Promise<void> {
|
|
58
|
+
this.state = await readState();
|
|
59
|
+
this.countProfileUsage();
|
|
60
|
+
if (this.section === "models" && !this.state.providers[this.providerId]) this.section = "providers";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async leave(action: () => void | Promise<void>): Promise<void> {
|
|
64
|
+
if (this.dirty()) {
|
|
65
|
+
const choice = await this.window.choose(ui("有未保存的修改", "Unsaved changes"), [
|
|
66
|
+
{ id: "continue", label: ui("继续编辑", "Keep editing") },
|
|
67
|
+
{ id: "save", label: ui("保存并继续", "Save and continue") },
|
|
68
|
+
{ id: "discard", label: ui("放弃修改", "Discard changes") },
|
|
69
|
+
]);
|
|
70
|
+
if (choice === undefined || choice === "continue") return;
|
|
71
|
+
if (choice === "save" && !await this.save()) return;
|
|
72
|
+
}
|
|
73
|
+
if (this.window.disposed) return;
|
|
74
|
+
await this.refreshState();
|
|
75
|
+
this.edit = undefined;
|
|
76
|
+
await action();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private async enterProvider(id: string): Promise<void> {
|
|
80
|
+
await this.refreshState();
|
|
81
|
+
if (!this.state.providers[id]) throw new Error(t("接入配置已不存在:{providerId}", { providerId: id }));
|
|
82
|
+
this.providerId = id;
|
|
83
|
+
this.section = "models";
|
|
84
|
+
this.window.focusContent();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private openProvider(): void {
|
|
88
|
+
const provider = this.state.providers[this.providerId];
|
|
89
|
+
if (!provider) return;
|
|
90
|
+
this.edit = { kind: "provider", draft: createProviderDraftFromStored(this.providerId, provider), originalId: this.providerId, baseline: "", keyVisible: false };
|
|
91
|
+
this.markBaseline();
|
|
92
|
+
this.window.focusContent();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private createProvider(): void {
|
|
96
|
+
const draft = createProviderDraft(); draft.providerName = "";
|
|
97
|
+
this.edit = { kind: "provider", draft, baseline: "", keyVisible: false };
|
|
98
|
+
this.markBaseline();
|
|
99
|
+
this.window.focusContent("providerId");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private async openModel(id?: string): Promise<void> {
|
|
103
|
+
await this.leave(async () => {
|
|
104
|
+
const provider = this.state.providers[this.providerId];
|
|
105
|
+
if (!provider) throw new Error(t("接入配置已不存在:{providerId}", { providerId: this.providerId }));
|
|
106
|
+
const model = provider.models.find((model) => model.id === id);
|
|
107
|
+
if (id && !model) throw new Error(t("模型不存在:{fullId}", { fullId: `${this.providerId}/${id}` }));
|
|
108
|
+
const draft = model ? createModelDraftFromStoredModel(this.providerId, provider, model) : createModelDraftForStoredProvider(this.providerId, provider);
|
|
109
|
+
this.section = "models";
|
|
110
|
+
this.edit = { kind: "model", draft, originalId: model?.id, baseline: "" };
|
|
111
|
+
this.markBaseline(); this.window.focusContent("modelId");
|
|
112
|
+
if (!model) await this.discover(draft);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private openHeader(id?: string): void {
|
|
117
|
+
const profile = id ? this.state.requestHeaderProfiles[id] : undefined;
|
|
118
|
+
if (id && !profile) throw new Error(ui("请求头已不存在:", "Header profile no longer exists: ") + id);
|
|
119
|
+
this.edit = { kind: "header", draft: profile ? createRequestHeaderProfileDraftFromStored(id!, profile) : createRequestHeaderProfileDraft(Object.keys(this.state.requestHeaderProfiles)), originalId: profile ? id : undefined, baseline: "" };
|
|
120
|
+
this.markBaseline(); this.window.focusContent("profileName");
|
|
121
|
+
if (!profile) this.window.editRow(this.view().page.rows.find((row) => row.id === "profileName"));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private async discover(draft: ModelDraft): Promise<void> {
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
const outcome = await this.window.wait(ui("正在拉取模型列表,最多 10 秒…", "Fetching models, up to 10 seconds…"), () => fetchModelIds({
|
|
127
|
+
...draft,
|
|
128
|
+
customClientHeaders: draft.clientHeaderProfile === "custom" ? resolveProviderCustomHeaders(draft, this.state.requestHeaderProfiles) : {},
|
|
129
|
+
}, controller.signal), () => controller.abort());
|
|
130
|
+
if (this.window.disposed || outcome.status === "cancelled") return;
|
|
131
|
+
if (outcome.status === "failed") { this.window.notify(t("拉取失败:{error}", { error: outcome.message }), true); return; }
|
|
132
|
+
if (!outcome.modelIds.length) { this.window.notify(t("上游返回空列表")); return; }
|
|
133
|
+
const id = await this.window.choose(t("上游模型列表"), outcome.modelIds.map((id) => ({ id, label: id })), draft.modelId, { searchable: true, rank: sortModelIdsForSearch });
|
|
134
|
+
if (id !== undefined) draft.modelId = id.trim();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private copyBuiltinHeader(id: BuiltInClientHeaderProfileId): void {
|
|
138
|
+
const draft = createRequestHeaderProfileDraft(Object.keys(this.state.requestHeaderProfiles));
|
|
139
|
+
this.edit = { kind: "header", draft, baseline: JSON.stringify(draft) };
|
|
140
|
+
draft.profileName = getClientHeaderProfileLabel(id);
|
|
141
|
+
draft.headers = { ...BUILTIN_HEADERS[id] };
|
|
142
|
+
this.builtinHeader = undefined;
|
|
143
|
+
this.window.focusContent("profileName");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private builtinHeaderPage(id: BuiltInClientHeaderProfileId): WorkbenchPage {
|
|
147
|
+
const page = this.basePage();
|
|
148
|
+
page.id = `builtin-header:${id}`;
|
|
149
|
+
page.title = `${getClientHeaderProfileLabel(id)} · ${ui("内置 · 只读", "Built-in · Read-only")}`;
|
|
150
|
+
page.summary = ui("直接使用插件定义,随插件更新;按 C 复制后自定义。", "Uses plugin definitions and follows updates; press C to customize a copy.");
|
|
151
|
+
page.searchable = true;
|
|
152
|
+
page.rows = Object.entries(BUILTIN_HEADERS[id]).map(([name, value]) => ({ id: name, label: name, value, description: `${name}\n${value}` }));
|
|
153
|
+
page.actions = [{ key: "c", label: ui("复制为自定义", "Copy to custom"), run: () => this.copyBuiltinHeader(id) }];
|
|
154
|
+
return page;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private async template(draft: RequestHeaderProfileDraft): Promise<void> {
|
|
158
|
+
const choice = await this.window.choose(t("快速模板"), [
|
|
159
|
+
{ id: "codex-cli", label: "Codex", description: t("OpenAI 兼容端点使用的 Codex Rust CLI 请求头。") },
|
|
160
|
+
{ id: "claude-code", label: "ClaudeCode", description: t("Anthropic 兼容端点常用的 ClaudeCode 请求头。") },
|
|
161
|
+
{ id: "antigravity-cli", label: "Antigravity CLI", description: ui("Gemini 接入默认使用的 Antigravity CLI 身份请求头。", "Default Antigravity CLI identity headers for Gemini providers.") },
|
|
162
|
+
{ id: "empty", label: t("清空"), description: t("清空当前请求头,重新手动填写。") },
|
|
163
|
+
]);
|
|
164
|
+
if (!choice) return;
|
|
165
|
+
if (Object.keys(draft.headers).length && !await this.window.confirm(t("快速模板"), ui("将替换草稿中的全部请求头。", "Replace all headers in this draft?"))) return;
|
|
166
|
+
draft.headers = { ...(choice === "empty" ? {} : BUILTIN_HEADERS[choice as BuiltInClientHeaderProfileId]) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private validate(errors: string[], field: string): boolean {
|
|
170
|
+
if (!errors.length) return true;
|
|
171
|
+
this.window.focusContent(field);
|
|
172
|
+
this.window.notify(errors.join(" · "), true);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private applyChange(result: ConfigurationChangeResult): void {
|
|
177
|
+
this.state = result.document;
|
|
178
|
+
this.countProfileUsage();
|
|
179
|
+
if (this.section === "models" && !this.state.providers[this.providerId]) this.section = "providers";
|
|
180
|
+
this.window.notify([...result.messages, ...result.warnings].join("\n"), result.warnings.length > 0);
|
|
181
|
+
}
|
|
182
|
+
private async save(): Promise<boolean> {
|
|
183
|
+
const edit = this.edit;
|
|
184
|
+
if (!edit || this.window.disposed) return false;
|
|
185
|
+
const previousPageId = this.view().page.id;
|
|
186
|
+
try {
|
|
187
|
+
const latest = await readState();
|
|
188
|
+
if (this.window.disposed) return false;
|
|
189
|
+
if (edit.kind === "provider") {
|
|
190
|
+
if (!this.validate(validateProviderDraft(edit.draft, latest, this.builtins, edit.originalId), "providerId")) return false;
|
|
191
|
+
const warnings = edit.originalId ? getProviderChangeWarnings(latest.providers[edit.originalId], edit.draft) : [];
|
|
192
|
+
if (warnings.length && !await this.window.confirm(t("请确认改动"), warnings.join("\n"))) return false;
|
|
193
|
+
if (this.window.disposed) return false;
|
|
194
|
+
this.applyChange(await this.window.wait(ui("正在保存…", "Saving…"), () => saveProviderConfiguration(this.pi, this.ctx, edit.draft, edit.originalId)));
|
|
195
|
+
edit.originalId = edit.draft.providerId;
|
|
196
|
+
this.providerId = edit.draft.providerId;
|
|
197
|
+
this.window.selectRow("providers", this.providerId);
|
|
198
|
+
} else if (edit.kind === "model") {
|
|
199
|
+
if (!this.validate(validateModelDraft(edit.draft, latest, edit.originalId), "modelId")) return false;
|
|
200
|
+
this.applyChange(await this.window.wait(ui("正在保存…", "Saving…"), () => saveModelConfiguration(this.pi, this.ctx, edit.draft, edit.originalId)));
|
|
201
|
+
edit.originalId = edit.draft.modelId.trim();
|
|
202
|
+
edit.draft.modelId = edit.originalId;
|
|
203
|
+
this.window.selectRow(`models:${this.providerId}`, edit.originalId);
|
|
204
|
+
} else {
|
|
205
|
+
if (!this.validate(validateRequestHeaderProfileDraft(edit.draft, latest, edit.originalId), NEW_HEADER_ROW)) return false;
|
|
206
|
+
this.applyChange(await this.window.wait(ui("正在保存…", "Saving…"), () => saveRequestHeaderProfileConfiguration(this.pi, this.ctx, edit.draft, edit.originalId)));
|
|
207
|
+
edit.originalId = edit.draft.profileId.trim();
|
|
208
|
+
edit.draft.profileId = edit.originalId;
|
|
209
|
+
this.window.selectRow("headers", edit.originalId);
|
|
210
|
+
}
|
|
211
|
+
this.markBaseline();
|
|
212
|
+
this.window.preservePageState(previousPageId);
|
|
213
|
+
} catch (error) { this.window.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), true); return false; }
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private async removeProvider(id: string): Promise<void> {
|
|
218
|
+
await this.refreshState();
|
|
219
|
+
const provider = this.state.providers[id];
|
|
220
|
+
if (!provider) return;
|
|
221
|
+
if (!await this.window.confirm(t("删除接入 {providerId}", { providerId: id }), t("将删除 {name} 下全部 {count} 个模型。", { name: getProviderDisplayName(id, provider), count: provider.models.length }))) return;
|
|
222
|
+
this.applyChange(await this.window.wait(ui("正在删除…", "Deleting…"), () => deleteProviderConfiguration(this.pi, this.ctx, id)));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private async removeModel(id: string): Promise<void> {
|
|
226
|
+
const providerId = this.providerId;
|
|
227
|
+
await this.refreshState();
|
|
228
|
+
const provider = this.state.providers[providerId];
|
|
229
|
+
if (!provider?.models.some((model) => model.id === id)) throw new Error(t("模型不存在:{fullId}", { fullId: `${providerId}/${id}` }));
|
|
230
|
+
if (!await this.window.confirm(t("删除模型 {fullId}", { fullId: `${providerId}/${id}` }), ui("只删除该模型,接入及连接配置保留。", "Only this model is deleted; the provider and connection settings are kept."))) return;
|
|
231
|
+
this.applyChange(await this.window.wait(ui("正在删除…", "Deleting…"), () => deleteModelConfiguration(this.pi, this.ctx, providerId, id)));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private async removeHeader(id: string): Promise<void> {
|
|
235
|
+
await this.refreshState();
|
|
236
|
+
if (!this.state.requestHeaderProfiles[id]) throw new Error(ui("请求头已不存在:", "Header profile no longer exists: ") + id);
|
|
237
|
+
const providers = this.profileUsage.get(id) ?? [];
|
|
238
|
+
if (!await this.window.confirm(t("删除请求头 {profileId}", { profileId: id }), ui(`${providers.length} 个接入会改回“自动推荐”。`, `${providers.length} providers will revert to automatic headers.`) + (providers.length ? `\n${providers.join(" · ")}` : ""))) return;
|
|
239
|
+
this.applyChange(await this.window.wait(ui("正在删除…", "Deleting…"), () => deleteRequestHeaderProfileConfiguration(this.pi, this.ctx, id)));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private async back(): Promise<void> {
|
|
243
|
+
if (this.builtinHeader) { this.builtinHeader = undefined; this.window.focusContent(); }
|
|
244
|
+
else if (this.edit) await this.leave(() => this.window.focusContent());
|
|
245
|
+
else if (this.section !== "providers") { this.section = "providers"; this.window.focusContent(); }
|
|
246
|
+
else this.window.close();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private async language(): Promise<void> {
|
|
250
|
+
const value = await this.window.choose("语言 / Language", [{ id: "zh-CN", label: "简体中文" }, { id: "en", label: "English" }], getUiLanguage());
|
|
251
|
+
if ((value !== "zh-CN" && value !== "en") || value === getUiLanguage()) return;
|
|
252
|
+
await writeUiLanguage(value); setUiLanguage(value);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private basePage(): WorkbenchPage {
|
|
256
|
+
return { id: this.section, title: "", rows: [], actions: [], back: () => this.back() };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private editPage(edit: Edit): WorkbenchPage {
|
|
260
|
+
const page = this.basePage();
|
|
261
|
+
page.id = `edit-${edit.kind}:${edit.kind === "model" ? `${this.providerId}:` : ""}${edit.originalId ?? "+"}`;
|
|
262
|
+
page.dirty = this.dirty();
|
|
263
|
+
page.save = async () => { await this.save(); };
|
|
264
|
+
if (edit.kind === "provider") {
|
|
265
|
+
page.title = edit.originalId ? `${edit.draft.providerId} / ${ui("接入设置", "Connection")}` : t("新建接入");
|
|
266
|
+
page.rows = providerFields(edit.draft, edit.originalId === undefined, this.state.requestHeaderProfiles, async () => {
|
|
267
|
+
if (await this.window.confirm(ui("清除 API key", "Clear API key"), ui("仅清除草稿中的认证值,保存后生效。", "Clear the draft credential; takes effect after saving."))) edit.draft.apiKey = "";
|
|
268
|
+
}, { visible: edit.keyVisible, toggle: () => { edit.keyVisible = !edit.keyVisible; } });
|
|
269
|
+
page.summary = ui("接入可单独保存,不要求配置模型。", "Save the connection independently; no model is required.");
|
|
270
|
+
if (edit.originalId) page.actions = [{ key: "a", label: t("添加模型"), run: () => this.openModel() }];
|
|
271
|
+
} else if (edit.kind === "model") {
|
|
272
|
+
page.title = `${this.providerId} / ${edit.draft.modelId || t("添加模型")}`;
|
|
273
|
+
page.rows = modelFields(edit.draft, () => this.discover(edit.draft));
|
|
274
|
+
page.summary = !edit.draft.providerManaged && edit.draft.api === "openai-responses" ? t("Fast mode 需要先接管该接入;请先保存接入配置。") : `${formatApiShort(edit.draft.api)} · ${ui("保存不切换当前模型", "Saving keeps the current model")}`;
|
|
275
|
+
} else {
|
|
276
|
+
page.title = `${t("请求头管理")} / ${edit.draft.profileName || t("自定义请求头")}`;
|
|
277
|
+
page.rows = headerFields(edit.draft, (id) => this.window.focusContent(id));
|
|
278
|
+
const providers = this.profileUsage.get(edit.originalId ?? "") ?? [];
|
|
279
|
+
page.summary = ui(`使用接入:${providers.length}`, `Used by ${providers.length} providers`) + (providers.length ? ` · ${providers.join(" · ")}` : "");
|
|
280
|
+
page.actions = [
|
|
281
|
+
{ key: "a", label: ui("添加字段", "Add field"), run: () => { this.window.focusContent(NEW_HEADER_ROW); this.window.editRow(this.view().page.rows.find((row) => row.id === NEW_HEADER_ROW)); } },
|
|
282
|
+
{ key: "t", label: ui("模板", "Template"), run: () => this.template(edit.draft) },
|
|
283
|
+
{ key: "j", label: "JSON", run: () => this.window.editRow(headerJsonField(edit.draft)) },
|
|
284
|
+
];
|
|
285
|
+
}
|
|
286
|
+
return page;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private providersPage(): WorkbenchPage {
|
|
290
|
+
const page = this.basePage();
|
|
291
|
+
const ids = this.providerIds();
|
|
292
|
+
const modelCount = ids.reduce((count, id) => count + this.state.providers[id]!.models.length, 0);
|
|
293
|
+
page.title = `${t("接入")} (${ids.length}) · ${modelCount} ${t("模型")}`;
|
|
294
|
+
page.enterLabel = t("进入");
|
|
295
|
+
page.searchable = true;
|
|
296
|
+
page.empty = ui("暂无接入 · 按 N 新建", "No providers · Press N to create one");
|
|
297
|
+
page.tableHeader = formatProviderListHeader;
|
|
298
|
+
page.rows = ids.map((id) => {
|
|
299
|
+
const provider = this.state.providers[id]!;
|
|
300
|
+
return { id, label: id, searchText: `${id} ${provider.name ?? ""} ${formatApiShort(provider.api)}`,
|
|
301
|
+
format: (width, theme) => formatProviderListRow(id, provider, width, theme),
|
|
302
|
+
description: `${formatProviderEndpointLine(provider)}\n${id} · ${getProviderDisplayName(id, provider)} · ${formatProviderSummaryLine(provider, this.state.requestHeaderProfiles)}\n${t("认证")}: ${getAuthStatusText(provider.apiKey)}`, activate: () => this.enterProvider(id) };
|
|
303
|
+
});
|
|
304
|
+
page.actions = [
|
|
305
|
+
{ key: "n", label: t("新建接入"), run: () => this.createProvider() },
|
|
306
|
+
{ key: "d", label: t("删除"), requiresSelection: true, run: () => this.removeProvider(this.window.selectedRow()!) },
|
|
307
|
+
{ key: "h", label: t("请求头"), run: () => { this.section = "headers"; this.window.focusContent(); } },
|
|
308
|
+
{ key: "l", label: ui("语言", "Language"), run: () => this.language() },
|
|
309
|
+
];
|
|
310
|
+
return page;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private modelsPage(): WorkbenchPage {
|
|
314
|
+
const page = this.basePage();
|
|
315
|
+
const provider = this.state.providers[this.providerId]!;
|
|
316
|
+
const columns = getModelColumnWidths(provider.models);
|
|
317
|
+
page.id = `models:${this.providerId}`;
|
|
318
|
+
page.title = `${this.providerId} / ${ui("模型", "Models")}`;
|
|
319
|
+
page.enterLabel = t("编辑模型");
|
|
320
|
+
page.summary = formatProviderSummaryLine(provider, this.state.requestHeaderProfiles);
|
|
321
|
+
page.rows = [...provider.models].sort((a, b) => a.id.localeCompare(b.id)).map((model) => ({
|
|
322
|
+
id: model.id, label: model.id, searchText: `${model.id} ${model.name ?? ""}`,
|
|
323
|
+
description: `${model.id}${model.name ? ` · ${model.name}` : ""}\n${formatModelSummaryLine(provider, model)}`,
|
|
324
|
+
format: (width, theme) => formatModelListRow(provider, model, width, { ...columns, theme }), activate: () => this.openModel(model.id),
|
|
325
|
+
}));
|
|
326
|
+
page.tableHeader = (width) => formatModelListHeader(provider, width, columns);
|
|
327
|
+
page.searchable = true;
|
|
328
|
+
page.empty = ui("接入已保留,暂无模型 · 按 A 添加", "Connection kept, no models yet · Press A to add");
|
|
329
|
+
page.actions = [
|
|
330
|
+
{ key: "a", label: t("添加模型"), run: () => this.openModel() },
|
|
331
|
+
{ key: "e", label: t("编辑接入"), run: () => this.openProvider() },
|
|
332
|
+
{ key: "d", label: t("删除"), requiresSelection: true, run: () => this.removeModel(this.window.selectedRow()!) },
|
|
333
|
+
];
|
|
334
|
+
return page;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private headersPage(): WorkbenchPage {
|
|
338
|
+
const page = this.basePage();
|
|
339
|
+
const profiles = Object.entries(this.state.requestHeaderProfiles).sort(([, a], [, b]) => a.name.localeCompare(b.name));
|
|
340
|
+
page.title = `${t("请求头管理")} · ${Object.keys(BUILTIN_HEADERS).length} ${ui("内置", "built-in")} · ${profiles.length} ${ui("自定义", "custom")}`;
|
|
341
|
+
page.enterLabel = ui("查看 / 编辑", "Open");
|
|
342
|
+
page.searchable = true;
|
|
343
|
+
page.empty = ui("暂无自定义请求头 · 按 N 新建", "No custom profiles · Press N to create one");
|
|
344
|
+
page.tableHeader = formatHeaderProfileListHeader;
|
|
345
|
+
page.rows = (Object.keys(BUILTIN_HEADERS) as BuiltInClientHeaderProfileId[]).map((id) => {
|
|
346
|
+
const profile = { name: `${getClientHeaderProfileLabel(id)} · ${ui("内置", "Built-in")}`, headers: BUILTIN_HEADERS[id] };
|
|
347
|
+
const providers = this.profileUsage.get(`builtin:${id}`) ?? [];
|
|
348
|
+
return {
|
|
349
|
+
id: `builtin:${id}`, label: profile.name, searchText: `${id} ${profile.name} ${Object.keys(profile.headers).join(" ")}`,
|
|
350
|
+
format: (width: number) => formatHeaderProfileListRow(id, profile, providers.length, width),
|
|
351
|
+
description: `${ui("随插件更新 · 只读", "Follows plugin updates · Read-only")}\n${profile.headers["user-agent"]}\n${ui("使用接入", "Used by")}: ${providers.join(" · ") || ui("无", "None")}`,
|
|
352
|
+
activate: () => { this.builtinHeader = id; this.window.focusContent(); },
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
page.rows.push(...profiles.map(([id, profile]) => {
|
|
356
|
+
const providers = this.profileUsage.get(id) ?? [];
|
|
357
|
+
return {
|
|
358
|
+
id, label: profile.name, searchText: `${id} ${profile.name} ${Object.keys(profile.headers).join(" ")}`,
|
|
359
|
+
format: (width) => formatHeaderProfileListRow(id, profile, providers.length, width),
|
|
360
|
+
description: `${id} · ${profile.name}\n${ui("使用接入", "Used by")}: ${providers.join(" · ") || ui("无", "None")}\n${Object.keys(profile.headers).join(", ")}`,
|
|
361
|
+
activate: () => this.openHeader(id),
|
|
362
|
+
actions: [{ key: "d", label: t("删除"), run: () => this.removeHeader(id) }],
|
|
363
|
+
};
|
|
364
|
+
}));
|
|
365
|
+
page.actions = [{ key: "n", label: t("新建自定义"), run: () => this.openHeader() }];
|
|
366
|
+
return page;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
view(): WorkbenchView {
|
|
370
|
+
return { page: this.builtinHeader ? this.builtinHeaderPage(this.builtinHeader) : this.edit ? this.editPage(this.edit) : this.section === "models" ? this.modelsPage() : this.section === "headers" ? this.headersPage() : this.providersPage() };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export async function runWorkbench(pi: ExtensionAPI, ctx: ExtensionCommandContext): Promise<void> {
|
|
375
|
+
const [state, builtins] = await Promise.all([readState(), getBuiltinProviderIds()]);
|
|
376
|
+
await ctx.ui.custom<void>((tui, theme, _keybindings, done) => {
|
|
377
|
+
const controller = new WorkbenchController(pi, ctx, state, builtins);
|
|
378
|
+
const window = new ModelManagerWindow(tui, theme, () => controller.view(), () => done());
|
|
379
|
+
controller.window = window;
|
|
380
|
+
return window;
|
|
381
|
+
}, {
|
|
382
|
+
overlay: true,
|
|
383
|
+
overlayOptions: { anchor: "center", width: 128, maxHeight: "100%", margin: { top: 1, bottom: 1, left: 2, right: 2 } },
|
|
384
|
+
});
|
|
385
|
+
}
|
package/types.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type ReasoningMode = "enabled" | "disabled";
|
|
|
14
14
|
export type AnthropicThinkingProtocol = "adaptive" | "legacy";
|
|
15
15
|
export type OpenAIChatCompatibilityMode = "standard" | "compatible";
|
|
16
16
|
export type OpenAIResponsesStreamCompletionMode = "standard" | "terminal-event";
|
|
17
|
-
export type BuiltInClientHeaderProfileId = "claude-code" | "codex-cli";
|
|
17
|
+
export type BuiltInClientHeaderProfileId = "claude-code" | "codex-cli" | "antigravity-cli";
|
|
18
18
|
export type ClientHeaderProfileId = "recommended" | "disabled" | BuiltInClientHeaderProfileId | "custom";
|
|
19
19
|
export type CompatSettings = Record<string, unknown>;
|
|
20
20
|
export type OpenAIServiceTier = "priority" | "ultrafast";
|
|
@@ -44,17 +44,12 @@ export interface StoredRequestHeaderProfile {
|
|
|
44
44
|
headers: Record<string, string>;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export interface StoredClientHeaderCapture {
|
|
48
|
-
capturedAt: string;
|
|
49
|
-
headers: Record<string, string>;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
47
|
export interface StoredModel {
|
|
53
48
|
id: string;
|
|
54
49
|
/** 仅保存相对 provider 默认值的显式模型级覆盖。 */
|
|
55
50
|
api?: string;
|
|
56
51
|
baseUrl?: string;
|
|
57
|
-
/**
|
|
52
|
+
/** 用户显式模型请求头,不含插件生成的内置身份头。 */
|
|
58
53
|
headers?: Record<string, string>;
|
|
59
54
|
name?: string;
|
|
60
55
|
reasoning: boolean;
|
|
@@ -102,7 +97,6 @@ export interface StateDocument {
|
|
|
102
97
|
/** state.json 中明确由插件创建或接管的 Provider ID。 */
|
|
103
98
|
managedProviderIds: string[];
|
|
104
99
|
requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>;
|
|
105
|
-
clientHeaderCaptures: Partial<Record<BuiltInClientHeaderProfileId, StoredClientHeaderCapture>>;
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
// ========== TUI draft(可变中间态) ==========
|
|
@@ -124,17 +118,14 @@ export interface ProviderDraft {
|
|
|
124
118
|
customClientHeaders: Record<string, string>;
|
|
125
119
|
httpProxyEnabled: boolean;
|
|
126
120
|
httpProxyUrl: string;
|
|
127
|
-
selectedIndex: number;
|
|
128
121
|
}
|
|
129
122
|
|
|
130
123
|
export interface ModelDraft {
|
|
131
124
|
providerId: string;
|
|
132
|
-
providerName: string;
|
|
133
125
|
providerManaged: boolean;
|
|
134
|
-
/**
|
|
126
|
+
/** 模型发现沿用的原生头,不作为表单可编辑字段。 */
|
|
135
127
|
providerHeaders?: Record<string, string>;
|
|
136
128
|
modelHeaders?: Record<string, string>;
|
|
137
|
-
compat?: CompatSettings;
|
|
138
129
|
api: ApiKind;
|
|
139
130
|
baseUrl: string;
|
|
140
131
|
apiKey: string;
|
|
@@ -152,18 +143,22 @@ export interface ModelDraft {
|
|
|
152
143
|
contextWindow: number;
|
|
153
144
|
maxTokens: number;
|
|
154
145
|
openAIServiceTier?: OpenAIServiceTier;
|
|
155
|
-
selectedIndex: number;
|
|
156
146
|
}
|
|
157
147
|
|
|
158
148
|
export interface RequestHeaderProfileDraft {
|
|
159
149
|
profileId: string;
|
|
160
150
|
profileName: string;
|
|
161
151
|
headers: Record<string, string>;
|
|
162
|
-
selectedIndex: number;
|
|
163
152
|
}
|
|
164
153
|
|
|
165
154
|
// ========== 异步流程结果 ==========
|
|
166
155
|
|
|
156
|
+
export interface ConfigurationChangeResult {
|
|
157
|
+
document: StateDocument;
|
|
158
|
+
messages: string[];
|
|
159
|
+
warnings: string[];
|
|
160
|
+
}
|
|
161
|
+
|
|
167
162
|
export type ModelListFetchOutcome =
|
|
168
163
|
| { status: "loaded"; modelIds: string[] }
|
|
169
164
|
| { status: "failed"; message: string }
|