Skip to content

Commit 35c95ea

Browse files
fix(api): normalize abort errors raised during stream iteration
Root cause: the creation-site catches only cover chat.completions.create; an abort that surfaces while the async iterator is being consumed (APIUserAbortError / fetch-level AbortError thrown mid-stream) leaked as the raw SDK error, which violates the Task.ts abort contract (an Error whose name is "AbortError" and whose message ends in "aborted"). The stream iteration is now wrapped and normalized through the same abort-aware handleOpenAIRequestError used at the creation sites: - base-openai-compatible-provider.ts: the createMessage for-await loop - openai.ts: the streaming createMessage for-await loop - openai.ts: the o3-family yield* this.handleStreamResponse(stream) The Z.ai thinking path inherits the base createMessage iteration, so it is covered by the base-provider fix. Non-abort iteration errors keep the existing provider-prefix wrap. Adds four regression tests (base, openai streaming, o3-family streaming, zai thinking path) with iterators that reject with APIUserAbortError after yielding the first chunk. Addresses the CodeRabbit pre-merge review comment on PR Zoo-Code-Org#1311.
1 parent 4a31692 commit 35c95ea

5 files changed

Lines changed: 170 additions & 55 deletions

File tree

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,29 @@ describe("BaseOpenAiCompatibleProvider", () => {
345345
expect(result.message.endsWith("aborted")).toBe(true)
346346
})
347347

