Skip to content

Commit 706d9b1

Browse files
committed
feat(api): abort signal support for openrouter, requesty, poe (completePrompt + createMessage)
1 parent 38d5ee0 commit 706d9b1

6 files changed

Lines changed: 1150 additions & 449 deletions

File tree

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

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import OpenAI from "openai"
1919

2020
import { OpenRouterHandler } from "../openrouter"
2121
import { Package } from "../../../shared/package"
22-
import { makeApiHandlerOptions } from "../../../test-utils/api"
22+
import { makeApiHandlerOptions, makeCreateMessageMetadata } from "../../../test-utils/api"
2323
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
2424
import { clearAllMocks } from "../../../test-utils/reset"
2525

@@ -287,7 +287,10 @@ describe("OpenRouterHandler", () => {
287287
temperature: 0,
288288
top_p: undefined,
289289
}),
290-
{ headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } },
290+
{
291+
headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" },
292+
signal: expect.any(AbortSignal),
293+
},
291294
)
292295
})
293296

@@ -330,7 +333,10 @@ describe("OpenRouterHandler", () => {
330333
}),
331334
]),
332335
}),
333-
{ headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } },
336+
{
337+
headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" },
338+
signal: expect.any(AbortSignal),
339+
},
334340
)
335341
})
336342

@@ -537,6 +543,69 @@ describe("OpenRouterHandler", () => {
537543
expect(endChunks).toHaveLength(1)
538544
expect(endChunks[0].id).toBe("call_openrouter_test")
539545
})
546+
it("rejects with AbortError when the external signal is pre-aborted", async () => {
547+
const handler = new OpenRouterHandler(mockOptions)
548+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] })
549+
// The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts.
550+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
551+
client.chat = { completions: { create: mockCreate } }
552+
553+
const controller = new AbortController()
554+
controller.abort()
555+
const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal })
556+
557+
await expect(
558+
handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata).next(),
559+
).rejects.toMatchObject({
560+
name: "AbortError",
561+
})
562+
})
563+
564+
it("aborts the in-flight stream and rejects with AbortError when the external signal aborts", async () => {
565+
const handler = new OpenRouterHandler(mockOptions)
566+
const controller = new AbortController()
567+
568+
let requestSignal: AbortSignal | undefined
569+
const mockCreate = vitest
570+
.fn()
571+
.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => {
572+
requestSignal = options?.signal
573+
// Emulate the OpenAI SDK: the first chunk arrives, then the in-flight
574+
// response body rejects once the request signal aborts.
575+
return (async function* () {
576+
yield { id: "1", choices: [{ delta: { content: "first" } }] }
577+
await new Promise<void>((resolve) => {
578+
if (requestSignal?.aborted) {
579+
resolve()
580+
} else {
581+
requestSignal?.addEventListener("abort", () => resolve(), { once: true })
582+
}
583+
})
584+
const abortError = new Error("The user aborted a request")
585+
abortError.name = "AbortError"
586+
throw abortError
587+
})()
588+
})
589+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
590+
client.chat = { completions: { create: mockCreate } }
591+
592+
const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal })
593+
const generator = handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata)
594+
595+
const chunks: unknown[] = []
596+
const iteration = (async () => {
597+
for await (const chunk of generator) {
598+
chunks.push(chunk)
599+
if (chunk.type === "text") {
600+
// Abort while the stream is still in flight.
601+
controller.abort()
602+
}
603+
}
604+
})()
605+
606+
await expect(iteration).rejects.toMatchObject({ name: "AbortError" })
607+
expect(chunks).toContainEqual({ type: "text", text: "first" })
608+
})
540609
})
541610

