codexmate 0.0.32 → 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -363
- package/README.zh.md +147 -371
- package/cli/agents-files.js +230 -224
- package/cli/archive-helpers.js +453 -446
- package/cli/auth-profiles.js +375 -375
- package/cli/builtin-proxy.js +1725 -1725
- package/cli/claude-proxy.js +1022 -1022
- package/cli/config-bootstrap.js +402 -402
- package/cli/config-health.js +454 -454
- package/cli/doctor-core.js +903 -903
- package/cli/import-skills-url.js +356 -356
- package/cli/local-bridge.js +556 -324
- package/cli/openai-bridge.js +1653 -1653
- package/cli/openclaw-config.js +629 -629
- package/cli/session-convert-args.js +69 -69
- package/cli/session-convert-io.js +82 -82
- package/cli/session-convert.js +150 -150
- package/cli/session-usage.concurrent.js +28 -28
- package/cli/session-usage.js +118 -118
- package/cli/session-usage.models.js +176 -176
- package/cli/skills.js +1141 -1141
- package/cli/update.js +171 -0
- package/cli/zip-commands.js +510 -510
- package/cli.js +16079 -15829
- package/lib/automation.js +404 -404
- package/lib/cli-file-utils.js +151 -151
- package/lib/cli-models-utils.js +440 -440
- package/lib/cli-network-utils.js +190 -190
- package/lib/cli-path-utils.js +85 -85
- package/lib/cli-session-utils.js +121 -121
- package/lib/cli-sessions.js +427 -426
- package/lib/cli-utils.js +155 -155
- package/lib/cli-webhook.js +154 -126
- package/lib/download-artifacts.js +92 -92
- package/lib/mcp-stdio.js +453 -453
- package/lib/task-orchestrator.js +869 -869
- package/lib/text-diff.js +303 -303
- package/lib/win-tray.js +119 -0
- package/lib/workflow-engine.js +340 -340
- package/package.json +76 -76
- package/plugins/README.md +20 -20
- package/plugins/README.zh-CN.md +20 -20
- package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
- package/plugins/prompt-templates/computed.mjs +253 -253
- package/plugins/prompt-templates/index.mjs +8 -8
- package/plugins/prompt-templates/manifest.mjs +15 -15
- package/plugins/prompt-templates/methods.mjs +553 -553
- package/plugins/prompt-templates/overview.mjs +91 -91
- package/plugins/prompt-templates/ownership.mjs +19 -19
- package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
- package/plugins/prompt-templates/storage.mjs +64 -64
- package/plugins/registry.mjs +16 -16
- package/web-ui/app.js +654 -647
- package/web-ui/index.html +37 -36
- package/web-ui/logic.agents-diff.mjs +386 -386
- package/web-ui/logic.claude.mjs +172 -168
- package/web-ui/logic.codex.mjs +69 -69
- package/web-ui/logic.mjs +5 -5
- package/web-ui/logic.runtime.mjs +128 -128
- package/web-ui/logic.session-convert.mjs +70 -70
- package/web-ui/logic.sessions.mjs +781 -781
- package/web-ui/modules/api.mjs +90 -90
- package/web-ui/modules/app.computed.dashboard.mjs +248 -248
- package/web-ui/modules/app.computed.index.mjs +17 -17
- package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
- package/web-ui/modules/app.computed.session.mjs +735 -693
- package/web-ui/modules/app.constants.mjs +15 -15
- package/web-ui/modules/app.methods.agents.mjs +651 -651
- package/web-ui/modules/app.methods.claude-config.mjs +306 -200
- package/web-ui/modules/app.methods.codex-config.mjs +869 -861
- package/web-ui/modules/app.methods.index.mjs +94 -94
- package/web-ui/modules/app.methods.install.mjs +205 -205
- package/web-ui/modules/app.methods.navigation.mjs +788 -774
- package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
- package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
- package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
- package/web-ui/modules/app.methods.providers.mjs +575 -529
- package/web-ui/modules/app.methods.runtime.mjs +345 -345
- package/web-ui/modules/app.methods.session-actions.mjs +591 -591
- package/web-ui/modules/app.methods.session-browser.mjs +1011 -1012
- package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
- package/web-ui/modules/app.methods.session-trash.mjs +438 -438
- package/web-ui/modules/app.methods.startup-claude.mjs +547 -537
- package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
- package/web-ui/modules/app.methods.webhook.mjs +87 -79
- package/web-ui/modules/config-mode.computed.mjs +124 -124
- package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
- package/web-ui/modules/i18n.dict.mjs +3195 -3177
- package/web-ui/modules/i18n.mjs +62 -62
- package/web-ui/modules/plugins.computed.mjs +3 -3
- package/web-ui/modules/plugins.methods.mjs +3 -3
- package/web-ui/modules/plugins.storage.mjs +11 -11
- package/web-ui/modules/provider-url-display.mjs +17 -17
- package/web-ui/modules/sessions-filters-url.mjs +85 -85
- package/web-ui/modules/skills.computed.mjs +107 -107
- package/web-ui/modules/skills.methods.mjs +482 -482
- package/web-ui/partials/index/layout-footer.html +13 -13
- package/web-ui/partials/index/layout-header.html +499 -503
- package/web-ui/partials/index/modal-config-template-agents.html +185 -185
- package/web-ui/partials/index/modal-confirm-toast.html +32 -32
- package/web-ui/partials/index/modal-health-check.html +45 -45
- package/web-ui/partials/index/modal-openclaw-config.html +280 -280
- package/web-ui/partials/index/modal-skills.html +200 -200
- package/web-ui/partials/index/modal-webhook.html +42 -0
- package/web-ui/partials/index/modals-basic.html +223 -162
- package/web-ui/partials/index/panel-config-claude.html +155 -136
- package/web-ui/partials/index/panel-config-codex.html +176 -196
- package/web-ui/partials/index/panel-config-codex.html.bak +337 -0
- package/web-ui/partials/index/panel-config-openclaw.html +83 -83
- package/web-ui/partials/index/panel-dashboard.html +186 -219
- package/web-ui/partials/index/panel-docs.html +114 -114
- package/web-ui/partials/index/panel-market.html +177 -177
- package/web-ui/partials/index/panel-orchestration.html +391 -391
- package/web-ui/partials/index/panel-plugins.html +253 -253
- package/web-ui/partials/index/panel-sessions.html +319 -313
- package/web-ui/partials/index/panel-settings.html +158 -190
- package/web-ui/partials/index/panel-trash.html +82 -82
- package/web-ui/partials/index/panel-usage.html +137 -137
- package/web-ui/res/json5.min.js +1 -1
- package/web-ui/res/vue.global.prod.js +13 -13
- package/web-ui/session-helpers.mjs +591 -591
- package/web-ui/source-bundle.cjs +233 -233
- package/web-ui/styles/base-theme.css +281 -281
- package/web-ui/styles/bridge-pool.css +266 -197
- package/web-ui/styles/controls-forms.css +433 -433
- package/web-ui/styles/dashboard.css +406 -406
- package/web-ui/styles/docs-panel.css +245 -245
- package/web-ui/styles/feedback.css +108 -108
- package/web-ui/styles/health-check-dialog.css +144 -144
- package/web-ui/styles/layout-shell.css +628 -638
- package/web-ui/styles/modals-core.css +499 -466
- package/web-ui/styles/navigation-panels.css +391 -391
- package/web-ui/styles/openclaw-structured.css +266 -266
- package/web-ui/styles/plugins-panel.css +564 -564
- package/web-ui/styles/responsive.css +392 -392
- package/web-ui/styles/sessions-list.css +683 -647
- package/web-ui/styles/sessions-preview.css +407 -407
- package/web-ui/styles/sessions-toolbar-trash.css +518 -518
- package/web-ui/styles/sessions-usage.css +588 -588
- package/web-ui/styles/settings-panel.css +415 -349
- package/web-ui/styles/skills-list.css +305 -305
- package/web-ui/styles/skills-market.css +429 -429
- package/web-ui/styles/task-orchestration.css +822 -822
- package/web-ui/styles/titles-cards.css +472 -472
- package/web-ui/styles/trash-panel.css +90 -90
- package/web-ui/styles/webhook.css +115 -81
- package/web-ui/styles.css +24 -24
- package/web-ui.html +17 -17
package/cli/builtin-proxy.js
CHANGED
|
@@ -1,1725 +1,1725 @@
|
|
|
1
|
-
const http = require('http');
|
|
2
|
-
const net = require('net');
|
|
3
|
-
const crypto = require('crypto');
|
|
4
|
-
const toml = require('@iarna/toml');
|
|
5
|
-
const { readJsonFile, writeJsonAtomic } = require('../lib/cli-file-utils');
|
|
6
|
-
const { isValidHttpUrl, normalizeBaseUrl, joinApiUrl } = require('../lib/cli-utils');
|
|
7
|
-
const { toIsoTime } = require('../lib/cli-session-utils');
|
|
8
|
-
|
|
9
|
-
function createBuiltinProxyRuntimeController(deps = {}) {
|
|
10
|
-
const {
|
|
11
|
-
fs,
|
|
12
|
-
https,
|
|
13
|
-
CONFIG_FILE,
|
|
14
|
-
BUILTIN_PROXY_SETTINGS_FILE,
|
|
15
|
-
DEFAULT_BUILTIN_PROXY_SETTINGS,
|
|
16
|
-
BUILTIN_PROXY_PROVIDER_NAME,
|
|
17
|
-
CODEXMATE_MANAGED_MARKER,
|
|
18
|
-
HTTP_KEEP_ALIVE_AGENT,
|
|
19
|
-
HTTPS_KEEP_ALIVE_AGENT,
|
|
20
|
-
readConfig,
|
|
21
|
-
writeConfig,
|
|
22
|
-
readConfigOrVirtualDefault,
|
|
23
|
-
resolveAuthTokenFromCurrentProfile,
|
|
24
|
-
isPlainObject,
|
|
25
|
-
isBuiltinManagedProvider,
|
|
26
|
-
findProviderSectionRanges,
|
|
27
|
-
findProviderDescendantSectionRanges,
|
|
28
|
-
normalizeLegacySegments,
|
|
29
|
-
buildLegacySegmentsKey,
|
|
30
|
-
formatHostForUrl
|
|
31
|
-
} = deps;
|
|
32
|
-
|
|
33
|
-
if (!fs) throw new Error('createBuiltinProxyRuntimeController 缺少 fs');
|
|
34
|
-
if (!https) throw new Error('createBuiltinProxyRuntimeController 缺少 https');
|
|
35
|
-
if (!CONFIG_FILE) throw new Error('createBuiltinProxyRuntimeController 缺少 CONFIG_FILE');
|
|
36
|
-
if (!BUILTIN_PROXY_SETTINGS_FILE) throw new Error('createBuiltinProxyRuntimeController 缺少 BUILTIN_PROXY_SETTINGS_FILE');
|
|
37
|
-
if (!DEFAULT_BUILTIN_PROXY_SETTINGS || typeof DEFAULT_BUILTIN_PROXY_SETTINGS !== 'object') {
|
|
38
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 DEFAULT_BUILTIN_PROXY_SETTINGS');
|
|
39
|
-
}
|
|
40
|
-
if (!BUILTIN_PROXY_PROVIDER_NAME) throw new Error('createBuiltinProxyRuntimeController 缺少 BUILTIN_PROXY_PROVIDER_NAME');
|
|
41
|
-
if (typeof readConfig !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 readConfig');
|
|
42
|
-
if (typeof writeConfig !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 writeConfig');
|
|
43
|
-
if (typeof readConfigOrVirtualDefault !== 'function') {
|
|
44
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 readConfigOrVirtualDefault');
|
|
45
|
-
}
|
|
46
|
-
if (typeof resolveAuthTokenFromCurrentProfile !== 'function') {
|
|
47
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 resolveAuthTokenFromCurrentProfile');
|
|
48
|
-
}
|
|
49
|
-
if (typeof isPlainObject !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 isPlainObject');
|
|
50
|
-
if (typeof isBuiltinManagedProvider !== 'function') {
|
|
51
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 isBuiltinManagedProvider');
|
|
52
|
-
}
|
|
53
|
-
if (typeof findProviderSectionRanges !== 'function') {
|
|
54
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 findProviderSectionRanges');
|
|
55
|
-
}
|
|
56
|
-
if (typeof findProviderDescendantSectionRanges !== 'function') {
|
|
57
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 findProviderDescendantSectionRanges');
|
|
58
|
-
}
|
|
59
|
-
if (typeof normalizeLegacySegments !== 'function') {
|
|
60
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 normalizeLegacySegments');
|
|
61
|
-
}
|
|
62
|
-
if (typeof buildLegacySegmentsKey !== 'function') {
|
|
63
|
-
throw new Error('createBuiltinProxyRuntimeController 缺少 buildLegacySegmentsKey');
|
|
64
|
-
}
|
|
65
|
-
if (typeof formatHostForUrl !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 formatHostForUrl');
|
|
66
|
-
|
|
67
|
-
let runtime = null;
|
|
68
|
-
|
|
69
|
-
function readRequestBody(req, maxBytes) {
|
|
70
|
-
return new Promise((resolve) => {
|
|
71
|
-
let body = '';
|
|
72
|
-
let size = 0;
|
|
73
|
-
let aborted = false;
|
|
74
|
-
req.on('data', (chunk) => {
|
|
75
|
-
if (aborted) return;
|
|
76
|
-
size += chunk.length;
|
|
77
|
-
if (Number.isFinite(maxBytes) && maxBytes > 0 && size > maxBytes) {
|
|
78
|
-
aborted = true;
|
|
79
|
-
try { req.destroy(); } catch (_) {}
|
|
80
|
-
resolve({ error: '请求体过大' });
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
body += chunk;
|
|
84
|
-
});
|
|
85
|
-
req.on('end', () => {
|
|
86
|
-
if (aborted) return;
|
|
87
|
-
resolve({ body });
|
|
88
|
-
});
|
|
89
|
-
req.on('error', (err) => resolve({ error: err && err.message ? err.message : 'request failed' }));
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function parseJsonOrError(text) {
|
|
94
|
-
if (typeof text !== 'string' || !text.trim()) {
|
|
95
|
-
return { value: null, error: 'empty body' };
|
|
96
|
-
}
|
|
97
|
-
try {
|
|
98
|
-
return { value: JSON.parse(text), error: '' };
|
|
99
|
-
} catch (e) {
|
|
100
|
-
return { value: null, error: e && e.message ? e.message : 'invalid json' };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function shouldFallbackFromUpstreamResponses(status, bodyText) {
|
|
105
|
-
if (!Number.isFinite(status)) return false;
|
|
106
|
-
if (status === 404 || status === 405 || status === 501) return true;
|
|
107
|
-
const text = String(bodyText || '');
|
|
108
|
-
if (!text) return false;
|
|
109
|
-
if (/not implemented/i.test(text)) return true;
|
|
110
|
-
if (/convert_request_failed/i.test(text)) return true;
|
|
111
|
-
try {
|
|
112
|
-
const parsed = JSON.parse(text);
|
|
113
|
-
const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
|
|
114
|
-
const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
|
|
115
|
-
if (code === 'convert_request_failed') return true;
|
|
116
|
-
if (/not implemented/i.test(msg)) return true;
|
|
117
|
-
} catch (_) {}
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function shouldFallbackFromUpstreamResponsesFailure(error) {
|
|
122
|
-
const text = String(error || '').trim();
|
|
123
|
-
if (!text) return false;
|
|
124
|
-
if (/timeout/i.test(text)) return true;
|
|
125
|
-
if (/socket hang up/i.test(text)) return true;
|
|
126
|
-
if (/ECONNRESET/i.test(text)) return true;
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function isTransientNetworkError(error) {
|
|
131
|
-
const text = String(error || '').trim();
|
|
132
|
-
if (!text) return false;
|
|
133
|
-
if (/socket hang up/i.test(text)) return true;
|
|
134
|
-
if (/ECONNRESET|ECONNREFUSED|EPIPE|EPROTO|ETIMEDOUT/i.test(text)) return true;
|
|
135
|
-
if (/EAI_AGAIN/i.test(text)) return true;
|
|
136
|
-
if (/UND_ERR_SOCKET/i.test(text)) return true;
|
|
137
|
-
if (/disconnected before|secure tls|tls handshake/i.test(text)) return true;
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const TRANSIENT_RETRY_DELAYS_MS = [200, 600];
|
|
142
|
-
|
|
143
|
-
async function retryTransientRequest(executor) {
|
|
144
|
-
let lastResult = null;
|
|
145
|
-
for (let attempt = 0; attempt <= TRANSIENT_RETRY_DELAYS_MS.length; attempt += 1) {
|
|
146
|
-
if (attempt > 0) {
|
|
147
|
-
const delay = TRANSIENT_RETRY_DELAYS_MS[attempt - 1];
|
|
148
|
-
// eslint-disable-next-line no-await-in-loop
|
|
149
|
-
await new Promise((r) => {
|
|
150
|
-
const t = setTimeout(r, delay);
|
|
151
|
-
if (typeof t.unref === 'function') t.unref();
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
// eslint-disable-next-line no-await-in-loop
|
|
155
|
-
const result = await executor(attempt);
|
|
156
|
-
lastResult = result;
|
|
157
|
-
if (!result) return result;
|
|
158
|
-
if (result.ok) return result;
|
|
159
|
-
if (result.retry) return result;
|
|
160
|
-
if (result.status && result.status > 0) return result;
|
|
161
|
-
if (!isTransientNetworkError(result.error)) return result;
|
|
162
|
-
}
|
|
163
|
-
return lastResult;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function proxyRequestJson(targetUrl, options = {}) {
|
|
167
|
-
const parsed = new URL(targetUrl);
|
|
168
|
-
const transport = parsed.protocol === 'https:' ? https : http;
|
|
169
|
-
const bodyText = options.body ? JSON.stringify(options.body) : '';
|
|
170
|
-
const headers = {
|
|
171
|
-
'Accept': 'application/json',
|
|
172
|
-
...(options.body ? { 'Content-Type': 'application/json' } : {}),
|
|
173
|
-
...(options.headers || {})
|
|
174
|
-
};
|
|
175
|
-
if (options.body) {
|
|
176
|
-
headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
|
|
177
|
-
}
|
|
178
|
-
const timeoutMs = Number.isFinite(options.timeoutMs)
|
|
179
|
-
? Math.max(1000, Number(options.timeoutMs))
|
|
180
|
-
: 30000;
|
|
181
|
-
|
|
182
|
-
return new Promise((resolve) => {
|
|
183
|
-
let settled = false;
|
|
184
|
-
const finish = (value) => {
|
|
185
|
-
if (settled) return;
|
|
186
|
-
settled = true;
|
|
187
|
-
resolve(value);
|
|
188
|
-
};
|
|
189
|
-
const req = transport.request({
|
|
190
|
-
protocol: parsed.protocol,
|
|
191
|
-
hostname: parsed.hostname,
|
|
192
|
-
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
193
|
-
method: options.method || 'GET',
|
|
194
|
-
path: `${parsed.pathname}${parsed.search}`,
|
|
195
|
-
headers,
|
|
196
|
-
agent: parsed.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
197
|
-
}, (upstreamRes) => {
|
|
198
|
-
const chunks = [];
|
|
199
|
-
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
200
|
-
upstreamRes.on('end', () => {
|
|
201
|
-
const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
|
|
202
|
-
finish({
|
|
203
|
-
ok: true,
|
|
204
|
-
status: upstreamRes.statusCode || 0,
|
|
205
|
-
headers: upstreamRes.headers || {},
|
|
206
|
-
bodyText: text
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
req.setTimeout(timeoutMs, () => {
|
|
211
|
-
try { req.destroy(new Error('timeout')); } catch (_) {}
|
|
212
|
-
finish({ ok: false, error: 'timeout' });
|
|
213
|
-
});
|
|
214
|
-
req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
|
|
215
|
-
if (bodyText) {
|
|
216
|
-
req.write(bodyText);
|
|
217
|
-
}
|
|
218
|
-
req.end();
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function buildUpstreamUrlCandidates(baseUrl, pathSuffix) {
|
|
223
|
-
const safeSuffix = String(pathSuffix || '').replace(/^\/+/, '');
|
|
224
|
-
const candidates = [];
|
|
225
|
-
const push = (url) => {
|
|
226
|
-
if (url && !candidates.includes(url)) {
|
|
227
|
-
candidates.push(url);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
push(joinApiUrl(baseUrl, safeSuffix));
|
|
231
|
-
const trimmed = normalizeBaseUrl(baseUrl);
|
|
232
|
-
if (trimmed && safeSuffix) {
|
|
233
|
-
push(`${trimmed}/${safeSuffix}`);
|
|
234
|
-
}
|
|
235
|
-
return candidates;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async function proxyRequestJsonWithFallbackUrls(baseUrl, pathSuffix, options = {}) {
|
|
239
|
-
const urls = buildUpstreamUrlCandidates(baseUrl, pathSuffix);
|
|
240
|
-
if (urls.length === 0) {
|
|
241
|
-
return { ok: false, error: 'failed to build upstream URL' };
|
|
242
|
-
}
|
|
243
|
-
let lastResult = null;
|
|
244
|
-
for (let index = 0; index < urls.length; index += 1) {
|
|
245
|
-
const result = await retryTransientRequest(() => proxyRequestJson(urls[index], options));
|
|
246
|
-
lastResult = result;
|
|
247
|
-
if (!result.ok) {
|
|
248
|
-
return result;
|
|
249
|
-
}
|
|
250
|
-
if (!(result.status === 404 || result.status === 405)) {
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return lastResult || { ok: false, error: 'failed to build upstream URL' };
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function stringifyJsonValue(value, fallback = '') {
|
|
258
|
-
if (typeof value === 'string') return value;
|
|
259
|
-
if (value == null) return fallback;
|
|
260
|
-
try {
|
|
261
|
-
return JSON.stringify(value);
|
|
262
|
-
} catch (_) {
|
|
263
|
-
return fallback;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function normalizeChatUsageToResponsesUsage(usage) {
|
|
268
|
-
if (!usage || typeof usage !== 'object' || Array.isArray(usage)) return undefined;
|
|
269
|
-
const pickNumber = (...keys) => {
|
|
270
|
-
for (const key of keys) {
|
|
271
|
-
if (Number.isFinite(usage[key])) return usage[key];
|
|
272
|
-
}
|
|
273
|
-
return undefined;
|
|
274
|
-
};
|
|
275
|
-
const inputTokens = pickNumber('input_tokens', 'prompt_tokens');
|
|
276
|
-
const outputTokens = pickNumber('output_tokens', 'completion_tokens');
|
|
277
|
-
const totalTokens = pickNumber('total_tokens');
|
|
278
|
-
const result = {};
|
|
279
|
-
if (inputTokens != null) result.input_tokens = inputTokens;
|
|
280
|
-
if (outputTokens != null) result.output_tokens = outputTokens;
|
|
281
|
-
if (totalTokens != null) result.total_tokens = totalTokens;
|
|
282
|
-
if (usage.input_tokens_details && typeof usage.input_tokens_details === 'object') {
|
|
283
|
-
result.input_tokens_details = usage.input_tokens_details;
|
|
284
|
-
} else if (usage.prompt_tokens_details && typeof usage.prompt_tokens_details === 'object') {
|
|
285
|
-
result.input_tokens_details = usage.prompt_tokens_details;
|
|
286
|
-
}
|
|
287
|
-
if (usage.output_tokens_details && typeof usage.output_tokens_details === 'object') {
|
|
288
|
-
result.output_tokens_details = usage.output_tokens_details;
|
|
289
|
-
} else if (usage.completion_tokens_details && typeof usage.completion_tokens_details === 'object') {
|
|
290
|
-
result.output_tokens_details = usage.completion_tokens_details;
|
|
291
|
-
}
|
|
292
|
-
return Object.keys(result).length > 0 ? result : usage;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function mapChatFinishReasonToResponses(choice) {
|
|
296
|
-
const finishReason = choice && typeof choice === 'object' && typeof choice.finish_reason === 'string'
|
|
297
|
-
? choice.finish_reason
|
|
298
|
-
: '';
|
|
299
|
-
if (finishReason === 'length') {
|
|
300
|
-
return { status: 'incomplete', incomplete_details: { reason: 'max_output_tokens' } };
|
|
301
|
-
}
|
|
302
|
-
if (finishReason === 'content_filter') {
|
|
303
|
-
return { status: 'incomplete', incomplete_details: { reason: 'content_filter' } };
|
|
304
|
-
}
|
|
305
|
-
return { status: 'completed' };
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function normalizeChatMessageContentToResponsesContent(content, refusal = '') {
|
|
309
|
-
const blocks = [];
|
|
310
|
-
const pushText = (text) => {
|
|
311
|
-
if (typeof text === 'string' && text) {
|
|
312
|
-
blocks.push({ type: 'output_text', text });
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
if (typeof content === 'string') {
|
|
316
|
-
pushText(content);
|
|
317
|
-
} else if (Array.isArray(content)) {
|
|
318
|
-
for (const item of content) {
|
|
319
|
-
if (!item) continue;
|
|
320
|
-
if (typeof item === 'string') {
|
|
321
|
-
pushText(item);
|
|
322
|
-
continue;
|
|
323
|
-
}
|
|
324
|
-
if (typeof item !== 'object') continue;
|
|
325
|
-
const type = typeof item.type === 'string' ? item.type : '';
|
|
326
|
-
if ((type === 'text' || type === 'output_text') && typeof item.text === 'string') {
|
|
327
|
-
pushText(item.text);
|
|
328
|
-
continue;
|
|
329
|
-
}
|
|
330
|
-
if (typeof item.content === 'string') {
|
|
331
|
-
pushText(item.content);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
if (typeof refusal === 'string' && refusal) {
|
|
336
|
-
blocks.push({ type: 'refusal', refusal });
|
|
337
|
-
}
|
|
338
|
-
return blocks;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function buildResponsesPayloadFromChatCompletion(payload, fallbackModel = '') {
|
|
342
|
-
const base = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
343
|
-
const choice = Array.isArray(base.choices) ? base.choices[0] : null;
|
|
344
|
-
const message = choice && typeof choice === 'object' && choice.message && typeof choice.message === 'object'
|
|
345
|
-
? choice.message
|
|
346
|
-
: {};
|
|
347
|
-
const output = [];
|
|
348
|
-
const messageContent = normalizeChatMessageContentToResponsesContent(message.content, message.refusal);
|
|
349
|
-
if (messageContent.length > 0 || !Array.isArray(message.tool_calls) || message.tool_calls.length === 0) {
|
|
350
|
-
output.push({
|
|
351
|
-
type: 'message',
|
|
352
|
-
role: 'assistant',
|
|
353
|
-
content: messageContent.length > 0 ? messageContent : [{ type: 'output_text', text: '' }]
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
if (Array.isArray(message.tool_calls)) {
|
|
357
|
-
for (const toolCall of message.tool_calls) {
|
|
358
|
-
if (!toolCall || typeof toolCall !== 'object') continue;
|
|
359
|
-
const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : {};
|
|
360
|
-
const name = typeof fn.name === 'string' ? fn.name : '';
|
|
361
|
-
if (!name) continue;
|
|
362
|
-
output.push({
|
|
363
|
-
type: 'function_call',
|
|
364
|
-
call_id: typeof toolCall.id === 'string' && toolCall.id ? toolCall.id : `call_${crypto.randomBytes(8).toString('hex')}`,
|
|
365
|
-
name,
|
|
366
|
-
arguments: stringifyJsonValue(fn.arguments, '{}')
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
const finish = mapChatFinishReasonToResponses(choice);
|
|
371
|
-
return ensureResponseMetadata({
|
|
372
|
-
id: typeof base.id === 'string' ? base.id : undefined,
|
|
373
|
-
model: typeof base.model === 'string' ? base.model : fallbackModel,
|
|
374
|
-
status: finish.status,
|
|
375
|
-
...(finish.incomplete_details ? { incomplete_details: finish.incomplete_details } : {}),
|
|
376
|
-
output,
|
|
377
|
-
usage: normalizeChatUsageToResponsesUsage(base.usage)
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
function normalizeResponsesInputToChatMessages(input) {
|
|
382
|
-
// 参考 cc-switch 的 Responses 转换形态:message content 保持为消息,function_call /
|
|
383
|
-
// function_call_output 提升为 OpenAI Chat 的 assistant tool_calls / tool 消息。
|
|
384
|
-
const toChatContent = (blocks) => {
|
|
385
|
-
if (!Array.isArray(blocks)) return '';
|
|
386
|
-
const out = [];
|
|
387
|
-
for (const block of blocks) {
|
|
388
|
-
if (!block || typeof block !== 'object') continue;
|
|
389
|
-
const type = typeof block.type === 'string' ? block.type : '';
|
|
390
|
-
if ((type === 'input_text' || type === 'output_text' || type === 'text') && typeof block.text === 'string') {
|
|
391
|
-
out.push({ type: 'text', text: block.text });
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
if (type === 'refusal' && typeof block.refusal === 'string') {
|
|
395
|
-
out.push({ type: 'text', text: block.refusal });
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
if (type === 'input_image') {
|
|
399
|
-
const raw = block.image_url != null ? block.image_url : block.imageUrl;
|
|
400
|
-
const url = typeof raw === 'string'
|
|
401
|
-
? raw
|
|
402
|
-
: (raw && typeof raw === 'object' && typeof raw.url === 'string' ? raw.url : '');
|
|
403
|
-
if (url) {
|
|
404
|
-
out.push({ type: 'image_url', image_url: { url } });
|
|
405
|
-
}
|
|
406
|
-
continue;
|
|
407
|
-
}
|
|
408
|
-
if (type === 'image_url' && block.image_url) {
|
|
409
|
-
out.push({ type: 'image_url', image_url: block.image_url });
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
if (out.length === 0) return '';
|
|
413
|
-
return out;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const messageFromResponsesItem = (item) => {
|
|
417
|
-
if (!item || typeof item !== 'object') return null;
|
|
418
|
-
const type = typeof item.type === 'string' ? item.type : '';
|
|
419
|
-
if (type === 'function_call') {
|
|
420
|
-
const name = typeof item.name === 'string' ? item.name : '';
|
|
421
|
-
if (!name) return null;
|
|
422
|
-
return {
|
|
423
|
-
role: 'assistant',
|
|
424
|
-
content: null,
|
|
425
|
-
tool_calls: [{
|
|
426
|
-
id: typeof item.call_id === 'string' && item.call_id ? item.call_id : (typeof item.id === 'string' ? item.id : `call_${crypto.randomBytes(8).toString('hex')}`),
|
|
427
|
-
type: 'function',
|
|
428
|
-
function: {
|
|
429
|
-
name,
|
|
430
|
-
arguments: stringifyJsonValue(item.arguments, '{}')
|
|
431
|
-
}
|
|
432
|
-
}]
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
if (type === 'function_call_output') {
|
|
436
|
-
const callId = typeof item.call_id === 'string' ? item.call_id : '';
|
|
437
|
-
return {
|
|
438
|
-
role: 'tool',
|
|
439
|
-
tool_call_id: callId,
|
|
440
|
-
content: stringifyJsonValue(item.output, '')
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
if (typeof item.role === 'string' && item.content != null) {
|
|
444
|
-
const role = item.role.trim() || 'user';
|
|
445
|
-
const content = Array.isArray(item.content)
|
|
446
|
-
? toChatContent(item.content)
|
|
447
|
-
: item.content;
|
|
448
|
-
return content || content === null ? { role, content } : null;
|
|
449
|
-
}
|
|
450
|
-
if (type) {
|
|
451
|
-
const content = toChatContent([item]);
|
|
452
|
-
return content ? { role: 'user', content } : null;
|
|
453
|
-
}
|
|
454
|
-
return null;
|
|
455
|
-
};
|
|
456
|
-
|
|
457
|
-
if (typeof input === 'string') {
|
|
458
|
-
return [{ role: 'user', content: input }];
|
|
459
|
-
}
|
|
460
|
-
if (input && typeof input === 'object' && !Array.isArray(input)) {
|
|
461
|
-
const message = messageFromResponsesItem(input);
|
|
462
|
-
return message ? [message] : [];
|
|
463
|
-
}
|
|
464
|
-
if (!Array.isArray(input)) {
|
|
465
|
-
return [];
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const messages = [];
|
|
469
|
-
for (const item of input) {
|
|
470
|
-
const message = messageFromResponsesItem(item);
|
|
471
|
-
if (message) messages.push(message);
|
|
472
|
-
}
|
|
473
|
-
if (messages.length > 0) {
|
|
474
|
-
return messages;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
const fallbackContent = toChatContent(input);
|
|
478
|
-
if (fallbackContent) {
|
|
479
|
-
return [{ role: 'user', content: fallbackContent }];
|
|
480
|
-
}
|
|
481
|
-
return [];
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function normalizeResponsesToolsToChatTools(tools) {
|
|
485
|
-
if (!Array.isArray(tools)) return tools;
|
|
486
|
-
return tools
|
|
487
|
-
.map((tool) => {
|
|
488
|
-
if (!tool || typeof tool !== 'object') return null;
|
|
489
|
-
if (tool.type !== 'function') return tool;
|
|
490
|
-
const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
|
|
491
|
-
? tool.function
|
|
492
|
-
: {};
|
|
493
|
-
const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
|
|
494
|
-
? sourceFn.name.trim()
|
|
495
|
-
: (typeof tool.name === 'string' ? tool.name.trim() : '');
|
|
496
|
-
if (!name) return null;
|
|
497
|
-
const description = typeof sourceFn.description === 'string'
|
|
498
|
-
? sourceFn.description
|
|
499
|
-
: (typeof tool.description === 'string' ? tool.description : undefined);
|
|
500
|
-
const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
|
|
501
|
-
? sourceFn.parameters
|
|
502
|
-
: (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : {});
|
|
503
|
-
const strict = typeof sourceFn.strict === 'boolean'
|
|
504
|
-
? sourceFn.strict
|
|
505
|
-
: (typeof tool.strict === 'boolean' ? tool.strict : undefined);
|
|
506
|
-
const fn = { name, parameters };
|
|
507
|
-
if (description !== undefined) fn.description = description;
|
|
508
|
-
if (strict !== undefined) fn.strict = strict;
|
|
509
|
-
return { type: 'function', function: fn };
|
|
510
|
-
})
|
|
511
|
-
.filter(Boolean);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
function normalizeResponsesToolChoiceToChatToolChoice(toolChoice) {
|
|
515
|
-
if (!toolChoice || typeof toolChoice !== 'object' || Array.isArray(toolChoice)) return toolChoice;
|
|
516
|
-
if (toolChoice.type === 'function' && typeof toolChoice.name === 'string') {
|
|
517
|
-
return { type: 'function', function: { name: toolChoice.name } };
|
|
518
|
-
}
|
|
519
|
-
return toolChoice;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
function buildChatCompletionsBodyFromResponsesPayload(payload) {
|
|
523
|
-
const source = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
524
|
-
const messages = normalizeResponsesInputToChatMessages(source.input);
|
|
525
|
-
const instructions = typeof source.instructions === 'string' ? source.instructions.trim() : '';
|
|
526
|
-
if (instructions) {
|
|
527
|
-
messages.unshift({ role: 'system', content: instructions });
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
const chatBody = {
|
|
531
|
-
model: typeof source.model === 'string' ? source.model : '',
|
|
532
|
-
messages,
|
|
533
|
-
stream: false
|
|
534
|
-
};
|
|
535
|
-
|
|
536
|
-
const passthroughKeys = [
|
|
537
|
-
'frequency_penalty',
|
|
538
|
-
'presence_penalty',
|
|
539
|
-
'response_format',
|
|
540
|
-
'stop',
|
|
541
|
-
'temperature',
|
|
542
|
-
'top_p',
|
|
543
|
-
'tools',
|
|
544
|
-
'tool_choice',
|
|
545
|
-
'logprobs',
|
|
546
|
-
'top_logprobs',
|
|
547
|
-
'kbs',
|
|
548
|
-
'is_online',
|
|
549
|
-
'user',
|
|
550
|
-
'seed',
|
|
551
|
-
'n',
|
|
552
|
-
'modalities',
|
|
553
|
-
'audio',
|
|
554
|
-
'reasoning_effort'
|
|
555
|
-
];
|
|
556
|
-
for (const key of passthroughKeys) {
|
|
557
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
558
|
-
if (key === 'tools') {
|
|
559
|
-
chatBody[key] = normalizeResponsesToolsToChatTools(source[key]);
|
|
560
|
-
} else if (key === 'tool_choice') {
|
|
561
|
-
chatBody[key] = normalizeResponsesToolChoiceToChatToolChoice(source[key]);
|
|
562
|
-
} else {
|
|
563
|
-
chatBody[key] = source[key];
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
if (Object.prototype.hasOwnProperty.call(source, 'max_tokens')) {
|
|
569
|
-
chatBody.max_tokens = source.max_tokens;
|
|
570
|
-
} else if (source.max_output_tokens != null) {
|
|
571
|
-
chatBody.max_tokens = source.max_output_tokens;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
return chatBody;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
function ensureResponseMetadata(payload) {
|
|
578
|
-
const base = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
579
|
-
const id = typeof base.id === 'string' && base.id.trim()
|
|
580
|
-
? base.id.trim()
|
|
581
|
-
: `resp_${crypto.randomBytes(10).toString('hex')}`;
|
|
582
|
-
const model = typeof base.model === 'string' ? base.model : '';
|
|
583
|
-
return {
|
|
584
|
-
object: 'response',
|
|
585
|
-
id,
|
|
586
|
-
model,
|
|
587
|
-
...base
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
function writeSse(res, eventName, dataObj) {
|
|
592
|
-
if (eventName) {
|
|
593
|
-
res.write(`event: ${eventName}\n`);
|
|
594
|
-
}
|
|
595
|
-
if (dataObj === '[DONE]') {
|
|
596
|
-
res.write('data: [DONE]\n\n');
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
res.write(`data: ${JSON.stringify(dataObj)}\n\n`);
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
function sendResponsesSse(res, responsePayload) {
|
|
603
|
-
const response = ensureResponseMetadata(responsePayload);
|
|
604
|
-
const responseId = response.id;
|
|
605
|
-
const model = response.model;
|
|
606
|
-
let sequence = 0;
|
|
607
|
-
const nextSeq = () => {
|
|
608
|
-
sequence += 1;
|
|
609
|
-
return sequence;
|
|
610
|
-
};
|
|
611
|
-
|
|
612
|
-
writeSse(res, 'response.created', {
|
|
613
|
-
type: 'response.created',
|
|
614
|
-
response: {
|
|
615
|
-
id: responseId,
|
|
616
|
-
model,
|
|
617
|
-
created_at: response.created_at
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
|
|
621
|
-
const output = Array.isArray(response.output) ? response.output : [];
|
|
622
|
-
for (let outputIndex = 0; outputIndex < output.length; outputIndex += 1) {
|
|
623
|
-
const item = output[outputIndex];
|
|
624
|
-
if (!item || typeof item !== 'object') continue;
|
|
625
|
-
const itemType = typeof item.type === 'string' ? item.type : '';
|
|
626
|
-
const itemId = typeof item.id === 'string' && item.id.trim()
|
|
627
|
-
? item.id.trim()
|
|
628
|
-
: `item_${crypto.randomBytes(8).toString('hex')}`;
|
|
629
|
-
|
|
630
|
-
writeSse(res, 'response.output_item.added', {
|
|
631
|
-
type: 'response.output_item.added',
|
|
632
|
-
output_index: outputIndex,
|
|
633
|
-
item: { ...item, id: itemId }
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
if (itemType === 'message') {
|
|
637
|
-
const content = Array.isArray(item.content) ? item.content : [];
|
|
638
|
-
for (let contentIndex = 0; contentIndex < content.length; contentIndex += 1) {
|
|
639
|
-
const block = content[contentIndex];
|
|
640
|
-
if (!block || typeof block !== 'object') continue;
|
|
641
|
-
if (block.type !== 'output_text') continue;
|
|
642
|
-
const text = typeof block.text === 'string' ? block.text : '';
|
|
643
|
-
if (text) {
|
|
644
|
-
writeSse(res, 'response.output_text.delta', {
|
|
645
|
-
type: 'response.output_text.delta',
|
|
646
|
-
item_id: itemId,
|
|
647
|
-
output_index: outputIndex,
|
|
648
|
-
content_index: contentIndex,
|
|
649
|
-
delta: text,
|
|
650
|
-
sequence_number: nextSeq()
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
writeSse(res, 'response.output_text.done', {
|
|
654
|
-
type: 'response.output_text.done',
|
|
655
|
-
item_id: itemId,
|
|
656
|
-
output_index: outputIndex,
|
|
657
|
-
content_index: contentIndex,
|
|
658
|
-
text,
|
|
659
|
-
sequence_number: nextSeq()
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
writeSse(res, 'response.output_item.done', {
|
|
665
|
-
type: 'response.output_item.done',
|
|
666
|
-
output_index: outputIndex,
|
|
667
|
-
item: { ...item, id: itemId },
|
|
668
|
-
sequence_number: nextSeq()
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
writeSse(res, 'response.completed', { type: 'response.completed', response });
|
|
673
|
-
writeSse(res, 'done', '[DONE]');
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
function appendChatStreamToolCall(target, toolCall) {
|
|
677
|
-
if (!toolCall || typeof toolCall !== 'object') return;
|
|
678
|
-
const index = Number.isFinite(toolCall.index) ? toolCall.index : target.length;
|
|
679
|
-
if (!target[index]) {
|
|
680
|
-
target[index] = {
|
|
681
|
-
id: '',
|
|
682
|
-
type: 'function',
|
|
683
|
-
function: { name: '', arguments: '' }
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
const current = target[index];
|
|
687
|
-
if (typeof toolCall.id === 'string' && toolCall.id) current.id = toolCall.id;
|
|
688
|
-
if (typeof toolCall.type === 'string' && toolCall.type) current.type = toolCall.type;
|
|
689
|
-
const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : null;
|
|
690
|
-
if (fn) {
|
|
691
|
-
if (typeof fn.name === 'string' && fn.name) current.function.name = fn.name;
|
|
692
|
-
if (typeof fn.arguments === 'string') current.function.arguments += fn.arguments;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
function writeChatCompletionChunkAsResponsesSse(state, chunk) {
|
|
697
|
-
if (!chunk || typeof chunk !== 'object') return;
|
|
698
|
-
if (typeof chunk.model === 'string' && chunk.model) {
|
|
699
|
-
state.model = chunk.model;
|
|
700
|
-
}
|
|
701
|
-
const choices = Array.isArray(chunk.choices) ? chunk.choices : [];
|
|
702
|
-
for (const choice of choices) {
|
|
703
|
-
const delta = choice && choice.delta && typeof choice.delta === 'object' ? choice.delta : null;
|
|
704
|
-
if (!delta) continue;
|
|
705
|
-
|
|
706
|
-
if (typeof delta.content === 'string' && delta.content) {
|
|
707
|
-
if (!state.messageItem) {
|
|
708
|
-
state.messageItem = {
|
|
709
|
-
id: `msg_${crypto.randomBytes(8).toString('hex')}`,
|
|
710
|
-
type: 'message',
|
|
711
|
-
role: 'assistant',
|
|
712
|
-
content: [{ type: 'output_text', text: '' }]
|
|
713
|
-
};
|
|
714
|
-
state.output.push(state.messageItem);
|
|
715
|
-
writeSse(state.res, 'response.output_item.added', {
|
|
716
|
-
type: 'response.output_item.added',
|
|
717
|
-
output_index: state.output.length - 1,
|
|
718
|
-
item: state.messageItem
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
state.messageText += delta.content;
|
|
722
|
-
state.messageItem.content[0].text = state.messageText;
|
|
723
|
-
writeSse(state.res, 'response.output_text.delta', {
|
|
724
|
-
type: 'response.output_text.delta',
|
|
725
|
-
item_id: state.messageItem.id,
|
|
726
|
-
output_index: state.output.length - 1,
|
|
727
|
-
content_index: 0,
|
|
728
|
-
delta: delta.content,
|
|
729
|
-
sequence_number: state.nextSeq()
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
if (Array.isArray(delta.tool_calls)) {
|
|
734
|
-
for (const toolCall of delta.tool_calls) {
|
|
735
|
-
appendChatStreamToolCall(state.toolCalls, toolCall);
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
function stopChatStreamHeartbeat(state) {
|
|
742
|
-
if (!state || !state.heartbeatTimer) return;
|
|
743
|
-
clearInterval(state.heartbeatTimer);
|
|
744
|
-
state.heartbeatTimer = null;
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
function startChatStreamHeartbeat(state) {
|
|
748
|
-
if (!state || state.heartbeatTimer) return;
|
|
749
|
-
const timer = setInterval(() => {
|
|
750
|
-
if (state.finished) {
|
|
751
|
-
stopChatStreamHeartbeat(state);
|
|
752
|
-
return;
|
|
753
|
-
}
|
|
754
|
-
const target = state.res;
|
|
755
|
-
if (!target || target.writableEnded || target.destroyed) {
|
|
756
|
-
stopChatStreamHeartbeat(state);
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
try { target.write(': keepalive\n\n'); } catch (_) {}
|
|
760
|
-
}, 15000);
|
|
761
|
-
if (typeof timer.unref === 'function') timer.unref();
|
|
762
|
-
state.heartbeatTimer = timer;
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
function finishChatStreamResponsesSse(state) {
|
|
766
|
-
if (state.finished) return;
|
|
767
|
-
state.finished = true;
|
|
768
|
-
stopChatStreamHeartbeat(state);
|
|
769
|
-
|
|
770
|
-
if (state.messageItem) {
|
|
771
|
-
const outputIndex = state.output.indexOf(state.messageItem);
|
|
772
|
-
writeSse(state.res, 'response.output_text.done', {
|
|
773
|
-
type: 'response.output_text.done',
|
|
774
|
-
item_id: state.messageItem.id,
|
|
775
|
-
output_index: outputIndex,
|
|
776
|
-
content_index: 0,
|
|
777
|
-
text: state.messageText,
|
|
778
|
-
sequence_number: state.nextSeq()
|
|
779
|
-
});
|
|
780
|
-
writeSse(state.res, 'response.output_item.done', {
|
|
781
|
-
type: 'response.output_item.done',
|
|
782
|
-
output_index: outputIndex,
|
|
783
|
-
item: state.messageItem,
|
|
784
|
-
sequence_number: state.nextSeq()
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
for (const toolCall of state.toolCalls) {
|
|
789
|
-
if (!toolCall) continue;
|
|
790
|
-
const item = {
|
|
791
|
-
type: 'function_call',
|
|
792
|
-
call_id: toolCall.id || `call_${crypto.randomBytes(8).toString('hex')}`,
|
|
793
|
-
name: toolCall.function && typeof toolCall.function.name === 'string' ? toolCall.function.name : '',
|
|
794
|
-
arguments: toolCall.function && typeof toolCall.function.arguments === 'string' ? toolCall.function.arguments : ''
|
|
795
|
-
};
|
|
796
|
-
const outputIndex = state.output.length;
|
|
797
|
-
state.output.push(item);
|
|
798
|
-
writeSse(state.res, 'response.output_item.added', {
|
|
799
|
-
type: 'response.output_item.added',
|
|
800
|
-
output_index: outputIndex,
|
|
801
|
-
item
|
|
802
|
-
});
|
|
803
|
-
writeSse(state.res, 'response.output_item.done', {
|
|
804
|
-
type: 'response.output_item.done',
|
|
805
|
-
output_index: outputIndex,
|
|
806
|
-
item,
|
|
807
|
-
sequence_number: state.nextSeq()
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
const response = ensureResponseMetadata({
|
|
812
|
-
id: state.responseId,
|
|
813
|
-
model: state.model,
|
|
814
|
-
created_at: state.createdAt,
|
|
815
|
-
status: 'completed',
|
|
816
|
-
output: state.output
|
|
817
|
-
});
|
|
818
|
-
writeSse(state.res, 'response.completed', { type: 'response.completed', response });
|
|
819
|
-
writeSse(state.res, 'done', '[DONE]');
|
|
820
|
-
state.res.end();
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
function failResponsesSseRaw(res, message) {
|
|
824
|
-
if (!res || res.writableEnded || res.destroyed) return;
|
|
825
|
-
try {
|
|
826
|
-
writeSse(res, 'response.failed', { type: 'response.failed', error: message || 'upstream stream failed' });
|
|
827
|
-
writeSse(res, 'done', '[DONE]');
|
|
828
|
-
res.end();
|
|
829
|
-
} catch (_) {}
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
function failChatStreamResponsesSse(state, message) {
|
|
833
|
-
if (!state || state.finished) return;
|
|
834
|
-
state.finished = true;
|
|
835
|
-
stopChatStreamHeartbeat(state);
|
|
836
|
-
failResponsesSseRaw(state.res, message);
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
function streamChatCompletionsAsResponsesSse(targetUrl, options = {}) {
|
|
840
|
-
const parsed = new URL(targetUrl);
|
|
841
|
-
const transport = parsed.protocol === 'https:' ? https : http;
|
|
842
|
-
const bodyText = options.body ? JSON.stringify(options.body) : '';
|
|
843
|
-
const headers = {
|
|
844
|
-
'Accept': 'text/event-stream',
|
|
845
|
-
...(options.body ? { 'Content-Type': 'application/json' } : {}),
|
|
846
|
-
...(options.headers || {})
|
|
847
|
-
};
|
|
848
|
-
if (options.body) {
|
|
849
|
-
headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
|
|
850
|
-
}
|
|
851
|
-
const timeoutMs = Number.isFinite(options.timeoutMs)
|
|
852
|
-
? Math.max(1000, Number(options.timeoutMs))
|
|
853
|
-
: 30000;
|
|
854
|
-
const res = options.res;
|
|
855
|
-
const model = typeof options.model === 'string' ? options.model : '';
|
|
856
|
-
|
|
857
|
-
return new Promise((resolve) => {
|
|
858
|
-
let settled = false;
|
|
859
|
-
const finish = (value) => {
|
|
860
|
-
if (settled) return;
|
|
861
|
-
settled = true;
|
|
862
|
-
resolve(value);
|
|
863
|
-
};
|
|
864
|
-
const req = transport.request({
|
|
865
|
-
protocol: parsed.protocol,
|
|
866
|
-
hostname: parsed.hostname,
|
|
867
|
-
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
868
|
-
method: options.method || 'POST',
|
|
869
|
-
path: `${parsed.pathname}${parsed.search}`,
|
|
870
|
-
headers,
|
|
871
|
-
agent: parsed.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
872
|
-
}, (upstreamRes) => {
|
|
873
|
-
const status = upstreamRes.statusCode || 0;
|
|
874
|
-
const chunks = [];
|
|
875
|
-
const contentType = String(upstreamRes.headers && upstreamRes.headers['content-type'] || '');
|
|
876
|
-
let streamState = null;
|
|
877
|
-
|
|
878
|
-
const handleAbort = (reason) => {
|
|
879
|
-
if (settled) return;
|
|
880
|
-
if (streamState) {
|
|
881
|
-
failChatStreamResponsesSse(streamState, reason);
|
|
882
|
-
finish({ ok: true });
|
|
883
|
-
return;
|
|
884
|
-
}
|
|
885
|
-
if (res.headersSent) {
|
|
886
|
-
failResponsesSseRaw(res, reason);
|
|
887
|
-
finish({ ok: true });
|
|
888
|
-
return;
|
|
889
|
-
}
|
|
890
|
-
finish({
|
|
891
|
-
ok: false,
|
|
892
|
-
status,
|
|
893
|
-
error: reason,
|
|
894
|
-
bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : ''
|
|
895
|
-
});
|
|
896
|
-
};
|
|
897
|
-
upstreamRes.on('error', (err) => handleAbort(err && err.message ? err.message : 'upstream stream failed'));
|
|
898
|
-
upstreamRes.on('aborted', () => handleAbort('upstream stream aborted'));
|
|
899
|
-
|
|
900
|
-
if (status === 404 || status === 405) {
|
|
901
|
-
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
902
|
-
upstreamRes.on('end', () => finish({ retry: true, status, bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : '' }));
|
|
903
|
-
return;
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
if (status >= 400) {
|
|
907
|
-
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
908
|
-
upstreamRes.on('end', () => finish({ ok: false, status, bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : '' }));
|
|
909
|
-
return;
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
res.writeHead(200, {
|
|
913
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
914
|
-
'Cache-Control': 'no-cache',
|
|
915
|
-
'Connection': 'keep-alive',
|
|
916
|
-
'X-Accel-Buffering': 'no'
|
|
917
|
-
});
|
|
918
|
-
|
|
919
|
-
if (!/text\/event-stream/i.test(contentType)) {
|
|
920
|
-
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
921
|
-
upstreamRes.on('end', () => {
|
|
922
|
-
const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
|
|
923
|
-
const parsedJson = parseJsonOrError(text);
|
|
924
|
-
if (parsedJson.error) {
|
|
925
|
-
writeSse(res, 'response.failed', { type: 'response.failed', error: `invalid upstream response: ${parsedJson.error}` });
|
|
926
|
-
writeSse(res, 'done', '[DONE]');
|
|
927
|
-
res.end();
|
|
928
|
-
finish({ ok: true });
|
|
929
|
-
return;
|
|
930
|
-
}
|
|
931
|
-
sendResponsesSse(res, buildResponsesPayloadFromChatCompletion(parsedJson.value, model));
|
|
932
|
-
res.end();
|
|
933
|
-
finish({ ok: true });
|
|
934
|
-
});
|
|
935
|
-
return;
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
let sequence = 0;
|
|
939
|
-
const state = {
|
|
940
|
-
res,
|
|
941
|
-
responseId: `resp_${crypto.randomBytes(10).toString('hex')}`,
|
|
942
|
-
model,
|
|
943
|
-
createdAt: Math.floor(Date.now() / 1000),
|
|
944
|
-
output: [],
|
|
945
|
-
messageItem: null,
|
|
946
|
-
messageText: '',
|
|
947
|
-
toolCalls: [],
|
|
948
|
-
finished: false,
|
|
949
|
-
nextSeq: () => {
|
|
950
|
-
sequence += 1;
|
|
951
|
-
return sequence;
|
|
952
|
-
}
|
|
953
|
-
};
|
|
954
|
-
streamState = state;
|
|
955
|
-
startChatStreamHeartbeat(state);
|
|
956
|
-
if (typeof res.on === 'function') {
|
|
957
|
-
res.on('close', () => stopChatStreamHeartbeat(state));
|
|
958
|
-
}
|
|
959
|
-
writeSse(res, 'response.created', {
|
|
960
|
-
type: 'response.created',
|
|
961
|
-
response: {
|
|
962
|
-
id: state.responseId,
|
|
963
|
-
model: state.model,
|
|
964
|
-
created_at: state.createdAt
|
|
965
|
-
}
|
|
966
|
-
});
|
|
967
|
-
|
|
968
|
-
let buffer = '';
|
|
969
|
-
const handleEventBlock = (block) => {
|
|
970
|
-
const dataLines = String(block || '')
|
|
971
|
-
.split(/\r?\n/)
|
|
972
|
-
.filter((line) => line.startsWith('data:'))
|
|
973
|
-
.map((line) => line.slice(5).trimStart());
|
|
974
|
-
if (dataLines.length === 0) return;
|
|
975
|
-
const data = dataLines.join('\n').trim();
|
|
976
|
-
if (!data) return;
|
|
977
|
-
if (data === '[DONE]') {
|
|
978
|
-
finishChatStreamResponsesSse(state);
|
|
979
|
-
finish({ ok: true });
|
|
980
|
-
return;
|
|
981
|
-
}
|
|
982
|
-
const parsedChunk = parseJsonOrError(data);
|
|
983
|
-
if (!parsedChunk.error) {
|
|
984
|
-
writeChatCompletionChunkAsResponsesSse(state, parsedChunk.value);
|
|
985
|
-
}
|
|
986
|
-
};
|
|
987
|
-
|
|
988
|
-
upstreamRes.on('data', (chunk) => {
|
|
989
|
-
buffer += chunk.toString('utf-8');
|
|
990
|
-
let boundary = buffer.search(/\r?\n\r?\n/);
|
|
991
|
-
while (boundary >= 0) {
|
|
992
|
-
const block = buffer.slice(0, boundary);
|
|
993
|
-
const match = buffer.slice(boundary).match(/^\r?\n\r?\n/);
|
|
994
|
-
buffer = buffer.slice(boundary + (match ? match[0].length : 2));
|
|
995
|
-
handleEventBlock(block);
|
|
996
|
-
boundary = buffer.search(/\r?\n\r?\n/);
|
|
997
|
-
}
|
|
998
|
-
});
|
|
999
|
-
upstreamRes.on('end', () => {
|
|
1000
|
-
if (buffer.trim()) handleEventBlock(buffer);
|
|
1001
|
-
finishChatStreamResponsesSse(state);
|
|
1002
|
-
finish({ ok: true });
|
|
1003
|
-
});
|
|
1004
|
-
});
|
|
1005
|
-
req.setTimeout(timeoutMs, () => {
|
|
1006
|
-
try { req.destroy(new Error('timeout')); } catch (_) {}
|
|
1007
|
-
finish({ ok: false, error: 'timeout' });
|
|
1008
|
-
});
|
|
1009
|
-
req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
|
|
1010
|
-
if (bodyText) req.write(bodyText);
|
|
1011
|
-
req.end();
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
async function streamChatCompletionsAsResponsesSseWithFallbackUrls(baseUrl, pathSuffix, options = {}) {
|
|
1016
|
-
const urls = buildUpstreamUrlCandidates(baseUrl, pathSuffix);
|
|
1017
|
-
if (urls.length === 0) {
|
|
1018
|
-
return { ok: false, error: 'failed to build upstream URL' };
|
|
1019
|
-
}
|
|
1020
|
-
let lastResult = null;
|
|
1021
|
-
for (const url of urls) {
|
|
1022
|
-
const result = await retryTransientRequest(() => streamChatCompletionsAsResponsesSse(url, options));
|
|
1023
|
-
lastResult = result;
|
|
1024
|
-
if (result && result.retry) continue;
|
|
1025
|
-
return result;
|
|
1026
|
-
}
|
|
1027
|
-
return lastResult || { ok: false, error: 'failed to build upstream URL' };
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
function canListenPort(host, port) {
|
|
1031
|
-
return new Promise((resolve) => {
|
|
1032
|
-
const tester = net.createServer();
|
|
1033
|
-
tester.unref();
|
|
1034
|
-
tester.once('error', () => resolve(false));
|
|
1035
|
-
tester.once('listening', () => {
|
|
1036
|
-
tester.close(() => resolve(true));
|
|
1037
|
-
});
|
|
1038
|
-
tester.listen(port, host);
|
|
1039
|
-
});
|
|
1040
|
-
}
|
|
1041
|
-
|
|
1042
|
-
async function findAvailablePort(host, startPort, maxAttempts = 20) {
|
|
1043
|
-
const start = parseInt(String(startPort), 10);
|
|
1044
|
-
if (!Number.isFinite(start) || start <= 0) {
|
|
1045
|
-
return 0;
|
|
1046
|
-
}
|
|
1047
|
-
const attempts = Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 20;
|
|
1048
|
-
for (let offset = 0; offset < attempts; offset += 1) {
|
|
1049
|
-
const candidate = start + offset;
|
|
1050
|
-
if (candidate > 65535) {
|
|
1051
|
-
break;
|
|
1052
|
-
}
|
|
1053
|
-
// eslint-disable-next-line no-await-in-loop
|
|
1054
|
-
const ok = await canListenPort(host, candidate);
|
|
1055
|
-
if (ok) {
|
|
1056
|
-
return candidate;
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
return 0;
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
function resolveBuiltinProxyProviderName(rawProviderName, providers = {}, preferredProvider = '') {
|
|
1063
|
-
const providerMap = providers && isPlainObject(providers) ? providers : {};
|
|
1064
|
-
const providerNames = Object.keys(providerMap)
|
|
1065
|
-
.filter((name) => name && !isBuiltinManagedProvider(name));
|
|
1066
|
-
const requested = typeof rawProviderName === 'string' ? rawProviderName.trim() : '';
|
|
1067
|
-
if (requested && !isBuiltinManagedProvider(requested) && providerMap[requested]) {
|
|
1068
|
-
return requested;
|
|
1069
|
-
}
|
|
1070
|
-
const preferred = typeof preferredProvider === 'string' ? preferredProvider.trim() : '';
|
|
1071
|
-
if (preferred && !isBuiltinManagedProvider(preferred) && providerMap[preferred]) {
|
|
1072
|
-
return preferred;
|
|
1073
|
-
}
|
|
1074
|
-
return providerNames[0] || '';
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
function normalizeBuiltinProxySettings(raw) {
|
|
1078
|
-
const merged = {
|
|
1079
|
-
...DEFAULT_BUILTIN_PROXY_SETTINGS,
|
|
1080
|
-
...(isPlainObject(raw) ? raw : {})
|
|
1081
|
-
};
|
|
1082
|
-
const host = typeof merged.host === 'string' ? merged.host.trim() : '';
|
|
1083
|
-
const port = parseInt(String(merged.port), 10);
|
|
1084
|
-
const provider = typeof merged.provider === 'string' ? merged.provider.trim() : '';
|
|
1085
|
-
const authSourceRaw = typeof merged.authSource === 'string' ? merged.authSource.trim().toLowerCase() : '';
|
|
1086
|
-
const timeoutMs = parseInt(String(merged.timeoutMs), 10);
|
|
1087
|
-
const authSource = authSourceRaw === 'profile' || authSourceRaw === 'none' ? authSourceRaw : 'provider';
|
|
1088
|
-
|
|
1089
|
-
return {
|
|
1090
|
-
enabled: merged.enabled !== false,
|
|
1091
|
-
host: host || DEFAULT_BUILTIN_PROXY_SETTINGS.host,
|
|
1092
|
-
port: Number.isFinite(port) && port > 0 && port <= 65535 ? port : DEFAULT_BUILTIN_PROXY_SETTINGS.port,
|
|
1093
|
-
provider,
|
|
1094
|
-
authSource,
|
|
1095
|
-
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs >= 1000
|
|
1096
|
-
? timeoutMs
|
|
1097
|
-
: DEFAULT_BUILTIN_PROXY_SETTINGS.timeoutMs
|
|
1098
|
-
};
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
function readBuiltinProxySettings() {
|
|
1102
|
-
const parsed = readJsonFile(BUILTIN_PROXY_SETTINGS_FILE, null);
|
|
1103
|
-
return normalizeBuiltinProxySettings(parsed);
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
function saveBuiltinProxySettings(payload = {}, options = {}) {
|
|
1107
|
-
const current = readBuiltinProxySettings();
|
|
1108
|
-
const merged = normalizeBuiltinProxySettings({
|
|
1109
|
-
...current,
|
|
1110
|
-
...(isPlainObject(payload) ? payload : {})
|
|
1111
|
-
});
|
|
1112
|
-
|
|
1113
|
-
if (!merged.host) {
|
|
1114
|
-
return { error: '代理 host 不能为空' };
|
|
1115
|
-
}
|
|
1116
|
-
if (!Number.isFinite(merged.port) || merged.port <= 0 || merged.port > 65535) {
|
|
1117
|
-
return { error: '代理端口无效(1-65535)' };
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
const { config } = readConfigOrVirtualDefault();
|
|
1121
|
-
const providers = config && isPlainObject(config.model_providers) ? config.model_providers : {};
|
|
1122
|
-
const preferredProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
|
|
1123
|
-
const finalProvider = resolveBuiltinProxyProviderName(merged.provider, providers, preferredProvider);
|
|
1124
|
-
|
|
1125
|
-
const normalized = {
|
|
1126
|
-
...merged,
|
|
1127
|
-
provider: finalProvider
|
|
1128
|
-
};
|
|
1129
|
-
|
|
1130
|
-
if (!options.skipWrite) {
|
|
1131
|
-
writeJsonAtomic(BUILTIN_PROXY_SETTINGS_FILE, normalized);
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
return {
|
|
1135
|
-
success: true,
|
|
1136
|
-
settings: normalized
|
|
1137
|
-
};
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
function buildProxyListenUrl(settings) {
|
|
1141
|
-
const host = formatHostForUrl(settings.host || DEFAULT_BUILTIN_PROXY_SETTINGS.host);
|
|
1142
|
-
return `http://${host}:${settings.port}`;
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
function buildBuiltinProxyProviderBaseUrl(settings) {
|
|
1146
|
-
return `${buildProxyListenUrl(settings).replace(/\/+$/, '')}/v1`;
|
|
1147
|
-
}
|
|
1148
|
-
|
|
1149
|
-
function removePersistedBuiltinProxyProviderFromConfig() {
|
|
1150
|
-
if (!fs.existsSync(CONFIG_FILE)) {
|
|
1151
|
-
return { success: true, removed: false };
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
let config;
|
|
1155
|
-
try {
|
|
1156
|
-
config = readConfig();
|
|
1157
|
-
} catch (e) {
|
|
1158
|
-
return { error: e.message || '读取 config.toml 失败' };
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1161
|
-
if (!config.model_providers || !config.model_providers[BUILTIN_PROXY_PROVIDER_NAME]) {
|
|
1162
|
-
return { success: true, removed: false };
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
|
1166
|
-
const lineEnding = content.includes('\r\n') ? '\r\n' : '\n';
|
|
1167
|
-
const hasBom = content.charCodeAt(0) === 0xFEFF;
|
|
1168
|
-
const providerConfig = config.model_providers[BUILTIN_PROXY_PROVIDER_NAME];
|
|
1169
|
-
const providerSegments = providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segments)
|
|
1170
|
-
? providerConfig.__codexmate_legacy_segments
|
|
1171
|
-
: null;
|
|
1172
|
-
const providerSegmentVariants = (() => {
|
|
1173
|
-
const variants = [];
|
|
1174
|
-
const seen = new Set();
|
|
1175
|
-
const pushVariant = (segments) => {
|
|
1176
|
-
const normalized = normalizeLegacySegments(segments);
|
|
1177
|
-
const key = buildLegacySegmentsKey(normalized);
|
|
1178
|
-
if (!key || seen.has(key)) return;
|
|
1179
|
-
seen.add(key);
|
|
1180
|
-
variants.push(normalized);
|
|
1181
|
-
};
|
|
1182
|
-
if (providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segments)) {
|
|
1183
|
-
pushVariant(providerConfig.__codexmate_legacy_segments);
|
|
1184
|
-
}
|
|
1185
|
-
if (providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segment_variants)) {
|
|
1186
|
-
for (const segments of providerConfig.__codexmate_legacy_segment_variants) {
|
|
1187
|
-
pushVariant(segments);
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
if (providerSegments) {
|
|
1191
|
-
pushVariant(providerSegments);
|
|
1192
|
-
}
|
|
1193
|
-
if (variants.length === 0) {
|
|
1194
|
-
pushVariant(String(BUILTIN_PROXY_PROVIDER_NAME || '').split('.').filter((item) => item));
|
|
1195
|
-
}
|
|
1196
|
-
return variants;
|
|
1197
|
-
})();
|
|
1198
|
-
|
|
1199
|
-
let updatedContent = null;
|
|
1200
|
-
const combinedRanges = [];
|
|
1201
|
-
for (const segments of providerSegmentVariants) {
|
|
1202
|
-
combinedRanges.push(...findProviderSectionRanges(content, BUILTIN_PROXY_PROVIDER_NAME, segments));
|
|
1203
|
-
combinedRanges.push(...findProviderDescendantSectionRanges(content, segments));
|
|
1204
|
-
}
|
|
1205
|
-
if (combinedRanges.length === 0) {
|
|
1206
|
-
combinedRanges.push(...findProviderSectionRanges(content, BUILTIN_PROXY_PROVIDER_NAME, providerSegments));
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
if (combinedRanges.length > 0) {
|
|
1210
|
-
const sorted = combinedRanges.sort((a, b) => b.start - a.start || b.end - a.end);
|
|
1211
|
-
const seen = new Set();
|
|
1212
|
-
let removedContent = content;
|
|
1213
|
-
for (const range of sorted) {
|
|
1214
|
-
const rangeKey = `${range.start}:${range.end}`;
|
|
1215
|
-
if (seen.has(rangeKey)) continue;
|
|
1216
|
-
seen.add(rangeKey);
|
|
1217
|
-
removedContent = removedContent.slice(0, range.start) + removedContent.slice(range.end);
|
|
1218
|
-
}
|
|
1219
|
-
updatedContent = removedContent.replace(/\n{3,}/g, lineEnding + lineEnding);
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
if (!updatedContent) {
|
|
1223
|
-
const rebuilt = JSON.parse(JSON.stringify(config));
|
|
1224
|
-
delete rebuilt.model_providers[BUILTIN_PROXY_PROVIDER_NAME];
|
|
1225
|
-
const hasMarker = content.includes(CODEXMATE_MANAGED_MARKER);
|
|
1226
|
-
let rebuiltToml = toml.stringify(rebuilt).trimEnd();
|
|
1227
|
-
rebuiltToml = rebuiltToml.replace(/\n/g, lineEnding);
|
|
1228
|
-
if (hasMarker && !rebuiltToml.includes(CODEXMATE_MANAGED_MARKER)) {
|
|
1229
|
-
rebuiltToml = `${CODEXMATE_MANAGED_MARKER}${lineEnding}${rebuiltToml}`;
|
|
1230
|
-
}
|
|
1231
|
-
updatedContent = rebuiltToml + lineEnding;
|
|
1232
|
-
if (hasBom && updatedContent.charCodeAt(0) !== 0xFEFF) {
|
|
1233
|
-
updatedContent = '\uFEFF' + updatedContent;
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
|
|
1237
|
-
try {
|
|
1238
|
-
writeConfig(updatedContent.trimEnd() + lineEnding);
|
|
1239
|
-
} catch (e) {
|
|
1240
|
-
return { error: e.message || '写入 config.toml 失败' };
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
return { success: true, removed: true };
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
function hasCodexConfigReadyForProxy() {
|
|
1247
|
-
const result = readConfigOrVirtualDefault();
|
|
1248
|
-
if (!result || result.isVirtual) {
|
|
1249
|
-
return false;
|
|
1250
|
-
}
|
|
1251
|
-
const config = result.config || {};
|
|
1252
|
-
if (!isPlainObject(config.model_providers)) {
|
|
1253
|
-
return false;
|
|
1254
|
-
}
|
|
1255
|
-
const providerNames = Object.keys(config.model_providers)
|
|
1256
|
-
.filter((name) => name && !isBuiltinManagedProvider(name));
|
|
1257
|
-
return providerNames.length > 0;
|
|
1258
|
-
}
|
|
1259
|
-
|
|
1260
|
-
function resolveBuiltinProxyUpstream(settings) {
|
|
1261
|
-
const { config } = readConfigOrVirtualDefault();
|
|
1262
|
-
const providers = config && isPlainObject(config.model_providers) ? config.model_providers : {};
|
|
1263
|
-
const currentProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
|
|
1264
|
-
const providerName = resolveBuiltinProxyProviderName(settings.provider, providers, currentProvider);
|
|
1265
|
-
if (!providerName) {
|
|
1266
|
-
return { error: '未找到可用的上游 provider,请先添加 provider' };
|
|
1267
|
-
}
|
|
1268
|
-
if (providerName === BUILTIN_PROXY_PROVIDER_NAME) {
|
|
1269
|
-
return { error: `上游 provider 不能是 ${BUILTIN_PROXY_PROVIDER_NAME}` };
|
|
1270
|
-
}
|
|
1271
|
-
const provider = providers[providerName];
|
|
1272
|
-
if (!provider || !isPlainObject(provider)) {
|
|
1273
|
-
return { error: `上游 provider 不存在: ${providerName}` };
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
|
-
const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
|
|
1277
|
-
if (!baseUrl || !isValidHttpUrl(baseUrl)) {
|
|
1278
|
-
return { error: `上游 provider base_url 无效: ${providerName}` };
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
let token = '';
|
|
1282
|
-
if (settings.authSource === 'profile') {
|
|
1283
|
-
token = resolveAuthTokenFromCurrentProfile();
|
|
1284
|
-
} else if (settings.authSource === 'provider') {
|
|
1285
|
-
token = typeof provider.preferred_auth_method === 'string' ? provider.preferred_auth_method.trim() : '';
|
|
1286
|
-
if (!token) {
|
|
1287
|
-
token = resolveAuthTokenFromCurrentProfile();
|
|
1288
|
-
}
|
|
1289
|
-
}
|
|
1290
|
-
|
|
1291
|
-
let authHeader = '';
|
|
1292
|
-
if (token) {
|
|
1293
|
-
authHeader = /^bearer\s+/i.test(token) ? token : `Bearer ${token}`;
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
return {
|
|
1297
|
-
providerName,
|
|
1298
|
-
baseUrl: normalizeBaseUrl(baseUrl),
|
|
1299
|
-
authHeader
|
|
1300
|
-
};
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
function createBuiltinProxyServer(settings, upstream) {
|
|
1304
|
-
const connections = new Set();
|
|
1305
|
-
const timeoutMs = settings.timeoutMs;
|
|
1306
|
-
|
|
1307
|
-
const server = http.createServer((req, res) => {
|
|
1308
|
-
let parsedIncoming;
|
|
1309
|
-
try {
|
|
1310
|
-
parsedIncoming = new URL(req.url || '/', 'http://localhost');
|
|
1311
|
-
} catch (e) {
|
|
1312
|
-
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1313
|
-
res.end(JSON.stringify({ error: 'invalid request path' }));
|
|
1314
|
-
return;
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
|
-
const remoteAddr = req && req.socket ? req.socket.remoteAddress : '';
|
|
1318
|
-
const isLoopback = !remoteAddr
|
|
1319
|
-
|| remoteAddr === '127.0.0.1'
|
|
1320
|
-
|| remoteAddr === '::1'
|
|
1321
|
-
|| remoteAddr === '::ffff:127.0.0.1';
|
|
1322
|
-
if (!isLoopback) {
|
|
1323
|
-
const expected = typeof process.env.CODEXMATE_HTTP_TOKEN === 'string'
|
|
1324
|
-
? process.env.CODEXMATE_HTTP_TOKEN.trim()
|
|
1325
|
-
: '';
|
|
1326
|
-
if (!expected) {
|
|
1327
|
-
const body = JSON.stringify({ error: 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' });
|
|
1328
|
-
res.writeHead(403, {
|
|
1329
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1330
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1331
|
-
});
|
|
1332
|
-
res.end(body, 'utf-8');
|
|
1333
|
-
return;
|
|
1334
|
-
}
|
|
1335
|
-
const headers = req && req.headers && typeof req.headers === 'object' ? req.headers : {};
|
|
1336
|
-
const rawAuth = typeof headers.authorization === 'string' ? headers.authorization.trim() : '';
|
|
1337
|
-
const match = rawAuth ? rawAuth.match(/^bearer\s+(.+)$/i) : null;
|
|
1338
|
-
const actual = match && match[1]
|
|
1339
|
-
? match[1].trim()
|
|
1340
|
-
: (rawAuth ? rawAuth : (typeof headers['x-codexmate-token'] === 'string' ? String(headers['x-codexmate-token']).trim() : ''));
|
|
1341
|
-
if (!actual || actual !== expected) {
|
|
1342
|
-
const body = JSON.stringify({ error: 'Unauthorized' });
|
|
1343
|
-
res.writeHead(401, {
|
|
1344
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1345
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1346
|
-
});
|
|
1347
|
-
res.end(body, 'utf-8');
|
|
1348
|
-
return;
|
|
1349
|
-
}
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
const incomingPath = parsedIncoming.pathname || '/';
|
|
1353
|
-
if (incomingPath === '/health' || incomingPath === '/status') {
|
|
1354
|
-
const body = JSON.stringify({
|
|
1355
|
-
ok: true,
|
|
1356
|
-
upstreamProvider: upstream.providerName,
|
|
1357
|
-
upstreamBaseUrl: upstream.baseUrl
|
|
1358
|
-
});
|
|
1359
|
-
res.writeHead(200, {
|
|
1360
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1361
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1362
|
-
});
|
|
1363
|
-
res.end(body, 'utf-8');
|
|
1364
|
-
return;
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
|
-
if (!(incomingPath === '/v1' || incomingPath.startsWith('/v1/'))) {
|
|
1368
|
-
const body = JSON.stringify({ error: 'proxy only supports /v1/* paths' });
|
|
1369
|
-
res.writeHead(404, {
|
|
1370
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1371
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1372
|
-
});
|
|
1373
|
-
res.end(body, 'utf-8');
|
|
1374
|
-
return;
|
|
1375
|
-
}
|
|
1376
|
-
|
|
1377
|
-
// Responses shim:
|
|
1378
|
-
// - Codex CLI 默认走 /v1/responses(含 SSE)
|
|
1379
|
-
// - 某些上游只支持 /v1/chat/completions
|
|
1380
|
-
// 因此这里优先尝试 /v1/responses(stream=false),失败再转换到 chat/completions 并回包为 responses。
|
|
1381
|
-
if ((incomingPath === '/v1/responses' || incomingPath === '/v1/responses/') && (req.method || 'GET').toUpperCase() === 'POST') {
|
|
1382
|
-
void (async () => {
|
|
1383
|
-
const { body, error } = await readRequestBody(req, 10 * 1024 * 1024);
|
|
1384
|
-
if (error) {
|
|
1385
|
-
res.writeHead(413, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1386
|
-
res.end(JSON.stringify({ error }));
|
|
1387
|
-
return;
|
|
1388
|
-
}
|
|
1389
|
-
const parsed = parseJsonOrError(body);
|
|
1390
|
-
if (parsed.error) {
|
|
1391
|
-
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1392
|
-
res.end(JSON.stringify({ error: `invalid json: ${parsed.error}` }));
|
|
1393
|
-
return;
|
|
1394
|
-
}
|
|
1395
|
-
|
|
1396
|
-
const payload = parsed.value && typeof parsed.value === 'object' ? parsed.value : {};
|
|
1397
|
-
const wantsStream = payload.stream === true;
|
|
1398
|
-
|
|
1399
|
-
const commonHeaders = {
|
|
1400
|
-
...(upstream.authHeader ? { 'Authorization': upstream.authHeader } : {}),
|
|
1401
|
-
'X-Codexmate-Proxy': '1'
|
|
1402
|
-
};
|
|
1403
|
-
|
|
1404
|
-
const upstreamResponses = await proxyRequestJsonWithFallbackUrls(upstream.baseUrl, 'responses', {
|
|
1405
|
-
method: 'POST',
|
|
1406
|
-
headers: commonHeaders,
|
|
1407
|
-
timeoutMs,
|
|
1408
|
-
body: { ...payload, stream: false }
|
|
1409
|
-
});
|
|
1410
|
-
|
|
1411
|
-
// 优先走上游 /responses(如果支持)。若上游报错且不是“端点不支持”,则直接透传错误。
|
|
1412
|
-
if (upstreamResponses.ok && upstreamResponses.status >= 200 && upstreamResponses.status < 300) {
|
|
1413
|
-
const json = parseJsonOrError(upstreamResponses.bodyText);
|
|
1414
|
-
if (json.error) {
|
|
1415
|
-
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1416
|
-
res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${json.error}` }));
|
|
1417
|
-
return;
|
|
1418
|
-
}
|
|
1419
|
-
const responsesPayload = ensureResponseMetadata(json.value);
|
|
1420
|
-
if (wantsStream) {
|
|
1421
|
-
res.writeHead(200, {
|
|
1422
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
1423
|
-
'Cache-Control': 'no-cache',
|
|
1424
|
-
'Connection': 'keep-alive',
|
|
1425
|
-
'X-Accel-Buffering': 'no'
|
|
1426
|
-
});
|
|
1427
|
-
sendResponsesSse(res, responsesPayload);
|
|
1428
|
-
res.end();
|
|
1429
|
-
return;
|
|
1430
|
-
}
|
|
1431
|
-
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1432
|
-
res.end(JSON.stringify(responsesPayload));
|
|
1433
|
-
return;
|
|
1434
|
-
}
|
|
1435
|
-
|
|
1436
|
-
if (upstreamResponses.ok && upstreamResponses.status >= 400) {
|
|
1437
|
-
if (!shouldFallbackFromUpstreamResponses(upstreamResponses.status, upstreamResponses.bodyText)) {
|
|
1438
|
-
res.writeHead(upstreamResponses.status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1439
|
-
res.end(upstreamResponses.bodyText || JSON.stringify({ error: 'Upstream error' }));
|
|
1440
|
-
return;
|
|
1441
|
-
}
|
|
1442
|
-
// fallthrough to chat/completions conversion
|
|
1443
|
-
}
|
|
1444
|
-
|
|
1445
|
-
if (!upstreamResponses.ok) {
|
|
1446
|
-
if (!shouldFallbackFromUpstreamResponsesFailure(upstreamResponses.error)) {
|
|
1447
|
-
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1448
|
-
res.end(JSON.stringify({ error: upstreamResponses.error || 'Upstream request failed' }));
|
|
1449
|
-
return;
|
|
1450
|
-
}
|
|
1451
|
-
// Some OpenAI-compatible gateways accept /responses but never complete it.
|
|
1452
|
-
// Treat that as an unsupported Responses endpoint and try the chat fallback.
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
const model = typeof payload.model === 'string' ? payload.model : '';
|
|
1456
|
-
const chatBody = buildChatCompletionsBodyFromResponsesPayload(payload);
|
|
1457
|
-
|
|
1458
|
-
if (wantsStream) {
|
|
1459
|
-
const streamingChatBody = { ...chatBody, stream: true };
|
|
1460
|
-
const streamed = await streamChatCompletionsAsResponsesSseWithFallbackUrls(upstream.baseUrl, 'chat/completions', {
|
|
1461
|
-
method: 'POST',
|
|
1462
|
-
headers: commonHeaders,
|
|
1463
|
-
timeoutMs,
|
|
1464
|
-
body: streamingChatBody,
|
|
1465
|
-
res,
|
|
1466
|
-
model
|
|
1467
|
-
});
|
|
1468
|
-
if (!streamed.ok) {
|
|
1469
|
-
if (!res.headersSent) {
|
|
1470
|
-
res.writeHead(streamed.status && streamed.status >= 400 ? streamed.status : 502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1471
|
-
res.end(streamed.bodyText || JSON.stringify({ error: streamed.error || 'proxy request failed' }));
|
|
1472
|
-
} else if (!res.writableEnded) {
|
|
1473
|
-
writeSse(res, 'response.failed', { type: 'response.failed', error: streamed.error || streamed.bodyText || 'proxy request failed' });
|
|
1474
|
-
writeSse(res, 'done', '[DONE]');
|
|
1475
|
-
res.end();
|
|
1476
|
-
}
|
|
1477
|
-
}
|
|
1478
|
-
return;
|
|
1479
|
-
}
|
|
1480
|
-
|
|
1481
|
-
const upstreamChat = await proxyRequestJsonWithFallbackUrls(upstream.baseUrl, 'chat/completions', {
|
|
1482
|
-
method: 'POST',
|
|
1483
|
-
headers: commonHeaders,
|
|
1484
|
-
timeoutMs,
|
|
1485
|
-
body: chatBody
|
|
1486
|
-
});
|
|
1487
|
-
if (!upstreamChat.ok) {
|
|
1488
|
-
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1489
|
-
res.end(JSON.stringify({ error: upstreamChat.error || 'proxy request failed' }));
|
|
1490
|
-
return;
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1493
|
-
if (upstreamChat.status >= 400) {
|
|
1494
|
-
res.writeHead(upstreamChat.status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1495
|
-
res.end(upstreamChat.bodyText || JSON.stringify({ error: 'Upstream error' }));
|
|
1496
|
-
return;
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
const chatJson = parseJsonOrError(upstreamChat.bodyText);
|
|
1500
|
-
if (chatJson.error) {
|
|
1501
|
-
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1502
|
-
res.end(JSON.stringify({ error: `invalid upstream response: ${chatJson.error}` }));
|
|
1503
|
-
return;
|
|
1504
|
-
}
|
|
1505
|
-
|
|
1506
|
-
const responsesPayload = buildResponsesPayloadFromChatCompletion(chatJson.value, model);
|
|
1507
|
-
|
|
1508
|
-
if (wantsStream) {
|
|
1509
|
-
res.writeHead(200, {
|
|
1510
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
1511
|
-
'Cache-Control': 'no-cache',
|
|
1512
|
-
'Connection': 'keep-alive',
|
|
1513
|
-
'X-Accel-Buffering': 'no'
|
|
1514
|
-
});
|
|
1515
|
-
sendResponsesSse(res, responsesPayload);
|
|
1516
|
-
res.end();
|
|
1517
|
-
return;
|
|
1518
|
-
}
|
|
1519
|
-
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1520
|
-
res.end(JSON.stringify(responsesPayload));
|
|
1521
|
-
})();
|
|
1522
|
-
return;
|
|
1523
|
-
}
|
|
1524
|
-
|
|
1525
|
-
const suffix = incomingPath === '/v1'
|
|
1526
|
-
? ''
|
|
1527
|
-
: incomingPath.replace(/^\/v1\/?/, '');
|
|
1528
|
-
const targetBase = joinApiUrl(upstream.baseUrl, suffix);
|
|
1529
|
-
if (!targetBase) {
|
|
1530
|
-
const body = JSON.stringify({ error: 'failed to build upstream URL' });
|
|
1531
|
-
res.writeHead(500, {
|
|
1532
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1533
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1534
|
-
});
|
|
1535
|
-
res.end(body, 'utf-8');
|
|
1536
|
-
return;
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
let targetUrl;
|
|
1540
|
-
try {
|
|
1541
|
-
targetUrl = new URL(targetBase);
|
|
1542
|
-
targetUrl.search = parsedIncoming.search || '';
|
|
1543
|
-
} catch (e) {
|
|
1544
|
-
const body = JSON.stringify({ error: `invalid upstream URL: ${e.message}` });
|
|
1545
|
-
res.writeHead(500, {
|
|
1546
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1547
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1548
|
-
});
|
|
1549
|
-
res.end(body, 'utf-8');
|
|
1550
|
-
return;
|
|
1551
|
-
}
|
|
1552
|
-
|
|
1553
|
-
const requestHeaders = { ...req.headers };
|
|
1554
|
-
delete requestHeaders.host;
|
|
1555
|
-
delete requestHeaders.connection;
|
|
1556
|
-
delete requestHeaders['content-length'];
|
|
1557
|
-
if (upstream.authHeader) {
|
|
1558
|
-
requestHeaders.authorization = upstream.authHeader;
|
|
1559
|
-
}
|
|
1560
|
-
requestHeaders['x-codexmate-proxy'] = '1';
|
|
1561
|
-
if (!requestHeaders['x-forwarded-for'] && req.socket && req.socket.remoteAddress) {
|
|
1562
|
-
requestHeaders['x-forwarded-for'] = req.socket.remoteAddress;
|
|
1563
|
-
}
|
|
1564
|
-
|
|
1565
|
-
const transport = targetUrl.protocol === 'https:' ? https : http;
|
|
1566
|
-
const upstreamReq = transport.request({
|
|
1567
|
-
protocol: targetUrl.protocol,
|
|
1568
|
-
hostname: targetUrl.hostname,
|
|
1569
|
-
port: targetUrl.port || (targetUrl.protocol === 'https:' ? 443 : 80),
|
|
1570
|
-
method: req.method || 'GET',
|
|
1571
|
-
path: `${targetUrl.pathname}${targetUrl.search}`,
|
|
1572
|
-
headers: requestHeaders,
|
|
1573
|
-
agent: targetUrl.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
1574
|
-
}, (upstreamRes) => {
|
|
1575
|
-
const responseHeaders = { ...upstreamRes.headers };
|
|
1576
|
-
delete responseHeaders.connection;
|
|
1577
|
-
res.writeHead(upstreamRes.statusCode || 502, responseHeaders);
|
|
1578
|
-
upstreamRes.pipe(res);
|
|
1579
|
-
});
|
|
1580
|
-
|
|
1581
|
-
upstreamReq.setTimeout(timeoutMs, () => {
|
|
1582
|
-
upstreamReq.destroy(new Error(`upstream timeout (${timeoutMs}ms)`));
|
|
1583
|
-
});
|
|
1584
|
-
|
|
1585
|
-
upstreamReq.on('error', (err) => {
|
|
1586
|
-
if (res.headersSent) {
|
|
1587
|
-
try { res.destroy(err); } catch (_) {}
|
|
1588
|
-
return;
|
|
1589
|
-
}
|
|
1590
|
-
const body = JSON.stringify({ error: `proxy request failed: ${err.message}` });
|
|
1591
|
-
res.writeHead(502, {
|
|
1592
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
1593
|
-
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1594
|
-
});
|
|
1595
|
-
res.end(body, 'utf-8');
|
|
1596
|
-
});
|
|
1597
|
-
|
|
1598
|
-
req.pipe(upstreamReq);
|
|
1599
|
-
});
|
|
1600
|
-
|
|
1601
|
-
server.on('connection', (socket) => {
|
|
1602
|
-
connections.add(socket);
|
|
1603
|
-
socket.on('close', () => connections.delete(socket));
|
|
1604
|
-
});
|
|
1605
|
-
|
|
1606
|
-
return new Promise((resolve, reject) => {
|
|
1607
|
-
server.once('error', reject);
|
|
1608
|
-
server.listen(settings.port, settings.host, () => {
|
|
1609
|
-
server.removeListener('error', reject);
|
|
1610
|
-
resolve({
|
|
1611
|
-
server,
|
|
1612
|
-
connections,
|
|
1613
|
-
settings,
|
|
1614
|
-
upstream,
|
|
1615
|
-
startedAt: toIsoTime(Date.now()),
|
|
1616
|
-
listenUrl: buildProxyListenUrl(settings)
|
|
1617
|
-
});
|
|
1618
|
-
});
|
|
1619
|
-
});
|
|
1620
|
-
}
|
|
1621
|
-
|
|
1622
|
-
async function startBuiltinProxyRuntime(payload = {}) {
|
|
1623
|
-
if (runtime) {
|
|
1624
|
-
return {
|
|
1625
|
-
error: '内建代理已在运行',
|
|
1626
|
-
runtime: {
|
|
1627
|
-
listenUrl: runtime.listenUrl,
|
|
1628
|
-
upstreamProvider: runtime.upstream.providerName
|
|
1629
|
-
}
|
|
1630
|
-
};
|
|
1631
|
-
}
|
|
1632
|
-
|
|
1633
|
-
const saveResult = saveBuiltinProxySettings(payload);
|
|
1634
|
-
if (saveResult.error) {
|
|
1635
|
-
return { error: saveResult.error };
|
|
1636
|
-
}
|
|
1637
|
-
const settings = saveResult.settings;
|
|
1638
|
-
const upstream = resolveBuiltinProxyUpstream(settings);
|
|
1639
|
-
if (upstream.error) {
|
|
1640
|
-
return { error: upstream.error };
|
|
1641
|
-
}
|
|
1642
|
-
|
|
1643
|
-
try {
|
|
1644
|
-
runtime = await createBuiltinProxyServer(settings, upstream);
|
|
1645
|
-
return {
|
|
1646
|
-
success: true,
|
|
1647
|
-
running: true,
|
|
1648
|
-
listenUrl: runtime.listenUrl,
|
|
1649
|
-
upstreamProvider: upstream.providerName,
|
|
1650
|
-
settings
|
|
1651
|
-
};
|
|
1652
|
-
} catch (e) {
|
|
1653
|
-
return { error: `启动内建代理失败: ${e.message}` };
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
|
|
1657
|
-
async function stopBuiltinProxyRuntime() {
|
|
1658
|
-
if (!runtime) {
|
|
1659
|
-
return { success: true, running: false };
|
|
1660
|
-
}
|
|
1661
|
-
const currentRuntime = runtime;
|
|
1662
|
-
runtime = null;
|
|
1663
|
-
|
|
1664
|
-
await new Promise((resolve) => {
|
|
1665
|
-
let settled = false;
|
|
1666
|
-
const finish = () => {
|
|
1667
|
-
if (settled) return;
|
|
1668
|
-
settled = true;
|
|
1669
|
-
resolve();
|
|
1670
|
-
};
|
|
1671
|
-
|
|
1672
|
-
currentRuntime.server.close(() => finish());
|
|
1673
|
-
setTimeout(() => finish(), 1000);
|
|
1674
|
-
});
|
|
1675
|
-
|
|
1676
|
-
for (const socket of currentRuntime.connections) {
|
|
1677
|
-
try { socket.destroy(); } catch (_) {}
|
|
1678
|
-
}
|
|
1679
|
-
currentRuntime.connections.clear();
|
|
1680
|
-
|
|
1681
|
-
return {
|
|
1682
|
-
success: true,
|
|
1683
|
-
running: false
|
|
1684
|
-
};
|
|
1685
|
-
}
|
|
1686
|
-
|
|
1687
|
-
function getBuiltinProxyStatus() {
|
|
1688
|
-
const settings = readBuiltinProxySettings();
|
|
1689
|
-
return {
|
|
1690
|
-
running: !!runtime,
|
|
1691
|
-
settings,
|
|
1692
|
-
runtime: runtime
|
|
1693
|
-
? {
|
|
1694
|
-
provider: BUILTIN_PROXY_PROVIDER_NAME,
|
|
1695
|
-
startedAt: runtime.startedAt,
|
|
1696
|
-
listenUrl: runtime.listenUrl,
|
|
1697
|
-
upstreamProvider: runtime.upstream.providerName,
|
|
1698
|
-
upstreamBaseUrl: runtime.upstream.baseUrl
|
|
1699
|
-
}
|
|
1700
|
-
: null
|
|
1701
|
-
};
|
|
1702
|
-
}
|
|
1703
|
-
|
|
1704
|
-
return {
|
|
1705
|
-
canListenPort,
|
|
1706
|
-
findAvailablePort,
|
|
1707
|
-
normalizeBuiltinProxySettings,
|
|
1708
|
-
readBuiltinProxySettings,
|
|
1709
|
-
resolveBuiltinProxyProviderName,
|
|
1710
|
-
saveBuiltinProxySettings,
|
|
1711
|
-
buildProxyListenUrl,
|
|
1712
|
-
buildBuiltinProxyProviderBaseUrl,
|
|
1713
|
-
removePersistedBuiltinProxyProviderFromConfig,
|
|
1714
|
-
hasCodexConfigReadyForProxy,
|
|
1715
|
-
resolveBuiltinProxyUpstream,
|
|
1716
|
-
createBuiltinProxyServer,
|
|
1717
|
-
startBuiltinProxyRuntime,
|
|
1718
|
-
stopBuiltinProxyRuntime,
|
|
1719
|
-
getBuiltinProxyStatus
|
|
1720
|
-
};
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
|
-
module.exports = {
|
|
1724
|
-
createBuiltinProxyRuntimeController
|
|
1725
|
-
};
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const net = require('net');
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const toml = require('@iarna/toml');
|
|
5
|
+
const { readJsonFile, writeJsonAtomic } = require('../lib/cli-file-utils');
|
|
6
|
+
const { isValidHttpUrl, normalizeBaseUrl, joinApiUrl } = require('../lib/cli-utils');
|
|
7
|
+
const { toIsoTime } = require('../lib/cli-session-utils');
|
|
8
|
+
|
|
9
|
+
function createBuiltinProxyRuntimeController(deps = {}) {
|
|
10
|
+
const {
|
|
11
|
+
fs,
|
|
12
|
+
https,
|
|
13
|
+
CONFIG_FILE,
|
|
14
|
+
BUILTIN_PROXY_SETTINGS_FILE,
|
|
15
|
+
DEFAULT_BUILTIN_PROXY_SETTINGS,
|
|
16
|
+
BUILTIN_PROXY_PROVIDER_NAME,
|
|
17
|
+
CODEXMATE_MANAGED_MARKER,
|
|
18
|
+
HTTP_KEEP_ALIVE_AGENT,
|
|
19
|
+
HTTPS_KEEP_ALIVE_AGENT,
|
|
20
|
+
readConfig,
|
|
21
|
+
writeConfig,
|
|
22
|
+
readConfigOrVirtualDefault,
|
|
23
|
+
resolveAuthTokenFromCurrentProfile,
|
|
24
|
+
isPlainObject,
|
|
25
|
+
isBuiltinManagedProvider,
|
|
26
|
+
findProviderSectionRanges,
|
|
27
|
+
findProviderDescendantSectionRanges,
|
|
28
|
+
normalizeLegacySegments,
|
|
29
|
+
buildLegacySegmentsKey,
|
|
30
|
+
formatHostForUrl
|
|
31
|
+
} = deps;
|
|
32
|
+
|
|
33
|
+
if (!fs) throw new Error('createBuiltinProxyRuntimeController 缺少 fs');
|
|
34
|
+
if (!https) throw new Error('createBuiltinProxyRuntimeController 缺少 https');
|
|
35
|
+
if (!CONFIG_FILE) throw new Error('createBuiltinProxyRuntimeController 缺少 CONFIG_FILE');
|
|
36
|
+
if (!BUILTIN_PROXY_SETTINGS_FILE) throw new Error('createBuiltinProxyRuntimeController 缺少 BUILTIN_PROXY_SETTINGS_FILE');
|
|
37
|
+
if (!DEFAULT_BUILTIN_PROXY_SETTINGS || typeof DEFAULT_BUILTIN_PROXY_SETTINGS !== 'object') {
|
|
38
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 DEFAULT_BUILTIN_PROXY_SETTINGS');
|
|
39
|
+
}
|
|
40
|
+
if (!BUILTIN_PROXY_PROVIDER_NAME) throw new Error('createBuiltinProxyRuntimeController 缺少 BUILTIN_PROXY_PROVIDER_NAME');
|
|
41
|
+
if (typeof readConfig !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 readConfig');
|
|
42
|
+
if (typeof writeConfig !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 writeConfig');
|
|
43
|
+
if (typeof readConfigOrVirtualDefault !== 'function') {
|
|
44
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 readConfigOrVirtualDefault');
|
|
45
|
+
}
|
|
46
|
+
if (typeof resolveAuthTokenFromCurrentProfile !== 'function') {
|
|
47
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 resolveAuthTokenFromCurrentProfile');
|
|
48
|
+
}
|
|
49
|
+
if (typeof isPlainObject !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 isPlainObject');
|
|
50
|
+
if (typeof isBuiltinManagedProvider !== 'function') {
|
|
51
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 isBuiltinManagedProvider');
|
|
52
|
+
}
|
|
53
|
+
if (typeof findProviderSectionRanges !== 'function') {
|
|
54
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 findProviderSectionRanges');
|
|
55
|
+
}
|
|
56
|
+
if (typeof findProviderDescendantSectionRanges !== 'function') {
|
|
57
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 findProviderDescendantSectionRanges');
|
|
58
|
+
}
|
|
59
|
+
if (typeof normalizeLegacySegments !== 'function') {
|
|
60
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 normalizeLegacySegments');
|
|
61
|
+
}
|
|
62
|
+
if (typeof buildLegacySegmentsKey !== 'function') {
|
|
63
|
+
throw new Error('createBuiltinProxyRuntimeController 缺少 buildLegacySegmentsKey');
|
|
64
|
+
}
|
|
65
|
+
if (typeof formatHostForUrl !== 'function') throw new Error('createBuiltinProxyRuntimeController 缺少 formatHostForUrl');
|
|
66
|
+
|
|
67
|
+
let runtime = null;
|
|
68
|
+
|
|
69
|
+
function readRequestBody(req, maxBytes) {
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
let body = '';
|
|
72
|
+
let size = 0;
|
|
73
|
+
let aborted = false;
|
|
74
|
+
req.on('data', (chunk) => {
|
|
75
|
+
if (aborted) return;
|
|
76
|
+
size += chunk.length;
|
|
77
|
+
if (Number.isFinite(maxBytes) && maxBytes > 0 && size > maxBytes) {
|
|
78
|
+
aborted = true;
|
|
79
|
+
try { req.destroy(); } catch (_) {}
|
|
80
|
+
resolve({ error: '请求体过大' });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
body += chunk;
|
|
84
|
+
});
|
|
85
|
+
req.on('end', () => {
|
|
86
|
+
if (aborted) return;
|
|
87
|
+
resolve({ body });
|
|
88
|
+
});
|
|
89
|
+
req.on('error', (err) => resolve({ error: err && err.message ? err.message : 'request failed' }));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function parseJsonOrError(text) {
|
|
94
|
+
if (typeof text !== 'string' || !text.trim()) {
|
|
95
|
+
return { value: null, error: 'empty body' };
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
return { value: JSON.parse(text), error: '' };
|
|
99
|
+
} catch (e) {
|
|
100
|
+
return { value: null, error: e && e.message ? e.message : 'invalid json' };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function shouldFallbackFromUpstreamResponses(status, bodyText) {
|
|
105
|
+
if (!Number.isFinite(status)) return false;
|
|
106
|
+
if (status === 404 || status === 405 || status === 501) return true;
|
|
107
|
+
const text = String(bodyText || '');
|
|
108
|
+
if (!text) return false;
|
|
109
|
+
if (/not implemented/i.test(text)) return true;
|
|
110
|
+
if (/convert_request_failed/i.test(text)) return true;
|
|
111
|
+
try {
|
|
112
|
+
const parsed = JSON.parse(text);
|
|
113
|
+
const code = parsed && parsed.error && typeof parsed.error.code === 'string' ? parsed.error.code : '';
|
|
114
|
+
const msg = parsed && parsed.error && typeof parsed.error.message === 'string' ? parsed.error.message : '';
|
|
115
|
+
if (code === 'convert_request_failed') return true;
|
|
116
|
+
if (/not implemented/i.test(msg)) return true;
|
|
117
|
+
} catch (_) {}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function shouldFallbackFromUpstreamResponsesFailure(error) {
|
|
122
|
+
const text = String(error || '').trim();
|
|
123
|
+
if (!text) return false;
|
|
124
|
+
if (/timeout/i.test(text)) return true;
|
|
125
|
+
if (/socket hang up/i.test(text)) return true;
|
|
126
|
+
if (/ECONNRESET/i.test(text)) return true;
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isTransientNetworkError(error) {
|
|
131
|
+
const text = String(error || '').trim();
|
|
132
|
+
if (!text) return false;
|
|
133
|
+
if (/socket hang up/i.test(text)) return true;
|
|
134
|
+
if (/ECONNRESET|ECONNREFUSED|EPIPE|EPROTO|ETIMEDOUT/i.test(text)) return true;
|
|
135
|
+
if (/EAI_AGAIN/i.test(text)) return true;
|
|
136
|
+
if (/UND_ERR_SOCKET/i.test(text)) return true;
|
|
137
|
+
if (/disconnected before|secure tls|tls handshake/i.test(text)) return true;
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const TRANSIENT_RETRY_DELAYS_MS = [200, 600];
|
|
142
|
+
|
|
143
|
+
async function retryTransientRequest(executor) {
|
|
144
|
+
let lastResult = null;
|
|
145
|
+
for (let attempt = 0; attempt <= TRANSIENT_RETRY_DELAYS_MS.length; attempt += 1) {
|
|
146
|
+
if (attempt > 0) {
|
|
147
|
+
const delay = TRANSIENT_RETRY_DELAYS_MS[attempt - 1];
|
|
148
|
+
// eslint-disable-next-line no-await-in-loop
|
|
149
|
+
await new Promise((r) => {
|
|
150
|
+
const t = setTimeout(r, delay);
|
|
151
|
+
if (typeof t.unref === 'function') t.unref();
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// eslint-disable-next-line no-await-in-loop
|
|
155
|
+
const result = await executor(attempt);
|
|
156
|
+
lastResult = result;
|
|
157
|
+
if (!result) return result;
|
|
158
|
+
if (result.ok) return result;
|
|
159
|
+
if (result.retry) return result;
|
|
160
|
+
if (result.status && result.status > 0) return result;
|
|
161
|
+
if (!isTransientNetworkError(result.error)) return result;
|
|
162
|
+
}
|
|
163
|
+
return lastResult;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function proxyRequestJson(targetUrl, options = {}) {
|
|
167
|
+
const parsed = new URL(targetUrl);
|
|
168
|
+
const transport = parsed.protocol === 'https:' ? https : http;
|
|
169
|
+
const bodyText = options.body ? JSON.stringify(options.body) : '';
|
|
170
|
+
const headers = {
|
|
171
|
+
'Accept': 'application/json',
|
|
172
|
+
...(options.body ? { 'Content-Type': 'application/json' } : {}),
|
|
173
|
+
...(options.headers || {})
|
|
174
|
+
};
|
|
175
|
+
if (options.body) {
|
|
176
|
+
headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
|
|
177
|
+
}
|
|
178
|
+
const timeoutMs = Number.isFinite(options.timeoutMs)
|
|
179
|
+
? Math.max(1000, Number(options.timeoutMs))
|
|
180
|
+
: 30000;
|
|
181
|
+
|
|
182
|
+
return new Promise((resolve) => {
|
|
183
|
+
let settled = false;
|
|
184
|
+
const finish = (value) => {
|
|
185
|
+
if (settled) return;
|
|
186
|
+
settled = true;
|
|
187
|
+
resolve(value);
|
|
188
|
+
};
|
|
189
|
+
const req = transport.request({
|
|
190
|
+
protocol: parsed.protocol,
|
|
191
|
+
hostname: parsed.hostname,
|
|
192
|
+
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
193
|
+
method: options.method || 'GET',
|
|
194
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
195
|
+
headers,
|
|
196
|
+
agent: parsed.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
197
|
+
}, (upstreamRes) => {
|
|
198
|
+
const chunks = [];
|
|
199
|
+
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
200
|
+
upstreamRes.on('end', () => {
|
|
201
|
+
const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
|
|
202
|
+
finish({
|
|
203
|
+
ok: true,
|
|
204
|
+
status: upstreamRes.statusCode || 0,
|
|
205
|
+
headers: upstreamRes.headers || {},
|
|
206
|
+
bodyText: text
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
req.setTimeout(timeoutMs, () => {
|
|
211
|
+
try { req.destroy(new Error('timeout')); } catch (_) {}
|
|
212
|
+
finish({ ok: false, error: 'timeout' });
|
|
213
|
+
});
|
|
214
|
+
req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
|
|
215
|
+
if (bodyText) {
|
|
216
|
+
req.write(bodyText);
|
|
217
|
+
}
|
|
218
|
+
req.end();
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function buildUpstreamUrlCandidates(baseUrl, pathSuffix) {
|
|
223
|
+
const safeSuffix = String(pathSuffix || '').replace(/^\/+/, '');
|
|
224
|
+
const candidates = [];
|
|
225
|
+
const push = (url) => {
|
|
226
|
+
if (url && !candidates.includes(url)) {
|
|
227
|
+
candidates.push(url);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
push(joinApiUrl(baseUrl, safeSuffix));
|
|
231
|
+
const trimmed = normalizeBaseUrl(baseUrl);
|
|
232
|
+
if (trimmed && safeSuffix) {
|
|
233
|
+
push(`${trimmed}/${safeSuffix}`);
|
|
234
|
+
}
|
|
235
|
+
return candidates;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function proxyRequestJsonWithFallbackUrls(baseUrl, pathSuffix, options = {}) {
|
|
239
|
+
const urls = buildUpstreamUrlCandidates(baseUrl, pathSuffix);
|
|
240
|
+
if (urls.length === 0) {
|
|
241
|
+
return { ok: false, error: 'failed to build upstream URL' };
|
|
242
|
+
}
|
|
243
|
+
let lastResult = null;
|
|
244
|
+
for (let index = 0; index < urls.length; index += 1) {
|
|
245
|
+
const result = await retryTransientRequest(() => proxyRequestJson(urls[index], options));
|
|
246
|
+
lastResult = result;
|
|
247
|
+
if (!result.ok) {
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
if (!(result.status === 404 || result.status === 405)) {
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return lastResult || { ok: false, error: 'failed to build upstream URL' };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function stringifyJsonValue(value, fallback = '') {
|
|
258
|
+
if (typeof value === 'string') return value;
|
|
259
|
+
if (value == null) return fallback;
|
|
260
|
+
try {
|
|
261
|
+
return JSON.stringify(value);
|
|
262
|
+
} catch (_) {
|
|
263
|
+
return fallback;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function normalizeChatUsageToResponsesUsage(usage) {
|
|
268
|
+
if (!usage || typeof usage !== 'object' || Array.isArray(usage)) return undefined;
|
|
269
|
+
const pickNumber = (...keys) => {
|
|
270
|
+
for (const key of keys) {
|
|
271
|
+
if (Number.isFinite(usage[key])) return usage[key];
|
|
272
|
+
}
|
|
273
|
+
return undefined;
|
|
274
|
+
};
|
|
275
|
+
const inputTokens = pickNumber('input_tokens', 'prompt_tokens');
|
|
276
|
+
const outputTokens = pickNumber('output_tokens', 'completion_tokens');
|
|
277
|
+
const totalTokens = pickNumber('total_tokens');
|
|
278
|
+
const result = {};
|
|
279
|
+
if (inputTokens != null) result.input_tokens = inputTokens;
|
|
280
|
+
if (outputTokens != null) result.output_tokens = outputTokens;
|
|
281
|
+
if (totalTokens != null) result.total_tokens = totalTokens;
|
|
282
|
+
if (usage.input_tokens_details && typeof usage.input_tokens_details === 'object') {
|
|
283
|
+
result.input_tokens_details = usage.input_tokens_details;
|
|
284
|
+
} else if (usage.prompt_tokens_details && typeof usage.prompt_tokens_details === 'object') {
|
|
285
|
+
result.input_tokens_details = usage.prompt_tokens_details;
|
|
286
|
+
}
|
|
287
|
+
if (usage.output_tokens_details && typeof usage.output_tokens_details === 'object') {
|
|
288
|
+
result.output_tokens_details = usage.output_tokens_details;
|
|
289
|
+
} else if (usage.completion_tokens_details && typeof usage.completion_tokens_details === 'object') {
|
|
290
|
+
result.output_tokens_details = usage.completion_tokens_details;
|
|
291
|
+
}
|
|
292
|
+
return Object.keys(result).length > 0 ? result : usage;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function mapChatFinishReasonToResponses(choice) {
|
|
296
|
+
const finishReason = choice && typeof choice === 'object' && typeof choice.finish_reason === 'string'
|
|
297
|
+
? choice.finish_reason
|
|
298
|
+
: '';
|
|
299
|
+
if (finishReason === 'length') {
|
|
300
|
+
return { status: 'incomplete', incomplete_details: { reason: 'max_output_tokens' } };
|
|
301
|
+
}
|
|
302
|
+
if (finishReason === 'content_filter') {
|
|
303
|
+
return { status: 'incomplete', incomplete_details: { reason: 'content_filter' } };
|
|
304
|
+
}
|
|
305
|
+
return { status: 'completed' };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function normalizeChatMessageContentToResponsesContent(content, refusal = '') {
|
|
309
|
+
const blocks = [];
|
|
310
|
+
const pushText = (text) => {
|
|
311
|
+
if (typeof text === 'string' && text) {
|
|
312
|
+
blocks.push({ type: 'output_text', text });
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
if (typeof content === 'string') {
|
|
316
|
+
pushText(content);
|
|
317
|
+
} else if (Array.isArray(content)) {
|
|
318
|
+
for (const item of content) {
|
|
319
|
+
if (!item) continue;
|
|
320
|
+
if (typeof item === 'string') {
|
|
321
|
+
pushText(item);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (typeof item !== 'object') continue;
|
|
325
|
+
const type = typeof item.type === 'string' ? item.type : '';
|
|
326
|
+
if ((type === 'text' || type === 'output_text') && typeof item.text === 'string') {
|
|
327
|
+
pushText(item.text);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (typeof item.content === 'string') {
|
|
331
|
+
pushText(item.content);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (typeof refusal === 'string' && refusal) {
|
|
336
|
+
blocks.push({ type: 'refusal', refusal });
|
|
337
|
+
}
|
|
338
|
+
return blocks;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function buildResponsesPayloadFromChatCompletion(payload, fallbackModel = '') {
|
|
342
|
+
const base = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
343
|
+
const choice = Array.isArray(base.choices) ? base.choices[0] : null;
|
|
344
|
+
const message = choice && typeof choice === 'object' && choice.message && typeof choice.message === 'object'
|
|
345
|
+
? choice.message
|
|
346
|
+
: {};
|
|
347
|
+
const output = [];
|
|
348
|
+
const messageContent = normalizeChatMessageContentToResponsesContent(message.content, message.refusal);
|
|
349
|
+
if (messageContent.length > 0 || !Array.isArray(message.tool_calls) || message.tool_calls.length === 0) {
|
|
350
|
+
output.push({
|
|
351
|
+
type: 'message',
|
|
352
|
+
role: 'assistant',
|
|
353
|
+
content: messageContent.length > 0 ? messageContent : [{ type: 'output_text', text: '' }]
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (Array.isArray(message.tool_calls)) {
|
|
357
|
+
for (const toolCall of message.tool_calls) {
|
|
358
|
+
if (!toolCall || typeof toolCall !== 'object') continue;
|
|
359
|
+
const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : {};
|
|
360
|
+
const name = typeof fn.name === 'string' ? fn.name : '';
|
|
361
|
+
if (!name) continue;
|
|
362
|
+
output.push({
|
|
363
|
+
type: 'function_call',
|
|
364
|
+
call_id: typeof toolCall.id === 'string' && toolCall.id ? toolCall.id : `call_${crypto.randomBytes(8).toString('hex')}`,
|
|
365
|
+
name,
|
|
366
|
+
arguments: stringifyJsonValue(fn.arguments, '{}')
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const finish = mapChatFinishReasonToResponses(choice);
|
|
371
|
+
return ensureResponseMetadata({
|
|
372
|
+
id: typeof base.id === 'string' ? base.id : undefined,
|
|
373
|
+
model: typeof base.model === 'string' ? base.model : fallbackModel,
|
|
374
|
+
status: finish.status,
|
|
375
|
+
...(finish.incomplete_details ? { incomplete_details: finish.incomplete_details } : {}),
|
|
376
|
+
output,
|
|
377
|
+
usage: normalizeChatUsageToResponsesUsage(base.usage)
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function normalizeResponsesInputToChatMessages(input) {
|
|
382
|
+
// 参考 cc-switch 的 Responses 转换形态:message content 保持为消息,function_call /
|
|
383
|
+
// function_call_output 提升为 OpenAI Chat 的 assistant tool_calls / tool 消息。
|
|
384
|
+
const toChatContent = (blocks) => {
|
|
385
|
+
if (!Array.isArray(blocks)) return '';
|
|
386
|
+
const out = [];
|
|
387
|
+
for (const block of blocks) {
|
|
388
|
+
if (!block || typeof block !== 'object') continue;
|
|
389
|
+
const type = typeof block.type === 'string' ? block.type : '';
|
|
390
|
+
if ((type === 'input_text' || type === 'output_text' || type === 'text') && typeof block.text === 'string') {
|
|
391
|
+
out.push({ type: 'text', text: block.text });
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (type === 'refusal' && typeof block.refusal === 'string') {
|
|
395
|
+
out.push({ type: 'text', text: block.refusal });
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (type === 'input_image') {
|
|
399
|
+
const raw = block.image_url != null ? block.image_url : block.imageUrl;
|
|
400
|
+
const url = typeof raw === 'string'
|
|
401
|
+
? raw
|
|
402
|
+
: (raw && typeof raw === 'object' && typeof raw.url === 'string' ? raw.url : '');
|
|
403
|
+
if (url) {
|
|
404
|
+
out.push({ type: 'image_url', image_url: { url } });
|
|
405
|
+
}
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (type === 'image_url' && block.image_url) {
|
|
409
|
+
out.push({ type: 'image_url', image_url: block.image_url });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (out.length === 0) return '';
|
|
413
|
+
return out;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const messageFromResponsesItem = (item) => {
|
|
417
|
+
if (!item || typeof item !== 'object') return null;
|
|
418
|
+
const type = typeof item.type === 'string' ? item.type : '';
|
|
419
|
+
if (type === 'function_call') {
|
|
420
|
+
const name = typeof item.name === 'string' ? item.name : '';
|
|
421
|
+
if (!name) return null;
|
|
422
|
+
return {
|
|
423
|
+
role: 'assistant',
|
|
424
|
+
content: null,
|
|
425
|
+
tool_calls: [{
|
|
426
|
+
id: typeof item.call_id === 'string' && item.call_id ? item.call_id : (typeof item.id === 'string' ? item.id : `call_${crypto.randomBytes(8).toString('hex')}`),
|
|
427
|
+
type: 'function',
|
|
428
|
+
function: {
|
|
429
|
+
name,
|
|
430
|
+
arguments: stringifyJsonValue(item.arguments, '{}')
|
|
431
|
+
}
|
|
432
|
+
}]
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
if (type === 'function_call_output') {
|
|
436
|
+
const callId = typeof item.call_id === 'string' ? item.call_id : '';
|
|
437
|
+
return {
|
|
438
|
+
role: 'tool',
|
|
439
|
+
tool_call_id: callId,
|
|
440
|
+
content: stringifyJsonValue(item.output, '')
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
if (typeof item.role === 'string' && item.content != null) {
|
|
444
|
+
const role = item.role.trim() || 'user';
|
|
445
|
+
const content = Array.isArray(item.content)
|
|
446
|
+
? toChatContent(item.content)
|
|
447
|
+
: item.content;
|
|
448
|
+
return content || content === null ? { role, content } : null;
|
|
449
|
+
}
|
|
450
|
+
if (type) {
|
|
451
|
+
const content = toChatContent([item]);
|
|
452
|
+
return content ? { role: 'user', content } : null;
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
if (typeof input === 'string') {
|
|
458
|
+
return [{ role: 'user', content: input }];
|
|
459
|
+
}
|
|
460
|
+
if (input && typeof input === 'object' && !Array.isArray(input)) {
|
|
461
|
+
const message = messageFromResponsesItem(input);
|
|
462
|
+
return message ? [message] : [];
|
|
463
|
+
}
|
|
464
|
+
if (!Array.isArray(input)) {
|
|
465
|
+
return [];
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const messages = [];
|
|
469
|
+
for (const item of input) {
|
|
470
|
+
const message = messageFromResponsesItem(item);
|
|
471
|
+
if (message) messages.push(message);
|
|
472
|
+
}
|
|
473
|
+
if (messages.length > 0) {
|
|
474
|
+
return messages;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const fallbackContent = toChatContent(input);
|
|
478
|
+
if (fallbackContent) {
|
|
479
|
+
return [{ role: 'user', content: fallbackContent }];
|
|
480
|
+
}
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function normalizeResponsesToolsToChatTools(tools) {
|
|
485
|
+
if (!Array.isArray(tools)) return tools;
|
|
486
|
+
return tools
|
|
487
|
+
.map((tool) => {
|
|
488
|
+
if (!tool || typeof tool !== 'object') return null;
|
|
489
|
+
if (tool.type !== 'function') return tool;
|
|
490
|
+
const sourceFn = tool.function && typeof tool.function === 'object' && !Array.isArray(tool.function)
|
|
491
|
+
? tool.function
|
|
492
|
+
: {};
|
|
493
|
+
const name = typeof sourceFn.name === 'string' && sourceFn.name.trim()
|
|
494
|
+
? sourceFn.name.trim()
|
|
495
|
+
: (typeof tool.name === 'string' ? tool.name.trim() : '');
|
|
496
|
+
if (!name) return null;
|
|
497
|
+
const description = typeof sourceFn.description === 'string'
|
|
498
|
+
? sourceFn.description
|
|
499
|
+
: (typeof tool.description === 'string' ? tool.description : undefined);
|
|
500
|
+
const parameters = sourceFn.parameters && typeof sourceFn.parameters === 'object' && !Array.isArray(sourceFn.parameters)
|
|
501
|
+
? sourceFn.parameters
|
|
502
|
+
: (tool.parameters && typeof tool.parameters === 'object' && !Array.isArray(tool.parameters) ? tool.parameters : {});
|
|
503
|
+
const strict = typeof sourceFn.strict === 'boolean'
|
|
504
|
+
? sourceFn.strict
|
|
505
|
+
: (typeof tool.strict === 'boolean' ? tool.strict : undefined);
|
|
506
|
+
const fn = { name, parameters };
|
|
507
|
+
if (description !== undefined) fn.description = description;
|
|
508
|
+
if (strict !== undefined) fn.strict = strict;
|
|
509
|
+
return { type: 'function', function: fn };
|
|
510
|
+
})
|
|
511
|
+
.filter(Boolean);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function normalizeResponsesToolChoiceToChatToolChoice(toolChoice) {
|
|
515
|
+
if (!toolChoice || typeof toolChoice !== 'object' || Array.isArray(toolChoice)) return toolChoice;
|
|
516
|
+
if (toolChoice.type === 'function' && typeof toolChoice.name === 'string') {
|
|
517
|
+
return { type: 'function', function: { name: toolChoice.name } };
|
|
518
|
+
}
|
|
519
|
+
return toolChoice;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function buildChatCompletionsBodyFromResponsesPayload(payload) {
|
|
523
|
+
const source = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
524
|
+
const messages = normalizeResponsesInputToChatMessages(source.input);
|
|
525
|
+
const instructions = typeof source.instructions === 'string' ? source.instructions.trim() : '';
|
|
526
|
+
if (instructions) {
|
|
527
|
+
messages.unshift({ role: 'system', content: instructions });
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const chatBody = {
|
|
531
|
+
model: typeof source.model === 'string' ? source.model : '',
|
|
532
|
+
messages,
|
|
533
|
+
stream: false
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const passthroughKeys = [
|
|
537
|
+
'frequency_penalty',
|
|
538
|
+
'presence_penalty',
|
|
539
|
+
'response_format',
|
|
540
|
+
'stop',
|
|
541
|
+
'temperature',
|
|
542
|
+
'top_p',
|
|
543
|
+
'tools',
|
|
544
|
+
'tool_choice',
|
|
545
|
+
'logprobs',
|
|
546
|
+
'top_logprobs',
|
|
547
|
+
'kbs',
|
|
548
|
+
'is_online',
|
|
549
|
+
'user',
|
|
550
|
+
'seed',
|
|
551
|
+
'n',
|
|
552
|
+
'modalities',
|
|
553
|
+
'audio',
|
|
554
|
+
'reasoning_effort'
|
|
555
|
+
];
|
|
556
|
+
for (const key of passthroughKeys) {
|
|
557
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
558
|
+
if (key === 'tools') {
|
|
559
|
+
chatBody[key] = normalizeResponsesToolsToChatTools(source[key]);
|
|
560
|
+
} else if (key === 'tool_choice') {
|
|
561
|
+
chatBody[key] = normalizeResponsesToolChoiceToChatToolChoice(source[key]);
|
|
562
|
+
} else {
|
|
563
|
+
chatBody[key] = source[key];
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (Object.prototype.hasOwnProperty.call(source, 'max_tokens')) {
|
|
569
|
+
chatBody.max_tokens = source.max_tokens;
|
|
570
|
+
} else if (source.max_output_tokens != null) {
|
|
571
|
+
chatBody.max_tokens = source.max_output_tokens;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return chatBody;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function ensureResponseMetadata(payload) {
|
|
578
|
+
const base = payload && typeof payload === 'object' && !Array.isArray(payload) ? payload : {};
|
|
579
|
+
const id = typeof base.id === 'string' && base.id.trim()
|
|
580
|
+
? base.id.trim()
|
|
581
|
+
: `resp_${crypto.randomBytes(10).toString('hex')}`;
|
|
582
|
+
const model = typeof base.model === 'string' ? base.model : '';
|
|
583
|
+
return {
|
|
584
|
+
object: 'response',
|
|
585
|
+
id,
|
|
586
|
+
model,
|
|
587
|
+
...base
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function writeSse(res, eventName, dataObj) {
|
|
592
|
+
if (eventName) {
|
|
593
|
+
res.write(`event: ${eventName}\n`);
|
|
594
|
+
}
|
|
595
|
+
if (dataObj === '[DONE]') {
|
|
596
|
+
res.write('data: [DONE]\n\n');
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
res.write(`data: ${JSON.stringify(dataObj)}\n\n`);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function sendResponsesSse(res, responsePayload) {
|
|
603
|
+
const response = ensureResponseMetadata(responsePayload);
|
|
604
|
+
const responseId = response.id;
|
|
605
|
+
const model = response.model;
|
|
606
|
+
let sequence = 0;
|
|
607
|
+
const nextSeq = () => {
|
|
608
|
+
sequence += 1;
|
|
609
|
+
return sequence;
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
writeSse(res, 'response.created', {
|
|
613
|
+
type: 'response.created',
|
|
614
|
+
response: {
|
|
615
|
+
id: responseId,
|
|
616
|
+
model,
|
|
617
|
+
created_at: response.created_at
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
const output = Array.isArray(response.output) ? response.output : [];
|
|
622
|
+
for (let outputIndex = 0; outputIndex < output.length; outputIndex += 1) {
|
|
623
|
+
const item = output[outputIndex];
|
|
624
|
+
if (!item || typeof item !== 'object') continue;
|
|
625
|
+
const itemType = typeof item.type === 'string' ? item.type : '';
|
|
626
|
+
const itemId = typeof item.id === 'string' && item.id.trim()
|
|
627
|
+
? item.id.trim()
|
|
628
|
+
: `item_${crypto.randomBytes(8).toString('hex')}`;
|
|
629
|
+
|
|
630
|
+
writeSse(res, 'response.output_item.added', {
|
|
631
|
+
type: 'response.output_item.added',
|
|
632
|
+
output_index: outputIndex,
|
|
633
|
+
item: { ...item, id: itemId }
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
if (itemType === 'message') {
|
|
637
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
638
|
+
for (let contentIndex = 0; contentIndex < content.length; contentIndex += 1) {
|
|
639
|
+
const block = content[contentIndex];
|
|
640
|
+
if (!block || typeof block !== 'object') continue;
|
|
641
|
+
if (block.type !== 'output_text') continue;
|
|
642
|
+
const text = typeof block.text === 'string' ? block.text : '';
|
|
643
|
+
if (text) {
|
|
644
|
+
writeSse(res, 'response.output_text.delta', {
|
|
645
|
+
type: 'response.output_text.delta',
|
|
646
|
+
item_id: itemId,
|
|
647
|
+
output_index: outputIndex,
|
|
648
|
+
content_index: contentIndex,
|
|
649
|
+
delta: text,
|
|
650
|
+
sequence_number: nextSeq()
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
writeSse(res, 'response.output_text.done', {
|
|
654
|
+
type: 'response.output_text.done',
|
|
655
|
+
item_id: itemId,
|
|
656
|
+
output_index: outputIndex,
|
|
657
|
+
content_index: contentIndex,
|
|
658
|
+
text,
|
|
659
|
+
sequence_number: nextSeq()
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
writeSse(res, 'response.output_item.done', {
|
|
665
|
+
type: 'response.output_item.done',
|
|
666
|
+
output_index: outputIndex,
|
|
667
|
+
item: { ...item, id: itemId },
|
|
668
|
+
sequence_number: nextSeq()
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
writeSse(res, 'response.completed', { type: 'response.completed', response });
|
|
673
|
+
writeSse(res, 'done', '[DONE]');
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function appendChatStreamToolCall(target, toolCall) {
|
|
677
|
+
if (!toolCall || typeof toolCall !== 'object') return;
|
|
678
|
+
const index = Number.isFinite(toolCall.index) ? toolCall.index : target.length;
|
|
679
|
+
if (!target[index]) {
|
|
680
|
+
target[index] = {
|
|
681
|
+
id: '',
|
|
682
|
+
type: 'function',
|
|
683
|
+
function: { name: '', arguments: '' }
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
const current = target[index];
|
|
687
|
+
if (typeof toolCall.id === 'string' && toolCall.id) current.id = toolCall.id;
|
|
688
|
+
if (typeof toolCall.type === 'string' && toolCall.type) current.type = toolCall.type;
|
|
689
|
+
const fn = toolCall.function && typeof toolCall.function === 'object' ? toolCall.function : null;
|
|
690
|
+
if (fn) {
|
|
691
|
+
if (typeof fn.name === 'string' && fn.name) current.function.name = fn.name;
|
|
692
|
+
if (typeof fn.arguments === 'string') current.function.arguments += fn.arguments;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function writeChatCompletionChunkAsResponsesSse(state, chunk) {
|
|
697
|
+
if (!chunk || typeof chunk !== 'object') return;
|
|
698
|
+
if (typeof chunk.model === 'string' && chunk.model) {
|
|
699
|
+
state.model = chunk.model;
|
|
700
|
+
}
|
|
701
|
+
const choices = Array.isArray(chunk.choices) ? chunk.choices : [];
|
|
702
|
+
for (const choice of choices) {
|
|
703
|
+
const delta = choice && choice.delta && typeof choice.delta === 'object' ? choice.delta : null;
|
|
704
|
+
if (!delta) continue;
|
|
705
|
+
|
|
706
|
+
if (typeof delta.content === 'string' && delta.content) {
|
|
707
|
+
if (!state.messageItem) {
|
|
708
|
+
state.messageItem = {
|
|
709
|
+
id: `msg_${crypto.randomBytes(8).toString('hex')}`,
|
|
710
|
+
type: 'message',
|
|
711
|
+
role: 'assistant',
|
|
712
|
+
content: [{ type: 'output_text', text: '' }]
|
|
713
|
+
};
|
|
714
|
+
state.output.push(state.messageItem);
|
|
715
|
+
writeSse(state.res, 'response.output_item.added', {
|
|
716
|
+
type: 'response.output_item.added',
|
|
717
|
+
output_index: state.output.length - 1,
|
|
718
|
+
item: state.messageItem
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
state.messageText += delta.content;
|
|
722
|
+
state.messageItem.content[0].text = state.messageText;
|
|
723
|
+
writeSse(state.res, 'response.output_text.delta', {
|
|
724
|
+
type: 'response.output_text.delta',
|
|
725
|
+
item_id: state.messageItem.id,
|
|
726
|
+
output_index: state.output.length - 1,
|
|
727
|
+
content_index: 0,
|
|
728
|
+
delta: delta.content,
|
|
729
|
+
sequence_number: state.nextSeq()
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
734
|
+
for (const toolCall of delta.tool_calls) {
|
|
735
|
+
appendChatStreamToolCall(state.toolCalls, toolCall);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function stopChatStreamHeartbeat(state) {
|
|
742
|
+
if (!state || !state.heartbeatTimer) return;
|
|
743
|
+
clearInterval(state.heartbeatTimer);
|
|
744
|
+
state.heartbeatTimer = null;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function startChatStreamHeartbeat(state) {
|
|
748
|
+
if (!state || state.heartbeatTimer) return;
|
|
749
|
+
const timer = setInterval(() => {
|
|
750
|
+
if (state.finished) {
|
|
751
|
+
stopChatStreamHeartbeat(state);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const target = state.res;
|
|
755
|
+
if (!target || target.writableEnded || target.destroyed) {
|
|
756
|
+
stopChatStreamHeartbeat(state);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
try { target.write(': keepalive\n\n'); } catch (_) {}
|
|
760
|
+
}, 15000);
|
|
761
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
762
|
+
state.heartbeatTimer = timer;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function finishChatStreamResponsesSse(state) {
|
|
766
|
+
if (state.finished) return;
|
|
767
|
+
state.finished = true;
|
|
768
|
+
stopChatStreamHeartbeat(state);
|
|
769
|
+
|
|
770
|
+
if (state.messageItem) {
|
|
771
|
+
const outputIndex = state.output.indexOf(state.messageItem);
|
|
772
|
+
writeSse(state.res, 'response.output_text.done', {
|
|
773
|
+
type: 'response.output_text.done',
|
|
774
|
+
item_id: state.messageItem.id,
|
|
775
|
+
output_index: outputIndex,
|
|
776
|
+
content_index: 0,
|
|
777
|
+
text: state.messageText,
|
|
778
|
+
sequence_number: state.nextSeq()
|
|
779
|
+
});
|
|
780
|
+
writeSse(state.res, 'response.output_item.done', {
|
|
781
|
+
type: 'response.output_item.done',
|
|
782
|
+
output_index: outputIndex,
|
|
783
|
+
item: state.messageItem,
|
|
784
|
+
sequence_number: state.nextSeq()
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
for (const toolCall of state.toolCalls) {
|
|
789
|
+
if (!toolCall) continue;
|
|
790
|
+
const item = {
|
|
791
|
+
type: 'function_call',
|
|
792
|
+
call_id: toolCall.id || `call_${crypto.randomBytes(8).toString('hex')}`,
|
|
793
|
+
name: toolCall.function && typeof toolCall.function.name === 'string' ? toolCall.function.name : '',
|
|
794
|
+
arguments: toolCall.function && typeof toolCall.function.arguments === 'string' ? toolCall.function.arguments : ''
|
|
795
|
+
};
|
|
796
|
+
const outputIndex = state.output.length;
|
|
797
|
+
state.output.push(item);
|
|
798
|
+
writeSse(state.res, 'response.output_item.added', {
|
|
799
|
+
type: 'response.output_item.added',
|
|
800
|
+
output_index: outputIndex,
|
|
801
|
+
item
|
|
802
|
+
});
|
|
803
|
+
writeSse(state.res, 'response.output_item.done', {
|
|
804
|
+
type: 'response.output_item.done',
|
|
805
|
+
output_index: outputIndex,
|
|
806
|
+
item,
|
|
807
|
+
sequence_number: state.nextSeq()
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const response = ensureResponseMetadata({
|
|
812
|
+
id: state.responseId,
|
|
813
|
+
model: state.model,
|
|
814
|
+
created_at: state.createdAt,
|
|
815
|
+
status: 'completed',
|
|
816
|
+
output: state.output
|
|
817
|
+
});
|
|
818
|
+
writeSse(state.res, 'response.completed', { type: 'response.completed', response });
|
|
819
|
+
writeSse(state.res, 'done', '[DONE]');
|
|
820
|
+
state.res.end();
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
function failResponsesSseRaw(res, message) {
|
|
824
|
+
if (!res || res.writableEnded || res.destroyed) return;
|
|
825
|
+
try {
|
|
826
|
+
writeSse(res, 'response.failed', { type: 'response.failed', error: message || 'upstream stream failed' });
|
|
827
|
+
writeSse(res, 'done', '[DONE]');
|
|
828
|
+
res.end();
|
|
829
|
+
} catch (_) {}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function failChatStreamResponsesSse(state, message) {
|
|
833
|
+
if (!state || state.finished) return;
|
|
834
|
+
state.finished = true;
|
|
835
|
+
stopChatStreamHeartbeat(state);
|
|
836
|
+
failResponsesSseRaw(state.res, message);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function streamChatCompletionsAsResponsesSse(targetUrl, options = {}) {
|
|
840
|
+
const parsed = new URL(targetUrl);
|
|
841
|
+
const transport = parsed.protocol === 'https:' ? https : http;
|
|
842
|
+
const bodyText = options.body ? JSON.stringify(options.body) : '';
|
|
843
|
+
const headers = {
|
|
844
|
+
'Accept': 'text/event-stream',
|
|
845
|
+
...(options.body ? { 'Content-Type': 'application/json' } : {}),
|
|
846
|
+
...(options.headers || {})
|
|
847
|
+
};
|
|
848
|
+
if (options.body) {
|
|
849
|
+
headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
|
|
850
|
+
}
|
|
851
|
+
const timeoutMs = Number.isFinite(options.timeoutMs)
|
|
852
|
+
? Math.max(1000, Number(options.timeoutMs))
|
|
853
|
+
: 30000;
|
|
854
|
+
const res = options.res;
|
|
855
|
+
const model = typeof options.model === 'string' ? options.model : '';
|
|
856
|
+
|
|
857
|
+
return new Promise((resolve) => {
|
|
858
|
+
let settled = false;
|
|
859
|
+
const finish = (value) => {
|
|
860
|
+
if (settled) return;
|
|
861
|
+
settled = true;
|
|
862
|
+
resolve(value);
|
|
863
|
+
};
|
|
864
|
+
const req = transport.request({
|
|
865
|
+
protocol: parsed.protocol,
|
|
866
|
+
hostname: parsed.hostname,
|
|
867
|
+
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
868
|
+
method: options.method || 'POST',
|
|
869
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
870
|
+
headers,
|
|
871
|
+
agent: parsed.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
872
|
+
}, (upstreamRes) => {
|
|
873
|
+
const status = upstreamRes.statusCode || 0;
|
|
874
|
+
const chunks = [];
|
|
875
|
+
const contentType = String(upstreamRes.headers && upstreamRes.headers['content-type'] || '');
|
|
876
|
+
let streamState = null;
|
|
877
|
+
|
|
878
|
+
const handleAbort = (reason) => {
|
|
879
|
+
if (settled) return;
|
|
880
|
+
if (streamState) {
|
|
881
|
+
failChatStreamResponsesSse(streamState, reason);
|
|
882
|
+
finish({ ok: true });
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (res.headersSent) {
|
|
886
|
+
failResponsesSseRaw(res, reason);
|
|
887
|
+
finish({ ok: true });
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
finish({
|
|
891
|
+
ok: false,
|
|
892
|
+
status,
|
|
893
|
+
error: reason,
|
|
894
|
+
bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : ''
|
|
895
|
+
});
|
|
896
|
+
};
|
|
897
|
+
upstreamRes.on('error', (err) => handleAbort(err && err.message ? err.message : 'upstream stream failed'));
|
|
898
|
+
upstreamRes.on('aborted', () => handleAbort('upstream stream aborted'));
|
|
899
|
+
|
|
900
|
+
if (status === 404 || status === 405) {
|
|
901
|
+
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
902
|
+
upstreamRes.on('end', () => finish({ retry: true, status, bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : '' }));
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
if (status >= 400) {
|
|
907
|
+
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
908
|
+
upstreamRes.on('end', () => finish({ ok: false, status, bodyText: chunks.length ? Buffer.concat(chunks).toString('utf-8') : '' }));
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
res.writeHead(200, {
|
|
913
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
914
|
+
'Cache-Control': 'no-cache',
|
|
915
|
+
'Connection': 'keep-alive',
|
|
916
|
+
'X-Accel-Buffering': 'no'
|
|
917
|
+
});
|
|
918
|
+
|
|
919
|
+
if (!/text\/event-stream/i.test(contentType)) {
|
|
920
|
+
upstreamRes.on('data', (chunk) => chunk && chunks.push(chunk));
|
|
921
|
+
upstreamRes.on('end', () => {
|
|
922
|
+
const text = chunks.length ? Buffer.concat(chunks).toString('utf-8') : '';
|
|
923
|
+
const parsedJson = parseJsonOrError(text);
|
|
924
|
+
if (parsedJson.error) {
|
|
925
|
+
writeSse(res, 'response.failed', { type: 'response.failed', error: `invalid upstream response: ${parsedJson.error}` });
|
|
926
|
+
writeSse(res, 'done', '[DONE]');
|
|
927
|
+
res.end();
|
|
928
|
+
finish({ ok: true });
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
sendResponsesSse(res, buildResponsesPayloadFromChatCompletion(parsedJson.value, model));
|
|
932
|
+
res.end();
|
|
933
|
+
finish({ ok: true });
|
|
934
|
+
});
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
let sequence = 0;
|
|
939
|
+
const state = {
|
|
940
|
+
res,
|
|
941
|
+
responseId: `resp_${crypto.randomBytes(10).toString('hex')}`,
|
|
942
|
+
model,
|
|
943
|
+
createdAt: Math.floor(Date.now() / 1000),
|
|
944
|
+
output: [],
|
|
945
|
+
messageItem: null,
|
|
946
|
+
messageText: '',
|
|
947
|
+
toolCalls: [],
|
|
948
|
+
finished: false,
|
|
949
|
+
nextSeq: () => {
|
|
950
|
+
sequence += 1;
|
|
951
|
+
return sequence;
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
streamState = state;
|
|
955
|
+
startChatStreamHeartbeat(state);
|
|
956
|
+
if (typeof res.on === 'function') {
|
|
957
|
+
res.on('close', () => stopChatStreamHeartbeat(state));
|
|
958
|
+
}
|
|
959
|
+
writeSse(res, 'response.created', {
|
|
960
|
+
type: 'response.created',
|
|
961
|
+
response: {
|
|
962
|
+
id: state.responseId,
|
|
963
|
+
model: state.model,
|
|
964
|
+
created_at: state.createdAt
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
let buffer = '';
|
|
969
|
+
const handleEventBlock = (block) => {
|
|
970
|
+
const dataLines = String(block || '')
|
|
971
|
+
.split(/\r?\n/)
|
|
972
|
+
.filter((line) => line.startsWith('data:'))
|
|
973
|
+
.map((line) => line.slice(5).trimStart());
|
|
974
|
+
if (dataLines.length === 0) return;
|
|
975
|
+
const data = dataLines.join('\n').trim();
|
|
976
|
+
if (!data) return;
|
|
977
|
+
if (data === '[DONE]') {
|
|
978
|
+
finishChatStreamResponsesSse(state);
|
|
979
|
+
finish({ ok: true });
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
const parsedChunk = parseJsonOrError(data);
|
|
983
|
+
if (!parsedChunk.error) {
|
|
984
|
+
writeChatCompletionChunkAsResponsesSse(state, parsedChunk.value);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
upstreamRes.on('data', (chunk) => {
|
|
989
|
+
buffer += chunk.toString('utf-8');
|
|
990
|
+
let boundary = buffer.search(/\r?\n\r?\n/);
|
|
991
|
+
while (boundary >= 0) {
|
|
992
|
+
const block = buffer.slice(0, boundary);
|
|
993
|
+
const match = buffer.slice(boundary).match(/^\r?\n\r?\n/);
|
|
994
|
+
buffer = buffer.slice(boundary + (match ? match[0].length : 2));
|
|
995
|
+
handleEventBlock(block);
|
|
996
|
+
boundary = buffer.search(/\r?\n\r?\n/);
|
|
997
|
+
}
|
|
998
|
+
});
|
|
999
|
+
upstreamRes.on('end', () => {
|
|
1000
|
+
if (buffer.trim()) handleEventBlock(buffer);
|
|
1001
|
+
finishChatStreamResponsesSse(state);
|
|
1002
|
+
finish({ ok: true });
|
|
1003
|
+
});
|
|
1004
|
+
});
|
|
1005
|
+
req.setTimeout(timeoutMs, () => {
|
|
1006
|
+
try { req.destroy(new Error('timeout')); } catch (_) {}
|
|
1007
|
+
finish({ ok: false, error: 'timeout' });
|
|
1008
|
+
});
|
|
1009
|
+
req.on('error', (err) => finish({ ok: false, error: err && err.message ? err.message : 'request failed' }));
|
|
1010
|
+
if (bodyText) req.write(bodyText);
|
|
1011
|
+
req.end();
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
async function streamChatCompletionsAsResponsesSseWithFallbackUrls(baseUrl, pathSuffix, options = {}) {
|
|
1016
|
+
const urls = buildUpstreamUrlCandidates(baseUrl, pathSuffix);
|
|
1017
|
+
if (urls.length === 0) {
|
|
1018
|
+
return { ok: false, error: 'failed to build upstream URL' };
|
|
1019
|
+
}
|
|
1020
|
+
let lastResult = null;
|
|
1021
|
+
for (const url of urls) {
|
|
1022
|
+
const result = await retryTransientRequest(() => streamChatCompletionsAsResponsesSse(url, options));
|
|
1023
|
+
lastResult = result;
|
|
1024
|
+
if (result && result.retry) continue;
|
|
1025
|
+
return result;
|
|
1026
|
+
}
|
|
1027
|
+
return lastResult || { ok: false, error: 'failed to build upstream URL' };
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function canListenPort(host, port) {
|
|
1031
|
+
return new Promise((resolve) => {
|
|
1032
|
+
const tester = net.createServer();
|
|
1033
|
+
tester.unref();
|
|
1034
|
+
tester.once('error', () => resolve(false));
|
|
1035
|
+
tester.once('listening', () => {
|
|
1036
|
+
tester.close(() => resolve(true));
|
|
1037
|
+
});
|
|
1038
|
+
tester.listen(port, host);
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
async function findAvailablePort(host, startPort, maxAttempts = 20) {
|
|
1043
|
+
const start = parseInt(String(startPort), 10);
|
|
1044
|
+
if (!Number.isFinite(start) || start <= 0) {
|
|
1045
|
+
return 0;
|
|
1046
|
+
}
|
|
1047
|
+
const attempts = Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 20;
|
|
1048
|
+
for (let offset = 0; offset < attempts; offset += 1) {
|
|
1049
|
+
const candidate = start + offset;
|
|
1050
|
+
if (candidate > 65535) {
|
|
1051
|
+
break;
|
|
1052
|
+
}
|
|
1053
|
+
// eslint-disable-next-line no-await-in-loop
|
|
1054
|
+
const ok = await canListenPort(host, candidate);
|
|
1055
|
+
if (ok) {
|
|
1056
|
+
return candidate;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
return 0;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
function resolveBuiltinProxyProviderName(rawProviderName, providers = {}, preferredProvider = '') {
|
|
1063
|
+
const providerMap = providers && isPlainObject(providers) ? providers : {};
|
|
1064
|
+
const providerNames = Object.keys(providerMap)
|
|
1065
|
+
.filter((name) => name && !isBuiltinManagedProvider(name));
|
|
1066
|
+
const requested = typeof rawProviderName === 'string' ? rawProviderName.trim() : '';
|
|
1067
|
+
if (requested && !isBuiltinManagedProvider(requested) && providerMap[requested]) {
|
|
1068
|
+
return requested;
|
|
1069
|
+
}
|
|
1070
|
+
const preferred = typeof preferredProvider === 'string' ? preferredProvider.trim() : '';
|
|
1071
|
+
if (preferred && !isBuiltinManagedProvider(preferred) && providerMap[preferred]) {
|
|
1072
|
+
return preferred;
|
|
1073
|
+
}
|
|
1074
|
+
return providerNames[0] || '';
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function normalizeBuiltinProxySettings(raw) {
|
|
1078
|
+
const merged = {
|
|
1079
|
+
...DEFAULT_BUILTIN_PROXY_SETTINGS,
|
|
1080
|
+
...(isPlainObject(raw) ? raw : {})
|
|
1081
|
+
};
|
|
1082
|
+
const host = typeof merged.host === 'string' ? merged.host.trim() : '';
|
|
1083
|
+
const port = parseInt(String(merged.port), 10);
|
|
1084
|
+
const provider = typeof merged.provider === 'string' ? merged.provider.trim() : '';
|
|
1085
|
+
const authSourceRaw = typeof merged.authSource === 'string' ? merged.authSource.trim().toLowerCase() : '';
|
|
1086
|
+
const timeoutMs = parseInt(String(merged.timeoutMs), 10);
|
|
1087
|
+
const authSource = authSourceRaw === 'profile' || authSourceRaw === 'none' ? authSourceRaw : 'provider';
|
|
1088
|
+
|
|
1089
|
+
return {
|
|
1090
|
+
enabled: merged.enabled !== false,
|
|
1091
|
+
host: host || DEFAULT_BUILTIN_PROXY_SETTINGS.host,
|
|
1092
|
+
port: Number.isFinite(port) && port > 0 && port <= 65535 ? port : DEFAULT_BUILTIN_PROXY_SETTINGS.port,
|
|
1093
|
+
provider,
|
|
1094
|
+
authSource,
|
|
1095
|
+
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs >= 1000
|
|
1096
|
+
? timeoutMs
|
|
1097
|
+
: DEFAULT_BUILTIN_PROXY_SETTINGS.timeoutMs
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
function readBuiltinProxySettings() {
|
|
1102
|
+
const parsed = readJsonFile(BUILTIN_PROXY_SETTINGS_FILE, null);
|
|
1103
|
+
return normalizeBuiltinProxySettings(parsed);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
function saveBuiltinProxySettings(payload = {}, options = {}) {
|
|
1107
|
+
const current = readBuiltinProxySettings();
|
|
1108
|
+
const merged = normalizeBuiltinProxySettings({
|
|
1109
|
+
...current,
|
|
1110
|
+
...(isPlainObject(payload) ? payload : {})
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
if (!merged.host) {
|
|
1114
|
+
return { error: '代理 host 不能为空' };
|
|
1115
|
+
}
|
|
1116
|
+
if (!Number.isFinite(merged.port) || merged.port <= 0 || merged.port > 65535) {
|
|
1117
|
+
return { error: '代理端口无效(1-65535)' };
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
const { config } = readConfigOrVirtualDefault();
|
|
1121
|
+
const providers = config && isPlainObject(config.model_providers) ? config.model_providers : {};
|
|
1122
|
+
const preferredProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
|
|
1123
|
+
const finalProvider = resolveBuiltinProxyProviderName(merged.provider, providers, preferredProvider);
|
|
1124
|
+
|
|
1125
|
+
const normalized = {
|
|
1126
|
+
...merged,
|
|
1127
|
+
provider: finalProvider
|
|
1128
|
+
};
|
|
1129
|
+
|
|
1130
|
+
if (!options.skipWrite) {
|
|
1131
|
+
writeJsonAtomic(BUILTIN_PROXY_SETTINGS_FILE, normalized);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
return {
|
|
1135
|
+
success: true,
|
|
1136
|
+
settings: normalized
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
function buildProxyListenUrl(settings) {
|
|
1141
|
+
const host = formatHostForUrl(settings.host || DEFAULT_BUILTIN_PROXY_SETTINGS.host);
|
|
1142
|
+
return `http://${host}:${settings.port}`;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
function buildBuiltinProxyProviderBaseUrl(settings) {
|
|
1146
|
+
return `${buildProxyListenUrl(settings).replace(/\/+$/, '')}/v1`;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
function removePersistedBuiltinProxyProviderFromConfig() {
|
|
1150
|
+
if (!fs.existsSync(CONFIG_FILE)) {
|
|
1151
|
+
return { success: true, removed: false };
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
let config;
|
|
1155
|
+
try {
|
|
1156
|
+
config = readConfig();
|
|
1157
|
+
} catch (e) {
|
|
1158
|
+
return { error: e.message || '读取 config.toml 失败' };
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
if (!config.model_providers || !config.model_providers[BUILTIN_PROXY_PROVIDER_NAME]) {
|
|
1162
|
+
return { success: true, removed: false };
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
|
1166
|
+
const lineEnding = content.includes('\r\n') ? '\r\n' : '\n';
|
|
1167
|
+
const hasBom = content.charCodeAt(0) === 0xFEFF;
|
|
1168
|
+
const providerConfig = config.model_providers[BUILTIN_PROXY_PROVIDER_NAME];
|
|
1169
|
+
const providerSegments = providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segments)
|
|
1170
|
+
? providerConfig.__codexmate_legacy_segments
|
|
1171
|
+
: null;
|
|
1172
|
+
const providerSegmentVariants = (() => {
|
|
1173
|
+
const variants = [];
|
|
1174
|
+
const seen = new Set();
|
|
1175
|
+
const pushVariant = (segments) => {
|
|
1176
|
+
const normalized = normalizeLegacySegments(segments);
|
|
1177
|
+
const key = buildLegacySegmentsKey(normalized);
|
|
1178
|
+
if (!key || seen.has(key)) return;
|
|
1179
|
+
seen.add(key);
|
|
1180
|
+
variants.push(normalized);
|
|
1181
|
+
};
|
|
1182
|
+
if (providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segments)) {
|
|
1183
|
+
pushVariant(providerConfig.__codexmate_legacy_segments);
|
|
1184
|
+
}
|
|
1185
|
+
if (providerConfig && Array.isArray(providerConfig.__codexmate_legacy_segment_variants)) {
|
|
1186
|
+
for (const segments of providerConfig.__codexmate_legacy_segment_variants) {
|
|
1187
|
+
pushVariant(segments);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
if (providerSegments) {
|
|
1191
|
+
pushVariant(providerSegments);
|
|
1192
|
+
}
|
|
1193
|
+
if (variants.length === 0) {
|
|
1194
|
+
pushVariant(String(BUILTIN_PROXY_PROVIDER_NAME || '').split('.').filter((item) => item));
|
|
1195
|
+
}
|
|
1196
|
+
return variants;
|
|
1197
|
+
})();
|
|
1198
|
+
|
|
1199
|
+
let updatedContent = null;
|
|
1200
|
+
const combinedRanges = [];
|
|
1201
|
+
for (const segments of providerSegmentVariants) {
|
|
1202
|
+
combinedRanges.push(...findProviderSectionRanges(content, BUILTIN_PROXY_PROVIDER_NAME, segments));
|
|
1203
|
+
combinedRanges.push(...findProviderDescendantSectionRanges(content, segments));
|
|
1204
|
+
}
|
|
1205
|
+
if (combinedRanges.length === 0) {
|
|
1206
|
+
combinedRanges.push(...findProviderSectionRanges(content, BUILTIN_PROXY_PROVIDER_NAME, providerSegments));
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
if (combinedRanges.length > 0) {
|
|
1210
|
+
const sorted = combinedRanges.sort((a, b) => b.start - a.start || b.end - a.end);
|
|
1211
|
+
const seen = new Set();
|
|
1212
|
+
let removedContent = content;
|
|
1213
|
+
for (const range of sorted) {
|
|
1214
|
+
const rangeKey = `${range.start}:${range.end}`;
|
|
1215
|
+
if (seen.has(rangeKey)) continue;
|
|
1216
|
+
seen.add(rangeKey);
|
|
1217
|
+
removedContent = removedContent.slice(0, range.start) + removedContent.slice(range.end);
|
|
1218
|
+
}
|
|
1219
|
+
updatedContent = removedContent.replace(/\n{3,}/g, lineEnding + lineEnding);
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
if (!updatedContent) {
|
|
1223
|
+
const rebuilt = JSON.parse(JSON.stringify(config));
|
|
1224
|
+
delete rebuilt.model_providers[BUILTIN_PROXY_PROVIDER_NAME];
|
|
1225
|
+
const hasMarker = content.includes(CODEXMATE_MANAGED_MARKER);
|
|
1226
|
+
let rebuiltToml = toml.stringify(rebuilt).trimEnd();
|
|
1227
|
+
rebuiltToml = rebuiltToml.replace(/\n/g, lineEnding);
|
|
1228
|
+
if (hasMarker && !rebuiltToml.includes(CODEXMATE_MANAGED_MARKER)) {
|
|
1229
|
+
rebuiltToml = `${CODEXMATE_MANAGED_MARKER}${lineEnding}${rebuiltToml}`;
|
|
1230
|
+
}
|
|
1231
|
+
updatedContent = rebuiltToml + lineEnding;
|
|
1232
|
+
if (hasBom && updatedContent.charCodeAt(0) !== 0xFEFF) {
|
|
1233
|
+
updatedContent = '\uFEFF' + updatedContent;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
try {
|
|
1238
|
+
writeConfig(updatedContent.trimEnd() + lineEnding);
|
|
1239
|
+
} catch (e) {
|
|
1240
|
+
return { error: e.message || '写入 config.toml 失败' };
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
return { success: true, removed: true };
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
function hasCodexConfigReadyForProxy() {
|
|
1247
|
+
const result = readConfigOrVirtualDefault();
|
|
1248
|
+
if (!result || result.isVirtual) {
|
|
1249
|
+
return false;
|
|
1250
|
+
}
|
|
1251
|
+
const config = result.config || {};
|
|
1252
|
+
if (!isPlainObject(config.model_providers)) {
|
|
1253
|
+
return false;
|
|
1254
|
+
}
|
|
1255
|
+
const providerNames = Object.keys(config.model_providers)
|
|
1256
|
+
.filter((name) => name && !isBuiltinManagedProvider(name));
|
|
1257
|
+
return providerNames.length > 0;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
function resolveBuiltinProxyUpstream(settings) {
|
|
1261
|
+
const { config } = readConfigOrVirtualDefault();
|
|
1262
|
+
const providers = config && isPlainObject(config.model_providers) ? config.model_providers : {};
|
|
1263
|
+
const currentProvider = typeof config.model_provider === 'string' ? config.model_provider.trim() : '';
|
|
1264
|
+
const providerName = resolveBuiltinProxyProviderName(settings.provider, providers, currentProvider);
|
|
1265
|
+
if (!providerName) {
|
|
1266
|
+
return { error: '未找到可用的上游 provider,请先添加 provider' };
|
|
1267
|
+
}
|
|
1268
|
+
if (providerName === BUILTIN_PROXY_PROVIDER_NAME) {
|
|
1269
|
+
return { error: `上游 provider 不能是 ${BUILTIN_PROXY_PROVIDER_NAME}` };
|
|
1270
|
+
}
|
|
1271
|
+
const provider = providers[providerName];
|
|
1272
|
+
if (!provider || !isPlainObject(provider)) {
|
|
1273
|
+
return { error: `上游 provider 不存在: ${providerName}` };
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
const baseUrl = typeof provider.base_url === 'string' ? provider.base_url.trim() : '';
|
|
1277
|
+
if (!baseUrl || !isValidHttpUrl(baseUrl)) {
|
|
1278
|
+
return { error: `上游 provider base_url 无效: ${providerName}` };
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
let token = '';
|
|
1282
|
+
if (settings.authSource === 'profile') {
|
|
1283
|
+
token = resolveAuthTokenFromCurrentProfile();
|
|
1284
|
+
} else if (settings.authSource === 'provider') {
|
|
1285
|
+
token = typeof provider.preferred_auth_method === 'string' ? provider.preferred_auth_method.trim() : '';
|
|
1286
|
+
if (!token) {
|
|
1287
|
+
token = resolveAuthTokenFromCurrentProfile();
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
let authHeader = '';
|
|
1292
|
+
if (token) {
|
|
1293
|
+
authHeader = /^bearer\s+/i.test(token) ? token : `Bearer ${token}`;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
return {
|
|
1297
|
+
providerName,
|
|
1298
|
+
baseUrl: normalizeBaseUrl(baseUrl),
|
|
1299
|
+
authHeader
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
function createBuiltinProxyServer(settings, upstream) {
|
|
1304
|
+
const connections = new Set();
|
|
1305
|
+
const timeoutMs = settings.timeoutMs;
|
|
1306
|
+
|
|
1307
|
+
const server = http.createServer((req, res) => {
|
|
1308
|
+
let parsedIncoming;
|
|
1309
|
+
try {
|
|
1310
|
+
parsedIncoming = new URL(req.url || '/', 'http://localhost');
|
|
1311
|
+
} catch (e) {
|
|
1312
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1313
|
+
res.end(JSON.stringify({ error: 'invalid request path' }));
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
const remoteAddr = req && req.socket ? req.socket.remoteAddress : '';
|
|
1318
|
+
const isLoopback = !remoteAddr
|
|
1319
|
+
|| remoteAddr === '127.0.0.1'
|
|
1320
|
+
|| remoteAddr === '::1'
|
|
1321
|
+
|| remoteAddr === '::ffff:127.0.0.1';
|
|
1322
|
+
if (!isLoopback) {
|
|
1323
|
+
const expected = typeof process.env.CODEXMATE_HTTP_TOKEN === 'string'
|
|
1324
|
+
? process.env.CODEXMATE_HTTP_TOKEN.trim()
|
|
1325
|
+
: '';
|
|
1326
|
+
if (!expected) {
|
|
1327
|
+
const body = JSON.stringify({ error: 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' });
|
|
1328
|
+
res.writeHead(403, {
|
|
1329
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1330
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1331
|
+
});
|
|
1332
|
+
res.end(body, 'utf-8');
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
const headers = req && req.headers && typeof req.headers === 'object' ? req.headers : {};
|
|
1336
|
+
const rawAuth = typeof headers.authorization === 'string' ? headers.authorization.trim() : '';
|
|
1337
|
+
const match = rawAuth ? rawAuth.match(/^bearer\s+(.+)$/i) : null;
|
|
1338
|
+
const actual = match && match[1]
|
|
1339
|
+
? match[1].trim()
|
|
1340
|
+
: (rawAuth ? rawAuth : (typeof headers['x-codexmate-token'] === 'string' ? String(headers['x-codexmate-token']).trim() : ''));
|
|
1341
|
+
if (!actual || actual !== expected) {
|
|
1342
|
+
const body = JSON.stringify({ error: 'Unauthorized' });
|
|
1343
|
+
res.writeHead(401, {
|
|
1344
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1345
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1346
|
+
});
|
|
1347
|
+
res.end(body, 'utf-8');
|
|
1348
|
+
return;
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
const incomingPath = parsedIncoming.pathname || '/';
|
|
1353
|
+
if (incomingPath === '/health' || incomingPath === '/status') {
|
|
1354
|
+
const body = JSON.stringify({
|
|
1355
|
+
ok: true,
|
|
1356
|
+
upstreamProvider: upstream.providerName,
|
|
1357
|
+
upstreamBaseUrl: upstream.baseUrl
|
|
1358
|
+
});
|
|
1359
|
+
res.writeHead(200, {
|
|
1360
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1361
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1362
|
+
});
|
|
1363
|
+
res.end(body, 'utf-8');
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
if (!(incomingPath === '/v1' || incomingPath.startsWith('/v1/'))) {
|
|
1368
|
+
const body = JSON.stringify({ error: 'proxy only supports /v1/* paths' });
|
|
1369
|
+
res.writeHead(404, {
|
|
1370
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1371
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1372
|
+
});
|
|
1373
|
+
res.end(body, 'utf-8');
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
// Responses shim:
|
|
1378
|
+
// - Codex CLI 默认走 /v1/responses(含 SSE)
|
|
1379
|
+
// - 某些上游只支持 /v1/chat/completions
|
|
1380
|
+
// 因此这里优先尝试 /v1/responses(stream=false),失败再转换到 chat/completions 并回包为 responses。
|
|
1381
|
+
if ((incomingPath === '/v1/responses' || incomingPath === '/v1/responses/') && (req.method || 'GET').toUpperCase() === 'POST') {
|
|
1382
|
+
void (async () => {
|
|
1383
|
+
const { body, error } = await readRequestBody(req, 10 * 1024 * 1024);
|
|
1384
|
+
if (error) {
|
|
1385
|
+
res.writeHead(413, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1386
|
+
res.end(JSON.stringify({ error }));
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
const parsed = parseJsonOrError(body);
|
|
1390
|
+
if (parsed.error) {
|
|
1391
|
+
res.writeHead(400, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1392
|
+
res.end(JSON.stringify({ error: `invalid json: ${parsed.error}` }));
|
|
1393
|
+
return;
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
const payload = parsed.value && typeof parsed.value === 'object' ? parsed.value : {};
|
|
1397
|
+
const wantsStream = payload.stream === true;
|
|
1398
|
+
|
|
1399
|
+
const commonHeaders = {
|
|
1400
|
+
...(upstream.authHeader ? { 'Authorization': upstream.authHeader } : {}),
|
|
1401
|
+
'X-Codexmate-Proxy': '1'
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
const upstreamResponses = await proxyRequestJsonWithFallbackUrls(upstream.baseUrl, 'responses', {
|
|
1405
|
+
method: 'POST',
|
|
1406
|
+
headers: commonHeaders,
|
|
1407
|
+
timeoutMs,
|
|
1408
|
+
body: { ...payload, stream: false }
|
|
1409
|
+
});
|
|
1410
|
+
|
|
1411
|
+
// 优先走上游 /responses(如果支持)。若上游报错且不是“端点不支持”,则直接透传错误。
|
|
1412
|
+
if (upstreamResponses.ok && upstreamResponses.status >= 200 && upstreamResponses.status < 300) {
|
|
1413
|
+
const json = parseJsonOrError(upstreamResponses.bodyText);
|
|
1414
|
+
if (json.error) {
|
|
1415
|
+
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1416
|
+
res.end(JSON.stringify({ error: `Upstream JSON parse failed: ${json.error}` }));
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
const responsesPayload = ensureResponseMetadata(json.value);
|
|
1420
|
+
if (wantsStream) {
|
|
1421
|
+
res.writeHead(200, {
|
|
1422
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
1423
|
+
'Cache-Control': 'no-cache',
|
|
1424
|
+
'Connection': 'keep-alive',
|
|
1425
|
+
'X-Accel-Buffering': 'no'
|
|
1426
|
+
});
|
|
1427
|
+
sendResponsesSse(res, responsesPayload);
|
|
1428
|
+
res.end();
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1432
|
+
res.end(JSON.stringify(responsesPayload));
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
if (upstreamResponses.ok && upstreamResponses.status >= 400) {
|
|
1437
|
+
if (!shouldFallbackFromUpstreamResponses(upstreamResponses.status, upstreamResponses.bodyText)) {
|
|
1438
|
+
res.writeHead(upstreamResponses.status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1439
|
+
res.end(upstreamResponses.bodyText || JSON.stringify({ error: 'Upstream error' }));
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
// fallthrough to chat/completions conversion
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
if (!upstreamResponses.ok) {
|
|
1446
|
+
if (!shouldFallbackFromUpstreamResponsesFailure(upstreamResponses.error)) {
|
|
1447
|
+
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1448
|
+
res.end(JSON.stringify({ error: upstreamResponses.error || 'Upstream request failed' }));
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
// Some OpenAI-compatible gateways accept /responses but never complete it.
|
|
1452
|
+
// Treat that as an unsupported Responses endpoint and try the chat fallback.
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
const model = typeof payload.model === 'string' ? payload.model : '';
|
|
1456
|
+
const chatBody = buildChatCompletionsBodyFromResponsesPayload(payload);
|
|
1457
|
+
|
|
1458
|
+
if (wantsStream) {
|
|
1459
|
+
const streamingChatBody = { ...chatBody, stream: true };
|
|
1460
|
+
const streamed = await streamChatCompletionsAsResponsesSseWithFallbackUrls(upstream.baseUrl, 'chat/completions', {
|
|
1461
|
+
method: 'POST',
|
|
1462
|
+
headers: commonHeaders,
|
|
1463
|
+
timeoutMs,
|
|
1464
|
+
body: streamingChatBody,
|
|
1465
|
+
res,
|
|
1466
|
+
model
|
|
1467
|
+
});
|
|
1468
|
+
if (!streamed.ok) {
|
|
1469
|
+
if (!res.headersSent) {
|
|
1470
|
+
res.writeHead(streamed.status && streamed.status >= 400 ? streamed.status : 502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1471
|
+
res.end(streamed.bodyText || JSON.stringify({ error: streamed.error || 'proxy request failed' }));
|
|
1472
|
+
} else if (!res.writableEnded) {
|
|
1473
|
+
writeSse(res, 'response.failed', { type: 'response.failed', error: streamed.error || streamed.bodyText || 'proxy request failed' });
|
|
1474
|
+
writeSse(res, 'done', '[DONE]');
|
|
1475
|
+
res.end();
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
const upstreamChat = await proxyRequestJsonWithFallbackUrls(upstream.baseUrl, 'chat/completions', {
|
|
1482
|
+
method: 'POST',
|
|
1483
|
+
headers: commonHeaders,
|
|
1484
|
+
timeoutMs,
|
|
1485
|
+
body: chatBody
|
|
1486
|
+
});
|
|
1487
|
+
if (!upstreamChat.ok) {
|
|
1488
|
+
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1489
|
+
res.end(JSON.stringify({ error: upstreamChat.error || 'proxy request failed' }));
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
if (upstreamChat.status >= 400) {
|
|
1494
|
+
res.writeHead(upstreamChat.status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1495
|
+
res.end(upstreamChat.bodyText || JSON.stringify({ error: 'Upstream error' }));
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
const chatJson = parseJsonOrError(upstreamChat.bodyText);
|
|
1500
|
+
if (chatJson.error) {
|
|
1501
|
+
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1502
|
+
res.end(JSON.stringify({ error: `invalid upstream response: ${chatJson.error}` }));
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
const responsesPayload = buildResponsesPayloadFromChatCompletion(chatJson.value, model);
|
|
1507
|
+
|
|
1508
|
+
if (wantsStream) {
|
|
1509
|
+
res.writeHead(200, {
|
|
1510
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
1511
|
+
'Cache-Control': 'no-cache',
|
|
1512
|
+
'Connection': 'keep-alive',
|
|
1513
|
+
'X-Accel-Buffering': 'no'
|
|
1514
|
+
});
|
|
1515
|
+
sendResponsesSse(res, responsesPayload);
|
|
1516
|
+
res.end();
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
1520
|
+
res.end(JSON.stringify(responsesPayload));
|
|
1521
|
+
})();
|
|
1522
|
+
return;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
const suffix = incomingPath === '/v1'
|
|
1526
|
+
? ''
|
|
1527
|
+
: incomingPath.replace(/^\/v1\/?/, '');
|
|
1528
|
+
const targetBase = joinApiUrl(upstream.baseUrl, suffix);
|
|
1529
|
+
if (!targetBase) {
|
|
1530
|
+
const body = JSON.stringify({ error: 'failed to build upstream URL' });
|
|
1531
|
+
res.writeHead(500, {
|
|
1532
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1533
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1534
|
+
});
|
|
1535
|
+
res.end(body, 'utf-8');
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
let targetUrl;
|
|
1540
|
+
try {
|
|
1541
|
+
targetUrl = new URL(targetBase);
|
|
1542
|
+
targetUrl.search = parsedIncoming.search || '';
|
|
1543
|
+
} catch (e) {
|
|
1544
|
+
const body = JSON.stringify({ error: `invalid upstream URL: ${e.message}` });
|
|
1545
|
+
res.writeHead(500, {
|
|
1546
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1547
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1548
|
+
});
|
|
1549
|
+
res.end(body, 'utf-8');
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
const requestHeaders = { ...req.headers };
|
|
1554
|
+
delete requestHeaders.host;
|
|
1555
|
+
delete requestHeaders.connection;
|
|
1556
|
+
delete requestHeaders['content-length'];
|
|
1557
|
+
if (upstream.authHeader) {
|
|
1558
|
+
requestHeaders.authorization = upstream.authHeader;
|
|
1559
|
+
}
|
|
1560
|
+
requestHeaders['x-codexmate-proxy'] = '1';
|
|
1561
|
+
if (!requestHeaders['x-forwarded-for'] && req.socket && req.socket.remoteAddress) {
|
|
1562
|
+
requestHeaders['x-forwarded-for'] = req.socket.remoteAddress;
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
const transport = targetUrl.protocol === 'https:' ? https : http;
|
|
1566
|
+
const upstreamReq = transport.request({
|
|
1567
|
+
protocol: targetUrl.protocol,
|
|
1568
|
+
hostname: targetUrl.hostname,
|
|
1569
|
+
port: targetUrl.port || (targetUrl.protocol === 'https:' ? 443 : 80),
|
|
1570
|
+
method: req.method || 'GET',
|
|
1571
|
+
path: `${targetUrl.pathname}${targetUrl.search}`,
|
|
1572
|
+
headers: requestHeaders,
|
|
1573
|
+
agent: targetUrl.protocol === 'https:' ? HTTPS_KEEP_ALIVE_AGENT : HTTP_KEEP_ALIVE_AGENT
|
|
1574
|
+
}, (upstreamRes) => {
|
|
1575
|
+
const responseHeaders = { ...upstreamRes.headers };
|
|
1576
|
+
delete responseHeaders.connection;
|
|
1577
|
+
res.writeHead(upstreamRes.statusCode || 502, responseHeaders);
|
|
1578
|
+
upstreamRes.pipe(res);
|
|
1579
|
+
});
|
|
1580
|
+
|
|
1581
|
+
upstreamReq.setTimeout(timeoutMs, () => {
|
|
1582
|
+
upstreamReq.destroy(new Error(`upstream timeout (${timeoutMs}ms)`));
|
|
1583
|
+
});
|
|
1584
|
+
|
|
1585
|
+
upstreamReq.on('error', (err) => {
|
|
1586
|
+
if (res.headersSent) {
|
|
1587
|
+
try { res.destroy(err); } catch (_) {}
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
const body = JSON.stringify({ error: `proxy request failed: ${err.message}` });
|
|
1591
|
+
res.writeHead(502, {
|
|
1592
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1593
|
+
'Content-Length': Buffer.byteLength(body, 'utf-8')
|
|
1594
|
+
});
|
|
1595
|
+
res.end(body, 'utf-8');
|
|
1596
|
+
});
|
|
1597
|
+
|
|
1598
|
+
req.pipe(upstreamReq);
|
|
1599
|
+
});
|
|
1600
|
+
|
|
1601
|
+
server.on('connection', (socket) => {
|
|
1602
|
+
connections.add(socket);
|
|
1603
|
+
socket.on('close', () => connections.delete(socket));
|
|
1604
|
+
});
|
|
1605
|
+
|
|
1606
|
+
return new Promise((resolve, reject) => {
|
|
1607
|
+
server.once('error', reject);
|
|
1608
|
+
server.listen(settings.port, settings.host, () => {
|
|
1609
|
+
server.removeListener('error', reject);
|
|
1610
|
+
resolve({
|
|
1611
|
+
server,
|
|
1612
|
+
connections,
|
|
1613
|
+
settings,
|
|
1614
|
+
upstream,
|
|
1615
|
+
startedAt: toIsoTime(Date.now()),
|
|
1616
|
+
listenUrl: buildProxyListenUrl(settings)
|
|
1617
|
+
});
|
|
1618
|
+
});
|
|
1619
|
+
});
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
async function startBuiltinProxyRuntime(payload = {}) {
|
|
1623
|
+
if (runtime) {
|
|
1624
|
+
return {
|
|
1625
|
+
error: '内建代理已在运行',
|
|
1626
|
+
runtime: {
|
|
1627
|
+
listenUrl: runtime.listenUrl,
|
|
1628
|
+
upstreamProvider: runtime.upstream.providerName
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
const saveResult = saveBuiltinProxySettings(payload);
|
|
1634
|
+
if (saveResult.error) {
|
|
1635
|
+
return { error: saveResult.error };
|
|
1636
|
+
}
|
|
1637
|
+
const settings = saveResult.settings;
|
|
1638
|
+
const upstream = resolveBuiltinProxyUpstream(settings);
|
|
1639
|
+
if (upstream.error) {
|
|
1640
|
+
return { error: upstream.error };
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
try {
|
|
1644
|
+
runtime = await createBuiltinProxyServer(settings, upstream);
|
|
1645
|
+
return {
|
|
1646
|
+
success: true,
|
|
1647
|
+
running: true,
|
|
1648
|
+
listenUrl: runtime.listenUrl,
|
|
1649
|
+
upstreamProvider: upstream.providerName,
|
|
1650
|
+
settings
|
|
1651
|
+
};
|
|
1652
|
+
} catch (e) {
|
|
1653
|
+
return { error: `启动内建代理失败: ${e.message}` };
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
async function stopBuiltinProxyRuntime() {
|
|
1658
|
+
if (!runtime) {
|
|
1659
|
+
return { success: true, running: false };
|
|
1660
|
+
}
|
|
1661
|
+
const currentRuntime = runtime;
|
|
1662
|
+
runtime = null;
|
|
1663
|
+
|
|
1664
|
+
await new Promise((resolve) => {
|
|
1665
|
+
let settled = false;
|
|
1666
|
+
const finish = () => {
|
|
1667
|
+
if (settled) return;
|
|
1668
|
+
settled = true;
|
|
1669
|
+
resolve();
|
|
1670
|
+
};
|
|
1671
|
+
|
|
1672
|
+
currentRuntime.server.close(() => finish());
|
|
1673
|
+
setTimeout(() => finish(), 1000);
|
|
1674
|
+
});
|
|
1675
|
+
|
|
1676
|
+
for (const socket of currentRuntime.connections) {
|
|
1677
|
+
try { socket.destroy(); } catch (_) {}
|
|
1678
|
+
}
|
|
1679
|
+
currentRuntime.connections.clear();
|
|
1680
|
+
|
|
1681
|
+
return {
|
|
1682
|
+
success: true,
|
|
1683
|
+
running: false
|
|
1684
|
+
};
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
function getBuiltinProxyStatus() {
|
|
1688
|
+
const settings = readBuiltinProxySettings();
|
|
1689
|
+
return {
|
|
1690
|
+
running: !!runtime,
|
|
1691
|
+
settings,
|
|
1692
|
+
runtime: runtime
|
|
1693
|
+
? {
|
|
1694
|
+
provider: BUILTIN_PROXY_PROVIDER_NAME,
|
|
1695
|
+
startedAt: runtime.startedAt,
|
|
1696
|
+
listenUrl: runtime.listenUrl,
|
|
1697
|
+
upstreamProvider: runtime.upstream.providerName,
|
|
1698
|
+
upstreamBaseUrl: runtime.upstream.baseUrl
|
|
1699
|
+
}
|
|
1700
|
+
: null
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
return {
|
|
1705
|
+
canListenPort,
|
|
1706
|
+
findAvailablePort,
|
|
1707
|
+
normalizeBuiltinProxySettings,
|
|
1708
|
+
readBuiltinProxySettings,
|
|
1709
|
+
resolveBuiltinProxyProviderName,
|
|
1710
|
+
saveBuiltinProxySettings,
|
|
1711
|
+
buildProxyListenUrl,
|
|
1712
|
+
buildBuiltinProxyProviderBaseUrl,
|
|
1713
|
+
removePersistedBuiltinProxyProviderFromConfig,
|
|
1714
|
+
hasCodexConfigReadyForProxy,
|
|
1715
|
+
resolveBuiltinProxyUpstream,
|
|
1716
|
+
createBuiltinProxyServer,
|
|
1717
|
+
startBuiltinProxyRuntime,
|
|
1718
|
+
stopBuiltinProxyRuntime,
|
|
1719
|
+
getBuiltinProxyStatus
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
module.exports = {
|
|
1724
|
+
createBuiltinProxyRuntimeController
|
|
1725
|
+
};
|