|
| 1 | +// Tests for OpenAICompatibleHandler's abortSignal passing to streamText() |
| 2 | +// Verifies that when createMessage() is called with metadata containing an abortSignal, |
| 3 | +// the signal is passed through to AI SDK's streamText() so HTTP requests can be aborted. |
| 4 | + |
| 5 | +const { mockStreamText } = vi.hoisted(() => ({ |
| 6 | + mockStreamText: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock("ai", async (importOriginal) => { |
| 10 | + const actual = await importOriginal<typeof import("ai")>() |
| 11 | + return { |
| 12 | + ...actual, |
| 13 | + streamText: mockStreamText, |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +vi.mock("@ai-sdk/openai-compatible", () => ({ |
| 18 | + createOpenAICompatible: vi.fn(() => { |
| 19 | + return vi.fn(() => ({ |
| 20 | + modelId: "test-model", |
| 21 | + provider: "test-provider", |
| 22 | + })) |
| 23 | + }), |
| 24 | +})) |
| 25 | + |
| 26 | +import type { Anthropic } from "@anthropic-ai/sdk" |
| 27 | + |
| 28 | +import { OpenAICompatibleHandler, OpenAICompatibleConfig } from "../openai-compatible" |
| 29 | +import type { ApiHandlerOptions } from "../../../shared/api" |
| 30 | +import type { ModelInfo } from "@roo-code/types" |
| 31 | + |
| 32 | +// Concrete test subclass of the abstract OpenAICompatibleHandler |
| 33 | +class TestOpenAiCompatibleHandler extends OpenAICompatibleHandler { |
| 34 | + constructor(options: ApiHandlerOptions, config: OpenAICompatibleConfig) { |
| 35 | + super(options, config) |
| 36 | + } |
| 37 | + |
| 38 | + override getModel(): { id: string; info: ModelInfo } { |
| 39 | + return { id: this.config.modelId, info: this.config.modelInfo } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +describe("OpenAICompatibleHandler abort signal", () => { |
| 44 | + let handler: TestOpenAiCompatibleHandler |
| 45 | + const mockOptions: ApiHandlerOptions = {} |
| 46 | + const config: OpenAICompatibleConfig = { |
| 47 | + providerName: "test-provider", |
| 48 | + baseURL: "https://api.test.com/v1", |
| 49 | + apiKey: "test-key", |
| 50 | + modelId: "test-model", |
| 51 | + modelInfo: { maxTokens: 8192, contextWindow: 128000, supportsImages: false, supportsPromptCache: true }, |
| 52 | + } |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks() |
| 56 | + handler = new TestOpenAiCompatibleHandler(mockOptions, config) |
| 57 | + }) |
| 58 | + |
| 59 | + describe("createMessage abortSignal passing", () => { |
| 60 | + const systemPrompt = "You are a helpful assistant." |
| 61 | + const messages: Anthropic.Messages.MessageParam[] = [ |
| 62 | + { role: "user", content: [{ type: "text" as const, text: "Hello!" }] }, |
| 63 | + ] |
| 64 | + |
| 65 | + it("should pass abortSignal to streamText when provided in metadata", async () => { |
| 66 | + const controller = new AbortController() |
| 67 | + const mockAbortSignal = controller.signal |
| 68 | + |
| 69 | + async function* mockFullStream() { |
| 70 | + yield { type: "text-delta", text: "Test response" } |
| 71 | + } |
| 72 | + |
| 73 | + function createMockStream(yieldValue: any) { |
| 74 | + return { |
| 75 | + fullStream: (async function* () { |
| 76 | + yield yieldValue |
| 77 | + })(), |
| 78 | + usage: Promise.resolve({ inputTokens: 10, outputTokens: 5 }), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const mockUsage = Promise.resolve({ inputTokens: 10, outputTokens: 5 }) |
| 83 | + |
| 84 | + mockStreamText.mockReturnValue({ |
| 85 | + fullStream: mockFullStream(), |
| 86 | + usage: mockUsage, |
| 87 | + }) |
| 88 | + |
| 89 | + await handler |
| 90 | + .createMessage(systemPrompt, messages, { |
| 91 | + taskId: "test-task", |
| 92 | + abortSignal: mockAbortSignal, |
| 93 | + }) |
| 94 | + .next() |
| 95 | + |
| 96 | + expect(mockStreamText).toHaveBeenCalledWith( |
| 97 | + expect.objectContaining({ |
| 98 | + signal: mockAbortSignal, |
| 99 | + }), |
| 100 | + ) |
| 101 | + }) |
| 102 | + |
| 103 | + it("should pass undefined signal to streamText when abortSignal is not provided", async () => { |
| 104 | + async function* mockFullStream() { |
| 105 | + yield { type: "text-delta", text: "Test response" } |
| 106 | + } |
| 107 | + |
| 108 | + function createMockStream(yieldValue: any) { |
| 109 | + return { |
| 110 | + fullStream: (async function* () { |
| 111 | + yield yieldValue |
| 112 | + })(), |
| 113 | + usage: Promise.resolve({ inputTokens: 10, outputTokens: 5 }), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const mockUsage = Promise.resolve({ inputTokens: 10, outputTokens: 5 }) |
| 118 | + |
| 119 | + mockStreamText.mockReturnValue({ |
| 120 | + fullStream: mockFullStream(), |
| 121 | + usage: mockUsage, |
| 122 | + }) |
| 123 | + |
| 124 | + await handler |
| 125 | + .createMessage(systemPrompt, messages, { |
| 126 | + taskId: "test-task", |
| 127 | + }) |
| 128 | + .next() |
| 129 | + |
| 130 | + expect(mockStreamText).toHaveBeenCalledWith( |
| 131 | + expect.objectContaining({ |
| 132 | + signal: undefined, |
| 133 | + }), |
| 134 | + ) |
| 135 | + }) |
| 136 | + |
| 137 | + it("should pass signal to streamText when metadata is undefined", async () => { |
| 138 | + async function* mockFullStream() { |
| 139 | + yield { type: "text-delta", text: "Test response" } |
| 140 | + } |
| 141 | + |
| 142 | + function createMockStream(yieldValue: any) { |
| 143 | + return { |
| 144 | + fullStream: (async function* () { |
| 145 | + yield yieldValue |
| 146 | + })(), |
| 147 | + usage: Promise.resolve({ inputTokens: 10, outputTokens: 5 }), |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + const mockUsage = Promise.resolve({ inputTokens: 10, outputTokens: 5 }) |
| 152 | + |
| 153 | + mockStreamText.mockReturnValue({ |
| 154 | + fullStream: mockFullStream(), |
| 155 | + usage: mockUsage, |
| 156 | + }) |
| 157 | + |
| 158 | + await handler.createMessage(systemPrompt, messages).next() |
| 159 | + |
| 160 | + expect(mockStreamText).toHaveBeenCalledWith( |
| 161 | + expect.objectContaining({ |
| 162 | + signal: undefined, |
| 163 | + }), |
| 164 | + ) |
| 165 | + }) |
| 166 | + |
| 167 | + it("should pass the correct signal when it fires during streaming", async () => { |
| 168 | + const controller = new AbortController() |
| 169 | + const mockAbortSignal = controller.signal |
| 170 | + |
| 171 | + let capturedOptions: any = null |
| 172 | + |
| 173 | + mockStreamText.mockImplementation((options) => { |
| 174 | + capturedOptions = options |
| 175 | + return { |
| 176 | + fullStream: (async function* () { |
| 177 | + yield { type: "text-delta", text: "Partial" } |
| 178 | + })(), |
| 179 | + usage: Promise.resolve({ inputTokens: 5, outputTokens: 3 }), |
| 180 | + } |
| 181 | + }) |
| 182 | + |
| 183 | + const stream = handler.createMessage(systemPrompt, messages, { |
| 184 | + taskId: "test-task", |
| 185 | + abortSignal: mockAbortSignal, |
| 186 | + }) |
| 187 | + |
| 188 | + // Verify the signal was captured before aborting |
| 189 | + expect(capturedOptions).toBeDefined() |
| 190 | + expect(capturedOptions.signal).toBe(mockAbortSignal) |
| 191 | + |
| 192 | + // Now abort - this should cause streamText to receive an aborted signal |
| 193 | + controller.abort() |
| 194 | + expect(controller.signal.aborted).toBe(true) |
| 195 | + }) |
| 196 | + |
| 197 | + it("should pass all other request options alongside the signal", async () => { |
| 198 | + const controller = new AbortController() |
| 199 | + |
| 200 | + let capturedOptions: any = null |
| 201 | + |
| 202 | + mockStreamText.mockImplementation((options) => { |
| 203 | + capturedOptions = options |
| 204 | + return { |
| 205 | + fullStream: (async function* () { |
| 206 | + yield { type: "text-delta", text: "Test" } |
| 207 | + })(), |
| 208 | + usage: Promise.resolve({ inputTokens: 10, outputTokens: 5 }), |
| 209 | + } |
| 210 | + }) |
| 211 | + |
| 212 | + await handler |
| 213 | + .createMessage(systemPrompt, messages, { |
| 214 | + taskId: "test-task", |
| 215 | + abortSignal: controller.signal, |
| 216 | + }) |
| 217 | + .next() |
| 218 | + |
| 219 | + expect(capturedOptions).toHaveProperty("model") |
| 220 | + expect(capturedOptions).toHaveProperty("system", systemPrompt) |
| 221 | + expect(capturedOptions).toHaveProperty("messages") |
| 222 | + expect(capturedOptions).toHaveProperty("signal", controller.signal) |
| 223 | + }) |
| 224 | + }) |
| 225 | +}) |
0 commit comments