@zenmux/rosetta-ai 0.11.0 → 0.13.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.
|
@@ -2,6 +2,7 @@ import type OpenAI from "openai";
|
|
|
2
2
|
import type Anthropic from "@anthropic-ai/sdk";
|
|
3
3
|
export declare class MessagesToChatCompletionConverter {
|
|
4
4
|
private streamState;
|
|
5
|
+
private streamUsageExpected;
|
|
5
6
|
constructor();
|
|
6
7
|
convertRequest(params: Anthropic.MessageCreateParams): OpenAI.ChatCompletionCreateParams;
|
|
7
8
|
convertResponse(response: OpenAI.ChatCompletion): Anthropic.Message;
|
|
@@ -9,12 +10,14 @@ export declare class MessagesToChatCompletionConverter {
|
|
|
9
10
|
private generateId;
|
|
10
11
|
convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk>): AsyncIterable<Anthropic.RawMessageStreamEvent>;
|
|
11
12
|
convertStreamChunk(chunk: OpenAI.ChatCompletionChunk): Anthropic.RawMessageStreamEvent[];
|
|
13
|
+
private emitTerminalEvents;
|
|
12
14
|
private createStreamState;
|
|
13
15
|
private transitionBlock;
|
|
14
16
|
private hasReasoning;
|
|
15
17
|
private getReasoning;
|
|
16
18
|
private mapFinishReasonToStopReason;
|
|
17
19
|
private convertUserMessage;
|
|
20
|
+
private convertToolResultContent;
|
|
18
21
|
private convertImageBlock;
|
|
19
22
|
private convertDocumentBlock;
|
|
20
23
|
private convertAssistantMessage;
|
|
@@ -3,6 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MessagesToChatCompletionConverter = void 0;
|
|
4
4
|
class MessagesToChatCompletionConverter {
|
|
5
5
|
streamState;
|
|
6
|
+
// Set when the forward request enables stream_options.include_usage, so the
|
|
7
|
+
// backward stream knows usage arrives in a trailing usage-only chunk and can
|
|
8
|
+
// defer terminal events until then.
|
|
9
|
+
streamUsageExpected = false;
|
|
6
10
|
constructor() {
|
|
7
11
|
this.streamState = this.createStreamState();
|
|
8
12
|
}
|
|
@@ -73,6 +77,8 @@ class MessagesToChatCompletionConverter {
|
|
|
73
77
|
}
|
|
74
78
|
if (params.stream === true) {
|
|
75
79
|
result.stream = true;
|
|
80
|
+
result.stream_options = { include_usage: true };
|
|
81
|
+
this.streamUsageExpected = true;
|
|
76
82
|
}
|
|
77
83
|
return result;
|
|
78
84
|
}
|
|
@@ -164,13 +170,27 @@ class MessagesToChatCompletionConverter {
|
|
|
164
170
|
yield event;
|
|
165
171
|
}
|
|
166
172
|
}
|
|
173
|
+
// Safety flush: if the backend signalled finish but never sent the trailing
|
|
174
|
+
// usage chunk, emit terminal events so the stream is well-formed.
|
|
175
|
+
if (this.streamState.stopReason !== null && !this.streamState.finished) {
|
|
176
|
+
const events = [];
|
|
177
|
+
this.emitTerminalEvents(this.streamState, events, undefined);
|
|
178
|
+
for (const event of events) {
|
|
179
|
+
yield event;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
167
182
|
}
|
|
168
183
|
convertStreamChunk(chunk) {
|
|
169
184
|
const state = this.streamState;
|
|
170
185
|
const events = [];
|
|
171
186
|
const choice = chunk.choices[0];
|
|
172
|
-
if (!choice)
|
|
187
|
+
if (!choice) {
|
|
188
|
+
// Usage-only chunk (empty choices) emitted last when stream_options.include_usage is set.
|
|
189
|
+
if (chunk.usage) {
|
|
190
|
+
this.emitTerminalEvents(state, events, chunk.usage);
|
|
191
|
+
}
|
|
173
192
|
return events;
|
|
193
|
+
}
|
|
174
194
|
state.id = chunk.id;
|
|
175
195
|
state.model = chunk.model;
|
|
176
196
|
const delta = choice.delta;
|
|
@@ -298,33 +318,50 @@ class MessagesToChatCompletionConverter {
|
|
|
298
318
|
});
|
|
299
319
|
}
|
|
300
320
|
}
|
|
301
|
-
// Finish reason - emit
|
|
321
|
+
// Finish reason - close the open block, then emit terminal events.
|
|
322
|
+
// When stream_options.include_usage is set, usage arrives in a trailing
|
|
323
|
+
// usage-only chunk; defer message_delta/message_stop until then so we can
|
|
324
|
+
// report real output_tokens. Otherwise (usage already on this chunk, or no
|
|
325
|
+
// usage at all) emit immediately.
|
|
302
326
|
if (choice.finish_reason) {
|
|
327
|
+
state.stopReason = this.mapFinishReasonToStopReason(choice.finish_reason);
|
|
303
328
|
if (state.currentBlockType !== null) {
|
|
304
329
|
events.push({
|
|
305
330
|
type: "content_block_stop",
|
|
306
331
|
index: state.currentBlockIndex,
|
|
307
332
|
});
|
|
333
|
+
state.currentBlockType = null;
|
|
334
|
+
}
|
|
335
|
+
if (chunk.usage) {
|
|
336
|
+
this.emitTerminalEvents(state, events, chunk.usage);
|
|
337
|
+
}
|
|
338
|
+
else if (!this.streamUsageExpected) {
|
|
339
|
+
this.emitTerminalEvents(state, events, undefined);
|
|
308
340
|
}
|
|
309
|
-
events.push({
|
|
310
|
-
type: "message_delta",
|
|
311
|
-
delta: {
|
|
312
|
-
stop_reason: this.mapFinishReasonToStopReason(choice.finish_reason),
|
|
313
|
-
stop_sequence: null,
|
|
314
|
-
container: null,
|
|
315
|
-
},
|
|
316
|
-
usage: {
|
|
317
|
-
output_tokens: 0,
|
|
318
|
-
input_tokens: null,
|
|
319
|
-
cache_creation_input_tokens: null,
|
|
320
|
-
cache_read_input_tokens: null,
|
|
321
|
-
server_tool_use: null,
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
events.push({ type: "message_stop" });
|
|
325
341
|
}
|
|
326
342
|
return events;
|
|
327
343
|
}
|
|
344
|
+
emitTerminalEvents(state, events, usage) {
|
|
345
|
+
if (state.finished)
|
|
346
|
+
return;
|
|
347
|
+
state.finished = true;
|
|
348
|
+
events.push({
|
|
349
|
+
type: "message_delta",
|
|
350
|
+
delta: {
|
|
351
|
+
stop_reason: state.stopReason,
|
|
352
|
+
stop_sequence: null,
|
|
353
|
+
container: null,
|
|
354
|
+
},
|
|
355
|
+
usage: {
|
|
356
|
+
output_tokens: usage?.completion_tokens ?? 0,
|
|
357
|
+
input_tokens: usage?.prompt_tokens ?? null,
|
|
358
|
+
cache_creation_input_tokens: null,
|
|
359
|
+
cache_read_input_tokens: usage?.prompt_tokens_details?.cached_tokens ?? null,
|
|
360
|
+
server_tool_use: null,
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
events.push({ type: "message_stop" });
|
|
364
|
+
}
|
|
328
365
|
// --- Stream helpers ---
|
|
329
366
|
createStreamState() {
|
|
330
367
|
return {
|
|
@@ -334,6 +371,8 @@ class MessagesToChatCompletionConverter {
|
|
|
334
371
|
id: "",
|
|
335
372
|
model: "",
|
|
336
373
|
toolCallIndexMap: new Map(),
|
|
374
|
+
stopReason: null,
|
|
375
|
+
finished: false,
|
|
337
376
|
};
|
|
338
377
|
}
|
|
339
378
|
transitionBlock(state, events, newType) {
|
|
@@ -386,7 +425,7 @@ class MessagesToChatCompletionConverter {
|
|
|
386
425
|
break;
|
|
387
426
|
case "document":
|
|
388
427
|
hasNonText = true;
|
|
389
|
-
contentParts.push(this.convertDocumentBlock(block));
|
|
428
|
+
contentParts.push(...this.convertDocumentBlock(block));
|
|
390
429
|
break;
|
|
391
430
|
case "tool_result":
|
|
392
431
|
toolResults.push(block);
|
|
@@ -395,18 +434,10 @@ class MessagesToChatCompletionConverter {
|
|
|
395
434
|
}
|
|
396
435
|
if (toolResults.length > 0) {
|
|
397
436
|
for (const tr of toolResults) {
|
|
398
|
-
const content = typeof tr.content === "string"
|
|
399
|
-
? tr.content
|
|
400
|
-
: tr.content
|
|
401
|
-
? tr.content
|
|
402
|
-
.filter((b) => b.type === "text")
|
|
403
|
-
.map(b => b.text)
|
|
404
|
-
.join("\n")
|
|
405
|
-
: "";
|
|
406
437
|
messages.push({
|
|
407
438
|
role: "tool",
|
|
408
439
|
tool_call_id: tr.tool_use_id,
|
|
409
|
-
content,
|
|
440
|
+
content: this.convertToolResultContent(tr.content),
|
|
410
441
|
});
|
|
411
442
|
}
|
|
412
443
|
return;
|
|
@@ -417,6 +448,33 @@ class MessagesToChatCompletionConverter {
|
|
|
417
448
|
}
|
|
418
449
|
messages.push({ role: "user", content: contentParts });
|
|
419
450
|
}
|
|
451
|
+
convertToolResultContent(content) {
|
|
452
|
+
if (typeof content === "string") {
|
|
453
|
+
return content;
|
|
454
|
+
}
|
|
455
|
+
if (!content) {
|
|
456
|
+
return "";
|
|
457
|
+
}
|
|
458
|
+
const texts = [];
|
|
459
|
+
for (const block of content) {
|
|
460
|
+
if (block.type === "text") {
|
|
461
|
+
texts.push(block.text);
|
|
462
|
+
}
|
|
463
|
+
else if (block.type === "search_result") {
|
|
464
|
+
// OpenAI tool role has no search_result type; keep the text portions
|
|
465
|
+
const sr = block;
|
|
466
|
+
for (const inner of sr.content) {
|
|
467
|
+
texts.push(inner.text);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
else if (block.type === "tool_reference") {
|
|
471
|
+
// keep only the referenced tool name
|
|
472
|
+
texts.push(block.tool_name);
|
|
473
|
+
}
|
|
474
|
+
// image/document blocks are unsupported by OpenAI's tool role — skip
|
|
475
|
+
}
|
|
476
|
+
return texts.join("\n");
|
|
477
|
+
}
|
|
420
478
|
convertImageBlock(block) {
|
|
421
479
|
const source = block.source;
|
|
422
480
|
if (source.type === "base64") {
|
|
@@ -436,30 +494,51 @@ class MessagesToChatCompletionConverter {
|
|
|
436
494
|
const source = block.source;
|
|
437
495
|
if (source.type === "base64") {
|
|
438
496
|
const src = source;
|
|
439
|
-
return
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
497
|
+
return [
|
|
498
|
+
{
|
|
499
|
+
type: "file",
|
|
500
|
+
file: {
|
|
501
|
+
file_data: `data:${src.media_type};base64,${src.data}`,
|
|
502
|
+
filename: block.title ?? undefined,
|
|
503
|
+
},
|
|
444
504
|
},
|
|
445
|
-
|
|
505
|
+
];
|
|
446
506
|
}
|
|
447
507
|
if (source.type === "url") {
|
|
448
508
|
const src = source;
|
|
449
|
-
return
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
509
|
+
return [
|
|
510
|
+
{
|
|
511
|
+
type: "file",
|
|
512
|
+
file: {
|
|
513
|
+
file_data: src.url,
|
|
514
|
+
filename: block.title ?? undefined,
|
|
515
|
+
},
|
|
454
516
|
},
|
|
455
|
-
|
|
517
|
+
];
|
|
456
518
|
}
|
|
457
519
|
// plain text source — encode as text content
|
|
458
520
|
if (source.type === "text") {
|
|
459
521
|
const src = source;
|
|
460
|
-
return { type: "text", text: src.data };
|
|
522
|
+
return [{ type: "text", text: src.data }];
|
|
523
|
+
}
|
|
524
|
+
// content source — unpack nested text/image blocks into content parts
|
|
525
|
+
if (source.type === "content") {
|
|
526
|
+
const src = source;
|
|
527
|
+
if (typeof src.content === "string") {
|
|
528
|
+
return [{ type: "text", text: src.content }];
|
|
529
|
+
}
|
|
530
|
+
const parts = [];
|
|
531
|
+
for (const nested of src.content) {
|
|
532
|
+
if (nested.type === "text") {
|
|
533
|
+
parts.push({ type: "text", text: nested.text });
|
|
534
|
+
}
|
|
535
|
+
else if (nested.type === "image") {
|
|
536
|
+
parts.push(this.convertImageBlock(nested));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return parts;
|
|
461
540
|
}
|
|
462
|
-
return { type: "text", text: "[Unsupported document source]" };
|
|
541
|
+
return [{ type: "text", text: "[Unsupported document source]" }];
|
|
463
542
|
}
|
|
464
543
|
convertAssistantMessage(messages, msg) {
|
|
465
544
|
if (typeof msg.content === "string") {
|
|
@@ -361,10 +361,15 @@ class MessagesToResponsesConverter {
|
|
|
361
361
|
parts.push({
|
|
362
362
|
type: "input_file",
|
|
363
363
|
file_data: `data:${src.media_type};base64,${src.data}`,
|
|
364
|
+
filename: block.title || "file",
|
|
364
365
|
});
|
|
365
366
|
}
|
|
366
367
|
else if (src.type === "url") {
|
|
367
|
-
parts.push({
|
|
368
|
+
parts.push({
|
|
369
|
+
type: "input_file",
|
|
370
|
+
file_data: src.url,
|
|
371
|
+
filename: block.title || "file",
|
|
372
|
+
});
|
|
368
373
|
}
|
|
369
374
|
else if (src.type === "text") {
|
|
370
375
|
parts.push({ type: "input_text", text: src.data });
|