codexmate 0.0.30 → 0.0.32

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.
Files changed (144) hide show
  1. package/README.md +363 -421
  2. package/README.zh.md +371 -354
  3. package/cli/agents-files.js +224 -224
  4. package/cli/archive-helpers.js +446 -446
  5. package/cli/auth-profiles.js +375 -375
  6. package/cli/builtin-proxy.js +1725 -1725
  7. package/cli/claude-proxy.js +1022 -1022
  8. package/cli/config-bootstrap.js +402 -402
  9. package/cli/config-health.js +454 -454
  10. package/cli/doctor-core.js +903 -903
  11. package/cli/import-skills-url.js +356 -356
  12. package/cli/local-bridge.js +324 -324
  13. package/cli/openai-bridge.js +1653 -1653
  14. package/cli/openclaw-config.js +629 -629
  15. package/cli/session-convert-args.js +69 -65
  16. package/cli/session-convert-io.js +82 -82
  17. package/cli/session-convert.js +150 -43
  18. package/cli/session-usage.concurrent.js +28 -28
  19. package/cli/session-usage.js +118 -118
  20. package/cli/session-usage.models.js +176 -176
  21. package/cli/skills.js +1141 -1141
  22. package/cli/zip-commands.js +510 -510
  23. package/cli.js +15829 -15481
  24. package/lib/automation.js +404 -404
  25. package/lib/cli-file-utils.js +151 -151
  26. package/lib/cli-models-utils.js +440 -440
  27. package/lib/cli-network-utils.js +190 -190
  28. package/lib/cli-path-utils.js +85 -85
  29. package/lib/cli-session-utils.js +121 -121
  30. package/lib/cli-sessions.js +426 -417
  31. package/lib/cli-utils.js +155 -155
  32. package/lib/cli-webhook.js +126 -126
  33. package/lib/download-artifacts.js +92 -92
  34. package/lib/mcp-stdio.js +453 -453
  35. package/lib/task-orchestrator.js +869 -869
  36. package/lib/text-diff.js +303 -303
  37. package/lib/workflow-engine.js +340 -340
  38. package/package.json +76 -76
  39. package/plugins/README.md +20 -20
  40. package/plugins/README.zh-CN.md +20 -20
  41. package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
  42. package/plugins/prompt-templates/computed.mjs +253 -253
  43. package/plugins/prompt-templates/index.mjs +8 -8
  44. package/plugins/prompt-templates/manifest.mjs +15 -15
  45. package/plugins/prompt-templates/methods.mjs +553 -553
  46. package/plugins/prompt-templates/overview.mjs +91 -91
  47. package/plugins/prompt-templates/ownership.mjs +19 -19
  48. package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
  49. package/plugins/prompt-templates/storage.mjs +64 -64
  50. package/plugins/registry.mjs +16 -16
  51. package/web-ui/app.js +647 -645
  52. package/web-ui/index.html +36 -36
  53. package/web-ui/logic.agents-diff.mjs +386 -386
  54. package/web-ui/logic.claude.mjs +168 -168
  55. package/web-ui/logic.codex.mjs +69 -69
  56. package/web-ui/logic.mjs +5 -5
  57. package/web-ui/logic.runtime.mjs +128 -128
  58. package/web-ui/logic.session-convert.mjs +70 -70
  59. package/web-ui/logic.sessions.mjs +781 -765
  60. package/web-ui/modules/api.mjs +90 -90
  61. package/web-ui/modules/app.computed.dashboard.mjs +248 -248
  62. package/web-ui/modules/app.computed.index.mjs +17 -17
  63. package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
  64. package/web-ui/modules/app.computed.session.mjs +693 -999
  65. package/web-ui/modules/app.constants.mjs +15 -15
  66. package/web-ui/modules/app.methods.agents.mjs +651 -632
  67. package/web-ui/modules/app.methods.claude-config.mjs +200 -200
  68. package/web-ui/modules/app.methods.codex-config.mjs +861 -917
  69. package/web-ui/modules/app.methods.index.mjs +94 -94
  70. package/web-ui/modules/app.methods.install.mjs +205 -205
  71. package/web-ui/modules/app.methods.navigation.mjs +774 -774
  72. package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
  73. package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
  74. package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
  75. package/web-ui/modules/app.methods.providers.mjs +529 -529
  76. package/web-ui/modules/app.methods.runtime.mjs +345 -345
  77. package/web-ui/modules/app.methods.session-actions.mjs +591 -593
  78. package/web-ui/modules/app.methods.session-browser.mjs +1012 -984
  79. package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
  80. package/web-ui/modules/app.methods.session-trash.mjs +438 -438
  81. package/web-ui/modules/app.methods.startup-claude.mjs +537 -534
  82. package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
  83. package/web-ui/modules/app.methods.webhook.mjs +79 -79
  84. package/web-ui/modules/config-mode.computed.mjs +124 -124
  85. package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
  86. package/web-ui/modules/i18n.dict.mjs +3177 -3146
  87. package/web-ui/modules/i18n.mjs +62 -62
  88. package/web-ui/modules/plugins.computed.mjs +3 -3
  89. package/web-ui/modules/plugins.methods.mjs +3 -3
  90. package/web-ui/modules/plugins.storage.mjs +11 -11
  91. package/web-ui/modules/provider-url-display.mjs +17 -17
  92. package/web-ui/modules/sessions-filters-url.mjs +85 -85
  93. package/web-ui/modules/skills.computed.mjs +107 -107
  94. package/web-ui/modules/skills.methods.mjs +482 -482
  95. package/web-ui/partials/index/layout-footer.html +13 -13
  96. package/web-ui/partials/index/layout-header.html +503 -500
  97. package/web-ui/partials/index/modal-config-template-agents.html +185 -174
  98. package/web-ui/partials/index/modal-confirm-toast.html +32 -32
  99. package/web-ui/partials/index/modal-health-check.html +45 -45
  100. package/web-ui/partials/index/modal-openclaw-config.html +280 -280
  101. package/web-ui/partials/index/modal-skills.html +200 -200
  102. package/web-ui/partials/index/modals-basic.html +162 -162
  103. package/web-ui/partials/index/panel-config-claude.html +136 -194
  104. package/web-ui/partials/index/panel-config-codex.html +197 -337
  105. package/web-ui/partials/index/panel-config-openclaw.html +83 -83
  106. package/web-ui/partials/index/panel-dashboard.html +219 -219
  107. package/web-ui/partials/index/panel-docs.html +115 -147
  108. package/web-ui/partials/index/panel-market.html +177 -177
  109. package/web-ui/partials/index/panel-orchestration.html +391 -391
  110. package/web-ui/partials/index/panel-plugins.html +253 -253
  111. package/web-ui/partials/index/panel-sessions.html +313 -308
  112. package/web-ui/partials/index/panel-settings.html +190 -190
  113. package/web-ui/partials/index/panel-trash.html +83 -88
  114. package/web-ui/partials/index/panel-usage.html +138 -371
  115. package/web-ui/res/json5.min.js +1 -1
  116. package/web-ui/res/vue.global.prod.js +13 -13
  117. package/web-ui/session-helpers.mjs +591 -594
  118. package/web-ui/source-bundle.cjs +233 -233
  119. package/web-ui/styles/base-theme.css +281 -281
  120. package/web-ui/styles/bridge-pool.css +197 -0
  121. package/web-ui/styles/controls-forms.css +433 -422
  122. package/web-ui/styles/dashboard.css +406 -406
  123. package/web-ui/styles/docs-panel.css +245 -271
  124. package/web-ui/styles/feedback.css +108 -108
  125. package/web-ui/styles/health-check-dialog.css +144 -144
  126. package/web-ui/styles/layout-shell.css +638 -626
  127. package/web-ui/styles/modals-core.css +466 -466
  128. package/web-ui/styles/navigation-panels.css +391 -391
  129. package/web-ui/styles/openclaw-structured.css +266 -266
  130. package/web-ui/styles/plugins-panel.css +564 -564
  131. package/web-ui/styles/responsive.css +392 -454
  132. package/web-ui/styles/sessions-list.css +647 -417
  133. package/web-ui/styles/sessions-preview.css +407 -407
  134. package/web-ui/styles/sessions-toolbar-trash.css +518 -334
  135. package/web-ui/styles/sessions-usage.css +588 -1040
  136. package/web-ui/styles/settings-panel.css +349 -349
  137. package/web-ui/styles/skills-list.css +305 -305
  138. package/web-ui/styles/skills-market.css +429 -429
  139. package/web-ui/styles/task-orchestration.css +822 -822
  140. package/web-ui/styles/titles-cards.css +472 -472
  141. package/web-ui/styles/trash-panel.css +90 -90
  142. package/web-ui/styles/webhook.css +81 -81
  143. package/web-ui/styles.css +24 -23
  144. package/web-ui.html +17 -17