542611
describe("completePrompt", () => {
@@ -709,5 +778,85 @@ describe("OpenRouterHandler", () => {
709778
}),
710779
)
711780
})
781+
it("should pass abort signal through to client", async () => {
782+
const handler = new OpenRouterHandler(mockOptions)
783+
const controller = new AbortController()
784+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] })
785+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
786+
client.chat = { completions: { create: mockCreate } }
787+
788+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
789+
expect(mockCreate).toHaveBeenCalledWith(
790+
expect.objectContaining({ model: expect.any(String) }),
791+
expect.objectContaining({ signal: controller.signal }),
792+
)
793+
})
794+
795+
it("should pass timeout through to client", async () => {
796+
const handler = new OpenRouterHandler(mockOptions)
797+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] })
798+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
799+
client.chat = { completions: { create: mockCreate } }
800+
801+
await handler.completePrompt("test prompt", { timeoutMs: 5000 })
802+
expect(mockCreate).toHaveBeenCalledWith(
803+
expect.objectContaining({ model: expect.any(String) }),
804+
expect.objectContaining({ timeout: 5000 }),
805+
)
806+
})
807+
808+
it("should work without options (backward compatible)", async () => {
809+
const handler = new OpenRouterHandler(mockOptions)
810+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] })
811+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
812+
client.chat = { completions: { create: mockCreate } }
813+
814+
const result = await handler.completePrompt("test prompt")
815+
expect(result).toBe("response")
816+
})
817+
818+
it("rejects with AbortError when the signal is pre-aborted", async () => {
819+
const handler = new OpenRouterHandler(mockOptions)
820+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] })
821+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
822+
client.chat = { completions: { create: mockCreate } }
823+
824+
const controller = new AbortController()
825+
controller.abort()
826+
827+
await expect(
828+
handler.completePrompt("test prompt", { abortSignal: controller.signal }),
829+
).rejects.toMatchObject({
830+
name: "AbortError",
831+
})
832+
})
833+
834+
it("rejects with AbortError when aborted mid-flight", async () => {
835+
const handler = new OpenRouterHandler(mockOptions)
836+
const controller = new AbortController()
837+
838+
const mockCreate = vitest
839+
.fn()
840+
.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => {
841+
// Emulate the OpenAI SDK: the in-flight request rejects when the signal aborts.
842+
await new Promise<void>((resolve) => {
843+
if (options?.signal?.aborted) {
844+
resolve()
845+
} else {
846+
options?.signal?.addEventListener("abort", () => resolve(), { once: true })
847+
}
848+
})
849+
const abortError = new Error("The user aborted a request")
850+
abortError.name = "AbortError"
851+
throw abortError
852+
})
853+
const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } }
854+
client.chat = { completions: { create: mockCreate } }
855+
856+
const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal })
857+
controller.abort()
858+
859+
await expect(promise).rejects.toMatchObject({ name: "AbortError" })
860+
})
712861
})
713862
})

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

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ vitest.mock("../fetchers/modelCache", () => ({
7474
import { poeDefaultModelId } from "@roo-code/types"
7575
import { PoeHandler } from "../poe"
7676

77+
import { makeCreateMessageMetadata } from "../../../test-utils/api"
7778
import { clearAllMocks } from "../../../test-utils/reset"
7879

7980
describe("PoeHandler", () => {
@@ -166,6 +167,69 @@ describe("PoeHandler", () => {
166167
expect(chunks).toContainEqual({ type: "text", text: "world!" })
167168
expect(chunks).toContainEqual(expect.objectContaining({ type: "usage", inputTokens: 10, outputTokens: 5 }))
168169
})
170+
it("rejects with AbortError when the external signal is pre-aborted", async () => {
171+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
172+
mockStreamText.mockReturnValue({
173+
fullStream: (async function* () {})(),
174+
usage: Promise.resolve(undefined),
175+
})
176+
177+
const controller = new AbortController()
178+
controller.abort()
179+
const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal })
180+
181+
await expect(
182+
handler.createMessage("system", [{ role: "user" as const, content: "hi" }], metadata).next(),
183+
).rejects.toMatchObject({
184+
name: "AbortError",
185+
})
186+
})
187+
188+
it("aborts the in-flight stream and rejects with AbortError when the external signal aborts", async () => {
189+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
190+
191+
let requestSignal: AbortSignal | undefined
192+
mockStreamText.mockImplementationOnce((args: { abortSignal?: AbortSignal }) => {
193+
requestSignal = args.abortSignal
194+
// Emulate the AI SDK: the first chunk arrives, then the stream errors once
195+
// the abort signal fires.
196+
const fullStream = (async function* () {
197+
yield { type: "text-delta", text: "Hello " }
198+
await new Promise<void>((resolve) => {
199+
if (requestSignal?.aborted) {
200+
resolve()
201+
} else {
202+
requestSignal?.addEventListener("abort", () => resolve(), { once: true })
203+
}
204+
})
205+
const abortError = new Error("The operation was aborted")
206+
abortError.name = "AbortError"
207+
throw abortError
208+
})()
209+
return { fullStream, usage: Promise.resolve(undefined) }
210+
})
211+
212+
const controller = new AbortController()
213+
const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal })
214+
215+
const chunks: unknown[] = []
216+
const iteration = (async () => {
217+
for await (const chunk of handler.createMessage(
218+
"system",
219+
[{ role: "user" as const, content: "hi" }],
220+
metadata,
221+
)) {
222+
chunks.push(chunk)
223+
if (chunk.type === "text") {
224+
// Abort while the stream is still in flight.
225+
controller.abort()
226+
}
227+
}
228+
})()
229+
230+
await expect(iteration).rejects.toMatchObject({ name: "AbortError" })
231+
expect(chunks).toContainEqual({ type: "text", text: "Hello " })
232+
})
169233
})
170234

