@zenmux/rosetta-ai 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,13 +3,57 @@ type RespResponse = OpenAI.Responses.Response;
|
|
|
3
3
|
type RespStreamEvent = OpenAI.Responses.ResponseStreamEvent;
|
|
4
4
|
export declare class ResponsesToChatCompletionConverter {
|
|
5
5
|
private streamState;
|
|
6
|
+
/**
|
|
7
|
+
* The original Responses request params, stashed during `convertRequest` so
|
|
8
|
+
* the streaming terminal event can echo request-scoped fields onto the
|
|
9
|
+
* `response.completed` / `response.incomplete` payload. Only populated when
|
|
10
|
+
* `convertRequest` ran before streaming (the normal gateway flow).
|
|
11
|
+
*/
|
|
12
|
+
private requestParams?;
|
|
6
13
|
constructor();
|
|
7
14
|
convertRequest(params: OpenAI.Responses.ResponseCreateParams): OpenAI.Chat.Completions.ChatCompletionCreateParams;
|
|
8
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Convert a ChatCompletion response back to a Responses Response.
|
|
17
|
+
*
|
|
18
|
+
* @param response The upstream ChatCompletion.
|
|
19
|
+
* @param params Optional original Responses request params. When provided,
|
|
20
|
+
* request-scoped fields (instructions, parallel_tool_calls, tool_choice,
|
|
21
|
+
* tools, reasoning, temperature, text, etc.) are echoed back onto the
|
|
22
|
+
* response — matching the behavior of a gateway that round-trips the
|
|
23
|
+
* request through the Responses API. When omitted, these fields fall back
|
|
24
|
+
* to protocol defaults.
|
|
25
|
+
*/
|
|
26
|
+
convertResponse(response: OpenAI.ChatCompletion, params?: OpenAI.Responses.ResponseCreateParams): RespResponse;
|
|
27
|
+
/**
|
|
28
|
+
* Compute the request-scoped field values to echo onto a converted response,
|
|
29
|
+
* mirroring the legacy `toOpenAIRespResponse(params, result)` mapping. Returns
|
|
30
|
+
* protocol defaults when `params` is absent.
|
|
31
|
+
*/
|
|
32
|
+
private echoRequestFields;
|
|
33
|
+
/**
|
|
34
|
+
* Map a Responses request tool back to a Responses response tool, preserving
|
|
35
|
+
* function / web_search / custom tools. Mirrors the legacy `toRespTool`.
|
|
36
|
+
*/
|
|
37
|
+
private toRespTool;
|
|
9
38
|
convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk>): AsyncIterable<RespStreamEvent>;
|
|
10
39
|
convertStreamChunk(chunk: OpenAI.ChatCompletionChunk): RespStreamEvent[];
|
|
40
|
+
/** Open a message output item and its first content part. */
|
|
41
|
+
private openMessage;
|
|
42
|
+
/**
|
|
43
|
+
* Close the currently open output item, emitting the `*.done` and
|
|
44
|
+
* `response.output_item.done` events. Mirrors the legacy converter's
|
|
45
|
+
* stopReasoning / stopOutputTextRefusal / stopFunctionCallArguments /
|
|
46
|
+
* stopCustomToolCallInput helpers.
|
|
47
|
+
*/
|
|
48
|
+
private closeCurrent;
|
|
11
49
|
private convertInput;
|
|
12
50
|
private convertInputContent;
|
|
51
|
+
/**
|
|
52
|
+
* Normalize a Responses tool-call output payload to a CC tool message
|
|
53
|
+
* content. A string passes through; an array of input parts is reduced to
|
|
54
|
+
* its `input_text` items as CC text parts. Mirrors the legacy `toToolContent`.
|
|
55
|
+
*/
|
|
56
|
+
private toToolContent;
|
|
13
57
|
private convertTools;
|
|
14
58
|
private isWebSearch;
|
|
15
59
|
private convertToolChoice;
|
|
@@ -3,11 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ResponsesToChatCompletionConverter = void 0;
|
|
4
4
|
class ResponsesToChatCompletionConverter {
|
|
5
5
|
streamState;
|
|
6
|
+
/**
|
|
7
|
+
* The original Responses request params, stashed during `convertRequest` so
|
|
8
|
+
* the streaming terminal event can echo request-scoped fields onto the
|
|
9
|
+
* `response.completed` / `response.incomplete` payload. Only populated when
|
|
10
|
+
* `convertRequest` ran before streaming (the normal gateway flow).
|
|
11
|
+
*/
|
|
12
|
+
requestParams;
|
|
6
13
|
constructor() {
|
|
7
14
|
this.streamState = this.createStreamState();
|
|
8
15
|
}
|
|
9
16
|
// --- Request conversion (Responses → CC, forward) ---
|
|
10
17
|
convertRequest(params) {
|
|
18
|
+
this.requestParams = params;
|
|
11
19
|
const messages = [];
|
|
12
20
|
if (params.instructions) {
|
|
13
21
|
messages.push({ role: "system", content: params.instructions });
|
|
@@ -79,10 +87,27 @@ class ResponsesToChatCompletionConverter {
|
|
|
79
87
|
result.stream_options = params.stream_options;
|
|
80
88
|
}
|
|
81
89
|
}
|
|
90
|
+
else if (params.stream_options?.include_obfuscation != null) {
|
|
91
|
+
// include_obfuscation is meaningful even for non-streaming Responses
|
|
92
|
+
// requests (it governs how the gateway streams back to the client).
|
|
93
|
+
// Forward it so it is not dropped when params.stream is falsy.
|
|
94
|
+
result.stream_options = { include_obfuscation: params.stream_options.include_obfuscation };
|
|
95
|
+
}
|
|
82
96
|
return result;
|
|
83
97
|
}
|
|
84
98
|
// --- Response conversion (CC → Responses, backward) ---
|
|
85
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Convert a ChatCompletion response back to a Responses Response.
|
|
101
|
+
*
|
|
102
|
+
* @param response The upstream ChatCompletion.
|
|
103
|
+
* @param params Optional original Responses request params. When provided,
|
|
104
|
+
* request-scoped fields (instructions, parallel_tool_calls, tool_choice,
|
|
105
|
+
* tools, reasoning, temperature, text, etc.) are echoed back onto the
|
|
106
|
+
* response — matching the behavior of a gateway that round-trips the
|
|
107
|
+
* request through the Responses API. When omitted, these fields fall back
|
|
108
|
+
* to protocol defaults.
|
|
109
|
+
*/
|
|
110
|
+
convertResponse(response, params) {
|
|
86
111
|
const choice = response.choices[0];
|
|
87
112
|
const msg = choice?.message;
|
|
88
113
|
const output = [];
|
|
@@ -142,6 +167,10 @@ class ResponsesToChatCompletionConverter {
|
|
|
142
167
|
});
|
|
143
168
|
}
|
|
144
169
|
const status = this.finishReasonToStatus(choice?.finish_reason);
|
|
170
|
+
// Request-scoped fields echoed back onto the response. When `params` is
|
|
171
|
+
// absent, fall back to protocol defaults so behavior matches a gateway
|
|
172
|
+
// that does not round-trip the request.
|
|
173
|
+
const echoed = this.echoRequestFields(params);
|
|
145
174
|
return {
|
|
146
175
|
id: response.id,
|
|
147
176
|
object: "response",
|
|
@@ -151,19 +180,35 @@ class ResponsesToChatCompletionConverter {
|
|
|
151
180
|
status,
|
|
152
181
|
error: null,
|
|
153
182
|
incomplete_details: status === "incomplete" ? { reason: "max_output_tokens" } : null,
|
|
154
|
-
instructions:
|
|
183
|
+
instructions: echoed.instructions,
|
|
155
184
|
metadata: {},
|
|
156
|
-
temperature:
|
|
157
|
-
top_p:
|
|
158
|
-
max_output_tokens:
|
|
159
|
-
previous_response_id:
|
|
160
|
-
parallel_tool_calls:
|
|
161
|
-
tool_choice:
|
|
162
|
-
tools:
|
|
163
|
-
text:
|
|
164
|
-
reasoning:
|
|
165
|
-
truncation:
|
|
185
|
+
temperature: echoed.temperature,
|
|
186
|
+
top_p: echoed.top_p,
|
|
187
|
+
max_output_tokens: echoed.max_output_tokens,
|
|
188
|
+
previous_response_id: echoed.previous_response_id,
|
|
189
|
+
parallel_tool_calls: echoed.parallel_tool_calls,
|
|
190
|
+
tool_choice: echoed.tool_choice,
|
|
191
|
+
tools: echoed.tools,
|
|
192
|
+
text: echoed.text,
|
|
193
|
+
reasoning: echoed.reasoning,
|
|
194
|
+
truncation: echoed.truncation,
|
|
166
195
|
user: undefined,
|
|
196
|
+
// Non-standard echoes (top_logprobs / safety_identifier / service_tier)
|
|
197
|
+
// are attached via cast; the Responses Response type does not declare them.
|
|
198
|
+
...(echoed.top_logprobs !== undefined
|
|
199
|
+
? { top_logprobs: echoed.top_logprobs }
|
|
200
|
+
: {}),
|
|
201
|
+
...(echoed.safety_identifier !== undefined
|
|
202
|
+
? { safety_identifier: echoed.safety_identifier }
|
|
203
|
+
: {}),
|
|
204
|
+
...(echoed.service_tier !== undefined ? { service_tier: echoed.service_tier } : {}),
|
|
205
|
+
...(params?.background != null ? { background: params.background } : {}),
|
|
206
|
+
...(params?.prompt_cache_key !== undefined
|
|
207
|
+
? { prompt_cache_key: params.prompt_cache_key }
|
|
208
|
+
: {}),
|
|
209
|
+
...(params?.prompt_cache_retention !== undefined
|
|
210
|
+
? { prompt_cache_retention: params.prompt_cache_retention }
|
|
211
|
+
: {}),
|
|
167
212
|
usage: response.usage
|
|
168
213
|
? {
|
|
169
214
|
input_tokens: response.usage.prompt_tokens,
|
|
@@ -179,6 +224,80 @@ class ResponsesToChatCompletionConverter {
|
|
|
179
224
|
: undefined,
|
|
180
225
|
};
|
|
181
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Compute the request-scoped field values to echo onto a converted response,
|
|
229
|
+
* mirroring the legacy `toOpenAIRespResponse(params, result)` mapping. Returns
|
|
230
|
+
* protocol defaults when `params` is absent.
|
|
231
|
+
*/
|
|
232
|
+
echoRequestFields(params) {
|
|
233
|
+
if (!params) {
|
|
234
|
+
return {
|
|
235
|
+
instructions: null,
|
|
236
|
+
temperature: null,
|
|
237
|
+
top_p: null,
|
|
238
|
+
max_output_tokens: null,
|
|
239
|
+
previous_response_id: null,
|
|
240
|
+
parallel_tool_calls: true,
|
|
241
|
+
tool_choice: "auto",
|
|
242
|
+
tools: [],
|
|
243
|
+
text: { format: { type: "text" } },
|
|
244
|
+
reasoning: null,
|
|
245
|
+
truncation: null,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
const tools = (params.tools ?? [])
|
|
249
|
+
.map(t => this.toRespTool(t))
|
|
250
|
+
.filter(Boolean);
|
|
251
|
+
return {
|
|
252
|
+
instructions: params.instructions ?? null,
|
|
253
|
+
temperature: params.temperature ?? null,
|
|
254
|
+
top_p: params.top_p ?? null,
|
|
255
|
+
max_output_tokens: params.max_output_tokens ?? null,
|
|
256
|
+
previous_response_id: params.previous_response_id ?? null,
|
|
257
|
+
parallel_tool_calls: params.parallel_tool_calls ?? true,
|
|
258
|
+
tool_choice: params.tool_choice ?? "auto",
|
|
259
|
+
tools,
|
|
260
|
+
text: params.text ?? { format: { type: "text" } },
|
|
261
|
+
reasoning: params.reasoning ?? null,
|
|
262
|
+
truncation: params.truncation ?? null,
|
|
263
|
+
top_logprobs: params.top_logprobs,
|
|
264
|
+
safety_identifier: params.safety_identifier,
|
|
265
|
+
// service_tier is normally returned by the upstream model; echo the
|
|
266
|
+
// request's value as a fallback (the legacy converter did the same).
|
|
267
|
+
service_tier: params.service_tier,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Map a Responses request tool back to a Responses response tool, preserving
|
|
272
|
+
* function / web_search / custom tools. Mirrors the legacy `toRespTool`.
|
|
273
|
+
*/
|
|
274
|
+
toRespTool(tool) {
|
|
275
|
+
const t = tool;
|
|
276
|
+
if (t.type === "function") {
|
|
277
|
+
return {
|
|
278
|
+
type: "function",
|
|
279
|
+
name: t.name,
|
|
280
|
+
description: t.description,
|
|
281
|
+
parameters: !t.parameters
|
|
282
|
+
? { additionalProperties: false, type: "object", properties: {}, required: [] }
|
|
283
|
+
: {
|
|
284
|
+
additionalProperties: t.parameters.additionalProperties ?? false,
|
|
285
|
+
type: t.parameters.type ?? "object",
|
|
286
|
+
properties: t.parameters.properties ?? {},
|
|
287
|
+
required: t.parameters.required ?? [],
|
|
288
|
+
},
|
|
289
|
+
strict: t.strict,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
if (t.type === "web_search" ||
|
|
293
|
+
t.type === "web_search_preview" ||
|
|
294
|
+
t.type === "web_search_2025_08_26" ||
|
|
295
|
+
t.type === "web_search_preview_2025_03_11" ||
|
|
296
|
+
t.type === "custom") {
|
|
297
|
+
return t;
|
|
298
|
+
}
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
182
301
|
// --- Stream conversion (CC → Responses, backward) ---
|
|
183
302
|
async *convertStream(stream) {
|
|
184
303
|
for await (const chunk of stream) {
|
|
@@ -217,111 +336,216 @@ class ResponsesToChatCompletionConverter {
|
|
|
217
336
|
return events;
|
|
218
337
|
}
|
|
219
338
|
const delta = choice.delta;
|
|
220
|
-
// Reasoning delta
|
|
339
|
+
// Reasoning delta — open a reasoning item if not already open.
|
|
221
340
|
const reasoning = delta?.reasoning || delta?.reasoning_content;
|
|
222
341
|
if (reasoning) {
|
|
223
|
-
if (!state.
|
|
224
|
-
|
|
342
|
+
if (!state.current || state.current.type !== "reasoning") {
|
|
343
|
+
// Close any other open item before starting reasoning.
|
|
344
|
+
events.push(...this.closeCurrent());
|
|
345
|
+
const itemId = `rs_${this.generateId()}`;
|
|
346
|
+
state.current = {
|
|
347
|
+
type: "reasoning",
|
|
348
|
+
itemId,
|
|
349
|
+
outputIndex: state.outputIndex,
|
|
350
|
+
summaryIndex: 0,
|
|
351
|
+
text: "",
|
|
352
|
+
};
|
|
225
353
|
events.push({
|
|
226
354
|
type: "response.output_item.added",
|
|
227
|
-
item: {
|
|
228
|
-
type: "reasoning",
|
|
229
|
-
id: `rs_${this.generateId()}`,
|
|
230
|
-
summary: [],
|
|
231
|
-
},
|
|
355
|
+
item: { type: "reasoning", id: itemId, summary: [] },
|
|
232
356
|
output_index: state.outputIndex,
|
|
233
357
|
sequence_number: state.seq++,
|
|
234
358
|
});
|
|
235
359
|
events.push({
|
|
236
360
|
type: "response.reasoning_summary_part.added",
|
|
237
|
-
item_id:
|
|
361
|
+
item_id: itemId,
|
|
238
362
|
output_index: state.outputIndex,
|
|
239
363
|
summary_index: 0,
|
|
240
364
|
part: { type: "summary_text", text: "" },
|
|
241
365
|
sequence_number: state.seq++,
|
|
242
366
|
});
|
|
243
367
|
}
|
|
368
|
+
state.current.text += reasoning;
|
|
244
369
|
events.push({
|
|
245
370
|
type: "response.reasoning_summary_text.delta",
|
|
246
|
-
item_id:
|
|
371
|
+
item_id: state.current.itemId,
|
|
247
372
|
output_index: state.outputIndex,
|
|
248
373
|
summary_index: 0,
|
|
249
374
|
delta: reasoning,
|
|
250
375
|
sequence_number: state.seq++,
|
|
251
376
|
});
|
|
252
377
|
}
|
|
253
|
-
// Tool
|
|
378
|
+
// Tool calls — function or custom.
|
|
254
379
|
if (delta?.tool_calls) {
|
|
255
380
|
for (const tc of delta.tool_calls) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
381
|
+
const isCustom = tc.type === "custom" || tc.custom;
|
|
382
|
+
// A new tool call begins when an id + name arrive.
|
|
383
|
+
if (tc.id && ((tc.function?.name && !isCustom) || (tc.custom?.name && isCustom))) {
|
|
384
|
+
const callId = tc.id;
|
|
385
|
+
const name = isCustom ? tc.custom.name : tc.function.name;
|
|
386
|
+
// Close any open reasoning/message item before starting a tool call.
|
|
387
|
+
if (state.current &&
|
|
388
|
+
state.current.type !== "function_call" &&
|
|
389
|
+
state.current.type !== "custom_tool_call") {
|
|
390
|
+
events.push(...this.closeCurrent());
|
|
391
|
+
}
|
|
392
|
+
// If a tool call of a different type/id is already open, close it.
|
|
393
|
+
if (state.current &&
|
|
394
|
+
((state.current.type === "function_call" && isCustom) ||
|
|
395
|
+
(state.current.type === "custom_tool_call" && !isCustom) ||
|
|
396
|
+
(state.current.type !== "custom_tool_call" && state.current.type !== "function_call"))) {
|
|
397
|
+
events.push(...this.closeCurrent());
|
|
398
|
+
}
|
|
399
|
+
if (state.current &&
|
|
400
|
+
(state.current.type === "function_call" || state.current.type === "custom_tool_call") &&
|
|
401
|
+
state.current.callId !== callId) {
|
|
402
|
+
events.push(...this.closeCurrent());
|
|
259
403
|
}
|
|
260
|
-
state.toolCallStarted = true;
|
|
261
404
|
state.toolCallCount++;
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
405
|
+
// Tool calls live at output_index = outputIndex + toolCallCount - 1,
|
|
406
|
+
// matching the legacy converter's output_index math.
|
|
407
|
+
const outputIndex = state.outputIndex + state.toolCallCount - 1;
|
|
408
|
+
if (isCustom) {
|
|
409
|
+
const itemId = `ct_${this.generateId()}`;
|
|
410
|
+
state.current = {
|
|
411
|
+
type: "custom_tool_call",
|
|
412
|
+
itemId,
|
|
413
|
+
outputIndex,
|
|
414
|
+
callId,
|
|
415
|
+
name,
|
|
416
|
+
input: "",
|
|
417
|
+
};
|
|
418
|
+
events.push({
|
|
419
|
+
type: "response.output_item.added",
|
|
420
|
+
item: {
|
|
421
|
+
type: "custom_tool_call",
|
|
422
|
+
id: itemId,
|
|
423
|
+
call_id: callId,
|
|
424
|
+
name,
|
|
425
|
+
input: "",
|
|
426
|
+
status: "in_progress",
|
|
427
|
+
},
|
|
428
|
+
output_index: outputIndex,
|
|
429
|
+
sequence_number: state.seq++,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
const itemId = `fc_${this.generateId()}`;
|
|
434
|
+
state.current = {
|
|
265
435
|
type: "function_call",
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
436
|
+
itemId,
|
|
437
|
+
outputIndex,
|
|
438
|
+
callId,
|
|
439
|
+
name,
|
|
269
440
|
arguments: "",
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
441
|
+
};
|
|
442
|
+
events.push({
|
|
443
|
+
type: "response.output_item.added",
|
|
444
|
+
item: {
|
|
445
|
+
type: "function_call",
|
|
446
|
+
id: itemId,
|
|
447
|
+
call_id: callId,
|
|
448
|
+
name,
|
|
449
|
+
arguments: "",
|
|
450
|
+
status: "in_progress",
|
|
451
|
+
},
|
|
452
|
+
output_index: outputIndex,
|
|
453
|
+
sequence_number: state.seq++,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
275
456
|
}
|
|
276
|
-
//
|
|
277
|
-
if (tc.function?.arguments) {
|
|
457
|
+
// Arguments/input delta.
|
|
458
|
+
if (state.current?.type === "function_call" && tc.function?.arguments) {
|
|
459
|
+
state.current.arguments += tc.function.arguments;
|
|
278
460
|
events.push({
|
|
279
461
|
type: "response.function_call_arguments.delta",
|
|
280
|
-
item_id:
|
|
281
|
-
output_index: state.outputIndex
|
|
462
|
+
item_id: state.current.itemId,
|
|
463
|
+
output_index: state.current.outputIndex,
|
|
282
464
|
delta: tc.function.arguments,
|
|
283
465
|
sequence_number: state.seq++,
|
|
284
466
|
});
|
|
285
467
|
}
|
|
468
|
+
else if (state.current?.type === "custom_tool_call") {
|
|
469
|
+
const inputDelta = tc.custom?.input ?? tc.function?.arguments ?? "";
|
|
470
|
+
if (inputDelta) {
|
|
471
|
+
state.current.input += inputDelta;
|
|
472
|
+
events.push({
|
|
473
|
+
type: "response.custom_tool_call_input.delta",
|
|
474
|
+
item_id: state.current.itemId,
|
|
475
|
+
output_index: state.current.outputIndex,
|
|
476
|
+
delta: inputDelta,
|
|
477
|
+
sequence_number: state.seq++,
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
}
|
|
286
481
|
}
|
|
287
482
|
}
|
|
288
|
-
//
|
|
483
|
+
// Refusal delta.
|
|
484
|
+
const refusalDelta = delta?.refusal;
|
|
485
|
+
if (refusalDelta) {
|
|
486
|
+
if (!state.current || state.current.type !== "message" || state.current.kind !== "refusal") {
|
|
487
|
+
events.push(...this.closeCurrent());
|
|
488
|
+
this.openMessage(state, "refusal", events);
|
|
489
|
+
}
|
|
490
|
+
const msgCtx = state.current;
|
|
491
|
+
msgCtx.refusal += refusalDelta;
|
|
492
|
+
events.push({
|
|
493
|
+
type: "response.refusal.delta",
|
|
494
|
+
item_id: msgCtx.itemId,
|
|
495
|
+
output_index: msgCtx.outputIndex,
|
|
496
|
+
content_index: msgCtx.contentIndex,
|
|
497
|
+
delta: refusalDelta,
|
|
498
|
+
sequence_number: state.seq++,
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
// Text content delta.
|
|
289
502
|
if (delta?.content && delta.content !== "") {
|
|
290
|
-
if (!state.
|
|
291
|
-
state.
|
|
292
|
-
|
|
293
|
-
events.push(
|
|
294
|
-
|
|
295
|
-
item: {
|
|
296
|
-
type: "message",
|
|
297
|
-
id: `msg_${this.generateId()}`,
|
|
298
|
-
role: "assistant",
|
|
299
|
-
status: "in_progress",
|
|
300
|
-
content: [],
|
|
301
|
-
},
|
|
302
|
-
output_index: msgOutputIndex,
|
|
303
|
-
sequence_number: state.seq++,
|
|
304
|
-
});
|
|
305
|
-
events.push({
|
|
306
|
-
type: "response.content_part.added",
|
|
307
|
-
item_id: `msg_${this.generateId()}`,
|
|
308
|
-
output_index: msgOutputIndex,
|
|
309
|
-
content_index: 0,
|
|
310
|
-
part: { type: "output_text", text: "", annotations: [] },
|
|
311
|
-
sequence_number: state.seq++,
|
|
312
|
-
});
|
|
503
|
+
if (!state.current ||
|
|
504
|
+
state.current.type !== "message" ||
|
|
505
|
+
state.current.kind !== "output_text") {
|
|
506
|
+
events.push(...this.closeCurrent());
|
|
507
|
+
this.openMessage(state, "output_text", events);
|
|
313
508
|
}
|
|
509
|
+
state.current.outputText += delta.content;
|
|
314
510
|
events.push({
|
|
315
511
|
type: "response.output_text.delta",
|
|
316
|
-
item_id:
|
|
317
|
-
output_index: state.outputIndex
|
|
318
|
-
content_index:
|
|
512
|
+
item_id: state.current.itemId,
|
|
513
|
+
output_index: state.current.outputIndex,
|
|
514
|
+
content_index: state.current.contentIndex,
|
|
319
515
|
delta: delta.content,
|
|
516
|
+
logprobs: [],
|
|
320
517
|
sequence_number: state.seq++,
|
|
321
518
|
});
|
|
322
519
|
}
|
|
323
|
-
//
|
|
520
|
+
// Annotations on the message delta.
|
|
521
|
+
const annotations = delta?.annotations;
|
|
522
|
+
if (annotations && state.current?.type === "message") {
|
|
523
|
+
for (const a of annotations) {
|
|
524
|
+
if (a?.type !== "url_citation" || !a.url_citation)
|
|
525
|
+
continue;
|
|
526
|
+
state.annotationIndex++;
|
|
527
|
+
const annotation = {
|
|
528
|
+
type: "url_citation",
|
|
529
|
+
url: a.url_citation.url,
|
|
530
|
+
title: a.url_citation.title,
|
|
531
|
+
start_index: a.url_citation.start_index,
|
|
532
|
+
end_index: a.url_citation.end_index,
|
|
533
|
+
};
|
|
534
|
+
state.current.annotations.push(annotation);
|
|
535
|
+
events.push({
|
|
536
|
+
type: "response.output_text.annotation.added",
|
|
537
|
+
item_id: state.current.itemId,
|
|
538
|
+
output_index: state.current.outputIndex,
|
|
539
|
+
content_index: state.current.contentIndex,
|
|
540
|
+
annotation_index: state.annotationIndex,
|
|
541
|
+
annotation,
|
|
542
|
+
sequence_number: state.seq++,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
// Finish reason — close the open item, then emit the terminal event.
|
|
324
547
|
if (choice.finish_reason) {
|
|
548
|
+
events.push(...this.closeCurrent());
|
|
325
549
|
const status = this.finishReasonToStatus(choice.finish_reason);
|
|
326
550
|
if (chunk.usage) {
|
|
327
551
|
events.push(...this.emitCompleted(chunk.usage, status));
|
|
@@ -332,6 +556,192 @@ class ResponsesToChatCompletionConverter {
|
|
|
332
556
|
}
|
|
333
557
|
return events;
|
|
334
558
|
}
|
|
559
|
+
/** Open a message output item and its first content part. */
|
|
560
|
+
openMessage(state, kind, events) {
|
|
561
|
+
state.messageStarted = true;
|
|
562
|
+
const itemId = `msg_${this.generateId()}`;
|
|
563
|
+
const msgOutputIndex = state.outputIndex + state.toolCallCount;
|
|
564
|
+
state.current = {
|
|
565
|
+
type: "message",
|
|
566
|
+
itemId,
|
|
567
|
+
outputIndex: msgOutputIndex,
|
|
568
|
+
contentIndex: state.contentIndex,
|
|
569
|
+
kind,
|
|
570
|
+
outputText: "",
|
|
571
|
+
refusal: "",
|
|
572
|
+
annotations: [],
|
|
573
|
+
};
|
|
574
|
+
events.push({
|
|
575
|
+
type: "response.output_item.added",
|
|
576
|
+
item: {
|
|
577
|
+
type: "message",
|
|
578
|
+
id: itemId,
|
|
579
|
+
role: "assistant",
|
|
580
|
+
status: "in_progress",
|
|
581
|
+
content: [],
|
|
582
|
+
},
|
|
583
|
+
output_index: msgOutputIndex,
|
|
584
|
+
sequence_number: state.seq++,
|
|
585
|
+
});
|
|
586
|
+
events.push({
|
|
587
|
+
type: "response.content_part.added",
|
|
588
|
+
item_id: itemId,
|
|
589
|
+
output_index: msgOutputIndex,
|
|
590
|
+
content_index: state.contentIndex,
|
|
591
|
+
part: kind === "output_text"
|
|
592
|
+
? { type: "output_text", text: "", annotations: [] }
|
|
593
|
+
: { type: "refusal", refusal: "" },
|
|
594
|
+
sequence_number: state.seq++,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Close the currently open output item, emitting the `*.done` and
|
|
599
|
+
* `response.output_item.done` events. Mirrors the legacy converter's
|
|
600
|
+
* stopReasoning / stopOutputTextRefusal / stopFunctionCallArguments /
|
|
601
|
+
* stopCustomToolCallInput helpers.
|
|
602
|
+
*/
|
|
603
|
+
closeCurrent() {
|
|
604
|
+
const state = this.streamState;
|
|
605
|
+
const ctx = state.current;
|
|
606
|
+
if (!ctx)
|
|
607
|
+
return [];
|
|
608
|
+
const events = [];
|
|
609
|
+
if (ctx.type === "reasoning") {
|
|
610
|
+
events.push({
|
|
611
|
+
type: "response.reasoning_summary_text.done",
|
|
612
|
+
item_id: ctx.itemId,
|
|
613
|
+
output_index: ctx.outputIndex,
|
|
614
|
+
summary_index: ctx.summaryIndex,
|
|
615
|
+
text: ctx.text,
|
|
616
|
+
sequence_number: state.seq++,
|
|
617
|
+
});
|
|
618
|
+
events.push({
|
|
619
|
+
type: "response.reasoning_summary_part.done",
|
|
620
|
+
item_id: ctx.itemId,
|
|
621
|
+
output_index: ctx.outputIndex,
|
|
622
|
+
summary_index: ctx.summaryIndex,
|
|
623
|
+
part: { type: "summary_text", text: ctx.text },
|
|
624
|
+
sequence_number: state.seq++,
|
|
625
|
+
});
|
|
626
|
+
const item = {
|
|
627
|
+
id: ctx.itemId,
|
|
628
|
+
type: "reasoning",
|
|
629
|
+
summary: [{ type: "summary_text", text: ctx.text }],
|
|
630
|
+
};
|
|
631
|
+
state.output.push(item);
|
|
632
|
+
events.push({
|
|
633
|
+
type: "response.output_item.done",
|
|
634
|
+
item,
|
|
635
|
+
output_index: ctx.outputIndex,
|
|
636
|
+
sequence_number: state.seq++,
|
|
637
|
+
});
|
|
638
|
+
state.outputIndex++;
|
|
639
|
+
}
|
|
640
|
+
else if (ctx.type === "message") {
|
|
641
|
+
const status = "completed";
|
|
642
|
+
const part = ctx.kind === "output_text"
|
|
643
|
+
? {
|
|
644
|
+
type: "output_text",
|
|
645
|
+
annotations: ctx.annotations,
|
|
646
|
+
logprobs: [],
|
|
647
|
+
text: ctx.outputText,
|
|
648
|
+
}
|
|
649
|
+
: { type: "refusal", refusal: ctx.refusal };
|
|
650
|
+
events.push(ctx.kind === "output_text"
|
|
651
|
+
? {
|
|
652
|
+
type: "response.output_text.done",
|
|
653
|
+
item_id: ctx.itemId,
|
|
654
|
+
output_index: ctx.outputIndex,
|
|
655
|
+
content_index: ctx.contentIndex,
|
|
656
|
+
text: ctx.outputText,
|
|
657
|
+
logprobs: [],
|
|
658
|
+
sequence_number: state.seq++,
|
|
659
|
+
}
|
|
660
|
+
: {
|
|
661
|
+
type: "response.refusal.done",
|
|
662
|
+
item_id: ctx.itemId,
|
|
663
|
+
output_index: ctx.outputIndex,
|
|
664
|
+
content_index: ctx.contentIndex,
|
|
665
|
+
refusal: ctx.refusal,
|
|
666
|
+
sequence_number: state.seq++,
|
|
667
|
+
});
|
|
668
|
+
events.push({
|
|
669
|
+
type: "response.content_part.done",
|
|
670
|
+
item_id: ctx.itemId,
|
|
671
|
+
output_index: ctx.outputIndex,
|
|
672
|
+
content_index: ctx.contentIndex,
|
|
673
|
+
part,
|
|
674
|
+
sequence_number: state.seq++,
|
|
675
|
+
});
|
|
676
|
+
const item = {
|
|
677
|
+
id: ctx.itemId,
|
|
678
|
+
type: "message",
|
|
679
|
+
role: "assistant",
|
|
680
|
+
status,
|
|
681
|
+
content: [part],
|
|
682
|
+
};
|
|
683
|
+
state.output.push(item);
|
|
684
|
+
events.push({
|
|
685
|
+
type: "response.output_item.done",
|
|
686
|
+
item,
|
|
687
|
+
output_index: ctx.outputIndex,
|
|
688
|
+
sequence_number: state.seq++,
|
|
689
|
+
});
|
|
690
|
+
state.outputIndex++;
|
|
691
|
+
}
|
|
692
|
+
else if (ctx.type === "function_call") {
|
|
693
|
+
events.push({
|
|
694
|
+
type: "response.function_call_arguments.done",
|
|
695
|
+
item_id: ctx.itemId,
|
|
696
|
+
output_index: ctx.outputIndex,
|
|
697
|
+
name: ctx.name,
|
|
698
|
+
arguments: ctx.arguments,
|
|
699
|
+
sequence_number: state.seq++,
|
|
700
|
+
});
|
|
701
|
+
const item = {
|
|
702
|
+
id: ctx.itemId,
|
|
703
|
+
type: "function_call",
|
|
704
|
+
status: "completed",
|
|
705
|
+
call_id: ctx.callId,
|
|
706
|
+
name: ctx.name,
|
|
707
|
+
arguments: ctx.arguments,
|
|
708
|
+
};
|
|
709
|
+
state.output.push(item);
|
|
710
|
+
events.push({
|
|
711
|
+
type: "response.output_item.done",
|
|
712
|
+
item,
|
|
713
|
+
output_index: ctx.outputIndex,
|
|
714
|
+
sequence_number: state.seq++,
|
|
715
|
+
});
|
|
716
|
+
state.outputIndex++;
|
|
717
|
+
}
|
|
718
|
+
else if (ctx.type === "custom_tool_call") {
|
|
719
|
+
events.push({
|
|
720
|
+
type: "response.custom_tool_call_input.done",
|
|
721
|
+
item_id: ctx.itemId,
|
|
722
|
+
output_index: ctx.outputIndex,
|
|
723
|
+
input: ctx.input,
|
|
724
|
+
sequence_number: state.seq++,
|
|
725
|
+
});
|
|
726
|
+
const item = {
|
|
727
|
+
id: ctx.itemId,
|
|
728
|
+
type: "custom_tool_call",
|
|
729
|
+
call_id: ctx.callId,
|
|
730
|
+
name: ctx.name,
|
|
731
|
+
input: ctx.input,
|
|
732
|
+
};
|
|
733
|
+
state.output.push(item);
|
|
734
|
+
events.push({
|
|
735
|
+
type: "response.output_item.done",
|
|
736
|
+
item,
|
|
737
|
+
output_index: ctx.outputIndex,
|
|
738
|
+
sequence_number: state.seq++,
|
|
739
|
+
});
|
|
740
|
+
state.outputIndex++;
|
|
741
|
+
}
|
|
742
|
+
state.current = null;
|
|
743
|
+
return events;
|
|
744
|
+
}
|
|
335
745
|
// --- Private: request helpers ---
|
|
336
746
|
convertInput(messages, input) {
|
|
337
747
|
if (typeof input === "string") {
|
|
@@ -418,7 +828,18 @@ class ResponsesToChatCompletionConverter {
|
|
|
418
828
|
messages.push({
|
|
419
829
|
role: "tool",
|
|
420
830
|
tool_call_id: typed.call_id,
|
|
421
|
-
content: typed.output,
|
|
831
|
+
content: this.toToolContent(typed.output),
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
else if (typed.type === "custom_tool_call_output") {
|
|
835
|
+
// A custom tool's output. There is no CC "custom tool" message role;
|
|
836
|
+
// surface it as a tool message keyed by the call id, matching the
|
|
837
|
+
// legacy converter's behavior.
|
|
838
|
+
flushToolCalls();
|
|
839
|
+
messages.push({
|
|
840
|
+
role: "tool",
|
|
841
|
+
tool_call_id: typed.call_id,
|
|
842
|
+
content: this.toToolContent(typed.output),
|
|
422
843
|
});
|
|
423
844
|
}
|
|
424
845
|
}
|
|
@@ -437,7 +858,11 @@ class ResponsesToChatCompletionConverter {
|
|
|
437
858
|
if (part.type === "input_image") {
|
|
438
859
|
return {
|
|
439
860
|
type: "image_url",
|
|
440
|
-
image_url: {
|
|
861
|
+
image_url: {
|
|
862
|
+
url: part.image_url || part.file_id || "",
|
|
863
|
+
// "original" is not a valid CC image_url.detail; omit it to use the default.
|
|
864
|
+
...(part.detail && part.detail !== "original" ? { detail: part.detail } : {}),
|
|
865
|
+
},
|
|
441
866
|
};
|
|
442
867
|
}
|
|
443
868
|
if (part.type === "input_file") {
|
|
@@ -446,13 +871,47 @@ class ResponsesToChatCompletionConverter {
|
|
|
446
871
|
file: {
|
|
447
872
|
file_data: part.file_data || part.file_url || "",
|
|
448
873
|
filename: part.filename,
|
|
449
|
-
file_id: part.file_id,
|
|
874
|
+
file_id: part.file_id || undefined,
|
|
450
875
|
},
|
|
451
876
|
};
|
|
452
877
|
}
|
|
878
|
+
if (part.type === "input_audio") {
|
|
879
|
+
return {
|
|
880
|
+
type: "input_audio",
|
|
881
|
+
input_audio: {
|
|
882
|
+
data: part.input_audio?.data,
|
|
883
|
+
format: part.input_audio?.format,
|
|
884
|
+
},
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
// Non-standard extension: ZenMux supports input_video on Responses user
|
|
888
|
+
// input. Map to a video_url content part (not in the SDK types).
|
|
889
|
+
if (part.type === "input_video") {
|
|
890
|
+
const v = part.input_video;
|
|
891
|
+
const url = v?.data ? `data:video/${v?.format ?? "mp4"};base64,${v.data}` : (v?.url ?? "");
|
|
892
|
+
return {
|
|
893
|
+
type: "video_url",
|
|
894
|
+
video_url: { url: url || null },
|
|
895
|
+
};
|
|
896
|
+
}
|
|
453
897
|
return { type: "text", text: "" };
|
|
454
898
|
});
|
|
455
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* Normalize a Responses tool-call output payload to a CC tool message
|
|
902
|
+
* content. A string passes through; an array of input parts is reduced to
|
|
903
|
+
* its `input_text` items as CC text parts. Mirrors the legacy `toToolContent`.
|
|
904
|
+
*/
|
|
905
|
+
toToolContent(output) {
|
|
906
|
+
if (typeof output === "string")
|
|
907
|
+
return output;
|
|
908
|
+
if (!Array.isArray(output))
|
|
909
|
+
return "";
|
|
910
|
+
const parts = output
|
|
911
|
+
.filter((item) => item.type === "input_text")
|
|
912
|
+
.map((item) => ({ type: "text", text: item.text }));
|
|
913
|
+
return parts.length ? parts : "";
|
|
914
|
+
}
|
|
456
915
|
convertTools(tools) {
|
|
457
916
|
if (!tools)
|
|
458
917
|
return { tools: [] };
|
|
@@ -471,6 +930,25 @@ class ResponsesToChatCompletionConverter {
|
|
|
471
930
|
},
|
|
472
931
|
});
|
|
473
932
|
}
|
|
933
|
+
else if (tt.type === "custom") {
|
|
934
|
+
// Responses custom tool → CC custom tool.
|
|
935
|
+
const custom = { name: tt.name };
|
|
936
|
+
if (tt.description != null)
|
|
937
|
+
custom.description = tt.description;
|
|
938
|
+
if (tt.format != null) {
|
|
939
|
+
custom.format =
|
|
940
|
+
tt.format.type === "text"
|
|
941
|
+
? { type: "text" }
|
|
942
|
+
: {
|
|
943
|
+
type: "grammar",
|
|
944
|
+
grammar: {
|
|
945
|
+
definition: tt.format.definition,
|
|
946
|
+
syntax: tt.format.syntax,
|
|
947
|
+
},
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
ccTools.push({ type: "custom", custom });
|
|
951
|
+
}
|
|
474
952
|
else if (this.isWebSearch(tt)) {
|
|
475
953
|
webSearchOptions = {};
|
|
476
954
|
if (tt.search_context_size) {
|
|
@@ -501,18 +979,34 @@ class ResponsesToChatCompletionConverter {
|
|
|
501
979
|
if (c.type === "function" && c.name) {
|
|
502
980
|
return { type: "function", function: { name: c.name } };
|
|
503
981
|
}
|
|
982
|
+
if (c.type === "custom" && c.name) {
|
|
983
|
+
return { type: "custom", custom: { name: c.name } };
|
|
984
|
+
}
|
|
985
|
+
if (c.type === "allowed_tools") {
|
|
986
|
+
return {
|
|
987
|
+
type: "allowed_tools",
|
|
988
|
+
allowed_tools: {
|
|
989
|
+
mode: c.mode,
|
|
990
|
+
tools: c.tools,
|
|
991
|
+
},
|
|
992
|
+
};
|
|
993
|
+
}
|
|
504
994
|
}
|
|
505
995
|
return "auto";
|
|
506
996
|
}
|
|
507
997
|
convertTextFormat(format) {
|
|
508
998
|
if (format.type === "json_schema") {
|
|
999
|
+
const json_schema = {
|
|
1000
|
+
name: format.name || "response",
|
|
1001
|
+
schema: format.schema,
|
|
1002
|
+
strict: format.strict,
|
|
1003
|
+
};
|
|
1004
|
+
// description is optional but supported by the CC json_schema format.
|
|
1005
|
+
if (format.description != null)
|
|
1006
|
+
json_schema.description = format.description;
|
|
509
1007
|
return {
|
|
510
1008
|
type: "json_schema",
|
|
511
|
-
json_schema
|
|
512
|
-
name: format.name || "response",
|
|
513
|
-
schema: format.schema,
|
|
514
|
-
strict: format.strict,
|
|
515
|
-
},
|
|
1009
|
+
json_schema,
|
|
516
1010
|
};
|
|
517
1011
|
}
|
|
518
1012
|
if (format.type === "json_object") {
|
|
@@ -546,8 +1040,9 @@ class ResponsesToChatCompletionConverter {
|
|
|
546
1040
|
seq: 0,
|
|
547
1041
|
outputIndex: 0,
|
|
548
1042
|
contentIndex: 0,
|
|
549
|
-
|
|
550
|
-
|
|
1043
|
+
annotationIndex: -1,
|
|
1044
|
+
output: [],
|
|
1045
|
+
current: null,
|
|
551
1046
|
messageStarted: false,
|
|
552
1047
|
toolCallCount: 0,
|
|
553
1048
|
};
|
|
@@ -582,6 +1077,36 @@ class ResponsesToChatCompletionConverter {
|
|
|
582
1077
|
const finalStatus = status ?? "completed";
|
|
583
1078
|
const resp = this.makeSkeletonResponse();
|
|
584
1079
|
resp.status = finalStatus;
|
|
1080
|
+
// Carry the output items accumulated during the stream.
|
|
1081
|
+
resp.output = [...state.output];
|
|
1082
|
+
// Echo request-scoped fields onto the terminal response when available.
|
|
1083
|
+
const echoed = this.echoRequestFields(this.requestParams);
|
|
1084
|
+
resp.instructions = echoed.instructions;
|
|
1085
|
+
resp.temperature = echoed.temperature;
|
|
1086
|
+
resp.top_p = echoed.top_p;
|
|
1087
|
+
resp.max_output_tokens = echoed.max_output_tokens;
|
|
1088
|
+
resp.previous_response_id = echoed.previous_response_id;
|
|
1089
|
+
resp.parallel_tool_calls = echoed.parallel_tool_calls;
|
|
1090
|
+
resp.tool_choice = echoed.tool_choice;
|
|
1091
|
+
resp.tools = echoed.tools;
|
|
1092
|
+
resp.text = echoed.text;
|
|
1093
|
+
resp.reasoning = echoed.reasoning;
|
|
1094
|
+
resp.truncation = echoed.truncation;
|
|
1095
|
+
if (echoed.top_logprobs !== undefined)
|
|
1096
|
+
resp.top_logprobs = echoed.top_logprobs;
|
|
1097
|
+
if (echoed.safety_identifier !== undefined)
|
|
1098
|
+
resp.safety_identifier = echoed.safety_identifier;
|
|
1099
|
+
if (echoed.service_tier !== undefined)
|
|
1100
|
+
resp.service_tier = echoed.service_tier;
|
|
1101
|
+
if (this.requestParams?.background != null)
|
|
1102
|
+
resp.background = this.requestParams.background;
|
|
1103
|
+
if (this.requestParams?.prompt_cache_key !== undefined)
|
|
1104
|
+
resp.prompt_cache_key = this.requestParams.prompt_cache_key;
|
|
1105
|
+
if (this.requestParams?.prompt_cache_retention !== undefined)
|
|
1106
|
+
resp.prompt_cache_retention = this.requestParams.prompt_cache_retention;
|
|
1107
|
+
if (finalStatus === "incomplete") {
|
|
1108
|
+
resp.incomplete_details = { reason: "max_output_tokens" };
|
|
1109
|
+
}
|
|
585
1110
|
if (usage) {
|
|
586
1111
|
resp.usage = {
|
|
587
1112
|
input_tokens: usage.prompt_tokens,
|
|
@@ -595,6 +1120,15 @@ class ResponsesToChatCompletionConverter {
|
|
|
595
1120
|
},
|
|
596
1121
|
};
|
|
597
1122
|
}
|
|
1123
|
+
if (finalStatus === "failed") {
|
|
1124
|
+
return [
|
|
1125
|
+
{
|
|
1126
|
+
type: "response.failed",
|
|
1127
|
+
response: resp,
|
|
1128
|
+
sequence_number: state.seq++,
|
|
1129
|
+
},
|
|
1130
|
+
];
|
|
1131
|
+
}
|
|
598
1132
|
if (finalStatus === "incomplete") {
|
|
599
1133
|
return [
|
|
600
1134
|
{
|