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

AIAssistant.tsx

181 lines6.6 KB

1import { useEffect, useRef } from "react";
2import { useStore } from "@tanstack/react-store";
3import { Send, X } from "lucide-react";
4import ReactMarkdown from "react-markdown";
5import rehypeRaw from "rehype-raw";
6import rehypeSanitize from "rehype-sanitize";
7import rehypeHighlight from "rehype-highlight";
8import remarkGfm from "remark-gfm";
9import { useChat } from "@ai-sdk/react";
10​
11import { genAIResponse } from "@/utils/demo.ai";
12import { showAIAssistant } from "@/store/assistant";
13​
14import MotorcycleRecommendation from "./MotorcycleRecommendation";
15​
16import type { UIMessage } from "ai";
17​
18function Messages({ messages }: { messages: Array<UIMessage> }) {
19 const messagesContainerRef = useRef<HTMLDivElement>(null);
20​
21 useEffect(() => {
22 if (messagesContainerRef.current) {
23 messagesContainerRef.current.scrollTop =
24 messagesContainerRef.current.scrollHeight;
25 }
26 }, [messages]);
27​
28 if (!messages.length) {
29 return (
30 <div className="flex-1 flex items-center justify-center text-gray-400 text-sm">
31 Ask me about your next motorcycle.
32 </div>
33 );
34 }
35​
36 return (
37 <div ref={messagesContainerRef} className="flex-1 overflow-y-auto">
38 {messages.map(({ id, role, content, parts }) => (
39 <div
40 key={id}
41 className={`py-3 ${
42 role === "assistant"
43 ? "bg-gradient-to-r from-orange-500/5 to-red-600/5"
44 : "bg-transparent"
45 }`}
46 >
47 {content.length > 0 && (
48 <div className="flex items-start gap-2 px-4">
49 {role === "assistant" ? (
50 <div className="w-6 h-6 rounded-lg bg-gradient-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs font-medium text-white flex-shrink-0">
51 AI
52 </div>
53 ) : (
54 <div className="w-6 h-6 rounded-lg bg-gray-700 flex items-center justify-center text-xs font-medium text-white flex-shrink-0">
55 Y
56 </div>
57 )}
58 <div className="flex-1 min-w-0">
59 <ReactMarkdown
60 className="prose dark:prose-invert max-w-none prose-sm"
61 rehypePlugins={[
62 rehypeRaw,
63 rehypeSanitize,
64 rehypeHighlight,
65 remarkGfm,
66 ]}
67 >
68 {content}
69 </ReactMarkdown>
70 </div>
71 </div>
72 )}
73 {parts
74 .filter((part) => part.type === "tool-invocation")
75 .filter(
76 (part) => part.toolInvocation.toolName === "recommendMotorcycle"
77 )
78 .map((toolCall) => (
79 <div
80 key={toolCall.toolInvocation.toolName}
81 className="max-w-[80%] mx-auto"
82 >
83 <MotorcycleRecommendation
84 id={toolCall.toolInvocation.args.id}
85 />
86 </div>
87 ))}
88 </div>
89 ))}
90 </div>
91 );
93​
94export default function AIAssistant() {
95 const isOpen = useStore(showAIAssistant);
96 const { messages, input, handleInputChange, handleSubmit } = useChat({
97 initialMessages: [],
98 fetch: (_url, options) => {
99 const { messages } = JSON.parse(options!.body! as string);
100 return genAIResponse({
101 data: {
102 messages,
103 },
104 });
105 },
106 onToolCall: (call) => {
107 if (call.toolCall.toolName === "recommendMotorcycle") {
108 return "Handled by the UI";
109 }
110 },
111 });
112​
113 return (
114 <div className="relative z-50">
115 <button
116 onClick={() => showAIAssistant.setState((state) => !state)}
117 className="flex items-center gap-2 px-4 py-2 rounded-lg bg-black/90 text-white hover:bg-black/80 transition-all border border-orange-500/20 shadow-lg shadow-orange-500/10"
118 >
119 <div className="w-5 h-5 rounded-lg bg-gradient-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs font-medium">
120 �
121 </div>
122 Motorcycle Expert
123 </button>
124​
125 {isOpen && (
126 <div className="absolute top-full right-0 mt-2 w-[700px] h-[600px] bg-black/95 rounded-lg shadow-2xl border border-orange-500/20 flex flex-col backdrop-blur-sm">
127 <div className="flex items-center justify-between p-4 border-b border-orange-500/20 bg-gradient-to-r from-orange-500/5 to-red-600/5">
128 <h3 className="font-semibold text-white flex items-center gap-2">
129 <span className="w-6 h-6 rounded-lg bg-gradient-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs">
130 �️
131 </span>
132 Motorcycle Expert
133 </h3>
134 <button
135 onClick={() => showAIAssistant.setState((state) => !state)}
136 className="text-gray-400 hover:text-white transition-colors"
137 >
138 <X className="w-4 h-4" />
139 </button>
140 </div>
141​
142 <Messages messages={messages} />
143​
144 <div className="p-4 border-t border-orange-500/20 bg-gradient-to-r from-orange-500/5 to-red-600/5">
145 <form onSubmit={handleSubmit}>
146 <div className="relative">
147 <textarea
148 value={input}
149 onChange={handleInputChange}
150 placeholder="Ask about our motorcycles..."
151 className="w-full rounded-lg border border-orange-500/20 bg-black/50 pl-3 pr-10 py-3 text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-orange-500/30 focus:border-transparent resize-none overflow-hidden shadow-inner"
152 rows={1}
153 style={{ minHeight: "44px", maxHeight: "120px" }}
154 onInput={(e) => {
155 const target = e.target as HTMLTextAreaElement;
156 target.style.height = "auto";
157 target.style.height =
158 Math.min(target.scrollHeight, 120) + "px";
159 }}
160 onKeyDown={(e) => {
161 if (e.key === "Enter" && !e.shiftKey) {
162 e.preventDefault();
163 handleSubmit(e);
164 }
165 }}
166 />
167 <button
168 type="submit"
169 disabled={!input.trim()}
170 className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-orange-500 hover:text-orange-400 disabled:text-gray-500 transition-colors focus:outline-none"
171 >
172 <Send className="w-4 h-4" />
173 </button>
174 </div>
175 </form>
176 </div>
177 </div>
178 )}
179 </div>
180 );