Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * Inside a Worker, `cloudflareBindingFetch` sends the requests of the real
5 * Anthropic and OpenAI adapters through `env.AI.run` to the AI Gateway
6 * `anthropic/...` and `openai/...` models. `/api/cloudflare-binding-wire` uses a
7 * fake `env.AI` and returns what each `run` call got.
8 */
9test.describe('cloudflare -- gateway models through the Workers AI binding', () => {
10 test('Claude and GPT stream through env.AI.run', async ({ request }) => {
11 const response = await request.post('/api/cloudflare-binding-wire')
12 expect(response.ok()).toBe(true)
13 const result: {
14 ok: boolean
15 error?: string
16 claudeText: string
17 gptText: string
18 calls: Array<{
19 model: string
20 modelInBody: boolean
21 betasInBody: boolean
22 systemSent: boolean
23 toolNames: Array<string>
24 returnRawResponse: boolean
25 gateway?: { id: string }
26 extraHeaders?: Record<string, string>
27 }>
28 } = await response.json()
29 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
30
31 expect(result.claudeText).toBe('Hi from Claude.')
32 expect(result.gptText).toBe('Hi from GPT.')
33
34 const [claude, gpt] = result.calls
35 // The gateway id names the vendor. The body keeps the provider's own shape.
36 expect(claude).toMatchObject({
37 model: 'anthropic/claude-opus-5-5',
38 modelInBody: false,
39 betasInBody: false,
40 systemSent: true,
41 toolNames: ['lookup'],
42 returnRawResponse: true,
43 gateway: { id: 'e2e-gateway' },
44 })
45 // The beta goes along as a header. The SDK's auth headers do not.
46 expect(claude?.extraHeaders?.['anthropic-beta']).toContain(
47 'files-api-2025-04-14',
48 )
49 expect(claude?.extraHeaders).not.toHaveProperty('x-api-key')
50 expect(claude?.extraHeaders).not.toHaveProperty('authorization')
51 expect(gpt).toMatchObject({
52 model: 'openai/gpt-6.1-sol',
53 modelInBody: false,
54 systemSent: true,
55 toolNames: ['lookup'],
56 returnRawResponse: true,
57 })
58 })
59})