Open History to see the last commit
1import { useEffect } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4import { toolDefinition } from '@tanstack/ai'
5import { z } from 'zod'
6
7/**
8 * Client half of the issue #1464 harness. `/api/foreign-client-tool` is an
9 * AG-UI producer that is not TanStack AI. It calls the frontend tool `ping`
10 * and ends the run with a success outcome. The client must run `ping` and
11 * start the next run with its result.
12 */
13
14const ping = toolDefinition({
15 name: 'ping',
16 description: 'Reply with pong',
17 inputSchema: z.object({}),
18}).client(() => ({ reply: 'pong' }))
19
20export const Route = createFileRoute('/foreign-client-tool')({
21 component: ForeignClientToolPage,
22})
23
24function ForeignClientToolPage() {
25 const { messages, sendMessage } = useChat({
26 connection: fetchServerSentEvents('/api/foreign-client-tool'),
27 tools: [ping],
28 })
29
30 const assistantText = messages
31 .filter((message) => message.role === 'assistant')
32 .flatMap((message) =>
33 message.parts.flatMap((part) =>
34 part.type === 'text' ? [part.content] : [],
35 ),
36 )
37 .join('')
38
39 const pingOutputs = messages.flatMap((message) =>
40 message.parts.flatMap((part) =>
41 part.type === 'tool-call' && part.name === 'ping' && part.output
42 ? [JSON.stringify(part.output)]
43 : [],
44 ),
45 )
46
47 useEffect(() => {
48 void sendMessage('ping me')
49 // Fire the single run once on mount; the harness route ignores the content.
50 // eslint-disable-next-line react-hooks/exhaustive-deps
51 }, [])
52
53 return (
54 <div data-testid="foreign-client-tool-page">
55 <div data-testid="ping-outputs">{pingOutputs.join(',')}</div>
56 <div data-testid="assistant-text">{assistantText}</div>
57 </div>
58 )
59}