pi-model-manager 0.3.5 → 0.3.6

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 CHANGED
@@ -4,7 +4,7 @@ English · [简体中文](./README.md)
4
4
 
5
5
  [![Pi](https://img.shields.io/badge/Pi-%3E%3D0.85.1-6f42c1)](https://github.com/earendil-works/pi)
6
6
  [![License](https://img.shields.io/badge/license-AGPL--3.0-blue.svg)](./LICENSE)
7
- [![Version](https://img.shields.io/badge/version-0.3.5-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
7
+ [![Version](https://img.shields.io/badge/version-0.3.6-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
8
8
 
9
9
  A TUI model and provider manager for [Pi](https://github.com/earendil-works/pi). It keeps Pi's native `models.json` as the single source of truth for model configuration while adding provider/model editing, client-header identities, proxy routing, and protocol compatibility controls.
10
10
 
@@ -241,8 +241,8 @@ Model capabilities include:
241
241
 
242
242
  The current built-in values were derived from real client requests with authentication fields removed:
243
243
 
244
- - Claude Code `2.1.243`
245
- - Codex TUI `0.149.1`
244
+ - Claude Code `2.1.278`
245
+ - Codex TUI `0.155.1`
246
246
 
247
247
  These headers only help API gateways that require a recognized client identity; they do not replace an API key. The public repository and npm package contain **no request-capture tooling, user captures, authentication headers, or machine-local state**. Disable identity headers or create a custom profile if a built-in profile does not fit your endpoint.
248
248
 
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![Pi](https://img.shields.io/badge/Pi-%3E%3D0.85.1-6f42c1)](https://github.com/earendil-works/pi)
6
6
  [![License](https://img.shields.io/badge/license-AGPL--3.0-blue.svg)](./LICENSE)
7
- [![Version](https://img.shields.io/badge/version-0.3.5-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
7
+ [![Version](https://img.shields.io/badge/version-0.3.6-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
8
8
 
9
9
  一个面向 [Pi](https://github.com/earendil-works/pi) 的 TUI 模型与接入管理扩展。它以 Pi 原生 `models.json` 为模型配置的唯一权威来源,并提供接入/模型编辑、请求头身份、代理路由和协议兼容配置。
10
10
 
@@ -245,8 +245,8 @@ Anthropic 标准端点在写入 `models.json` 时转换为 Pi 原生 SDK 所需
245
245
 
246
246
  当前内置值来自真实客户端请求并已移除认证信息:
247
247
 
248
- - Claude Code `2.1.243`
249
- - Codex TUI `0.149.1`
248
+ - Claude Code `2.1.278`
249
+ - Codex TUI `0.155.1`
250
250
 
251
251
  这些值只用于兼容需要识别客户端身份的 API 中转,不代替 API key。公开仓库和 npm 包**不包含请求捕获工具、用户抓包、认证头或本机状态**。如果内置值不适合你的服务,请关闭身份头或创建自定义请求头。
252
252
 
package/i18n.ts CHANGED
@@ -96,6 +96,15 @@ const ENGLISH_MESSAGES = {
96
96
  "兼容 — 强制 system role,适合忽略 developer 的中转": "Compatible — force the system role for relays that ignore developer",
97
97
  "标准模式:保持 Pi 默认兼容判断。": "Standard mode: keep Pi's default compatibility detection.",
98
98
  "兼容模式:系统提示词强制使用 system role,适合忽略 developer 的中转。": "Compatibility mode: the system prompt always uses the system role for relays that ignore developer.",
99
+ "流结束兼容": "Stream completion",
100
+ "兼容 · 终态即结束": "Compatible · end on terminal event",
101
+ "标准 · 等待连接关闭": "Standard · wait for connection close",
102
+ "选择流结束兼容": "Select stream completion mode",
103
+ "标准 — 等待上游正常关闭连接": "Standard — wait for upstream to close the connection",
104
+ "兼容 — 收到正式终态事件后主动结束": "Compatible — finish after receiving a formal terminal event",
105
+ "兼容模式:正式终态事件已转交后主动结束,不等待上游关闭连接。": "Compatibility mode: finish after forwarding the formal terminal event without waiting for upstream to close.",
106
+ "标准模式:等待上游正常结束流。若回复完成后长期卡住,可启用兼容模式。": "Standard mode: wait for upstream to finish the stream normally. Enable compatibility mode if completed replies remain stuck.",
107
+ "未知 Responses 流结束模式:{value}": "Unknown Responses stream completion mode: {value}",
99
108
  "接入 ID 必填,且不能与已有或 pi 内置接入重复": "Provider ID is required and must not duplicate an existing or built-in Pi provider",
100
109
  "Ctrl+S 保存并同步 models.json;不切换当前会话模型": "Ctrl+S saves and syncs models.json without switching the current session model",
101
110
  "选择 API 协议": "Select API protocol",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-model-manager",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
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",
@@ -1,10 +1,10 @@
1
1
  // 由私有 request-capture-service 根据真实客户端请求生成;公开插件仅消费这些脱敏后的内置值。
2
2
 
3
3
  export const CLAUDE_CODE_CLIENT_HEADERS: Record<string, string> = {
4
- "anthropic-beta": "claude-code-20250219,context-1m-2025-08-07,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,per-turn-control-2026-07-01,effort-2025-11-24",
4
+ "anthropic-beta": "claude-code-20250219,context-1m-2025-08-07,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,per-turn-control-2026-07-01,mid-conversation-tool-changes-2026-07-01,effort-2025-11-24",
5
5
  "anthropic-dangerous-direct-browser-access": "true",
6
6
  "anthropic-version": "2023-06-01",
7
- "user-agent": "claude-cli/2.1.263 (external, sdk-cli)",
7
+ "user-agent": "claude-cli/2.1.278 (external, sdk-cli)",
8
8
  "x-app": "cli",
9
9
  "x-stainless-arch": "x64",
10
10
  "x-stainless-lang": "js",
@@ -18,6 +18,6 @@ export const CLAUDE_CODE_CLIENT_HEADERS: Record<string, string> = {
18
18
 
19
19
  export const CODEX_CLI_CLIENT_HEADERS: Record<string, string> = {
20
20
  "originator": "codex-tui",
21
- "user-agent": "codex-tui/0.152.1 (Windows 10.0.26200; x86_64) unknown (codex-tui; 0.152.1)",
21
+ "user-agent": "codex-tui/0.155.1 (Windows 10.0.26200; x86_64) unknown (codex-tui; 0.155.1)",
22
22
  "x-codex-beta-features": "remote_compaction_v2",
23
23
  };
@@ -8,6 +8,88 @@ import { getProviderHttpProxyUrl, openTemporaryLocalProxyRoute } from "./local-p
8
8
  import { appendUrlPath, resolveRuntimeBaseUrl, validateRequestBaseUrl } from "./runtime-base-url.ts";
9
9
  import type { ApiKind, StoredProvider } from "./types.ts";
10
10
 
11
+ const RESPONSES_TERMINAL_EVENTS = new Set([
12
+ "response.completed",
13
+ "response.incomplete",
14
+ "response.failed",
15
+ "error",
16
+ ]);
17
+
18
+ function findSseFrameBoundary(buffer: Uint8Array): number {
19
+ for (let index = 0; index < buffer.length; index += 1) {
20
+ if (buffer[index] === 10 && buffer[index + 1] === 10) return index + 2;
21
+ if (buffer[index] === 13 && buffer[index + 1] === 13) return index + 2;
22
+ if (buffer[index] === 13 && buffer[index + 1] === 10 && buffer[index + 2] === 13 && buffer[index + 3] === 10) return index + 4;
23
+ }
24
+ return -1;
25
+ }
26
+
27
+ function isResponsesTerminalFrame(frame: Uint8Array): boolean {
28
+ const text = new TextDecoder().decode(frame);
29
+ let eventType: string | undefined;
30
+ const dataLines: string[] = [];
31
+ for (const line of text.split(/\r?\n|\r/)) {
32
+ if (line.startsWith("event:")) eventType = line.slice("event:".length).trim();
33
+ if (line.startsWith("data:")) dataLines.push(line.slice("data:".length).trimStart());
34
+ }
35
+ if (eventType && RESPONSES_TERMINAL_EVENTS.has(eventType)) return true;
36
+ const data = dataLines.join("\n").trim();
37
+ if (!data || data === "[DONE]") return false;
38
+ try {
39
+ const parsed = JSON.parse(data) as { type?: unknown };
40
+ return typeof parsed.type === "string" && RESPONSES_TERMINAL_EVENTS.has(parsed.type);
41
+ } catch {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ function appendBytes(left: Uint8Array, right: Uint8Array): Uint8Array {
47
+ const combined = new Uint8Array(left.length + right.length);
48
+ combined.set(left);
49
+ combined.set(right, left.length);
50
+ return combined;
51
+ }
52
+
53
+ function wrapResponsesTerminalStream(response: Response): Response {
54
+ if (!response.body || !response.ok) return response;
55
+ const reader = response.body.getReader();
56
+ let buffer = new Uint8Array();
57
+ let terminal = false;
58
+ const body = new ReadableStream<Uint8Array>({
59
+ async pull(controller) {
60
+ while (!terminal) {
61
+ const boundary = findSseFrameBoundary(buffer);
62
+ if (boundary >= 0) {
63
+ const frame = buffer.slice(0, boundary);
64
+ buffer = buffer.slice(boundary);
65
+ terminal = isResponsesTerminalFrame(frame);
66
+ controller.enqueue(frame);
67
+ if (terminal) {
68
+ controller.close();
69
+ await reader.cancel().catch(() => {});
70
+ }
71
+ return;
72
+ }
73
+ const next = await reader.read();
74
+ if (next.done) {
75
+ if (buffer.length > 0) controller.enqueue(buffer);
76
+ controller.close();
77
+ return;
78
+ }
79
+ buffer = appendBytes(buffer, next.value);
80
+ }
81
+ },
82
+ cancel(reason) {
83
+ return reader.cancel(reason);
84
+ },
85
+ });
86
+ return new Response(body, {
87
+ status: response.status,
88
+ statusText: response.statusText,
89
+ headers: response.headers,
90
+ });
91
+ }
92
+
11
93
  export function createProviderTransport(runtime: ModelRuntime, native: Provider, provider: StoredProvider): Provider {
12
94
  const endpoints = new Map(provider.models.map((model) => {
13
95
  const api = model.api ?? provider.api;
@@ -48,7 +130,11 @@ export function createProviderTransport(runtime: ModelRuntime, native: Provider,
48
130
  if (proxyUrl) throw new Error(t("该协议尚不支持接入级代理:{api}", { api: model.api }));
49
131
  return stream(upstreamModel, context, options);
50
132
  }
51
- if (!proxyUrl && model.api !== "anthropic-messages") return stream(upstreamModel, context, options);
133
+ if (!proxyUrl
134
+ && model.api !== "anthropic-messages"
135
+ && !(model.api === "openai-responses" && provider.openAIResponsesStreamCompletionMode === "terminal-event")) {
136
+ return stream(upstreamModel, context, options);
137
+ }
52
138
  const fetchImpl = options?.fetch ?? globalThis.fetch;
53
139
  const transportFetch: typeof fetch = async (input, init) => {
54
140
  let request = new Request(input, init);
@@ -62,14 +148,24 @@ export function createProviderTransport(runtime: ModelRuntime, native: Provider,
62
148
  url.pathname = new URL(appendUrlPath(endpoint.baseUrl, "messages")).pathname;
63
149
  request = new Request(url, request);
64
150
  }
65
- if (!proxyUrl) return fetchImpl(request);
66
- const route = await openTemporaryLocalProxyRoute(native.id, request.url, proxyUrl);
67
- try {
68
- return await fetchImpl(new Request(route.url, request));
69
- } finally {
70
- // [喵喵喵]: 收到响应头时转发端已经取得路由;删除查找项不会中断已建立的 SSE 流。
71
- route.close();
151
+ let response: Response;
152
+ if (!proxyUrl) {
153
+ response = await fetchImpl(request);
154
+ } else {
155
+ const route = await openTemporaryLocalProxyRoute(native.id, request.url, proxyUrl);
156
+ try {
157
+ response = await fetchImpl(new Request(route.url, request));
158
+ } finally {
159
+ // [喵喵喵]: 收到响应头时转发端已经取得路由;删除查找项不会中断已建立的 SSE 流。
160
+ route.close();
161
+ }
162
+ }
163
+ if (model.api === "openai-responses"
164
+ && provider.openAIResponsesStreamCompletionMode === "terminal-event") {
165
+ // [喵喵喵]: 某些中转已发出正式终态事件却保持连接;完整转交该 frame 后即可按协议结束本地流。
166
+ return wrapResponsesTerminalStream(response);
72
167
  }
168
+ return response;
73
169
  };
74
170
  return stream(upstreamModel, context, { ...options, fetch: transportFetch });
75
171
  });
package/state-document.ts CHANGED
@@ -90,6 +90,7 @@ export function createProviderDraft(api: ApiKind = "openai-responses"): Provider
90
90
  customClientHeaders: {},
91
91
  httpProxyEnabled: false,
92
92
  httpProxyUrl: DEFAULT_PROVIDER_HTTP_PROXY_URL,
93
+ openAIResponsesStreamCompletionMode: "standard",
93
94
  selectedIndex: 0,
94
95
  };
95
96
  }
@@ -110,6 +111,7 @@ export function createProviderDraftFromStored(providerId: string, stored: Stored
110
111
  customClientHeaders: cloneStringRecord(stored.customClientHeaders),
111
112
  httpProxyEnabled: stored.httpProxyEnabled ?? false,
112
113
  httpProxyUrl: stored.httpProxyUrl?.trim() || DEFAULT_PROVIDER_HTTP_PROXY_URL,
114
+ openAIResponsesStreamCompletionMode: stored.openAIResponsesStreamCompletionMode ?? "standard",
113
115
  selectedIndex: 0,
114
116
  };
115
117
  }
@@ -128,6 +130,7 @@ function createModelDraftFromProvider(providerDraft: ProviderDraft): ModelDraft
128
130
  customClientHeaders: cloneStringRecord(providerDraft.customClientHeaders),
129
131
  httpProxyEnabled: providerDraft.httpProxyEnabled,
130
132
  httpProxyUrl: providerDraft.httpProxyUrl,
133
+
131
134
  modelId: "",
132
135
  modelName: "",
133
136
  // 新建模型默认开启视觉输入;不支持图片的模型可在编辑器里关闭。
@@ -171,6 +174,7 @@ export function createModelDraftFromStoredModel(
171
174
  customClientHeaders: cloneStringRecord(stored.customClientHeaders),
172
175
  httpProxyEnabled: stored.httpProxyEnabled ?? false,
173
176
  httpProxyUrl: stored.httpProxyUrl?.trim() || DEFAULT_PROVIDER_HTTP_PROXY_URL,
177
+
174
178
  modelId: model.id,
175
179
  modelName: model.name ?? "",
176
180
  inputKinds: [...(model.input ?? preset.inputKinds)],
@@ -287,6 +291,10 @@ export function getProviderChangeWarnings(
287
291
  && getOpenAIChatCompatibilityMode(current.compat) !== (draft.openAIChatCompatibilityMode ?? "standard")) {
288
292
  changes.push(t("协议兼容"));
289
293
  }
294
+ if (draft.api === "openai-responses"
295
+ && (current.openAIResponsesStreamCompletionMode ?? "standard") !== (draft.openAIResponsesStreamCompletionMode ?? "standard")) {
296
+ changes.push(t("流结束兼容"));
297
+ }
290
298
  if ((current.clientHeaderProfile ?? "recommended") !== draft.clientHeaderProfile
291
299
  || (current.requestHeaderProfileId ?? "") !== (draft.requestHeaderProfileId ?? ""))
292
300
  changes.push(t("请求头"));
@@ -362,6 +370,11 @@ function buildProviderFromDraft(
362
370
  else delete next.httpProxyEnabled;
363
371
  if (draft.httpProxyEnabled || httpProxyUrl !== DEFAULT_PROVIDER_HTTP_PROXY_URL) next.httpProxyUrl = httpProxyUrl;
364
372
  else delete next.httpProxyUrl;
373
+ if (draft.api === "openai-responses" && draft.openAIResponsesStreamCompletionMode === "terminal-event") {
374
+ next.openAIResponsesStreamCompletionMode = "terminal-event";
375
+ } else {
376
+ delete next.openAIResponsesStreamCompletionMode;
377
+ }
365
378
  if (draft.clientHeaderProfile === "custom" && draft.requestHeaderProfileId?.trim()) {
366
379
  next.requestHeaderProfileId = draft.requestHeaderProfileId.trim();
367
380
  } else {
@@ -22,6 +22,7 @@ import type {
22
22
  StoredModel,
23
23
  StoredProvider,
24
24
  StoredRequestHeaderProfile,
25
+ OpenAIResponsesStreamCompletionMode,
25
26
  ThinkingLevelMap,
26
27
  TokenCost,
27
28
  } from "./types.ts";
@@ -44,6 +45,7 @@ interface ProviderMetadata {
44
45
  customClientHeaders?: Record<string, string>;
45
46
  httpProxyEnabled?: boolean;
46
47
  httpProxyUrl?: string;
48
+ openAIResponsesStreamCompletionMode?: OpenAIResponsesStreamCompletionMode;
47
49
  anthropicApiRoot?: boolean;
48
50
  }
49
51
 
@@ -158,6 +160,19 @@ function readOptionalOpenAIServiceTier(record: Record<string, unknown>, key: str
158
160
  return value as OpenAIServiceTier;
159
161
  }
160
162
 
163
+ function readOptionalOpenAIResponsesStreamCompletionMode(
164
+ record: Record<string, unknown>,
165
+ key: string,
166
+ path: string,
167
+ ): OpenAIResponsesStreamCompletionMode | undefined {
168
+ const value = readOptionalString(record, key, path);
169
+ if (value === undefined) return undefined;
170
+ if (value !== "standard" && value !== "terminal-event") {
171
+ fail(`${path}.${key}`, t("未知 Responses 流结束模式:{value}", { value }));
172
+ }
173
+ return value;
174
+ }
175
+
161
176
  function readInputKinds(record: Record<string, unknown>, key: string, path: string): ModelInputKind[] {
162
177
  const value = record[key];
163
178
  if (!Array.isArray(value) || value.length === 0) fail(`${path}.${key}`, t("必须是非空数组"));
@@ -239,6 +254,8 @@ function readProviderMetadata(raw: unknown, path: string): ProviderMetadata {
239
254
  if (httpProxyEnabled !== undefined) metadata.httpProxyEnabled = httpProxyEnabled;
240
255
  const httpProxyUrl = readOptionalString(raw, "httpProxyUrl", path);
241
256
  if (httpProxyUrl !== undefined) metadata.httpProxyUrl = httpProxyUrl;
257
+ const streamCompletionMode = readOptionalOpenAIResponsesStreamCompletionMode(raw, "openAIResponsesStreamCompletionMode", path);
258
+ if (streamCompletionMode !== undefined) metadata.openAIResponsesStreamCompletionMode = streamCompletionMode;
242
259
  return metadata;
243
260
  }
244
261
 
@@ -308,6 +325,8 @@ function readStoredProvider(raw: unknown, path: string, managed: boolean): Store
308
325
  if (httpProxyEnabled !== undefined) provider.httpProxyEnabled = httpProxyEnabled;
309
326
  const httpProxyUrl = readOptionalString(raw, "httpProxyUrl", path);
310
327
  if (httpProxyUrl !== undefined) provider.httpProxyUrl = httpProxyUrl;
328
+ const streamCompletionMode = readOptionalOpenAIResponsesStreamCompletionMode(raw, "openAIResponsesStreamCompletionMode", path);
329
+ if (streamCompletionMode !== undefined) provider.openAIResponsesStreamCompletionMode = streamCompletionMode;
311
330
  return provider;
312
331
  }
313
332
 
@@ -381,6 +400,9 @@ function extractProviderMetadata(provider: StoredProvider): ProviderMetadata {
381
400
  if (provider.httpProxyEnabled) metadata.httpProxyEnabled = true;
382
401
  const httpProxyUrl = provider.httpProxyUrl?.trim();
383
402
  if (httpProxyUrl) metadata.httpProxyUrl = httpProxyUrl;
403
+ if (provider.openAIResponsesStreamCompletionMode === "terminal-event") {
404
+ metadata.openAIResponsesStreamCompletionMode = "terminal-event";
405
+ }
384
406
  return metadata;
385
407
  }
386
408
 
package/state-store.ts CHANGED
@@ -229,6 +229,9 @@ async function buildStoredProviderFromModelsJson(
229
229
  }
230
230
  if (managed && providerMetadata?.httpProxyEnabled !== undefined) provider.httpProxyEnabled = providerMetadata.httpProxyEnabled;
231
231
  if (managed && providerMetadata?.httpProxyUrl !== undefined) provider.httpProxyUrl = providerMetadata.httpProxyUrl;
232
+ if (managed && providerMetadata?.openAIResponsesStreamCompletionMode !== undefined) {
233
+ provider.openAIResponsesStreamCompletionMode = providerMetadata.openAIResponsesStreamCompletionMode;
234
+ }
232
235
  return provider;
233
236
  }
234
237
 
@@ -9,7 +9,11 @@ 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, type OpenAIChatCompatibilityMode } from "../types.ts";
12
+ import {
13
+ DEFAULT_PROVIDER_HTTP_PROXY_URL,
14
+ type OpenAIChatCompatibilityMode,
15
+ type OpenAIResponsesStreamCompletionMode,
16
+ } from "../types.ts";
13
17
  import { showOptionPicker, showPersistentFormMenu, padLabel, type HorizontalDirection, type MenuCursor } from "./persistent-menu.ts";
14
18
  import {
15
19
  describeProfile,
@@ -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>,
@@ -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,
@@ -251,6 +304,9 @@ export async function editProvider(
251
304
  if (draft.api === "openai-completions") {
252
305
  summaryLines.push(getOpenAIChatCompatibilityModeHint(getOpenAIChatCompatibilityMode(draft)));
253
306
  }
307
+ if (draft.api === "openai-responses") {
308
+ summaryLines.push(getOpenAIResponsesStreamCompletionModeHint(getOpenAIResponsesStreamCompletionMode(draft)));
309
+ }
254
310
  return summaryLines;
255
311
  };
256
312
  const action = await showPersistentFormMenu(
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;