@zenmux/rosetta-ai 1.0.25 → 1.0.27

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.
@@ -412,38 +412,26 @@ class ChatCompletionToResponsesConverter {
412
412
  convertReasoningInput(message) {
413
413
  const items = [];
414
414
  const details = message.reasoning_details;
415
- if (Array.isArray(details) && details.length > 0) {
416
- let summary = [];
417
- const flush = (id, encrypted) => {
418
- if (summary.length === 0 && !encrypted)
419
- return;
415
+ if (!Array.isArray(details) || details.length === 0)
416
+ return items;
417
+ // Only reasoning that carries encrypted_content can be sent back: the
418
+ // Responses backend either matches a real stored id or reconstructs from
419
+ // encrypted_content. A summary-only item with a fabricated id is rejected
420
+ // ("item with id rs_... not found"), so drop those groups.
421
+ let summary = [];
422
+ for (const d of details) {
423
+ if (d?.type === "reasoning.summary" && typeof d.summary === "string") {
424
+ summary.push({ type: "summary_text", text: d.summary });
425
+ }
426
+ else if (d?.type === "reasoning.encrypted" && typeof d.data === "string") {
420
427
  items.push({
421
428
  type: "reasoning",
422
- id: id ?? `rs_${this.generateId()}`,
429
+ id: typeof d.id === "string" ? d.id : `rs_${this.generateId()}`,
423
430
  summary,
424
- ...(encrypted ? { encrypted_content: encrypted } : {}),
431
+ encrypted_content: d.data,
425
432
  });
426
433
  summary = [];
427
- };
428
- for (const d of details) {
429
- if (d?.type === "reasoning.summary" && typeof d.summary === "string") {
430
- summary.push({ type: "summary_text", text: d.summary });
431
- }
432
- else if (d?.type === "reasoning.encrypted" && typeof d.data === "string") {
433
- flush(typeof d.id === "string" ? d.id : undefined, d.data);
434
- }
435
434
  }
436
- flush();
437
- return items;
438
- }
439
- // Fallback: only a plain `reasoning` string, no structured details.
440
- const reasoning = message.reasoning;
441
- if (typeof reasoning === "string" && reasoning.length > 0) {
442
- items.push({
443
- type: "reasoning",
444
- id: `rs_${this.generateId()}`,
445
- summary: [{ type: "summary_text", text: reasoning }],
446
- });
447
435
  }
448
436
  return items;
449
437
  }
@@ -16,6 +16,8 @@ export declare class MessagesToResponsesConverter {
16
16
  private convertThinking;
17
17
  private statusToStopReason;
18
18
  private generateId;
19
+ private packReasoningSignature;
20
+ private unpackReasoningSignature;
19
21
  private createStreamState;
20
22
  }
21
23
  export {};
@@ -76,7 +76,7 @@ class MessagesToResponsesConverter {
76
76
  content.push({
77
77
  type: "thinking",
78
78
  thinking: summaryText,
79
- signature: ri.encrypted_content ?? "",
79
+ signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
80
80
  });
81
81
  }
82
82
  else if (item.type === "web_search_call" &&
@@ -301,7 +301,7 @@ class MessagesToResponsesConverter {
301
301
  index: state.currentBlockIndex,
302
302
  delta: {
303
303
  type: "signature_delta",
304
- signature: ri.encrypted_content,
304
+ signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
305
305
  },
306
306
  });
307
307
  }
@@ -434,14 +434,12 @@ class MessagesToResponsesConverter {
434
434
  else {
435
435
  for (const block of msg.content) {
436
436
  if (block.type === "thinking") {
437
- // ponytail: fresh reasoning id, not the backend's original. Fine for
438
- // stateless (store:false) encrypted reasoning; if a backend rejects
439
- // mismatched ids, the original id would need a carrier channel.
437
+ const { id, encryptedContent } = this.unpackReasoningSignature(block.signature);
440
438
  input.push({
441
439
  type: "reasoning",
442
- id: `rs_${this.generateId()}`,
440
+ id: id ?? `rs_${this.generateId()}`,
443
441
  summary: block.thinking ? [{ type: "summary_text", text: block.thinking }] : [],
444
- encrypted_content: block.signature || null,
442
+ encrypted_content: encryptedContent,
445
443
  });
446
444
  }
447
445
  else if (block.type === "text") {
@@ -533,6 +531,34 @@ class MessagesToResponsesConverter {
533
531
  generateId() {
534
532
  return Math.random().toString(36).substring(2, 15);
535
533
  }
534
+ // Pack the Responses reasoning id + encrypted_content into the opaque Anthropic
535
+ // `signature` (base64url JSON, no prefix) so the backend-bound id survives the
536
+ // round-trip. Returns "" when there is no encrypted_content to preserve.
537
+ packReasoningSignature(id, encryptedContent) {
538
+ if (!encryptedContent)
539
+ return "";
540
+ return Buffer.from(JSON.stringify({ id, encrypted_content: encryptedContent }), "utf8").toString("base64url");
541
+ }
542
+ // Recover { id, encrypted_content } from a packed signature. Signatures not
543
+ // produced by this converter (or otherwise undecodable) fall back to being
544
+ // treated as raw encrypted_content with no id.
545
+ unpackReasoningSignature(signature) {
546
+ if (!signature)
547
+ return { encryptedContent: null };
548
+ try {
549
+ const parsed = JSON.parse(Buffer.from(signature, "base64url").toString("utf8"));
550
+ if (parsed && typeof parsed.encrypted_content === "string") {
551
+ return {
552
+ id: typeof parsed.id === "string" ? parsed.id : undefined,
553
+ encryptedContent: parsed.encrypted_content,
554
+ };
555
+ }
556
+ }
557
+ catch {
558
+ // not a packed signature
559
+ }
560
+ return { encryptedContent: signature };
561
+ }
536
562
  createStreamState() {
537
563
  return {
538
564
  id: "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",