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

tool-first-text.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: a tool call whose `parentMessageId` precedes
7 * that message's `TEXT_MESSAGE_START`. `/api/tool-first-text-wire` streams
8 * that shape once; this page renders the resulting assistant text so the
9 * spec can assert the first delta was not dropped.
10 */
11function ToolFirstTextPage() {
12 const { messages, sendMessage } = useChat({
13 threadId: 'tool-first-text-1',
14 connection: fetchServerSentEvents('/api/tool-first-text-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="tool-first-text-page">
34 <div data-testid="assistant-text">{assistantText}</div>
35 </div>
36 )
38​
39export const Route = createFileRoute('/tool-first-text')({
40 component: ToolFirstTextPage,
41})