@zenmux/rosetta-ai 0.0.8 → 0.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.
@@ -21,6 +21,10 @@ export declare class MessagesToGeminiConverter {
21
21
  private parseGeminiThoughtSignatureBlock;
22
22
  private emitGeminiThoughtSignatureBlock;
23
23
  private generateId;
24
+ private generateToolUseId;
25
+ private getFunctionCallRawKey;
26
+ private getStreamToolUseId;
27
+ private collectToolUseNames;
24
28
  private createStreamState;
25
29
  private transitionBlock;
26
30
  }
@@ -12,6 +12,7 @@ class MessagesToGeminiConverter {
12
12
  convertRequest(params) {
13
13
  const systemParts = [];
14
14
  const contents = [];
15
+ const toolUseNames = this.collectToolUseNames(params.messages);
15
16
  if (params.system) {
16
17
  if (typeof params.system === "string") {
17
18
  systemParts.push({ text: params.system });
@@ -24,7 +25,7 @@ class MessagesToGeminiConverter {
24
25
  }
25
26
  for (const msg of params.messages) {
26
27
  if (msg.role === "user") {
27
- this.convertUserMessage(contents, msg);
28
+ this.convertUserMessage(contents, msg, toolUseNames);
28
29
  }
29
30
  else if (msg.role === "assistant") {
30
31
  this.convertAssistantMessage(contents, msg);
@@ -110,7 +111,7 @@ class MessagesToGeminiConverter {
110
111
  for (const part of parts) {
111
112
  if (part.functionCall) {
112
113
  const fc = part.functionCall;
113
- const id = fc.id ?? fc.name ?? this.generateId();
114
+ const id = this.generateToolUseId();
114
115
  if (part.thoughtSignature) {
115
116
  content.push(this.createGeminiThoughtSignatureBlock(id, part.thoughtSignature));
116
117
  }
@@ -238,6 +239,7 @@ class MessagesToGeminiConverter {
238
239
  },
239
240
  });
240
241
  }
242
+ const functionCallOccurrences = new Map();
241
243
  for (const part of parts) {
242
244
  if (part.thought && part.text) {
243
245
  if (state.currentBlockType !== "thinking") {
@@ -256,7 +258,10 @@ class MessagesToGeminiConverter {
256
258
  }
257
259
  else if (part.functionCall) {
258
260
  const fc = part.functionCall;
259
- const fcId = fc.id ?? fc.name ?? this.generateId();
261
+ const rawKey = this.getFunctionCallRawKey(fc);
262
+ const occurrence = functionCallOccurrences.get(rawKey) ?? 0;
263
+ functionCallOccurrences.set(rawKey, occurrence + 1);
264
+ const fcId = this.getStreamToolUseId(state, fc, occurrence);
260
265
  if (!state.seenFunctionCallIds.has(fcId)) {
261
266
  state.seenFunctionCallIds.add(fcId);
262
267
  if (part.thoughtSignature) {
@@ -338,7 +343,7 @@ class MessagesToGeminiConverter {
338
343
  return events;
339
344
  }
340
345
  // --- Private: request helpers ---
341
- convertUserMessage(contents, msg) {
346
+ convertUserMessage(contents, msg, toolUseNames) {
342
347
  if (typeof msg.content === "string") {
343
348
  contents.push({ role: "user", parts: [{ text: msg.content }] });
344
349
  return;
@@ -373,7 +378,7 @@ class MessagesToGeminiConverter {
373
378
  toolResponseParts.push({
374
379
  functionResponse: {
375
380
  id: tr.tool_use_id,
376
- name: tr.tool_use_id,
381
+ name: toolUseNames.get(tr.tool_use_id) || tr.tool_use_id,
377
382
  response: { output },
378
383
  },
379
384
  });
@@ -609,6 +614,35 @@ class MessagesToGeminiConverter {
609
614
  generateId() {
610
615
  return Math.random().toString(36).substring(2, 15);
611
616
  }
617
+ generateToolUseId() {
618
+ return `toolu_${Date.now()}_${this.generateId()}`;
619
+ }
620
+ getFunctionCallRawKey(fc) {
621
+ return fc.id ?? fc.name ?? "__missing_function_call_id__";
622
+ }
623
+ getStreamToolUseId(state, fc, occurrence) {
624
+ const key = `${this.getFunctionCallRawKey(fc)}:${occurrence}`;
625
+ let id = state.streamFunctionCallIds.get(key);
626
+ if (!id) {
627
+ id = this.generateToolUseId();
628
+ state.streamFunctionCallIds.set(key, id);
629
+ }
630
+ return id;
631
+ }
632
+ collectToolUseNames(messages) {
633
+ return messages.reduce((toolUseNames, msg) => {
634
+ if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
635
+ return toolUseNames;
636
+ }
637
+ for (const block of msg.content) {
638
+ if (block.type !== "tool_use")
639
+ continue;
640
+ const tu = block;
641
+ toolUseNames.set(tu.id, tu.name);
642
+ }
643
+ return toolUseNames;
644
+ }, new Map());
645
+ }
612
646
  // --- Private: stream helpers ---
613
647
  createStreamState() {
614
648
  return {
@@ -620,6 +654,7 @@ class MessagesToGeminiConverter {
620
654
  prevText: "",
621
655
  prevThought: "",
622
656
  seenFunctionCallIds: new Set(),
657
+ streamFunctionCallIds: new Map(),
623
658
  toolCallBlockIndices: new Map(),
624
659
  };
625
660
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",