@zenmux/rosetta-ai 1.0.21 → 1.0.23

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.
@@ -15,6 +15,9 @@ export declare class MessagesToResponsesConverter {
15
15
  private convertToolChoice;
16
16
  private convertThinking;
17
17
  private statusToStopReason;
18
+ private generateId;
19
+ private packReasoningSignature;
20
+ private unpackReasoningSignature;
18
21
  private createStreamState;
19
22
  }
20
23
  export {};
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MessagesToResponsesConverter = void 0;
4
+ // The Responses backend binds a reasoning item's encrypted_content to its id, so
5
+ // the id must survive the round-trip. Anthropic thinking blocks carry no id, so
6
+ // we pack {id, encrypted_content} into the opaque `signature` string and unpack
7
+ // it when converting the follow-up request back to Responses input.
8
+ const REASONING_SIG_PREFIX = "rosetta-ai/responses-reasoning:v1:";
4
9
  class MessagesToResponsesConverter {
5
10
  streamState;
6
11
  constructor() {
@@ -41,6 +46,9 @@ class MessagesToResponsesConverter {
41
46
  }
42
47
  if (params.thinking) {
43
48
  result.reasoning = this.convertThinking(params.thinking);
49
+ // Ask the backend to return encrypted reasoning so we can reconstruct the
50
+ // Anthropic thinking `signature` on the way back.
51
+ result.include = ["reasoning.encrypted_content"];
44
52
  }
45
53
  if (params.output_config?.format?.type === "json_schema") {
46
54
  result.text = {
@@ -73,7 +81,7 @@ class MessagesToResponsesConverter {
73
81
  content.push({
74
82
  type: "thinking",
75
83
  thinking: summaryText,
76
- signature: "",
84
+ signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
77
85
  });
78
86
  }
79
87
  else if (item.type === "web_search_call" &&
@@ -288,6 +296,21 @@ class MessagesToResponsesConverter {
288
296
  }
289
297
  break;
290
298
  }
299
+ // Emit the thinking signature (Responses encrypted_content) before closing
300
+ // the reasoning block, mirroring Anthropic's signature_delta event.
301
+ if (item?.type === "reasoning") {
302
+ const ri = item;
303
+ if (ri.encrypted_content) {
304
+ events.push({
305
+ type: "content_block_delta",
306
+ index: state.currentBlockIndex,
307
+ delta: {
308
+ type: "signature_delta",
309
+ signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
310
+ },
311
+ });
312
+ }
313
+ }
291
314
  events.push({
292
315
  type: "content_block_stop",
293
316
  index: state.currentBlockIndex,
@@ -415,7 +438,16 @@ class MessagesToResponsesConverter {
415
438
  }
416
439
  else {
417
440
  for (const block of msg.content) {
418
- if (block.type === "text") {
441
+ if (block.type === "thinking") {
442
+ const { id, encryptedContent } = this.unpackReasoningSignature(block.signature);
443
+ input.push({
444
+ type: "reasoning",
445
+ id: id ?? `rs_${this.generateId()}`,
446
+ summary: block.thinking ? [{ type: "summary_text", text: block.thinking }] : [],
447
+ encrypted_content: encryptedContent,
448
+ });
449
+ }
450
+ else if (block.type === "text") {
419
451
  input.push({
420
452
  role: "assistant",
421
453
  type: "message",
@@ -501,6 +533,41 @@ class MessagesToResponsesConverter {
501
533
  }
502
534
  }
503
535
  // --- Private: stream helpers ---
536
+ generateId() {
537
+ return Math.random().toString(36).substring(2, 15);
538
+ }
539
+ // Encode the Responses reasoning id + encrypted_content into an opaque signature
540
+ // string. Returns "" when there is no encrypted_content to preserve.
541
+ packReasoningSignature(id, encryptedContent) {
542
+ if (!encryptedContent)
543
+ return "";
544
+ const data = Buffer.from(JSON.stringify({ id, encrypted_content: encryptedContent }), "utf8").toString("base64url");
545
+ return `${REASONING_SIG_PREFIX}${data}`;
546
+ }
547
+ // Recover { id, encrypted_content } from a packed signature. Falls back to
548
+ // treating the raw signature as encrypted_content (id undefined) for
549
+ // signatures not produced by this converter.
550
+ unpackReasoningSignature(signature) {
551
+ if (!signature)
552
+ return { encryptedContent: null };
553
+ if (!signature.startsWith(REASONING_SIG_PREFIX)) {
554
+ return { encryptedContent: signature };
555
+ }
556
+ try {
557
+ const encoded = signature.slice(REASONING_SIG_PREFIX.length);
558
+ const parsed = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"));
559
+ if (parsed && typeof parsed.encrypted_content === "string") {
560
+ return {
561
+ id: typeof parsed.id === "string" ? parsed.id : undefined,
562
+ encryptedContent: parsed.encrypted_content,
563
+ };
564
+ }
565
+ }
566
+ catch {
567
+ // fall through
568
+ }
569
+ return { encryptedContent: signature };
570
+ }
504
571
  createStreamState() {
505
572
  return {
506
573
  id: "",
@@ -832,7 +832,9 @@ class ResponsesToChatCompletionConverter {
832
832
  if (typed.type === "message") {
833
833
  flushToolCalls();
834
834
  if (typed.role === "user" || typed.role === "system" || typed.role === "developer") {
835
- const content = this.convertInputContent(typed.content);
835
+ const content = typeof typed.content === "string"
836
+ ? typed.content
837
+ : this.convertInputContent(typed.content);
836
838
  messages.push({
837
839
  role: typed.role,
838
840
  content,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",