Skip to content

Commit 319aafa

Browse files
committed
fix(mistral): cancel unread acceptance failures
1 parent dfc0727 commit 319aafa

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎packages/ai/src/providers/mistral.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,37 @@ describe("Mistral provider", () => {
299299
expect(hostFetch).toHaveBeenCalledOnce();
300300
});
301301

302+
it("cancels an unread Mistral stream when provider acceptance fails", async () => {
303+
mistralMockState.requestThroughHttpClient = true;
304+
const cancel = vi.fn(async () => undefined);
305+
mistralMockState.streamResult = {
306+
cancel,
307+
async *[Symbol.asyncIterator]() {
308+
yield {
309+
data: {
310+
id: "resp-http-ack",
311+
model: "mistral-large-latest",
312+
choices: [{ finishReason: "stop", delta: { content: "ok" } }],
313+
},
314+
};
315+
},
316+
};
317+
configureAiTransportHost({
318+
buildModelFetch: () => async () => new Response("stream", { status: 200 }),
319+
});
320+
const hookError = new Error("acceptance callback failed");
321+
322+
const result = await runSimpleMistralFixture(context, {
323+
onProviderAccepted: () => Promise.reject(hookError),
324+
});
325+
326+
expect(result).toMatchObject({
327+
stopReason: "error",
328+
errorMessage: "acceptance callback failed",
329+
});
330+
expect(cancel).toHaveBeenCalledWith(hookError);
331+
});
332+
302333
it("reports a rejected HTTP response without marking it accepted", async () => {
303334
mistralMockState.requestThroughHttpClient = true;
304335
const hostFetch = vi.fn<typeof fetch>(

‎packages/ai/src/providers/mistral.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ export const streamMistral: StreamFunction<"mistral-conversations", MistralOptio
186186
signal: options?.signal,
187187
});
188188
if (mistralResponse && mistralResponse !== reportedResponse) {
189-
await notifyProviderHttpResponse({ options, response: mistralResponse, model });
189+
try {
190+
await notifyProviderHttpResponse({ options, response: mistralResponse, model });
191+
} catch (error) {
192+
// The SDK EventStream owns the locked response reader after chat.stream resolves.
193+
// Cancellation is best effort and must not delay the lifecycle callback failure.
194+
void mistralStream.cancel(error).catch(() => undefined);
195+
throw error;
196+
}
190197
}
191198
stream.push({ type: "start", partial: output });
192199
await consumeChatStream(model, output, stream, mistralStream);

0 commit comments

Comments
 (0)