@maplezzk/llm-proxy 0.14.0 → 0.15.0
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/dist/api/admin/components/adapters.d.ts +2 -0
- package/dist/api/admin/components/adapters.d.ts.map +1 -1
- package/dist/api/admin/components/adapters.js +34 -0
- package/dist/api/admin/components/adapters.js.map +1 -1
- package/dist/api/admin/components/providers.d.ts.map +1 -1
- package/dist/api/admin/components/providers.js +8 -3
- package/dist/api/admin/components/providers.js.map +1 -1
- package/dist/api/admin-app.js +5 -5
- package/dist/api/admin-ui.html +11 -1
- package/dist/proxy/pipeline.d.ts.map +1 -1
- package/dist/proxy/pipeline.js +1 -0
- package/dist/proxy/pipeline.js.map +1 -1
- package/dist/proxy/provider.d.ts +1 -0
- package/dist/proxy/provider.d.ts.map +1 -1
- package/dist/proxy/provider.js +18 -3
- package/dist/proxy/provider.js.map +1 -1
- package/dist/proxy/stream-converter.d.ts +2 -2
- package/dist/proxy/stream-converter.d.ts.map +1 -1
- package/dist/proxy/stream-converter.js +158 -42
- package/dist/proxy/stream-converter.js.map +1 -1
- package/dist/proxy/translation.d.ts +19 -0
- package/dist/proxy/translation.d.ts.map +1 -1
- package/dist/proxy/translation.js +531 -95
- package/dist/proxy/translation.js.map +1 -1
- package/dist/types/i18n.generated.d.ts +1 -1
- package/dist/types/i18n.generated.d.ts.map +1 -1
- package/locales/en/translation.json +5 -0
- package/locales/zh/translation.json +5 -0
- package/package.json +1 -1
|
@@ -1,5 +1,55 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { sanitizeApiBase } from '../lib/http-utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build a lookup table of namespace function tools from the original request tools array.
|
|
5
|
+
* CCX's BuildCodexToolContext scans tools and records namespace children.
|
|
6
|
+
*/
|
|
7
|
+
export function buildNamespaceToolContext(tools) {
|
|
8
|
+
const ctx = new Map();
|
|
9
|
+
if (!Array.isArray(tools))
|
|
10
|
+
return ctx;
|
|
11
|
+
for (const raw of tools) {
|
|
12
|
+
const t = raw;
|
|
13
|
+
if (t.type === 'namespace') {
|
|
14
|
+
const namespaceName = t.name ?? '';
|
|
15
|
+
const children = t.tools;
|
|
16
|
+
if (!namespaceName || !children)
|
|
17
|
+
continue;
|
|
18
|
+
for (const child of children) {
|
|
19
|
+
const childItem = child;
|
|
20
|
+
if (childItem.type !== 'function')
|
|
21
|
+
continue;
|
|
22
|
+
const childName = childItem.name;
|
|
23
|
+
if (!childName)
|
|
24
|
+
continue;
|
|
25
|
+
// CCX convention: namespace__name (namespace ends with __ → no extra separator)
|
|
26
|
+
const flatName = namespaceName.endsWith('__') ? `${namespaceName}${childName}` : `${namespaceName}__${childName}`;
|
|
27
|
+
ctx.set(flatName, { namespace: namespaceName, name: childName });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return ctx;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Remap namespace function calls in a response output array.
|
|
35
|
+
* CCX's RemapNamespaceFunctionCallsInResponse uses the context to add namespace field.
|
|
36
|
+
*/
|
|
37
|
+
export function remapNamespaceFunctionCalls(output, namespaceCtx) {
|
|
38
|
+
if (!namespaceCtx.size)
|
|
39
|
+
return;
|
|
40
|
+
for (const item of output) {
|
|
41
|
+
if (item.type !== 'function_call')
|
|
42
|
+
continue;
|
|
43
|
+
const name = item.name;
|
|
44
|
+
if (!name)
|
|
45
|
+
continue;
|
|
46
|
+
const spec = namespaceCtx.get(name);
|
|
47
|
+
if (!spec)
|
|
48
|
+
continue;
|
|
49
|
+
item.name = spec.name;
|
|
50
|
+
item.namespace = spec.namespace;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
3
53
|
// --- Helpers ---
|
|
4
54
|
/** 从 thinking 内容生成确定性伪签名(与 stream-converter.ts 中的一致) */
|
|
5
55
|
function makeSignature(thinkingText) {
|
|
@@ -29,72 +79,146 @@ function truncateBody(obj, maxLen = 500) {
|
|
|
29
79
|
function convertToolsToAnthropic(tools) {
|
|
30
80
|
if (!Array.isArray(tools))
|
|
31
81
|
return undefined;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// Filter out non-function tools (web_search, code_interpreter, etc.) -
|
|
35
|
-
// Anthropic API only supports standard tools
|
|
36
|
-
const item = t;
|
|
37
|
-
return item.type === 'function' || (item.name && item.input_schema);
|
|
38
|
-
})
|
|
39
|
-
.map((t) => {
|
|
82
|
+
const result = [];
|
|
83
|
+
for (const t of tools) {
|
|
40
84
|
const item = t;
|
|
85
|
+
const type = String(item.type ?? '');
|
|
86
|
+
// OpenAI Responses built-in: computer_use_preview → Anthropic computer_20251124
|
|
87
|
+
if (type.startsWith('computer_use_preview')) {
|
|
88
|
+
result.push({
|
|
89
|
+
type: 'computer_20251124',
|
|
90
|
+
name: 'computer',
|
|
91
|
+
display_width_px: (item.display_width ?? item.display_width_px),
|
|
92
|
+
display_height_px: (item.display_height ?? item.display_height_px),
|
|
93
|
+
display_number: item.display_number,
|
|
94
|
+
});
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
// OpenAI built-in tools with no Anthropic equivalent → skip
|
|
98
|
+
if (['web_search_preview', 'web_search', 'code_interpreter', 'file_search'].includes(type)) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// OpenAI namespace tools → skip (Anthropic has no equivalent)
|
|
102
|
+
// CCX approach: strip namespace tools and remap via CodexToolContext on response
|
|
103
|
+
if (type === 'namespace') {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
41
106
|
// OpenAI Chat format: { type: "function", function: { name, description, parameters } }
|
|
42
|
-
if (
|
|
107
|
+
if (type === 'function' && item.function) {
|
|
43
108
|
const fn = item.function;
|
|
44
|
-
|
|
109
|
+
result.push({
|
|
45
110
|
name: fn.name ?? '',
|
|
46
111
|
description: fn.description || undefined,
|
|
47
112
|
input_schema: fn.parameters ?? {},
|
|
48
|
-
};
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
49
115
|
}
|
|
50
116
|
// OpenAI Responses flat format: { type: "function", name, description, parameters } (no "function" wrapper)
|
|
51
|
-
if (
|
|
52
|
-
|
|
117
|
+
if (type === 'function' && item.name) {
|
|
118
|
+
result.push({
|
|
53
119
|
name: item.name ?? '',
|
|
54
120
|
description: item.description || undefined,
|
|
55
121
|
input_schema: item.parameters ?? {},
|
|
56
|
-
};
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
57
124
|
}
|
|
58
|
-
// Already Anthropic format
|
|
59
|
-
|
|
60
|
-
|
|
125
|
+
// Already Anthropic format: { name, input_schema }
|
|
126
|
+
if (item.name && item.input_schema) {
|
|
127
|
+
result.push(item);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return result.length > 0 ? result : undefined;
|
|
61
132
|
}
|
|
62
133
|
function convertToolsToOpenAI(tools) {
|
|
63
134
|
if (!Array.isArray(tools))
|
|
64
135
|
return undefined;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// Filter out non-function tools (web_search, code_interpreter, etc.) -
|
|
68
|
-
// Chat Completions API only supports type: "function"
|
|
69
|
-
const item = t;
|
|
70
|
-
return item.type === 'function' || (item.name && item.input_schema);
|
|
71
|
-
})
|
|
72
|
-
.map((t) => {
|
|
136
|
+
const result = [];
|
|
137
|
+
for (const t of tools) {
|
|
73
138
|
const item = t;
|
|
139
|
+
const type = String(item.type ?? '');
|
|
140
|
+
// Anthropic built-in: computer_2025* (versioned type) → OpenAI computer_use_preview
|
|
141
|
+
// Must be checked BEFORE the strip below (computer_use_preview keywords overlap)
|
|
142
|
+
if (type.startsWith('computer_20')) {
|
|
143
|
+
result.push({
|
|
144
|
+
type: 'computer_use_preview',
|
|
145
|
+
display_width: (item.display_width_px ?? item.display_width),
|
|
146
|
+
display_height: (item.display_height_px ?? item.display_height),
|
|
147
|
+
display_number: item.display_number,
|
|
148
|
+
});
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
// CCX stripCodexClientOnlyTools: drop non-function tool types (except namespace)
|
|
152
|
+
// Namespace tools are flattened below (CCX's namespaceToolsToOpenAI)
|
|
153
|
+
if (['custom', 'web_search', 'web_search_preview', 'computer_use', 'computer_use_preview',
|
|
154
|
+
'local_shell', 'code_interpreter', 'file_search'].includes(type)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
// CCX namespaceToolsToOpenAI: flatten namespace tools to OpenAI function tools
|
|
158
|
+
// e.g. namespace "mcp__computer_use__" with child "get_app_state" → tool "mcp__computer_use__get_app_state"
|
|
159
|
+
if (type === 'namespace') {
|
|
160
|
+
const nsName = item.name ?? '';
|
|
161
|
+
const nsDesc = item.description ?? '';
|
|
162
|
+
const children = item.tools;
|
|
163
|
+
if (nsName && children) {
|
|
164
|
+
for (const child of children) {
|
|
165
|
+
const childItem = child;
|
|
166
|
+
if (childItem.type !== 'function')
|
|
167
|
+
continue;
|
|
168
|
+
const childName = childItem.name;
|
|
169
|
+
if (!childName)
|
|
170
|
+
continue;
|
|
171
|
+
const childFn = childItem.function;
|
|
172
|
+
const childDesc = childFn?.description ?? childItem.description ?? '';
|
|
173
|
+
const childParams = (childFn?.parameters ?? childItem.parameters) ?? {};
|
|
174
|
+
// CCX flattenNamespaceToolName: namespace ends with __ → no extra separator
|
|
175
|
+
const flatName = nsName.endsWith('__') ? `${nsName}${childName}` : `${nsName}__${childName}`;
|
|
176
|
+
// CCX combineNamespaceDescription
|
|
177
|
+
const combinedDesc = nsDesc && childDesc ? `${nsDesc}\n\n${childDesc}` : (nsDesc || childDesc || undefined);
|
|
178
|
+
result.push({
|
|
179
|
+
type: 'function',
|
|
180
|
+
function: {
|
|
181
|
+
name: flatName,
|
|
182
|
+
description: combinedDesc,
|
|
183
|
+
parameters: childParams,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
74
190
|
// Anthropic format: { name, description, input_schema }
|
|
75
191
|
if (item.name && item.input_schema) {
|
|
76
|
-
|
|
192
|
+
result.push({
|
|
77
193
|
type: 'function',
|
|
78
194
|
function: {
|
|
79
195
|
name: item.name,
|
|
80
196
|
description: item.description || undefined,
|
|
81
197
|
parameters: item.input_schema,
|
|
82
198
|
},
|
|
83
|
-
};
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
84
201
|
}
|
|
85
202
|
// OpenAI Responses flat format: { type: "function", name, parameters } → wrap in "function" for Chat Completions
|
|
86
|
-
if (
|
|
87
|
-
|
|
203
|
+
if (type === 'function' && item.name && !item.function) {
|
|
204
|
+
result.push({
|
|
88
205
|
type: 'function',
|
|
89
206
|
function: {
|
|
90
207
|
name: item.name ?? '',
|
|
91
208
|
description: item.description || undefined,
|
|
92
209
|
parameters: item.parameters ?? {},
|
|
93
210
|
},
|
|
94
|
-
};
|
|
211
|
+
});
|
|
212
|
+
continue;
|
|
95
213
|
}
|
|
96
|
-
|
|
97
|
-
|
|
214
|
+
// Already valid Chat format (type: "function" with nested "function") → keep
|
|
215
|
+
if (type === 'function' && item.function) {
|
|
216
|
+
result.push(item);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
// Unknown tool type → skip (OpenAI Chat only accepts type: "function")
|
|
220
|
+
}
|
|
221
|
+
return result.length > 0 ? result : undefined;
|
|
98
222
|
}
|
|
99
223
|
function convertToolChoiceToAnthropic(toolChoice) {
|
|
100
224
|
if (toolChoice === undefined || toolChoice === null)
|
|
@@ -211,17 +335,25 @@ function convertResponsesInputToMessages(input) {
|
|
|
211
335
|
messages.push({ role: it.role, content: normalizedContent });
|
|
212
336
|
}
|
|
213
337
|
else if (it.type === 'function_call') {
|
|
214
|
-
// { type: "function_call", call_id, name, arguments }
|
|
338
|
+
// { type: "function_call", call_id, name, arguments, namespace? }
|
|
339
|
+
// Encode namespace in function name using `__` prefix convention (CCX-compatible)
|
|
340
|
+
let fnName = it.name ?? '';
|
|
341
|
+
const namespace = it.namespace;
|
|
342
|
+
if (namespace) {
|
|
343
|
+
// CCX convention: if namespace ends with __, don't add separator
|
|
344
|
+
fnName = namespace.endsWith('__') ? `${namespace}${fnName}` : `${namespace}__${fnName}`;
|
|
345
|
+
}
|
|
215
346
|
// 检查上一条消息是否是 assistant 消息,如果是则合并 tool_calls,避免两条连续 assistant 消息
|
|
216
347
|
const lastMsg = messages.length > 0 ? messages[messages.length - 1] : null;
|
|
348
|
+
const tcEntry = {
|
|
349
|
+
id: it.call_id ?? it.id,
|
|
350
|
+
type: 'function',
|
|
351
|
+
function: { name: fnName, arguments: it.arguments ?? '' },
|
|
352
|
+
};
|
|
217
353
|
if (lastMsg && lastMsg.role === 'assistant') {
|
|
218
354
|
if (!lastMsg.tool_calls)
|
|
219
355
|
lastMsg.tool_calls = [];
|
|
220
|
-
lastMsg.tool_calls.push(
|
|
221
|
-
id: it.call_id ?? it.id,
|
|
222
|
-
type: 'function',
|
|
223
|
-
function: { name: it.name ?? '', arguments: it.arguments ?? '' },
|
|
224
|
-
});
|
|
356
|
+
lastMsg.tool_calls.push(tcEntry);
|
|
225
357
|
// 确保 content 不为 undefined(OpenAI 要求 tool_calls 时 content 为 null)
|
|
226
358
|
if (lastMsg.content === undefined)
|
|
227
359
|
lastMsg.content = null;
|
|
@@ -230,20 +362,56 @@ function convertResponsesInputToMessages(input) {
|
|
|
230
362
|
messages.push({
|
|
231
363
|
role: 'assistant',
|
|
232
364
|
content: null,
|
|
233
|
-
tool_calls: [
|
|
234
|
-
id: it.call_id ?? it.id,
|
|
235
|
-
type: 'function',
|
|
236
|
-
function: { name: it.name ?? '', arguments: it.arguments ?? '' },
|
|
237
|
-
}],
|
|
365
|
+
tool_calls: [tcEntry],
|
|
238
366
|
});
|
|
239
367
|
}
|
|
240
368
|
}
|
|
369
|
+
else if (it.type === 'reasoning') {
|
|
370
|
+
// { type: "reasoning", summary, content } → skip (not a message type in Chat format)
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
241
373
|
else if (it.type === 'function_call_output') {
|
|
242
|
-
// { type: "function_call_output", call_id, output }
|
|
374
|
+
// { type: "function_call_output", call_id, output: string | array of content blocks }
|
|
375
|
+
let outputContent = it.output ?? '';
|
|
376
|
+
// Normalize Responses content blocks to Anthropic-compatible format
|
|
377
|
+
if (Array.isArray(outputContent)) {
|
|
378
|
+
outputContent = outputContent.map((block) => {
|
|
379
|
+
if (block.type === 'input_text') {
|
|
380
|
+
return { type: 'text', text: block.text };
|
|
381
|
+
}
|
|
382
|
+
if (block.type === 'output_text') {
|
|
383
|
+
return { type: 'text', text: block.text };
|
|
384
|
+
}
|
|
385
|
+
if (block.type === 'input_image') {
|
|
386
|
+
const imageUrl = block.image_url;
|
|
387
|
+
if (imageUrl) {
|
|
388
|
+
return { type: 'image', source: { type: 'url', url: imageUrl } };
|
|
389
|
+
}
|
|
390
|
+
return { type: 'text', text: '[image]' };
|
|
391
|
+
}
|
|
392
|
+
return block;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
messages.push({
|
|
396
|
+
role: 'tool',
|
|
397
|
+
tool_call_id: it.call_id,
|
|
398
|
+
content: outputContent,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
else if (it.type === 'computer_call_output') {
|
|
402
|
+
// { type: "computer_call_output", call_id, output: { type: "computer_screenshot", image_url } }
|
|
403
|
+
const output = it.output;
|
|
404
|
+
const imageUrl = output?.image_url;
|
|
405
|
+
const fileId = output?.file_id;
|
|
406
|
+
const content = imageUrl
|
|
407
|
+
? [{ type: 'image', source: { type: 'url', url: imageUrl } }]
|
|
408
|
+
: fileId
|
|
409
|
+
? [{ type: 'text', text: '[screenshot from file_id]' }]
|
|
410
|
+
: '';
|
|
243
411
|
messages.push({
|
|
244
412
|
role: 'tool',
|
|
245
413
|
tool_call_id: it.call_id,
|
|
246
|
-
content
|
|
414
|
+
content,
|
|
247
415
|
});
|
|
248
416
|
}
|
|
249
417
|
else if (it.type === 'item_reference') {
|
|
@@ -295,8 +463,69 @@ function convertMessagesToResponsesInput(messages) {
|
|
|
295
463
|
input.push({ type: 'message', role: 'system', content: m.content });
|
|
296
464
|
}
|
|
297
465
|
else {
|
|
298
|
-
// user
|
|
299
|
-
|
|
466
|
+
// Check for Anthropic tool_result blocks in user message content array
|
|
467
|
+
const content = m.content;
|
|
468
|
+
if (m.role === 'user' && Array.isArray(content)) {
|
|
469
|
+
const blocks = content;
|
|
470
|
+
const toolResults = blocks.filter((b) => b.type === 'tool_result');
|
|
471
|
+
const otherBlocks = blocks.filter((b) => b.type !== 'tool_result');
|
|
472
|
+
// Emit computer_call_output for tool_results with image content
|
|
473
|
+
for (const tr of toolResults) {
|
|
474
|
+
const trContent = tr.content;
|
|
475
|
+
let imageUrl;
|
|
476
|
+
let fileId;
|
|
477
|
+
if (Array.isArray(trContent)) {
|
|
478
|
+
const imgBlock = trContent.find((b) => b.type === 'image');
|
|
479
|
+
if (imgBlock) {
|
|
480
|
+
const src = imgBlock.source;
|
|
481
|
+
if (src?.type === 'url') {
|
|
482
|
+
imageUrl = src.url;
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
fileId = imgBlock.file_id;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (imageUrl) {
|
|
490
|
+
input.push({
|
|
491
|
+
type: 'computer_call_output',
|
|
492
|
+
call_id: tr.tool_use_id,
|
|
493
|
+
output: { type: 'computer_screenshot', image_url: imageUrl },
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
else if (fileId) {
|
|
497
|
+
input.push({
|
|
498
|
+
type: 'computer_call_output',
|
|
499
|
+
call_id: tr.tool_use_id,
|
|
500
|
+
output: { type: 'computer_screenshot', file_id: fileId },
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
// tool_result without image → function_call_output
|
|
505
|
+
input.push({
|
|
506
|
+
type: 'function_call_output',
|
|
507
|
+
call_id: tr.tool_use_id,
|
|
508
|
+
output: typeof trContent === 'string' ? trContent : JSON.stringify(trContent),
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// Emit remaining non-tool_result blocks as message items
|
|
513
|
+
if (otherBlocks.length > 0) {
|
|
514
|
+
const textBlocks = otherBlocks.filter((b) => b.type === 'text' || b.type === 'input_text');
|
|
515
|
+
const text = textBlocks.map((b) => b.text ?? '').join('');
|
|
516
|
+
if (text || otherBlocks.length !== textBlocks.length) {
|
|
517
|
+
input.push({
|
|
518
|
+
type: 'message',
|
|
519
|
+
role: m.role ?? 'user',
|
|
520
|
+
content: otherBlocks,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
// user, developer, etc. (non-Anthropic format)
|
|
527
|
+
input.push({ type: 'message', role: m.role ?? 'user', content: m.content });
|
|
528
|
+
}
|
|
300
529
|
}
|
|
301
530
|
}
|
|
302
531
|
return input;
|
|
@@ -378,7 +607,9 @@ function convertMessagesToAnthropic(messages) {
|
|
|
378
607
|
toolResults.push({
|
|
379
608
|
type: 'tool_result',
|
|
380
609
|
tool_use_id: cur.tool_call_id,
|
|
381
|
-
content: typeof cur.content === 'string'
|
|
610
|
+
content: typeof cur.content === 'string' || Array.isArray(cur.content)
|
|
611
|
+
? cur.content
|
|
612
|
+
: JSON.stringify(cur.content),
|
|
382
613
|
});
|
|
383
614
|
i++;
|
|
384
615
|
}
|
|
@@ -472,8 +703,8 @@ function buildAnthropicFromOpenAI(params) {
|
|
|
472
703
|
body.system = params.system;
|
|
473
704
|
if (params.temperature !== undefined)
|
|
474
705
|
body.temperature = params.temperature;
|
|
475
|
-
if (params.stream)
|
|
476
|
-
body.stream =
|
|
706
|
+
if (params.stream !== undefined)
|
|
707
|
+
body.stream = params.stream;
|
|
477
708
|
if (params.top_p !== undefined)
|
|
478
709
|
body.top_p = params.top_p;
|
|
479
710
|
if (params.stop)
|
|
@@ -503,13 +734,52 @@ function convertMessagesToOpenAI(messages) {
|
|
|
503
734
|
});
|
|
504
735
|
}
|
|
505
736
|
// Emit remaining user content if any
|
|
737
|
+
// Convert Anthropic image blocks → OpenAI image_url format
|
|
506
738
|
if (otherBlocks.length > 0) {
|
|
507
|
-
const
|
|
508
|
-
|
|
739
|
+
const parts = otherBlocks.map((b) => {
|
|
740
|
+
if (b.type === 'image' && b.source) {
|
|
741
|
+
const source = b.source;
|
|
742
|
+
const url = source.url || source.data;
|
|
743
|
+
return {
|
|
744
|
+
type: 'image_url',
|
|
745
|
+
image_url: { url: url, ...(source.media_type ? {} : {}) },
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
if (b.type === 'text')
|
|
749
|
+
return { type: 'text', text: b.text };
|
|
750
|
+
return b;
|
|
751
|
+
});
|
|
752
|
+
// If single text part, collapse to string
|
|
753
|
+
if (parts.length === 1 && parts[0].type === 'text') {
|
|
754
|
+
result.push({ role: 'user', content: parts[0].text });
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
result.push({ role: 'user', content: parts });
|
|
758
|
+
}
|
|
509
759
|
}
|
|
510
760
|
continue;
|
|
511
761
|
}
|
|
512
762
|
}
|
|
763
|
+
// Handle tool messages with array content (e.g., computer_call_output screenshots)
|
|
764
|
+
// Convert image blocks → text for models that don't support image_url in tool messages
|
|
765
|
+
if (m.role === 'tool' && Array.isArray(m.content)) {
|
|
766
|
+
const blocks = m.content;
|
|
767
|
+
const textParts = [];
|
|
768
|
+
for (const b of blocks) {
|
|
769
|
+
if (b.type === 'text') {
|
|
770
|
+
textParts.push(b.text);
|
|
771
|
+
}
|
|
772
|
+
else if (b.type === 'image') {
|
|
773
|
+
const source = b.source;
|
|
774
|
+
if (source?.url)
|
|
775
|
+
textParts.push(`[image: ${source.url}]`);
|
|
776
|
+
else
|
|
777
|
+
textParts.push('[image]');
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
result.push({ role: 'tool', tool_call_id: m.tool_call_id, content: textParts.join('\n') || '[output]' });
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
513
783
|
// developer role → system (older Chat Completions APIs don't support "developer")
|
|
514
784
|
if (m.role === 'developer') {
|
|
515
785
|
result.push({ role: 'system', content: m.content });
|
|
@@ -585,8 +855,8 @@ function buildOpenAIFromAnthropic(params) {
|
|
|
585
855
|
body.max_tokens = params.max_tokens;
|
|
586
856
|
if (params.temperature !== undefined)
|
|
587
857
|
body.temperature = params.temperature;
|
|
588
|
-
if (params.stream)
|
|
589
|
-
body.stream =
|
|
858
|
+
if (params.stream !== undefined)
|
|
859
|
+
body.stream = params.stream;
|
|
590
860
|
if (params.top_p !== undefined)
|
|
591
861
|
body.top_p = params.top_p;
|
|
592
862
|
if (params.stop)
|
|
@@ -635,8 +905,8 @@ function buildAnthropicFromOpenAIResponses(params) {
|
|
|
635
905
|
body.system = params.system;
|
|
636
906
|
if (params.temperature !== undefined)
|
|
637
907
|
body.temperature = params.temperature;
|
|
638
|
-
if (params.stream)
|
|
639
|
-
body.stream =
|
|
908
|
+
if (params.stream !== undefined)
|
|
909
|
+
body.stream = params.stream;
|
|
640
910
|
if (params.top_p !== undefined)
|
|
641
911
|
body.top_p = params.top_p;
|
|
642
912
|
if (params.stop)
|
|
@@ -667,8 +937,8 @@ function buildOpenAIResponsesFromFullParams(params) {
|
|
|
667
937
|
body.temperature = params.temperature;
|
|
668
938
|
if (params.max_tokens !== undefined)
|
|
669
939
|
body.max_output_tokens = params.max_tokens;
|
|
670
|
-
if (params.stream)
|
|
671
|
-
body.stream =
|
|
940
|
+
if (params.stream !== undefined)
|
|
941
|
+
body.stream = params.stream;
|
|
672
942
|
if (params.top_p !== undefined)
|
|
673
943
|
body.top_p = params.top_p;
|
|
674
944
|
if (params.stop)
|
|
@@ -698,9 +968,9 @@ export async function transformInboundRequest(inboundType, route, body, logger)
|
|
|
698
968
|
}
|
|
699
969
|
if (sameProtocol) {
|
|
700
970
|
const upstreamBody = { ...body, model: route.modelId };
|
|
701
|
-
// 客户端没传 stream
|
|
971
|
+
// 客户端没传 stream 时默认流式
|
|
702
972
|
if (upstreamBody.stream === undefined) {
|
|
703
|
-
upstreamBody.stream =
|
|
973
|
+
upstreamBody.stream = true;
|
|
704
974
|
}
|
|
705
975
|
// max_tokens: 0 → 不传(让上游用默认值), 应用路由级默认值
|
|
706
976
|
sanitizeMaxTokens(upstreamBody, route);
|
|
@@ -723,6 +993,33 @@ export async function transformInboundRequest(inboundType, route, body, logger)
|
|
|
723
993
|
: inboundType === 'openai'
|
|
724
994
|
? extractFullOpenAI(body)
|
|
725
995
|
: extractFullAnthropic(body);
|
|
996
|
+
// Strip Codex-internal MCP probe tools and exec_command BEFORE any builder sees them.
|
|
997
|
+
// These tools trigger MCP server calls (resources/list, resources/read, etc.) when
|
|
998
|
+
// returned as function_calls. They are generated by Codex for Responses API's server-side
|
|
999
|
+
// MCP handling and don't work with non-Responses upstreams.
|
|
1000
|
+
//
|
|
1001
|
+
// NOTE: namespace tools (mcp__computer_use__ etc.) are NOT stripped here.
|
|
1002
|
+
// They are flattened to function tools in convertToolsToOpenAI (like CCX's namespaceToolsToOpenAI).
|
|
1003
|
+
if (params.tools && inboundType === 'openai-responses') {
|
|
1004
|
+
const mcpPrefixes = ['list_mcp_', 'read_mcp_', 'write_mcp_', 'subscribe_mcp_'];
|
|
1005
|
+
const codexTools = ['exec_command', 'exec'];
|
|
1006
|
+
params.tools = params.tools.filter((t) => {
|
|
1007
|
+
if (typeof t === 'string')
|
|
1008
|
+
return !codexTools.includes(t);
|
|
1009
|
+
const item = t;
|
|
1010
|
+
// Check by tool name AND nested function name (Chat format)
|
|
1011
|
+
const itemName = String(item.name ?? item.function?.name ?? '');
|
|
1012
|
+
if (mcpPrefixes.some((p) => itemName.startsWith(p)))
|
|
1013
|
+
return false;
|
|
1014
|
+
if (codexTools.includes(itemName))
|
|
1015
|
+
return false;
|
|
1016
|
+
return true;
|
|
1017
|
+
});
|
|
1018
|
+
// Strip only by name — type-based stripping stays in convertToolsToOpenAI
|
|
1019
|
+
// (computer_use_preview is valid for Anthropic path, dropped only for Chat path)
|
|
1020
|
+
if (params.tools.length === 0)
|
|
1021
|
+
delete params.tools;
|
|
1022
|
+
}
|
|
726
1023
|
params.model = route.modelId;
|
|
727
1024
|
// max_tokens: 0 → undefined(不传,让 builder 走默认值), 应用路由级默认值
|
|
728
1025
|
if (params.max_tokens === 0)
|
|
@@ -746,9 +1043,9 @@ export async function transformInboundRequest(inboundType, route, body, logger)
|
|
|
746
1043
|
sanitizeMaxTokens(upstreamBody, route);
|
|
747
1044
|
// 注入 thinking 配置到转换后的请求体
|
|
748
1045
|
injectThinkingConfig(upstreamBody, route);
|
|
749
|
-
// 客户端没传 stream
|
|
1046
|
+
// 客户端没传 stream 时默认流式
|
|
750
1047
|
if (upstreamBody.stream === undefined) {
|
|
751
|
-
upstreamBody.stream =
|
|
1048
|
+
upstreamBody.stream = true;
|
|
752
1049
|
}
|
|
753
1050
|
// thinking 模式检测:配置开启了 或 消息中已有 thinking 块,都需要补全
|
|
754
1051
|
if (route.providerType === 'anthropic') {
|
|
@@ -768,21 +1065,6 @@ export async function transformInboundRequest(inboundType, route, body, logger)
|
|
|
768
1065
|
}, 'debug');
|
|
769
1066
|
return { url, headers, body: upstreamBody, crossProtocol: true };
|
|
770
1067
|
}
|
|
771
|
-
/**
|
|
772
|
-
* 根据 content 块生成描述性的占位 thinking 文本。
|
|
773
|
-
*/
|
|
774
|
-
function generatePlaceholderThinking(content) {
|
|
775
|
-
const toolUses = content.filter((c) => c.type === 'tool_use');
|
|
776
|
-
if (toolUses.length > 0) {
|
|
777
|
-
const names = toolUses.map((c) => c.name).filter(Boolean);
|
|
778
|
-
const uniqueNames = [...new Set(names)];
|
|
779
|
-
if (uniqueNames.length === 1) {
|
|
780
|
-
return `让我调用 ${uniqueNames[0]} 工具`;
|
|
781
|
-
}
|
|
782
|
-
return `让我调用 ${uniqueNames.join('、')} 等多个工具`;
|
|
783
|
-
}
|
|
784
|
-
return '让我思考一下';
|
|
785
|
-
}
|
|
786
1068
|
/**
|
|
787
1069
|
* 检查消息列表中是否有任意 assistant 消息已包含 thinking 块(或 OpenAI 的 reasoning_content)。
|
|
788
1070
|
* 如果有,说明对话已经在 thinking 模式下,需要保证所有 assistant 消息都有 thinking 块。
|
|
@@ -814,10 +1096,8 @@ function ensureThinkingBlocks(messages) {
|
|
|
814
1096
|
const content = msg.content;
|
|
815
1097
|
if (!Array.isArray(content))
|
|
816
1098
|
continue;
|
|
817
|
-
// 如果第一个 block 不是 thinking,在前面插入占位 thinking
|
|
818
1099
|
if (content.length === 0 || content[0].type !== 'thinking') {
|
|
819
|
-
|
|
820
|
-
content.unshift({ type: 'thinking', thinking: placeholder, signature: makeSignature(placeholder) });
|
|
1100
|
+
content.unshift({ type: 'thinking', thinking: '', signature: '' });
|
|
821
1101
|
}
|
|
822
1102
|
}
|
|
823
1103
|
}
|
|
@@ -865,6 +1145,99 @@ function injectThinkingConfig(upstreamBody, route) {
|
|
|
865
1145
|
}
|
|
866
1146
|
}
|
|
867
1147
|
}
|
|
1148
|
+
// --- Action mapping helpers (Computer Use) ---
|
|
1149
|
+
/** OpenAI ComputerAction → Anthropic tool_use input */
|
|
1150
|
+
export function convertActionToAnthropic(action) {
|
|
1151
|
+
const type = action.type;
|
|
1152
|
+
const result = { action: type };
|
|
1153
|
+
switch (type) {
|
|
1154
|
+
case 'click':
|
|
1155
|
+
case 'double_click':
|
|
1156
|
+
case 'drag':
|
|
1157
|
+
result.coordinate = [action.x, action.y];
|
|
1158
|
+
break;
|
|
1159
|
+
case 'move':
|
|
1160
|
+
result.action = 'mouse_move';
|
|
1161
|
+
result.coordinate = [action.x, action.y];
|
|
1162
|
+
break;
|
|
1163
|
+
case 'keypress':
|
|
1164
|
+
result.action = 'key';
|
|
1165
|
+
const keys = action.keys;
|
|
1166
|
+
result.text = keys ? keys.join('') : '';
|
|
1167
|
+
break;
|
|
1168
|
+
case 'scroll':
|
|
1169
|
+
result.coordinate = [action.x, action.y];
|
|
1170
|
+
result.scroll_x = action.scroll_x;
|
|
1171
|
+
result.scroll_y = action.scroll_y;
|
|
1172
|
+
break;
|
|
1173
|
+
case 'type':
|
|
1174
|
+
result.text = action.text ?? '';
|
|
1175
|
+
break;
|
|
1176
|
+
case 'wait':
|
|
1177
|
+
result.action = 'wait';
|
|
1178
|
+
result.duration = action.ms ?? action.duration ?? 0;
|
|
1179
|
+
break;
|
|
1180
|
+
case 'screenshot':
|
|
1181
|
+
break;
|
|
1182
|
+
default:
|
|
1183
|
+
// Pass through unknown actions as-is
|
|
1184
|
+
Object.assign(result, action);
|
|
1185
|
+
}
|
|
1186
|
+
return result;
|
|
1187
|
+
}
|
|
1188
|
+
/** Anthropic tool_use input → OpenAI ComputerAction */
|
|
1189
|
+
export function convertActionToOpenAI(input) {
|
|
1190
|
+
const action = input.action;
|
|
1191
|
+
const coord = input.coordinate;
|
|
1192
|
+
const result = {};
|
|
1193
|
+
switch (action) {
|
|
1194
|
+
case 'click':
|
|
1195
|
+
case 'double_click':
|
|
1196
|
+
case 'drag':
|
|
1197
|
+
result.type = action;
|
|
1198
|
+
if (coord) {
|
|
1199
|
+
result.x = coord[0];
|
|
1200
|
+
result.y = coord[1];
|
|
1201
|
+
}
|
|
1202
|
+
break;
|
|
1203
|
+
case 'mouse_move':
|
|
1204
|
+
result.type = 'move';
|
|
1205
|
+
if (coord) {
|
|
1206
|
+
result.x = coord[0];
|
|
1207
|
+
result.y = coord[1];
|
|
1208
|
+
}
|
|
1209
|
+
break;
|
|
1210
|
+
case 'key':
|
|
1211
|
+
result.type = 'keypress';
|
|
1212
|
+
result.keys = [input.text ?? ''];
|
|
1213
|
+
break;
|
|
1214
|
+
case 'scroll':
|
|
1215
|
+
result.type = 'scroll';
|
|
1216
|
+
if (coord) {
|
|
1217
|
+
result.x = coord[0];
|
|
1218
|
+
result.y = coord[1];
|
|
1219
|
+
}
|
|
1220
|
+
result.scroll_x = input.scroll_x;
|
|
1221
|
+
result.scroll_y = input.scroll_y;
|
|
1222
|
+
break;
|
|
1223
|
+
case 'type':
|
|
1224
|
+
result.type = 'type';
|
|
1225
|
+
result.text = input.text ?? '';
|
|
1226
|
+
break;
|
|
1227
|
+
case 'wait':
|
|
1228
|
+
result.type = 'wait';
|
|
1229
|
+
result.ms = input.duration ?? 0;
|
|
1230
|
+
break;
|
|
1231
|
+
case 'screenshot':
|
|
1232
|
+
result.type = 'screenshot';
|
|
1233
|
+
break;
|
|
1234
|
+
default:
|
|
1235
|
+
// Pass through unknown actions as-is
|
|
1236
|
+
result.type = action;
|
|
1237
|
+
Object.assign(result, input);
|
|
1238
|
+
}
|
|
1239
|
+
return result;
|
|
1240
|
+
}
|
|
868
1241
|
// --- OpenAI Responses ↔ Anthropic response conversion ---
|
|
869
1242
|
function mapResponsesStopReason(stop, hasToolCalls) {
|
|
870
1243
|
if (hasToolCalls)
|
|
@@ -923,6 +1296,31 @@ export function convertOpenAIResponsesToAnthropic(responsesBody) {
|
|
|
923
1296
|
});
|
|
924
1297
|
stopReason = 'tool_use';
|
|
925
1298
|
}
|
|
1299
|
+
else if (item.type === 'computer_call') {
|
|
1300
|
+
// OpenAI computer_call → Anthropic tool_use with name "computer"
|
|
1301
|
+
const action = item.action;
|
|
1302
|
+
content.push({
|
|
1303
|
+
type: 'tool_use',
|
|
1304
|
+
id: item.call_id ?? item.id,
|
|
1305
|
+
name: 'computer',
|
|
1306
|
+
input: action ? convertActionToAnthropic(action) : {},
|
|
1307
|
+
});
|
|
1308
|
+
stopReason = 'tool_use';
|
|
1309
|
+
}
|
|
1310
|
+
else if (['web_search_call', 'code_interpreter_call', 'file_search_call'].includes(item.type)) {
|
|
1311
|
+
// 忽略无 Anthropic 对应物的内置工具输出
|
|
1312
|
+
continue;
|
|
1313
|
+
}
|
|
1314
|
+
else if (item.type === 'reasoning') {
|
|
1315
|
+
// Standalone reasoning output item → thinking block
|
|
1316
|
+
const summary = item.summary;
|
|
1317
|
+
if (summary) {
|
|
1318
|
+
const reasonText = summary.map((s) => s.text ?? '').join('');
|
|
1319
|
+
if (reasonText) {
|
|
1320
|
+
content.push({ type: 'thinking', thinking: reasonText, signature: '' });
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
926
1324
|
}
|
|
927
1325
|
}
|
|
928
1326
|
const hasToolCalls = content.some((b) => b.type === 'tool_use');
|
|
@@ -958,24 +1356,45 @@ export function convertAnthropicResponseToOpenAIResponses(anthropicBody) {
|
|
|
958
1356
|
thinkingText += block.thinking ?? '';
|
|
959
1357
|
}
|
|
960
1358
|
else if (block.type === 'tool_use') {
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1359
|
+
const name = block.name ?? '';
|
|
1360
|
+
if (name === 'computer') {
|
|
1361
|
+
// Computer use tool → computer_call output item
|
|
1362
|
+
const input = block.input ?? {};
|
|
1363
|
+
output.push({
|
|
1364
|
+
type: 'computer_call',
|
|
1365
|
+
id: `cc_${Date.now().toString(36)}_${block.id ?? ''}`,
|
|
1366
|
+
call_id: block.id,
|
|
1367
|
+
action: convertActionToOpenAI(input),
|
|
1368
|
+
pending_safety_checks: [],
|
|
1369
|
+
status: 'completed',
|
|
1370
|
+
});
|
|
1371
|
+
}
|
|
1372
|
+
else {
|
|
1373
|
+
// Regular tool_use → function_call
|
|
1374
|
+
// CCX always sets status: "completed" on function_call items
|
|
1375
|
+
output.push({
|
|
1376
|
+
type: 'function_call',
|
|
1377
|
+
id: `fc_${Date.now().toString(36)}_${block.id ?? ''}`,
|
|
1378
|
+
call_id: block.id,
|
|
1379
|
+
name,
|
|
1380
|
+
arguments: JSON.stringify(block.input ?? {}),
|
|
1381
|
+
status: 'completed',
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
968
1384
|
}
|
|
969
1385
|
}
|
|
970
1386
|
}
|
|
971
|
-
// Message output item goes first (before function_call items
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1387
|
+
// Message output item goes first (before function_call/computer_call items)
|
|
1388
|
+
// CCX compatibility: only add message item when there's actual content
|
|
1389
|
+
if (outputMessageContent.length > 0) {
|
|
1390
|
+
output.unshift({
|
|
1391
|
+
type: 'message',
|
|
1392
|
+
id: `msg_${Date.now().toString(36)}`,
|
|
1393
|
+
status: stopReason === 'end_turn' ? 'completed' : stopReason === 'max_tokens' ? 'incomplete' : 'completed',
|
|
1394
|
+
role: 'assistant',
|
|
1395
|
+
content: outputMessageContent,
|
|
1396
|
+
});
|
|
1397
|
+
}
|
|
979
1398
|
return {
|
|
980
1399
|
id: `resp_${Date.now().toString(36)}`,
|
|
981
1400
|
object: 'response',
|
|
@@ -1154,6 +1573,7 @@ export function convertOpenAIResponseToOpenAIResponses(chatBody) {
|
|
|
1154
1573
|
call_id: tc.id,
|
|
1155
1574
|
name: tc.function?.name ?? '',
|
|
1156
1575
|
arguments: tc.function?.arguments ?? '',
|
|
1576
|
+
status: 'completed',
|
|
1157
1577
|
});
|
|
1158
1578
|
}
|
|
1159
1579
|
}
|
|
@@ -1219,6 +1639,22 @@ export function convertOpenAIResponsesResponseToOpenAI(responsesBody) {
|
|
|
1219
1639
|
},
|
|
1220
1640
|
});
|
|
1221
1641
|
}
|
|
1642
|
+
else if (item.type === 'computer_call') {
|
|
1643
|
+
// Computer call → Chat tool_calls (lossy conversion: serialize action as arguments)
|
|
1644
|
+
const action = item.action;
|
|
1645
|
+
toolCalls.push({
|
|
1646
|
+
id: item.call_id ?? item.id,
|
|
1647
|
+
type: 'function',
|
|
1648
|
+
function: {
|
|
1649
|
+
name: 'computer',
|
|
1650
|
+
arguments: JSON.stringify(action ?? {}),
|
|
1651
|
+
},
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
else if (['web_search_call', 'code_interpreter_call', 'file_search_call'].includes(item.type)) {
|
|
1655
|
+
// 无 Chat 对应物 → 跳过
|
|
1656
|
+
continue;
|
|
1657
|
+
}
|
|
1222
1658
|
}
|
|
1223
1659
|
}
|
|
1224
1660
|
const finishMap = { completed: 'stop', incomplete: 'length' };
|