pi-model-manager 0.3.2 → 0.3.3

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/i18n.ts CHANGED
@@ -75,6 +75,17 @@ const ENGLISH_MESSAGES = {
75
75
  "本机代理": "Local proxy",
76
76
  "代理地址": "Proxy URL",
77
77
  "认证头": "Auth header",
78
+ "协议兼容": "Protocol compatibility",
79
+ "自动 · Pi 默认": "Auto · Pi default",
80
+ "标准 · developer": "Standard · developer",
81
+ "兼容 · system": "Compatible · system",
82
+ "选择协议兼容": "Select protocol compatibility",
83
+ "自动 — 不写强制配置,保持 Pi 当前判断": "Auto — keep Pi's current detection without an override",
84
+ "标准 — 对 reasoning 模型使用 developer role,适合标准 OpenAI 端点": "Standard — use the developer role for reasoning models on conformant OpenAI endpoints",
85
+ "兼容 — 强制 system role,适合忽略 developer 的中转": "Compatible — force the system role for relays that ignore developer",
86
+ "自动模式:由 Pi 判断系统提示词使用 developer 或 system role。": "Auto mode: Pi decides whether the system prompt uses the developer or system role.",
87
+ "标准模式:reasoning 模型的系统提示词使用 developer role。": "Standard mode: reasoning-model system prompts use the developer role.",
88
+ "兼容模式:系统提示词强制使用 system role,适合忽略 developer 的中转。": "Compatibility mode: the system prompt always uses the system role for relays that ignore developer.",
78
89
  "接入 ID 必填,且不能与已有或 pi 内置接入重复": "Provider ID is required and must not duplicate an existing or built-in Pi provider",
79
90
  "Ctrl+S 保存并同步 models.json;不切换当前会话模型": "Ctrl+S saves and syncs models.json without switching the current session model",
80
91
  "选择 API 协议": "Select API protocol",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-model-manager",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
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",
@@ -99,6 +99,10 @@ export function switchProviderDraftApiPreset(draft: ProviderDraft, nextApi: ApiK
99
99
  const replaceBaseUrl = stillUsesPresetUrl(draft.baseUrl, previousPreset.baseUrl);
100
100
  const replaceApiKey = draft.apiKey === previousPreset.apiKey;
101
101
  draft.api = nextApi;
102
+ // [喵喵喵]: 协议切换回来时保留用户原先的选择;首次切入 Chat 才补默认自动模式。
103
+ if (nextApi === "openai-completions" && draft.openAIChatDeveloperRole === undefined) {
104
+ draft.openAIChatDeveloperRole = "auto";
105
+ }
102
106
  // [喵喵喵]: 版本路径规则随协议而变(OpenAI 要 /v1、Anthropic 不要),
103
107
  // 保留的自定义地址必须按新协议重新归一化,否则切完协议就指向错误端点。
104
108
  draft.baseUrl = replaceBaseUrl ? nextPreset.baseUrl : resolveRuntimeBaseUrl(nextApi, draft.baseUrl);
package/state-document.ts CHANGED
@@ -23,6 +23,7 @@ import { resolveRuntimeBaseUrl } from "./runtime-base-url.ts";
23
23
  import type {
24
24
  ApiKind,
25
25
  CompatSettings,
26
+ OpenAIChatDeveloperRole,
26
27
  ModelDraft,
27
28
  ProviderDraft,
28
29
  RequestHeaderProfileDraft,
@@ -44,6 +45,12 @@ function isApiKind(value: unknown): value is ApiKind {
44
45
  return typeof value === "string" && API_KINDS.includes(value as ApiKind);
45
46
  }
46
47
 
48
+ function getOpenAIChatDeveloperRole(compat: CompatSettings | undefined): OpenAIChatDeveloperRole {
49
+ if (compat?.supportsDeveloperRole === true) return "developer";
50
+ if (compat?.supportsDeveloperRole === false) return "system";
51
+ return "auto";
52
+ }
53
+
47
54
  export function getModelFullId(providerId: string, modelId: string): string {
48
55
  return `${providerId}/${modelId}`;
49
56
  }
@@ -77,6 +84,7 @@ export function createProviderDraft(api: ApiKind = "openai-responses"): Provider
77
84
  providerId: "",
78
85
  providerName: preset.defaultProviderName,
79
86
  api,
87
+ openAIChatDeveloperRole: "auto",
80
88
  baseUrl: preset.baseUrl,
81
89
  apiKey: preset.apiKey,
82
90
  authHeader: preset.authHeader,
@@ -95,6 +103,7 @@ export function createProviderDraftFromStored(providerId: string, stored: Stored
95
103
  providerId,
96
104
  providerName: stored.name ?? (providerId.replace(/^custom-/, "") || preset.defaultProviderName),
97
105
  api,
106
+ openAIChatDeveloperRole: getOpenAIChatDeveloperRole(stored.compat),
98
107
  baseUrl: stored.baseUrl ?? preset.baseUrl,
99
108
  apiKey: stored.apiKey ?? "",
100
109
  authHeader: stored.authHeader ?? preset.authHeader,
@@ -277,6 +286,10 @@ export function getProviderChangeWarnings(
277
286
  if ((current.apiKey ?? "") !== draft.apiKey.trim()) changes.push("API key");
278
287
  if ((current.api ?? "") !== draft.api) changes.push(t("API 协议"));
279
288
  if ((current.authHeader ?? false) !== draft.authHeader) changes.push(t("认证头"));
289
+ if (draft.api === "openai-completions"
290
+ && getOpenAIChatDeveloperRole(current.compat) !== (draft.openAIChatDeveloperRole ?? "auto")) {
291
+ changes.push(t("协议兼容"));
292
+ }
280
293
  if ((current.clientHeaderProfile ?? "recommended") !== draft.clientHeaderProfile
281
294
  || (current.requestHeaderProfileId ?? "") !== (draft.requestHeaderProfileId ?? ""))
282
295
  changes.push(t("请求头"));
@@ -352,6 +365,13 @@ function buildProviderFromDraft(
352
365
  } else {
353
366
  delete next.customClientHeaders;
354
367
  }
368
+ if (draft.api === "openai-completions" && draft.openAIChatDeveloperRole !== undefined) {
369
+ const compat: CompatSettings = cloneJson(next.compat) ?? {};
370
+ if (draft.openAIChatDeveloperRole === "auto") delete compat.supportsDeveloperRole;
371
+ else compat.supportsDeveloperRole = draft.openAIChatDeveloperRole === "developer";
372
+ if (Object.keys(compat).length > 0) next.compat = compat;
373
+ else delete next.compat;
374
+ }
355
375
  return next;
356
376
  }
357
377
 
@@ -9,8 +9,8 @@ import { t } from "../i18n.ts";
9
9
  import { switchProviderDraftApiPreset } from "../presets/providers.ts";
10
10
  import { redactUrlForDisplay } from "../sensitive-redaction.ts";
11
11
  import { resolveRuntimeBaseUrl } from "../runtime-base-url.ts";
12
- import { DEFAULT_PROVIDER_HTTP_PROXY_URL } from "../types.ts";
13
- import { showOptionPicker, showPersistentFormMenu, padLabel, type MenuCursor } from "./persistent-menu.ts";
12
+ import { DEFAULT_PROVIDER_HTTP_PROXY_URL, type OpenAIChatDeveloperRole } from "../types.ts";
13
+ import { showOptionPicker, showPersistentFormMenu, padLabel, type HorizontalDirection, type MenuCursor } from "./persistent-menu.ts";
14
14
  import {
15
15
  describeProfile,
16
16
  formatApiShort,
@@ -28,6 +28,33 @@ interface FieldRow {
28
28
  adjustable?: boolean;
29
29
  }
30
30
 
31
+ const OPENAI_CHAT_DEVELOPER_ROLE_ORDER: readonly OpenAIChatDeveloperRole[] = ["auto", "developer", "system"];
32
+
33
+ function getOpenAIChatDeveloperRole(draft: ProviderDraft): OpenAIChatDeveloperRole {
34
+ return draft.openAIChatDeveloperRole ?? "auto";
35
+ }
36
+
37
+ function describeOpenAIChatDeveloperRole(role: OpenAIChatDeveloperRole): string {
38
+ if (role === "developer") return t("标准 · developer");
39
+ if (role === "system") return t("兼容 · system");
40
+ return t("自动 · Pi 默认");
41
+ }
42
+
43
+ function getOpenAIChatDeveloperRoleHint(role: OpenAIChatDeveloperRole): string {
44
+ if (role === "developer") return t("标准模式:reasoning 模型的系统提示词使用 developer role。");
45
+ if (role === "system") return t("兼容模式:系统提示词强制使用 system role,适合忽略 developer 的中转。");
46
+ return t("自动模式:由 Pi 判断系统提示词使用 developer 或 system role。");
47
+ }
48
+
49
+ function cycleOpenAIChatDeveloperRole(
50
+ role: OpenAIChatDeveloperRole,
51
+ direction: HorizontalDirection,
52
+ ): OpenAIChatDeveloperRole {
53
+ const index = OPENAI_CHAT_DEVELOPER_ROLE_ORDER.indexOf(role);
54
+ const offset = direction === "right" ? 1 : -1;
55
+ return OPENAI_CHAT_DEVELOPER_ROLE_ORDER[(index + offset + OPENAI_CHAT_DEVELOPER_ROLE_ORDER.length) % OPENAI_CHAT_DEVELOPER_ROLE_ORDER.length]!;
56
+ }
57
+
31
58
  function buildRows(
32
59
  draft: ProviderDraft,
33
60
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
@@ -38,23 +65,38 @@ function buildRows(
38
65
  const profileDisplay = inlineCustomProfile
39
66
  ? t("内联自定义请求头({count}项)", { count: Object.keys(draft.customClientHeaders).length })
40
67
  : describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles);
41
- return [
68
+ const rows: FieldRow[] = [
42
69
  { id: "api", label: t("API 协议"), value: draft.api },
43
70
  { id: "providerId", label: t("接入 ID(必填)"), value: draft.providerId || t("<必填>") },
44
71
  { id: "providerName", label: t("名称"), value: draft.providerName || t("<空>") },
45
72
  { id: "baseUrl", label: "Base URL", value: draft.baseUrl },
73
+ ];
74
+ if (draft.api === "openai-completions") {
75
+ rows.push({
76
+ id: "openAIChatDeveloperRole",
77
+ label: t("协议兼容"),
78
+ value: describeOpenAIChatDeveloperRole(getOpenAIChatDeveloperRole(draft)),
79
+ adjustable: true,
80
+ });
81
+ }
82
+ rows.push(
46
83
  { id: "httpProxyEnabled", label: t("本机代理"), value: draft.httpProxyEnabled ? t("开启") : t("关闭"), adjustable: true },
47
84
  { id: "httpProxyUrl", label: t("代理地址"), value: draft.httpProxyEnabled ? redactUrlForDisplay(draft.httpProxyUrl || DEFAULT_PROVIDER_HTTP_PROXY_URL) : t("关闭时不使用") },
48
85
  { id: "apiKey", label: "API key", value: maskSecret(draft.apiKey) },
49
86
  { id: "authHeader", label: t("认证头"), value: draft.authHeader ? "Bearer" : t("默认") },
50
87
  { id: "clientHeaderProfile", label: t("请求头"), value: profileDisplay },
51
- ];
88
+ );
89
+ return rows;
52
90
  }
53
91
 
54
92
 
55
93
 
56
- // 开关字段在两个方向上都是取反,因此不看 direction;返回是否真的切换了字段。
57
- function applyHorizontalToggle(draft: ProviderDraft, fieldId: string): boolean {
94
+ // 二值开关的两个方向都取反;三态协议兼容按方向循环。
95
+ function applyHorizontalToggle(draft: ProviderDraft, fieldId: string, direction: HorizontalDirection): boolean {
96
+ if (fieldId === "openAIChatDeveloperRole") {
97
+ draft.openAIChatDeveloperRole = cycleOpenAIChatDeveloperRole(getOpenAIChatDeveloperRole(draft), direction);
98
+ return true;
99
+ }
58
100
  if (fieldId !== "httpProxyEnabled") return false;
59
101
  draft.httpProxyEnabled = !draft.httpProxyEnabled;
60
102
  if (draft.httpProxyEnabled && !draft.httpProxyUrl.trim()) draft.httpProxyUrl = DEFAULT_PROVIDER_HTTP_PROXY_URL;
@@ -100,6 +142,20 @@ async function editField(
100
142
  if (choice) switchProviderDraftApiPreset(draft, choice.id);
101
143
  return;
102
144
  }
145
+ if (fieldId === "openAIChatDeveloperRole") {
146
+ const choice = await showOptionPicker(
147
+ ctx,
148
+ t("选择协议兼容"),
149
+ [
150
+ { id: "auto", label: t("自动 — 不写强制配置,保持 Pi 当前判断") },
151
+ { id: "developer", label: t("标准 — 对 reasoning 模型使用 developer role,适合标准 OpenAI 端点") },
152
+ { id: "system", label: t("兼容 — 强制 system role,适合忽略 developer 的中转") },
153
+ ],
154
+ getOpenAIChatDeveloperRole(draft),
155
+ );
156
+ if (choice) draft.openAIChatDeveloperRole = choice.id as OpenAIChatDeveloperRole;
157
+ return;
158
+ }
103
159
  if (fieldId === "authHeader") {
104
160
  const choice = await showOptionPicker(
105
161
  ctx,
@@ -189,6 +245,17 @@ export async function editProvider(
189
245
  }));
190
246
  const rows = buildRows(draft, requestHeaderProfiles);
191
247
  const menuRows = toMenuRows(rows);
248
+ const getSummaryLines = (): string[] => {
249
+ const summaryLines = [
250
+ `API ${formatApiShort(draft.api)} · ${t("请求头")} ${describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles)}`,
251
+ t("接入 ID 必填,且不能与已有或 pi 内置接入重复"),
252
+ t("Ctrl+S 保存并同步 models.json;不切换当前会话模型"),
253
+ ];
254
+ if (draft.api === "openai-completions") {
255
+ summaryLines.push(getOpenAIChatDeveloperRoleHint(getOpenAIChatDeveloperRole(draft)));
256
+ }
257
+ return summaryLines;
258
+ };
192
259
  const action = await showPersistentFormMenu(
193
260
  ctx,
194
261
  titlePrefix,
@@ -196,13 +263,9 @@ export async function editProvider(
196
263
  menuRows,
197
264
  cursor,
198
265
  {
199
- summaryLines: [
200
- `API ${formatApiShort(draft.api)} · ${t("请求头")} ${describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles)}`,
201
- t("接入 ID 必填,且不能与已有或 pi 内置接入重复"),
202
- t("Ctrl+S 保存并同步 models.json;不切换当前会话模型"),
203
- ],
204
- onAdjust: (id) => {
205
- if (!applyHorizontalToggle(draft, id)) return undefined;
266
+ getSummaryLines,
267
+ onAdjust: (id, direction) => {
268
+ if (!applyHorizontalToggle(draft, id, direction)) return undefined;
206
269
  return toMenuRows(buildRows(draft, requestHeaderProfiles));
207
270
  },
208
271
  hints: [
@@ -29,6 +29,8 @@ export interface MenuHint {
29
29
 
30
30
  export interface PersistentMenuOptions {
31
31
  summaryLines?: readonly string[];
32
+ // [喵喵喵]: 就地切换字段时,摘要也可能依赖草稿状态;渲染时取值才能同步刷新。
33
+ getSummaryLines?: () => readonly string[];
32
34
  // [喵喵喵]: 渲染回调拿到 theme 才能给列与详情上语义色;theme 只存在于 custom 回调作用域。
33
35
  tableHeader?: string | ((width: number, theme: Theme) => string);
34
36
  formatRow?: (row: MenuRow, width: number, theme: Theme) => string;
@@ -419,7 +421,10 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
419
421
 
420
422
  const border = theme.fg("borderMuted", "─".repeat(Math.max(0, width)));
421
423
  const hintLines = layoutHintLines(getHints(), theme, width);
422
- const summaryLines = (options.summaryLines ?? (help ? help.split("\n") : []))
424
+ const summarySource = options.getSummaryLines?.()
425
+ ?? options.summaryLines
426
+ ?? (help ? help.split("\n") : []);
427
+ const summaryLines = summarySource
423
428
  .map((line) => truncateToWidth(theme.fg("dim", line), width));
424
429
  const searchLine = searchable && (searchActive || searchQuery) ? renderQueryLine(width) : undefined;
425
430
  const tableHeaderText = typeof options.tableHeader === "function"
package/types.ts CHANGED
@@ -12,6 +12,7 @@ type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "
12
12
  export type ThinkingLevelMap = Partial<Record<ThinkingLevel, string | null>>;
13
13
  export type ReasoningMode = "enabled" | "disabled";
14
14
  export type AnthropicThinkingProtocol = "adaptive" | "legacy";
15
+ export type OpenAIChatDeveloperRole = "auto" | "developer" | "system";
15
16
  export type BuiltInClientHeaderProfileId = "claude-code" | "codex-cli";
16
17
  export type ClientHeaderProfileId = "recommended" | "disabled" | BuiltInClientHeaderProfileId | "custom";
17
18
  export type CompatSettings = Record<string, unknown>;
@@ -105,6 +106,8 @@ export interface ProviderDraft {
105
106
  providerId: string;
106
107
  providerName: string;
107
108
  api: ApiKind;
109
+ /** Chat Completions 的 system prompt role;auto 不写 compat 覆盖。 */
110
+ openAIChatDeveloperRole?: OpenAIChatDeveloperRole;
108
111
  baseUrl: string;
109
112
  apiKey: string;
110
113
  authHeader: boolean;