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

chat-ui.tsx

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