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

foreign-client-tool.tsx

60 lines1.8 KB

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 threadId: 'foreign-client-tool',
27 connection: fetchServerSentEvents('/api/foreign-client-tool'),
28 tools: [ping],
29 })
30​
31 const assistantText = messages
32 .filter((message) => message.role === 'assistant')
33 .flatMap((message) =>
34 message.parts.flatMap((part) =>
35 part.type === 'text' ? [part.content] : [],
36 ),
37 )
38 .join('')
39​
40 const pingOutputs = messages.flatMap((message) =>
41 message.parts.flatMap((part) =>
42 part.type === 'tool-call' && part.name === 'ping' && part.output
43 ? [JSON.stringify(part.output)]
44 : [],
45 ),
46 )
47​
48 useEffect(() => {
49 void sendMessage('ping me')
50 // Fire the single run once on mount; the harness route ignores the content.
51 // eslint-disable-next-line react-hooks/exhaustive-deps
52 }, [])
53​
54 return (
55 <div data-testid="foreign-client-tool-page">
56 <div data-testid="ping-outputs">{pingOutputs.join(',')}</div>
57 <div data-testid="assistant-text">{assistantText}</div>
58 </div>
59 )