abort.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { complete, stream } from "../src/stream.ts";
  4. import type { Api, Context, Model, StreamOptions } from "../src/types.ts";
  5. type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;
  6. import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.ts";
  7. import { hasBedrockCredentials } from "./bedrock-utils.ts";
  8. import { resolveApiKey } from "./oauth.ts";
  9. // Resolve OAuth tokens at module level (async, runs before tests)
  10. const [openaiCodexToken] = await Promise.all([resolveApiKey("openai-codex")]);
  11. async function testAbortSignal<TApi extends Api>(llm: Model<TApi>, options: StreamOptionsWithExtras = {}) {
  12. const context: Context = {
  13. messages: [
  14. {
  15. role: "user",
  16. content: "What is 15 + 27? Think step by step. Then list 50 first names.",
  17. timestamp: Date.now(),
  18. },
  19. ],
  20. systemPrompt: "You are a helpful assistant.",
  21. };
  22. let abortFired = false;
  23. let text = "";
  24. const controller = new AbortController();
  25. const response = await stream(llm, context, { ...options, signal: controller.signal });
  26. for await (const event of response) {
  27. if (abortFired) return;
  28. if (event.type === "text_delta" || event.type === "thinking_delta") {
  29. text += event.delta;
  30. }
  31. if (text.length >= 50) {
  32. controller.abort();
  33. abortFired = true;
  34. }
  35. }
  36. const msg = await response.result();
  37. // If we get here without throwing, the abort didn't work
  38. expect(msg.stopReason).toBe("aborted");
  39. expect(msg.content.length).toBeGreaterThan(0);
  40. context.messages.push(msg);
  41. context.messages.push({
  42. role: "user",
  43. content: "Please continue, but only generate 5 names.",
  44. timestamp: Date.now(),
  45. });
  46. const followUp = await complete(llm, context, options);
  47. expect(followUp.stopReason).toBe("stop");
  48. expect(followUp.content.length).toBeGreaterThan(0);
  49. }
  50. async function testImmediateAbort<TApi extends Api>(llm: Model<TApi>, options: StreamOptionsWithExtras = {}) {
  51. const controller = new AbortController();
  52. controller.abort();
  53. const context: Context = {
  54. messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
  55. };
  56. const response = await complete(llm, context, { ...options, signal: controller.signal });
  57. expect(response.stopReason).toBe("aborted");
  58. }
  59. async function testAbortThenNewMessage<TApi extends Api>(llm: Model<TApi>, options: StreamOptionsWithExtras = {}) {
  60. // First request: abort immediately before any response content arrives
  61. const controller = new AbortController();
  62. controller.abort();
  63. const context: Context = {
  64. messages: [{ role: "user", content: "Hello, how are you?", timestamp: Date.now() }],
  65. };
  66. const abortedResponse = await complete(llm, context, { ...options, signal: controller.signal });
  67. expect(abortedResponse.stopReason).toBe("aborted");
  68. // The aborted message has empty content since we aborted before anything arrived
  69. expect(abortedResponse.content.length).toBe(0);
  70. // Add the aborted assistant message to context (this is what happens in the real coding agent)
  71. context.messages.push(abortedResponse);
  72. // Second request: send a new message - this should work even with the aborted message in context
  73. context.messages.push({
  74. role: "user",
  75. content: "What is 2 + 2?",
  76. timestamp: Date.now(),
  77. });
  78. const followUp = await complete(llm, context, options);
  79. expect(followUp.stopReason).toBe("stop");
  80. expect(followUp.content.length).toBeGreaterThan(0);
  81. }
  82. describe("AI Providers Abort Tests", () => {
  83. describe.skipIf(!process.env.GEMINI_API_KEY)("Google Provider Abort", () => {
  84. const llm = getModel("google", "gemini-2.5-flash");
  85. it("should abort mid-stream", { retry: 3 }, async () => {
  86. await testAbortSignal(llm, { thinking: { enabled: true } });
  87. });
  88. it("should handle immediate abort", { retry: 3 }, async () => {
  89. await testImmediateAbort(llm, { thinking: { enabled: true } });
  90. });
  91. });
  92. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider Abort", () => {
  93. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  94. void _compat;
  95. const llm: Model<"openai-completions"> = {
  96. ...baseModel,
  97. api: "openai-completions",
  98. };
  99. it("should abort mid-stream", { retry: 3 }, async () => {
  100. await testAbortSignal(llm);
  101. });
  102. it("should handle immediate abort", { retry: 3 }, async () => {
  103. await testImmediateAbort(llm);
  104. });
  105. });
  106. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider Abort", () => {
  107. const llm = getModel("openai", "gpt-5-mini");
  108. it("should abort mid-stream", { retry: 3 }, async () => {
  109. await testAbortSignal(llm);
  110. });
  111. it("should handle immediate abort", { retry: 3 }, async () => {
  112. await testImmediateAbort(llm);
  113. });
  114. });
  115. describe.skipIf(!hasAzureOpenAICredentials())("Azure OpenAI Responses Provider Abort", () => {
  116. const llm = getModel("azure-openai-responses", "gpt-4o-mini");
  117. const azureDeploymentName = resolveAzureDeploymentName(llm.id);
  118. const azureOptions = azureDeploymentName ? { azureDeploymentName } : {};
  119. it("should abort mid-stream", { retry: 3 }, async () => {
  120. await testAbortSignal(llm, azureOptions);
  121. });
  122. it("should handle immediate abort", { retry: 3 }, async () => {
  123. await testImmediateAbort(llm, azureOptions);
  124. });
  125. });
  126. describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider Abort", () => {
  127. const llm = getModel("anthropic", "claude-opus-4-1-20250805");
  128. it("should abort mid-stream", { retry: 3 }, async () => {
  129. await testAbortSignal(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 });
  130. });
  131. it("should handle immediate abort", { retry: 3 }, async () => {
  132. await testImmediateAbort(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 });
  133. });
  134. });
  135. describe.skipIf(!process.env.MISTRAL_API_KEY)("Mistral Provider Abort", () => {
  136. const llm = getModel("mistral", "devstral-medium-latest");
  137. it("should abort mid-stream", { retry: 3 }, async () => {
  138. await testAbortSignal(llm);
  139. });
  140. it("should handle immediate abort", { retry: 3 }, async () => {
  141. await testImmediateAbort(llm);
  142. });
  143. });
  144. describe.skipIf(!process.env.TOGETHER_API_KEY)("Together AI Provider Abort", () => {
  145. const llm = getModel("together", "moonshotai/Kimi-K2.6");
  146. it("should abort mid-stream", { retry: 3 }, async () => {
  147. await testAbortSignal(llm, { reasoningEffort: "high" });
  148. });
  149. it("should handle immediate abort", { retry: 3 }, async () => {
  150. await testImmediateAbort(llm, { reasoningEffort: "high" });
  151. });
  152. });
  153. describe.skipIf(!process.env.MINIMAX_API_KEY)("MiniMax Provider Abort", () => {
  154. const llm = getModel("minimax", "MiniMax-M2.7");
  155. it("should abort mid-stream", { retry: 3 }, async () => {
  156. await testAbortSignal(llm);
  157. });
  158. it("should handle immediate abort", { retry: 3 }, async () => {
  159. await testImmediateAbort(llm);
  160. });
  161. });
  162. describe.skipIf(!process.env.XIAOMI_API_KEY)("Xiaomi MiMo (API billing) Provider Abort", () => {
  163. const llm = getModel("xiaomi", "mimo-v2.5-pro");
  164. it("should abort mid-stream", { retry: 3 }, async () => {
  165. await testAbortSignal(llm);
  166. });
  167. it("should handle immediate abort", { retry: 3 }, async () => {
  168. await testImmediateAbort(llm);
  169. });
  170. });
  171. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY)("Xiaomi MiMo Token Plan (CN) Provider Abort", () => {
  172. const llm = getModel("xiaomi-token-plan-cn", "mimo-v2.5-pro");
  173. it("should abort mid-stream", { retry: 3 }, async () => {
  174. await testAbortSignal(llm);
  175. });
  176. it("should handle immediate abort", { retry: 3 }, async () => {
  177. await testImmediateAbort(llm);
  178. });
  179. });
  180. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY)("Xiaomi MiMo Token Plan (AMS) Provider Abort", () => {
  181. const llm = getModel("xiaomi-token-plan-ams", "mimo-v2.5-pro");
  182. it("should abort mid-stream", { retry: 3 }, async () => {
  183. await testAbortSignal(llm);
  184. });
  185. it("should handle immediate abort", { retry: 3 }, async () => {
  186. await testImmediateAbort(llm);
  187. });
  188. });
  189. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY)("Xiaomi MiMo Token Plan (SGP) Provider Abort", () => {
  190. const llm = getModel("xiaomi-token-plan-sgp", "mimo-v2.5-pro");
  191. it("should abort mid-stream", { retry: 3 }, async () => {
  192. await testAbortSignal(llm);
  193. });
  194. it("should handle immediate abort", { retry: 3 }, async () => {
  195. await testImmediateAbort(llm);
  196. });
  197. });
  198. describe.skipIf(!process.env.KIMI_API_KEY)("Kimi For Coding Provider Abort", () => {
  199. const llm = getModel("kimi-coding", "kimi-k2-thinking");
  200. it("should abort mid-stream", { retry: 3 }, async () => {
  201. await testAbortSignal(llm);
  202. });
  203. it("should handle immediate abort", { retry: 3 }, async () => {
  204. await testImmediateAbort(llm);
  205. });
  206. });
  207. describe.skipIf(!process.env.AI_GATEWAY_API_KEY)("Vercel AI Gateway Provider Abort", () => {
  208. const llm = getModel("vercel-ai-gateway", "google/gemini-2.5-flash");
  209. it("should abort mid-stream", { retry: 3 }, async () => {
  210. await testAbortSignal(llm);
  211. });
  212. it("should handle immediate abort", { retry: 3 }, async () => {
  213. await testImmediateAbort(llm);
  214. });
  215. });
  216. describe("OpenAI Codex Provider Abort", () => {
  217. it.skipIf(!openaiCodexToken)("should abort mid-stream", { retry: 3 }, async () => {
  218. const llm = getModel("openai-codex", "gpt-5.5");
  219. await testAbortSignal(llm, { apiKey: openaiCodexToken });
  220. });
  221. it.skipIf(!openaiCodexToken)("should handle immediate abort", { retry: 3 }, async () => {
  222. const llm = getModel("openai-codex", "gpt-5.5");
  223. await testImmediateAbort(llm, { apiKey: openaiCodexToken });
  224. });
  225. });
  226. describe.skipIf(!hasBedrockCredentials())("Amazon Bedrock Provider Abort", () => {
  227. const llm = getModel("amazon-bedrock", "global.anthropic.claude-sonnet-4-5-20250929-v1:0");
  228. it("should abort mid-stream", { retry: 3 }, async () => {
  229. await testAbortSignal(llm, { reasoning: "medium" });
  230. });
  231. it("should handle immediate abort", { retry: 3 }, async () => {
  232. await testImmediateAbort(llm);
  233. });
  234. it("should handle abort then new message", { retry: 3 }, async () => {
  235. await testAbortThenNewMessage(llm);
  236. });
  237. });
  238. });