@zenmux/rosetta-ai 1.0.34 → 1.0.35

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.
@@ -46,7 +46,15 @@ export declare class ResponsesToChatCompletionConverter {
46
46
  * default of `output_text: ""`.
47
47
  */
48
48
  private collectOutputText;
49
- convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk>): AsyncIterable<RespStreamEvent>;
49
+ convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk> & {
50
+ controller?: AbortController;
51
+ }): AsyncIterable<RespStreamEvent>;
52
+ /**
53
+ * Flush a pending terminal event on normal upstream EOF when no final usage
54
+ * chunk arrived. Call after convertStreamChunk iteration, never on errors or
55
+ * cancellation. Streams without a finish marker are left unfinished.
56
+ */
57
+ finalizeStream(): RespStreamEvent[];
50
58
  convertStreamChunk(chunk: OpenAI.ChatCompletionChunk): RespStreamEvent[];
51
59
  /** Open a message output item and its first content part. */
52
60
  private openMessage;
@@ -226,7 +226,7 @@ class ResponsesToChatCompletionConverter {
226
226
  total_tokens: response.usage.total_tokens,
227
227
  input_tokens_details: {
228
228
  cached_tokens: response.usage.prompt_tokens_details?.cached_tokens ?? 0,
229
- cache_write_tokens: response.usage.prompt_tokens_details?.cache_write_tokens ?? null,
229
+ cache_write_tokens: response.usage.prompt_tokens_details?.cache_write_tokens ?? 0,
230
230
  },
231
231
  output_tokens_details: {
232
232
  reasoning_tokens: response.usage.completion_tokens_details?.reasoning_tokens ?? 0,
@@ -354,9 +354,27 @@ class ResponsesToChatCompletionConverter {
354
354
  yield event;
355
355
  }
356
356
  }
357
+ // The OpenAI SDK may end iteration normally after swallowing an abort.
358
+ if (!stream.controller?.signal.aborted)
359
+ yield* this.finalizeStream();
360
+ }
361
+ /**
362
+ * Flush a pending terminal event on normal upstream EOF when no final usage
363
+ * chunk arrived. Call after convertStreamChunk iteration, never on errors or
364
+ * cancellation. Streams without a finish marker are left unfinished.
365
+ */
366
+ finalizeStream() {
367
+ const state = this.streamState;
368
+ if (!state.finishStatus || state.terminalEmitted)
369
+ return [];
370
+ return (0, utils_1.denamespaceStreamEvents)(this.emitCompleted(state.usage, state.finishStatus));
357
371
  }
358
372
  convertStreamChunk(chunk) {
359
373
  const state = this.streamState;
374
+ if (state.terminalEmitted)
375
+ return [];
376
+ if (chunk.usage)
377
+ state.usage = chunk.usage;
360
378
  const events = [];
361
379
  const choice = chunk.choices?.[0];
362
380
  // First chunk — emit response.created + response.in_progress
@@ -379,10 +397,14 @@ class ResponsesToChatCompletionConverter {
379
397
  if (!choice) {
380
398
  // Usage-only chunk (empty choices) at the end
381
399
  if (chunk.usage) {
382
- events.push(...this.emitCompleted(chunk.usage));
400
+ events.push(...this.closeCurrent());
401
+ events.push(...this.emitCompleted(chunk.usage, state.finishStatus));
383
402
  }
384
403
  return (0, utils_1.denamespaceStreamEvents)(events);
385
404
  }
405
+ // Output has already finished; only accept usage until normal EOF.
406
+ if (state.finishStatus)
407
+ return [];
386
408
  const delta = choice.delta;
387
409
  // Reasoning delta — open a reasoning item if not already open.
388
410
  const reasoning = delta?.reasoning || delta?.reasoning_content;
@@ -591,16 +613,11 @@ class ResponsesToChatCompletionConverter {
591
613
  });
592
614
  }
593
615
  }
594
- // Finish reason — close the open item, then emit the terminal event.
616
+ // Close output immediately, but wait for the final usage chunk or EOF
617
+ // before terminating the Responses stream.
595
618
  if (choice.finish_reason) {
596
619
  events.push(...this.closeCurrent());
597
- const status = this.finishReasonToStatus(choice.finish_reason);
598
- if (chunk.usage) {
599
- events.push(...this.emitCompleted(chunk.usage, status));
600
- }
601
- else {
602
- events.push(...this.emitCompleted(undefined, status));
603
- }
620
+ state.finishStatus = this.finishReasonToStatus(choice.finish_reason);
604
621
  }
605
622
  return (0, utils_1.denamespaceStreamEvents)(events);
606
623
  }
@@ -1104,6 +1121,7 @@ class ResponsesToChatCompletionConverter {
1104
1121
  current: null,
1105
1122
  messageStarted: false,
1106
1123
  toolCallCount: 0,
1124
+ terminalEmitted: false,
1107
1125
  };
1108
1126
  }
1109
1127
  makeSkeletonResponse() {
@@ -1134,6 +1152,9 @@ class ResponsesToChatCompletionConverter {
1134
1152
  }
1135
1153
  emitCompleted(usage, status) {
1136
1154
  const state = this.streamState;
1155
+ if (state.terminalEmitted)
1156
+ return [];
1157
+ state.terminalEmitted = true;
1137
1158
  const finalStatus = status ?? "completed";
1138
1159
  const resp = this.makeSkeletonResponse();
1139
1160
  resp.status = finalStatus;
@@ -1180,7 +1201,7 @@ class ResponsesToChatCompletionConverter {
1180
1201
  total_tokens: usage.total_tokens,
1181
1202
  input_tokens_details: {
1182
1203
  cached_tokens: usage.prompt_tokens_details?.cached_tokens ?? 0,
1183
- cache_write_tokens: usage.prompt_tokens_details?.cache_write_tokens ?? null,
1204
+ cache_write_tokens: usage.prompt_tokens_details?.cache_write_tokens ?? 0,
1184
1205
  },
1185
1206
  output_tokens_details: {
1186
1207
  reasoning_tokens: usage.completion_tokens_details?.reasoning_tokens ?? 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",