'use client' import React, { useState, useEffect } from 'react' import { useSDK } from '@/lib/sdk' // ============================================================================= // TYPES // ============================================================================= interface ChatMessage { id: string role: 'user' | 'assistant' content: string sources?: Source[] timestamp: Date } interface Source { title: string reference: string relevance: number } interface Regulation { name: string description?: string collection?: string } // ============================================================================= // COMPONENTS // ============================================================================= function MessageBubble({ message }: { message: ChatMessage }) { const isUser = message.role === 'user' return (
{message.content}
{message.sources && message.sources.length > 0 && (

Quellen:

{message.sources.map((source, i) => ( {source.title} {source.relevance > 0 && ( ({Math.round(source.relevance * 100)}%) )} ))}
)}

{message.timestamp.toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' })}

) } // ============================================================================= // MAIN PAGE // ============================================================================= const RAG_API = '/api/sdk/v1/rag' const DEFAULT_QUESTIONS = [ 'Was sind die Rechte der Betroffenen nach DSGVO?', 'Wie lange betraegt die Meldefrist bei einer Datenpanne?', 'Welche Anforderungen stellt der AI Act an Hochrisiko-KI?', 'Wann brauche ich einen Auftragsverarbeitungsvertrag?', 'Was muss in einer Datenschutzerklaerung stehen?', ] export default function RAGPage() { const { state } = useSDK() const [messages, setMessages] = useState([]) const [inputValue, setInputValue] = useState('') const [isLoading, setIsLoading] = useState(false) const [suggestedQuestions, setSuggestedQuestions] = useState(DEFAULT_QUESTIONS) const [apiError, setApiError] = useState(null) // Load regulations for suggested questions useEffect(() => { fetch(`${RAG_API}/regulations`) .then(res => res.ok ? res.json() : null) .then(data => { if (data && Array.isArray(data.regulations) && data.regulations.length > 0) { const regs: Regulation[] = data.regulations const dynamicQuestions = regs.slice(0, 5).map((r: Regulation) => `Was sind die wichtigsten Anforderungen aus ${r.name}?` ) setSuggestedQuestions(dynamicQuestions.length > 0 ? dynamicQuestions : DEFAULT_QUESTIONS) } }) .catch(() => { // Keep default questions if API unavailable }) }, []) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!inputValue.trim() || isLoading) return const userMessage: ChatMessage = { id: `msg-${Date.now()}`, role: 'user', content: inputValue, timestamp: new Date(), } setMessages(prev => [...prev, userMessage]) setInputValue('') setIsLoading(true) setApiError(null) try { const res = await fetch(`${RAG_API}/search`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: inputValue, limit: 5 }), }) if (!res.ok) { throw new Error(`HTTP ${res.status}`) } const data = await res.json() // Build assistant message from results const results = data.results || [] let content = '' const sources: Source[] = [] if (results.length === 0) { content = 'Zu dieser Frage wurden keine passenden Dokumente gefunden. Bitte formulieren Sie Ihre Frage anders oder waehlen Sie ein spezifischeres Thema.' } else { const snippets = results.map((r: any, i: number) => { const title = r.regulation_name || r.regulation_short || `Dokument ${i + 1}` const ref = r.regulation_code || '' sources.push({ title, reference: ref, relevance: r.score || 0 }) return `**${title}${ref ? ` (${ref})` : ''}**\n${r.text || ''}` }) content = snippets.join('\n\n---\n\n') } const assistantMessage: ChatMessage = { id: `msg-${Date.now() + 1}`, role: 'assistant', content, sources, timestamp: new Date(), } setMessages(prev => [...prev, assistantMessage]) } catch (err) { setApiError('RAG-Backend nicht erreichbar. Bitte pruefen Sie die Verbindung zum AI Compliance SDK.') // Remove the user message that didn't get a response setMessages(prev => prev.filter(m => m.id !== userMessage.id)) } finally { setIsLoading(false) } } const handleSuggestedQuestion = (question: string) => { setInputValue(question) } return (
{/* Header */}

Legal RAG

Stellen Sie rechtliche Fragen zu DSGVO, AI Act und anderen Regelwerken

{/* Info Box */}

KI-gestuetzte Rechtsauskunft

Das System durchsucht DSGVO, AI Act, BDSG und weitere Regelwerke, um Ihre Fragen zu beantworten. Die Antworten ersetzen keine Rechtsberatung.

{/* Error Box */} {apiError && (

{apiError}

)} {/* Chat Area */}
{messages.length === 0 ? (

Stellen Sie eine Frage

Fragen Sie zu DSGVO, AI Act, Datenschutz oder Compliance-Themen.

) : ( messages.map(message => ( )) )} {isLoading && (
)}
{/* Suggested Questions */} {messages.length === 0 && (

Vorgeschlagene Fragen:

{suggestedQuestions.map((question, i) => ( ))}
)} {/* Input Area */}