pi-model-manager 0.3.2 → 0.3.4

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,14 @@ const ENGLISH_MESSAGES = {
75
75
  "本机代理": "Local proxy",
76
76
  "代理地址": "Proxy URL",
77
77
  "认证头": "Auth header",
78
+ "协议兼容": "Protocol compatibility",
79
+ "标准 · Pi 默认": "Standard · Pi default",
80
+ "兼容 · system": "Compatible · system",
81
+ "选择协议兼容": "Select protocol compatibility",
82
+ "标准 — 保持 Pi 默认兼容判断": "Standard — keep Pi's default compatibility detection",
83
+ "兼容 — 强制 system role,适合忽略 developer 的中转": "Compatible — force the system role for relays that ignore developer",
84
+ "标准模式:保持 Pi 默认兼容判断。": "Standard mode: keep Pi's default compatibility detection.",
85
+ "兼容模式:系统提示词强制使用 system role,适合忽略 developer 的中转。": "Compatibility mode: the system prompt always uses the system role for relays that ignore developer.",
78
86
  "接入 ID 必填,且不能与已有或 pi 内置接入重复": "Provider ID is required and must not duplicate an existing or built-in Pi provider",
79
87
  "Ctrl+S 保存并同步 models.json;不切换当前会话模型": "Ctrl+S saves and syncs models.json without switching the current session model",
80
88
  "选择 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.4",
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.openAIChatCompatibilityMode === undefined) {
104
+ draft.openAIChatCompatibilityMode = "standard";
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
+ OpenAIChatCompatibilityMode,
26
27
  ModelDraft,
27
28
  ProviderDraft,
28
29
  RequestHeaderProfileDraft,
@@ -44,6 +45,10 @@ 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 getOpenAIChatCompatibilityMode(compat: CompatSettings | undefined): OpenAIChatCompatibilityMode {
49
+ return compat?.supportsDeveloperRole === false ? "compatible" : "standard";
50
+ }
51
+
47
52
  export function getModelFullId(providerId: string, modelId: string): string {
48
53
  return `${providerId}/${modelId}`;
49
54
  }
@@ -77,6 +82,7 @@ export function createProviderDraft(api: ApiKind = "openai-responses"): Provider
77
82
  providerId: "",
78
83
  providerName: preset.defaultProviderName,
79
84
  api,
85
+ openAIChatCompatibilityMode: "standard",
80
86
  baseUrl: preset.baseUrl,
81
87
  apiKey: preset.apiKey,
82
88
  authHeader: preset.authHeader,
@@ -95,6 +101,7 @@ export function createProviderDraftFromStored(providerId: string, stored: Stored
95
101
  providerId,
96
102
  providerName: stored.name ?? (providerId.replace(/^custom-/, "") || preset.defaultProviderName),
97
103
  api,
104
+ openAIChatCompatibilityMode: getOpenAIChatCompatibilityMode(stored.compat),
98
105
  baseUrl: stored.baseUrl ?? preset.baseUrl,
99
106
  apiKey: stored.apiKey ?? "",
100
107
  authHeader: stored.authHeader ?? preset.authHeader,
@@ -277,6 +284,10 @@ export function getProviderChangeWarnings(
277
284
  if ((current.apiKey ?? "") !== draft.apiKey.trim()) changes.push("API key");
278
285
  if ((current.api ?? "") !== draft.api) changes.push(t("API 协议"));
279
286
  if ((current.authHeader ?? false) !== draft.authHeader) changes.push(t("认证头"));
287
+ if (draft.api === "openai-completions"
288
+ && getOpenAIChatCompatibilityMode(current.compat) !== (draft.openAIChatCompatibilityMode ?? "standard")) {
289
+ changes.push(t("协议兼容"));
290
+ }
280
291
  if ((current.clientHeaderProfile ?? "recommended") !== draft.clientHeaderProfile
281
292
  || (current.requestHeaderProfileId ?? "") !== (draft.requestHeaderProfileId ?? ""))
282
293
  changes.push(t("请求头"));
@@ -352,6 +363,13 @@ function buildProviderFromDraft(
352
363
  } else {
353
364
  delete next.customClientHeaders;
354
365
  }
366
+ if (draft.api === "openai-completions" && draft.openAIChatCompatibilityMode !== undefined) {
367
+ const compat: CompatSettings = cloneJson(next.compat) ?? {};
368
+ if (draft.openAIChatCompatibilityMode === "standard") delete compat.supportsDeveloperRole;
369
+ else compat.supportsDeveloperRole = false;
370
+ if (Object.keys(compat).length > 0) next.compat = compat;
371
+ else delete next.compat;
372
+ }
355
373
  return next;
356
374
  }
357
375
 
@@ -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 OpenAIChatCompatibilityMode } 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,31 @@ interface FieldRow {
28
28
  adjustable?: boolean;
29
29
  }
30
30
 
31
+ const OPENAI_CHAT_COMPATIBILITY_MODE_ORDER: readonly OpenAIChatCompatibilityMode[] = ["standard", "compatible"];
32
+
33
+ function getOpenAIChatCompatibilityMode(draft: ProviderDraft): OpenAIChatCompatibilityMode {
34
+ return draft.openAIChatCompatibilityMode ?? "standard";
35
+ }
36
+
37
+ function describeOpenAIChatCompatibilityMode(mode: OpenAIChatCompatibilityMode): string {
38
+ if (mode === "compatible") return t("兼容 · system");
39
+ return t("标准 · Pi 默认");
40
+ }
41
+
42
+ function getOpenAIChatCompatibilityModeHint(mode: OpenAIChatCompatibilityMode): string {
43
+ if (mode === "compatible") return t("兼容模式:系统提示词强制使用 system role,适合忽略 developer 的中转。");
44
+ return t("标准模式:保持 Pi 默认兼容判断。");
45
+ }
46
+
47
+ function cycleOpenAIChatCompatibilityMode(
48
+ mode: OpenAIChatCompatibilityMode,
49
+ direction: HorizontalDirection,
50
+ ): OpenAIChatCompatibilityMode {
51
+ const index = OPENAI_CHAT_COMPATIBILITY_MODE_ORDER.indexOf(mode);
52
+ const offset = direction === "right" ? 1 : -1;
53
+ return OPENAI_CHAT_COMPATIBILITY_MODE_ORDER[(index + offset + OPENAI_CHAT_COMPATIBILITY_MODE_ORDER.length) % OPENAI_CHAT_COMPATIBILITY_MODE_ORDER.length]!;
54
+ }
55
+
31
56
  function buildRows(
32
57
  draft: ProviderDraft,
33
58
  requestHeaderProfiles: Record<string, StoredRequestHeaderProfile>,
@@ -38,23 +63,38 @@ function buildRows(
38
63
  const profileDisplay = inlineCustomProfile
39
64
  ? t("内联自定义请求头({count}项)", { count: Object.keys(draft.customClientHeaders).length })
40
65
  : describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles);
41
- return [
66
+ const rows: FieldRow[] = [
42
67
  { id: "api", label: t("API 协议"), value: draft.api },
43
68
  { id: "providerId", label: t("接入 ID(必填)"), value: draft.providerId || t("<必填>") },
44
69
  { id: "providerName", label: t("名称"), value: draft.providerName || t("<空>") },
45
70
  { id: "baseUrl", label: "Base URL", value: draft.baseUrl },
71
+ ];
72
+ if (draft.api === "openai-completions") {
73
+ rows.push({
74
+ id: "openAIChatCompatibilityMode",
75
+ label: t("协议兼容"),
76
+ value: describeOpenAIChatCompatibilityMode(getOpenAIChatCompatibilityMode(draft)),
77
+ adjustable: true,
78
+ });
79
+ }
80
+ rows.push(
46
81
  { id: "httpProxyEnabled", label: t("本机代理"), value: draft.httpProxyEnabled ? t("开启") : t("关闭"), adjustable: true },
47
82
  { id: "httpProxyUrl", label: t("代理地址"), value: draft.httpProxyEnabled ? redactUrlForDisplay(draft.httpProxyUrl || DEFAULT_PROVIDER_HTTP_PROXY_URL) : t("关闭时不使用") },
48
83
  { id: "apiKey", label: "API key", value: maskSecret(draft.apiKey) },
49
84
  { id: "authHeader", label: t("认证头"), value: draft.authHeader ? "Bearer" : t("默认") },
50
85
  { id: "clientHeaderProfile", label: t("请求头"), value: profileDisplay },
51
- ];
86
+ );
87
+ return rows;
52
88
  }
53
89
 
54
90
 
55
91
 
56
- // 开关字段在两个方向上都是取反,因此不看 direction;返回是否真的切换了字段。
57
- function applyHorizontalToggle(draft: ProviderDraft, fieldId: string): boolean {
92
+ // 二值开关的两个方向都取反。
93
+ function applyHorizontalToggle(draft: ProviderDraft, fieldId: string, direction: HorizontalDirection): boolean {
94
+ if (fieldId === "openAIChatCompatibilityMode") {
95
+ draft.openAIChatCompatibilityMode = cycleOpenAIChatCompatibilityMode(getOpenAIChatCompatibilityMode(draft), direction);
96
+ return true;
97
+ }
58
98
  if (fieldId !== "httpProxyEnabled") return false;
59
99
  draft.httpProxyEnabled = !draft.httpProxyEnabled;
60
100
  if (draft.httpProxyEnabled && !draft.httpProxyUrl.trim()) draft.httpProxyUrl = DEFAULT_PROVIDER_HTTP_PROXY_URL;
@@ -100,6 +140,19 @@ async function editField(
100
140
  if (choice) switchProviderDraftApiPreset(draft, choice.id);
101
141
  return;
102
142
  }
143
+ if (fieldId === "openAIChatCompatibilityMode") {
144
+ const choice = await showOptionPicker(
145
+ ctx,
146
+ t("选择协议兼容"),
147
+ [
148
+ { id: "standard", label: t("标准 — 保持 Pi 默认兼容判断") },
149
+ { id: "compatible", label: t("兼容 — 强制 system role,适合忽略 developer 的中转") },
150
+ ],
151
+ getOpenAIChatCompatibilityMode(draft),
152
+ );
153
+ if (choice) draft.openAIChatCompatibilityMode = choice.id as OpenAIChatCompatibilityMode;
154
+ return;
155
+ }
103
156
  if (fieldId === "authHeader") {
104
157
  const choice = await showOptionPicker(
105
158
  ctx,
@@ -189,6 +242,17 @@ export async function editProvider(
189
242
  }));
190
243
  const rows = buildRows(draft, requestHeaderProfiles);
191
244
  const menuRows = toMenuRows(rows);
245
+ const getSummaryLines = (): string[] => {
246
+ const summaryLines = [
247
+ `API ${formatApiShort(draft.api)} · ${t("请求头")} ${describeProfile(draft.clientHeaderProfile, draft.api, draft.requestHeaderProfileId, requestHeaderProfiles)}`,
248
+ t("接入 ID 必填,且不能与已有或 pi 内置接入重复"),
249
+ t("Ctrl+S 保存并同步 models.json;不切换当前会话模型"),
250
+ ];
251
+ if (draft.api === "openai-completions") {
252
+ summaryLines.push(getOpenAIChatCompatibilityModeHint(getOpenAIChatCompatibilityMode(draft)));
253
+ }
254
+ return summaryLines;
255
+ };
192
256
  const action = await showPersistentFormMenu(
193
257
  ctx,
194
258
  titlePrefix,
@@ -196,13 +260,9 @@ export async function editProvider(
196
260
  menuRows,
197
261
  cursor,
198
262
  {
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;
263
+ getSummaryLines,
264
+ onAdjust: (id, direction) => {
265
+ if (!applyHorizontalToggle(draft, id, direction)) return undefined;
206
266
  return toMenuRows(buildRows(draft, requestHeaderProfiles));
207
267
  },
208
268
  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 OpenAIChatCompatibilityMode = "standard" | "compatible";
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 系统提示词兼容模式;standard 保持 Pi 默认判断。 */
110
+ openAIChatCompatibilityMode?: OpenAIChatCompatibilityMode;
108
111
  baseUrl: string;
109
112
  apiKey: string;
110
113
  authHeader: boolean;