pi-model-manager 0.2.0 → 0.3.0
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 +113 -20
- package/README.md +113 -20
- package/configuration-persistence.ts +11 -7
- package/index.ts +20 -1
- package/package.json +2 -3
- package/presets/builtin-client-headers.ts +7 -7
- package/presets/providers.ts +4 -1
- package/runtime-base-url.ts +9 -3
- package/state-document.ts +23 -0
- package/tui/dashboard.ts +47 -21
- package/tui/editor-model.ts +56 -95
- package/tui/editor-provider.ts +59 -59
- package/tui/header-profiles-panel.ts +40 -23
- package/tui/model-picker.ts +5 -3
- package/tui/persistent-menu.ts +229 -86
- package/tui/ui-helpers.ts +140 -84
package/tui/dashboard.ts
CHANGED
|
@@ -44,38 +44,44 @@ import {
|
|
|
44
44
|
formatProviderDetailLines,
|
|
45
45
|
formatProviderEndpointLine,
|
|
46
46
|
formatProviderSummaryLine,
|
|
47
|
+
getModelColumnWidths,
|
|
48
|
+
getProviderDisplayLabel,
|
|
49
|
+
getProviderNameColumnWidth,
|
|
47
50
|
} from "./ui-helpers.ts";
|
|
48
51
|
import { runHeaderProfilesPanel } from "./header-profiles-panel.ts";
|
|
49
52
|
|
|
50
53
|
interface DashboardRow {
|
|
51
54
|
providerId: string;
|
|
52
55
|
label: string;
|
|
56
|
+
searchText: string;
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
interface ProviderMenuRow {
|
|
56
60
|
modelId: string;
|
|
57
61
|
model: StoredModel;
|
|
58
62
|
label: string;
|
|
63
|
+
searchText: string;
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
function buildRows(document: StateDocument, builtInProviderIds: ReadonlySet<string>): DashboardRow[] {
|
|
62
67
|
return Object.keys(document.providers)
|
|
63
68
|
.filter((providerId) => !builtInProviderIds.has(providerId))
|
|
64
69
|
.sort((a, b) => a.localeCompare(b))
|
|
65
|
-
.map((providerId) =>
|
|
66
|
-
providerId
|
|
67
|
-
label
|
|
68
|
-
|
|
70
|
+
.map((providerId) => {
|
|
71
|
+
const provider = document.providers[providerId]!;
|
|
72
|
+
const label = formatProviderConsoleRow(providerId, provider, 81, { requestHeaderProfiles: document.requestHeaderProfiles });
|
|
73
|
+
// [喵喵喵]: label 已被列宽截断,搜索需要补上完整的接入 ID 与名称。
|
|
74
|
+
return { providerId, label, searchText: `${providerId} ${getProviderDisplayName(providerId, provider)} ${label}` };
|
|
75
|
+
});
|
|
69
76
|
}
|
|
70
77
|
|
|
71
78
|
function buildProviderMenuRows(provider: StoredProvider): ProviderMenuRow[] {
|
|
72
79
|
return [...provider.models]
|
|
73
80
|
.sort((a, b) => a.id.localeCompare(b.id))
|
|
74
|
-
.map((model) =>
|
|
75
|
-
|
|
76
|
-
model,
|
|
77
|
-
|
|
78
|
-
}));
|
|
81
|
+
.map((model) => {
|
|
82
|
+
const label = formatModelListRow(provider, model);
|
|
83
|
+
return { modelId: model.id, model, label, searchText: `${model.id} ${model.name ?? ""} ${label}` };
|
|
84
|
+
});
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
function notifyValidationErrors(ctx: ExtensionCommandContext, title: string, errors: string[]): void {
|
|
@@ -234,11 +240,12 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
234
240
|
return true;
|
|
235
241
|
}
|
|
236
242
|
const rows = buildProviderMenuRows(currentProvider);
|
|
243
|
+
const modelColumns = getModelColumnWidths(currentProvider.models);
|
|
237
244
|
const action = await showPersistentShortcutMenu<ProviderShortcut>(
|
|
238
245
|
ctx,
|
|
239
246
|
`/model-manager / ${getProviderDisplayName(providerId, currentProvider)}`,
|
|
240
247
|
"",
|
|
241
|
-
rows.map((row, index) => ({ id: `${index}`, label: row.label })),
|
|
248
|
+
rows.map((row, index) => ({ id: `${index}`, label: row.label, searchText: row.searchText })),
|
|
242
249
|
cursor,
|
|
243
250
|
[
|
|
244
251
|
{ input: "a", shortcut: "add-model" },
|
|
@@ -250,13 +257,20 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
250
257
|
formatProviderSummaryLine(currentProvider, state.requestHeaderProfiles),
|
|
251
258
|
formatProviderEndpointLine(currentProvider),
|
|
252
259
|
],
|
|
253
|
-
tableHeader: (width) => formatModelListHeader(currentProvider, width),
|
|
254
|
-
formatRow: (menuRow, width) => {
|
|
260
|
+
tableHeader: (width) => formatModelListHeader(currentProvider, width, modelColumns),
|
|
261
|
+
formatRow: (menuRow, width, theme) => {
|
|
255
262
|
const row = rows[Number.parseInt(menuRow.id, 10)];
|
|
256
|
-
return row ? formatModelListRow(currentProvider, row.model, width) : menuRow.label;
|
|
263
|
+
return row ? formatModelListRow(currentProvider, row.model, width, { ...modelColumns, theme }) : menuRow.label;
|
|
257
264
|
},
|
|
258
265
|
visibleRows: Math.min(12, Math.max(1, rows.length)),
|
|
259
|
-
|
|
266
|
+
hints: [
|
|
267
|
+
{ key: "↑↓", label: "选择" },
|
|
268
|
+
{ key: "Enter", label: "编辑模型" },
|
|
269
|
+
{ key: "A", label: "添加模型" },
|
|
270
|
+
{ key: "E", label: "编辑接入" },
|
|
271
|
+
{ key: "D", label: "删除模型" },
|
|
272
|
+
{ key: "Esc", label: "返回" },
|
|
273
|
+
],
|
|
260
274
|
emptyLabel: "暂无模型;按 a 添加第一个模型",
|
|
261
275
|
},
|
|
262
276
|
);
|
|
@@ -330,10 +344,13 @@ export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContex
|
|
|
330
344
|
const state = await readState();
|
|
331
345
|
const builtInProviderIds = await getBuiltinProviderIds();
|
|
332
346
|
const rows = buildRows(state, builtInProviderIds);
|
|
347
|
+
const nameWidth = getProviderNameColumnWidth(
|
|
348
|
+
rows.map((row) => getProviderDisplayLabel(row.providerId, state.providers[row.providerId]!)),
|
|
349
|
+
);
|
|
333
350
|
const modelCount = rows.reduce((sum, row) => sum + (state.providers[row.providerId]?.models.length ?? 0), 0);
|
|
334
351
|
const profileCount = Object.keys(state.requestHeaderProfiles).length;
|
|
335
352
|
const captureCount = Object.keys(state.clientHeaderCaptures).length;
|
|
336
|
-
const menuRows = rows.map((r, index) => ({ id: `${index}`, label: r.label }));
|
|
353
|
+
const menuRows = rows.map((r, index) => ({ id: `${index}`, label: r.label, searchText: r.searchText }));
|
|
337
354
|
const action = await showPersistentShortcutMenu<DashboardShortcut>(
|
|
338
355
|
ctx,
|
|
339
356
|
"/model-manager",
|
|
@@ -347,20 +364,29 @@ export async function runDashboard(pi: ExtensionAPI, ctx: ExtensionCommandContex
|
|
|
347
364
|
],
|
|
348
365
|
{
|
|
349
366
|
summaryLines: [`${rows.length} 接入 · ${modelCount} 模型 · ${captureCount} 内置抓包 · ${profileCount} 自定义请求头`],
|
|
350
|
-
tableHeader: formatProviderConsoleHeader,
|
|
351
|
-
formatRow: (menuRow, width) => {
|
|
367
|
+
tableHeader: (width) => formatProviderConsoleHeader(width, { nameWidth }),
|
|
368
|
+
formatRow: (menuRow, width, theme) => {
|
|
352
369
|
const row = rows[Number.parseInt(menuRow.id, 10)];
|
|
353
370
|
const provider = row ? state.providers[row.providerId] : undefined;
|
|
354
371
|
return row && provider
|
|
355
|
-
? formatProviderConsoleRow(row.providerId, provider, state.requestHeaderProfiles,
|
|
372
|
+
? formatProviderConsoleRow(row.providerId, provider, width, { requestHeaderProfiles: state.requestHeaderProfiles, theme, nameWidth })
|
|
356
373
|
: menuRow.label;
|
|
357
374
|
},
|
|
358
|
-
getDetailLines: (selectedRow) => {
|
|
375
|
+
getDetailLines: (selectedRow, theme) => {
|
|
359
376
|
const row = rows[Number.parseInt(selectedRow?.id ?? "", 10)];
|
|
360
377
|
const provider = row ? state.providers[row.providerId] : undefined;
|
|
361
|
-
return row && provider
|
|
378
|
+
return row && provider
|
|
379
|
+
? formatProviderDetailLines(row.providerId, provider, { requestHeaderProfiles: state.requestHeaderProfiles, theme })
|
|
380
|
+
: [];
|
|
362
381
|
},
|
|
363
|
-
|
|
382
|
+
hints: [
|
|
383
|
+
{ key: "↑↓", label: "选择" },
|
|
384
|
+
{ key: "Enter", label: "进入" },
|
|
385
|
+
{ key: "N", label: "新建接入" },
|
|
386
|
+
{ key: "D", label: "删除接入" },
|
|
387
|
+
{ key: "H", label: "请求头" },
|
|
388
|
+
{ key: "Esc", label: "退出" },
|
|
389
|
+
],
|
|
364
390
|
emptyLabel: "暂无自定义接入;按 n 新建",
|
|
365
391
|
},
|
|
366
392
|
);
|
package/tui/editor-model.ts
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
// 关键差异:
|
|
5
5
|
// - 请求头已经收敛到接入级;模型编辑器只编辑模型自身能力
|
|
6
6
|
// - 提供"从上游拉取模型 ID 列表"入口(OpenAI/Anthropic/Google)
|
|
7
|
-
// -
|
|
7
|
+
// - 视觉支持用开关式 select,保存时仍映射为 text / text,image
|
|
8
8
|
|
|
9
9
|
import { BorderedLoader, type ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import type { AnthropicThinkingProtocol, BuiltInClientHeaderProfileId, ModelDraft, ModelListFetchOutcome, ReasoningMode, StoredClientHeaderCapture, StoredRequestHeaderProfile } from "../types.ts";
|
|
11
11
|
import { fetchModelIds } from "./model-list-fetch.ts";
|
|
12
12
|
import { pickModelIdFromList } from "./model-picker.ts";
|
|
13
|
-
import {
|
|
13
|
+
import { showOptionPicker, showPersistentFormMenu, padLabel, type MenuCursor } from "./persistent-menu.ts";
|
|
14
14
|
import {
|
|
15
15
|
describeVisionInput,
|
|
16
16
|
formatApiShort,
|
|
@@ -22,6 +22,8 @@ interface FieldRow {
|
|
|
22
22
|
id: string;
|
|
23
23
|
label: string;
|
|
24
24
|
value: string;
|
|
25
|
+
// 开关类字段可用 ←→ 就地切换;其余字段需要 Enter 进入输入或选择器。
|
|
26
|
+
adjustable?: boolean;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
|
|
@@ -50,32 +52,23 @@ function getIntegerFieldLabel(field: "contextWindow" | "maxTokens"): string {
|
|
|
50
52
|
return field === "contextWindow" ? "上下文窗口" : "最大输出";
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
function cycleSwitchState(current: boolean, direction: HorizontalDirection): boolean {
|
|
54
|
-
const states = [false, true];
|
|
55
|
-
const currentIndex = current ? 1 : 0;
|
|
56
|
-
const delta = direction === "right" ? 1 : -1;
|
|
57
|
-
return states[(currentIndex + delta + states.length) % states.length]!;
|
|
58
|
-
}
|
|
59
55
|
|
|
60
|
-
|
|
56
|
+
// 开关字段在两个方向上都是取反,因此不看 direction;返回是否真的切换了字段。
|
|
57
|
+
function applyHorizontalToggle(draft: ModelDraft, fieldId: string): boolean {
|
|
61
58
|
if (fieldId === "visionInput") {
|
|
62
|
-
|
|
63
|
-
draft.inputKinds = enabled ? ["text", "image"] : ["text"];
|
|
59
|
+
draft.inputKinds = supportsVisionInput(draft.inputKinds) ? ["text"] : ["text", "image"];
|
|
64
60
|
return true;
|
|
65
61
|
}
|
|
66
62
|
if (fieldId === "reasoning") {
|
|
67
|
-
|
|
68
|
-
draft.reasoningMode = enabled ? "enabled" : "disabled";
|
|
63
|
+
draft.reasoningMode = draft.reasoningMode === "enabled" ? "disabled" : "enabled";
|
|
69
64
|
return true;
|
|
70
65
|
}
|
|
71
66
|
if (fieldId === "anthropicThinkingProtocol") {
|
|
72
|
-
|
|
73
|
-
draft.anthropicThinkingProtocol = enabled ? "adaptive" : "legacy";
|
|
67
|
+
draft.anthropicThinkingProtocol = draft.anthropicThinkingProtocol === "adaptive" ? "legacy" : "adaptive";
|
|
74
68
|
return true;
|
|
75
69
|
}
|
|
76
70
|
if (fieldId === "openAIServiceTier") {
|
|
77
|
-
|
|
78
|
-
draft.openAIServiceTier = enabled ? "priority" : undefined;
|
|
71
|
+
draft.openAIServiceTier = draft.openAIServiceTier === "priority" ? undefined : "priority";
|
|
79
72
|
return true;
|
|
80
73
|
}
|
|
81
74
|
return false;
|
|
@@ -86,18 +79,19 @@ function buildRows(draft: ModelDraft): FieldRow[] {
|
|
|
86
79
|
{ id: "modelId", label: "模型 ID", value: draft.modelId || "<未填写>" },
|
|
87
80
|
{ id: "fetch", label: "重新拉取", value: "上游模型列表" },
|
|
88
81
|
{ id: "modelName", label: "显示名称", value: draft.modelName || "默认 = 模型 ID" },
|
|
89
|
-
{ id: "visionInput", label: "
|
|
90
|
-
{ id: "reasoning", label: "Thinking", value: describeReasoningMode(draft.reasoningMode) },
|
|
82
|
+
{ id: "visionInput", label: "视觉支持", value: describeVisionInput(draft.inputKinds), adjustable: true },
|
|
83
|
+
{ id: "reasoning", label: "Thinking", value: describeReasoningMode(draft.reasoningMode), adjustable: true },
|
|
91
84
|
];
|
|
92
85
|
if (shouldShowAnthropicThinkingProtocol(draft)) {
|
|
93
86
|
rows.push({
|
|
94
87
|
id: "anthropicThinkingProtocol",
|
|
95
88
|
label: "Adaptive",
|
|
96
89
|
value: describeAnthropicThinkingProtocol(draft.anthropicThinkingProtocol!),
|
|
90
|
+
adjustable: true,
|
|
97
91
|
});
|
|
98
92
|
}
|
|
99
93
|
if (shouldShowOpenAIServiceTier(draft)) {
|
|
100
|
-
rows.push({ id: "openAIServiceTier", label: "Fast mode", value: describeOpenAIServiceTier(draft) });
|
|
94
|
+
rows.push({ id: "openAIServiceTier", label: "Fast mode", value: describeOpenAIServiceTier(draft), adjustable: true });
|
|
101
95
|
}
|
|
102
96
|
rows.push(
|
|
103
97
|
{ id: "contextWindow", label: "上下文窗口", value: String(draft.contextWindow) },
|
|
@@ -106,13 +100,6 @@ function buildRows(draft: ModelDraft): FieldRow[] {
|
|
|
106
100
|
return rows;
|
|
107
101
|
}
|
|
108
102
|
|
|
109
|
-
function getAdjustableRowIds(draft: ModelDraft): string[] {
|
|
110
|
-
const ids = ["visionInput", "reasoning"];
|
|
111
|
-
if (shouldShowAnthropicThinkingProtocol(draft)) ids.push("anthropicThinkingProtocol");
|
|
112
|
-
if (shouldShowOpenAIServiceTier(draft)) ids.push("openAIServiceTier");
|
|
113
|
-
return ids;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
103
|
function getSelectedCustomHeaders(
|
|
117
104
|
draft: ModelDraft,
|
|
118
105
|
requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
|
|
@@ -219,85 +206,51 @@ async function editField(
|
|
|
219
206
|
return;
|
|
220
207
|
}
|
|
221
208
|
if (fieldId === "visionInput") {
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
VISION_INPUT_CHOICES.map((choice) => ({
|
|
229
|
-
id: choice.enabled ? "enabled" : "disabled",
|
|
230
|
-
label: choice.enabled === current ? `${choice.label} ← 当前` : choice.label,
|
|
231
|
-
})),
|
|
232
|
-
cursor,
|
|
233
|
-
);
|
|
234
|
-
if (action.type === "cancel") return;
|
|
235
|
-
const choice = VISION_INPUT_CHOICES.find((candidate) => (candidate.enabled ? "enabled" : "disabled") === action.id);
|
|
209
|
+
const choices = VISION_INPUT_CHOICES.map((choice) => ({
|
|
210
|
+
id: choice.enabled ? "enabled" : "disabled",
|
|
211
|
+
label: choice.label,
|
|
212
|
+
kinds: choice.kinds,
|
|
213
|
+
}));
|
|
214
|
+
const choice = await showOptionPicker(ctx, "视觉支持", choices, supportsVisionInput(draft.inputKinds) ? "enabled" : "disabled");
|
|
236
215
|
if (choice) draft.inputKinds = [...choice.kinds];
|
|
237
216
|
return;
|
|
238
217
|
}
|
|
239
218
|
if (fieldId === "reasoning") {
|
|
240
|
-
const
|
|
241
|
-
{ mode: "disabled", label: "关闭 — 不发送模型推理参数" },
|
|
242
|
-
{ mode: "enabled", label: "开启 — 启用模型推理参数" },
|
|
243
|
-
];
|
|
244
|
-
const cursor: MenuCursor = { index: choices.findIndex((choice) => choice.mode === draft.reasoningMode) };
|
|
245
|
-
const action = await showPersistentMenu(
|
|
219
|
+
const choice = await showOptionPicker(
|
|
246
220
|
ctx,
|
|
247
221
|
"Thinking",
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
id:
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
cursor,
|
|
222
|
+
[
|
|
223
|
+
{ id: "disabled" as ReasoningMode, label: "关闭 — 不发送模型推理参数" },
|
|
224
|
+
{ id: "enabled" as ReasoningMode, label: "开启 — 启用模型推理参数" },
|
|
225
|
+
],
|
|
226
|
+
draft.reasoningMode,
|
|
254
227
|
);
|
|
255
|
-
if (
|
|
256
|
-
const choice = choices.find((candidate) => candidate.mode === action.id);
|
|
257
|
-
if (choice) draft.reasoningMode = choice.mode;
|
|
228
|
+
if (choice) draft.reasoningMode = choice.id;
|
|
258
229
|
return;
|
|
259
230
|
}
|
|
260
231
|
if (fieldId === "anthropicThinkingProtocol") {
|
|
261
|
-
const
|
|
262
|
-
{ protocol: "adaptive", label: "开启:新版模型,发送 thinking.type=adaptive 和 output_config.effort" },
|
|
263
|
-
{ protocol: "legacy", label: "关闭:旧版接口,发送 thinking.type=enabled 和 budget_tokens" },
|
|
264
|
-
];
|
|
265
|
-
const current = draft.anthropicThinkingProtocol ?? "legacy";
|
|
266
|
-
const cursor: MenuCursor = { index: choices.findIndex((choice) => choice.protocol === current) };
|
|
267
|
-
const action = await showPersistentMenu(
|
|
232
|
+
const choice = await showOptionPicker(
|
|
268
233
|
ctx,
|
|
269
234
|
"Adaptive",
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
id:
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
cursor,
|
|
235
|
+
[
|
|
236
|
+
{ id: "adaptive" as AnthropicThinkingProtocol, label: "开启:新版模型,发送 thinking.type=adaptive 和 output_config.effort" },
|
|
237
|
+
{ id: "legacy" as AnthropicThinkingProtocol, label: "关闭:旧版接口,发送 thinking.type=enabled 和 budget_tokens" },
|
|
238
|
+
],
|
|
239
|
+
draft.anthropicThinkingProtocol ?? "legacy",
|
|
276
240
|
);
|
|
277
|
-
if (
|
|
278
|
-
const choice = choices.find((candidate) => candidate.protocol === action.id);
|
|
279
|
-
if (choice) draft.anthropicThinkingProtocol = choice.protocol;
|
|
241
|
+
if (choice) draft.anthropicThinkingProtocol = choice.id;
|
|
280
242
|
return;
|
|
281
243
|
}
|
|
282
244
|
if (fieldId === "openAIServiceTier") {
|
|
283
|
-
const
|
|
284
|
-
{ id: "disabled", tier: undefined, label: "关闭 — 不发送 service_tier(默认)" },
|
|
285
|
-
{ id: "priority", tier: "priority", label: "开启 — service_tier=priority,可能消耗 Fast / priority 额度" },
|
|
286
|
-
];
|
|
287
|
-
const current = draft.openAIServiceTier === "priority" ? "priority" : "disabled";
|
|
288
|
-
const cursor: MenuCursor = { index: choices.findIndex((choice) => choice.id === current) };
|
|
289
|
-
const action = await showPersistentMenu(
|
|
245
|
+
const choice = await showOptionPicker(
|
|
290
246
|
ctx,
|
|
291
247
|
"Fast mode",
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
id:
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
cursor,
|
|
248
|
+
[
|
|
249
|
+
{ id: "disabled", tier: undefined, label: "关闭 — 不发送 service_tier(默认)" },
|
|
250
|
+
{ id: "priority", tier: "priority", label: "开启 — service_tier=priority,可能消耗 Fast / priority 额度" },
|
|
251
|
+
] as const satisfies readonly { id: string; tier: ModelDraft["openAIServiceTier"]; label: string }[],
|
|
252
|
+
draft.openAIServiceTier === "priority" ? "priority" : "disabled",
|
|
298
253
|
);
|
|
299
|
-
if (action.type === "cancel") return;
|
|
300
|
-
const choice = choices.find((candidate) => candidate.id === action.id);
|
|
301
254
|
if (choice) draft.openAIServiceTier = choice.tier;
|
|
302
255
|
return;
|
|
303
256
|
}
|
|
@@ -322,11 +275,14 @@ export async function editModel(
|
|
|
322
275
|
}
|
|
323
276
|
const cursor: MenuCursor = { index: 0 };
|
|
324
277
|
while (true) {
|
|
325
|
-
|
|
326
|
-
const
|
|
278
|
+
// [喵喵喵]: 字段行的排版在开关切换后要原样重建,抽成函数避免两处写法飘移。
|
|
279
|
+
const toMenuRows = (fieldRows: FieldRow[]) => fieldRows.map((row) => ({
|
|
327
280
|
id: row.id,
|
|
328
281
|
label: `${padLabel(row.label, 16)}${row.value}`,
|
|
282
|
+
adjustable: row.adjustable,
|
|
329
283
|
}));
|
|
284
|
+
const rows = buildRows(draft);
|
|
285
|
+
const menuRows = toMenuRows(rows);
|
|
330
286
|
const action = await showPersistentFormMenu(
|
|
331
287
|
ctx,
|
|
332
288
|
titlePrefix,
|
|
@@ -334,20 +290,25 @@ export async function editModel(
|
|
|
334
290
|
menuRows,
|
|
335
291
|
cursor,
|
|
336
292
|
{
|
|
337
|
-
adjustableRowIds: getAdjustableRowIds(draft),
|
|
338
293
|
summaryLines: [
|
|
339
294
|
`接入 ${draft.providerId} · API ${formatApiShort(draft.api)}`,
|
|
340
295
|
"Ctrl+S 保存并启用模型;不切换当前会话模型",
|
|
341
296
|
],
|
|
342
|
-
|
|
297
|
+
onAdjust: (id) => {
|
|
298
|
+
if (!applyHorizontalToggle(draft, id)) return undefined;
|
|
299
|
+
return toMenuRows(buildRows(draft));
|
|
300
|
+
},
|
|
301
|
+
hints: [
|
|
302
|
+
{ key: "↑↓", label: "选择" },
|
|
303
|
+
{ key: "←→", label: "切换选项" },
|
|
304
|
+
{ key: "Enter", label: "编辑" },
|
|
305
|
+
{ key: "Ctrl+S", label: "保存并启用" },
|
|
306
|
+
{ key: "Esc", label: "返回" },
|
|
307
|
+
],
|
|
343
308
|
},
|
|
344
309
|
);
|
|
345
310
|
if (action.type === "cancel") return { action: "cancel" };
|
|
346
311
|
if (action.type === "save") return { action: "save", draft };
|
|
347
|
-
if (action.type === "adjust") {
|
|
348
|
-
applyHorizontalToggle(draft, action.id, action.direction);
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
312
|
await editField(ctx, draft, action.id, requestHeaderProfiles, clientHeaderCaptures);
|
|
352
313
|
}
|
|
353
314
|
}
|
package/tui/editor-provider.ts
CHANGED
|
@@ -7,8 +7,9 @@ import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
|
7
7
|
import { hasStringRecordEntries } from "../common.ts";
|
|
8
8
|
import { switchProviderDraftApiPreset } from "../presets/providers.ts";
|
|
9
9
|
import { redactUrlForDisplay } from "../sensitive-redaction.ts";
|
|
10
|
+
import { resolveRuntimeBaseUrl } from "../runtime-base-url.ts";
|
|
10
11
|
import { DEFAULT_PROVIDER_HTTP_PROXY_URL } from "../types.ts";
|
|
11
|
-
import { showPersistentFormMenu, padLabel, type
|
|
12
|
+
import { showOptionPicker, showPersistentFormMenu, padLabel, type MenuCursor } from "./persistent-menu.ts";
|
|
12
13
|
import {
|
|
13
14
|
API_CHOICES,
|
|
14
15
|
BUILT_IN_PROFILE_CHOICES,
|
|
@@ -22,6 +23,8 @@ interface FieldRow {
|
|
|
22
23
|
id: string;
|
|
23
24
|
label: string;
|
|
24
25
|
value: string;
|
|
26
|
+
// 开关类字段可用 ←→ 就地切换;其余字段需要 Enter 进入输入或选择器。
|
|
27
|
+
adjustable?: boolean;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
function buildRows(
|
|
@@ -39,7 +42,7 @@ function buildRows(
|
|
|
39
42
|
{ id: "providerId", label: "接入 ID(必填)", value: draft.providerId || "<必填>" },
|
|
40
43
|
{ id: "providerName", label: "名称", value: draft.providerName || "<空>" },
|
|
41
44
|
{ id: "baseUrl", label: "Base URL", value: draft.baseUrl },
|
|
42
|
-
{ id: "httpProxyEnabled", label: "本机代理", value: draft.httpProxyEnabled ? "开启" : "关闭" },
|
|
45
|
+
{ id: "httpProxyEnabled", label: "本机代理", value: draft.httpProxyEnabled ? "开启" : "关闭", adjustable: true },
|
|
43
46
|
{ id: "httpProxyUrl", label: "代理地址", value: draft.httpProxyEnabled ? redactUrlForDisplay(draft.httpProxyUrl || DEFAULT_PROVIDER_HTTP_PROXY_URL) : "关闭时不使用" },
|
|
44
47
|
{ id: "apiKey", label: "API key", value: maskSecret(draft.apiKey) },
|
|
45
48
|
{ id: "authHeader", label: "认证头", value: draft.authHeader ? "Bearer" : "默认" },
|
|
@@ -47,29 +50,12 @@ function buildRows(
|
|
|
47
50
|
];
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
async function selectChoice<T extends { id: string; label: string }>(
|
|
51
|
-
ctx: ExtensionCommandContext,
|
|
52
|
-
title: string,
|
|
53
|
-
choices: T[],
|
|
54
|
-
currentId: string,
|
|
55
|
-
): Promise<T | undefined> {
|
|
56
|
-
const labels = choices.map((c) => c.id === currentId ? `${c.label} ← 当前` : c.label);
|
|
57
|
-
const picked = await ctx.ui.select(title, labels);
|
|
58
|
-
if (!picked) return undefined;
|
|
59
|
-
const index = labels.indexOf(picked);
|
|
60
|
-
return index >= 0 ? choices[index] : undefined;
|
|
61
|
-
}
|
|
62
53
|
|
|
63
|
-
function cycleSwitchState(current: boolean, direction: HorizontalDirection): boolean {
|
|
64
|
-
const states = [false, true];
|
|
65
|
-
const currentIndex = current ? 1 : 0;
|
|
66
|
-
const delta = direction === "right" ? 1 : -1;
|
|
67
|
-
return states[(currentIndex + delta + states.length) % states.length]!;
|
|
68
|
-
}
|
|
69
54
|
|
|
70
|
-
|
|
55
|
+
// 开关字段在两个方向上都是取反,因此不看 direction;返回是否真的切换了字段。
|
|
56
|
+
function applyHorizontalToggle(draft: ProviderDraft, fieldId: string): boolean {
|
|
71
57
|
if (fieldId !== "httpProxyEnabled") return false;
|
|
72
|
-
draft.httpProxyEnabled =
|
|
58
|
+
draft.httpProxyEnabled = !draft.httpProxyEnabled;
|
|
73
59
|
if (draft.httpProxyEnabled && !draft.httpProxyUrl.trim()) draft.httpProxyUrl = DEFAULT_PROVIDER_HTTP_PROXY_URL;
|
|
74
60
|
return true;
|
|
75
61
|
}
|
|
@@ -80,22 +66,18 @@ async function editClientHeaderProfile(
|
|
|
80
66
|
requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
|
|
81
67
|
): Promise<void> {
|
|
82
68
|
const choices = [
|
|
83
|
-
...BUILT_IN_PROFILE_CHOICES.map((choice) => ({
|
|
84
|
-
id: choice.id,
|
|
85
|
-
label: draft.clientHeaderProfile === choice.id ? `${choice.label} ← 当前` : choice.label,
|
|
86
|
-
})),
|
|
69
|
+
...BUILT_IN_PROFILE_CHOICES.map((choice) => ({ id: choice.id as string, label: choice.label })),
|
|
87
70
|
...Object.entries(requestHeaderProfiles)
|
|
88
71
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
89
72
|
.map(([profileId, profile]) => ({
|
|
90
73
|
id: `custom:${profileId}`,
|
|
91
|
-
label:
|
|
92
|
-
? `自定义:${profileId} — ${profile.name} ← 当前`
|
|
93
|
-
: `自定义:${profileId} — ${profile.name}`,
|
|
74
|
+
label: `自定义:${profileId} — ${profile.name}`,
|
|
94
75
|
})),
|
|
95
76
|
];
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
77
|
+
const currentId = draft.clientHeaderProfile === "custom" && draft.requestHeaderProfileId
|
|
78
|
+
? `custom:${draft.requestHeaderProfileId}`
|
|
79
|
+
: draft.clientHeaderProfile;
|
|
80
|
+
const choice = await showOptionPicker(ctx, "请求头", choices, currentId);
|
|
99
81
|
if (!choice) return;
|
|
100
82
|
if (choice.id.startsWith("custom:")) {
|
|
101
83
|
draft.clientHeaderProfile = "custom";
|
|
@@ -113,16 +95,21 @@ async function editField(
|
|
|
113
95
|
requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
|
|
114
96
|
): Promise<void> {
|
|
115
97
|
if (fieldId === "api") {
|
|
116
|
-
const choice = await
|
|
98
|
+
const choice = await showOptionPicker(ctx, "选择 API 协议", API_CHOICES, draft.api);
|
|
117
99
|
if (choice) switchProviderDraftApiPreset(draft, choice.id);
|
|
118
100
|
return;
|
|
119
101
|
}
|
|
120
102
|
if (fieldId === "authHeader") {
|
|
121
|
-
const choice = await
|
|
122
|
-
|
|
123
|
-
"
|
|
124
|
-
|
|
125
|
-
|
|
103
|
+
const choice = await showOptionPicker(
|
|
104
|
+
ctx,
|
|
105
|
+
"认证头(API key 放在哪里)",
|
|
106
|
+
[
|
|
107
|
+
{ id: "default", label: "默认 — 交给协议 SDK / 接入默认行为" },
|
|
108
|
+
{ id: "bearer", label: "Bearer — 强制 Authorization: Bearer <apiKey>" },
|
|
109
|
+
],
|
|
110
|
+
draft.authHeader ? "bearer" : "default",
|
|
111
|
+
);
|
|
112
|
+
if (choice) draft.authHeader = choice.id === "bearer";
|
|
126
113
|
return;
|
|
127
114
|
}
|
|
128
115
|
if (fieldId === "clientHeaderProfile") {
|
|
@@ -130,12 +117,17 @@ async function editField(
|
|
|
130
117
|
return;
|
|
131
118
|
}
|
|
132
119
|
if (fieldId === "httpProxyEnabled") {
|
|
133
|
-
const choice = await
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
120
|
+
const choice = await showOptionPicker(
|
|
121
|
+
ctx,
|
|
122
|
+
"本机代理(仅当前接入点)",
|
|
123
|
+
[
|
|
124
|
+
{ id: "disabled", label: "关闭 — 请求直连上游" },
|
|
125
|
+
{ id: "enabled", label: `开启 — 通过 ${redactUrlForDisplay(draft.httpProxyUrl || DEFAULT_PROVIDER_HTTP_PROXY_URL)}` },
|
|
126
|
+
],
|
|
127
|
+
draft.httpProxyEnabled ? "enabled" : "disabled",
|
|
128
|
+
);
|
|
137
129
|
if (!choice) return;
|
|
138
|
-
draft.httpProxyEnabled = choice.
|
|
130
|
+
draft.httpProxyEnabled = choice.id === "enabled";
|
|
139
131
|
if (draft.httpProxyEnabled && !draft.httpProxyUrl.trim()) draft.httpProxyUrl = DEFAULT_PROVIDER_HTTP_PROXY_URL;
|
|
140
132
|
return;
|
|
141
133
|
}
|
|
@@ -165,15 +157,15 @@ async function editField(
|
|
|
165
157
|
draft.apiKey = value.trim();
|
|
166
158
|
return;
|
|
167
159
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const current = (draft as any)[fieldId] as string;
|
|
172
|
-
const value = await ctx.ui.input(`${prompt ?? fieldId}(当前:${current || "<空>"},留空保持原值)`, current);
|
|
160
|
+
// [喵喵喵]: baseUrl 是唯一走通用文本输入的字段,显式列出才能保持 draft 的类型检查。
|
|
161
|
+
if (fieldId !== "baseUrl") return;
|
|
162
|
+
const value = await ctx.ui.input(`Base URL(http/https)(当前:${draft.baseUrl || "<空>"},留空保持原值)`, draft.baseUrl);
|
|
173
163
|
if (value === undefined) return;
|
|
174
164
|
const trimmed = value.trim();
|
|
175
165
|
if (!trimmed) return;
|
|
176
|
-
|
|
166
|
+
// [喵喵喵]: 当场归一化成 SDK 可直接使用的根地址,让 models.json 存的就是最终请求地址;
|
|
167
|
+
// 否则插件未加载时 pi 会用未补全的值直接请求,补全结果也无法在界面上核对。
|
|
168
|
+
draft.baseUrl = resolveRuntimeBaseUrl(draft.api, trimmed);
|
|
177
169
|
}
|
|
178
170
|
|
|
179
171
|
export type ProviderEditOutcome =
|
|
@@ -188,11 +180,14 @@ export async function editProvider(
|
|
|
188
180
|
): Promise<ProviderEditOutcome> {
|
|
189
181
|
const cursor: MenuCursor = { index: 0 };
|
|
190
182
|
while (true) {
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
id:
|
|
194
|
-
label: `${padLabel(
|
|
183
|
+
// [喵喵喵]: 字段行的排版在开关切换后要原样重建,抽成函数避免两处写法飘移。
|
|
184
|
+
const toMenuRows = (fieldRows: FieldRow[]) => fieldRows.map((row) => ({
|
|
185
|
+
id: row.id,
|
|
186
|
+
label: `${padLabel(row.label, 16)}${row.value}`,
|
|
187
|
+
adjustable: row.adjustable,
|
|
195
188
|
}));
|
|
189
|
+
const rows = buildRows(draft, requestHeaderProfiles);
|
|
190
|
+
const menuRows = toMenuRows(rows);
|
|
196
191
|
const action = await showPersistentFormMenu(
|
|
197
192
|
ctx,
|
|
198
193
|
titlePrefix,
|
|
@@ -200,21 +195,26 @@ export async function editProvider(
|
|
|
200
195
|
menuRows,
|
|
201
196
|
cursor,
|
|
202
197
|
{
|
|
203
|
-
adjustableRowIds: ["httpProxyEnabled"],
|
|
204
198
|
summaryLines: [
|
|
205
199
|
`API ${formatApiShort(draft.api)} · 请求头 ${describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles)}`,
|
|
206
200
|
"接入 ID 必填,且不能与已有或 pi 内置接入重复",
|
|
207
201
|
"Ctrl+S 保存并同步 models.json;不切换当前会话模型",
|
|
208
202
|
],
|
|
209
|
-
|
|
203
|
+
onAdjust: (id) => {
|
|
204
|
+
if (!applyHorizontalToggle(draft, id)) return undefined;
|
|
205
|
+
return toMenuRows(buildRows(draft, requestHeaderProfiles));
|
|
206
|
+
},
|
|
207
|
+
hints: [
|
|
208
|
+
{ key: "↑↓", label: "选择" },
|
|
209
|
+
{ key: "←→", label: "切换选项" },
|
|
210
|
+
{ key: "Enter", label: "编辑" },
|
|
211
|
+
{ key: "Ctrl+S", label: "保存并同步" },
|
|
212
|
+
{ key: "Esc", label: "返回" },
|
|
213
|
+
],
|
|
210
214
|
},
|
|
211
215
|
);
|
|
212
216
|
if (action.type === "cancel") return { action: "cancel" };
|
|
213
217
|
if (action.type === "save") return { action: "save", draft };
|
|
214
|
-
if (action.type === "adjust") {
|
|
215
|
-
applyHorizontalToggle(draft, action.id, action.direction);
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
218
|
await editField(ctx, draft, action.id, requestHeaderProfiles);
|
|
219
219
|
}
|
|
220
220
|
}
|