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

chat-ui.tsx

86 lines2.4 KB

1import { fetchServerSentEvents } from '@tanstack/ai-react'
2import {
3 createChatHook,
4 TextPart,
5 ThinkingPart,
6 type LayoutProps,
7} from '@tanstack/ai-react/ui'
8import { ChatInput } from '@/components/chat-input'
9import { OpenRouterKeyForm } from '@/components/open-router-key-form'
10import { Researcher, Seo, Writer } from '@/components/subagent-card'
11import { blogAgents } from '@/lib/blog-agents'
12import { byok } from '@/lib/byok'
13import { THREAD_ID } from '@/lib/thread'
14​
15export const chatOptions = {
16 connection: fetchServerSentEvents('/api/chat'),
17 byok,
18 threadId: THREAD_ID,
19 persistence: true,
20 subagents: blogAgents,
22​
23export type BlogChatOptions = typeof chatOptions
24​
25export const { useAppChat, useChatContext } = createChatHook({
26 options: chatOptions,
27 components: {
28 layout: function Layout({
29 Messages,
30 Input,
31 }: LayoutProps<typeof chatOptions>) {
32 return (
33 <div className="flex h-screen flex-col bg-gray-900">
34 <div className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
35 <h1 className="mb-2 text-sm font-semibold text-white">
36 Saved blog desk
37 </h1>
38 <p className="mb-2 text-xs text-gray-400">
39 Refresh keeps this chat. A refresh during a run continues the
40 stream.
41 </p>
42 <OpenRouterKeyForm />
43 </div>
44 <div className="flex-1 overflow-y-auto px-4 py-4">
45 <Messages />
46 </div>
47 <Input />
48 </div>
49 )
50 },
51 message: function Message({ message, Parts }) {
52 return (
53 <article
54 data-role={message.role}
55 className={`mb-2 rounded-lg p-4 text-gray-100 ${
56 message.role === 'assistant'
57 ? 'bg-linear-to-r from-orange-500/5 to-red-600/5'
58 : 'bg-transparent'
59 }`}
60 >
61 <Parts />
62 </article>
63 )
64 },
65 input: ChatInput,
66 },
67 partsComponents: {
68 text: ({ part }) => (
69 <TextPart className="chat-markdown" content={part.content} />
70 ),
71 thinking: ({ part }) => (
72 <ThinkingPart
73 content={part.content}
74 className="mb-2 rounded border border-gray-700 bg-gray-800/60 p-2 text-xs text-gray-400"
75 />
76 ),
77 fallback: ({ part }) => (
78 <p className="text-xs text-gray-400">{part.type}</p>
79 ),
80 },
81 subagentsComponents: {
82 researcher: Researcher,
83 writer: Writer,
84 seo: Seo,
85 },
86})