Spaces:
Running
Running
File size: 21,876 Bytes
afd56bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | import React, { useState, useRef, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { MessageSquare, X, Send, BookOpen, AlertCircle, CheckCircle2, ChevronDown, ChevronUp, Pin, PinOff, Trash2 } from 'lucide-react';
import { askProjectQuestion, clearProjectQuestionsHistory } from '../../api/client';
import toast from 'react-hot-toast';
interface ProjectQAPanelProps {
projectId: string;
projectName?: string;
}
interface Message {
id: string;
role: 'user' | 'agent';
content: string;
sources?: string[];
confidence?: number;
recommendation?: string;
created_at?: string;
}
const ProjectQAPanel: React.FC<ProjectQAPanelProps> = ({ projectId, projectName }) => {
const [isOpen, setIsOpen] = useState(false);
const [isPinned, setIsPinned] = useState(false);
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [isLoading, setIsLoading] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
useEffect(() => {
const fetchHistory = async () => {
try {
const history = await import('../../api/client').then(m => m.getProjectQuestionsHistory(projectId));
if (history && history.length > 0) {
const loadedMessages: Message[] = [];
history.forEach((h: any) => {
loadedMessages.push({ id: h.id + '_q', role: 'user', content: h.question, created_at: h.created_at });
loadedMessages.push({
id: h.id + '_a', role: 'agent', content: h.answer,
sources: h.sources, confidence: h.confidence, recommendation: h.recommendation, created_at: h.created_at
});
});
setMessages(loadedMessages);
}
} catch (err) {
console.error("Failed to load QA history", err);
}
};
fetchHistory();
}, [projectId]);
useEffect(() => {
scrollToBottom();
}, [messages, isOpen]);
useEffect(() => {
const handleCmdK = (e: KeyboardEvent) => {
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') {
e.preventDefault();
setIsOpen(prev => !prev);
}
};
window.addEventListener('keydown', handleCmdK);
return () => window.removeEventListener('keydown', handleCmdK);
}, []);
const handleClearHistory = async () => {
if (!window.confirm("Czy na pewno chcesz trwale usun膮膰 histori臋 rozm贸w z agentem dla tego projektu?")) {
return;
}
setIsLoading(true);
try {
await clearProjectQuestionsHistory(projectId);
setMessages([]);
toast.success("Historia zosta艂a pomy艣lnie wyczyszczona.");
} catch (error) {
console.error(error);
toast.error("Nie uda艂o si臋 usun膮膰 historii czatu.");
} finally {
setIsLoading(false);
}
};
const handleSend = async () => {
if (!input.trim() || isLoading) return;
const userMsg: Message = { id: Date.now().toString(), role: 'user', content: input };
setMessages(prev => [...prev, userMsg]);
setInput('');
setIsLoading(true);
try {
const data = await askProjectQuestion(projectId, userMsg.content);
const agentMsg: Message = {
id: (Date.now() + 1).toString(),
role: 'agent',
content: data.answer,
sources: data.sources,
confidence: data.confidence,
recommendation: data.recommendation
};
setMessages(prev => [...prev, agentMsg]);
} catch (error) {
console.error(error);
toast.error("Nie uda艂o si臋 pobra膰 odpowiedzi. Spr贸buj ponownie.");
const errorMsg: Message = {
id: (Date.now() + 1).toString(),
role: 'agent',
content: "Przepraszam, napotka艂em b艂膮d podczas analizy Twojego zapytania. Sprawd藕 po艂膮czenie i spr贸buj ponownie."
};
setMessages(prev => [...prev, errorMsg]);
} finally {
setIsLoading(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
return (
<div className="qa-panel-container" style={{ position: 'fixed', bottom: '4rem', right: '3rem', zIndex: 1000, display: 'flex', flexDirection: 'column', alignItems: 'flex-end', pointerEvents: 'none' }}>
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0, y: 20, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: 20, scale: 0.95 }}
transition={{ duration: 0.2 }}
style={{
width: '100%',
maxWidth: '400px',
height: '600px',
maxHeight: isPinned ? '100vh' : '80vh',
background: 'var(--bg-elevated)',
border: '1px solid rgba(255,255,255,0.1)',
borderRadius: isPinned ? '0' : '16px',
boxShadow: isPinned ? 'none' : '0 10px 40px rgba(0,0,0,0.5)',
display: 'flex',
flexDirection: 'column',
marginBottom: isPinned ? '0' : '1rem',
position: isPinned ? 'fixed' : 'relative',
right: isPinned ? 0 : 'auto',
top: isPinned ? 0 : 'auto',
bottom: isPinned ? 0 : 'auto',
pointerEvents: 'auto',
overflow: 'hidden',
zIndex: 1000
}}
>
{/* HEADER */}
<div style={{ padding: '1rem', borderBottom: '1px solid rgba(255,255,255,0.05)', display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', background: 'rgba(0,0,0,0.2)' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: '0.8rem' }}>
<div style={{ width: '32px', height: '32px', borderRadius: '8px', background: 'rgba(59, 130, 246, 0.2)', color: 'var(--accent-blue)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<MessageSquare size={16} />
</div>
<div>
<h3 style={{ margin: 0, fontSize: '1rem', fontWeight: 600 }}>Weryfikator (Ctrl+K)</h3>
{projectName && (
<div style={{ fontSize: '0.65rem', background: 'rgba(255,255,255,0.1)', padding: '2px 6px', borderRadius: '10px', marginTop: '4px', display: 'inline-block', maxWidth: '180px', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
{projectName}
</div>
)}
</div>
</div>
<div style={{ display: 'flex', gap: '0.3rem' }}>
<button onClick={handleClearHistory} style={{ background: 'transparent', border: 'none', color: 'var(--text-muted)', cursor: 'pointer', padding: '0.5rem' }} className="hover-bg rounded" title="Wyczy艣膰 histori臋">
<Trash2 size={16} />
</button>
<button onClick={() => setIsPinned(!isPinned)} style={{ background: 'transparent', border: 'none', color: isPinned ? 'var(--accent-blue)' : 'var(--text-muted)', cursor: 'pointer', padding: '0.5rem' }} className="hover-bg rounded" title={isPinned ? "Odepnij panel" : "Przypnij jako sidebar"}>
{isPinned ? <Pin size={16} fill="currentColor" /> : <Pin size={16} />}
</button>
{!isPinned && (
<button onClick={() => setIsOpen(false)} style={{ background: 'transparent', border: 'none', color: 'var(--text-muted)', cursor: 'pointer', padding: '0.5rem' }} className="hover-bg rounded" title="Zamknij (Ctrl+K)">
<X size={18} />
</button>
)}
</div>
</div>
{/* MESSAGES */}
<div style={{ flex: 1, overflowY: 'auto', padding: '1.5rem', display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
{messages.length === 0 ? (
<div style={{ textAlign: 'center', color: 'var(--text-muted)', marginTop: '2rem' }}>
<AlertCircle size={32} style={{ opacity: 0.5, margin: '0 auto 1rem' }} />
<p style={{ fontSize: '0.9rem', marginBottom: '0.5rem' }}>O co chcesz zapyta膰?</p>
<p style={{ fontSize: '0.8rem', opacity: 0.7 }}>Agent odpowie na podstawie regulamin贸w i Twoich wygenerowanych sekcji wniosku.</p>
</div>
) : (
messages.map((msg) => (
<div key={msg.id} style={{ display: 'flex', flexDirection: 'column', alignItems: msg.role === 'user' ? 'flex-end' : 'flex-start' }}>
<div style={{
maxWidth: '85%',
padding: '1rem',
borderRadius: '12px',
background: msg.role === 'user' ? 'rgba(59, 130, 246, 0.2)' : 'rgba(255,255,255,0.03)',
border: `1px solid ${msg.role === 'user' ? 'rgba(59, 130, 246, 0.3)' : 'rgba(255,255,255,0.05)'}`,
color: 'var(--text-primary)',
fontSize: '0.9rem',
lineHeight: '1.5',
whiteSpace: 'pre-wrap'
}}>
{/<SUGGESTION[^>]*>/.test(msg.content) ? (
<div dangerouslySetInnerHTML={{
__html: msg.content.replace(/<SUGGESTION[^>]*>/g, '<div style="margin-top: 10px; padding: 10px; background: rgba(59, 130, 246, 0.1); border-left: 3px solid var(--accent-blue); border-radius: 4px;"><strong>Sugestia:</strong><br/>').replace(/<\/SUGGESTION>/g, '</div>').replace(/\n/g, '<br/>')
}} />
) : (
msg.content
)}
{/* AGENT EXTRA DETAILS */}
{msg.role === 'agent' && (
<div style={{ marginTop: '1rem', paddingTop: '1rem', borderTop: '1px solid rgba(255,255,255,0.05)', display: 'flex', flexDirection: 'column', gap: '0.8rem' }}>
{msg.sources && msg.sources.length > 0 && (
<div>
<span style={{ fontSize: '0.75rem', color: 'var(--text-muted)', display: 'flex', alignItems: 'center', gap: '0.4rem', marginBottom: '0.4rem' }}>
<BookOpen size={12} /> Zidentyfikowane 殴r贸d艂a:
</span>
<ul style={{ margin: 0, paddingLeft: '1rem', fontSize: '0.8rem', color: 'var(--text-secondary)' }}>
{msg.sources.map((s, i) => <li key={i}>{s}</li>)}
</ul>
</div>
)}
{msg.recommendation && (
<div style={{ padding: '0.8rem', background: 'rgba(16, 185, 129, 0.1)', borderLeft: '3px solid var(--accent-green)', borderRadius: '0 4px 4px 0', fontSize: '0.85rem' }}>
<span style={{ display: 'block', color: 'var(--accent-green)', fontWeight: 600, marginBottom: '0.2rem', fontSize: '0.75rem' }}>REKOMENDACJA</span>
{msg.recommendation}
</div>
)}
{msg.confidence !== undefined && (
<div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', fontSize: '0.75rem', color: 'var(--text-muted)' }}>
{msg.confidence > 0.8 ? <CheckCircle2 size={12} color="var(--accent-green)" /> : <AlertCircle size={12} color="#f59e0b" />}
Pewno艣膰: {Math.round(msg.confidence * 100)}%
</div>
)}
<div style={{ marginTop: '0.5rem', display: 'flex', justifyContent: 'flex-end' }}>
<button
onClick={() => {
const textToCopy = msg.content.replace(/<\/?SUGGESTION[^>]*>/g, '').trim();
navigator.clipboard.writeText(textToCopy);
toast.success('Skopiowano tre艣膰 do schowka, mo偶esz wklei膰 w edytorze.');
}}
style={{ fontSize: '0.75rem', padding: '0.3rem 0.6rem', border: '1px solid rgba(255,255,255,0.1)', background: 'rgba(255,255,255,0.05)', color: '#fff', borderRadius: '4px', cursor: 'pointer' }}
className="hover-bg"
>
Kopiuj Odpowied藕
</button>
</div>
</div>
)}
</div>
</div>
))
)}
{isLoading && (
<div style={{ alignSelf: 'flex-start', maxWidth: '85%', padding: '1rem', borderRadius: '12px', background: 'rgba(255,255,255,0.03)', border: '1px solid rgba(255,255,255,0.05)', display: 'flex', gap: '0.5rem' }}>
<div style={{ width: '8px', height: '8px', borderRadius: '50%', background: 'var(--text-muted)', animation: 'pulse 1.5s infinite' }} />
<div style={{ width: '8px', height: '8px', borderRadius: '50%', background: 'var(--text-muted)', animation: 'pulse 1.5s infinite 0.2s' }} />
<div style={{ width: '8px', height: '8px', borderRadius: '50%', background: 'var(--text-muted)', animation: 'pulse 1.5s infinite 0.4s' }} />
</div>
)}
<div ref={messagesEndRef} />
</div>
{/* INPUT */}
<div style={{ padding: '1.2rem', borderTop: '1px solid rgba(255,255,255,0.05)', background: 'rgba(0,0,0,0.1)' }}>
<div style={{ position: 'relative' }}>
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Zadaj pytanie w kontek艣cie projektu..."
style={{
width: '100%',
background: 'rgba(255,255,255,0.03)',
border: '1px solid rgba(255,255,255,0.1)',
borderRadius: '8px',
padding: '0.8rem 3rem 0.8rem 1rem',
color: '#fff',
resize: 'none',
height: '60px',
fontSize: '0.9rem',
outline: 'none',
transition: 'all 0.2s'
}}
className="focus-ring"
/>
<button
onClick={handleSend}
disabled={!input.trim() || isLoading}
style={{
position: 'absolute',
right: '0.5rem',
bottom: '0.5rem',
height: '42px',
width: '42px',
borderRadius: '6px',
background: input.trim() && !isLoading ? 'var(--accent-blue)' : 'rgba(255,255,255,0.05)',
color: input.trim() && !isLoading ? '#fff' : 'var(--text-muted)',
border: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: input.trim() && !isLoading ? 'pointer' : 'default',
transition: 'all 0.2s'
}}
>
<Send size={16} />
</button>
</div>
<div style={{ fontSize: '0.7rem', color: 'var(--text-muted)', textAlign: 'center', marginTop: '0.8rem' }}>
Weryfikator powo艂uje si臋 na zaktualizowan膮 baz臋 regulamin贸w programu.
</div>
</div>
</motion.div>
)}
</AnimatePresence>
{/* TOGGLE BUTTON - Hidden if pinned */}
{!isPinned && (
<motion.button
className="tour-step-qa"
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
onClick={() => setIsOpen(!isOpen)}
style={{
width: '60px',
height: '60px',
borderRadius: '30px',
background: 'var(--accent-blue)',
color: '#fff',
border: 'none',
boxShadow: '0 4px 20px rgba(59, 130, 246, 0.4)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'auto',
zIndex: 1001
}}
>
{isOpen ? <ChevronDown size={28} /> : <MessageSquare size={28} />}
</motion.button>
)}
<style>{`
@keyframes pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }
`}</style>
</div>
);
};
export default ProjectQAPanel;
|