Skip to content

Commit 81a75d4

Browse files
committed
fix(api): address CodeRabbit review on anthropic family abort handling
- anthropic.ts: use options?.timeoutMs !== undefined (was truthy) so a caller-supplied timeoutMs: 0 is forwarded to the SDK instead of silently dropped; all 4 family providers now share the same defined-check - xai.ts: rethrow the OpenAI SDK's APIUserAbortError (exported from openai v5) unmodified from createMessage/completePrompt alongside native AbortError, since the SDK throws it when the request signal aborts and it would otherwise be mangled by handleOpenAIError - tests: anthropic.spec.ts regression test asserting timeoutMs: 0 reaches the SDK as timeout: 0; xai.spec.ts exposes the real APIUserAbortError in the openai mock and asserts the SDK abort error surfaces as the same instance (unwrapped) through both createMessage and completePrompt
1 parent dfbc1d8 commit 81a75d4

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,15 @@ describe("AnthropicHandler", () => {
656656
await handler.completePrompt("test prompt")
657657
expect(mockCreate).toHaveBeenCalledWith(expect.any(Object), undefined)
658658
})
659+
660+
it("should pass timeout when timeoutMs=0 (defined check)", async () => {
661+
mockCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "response" }] })
662+
await handler.completePrompt("test prompt", { timeoutMs: 0 })
663+
// timeoutMs=0 must be forwarded as an explicit 0 timeout, not dropped as if unset
664+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: mockOptions.apiModelId }), {
665+
timeout: 0,
666+
})
667+
})
659668
})
660669

661670
describe("getModel", () => {

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ const mockResponsesCreate = vitest.hoisted(() => vitest.fn())
1414

1515
vitest.mock("openai", async () => {
1616
const { mockOpenAiResponsesClient } = await import("../../../test-utils/api")
17-
return mockOpenAiResponsesClient(mockResponsesCreate)
17+
const actual = await vi.importActual<typeof import("openai")>("openai")
18+
return {
19+
...mockOpenAiResponsesClient(mockResponsesCreate),
20+
// Expose the real SDK error class so the mock transport can throw it exactly
21+
// the way the OpenAI SDK does when a request signal aborts.
22+
APIUserAbortError: actual.APIUserAbortError,
23+
}
1824
})
1925

20-
import OpenAI from "openai"
26+
import OpenAI, { APIUserAbortError } from "openai"
2127
import type { Anthropic } from "@anthropic-ai/sdk"
2228

2329
import { xaiDefaultModelId, xaiModels } from "@roo-code/types"
@@ -235,6 +241,18 @@ describe("XAIHandler", () => {
235241
await expect(handler.completePrompt("test prompt")).rejects.toThrow(`xAI completion error: ${errorMessage}`)
236242
})
237243

244+
it("completePrompt should surface the SDK APIUserAbortError unmodified on abort", async () => {
245+
const controller = new AbortController()
246+
controller.abort()
247+
const sdkAbortError = new APIUserAbortError()
248+
mockResponsesCreate.mockRejectedValueOnce(sdkAbortError)
249+
250+
// The error must surface as the same SDK instance, not wrapped by handleOpenAIError
251+
await expect(handler.completePrompt("test prompt", { abortSignal: controller.signal })).rejects.toBe(
252+
sdkAbortError,
253+
)
254+
})
255+
238256
it("completePrompt should pass abort signal through to client", async () => {
239257
const controller = new AbortController()
240258
mockResponsesCreate.mockResolvedValueOnce({ output_text: "response" })
@@ -373,6 +391,30 @@ describe("XAIHandler", () => {
373391
await expect(stream.next()).rejects.toThrow(`xAI completion error: ${errorMessage}`)
374392
})
375393

394+
it("createMessage should surface the SDK APIUserAbortError unmodified on abort", async () => {
395+
const abortedController = new AbortController()
396+
abortedController.abort()
397+
const sdkAbortError = new APIUserAbortError()
398+
399+
mockResponsesCreate.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => {
400+
// Mimic the OpenAI SDK: reject with its own APIUserAbortError when the
401+
// request signal is aborted.
402+
if (options?.signal?.aborted) {
403+
throw sdkAbortError
404+
}
405+
return asyncStreamFrom([])
406+
})
407+
408+
const stream = handler.createMessage(
409+
"test prompt",
410+
[],
411+
makeCreateMessageMetadata({ abortSignal: abortedController.signal }),
412+
)
413+
414+
// The SDK error must surface as the same instance, not wrapped by handleOpenAIError
415+
await expect(stream.next()).rejects.toBe(sdkAbortError)
416+
})
417+
376418
it("should reject with AbortError when createMessage is called with an already-aborted signal", async () => {
377419
const abortedController = new AbortController()
378420
abortedController.abort()

‎src/api/providers/anthropic.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
462462
if (options?.abortSignal) {
463463
requestOptions.signal = options.abortSignal
464464
}
465-
if (options?.timeoutMs) {
465+
if (options?.timeoutMs !== undefined) {
466466
requestOptions.timeout = options.timeoutMs
467467
}
468468

‎src/api/providers/xai.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
2-
import OpenAI from "openai"
2+
import OpenAI, { APIUserAbortError } from "openai"
33

44
import { type XAIModelId, xaiDefaultModelId, xaiModels, ApiProviderError } from "@roo-code/types"
55
import { TelemetryService } from "@roo-code/telemetry"
@@ -154,9 +154,12 @@ export class XAIHandler extends BaseProvider implements SingleCompletionHandler
154154
abortSignal ? { signal: abortSignal } : undefined,
155155
)) as unknown as AsyncIterable<any>
156156
} catch (error) {
157-
// Let abort errors propagate unmodified so callers can recognize them
158-
// via error.name === "AbortError".
159-
if (error instanceof Error && error.name === "AbortError") {
157+
// Let abort errors propagate unmodified so callers can recognize them:
158+
// native AbortError (error.name === "AbortError") and the OpenAI SDK's
159+
// APIUserAbortError, which the SDK throws when the request signal aborts
160+
// (the SDK class does not set a distinctive error.name in v5, so use
161+
// instanceof).
162+
if ((error instanceof Error && error.name === "AbortError") || error instanceof APIUserAbortError) {
160163
throw error
161164
}
162165
const errorMessage = error instanceof Error ? error.message : String(error)
@@ -194,9 +197,12 @@ export class XAIHandler extends BaseProvider implements SingleCompletionHandler
194197
// output_text is a convenience field on the Responses API response
195198
return response.output_text || ""
196199
} catch (error) {
197-
// Let abort errors propagate unmodified so callers can recognize them
198-
// via error.name === "AbortError".
199-
if (error instanceof Error && error.name === "AbortError") {
200+
// Let abort errors propagate unmodified so callers can recognize them:
201+
// native AbortError (error.name === "AbortError") and the OpenAI SDK's
202+
// APIUserAbortError, which the SDK throws when the request signal aborts
203+
// (the SDK class does not set a distinctive error.name in v5, so use
204+
// instanceof).
205+
if ((error instanceof Error && error.name === "AbortError") || error instanceof APIUserAbortError) {
200206
throw error
201207
}
202208
const errorMessage = error instanceof Error ? error.message : String(error)

0 commit comments

Comments
 (0)