@zenmux/rosetta-ai 0.12.0 → 0.14.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,19 +2,22 @@ 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;
|
|
8
|
-
private
|
|
9
|
+
private buildUsage;
|
|
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
|
}
|
|
@@ -133,24 +139,42 @@ class MessagesToChatCompletionConverter {
|
|
|
133
139
|
content,
|
|
134
140
|
stop_reason: this.mapFinishReasonToStopReason(choice?.finish_reason),
|
|
135
141
|
stop_sequence: null,
|
|
136
|
-
usage: this.
|
|
142
|
+
usage: this.buildUsage(response.usage),
|
|
137
143
|
container: null,
|
|
138
144
|
};
|
|
139
145
|
}
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
const
|
|
146
|
+
buildUsage(usage) {
|
|
147
|
+
const u = usage ?? {};
|
|
148
|
+
const inputTokens = u.prompt_tokens ?? 0;
|
|
149
|
+
const outputTokens = u.completion_tokens ?? 0;
|
|
150
|
+
const totalTokens = u.total_tokens ?? inputTokens + outputTokens;
|
|
151
|
+
const cachedTokens = u.prompt_tokens_details?.cached_tokens ?? 0;
|
|
152
|
+
const reasoningTokens = u.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
153
|
+
const webSearch = u.prompt_tokens_details?.web_search ?? 0;
|
|
145
154
|
return {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
155
|
+
completion_tokens: outputTokens,
|
|
156
|
+
prompt_tokens: inputTokens,
|
|
157
|
+
total_tokens: totalTokens,
|
|
158
|
+
completion_tokens_details: {
|
|
159
|
+
reasoning_tokens: reasoningTokens,
|
|
160
|
+
},
|
|
161
|
+
prompt_tokens_details: {
|
|
162
|
+
cached_tokens: cachedTokens,
|
|
163
|
+
...(webSearch > 0 ? { web_search: webSearch } : {}),
|
|
164
|
+
},
|
|
165
|
+
input_tokens: inputTokens,
|
|
166
|
+
output_tokens: outputTokens,
|
|
167
|
+
cache_read_input_tokens: cachedTokens,
|
|
168
|
+
server_tool_use: webSearch > 0
|
|
169
|
+
? {
|
|
170
|
+
web_fetch_requests: 0,
|
|
171
|
+
web_search_requests: webSearch,
|
|
172
|
+
}
|
|
173
|
+
: null,
|
|
149
174
|
cache_creation_input_tokens: 0,
|
|
150
|
-
cache_creation: null,
|
|
151
|
-
inference_geo: null,
|
|
152
|
-
server_tool_use: webSearch > 0 ? { web_search_requests: webSearch, web_fetch_requests: 0 } : null,
|
|
153
175
|
service_tier: "standard",
|
|
176
|
+
audio_input_tokens: u.prompt_tokens_details?.audio_tokens ?? 0,
|
|
177
|
+
audio_cache_read_tokens: u.prompt_tokens_details?.audio_cached_tokens ?? 0,
|
|
154
178
|
};
|
|
155
179
|
}
|
|
156
180
|
generateId() {
|
|
@@ -164,13 +188,27 @@ class MessagesToChatCompletionConverter {
|
|
|
164
188
|
yield event;
|
|
165
189
|
}
|
|
166
190
|
}
|
|
191
|
+
// Safety flush: if the backend signalled finish but never sent the trailing
|
|
192
|
+
// usage chunk, emit terminal events so the stream is well-formed.
|
|
193
|
+
if (this.streamState.stopReason !== null && !this.streamState.finished) {
|
|
194
|
+
const events = [];
|
|
195
|
+
this.emitTerminalEvents(this.streamState, events, undefined);
|
|
196
|
+
for (const event of events) {
|
|
197
|
+
yield event;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
167
200
|
}
|
|
168
201
|
convertStreamChunk(chunk) {
|
|
169
202
|
const state = this.streamState;
|
|
170
203
|
const events = [];
|
|
171
204
|
const choice = chunk.choices[0];
|
|
172
|
-
if (!choice)
|
|
205
|
+
if (!choice) {
|
|
206
|
+
// Usage-only chunk (empty choices) emitted last when stream_options.include_usage is set.
|
|
207
|
+
if (chunk.usage) {
|
|
208
|
+
this.emitTerminalEvents(state, events, chunk.usage);
|
|
209
|
+
}
|
|
173
210
|
return events;
|
|
211
|
+
}
|
|
174
212
|
state.id = chunk.id;
|
|
175
213
|
state.model = chunk.model;
|
|
176
214
|
const delta = choice.delta;
|
|
@@ -298,33 +336,44 @@ class MessagesToChatCompletionConverter {
|
|
|
298
336
|
});
|
|
299
337
|
}
|
|
300
338
|
}
|
|
301
|
-
// Finish reason - emit
|
|
339
|
+
// Finish reason - close the open block, then emit terminal events.
|
|
340
|
+
// When stream_options.include_usage is set, usage arrives in a trailing
|
|
341
|
+
// usage-only chunk; defer message_delta/message_stop until then so we can
|
|
342
|
+
// report real output_tokens. Otherwise (usage already on this chunk, or no
|
|
343
|
+
// usage at all) emit immediately.
|
|
302
344
|
if (choice.finish_reason) {
|
|
345
|
+
state.stopReason = this.mapFinishReasonToStopReason(choice.finish_reason);
|
|
303
346
|
if (state.currentBlockType !== null) {
|
|
304
347
|
events.push({
|
|
305
348
|
type: "content_block_stop",
|
|
306
349
|
index: state.currentBlockIndex,
|
|
307
350
|
});
|
|
351
|
+
state.currentBlockType = null;
|
|
352
|
+
}
|
|
353
|
+
if (chunk.usage) {
|
|
354
|
+
this.emitTerminalEvents(state, events, chunk.usage);
|
|
355
|
+
}
|
|
356
|
+
else if (!this.streamUsageExpected) {
|
|
357
|
+
this.emitTerminalEvents(state, events, undefined);
|
|
308
358
|
}
|
|
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
359
|
}
|
|
326
360
|
return events;
|
|
327
361
|
}
|
|
362
|
+
emitTerminalEvents(state, events, usage) {
|
|
363
|
+
if (state.finished)
|
|
364
|
+
return;
|
|
365
|
+
state.finished = true;
|
|
366
|
+
events.push({
|
|
367
|
+
type: "message_delta",
|
|
368
|
+
delta: {
|
|
369
|
+
stop_reason: state.stopReason,
|
|
370
|
+
stop_sequence: null,
|
|
371
|
+
container: null,
|
|
372
|
+
},
|
|
373
|
+
usage: this.buildUsage(usage),
|
|
374
|
+
});
|
|
375
|
+
events.push({ type: "message_stop" });
|
|
376
|
+
}
|
|
328
377
|
// --- Stream helpers ---
|
|
329
378
|
createStreamState() {
|
|
330
379
|
return {
|
|
@@ -334,6 +383,8 @@ class MessagesToChatCompletionConverter {
|
|
|
334
383
|
id: "",
|
|
335
384
|
model: "",
|
|
336
385
|
toolCallIndexMap: new Map(),
|
|
386
|
+
stopReason: null,
|
|
387
|
+
finished: false,
|
|
337
388
|
};
|
|
338
389
|
}
|
|
339
390
|
transitionBlock(state, events, newType) {
|
|
@@ -386,7 +437,7 @@ class MessagesToChatCompletionConverter {
|
|
|
386
437
|
break;
|
|
387
438
|
case "document":
|
|
388
439
|
hasNonText = true;
|
|
389
|
-
contentParts.push(this.convertDocumentBlock(block));
|
|
440
|
+
contentParts.push(...this.convertDocumentBlock(block));
|
|
390
441
|
break;
|
|
391
442
|
case "tool_result":
|
|
392
443
|
toolResults.push(block);
|
|
@@ -395,18 +446,10 @@ class MessagesToChatCompletionConverter {
|
|
|
395
446
|
}
|
|
396
447
|
if (toolResults.length > 0) {
|
|
397
448
|
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
449
|
messages.push({
|
|
407
450
|
role: "tool",
|
|
408
451
|
tool_call_id: tr.tool_use_id,
|
|
409
|
-
content,
|
|
452
|
+
content: this.convertToolResultContent(tr.content),
|
|
410
453
|
});
|
|
411
454
|
}
|
|
412
455
|
return;
|
|
@@ -417,6 +460,33 @@ class MessagesToChatCompletionConverter {
|
|
|
417
460
|
}
|
|
418
461
|
messages.push({ role: "user", content: contentParts });
|
|
419
462
|
}
|
|
463
|
+
convertToolResultContent(content) {
|
|
464
|
+
if (typeof content === "string") {
|
|
465
|
+
return content;
|
|
466
|
+
}
|
|
467
|
+
if (!content) {
|
|
468
|
+
return "";
|
|
469
|
+
}
|
|
470
|
+
const texts = [];
|
|
471
|
+
for (const block of content) {
|
|
472
|
+
if (block.type === "text") {
|
|
473
|
+
texts.push(block.text);
|
|
474
|
+
}
|
|
475
|
+
else if (block.type === "search_result") {
|
|
476
|
+
// OpenAI tool role has no search_result type; keep the text portions
|
|
477
|
+
const sr = block;
|
|
478
|
+
for (const inner of sr.content) {
|
|
479
|
+
texts.push(inner.text);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else if (block.type === "tool_reference") {
|
|
483
|
+
// keep only the referenced tool name
|
|
484
|
+
texts.push(block.tool_name);
|
|
485
|
+
}
|
|
486
|
+
// image/document blocks are unsupported by OpenAI's tool role — skip
|
|
487
|
+
}
|
|
488
|
+
return texts.join("\n");
|
|
489
|
+
}
|
|
420
490
|
convertImageBlock(block) {
|
|
421
491
|
const source = block.source;
|
|
422
492
|
if (source.type === "base64") {
|
|
@@ -436,30 +506,51 @@ class MessagesToChatCompletionConverter {
|
|
|
436
506
|
const source = block.source;
|
|
437
507
|
if (source.type === "base64") {
|
|
438
508
|
const src = source;
|
|
439
|
-
return
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
509
|
+
return [
|
|
510
|
+
{
|
|
511
|
+
type: "file",
|
|
512
|
+
file: {
|
|
513
|
+
file_data: `data:${src.media_type};base64,${src.data}`,
|
|
514
|
+
filename: block.title ?? undefined,
|
|
515
|
+
},
|
|
444
516
|
},
|
|
445
|
-
|
|
517
|
+
];
|
|
446
518
|
}
|
|
447
519
|
if (source.type === "url") {
|
|
448
520
|
const src = source;
|
|
449
|
-
return
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
521
|
+
return [
|
|
522
|
+
{
|
|
523
|
+
type: "file",
|
|
524
|
+
file: {
|
|
525
|
+
file_data: src.url,
|
|
526
|
+
filename: block.title ?? undefined,
|
|
527
|
+
},
|
|
454
528
|
},
|
|
455
|
-
|
|
529
|
+
];
|
|
456
530
|
}
|
|
457
531
|
// plain text source — encode as text content
|
|
458
532
|
if (source.type === "text") {
|
|
459
533
|
const src = source;
|
|
460
|
-
return { type: "text", text: src.data };
|
|
534
|
+
return [{ type: "text", text: src.data }];
|
|
535
|
+
}
|
|
536
|
+
// content source — unpack nested text/image blocks into content parts
|
|
537
|
+
if (source.type === "content") {
|
|
538
|
+
const src = source;
|
|
539
|
+
if (typeof src.content === "string") {
|
|
540
|
+
return [{ type: "text", text: src.content }];
|
|
541
|
+
}
|
|
542
|
+
const parts = [];
|
|
543
|
+
for (const nested of src.content) {
|
|
544
|
+
if (nested.type === "text") {
|
|
545
|
+
parts.push({ type: "text", text: nested.text });
|
|
546
|
+
}
|
|
547
|
+
else if (nested.type === "image") {
|
|
548
|
+
parts.push(this.convertImageBlock(nested));
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return parts;
|
|
461
552
|
}
|
|
462
|
-
return { type: "text", text: "[Unsupported document source]" };
|
|
553
|
+
return [{ type: "text", text: "[Unsupported document source]" }];
|
|
463
554
|
}
|
|
464
555
|
convertAssistantMessage(messages, msg) {
|
|
465
556
|
if (typeof msg.content === "string") {
|