Open History to see the last commit
1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3
4const BETA = 'mid-conversation-tool-changes-2026-07-01'
5const PLACEHOLDER = '__tanstack_deferred_placeholder__'
6
7type WireCase = {
8 name: string
9 errors: Array<string>
10 requestCount: number
11 firstTools: Array<string>
12 secondTools: Array<string>
13 additionalTools: Array<string>
14 toolAdditions: Array<string>
15 deferred: Array<string>
16 toolCacheMarks: Array<Array<string>>
17 betas: Array<Array<string>>
18 lastItem: string | null
19}
20
21/**
22 * A middleware adds `get_forecast` after the first tool batch.
23 * `/api/mid-conversation-changes-wire` runs that with the real adapters and a
24 * capturing `fetch`. Returns a lookup of what each case's two requests held.
25 */
26async function casesFrom(request: APIRequestContext) {
27 const response = await request.post('/api/mid-conversation-changes-wire')
28 expect(response.ok()).toBe(true)
29 const result = (await response.json()) as {
30 ok: boolean
31 error?: string
32 cases: Array<WireCase>
33 }
34 if (!result.ok) throw new Error(`Route failed: ${result.error}`)
35 return (name: string) => {
36 const found = result.cases.find((entry) => entry.name === name)
37 if (!found) throw new Error(`No case named ${name}`)
38 return found
39 }
40}
41
42/** The request of today: full tools, no placeholder, no beta, no change. */
43function expectFullTools(plain: WireCase) {
44 expect(plain.errors).toEqual([])
45 expect(plain.requestCount).toBe(2)
46 expect(plain.firstTools).toEqual(['get_weather'])
47 expect(plain.secondTools).toEqual(['get_weather', 'get_forecast'])
48 expect(plain.additionalTools).toEqual([])
49 expect(plain.toolAdditions).toEqual([])
50 expect(plain.deferred).toEqual([])
51 expect(plain.betas.flat()).not.toContain(BETA)
52}
53
54test.describe('mid-conversation changes: a tool added after the first tool batch', () => {
55 test('a GPT model with the channel sends it as additional_tools', async ({
56 request,
57 }) => {
58 const gpt = (await casesFrom(request))('gpt-6-astra')
59 expect(gpt.errors).toEqual([])
60 expect(gpt.requestCount).toBe(2)
61 expect(gpt.firstTools).toEqual(['get_weather'])
62 // The start of the request stays the same.
63 expect(gpt.secondTools).toEqual(['get_weather'])
64 expect(gpt.additionalTools).toEqual(['get_forecast'])
65 // The change comes after the tool result.
66 expect(gpt.lastItem).toBe('additional_tools')
67 })
68
69 test('a Claude model with the channel sends a tool_addition block and the beta', async ({
70 request,
71 }) => {
72 const claude = (await casesFrom(request))('claude-opus-5-5')
73 expect(claude.errors).toEqual([])
74 expect(claude.requestCount).toBe(2)
75 expect(claude.firstTools).toEqual(['get_weather', PLACEHOLDER])
76 expect(claude.secondTools).toEqual([
77 'get_weather',
78 PLACEHOLDER,
79 'get_forecast',
80 ])
81 expect(claude.deferred).toEqual([PLACEHOLDER, 'get_forecast'])
82 // The automatic prompt-cache marker sits on the last start tool, not on
83 // the placeholder or the deferred tool, so the cached start stays put.
84 expect(claude.toolCacheMarks).toEqual([['get_weather'], ['get_weather']])
85 expect(claude.toolAdditions).toEqual(['get_forecast'])
86 expect(claude.lastItem).toBe('system')
87 expect(claude.betas).toHaveLength(2)
88 for (const betas of claude.betas) expect(betas).toContain(BETA)
89 })
90
91 test('models without the channel send the full tools', async ({
92 request,
93 }) => {
94 const caseOf = await casesFrom(request)
95 // Both have `midConversationChannels: true`. The map still decides.
96 expectFullTools(caseOf('gpt-6.1-sol'))
97 expectFullTools(caseOf('claude-sonnet-5-5'))
98 })
99
100 test('a custom fetch with no option sends the full tools (the gateway case)', async ({
101 request,
102 }) => {
103 // claude-opus-5-5 is in the map, but a custom fetch turns the default off.
104 expectFullTools((await casesFrom(request))('claude-opus-5-5 gateway'))
105 })
106})