@zenmux/rosetta-ai 0.0.7 → 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.
@@ -17,7 +17,14 @@ export declare class MessagesToGeminiConverter {
17
17
  private convertThinking;
18
18
  private mapFinishReasonToStopReason;
19
19
  private extractGroundingAnnotations;
20
+ private createGeminiThoughtSignatureBlock;
21
+ private parseGeminiThoughtSignatureBlock;
22
+ private emitGeminiThoughtSignatureBlock;
20
23
  private generateId;
24
+ private generateToolUseId;
25
+ private getFunctionCallRawKey;
26
+ private getStreamToolUseId;
27
+ private collectToolUseNames;
21
28
  private createStreamState;
22
29
  private transitionBlock;
23
30
  }
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MessagesToGeminiConverter = void 0;
4
+ const GEMINI_THOUGHT_SIGNATURE_PREFIX = "rosetta-ai/gemini-function-call-thought-signature:v1:";
5
+ const ENCODING = { UTF8: "utf8", BASE64URL: "base64url" };
4
6
  class MessagesToGeminiConverter {
5
7
  streamState;
6
8
  constructor() {
@@ -10,6 +12,7 @@ class MessagesToGeminiConverter {
10
12
  convertRequest(params) {
11
13
  const systemParts = [];
12
14
  const contents = [];
15
+ const toolUseNames = this.collectToolUseNames(params.messages);
13
16
  if (params.system) {
14
17
  if (typeof params.system === "string") {
15
18
  systemParts.push({ text: params.system });
@@ -22,7 +25,7 @@ class MessagesToGeminiConverter {
22
25
  }
23
26
  for (const msg of params.messages) {
24
27
  if (msg.role === "user") {
25
- this.convertUserMessage(contents, msg);
28
+ this.convertUserMessage(contents, msg, toolUseNames);
26
29
  }
27
30
  else if (msg.role === "assistant") {
28
31
  this.convertAssistantMessage(contents, msg);
@@ -108,13 +111,16 @@ class MessagesToGeminiConverter {
108
111
  for (const part of parts) {
109
112
  if (part.functionCall) {
110
113
  const fc = part.functionCall;
114
+ const id = this.generateToolUseId();
115
+ if (part.thoughtSignature) {
116
+ content.push(this.createGeminiThoughtSignatureBlock(id, part.thoughtSignature));
117
+ }
111
118
  content.push({
112
119
  type: "tool_use",
113
- id: fc.id ?? fc.name ?? this.generateId(),
120
+ id,
114
121
  name: fc.name ?? "",
115
122
  input: fc.args ?? {},
116
123
  caller: { type: "direct" },
117
- ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
118
124
  });
119
125
  }
120
126
  }
@@ -233,6 +239,7 @@ class MessagesToGeminiConverter {
233
239
  },
234
240
  });
235
241
  }
242
+ const functionCallOccurrences = new Map();
236
243
  for (const part of parts) {
237
244
  if (part.thought && part.text) {
238
245
  if (state.currentBlockType !== "thinking") {
@@ -251,9 +258,15 @@ class MessagesToGeminiConverter {
251
258
  }
252
259
  else if (part.functionCall) {
253
260
  const fc = part.functionCall;
254
- const fcId = fc.id ?? fc.name ?? "";
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);
255
265
  if (!state.seenFunctionCallIds.has(fcId)) {
256
266
  state.seenFunctionCallIds.add(fcId);
267
+ if (part.thoughtSignature) {
268
+ this.emitGeminiThoughtSignatureBlock(state, events, fcId, part.thoughtSignature);
269
+ }
257
270
  this.transitionBlock(state, events, "tool_use");
258
271
  state.toolCallBlockIndices.set(fcId, state.currentBlockIndex);
259
272
  events.push({
@@ -261,11 +274,10 @@ class MessagesToGeminiConverter {
261
274
  index: state.currentBlockIndex,
262
275
  content_block: {
263
276
  type: "tool_use",
264
- id: fc.id ?? fc.name ?? this.generateId(),
277
+ id: fcId,
265
278
  name: fc.name ?? "",
266
279
  input: {},
267
280
  caller: { type: "direct" },
268
- ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
269
281
  },
270
282
  });
271
283
  const args = JSON.stringify(fc.args ?? {});
@@ -331,7 +343,7 @@ class MessagesToGeminiConverter {
331
343
  return events;
332
344
  }
333
345
  // --- Private: request helpers ---
334
- convertUserMessage(contents, msg) {
346
+ convertUserMessage(contents, msg, toolUseNames) {
335
347
  if (typeof msg.content === "string") {
336
348
  contents.push({ role: "user", parts: [{ text: msg.content }] });
337
349
  return;
@@ -366,7 +378,7 @@ class MessagesToGeminiConverter {
366
378
  toolResponseParts.push({
367
379
  functionResponse: {
368
380
  id: tr.tool_use_id,
369
- name: tr.tool_use_id,
381
+ name: toolUseNames.get(tr.tool_use_id) || tr.tool_use_id,
370
382
  response: { output },
371
383
  },
372
384
  });
@@ -434,19 +446,32 @@ class MessagesToGeminiConverter {
434
446
  return;
435
447
  }
436
448
  const parts = [];
449
+ const thoughtSignaturesByToolUseId = msg.content.reduce((map, block) => {
450
+ if (block.type !== "redacted_thinking")
451
+ return map;
452
+ const carrier = this.parseGeminiThoughtSignatureBlock(block);
453
+ if (!carrier)
454
+ return map;
455
+ map.set(carrier.tool_use_id, carrier.signature);
456
+ return map;
457
+ }, new Map());
437
458
  for (const block of msg.content) {
459
+ if (block.type === "redacted_thinking") {
460
+ continue;
461
+ }
438
462
  if (block.type === "text") {
439
463
  parts.push({ text: block.text });
440
464
  }
441
465
  else if (block.type === "tool_use") {
442
466
  const tu = block;
467
+ const thoughtSignature = thoughtSignaturesByToolUseId.get(tu.id);
443
468
  const part = {
444
469
  functionCall: {
445
470
  id: tu.id,
446
471
  name: tu.name,
447
472
  args: tu.input,
448
473
  },
449
- ...(tu.thought_signature ? { thoughtSignature: tu.thought_signature } : {}),
474
+ ...(thoughtSignature ? { thoughtSignature } : {}),
450
475
  };
451
476
  parts.push(part);
452
477
  }
@@ -539,9 +564,85 @@ class MessagesToGeminiConverter {
539
564
  }
540
565
  return results;
541
566
  }
567
+ createGeminiThoughtSignatureBlock(toolUseId, signature) {
568
+ const data = Buffer.from(JSON.stringify({ tool_use_id: toolUseId, signature }), ENCODING.UTF8).toString(ENCODING.BASE64URL);
569
+ return {
570
+ type: "redacted_thinking",
571
+ data: `${GEMINI_THOUGHT_SIGNATURE_PREFIX}${data}`,
572
+ };
573
+ }
574
+ parseGeminiThoughtSignatureBlock(block) {
575
+ if (!block.data.startsWith(GEMINI_THOUGHT_SIGNATURE_PREFIX)) {
576
+ return null;
577
+ }
578
+ try {
579
+ const encoded = block.data.slice(GEMINI_THOUGHT_SIGNATURE_PREFIX.length);
580
+ const parsed = JSON.parse(Buffer.from(encoded, ENCODING.BASE64URL).toString(ENCODING.UTF8));
581
+ if (typeof parsed === "object" &&
582
+ parsed !== null &&
583
+ typeof parsed.tool_use_id === "string" &&
584
+ typeof parsed.signature === "string" &&
585
+ parsed.tool_use_id.length > 0 &&
586
+ parsed.signature.length > 0) {
587
+ return parsed;
588
+ }
589
+ }
590
+ catch {
591
+ return null;
592
+ }
593
+ return null;
594
+ }
595
+ emitGeminiThoughtSignatureBlock(state, events, toolUseId, signature) {
596
+ if (state.currentBlockType !== null) {
597
+ events.push({
598
+ type: "content_block_stop",
599
+ index: state.currentBlockIndex,
600
+ });
601
+ state.currentBlockType = null;
602
+ }
603
+ state.currentBlockIndex++;
604
+ events.push({
605
+ type: "content_block_start",
606
+ index: state.currentBlockIndex,
607
+ content_block: this.createGeminiThoughtSignatureBlock(toolUseId, signature),
608
+ });
609
+ events.push({
610
+ type: "content_block_stop",
611
+ index: state.currentBlockIndex,
612
+ });
613
+ }
542
614
  generateId() {
543
615
  return Math.random().toString(36).substring(2, 15);
544
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
+ }
545
646
  // --- Private: stream helpers ---
546
647
  createStreamState() {
547
648
  return {
@@ -553,6 +654,7 @@ class MessagesToGeminiConverter {
553
654
  prevText: "",
554
655
  prevThought: "",
555
656
  seenFunctionCallIds: new Set(),
657
+ streamFunctionCallIds: new Map(),
556
658
  toolCallBlockIndices: new Map(),
557
659
  };
558
660
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "0.0.7",
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",
@@ -60,4 +60,4 @@
60
60
  "typescript": "^6.0.2",
61
61
  "typescript-eslint": "^8.58.2"
62
62
  }
63
- }
63
+ }