@zenmux/rosetta-ai 1.0.6 → 1.0.9
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.
|
@@ -35,6 +35,14 @@ export declare class ResponsesToChatCompletionConverter {
|
|
|
35
35
|
* function / web_search / custom tools. Mirrors the legacy `toRespTool`.
|
|
36
36
|
*/
|
|
37
37
|
private toRespTool;
|
|
38
|
+
/**
|
|
39
|
+
* Compute `output_text` — the concatenation of all `output_text` content
|
|
40
|
+
* parts across the response's message output items, matching the Responses
|
|
41
|
+
* API convention. Returns "" when there are no text parts (e.g. tool-only
|
|
42
|
+
* or refusal-only responses), matching the legacy `toOpenAIRespResponse`
|
|
43
|
+
* default of `output_text: ""`.
|
|
44
|
+
*/
|
|
45
|
+
private collectOutputText;
|
|
38
46
|
convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk>): AsyncIterable<RespStreamEvent>;
|
|
39
47
|
convertStreamChunk(chunk: OpenAI.ChatCompletionChunk): RespStreamEvent[];
|
|
40
48
|
/** Open a message output item and its first content part. */
|
|
@@ -59,7 +67,6 @@ export declare class ResponsesToChatCompletionConverter {
|
|
|
59
67
|
private convertToolChoice;
|
|
60
68
|
private convertTextFormat;
|
|
61
69
|
private finishReasonToStatus;
|
|
62
|
-
private generateId;
|
|
63
70
|
private createStreamState;
|
|
64
71
|
private makeSkeletonResponse;
|
|
65
72
|
private emitCompleted;
|
|
@@ -116,7 +116,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
116
116
|
if (reasoning) {
|
|
117
117
|
output.push({
|
|
118
118
|
type: "reasoning",
|
|
119
|
-
id: `rs_${
|
|
119
|
+
id: `rs_${choice?.index ?? 0}_${response.id}`,
|
|
120
120
|
summary: [{ type: "summary_text", text: reasoning }],
|
|
121
121
|
});
|
|
122
122
|
}
|
|
@@ -126,7 +126,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
126
126
|
if (tc.type === "function") {
|
|
127
127
|
output.push({
|
|
128
128
|
type: "function_call",
|
|
129
|
-
id: `fc_${
|
|
129
|
+
id: `fc_${choice?.index ?? 0}_${response.id}`,
|
|
130
130
|
call_id: tc.id,
|
|
131
131
|
name: tc.function.name,
|
|
132
132
|
arguments: tc.function.arguments,
|
|
@@ -160,7 +160,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
160
160
|
}
|
|
161
161
|
output.push({
|
|
162
162
|
type: "message",
|
|
163
|
-
id: `msg_${
|
|
163
|
+
id: `msg_${choice?.index ?? 0}_${response.id}`,
|
|
164
164
|
role: "assistant",
|
|
165
165
|
status: "completed",
|
|
166
166
|
content,
|
|
@@ -177,6 +177,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
177
177
|
created_at: response.created,
|
|
178
178
|
model: response.model,
|
|
179
179
|
output,
|
|
180
|
+
output_text: this.collectOutputText(output),
|
|
180
181
|
status,
|
|
181
182
|
error: null,
|
|
182
183
|
incomplete_details: status === "incomplete" ? { reason: "max_output_tokens" } : null,
|
|
@@ -192,23 +193,28 @@ class ResponsesToChatCompletionConverter {
|
|
|
192
193
|
text: echoed.text,
|
|
193
194
|
reasoning: echoed.reasoning,
|
|
194
195
|
truncation: echoed.truncation,
|
|
195
|
-
user:
|
|
196
|
+
user: null,
|
|
197
|
+
// Fields the legacy converter always emitted; keep them for parity.
|
|
198
|
+
conversation: null,
|
|
199
|
+
prompt: null,
|
|
200
|
+
background: params?.background ?? false,
|
|
196
201
|
// Non-standard echoes (top_logprobs / safety_identifier / service_tier)
|
|
197
202
|
// are attached via cast; the Responses Response type does not declare them.
|
|
198
203
|
...(echoed.top_logprobs !== undefined
|
|
199
204
|
? { top_logprobs: echoed.top_logprobs }
|
|
200
205
|
: {}),
|
|
201
|
-
...(echoed.safety_identifier
|
|
206
|
+
...(echoed.safety_identifier != null
|
|
202
207
|
? { safety_identifier: echoed.safety_identifier }
|
|
203
|
-
: {}),
|
|
204
|
-
...(echoed.service_tier
|
|
205
|
-
|
|
208
|
+
: { safety_identifier: null }),
|
|
209
|
+
...(echoed.service_tier != null
|
|
210
|
+
? { service_tier: echoed.service_tier }
|
|
211
|
+
: { service_tier: response.service_tier ?? "default" }),
|
|
206
212
|
...(params?.prompt_cache_key !== undefined
|
|
207
213
|
? { prompt_cache_key: params.prompt_cache_key }
|
|
208
214
|
: {}),
|
|
209
215
|
...(params?.prompt_cache_retention !== undefined
|
|
210
216
|
? { prompt_cache_retention: params.prompt_cache_retention }
|
|
211
|
-
: {}),
|
|
217
|
+
: { prompt_cache_retention: null }),
|
|
212
218
|
usage: response.usage
|
|
213
219
|
? {
|
|
214
220
|
input_tokens: response.usage.prompt_tokens,
|
|
@@ -233,16 +239,16 @@ class ResponsesToChatCompletionConverter {
|
|
|
233
239
|
if (!params) {
|
|
234
240
|
return {
|
|
235
241
|
instructions: null,
|
|
236
|
-
temperature:
|
|
237
|
-
top_p:
|
|
242
|
+
temperature: 1,
|
|
243
|
+
top_p: 1,
|
|
238
244
|
max_output_tokens: null,
|
|
239
245
|
previous_response_id: null,
|
|
240
246
|
parallel_tool_calls: true,
|
|
241
247
|
tool_choice: "auto",
|
|
242
248
|
tools: [],
|
|
243
|
-
text: { format: { type: "text" } },
|
|
249
|
+
text: { format: { type: "text" }, verbosity: "medium" },
|
|
244
250
|
reasoning: null,
|
|
245
|
-
truncation:
|
|
251
|
+
truncation: "disabled",
|
|
246
252
|
};
|
|
247
253
|
}
|
|
248
254
|
const tools = (params.tools ?? [])
|
|
@@ -250,20 +256,18 @@ class ResponsesToChatCompletionConverter {
|
|
|
250
256
|
.filter(Boolean);
|
|
251
257
|
return {
|
|
252
258
|
instructions: params.instructions ?? null,
|
|
253
|
-
temperature: params.temperature ??
|
|
254
|
-
top_p: params.top_p ??
|
|
259
|
+
temperature: params.temperature ?? 1,
|
|
260
|
+
top_p: params.top_p ?? 1,
|
|
255
261
|
max_output_tokens: params.max_output_tokens ?? null,
|
|
256
262
|
previous_response_id: params.previous_response_id ?? null,
|
|
257
263
|
parallel_tool_calls: params.parallel_tool_calls ?? true,
|
|
258
264
|
tool_choice: params.tool_choice ?? "auto",
|
|
259
265
|
tools,
|
|
260
|
-
text: params.text ?? { format: { type: "text" } },
|
|
266
|
+
text: params.text ?? { format: { type: "text" }, verbosity: "medium" },
|
|
261
267
|
reasoning: params.reasoning ?? null,
|
|
262
|
-
truncation: params.truncation ??
|
|
268
|
+
truncation: params.truncation ?? "disabled",
|
|
263
269
|
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).
|
|
270
|
+
safety_identifier: params.safety_identifier ?? null,
|
|
267
271
|
service_tier: params.service_tier,
|
|
268
272
|
};
|
|
269
273
|
}
|
|
@@ -298,6 +302,29 @@ class ResponsesToChatCompletionConverter {
|
|
|
298
302
|
}
|
|
299
303
|
return undefined;
|
|
300
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Compute `output_text` — the concatenation of all `output_text` content
|
|
307
|
+
* parts across the response's message output items, matching the Responses
|
|
308
|
+
* API convention. Returns "" when there are no text parts (e.g. tool-only
|
|
309
|
+
* or refusal-only responses), matching the legacy `toOpenAIRespResponse`
|
|
310
|
+
* default of `output_text: ""`.
|
|
311
|
+
*/
|
|
312
|
+
collectOutputText(output) {
|
|
313
|
+
let text = "";
|
|
314
|
+
for (const item of output ?? []) {
|
|
315
|
+
if (item.type !== "message")
|
|
316
|
+
continue;
|
|
317
|
+
const content = item.content;
|
|
318
|
+
if (!Array.isArray(content))
|
|
319
|
+
continue;
|
|
320
|
+
for (const part of content) {
|
|
321
|
+
if (part?.type === "output_text" && typeof part.text === "string") {
|
|
322
|
+
text += part.text;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return text;
|
|
327
|
+
}
|
|
301
328
|
// --- Stream conversion (CC → Responses, backward) ---
|
|
302
329
|
async *convertStream(stream) {
|
|
303
330
|
for await (const chunk of stream) {
|
|
@@ -342,7 +369,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
342
369
|
if (!state.current || state.current.type !== "reasoning") {
|
|
343
370
|
// Close any other open item before starting reasoning.
|
|
344
371
|
events.push(...this.closeCurrent());
|
|
345
|
-
const itemId = `rs_${
|
|
372
|
+
const itemId = `rs_${choice.index}_${state.id}`;
|
|
346
373
|
state.current = {
|
|
347
374
|
type: "reasoning",
|
|
348
375
|
itemId,
|
|
@@ -406,7 +433,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
406
433
|
// matching the legacy converter's output_index math.
|
|
407
434
|
const outputIndex = state.outputIndex + state.toolCallCount - 1;
|
|
408
435
|
if (isCustom) {
|
|
409
|
-
const itemId = `ct_${
|
|
436
|
+
const itemId = `ct_${choice.index}_${state.id}`;
|
|
410
437
|
state.current = {
|
|
411
438
|
type: "custom_tool_call",
|
|
412
439
|
itemId,
|
|
@@ -430,7 +457,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
430
457
|
});
|
|
431
458
|
}
|
|
432
459
|
else {
|
|
433
|
-
const itemId = `fc_${
|
|
460
|
+
const itemId = `fc_${choice.index}_${state.id}`;
|
|
434
461
|
state.current = {
|
|
435
462
|
type: "function_call",
|
|
436
463
|
itemId,
|
|
@@ -559,7 +586,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
559
586
|
/** Open a message output item and its first content part. */
|
|
560
587
|
openMessage(state, kind, events) {
|
|
561
588
|
state.messageStarted = true;
|
|
562
|
-
const itemId = `
|
|
589
|
+
const itemId = `msg_0_${state.id}`;
|
|
563
590
|
const msgOutputIndex = state.outputIndex + state.toolCallCount;
|
|
564
591
|
state.current = {
|
|
565
592
|
type: "message",
|
|
@@ -1028,9 +1055,6 @@ class ResponsesToChatCompletionConverter {
|
|
|
1028
1055
|
return "completed";
|
|
1029
1056
|
}
|
|
1030
1057
|
}
|
|
1031
|
-
generateId() {
|
|
1032
|
-
return Math.random().toString(36).substring(2, 15);
|
|
1033
|
-
}
|
|
1034
1058
|
// --- Private: stream helpers ---
|
|
1035
1059
|
createStreamState() {
|
|
1036
1060
|
return {
|
|
@@ -1079,6 +1103,7 @@ class ResponsesToChatCompletionConverter {
|
|
|
1079
1103
|
resp.status = finalStatus;
|
|
1080
1104
|
// Carry the output items accumulated during the stream.
|
|
1081
1105
|
resp.output = [...state.output];
|
|
1106
|
+
resp.output_text = this.collectOutputText(resp.output);
|
|
1082
1107
|
// Echo request-scoped fields onto the terminal response when available.
|
|
1083
1108
|
const echoed = this.echoRequestFields(this.requestParams);
|
|
1084
1109
|
resp.instructions = echoed.instructions;
|
|
@@ -1092,18 +1117,21 @@ class ResponsesToChatCompletionConverter {
|
|
|
1092
1117
|
resp.text = echoed.text;
|
|
1093
1118
|
resp.reasoning = echoed.reasoning;
|
|
1094
1119
|
resp.truncation = echoed.truncation;
|
|
1120
|
+
resp.user = null;
|
|
1121
|
+
resp.conversation = null;
|
|
1122
|
+
resp.prompt = null;
|
|
1123
|
+
resp.background = this.requestParams?.background ?? false;
|
|
1095
1124
|
if (echoed.top_logprobs !== undefined)
|
|
1096
1125
|
resp.top_logprobs = echoed.top_logprobs;
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
resp.service_tier = echoed.service_tier;
|
|
1101
|
-
if (this.requestParams?.background != null)
|
|
1102
|
-
resp.background = this.requestParams.background;
|
|
1126
|
+
resp.safety_identifier =
|
|
1127
|
+
echoed.safety_identifier != null ? echoed.safety_identifier : null;
|
|
1128
|
+
resp.service_tier = echoed.service_tier != null ? echoed.service_tier : "default";
|
|
1103
1129
|
if (this.requestParams?.prompt_cache_key !== undefined)
|
|
1104
1130
|
resp.prompt_cache_key = this.requestParams.prompt_cache_key;
|
|
1105
|
-
|
|
1106
|
-
|
|
1131
|
+
resp.prompt_cache_retention =
|
|
1132
|
+
this.requestParams?.prompt_cache_retention !== undefined
|
|
1133
|
+
? this.requestParams.prompt_cache_retention
|
|
1134
|
+
: null;
|
|
1107
1135
|
if (finalStatus === "incomplete") {
|
|
1108
1136
|
resp.incomplete_details = { reason: "max_output_tokens" };
|
|
1109
1137
|
}
|