171235
describe("reasoning", () => {
@@ -311,5 +375,132 @@ describe("PoeHandler", () => {
311375
}),
312376
)
313377
})
378+
it("completePrompt should pass abort signal through to generateText", async () => {
379+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
380+
const controller = new AbortController()
381+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
382+
383+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
384+
expect(mockGenerateText).toHaveBeenCalledWith(
385+
expect.objectContaining({
386+
model: mockLanguageModel,
387+
prompt: "test prompt",
388+
abortSignal: controller.signal,
389+
}),
390+
)
391+
})
392+
393+
it("completePrompt should work without options (backward compatible)", async () => {
394+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
395+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
396+
397+
const result = await handler.completePrompt("test prompt")
398+
expect(result).toBe("response")
399+
expect(mockGenerateText).toHaveBeenCalledWith(
400+
expect.objectContaining({
401+
model: mockLanguageModel,
402+
prompt: "test prompt",
403+
}),
404+
)
405+
})
406+
407+
it("completePrompt should merge signal and timeoutMs into combined abortSignal", async () => {
408+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
409+
const controller = new AbortController()
410+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
411+
412+
await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 5000 })
413+
expect(mockGenerateText).toHaveBeenCalledWith(
414+
expect.objectContaining({
415+
model: mockLanguageModel,
416+
prompt: "test prompt",
417+
abortSignal: expect.any(AbortSignal),
418+
}),
419+
)
420+
// The abortSignal should be a merged signal (not the original controller.signal)
421+
const callArgs = mockGenerateText.mock.calls[0][0]
422+
expect(callArgs.abortSignal).toBeDefined()
423+
expect(callArgs.abortSignal).toBeInstanceOf(AbortSignal)
424+
})
425+
426+
it("completePrompt should use AbortSignal.timeout when only timeoutMs is provided", async () => {
427+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
428+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
429+
430+
await handler.completePrompt("test prompt", { timeoutMs: 3000 })
431+
expect(mockGenerateText).toHaveBeenCalledWith(
432+
expect.objectContaining({
433+
model: mockLanguageModel,
434+
prompt: "test prompt",
435+
abortSignal: expect.any(AbortSignal),
436+
}),
437+
)
438+
const callArgs = mockGenerateText.mock.calls[0][0]
439+
expect(callArgs.abortSignal).toBeDefined()
440+
expect(callArgs.abortSignal).toBeInstanceOf(AbortSignal)
441+
})
442+
443+
it("completePrompt should prefer signal over timeoutMs when both are provided", async () => {
444+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
445+
const controller = new AbortController()
446+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
447+
448+
await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 5000 })
449+
const callArgs = mockGenerateText.mock.calls[0][0]
450+
// Should have a merged abortSignal (not the original controller.signal)
451+
expect(callArgs.abortSignal).toBeInstanceOf(AbortSignal)
452+
expect(callArgs.abortSignal).not.toBe(controller.signal)
453+
})
454+
455+
it("completePrompt rejects with AbortError when the external signal aborts mid-flight", async () => {
456+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
457+
const controller = new AbortController()
458+
// Emulate the AI SDK: the in-flight generation rejects when the abort signal fires.
459+
mockGenerateText.mockImplementationOnce(async (args: { abortSignal?: AbortSignal }) => {
460+
await new Promise<void>((resolve) => {
461+
if (args.abortSignal?.aborted) {
462+
resolve()
463+
} else {
464+
args.abortSignal?.addEventListener("abort", () => resolve(), { once: true })
465+
}
466+
})
467+
const abortError = new Error("The operation was aborted")
468+
abortError.name = "AbortError"
469+
throw abortError
470+
})
471+
472+
const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 5000 })
473+
const callArgs = mockGenerateText.mock.calls[0][0]
474+
expect(callArgs.abortSignal).toBeDefined()
475+
476+
// Abort the external signal before the generation settles.
477+
controller.abort()
478+
479+
await expect(promise).rejects.toMatchObject({ name: "AbortError" })
480+
// The merged signal should be aborted once the user signal aborts.
481+
expect(callArgs.abortSignal.aborted).toBe(true)
482+
})
483+
484+
it("completePrompt should handle timeoutMs=0 as no timeout", async () => {
485+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
486+
mockGenerateText.mockResolvedValueOnce({ text: "response" })
487+
488+
await handler.completePrompt("test prompt", { timeoutMs: 0 })
489+
expect(mockGenerateText).toHaveBeenCalledWith(
490+
expect.objectContaining({
491+
model: mockLanguageModel,
492+
prompt: "test prompt",
493+
}),
494+
)
495+
const callArgs = mockGenerateText.mock.calls[0][0]
496+
expect(callArgs.abortSignal).toBeUndefined()
497+
})
498+
499+
it("completePrompt should handle non-Error values in catch block", async () => {
500+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
501+
mockGenerateText.mockRejectedValueOnce("not an error")
502+
503+
await expect(handler.completePrompt("test prompt")).rejects.toThrow()
504+
})
314505
})
315506
})

0 commit comments

Comments
 (0)