348+
it("should normalize an abort error raised during stream iteration into the abort contract", async () => {
349+
mockCreate.mockImplementationOnce(() =>
350+
(async function* () {
351+
yield { choices: [{ delta: { content: "partial" } }] }
352+
throw new APIUserAbortError()
353+
})(),
354+
)
355+
356+
const result = await captureError(
357+
(async () => {
358+
for await (const _ of handler.createMessage("system prompt", [])) {
359+
// consume
360+
}
361+
})(),
362+
)
363+
364+
// The iterator rejects after the first chunk; the provider must normalize
365+
// it to the Task.ts contract shape (name + message ending in "aborted").
366+
expect(result.name).toBe("AbortError")
367+
expect(result.message).toBe("TestProvider request aborted")
368+
expect(result.message.endsWith("aborted")).toBe(true)
369+
})
370+
348371
it("should still wrap non-abort request errors with the provider prefix", async () => {
349372
mockCreate.mockImplementationOnce(() => {
350373
throw new Error("boom")

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,30 @@ describe("OpenAiHandler", () => {
10211021
expect(result.message.endsWith("aborted")).toBe(true)
10221022
})
10231023

1024+
it("should normalize an abort error raised during stream iteration into the abort contract", async () => {
1025+
const controller = new AbortController()
1026+
mockCreate.mockImplementationOnce(() =>
1027+
(async function* () {
1028+
yield { choices: [{ delta: { content: "partial" } }] }
1029+
throw new APIUserAbortError()
1030+
})(),
1031+
)
1032+
const metadata = { taskId: "test-task", abortSignal: controller.signal }
1033+
const result = await captureError(
1034+
(async () => {
1035+
for await (const _ of handler.createMessage("system prompt", [], metadata)) {
1036+
// consume
1037+
}
1038+
})(),
1039+
)
1040+
1041+
// The iterator rejects after the first chunk; the provider must normalize
1042+
// it to the Task.ts contract shape instead of leaking the raw SDK error.
1043+
expect(result.name).toBe("AbortError")
1044+
expect(result.message).toBe("OpenAI request aborted")
1045+
expect(result.message.endsWith("aborted")).toBe(true)
1046+
})
1047+
10241048
it("should normalize the SDK APIUserAbortError from completePrompt without an external signal", async () => {
10251049
mockCreate.mockImplementationOnce(() => {
10261050
throw new APIUserAbortError()
@@ -1074,6 +1098,28 @@ describe("OpenAiHandler", () => {
10741098
expect(result.message).toBe("OpenAI request aborted")
10751099
})
10761100

1101+
it("should normalize an abort error raised during o3-family stream iteration into the abort contract", async () => {
1102+
mockCreate.mockImplementationOnce(() =>
1103+
(async function* () {
1104+
yield { choices: [{ delta: { content: "partial" } }] }
1105+
throw new APIUserAbortError()
1106+
})(),
1107+
)
1108+
const o3Handler = new OpenAiHandler({ ...mockOptions, openAiModelId: "o3-mini" })
1109+
const result = await captureError(
1110+
(async () => {
1111+
for await (const _ of o3Handler.createMessage("system prompt", [])) {
1112+
// consume
1113+
}
1114+
})(),
1115+
)
1116+
1117+
// handleStreamResponse consumes the iterator; a mid-stream abort must be
1118+
// normalized to the Task.ts contract shape, not leak as the raw SDK error.
1119+
expect(result.name).toBe("AbortError")
1120+
expect(result.message).toBe("OpenAI request aborted")
1121+
})
1122+
10771123
it("should normalize the SDK APIUserAbortError from the o3-family non-streaming path", async () => {
10781124
mockCreate.mockImplementationOnce(() => {
10791125
throw new APIUserAbortError()

‎src/api/providers/__tests__/zai.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,34 @@ describe("ZAiHandler", () => {
650650
expect(result.message.endsWith("aborted")).toBe(true)
651651
})
652652

653+
it("createMessage should normalize an abort error raised during stream iteration on the thinking path", async () => {
654+
const thinkingHandler = new ZAiHandler({
655+
apiModelId: "glm-4.7",
656+
zaiApiKey: "test-zai-api-key",
657+
zaiApiLine: "international_coding",
658+
})
659+
mockCreate.mockImplementationOnce(() =>
660+
(async function* () {
661+
yield { choices: [{ delta: { content: "partial" } }] }
662+
throw new APIUserAbortError()
663+
})(),
664+
)
665+
666+
const result = await captureError(
667+
(async () => {
668+
for await (const _ of thinkingHandler.createMessage("system prompt", [])) {
669+
// consume
670+
}
671+
})(),
672+
)
673+
674+
// The thinking path inherits stream iteration from the base provider; a
675+
// mid-stream abort must normalize to the Task.ts contract shape.
676+
expect(result.name).toBe("AbortError")
677+
expect(result.message).toBe("Z.ai request aborted")
678+
expect(result.message.endsWith("aborted")).toBe(true)
679+
})
680+
653681
it("completePrompt should pass the abort signal through to the client", async () => {
654682
const controller = new AbortController()
655683
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })

‎src/api/providers/base-openai-compatible-provider.ts‎

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -162,57 +162,63 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
162162
let lastUsage: OpenAI.CompletionUsage | undefined
163163
const activeToolCallIds = new Set<string>()
164164

165-
for await (const chunk of stream) {
166-
// Check for provider-specific error responses (e.g., MiniMax base_resp)
167-
const chunkAny = chunk as any
168-
if (chunkAny.base_resp?.status_code && chunkAny.base_resp.status_code !== 0) {
169-
throw new Error(
170-
`${this.providerName} API Error (${chunkAny.base_resp.status_code}): ${chunkAny.base_resp.status_msg || "Unknown error"}`,
171-
)
172-
}
165+
try {
166+
for await (const chunk of stream) {
167+
// Check for provider-specific error responses (e.g., MiniMax base_resp)
168+
const chunkAny = chunk as any
169+
if (chunkAny.base_resp?.status_code && chunkAny.base_resp.status_code !== 0) {
170+
throw new Error(
171+
`${this.providerName} API Error (${chunkAny.base_resp.status_code}): ${chunkAny.base_resp.status_msg || "Unknown error"}`,
172+
)
173+
}
173174

174-
const delta = chunk.choices?.[0]?.delta
175-
const finishReason = chunk.choices?.[0]?.finish_reason
175+
const delta = chunk.choices?.[0]?.delta
176+
const finishReason = chunk.choices?.[0]?.finish_reason
176177

177-
if (delta?.content) {
178-
for (const processedChunk of matcher.update(delta.content)) {
179-
yield processedChunk
178+
if (delta?.content) {
179+
for (const processedChunk of matcher.update(delta.content)) {
180+
yield processedChunk
181+
}
180182
}
181-
}
182183

183-
const reasoningText = extractReasoningFromDelta(delta)
184-
if (reasoningText) {
185-
yield { type: "reasoning", text: reasoningText }
186-
}
184+
const reasoningText = extractReasoningFromDelta(delta)
185+
if (reasoningText) {
186+
yield { type: "reasoning", text: reasoningText }
187+
}
187188

188-
// Emit raw tool call chunks - NativeToolCallParser handles state management
189-
if (delta?.tool_calls) {
190-
for (const toolCall of delta.tool_calls) {
191-
if (toolCall.id) {
192-
activeToolCallIds.add(toolCall.id)
193-
}
194-
yield {
195-
type: "tool_call_partial",
196-
index: toolCall.index,
197-
id: toolCall.id,
198-
name: toolCall.function?.name,
199-
arguments: toolCall.function?.arguments,
189+
// Emit raw tool call chunks - NativeToolCallParser handles state management
190+
if (delta?.tool_calls) {
191+
for (const toolCall of delta.tool_calls) {
192+
if (toolCall.id) {
193+
activeToolCallIds.add(toolCall.id)
194+
}
195+
yield {
196+
type: "tool_call_partial",
197+
index: toolCall.index,
198+
id: toolCall.id,
199+
name: toolCall.function?.name,
200+
arguments: toolCall.function?.arguments,
201+
}
200202
}
201203
}
202-
}
203204

204-
// Emit tool_call_end events when finish_reason is "tool_calls"
205-
// This ensures tool calls are finalized even if the stream doesn't properly close
206-
if (finishReason === "tool_calls" && activeToolCallIds.size > 0) {
207-
for (const id of activeToolCallIds) {
208-
yield { type: "tool_call_end", id }
205+
// Emit tool_call_end events when finish_reason is "tool_calls"
206+
// This ensures tool calls are finalized even if the stream doesn't properly close
207+
if (finishReason === "tool_calls" && activeToolCallIds.size > 0) {
208+
for (const id of activeToolCallIds) {
209+
yield { type: "tool_call_end", id }
210+
}
211+
activeToolCallIds.clear()
209212
}
210-
activeToolCallIds.clear()
211-
}
212213

213-
if (chunk.usage) {
214-
lastUsage = chunk.usage
214+
if (chunk.usage) {
215+
lastUsage = chunk.usage
216+
}
215217
}
218+
} catch (error) {
219+
// The creation-site catch does not cover errors raised by the async
220+
// iterator itself (e.g. a mid-stream abort); normalize them the same way.
221+
throw handleOpenAIRequestError(error, this.providerName, metadata?.abortSignal)
216222
}
217223

218224
if (lastUsage) {

‎src/api/providers/openai.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,32 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
228228
let lastUsage
229229
const activeToolCallIds = new Set<string>()
230230

231-
for await (const chunk of stream) {
232-
const delta = chunk.choices?.[0]?.delta ?? {}
233-
const finishReason = chunk.choices?.[0]?.finish_reason
231+
try {
232+
for await (const chunk of stream) {
233+
const delta = chunk.choices?.[0]?.delta ?? {}
234+
const finishReason = chunk.choices?.[0]?.finish_reason
234235

235-
if (delta.content) {
236-
for (const chunk of matcher.update(delta.content)) {
237-
yield chunk
236+
if (delta.content) {
237+
for (const chunk of matcher.update(delta.content)) {
238+
yield chunk
239+
}
238240
}
239-
}
240241

241-
const reasoningText = extractReasoningFromDelta(delta)
242-
if (reasoningText) {
243-
yield { type: "reasoning", text: reasoningText }
244-
}
242+
const reasoningText = extractReasoningFromDelta(delta)
243+
if (reasoningText) {
244+
yield { type: "reasoning", text: reasoningText }
245+
}
245246

246-
yield* this.processToolCalls(delta, finishReason, activeToolCallIds)
247+
yield* this.processToolCalls(delta, finishReason, activeToolCallIds)
247248

248-
if (chunk.usage) {
249-
lastUsage = chunk.usage
249+
if (chunk.usage) {
250+
lastUsage = chunk.usage
251+
}
250252
}
253+
} catch (error) {
254+
// The creation-site catch does not cover errors raised by the async
255+
// iterator itself (e.g. a mid-stream abort); normalize them the same way.
256+
throw handleOpenAIRequestError(error, this.providerName, metadata?.abortSignal)
251257
}
252258

253259
for (const chunk of matcher.final()) {
@@ -455,7 +461,13 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
455461
throw handleOpenAIRequestError(error, this.providerName, metadata?.abortSignal)
456462
}
457463

458-
yield* this.handleStreamResponse(stream)
464+
try {
465+
yield* this.handleStreamResponse(stream)
466+
} catch (error) {
467+
// The creation-site catch does not cover errors raised by the async
468+
// iterator itself (e.g. a mid-stream abort); normalize them the same way.
469+
throw handleOpenAIRequestError(error, this.providerName, metadata?.abortSignal)
470+
}
459471
} else {
460472
const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = {
461473
model: modelId,

0 commit comments

Comments
 (0)