|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { useState, useRef, useEffect, useMemo } from 'react'; |
| 4 | +import { useChat } from '@ai-sdk/react'; |
| 5 | +import { DefaultChatTransport } from 'ai'; |
| 6 | +import type { UIMessage } from 'ai'; |
| 7 | +import { Bot, X, Send, Trash2, Sparkles } from 'lucide-react'; |
| 8 | +import { Button } from '@/components/ui/button'; |
| 9 | +import { ScrollArea } from '@/components/ui/scroll-area'; |
| 10 | +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; |
| 11 | +import { cn } from '@/lib/utils'; |
| 12 | +import { useAiChatPanel, loadMessages, saveMessages } from '@/hooks/use-ai-chat-panel'; |
| 13 | +import { getApiBaseUrl } from '@/lib/config'; |
| 14 | + |
| 15 | +const PANEL_WIDTH = 380; |
| 16 | +const COLLAPSED_WIDTH = 48; |
| 17 | + |
| 18 | +/** |
| 19 | + * Extract the text content from a UIMessage's parts array. |
| 20 | + */ |
| 21 | +function getMessageText(msg: UIMessage): string { |
| 22 | + return (msg.parts ?? []) |
| 23 | + .filter((p): p is { type: 'text'; text: string } => p.type === 'text') |
| 24 | + .map((p) => p.text) |
| 25 | + .join(''); |
| 26 | +} |
| 27 | + |
| 28 | +export function AiChatPanel() { |
| 29 | + const { isOpen, setOpen, toggle } = useAiChatPanel(); |
| 30 | + const [input, setInput] = useState(''); |
| 31 | + const scrollRef = useRef<HTMLDivElement>(null); |
| 32 | + const inputRef = useRef<HTMLTextAreaElement>(null); |
| 33 | + const baseUrl = getApiBaseUrl(); |
| 34 | + |
| 35 | + const transport = useMemo( |
| 36 | + () => new DefaultChatTransport({ api: `${baseUrl}/api/v1/ai/chat` }), |
| 37 | + [baseUrl], |
| 38 | + ); |
| 39 | + |
| 40 | + const { messages, sendMessage, setMessages, status, error } = useChat({ |
| 41 | + transport, |
| 42 | + messages: loadMessages() as UIMessage[], |
| 43 | + }); |
| 44 | + |
| 45 | + const isStreaming = status === 'streaming' || status === 'submitted'; |
| 46 | + |
| 47 | + // Persist messages to localStorage whenever they change |
| 48 | + useEffect(() => { |
| 49 | + if (messages.length > 0) { |
| 50 | + saveMessages(messages); |
| 51 | + } |
| 52 | + }, [messages]); |
| 53 | + |
| 54 | + // Auto-scroll to bottom on new messages |
| 55 | + useEffect(() => { |
| 56 | + if (scrollRef.current) { |
| 57 | + scrollRef.current.scrollTop = scrollRef.current.scrollHeight; |
| 58 | + } |
| 59 | + }, [messages]); |
| 60 | + |
| 61 | + // Focus input when panel opens |
| 62 | + useEffect(() => { |
| 63 | + if (isOpen && inputRef.current) { |
| 64 | + inputRef.current.focus(); |
| 65 | + } |
| 66 | + }, [isOpen]); |
| 67 | + |
| 68 | + const clearHistory = () => { |
| 69 | + setMessages([]); |
| 70 | + saveMessages([]); |
| 71 | + }; |
| 72 | + |
| 73 | + const handleSend = () => { |
| 74 | + const text = input.trim(); |
| 75 | + if (!text || isStreaming) return; |
| 76 | + setInput(''); |
| 77 | + sendMessage({ text }); |
| 78 | + }; |
| 79 | + |
| 80 | + // Handle Enter to submit, Shift+Enter for newline |
| 81 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 82 | + if (e.key === 'Enter' && !e.shiftKey) { |
| 83 | + e.preventDefault(); |
| 84 | + handleSend(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + // ── Collapsed state: edge button ── |
| 89 | + if (!isOpen) { |
| 90 | + return ( |
| 91 | + <TooltipProvider> |
| 92 | + <Tooltip> |
| 93 | + <TooltipTrigger asChild> |
| 94 | + <button |
| 95 | + onClick={toggle} |
| 96 | + data-testid="ai-chat-toggle" |
| 97 | + className={cn( |
| 98 | + 'fixed right-0 top-1/2 -translate-y-1/2 z-50', |
| 99 | + 'flex items-center justify-center', |
| 100 | + 'h-10 rounded-l-md border border-r-0 border-border', |
| 101 | + 'bg-background text-foreground shadow-md', |
| 102 | + 'hover:bg-accent transition-colors', |
| 103 | + )} |
| 104 | + style={{ width: COLLAPSED_WIDTH }} |
| 105 | + > |
| 106 | + <Sparkles className="h-5 w-5" /> |
| 107 | + </button> |
| 108 | + </TooltipTrigger> |
| 109 | + <TooltipContent side="left"> |
| 110 | + <p>AI Chat <kbd className="ml-1 text-[10px] opacity-60">⌘⇧I</kbd></p> |
| 111 | + </TooltipContent> |
| 112 | + </Tooltip> |
| 113 | + </TooltipProvider> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + // ── Expanded panel ── |
| 118 | + return ( |
| 119 | + <aside |
| 120 | + data-testid="ai-chat-panel" |
| 121 | + className={cn( |
| 122 | + 'fixed right-0 top-0 z-50 h-full', |
| 123 | + 'flex flex-col border-l border-border', |
| 124 | + 'bg-background shadow-xl', |
| 125 | + 'animate-in slide-in-from-right duration-200', |
| 126 | + )} |
| 127 | + style={{ width: PANEL_WIDTH }} |
| 128 | + > |
| 129 | + {/* ── Header ── */} |
| 130 | + <div className="flex h-12 shrink-0 items-center justify-between border-b px-3"> |
| 131 | + <div className="flex items-center gap-2 text-sm font-semibold"> |
| 132 | + <Bot className="h-4 w-4 text-primary" /> |
| 133 | + AI Chat |
| 134 | + </div> |
| 135 | + <div className="flex items-center gap-1"> |
| 136 | + <TooltipProvider> |
| 137 | + <Tooltip> |
| 138 | + <TooltipTrigger asChild> |
| 139 | + <Button variant="ghost" size="icon" className="h-7 w-7" onClick={clearHistory}> |
| 140 | + <Trash2 className="h-3.5 w-3.5" /> |
| 141 | + <span className="sr-only">Clear chat</span> |
| 142 | + </Button> |
| 143 | + </TooltipTrigger> |
| 144 | + <TooltipContent><p>Clear history</p></TooltipContent> |
| 145 | + </Tooltip> |
| 146 | + </TooltipProvider> |
| 147 | + <Button variant="ghost" size="icon" className="h-7 w-7" onClick={() => setOpen(false)}> |
| 148 | + <X className="h-4 w-4" /> |
| 149 | + <span className="sr-only">Close</span> |
| 150 | + </Button> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + |
| 154 | + {/* ── Messages ── */} |
| 155 | + <ScrollArea className="flex-1 overflow-hidden"> |
| 156 | + <div ref={scrollRef} className="flex flex-col gap-3 p-3 overflow-y-auto h-full"> |
| 157 | + {messages.length === 0 && ( |
| 158 | + <div className="flex flex-1 flex-col items-center justify-center gap-2 py-12 text-center text-muted-foreground"> |
| 159 | + <Sparkles className="h-8 w-8 opacity-40" /> |
| 160 | + <p className="text-sm">Ask anything about your project.</p> |
| 161 | + <p className="text-xs opacity-60"> |
| 162 | + <kbd>⌘⇧I</kbd> to toggle this panel |
| 163 | + </p> |
| 164 | + </div> |
| 165 | + )} |
| 166 | + {messages.map((msg) => { |
| 167 | + const text = getMessageText(msg); |
| 168 | + if (!text && msg.role !== 'user') return null; |
| 169 | + return ( |
| 170 | + <div |
| 171 | + key={msg.id} |
| 172 | + className={cn( |
| 173 | + 'flex flex-col gap-1 rounded-lg px-3 py-2 text-sm', |
| 174 | + msg.role === 'user' |
| 175 | + ? 'ml-8 bg-primary text-primary-foreground' |
| 176 | + : 'mr-8 bg-muted text-foreground', |
| 177 | + )} |
| 178 | + > |
| 179 | + <span className="text-[10px] font-medium opacity-60 uppercase"> |
| 180 | + {msg.role === 'user' ? 'You' : 'Assistant'} |
| 181 | + </span> |
| 182 | + <div className="whitespace-pre-wrap break-words">{text}</div> |
| 183 | + </div> |
| 184 | + ); |
| 185 | + })} |
| 186 | + {isStreaming && ( |
| 187 | + <div className="mr-8 flex items-center gap-2 rounded-lg bg-muted px-3 py-2 text-sm text-muted-foreground"> |
| 188 | + <span className="inline-block h-2 w-2 animate-pulse rounded-full bg-primary" /> |
| 189 | + Thinking… |
| 190 | + </div> |
| 191 | + )} |
| 192 | + {error && ( |
| 193 | + <div className="rounded-lg border border-destructive/50 bg-destructive/10 px-3 py-2 text-sm text-destructive"> |
| 194 | + Error: {error.message || 'Something went wrong'} |
| 195 | + </div> |
| 196 | + )} |
| 197 | + </div> |
| 198 | + </ScrollArea> |
| 199 | + |
| 200 | + {/* ── Input ── */} |
| 201 | + <div className="shrink-0 border-t p-3"> |
| 202 | + <div className="flex items-end gap-2"> |
| 203 | + <textarea |
| 204 | + ref={inputRef} |
| 205 | + data-testid="ai-chat-input" |
| 206 | + value={input} |
| 207 | + onChange={(e) => setInput(e.target.value)} |
| 208 | + onKeyDown={handleKeyDown} |
| 209 | + placeholder="Ask AI…" |
| 210 | + rows={1} |
| 211 | + className={cn( |
| 212 | + 'flex-1 resize-none rounded-md border border-input bg-transparent px-3 py-2 text-sm', |
| 213 | + 'placeholder:text-muted-foreground', |
| 214 | + 'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring', |
| 215 | + 'max-h-32 min-h-[36px]', |
| 216 | + )} |
| 217 | + /> |
| 218 | + <Button |
| 219 | + type="button" |
| 220 | + size="icon" |
| 221 | + className="h-9 w-9 shrink-0" |
| 222 | + disabled={!input.trim() || isStreaming} |
| 223 | + onClick={handleSend} |
| 224 | + > |
| 225 | + <Send className="h-4 w-4" /> |
| 226 | + <span className="sr-only">Send</span> |
| 227 | + </Button> |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + </aside> |
| 231 | + ); |
| 232 | +} |
0 commit comments