@zenmux/rosetta-ai 1.0.23 → 1.0.25

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.
@@ -13,6 +13,8 @@ export declare class ChatCompletionToResponsesConverter {
13
13
  convertStream(stream: AsyncIterable<RespStreamEvent>): AsyncIterable<OpenAI.ChatCompletionChunk>;
14
14
  convertStreamEvent(event: RespStreamEvent): OpenAI.ChatCompletionChunk | OpenAI.ChatCompletionChunk[] | null;
15
15
  private convertMessages;
16
+ private convertReasoningInput;
17
+ private generateId;
16
18
  private convertAssistantMessage;
17
19
  private convertUserContent;
18
20
  private convertTools;
@@ -295,8 +295,7 @@ class ChatCompletionToResponsesConverter {
295
295
  case "response.web_search_call.completed":
296
296
  return null;
297
297
  case "response.output_item.done":
298
- this.handleOutputItemDone(event);
299
- return null;
298
+ return this.handleOutputItemDone(event);
300
299
  case "response.output_text.annotation.added":
301
300
  this.recordStreamAnnotation(event.annotation);
302
301
  return null;
@@ -335,6 +334,11 @@ class ChatCompletionToResponsesConverter {
335
334
  }
336
335
  }
337
336
  else if (msg.role === "assistant") {
337
+ // Reconstruct reasoning items (with encrypted_content + id) so the backend
338
+ // sees the same reasoning it produced. Reasoning precedes the message.
339
+ for (const reasoningItem of this.convertReasoningInput(msg)) {
340
+ input.push(reasoningItem);
341
+ }
338
342
  const toolCalls = msg.tool_calls ?? [];
339
343
  const hasToolCalls = toolCalls.length > 0;
340
344
  const hasLegacyFunctionCall = msg.function_call != null;
@@ -402,6 +406,50 @@ class ChatCompletionToResponsesConverter {
402
406
  }
403
407
  return input;
404
408
  }
409
+ // Rebuild Responses reasoning input items from a CC assistant message's
410
+ // `reasoning_details` (produced by convertReasoning), preserving the original
411
+ // reasoning id + encrypted_content that the Responses backend binds together.
412
+ convertReasoningInput(message) {
413
+ const items = [];
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;
420
+ items.push({
421
+ type: "reasoning",
422
+ id: id ?? `rs_${this.generateId()}`,
423
+ summary,
424
+ ...(encrypted ? { encrypted_content: encrypted } : {}),
425
+ });
426
+ 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
+ }
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
+ }
448
+ return items;
449
+ }
450
+ generateId() {
451
+ return Math.random().toString(36).substring(2, 15);
452
+ }
405
453
  convertAssistantMessage(message) {
406
454
  const content = message.content;
407
455
  if (typeof content === "string") {
@@ -756,7 +804,31 @@ class ChatCompletionToResponsesConverter {
756
804
  if (item.status === "completed" && item.action.type === "search") {
757
805
  this.streamState.webSearchCount++;
758
806
  }
807
+ return null;
759
808
  }
809
+ // Emit the encrypted reasoning (the Responses "signature") once the reasoning
810
+ // item is complete, mirroring the non-streaming convertReasoning output.
811
+ if (item?.type === "reasoning") {
812
+ const ri = item;
813
+ if (ri.encrypted_content) {
814
+ return this.makeChunk({
815
+ role: "assistant",
816
+ content: "",
817
+ ...{
818
+ reasoning_details: [
819
+ {
820
+ id: ri.id,
821
+ index: "0",
822
+ type: "reasoning.encrypted",
823
+ format: "openai-responses-v1",
824
+ data: ri.encrypted_content,
825
+ },
826
+ ],
827
+ },
828
+ });
829
+ }
830
+ }
831
+ return null;
760
832
  }
761
833
  handleTextDelta(event) {
762
834
  const logprobs = this.streamState.includeLogprobs
@@ -16,8 +16,6 @@ export declare class MessagesToResponsesConverter {
16
16
  private convertThinking;
17
17
  private statusToStopReason;
18
18
  private generateId;
19
- private packReasoningSignature;
20
- private unpackReasoningSignature;
21
19
  private createStreamState;
22
20
  }
23
21
  export {};
@@ -1,11 +1,6 @@
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:";
9
4
  class MessagesToResponsesConverter {
10
5
  streamState;
11
6
  constructor() {
@@ -81,7 +76,7 @@ class MessagesToResponsesConverter {
81
76
  content.push({
82
77
  type: "thinking",
83
78
  thinking: summaryText,
84
- signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
79
+ signature: ri.encrypted_content ?? "",
85
80
  });
86
81
  }
87
82
  else if (item.type === "web_search_call" &&
@@ -306,7 +301,7 @@ class MessagesToResponsesConverter {
306
301
  index: state.currentBlockIndex,
307
302
  delta: {
308
303
  type: "signature_delta",
309
- signature: this.packReasoningSignature(ri.id, ri.encrypted_content),
304
+ signature: ri.encrypted_content,
310
305
  },
311
306
  });
312
307
  }
@@ -439,12 +434,14 @@ class MessagesToResponsesConverter {
439
434
  else {
440
435
  for (const block of msg.content) {
441
436
  if (block.type === "thinking") {
442
- const { id, encryptedContent } = this.unpackReasoningSignature(block.signature);
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.
443
440
  input.push({
444
441
  type: "reasoning",
445
- id: id ?? `rs_${this.generateId()}`,
442
+ id: `rs_${this.generateId()}`,
446
443
  summary: block.thinking ? [{ type: "summary_text", text: block.thinking }] : [],
447
- encrypted_content: encryptedContent,
444
+ encrypted_content: block.signature || null,
448
445
  });
449
446
  }
450
447
  else if (block.type === "text") {
@@ -536,38 +533,6 @@ class MessagesToResponsesConverter {
536
533
  generateId() {
537
534
  return Math.random().toString(36).substring(2, 15);
538
535
  }
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
- }
571
536
  createStreamState() {
572
537
  return {
573
538
  id: "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",