Skip to content

Commit c87acbe

Browse files
committed
feat(api): abort signal support for openai-compatible provider (completePrompt + createMessage)
- completePrompt merges options.abortSignal and options.timeoutMs via mergeAbortSignalAndTimeout and forwards the merged signal to the AI SDK generateText abortSignal option - createMessage forwards metadata.abortSignal to streamText so in-flight streams are aborted on task cancellation Tests: new openai-compatible.spec.ts covering completePrompt signal/timeout passthrough, timeoutMs <= 0 disabled, pre-aborted AbortError, error propagation, and createMessage abortSignal bridging (pass-through, absent metadata, pre-aborted, mid-request abort)
1 parent 8108249 commit c87acbe

2 files changed

Lines changed: 244 additions & 2 deletions

File tree

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
// npx vitest run api/providers/__tests__/openai-compatible.spec.ts
2+
3+
import { OpenAICompatibleHandler } from "../openai-compatible"
4+
import { makeApiHandlerOptions } from "../../../test-utils/api"
5+
import { collectStream } from "../../../test-utils/stream"
6+
7+
const mockGenerateText = vitest.fn()
8+
const mockStreamText = vitest.fn()
9+
10+
// The factory must not touch the mock bindings at factory-execution time (vi.mock is
11+
// hoisted above the consts), so forward lazily through wrapper functions.
12+
vitest.mock("ai", () => ({
13+
generateText: (...args: unknown[]) => mockGenerateText(...(args as [])),
14+
streamText: (...args: unknown[]) => mockStreamText(...(args as [])),
15+
}))
16+
17+
// Concrete test implementation of the abstract OpenAI-compatible base class
18+
class TestOpenAICompatibleHandler extends OpenAICompatibleHandler {
19+
constructor(apiKey: string) {
20+
super(makeApiHandlerOptions({ apiModelId: "test-model" }), {
21+
providerName: "TestProvider",
22+
baseURL: "https://test.example.com/v1",
23+
apiKey,
24+
modelId: "test-model",
25+
modelInfo: {
26+
maxTokens: 4096,
27+
contextWindow: 128000,
28+
supportsImages: false,
29+
supportsPromptCache: false,
30+
inputPrice: 0.5,
31+
outputPrice: 1.5,
32+
},
33+
})
34+
}
35+
36+
override getModel() {
37+
return { id: "test-model", info: this.config.modelInfo }
38+
}
39+
}
40+
41+
function makeEmptyStreamResult() {
42+
return {
43+
fullStream: {
44+
[Symbol.asyncIterator]: async function* () {
45+
// Emit no parts
46+
yield* []
47+
},
48+
},
49+
usage: Promise.resolve(undefined),
50+
}
51+
}
52+
53+
describe("OpenAICompatibleHandler", () => {
54+
let handler: TestOpenAICompatibleHandler
55+
56+
beforeEach(() => {
57+
vi.clearAllMocks()
58+
handler = new TestOpenAICompatibleHandler("test-api-key")
59+
})
60+
61+
describe("completePrompt", () => {
62+
it("should return message content from successful response", async () => {
63+
mockGenerateText.mockResolvedValue({ text: "response" })
64+
65+
const result = await handler.completePrompt("test prompt")
66+
67+
expect(result).toBe("response")
68+
expect(mockGenerateText).toHaveBeenCalledTimes(1)
69+
expect(mockGenerateText.mock.calls[0][0].prompt).toBe("test prompt")
70+
})
71+
72+
it("should pass abortSignal through to generateText", async () => {
73+
mockGenerateText.mockResolvedValue({ text: "response" })
74+
75+
const controller = new AbortController()
76+
await handler.completePrompt("test prompt", { abortSignal: controller.signal })
77+
78+
expect(mockGenerateText.mock.calls[0][0].abortSignal).toBe(controller.signal)
79+
})
80+
81+
it("should pass timeoutMs through to generateText as a timeout abort signal", async () => {
82+
mockGenerateText.mockResolvedValue({ text: "response" })
83+
84+
await handler.completePrompt("test prompt", { timeoutMs: 5000 })
85+
86+
const { abortSignal } = mockGenerateText.mock.calls[0][0]
87+
expect(abortSignal).toBeInstanceOf(AbortSignal)
88+
expect(abortSignal.aborted).toBe(false)
89+
})
90+
91+
it("should merge signal and timeout when both are provided", async () => {
92+
mockGenerateText.mockResolvedValue({ text: "response" })
93+
94+
const controller = new AbortController()
95+
await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: 10000 })
96+
97+
const mergedSignal = mockGenerateText.mock.calls[0][0].abortSignal as AbortSignal
98+
expect(mergedSignal).toBeInstanceOf(AbortSignal)
99+
100+
// Aborting the external signal must abort the merged signal synchronously
101+
controller.abort()
102+
expect(mergedSignal.aborted).toBe(true)
103+
})
104+
105+
it("should work without options (backward compatible)", async () => {
106+
mockGenerateText.mockResolvedValue({ text: "response" })
107+
108+
const result = await handler.completePrompt("test prompt")
109+
110+
expect(result).toBe("response")
111+
expect(mockGenerateText.mock.calls[0][0].abortSignal).toBeUndefined()
112+
})
113+
114+
it("should treat timeoutMs <= 0 as disabled", async () => {
115+
mockGenerateText.mockResolvedValue({ text: "response" })
116+
117+
await handler.completePrompt("test prompt", { timeoutMs: 0 })
118+
expect(mockGenerateText.mock.calls[0][0].abortSignal).toBeUndefined()
119+
120+
await handler.completePrompt("test prompt", { timeoutMs: -1 })
121+
expect(mockGenerateText.mock.calls[1][0].abortSignal).toBeUndefined()
122+
})
123+
124+
it("should reject with AbortError when abortSignal is already aborted before request", async () => {
125+
mockGenerateText.mockImplementation((options: { abortSignal?: AbortSignal }) => {
126+
if (options.abortSignal?.aborted) {
127+
const error = new Error("This operation was aborted")
128+
error.name = "AbortError"
129+
return Promise.reject(error)
130+
}
131+
return Promise.resolve({ text: "response" })
132+
})
133+
134+
const controller = new AbortController()
135+
controller.abort()
136+
137+
await expect(
138+
handler.completePrompt("test prompt", { abortSignal: controller.signal }),
139+
).rejects.toMatchObject({
140+
name: "AbortError",
141+
})
142+
})
143+
144+
it("should throw handled error when API call fails", async () => {
145+
mockGenerateText.mockRejectedValue(new Error("Network error"))
146+
147+
await expect(handler.completePrompt("test prompt")).rejects.toThrow("Network error")
148+
})
149+
})
150+
151+
describe("createMessage", () => {
152+
it("should pass the external abortSignal to streamText", async () => {
153+
mockStreamText.mockReturnValue(makeEmptyStreamResult())
154+
155+
const controller = new AbortController()
156+
const stream = handler.createMessage("You are helpful.", [], {
157+
taskId: "test",
158+
abortSignal: controller.signal,
159+
})
160+
await collectStream(stream)
161+
162+
expect(mockStreamText).toHaveBeenCalledTimes(1)
163+
expect(mockStreamText.mock.calls[0][0].abortSignal).toBe(controller.signal)
164+
})
165+
166+
it("should not set an abortSignal when metadata has none", async () => {
167+
mockStreamText.mockReturnValue(makeEmptyStreamResult())
168+
169+
const stream = handler.createMessage("You are helpful.", [], { taskId: "test" })
170+
await collectStream(stream)
171+
172+
expect(mockStreamText.mock.calls[0][0].abortSignal).toBeUndefined()
173+
})
174+
175+
it("should reject with AbortError when the external abortSignal is pre-aborted", async () => {
176+
mockStreamText.mockImplementation((options: { abortSignal?: AbortSignal }) => ({
177+
fullStream: {
178+
[Symbol.asyncIterator]: async function* () {
179+
if (options.abortSignal?.aborted) {
180+
const error = new Error("This operation was aborted")
181+
error.name = "AbortError"
182+
throw error
183+
}
184+
yield* []
185+
},
186+
},
187+
usage: Promise.resolve(undefined),
188+
}))
189+
190+
const controller = new AbortController()
191+
controller.abort()
192+
193+
const stream = handler.createMessage("You are helpful.", [], {
194+
taskId: "test",
195+
abortSignal: controller.signal,
196+
})
197+
await expect(collectStream(stream)).rejects.toMatchObject({ name: "AbortError" })
198+
})
199+
200+
it("should abort the stream when the external abortSignal is aborted mid-request", async () => {
201+
mockStreamText.mockImplementation((options: { abortSignal?: AbortSignal }) => ({
202+
fullStream: {
203+
[Symbol.asyncIterator]: async function* () {
204+
// Emulate a slow model response that ends when the request is aborted
205+
await new Promise<void>((resolve) => {
206+
options.abortSignal?.addEventListener("abort", () => resolve(), { once: true })
207+
})
208+
yield* []
209+
const error = new Error("This operation was aborted")
210+
error.name = "AbortError"
211+
throw error
212+
},
213+
},
214+
usage: Promise.resolve(undefined),
215+
}))
216+
217+
const controller = new AbortController()
218+
const stream = handler.createMessage("You are helpful.", [], {
219+
taskId: "test",
220+
abortSignal: controller.signal,
221+
})
222+
const collected = collectStream(stream)
223+
setTimeout(() => controller.abort(), 10)
224+
225+
await expect(collected).rejects.toMatchObject({ name: "AbortError" })
226+
})
227+
})
228+
})

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
1818
import { DEFAULT_HEADERS } from "./constants"
1919
import { BaseProvider } from "./base-provider"
2020
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
21+
import { mergeAbortSignalAndTimeout } from "./utils/abort-signal"
2122

