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

chat-ui.tsx

70 lines1.9 KB

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