|
| 1 | +// End-to-end regression test for opencode#24432. |
| 2 | +// |
| 3 | +// Routes through the actual ai-gateway-provider + @ai-sdk/openai-compatible |
| 4 | +// chain that provider.ts:811 builds at runtime, with only the network boundary |
| 5 | +// stubbed. Asserts that `reasoning_effort` (and other provider options the |
| 6 | +// transform emits) actually land in the body Cloudflare AI Gateway forwards |
| 7 | +// upstream, which is the only place the bug was observable. |
| 8 | + |
| 9 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 10 | +import type { JSONValue } from "ai" |
| 11 | +import { generateText } from "ai" |
| 12 | +import { createAiGateway } from "ai-gateway-provider" |
| 13 | +import { createUnified } from "ai-gateway-provider/providers/unified" |
| 14 | +import { ProviderTransform } from "@/provider/transform" |
| 15 | +import type * as Provider from "@/provider/provider" |
| 16 | +import { ModelID, ProviderID } from "@/provider/schema" |
| 17 | + |
| 18 | +type Captured = { url: string; outerBody: unknown } |
| 19 | +type ProviderOptions = Record<string, Record<string, JSONValue>> |
| 20 | + |
| 21 | +const realFetch = globalThis.fetch |
| 22 | +let captured: Captured | null = null |
| 23 | + |
| 24 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 25 | + return typeof value === "object" && value !== null && !Array.isArray(value) |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + captured = null |
| 30 | + const handle = async ( |
| 31 | + input: Parameters<typeof fetch>[0], |
| 32 | + init?: Parameters<typeof fetch>[1], |
| 33 | + ): Promise<Response> => { |
| 34 | + const url = |
| 35 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url |
| 36 | + if (url.startsWith("https://gateway.ai.cloudflare.com/")) { |
| 37 | + const bodyText = typeof init?.body === "string" ? init.body : "" |
| 38 | + captured = { url, outerBody: bodyText ? JSON.parse(bodyText) : null } |
| 39 | + return new Response( |
| 40 | + JSON.stringify({ |
| 41 | + id: "chatcmpl-test", |
| 42 | + object: "chat.completion", |
| 43 | + created: 0, |
| 44 | + model: "openai/gpt-5.4", |
| 45 | + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], |
| 46 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, |
| 47 | + }), |
| 48 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 49 | + ) |
| 50 | + } |
| 51 | + return realFetch(input, init) |
| 52 | + } |
| 53 | + // `typeof fetch` includes Bun's `preconnect` method; preserve it from realFetch. |
| 54 | + const stubFetch: typeof fetch = Object.assign(handle, { preconnect: realFetch.preconnect.bind(realFetch) }) |
| 55 | + globalThis.fetch = stubFetch |
| 56 | +}) |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + globalThis.fetch = realFetch |
| 60 | +}) |
| 61 | + |
| 62 | +const cfModel = (apiId: string, releaseDate = "2026-03-05"): Provider.Model => ({ |
| 63 | + id: ModelID.make(`cloudflare-ai-gateway/${apiId}`), |
| 64 | + providerID: ProviderID.make("cloudflare-ai-gateway"), |
| 65 | + name: apiId, |
| 66 | + api: { id: apiId, url: "https://gateway.ai.cloudflare.com/v1/compat", npm: "ai-gateway-provider" }, |
| 67 | + capabilities: { |
| 68 | + reasoning: true, |
| 69 | + temperature: false, |
| 70 | + attachment: true, |
| 71 | + toolcall: true, |
| 72 | + input: { text: true, audio: false, image: true, video: false, pdf: true }, |
| 73 | + output: { text: true, audio: false, image: false, video: false, pdf: false }, |
| 74 | + interleaved: false, |
| 75 | + }, |
| 76 | + cost: { input: 1, output: 1, cache: { read: 0, write: 0 } }, |
| 77 | + limit: { context: 1_000_000, output: 128_000 }, |
| 78 | + status: "active", |
| 79 | + options: {}, |
| 80 | + headers: {}, |
| 81 | + release_date: releaseDate, |
| 82 | +}) |
| 83 | + |
| 84 | +// ai-gateway-provider sends an array of step descriptors; each entry's `query` |
| 85 | +// is the body forwarded to the upstream provider. |
| 86 | +function extractUpstreamQuery(body: unknown): Record<string, unknown> | undefined { |
| 87 | + if (!Array.isArray(body) || body.length === 0) return undefined |
| 88 | + const first = body[0] |
| 89 | + if (!isRecord(first)) return undefined |
| 90 | + const query = first.query |
| 91 | + return isRecord(query) ? query : undefined |
| 92 | +} |
| 93 | + |
| 94 | +async function callThroughGateway(apiId: string, providerOptions: ProviderOptions) { |
| 95 | + const aigateway = createAiGateway({ accountId: "test", gateway: "test", apiKey: "test" }) |
| 96 | + const unified = createUnified() |
| 97 | + await generateText({ model: aigateway(unified(apiId)), prompt: "hi", providerOptions }) |
| 98 | + return extractUpstreamQuery(captured?.outerBody) |
| 99 | +} |
| 100 | + |
| 101 | +describe("cf-ai-gateway end-to-end (regression: #24432)", () => { |
| 102 | + test("ProviderTransform.providerOptions output puts reasoning_effort on the wire", async () => { |
| 103 | + // The full chain the runtime exercises: |
| 104 | + // transform.providerOptions() -> openaiCompatible key |
| 105 | + // -> @ai-sdk/openai-compatible reads it as compatibleOptions |
| 106 | + // -> emits body.reasoning_effort |
| 107 | + // -> ai-gateway-provider wraps the body and forwards to gateway.ai.cloudflare.com |
| 108 | + const opts = ProviderTransform.providerOptions(cfModel("openai/gpt-5.4"), { reasoningEffort: "xhigh" }) |
| 109 | + expect(opts).toEqual({ openaiCompatible: { reasoningEffort: "xhigh" } }) |
| 110 | + |
| 111 | + const upstream = await callThroughGateway("openai/gpt-5.4", opts) |
| 112 | + expect(upstream?.reasoning_effort).toBe("xhigh") |
| 113 | + }) |
| 114 | + |
| 115 | + test("variants() output for openai/gpt-5.4 lands xhigh on the wire", async () => { |
| 116 | + // The other half of the bug: workflow `variant: xhigh` flows through variants() |
| 117 | + // and must reach the wire. variants() returns the providerOptions payload |
| 118 | + // unwrapped; providerOptions() wraps it under the SDK key. |
| 119 | + const variants = ProviderTransform.variants(cfModel("openai/gpt-5.4")) |
| 120 | + expect(variants.xhigh).toEqual({ reasoningEffort: "xhigh" }) |
| 121 | + |
| 122 | + const opts = ProviderTransform.providerOptions(cfModel("openai/gpt-5.4"), variants.xhigh) |
| 123 | + const upstream = await callThroughGateway("openai/gpt-5.4", opts) |
| 124 | + expect(upstream?.reasoning_effort).toBe("xhigh") |
| 125 | + }) |
| 126 | + |
| 127 | + test("legacy buggy key 'cloudflare-ai-gateway' does NOT reach the wire (proves the bug)", async () => { |
| 128 | + // Sanity: confirms the bug class. If a future change accidentally restores |
| 129 | + // providerID-keyed providerOptions, this test fails before users notice. |
| 130 | + const upstream = await callThroughGateway("openai/gpt-5.4", { |
| 131 | + "cloudflare-ai-gateway": { reasoningEffort: "high" }, |
| 132 | + }) |
| 133 | + expect(upstream?.reasoning_effort).toBeUndefined() |
| 134 | + }) |
| 135 | +}) |
0 commit comments