2223
/**
2324
* Configuration options for creating an OpenAI-compatible provider.
@@ -176,6 +177,12 @@ export abstract class OpenAICompatibleHandler extends BaseProvider implements Si
176177
toolChoice: this.mapToolChoice(metadata?.tool_choice),
177178
}
178179

180+
// Forward the external abort signal (e.g. task cancellation) so the in-flight
181+
// stream is aborted when the request is cancelled
182+
if (metadata?.abortSignal) {
183+
requestOptions.abortSignal = metadata.abortSignal
184+
}
185+
179186
// Use streamText for streaming responses
180187
const result = streamText(requestOptions)
181188

@@ -200,12 +207,19 @@ export abstract class OpenAICompatibleHandler extends BaseProvider implements Si
200207
async completePrompt(prompt: string, options?: CompletePromptOptions): Promise<string> {
201208
const languageModel = this.getLanguageModel()
202209

203-
const { text } = await generateText({
210+
const generateOptions: Parameters<typeof generateText>[0] & { abortSignal?: AbortSignal } = {
204211
model: languageModel,
205212
prompt,
206213
maxOutputTokens: this.getMaxOutputTokens(),
207214
temperature: this.config.temperature ?? 0,
208-
})
215+
}
216+
217+
const mergedAbortSignal = mergeAbortSignalAndTimeout(options?.abortSignal, options?.timeoutMs)
218+
if (mergedAbortSignal) {
219+
generateOptions.abortSignal = mergedAbortSignal
220+
}
221+
222+
const { text } = await generateText(generateOptions)
209223

210224
return text
211225
}

0 commit comments

Comments
 (0)