@zenmux/rosetta-ai 0.0.7 → 0.0.8

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,6 +17,9 @@ 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;
21
24
  private createStreamState;
22
25
  private transitionBlock;
@@ -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() {
@@ -108,13 +110,16 @@ class MessagesToGeminiConverter {
108
110
  for (const part of parts) {
109
111
  if (part.functionCall) {
110
112
  const fc = part.functionCall;
113
+ const id = fc.id ?? fc.name ?? this.generateId();
114
+ if (part.thoughtSignature) {
115
+ content.push(this.createGeminiThoughtSignatureBlock(id, part.thoughtSignature));
116
+ }
111
117
  content.push({
112
118
  type: "tool_use",
113
- id: fc.id ?? fc.name ?? this.generateId(),
119
+ id,
114
120
  name: fc.name ?? "",
115
121
  input: fc.args ?? {},
116
122
  caller: { type: "direct" },
117
- ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
118
123
  });
119
124
  }
120
125
  }
@@ -251,9 +256,12 @@ class MessagesToGeminiConverter {
251
256
  }
252
257
  else if (part.functionCall) {
253
258
  const fc = part.functionCall;
254
- const fcId = fc.id ?? fc.name ?? "";
259
+ const fcId = fc.id ?? fc.name ?? this.generateId();
255
260
  if (!state.seenFunctionCallIds.has(fcId)) {
256
261
  state.seenFunctionCallIds.add(fcId);
262
+ if (part.thoughtSignature) {
263
+ this.emitGeminiThoughtSignatureBlock(state, events, fcId, part.thoughtSignature);
264
+ }
257
265
  this.transitionBlock(state, events, "tool_use");
258
266
  state.toolCallBlockIndices.set(fcId, state.currentBlockIndex);
259
267
  events.push({
@@ -261,11 +269,10 @@ class MessagesToGeminiConverter {
261
269
  index: state.currentBlockIndex,
262
270
  content_block: {
263
271
  type: "tool_use",
264
- id: fc.id ?? fc.name ?? this.generateId(),
272
+ id: fcId,
265
273
  name: fc.name ?? "",
266
274
  input: {},
267
275
  caller: { type: "direct" },
268
- ...(part.thoughtSignature ? { thought_signature: part.thoughtSignature } : {}),
269
276
  },
270
277
  });
271
278
  const args = JSON.stringify(fc.args ?? {});
@@ -434,19 +441,32 @@ class MessagesToGeminiConverter {
434
441
  return;
435
442
  }
436
443
  const parts = [];
444
+ const thoughtSignaturesByToolUseId = msg.content.reduce((map, block) => {
445
+ if (block.type !== "redacted_thinking")
446
+ return map;
447
+ const carrier = this.parseGeminiThoughtSignatureBlock(block);
448
+ if (!carrier)
449
+ return map;
450
+ map.set(carrier.tool_use_id, carrier.signature);
451
+ return map;
452
+ }, new Map());
437
453
  for (const block of msg.content) {
454
+ if (block.type === "redacted_thinking") {
455
+ continue;
456
+ }
438
457
  if (block.type === "text") {
439
458
  parts.push({ text: block.text });
440
459
  }
441
460
  else if (block.type === "tool_use") {
442
461
  const tu = block;
462
+ const thoughtSignature = thoughtSignaturesByToolUseId.get(tu.id);
443
463
  const part = {
444
464
  functionCall: {
445
465
  id: tu.id,
446
466
  name: tu.name,
447
467
  args: tu.input,
448
468
  },
449
- ...(tu.thought_signature ? { thoughtSignature: tu.thought_signature } : {}),
469
+ ...(thoughtSignature ? { thoughtSignature } : {}),
450
470
  };
451
471
  parts.push(part);
452
472
  }
@@ -539,6 +559,53 @@ class MessagesToGeminiConverter {
539
559
  }
540
560
  return results;
541
561
  }
562
+ createGeminiThoughtSignatureBlock(toolUseId, signature) {
563
+ const data = Buffer.from(JSON.stringify({ tool_use_id: toolUseId, signature }), ENCODING.UTF8).toString(ENCODING.BASE64URL);
564
+ return {
565
+ type: "redacted_thinking",
566
+ data: `${GEMINI_THOUGHT_SIGNATURE_PREFIX}${data}`,
567
+ };
568
+ }
569
+ parseGeminiThoughtSignatureBlock(block) {
570
+ if (!block.data.startsWith(GEMINI_THOUGHT_SIGNATURE_PREFIX)) {
571
+ return null;
572
+ }
573
+ try {
574
+ const encoded = block.data.slice(GEMINI_THOUGHT_SIGNATURE_PREFIX.length);
575
+ const parsed = JSON.parse(Buffer.from(encoded, ENCODING.BASE64URL).toString(ENCODING.UTF8));
576
+ if (typeof parsed === "object" &&
577
+ parsed !== null &&
578
+ typeof parsed.tool_use_id === "string" &&
579
+ typeof parsed.signature === "string" &&
580
+ parsed.tool_use_id.length > 0 &&
581
+ parsed.signature.length > 0) {
582
+ return parsed;
583
+ }
584
+ }
585
+ catch {
586
+ return null;
587
+ }
588
+ return null;
589
+ }
590
+ emitGeminiThoughtSignatureBlock(state, events, toolUseId, signature) {
591
+ if (state.currentBlockType !== null) {
592
+ events.push({
593
+ type: "content_block_stop",
594
+ index: state.currentBlockIndex,
595
+ });
596
+ state.currentBlockType = null;
597
+ }
598
+ state.currentBlockIndex++;
599
+ events.push({
600
+ type: "content_block_start",
601
+ index: state.currentBlockIndex,
602
+ content_block: this.createGeminiThoughtSignatureBlock(toolUseId, signature),
603
+ });
604
+ events.push({
605
+ type: "content_block_stop",
606
+ index: state.currentBlockIndex,
607
+ });
608
+ }
542
609
  generateId() {
543
610
  return Math.random().toString(36).substring(2, 15);
544
611
  }
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.8",
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
+ }