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

subagents-ui-test.tsx

86 lines2.5 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { fetchServerSentEvents } from '@tanstack/ai-react'
3import { createChatHook } from '@tanstack/ai-react/ui'
4import type { SubagentPartsProps, SubagentProps } from '@tanstack/ai-react/ui'
5import { parseAimockPort } from '@/lib/devtools-test'
6import { SUBAGENT_PROMPTS, lookupFacts } from '@/lib/subagents-test'
7​
8/**
9 * The chat UI kit on the `route` scenario. The root registers `thinking`,
10 * `text`, and a `lookupFacts` tool widget. The researcher card overrides
11 * `thinking` only, so its tool call and text still use the root widgets.
12 */
13const chatOptions = {
14 connection: fetchServerSentEvents('/api/subagents-test'),
15 subagents: [{ name: 'researcher', tools: [lookupFacts] }] as const,
17​
18const researcherParts: SubagentPartsProps<
19 typeof chatOptions,
20 'researcher'
21>['partsComponents'] = {
22 thinking: ({ part }) => <p data-testid="card-thinking">{part.content}</p>,
24​
25function Researcher({
26 Parts,
27}: SubagentProps<typeof chatOptions, 'researcher'>) {
28 return (
29 <section data-testid="card-researcher">
30 <Parts partsComponents={researcherParts} />
31 </section>
32 )
34​
35const { useAppChat } = createChatHook({
36 options: chatOptions,
37 components: {
38 layout: ({ Messages }) => <Messages />,
39 message: ({ Parts }) => (
40 <div>
41 <Parts />
42 </div>
43 ),
44 },
45 partsComponents: {
46 text: ({ part }) => <p data-testid="root-text">{part.content}</p>,
47 thinking: ({ part }) => <p data-testid="root-thinking">{part.content}</p>,
48 fallback: () => null,
49 },
50 toolsComponents: {
51 lookupFacts: ({ part }) => (
52 <p data-testid="root-tool">
53 {part.name}:{part.state}
54 </p>
55 ),
56 },
57 subagentsComponents: { researcher: Researcher },
58})
59​
60function SubagentsUITestPage() {
61 const { testId, aimockPort } = Route.useSearch()
62 const chat = useAppChat({
63 threadId: `subagents-ui-${testId ?? 'manual'}`,
64 forwardedProps: { scenario: 'route', testId, aimockPort },
65 })
66 return (
67 <div data-testid="subagents-ui-page">
68 <button
69 data-testid="run"
70 onClick={() => void chat.sendMessage(SUBAGENT_PROMPTS.route)}
71 >
72 Run
73 </button>
74 <div data-testid="message-count">{chat.messages.length}</div>
75 <chat.AppChat />
76 </div>
77 )
79​
80export const Route = createFileRoute('/subagents-ui-test')({
81 component: SubagentsUITestPage,
82 validateSearch: (search: Record<string, unknown>) => ({
83 testId: typeof search.testId === 'string' ? search.testId : undefined,
84 aimockPort: parseAimockPort(search.aimockPort),
85 }),
86})