@zenmux/rosetta-ai 0.0.6 → 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,9 +110,13 @@ 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" },
@@ -250,9 +256,12 @@ class MessagesToGeminiConverter {
250
256
  }
251
257
  else if (part.functionCall) {
252
258
  const fc = part.functionCall;
253
- const fcId = fc.id ?? fc.name ?? "";
259
+ const fcId = fc.id ?? fc.name ?? this.generateId();
254
260
  if (!state.seenFunctionCallIds.has(fcId)) {
255
261
  state.seenFunctionCallIds.add(fcId);
262
+ if (part.thoughtSignature) {
263
+ this.emitGeminiThoughtSignatureBlock(state, events, fcId, part.thoughtSignature);
264
+ }
256
265
  this.transitionBlock(state, events, "tool_use");
257
266
  state.toolCallBlockIndices.set(fcId, state.currentBlockIndex);
258
267
  events.push({
@@ -260,7 +269,7 @@ class MessagesToGeminiConverter {
260
269
  index: state.currentBlockIndex,
261
270
  content_block: {
262
271
  type: "tool_use",
263
- id: fc.id ?? fc.name ?? this.generateId(),
272
+ id: fcId,
264
273
  name: fc.name ?? "",
265
274
  input: {},
266
275
  caller: { type: "direct" },
@@ -432,19 +441,34 @@ class MessagesToGeminiConverter {
432
441
  return;
433
442
  }
434
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());
435
453
  for (const block of msg.content) {
454
+ if (block.type === "redacted_thinking") {
455
+ continue;
456
+ }
436
457
  if (block.type === "text") {
437
458
  parts.push({ text: block.text });
438
459
  }
439
460
  else if (block.type === "tool_use") {
440
461
  const tu = block;
441
- parts.push({
462
+ const thoughtSignature = thoughtSignaturesByToolUseId.get(tu.id);
463
+ const part = {
442
464
  functionCall: {
443
465
  id: tu.id,
444
466
  name: tu.name,
445
467
  args: tu.input,
446
468
  },
447
- });
469
+ ...(thoughtSignature ? { thoughtSignature } : {}),
470
+ };
471
+ parts.push(part);
448
472
  }
449
473
  }
450
474
  if (parts.length > 0) {
@@ -535,6 +559,53 @@ class MessagesToGeminiConverter {
535
559
  }
536
560
  return results;
537
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
+ }
538
609
  generateId() {
539
610
  return Math.random().toString(36).substring(2, 15);
540
611
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "0.0.6",
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
+ }