@@ -1,1653 +1,1653 @@
1
- const http = require('http');
2
- const https = require('https');
3
- const crypto = require('crypto');
4
- const { StringDecoder } = require('string_decoder');
5
- const { readJsonFile, writeJsonAtomic } = require('../lib/cli-file-utils');
6
- const { isValidHttpUrl, normalizeBaseUrl, joinApiUrl } = require('../lib/cli-utils');
7
-
8
- const DEFAULT_BRIDGE_TOKEN = 'codexmate';
9
- const SETTINGS_VERSION = 1;
10
- // 推理模型 reasoning 阶段可能长时间无字节输出,需匹配 codex 的 stream_idle_timeout_ms=300000。
11
- const STREAM_IDLE_TIMEOUT_MS = 5 * 60 * 1000;
12
- const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
13
- const RESPONSES_UNSUPPORTED_TTL_MS = 30 * 60 * 1000;
14
-
15
- function normalizeText(value) {
16
- return typeof value === 'string' ? value.trim() : '';
17
- }
18
-
19
- function normalizeProviderName(value) {
20
- // Provider name validation is done elsewhere; keep this conservative.
21
- return normalizeText(value);
22
- }
23
-
24
- function normalizeOpenaiUpstreamBaseUrl(rawValue) {
25
- const normalized = normalizeBaseUrl(rawValue);
26
- if (!normalized) return '';
27
- try {
28
- const parsed = new URL(normalized);
29
- let pathname = String(parsed.pathname || '').replace(/\/+$/g, '');
30
-
31
- // If user accidentally pasted a full endpoint, strip it back to the base URL.
32
- // Keep direct provider routes (e.g. /project/ym) intact.
33
- pathname = pathname
34
- .replace(/\/v1\/chat\/completions$/i, '/v1')
35
- .replace(/\/chat\/completions$/i, '')
36
- .replace(/\/v1\/responses$/i, '/v1')
37
- .replace(/\/responses$/i, '')
38
- .replace(/\/v1\/models$/i, '/v1')
39
- .replace(/\/models$/i, '');
40
-
41
- // Normalize empty/root path.
42
- if (pathname === '/') pathname = '';
43
-
44
- const rebuilt = `${parsed.origin}${pathname}`;
45
- return normalizeBaseUrl(rebuilt);
46
- } catch (_) {
47
- return normalized;
48
- }
49
- }
50
-
51
- function normalizeUpstreamEntry(entry) {
52
- if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
53
- return null;
54
- }
55
- const baseUrl = normalizeOpenaiUpstreamBaseUrl(entry.baseUrl || entry.base_url || '');
56
- const apiKey = normalizeText(entry.apiKey || entry.api_key || entry.key || '');
57
- const headersRaw = entry.headers || entry.extraHeaders || entry.extra_headers || null;
58
- const headers = normalizeHeadersMap(headersRaw);
59
- if (!baseUrl || !isValidHttpUrl(baseUrl)) {
60
- return null;
61
- }
62
- return { baseUrl, apiKey, headers };
63
- }
64
-
65
- function normalizeHeadersMap(value) {
66
- if (!value || typeof value !== 'object' || Array.isArray(value)) {
67
- return {};
68
- }
69
- const forbidden = new Set([
70
- 'authorization',
71
- 'host',
72
- 'content-length',
73
- 'connection',
74
- 'transfer-encoding',
75
- 'keep-alive',
76
- 'proxy-authenticate',
77
- 'proxy-authorization',
78
- 'te',
79
- 'trailer',
80
- 'upgrade'
81
- ]);
82
- const result = {};
83
- for (const [rawKey, rawVal] of Object.entries(value)) {
84
- const key = typeof rawKey === 'string' ? rawKey.trim() : '';
85
- if (!key) continue;
86
- const lower = key.toLowerCase();
87
- if (forbidden.has(lower)) continue;
88
- if (typeof rawVal !== 'string') continue;
89
- result[key] = rawVal;
90
- }
91
- return result;
92
- }
93
-
94
- function readOpenaiBridgeSettings(filePath) {
95
- const parsed = readJsonFile(filePath, null);
96
- const providers = parsed && typeof parsed === 'object' && !Array.isArray(parsed)
97
- ? parsed.providers
98
- : null;
99
- const providerMap = providers && typeof providers === 'object' && !Array.isArray(providers)
100
- ? providers
101
- : {};
102
- return {
103
- version: SETTINGS_VERSION,
104
- providers: providerMap
105
- };
106
- }
107
-
108
- function upsertOpenaiBridgeProvider(filePath, providerName, upstreamBaseUrl, apiKey, headers) {
109
- const name = normalizeProviderName(providerName);
110
- const baseUrl = normalizeOpenaiUpstreamBaseUrl(upstreamBaseUrl);
111
- const key = normalizeText(apiKey);
112
- const nextHeaders = normalizeHeadersMap(headers);
113
-
114
- if (!name) {
115
- return { error: 'Provider name is required' };
116
- }
117
- if (!baseUrl || !isValidHttpUrl(baseUrl)) {
118
- return { error: 'Upstream base URL is invalid' };
119
- }
120
-
121
- const settings = readOpenaiBridgeSettings(filePath);
122
- const existing = settings && settings.providers ? settings.providers[name] : null;
123
- const existingHeaders = existing && typeof existing === 'object' && !Array.isArray(existing)
124
- ? normalizeHeadersMap(existing.headers || existing.extraHeaders || existing.extra_headers || null)
125
- : {};
126
- const next = {
127
- version: SETTINGS_VERSION,
128
- providers: {
129
- ...(settings.providers || {}),
130
- [name]: {
131
- baseUrl,
132
- apiKey: key,
133
- headers: Object.keys(nextHeaders).length ? nextHeaders : existingHeaders
134
- }
135
- }
136
- };
137
- writeJsonAtomic(filePath, next);
138
- return { success: true };
139
- }
140
-
141
- function resolveOpenaiBridgeUpstream(filePath, providerName) {
142
- const name = normalizeProviderName(providerName);
143
- if (!name) return { error: 'Provider name is required' };
144
- const settings = readOpenaiBridgeSettings(filePath);
145
- const entry = settings.providers ? settings.providers[name] : null;
146
- const normalized = normalizeUpstreamEntry(entry);
147
- if (!normalized) {
148
- return { error: `OpenAI 转换未配置: ${name}` };
149
- }
150
- return { provider: name, ...normalized };
151
- }
152
-
153
- function extractAuthorizationToken(req) {
154
- const header = typeof req.headers.authorization === 'string' ? req.headers.authorization.trim() : '';
155
- if (!header) return '';
156
- if (/^bearer\s+/i.test(header)) {
157
- return header.replace(/^bearer\s+/i, '').trim();
158
- }
159
- return header;
160
- }
161
-
162
- function readRequestBody(req, maxBytes) {
163
- return new Promise((resolve) => {
164
- let body = '';
165
- let size = 0;
166
- let aborted = false;
167
- req.on('data', (chunk) => {
168
- if (aborted) return;
169
- size += chunk.length;
170
- if (Number.isFinite(maxBytes) && maxBytes > 0 && size > maxBytes) {
171
- aborted = true;
172
- try { req.destroy(); } catch (_) {}
173
- resolve({ error: '请求体过大' });
174
- return;
175
- }
176
- body += chunk;
177
- });
178
- req.on('end', () => {
179
- if (aborted) return;
180
- resolve({ body });
181
- });
182
- req.on('error', (err) => resolve({ error: err && err.message ? err.message : 'request failed' }));
183
- });
184
- }
185
-
186
- function parseJsonOrError(text) {
187
- if (typeof text !== 'string' || !text.trim()) {
188
- return { value: null, error: 'empty body' };
189
- }
190
- try {
191
- return { value: JSON.parse(text), error: '' };
192
- } catch (e) {
193
- return { value: null, error: e && e.message ? e.message : 'invalid json' };
194
- }
195
- }
196
-
197
- function extractChatCompletionResult(payload) {
198
- if (!payload || typeof payload !== 'object') return { text: '', toolCalls: [] };
199
- const choice = Array.isArray(payload.choices) ? payload.choices[0] : null;
200
- const message = choice && typeof choice === 'object' ? choice.message : null;
201
- const toolCalls = message && typeof message === 'object' && Array.isArray(message.tool_calls)
202
- ? message.tool_calls
203
- : [];
204
- const content = message && typeof message === 'object' ? message.content : '';
205
- let text = '';
206
- if (typeof content === 'string') {
207
- text = content;
208
- } else if (Array.isArray(content)) {
209
- text = content
210
- .map((item) => {
211
- if (!item) return '';
212
- if (typeof item === 'string') return item;
213
- if (typeof item === 'object') {
214
- if (typeof item.text === 'string') return item.text;
215
- if (typeof item.content === 'string') return item.content;
216
- }
217
- return '';
218
- })
219
- .filter(Boolean)
220
- .join('');
221
- }
222
- return { text, toolCalls };
223
- }
224
-
225
- function normalizeResponsesInputToChatMessages(input) {
226
- // 支持:
227
- // - string
228
- // - { role, content }(单条 message)
229
- // - { type:"input_text"|"input_image", ... }(单个 block)
230
- // - [{ role, content: [...] }](messages array)
231
- // - [{ type:"input_text"|"input_image", ... }](blocks array -> 单条 user 消息)
232
- if (typeof input === 'string') {
233
- return [{ role: 'user', content: input }];
234
- }
235
-
236
- const toChatContent = (blocks) => {
237
- if (!Array.isArray(blocks)) return '';
238
- const out = [];
239
- for (const block of blocks) {
240
- if (!block || typeof block !== 'object') continue;
241
- const type = typeof block.type === 'string' ? block.type : '';
242
- if ((type === 'input_text' || type === 'output_text') && typeof block.text === 'string') {
243
- out.push({ type: 'text', text: block.text });
244
- continue;
245
- }
246
- if ((type === 'reasoning' || type === 'reasoning_text' || type === 'reasoning_content') && typeof block.text === 'string') {
247
- out.push({ type: 'text', text: block.text });
248
- continue;
249
- }
250
- if (type === 'input_image') {
251
- const raw = block.image_url != null ? block.image_url : block.imageUrl;
252
- const url = typeof raw === 'string'
253
- ? raw
254
- : (raw && typeof raw === 'object' && typeof raw.url === 'string' ? raw.url : '');
255
- if (url) {
256
- out.push({ type: 'image_url', image_url: { url } });
257
- }
258
- continue;
259
- }
260
- // 容错:兼容已是 chat content 的 {type:"text"} / {type:"image_url"}
261
- if (type === 'text' && typeof block.text === 'string') {
262
- out.push({ type: 'text', text: block.text });
263
- continue;
264
- }
265
- if (type === 'image_url' && block.image_url) {
266
- out.push({ type: 'image_url', image_url: block.image_url });
267
- continue;
268
- }
269
- const text = typeof block.text === 'string'
270
- ? block.text
271
- : (typeof block.content === 'string' ? block.content : '');
272
- if (text) {
273
- out.push({ type: 'text', text });
274
- continue;
275
- }
276
- try {
277
- const raw = JSON.stringify(block);
278
- if (raw) {
279
- out.push({ type: 'text', text: raw.slice(0, 4000) });
280
- }
281
- } catch (_) {}
282
- }
283
- if (out.length === 0) return '';
284
- return out;
285
- };
286
-
287
- const toRole = (value) => {
288
- const roleRaw = typeof value === 'string' ? value.trim().toLowerCase() : '';
289
- if (roleRaw === 'assistant') return 'assistant';
290
- // codex 把 AGENTS.md 注入 developer 角色;Responses 的 developer 在 chat 侧等价于 system。
291
- if (roleRaw === 'system' || roleRaw === 'developer') return 'system';
292
- return 'user';
293
- };
294
-
295
- if (input && typeof input === 'object' && !Array.isArray(input)) {
296
- if (typeof input.role === 'string' && input.content != null) {
297
- const role = toRole(input.role);
298
- const content = Array.isArray(input.content)
299
- ? toChatContent(input.content)
300
- : input.content;
301
- return content ? [{ role, content }] : [];
302
- }
303
- if (typeof input.type === 'string') {
304
- const content = toChatContent([input]);
305
- return content ? [{ role: 'user', content }] : [];
306
- }
307
- return [];
308
- }
309
-
310
- if (!Array.isArray(input)) {
311
- return [];
312
- }
313
-
314
- const messages = [];
315
- for (const item of input) {
316
- if (!item || typeof item !== 'object') continue;
317
-
318
- // Tool calls (Responses): { type: "function_call", call_id, name, arguments }
319
- // Chat Completions equivalent: assistant message with tool_calls
320
- if (typeof item.type === 'string' && item.type === 'function_call') {
321
- const callId = typeof item.call_id === 'string' ? item.call_id.trim() : '';
322
- const name = typeof item.name === 'string' ? item.name.trim() : '';
323
- const args = typeof item.arguments === 'string' ? item.arguments : '';
324
- if (callId && name) {
325
- messages.push({
326
- role: 'assistant',
327
- tool_calls: [{
328
- id: callId,
329
- type: 'function',
330
- function: { name, arguments: args || '' }
331
- }]
332
- });
333
- }
334
- continue;
335
- }
336
-
337
- // Tool results (Responses): { type: "function_call_output", call_id, output }
338
- // Chat Completions equivalent: { role: "tool", tool_call_id, content }
339
- if (typeof item.type === 'string' && item.type === 'function_call_output') {
340
- const toolCallId = typeof item.call_id === 'string' ? item.call_id.trim() : '';
341
- let content = item.output;
342
- if (typeof content !== 'string') {
343
- try {
344
- content = JSON.stringify(content);
345
- } catch (_) {
346
- content = String(content ?? '');
347
- }
348
- }
349
- if (toolCallId) {
350
- messages.push({ role: 'tool', tool_call_id: toolCallId, content: String(content || '') });
351
- }
352
- continue;
353
- }
354
-
355
- // message form
356
- if (typeof item.role === 'string' && item.content != null) {
357
- const role = toRole(item.role);
358
- const content = Array.isArray(item.content)
359
- ? toChatContent(item.content)
360
- : item.content;
361
- if (content) {
362
- messages.push({ role, content });
363
- }
364
- continue;
365
- }
366
- }
367
-
368
- if (messages.length > 0) {
369
- return messages;
370
- }
371
-
372
- // 退化:把 input array 当作单条 user content blocks
373
- const fallbackContent = toChatContent(input);
374
- if (fallbackContent) {
375
- return [{ role: 'user', content: fallbackContent }];
376
- }
377
- return [];
378
- }
379
-
380
-
381
- function normalizeResponsesToolsToChatTools(tools) {
382
- if (!Array.isArray(tools)) return tools;
383
- return tools
384
- .map((tool) => {
385
- if (!tool || typeof tool !== 'object') return null;
386
- if (tool.type !== 'function') return null;
387
- const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
388
- ? tool.function
389
- : {};
390
- const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
391
- ? sourceFn.name.trim()
392
- : (typeof tool.name === 'string' ? tool.name.trim() : '');
393
- if (!name) return null;
394
- const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
395
- ? sourceFn.parameters
396
- : (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : {});
397
- const fn = { name, parameters };
398
- const description = typeof sourceFn.description === 'string'
399
- ? sourceFn.description
400
- : (typeof tool.description === 'string' ? tool.description : undefined);
401
- const strict = typeof sourceFn.strict === 'boolean'
402
- ? sourceFn.strict
403
- : (typeof tool.strict === 'boolean' ? tool.strict : undefined);
404
- if (description !== undefined) fn.description = description;
405
- if (strict !== undefined) fn.strict = strict;
406
- return { type: 'function', function: fn };
407
- })
408
- .filter(Boolean);
409
- }
410
-
411
- function normalizeResponsesToolChoiceToChatToolChoice(toolChoice) {
412
- if (!toolChoice || typeof toolChoice !== 'object' || Array.isArray(toolChoice)) return toolChoice;
413
- if (toolChoice.type === 'function' && typeof toolChoice.name === 'string' && toolChoice.name.trim()) {
414
- return { type: 'function', function: { name: toolChoice.name.trim() } };
415
- }
416
- return toolChoice;
417
- }
418
-
419
- function normalizeResponsesToolsForResponsesApi(tools) {
420
- if (!Array.isArray(tools)) return tools;
421
- return tools
422
- .map((tool) => {
423
- if (!tool || typeof tool !== 'object') return null;
424
- if (tool.type !== 'function') return null;
425
- const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
426
- ? tool.function
427
- : {};
428
- const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
429
- ? sourceFn.name.trim()
430
- : (typeof tool.name === 'string' ? tool.name.trim() : '');
431
- if (!name) return null;
432
- const out = { type: 'function', name };
433
- const description = typeof sourceFn.description === 'string'
434
- ? sourceFn.description
435
- : (typeof tool.description === 'string' ? tool.description : undefined);
436
- const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
437
- ? sourceFn.parameters
438
- : (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : undefined);
439
- const strict = typeof sourceFn.strict === 'boolean'
440
- ? sourceFn.strict
441
- : (typeof tool.strict === 'boolean' ? tool.strict : undefined);
442
- if (description !== undefined) out.description = description;
443
- if (parameters !== undefined) out.parameters = parameters;
444
- if (strict !== undefined) out.strict = strict;
445
- return out;
446
- })
447
- .filter(Boolean);
448
- }
449
-
450
- function mergeLeadingSystemMessages(messages, leadingInstructions) {
451
- const segments = [];
452
- const seen = new Set();
453
- const pushSegment = (text) => {
454
- const trimmed = typeof text === 'string' ? text.trim() : '';
455
- if (!trimmed || seen.has(trimmed)) return;
456
- seen.add(trimmed);
457
- segments.push(trimmed);
458
- };
459
- if (typeof leadingInstructions === 'string') {
460
- pushSegment(leadingInstructions);
461
- }
462
- const rest = [];
463
- for (const msg of messages) {
464
- if (msg && msg.role === 'system') {
465
- const content = msg.content;
466
- if (typeof content === 'string') {
467
- pushSegment(content);
468
- } else if (Array.isArray(content)) {
469
- for (const part of content) {
470
- if (part && typeof part === 'object' && typeof part.text === 'string') {
471
- pushSegment(part.text);
472
- }
473
- }
474
- }
475
- continue;
476
- }
477
- rest.push(msg);
478
- }
479
- const out = [];
480
- if (segments.length) {
481
- out.push({ role: 'system', content: segments.join('\n\n---\n\n') });
482
- }
483
- for (const msg of rest) out.push(msg);
484
- return out;
485
- }
486
-
487
- function convertResponsesRequestToChatCompletions(payload) {
488
- const body = payload && typeof payload === 'object' ? payload : {};
489
- const model = typeof body.model === 'string' ? body.model.trim() : '';
490
- if (!model) {
491
- return { error: 'responses 请求缺少 model' };
492
- }
493
-
494
- const rawMessages = normalizeResponsesInputToChatMessages(body.input);
495
- // codex 同时下发 body.instructions(内置 prompt)与 input 内 developer/system 消息(AGENTS.md)。
496
- // 合流为一条领头 system,避免某些上游"只认第一条 system"导致 AGENTS.md 失效。
497
- const messages = mergeLeadingSystemMessages(rawMessages, body.instructions);
498
- if (!messages.length) {
499
- // codex sometimes sends empty input for probes; tolerate.
500
- messages.push({ role: 'user', content: '' });
501
- }
502
-
503
- const maxOutputTokens = Number.parseInt(String(body.max_output_tokens), 10);
504
- const stream = body.stream === true;
505
-
506
- const chat = {
507
- model,
508
- messages,
509
- stream: false,
510
- temperature: Number.isFinite(body.temperature) ? Number(body.temperature) : undefined,
511
- top_p: Number.isFinite(body.top_p) ? Number(body.top_p) : undefined,
512
- max_tokens: Number.isFinite(maxOutputTokens) && maxOutputTokens > 0 ? maxOutputTokens : undefined
513
- };
514
- if (Array.isArray(body.stop) && body.stop.length) {
515
- chat.stop = body.stop.filter((item) => typeof item === 'string' && item.trim());
516
- }
517
- if (Array.isArray(body.tools) && body.tools.length) {
518
- chat.tools = normalizeResponsesToolsToChatTools(body.tools);
519
- }
520
- if (body.tool_choice !== undefined) {
521
- chat.tool_choice = normalizeResponsesToolChoiceToChatToolChoice(body.tool_choice);
522
- }
523
- if (body.response_format !== undefined) {
524
- chat.response_format = body.response_format;
525
- }
526
- if (body.metadata !== undefined) {
527
- chat.metadata = body.metadata;
528
- }
529
-
530
- // Remove undefined keys
531
- Object.keys(chat).forEach((key) => chat[key] === undefined && delete chat[key]);
532
-
533
- return { chat, streamRequested: stream };
534
- }
535
-
536
- function buildResponsesPayloadFromChatResult(model, text, toolCalls, upstreamPayload) {
537
- const responseId = `resp_${crypto.randomBytes(10).toString('hex')}`;
538
- const usage = upstreamPayload && upstreamPayload.usage && typeof upstreamPayload.usage === 'object'
539
- ? upstreamPayload.usage
540
- : null;
541
- const createdAt = Math.floor(Date.now() / 1000);
542
- const output = [];
543
- const trimmedText = typeof text === 'string' ? text : '';
544
- if (trimmedText) {
545
- output.push({
546
- id: `msg_${crypto.randomBytes(8).toString('hex')}`,
547
- type: 'message',
548
- role: 'assistant',
549
- content: [{ type: 'output_text', text: trimmedText }]
550
- });
551
- }
552
-
553
- // Convert chat.completions tool_calls into Responses-style function_call output items.
554
- // This is important for Codex, which appends function_call + function_call_output back into `input`.
555
- if (Array.isArray(toolCalls)) {
556
- for (const call of toolCalls) {
557
- if (!call || typeof call !== 'object') continue;
558
- const callId = typeof call.id === 'string' && call.id.trim() ? call.id.trim() : `call_${crypto.randomBytes(8).toString('hex')}`;
559
- const fn = call.function && typeof call.function === 'object' ? call.function : {};
560
- const name = typeof fn.name === 'string' ? fn.name : '';
561
- const args = typeof fn.arguments === 'string' ? fn.arguments : '';
562
- if (!name) continue;
563
- output.push({
564
- type: 'function_call',
565
- call_id: callId,
566
- name,
567
- arguments: args
568
- });
569
- }
570
- }
571
-
572
- const payload = {
573
- id: responseId,
574
- object: 'response',
575
- model,
576
- created_at: createdAt,
577
- status: 'completed',
578
- output,
579
- output_text: trimmedText
580
- };
581
-
582
- if (usage) {
583
- // Map chat.completions usage -> responses usage shape when possible.
584
- const promptTokens = Number.isFinite(usage.prompt_tokens) ? Number(usage.prompt_tokens) : null;
585
- const completionTokens = Number.isFinite(usage.completion_tokens) ? Number(usage.completion_tokens) : null;
586
- const totalTokens = Number.isFinite(usage.total_tokens) ? Number(usage.total_tokens) : null;
587
- if (promptTokens !== null || completionTokens !== null || totalTokens !== null) {
588
- payload.usage = {
589
- input_tokens: promptTokens ?? undefined,
590
- output_tokens: completionTokens ?? undefined,
591
- total_tokens: totalTokens ?? undefined
592
- };
593
- Object.keys(payload.usage).forEach((key) => payload.usage[key] === undefined && delete payload.usage[key]);
594
- } else {
595
- payload.usage = usage;
596
- }
597
- }
598
-
599
- return payload;
600
- }
601
-
602
- function ensureResponseMetadata(response) {
603
- const payload = response && typeof response === 'object' ? response : {};
604
- if (typeof payload.object !== 'string' || !payload.object.trim()) {
605
- payload.object = 'response';
606
- }
607
- if (typeof payload.created_at !== 'number') {
608
- payload.created_at = Math.floor(Date.now() / 1000);
609
- }
610
- if (typeof payload.status !== 'string' || !payload.status.trim()) {
611
- payload.status = 'completed';
612
- }
613
- if (!Array.isArray(payload.output)) {
614
- payload.output = [];
615
- }
616
- return payload;
617
- }
618
-
619
- function sendResponsesSse(res, responsePayload) {
620
- const response = ensureResponseMetadata(responsePayload);
621
- const responseId = typeof response.id === 'string' && response.id.trim()
622
- ? response.id.trim()
623
- : `resp_${crypto.randomBytes(10).toString('hex')}`;
624
- const model = typeof response.model === 'string' ? response.model : '';
625
-
626
- let sequence = 0;
627
- const nextSeq = () => {
628
- sequence += 1;
629
- return sequence;
630
- };
631
-
632
- writeSse(res, 'response.created', {
633
- type: 'response.created',
634
- response: {
635
- id: responseId,
636
- model,
637
- created_at: response.created_at
638
- }
639
- });
640
-
641
- const output = Array.isArray(response.output) ? response.output : [];
642
- for (let outputIndex = 0; outputIndex < output.length; outputIndex += 1) {
643
- const item = output[outputIndex];
644
- if (!item || typeof item !== 'object') continue;
645
- const itemType = typeof item.type === 'string' ? item.type : '';
646
- const itemId = typeof item.id === 'string' && item.id.trim()
647
- ? item.id.trim()
648
- : (typeof item.call_id === 'string' && item.call_id.trim() ? item.call_id.trim() : `item_${crypto.randomBytes(8).toString('hex')}`);
649
-
650
- // Emit item added so Codex can anchor subsequent deltas by output_index/content_index/item_id.
651
- writeSse(res, 'response.output_item.added', {
652
- type: 'response.output_item.added',
653
- output_index: outputIndex,
654
- item: { ...item, id: itemId }
655
- });
656
-
657
- if (itemType === 'message') {
658
- const content = Array.isArray(item.content) ? item.content : [];
659
- for (let contentIndex = 0; contentIndex < content.length; contentIndex += 1) {
660
- const block = content[contentIndex];
661
- if (!block || typeof block !== 'object') continue;
662
- if (block.type !== 'output_text') continue;
663
- const text = typeof block.text === 'string' ? block.text : '';
664
- if (text) {
665
- writeSse(res, 'response.output_text.delta', {
666
- type: 'response.output_text.delta',
667
- item_id: itemId,
668
- output_index: outputIndex,
669
- content_index: contentIndex,
670
- delta: text,
671
- sequence_number: nextSeq()
672
- });
673
- }
674
- writeSse(res, 'response.output_text.done', {
675
- type: 'response.output_text.done',
676
- item_id: itemId,
677
- output_index: outputIndex,
678
- content_index: contentIndex,
679
- text,
680
- sequence_number: nextSeq()
681
- });
682
- }
683
- }
684
-
685
- // Emit item done for all item types (message/function_call/etc).
686
- writeSse(res, 'response.output_item.done', {
687
- type: 'response.output_item.done',
688
- output_index: outputIndex,
689
- item: { ...item, id: itemId },
690
- sequence_number: nextSeq()
691
- });
692
- }
693
-
694
- writeSse(res, 'response.completed', { type: 'response.completed', response });
695
- writeSse(res, 'done', '[DONE]');
696
- }
697
-
698
- function extractResponsesOutputText(payload) {
699
- if (!payload || typeof payload !== 'object') return '';
700
- const output = Array.isArray(payload.output) ? payload.output : [];
701
- for (const item of output) {
702
- if (!item || typeof item !== 'object') continue;
703
- if (item.type !== 'message') continue;
704
- const content = Array.isArray(item.content) ? item.content : [];
705
- for (const block of content) {
706
- if (!block || typeof block !== 'object') continue;
707
- if (block.type !== 'output_text') continue;
708
- if (typeof block.text === 'string') return block.text;
709
- }
710
- }
711
- if (typeof payload.output_text === 'string') return payload.output_text;
712
- return '';
713
- }
714
-
715
- function toUpstreamNonStreamingResponsesPayload(payload) {
716
- const body = payload && typeof payload === 'object' ? payload : {};
717
- const normalized = { ...body, stream: false };
718
- if (Array.isArray(body.tools)) {
719
- normalized.tools = normalizeResponsesToolsForResponsesApi(body.tools);
720
- }
721
- return normalized;
722
- }
723
-
724
- function shouldFallbackFromUpstreamResponses(status, bodyText) {
725
- if (!Number.isFinite(status)) return false;
726
- // Common "unsupported" status codes for a route.
727
- if (status === 404 || status === 405 || status === 501) return true;
728
-
729
- // Some OpenAI-compatible gateways respond with 500 + "not implemented" (e.g. convert_request_failed)
730
- // instead of 404/405 for unsupported endpoints. In that case we can safely fallback to chat/completions.
731
- const text = String(bodyText || '');
732
- if (!text) return false;
733
- if (/not implemented/i.test(text)) return true;
734
- if (/convert_request_failed/i.test(text)) return true;
735
- if (/unknown (endpoint|route)/i.test(text)) return true;
736
- if (/unsupported.*\/?v1\/responses/i.test(text)) return true;
737
- if (/does not support.*responses/i.test(text)) return true;
738
- if (/name['"`]?\s+is a required property/i.test(text) && /tools/i.test(text) && /function/i.test(text)) return true;
739
-
740
- // Best-effort parse for structured error codes.
741
- try {
742
- const parsed = JSON.parse(text);
743
- const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
744
- const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
745
- if (code === 'convert_request_failed') return true;
746
- if (/not implemented/i.test(msg)) return true;
747
- if (/unknown (endpoint|route)/i.test(msg)) return true;
748
- if (/unsupported.*\/?v1\/responses/i.test(msg)) return true;
749
- if (/does not support.*responses/i.test(msg)) return true;
750
- if (/name['"`]?\s+is a required property/i.test(msg) && /tools/i.test(msg) && /function/i.test(msg)) return true;
751
- } catch (_) {}
752
-
753
- return false;
754
- }
755
-
756
- // 仅识别"端点级别不支持"——可缓存,与 per-request 的 tool 格式错误区分。
757
- function isResponsesEndpointUnsupported(status, bodyText) {
758
- if (!Number.isFinite(status)) return false;
759
- if (status === 404 || status === 405 || status === 501) return true;
760
- const text = String(bodyText || '');
761
- if (!text) return false;
762
- if (/not implemented/i.test(text)) return true;
763
- if (/convert_request_failed/i.test(text)) return true;
764
- if (/unknown (endpoint|route)/i.test(text)) return true;
765
- if (/unsupported.*\/?v1\/responses/i.test(text)) return true;
766
- if (/does not support.*responses/i.test(text)) return true;
767
- try {
768
- const parsed = JSON.parse(text);
769
- const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
770
- const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
771
- if (code === 'convert_request_failed') return true;
772
- if (/not implemented/i.test(msg)) return true;
773
- if (/unknown (endpoint|route)/i.test(msg)) return true;
774
- if (/unsupported.*\/?v1\/responses/i.test(msg)) return true;
775
- if (/does not support.*responses/i.test(msg)) return true;
776
- } catch (_) {}
777
- return false;
778
- }
779
-
780
- function isLoopbackAddress(address) {
781
- if (!address) return false;
782
- const value = String(address);
783
- return value === '127.0.0.1' || value === '::1' || value === '::ffff:127.0.0.1';
784
- }
785
-
786
- function isTransientNetworkError(error) {
787
- const text = String(error || '').trim();
788
- if (!text) return false;
789
- if (/socket hang up/i.test(text)) return true;
790
- if (/ECONNRESET|ECONNREFUSED|EPIPE|EPROTO|ETIMEDOUT/i.test(text)) return true;
791
- if (/EAI_AGAIN/i.test(text)) return true;
792
- if (/UND_ERR_SOCKET/i.test(text)) return true;
793
- if (/disconnected before|secure tls|tls handshake/i.test(text)) return true;
794
- return false;
795
- }
796
-
797
- const TRANSIENT_RETRY_DELAYS_MS = [200, 600];
798
-
799
- async function retryTransientRequest(executor) {
800
- let lastResult = null;
801
- for (let attempt = 0; attempt <= TRANSIENT_RETRY_DELAYS_MS.length; attempt += 1) {
802
- if (attempt > 0) {
803
- const delay = TRANSIENT_RETRY_DELAYS_MS[attempt - 1];
804
- // eslint-disable-next-line no-await-in-loop
805
- await new Promise((r) => {
806
- const t = setTimeout(r, delay);
807
- if (typeof t.unref === 'function') t.unref();
808
- });
809
- }
810
- // eslint-disable-next-line no-await-in-loop
811
- const result = await executor(attempt);
812
- lastResult = result;
813
- if (!result) return result;
814
- if (result.ok) return result;
815
- if (result.retry) return result;
816
- if (result.status && result.status > 0) return result;
817
- if (!isTransientNetworkError(result.error)) return result;
818
- }
819
- return lastResult;
820
- }
821
-
822
- function writeSse(res, eventName, dataObj) {
823
- if (!res || res.writableEnded || res.destroyed) return;
824
- if (eventName) {
825
- res.write(`event: ${eventName}\n`);
826
- }
827
- if (dataObj === '[DONE]') {
828
- res.write('data: [DONE]\n\n');
829
- return;
830
- }
831
- res.write(`data: ${JSON.stringify(dataObj)}\n\n`);
832
- }
833
-
834
- function appendChatStreamToolCall(target, toolCall) {
835
- if (!toolCall || typeof toolCall !== 'object') return;
836
- const index = Number.isFinite(toolCall.index) ? toolCall.index : target.length;
837
- if (!target[index]) {
838
- target[index] = {
839
- id: '',
840
- type: 'function',
841
- function: { name: '', arguments: '' }
842
- };
843
- }
844
- const current = target[index];
845
- if (typeof toolCall.id === 'string' && toolCall.id) current.id = toolCall.id;
846
- if (typeof toolCall.type === 'string' && toolCall.type) current.type = toolCall.type;
847
- const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : null;
848
- if (fn) {
849
- if (typeof fn.name === 'string' && fn.name) current.function.name = fn.name;
850
- if (typeof fn.arguments === 'string') current.function.arguments += fn.arguments;
851
- }
852
- }
853
-
854
- function writeChatCompletionChunkAsResponsesSse(state, chunk) {
855
- if (!chunk || typeof chunk !== 'object') return;
856
- if (typeof chunk.model === 'string' && chunk.model) {
857
- state.model = chunk.model;
858
- }
859
- const choices = Array.isArray(chunk.choices) ? chunk.choices : [];
860
- for (const choice of choices) {
861
- const delta = choice && choice.delta && typeof choice.delta === 'object' ? choice.delta : null;
862
- if (!delta) continue;
863
-
864
- const segments = [];
865
- // DeepSeek-style OpenAI-compatible streams may emit private reasoning in
866
- // `reasoning_content` before the final answer. Responses `output_text`
867
- // must stay user-visible answer text only; forwarding reasoning here
868
- // pollutes Codex output and breaks exact-answer prompts.
869
- if (typeof delta.content === 'string' && delta.content) {
870
- segments.push(delta.content);
871
- }
872
- for (const seg of segments) {
873
- if (!state.messageItem) {
874
- state.messageItem = {
875
- id: `msg_${crypto.randomBytes(8).toString('hex')}`,
876
- type: 'message',
877
- role: 'assistant',
878
- content: [{ type: 'output_text', text: '' }]
879
- };
880
- state.output.push(state.messageItem);
881
- writeSse(state.res, 'response.output_item.added', {
882
- type: 'response.output_item.added',
883
- output_index: state.output.length - 1,
884
- item: state.messageItem
885
- });
886
- }
887
- state.messageText += seg;
888
- state.messageItem.content[0].text = state.messageText;
889
- writeSse(state.res, 'response.output_text.delta', {
890
- type: 'response.output_text.delta',
891
- item_id: state.messageItem.id,
892
- output_index: state.output.length - 1,
893
- content_index: 0,
894
- delta: seg,
895
- sequence_number: state.nextSeq()
896
- });
897
- }
898
-
899
- if (Array.isArray(delta.tool_calls)) {
900
- for (const toolCall of delta.tool_calls) {
901
- appendChatStreamToolCall(state.toolCalls, toolCall);
902
- }
903
- }
904
-
905
- if (typeof choice.finish_reason === 'string' && choice.finish_reason) {
906
- state.sawFinishReason = true;
907
- }
908
- }
909
- }
910
-
911
- function finishChatStreamResponsesSse(state) {
912
- if (!state || state.finished) return;
913
- state.finished = true;
914
-
915
- if (state.messageItem) {
916
- const outputIndex = state.output.indexOf(state.messageItem);
917
- writeSse(state.res, 'response.output_text.done', {
918
- type: 'response.output_text.done',
919
- item_id: state.messageItem.id,
920
- output_index: outputIndex,
921
- content_index: 0,
922
- text: state.messageText,
923
- sequence_number: state.nextSeq()
924
- });
925
- writeSse(state.res, 'response.output_item.done', {
926
- type: 'response.output_item.done',
927
- output_index: outputIndex,
928
- item: state.messageItem,
929
- sequence_number: state.nextSeq()
930
- });
931
- }
932
-
933
- for (const toolCall of state.toolCalls) {
934
- if (!toolCall) continue;
935
- const name = toolCall.function && typeof toolCall.function.name === 'string' ? toolCall.function.name : '';
936
- if (!name) continue;
937
- const item = {
938
- type: 'function_call',
939
- call_id: toolCall.id || `call_${crypto.randomBytes(8).toString('hex')}`,
940
- name,
941
- arguments: toolCall.function && typeof toolCall.function.arguments === 'string' ? toolCall.function.arguments : ''
942
- };
943
- const outputIndex = state.output.length;
944
- state.output.push(item);
945
- writeSse(state.res, 'response.output_item.added', {
946
- type: 'response.output_item.added',
947
- output_index: outputIndex,
948
- item
949
- });
950
- writeSse(state.res, 'response.output_item.done', {
951
- type: 'response.output_item.done',
952
- output_index: outputIndex,
953
- item,
954
- sequence_number: state.nextSeq()
955
- });
956
- }
957
-
958
- const response = ensureResponseMetadata({
959
- id: state.responseId,
960
- model: state.model,
961
- created_at: state.createdAt,
962
- status: 'completed',
963
- output: state.output,
964
- output_text: state.messageText
965
- });
966
- writeSse(state.res, 'response.completed', { type: 'response.completed', response });
967
- writeSse(state.res, 'done', '[DONE]');
968
- if (!state.res.writableEnded && !state.res.destroyed) {
969
- state.res.end();
970
- }
971
- }
972
-
973
- function failChatStreamResponsesSse(state, errorMessage) {
974
- if (!state || state.finished) return;
975
- state.finished = true;
976
- writeSse(state.res, 'response.failed', {
977
- type: 'response.failed',
978
- response: ensureResponseMetadata({
979
- id: state.responseId,
980
- model: state.model,
981
- created_at: state.createdAt,
982
- status: 'failed',
983
- output: state.output,
984
- output_text: state.messageText
985
- }),
986
- error: String(errorMessage || 'upstream stream failed')
987
- });
988
- writeSse(state.res, 'done', '[DONE]');
989
- if (!state.res.writableEnded && !state.res.destroyed) {
990
- state.res.end();
991
- }
992
- }
993
-
994
- function formatUpstreamStreamError(errorValue) {
995
- if (!errorValue) return 'upstream stream failed';
996
- if (typeof errorValue === 'string') return errorValue;
997
- if (typeof errorValue === 'object') {
998
- if (typeof errorValue.message === 'string' && errorValue.message) return errorValue.message;
999
- try { return JSON.stringify(errorValue); } catch (_) {}
1000
- }
1001
- return String(errorValue || 'upstream stream failed');
1002
- }
1003
-
1004
- function streamChatCompletionsAsResponsesSse(targetUrl, options = {}) {
1005
- const parsed = new URL(targetUrl);
1006
- const transport = parsed.protocol === 'https:' ? https : http;
1007
- const bodyText = options.body ? JSON.stringify(options.body) : '';
1008
- const maxBytes = Number.isFinite(options.maxBytes) && options.maxBytes > 0
1009
- ? Math.floor(options.maxBytes)
1010
- : 0;
1011
- const headers = {
1012
- 'Accept': 'text/event-stream',
1013
- ...(options.body ? { 'Content-Type': 'application/json' } : {}),
1014
- ...(options.headers || {})
1015
- };
1016
- if (options.body) {
1017
- headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
1018
- }
1019
- const timeoutMs = Number.isFinite(options.timeoutMs)
1020
- ? Math.max(1000, Number(options.timeoutMs))
1021
- : STREAM_IDLE_TIMEOUT_MS;
1022
- const res = options.res;
1023
- const fallbackModel = typeof options.model === 'string' ? options.model : '';
1024
-
1025
- return new Promise((resolve) => {
1026
- let settled = false;
1027
- let upstreamReq = null;
1028
- const finish = (value) => {
1029
- if (settled) return;
1030
- settled = true;
1031
- resolve(value);
1032
- };
1033
- const abortUpstream = () => {
1034
- if (upstreamReq) {
1035
- try { upstreamReq.destroy(new Error('client aborted')); } catch (_) {}
1036
- }
1037
- };
1038
- if (res && typeof res.once === 'function') {
1039
- res.once('close', abortUpstream);
1040
- }
1041
-
1042
- upstreamReq = transport.request({
1043
- protocol: parsed.protocol,
1044
- hostname: parsed.hostname,
1045
- port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
1046
- method: options.method || 'POST',
1047
- path: `${parsed.pathname}${parsed.search}`,
1048
- headers,
1049
- agent: parsed.protocol === 'https:' ? options.httpsAgent : options.httpAgent
1050
- }, (upstreamRes) => {
1051
- const status = upstreamRes.statusCode || 0;
1052
- const chunks = [];
1053
- let size = 0;
1054
- const contentType = String(upstreamRes.headers && upstreamRes.headers['content-type'] || '');
1055
-
1056
- const collectChunk = (chunk) => {
1057
- if (!chunk) return true;
1058
- if (maxBytes > 0) {
1059
- size += chunk.length;
1060
- if (size > maxBytes) {
1061
- chunks.length = 0;
1062
- try { upstreamRes.destroy(new Error('response too large')); } catch (_) {}
1063
- try { upstreamReq.destroy(new Error('response too large')); } catch (_) {}
1064
- finish({ ok: false, status, error: 'response too large' });
1065
- return false;
1066
- }
1067
- }
1068
- chunks.push(chunk);
1069
- return true;
1070
- };
1071
-
1072
- if (status >= 400) {
1073
- upstreamRes.on('data', collectChunk);
1074
- upstreamRes.on('end', () => finish({
1075
- ok: false,
1076
- status,
1077
- bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : ''
1078
- }));
1079
- return;
1080
- }
1081
-
1082
- if (!res.headersSent) {
1083
- res.writeHead(200, {
1084
- 'Content-Type': 'text/event-stream; charset=utf-8',
1085
- 'Cache-Control': 'no-cache',
1086
- 'Connection': 'keep-alive',
1087
- 'X-Accel-Buffering': 'no'
1088
- });
1089
- if (typeof res.flushHeaders === 'function') res.flushHeaders();
1090
- }
1091
-
1092
- if (!/text\/event-stream/i.test(contentType)) {
1093
- upstreamRes.on('data', collectChunk);
1094
- upstreamRes.on('end', () => {
1095
- const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
1096
- const parsedJson = parseJsonOrError(text);
1097
- if (parsedJson.error) {
1098
- writeSse(res, 'response.failed', { type: 'response.failed', error: `invalid upstream response: ${parsedJson.error}` });
1099
- writeSse(res, 'done', '[DONE]');
1100
- if (!res.writableEnded && !res.destroyed) res.end();
1101
- finish({ ok: true });
1102
- return;
1103
- }
1104
- const extracted = extractChatCompletionResult(parsedJson.value);
1105
- sendResponsesSse(res, buildResponsesPayloadFromChatResult(fallbackModel, extracted.text, extracted.toolCalls, parsedJson.value));
1106
- if (!res.writableEnded && !res.destroyed) res.end();
1107
- finish({ ok: true });
1108
- });
1109
- return;
1110
- }
1111
-
1112
- let sequence = 0;
1113
- const state = {
1114
- res,
1115
- responseId: `resp_${crypto.randomBytes(10).toString('hex')}`,
1116
- model: fallbackModel,
1117
- createdAt: Math.floor(Date.now() / 1000),
1118
- output: [],
1119
- messageItem: null,
1120
- messageText: '',
1121
- toolCalls: [],
1122
- finished: false,
1123
- sawDone: false,
1124
- sawFinishReason: false,
1125
- nextSeq: () => {
1126
- sequence += 1;
1127
- return sequence;
1128
- }
1129
- };
1130
- writeSse(res, 'response.created', {
1131
- type: 'response.created',
1132
- response: {
1133
- id: state.responseId,
1134
- model: state.model,
1135
- created_at: state.createdAt
1136
- }
1137
- });
1138
-
1139
- let buffer = '';
1140
- const utf8Decoder = new StringDecoder('utf8');
1141
- const handleEventBlock = (block) => {
1142
- const dataLines = String(block || '')
1143
- .split(/\r?\n/)
1144
- .filter((line) => line.startsWith('data:'))
1145
- .map((line) => line.slice(5).trimStart());
1146
- if (dataLines.length === 0) return;
1147
- const data = dataLines.join('\n').trim();
1148
- if (!data) return;
1149
- if (data === '[DONE]') {
1150
- state.sawDone = true;
1151
- finishChatStreamResponsesSse(state);
1152
- finish({ ok: true });
1153
- return;
1154
- }
1155
- const parsedChunk = parseJsonOrError(data);
1156
- if (!parsedChunk.error) {
1157
- if (parsedChunk.value && typeof parsedChunk.value === 'object' && parsedChunk.value.error) {
1158
- failChatStreamResponsesSse(state, formatUpstreamStreamError(parsedChunk.value.error));
1159
- finish({ ok: true });
1160
- return;
1161
- }
1162
- writeChatCompletionChunkAsResponsesSse(state, parsedChunk.value);
1163
- }
1164
- };
1165
-
1166
- upstreamRes.on('data', (chunk) => {
1167
- if (!chunk) return;
1168
- buffer += utf8Decoder.write(chunk);
1169
- let boundary = buffer.search(/\r?\n\r?\n/);
1170
- while (boundary >= 0) {
1171
- const block = buffer.slice(0, boundary);
1172
- const match = buffer.slice(boundary).match(/^\r?\n\r?\n/);
1173
- buffer = buffer.slice(boundary + (match ? match[0].length : 2));
1174
- handleEventBlock(block);
1175
- boundary = buffer.search(/\r?\n\r?\n/);
1176
- }
1177
- });
1178
- upstreamRes.on('end', () => {
1179
- buffer += utf8Decoder.end();
1180
- if (buffer.trim()) handleEventBlock(buffer);
1181
- if (!state.finished && !state.sawDone && !state.sawFinishReason) {
1182
- failChatStreamResponsesSse(state, 'upstream stream ended before [DONE]');
1183
- finish({ ok: true });
1184
- return;
1185
- }
1186
- finishChatStreamResponsesSse(state);
1187
- finish({ ok: true });
1188
- });
1189
- upstreamRes.on('aborted', () => {
1190
- failChatStreamResponsesSse(state, 'upstream stream aborted');
1191
- finish({ ok: true });
1192
- });
1193
- upstreamRes.on('error', (err) => {
1194
- failChatStreamResponsesSse(state, err && err.message ? err.message : 'upstream stream failed');
1195
- finish({ ok: true });
1196
- });
1197
- });
1198
- upstreamReq.setTimeout(timeoutMs, () => {
1199
- try { upstreamReq.destroy(new Error('timeout')); } catch (_) {}
1200
- finish({ ok: false, error: 'timeout' });
1201
- });
1202
- upstreamReq.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
1203
- if (bodyText) upstreamReq.write(bodyText);
1204
- upstreamReq.end();
1205
- });
1206
- }
1207
-
1208
- async function proxyRequestJson(targetUrl, options = {}) {
1209
- const parsed = new URL(targetUrl);
1210
- const transport = parsed.protocol === 'https:' ? https : http;
1211
- const bodyText = options.body ? JSON.stringify(options.body) : '';
1212
- const maxBytes = Number.isFinite(options.maxBytes) && options.maxBytes > 0
1213
- ? Math.floor(options.maxBytes)
1214
- : 0;
1215
- const headers = {
1216
- 'Accept': 'application/json',
1217
- ...(options.body ? { 'Content-Type': 'application/json' } : {}),
1218
- ...(options.headers || {})
1219
- };
1220
- if (options.body) {
1221
- headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
1222
- }
1223
-
1224
- const timeoutMs = Number.isFinite(options.timeoutMs)
1225
- ? Math.max(1000, Number(options.timeoutMs))
1226
- : REQUEST_TIMEOUT_MS;
1227
- return new Promise((resolve) => {
1228
- let settled = false;
1229
- const finish = (value) => {
1230
- if (settled) return;
1231
- settled = true;
1232
- resolve(value);
1233
- };
1234
- const req = transport.request({
1235
- protocol: parsed.protocol,
1236
- hostname: parsed.hostname,
1237
- port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
1238
- method: options.method || 'GET',
1239
- path: `${parsed.pathname}${parsed.search}`,
1240
- headers,
1241
- agent: parsed.protocol === 'https:' ? options.httpsAgent : options.httpAgent
1242
- }, (upstreamRes) => {
1243
- const chunks = [];
1244
- let size = 0;
1245
- upstreamRes.on('data', (chunk) => {
1246
- if (!chunk) return;
1247
- if (maxBytes > 0) {
1248
- size += chunk.length;
1249
- if (size > maxBytes) {
1250
- chunks.length = 0;
1251
- try { upstreamRes.destroy(new Error('response too large')); } catch (_) {}
1252
- try { req.destroy(new Error('response too large')); } catch (_) {}
1253
- finish({ ok: false, error: 'response too large' });
1254
- return;
1255
- }
1256
- }
1257
- chunks.push(chunk);
1258
- });
1259
- upstreamRes.on('end', () => {
1260
- const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
1261
- finish({
1262
- ok: true,
1263
- status: upstreamRes.statusCode || 0,
1264
- headers: upstreamRes.headers || {},
1265
- bodyText: text
1266
- });
1267
- });
1268
- });
1269
- req.setTimeout(timeoutMs, () => {
1270
- try { req.destroy(new Error('timeout')); } catch (_) {}
1271
- finish({ ok: false, error: 'timeout' });
1272
- });
1273
- req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
1274
- if (bodyText) {
1275
- req.write(bodyText);
1276
- }
1277
- req.end();
1278
- });
1279
- }
1280
-
1281
- function createOpenaiBridgeHttpHandler(options = {}) {
1282
- const settingsFile = options.settingsFile;
1283
- const expectedTokenRaw = typeof options.expectedToken === 'string' ? options.expectedToken.trim() : '';
1284
- const expectedToken = Object.prototype.hasOwnProperty.call(options, 'expectedToken')
1285
- ? expectedTokenRaw
1286
- : (expectedTokenRaw || DEFAULT_BRIDGE_TOKEN);
1287
- const maxBodySize = Number.isFinite(options.maxBodySize) ? options.maxBodySize : 0;
1288
- const httpAgent = options.httpAgent;
1289
- const httpsAgent = options.httpsAgent;
1290
- const maxUpstreamBytes = Number.isFinite(options.maxUpstreamBytes) && options.maxUpstreamBytes > 0
1291
- ? Math.floor(options.maxUpstreamBytes)
1292
- : Math.max(16 * 1024 * 1024, maxBodySize > 0 ? maxBodySize * 4 : 0);
1293
-
1294
- if (!settingsFile) {
1295
- throw new Error('createOpenaiBridgeHttpHandler 缺少 settingsFile');
1296
- }
1297
-
1298
- // 端点不支持的缓存(per-baseUrl, TTL 30 分钟):避免每次非流式请求重复探测 /v1/responses。
1299
- const unsupportedResponses = new Map();
1300
- const isResponsesKnownUnsupported = (baseUrl) => {
1301
- if (!baseUrl) return false;
1302
- const entry = unsupportedResponses.get(baseUrl);
1303
- if (!entry) return false;
1304
- if (entry.expiresAt <= Date.now()) {
1305
- unsupportedResponses.delete(baseUrl);
1306
- return false;
1307
- }
1308
- return true;
1309
- };
1310
- const markResponsesUnsupported = (baseUrl) => {
1311
- if (!baseUrl) return;
1312
- unsupportedResponses.set(baseUrl, { expiresAt: Date.now() + RESPONSES_UNSUPPORTED_TTL_MS });
1313
- };
1314
- const clearResponsesUnsupported = (baseUrl) => {
1315
- if (!baseUrl) return;
1316
- unsupportedResponses.delete(baseUrl);
1317
- };
1318
-
1319
- const matchPath = (requestPath) => {
1320
- const normalized = String(requestPath || '');
1321
- const prefix = '/bridge/openai/';
1322
- if (!normalized.startsWith(prefix)) return null;
1323
- const rest = normalized.slice(prefix.length);
1324
- const [provider, ...tail] = rest.split('/').filter((part) => part.length > 0);
1325
- if (!provider) return null;
1326
- const tailPath = '/' + tail.join('/');
1327
- if (!tailPath.startsWith('/v1')) return null;
1328
- const suffix = tailPath === '/v1' ? '' : tailPath.replace(/^\/v1\/?/, '');
1329
- return { provider, suffix };
1330
- };
1331
-
1332
- const handler = (req, res) => {
1333
- let parsedUrl;
1334
- try {
1335
- parsedUrl = new URL(req.url || '/', 'http://localhost');
1336
- } catch (_) {
1337
- return false;
1338
- }
1339
- const match = matchPath(parsedUrl.pathname || '/');
1340
- if (!match) return false;
1341
-
1342
- void (async () => {
1343
- try {
1344
- const token = extractAuthorizationToken(req);
1345
- // 兼容:某些客户端在自定义 base_url 时可能不带 Authorization。
1346
- // 为避免在 LAN 暴露无鉴权的代理,这里仅允许 loopback 连接缺省 token。
1347
- const remoteAddr = req && req.socket ? req.socket.remoteAddress : '';
1348
- const isLoopback = isLoopbackAddress(remoteAddr);
1349
- if (!isLoopback && !expectedToken) {
1350
- res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
1351
- res.end(JSON.stringify({ error: 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' }));
1352
- return;
1353
- }
1354
- if (!token && !isLoopback) {
1355
- res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
1356
- res.end(JSON.stringify({ error: 'Unauthorized' }));
1357
- return;
1358
- }
1359
- // loopback 上的本地代理:允许客户端携带任意 Authorization(例如 Codex 会附带 provider apiKey)。
1360
- // 非 loopback 时仍强制校验 expectedToken,避免局域网被未授权调用。
1361
- if (!isLoopback && token && token !== expectedToken) {
1362
- res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
1363
- res.end(JSON.stringify({ error: 'Unauthorized' }));
1364
- return;
1365
- }
1366
-
1367
- const upstream = resolveOpenaiBridgeUpstream(settingsFile, match.provider);
1368
- if (upstream.error) {
1369
- res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
1370
- res.end(JSON.stringify({ error: upstream.error }));
1371
- return;
1372
- }
1373
-
1374
- const suffix = match.suffix || '';
1375
- const normalizedSuffix = suffix.replace(/^\/+/, '');
1376
-
1377
- const authHeader = upstream.apiKey
1378
- ? (/^bearer\s+/i.test(upstream.apiKey) ? upstream.apiKey : `Bearer ${upstream.apiKey}`)
1379
- : '';
1380
- const upstreamHeaders = upstream && upstream.headers && typeof upstream.headers === 'object' && !Array.isArray(upstream.headers)
1381
- ? upstream.headers
1382
- : {};
1383
-
1384
- if (!normalizedSuffix) {
1385
- if ((req.method || 'GET').toUpperCase() !== 'GET') {
1386
- res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1387
- res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1388
- return;
1389
- }
1390
- res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1391
- res.end(JSON.stringify({
1392
- object: 'codexmate.openai_bridge',
1393
- provider: match.provider,
1394
- status: 'ok',
1395
- endpoints: ['/v1/responses', '/v1/models']
1396
- }));
1397
- return;
1398
- }
1399
-
1400
- if (normalizedSuffix === 'models') {
1401
- if ((req.method || 'GET').toUpperCase() !== 'GET') {
1402
- res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1403
- res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1404
- return;
1405
- }
1406
-
1407
- const url = joinApiUrl(upstream.baseUrl, 'models');
1408
- const result = await retryTransientRequest(() => proxyRequestJson(url, {
1409
- method: 'GET',
1410
- headers: {
1411
- ...(authHeader ? { Authorization: authHeader } : {}),
1412
- ...upstreamHeaders
1413
- },
1414
- maxBytes: maxUpstreamBytes,
1415
- httpAgent,
1416
- httpsAgent
1417
- }));
1418
- if (!result.ok) {
1419
- res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1420
- res.end(JSON.stringify({ error: `Upstream request failed: ${result.error}` }));
1421
- return;
1422
- }
1423
- res.writeHead(result.status || 502, { 'Content-Type': 'application/json; charset=utf-8' });
1424
- res.end(result.bodyText || '');
1425
- return;
1426
- }
1427
-
1428
- if (normalizedSuffix !== 'responses') {
1429
- res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
1430
- res.end(JSON.stringify({ error: 'Not Found' }));
1431
- return;
1432
- }
1433
-
1434
- if ((req.method || 'GET').toUpperCase() !== 'POST') {
1435
- res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1436
- res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1437
- return;
1438
- }
1439
-
1440
- const { body, error: bodyErr } = await readRequestBody(req, maxBodySize);
1441
- if (bodyErr) {
1442
- res.writeHead(413, { 'Content-Type': 'application/json; charset=utf-8' });
1443
- res.end(JSON.stringify({ error: bodyErr }));
1444
- return;
1445
- }
1446
- const parsed = parseJsonOrError(body);
1447
- if (parsed.error) {
1448
- res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1449
- res.end(JSON.stringify({ error: `Invalid JSON: ${parsed.error}` }));
1450
- return;
1451
- }
1452
-
1453
- const responsesRequest = parsed.value;
1454
- const streamRequested = !!(responsesRequest && typeof responsesRequest === 'object' && responsesRequest.stream === true);
1455
- const acceptHeader = req && req.headers ? (req.headers.accept || req.headers.Accept || '') : '';
1456
- const wantsSse = /text\/event-stream/i.test(String(acceptHeader || ''));
1457
-
1458
- if (streamRequested && wantsSse) {
1459
- const converted = convertResponsesRequestToChatCompletions(responsesRequest);
1460
- if (converted.error) {
1461
- res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1462
- res.end(JSON.stringify({ error: converted.error }));
1463
- return;
1464
- }
1465
- const upstreamUrl = joinApiUrl(upstream.baseUrl, 'chat/completions');
1466
- const chatBody = { ...converted.chat, stream: true };
1467
- const streamed = await retryTransientRequest(() => streamChatCompletionsAsResponsesSse(upstreamUrl, {
1468
- method: 'POST',
1469
- body: chatBody,
1470
- headers: {
1471
- ...(authHeader ? { Authorization: authHeader } : {}),
1472
- ...upstreamHeaders
1473
- },
1474
- maxBytes: maxUpstreamBytes,
1475
- httpAgent,
1476
- httpsAgent,
1477
- res,
1478
- model: typeof chatBody.model === 'string' ? chatBody.model : ''
1479
- }));
1480
- if (!streamed.ok) {
1481
- if (res.writableEnded || res.destroyed) {
1482
- return;
1483
- }
1484
- if (!res.headersSent) {
1485
- res.writeHead(streamed.status && streamed.status >= 400 ? streamed.status : 502, { 'Content-Type': 'application/json; charset=utf-8' });
1486
- res.end(streamed.bodyText || JSON.stringify({ error: streamed.error || 'Upstream request failed' }));
1487
- } else if (!res.writableEnded && !res.destroyed) {
1488
- writeSse(res, 'response.failed', { type: 'response.failed', error: streamed.error || streamed.bodyText || 'Upstream request failed' });
1489
- writeSse(res, 'done', '[DONE]');
1490
- res.end();
1491
- }
1492
- }
1493
- return;
1494
- }
1495
-
1496
- // Maxx-style behavior: prefer upstream /responses if supported.
1497
- // Fallback to /chat/completions conversion when upstream does not implement /responses (404/405).
1498
- // 已知不支持的上游:直接跳过探测,节省一次 round-trip。
1499
- const skipResponsesProbe = isResponsesKnownUnsupported(upstream.baseUrl);
1500
- const upstreamResponsesUrl = joinApiUrl(upstream.baseUrl, 'responses');
1501
- const upstreamResponsesResult = skipResponsesProbe
1502
- ? { ok: true, status: 404, bodyText: '' }
1503
- : await retryTransientRequest(() => proxyRequestJson(upstreamResponsesUrl, {
1504
- method: 'POST',
1505
- body: toUpstreamNonStreamingResponsesPayload(responsesRequest),
1506
- headers: {
1507
- ...(authHeader ? { Authorization: authHeader } : {}),
1508
- ...upstreamHeaders
1509
- },
1510
- maxBytes: maxUpstreamBytes,
1511
- httpAgent,
1512
- httpsAgent
1513
- }));
1514
-
1515
- if (upstreamResponsesResult.ok && upstreamResponsesResult.status >= 200 && upstreamResponsesResult.status < 300) {
1516
- clearResponsesUnsupported(upstream.baseUrl);
1517
- const upstreamJson = parseJsonOrError(upstreamResponsesResult.bodyText);
1518
- if (upstreamJson.error) {
1519
- res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1520
- res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${upstreamJson.error}` }));
1521
- return;
1522
- }
1523
- const upstreamPayload = upstreamJson.value;
1524
- if (streamRequested && wantsSse) {
1525
- res.writeHead(200, {
1526
- 'Content-Type': 'text/event-stream; charset=utf-8',
1527
- 'Cache-Control': 'no-cache',
1528
- 'Connection': 'keep-alive',
1529
- 'X-Accel-Buffering': 'no'
1530
- });
1531
- if (typeof res.flushHeaders === 'function') res.flushHeaders();
1532
- sendResponsesSse(res, upstreamPayload);
1533
- res.end();
1534
- return;
1535
- }
1536
-
1537
- res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1538
- res.end(JSON.stringify(ensureResponseMetadata(upstreamPayload)));
1539
- return;
1540
- }
1541
-
1542
- if (upstreamResponsesResult.ok && upstreamResponsesResult.status >= 400) {
1543
- if (!shouldFallbackFromUpstreamResponses(upstreamResponsesResult.status, upstreamResponsesResult.bodyText)) {
1544
- res.writeHead(upstreamResponsesResult.status, { 'Content-Type': 'application/json; charset=utf-8' });
1545
- res.end(upstreamResponsesResult.bodyText || JSON.stringify({ error: 'Upstream error' }));
1546
- return;
1547
- }
1548
- if (!skipResponsesProbe && isResponsesEndpointUnsupported(upstreamResponsesResult.status, upstreamResponsesResult.bodyText)) {
1549
- markResponsesUnsupported(upstream.baseUrl);
1550
- }
1551
- // fallthrough to chat/completions conversion
1552
- }
1553
-
1554
- if (!upstreamResponsesResult.ok) {
1555
- res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1556
- res.end(JSON.stringify({ error: `Upstream request failed: ${upstreamResponsesResult.error}` }));
1557
- return;
1558
- }
1559
-
1560
- const converted = convertResponsesRequestToChatCompletions(responsesRequest);
1561
- if (converted.error) {
1562
- res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1563
- res.end(JSON.stringify({ error: converted.error }));
1564
- return;
1565
- }
1566
-
1567
- const upstreamUrl = joinApiUrl(upstream.baseUrl, 'chat/completions');
1568
- const upstreamResult = await retryTransientRequest(() => proxyRequestJson(upstreamUrl, {
1569
- method: 'POST',
1570
- body: converted.chat,
1571
- headers: {
1572
- ...(authHeader ? { Authorization: authHeader } : {}),
1573
- ...upstreamHeaders
1574
- },
1575
- maxBytes: maxUpstreamBytes,
1576
- httpAgent,
1577
- httpsAgent
1578
- }));
1579
- if (!upstreamResult.ok) {
1580
- res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1581
- res.end(JSON.stringify({ error: `Upstream request failed: ${upstreamResult.error}` }));
1582
- return;
1583
- }
1584
-
1585
- const upstreamJson = parseJsonOrError(upstreamResult.bodyText);
1586
- if (upstreamResult.status >= 400) {
1587
- res.writeHead(upstreamResult.status, { 'Content-Type': 'application/json; charset=utf-8' });
1588
- res.end(upstreamResult.bodyText || JSON.stringify({ error: 'Upstream error' }));
1589
- return;
1590
- }
1591
- if (upstreamJson.error) {
1592
- res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1593
- res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${upstreamJson.error}` }));
1594
- return;
1595
- }
1596
-
1597
- const model = typeof converted.chat.model === 'string' ? converted.chat.model : '';
1598
- const extracted = extractChatCompletionResult(upstreamJson.value);
1599
- const text = extracted && typeof extracted.text === 'string' ? extracted.text : '';
1600
- const toolCalls = extracted && Array.isArray(extracted.toolCalls) ? extracted.toolCalls : [];
1601
- const responsesPayload = buildResponsesPayloadFromChatResult(model, text, toolCalls, upstreamJson.value);
1602
-
1603
- if (converted.streamRequested && wantsSse) {
1604
- res.writeHead(200, {
1605
- 'Content-Type': 'text/event-stream; charset=utf-8',
1606
- 'Cache-Control': 'no-cache',
1607
- 'Connection': 'keep-alive',
1608
- 'X-Accel-Buffering': 'no'
1609
- });
1610
- if (typeof res.flushHeaders === 'function') res.flushHeaders();
1611
- sendResponsesSse(res, responsesPayload);
1612
- res.end();
1613
- return;
1614
- }
1615
-
1616
- res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1617
- res.end(JSON.stringify(ensureResponseMetadata(responsesPayload)));
1618
- } catch (e) {
1619
- res.writeHead(500, { 'Content-Type': 'application/json; charset=utf-8' });
1620
- res.end(JSON.stringify({ error: e && e.message ? e.message : 'Internal Error' }));
1621
- }
1622
- })();
1623
-
1624
- return true;
1625
- };
1626
-
1627
- handler.matchPath = matchPath;
1628
- return handler;
1629
- }
1630
-
1631
- module.exports = {
1632
- readOpenaiBridgeSettings,
1633
- upsertOpenaiBridgeProvider,
1634
- resolveOpenaiBridgeUpstream,
1635
- createOpenaiBridgeHttpHandler,
1636
- // exported for local-bridge reuse
1637
- convertResponsesRequestToChatCompletions,
1638
- streamChatCompletionsAsResponsesSse,
1639
- proxyRequestJson,
1640
- ensureResponseMetadata,
1641
- sendResponsesSse,
1642
- extractAuthorizationToken,
1643
- readRequestBody,
1644
- parseJsonOrError,
1645
- extractChatCompletionResult,
1646
- buildResponsesPayloadFromChatResult,
1647
- retryTransientRequest,
1648
- normalizeOpenaiUpstreamBaseUrl,
1649
- extractResponsesOutputText,
1650
- shouldFallbackFromUpstreamResponses,
1651
- isTransientNetworkError,
1652
- isLoopbackAddress
1653
- };
1
+ const http = require('http');
2
+ const https = require('https');
3
+ const crypto = require('crypto');
4
+ const { StringDecoder } = require('string_decoder');
5
+ const { readJsonFile, writeJsonAtomic } = require('../lib/cli-file-utils');
6
+ const { isValidHttpUrl, normalizeBaseUrl, joinApiUrl } = require('../lib/cli-utils');
7
+
8
+ const DEFAULT_BRIDGE_TOKEN = 'codexmate';
9
+ const SETTINGS_VERSION = 1;
10
+ // 推理模型 reasoning 阶段可能长时间无字节输出,需匹配 codex 的 stream_idle_timeout_ms=300000。
11
+ const STREAM_IDLE_TIMEOUT_MS = 5 * 60 * 1000;
12
+ const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
13
+ const RESPONSES_UNSUPPORTED_TTL_MS = 30 * 60 * 1000;
14
+
15
+ function normalizeText(value) {
16
+ return typeof value === 'string' ? value.trim() : '';
17
+ }
18
+
19
+ function normalizeProviderName(value) {
20
+ // Provider name validation is done elsewhere; keep this conservative.
21
+ return normalizeText(value);
22
+ }
23
+
24
+ function normalizeOpenaiUpstreamBaseUrl(rawValue) {
25
+ const normalized = normalizeBaseUrl(rawValue);
26
+ if (!normalized) return '';
27
+ try {
28
+ const parsed = new URL(normalized);
29
+ let pathname = String(parsed.pathname || '').replace(/\/+$/g, '');
30
+
31
+ // If user accidentally pasted a full endpoint, strip it back to the base URL.
32
+ // Keep direct provider routes (e.g. /project/ym) intact.
33
+ pathname = pathname
34
+ .replace(/\/v1\/chat\/completions$/i, '/v1')
35
+ .replace(/\/chat\/completions$/i, '')
36
+ .replace(/\/v1\/responses$/i, '/v1')
37
+ .replace(/\/responses$/i, '')
38
+ .replace(/\/v1\/models$/i, '/v1')
39
+ .replace(/\/models$/i, '');
40
+
41
+ // Normalize empty/root path.
42
+ if (pathname === '/') pathname = '';
43
+
44
+ const rebuilt = `${parsed.origin}${pathname}`;
45
+ return normalizeBaseUrl(rebuilt);
46
+ } catch (_) {
47
+ return normalized;
48
+ }
49
+ }
50
+
51
+ function normalizeUpstreamEntry(entry) {
52
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
53
+ return null;
54
+ }
55
+ const baseUrl = normalizeOpenaiUpstreamBaseUrl(entry.baseUrl || entry.base_url || '');
56
+ const apiKey = normalizeText(entry.apiKey || entry.api_key || entry.key || '');
57
+ const headersRaw = entry.headers || entry.extraHeaders || entry.extra_headers || null;
58
+ const headers = normalizeHeadersMap(headersRaw);
59
+ if (!baseUrl || !isValidHttpUrl(baseUrl)) {
60
+ return null;
61
+ }
62
+ return { baseUrl, apiKey, headers };
63
+ }
64
+
65
+ function normalizeHeadersMap(value) {
66
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
67
+ return {};
68
+ }
69
+ const forbidden = new Set([
70
+ 'authorization',
71
+ 'host',
72
+ 'content-length',
73
+ 'connection',
74
+ 'transfer-encoding',
75
+ 'keep-alive',
76
+ 'proxy-authenticate',
77
+ 'proxy-authorization',
78
+ 'te',
79
+ 'trailer',
80
+ 'upgrade'
81
+ ]);
82
+ const result = {};
83
+ for (const [rawKey, rawVal] of Object.entries(value)) {
84
+ const key = typeof rawKey === 'string' ? rawKey.trim() : '';
85
+ if (!key) continue;
86
+ const lower = key.toLowerCase();
87
+ if (forbidden.has(lower)) continue;
88
+ if (typeof rawVal !== 'string') continue;
89
+ result[key] = rawVal;
90
+ }
91
+ return result;
92
+ }
93
+
94
+ function readOpenaiBridgeSettings(filePath) {
95
+ const parsed = readJsonFile(filePath, null);
96
+ const providers = parsed && typeof parsed === 'object' && !Array.isArray(parsed)
97
+ ? parsed.providers
98
+ : null;
99
+ const providerMap = providers && typeof providers === 'object' && !Array.isArray(providers)
100
+ ? providers
101
+ : {};
102
+ return {
103
+ version: SETTINGS_VERSION,
104
+ providers: providerMap
105
+ };
106
+ }
107
+
108
+ function upsertOpenaiBridgeProvider(filePath, providerName, upstreamBaseUrl, apiKey, headers) {
109
+ const name = normalizeProviderName(providerName);
110
+ const baseUrl = normalizeOpenaiUpstreamBaseUrl(upstreamBaseUrl);
111
+ const key = normalizeText(apiKey);
112
+ const nextHeaders = normalizeHeadersMap(headers);
113
+
114
+ if (!name) {
115
+ return { error: 'Provider name is required' };
116
+ }
117
+ if (!baseUrl || !isValidHttpUrl(baseUrl)) {
118
+ return { error: 'Upstream base URL is invalid' };
119
+ }
120
+
121
+ const settings = readOpenaiBridgeSettings(filePath);
122
+ const existing = settings && settings.providers ? settings.providers[name] : null;
123
+ const existingHeaders = existing && typeof existing === 'object' && !Array.isArray(existing)
124
+ ? normalizeHeadersMap(existing.headers || existing.extraHeaders || existing.extra_headers || null)
125
+ : {};
126
+ const next = {
127
+ version: SETTINGS_VERSION,
128
+ providers: {
129
+ ...(settings.providers || {}),
130
+ [name]: {
131
+ baseUrl,
132
+ apiKey: key,
133
+ headers: Object.keys(nextHeaders).length ? nextHeaders : existingHeaders
134
+ }
135
+ }
136
+ };
137
+ writeJsonAtomic(filePath, next);
138
+ return { success: true };
139
+ }
140
+
141
+ function resolveOpenaiBridgeUpstream(filePath, providerName) {
142
+ const name = normalizeProviderName(providerName);
143
+ if (!name) return { error: 'Provider name is required' };
144
+ const settings = readOpenaiBridgeSettings(filePath);
145
+ const entry = settings.providers ? settings.providers[name] : null;
146
+ const normalized = normalizeUpstreamEntry(entry);
147
+ if (!normalized) {
148
+ return { error: `OpenAI 转换未配置: ${name}` };
149
+ }
150
+ return { provider: name, ...normalized };
151
+ }
152
+
153
+ function extractAuthorizationToken(req) {
154
+ const header = typeof req.headers.authorization === 'string' ? req.headers.authorization.trim() : '';
155
+ if (!header) return '';
156
+ if (/^bearer\s+/i.test(header)) {
157
+ return header.replace(/^bearer\s+/i, '').trim();
158
+ }
159
+ return header;
160
+ }
161
+
162
+ function readRequestBody(req, maxBytes) {
163
+ return new Promise((resolve) => {
164
+ let body = '';
165
+ let size = 0;
166
+ let aborted = false;
167
+ req.on('data', (chunk) => {
168
+ if (aborted) return;
169
+ size += chunk.length;
170
+ if (Number.isFinite(maxBytes) && maxBytes > 0 && size > maxBytes) {
171
+ aborted = true;
172
+ try { req.destroy(); } catch (_) {}
173
+ resolve({ error: '请求体过大' });
174
+ return;
175
+ }
176
+ body += chunk;
177
+ });
178
+ req.on('end', () => {
179
+ if (aborted) return;
180
+ resolve({ body });
181
+ });
182
+ req.on('error', (err) => resolve({ error: err && err.message ? err.message : 'request failed' }));
183
+ });
184
+ }
185
+
186
+ function parseJsonOrError(text) {
187
+ if (typeof text !== 'string' || !text.trim()) {
188
+ return { value: null, error: 'empty body' };
189
+ }
190
+ try {
191
+ return { value: JSON.parse(text), error: '' };
192
+ } catch (e) {
193
+ return { value: null, error: e && e.message ? e.message : 'invalid json' };
194
+ }
195
+ }
196
+
197
+ function extractChatCompletionResult(payload) {
198
+ if (!payload || typeof payload !== 'object') return { text: '', toolCalls: [] };
199
+ const choice = Array.isArray(payload.choices) ? payload.choices[0] : null;
200
+ const message = choice && typeof choice === 'object' ? choice.message : null;
201
+ const toolCalls = message && typeof message === 'object' && Array.isArray(message.tool_calls)
202
+ ? message.tool_calls
203
+ : [];
204
+ const content = message && typeof message === 'object' ? message.content : '';
205
+ let text = '';
206
+ if (typeof content === 'string') {
207
+ text = content;
208
+ } else if (Array.isArray(content)) {
209
+ text = content
210
+ .map((item) => {
211
+ if (!item) return '';
212
+ if (typeof item === 'string') return item;
213
+ if (typeof item === 'object') {
214
+ if (typeof item.text === 'string') return item.text;
215
+ if (typeof item.content === 'string') return item.content;
216
+ }
217
+ return '';
218
+ })
219
+ .filter(Boolean)
220
+ .join('');
221
+ }
222
+ return { text, toolCalls };
223
+ }
224
+
225
+ function normalizeResponsesInputToChatMessages(input) {
226
+ // 支持:
227
+ // - string
228
+ // - { role, content }(单条 message)
229
+ // - { type:"input_text"|"input_image", ... }(单个 block)
230
+ // - [{ role, content: [...] }](messages array)
231
+ // - [{ type:"input_text"|"input_image", ... }](blocks array -> 单条 user 消息)
232
+ if (typeof input === 'string') {
233
+ return [{ role: 'user', content: input }];
234
+ }
235
+
236
+ const toChatContent = (blocks) => {
237
+ if (!Array.isArray(blocks)) return '';
238
+ const out = [];
239
+ for (const block of blocks) {
240
+ if (!block || typeof block !== 'object') continue;
241
+ const type = typeof block.type === 'string' ? block.type : '';
242
+ if ((type === 'input_text' || type === 'output_text') && typeof block.text === 'string') {
243
+ out.push({ type: 'text', text: block.text });
244
+ continue;
245
+ }
246
+ if ((type === 'reasoning' || type === 'reasoning_text' || type === 'reasoning_content') && typeof block.text === 'string') {
247
+ out.push({ type: 'text', text: block.text });
248
+ continue;
249
+ }
250
+ if (type === 'input_image') {
251
+ const raw = block.image_url != null ? block.image_url : block.imageUrl;
252
+ const url = typeof raw === 'string'
253
+ ? raw
254
+ : (raw && typeof raw === 'object' && typeof raw.url === 'string' ? raw.url : '');
255
+ if (url) {
256
+ out.push({ type: 'image_url', image_url: { url } });
257
+ }
258
+ continue;
259
+ }
260
+ // 容错:兼容已是 chat content 的 {type:"text"} / {type:"image_url"}
261
+ if (type === 'text' && typeof block.text === 'string') {
262
+ out.push({ type: 'text', text: block.text });
263
+ continue;
264
+ }
265
+ if (type === 'image_url' && block.image_url) {
266
+ out.push({ type: 'image_url', image_url: block.image_url });
267
+ continue;
268
+ }
269
+ const text = typeof block.text === 'string'
270
+ ? block.text
271
+ : (typeof block.content === 'string' ? block.content : '');
272
+ if (text) {
273
+ out.push({ type: 'text', text });
274
+ continue;
275
+ }
276
+ try {
277
+ const raw = JSON.stringify(block);
278
+ if (raw) {
279
+ out.push({ type: 'text', text: raw.slice(0, 4000) });
280
+ }
281
+ } catch (_) {}
282
+ }
283
+ if (out.length === 0) return '';
284
+ return out;
285
+ };
286
+
287
+ const toRole = (value) => {
288
+ const roleRaw = typeof value === 'string' ? value.trim().toLowerCase() : '';
289
+ if (roleRaw === 'assistant') return 'assistant';
290
+ // codex 把 AGENTS.md 注入 developer 角色;Responses 的 developer 在 chat 侧等价于 system。
291
+ if (roleRaw === 'system' || roleRaw === 'developer') return 'system';
292
+ return 'user';
293
+ };
294
+
295
+ if (input && typeof input === 'object' && !Array.isArray(input)) {
296
+ if (typeof input.role === 'string' && input.content != null) {
297
+ const role = toRole(input.role);
298
+ const content = Array.isArray(input.content)
299
+ ? toChatContent(input.content)
300
+ : input.content;
301
+ return content ? [{ role, content }] : [];
302
+ }
303
+ if (typeof input.type === 'string') {
304
+ const content = toChatContent([input]);
305
+ return content ? [{ role: 'user', content }] : [];
306
+ }
307
+ return [];
308
+ }
309
+
310
+ if (!Array.isArray(input)) {
311
+ return [];
312
+ }
313
+
314
+ const messages = [];
315
+ for (const item of input) {
316
+ if (!item || typeof item !== 'object') continue;
317
+
318
+ // Tool calls (Responses): { type: "function_call", call_id, name, arguments }
319
+ // Chat Completions equivalent: assistant message with tool_calls
320
+ if (typeof item.type === 'string' && item.type === 'function_call') {
321
+ const callId = typeof item.call_id === 'string' ? item.call_id.trim() : '';
322
+ const name = typeof item.name === 'string' ? item.name.trim() : '';
323
+ const args = typeof item.arguments === 'string' ? item.arguments : '';
324
+ if (callId && name) {
325
+ messages.push({
326
+ role: 'assistant',
327
+ tool_calls: [{
328
+ id: callId,
329
+ type: 'function',
330
+ function: { name, arguments: args || '' }
331
+ }]
332
+ });
333
+ }
334
+ continue;
335
+ }
336
+
337
+ // Tool results (Responses): { type: "function_call_output", call_id, output }
338
+ // Chat Completions equivalent: { role: "tool", tool_call_id, content }
339
+ if (typeof item.type === 'string' && item.type === 'function_call_output') {
340
+ const toolCallId = typeof item.call_id === 'string' ? item.call_id.trim() : '';
341
+ let content = item.output;
342
+ if (typeof content !== 'string') {
343
+ try {
344
+ content = JSON.stringify(content);
345
+ } catch (_) {
346
+ content = String(content ?? '');
347
+ }
348
+ }
349
+ if (toolCallId) {
350
+ messages.push({ role: 'tool', tool_call_id: toolCallId, content: String(content || '') });
351
+ }
352
+ continue;
353
+ }
354
+
355
+ // message form
356
+ if (typeof item.role === 'string' && item.content != null) {
357
+ const role = toRole(item.role);
358
+ const content = Array.isArray(item.content)
359
+ ? toChatContent(item.content)
360
+ : item.content;
361
+ if (content) {
362
+ messages.push({ role, content });
363
+ }
364
+ continue;
365
+ }
366
+ }
367
+
368
+ if (messages.length > 0) {
369
+ return messages;
370
+ }
371
+
372
+ // 退化:把 input array 当作单条 user content blocks
373
+ const fallbackContent = toChatContent(input);
374
+ if (fallbackContent) {
375
+ return [{ role: 'user', content: fallbackContent }];
376
+ }
377
+ return [];
378
+ }
379
+
380
+
381
+ function normalizeResponsesToolsToChatTools(tools) {
382
+ if (!Array.isArray(tools)) return tools;
383
+ return tools
384
+ .map((tool) => {
385
+ if (!tool || typeof tool !== 'object') return null;
386
+ if (tool.type !== 'function') return null;
387
+ const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
388
+ ? tool.function
389
+ : {};
390
+ const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
391
+ ? sourceFn.name.trim()
392
+ : (typeof tool.name === 'string' ? tool.name.trim() : '');
393
+ if (!name) return null;
394
+ const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
395
+ ? sourceFn.parameters
396
+ : (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : {});
397
+ const fn = { name, parameters };
398
+ const description = typeof sourceFn.description === 'string'
399
+ ? sourceFn.description
400
+ : (typeof tool.description === 'string' ? tool.description : undefined);
401
+ const strict = typeof sourceFn.strict === 'boolean'
402
+ ? sourceFn.strict
403
+ : (typeof tool.strict === 'boolean' ? tool.strict : undefined);
404
+ if (description !== undefined) fn.description = description;
405
+ if (strict !== undefined) fn.strict = strict;
406
+ return { type: 'function', function: fn };
407
+ })
408
+ .filter(Boolean);
409
+ }
410
+
411
+ function normalizeResponsesToolChoiceToChatToolChoice(toolChoice) {
412
+ if (!toolChoice || typeof toolChoice !== 'object' || Array.isArray(toolChoice)) return toolChoice;
413
+ if (toolChoice.type === 'function' && typeof toolChoice.name === 'string' && toolChoice.name.trim()) {
414
+ return { type: 'function', function: { name: toolChoice.name.trim() } };
415
+ }
416
+ return toolChoice;
417
+ }
418
+
419
+ function normalizeResponsesToolsForResponsesApi(tools) {
420
+ if (!Array.isArray(tools)) return tools;
421
+ return tools
422
+ .map((tool) => {
423
+ if (!tool || typeof tool !== 'object') return null;
424
+ if (tool.type !== 'function') return null;
425
+ const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
426
+ ? tool.function
427
+ : {};
428
+ const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
429
+ ? sourceFn.name.trim()
430
+ : (typeof tool.name === 'string' ? tool.name.trim() : '');
431
+ if (!name) return null;
432
+ const out = { type: 'function', name };
433
+ const description = typeof sourceFn.description === 'string'
434
+ ? sourceFn.description
435
+ : (typeof tool.description === 'string' ? tool.description : undefined);
436
+ const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
437
+ ? sourceFn.parameters
438
+ : (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : undefined);
439
+ const strict = typeof sourceFn.strict === 'boolean'
440
+ ? sourceFn.strict
441
+ : (typeof tool.strict === 'boolean' ? tool.strict : undefined);
442
+ if (description !== undefined) out.description = description;
443
+ if (parameters !== undefined) out.parameters = parameters;
444
+ if (strict !== undefined) out.strict = strict;
445
+ return out;
446
+ })
447
+ .filter(Boolean);
448
+ }
449
+
450
+ function mergeLeadingSystemMessages(messages, leadingInstructions) {
451
+ const segments = [];
452
+ const seen = new Set();
453
+ const pushSegment = (text) => {
454
+ const trimmed = typeof text === 'string' ? text.trim() : '';
455
+ if (!trimmed || seen.has(trimmed)) return;
456
+ seen.add(trimmed);
457
+ segments.push(trimmed);
458
+ };
459
+ if (typeof leadingInstructions === 'string') {
460
+ pushSegment(leadingInstructions);
461
+ }
462
+ const rest = [];
463
+ for (const msg of messages) {
464
+ if (msg && msg.role === 'system') {
465
+ const content = msg.content;
466
+ if (typeof content === 'string') {
467
+ pushSegment(content);
468
+ } else if (Array.isArray(content)) {
469
+ for (const part of content) {
470
+ if (part && typeof part === 'object' && typeof part.text === 'string') {
471
+ pushSegment(part.text);
472
+ }
473
+ }
474
+ }
475
+ continue;
476
+ }
477
+ rest.push(msg);
478
+ }
479
+ const out = [];
480
+ if (segments.length) {
481
+ out.push({ role: 'system', content: segments.join('\n\n---\n\n') });
482
+ }
483
+ for (const msg of rest) out.push(msg);
484
+ return out;
485
+ }
486
+
487
+ function convertResponsesRequestToChatCompletions(payload) {
488
+ const body = payload && typeof payload === 'object' ? payload : {};
489
+ const model = typeof body.model === 'string' ? body.model.trim() : '';
490
+ if (!model) {
491
+ return { error: 'responses 请求缺少 model' };
492
+ }
493
+
494
+ const rawMessages = normalizeResponsesInputToChatMessages(body.input);
495
+ // codex 同时下发 body.instructions(内置 prompt)与 input 内 developer/system 消息(AGENTS.md)。
496
+ // 合流为一条领头 system,避免某些上游"只认第一条 system"导致 AGENTS.md 失效。
497
+ const messages = mergeLeadingSystemMessages(rawMessages, body.instructions);
498
+ if (!messages.length) {
499
+ // codex sometimes sends empty input for probes; tolerate.
500
+ messages.push({ role: 'user', content: '' });
501
+ }
502
+
503
+ const maxOutputTokens = Number.parseInt(String(body.max_output_tokens), 10);
504
+ const stream = body.stream === true;
505
+
506
+ const chat = {
507
+ model,
508
+ messages,
509
+ stream: false,
510
+ temperature: Number.isFinite(body.temperature) ? Number(body.temperature) : undefined,
511
+ top_p: Number.isFinite(body.top_p) ? Number(body.top_p) : undefined,
512
+ max_tokens: Number.isFinite(maxOutputTokens) && maxOutputTokens > 0 ? maxOutputTokens : undefined
513
+ };
514
+ if (Array.isArray(body.stop) && body.stop.length) {
515
+ chat.stop = body.stop.filter((item) => typeof item === 'string' && item.trim());
516
+ }
517
+ if (Array.isArray(body.tools) && body.tools.length) {
518
+ chat.tools = normalizeResponsesToolsToChatTools(body.tools);
519
+ }
520
+ if (body.tool_choice !== undefined) {
521
+ chat.tool_choice = normalizeResponsesToolChoiceToChatToolChoice(body.tool_choice);
522
+ }
523
+ if (body.response_format !== undefined) {
524
+ chat.response_format = body.response_format;
525
+ }
526
+ if (body.metadata !== undefined) {
527
+ chat.metadata = body.metadata;
528
+ }
529
+
530
+ // Remove undefined keys
531
+ Object.keys(chat).forEach((key) => chat[key] === undefined && delete chat[key]);
532
+
533
+ return { chat, streamRequested: stream };
534
+ }
535
+
536
+ function buildResponsesPayloadFromChatResult(model, text, toolCalls, upstreamPayload) {
537
+ const responseId = `resp_${crypto.randomBytes(10).toString('hex')}`;
538
+ const usage = upstreamPayload && upstreamPayload.usage && typeof upstreamPayload.usage === 'object'
539
+ ? upstreamPayload.usage
540
+ : null;
541
+ const createdAt = Math.floor(Date.now() / 1000);
542
+ const output = [];
543
+ const trimmedText = typeof text === 'string' ? text : '';
544
+ if (trimmedText) {
545
+ output.push({
546
+ id: `msg_${crypto.randomBytes(8).toString('hex')}`,
547
+ type: 'message',
548
+ role: 'assistant',
549
+ content: [{ type: 'output_text', text: trimmedText }]
550
+ });
551
+ }
552
+
553
+ // Convert chat.completions tool_calls into Responses-style function_call output items.
554
+ // This is important for Codex, which appends function_call + function_call_output back into `input`.
555
+ if (Array.isArray(toolCalls)) {
556
+ for (const call of toolCalls) {
557
+ if (!call || typeof call !== 'object') continue;
558
+ const callId = typeof call.id === 'string' && call.id.trim() ? call.id.trim() : `call_${crypto.randomBytes(8).toString('hex')}`;
559
+ const fn = call.function && typeof call.function === 'object' ? call.function : {};
560
+ const name = typeof fn.name === 'string' ? fn.name : '';
561
+ const args = typeof fn.arguments === 'string' ? fn.arguments : '';
562
+ if (!name) continue;
563
+ output.push({
564
+ type: 'function_call',
565
+ call_id: callId,
566
+ name,
567
+ arguments: args
568
+ });
569
+ }
570
+ }
571
+
572
+ const payload = {
573
+ id: responseId,
574
+ object: 'response',
575
+ model,
576
+ created_at: createdAt,
577
+ status: 'completed',
578
+ output,
579
+ output_text: trimmedText
580
+ };
581
+
582
+ if (usage) {
583
+ // Map chat.completions usage -> responses usage shape when possible.
584
+ const promptTokens = Number.isFinite(usage.prompt_tokens) ? Number(usage.prompt_tokens) : null;
585
+ const completionTokens = Number.isFinite(usage.completion_tokens) ? Number(usage.completion_tokens) : null;
586
+ const totalTokens = Number.isFinite(usage.total_tokens) ? Number(usage.total_tokens) : null;
587
+ if (promptTokens !== null || completionTokens !== null || totalTokens !== null) {
588
+ payload.usage = {
589
+ input_tokens: promptTokens ?? undefined,
590
+ output_tokens: completionTokens ?? undefined,
591
+ total_tokens: totalTokens ?? undefined
592
+ };
593
+ Object.keys(payload.usage).forEach((key) => payload.usage[key] === undefined && delete payload.usage[key]);
594
+ } else {
595
+ payload.usage = usage;
596
+ }
597
+ }
598
+
599
+ return payload;
600
+ }
601
+
602
+ function ensureResponseMetadata(response) {
603
+ const payload = response && typeof response === 'object' ? response : {};
604
+ if (typeof payload.object !== 'string' || !payload.object.trim()) {
605
+ payload.object = 'response';
606
+ }
607
+ if (typeof payload.created_at !== 'number') {
608
+ payload.created_at = Math.floor(Date.now() / 1000);
609
+ }
610
+ if (typeof payload.status !== 'string' || !payload.status.trim()) {
611
+ payload.status = 'completed';
612
+ }
613
+ if (!Array.isArray(payload.output)) {
614
+ payload.output = [];
615
+ }
616
+ return payload;
617
+ }
618
+
619
+ function sendResponsesSse(res, responsePayload) {
620
+ const response = ensureResponseMetadata(responsePayload);
621
+ const responseId = typeof response.id === 'string' && response.id.trim()
622
+ ? response.id.trim()
623
+ : `resp_${crypto.randomBytes(10).toString('hex')}`;
624
+ const model = typeof response.model === 'string' ? response.model : '';
625
+
626
+ let sequence = 0;
627
+ const nextSeq = () => {
628
+ sequence += 1;
629
+ return sequence;
630
+ };
631
+
632
+ writeSse(res, 'response.created', {
633
+ type: 'response.created',
634
+ response: {
635
+ id: responseId,
636
+ model,
637
+ created_at: response.created_at
638
+ }
639
+ });
640
+
641
+ const output = Array.isArray(response.output) ? response.output : [];
642
+ for (let outputIndex = 0; outputIndex < output.length; outputIndex += 1) {
643
+ const item = output[outputIndex];
644
+ if (!item || typeof item !== 'object') continue;
645
+ const itemType = typeof item.type === 'string' ? item.type : '';
646
+ const itemId = typeof item.id === 'string' && item.id.trim()
647
+ ? item.id.trim()
648
+ : (typeof item.call_id === 'string' && item.call_id.trim() ? item.call_id.trim() : `item_${crypto.randomBytes(8).toString('hex')}`);
649
+
650
+ // Emit item added so Codex can anchor subsequent deltas by output_index/content_index/item_id.
651
+ writeSse(res, 'response.output_item.added', {
652
+ type: 'response.output_item.added',
653
+ output_index: outputIndex,
654
+ item: { ...item, id: itemId }
655
+ });
656
+
657
+ if (itemType === 'message') {
658
+ const content = Array.isArray(item.content) ? item.content : [];
659
+ for (let contentIndex = 0; contentIndex < content.length; contentIndex += 1) {
660
+ const block = content[contentIndex];
661
+ if (!block || typeof block !== 'object') continue;
662
+ if (block.type !== 'output_text') continue;
663
+ const text = typeof block.text === 'string' ? block.text : '';
664
+ if (text) {
665
+ writeSse(res, 'response.output_text.delta', {
666
+ type: 'response.output_text.delta',
667
+ item_id: itemId,
668
+ output_index: outputIndex,
669
+ content_index: contentIndex,
670
+ delta: text,
671
+ sequence_number: nextSeq()
672
+ });
673
+ }
674
+ writeSse(res, 'response.output_text.done', {
675
+ type: 'response.output_text.done',
676
+ item_id: itemId,
677
+ output_index: outputIndex,
678
+ content_index: contentIndex,
679
+ text,
680
+ sequence_number: nextSeq()
681
+ });
682
+ }
683
+ }
684
+
685
+ // Emit item done for all item types (message/function_call/etc).
686
+ writeSse(res, 'response.output_item.done', {
687
+ type: 'response.output_item.done',
688
+ output_index: outputIndex,
689
+ item: { ...item, id: itemId },
690
+ sequence_number: nextSeq()
691
+ });
692
+ }
693
+
694
+ writeSse(res, 'response.completed', { type: 'response.completed', response });
695
+ writeSse(res, 'done', '[DONE]');
696
+ }
697
+
698
+ function extractResponsesOutputText(payload) {
699
+ if (!payload || typeof payload !== 'object') return '';
700
+ const output = Array.isArray(payload.output) ? payload.output : [];
701
+ for (const item of output) {
702
+ if (!item || typeof item !== 'object') continue;
703
+ if (item.type !== 'message') continue;
704
+ const content = Array.isArray(item.content) ? item.content : [];
705
+ for (const block of content) {
706
+ if (!block || typeof block !== 'object') continue;
707
+ if (block.type !== 'output_text') continue;
708
+ if (typeof block.text === 'string') return block.text;
709
+ }
710
+ }
711
+ if (typeof payload.output_text === 'string') return payload.output_text;
712
+ return '';
713
+ }
714
+
715
+ function toUpstreamNonStreamingResponsesPayload(payload) {
716
+ const body = payload && typeof payload === 'object' ? payload : {};
717
+ const normalized = { ...body, stream: false };
718
+ if (Array.isArray(body.tools)) {
719
+ normalized.tools = normalizeResponsesToolsForResponsesApi(body.tools);
720
+ }
721
+ return normalized;
722
+ }
723
+
724
+ function shouldFallbackFromUpstreamResponses(status, bodyText) {
725
+ if (!Number.isFinite(status)) return false;
726
+ // Common "unsupported" status codes for a route.
727
+ if (status === 404 || status === 405 || status === 501) return true;
728
+
729
+ // Some OpenAI-compatible gateways respond with 500 + "not implemented" (e.g. convert_request_failed)
730
+ // instead of 404/405 for unsupported endpoints. In that case we can safely fallback to chat/completions.
731
+ const text = String(bodyText || '');
732
+ if (!text) return false;
733
+ if (/not implemented/i.test(text)) return true;
734
+ if (/convert_request_failed/i.test(text)) return true;
735
+ if (/unknown (endpoint|route)/i.test(text)) return true;
736
+ if (/unsupported.*\/?v1\/responses/i.test(text)) return true;
737
+ if (/does not support.*responses/i.test(text)) return true;
738
+ if (/name['"`]?\s+is a required property/i.test(text) && /tools/i.test(text) && /function/i.test(text)) return true;
739
+
740
+ // Best-effort parse for structured error codes.
741
+ try {
742
+ const parsed = JSON.parse(text);
743
+ const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
744
+ const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
745
+ if (code === 'convert_request_failed') return true;
746
+ if (/not implemented/i.test(msg)) return true;
747
+ if (/unknown (endpoint|route)/i.test(msg)) return true;
748
+ if (/unsupported.*\/?v1\/responses/i.test(msg)) return true;
749
+ if (/does not support.*responses/i.test(msg)) return true;
750
+ if (/name['"`]?\s+is a required property/i.test(msg) && /tools/i.test(msg) && /function/i.test(msg)) return true;
751
+ } catch (_) {}
752
+
753
+ return false;
754
+ }
755
+
756
+ // 仅识别"端点级别不支持"——可缓存,与 per-request 的 tool 格式错误区分。
757
+ function isResponsesEndpointUnsupported(status, bodyText) {
758
+ if (!Number.isFinite(status)) return false;
759
+ if (status === 404 || status === 405 || status === 501) return true;
760
+ const text = String(bodyText || '');
761
+ if (!text) return false;
762
+ if (/not implemented/i.test(text)) return true;
763
+ if (/convert_request_failed/i.test(text)) return true;
764
+ if (/unknown (endpoint|route)/i.test(text)) return true;
765
+ if (/unsupported.*\/?v1\/responses/i.test(text)) return true;
766
+ if (/does not support.*responses/i.test(text)) return true;
767
+ try {
768
+ const parsed = JSON.parse(text);
769
+ const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
770
+ const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
771
+ if (code === 'convert_request_failed') return true;
772
+ if (/not implemented/i.test(msg)) return true;
773
+ if (/unknown (endpoint|route)/i.test(msg)) return true;
774
+ if (/unsupported.*\/?v1\/responses/i.test(msg)) return true;
775
+ if (/does not support.*responses/i.test(msg)) return true;
776
+ } catch (_) {}
777
+ return false;
778
+ }
779
+
780
+ function isLoopbackAddress(address) {
781
+ if (!address) return false;
782
+ const value = String(address);
783
+ return value === '127.0.0.1' || value === '::1' || value === '::ffff:127.0.0.1';
784
+ }
785
+
786
+ function isTransientNetworkError(error) {
787
+ const text = String(error || '').trim();
788
+ if (!text) return false;
789
+ if (/socket hang up/i.test(text)) return true;
790
+ if (/ECONNRESET|ECONNREFUSED|EPIPE|EPROTO|ETIMEDOUT/i.test(text)) return true;
791
+ if (/EAI_AGAIN/i.test(text)) return true;
792
+ if (/UND_ERR_SOCKET/i.test(text)) return true;
793
+ if (/disconnected before|secure tls|tls handshake/i.test(text)) return true;
794
+ return false;
795
+ }
796
+
797
+ const TRANSIENT_RETRY_DELAYS_MS = [200, 600];
798
+
799
+ async function retryTransientRequest(executor) {
800
+ let lastResult = null;
801
+ for (let attempt = 0; attempt <= TRANSIENT_RETRY_DELAYS_MS.length; attempt += 1) {
802
+ if (attempt > 0) {
803
+ const delay = TRANSIENT_RETRY_DELAYS_MS[attempt - 1];
804
+ // eslint-disable-next-line no-await-in-loop
805
+ await new Promise((r) => {
806
+ const t = setTimeout(r, delay);
807
+ if (typeof t.unref === 'function') t.unref();
808
+ });
809
+ }
810
+ // eslint-disable-next-line no-await-in-loop
811
+ const result = await executor(attempt);
812
+ lastResult = result;
813
+ if (!result) return result;
814
+ if (result.ok) return result;
815
+ if (result.retry) return result;
816
+ if (result.status && result.status > 0) return result;
817
+ if (!isTransientNetworkError(result.error)) return result;
818
+ }
819
+ return lastResult;
820
+ }
821
+
822
+ function writeSse(res, eventName, dataObj) {
823
+ if (!res || res.writableEnded || res.destroyed) return;
824
+ if (eventName) {
825
+ res.write(`event: ${eventName}\n`);
826
+ }
827
+ if (dataObj === '[DONE]') {
828
+ res.write('data: [DONE]\n\n');
829
+ return;
830
+ }
831
+ res.write(`data: ${JSON.stringify(dataObj)}\n\n`);
832
+ }
833
+
834
+ function appendChatStreamToolCall(target, toolCall) {
835
+ if (!toolCall || typeof toolCall !== 'object') return;
836
+ const index = Number.isFinite(toolCall.index) ? toolCall.index : target.length;
837
+ if (!target[index]) {
838
+ target[index] = {
839
+ id: '',
840
+ type: 'function',
841
+ function: { name: '', arguments: '' }
842
+ };
843
+ }
844
+ const current = target[index];
845
+ if (typeof toolCall.id === 'string' && toolCall.id) current.id = toolCall.id;
846
+ if (typeof toolCall.type === 'string' && toolCall.type) current.type = toolCall.type;
847
+ const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : null;
848
+ if (fn) {
849
+ if (typeof fn.name === 'string' && fn.name) current.function.name = fn.name;
850
+ if (typeof fn.arguments === 'string') current.function.arguments += fn.arguments;
851
+ }
852
+ }
853
+
854
+ function writeChatCompletionChunkAsResponsesSse(state, chunk) {
855
+ if (!chunk || typeof chunk !== 'object') return;
856
+ if (typeof chunk.model === 'string' && chunk.model) {
857
+ state.model = chunk.model;
858
+ }
859
+ const choices = Array.isArray(chunk.choices) ? chunk.choices : [];
860
+ for (const choice of choices) {
861
+ const delta = choice && choice.delta && typeof choice.delta === 'object' ? choice.delta : null;
862
+ if (!delta) continue;
863
+
864
+ const segments = [];
865
+ // DeepSeek-style OpenAI-compatible streams may emit private reasoning in
866
+ // `reasoning_content` before the final answer. Responses `output_text`
867
+ // must stay user-visible answer text only; forwarding reasoning here
868
+ // pollutes Codex output and breaks exact-answer prompts.
869
+ if (typeof delta.content === 'string' && delta.content) {
870
+ segments.push(delta.content);
871
+ }
872
+ for (const seg of segments) {
873
+ if (!state.messageItem) {
874
+ state.messageItem = {
875
+ id: `msg_${crypto.randomBytes(8).toString('hex')}`,
876
+ type: 'message',
877
+ role: 'assistant',
878
+ content: [{ type: 'output_text', text: '' }]
879
+ };
880
+ state.output.push(state.messageItem);
881
+ writeSse(state.res, 'response.output_item.added', {
882
+ type: 'response.output_item.added',
883
+ output_index: state.output.length - 1,
884
+ item: state.messageItem
885
+ });
886
+ }
887
+ state.messageText += seg;
888
+ state.messageItem.content[0].text = state.messageText;
889
+ writeSse(state.res, 'response.output_text.delta', {
890
+ type: 'response.output_text.delta',
891
+ item_id: state.messageItem.id,
892
+ output_index: state.output.length - 1,
893
+ content_index: 0,
894
+ delta: seg,
895
+ sequence_number: state.nextSeq()
896
+ });
897
+ }
898
+
899
+ if (Array.isArray(delta.tool_calls)) {
900
+ for (const toolCall of delta.tool_calls) {
901
+ appendChatStreamToolCall(state.toolCalls, toolCall);
902
+ }
903
+ }
904
+
905
+ if (typeof choice.finish_reason === 'string' && choice.finish_reason) {
906
+ state.sawFinishReason = true;
907
+ }
908
+ }
909
+ }
910
+
911
+ function finishChatStreamResponsesSse(state) {
912
+ if (!state || state.finished) return;
913
+ state.finished = true;
914
+
915
+ if (state.messageItem) {
916
+ const outputIndex = state.output.indexOf(state.messageItem);
917
+ writeSse(state.res, 'response.output_text.done', {
918
+ type: 'response.output_text.done',
919
+ item_id: state.messageItem.id,
920
+ output_index: outputIndex,
921
+ content_index: 0,
922
+ text: state.messageText,
923
+ sequence_number: state.nextSeq()
924
+ });
925
+ writeSse(state.res, 'response.output_item.done', {
926
+ type: 'response.output_item.done',
927
+ output_index: outputIndex,
928
+ item: state.messageItem,
929
+ sequence_number: state.nextSeq()
930
+ });
931
+ }
932
+
933
+ for (const toolCall of state.toolCalls) {
934
+ if (!toolCall) continue;
935
+ const name = toolCall.function && typeof toolCall.function.name === 'string' ? toolCall.function.name : '';
936
+ if (!name) continue;
937
+ const item = {
938
+ type: 'function_call',
939
+ call_id: toolCall.id || `call_${crypto.randomBytes(8).toString('hex')}`,
940
+ name,
941
+ arguments: toolCall.function && typeof toolCall.function.arguments === 'string' ? toolCall.function.arguments : ''
942
+ };
943
+ const outputIndex = state.output.length;
944
+ state.output.push(item);
945
+ writeSse(state.res, 'response.output_item.added', {
946
+ type: 'response.output_item.added',
947
+ output_index: outputIndex,
948
+ item
949
+ });
950
+ writeSse(state.res, 'response.output_item.done', {
951
+ type: 'response.output_item.done',
952
+ output_index: outputIndex,
953
+ item,
954
+ sequence_number: state.nextSeq()
955
+ });
956
+ }
957
+
958
+ const response = ensureResponseMetadata({
959
+ id: state.responseId,
960
+ model: state.model,
961
+ created_at: state.createdAt,
962
+ status: 'completed',
963
+ output: state.output,
964
+ output_text: state.messageText
965
+ });
966
+ writeSse(state.res, 'response.completed', { type: 'response.completed', response });
967
+ writeSse(state.res, 'done', '[DONE]');
968
+ if (!state.res.writableEnded && !state.res.destroyed) {
969
+ state.res.end();
970
+ }
971
+ }
972
+
973
+ function failChatStreamResponsesSse(state, errorMessage) {
974
+ if (!state || state.finished) return;
975
+ state.finished = true;
976
+ writeSse(state.res, 'response.failed', {
977
+ type: 'response.failed',
978
+ response: ensureResponseMetadata({
979
+ id: state.responseId,
980
+ model: state.model,
981
+ created_at: state.createdAt,
982
+ status: 'failed',
983
+ output: state.output,
984
+ output_text: state.messageText
985
+ }),
986
+ error: String(errorMessage || 'upstream stream failed')
987
+ });
988
+ writeSse(state.res, 'done', '[DONE]');
989
+ if (!state.res.writableEnded && !state.res.destroyed) {
990
+ state.res.end();
991
+ }
992
+ }
993
+
994
+ function formatUpstreamStreamError(errorValue) {
995
+ if (!errorValue) return 'upstream stream failed';
996
+ if (typeof errorValue === 'string') return errorValue;
997
+ if (typeof errorValue === 'object') {
998
+ if (typeof errorValue.message === 'string' && errorValue.message) return errorValue.message;
999
+ try { return JSON.stringify(errorValue); } catch (_) {}
1000
+ }
1001
+ return String(errorValue || 'upstream stream failed');
1002
+ }
1003
+
1004
+ function streamChatCompletionsAsResponsesSse(targetUrl, options = {}) {
1005
+ const parsed = new URL(targetUrl);
1006
+ const transport = parsed.protocol === 'https:' ? https : http;
1007
+ const bodyText = options.body ? JSON.stringify(options.body) : '';
1008
+ const maxBytes = Number.isFinite(options.maxBytes) && options.maxBytes > 0
1009
+ ? Math.floor(options.maxBytes)
1010
+ : 0;
1011
+ const headers = {
1012
+ 'Accept': 'text/event-stream',
1013
+ ...(options.body ? { 'Content-Type': 'application/json' } : {}),
1014
+ ...(options.headers || {})
1015
+ };
1016
+ if (options.body) {
1017
+ headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
1018
+ }
1019
+ const timeoutMs = Number.isFinite(options.timeoutMs)
1020
+ ? Math.max(1000, Number(options.timeoutMs))
1021
+ : STREAM_IDLE_TIMEOUT_MS;
1022
+ const res = options.res;
1023
+ const fallbackModel = typeof options.model === 'string' ? options.model : '';
1024
+
1025
+ return new Promise((resolve) => {
1026
+ let settled = false;
1027
+ let upstreamReq = null;
1028
+ const finish = (value) => {
1029
+ if (settled) return;
1030
+ settled = true;
1031
+ resolve(value);
1032
+ };
1033
+ const abortUpstream = () => {
1034
+ if (upstreamReq) {
1035
+ try { upstreamReq.destroy(new Error('client aborted')); } catch (_) {}
1036
+ }
1037
+ };
1038
+ if (res && typeof res.once === 'function') {
1039
+ res.once('close', abortUpstream);
1040
+ }
1041
+
1042
+ upstreamReq = transport.request({
1043
+ protocol: parsed.protocol,
1044
+ hostname: parsed.hostname,
1045
+ port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
1046
+ method: options.method || 'POST',
1047
+ path: `${parsed.pathname}${parsed.search}`,
1048
+ headers,
1049
+ agent: parsed.protocol === 'https:' ? options.httpsAgent : options.httpAgent
1050
+ }, (upstreamRes) => {
1051
+ const status = upstreamRes.statusCode || 0;
1052
+ const chunks = [];
1053
+ let size = 0;
1054
+ const contentType = String(upstreamRes.headers && upstreamRes.headers['content-type'] || '');
1055
+
1056
+ const collectChunk = (chunk) => {
1057
+ if (!chunk) return true;
1058
+ if (maxBytes > 0) {
1059
+ size += chunk.length;
1060
+ if (size > maxBytes) {
1061
+ chunks.length = 0;
1062
+ try { upstreamRes.destroy(new Error('response too large')); } catch (_) {}
1063
+ try { upstreamReq.destroy(new Error('response too large')); } catch (_) {}
1064
+ finish({ ok: false, status, error: 'response too large' });
1065
+ return false;
1066
+ }
1067
+ }
1068
+ chunks.push(chunk);
1069
+ return true;
1070
+ };
1071
+
1072
+ if (status >= 400) {
1073
+ upstreamRes.on('data', collectChunk);
1074
+ upstreamRes.on('end', () => finish({
1075
+ ok: false,
1076
+ status,
1077
+ bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : ''
1078
+ }));
1079
+ return;
1080
+ }
1081
+
1082
+ if (!res.headersSent) {
1083
+ res.writeHead(200, {
1084
+ 'Content-Type': 'text/event-stream; charset=utf-8',
1085
+ 'Cache-Control': 'no-cache',
1086
+ 'Connection': 'keep-alive',
1087
+ 'X-Accel-Buffering': 'no'
1088
+ });
1089
+ if (typeof res.flushHeaders === 'function') res.flushHeaders();
1090
+ }
1091
+
1092
+ if (!/text\/event-stream/i.test(contentType)) {
1093
+ upstreamRes.on('data', collectChunk);
1094
+ upstreamRes.on('end', () => {
1095
+ const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
1096
+ const parsedJson = parseJsonOrError(text);
1097
+ if (parsedJson.error) {
1098
+ writeSse(res, 'response.failed', { type: 'response.failed', error: `invalid upstream response: ${parsedJson.error}` });
1099
+ writeSse(res, 'done', '[DONE]');
1100
+ if (!res.writableEnded && !res.destroyed) res.end();
1101
+ finish({ ok: true });
1102
+ return;
1103
+ }
1104
+ const extracted = extractChatCompletionResult(parsedJson.value);
1105
+ sendResponsesSse(res, buildResponsesPayloadFromChatResult(fallbackModel, extracted.text, extracted.toolCalls, parsedJson.value));
1106
+ if (!res.writableEnded && !res.destroyed) res.end();
1107
+ finish({ ok: true });
1108
+ });
1109
+ return;
1110
+ }
1111
+
1112
+ let sequence = 0;
1113
+ const state = {
1114
+ res,
1115
+ responseId: `resp_${crypto.randomBytes(10).toString('hex')}`,
1116
+ model: fallbackModel,
1117
+ createdAt: Math.floor(Date.now() / 1000),
1118
+ output: [],
1119
+ messageItem: null,
1120
+ messageText: '',
1121
+ toolCalls: [],
1122
+ finished: false,
1123
+ sawDone: false,
1124
+ sawFinishReason: false,
1125
+ nextSeq: () => {
1126
+ sequence += 1;
1127
+ return sequence;
1128
+ }
1129
+ };
1130
+ writeSse(res, 'response.created', {
1131
+ type: 'response.created',
1132
+ response: {
1133
+ id: state.responseId,
1134
+ model: state.model,
1135
+ created_at: state.createdAt
1136
+ }
1137
+ });
1138
+
1139
+ let buffer = '';
1140
+ const utf8Decoder = new StringDecoder('utf8');
1141
+ const handleEventBlock = (block) => {
1142
+ const dataLines = String(block || '')
1143
+ .split(/\r?\n/)
1144
+ .filter((line) => line.startsWith('data:'))
1145
+ .map((line) => line.slice(5).trimStart());
1146
+ if (dataLines.length === 0) return;
1147
+ const data = dataLines.join('\n').trim();
1148
+ if (!data) return;
1149
+ if (data === '[DONE]') {
1150
+ state.sawDone = true;
1151
+ finishChatStreamResponsesSse(state);
1152
+ finish({ ok: true });
1153
+ return;
1154
+ }
1155
+ const parsedChunk = parseJsonOrError(data);
1156
+ if (!parsedChunk.error) {
1157
+ if (parsedChunk.value && typeof parsedChunk.value === 'object' && parsedChunk.value.error) {
1158
+ failChatStreamResponsesSse(state, formatUpstreamStreamError(parsedChunk.value.error));
1159
+ finish({ ok: true });
1160
+ return;
1161
+ }
1162
+ writeChatCompletionChunkAsResponsesSse(state, parsedChunk.value);
1163
+ }
1164
+ };
1165
+
1166
+ upstreamRes.on('data', (chunk) => {
1167
+ if (!chunk) return;
1168
+ buffer += utf8Decoder.write(chunk);
1169
+ let boundary = buffer.search(/\r?\n\r?\n/);
1170
+ while (boundary >= 0) {
1171
+ const block = buffer.slice(0, boundary);
1172
+ const match = buffer.slice(boundary).match(/^\r?\n\r?\n/);
1173
+ buffer = buffer.slice(boundary + (match ? match[0].length : 2));
1174
+ handleEventBlock(block);
1175
+ boundary = buffer.search(/\r?\n\r?\n/);
1176
+ }
1177
+ });
1178
+ upstreamRes.on('end', () => {
1179
+ buffer += utf8Decoder.end();
1180
+ if (buffer.trim()) handleEventBlock(buffer);
1181
+ if (!state.finished && !state.sawDone && !state.sawFinishReason) {
1182
+ failChatStreamResponsesSse(state, 'upstream stream ended before [DONE]');
1183
+ finish({ ok: true });
1184
+ return;
1185
+ }
1186
+ finishChatStreamResponsesSse(state);
1187
+ finish({ ok: true });
1188
+ });
1189
+ upstreamRes.on('aborted', () => {
1190
+ failChatStreamResponsesSse(state, 'upstream stream aborted');
1191
+ finish({ ok: true });
1192
+ });
1193
+ upstreamRes.on('error', (err) => {
1194
+ failChatStreamResponsesSse(state, err && err.message ? err.message : 'upstream stream failed');
1195
+ finish({ ok: true });
1196
+ });
1197
+ });
1198
+ upstreamReq.setTimeout(timeoutMs, () => {
1199
+ try { upstreamReq.destroy(new Error('timeout')); } catch (_) {}
1200
+ finish({ ok: false, error: 'timeout' });
1201
+ });
1202
+ upstreamReq.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
1203
+ if (bodyText) upstreamReq.write(bodyText);
1204
+ upstreamReq.end();
1205
+ });
1206
+ }
1207
+
1208
+ async function proxyRequestJson(targetUrl, options = {}) {
1209
+ const parsed = new URL(targetUrl);
1210
+ const transport = parsed.protocol === 'https:' ? https : http;
1211
+ const bodyText = options.body ? JSON.stringify(options.body) : '';
1212
+ const maxBytes = Number.isFinite(options.maxBytes) && options.maxBytes > 0
1213
+ ? Math.floor(options.maxBytes)
1214
+ : 0;
1215
+ const headers = {
1216
+ 'Accept': 'application/json',
1217
+ ...(options.body ? { 'Content-Type': 'application/json' } : {}),
1218
+ ...(options.headers || {})
1219
+ };
1220
+ if (options.body) {
1221
+ headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
1222
+ }
1223
+
1224
+ const timeoutMs = Number.isFinite(options.timeoutMs)
1225
+ ? Math.max(1000, Number(options.timeoutMs))
1226
+ : REQUEST_TIMEOUT_MS;
1227
+ return new Promise((resolve) => {
1228
+ let settled = false;
1229
+ const finish = (value) => {
1230
+ if (settled) return;
1231
+ settled = true;
1232
+ resolve(value);
1233
+ };
1234
+ const req = transport.request({
1235
+ protocol: parsed.protocol,
1236
+ hostname: parsed.hostname,
1237
+ port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
1238
+ method: options.method || 'GET',
1239
+ path: `${parsed.pathname}${parsed.search}`,
1240
+ headers,
1241
+ agent: parsed.protocol === 'https:' ? options.httpsAgent : options.httpAgent
1242
+ }, (upstreamRes) => {
1243
+ const chunks = [];
1244
+ let size = 0;
1245
+ upstreamRes.on('data', (chunk) => {
1246
+ if (!chunk) return;
1247
+ if (maxBytes > 0) {
1248
+ size += chunk.length;
1249
+ if (size > maxBytes) {
1250
+ chunks.length = 0;
1251
+ try { upstreamRes.destroy(new Error('response too large')); } catch (_) {}
1252
+ try { req.destroy(new Error('response too large')); } catch (_) {}
1253
+ finish({ ok: false, error: 'response too large' });
1254
+ return;
1255
+ }
1256
+ }
1257
+ chunks.push(chunk);
1258
+ });
1259
+ upstreamRes.on('end', () => {
1260
+ const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
1261
+ finish({
1262
+ ok: true,
1263
+ status: upstreamRes.statusCode || 0,
1264
+ headers: upstreamRes.headers || {},
1265
+ bodyText: text
1266
+ });
1267
+ });
1268
+ });
1269
+ req.setTimeout(timeoutMs, () => {
1270
+ try { req.destroy(new Error('timeout')); } catch (_) {}
1271
+ finish({ ok: false, error: 'timeout' });
1272
+ });
1273
+ req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
1274
+ if (bodyText) {
1275
+ req.write(bodyText);
1276
+ }
1277
+ req.end();
1278
+ });
1279
+ }
1280
+
1281
+ function createOpenaiBridgeHttpHandler(options = {}) {
1282
+ const settingsFile = options.settingsFile;
1283
+ const expectedTokenRaw = typeof options.expectedToken === 'string' ? options.expectedToken.trim() : '';
1284
+ const expectedToken = Object.prototype.hasOwnProperty.call(options, 'expectedToken')
1285
+ ? expectedTokenRaw
1286
+ : (expectedTokenRaw || DEFAULT_BRIDGE_TOKEN);
1287
+ const maxBodySize = Number.isFinite(options.maxBodySize) ? options.maxBodySize : 0;
1288
+ const httpAgent = options.httpAgent;
1289
+ const httpsAgent = options.httpsAgent;
1290
+ const maxUpstreamBytes = Number.isFinite(options.maxUpstreamBytes) && options.maxUpstreamBytes > 0
1291
+ ? Math.floor(options.maxUpstreamBytes)
1292
+ : Math.max(16 * 1024 * 1024, maxBodySize > 0 ? maxBodySize * 4 : 0);
1293
+
1294
+ if (!settingsFile) {
1295
+ throw new Error('createOpenaiBridgeHttpHandler 缺少 settingsFile');
1296
+ }
1297
+
1298
+ // 端点不支持的缓存(per-baseUrl, TTL 30 分钟):避免每次非流式请求重复探测 /v1/responses。
1299
+ const unsupportedResponses = new Map();
1300
+ const isResponsesKnownUnsupported = (baseUrl) => {
1301
+ if (!baseUrl) return false;
1302
+ const entry = unsupportedResponses.get(baseUrl);
1303
+ if (!entry) return false;
1304
+ if (entry.expiresAt <= Date.now()) {
1305
+ unsupportedResponses.delete(baseUrl);
1306
+ return false;
1307
+ }
1308
+ return true;
1309
+ };
1310
+ const markResponsesUnsupported = (baseUrl) => {
1311
+ if (!baseUrl) return;
1312
+ unsupportedResponses.set(baseUrl, { expiresAt: Date.now() + RESPONSES_UNSUPPORTED_TTL_MS });
1313
+ };
1314
+ const clearResponsesUnsupported = (baseUrl) => {
1315
+ if (!baseUrl) return;
1316
+ unsupportedResponses.delete(baseUrl);
1317
+ };
1318
+
1319
+ const matchPath = (requestPath) => {
1320
+ const normalized = String(requestPath || '');
1321
+ const prefix = '/bridge/openai/';
1322
+ if (!normalized.startsWith(prefix)) return null;
1323
+ const rest = normalized.slice(prefix.length);
1324
+ const [provider, ...tail] = rest.split('/').filter((part) => part.length > 0);
1325
+ if (!provider) return null;
1326
+ const tailPath = '/' + tail.join('/');
1327
+ if (!tailPath.startsWith('/v1')) return null;
1328
+ const suffix = tailPath === '/v1' ? '' : tailPath.replace(/^\/v1\/?/, '');
1329
+ return { provider, suffix };
1330
+ };
1331
+
1332
+ const handler = (req, res) => {
1333
+ let parsedUrl;
1334
+ try {
1335
+ parsedUrl = new URL(req.url || '/', 'http://localhost');
1336
+ } catch (_) {
1337
+ return false;
1338
+ }
1339
+ const match = matchPath(parsedUrl.pathname || '/');
1340
+ if (!match) return false;
1341
+
1342
+ void (async () => {
1343
+ try {
1344
+ const token = extractAuthorizationToken(req);
1345
+ // 兼容:某些客户端在自定义 base_url 时可能不带 Authorization。
1346
+ // 为避免在 LAN 暴露无鉴权的代理,这里仅允许 loopback 连接缺省 token。
1347
+ const remoteAddr = req && req.socket ? req.socket.remoteAddress : '';
1348
+ const isLoopback = isLoopbackAddress(remoteAddr);
1349
+ if (!isLoopback && !expectedToken) {
1350
+ res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
1351
+ res.end(JSON.stringify({ error: 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' }));
1352
+ return;
1353
+ }
1354
+ if (!token && !isLoopback) {
1355
+ res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
1356
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
1357
+ return;
1358
+ }
1359
+ // loopback 上的本地代理:允许客户端携带任意 Authorization(例如 Codex 会附带 provider apiKey)。
1360
+ // 非 loopback 时仍强制校验 expectedToken,避免局域网被未授权调用。
1361
+ if (!isLoopback && token && token !== expectedToken) {
1362
+ res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
1363
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
1364
+ return;
1365
+ }
1366
+
1367
+ const upstream = resolveOpenaiBridgeUpstream(settingsFile, match.provider);
1368
+ if (upstream.error) {
1369
+ res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
1370
+ res.end(JSON.stringify({ error: upstream.error }));
1371
+ return;
1372
+ }
1373
+
1374
+ const suffix = match.suffix || '';
1375
+ const normalizedSuffix = suffix.replace(/^\/+/, '');
1376
+
1377
+ const authHeader = upstream.apiKey
1378
+ ? (/^bearer\s+/i.test(upstream.apiKey) ? upstream.apiKey : `Bearer ${upstream.apiKey}`)
1379
+ : '';
1380
+ const upstreamHeaders = upstream && upstream.headers && typeof upstream.headers === 'object' && !Array.isArray(upstream.headers)
1381
+ ? upstream.headers
1382
+ : {};
1383
+
1384
+ if (!normalizedSuffix) {
1385
+ if ((req.method || 'GET').toUpperCase() !== 'GET') {
1386
+ res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1387
+ res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1388
+ return;
1389
+ }
1390
+ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1391
+ res.end(JSON.stringify({
1392
+ object: 'codexmate.openai_bridge',
1393
+ provider: match.provider,
1394
+ status: 'ok',
1395
+ endpoints: ['/v1/responses', '/v1/models']
1396
+ }));
1397
+ return;
1398
+ }
1399
+
1400
+ if (normalizedSuffix === 'models') {
1401
+ if ((req.method || 'GET').toUpperCase() !== 'GET') {
1402
+ res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1403
+ res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1404
+ return;
1405
+ }
1406
+
1407
+ const url = joinApiUrl(upstream.baseUrl, 'models');
1408
+ const result = await retryTransientRequest(() => proxyRequestJson(url, {
1409
+ method: 'GET',
1410
+ headers: {
1411
+ ...(authHeader ? { Authorization: authHeader } : {}),
1412
+ ...upstreamHeaders
1413
+ },
1414
+ maxBytes: maxUpstreamBytes,
1415
+ httpAgent,
1416
+ httpsAgent
1417
+ }));
1418
+ if (!result.ok) {
1419
+ res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1420
+ res.end(JSON.stringify({ error: `Upstream request failed: ${result.error}` }));
1421
+ return;
1422
+ }
1423
+ res.writeHead(result.status || 502, { 'Content-Type': 'application/json; charset=utf-8' });
1424
+ res.end(result.bodyText || '');
1425
+ return;
1426
+ }
1427
+
1428
+ if (normalizedSuffix !== 'responses') {
1429
+ res.writeHead(404, { 'Content-Type': 'application/json; charset=utf-8' });
1430
+ res.end(JSON.stringify({ error: 'Not Found' }));
1431
+ return;
1432
+ }
1433
+
1434
+ if ((req.method || 'GET').toUpperCase() !== 'POST') {
1435
+ res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
1436
+ res.end(JSON.stringify({ error: 'Method Not Allowed' }));
1437
+ return;
1438
+ }
1439
+
1440
+ const { body, error: bodyErr } = await readRequestBody(req, maxBodySize);
1441
+ if (bodyErr) {
1442
+ res.writeHead(413, { 'Content-Type': 'application/json; charset=utf-8' });
1443
+ res.end(JSON.stringify({ error: bodyErr }));
1444
+ return;
1445
+ }
1446
+ const parsed = parseJsonOrError(body);
1447
+ if (parsed.error) {
1448
+ res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1449
+ res.end(JSON.stringify({ error: `Invalid JSON: ${parsed.error}` }));
1450
+ return;
1451
+ }
1452
+
1453
+ const responsesRequest = parsed.value;
1454
+ const streamRequested = !!(responsesRequest && typeof responsesRequest === 'object' && responsesRequest.stream === true);
1455
+ const acceptHeader = req && req.headers ? (req.headers.accept || req.headers.Accept || '') : '';
1456
+ const wantsSse = /text\/event-stream/i.test(String(acceptHeader || ''));
1457
+
1458
+ if (streamRequested && wantsSse) {
1459
+ const converted = convertResponsesRequestToChatCompletions(responsesRequest);
1460
+ if (converted.error) {
1461
+ res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1462
+ res.end(JSON.stringify({ error: converted.error }));
1463
+ return;
1464
+ }
1465
+ const upstreamUrl = joinApiUrl(upstream.baseUrl, 'chat/completions');
1466
+ const chatBody = { ...converted.chat, stream: true };
1467
+ const streamed = await retryTransientRequest(() => streamChatCompletionsAsResponsesSse(upstreamUrl, {
1468
+ method: 'POST',
1469
+ body: chatBody,
1470
+ headers: {
1471
+ ...(authHeader ? { Authorization: authHeader } : {}),
1472
+ ...upstreamHeaders
1473
+ },
1474
+ maxBytes: maxUpstreamBytes,
1475
+ httpAgent,
1476
+ httpsAgent,
1477
+ res,
1478
+ model: typeof chatBody.model === 'string' ? chatBody.model : ''
1479
+ }));
1480
+ if (!streamed.ok) {
1481
+ if (res.writableEnded || res.destroyed) {
1482
+ return;
1483
+ }
1484
+ if (!res.headersSent) {
1485
+ res.writeHead(streamed.status && streamed.status >= 400 ? streamed.status : 502, { 'Content-Type': 'application/json; charset=utf-8' });
1486
+ res.end(streamed.bodyText || JSON.stringify({ error: streamed.error || 'Upstream request failed' }));
1487
+ } else if (!res.writableEnded && !res.destroyed) {
1488
+ writeSse(res, 'response.failed', { type: 'response.failed', error: streamed.error || streamed.bodyText || 'Upstream request failed' });
1489
+ writeSse(res, 'done', '[DONE]');
1490
+ res.end();
1491
+ }
1492
+ }
1493
+ return;
1494
+ }
1495
+
1496
+ // Maxx-style behavior: prefer upstream /responses if supported.
1497
+ // Fallback to /chat/completions conversion when upstream does not implement /responses (404/405).
1498
+ // 已知不支持的上游:直接跳过探测,节省一次 round-trip。
1499
+ const skipResponsesProbe = isResponsesKnownUnsupported(upstream.baseUrl);
1500
+ const upstreamResponsesUrl = joinApiUrl(upstream.baseUrl, 'responses');
1501
+ const upstreamResponsesResult = skipResponsesProbe
1502
+ ? { ok: true, status: 404, bodyText: '' }
1503
+ : await retryTransientRequest(() => proxyRequestJson(upstreamResponsesUrl, {
1504
+ method: 'POST',
1505
+ body: toUpstreamNonStreamingResponsesPayload(responsesRequest),
1506
+ headers: {
1507
+ ...(authHeader ? { Authorization: authHeader } : {}),
1508
+ ...upstreamHeaders
1509
+ },
1510
+ maxBytes: maxUpstreamBytes,
1511
+ httpAgent,
1512
+ httpsAgent
1513
+ }));
1514
+
1515
+ if (upstreamResponsesResult.ok && upstreamResponsesResult.status >= 200 && upstreamResponsesResult.status < 300) {
1516
+ clearResponsesUnsupported(upstream.baseUrl);
1517
+ const upstreamJson = parseJsonOrError(upstreamResponsesResult.bodyText);
1518
+ if (upstreamJson.error) {
1519
+ res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1520
+ res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${upstreamJson.error}` }));
1521
+ return;
1522
+ }
1523
+ const upstreamPayload = upstreamJson.value;
1524
+ if (streamRequested && wantsSse) {
1525
+ res.writeHead(200, {
1526
+ 'Content-Type': 'text/event-stream; charset=utf-8',
1527
+ 'Cache-Control': 'no-cache',
1528
+ 'Connection': 'keep-alive',
1529
+ 'X-Accel-Buffering': 'no'
1530
+ });
1531
+ if (typeof res.flushHeaders === 'function') res.flushHeaders();
1532
+ sendResponsesSse(res, upstreamPayload);
1533
+ res.end();
1534
+ return;
1535
+ }
1536
+
1537
+ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1538
+ res.end(JSON.stringify(ensureResponseMetadata(upstreamPayload)));
1539
+ return;
1540
+ }
1541
+
1542
+ if (upstreamResponsesResult.ok && upstreamResponsesResult.status >= 400) {
1543
+ if (!shouldFallbackFromUpstreamResponses(upstreamResponsesResult.status, upstreamResponsesResult.bodyText)) {
1544
+ res.writeHead(upstreamResponsesResult.status, { 'Content-Type': 'application/json; charset=utf-8' });
1545
+ res.end(upstreamResponsesResult.bodyText || JSON.stringify({ error: 'Upstream error' }));
1546
+ return;
1547
+ }
1548
+ if (!skipResponsesProbe && isResponsesEndpointUnsupported(upstreamResponsesResult.status, upstreamResponsesResult.bodyText)) {
1549
+ markResponsesUnsupported(upstream.baseUrl);
1550
+ }
1551
+ // fallthrough to chat/completions conversion
1552
+ }
1553
+
1554
+ if (!upstreamResponsesResult.ok) {
1555
+ res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1556
+ res.end(JSON.stringify({ error: `Upstream request failed: ${upstreamResponsesResult.error}` }));
1557
+ return;
1558
+ }
1559
+
1560
+ const converted = convertResponsesRequestToChatCompletions(responsesRequest);
1561
+ if (converted.error) {
1562
+ res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
1563
+ res.end(JSON.stringify({ error: converted.error }));
1564
+ return;
1565
+ }
1566
+
1567
+ const upstreamUrl = joinApiUrl(upstream.baseUrl, 'chat/completions');
1568
+ const upstreamResult = await retryTransientRequest(() => proxyRequestJson(upstreamUrl, {
1569
+ method: 'POST',
1570
+ body: converted.chat,
1571
+ headers: {
1572
+ ...(authHeader ? { Authorization: authHeader } : {}),
1573
+ ...upstreamHeaders
1574
+ },
1575
+ maxBytes: maxUpstreamBytes,
1576
+ httpAgent,
1577
+ httpsAgent
1578
+ }));
1579
+ if (!upstreamResult.ok) {
1580
+ res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1581
+ res.end(JSON.stringify({ error: `Upstream request failed: ${upstreamResult.error}` }));
1582
+ return;
1583
+ }
1584
+
1585
+ const upstreamJson = parseJsonOrError(upstreamResult.bodyText);
1586
+ if (upstreamResult.status >= 400) {
1587
+ res.writeHead(upstreamResult.status, { 'Content-Type': 'application/json; charset=utf-8' });
1588
+ res.end(upstreamResult.bodyText || JSON.stringify({ error: 'Upstream error' }));
1589
+ return;
1590
+ }
1591
+ if (upstreamJson.error) {
1592
+ res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
1593
+ res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${upstreamJson.error}` }));
1594
+ return;
1595
+ }
1596
+
1597
+ const model = typeof converted.chat.model === 'string' ? converted.chat.model : '';
1598
+ const extracted = extractChatCompletionResult(upstreamJson.value);
1599
+ const text = extracted && typeof extracted.text === 'string' ? extracted.text : '';
1600
+ const toolCalls = extracted && Array.isArray(extracted.toolCalls) ? extracted.toolCalls : [];
1601
+ const responsesPayload = buildResponsesPayloadFromChatResult(model, text, toolCalls, upstreamJson.value);
1602
+
1603
+ if (converted.streamRequested && wantsSse) {
1604
+ res.writeHead(200, {
1605
+ 'Content-Type': 'text/event-stream; charset=utf-8',
1606
+ 'Cache-Control': 'no-cache',
1607
+ 'Connection': 'keep-alive',
1608
+ 'X-Accel-Buffering': 'no'
1609
+ });
1610
+ if (typeof res.flushHeaders === 'function') res.flushHeaders();
1611
+ sendResponsesSse(res, responsesPayload);
1612
+ res.end();
1613
+ return;
1614
+ }
1615
+
1616
+ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
1617
+ res.end(JSON.stringify(ensureResponseMetadata(responsesPayload)));
1618
+ } catch (e) {
1619
+ res.writeHead(500, { 'Content-Type': 'application/json; charset=utf-8' });
1620
+ res.end(JSON.stringify({ error: e && e.message ? e.message : 'Internal Error' }));
1621
+ }
1622
+ })();
1623
+
1624
+ return true;
1625
+ };
1626
+
1627
+ handler.matchPath = matchPath;
1628
+ return handler;
1629
+ }
1630
+
1631
+ module.exports = {
1632
+ readOpenaiBridgeSettings,
1633
+ upsertOpenaiBridgeProvider,
1634
+ resolveOpenaiBridgeUpstream,
1635
+ createOpenaiBridgeHttpHandler,
1636
+ // exported for local-bridge reuse
1637
+ convertResponsesRequestToChatCompletions,
1638
+ streamChatCompletionsAsResponsesSse,
1639
+ proxyRequestJson,
1640
+ ensureResponseMetadata,
1641
+ sendResponsesSse,
1642
+ extractAuthorizationToken,
1643
+ readRequestBody,
1644
+ parseJsonOrError,
1645
+ extractChatCompletionResult,
1646
+ buildResponsesPayloadFromChatResult,
1647
+ retryTransientRequest,
1648
+ normalizeOpenaiUpstreamBaseUrl,
1649
+ extractResponsesOutputText,
1650
+ shouldFallbackFromUpstreamResponses,
1651
+ isTransientNetworkError,
1652
+ isLoopbackAddress
1653
+ };