@zenmux/rosetta-ai 0.16.0 → 0.18.0

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.
@@ -9,6 +9,18 @@ export declare class MessagesToChatCompletionConverter {
9
9
  private buildUsage;
10
10
  private generateId;
11
11
  convertStream(stream: AsyncIterable<OpenAI.ChatCompletionChunk>): AsyncIterable<Anthropic.RawMessageStreamEvent>;
12
+ /**
13
+ * Emit the trailing terminal events (message_delta + message_stop) when the
14
+ * backend signalled a finish reason but never sent the trailing usage-only
15
+ * chunk that normally triggers them. Returns an empty array when the stream
16
+ * already terminated cleanly.
17
+ *
18
+ * Callers that drive the stream chunk-by-chunk via convertStreamChunk (instead
19
+ * of convertStream) must invoke this once the upstream stream is exhausted, or
20
+ * a backend that omits the usage chunk will leave the converted stream without
21
+ * a message_stop.
22
+ */
23
+ flushTerminalEvents(): Anthropic.RawMessageStreamEvent[];
12
24
  convertStreamChunk(chunk: OpenAI.ChatCompletionChunk): Anthropic.RawMessageStreamEvent[];
13
25
  private emitTerminalEvents;
14
26
  private createStreamState;
@@ -194,13 +194,27 @@ class MessagesToChatCompletionConverter {
194
194
  }
195
195
  // Safety flush: if the backend signalled finish but never sent the trailing
196
196
  // usage chunk, emit terminal events so the stream is well-formed.
197
+ for (const event of this.flushTerminalEvents()) {
198
+ yield event;
199
+ }
200
+ }
201
+ /**
202
+ * Emit the trailing terminal events (message_delta + message_stop) when the
203
+ * backend signalled a finish reason but never sent the trailing usage-only
204
+ * chunk that normally triggers them. Returns an empty array when the stream
205
+ * already terminated cleanly.
206
+ *
207
+ * Callers that drive the stream chunk-by-chunk via convertStreamChunk (instead
208
+ * of convertStream) must invoke this once the upstream stream is exhausted, or
209
+ * a backend that omits the usage chunk will leave the converted stream without
210
+ * a message_stop.
211
+ */
212
+ flushTerminalEvents() {
213
+ const events = [];
197
214
  if (this.streamState.stopReason !== null && !this.streamState.finished) {
198
- const events = [];
199
215
  this.emitTerminalEvents(this.streamState, events, undefined);
200
- for (const event of events) {
201
- yield event;
202
- }
203
216
  }
217
+ return events;
204
218
  }
205
219
  convertStreamChunk(chunk) {
206
220
  const state = this.streamState;
@@ -216,8 +230,11 @@ class MessagesToChatCompletionConverter {
216
230
  state.id = chunk.id;
217
231
  state.model = chunk.model;
218
232
  const delta = choice.delta;
219
- // First chunk with role - emit message_start
220
- if (!state.messageStarted && delta.role === "assistant") {
233
+ // Emit message_start on the first chunk that carries a choice. Do not gate
234
+ // on delta.role === "assistant": some OpenAI-compatible providers omit role
235
+ // on the first delta (or send it late), and gating would yield content
236
+ // blocks with no preceding message_start, producing a malformed stream.
237
+ if (!state.messageStarted) {
221
238
  state.messageStarted = true;
222
239
  events.push({
223
240
  type: "message_start",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenmux/rosetta-ai",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "Universal translator between AI provider protocols",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",