tahamajs/IE / Projects /front /src /components /rag /RagAssistantPanel.tsx
tahamajs's picture
download
raw
9.24 kB
// src/components/rag/RagAssistantPanel.tsx
import { useState, useRef, useEffect } from "react";
import { useRagSystem } from "@/hooks/useRagSystem";
import { RagCitationBlock } from "./RagCitationBlock";
import { RagSourceInspector } from "./RagSourceInspector";
import { RagModelSettings } from "./RagModelSettings";
import {
Send, Sparkles, Loader2, Brain, FileSearch, Trash2,
Settings, Database, ChevronDown, BookOpen, Paperclip,
} from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import { toast } from "react-toastify";
export function RagAssistantPanel() {
const {
conversation,
isQuerying,
lastSources,
panelOpen,
setPanelOpen,
settings,
setSettings,
queryRag,
clearConversation,
documents,
} = useRagSystem();
const [input, setInput] = useState("");
const [showSources, setShowSources] = useState(false);
const [showSettings, setShowSettings] = useState(false);
const scrollRef = useRef<HTMLDivElement>(null);
// Auto‑scroll to bottom when conversation updates
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollTo({
top: scrollRef.current.scrollHeight,
behavior: "smooth",
});
}
}, [conversation]);
const handleSend = async () => {
if (!input.trim() || isQuerying) return;
const q = input.trim();
setInput("");
try {
await queryRag(q);
} catch (err) {
// Error is already shown in the hook via toast
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
return (
<div className="flex flex-col h-full bg-white/30 dark:bg-slate-950/20 backdrop-blur-md rounded-b-2xl border border-white/5 overflow-hidden shadow-xl select-text font-sans">
{/* Header */}
<div className="px-4 py-3 border-b border-white/10 dark:border-white/5 flex items-center justify-between bg-white/30 dark:bg-slate-900/30 backdrop-blur-lg select-none">
<div className="flex items-center gap-2">
<Brain className="w-4.5 h-4.5 text-teal-500" />
<span className="font-extrabold text-sm text-slate-900 dark:text-white">Research Assistant</span>
<span className="text-[10px] bg-teal-500/10 text-teal-650 dark:text-teal-400 px-2 py-0.5 rounded-lg border border-teal-500/10 font-bold">
{documents.length} docs indexed
</span>
</div>
<div className="flex items-center gap-1">
<button
onClick={() => setShowSources(!showSources)}
className={`p-2 rounded-xl transition-all duration-200 active:scale-95 ${
showSources ? "bg-teal-500/10 text-teal-600 dark:text-teal-400" : "text-slate-400 hover:text-slate-700 dark:hover:text-white"
}`}
title="View last sources"
>
<FileSearch className="w-4 h-4" />
</button>
<button
onClick={() => setShowSettings(!showSettings)}
className={`p-2 rounded-xl transition-all duration-200 active:scale-95 ${
showSettings ? "bg-teal-500/10 text-teal-600 dark:text-teal-400" : "text-slate-400 hover:text-slate-700 dark:hover:text-white"
}`}
title="Settings"
>
<Settings className="w-4 h-4" />
</button>
<button
onClick={() => {
clearConversation();
toast.warn("Cleared assistant conversation logs.");
}}
className="p-2 text-slate-400 hover:text-rose-500 rounded-xl transition-all active:scale-95"
title="Clear conversation"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
</div>
{/* Settings panel (collapsible) */}
<AnimatePresence>
{showSettings && (
<motion.div
initial={{ height: 0 }}
animate={{ height: "auto" }}
exit={{ height: 0 }}
className="overflow-hidden border-b border-white/10 dark:border-white/5 bg-slate-50/20 dark:bg-slate-950/20"
>
<RagModelSettings settings={settings} onChange={setSettings} />
</motion.div>
)}
</AnimatePresence>
{/* Conversation area */}
<div ref={scrollRef} className="flex-1 overflow-y-auto p-4 space-y-4 custom-scrollbar">
{conversation.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full text-slate-400 py-12 px-6 text-center select-none">
<div className="w-16 h-16 rounded-2xl bg-teal-500/10 flex items-center justify-center mb-4">
<Brain className="w-8 h-8 text-teal-500" />
</div>
<h4 className="font-extrabold text-slate-700 dark:text-slate-350 text-sm mb-2">
Ask your research library
</h4>
<p className="text-xs text-slate-500 max-w-xs leading-relaxed font-medium">
I search across all your indexed PDFs, papers, and notes to answer with cited sources.
</p>
<div className="mt-5 flex flex-wrap gap-2 justify-center max-w-lg">
{[
"Summarize my papers on machine learning",
"What methods were used in the latest study?",
"Compare findings across my documents",
].map((s) => (
<button
key={s}
onClick={() => setInput(s)}
className="text-xs px-3.5 py-2 bg-white/40 dark:bg-slate-900/40 border border-white/10 dark:border-white/5 hover:bg-teal-500/10 hover:text-teal-650 dark:hover:text-teal-400 hover:border-teal-500/20 rounded-xl transition-all duration-200 font-bold text-slate-655 dark:text-slate-300"
>
{s}
</button>
))}
</div>
</div>
) : (
conversation.map((turn, i) => (
<div
key={i}
className={`flex ${turn.role === "user" ? "justify-end" : "justify-start"}`}
>
<div
className={`max-w-[85%] rounded-2xl px-4.5 py-3 ${
turn.role === "user"
? "bg-teal-600 text-white rounded-br-sm shadow-md shadow-teal-600/10 text-xs font-semibold"
: "bg-white/45 dark:bg-slate-900/45 border border-white/10 dark:border-white/5 text-slate-800 dark:text-slate-200 rounded-bl-sm shadow-sm text-xs font-medium"
}`}
>
<p className="whitespace-pre-wrap leading-relaxed">{turn.text}</p>
{turn.citations && turn.citations.length > 0 && (
<div className="mt-3.5 space-y-2 border-t border-white/10 dark:border-slate-800 pt-2.5">
<p className="text-[9px] text-slate-400 font-extrabold uppercase tracking-widest">Sources cited</p>
{turn.citations.map((cite, ci) => (
<RagCitationBlock key={ci} citation={cite} />
))}
</div>
)}
</div>
</div>
))
)}
{isQuerying && (
<div className="flex justify-start">
<div className="bg-white/40 dark:bg-slate-900/45 border border-white/10 dark:border-white/5 rounded-2xl rounded-bl-sm px-4.5 py-3 flex items-center gap-2.5 shadow-sm text-xs">
<Loader2 className="w-4 h-4 animate-spin text-teal-500" />
<span className="text-slate-500 dark:text-slate-450 font-bold">Searching your research index...</span>
</div>
</div>
)}
</div>
{/* Source inspector overlay */}
{showSources && lastSources.length > 0 && (
<RagSourceInspector sources={lastSources} onClose={() => setShowSources(false)} />
)}
{/* Input area */}
<div className="p-3.5 border-t border-white/10 dark:border-white/5 bg-slate-50/5 dark:bg-slate-900/10">
<div className="flex items-end gap-2.5">
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Ask about your research library..."
rows={1}
className="flex-1 resize-none px-4 py-3 bg-slate-100/50 dark:bg-slate-900/50 border border-white/10 dark:border-white/5 rounded-xl focus:outline-none focus:ring-1 focus:ring-teal-500/50 text-slate-805 dark:text-slate-205 text-xs font-semibold placeholder-slate-500 custom-scrollbar"
style={{ minHeight: "42px", maxHeight: "120px" }}
/>
<button
onClick={handleSend}
disabled={!input.trim() || isQuerying}
className="p-3 bg-teal-605 hover:bg-teal-700 text-white rounded-xl disabled:opacity-50 transition-all duration-200 active:scale-95 shrink-0 shadow-md shadow-teal-600/10"
>
{isQuerying ? <Loader2 className="w-4.5 h-4.5 animate-spin" /> : <Send className="w-4.5 h-4.5" />}
</button>
</div>
</div>
</div>
);
}
export default RagAssistantPanel;

Xet Storage Details

Size:
9.24 kB
·
Xet hash:
02489edbf7f0e7fb6f3477c5153e1102821da75c6abf1d9e5e7e66a69b9c059b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.