Skip to content

Commit e65cc08

Browse files
feat(api): abort signal support for openai, openai-compatible base, zai, kimi-code (round 2)
Round 2 of the abort-signal series: wires request-cancellation signals through the OpenAI family of providers (addresses #404). - openai.ts: all five client.chat.completions.create sites (createMessage streaming + non-streaming, O3-family streaming + non-streaming, completePrompt) build their request config through RequestConfigBuilder; the Azure AI Inference path option and the abort signal compose in one builder (setOption("path", ...) + setAbortSignal). Every catch normalizes abort failures to the Task.ts contract shape (name === "AbortError", message ending in "aborted") via an abort-aware handleOpenAIRequestError; non-abort errors keep the existing provider-prefix wrap. - base-openai-compatible-provider.ts: the shared createMessage / createStream / completePrompt path adopts RequestConfigBuilder for signal forwarding and gains the exported abort-aware error helper handleOpenAIRequestError (reused by zai.ts); subclasses that do not override these methods inherit the wiring. - zai.ts: audit finding fixed - the GLM thinking path in createStream no longer drops requestOptions; the thinking path and the glm-5.3 completePrompt path forward a merged signal (external signal + timeoutMs via mergeAbortSignalAndTimeout). - kimi-code.ts: completePrompt no longer drops CompletePromptOptions - options are forwarded on both the initial call and the 401 OAuth retry. - Design notes: CompletePromptOptions is not ApiHandlerCreateMessageMetadata (required taskId, gap G7), so completePrompt paths use setOption("signal", mergeAbortSignalAndTimeout(...)) instead of setAbortSignal(metadata); gap G5 - mergeAbortSignalAndTimeout treats timeoutMs <= 0 as no explicit timeout. Each call builds a fresh request-local config (no class-field abort controller) with a per-entry-point throwIfAborted guard that rejects before any network I/O. - eslint-suppressions.json: one stale suppression entry pruned (kimi-code.spec.ts @typescript-eslint/no-explicit-any 1 -> 0 - the spec rewrite removed the only as-any cast); no suppression count increased. This branch is STACKED on open PR #1288: the foundation commit e61feb1 (generic RequestConfigBuilder, mergeAbortSignalAndTimeout, mergeAbortSignals, throwIfAborted) rides inside by design.
1 parent e61feb1 commit e65cc08

9 files changed

Lines changed: 852 additions & 47 deletions

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

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// npx vitest run api/providers/__tests__/base-openai-compatible-provider.spec.ts
22

33
import { Anthropic } from "@anthropic-ai/sdk"
4-
import OpenAI from "openai"
4+
import OpenAI, { APIUserAbortError } from "openai"
55

66
import type { ModelInfo } from "@roo-code/types"
77

@@ -14,6 +14,8 @@ const mockCreate = vi.fn()
1414

1515
// Mock OpenAI module
1616
vi.mock("openai", () => ({
17+
// Named export consumed by the provider for abort-error normalization
18+
APIUserAbortError: class extends Error {},
1719
default: vi.fn(function () {
1820
return {
1921
chat: {
@@ -49,6 +51,20 @@ class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-mo
4951
}
5052
}
5153

54+
/**
55+
* Captures the rejection of an operation as an Error. The abort contract always
56+
* throws an Error; the guard keeps strict typing without a cast. Fails the test
57+
* if the operation resolves.
58+
*/
59+
async function captureError(operation: Promise<unknown>): Promise<Error> {
60+
try {
61+
await operation
62+
} catch (error) {
63+
return error instanceof Error ? error : new Error(String(error))
64+
}
65+
throw new Error("Expected the operation to reject")
66+
}
67+
5268
describe("BaseOpenAiCompatibleProvider", () => {
5369
let handler: TestOpenAiCompatibleProvider
5470

@@ -278,6 +294,128 @@ describe("BaseOpenAiCompatibleProvider", () => {
278294
})
279295
})
280296

297+
describe("abort signal wiring", () => {
298+
it("should pass the metadata abort signal to the client request", async () => {
299+
const controller = new AbortController()
300+
mockCreate.mockImplementationOnce(() => asyncStreamFrom([]))
301+
302+
const stream = handler.createMessage("system prompt", [], {
303+
taskId: "test-task",
304+
abortSignal: controller.signal,
305+
})
306+
await stream.next()
307+
308+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), {
309+
signal: controller.signal,
310+
})
311+
})
312+
313+
it("should reject before issuing any request when the abort signal is already aborted", async () => {
314+
const controller = new AbortController()
315+
controller.abort()
316+
317+
await expect(async () => {
318+
for await (const _ of handler.createMessage("system prompt", [], {
319+
taskId: "test-task",
320+
abortSignal: controller.signal,
321+
})) {
322+
// consume
323+
}
324+
}).rejects.toMatchObject({ name: "AbortError", message: "This operation was aborted" })
325+
expect(mockCreate).not.toHaveBeenCalled()
326+
})
327+
328+
it("should normalize the SDK APIUserAbortError from the stream path into the abort contract", async () => {
329+
mockCreate.mockImplementationOnce(() => {
330+
throw new APIUserAbortError()
331+
})
332+
333+
const result = await captureError(
334+
(async () => {
335+
for await (const _ of handler.createMessage("system prompt", [])) {
336+
// consume
337+
}
338+
})(),
339+
)
340+
341+
// The SDK error has name "Error" and a message ending in a period; the
342+
// provider must rethrow the Task.ts contract shape instead.
343+
expect(result.name).toBe("AbortError")
344+
expect(result.message).toBe("TestProvider request aborted")
345+
expect(result.message.endsWith("aborted")).toBe(true)
346+
})
347+
348+
it("should still wrap non-abort request errors with the provider prefix", async () => {
349+
mockCreate.mockImplementationOnce(() => {
350+
throw new Error("boom")
351+
})
352+
353+
const result = await captureError(
354+
(async () => {
355+
for await (const _ of handler.createMessage("system prompt", [])) {
356+
// consume
357+
}
358+
})(),
359+
)
360+
361+
expect(result.message).toBe("TestProvider completion error: boom")
362+
})
363+
364+
it("should pass the completePrompt abort signal to the client request", async () => {
365+
const controller = new AbortController()
366+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
367+
368+
const result = await handler.completePrompt("test prompt", { abortSignal: controller.signal })
369+
370+
expect(result).toBe("response")
371+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), {
372+
signal: controller.signal,
373+
})
374+
})
375+
376+
it("should merge completePrompt timeoutMs into the request signal", async () => {
377+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
378+
379+
await handler.completePrompt("test prompt", { timeoutMs: 5000 })
380+
381+
const requestOptions = mockCreate.mock.calls.at(-1)?.[1]
382+
expect(requestOptions?.signal).toBeInstanceOf(AbortSignal)
383+
expect(requestOptions?.signal.aborted).toBe(false)
384+
})
385+
386+
it("should not set a request signal for zero completePrompt timeoutMs", async () => {
387+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
388+
389+
await handler.completePrompt("test prompt", { timeoutMs: 0 })
390+
391+
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), undefined)
392+
})
393+
394+
it("should reject before any request when the completePrompt signal is already aborted", async () => {
395+
const controller = new AbortController()
396+
controller.abort()
397+
398+
await expect(
399+
handler.completePrompt("test prompt", { abortSignal: controller.signal }),
400+
).rejects.toMatchObject({
401+
name: "AbortError",
402+
message: "This operation was aborted",
403+
})
404+
expect(mockCreate).not.toHaveBeenCalled()
405+
})
406+
407+
it("should normalize the SDK APIUserAbortError from completePrompt", async () => {
408+
mockCreate.mockImplementationOnce(() => {
409+
throw new APIUserAbortError()
410+
})
411+
412+
const result = await captureError(handler.completePrompt("test prompt"))
413+
414+
expect(result.name).toBe("AbortError")
415+
expect(result.message).toBe("TestProvider request aborted")
416+
})
417+
})
418+
281419
describe("Tool call handling", () => {
282420
it("should yield tool_call_end events when finish_reason is tool_calls", async () => {
283421
mockCreate.mockImplementationOnce(() =>

‎src/api/providers/__tests__/kimi-code.spec.ts‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ vi.mock("../../../integrations/kimi-code/oauth", () => ({
1818

1919
vi.mock("../fetchers/modelCache", () => ({ getModels: mockGetModels }))
2020

21+
/**
22+
* Spies on the inherited OpenAI client's chat.completions.create. `client` is
23+
* protected on the OpenAiHandler base (not on the public interface), so it is
24+
* reached through a documented `as unknown as` double assertion (AGENTS.md
25+
* last resort; no `as any`).
26+
*/
27+
function completionsCreate(handler: KimiCodeHandler): ReturnType<typeof vi.fn> {
28+
const client = (
29+
handler as unknown as {
30+
client: { chat: { completions: Record<string, (...args: never[]) => never> } }
31+
}
32+
).client
33+
return vi.spyOn(client.chat.completions, "create") as unknown as ReturnType<typeof vi.fn>
34+
}
35+
36+
/** Captures the rejection of an operation; fails the test if it resolves. */
37+
async function captureError(operation: Promise<unknown>): Promise<Error> {
38+
try {
39+
await operation
40+
} catch (error) {
41+
// Abort normalization always throws an Error; the guard keeps strict typing without casts.
42+
return error instanceof Error ? error : new Error(String(error))
43+
}
44+
throw new Error("Expected the operation to reject")
45+
}
46+
2147
describe("KimiCodeHandler", () => {
2248
beforeEach(() => {
2349
clearAllMocks()
@@ -117,8 +143,7 @@ describe("KimiCodeHandler", () => {
117143
it("force-refreshes and retries exactly once after a non-streaming OAuth 401", async () => {
118144
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
119145
const unauthorized = Object.assign(new Error("Unauthorized"), { status: 401 })
120-
const createCompletion = vi
121-
.spyOn((handler as any).client.chat.completions, "create")
146+
const createCompletion = completionsCreate(handler)
122147
.mockRejectedValueOnce(unauthorized)
123148
.mockResolvedValueOnce({ choices: [{ message: { content: "retried" } }] })
124149

@@ -235,4 +260,89 @@ describe("KimiCodeHandler", () => {
235260
})
236261
expect(handler.getModel().reasoning).toEqual({ reasoning_effort: "max" })
237262
})
263+
264+
it("forwards the metadata abort signal to the inherited OpenAI SDK request", async () => {
265+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
266+
const controller = new AbortController()
267+
const streamChunks = (async function* () {
268+
yield { choices: [{ delta: { content: "hi" } }] }
269+
})()
270+
const createCompletion = completionsCreate(handler).mockResolvedValueOnce(streamChunks)
271+
272+
const gen = handler.createMessage("system", [{ role: "user", content: "test" }], {
273+
taskId: "test-task",
274+
abortSignal: controller.signal,
275+
})
276+
const first = await gen.next()
277+
278+
expect(first.value).toEqual({ type: "text", text: "hi" })
279+
expect(createCompletion).toHaveBeenCalledWith(expect.anything(), { signal: controller.signal })
280+
})
281+
282+
it("rejects before any request when the createMessage abort signal is already aborted", async () => {
283+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
284+
const controller = new AbortController()
285+
controller.abort()
286+
const createCompletion = completionsCreate(handler)
287+
288+
const gen = handler.createMessage("system", [{ role: "user", content: "test" }], {
289+
taskId: "test-task",
290+
abortSignal: controller.signal,
291+
})
292+
293+
await expect(async () => {
294+
for await (const _ of gen) {
295+
// consume
296+
}
297+
}).rejects.toMatchObject({ name: "AbortError", message: "This operation was aborted" })
298+
expect(createCompletion).not.toHaveBeenCalled()
299+
})
300+
301+
it("forwards completePrompt abort options through the override on both 401 retry attempts", async () => {
302+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
303+
const unauthorized = Object.assign(new Error("Unauthorized"), { status: 401 })
304+
const createCompletion = completionsCreate(handler)
305+
.mockRejectedValueOnce(unauthorized)
306+
.mockResolvedValueOnce({ choices: [{ message: { content: "retried" } }] })
307+
const controller = new AbortController()
308+
309+
await expect(handler.completePrompt("test", { abortSignal: controller.signal })).resolves.toBe("retried")
310+
expect(mockForceRefreshAccessToken).toHaveBeenCalledOnce()
311+
expect(createCompletion).toHaveBeenCalledTimes(2)
312+
for (const call of createCompletion.mock.calls) {
313+
expect(call[1]).toEqual({ signal: controller.signal })
314+
}
315+
})
316+
317+
it("rejects before any request when the completePrompt signal is already aborted", async () => {
318+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
319+
const controller = new AbortController()
320+
controller.abort()
321+
const createCompletion = completionsCreate(handler)
322+
323+
await expect(handler.completePrompt("test", { abortSignal: controller.signal })).rejects.toMatchObject({
324+
name: "AbortError",
325+
message: "This operation was aborted",
326+
})
327+
expect(createCompletion).not.toHaveBeenCalled()
328+
})
329+
330+
it("surfaces a normalized AbortError when the SDK aborts a streaming request", async () => {
331+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
332+
// The real SDK class: this spec does not mock the openai module.
333+
const { APIUserAbortError } = await import("openai")
334+
completionsCreate(handler).mockRejectedValueOnce(new APIUserAbortError())
335+
336+
const gen = handler.createMessage("system", [{ role: "user", content: "test" }], { taskId: "test-task" })
337+
const result = await captureError(
338+
(async () => {
339+
for await (const _ of gen) {
340+
// consume
341+
}
342+
})(),
343+
)
344+
345+
expect(result.name).toBe("AbortError")
346+
expect(result.message).toBe("OpenAI request aborted")
347+
})
238348
})

0 commit comments

Comments
 (0)