@zenmux/rosetta-ai 1.0.16 → 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.max_tokens ?? params.max_completion_tokens ?? DEFAULT_MAX_TOKENS,
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
- if (params.tools) {
53
- result.tools = this.convertTools(params.tools);
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
- if (params.user) {
65
- result.metadata = { user_id: params.user };
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 === true) {
74
- result.stream = true;
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
- toolCalls.push({
101
- id: block.id,
102
- type: "function",
103
- function: {
104
- name: block.name,
105
- arguments: JSON.stringify(block.input),
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.length > 0 ? textParts.join("") : null,
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: JSON.parse(tc.function.arguments || "{}"),
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
- .filter((t) => t.type === "function")
485
- .map(t => ({
486
- name: t.function.name,
487
- description: t.function.description,
488
- input_schema: (t.function.parameters ?? { type: "object" }),
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;
@@ -151,6 +151,8 @@ class MessagesToChatCompletionConverter {
151
151
  const outputTokens = u.completion_tokens ?? 0;
152
152
  const totalTokens = u.total_tokens ?? inputTokens + outputTokens;
153
153
  const cachedTokens = u.prompt_tokens_details?.cached_tokens ?? 0;
154
+ const cacheWriteTokens = u.prompt_tokens_details?.cache_write_tokens ?? 0;
155
+ const uncachedInputTokens = Math.max(0, inputTokens - cachedTokens - cacheWriteTokens);
154
156
  const reasoningTokens = u.completion_tokens_details?.reasoning_tokens ?? 0;
155
157
  const webSearch = u.prompt_tokens_details?.web_search ?? 0;
156
158
  return {
@@ -166,7 +168,7 @@ class MessagesToChatCompletionConverter {
166
168
  cached_tokens: cachedTokens,
167
169
  ...(webSearch > 0 ? { web_search: webSearch } : {}),
168
170
  },
169
- input_tokens: inputTokens,
171
+ input_tokens: uncachedInputTokens,
170
172
  output_tokens: outputTokens,
171
173
  cache_read_input_tokens: cachedTokens,
172
174
  server_tool_use: webSearch > 0
@@ -175,7 +177,7 @@ class MessagesToChatCompletionConverter {
175
177
  web_search_requests: webSearch,
176
178
  }
177
179
  : null,
178
- cache_creation_input_tokens: 0,
180
+ cache_creation_input_tokens: cacheWriteTokens,
179
181
  service_tier: "standard",
180
182
  audio_input_tokens: u.prompt_tokens_details?.audio_tokens ?? 0,
181
183
  audio_cache_read_tokens: u.prompt_tokens_details?.audio_cached_tokens ?? 0,
@@ -164,6 +164,7 @@ class MessagesToGeminiConverter {
164
164
  }
165
165
  const promptTokens = promptTokenCount + toolUsePromptTokenCount;
166
166
  const completionTokens = candidatesTokenCount + thoughtsTokenCount;
167
+ const uncachedInputTokens = Math.max(0, promptTokens - cachedContentTokenCount);
167
168
  return {
168
169
  prompt_tokens: promptTokens,
169
170
  completion_tokens: completionTokens,
@@ -180,7 +181,7 @@ class MessagesToGeminiConverter {
180
181
  web_search_queries: webSearchQueriesCount,
181
182
  tool_use: toolUsePromptTokenCount,
182
183
  trafficType,
183
- input_tokens: promptTokens,
184
+ input_tokens: uncachedInputTokens,
184
185
  output_tokens: completionTokens,
185
186
  cache_read_input_tokens: cachedContentTokenCount,
186
187
  cache_creation_input_tokens: 0,
@@ -140,6 +140,8 @@ class MessagesToResponsesConverter {
140
140
  const outputTokens = u.output_tokens ?? 0;
141
141
  const totalTokens = u.total_tokens ?? inputTokens + outputTokens;
142
142
  const cachedTokens = u.input_tokens_details?.cached_tokens ?? 0;
143
+ const cacheWriteTokens = u.input_tokens_details?.cache_write_tokens ?? 0;
144
+ const uncachedInputTokens = Math.max(0, inputTokens - cachedTokens - cacheWriteTokens);
143
145
  const reasoningTokens = u.output_tokens_details?.reasoning_tokens ?? 0;
144
146
  return {
145
147
  completion_tokens: outputTokens,
@@ -154,7 +156,7 @@ class MessagesToResponsesConverter {
154
156
  cached_tokens: cachedTokens,
155
157
  ...(webSearchCount > 0 ? { web_search: webSearchCount } : {}),
156
158
  },
157
- input_tokens: inputTokens,
159
+ input_tokens: uncachedInputTokens,
158
160
  output_tokens: outputTokens,
159
161
  cache_read_input_tokens: cachedTokens,
160
162
  server_tool_use: webSearchCount > 0
@@ -163,7 +165,7 @@ class MessagesToResponsesConverter {
163
165
  web_search_requests: webSearchCount,
164
166
  }
165
167
  : null,
166
- cache_creation_input_tokens: 0,
168
+ cache_creation_input_tokens: cacheWriteTokens,
167
169
  service_tier: "standard",
168
170
  audio_input_tokens: 0,
169
171
  audio_cache_read_tokens: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -60,4 +60,4 @@
60
60
  "typescript": "^6.0.2",
61
61
  "typescript-eslint": "^8.58.2"
62
62
  }
63
- }
63
+ }