pi-model-manager 0.3.5 → 0.4.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 +19 -8
- package/README.md +19 -8
- package/atomic-write.ts +4 -1
- package/builtin-model-catalog.ts +17 -15
- package/configuration-lock.ts +65 -3
- package/configuration-persistence.ts +4 -4
- package/i18n.ts +10 -0
- package/local-proxy-service.ts +48 -15
- package/models-json-sync.ts +3 -10
- package/package.json +85 -83
- package/presets/builtin-client-headers.ts +4 -4
- package/presets/providers.ts +6 -2
- package/proper-lockfile.d.ts +18 -0
- package/provider-transport.ts +105 -8
- package/runtime-base-url.ts +10 -5
- package/state-document.ts +33 -3
- package/state-metadata-store.ts +22 -0
- package/state-store.ts +23 -2
- package/tui/dashboard.ts +29 -12
- package/tui/editor-model.ts +75 -10
- package/tui/editor-provider.ts +69 -8
- package/tui/header-profiles-panel.ts +27 -6
- package/tui/model-list-fetch.ts +6 -1
- package/tui/persistent-menu.ts +11 -1
- package/types.ts +5 -0
package/tui/dashboard.ts
CHANGED
|
@@ -248,9 +248,13 @@ async function editStoredModel(
|
|
|
248
248
|
return false;
|
|
249
249
|
}
|
|
250
250
|
const draft = createModelDraftFromStoredModel(providerId, provider, model);
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
251
|
+
// [喵喵喵]: 保存失败时保留草稿继续编辑,防止用户修改的 contextWindow/maxTokens 等配置丢失
|
|
252
|
+
while (true) {
|
|
253
|
+
const outcome = await editModel(ctx, draft, t("编辑模型 {fullId}", { fullId: getModelFullId(providerId, modelId) }), requestHeaderProfiles, clientHeaderCaptures);
|
|
254
|
+
if (outcome.action !== "save") return false;
|
|
255
|
+
const saved = await saveModelDraft(pi, ctx, outcome.draft, modelId);
|
|
256
|
+
if (saved) return true;
|
|
257
|
+
}
|
|
254
258
|
}
|
|
255
259
|
|
|
256
260
|
async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext, providerId: string): Promise<boolean> {
|
|
@@ -301,16 +305,26 @@ async function showProviderMenu(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
301
305
|
if (action.type === "shortcut") {
|
|
302
306
|
if (action.shortcut === "add-model") {
|
|
303
307
|
const draft = createModelDraftForStoredProvider(providerId, currentProvider);
|
|
304
|
-
|
|
305
|
-
|
|
308
|
+
// [喵喵喵]: 新增模型校验或保存失败时保留草稿
|
|
309
|
+
while (true) {
|
|
310
|
+
const outcome = await editModel(ctx, draft, t("添加模型到 {providerId}", { providerId }), state.requestHeaderProfiles, state.clientHeaderCaptures);
|
|
311
|
+
if (outcome.action !== "save") break;
|
|
312
|
+
const saved = await saveModelDraft(pi, ctx, outcome.draft, undefined);
|
|
313
|
+
if (saved) break;
|
|
314
|
+
}
|
|
306
315
|
continue;
|
|
307
316
|
}
|
|
308
317
|
if (action.shortcut === "edit-provider") {
|
|
309
318
|
const draft = createProviderDraftFromStored(providerId, currentProvider);
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
await
|
|
313
|
-
if (outcome.
|
|
319
|
+
// [喵喵喵]: 编辑接入保存失败或重命名冲突时保留草稿,不退回主面板
|
|
320
|
+
while (true) {
|
|
321
|
+
const outcome = await editProvider(ctx, draft, t("编辑接入 {providerId}", { providerId }), state.requestHeaderProfiles);
|
|
322
|
+
if (outcome.action !== "save") break;
|
|
323
|
+
const saved = await saveProviderDraft(pi, ctx, outcome.draft, providerId);
|
|
324
|
+
if (saved) {
|
|
325
|
+
if (outcome.draft.providerId !== providerId) return true;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
314
328
|
}
|
|
315
329
|
continue;
|
|
316
330
|
}
|
|
@@ -351,9 +365,12 @@ async function createProviderAndModel(pi: ExtensionAPI, ctx: ExtensionCommandCon
|
|
|
351
365
|
const stored = stagedState.providers[providerOutcome.draft.providerId]!;
|
|
352
366
|
ctx.ui.notify(t("请继续添加第一个模型;取消模型编辑则不会创建该接入。"), "info");
|
|
353
367
|
const modelDraft = createModelDraftForStoredProvider(providerOutcome.draft.providerId, stored);
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
368
|
+
while (true) {
|
|
369
|
+
const modelOutcome = await editModel(ctx, modelDraft, t("添加模型到 {providerId}", { providerId: providerOutcome.draft.providerId }), stagedState.requestHeaderProfiles, stagedState.clientHeaderCaptures);
|
|
370
|
+
if (modelOutcome.action !== "save") return false;
|
|
371
|
+
const saved = await saveNewProviderAndModelDraft(pi, ctx, providerOutcome.draft, modelOutcome.draft);
|
|
372
|
+
if (saved) return true;
|
|
373
|
+
}
|
|
357
374
|
}
|
|
358
375
|
}
|
|
359
376
|
|
package/tui/editor-model.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { t } from "../i18n.ts";
|
|
|
11
11
|
import type { AnthropicThinkingProtocol, BuiltInClientHeaderProfileId, ModelDraft, ModelListFetchOutcome, ReasoningMode, StoredClientHeaderCapture, StoredRequestHeaderProfile } from "../types.ts";
|
|
12
12
|
import { fetchModelIds } from "./model-list-fetch.ts";
|
|
13
13
|
import { pickModelIdFromList } from "./model-picker.ts";
|
|
14
|
-
import { showOptionPicker, showPersistentFormMenu, padLabel, type MenuCursor } from "./persistent-menu.ts";
|
|
14
|
+
import { showOptionPicker, showPersistentFormMenu, padLabel, markAdjustableValue, type MenuCursor, type HorizontalDirection } from "./persistent-menu.ts";
|
|
15
15
|
import {
|
|
16
16
|
describeVisionInput,
|
|
17
17
|
formatApiShort,
|
|
@@ -54,8 +54,65 @@ function getIntegerFieldLabel(field: "contextWindow" | "maxTokens"): string {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
//
|
|
58
|
-
|
|
57
|
+
// [喵喵喵]: 上下文窗口常用十进制档位 (128k ~ 500k)
|
|
58
|
+
export const CONTEXT_WINDOW_PRESETS: readonly number[] = [
|
|
59
|
+
128_000,
|
|
60
|
+
200_000,
|
|
61
|
+
256_000,
|
|
62
|
+
272_000,
|
|
63
|
+
300_000,
|
|
64
|
+
400_000,
|
|
65
|
+
500_000,
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
// [喵喵喵]: 输出上限与上下文统一十进制档位 (16k ~ 128k);宁小勿大——写小仅封顶略低,写超真实上限会被 API 直接拒绝
|
|
69
|
+
export const MAX_TOKENS_PRESETS: readonly number[] = [
|
|
70
|
+
16_000,
|
|
71
|
+
32_000,
|
|
72
|
+
64_000,
|
|
73
|
+
128_000,
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
export function cycleNumericPreset(
|
|
77
|
+
current: number,
|
|
78
|
+
presets: readonly number[],
|
|
79
|
+
direction: HorizontalDirection,
|
|
80
|
+
): number {
|
|
81
|
+
if (presets.length === 0) return current;
|
|
82
|
+
if (direction === "left") {
|
|
83
|
+
for (let i = presets.length - 1; i >= 0; i--) {
|
|
84
|
+
if (presets[i] < current) return presets[i];
|
|
85
|
+
}
|
|
86
|
+
return presets[0];
|
|
87
|
+
}
|
|
88
|
+
for (let i = 0; i < presets.length; i++) {
|
|
89
|
+
if (presets[i] > current) return presets[i];
|
|
90
|
+
}
|
|
91
|
+
return presets[presets.length - 1];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function parsePositiveIntegerWithUnit(rawInput: string): number | undefined {
|
|
95
|
+
const trimmed = rawInput.trim();
|
|
96
|
+
if (!trimmed) return undefined;
|
|
97
|
+
|
|
98
|
+
const match = /^([0-9]+(?:\.[0-9]+)?)\s*([kmKM])?$/i.exec(trimmed);
|
|
99
|
+
if (!match) return undefined;
|
|
100
|
+
|
|
101
|
+
const num = Number.parseFloat(match[1]);
|
|
102
|
+
if (!Number.isFinite(num) || num <= 0) return undefined;
|
|
103
|
+
|
|
104
|
+
const unit = match[2]?.toLowerCase();
|
|
105
|
+
if (unit === "m") return Math.round(num * 1_000_000);
|
|
106
|
+
if (unit === "k") return Math.round(num * 1_000);
|
|
107
|
+
|
|
108
|
+
// 纯数字无单位时,若数值小于 1000(如输入 64、128、200),按习惯视作千倍处理
|
|
109
|
+
if (num < 1000) return Math.round(num * 1_000);
|
|
110
|
+
|
|
111
|
+
return Math.round(num);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 开关字段在两个方向上取反;数值字段按 direction 在预设间步进。
|
|
115
|
+
function applyHorizontalToggle(draft: ModelDraft, fieldId: string, direction: HorizontalDirection): boolean {
|
|
59
116
|
if (fieldId === "visionInput") {
|
|
60
117
|
draft.inputKinds = supportsVisionInput(draft.inputKinds) ? ["text"] : ["text", "image"];
|
|
61
118
|
return true;
|
|
@@ -72,6 +129,14 @@ function applyHorizontalToggle(draft: ModelDraft, fieldId: string): boolean {
|
|
|
72
129
|
draft.openAIServiceTier = draft.openAIServiceTier === "priority" ? undefined : "priority";
|
|
73
130
|
return true;
|
|
74
131
|
}
|
|
132
|
+
if (fieldId === "contextWindow") {
|
|
133
|
+
draft.contextWindow = cycleNumericPreset(draft.contextWindow, CONTEXT_WINDOW_PRESETS, direction);
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
if (fieldId === "maxTokens") {
|
|
137
|
+
draft.maxTokens = cycleNumericPreset(draft.maxTokens, MAX_TOKENS_PRESETS, direction);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
75
140
|
return false;
|
|
76
141
|
}
|
|
77
142
|
|
|
@@ -95,8 +160,8 @@ function buildRows(draft: ModelDraft): FieldRow[] {
|
|
|
95
160
|
rows.push({ id: "openAIServiceTier", label: "Fast mode", value: describeOpenAIServiceTier(draft), adjustable: true });
|
|
96
161
|
}
|
|
97
162
|
rows.push(
|
|
98
|
-
{ id: "contextWindow", label: t("上下文窗口"), value: String(draft.contextWindow) },
|
|
99
|
-
{ id: "maxTokens", label: t("最大输出"), value: String(draft.maxTokens) },
|
|
163
|
+
{ id: "contextWindow", label: t("上下文窗口"), value: String(draft.contextWindow), adjustable: true },
|
|
164
|
+
{ id: "maxTokens", label: t("最大输出"), value: String(draft.maxTokens), adjustable: true },
|
|
100
165
|
);
|
|
101
166
|
return rows;
|
|
102
167
|
}
|
|
@@ -172,8 +237,8 @@ async function editIntField(ctx: ExtensionCommandContext, draft: ModelDraft, fie
|
|
|
172
237
|
if (value === undefined) return;
|
|
173
238
|
const trimmed = value.trim();
|
|
174
239
|
if (!trimmed) return;
|
|
175
|
-
const parsed =
|
|
176
|
-
if (
|
|
240
|
+
const parsed = parsePositiveIntegerWithUnit(trimmed);
|
|
241
|
+
if (parsed === undefined || parsed <= 0) {
|
|
177
242
|
ctx.ui.notify(t("{label} 必须是正整数", { label }), "warning");
|
|
178
243
|
return;
|
|
179
244
|
}
|
|
@@ -279,7 +344,7 @@ export async function editModel(
|
|
|
279
344
|
// [喵喵喵]: 字段行的排版在开关切换后要原样重建,抽成函数避免两处写法飘移。
|
|
280
345
|
const toMenuRows = (fieldRows: FieldRow[]) => fieldRows.map((row) => ({
|
|
281
346
|
id: row.id,
|
|
282
|
-
label: `${padLabel(row.label, 16)}${row.value}`,
|
|
347
|
+
label: `${padLabel(row.label, 16)}${row.adjustable ? markAdjustableValue(row.value) : row.value}`,
|
|
283
348
|
adjustable: row.adjustable,
|
|
284
349
|
}));
|
|
285
350
|
const rows = buildRows(draft);
|
|
@@ -295,8 +360,8 @@ export async function editModel(
|
|
|
295
360
|
t("接入 {providerId} · API {api}", { providerId: draft.providerId, api: formatApiShort(draft.api) }),
|
|
296
361
|
t("Ctrl+S 保存并启用模型;不切换当前会话模型"),
|
|
297
362
|
],
|
|
298
|
-
onAdjust: (id) => {
|
|
299
|
-
if (!applyHorizontalToggle(draft, id)) return undefined;
|
|
363
|
+
onAdjust: (id, direction) => {
|
|
364
|
+
if (!applyHorizontalToggle(draft, id, direction)) return undefined;
|
|
300
365
|
return toMenuRows(buildRows(draft));
|
|
301
366
|
},
|
|
302
367
|
hints: [
|
package/tui/editor-provider.ts
CHANGED
|
@@ -8,9 +8,13 @@ import { hasStringRecordEntries } from "../common.ts";
|
|
|
8
8
|
import { t } from "../i18n.ts";
|
|
9
9
|
import { switchProviderDraftApiPreset } from "../presets/providers.ts";
|
|
10
10
|
import { redactUrlForDisplay } from "../sensitive-redaction.ts";
|
|
11
|
-
import { resolveRuntimeBaseUrl } from "../runtime-base-url.ts";
|
|
12
|
-
import {
|
|
13
|
-
|
|
11
|
+
import { resolveRuntimeBaseUrl, validateRequestBaseUrl } from "../runtime-base-url.ts";
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_PROVIDER_HTTP_PROXY_URL,
|
|
14
|
+
type OpenAIChatCompatibilityMode,
|
|
15
|
+
type OpenAIResponsesStreamCompletionMode,
|
|
16
|
+
} from "../types.ts";
|
|
17
|
+
import { showOptionPicker, showPersistentFormMenu, padLabel, markAdjustableValue, type HorizontalDirection, type MenuCursor } from "./persistent-menu.ts";
|
|
14
18
|
import {
|
|
15
19
|
describeProfile,
|
|
16
20
|
formatApiShort,
|
|
@@ -29,6 +33,7 @@ interface FieldRow {
|
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
const OPENAI_CHAT_COMPATIBILITY_MODE_ORDER: readonly OpenAIChatCompatibilityMode[] = ["standard", "compatible"];
|
|
36
|
+
const OPENAI_RESPONSES_STREAM_COMPLETION_MODE_ORDER: readonly OpenAIResponsesStreamCompletionMode[] = ["standard", "terminal-event"];
|
|
32
37
|
|
|
33
38
|
function getOpenAIChatCompatibilityMode(draft: ProviderDraft): OpenAIChatCompatibilityMode {
|
|
34
39
|
return draft.openAIChatCompatibilityMode ?? "standard";
|
|
@@ -53,6 +58,29 @@ function cycleOpenAIChatCompatibilityMode(
|
|
|
53
58
|
return OPENAI_CHAT_COMPATIBILITY_MODE_ORDER[(index + offset + OPENAI_CHAT_COMPATIBILITY_MODE_ORDER.length) % OPENAI_CHAT_COMPATIBILITY_MODE_ORDER.length]!;
|
|
54
59
|
}
|
|
55
60
|
|
|
61
|
+
function getOpenAIResponsesStreamCompletionMode(draft: ProviderDraft): OpenAIResponsesStreamCompletionMode {
|
|
62
|
+
return draft.openAIResponsesStreamCompletionMode ?? "standard";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function describeOpenAIResponsesStreamCompletionMode(mode: OpenAIResponsesStreamCompletionMode): string {
|
|
66
|
+
if (mode === "terminal-event") return t("兼容 · 终态即结束");
|
|
67
|
+
return t("标准 · 等待连接关闭");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getOpenAIResponsesStreamCompletionModeHint(mode: OpenAIResponsesStreamCompletionMode): string {
|
|
71
|
+
if (mode === "terminal-event") return t("兼容模式:正式终态事件已转交后主动结束,不等待上游关闭连接。");
|
|
72
|
+
return t("标准模式:等待上游正常结束流。若回复完成后长期卡住,可启用兼容模式。");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function cycleOpenAIResponsesStreamCompletionMode(
|
|
76
|
+
mode: OpenAIResponsesStreamCompletionMode,
|
|
77
|
+
direction: HorizontalDirection,
|
|
78
|
+
): OpenAIResponsesStreamCompletionMode {
|
|
79
|
+
const index = OPENAI_RESPONSES_STREAM_COMPLETION_MODE_ORDER.indexOf(mode);
|
|
80
|
+
const offset = direction === "right" ? 1 : -1;
|
|
81
|
+
return OPENAI_RESPONSES_STREAM_COMPLETION_MODE_ORDER[(index + offset + OPENAI_RESPONSES_STREAM_COMPLETION_MODE_ORDER.length) % OPENAI_RESPONSES_STREAM_COMPLETION_MODE_ORDER.length]!;
|
|
82
|
+
}
|
|
83
|
+
|
|
56
84
|
function buildRows(
|
|
57
85
|
draft: ProviderDraft,
|
|
58
86
|
requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
|
|
@@ -67,7 +95,7 @@ function buildRows(
|
|
|
67
95
|
{ id: "api", label: t("API 协议"), value: draft.api },
|
|
68
96
|
{ id: "providerId", label: t("接入 ID(必填)"), value: draft.providerId || t("<必填>") },
|
|
69
97
|
{ id: "providerName", label: t("名称"), value: draft.providerName || t("<空>") },
|
|
70
|
-
{ id: "baseUrl", label: "Base URL", value: draft.baseUrl },
|
|
98
|
+
{ id: "baseUrl", label: "Base URL", value: redactUrlForDisplay(draft.baseUrl) },
|
|
71
99
|
];
|
|
72
100
|
if (draft.api === "openai-completions") {
|
|
73
101
|
rows.push({
|
|
@@ -77,6 +105,14 @@ function buildRows(
|
|
|
77
105
|
adjustable: true,
|
|
78
106
|
});
|
|
79
107
|
}
|
|
108
|
+
if (draft.api === "openai-responses") {
|
|
109
|
+
rows.push({
|
|
110
|
+
id: "openAIResponsesStreamCompletionMode",
|
|
111
|
+
label: t("流结束兼容"),
|
|
112
|
+
value: describeOpenAIResponsesStreamCompletionMode(getOpenAIResponsesStreamCompletionMode(draft)),
|
|
113
|
+
adjustable: true,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
80
116
|
rows.push(
|
|
81
117
|
{ id: "httpProxyEnabled", label: t("本机代理"), value: draft.httpProxyEnabled ? t("开启") : t("关闭"), adjustable: true },
|
|
82
118
|
{ id: "httpProxyUrl", label: t("代理地址"), value: draft.httpProxyEnabled ? redactUrlForDisplay(draft.httpProxyUrl || DEFAULT_PROVIDER_HTTP_PROXY_URL) : t("关闭时不使用") },
|
|
@@ -95,6 +131,10 @@ function applyHorizontalToggle(draft: ProviderDraft, fieldId: string, direction:
|
|
|
95
131
|
draft.openAIChatCompatibilityMode = cycleOpenAIChatCompatibilityMode(getOpenAIChatCompatibilityMode(draft), direction);
|
|
96
132
|
return true;
|
|
97
133
|
}
|
|
134
|
+
if (fieldId === "openAIResponsesStreamCompletionMode") {
|
|
135
|
+
draft.openAIResponsesStreamCompletionMode = cycleOpenAIResponsesStreamCompletionMode(getOpenAIResponsesStreamCompletionMode(draft), direction);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
98
138
|
if (fieldId !== "httpProxyEnabled") return false;
|
|
99
139
|
draft.httpProxyEnabled = !draft.httpProxyEnabled;
|
|
100
140
|
if (draft.httpProxyEnabled && !draft.httpProxyUrl.trim()) draft.httpProxyUrl = DEFAULT_PROVIDER_HTTP_PROXY_URL;
|
|
@@ -153,6 +193,19 @@ async function editField(
|
|
|
153
193
|
if (choice) draft.openAIChatCompatibilityMode = choice.id as OpenAIChatCompatibilityMode;
|
|
154
194
|
return;
|
|
155
195
|
}
|
|
196
|
+
if (fieldId === "openAIResponsesStreamCompletionMode") {
|
|
197
|
+
const choice = await showOptionPicker(
|
|
198
|
+
ctx,
|
|
199
|
+
t("选择流结束兼容"),
|
|
200
|
+
[
|
|
201
|
+
{ id: "standard", label: t("标准 — 等待上游正常关闭连接") },
|
|
202
|
+
{ id: "terminal-event", label: t("兼容 — 收到正式终态事件后主动结束") },
|
|
203
|
+
],
|
|
204
|
+
getOpenAIResponsesStreamCompletionMode(draft),
|
|
205
|
+
);
|
|
206
|
+
if (choice) draft.openAIResponsesStreamCompletionMode = choice.id as OpenAIResponsesStreamCompletionMode;
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
156
209
|
if (fieldId === "authHeader") {
|
|
157
210
|
const choice = await showOptionPicker(
|
|
158
211
|
ctx,
|
|
@@ -217,9 +270,14 @@ async function editField(
|
|
|
217
270
|
if (value === undefined) return;
|
|
218
271
|
const trimmed = value.trim();
|
|
219
272
|
if (!trimmed) return;
|
|
220
|
-
// [喵喵喵]:
|
|
221
|
-
|
|
222
|
-
|
|
273
|
+
// [喵喵喵]: 当场校验并归一化成 SDK 可直接使用的根地址;若输入有误通过通知友好提示,避免未捕获异常导致 TUI 闪退丢失草稿
|
|
274
|
+
try {
|
|
275
|
+
validateRequestBaseUrl(trimmed);
|
|
276
|
+
draft.baseUrl = resolveRuntimeBaseUrl(draft.api, trimmed);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
279
|
+
ctx.ui.notify(t("Base URL 格式错误:{error}", { error: message }), "warning");
|
|
280
|
+
}
|
|
223
281
|
}
|
|
224
282
|
|
|
225
283
|
export type ProviderEditOutcome =
|
|
@@ -237,7 +295,7 @@ export async function editProvider(
|
|
|
237
295
|
// [喵喵喵]: 字段行的排版在开关切换后要原样重建,抽成函数避免两处写法飘移。
|
|
238
296
|
const toMenuRows = (fieldRows: FieldRow[]) => fieldRows.map((row) => ({
|
|
239
297
|
id: row.id,
|
|
240
|
-
label: `${padLabel(row.label, 16)}${row.value}`,
|
|
298
|
+
label: `${padLabel(row.label, 16)}${row.adjustable ? markAdjustableValue(row.value) : row.value}`,
|
|
241
299
|
adjustable: row.adjustable,
|
|
242
300
|
}));
|
|
243
301
|
const rows = buildRows(draft, requestHeaderProfiles);
|
|
@@ -251,6 +309,9 @@ export async function editProvider(
|
|
|
251
309
|
if (draft.api === "openai-completions") {
|
|
252
310
|
summaryLines.push(getOpenAIChatCompatibilityModeHint(getOpenAIChatCompatibilityMode(draft)));
|
|
253
311
|
}
|
|
312
|
+
if (draft.api === "openai-responses") {
|
|
313
|
+
summaryLines.push(getOpenAIResponsesStreamCompletionModeHint(getOpenAIResponsesStreamCompletionMode(draft)));
|
|
314
|
+
}
|
|
254
315
|
return summaryLines;
|
|
255
316
|
};
|
|
256
317
|
const action = await showPersistentFormMenu(
|
|
@@ -270,11 +270,21 @@ async function saveProfile(
|
|
|
270
270
|
}
|
|
271
271
|
try {
|
|
272
272
|
await saveRequestHeaderProfileConfiguration(pi, ctx, state, draft, oldProfileId);
|
|
273
|
-
return true;
|
|
274
273
|
} catch (error) {
|
|
275
274
|
ctx.ui.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), "error");
|
|
276
275
|
return false;
|
|
277
276
|
}
|
|
277
|
+
// [喵喵喵]: 保存后重新计数——重命名场景下引用已改指向新 ID,用保存前状态会误判 (issue #1);
|
|
278
|
+
// 提示属于锦上添花,读取失败不能反向误导成保存失败
|
|
279
|
+
try {
|
|
280
|
+
const savedState = await readState();
|
|
281
|
+
if (countModelsUsingRequestHeaderProfile(savedState, draft.profileId.trim()) === 0) {
|
|
282
|
+
ctx.ui.notify(t("请求头 {profileId} 尚未被任何接入使用;在接入编辑器的「请求头」行选择后才会生效。", { profileId: draft.profileId.trim() }), "info");
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
// 计数提示失败时静默跳过,保存结果不受影响
|
|
286
|
+
}
|
|
287
|
+
return true;
|
|
278
288
|
}
|
|
279
289
|
|
|
280
290
|
async function deleteProfile(pi: ExtensionAPI, ctx: ExtensionCommandContext, profileId: string): Promise<boolean> {
|
|
@@ -309,9 +319,14 @@ async function editStoredProfile(pi: ExtensionAPI, ctx: ExtensionCommandContext,
|
|
|
309
319
|
ctx.ui.notify(t("请求头不存在:{profileId}", { profileId }), "warning");
|
|
310
320
|
return true;
|
|
311
321
|
}
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
322
|
+
const draft = createRequestHeaderProfileDraftFromStored(profileId, profile);
|
|
323
|
+
// [喵喵喵]: 保存失败时保留当前草稿重试,防止用户输入的头信息与名称被强制退出丢弃
|
|
324
|
+
while (true) {
|
|
325
|
+
const outcome = await editProfile(ctx, draft, t("编辑请求头 {profileId}", { profileId }));
|
|
326
|
+
if (outcome.action !== "save") return false;
|
|
327
|
+
const saved = await saveProfile(pi, ctx, outcome.draft, profileId);
|
|
328
|
+
if (saved) return true;
|
|
329
|
+
}
|
|
315
330
|
}
|
|
316
331
|
|
|
317
332
|
type HeaderProfileShortcut = "new-profile" | "delete-profile";
|
|
@@ -367,8 +382,14 @@ export async function runHeaderProfilesPanel(pi: ExtensionAPI, ctx: ExtensionCom
|
|
|
367
382
|
if (action.type === "cancel") return;
|
|
368
383
|
if (action.type === "shortcut") {
|
|
369
384
|
if (action.shortcut === "new-profile") {
|
|
370
|
-
const
|
|
371
|
-
|
|
385
|
+
const draft = createRequestHeaderProfileDraft(profileIds);
|
|
386
|
+
// [喵喵喵]: 新建保存失败时保留已输入的草稿继续编辑
|
|
387
|
+
while (true) {
|
|
388
|
+
const outcome = await editProfile(ctx, draft, t("新建请求头"));
|
|
389
|
+
if (outcome.action !== "save") break;
|
|
390
|
+
const saved = await saveProfile(pi, ctx, outcome.draft, undefined);
|
|
391
|
+
if (saved) break;
|
|
392
|
+
}
|
|
372
393
|
continue;
|
|
373
394
|
}
|
|
374
395
|
const selectedProfileId = rows[cursor.index]?.profileId;
|
package/tui/model-list-fetch.ts
CHANGED
|
@@ -140,7 +140,12 @@ async function requestModelIds(
|
|
|
140
140
|
temporaryProxyRoute = proxyConfig
|
|
141
141
|
? await openProxyRouteWithSignal(proxyConfig, url, signal)
|
|
142
142
|
: undefined;
|
|
143
|
-
|
|
143
|
+
// [喵喵喵]: 经由本地代理时强制禁止自动跟随 3xx 重定向,防止直连上游泄露 IP
|
|
144
|
+
const response = await fetch(temporaryProxyRoute?.url ?? url, {
|
|
145
|
+
headers,
|
|
146
|
+
signal,
|
|
147
|
+
redirect: temporaryProxyRoute ? "manual" : "follow",
|
|
148
|
+
});
|
|
144
149
|
const text = await readBoundedResponseText(response, signal);
|
|
145
150
|
if (!response.ok) {
|
|
146
151
|
const reason = response.status === 401 || response.status === 403 ? t("认证失败,请检查 API key 和认证头")
|
package/tui/persistent-menu.ts
CHANGED
|
@@ -79,6 +79,11 @@ export function padLabel(label: string, columns: number): string {
|
|
|
79
79
|
return padToVisibleWidth(label, columns);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
// [喵喵喵]: 可左右键切换的行值两侧加滑块标记,让能力直接可见
|
|
83
|
+
export function markAdjustableValue(value: string): string {
|
|
84
|
+
return `← ${value} →`;
|
|
85
|
+
}
|
|
86
|
+
|
|
82
87
|
function getSearchText(row: MenuRow): string {
|
|
83
88
|
return (row.searchText ?? `${row.id}\n${row.label}`).toLocaleLowerCase();
|
|
84
89
|
}
|
|
@@ -196,7 +201,12 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
|
|
|
196
201
|
|
|
197
202
|
const syncCursor = (activeRows: MenuRow[]): void => {
|
|
198
203
|
const row = activeRows[selectedIndex];
|
|
199
|
-
if (row)
|
|
204
|
+
if (row) {
|
|
205
|
+
cursor.index = currentRows.indexOf(row);
|
|
206
|
+
} else {
|
|
207
|
+
// [喵喵喵]: 过滤为空时重置光标为 -1,防止外层快捷键取到陈旧索引误操作隐藏条目
|
|
208
|
+
cursor.index = -1;
|
|
209
|
+
}
|
|
200
210
|
};
|
|
201
211
|
|
|
202
212
|
const requestRender = (): void => {
|
package/types.ts
CHANGED
|
@@ -13,6 +13,7 @@ export type ThinkingLevelMap = Partial<Record<ThinkingLevel, string | null>>;
|
|
|
13
13
|
export type ReasoningMode = "enabled" | "disabled";
|
|
14
14
|
export type AnthropicThinkingProtocol = "adaptive" | "legacy";
|
|
15
15
|
export type OpenAIChatCompatibilityMode = "standard" | "compatible";
|
|
16
|
+
export type OpenAIResponsesStreamCompletionMode = "standard" | "terminal-event";
|
|
16
17
|
export type BuiltInClientHeaderProfileId = "claude-code" | "codex-cli";
|
|
17
18
|
export type ClientHeaderProfileId = "recommended" | "disabled" | BuiltInClientHeaderProfileId | "custom";
|
|
18
19
|
export type CompatSettings = Record<string, unknown>;
|
|
@@ -88,6 +89,8 @@ export interface StoredProvider {
|
|
|
88
89
|
customClientHeaders?: Record<string, string>;
|
|
89
90
|
httpProxyEnabled?: boolean;
|
|
90
91
|
httpProxyUrl?: string;
|
|
92
|
+
/** Responses 终态事件已完整转交后,是否主动结束本地 SSE 流。 */
|
|
93
|
+
openAIResponsesStreamCompletionMode?: OpenAIResponsesStreamCompletionMode;
|
|
91
94
|
models: StoredModel[];
|
|
92
95
|
}
|
|
93
96
|
|
|
@@ -108,6 +111,8 @@ export interface ProviderDraft {
|
|
|
108
111
|
api: ApiKind;
|
|
109
112
|
/** Chat Completions 系统提示词兼容模式;standard 保持 Pi 默认判断。 */
|
|
110
113
|
openAIChatCompatibilityMode?: OpenAIChatCompatibilityMode;
|
|
114
|
+
/** Responses 流结束模式;terminal-event 不等待上游关闭连接。 */
|
|
115
|
+
openAIResponsesStreamCompletionMode?: OpenAIResponsesStreamCompletionMode;
|
|
111
116
|
baseUrl: string;
|
|
112
117
|
apiKey: string;
|
|
113
118
|
authHeader: boolean;
|