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

text-first-tool.tsx

41 lines1.3 KB

1import { useEffect } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4​
5/**
6 * Harness page for issue #1247 in the order where the message's
7 * `TEXT_MESSAGE_START` precedes the tool call. `/api/text-first-tool-wire`
8 * streams that shape once; this page renders the resulting assistant text so
9 * the spec can assert the first delta was not dropped.
10 */
11function TextFirstToolPage() {
12 const { messages, sendMessage } = useChat({
13 threadId: 'text-first-tool-1',
14 connection: fetchServerSentEvents('/api/text-first-tool-wire'),
15 })
16​
17 const assistantText = messages
18 .filter((message) => message.role === 'assistant')
19 .flatMap((message) =>
20 message.parts.flatMap((part) =>
21 part.type === 'text' ? [part.content] : [],
22 ),
23 )
24 .join('')
25​
26 useEffect(() => {
27 void sendMessage('go')
28 // Fire the single run once on mount; the harness route ignores the content.
29 // eslint-disable-next-line react-hooks/exhaustive-deps
30 }, [])
31​
32 return (
33 <div data-testid="text-first-tool-page">
34 <div data-testid="assistant-text">{assistantText}</div>
35 </div>
36 )
38​
39export const Route = createFileRoute('/text-first-tool')({
40 component: TextFirstToolPage,
41})