Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

canonical-tool-input.spec.ts

56 lines2.0 KB

1import { expect, test } from '../fixtures'
2import type { Page } from '@playwright/test'
3import { getMessages, runTest, selectScenario } from './helpers'
4​
5/**
6 * Regression test for PR #1481.
7 *
8 * The `canonical-tool-input` scenario streams the wire arguments
9 * `{"component":"database","region":null}` and then sends
10 * `TOOL_CALL_END.input` without `region`. Before the fix, the client's
11 * tool-call part kept the streamed string in `arguments`, and the next user
12 * turn sent that string back to the model in its history.
13 */
14const CANONICAL_ARGUMENTS = '{"component":"database"}'
15​
16async function getHistoryTexts(page: Page): Promise<Array<string>> {
17 const messages = await getMessages(page)
18 return messages
19 .flatMap((msg) => msg.parts || [])
20 .filter((part) => part.type === 'text')
21 .map((part) => part.content as string)
22 .filter((content) => content.startsWith('History arguments:'))
24​
25async function waitForHistoryTexts(page: Page, count: number): Promise<void> {
26 await expect
27 .poll(async () => (await getHistoryTexts(page)).length, { timeout: 15000 })
28 .toBe(count)
30​
31test.describe('TOOL_CALL_END input after streamed args (tools-test page)', () => {
32 test('tool-call part and next request history use the END input', async ({
33 page,
34 testId,
35 aimockPort,
36 }) => {
37 await selectScenario(page, 'canonical-tool-input', testId, aimockPort)
38 await runTest(page)
39 await waitForHistoryTexts(page, 1)
40​
41 const messages = await getMessages(page)
42 const toolCall = messages
43 .flatMap((msg) => msg.parts || [])
44 .find((part) => part.type === 'tool-call')
45 expect(toolCall?.name).toBe('check_status')
46 expect(toolCall?.arguments).toBe(CANONICAL_ARGUMENTS)
47 expect(toolCall?.input).toEqual({ component: 'database' })
48​
49 // The second turn's history is built from the client's tool-call part.
50 await runTest(page)
51 await waitForHistoryTexts(page, 2)
52​
53 const historyTexts = await getHistoryTexts(page)
54 expect(historyTexts[1]).toBe(`History arguments: ${CANONICAL_ARGUMENTS}`)
55 })
56})