2026-07-17 00:52:17 +02:00
|
|
|
|
import React, { useState, useRef, useEffect } from 'react';
|
|
|
|
|
|
import clsx from 'clsx';
|
2026-07-17 13:45:21 +02:00
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
|
|
|
|
|
import remarkGfm from 'remark-gfm';
|
2026-07-17 09:21:40 +02:00
|
|
|
|
import {
|
|
|
|
|
|
streamChat, fetchMessages, fetchAttachments, uploadAttachment, getAttachmentDownloadUrl,
|
|
|
|
|
|
type ChatMessage, type ChatAttachment,
|
|
|
|
|
|
} from '@/api/ai';
|
2026-07-17 00:52:17 +02:00
|
|
|
|
|
|
|
|
|
|
interface ChatWindowProps {
|
|
|
|
|
|
sessionId: string;
|
|
|
|
|
|
agentId?: string | null;
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
compact?: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
|
function formatSize(bytes: number): string {
|
|
|
|
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
|
|
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
|
|
|
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-17 13:45:21 +02:00
|
|
|
|
function MessageContent({ content, role }: { content: string; role: string }) {
|
|
|
|
|
|
if (role === 'user') {
|
|
|
|
|
|
return <div className="whitespace-pre-wrap break-words">{content}</div>;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="prose prose-sm max-w-none break-words">
|
|
|
|
|
|
<ReactMarkdown remarkPlugins={[remarkGfm]}>{content}</ReactMarkdown>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-17 00:52:17 +02:00
|
|
|
|
export function ChatWindow({ sessionId, agentId, className, compact }: ChatWindowProps) {
|
|
|
|
|
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
2026-07-17 09:21:40 +02:00
|
|
|
|
const [attachments, setAttachments] = useState<ChatAttachment[]>([]);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
const [input, setInput] = useState('');
|
|
|
|
|
|
const [isStreaming, setIsStreaming] = useState(false);
|
|
|
|
|
|
const [streamingContent, setStreamingContent] = useState('');
|
|
|
|
|
|
const [toolStatus, setToolStatus] = useState<string | null>(null);
|
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-07-17 09:21:40 +02:00
|
|
|
|
const [uploadingFile, setUploadingFile] = useState(false);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
const scrollRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
const inputRef = useRef<HTMLTextAreaElement>(null);
|
2026-07-17 09:21:40 +02:00
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!sessionId) return;
|
|
|
|
|
|
setMessages([]);
|
2026-07-17 09:21:40 +02:00
|
|
|
|
setAttachments([]);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
setError(null);
|
2026-07-17 13:45:21 +02:00
|
|
|
|
fetchMessages(sessionId).then(setMessages).catch((e) => setError(e?.message || 'Failed to load'));
|
2026-07-17 09:21:40 +02:00
|
|
|
|
fetchAttachments(sessionId).then(setAttachments).catch(() => {});
|
2026-07-17 00:52:17 +02:00
|
|
|
|
}, [sessionId]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-07-17 13:45:21 +02:00
|
|
|
|
if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
2026-07-17 00:52:17 +02:00
|
|
|
|
}, [messages, streamingContent]);
|
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
|
const handleFileSelect = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
|
const files = Array.from(e.target.files || []);
|
|
|
|
|
|
if (files.length === 0) return;
|
|
|
|
|
|
setUploadingFile(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
|
const att = await uploadAttachment(sessionId, file);
|
|
|
|
|
|
setAttachments((prev) => [...prev, att]);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setError(e instanceof Error ? e.message : 'Upload failed');
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setUploadingFile(false);
|
|
|
|
|
|
if (fileInputRef.current) fileInputRef.current.value = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-07-17 00:52:17 +02:00
|
|
|
|
const handleSend = async () => {
|
|
|
|
|
|
const content = input.trim();
|
|
|
|
|
|
if (!content || isStreaming) return;
|
|
|
|
|
|
setInput('');
|
|
|
|
|
|
setIsStreaming(true);
|
|
|
|
|
|
setStreamingContent('');
|
|
|
|
|
|
setToolStatus(null);
|
|
|
|
|
|
setError(null);
|
|
|
|
|
|
|
2026-07-17 13:45:21 +02:00
|
|
|
|
const userMsg: ChatMessage = { id: 'temp-' + Date.now(), session_id: sessionId, role: 'user', content, tokens: 0, model_used: '' };
|
2026-07-17 00:52:17 +02:00
|
|
|
|
setMessages((prev) => [...prev, userMsg]);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const stream = streamChat(sessionId, content, agentId || undefined);
|
|
|
|
|
|
let accumulated = '';
|
|
|
|
|
|
for await (const event of stream) {
|
|
|
|
|
|
if (event.type === 'token' && event.content) {
|
|
|
|
|
|
accumulated += event.content;
|
|
|
|
|
|
setStreamingContent(accumulated);
|
|
|
|
|
|
} else if (event.type === 'tool_calls' && event.tools) {
|
|
|
|
|
|
setToolStatus(`Tools: ${event.tools.join(', ')}`);
|
|
|
|
|
|
} else if (event.type === 'tool_result' && event.tool) {
|
|
|
|
|
|
setToolStatus(`Tool '${event.tool}' ausgefuehrt`);
|
|
|
|
|
|
} else if (event.type === 'done') {
|
|
|
|
|
|
setToolStatus(null);
|
|
|
|
|
|
const msgs = await fetchMessages(sessionId);
|
|
|
|
|
|
setMessages(msgs);
|
|
|
|
|
|
setStreamingContent('');
|
|
|
|
|
|
} else if (event.type === 'error') {
|
|
|
|
|
|
setError(event.content || 'Unknown error');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setError(e instanceof Error ? e.message : 'Stream failed');
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsStreaming(false);
|
|
|
|
|
|
setStreamingContent('');
|
|
|
|
|
|
setToolStatus(null);
|
|
|
|
|
|
inputRef.current?.focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
2026-07-17 13:45:21 +02:00
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); }
|
2026-07-17 00:52:17 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className={clsx('flex flex-col h-full bg-white', className)}>
|
2026-07-17 09:21:40 +02:00
|
|
|
|
{attachments.length > 0 && (
|
|
|
|
|
|
<div className={clsx('border-b border-secondary-200 bg-secondary-50', compact ? 'px-2 py-1' : 'px-4 py-2')}>
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
{attachments.map((att) => (
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<a key={att.id} href={getAttachmentDownloadUrl(att.id)} target="_blank" rel="noopener noreferrer" className="flex items-center gap-1.5 rounded-md bg-white border border-secondary-200 px-2 py-1 text-xs hover:border-primary-400">
|
|
|
|
|
|
<svg className="w-3.5 h-3.5 text-secondary-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" /></svg>
|
2026-07-17 09:21:40 +02:00
|
|
|
|
<span className="max-w-32 truncate">{att.filename}</span>
|
|
|
|
|
|
<span className="text-secondary-400">{formatSize(att.size_bytes)}</span>
|
|
|
|
|
|
</a>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-07-17 00:52:17 +02:00
|
|
|
|
<div ref={scrollRef} className={clsx('flex-1 overflow-y-auto', compact ? 'p-3' : 'p-6')}>
|
|
|
|
|
|
{messages.length === 0 && !streamingContent && !error && (
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<div className="flex items-center justify-center h-full text-secondary-400 text-sm">Starte eine Konversation...</div>
|
2026-07-17 00:52:17 +02:00
|
|
|
|
)}
|
|
|
|
|
|
{messages.map((msg) => (
|
|
|
|
|
|
<div key={msg.id} className={clsx('mb-4 flex', msg.role === 'user' ? 'justify-end' : 'justify-start')}>
|
|
|
|
|
|
<div className={clsx('max-w-[80%] rounded-lg px-4 py-2 text-sm', msg.role === 'user' ? 'bg-primary-600 text-white' : 'bg-secondary-100 text-secondary-900')}>
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<MessageContent content={msg.content} role={msg.role} />
|
2026-07-17 00:52:17 +02:00
|
|
|
|
{msg.tool_calls && msg.tool_calls.length > 0 && (
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<div className="mt-2 text-xs opacity-70">Tools: {msg.tool_calls.map((tc: any) => tc.function?.name).join(', ')}</div>
|
2026-07-17 00:52:17 +02:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
{streamingContent && (
|
|
|
|
|
|
<div className="mb-4 flex justify-start">
|
|
|
|
|
|
<div className="max-w-[80%] rounded-lg px-4 py-2 text-sm bg-secondary-100 text-secondary-900">
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<MessageContent content={streamingContent} role="assistant" />
|
2026-07-17 00:52:17 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-07-17 13:45:21 +02:00
|
|
|
|
{toolStatus && <div className="mb-2 text-center text-xs text-secondary-500">⚙️ {toolStatus}</div>}
|
|
|
|
|
|
{error && <div className="mb-4 flex justify-center"><div className="rounded-lg px-4 py-2 text-sm bg-red-50 text-red-700">{error}</div></div>}
|
2026-07-17 00:52:17 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className={clsx('border-t border-secondary-200', compact ? 'p-2' : 'p-4')}>
|
|
|
|
|
|
<div className="flex items-end gap-2">
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<input ref={fileInputRef} type="file" multiple onChange={handleFileSelect} className="hidden" />
|
|
|
|
|
|
<button onClick={() => fileInputRef.current?.click()} disabled={uploadingFile || isStreaming} className="rounded-lg border border-secondary-300 p-2 text-secondary-500 hover:bg-secondary-100 disabled:opacity-50" title="Datei anhängen">
|
|
|
|
|
|
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" /></svg>
|
2026-07-17 00:52:17 +02:00
|
|
|
|
</button>
|
2026-07-17 13:45:21 +02:00
|
|
|
|
<textarea ref={inputRef} value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={handleKeyDown} disabled={isStreaming} placeholder="Nachricht eingeben..." rows={compact ? 1 : 2} className={clsx('flex-1 resize-none rounded-lg border border-secondary-300 px-3 py-2 text-sm', 'focus:outline-none focus:ring-2 focus:ring-primary-500', 'disabled:opacity-50')} />
|
|
|
|
|
|
<button onClick={handleSend} disabled={isStreaming || !input.trim()} className={clsx('rounded-lg px-4 py-2 text-sm font-medium text-white', 'bg-primary-600 hover:bg-primary-700', 'disabled:opacity-50 disabled:cursor-not-allowed')}>{isStreaming ? '...' : 'Senden'}</button>
|
2026-07-17 00:52:17 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|