|
| 1 | +'use client' |
| 2 | +import { useState, useRef, useEffect } from 'react' |
| 3 | +import { api } from '@/lib/api' |
| 4 | +import { Send, Bot, User, Loader2 } from 'lucide-react' |
| 5 | +import { fmtTime } from '@/lib/utils' |
| 6 | + |
| 7 | +interface Message { |
| 8 | + role: 'user' | 'assistant' |
| 9 | + content: string |
| 10 | + time: string |
| 11 | + context?: number |
| 12 | +} |
| 13 | + |
| 14 | +const SUGGESTIONS = [ |
| 15 | + 'How many incidents occurred in the last 24 hours?', |
| 16 | + 'List all critical incidents today', |
| 17 | + 'Which camera has the most incidents?', |
| 18 | + 'Summarize traffic accidents from this week', |
| 19 | +] |
| 20 | + |
| 21 | +export default function AgentPage() { |
| 22 | + const [messages, setMessages] = useState<Message[]>([]) |
| 23 | + const [input, setInput] = useState('') |
| 24 | + const [loading, setLoading] = useState(false) |
| 25 | + const bottomRef = useRef<HTMLDivElement>(null) |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + bottomRef.current?.scrollIntoView({ behavior: 'smooth' }) |
| 29 | + }, [messages]) |
| 30 | + |
| 31 | + async function send(question: string) { |
| 32 | + if (!question.trim() || loading) return |
| 33 | + const userMsg: Message = { role: 'user', content: question, time: new Date().toISOString() } |
| 34 | + setMessages((prev) => [...prev, userMsg]) |
| 35 | + setInput('') |
| 36 | + setLoading(true) |
| 37 | + |
| 38 | + try { |
| 39 | + const res = await api.agent.query(question) |
| 40 | + setMessages((prev) => [ |
| 41 | + ...prev, |
| 42 | + { |
| 43 | + role: 'assistant', |
| 44 | + content: res.answer, |
| 45 | + time: new Date().toISOString(), |
| 46 | + context: res.incidents_in_context, |
| 47 | + }, |
| 48 | + ]) |
| 49 | + } catch (e) { |
| 50 | + setMessages((prev) => [ |
| 51 | + ...prev, |
| 52 | + { role: 'assistant', content: 'Failed to get a response. Is the backend running?', time: new Date().toISOString() }, |
| 53 | + ]) |
| 54 | + } finally { |
| 55 | + setLoading(false) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className="flex flex-col h-full max-w-3xl mx-auto"> |
| 61 | + <h1 className="text-xl font-semibold text-white mb-4">AI Agent</h1> |
| 62 | + |
| 63 | + {/* Chat window */} |
| 64 | + <div className="flex-1 bg-gray-900 rounded-xl border border-gray-800 flex flex-col overflow-hidden"> |
| 65 | + <div className="flex-1 overflow-y-auto p-4 space-y-4 scrollbar-thin"> |
| 66 | + {messages.length === 0 && ( |
| 67 | + <div className="text-center pt-8"> |
| 68 | + <Bot size={40} className="mx-auto text-brand-500 mb-3" /> |
| 69 | + <p className="text-gray-400 text-sm mb-6">Ask anything about incidents, cameras, or traffic patterns.</p> |
| 70 | + <div className="grid grid-cols-2 gap-2"> |
| 71 | + {SUGGESTIONS.map((s) => ( |
| 72 | + <button |
| 73 | + key={s} |
| 74 | + onClick={() => send(s)} |
| 75 | + className="text-left text-xs bg-gray-800 hover:bg-gray-700 text-gray-300 px-3 py-2 rounded-lg transition-colors" |
| 76 | + > |
| 77 | + {s} |
| 78 | + </button> |
| 79 | + ))} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + )} |
| 83 | + |
| 84 | + {messages.map((msg, i) => ( |
| 85 | + <div key={i} className={`flex gap-3 ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}> |
| 86 | + {msg.role === 'assistant' && ( |
| 87 | + <div className="w-7 h-7 rounded-full bg-brand-600 flex items-center justify-center flex-shrink-0 mt-1"> |
| 88 | + <Bot size={14} /> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + <div className={`max-w-lg ${msg.role === 'user' ? 'items-end' : 'items-start'} flex flex-col gap-1`}> |
| 92 | + <div className={`px-4 py-2.5 rounded-2xl text-sm whitespace-pre-wrap ${ |
| 93 | + msg.role === 'user' |
| 94 | + ? 'bg-brand-600 text-white rounded-br-sm' |
| 95 | + : 'bg-gray-800 text-gray-200 rounded-bl-sm' |
| 96 | + }`}> |
| 97 | + {msg.content} |
| 98 | + </div> |
| 99 | + <span className="text-xs text-gray-600"> |
| 100 | + {fmtTime(msg.time)} |
| 101 | + {msg.context != null && ` · ${msg.context} incidents in context`} |
| 102 | + </span> |
| 103 | + </div> |
| 104 | + {msg.role === 'user' && ( |
| 105 | + <div className="w-7 h-7 rounded-full bg-gray-700 flex items-center justify-center flex-shrink-0 mt-1"> |
| 106 | + <User size={14} /> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + |
| 112 | + {loading && ( |
| 113 | + <div className="flex gap-3"> |
| 114 | + <div className="w-7 h-7 rounded-full bg-brand-600 flex items-center justify-center"> |
| 115 | + <Bot size={14} /> |
| 116 | + </div> |
| 117 | + <div className="bg-gray-800 rounded-2xl rounded-bl-sm px-4 py-3 flex items-center gap-2"> |
| 118 | + <Loader2 size={14} className="animate-spin text-brand-400" /> |
| 119 | + <span className="text-gray-400 text-sm">Thinking…</span> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + )} |
| 123 | + <div ref={bottomRef} /> |
| 124 | + </div> |
| 125 | + |
| 126 | + {/* Input bar */} |
| 127 | + <div className="border-t border-gray-800 p-3 flex gap-2"> |
| 128 | + <input |
| 129 | + type="text" |
| 130 | + value={input} |
| 131 | + onChange={(e) => setInput(e.target.value)} |
| 132 | + onKeyDown={(e) => e.key === 'Enter' && !e.shiftKey && send(input)} |
| 133 | + placeholder="Ask about incidents, cameras, or traffic…" |
| 134 | + className="flex-1 bg-gray-800 text-white text-sm rounded-lg px-4 py-2.5 outline-none placeholder-gray-600 focus:ring-1 focus:ring-brand-500" |
| 135 | + /> |
| 136 | + <button |
| 137 | + onClick={() => send(input)} |
| 138 | + disabled={loading || !input.trim()} |
| 139 | + className="bg-brand-600 hover:bg-brand-500 disabled:opacity-50 text-white px-4 py-2.5 rounded-lg transition-colors" |
| 140 | + > |
| 141 | + <Send size={15} /> |
| 142 | + </button> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + ) |
| 147 | +} |
0 commit comments