@zenmux/rosetta-ai 1.0.17 → 1.0.18
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 ChatCompletionToMessagesConverter {
|
|
4
4
|
private streamState;
|
|
5
|
+
private customToolNames;
|
|
5
6
|
constructor();
|
|
6
7
|
convertRequest(params: OpenAI.ChatCompletionCreateParams): Anthropic.MessageCreateParams;
|
|
7
8
|
convertResponse(message: Anthropic.Message): OpenAI.ChatCompletion;
|
|
@@ -24,6 +25,10 @@ export declare class ChatCompletionToMessagesConverter {
|
|
|
24
25
|
private convertAssistantMessage;
|
|
25
26
|
private appendToolResult;
|
|
26
27
|
private convertTools;
|
|
28
|
+
private customToolInputSchema;
|
|
29
|
+
private convertWebSearchOptions;
|
|
30
|
+
private convertFile;
|
|
31
|
+
private safeParseObject;
|
|
27
32
|
private convertToolChoice;
|
|
28
33
|
private convertResponseFormat;
|
|
29
34
|
private convertReasoningEffort;
|
|
@@ -4,11 +4,13 @@ exports.ChatCompletionToMessagesConverter = void 0;
|
|
|
4
4
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
5
5
|
class ChatCompletionToMessagesConverter {
|
|
6
6
|
streamState;
|
|
7
|
+
customToolNames = new Set();
|
|
7
8
|
constructor() {
|
|
8
9
|
this.streamState = this.createStreamState();
|
|
9
10
|
}
|
|
10
11
|
// --- Request conversion (CC → Messages, forward) ---
|
|
11
12
|
convertRequest(params) {
|
|
13
|
+
this.customToolNames.clear();
|
|
12
14
|
const systemBlocks = [];
|
|
13
15
|
const messages = [];
|
|
14
16
|
for (const msg of params.messages) {
|
|
@@ -33,7 +35,7 @@ class ChatCompletionToMessagesConverter {
|
|
|
33
35
|
}
|
|
34
36
|
const result = {
|
|
35
37
|
model: params.model,
|
|
36
|
-
max_tokens: params.
|
|
38
|
+
max_tokens: params.max_completion_tokens ?? params.max_tokens ?? DEFAULT_MAX_TOKENS,
|
|
37
39
|
messages,
|
|
38
40
|
};
|
|
39
41
|
if (systemBlocks.length > 0) {
|
|
@@ -49,20 +51,29 @@ class ChatCompletionToMessagesConverter {
|
|
|
49
51
|
const stop = params.stop;
|
|
50
52
|
result.stop_sequences = typeof stop === "string" ? [stop] : stop;
|
|
51
53
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
const tools = this.convertTools(params.tools ?? []);
|
|
55
|
+
const webSearchTool = this.convertWebSearchOptions(params.web_search_options);
|
|
56
|
+
if (webSearchTool) {
|
|
57
|
+
tools.push(webSearchTool);
|
|
58
|
+
}
|
|
59
|
+
if (tools.length > 0) {
|
|
60
|
+
result.tools = tools;
|
|
54
61
|
}
|
|
55
62
|
if (params.tool_choice !== undefined) {
|
|
56
63
|
result.tool_choice = this.convertToolChoice(params.tool_choice, params.parallel_tool_calls);
|
|
57
64
|
}
|
|
65
|
+
else if (params.parallel_tool_calls === false) {
|
|
66
|
+
result.tool_choice = { type: "auto", disable_parallel_tool_use: true };
|
|
67
|
+
}
|
|
58
68
|
if (params.response_format) {
|
|
59
69
|
result.output_config = this.convertResponseFormat(params.response_format);
|
|
60
70
|
}
|
|
61
71
|
if (params.reasoning_effort != null) {
|
|
62
72
|
result.thinking = this.convertReasoningEffort(params.reasoning_effort);
|
|
63
73
|
}
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
const metadataUserId = params.metadata?.user_id;
|
|
75
|
+
if (metadataUserId != null || params.user != null) {
|
|
76
|
+
result.metadata = { user_id: metadataUserId ?? params.user };
|
|
66
77
|
}
|
|
67
78
|
if (params.service_tier != null) {
|
|
68
79
|
const tier = params.service_tier;
|
|
@@ -70,8 +81,8 @@ class ChatCompletionToMessagesConverter {
|
|
|
70
81
|
result.service_tier = tier;
|
|
71
82
|
}
|
|
72
83
|
}
|
|
73
|
-
if (params.stream
|
|
74
|
-
result.stream =
|
|
84
|
+
if (params.stream != null) {
|
|
85
|
+
result.stream = params.stream;
|
|
75
86
|
}
|
|
76
87
|
return result;
|
|
77
88
|
}
|
|
@@ -97,14 +108,26 @@ class ChatCompletionToMessagesConverter {
|
|
|
97
108
|
});
|
|
98
109
|
}
|
|
99
110
|
else if (block.type === "tool_use" || block.type === "server_tool_use") {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
if (this.customToolNames.has(block.name)) {
|
|
112
|
+
toolCalls.push({
|
|
113
|
+
id: block.id,
|
|
114
|
+
type: "custom",
|
|
115
|
+
custom: {
|
|
116
|
+
name: block.name,
|
|
117
|
+
input: JSON.stringify(block.input),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
toolCalls.push({
|
|
123
|
+
id: block.id,
|
|
124
|
+
type: "function",
|
|
125
|
+
function: {
|
|
126
|
+
name: block.name,
|
|
127
|
+
arguments: JSON.stringify(block.input),
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
108
131
|
}
|
|
109
132
|
else if (block.type === "web_search_tool_result") {
|
|
110
133
|
const content = block.content;
|
|
@@ -125,7 +148,7 @@ class ChatCompletionToMessagesConverter {
|
|
|
125
148
|
}
|
|
126
149
|
const assistantMessage = {
|
|
127
150
|
role: "assistant",
|
|
128
|
-
content: textParts.
|
|
151
|
+
content: textParts.join(""),
|
|
129
152
|
refusal: null,
|
|
130
153
|
};
|
|
131
154
|
if (thinkingParts.length > 0) {
|
|
@@ -226,6 +249,7 @@ class ChatCompletionToMessagesConverter {
|
|
|
226
249
|
object: "chat.completion.chunk",
|
|
227
250
|
created: Math.floor(Date.now() / 1000),
|
|
228
251
|
model: this.streamState.model,
|
|
252
|
+
service_tier: null,
|
|
229
253
|
choices: [{ index: 0, delta, finish_reason }],
|
|
230
254
|
};
|
|
231
255
|
}
|
|
@@ -371,6 +395,7 @@ class ChatCompletionToMessagesConverter {
|
|
|
371
395
|
object: "chat.completion.chunk",
|
|
372
396
|
created: Math.floor(Date.now() / 1000),
|
|
373
397
|
model: state.model,
|
|
398
|
+
service_tier: null,
|
|
374
399
|
choices: [],
|
|
375
400
|
usage,
|
|
376
401
|
};
|
|
@@ -411,6 +436,8 @@ class ChatCompletionToMessagesConverter {
|
|
|
411
436
|
return { type: "text", text: part.text };
|
|
412
437
|
case "image_url":
|
|
413
438
|
return this.convertImageUrl(part);
|
|
439
|
+
case "file":
|
|
440
|
+
return this.convertFile(part);
|
|
414
441
|
default:
|
|
415
442
|
return { type: "text", text: `[Unsupported content type: ${part.type}]` };
|
|
416
443
|
}
|
|
@@ -454,7 +481,15 @@ class ChatCompletionToMessagesConverter {
|
|
|
454
481
|
type: "tool_use",
|
|
455
482
|
id: tc.id,
|
|
456
483
|
name: tc.function.name,
|
|
457
|
-
input:
|
|
484
|
+
input: this.safeParseObject(tc.function.arguments),
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
else if (tc.type === "custom") {
|
|
488
|
+
blocks.push({
|
|
489
|
+
type: "tool_use",
|
|
490
|
+
id: tc.id,
|
|
491
|
+
name: tc.custom.name,
|
|
492
|
+
input: this.safeParseObject(tc.custom.input),
|
|
458
493
|
});
|
|
459
494
|
}
|
|
460
495
|
}
|
|
@@ -480,13 +515,80 @@ class ChatCompletionToMessagesConverter {
|
|
|
480
515
|
messages.push({ role: "user", content: [toolResult] });
|
|
481
516
|
}
|
|
482
517
|
convertTools(tools) {
|
|
483
|
-
return tools
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
518
|
+
return tools.map(tool => {
|
|
519
|
+
if (tool.type === "function") {
|
|
520
|
+
return {
|
|
521
|
+
type: "custom",
|
|
522
|
+
name: tool.function.name,
|
|
523
|
+
description: tool.function.description,
|
|
524
|
+
input_schema: (tool.function.parameters ?? {
|
|
525
|
+
type: "object",
|
|
526
|
+
}),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
this.customToolNames.add(tool.custom.name);
|
|
530
|
+
return {
|
|
531
|
+
type: "custom",
|
|
532
|
+
name: tool.custom.name,
|
|
533
|
+
description: tool.custom.description ?? "",
|
|
534
|
+
input_schema: this.customToolInputSchema(tool.custom.format),
|
|
535
|
+
};
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
customToolInputSchema(format) {
|
|
539
|
+
if (format?.type === "grammar") {
|
|
540
|
+
return {
|
|
541
|
+
type: "object",
|
|
542
|
+
properties: {
|
|
543
|
+
content: {
|
|
544
|
+
type: "string",
|
|
545
|
+
description: `Content must follow grammar (${format.grammar.syntax}): ${format.grammar.definition}`,
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
required: ["content"],
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
return {
|
|
552
|
+
type: "object",
|
|
553
|
+
properties: { content: { type: "string" } },
|
|
554
|
+
required: ["content"],
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
convertWebSearchOptions(options) {
|
|
558
|
+
if (options == null)
|
|
559
|
+
return null;
|
|
560
|
+
return {
|
|
561
|
+
name: "web_search",
|
|
562
|
+
type: "web_search_20250305",
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
convertFile(part) {
|
|
566
|
+
const fileData = part.file.file_data;
|
|
567
|
+
if (!fileData) {
|
|
568
|
+
throw new Error("no file data found");
|
|
569
|
+
}
|
|
570
|
+
const dataUriMatch = fileData.match(/^data:([^;]+);base64,(.+)$/);
|
|
571
|
+
if (!dataUriMatch) {
|
|
572
|
+
throw new Error("invalid base64 file data");
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
type: "document",
|
|
576
|
+
source: {
|
|
577
|
+
type: "base64",
|
|
578
|
+
media_type: dataUriMatch[1],
|
|
579
|
+
data: dataUriMatch[2],
|
|
580
|
+
},
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
safeParseObject(value) {
|
|
584
|
+
if (!value)
|
|
585
|
+
return {};
|
|
586
|
+
try {
|
|
587
|
+
return JSON.parse(value);
|
|
588
|
+
}
|
|
589
|
+
catch {
|
|
590
|
+
return {};
|
|
591
|
+
}
|
|
490
592
|
}
|
|
491
593
|
convertToolChoice(choice, parallelToolCalls) {
|
|
492
594
|
const disableParallel = parallelToolCalls === false ? true : undefined;
|