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

subagent-brief.tsx

118 lines3.6 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { useState } from 'react'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4import { researcher } from '../lib/researcher-agent'
5import type { UIMessage } from '@tanstack/ai-react'
6​
7export const Route = createFileRoute('/subagent-brief')({
8 component: SubagentBriefPage,
9})
10​
11function childText(messages: ReadonlyArray<UIMessage>) {
12 return messages
13 .flatMap((message) =>
14 message.parts.flatMap((part) =>
15 part.type === 'text' ? [part.content] : [],
16 ),
17 )
18 .join('')
20​
21function SubagentBriefPage() {
22 const { messages, sendMessage, isLoading } = useChat({
23 threadId: 'subagent-brief',
24 connection: fetchServerSentEvents('/api/subagent-brief'),
25 // Types `part.subagent.input` from the researcher's inputSchema.
26 subagents: [researcher],
27 })
28 const [input, setInput] = useState(
29 'We picked three sea animals earlier: squid, octopus, and cuttlefish. Find how many hearts each one has.',
30 )
31​
32 return (
33 <div style={page}>
34 <h1>Subagent brief</h1>
35 <p style={{ color: '#555' }}>
36 The parent model writes a short task for the <code>researcher</code>{' '}
37 subagent. The researcher reads only that task.
38 </p>
39​
40 <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
41 {messages.map((message) => (
42 <div key={message.id} data-testid={`message-${message.role}`}>
43 <div style={roleLabel}>{message.role}</div>
44 {message.parts.map((part, index) => {
45 if (part.type === 'text' && part.content) {
46 return (
47 <p key={index} data-testid="text" style={{ margin: 0 }}>
48 {part.content}
49 </p>
50 )
51 }
52 if (part.type !== 'subagent') return null
53 return (
54 <section key={index} data-testid="card" style={card}>
55 <div>
56 <strong>{part.subagent.name}</strong>{' '}
57 <span data-testid="card-status">
58 {part.subagent.status}
59 </span>
60 </div>
61 <div data-testid="brief" style={{ color: '#444' }}>
62 Brief: {part.subagent.input?.task ?? '(none)'}
63 </div>
64 <p data-testid="card-text" style={{ margin: 0 }}>
65 {childText(part.subagent.messages)}
66 </p>
67 </section>
68 )
69 })}
70 </div>
71 ))}
72 </div>
73​
74 <form
75 onSubmit={(event) => {
76 event.preventDefault()
77 const text = input.trim()
78 if (!text || isLoading) return
79 setInput('')
80 void sendMessage(text)
81 }}
82 style={{ marginTop: 16, display: 'flex', gap: 8 }}
83 >
84 <input
85 data-testid="input"
86 value={input}
87 onChange={(event) => setInput(event.target.value)}
88 style={{ flex: 1, padding: 8 }}
89 />
90 <button data-testid="send" type="submit" disabled={isLoading}>
91 {isLoading ? 'Running...' : 'Send'}
92 </button>
93 </form>
94 </div>
95 )
97​
98const page: React.CSSProperties = {
99 maxWidth: 720,
100 margin: '0 auto',
101 padding: 24,
102 fontFamily: 'system-ui, sans-serif',
104​
105const card: React.CSSProperties = {
106 border: '1px solid #ddd',
107 borderRadius: 8,
108 padding: 12,
109 display: 'flex',
110 flexDirection: 'column',
111 gap: 6,
113​
114const roleLabel: React.CSSProperties = {
115 fontSize: 11,
116 textTransform: 'uppercase',
117 color: '#999',