Skip to content

Commit 1291d8b

Browse files
committed
feat(api): abort signal support for gemini, mistral, lite-llm (completePrompt + createMessage)
1 parent 38d5ee0 commit 1291d8b

8 files changed

Lines changed: 751 additions & 62 deletions

File tree

‎src/api/providers/__tests__/gemini-handler.spec.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@ describe("GeminiHandler backend support", () => {
5555
expect(promptConfig.tools).toBeUndefined()
5656
})
5757

58+
it("completePrompt should pass abort signal through to client via httpOptions", async () => {
59+
const options = {
60+
apiProvider: "gemini",
61+
enableUrlContext: false,
62+
enableGrounding: false,
63+
} as ApiHandlerOptions
64+
const handler = new GeminiHandler(options)
65+
66+
const controller = new AbortController()
67+
const stub = vi.fn().mockResolvedValue({ text: "response" })
68+
handler["client"].models.generateContent = stub
69+
70+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
71+
72+
expect(stub).toHaveBeenCalledWith(
73+
expect.objectContaining({
74+
config: expect.objectContaining({
75+
abortSignal: controller.signal,
76+
}),
77+
}),
78+
)
79+
})
80+
81+
it("completePrompt should work without options (backward compatible)", async () => {
82+
const options = {
83+
apiProvider: "gemini",
84+
enableUrlContext: false,
85+
enableGrounding: false,
86+
} as ApiHandlerOptions
87+
const handler = new GeminiHandler(options)
88+
89+
const stub = vi.fn().mockResolvedValue({ text: "response" })
90+
handler["client"].models.generateContent = stub
91+
92+
const result = await handler.completePrompt("test prompt")
93+
expect(result).toBe("response")
94+
})
95+
5896
describe("error scenarios", () => {
5997
it("should handle grounding metadata extraction failure gracefully", async () => {
6098
const options = {

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

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ vitest.mock("@roo-code/telemetry", () => ({
1212

1313
import { Anthropic } from "@anthropic-ai/sdk"
1414

15+
import type { GenerateContentResponse } from "@google/genai"
16+
1517
import { type ModelInfo, geminiDefaultModelId, ApiProviderError } from "@roo-code/types"
1618

1719
import { t } from "i18next"
1820
import { GeminiHandler } from "../gemini"
1921
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
22+
import { makeCreateMessageMetadata } from "../../../test-utils/api"
2023

2124
const GEMINI_MODEL_NAME = geminiDefaultModelId
2225

26+
// @google/genai's GenerateContentResponse exposes `text` via a getter backed by
27+
// `candidates`, so the stub only carries the field the provider reads; the double
28+
// cast is the least-friction way to satisfy the class type in mocks.
29+
const stubGenerateContentResponse = (text: string) => ({ text }) as unknown as GenerateContentResponse
30+
2331
describe("GeminiHandler", () => {
2432
let handler: GeminiHandler
2533

@@ -342,6 +350,71 @@ describe("GeminiHandler", () => {
342350
const result = await handler.completePrompt("Test prompt")
343351
expect(result).toBe("")
344352
})
353+
354+
it("should pass abort signal through to client via config.abortSignal", async () => {
355+
const controller = new AbortController()
356+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
357+
stubGenerateContentResponse("response"),
358+
)
359+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
360+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith({
361+
model: GEMINI_MODEL_NAME,
362+
contents: [{ role: "user", parts: [{ text: "test prompt" }] }],
363+
config: {
364+
abortSignal: controller.signal,
365+
httpOptions: undefined,
366+
temperature: 1,
367+
},
368+
})
369+
})
370+
371+
it("should work without options (backward compatible)", async () => {
372+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
373+
stubGenerateContentResponse("response"),
374+
)
375+
const result = await handler.completePrompt("test prompt")
376+
expect(result).toBe("response")
377+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith({
378+
model: GEMINI_MODEL_NAME,
379+
contents: [{ role: "user", parts: [{ text: "test prompt" }] }],
380+
config: {
381+
httpOptions: undefined,
382+
temperature: 1,
383+
},
384+
})
385+
})
386+
387+
it("should pass timeoutMs through to client via httpOptions with abortSignal on config", async () => {
388+
const controller = new AbortController()
389+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
390+
stubGenerateContentResponse("response"),
391+
)
392+
await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 10000 })
393+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith({
394+
model: GEMINI_MODEL_NAME,
395+
contents: [{ role: "user", parts: [{ text: "test prompt" }] }],
396+
config: {
397+
abortSignal: controller.signal,
398+
httpOptions: { timeout: 10000 },
399+
temperature: 1,
400+
},
401+
})
402+
})
403+
404+
it("should pass only timeoutMs when no signal is provided", async () => {
405+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
406+
stubGenerateContentResponse("response"),
407+
)
408+
await handler.completePrompt("test prompt", { timeoutMs: 5000 })
409+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith({
410+
model: GEMINI_MODEL_NAME,
411+
contents: [{ role: "user", parts: [{ text: "test prompt" }] }],
412+
config: {
413+
httpOptions: { timeout: 5000 },
414+
temperature: 1,
415+
},
416+
})
417+
})
345418
})
346419

347420
describe("getModel", () => {
@@ -475,6 +548,142 @@ describe("GeminiHandler", () => {
475548
})
476549
})
477550

551+
describe("completePrompt request options", () => {
552+
it("should pass timeout and baseUrl through httpOptions", async () => {
553+
const handlerWithBaseUrl = new GeminiHandler({
554+
apiKey: "test-key",
555+
apiModelId: GEMINI_MODEL_NAME,
556+
geminiApiKey: "test-key",
557+
googleGeminiBaseUrl: "https://gemini.example.test",
558+
})
559+
handlerWithBaseUrl["client"] = handler["client"]
560+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
561+
stubGenerateContentResponse("Response"),
562+
)
563+
564+
const result = await handlerWithBaseUrl.completePrompt("Test prompt", { timeoutMs: 1234 })
565+
566+
expect(result).toBe("Response")
567+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith(
568+
expect.objectContaining({
569+
config: expect.objectContaining({
570+
httpOptions: {
571+
timeout: 1234,
572+
baseUrl: "https://gemini.example.test",
573+
},
574+
}),
575+
}),
576+
)
577+
})
578+
579+
it("should pass abortSignal on config instead of httpOptions", async () => {
580+
const controller = new AbortController()
581+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
582+
stubGenerateContentResponse("Response"),
583+
)
584+
585+
await handler.completePrompt("Test prompt", { abortSignal: controller.signal })
586+
587+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith(
588+
expect.objectContaining({
589+
config: expect.objectContaining({
590+
abortSignal: controller.signal,
591+
httpOptions: undefined,
592+
}),
593+
}),
594+
)
595+
})
596+
597+
it("should omit httpOptions when timeoutMs and baseUrl are not provided", async () => {
598+
vi.mocked(handler["client"].models.generateContent).mockResolvedValue(
599+
stubGenerateContentResponse("Response"),
600+
)
601+
602+
await handler.completePrompt("Test prompt")
603+
604+
expect(handler["client"].models.generateContent).toHaveBeenCalledWith(
605+
expect.objectContaining({
606+
config: expect.objectContaining({
607+
httpOptions: undefined,
608+
}),
609+
}),
610+
)
611+
})
612+
})
613+
614+
describe("createMessage abort signal (bridging)", () => {
615+
const messages: Anthropic.Messages.MessageParam[] = [
616+
{
617+
role: "user",
618+
content: "Hello",
619+
},
620+
]
621+
622+
it("should reject immediately with AbortError when the external signal is pre-aborted", async () => {
623+
const controller = new AbortController()
624+
controller.abort()
625+
626+
const stream = handler.createMessage(
627+
"You are a helpful assistant",
628+
messages,
629+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
630+
)
631+
632+
const error = await collectStream(stream).catch((e: unknown) => e)
633+
expect(error).toBeInstanceOf(Error)
634+
expect((error as Error).name).toBe("AbortError")
635+
expect(handler["client"].models.generateContentStream).not.toHaveBeenCalled()
636+
})
637+
638+
it("should abort the in-flight request when the external signal is triggered", async () => {
639+
const controller = new AbortController()
640+
let capturedSignal: AbortSignal | undefined
641+
const stub = vi.fn().mockImplementation(async (params: { config?: { abortSignal?: AbortSignal } }) => {
642+
capturedSignal = params.config?.abortSignal
643+
return (async function* () {
644+
yield { text: "partial" }
645+
if (capturedSignal?.aborted) {
646+
throw new DOMException("aborted", "AbortError")
647+
}
648+
await new Promise((_resolve, reject) => {
649+
capturedSignal?.addEventListener(
650+
"abort",
651+
() => reject(new DOMException("aborted", "AbortError")),
652+
{ once: true },
653+
)
654+
})
655+
})()
656+
})
657+
handler["client"].models.generateContentStream = stub
658+
659+
const stream = handler.createMessage(
660+
"You are a helpful assistant",
661+
messages,
662+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
663+
)
664+
665+
const collector = collectStream(stream).catch((e: unknown) => e)
666+
await new Promise((resolve) => setTimeout(resolve, 10))
667+
controller.abort()
668+
669+
const error = await collector
670+
expect(error).toBeInstanceOf(Error)
671+
expect((error as Error).name).toBe("AbortError")
672+
expect(capturedSignal).toBeDefined()
673+
expect(capturedSignal?.aborted).toBe(true)
674+
})
675+
676+
it("should not set config.abortSignal when no external signal is provided", async () => {
677+
const stub = vi.fn().mockReturnValue((async function* () {})())
678+
handler["client"].models.generateContentStream = stub
679+
680+
await collectStream(handler.createMessage("You are a helpful assistant", messages))
681+
682+
const config = stub.mock.calls[0][0].config
683+
expect(config.abortSignal).toBeUndefined()
684+
})
685+
})
686+
478687
describe("error telemetry", () => {
479688
const mockMessages: Anthropic.Messages.MessageParam[] = [
480689
{

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ApiHandlerOptions } from "../../../shared/api"
66
import { litellmDefaultModelId, litellmDefaultModelInfo } from "@roo-code/types"
77
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
88
import { clearAllMocks } from "../../../test-utils/reset"
9+
import { makeCreateMessageMetadata } from "../../../test-utils/api"
910

1011
// Mock vscode first to avoid import errors
1112
vi.mock("vscode", () => ({
@@ -1235,4 +1236,107 @@ describe("LiteLLMHandler", () => {
12351236
expect(requestHeaders).not.toHaveProperty("X-Zoo-Session-ID")
12361237
})
12371238
})
1239+
1240+
describe("completePrompt", () => {
1241+
it("should pass abort signal through to client", async () => {
1242+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
1243+
const controller = new AbortController()
1244+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
1245+
expect(mockCreate).toHaveBeenCalledWith(
1246+
expect.objectContaining({ model: expect.any(String) }),
1247+
expect.objectContaining({ signal: controller.signal }),
1248+
)
1249+
})
1250+
1251+
it("should pass timeout through to client", async () => {
1252+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
1253+
await handler.completePrompt("test prompt", { timeoutMs: 5000 })
1254+
expect(mockCreate).toHaveBeenCalledWith(
1255+
expect.objectContaining({ model: expect.any(String) }),
1256+
expect.objectContaining({ timeout: 5000 }),
1257+
)
1258+
})
1259+
1260+
it("should merge signal and timeoutMs together", async () => {
1261+
const controller = new AbortController()
1262+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
1263+
await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 10000 })
1264+
expect(mockCreate).toHaveBeenCalledWith(
1265+
expect.objectContaining({ model: expect.any(String) }),
1266+
expect.objectContaining({ signal: controller.signal, timeout: 10000 }),
1267+
)
1268+
})
1269+
1270+
it("should work without options (backward compatible)", async () => {
1271+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })
1272+
const result = await handler.completePrompt("test prompt")
1273+
expect(result).toBe("response")
1274+
})
1275+
})
1276+
1277+
describe("createMessage abort signal (bridging)", () => {
1278+
const messages: Anthropic.Messages.MessageParam[] = [
1279+
{
1280+
role: "user",
1281+
content: "Hello",
1282+
},
1283+
]
1284+
1285+
it("should reject immediately with AbortError when the external signal is pre-aborted", async () => {
1286+
const controller = new AbortController()
1287+
controller.abort()
1288+
1289+
const stream = handler.createMessage(
1290+
"system",
1291+
messages,
1292+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
1293+
)
1294+
1295+
const error = await collectStream(stream).catch((e: unknown) => e)
1296+
expect(error).toBeInstanceOf(Error)
1297+
expect((error as Error).name).toBe("AbortError")
1298+
expect(mockCreate).not.toHaveBeenCalled()
1299+
})
1300+
1301+
it("should abort the in-flight stream when the external signal is triggered", async () => {
1302+
const controller = new AbortController()
1303+
let capturedSignal: AbortSignal | undefined
1304+
// The stream is built inside the mock implementation so that capturedSignal
1305+
// is already set before the abort-aware chunk is created.
1306+
mockCreate.mockImplementationOnce((_body: unknown, options?: { signal?: AbortSignal }) => {
1307+
capturedSignal = options?.signal
1308+
const mockStream = asyncStreamFrom([
1309+
{
1310+
choices: [{ delta: { content: "partial" } }],
1311+
usage: undefined,
1312+
},
1313+
new Promise<never>((_resolve, reject) => {
1314+
const onAbort = () => reject(new DOMException("aborted", "AbortError"))
1315+
if (capturedSignal?.aborted) {
1316+
onAbort()
1317+
return
1318+
}
1319+
capturedSignal?.addEventListener("abort", onAbort, { once: true })
1320+
}),
1321+
])
1322+
return { withResponse: vi.fn().mockResolvedValue({ data: mockStream }) }
1323+
})
1324+
1325+
const stream = handler.createMessage(
1326+
"system",
1327+
messages,
1328+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
1329+
)
1330+
1331+
const collector = collectStream(stream).catch((e: unknown) => e)
1332+
await new Promise((resolve) => setTimeout(resolve, 10))
1333+
controller.abort()
1334+
1335+
const error = await collector
1336+
expect(error).toBeInstanceOf(Error)
1337+
expect((error as Error).name).toBe("AbortError")
1338+
expect(capturedSignal).toBeDefined()
1339+
expect(capturedSignal?.aborted).toBe(true)
1340+
})
1341+
})
12381342
})

0 commit comments

Comments
 (0)