fix: Restore all files lost during destructive rebase

A previous `git pull --rebase origin main` dropped 177 local commits,
losing 3400+ files across admin-v2, backend, studio-v2, website,
klausur-service, and many other services. The partial restore attempt
(660295e2) only recovered some files.

This commit restores all missing files from pre-rebase ref 98933f5e
while preserving post-rebase additions (night-scheduler, night-mode UI,
NightModeWidget dashboard integration).

Restored features include:
- AI Module Sidebar (FAB), OCR Labeling, OCR Compare
- GPU Dashboard, RAG Pipeline, Magic Help
- Klausur-Korrektur (8 files), Abitur-Archiv (5+ files)
- Companion, Zeugnisse-Crawler, Screen Flow
- Full backend, studio-v2, website, klausur-service
- All compliance SDKs, agent-core, voice-service
- CI/CD configs, documentation, scripts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-02-09 09:51:32 +01:00
parent f7487ee240
commit bfdaf63ba9
2009 changed files with 749983 additions and 1731 deletions

View File

@@ -0,0 +1,593 @@
'use client'
import { useState } from 'react'
import type { Collection, CreateCollectionData } from '../types'
const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'
interface CollectionsTabProps {
collections: Collection[]
loading: boolean
onRefresh: () => void
}
function CollectionsTab({
collections,
loading,
onRefresh
}: CollectionsTabProps) {
const [showCreateModal, setShowCreateModal] = useState(false)
const [creating, setCreating] = useState(false)
const [createError, setCreateError] = useState<string | null>(null)
const [formData, setFormData] = useState<CreateCollectionData>({
name: 'bp_',
display_name: '',
bundesland: 'NI',
use_case: '',
description: '',
})
const handleCreate = async () => {
setCreating(true)
setCreateError(null)
try {
const res = await fetch(`${API_BASE}/api/v1/admin/rag/collections`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData),
})
if (res.ok) {
setShowCreateModal(false)
setFormData({
name: 'bp_',
display_name: '',
bundesland: 'NI',
use_case: '',
description: '',
})
onRefresh()
} else {
const error = await res.json()
setCreateError(error.detail || 'Fehler beim Erstellen')
}
} catch (err) {
setCreateError('Netzwerkfehler')
} finally {
setCreating(false)
}
}
const useCaseOptions = [
{ value: 'klausur', label: 'Klausurkorrektur' },
{ value: 'zeugnis', label: 'Zeugniserstellung' },
{ value: 'material', label: 'Unterrichtsmaterial' },
{ value: 'curriculum', label: 'Lehrplan' },
{ value: 'other', label: 'Sonstiges' },
]
const bundeslandOptions = [
{ value: 'NI', label: 'Niedersachsen' },
{ value: 'NW', label: 'Nordrhein-Westfalen' },
{ value: 'BY', label: 'Bayern' },
{ value: 'BW', label: 'Baden-Württemberg' },
{ value: 'HE', label: 'Hessen' },
{ value: 'DE', label: 'Bundesweit' },
]
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div>
<h2 className="text-lg font-semibold text-slate-900">RAG Sammlungen</h2>
<p className="text-sm text-slate-500">Verwaltung der indexierten Dokumentensammlungen</p>
</div>
<div className="flex gap-3">
<button
onClick={onRefresh}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"
>
Aktualisieren
</button>
<button
onClick={() => setShowCreateModal(true)}
className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700 flex items-center gap-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
Neue Sammlung
</button>
</div>
</div>
{/* Loading State */}
{loading && (
<div className="flex items-center justify-center py-12">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600"></div>
</div>
)}
{/* Collections Grid */}
{!loading && (
<div className="grid gap-4">
{collections.length === 0 ? (
<div className="bg-slate-50 rounded-lg p-8 text-center">
<svg className="w-12 h-12 text-slate-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4" />
</svg>
<h3 className="text-lg font-medium text-slate-900 mb-2">Keine Sammlungen vorhanden</h3>
<p className="text-slate-500 mb-4">Erstellen Sie eine neue Sammlung, um Dokumente zu indexieren.</p>
<button
onClick={() => setShowCreateModal(true)}
className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700"
>
Erste Sammlung erstellen
</button>
</div>
) : (
collections.map((col) => (
<CollectionCard key={col.name} collection={col} />
))
)}
{/* Add new collection card */}
{collections.length > 0 && (
<button
onClick={() => setShowCreateModal(true)}
className="border-2 border-dashed border-slate-300 rounded-lg p-6 text-center hover:border-primary-400 hover:bg-primary-50 transition-colors cursor-pointer"
>
<svg className="w-8 h-8 text-slate-400 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
<span className="text-sm font-medium text-slate-600">Neue Sammlung erstellen</span>
</button>
)}
</div>
)}
{/* Create Collection Modal */}
{showCreateModal && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-white rounded-xl shadow-xl w-full max-w-lg mx-4">
<div className="p-6 border-b border-slate-200">
<h3 className="text-lg font-semibold text-slate-900">Neue RAG-Sammlung erstellen</h3>
<p className="text-sm text-slate-500 mt-1">
Erstellen Sie eine neue Sammlung für einen spezifischen Anwendungsfall
</p>
</div>
<div className="p-6 space-y-4">
{createError && (
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700">
{createError}
</div>
)}
<div>
<label className="block text-sm font-medium text-slate-700 mb-1">
Anzeigename *
</label>
<input
type="text"
value={formData.display_name}
onChange={(e) => setFormData(prev => ({ ...prev, display_name: e.target.value }))}
placeholder="z.B. Niedersachsen - Zeugniserstellung"
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500"
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-slate-700 mb-1">
Bundesland
</label>
<select
value={formData.bundesland}
onChange={(e) => setFormData(prev => ({ ...prev, bundesland: e.target.value }))}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
>
{bundeslandOptions.map(opt => (
<option key={opt.value} value={opt.value}>{opt.label}</option>
))}
</select>
</div>
<div>
<label className="block text-sm font-medium text-slate-700 mb-1">
Anwendungsfall *
</label>
<select
value={formData.use_case}
onChange={(e) => setFormData(prev => ({ ...prev, use_case: e.target.value }))}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
>
<option value="">Auswählen...</option>
{useCaseOptions.map(opt => (
<option key={opt.value} value={opt.value}>{opt.label}</option>
))}
</select>
</div>
</div>
<div>
<label className="block text-sm font-medium text-slate-700 mb-1">
Technischer Name *
</label>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
placeholder="bp_ni_zeugnis"
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500 font-mono text-sm"
/>
<p className="text-xs text-slate-500 mt-1">Muss mit "bp_" beginnen. Nur Kleinbuchstaben und Unterstriche.</p>
</div>
<div>
<label className="block text-sm font-medium text-slate-700 mb-1">
Beschreibung
</label>
<textarea
value={formData.description}
onChange={(e) => setFormData(prev => ({ ...prev, description: e.target.value }))}
placeholder="Wofür wird diese Sammlung verwendet?"
rows={3}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
/>
</div>
</div>
<div className="p-6 border-t border-slate-200 flex justify-end gap-3">
<button
onClick={() => {
setShowCreateModal(false)
setCreateError(null)
}}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"
>
Abbrechen
</button>
<button
onClick={handleCreate}
disabled={creating || !formData.name || !formData.display_name || !formData.use_case}
className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
>
{creating ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
Wird erstellt...
</>
) : (
'Sammlung erstellen'
)}
</button>
</div>
</div>
</div>
)}
</div>
)
}
function CollectionCard({ collection }: { collection: Collection }) {
const [ingesting, setIngesting] = useState(false)
const [reindexing, setReindexing] = useState(false)
const [ingestMessage, setIngestMessage] = useState<string | null>(null)
const [showReindexConfirm, setShowReindexConfirm] = useState(false)
const [chunkingStrategy, setChunkingStrategy] = useState<'semantic' | 'recursive'>('semantic')
const statusColors = {
ready: 'bg-green-100 text-green-800',
indexing: 'bg-yellow-100 text-yellow-800',
empty: 'bg-slate-100 text-slate-800',
}
const statusLabels = {
ready: 'Bereit',
indexing: 'Indexierung...',
empty: 'Leer',
}
const useCaseLabels: Record<string, string> = {
klausur: 'Klausurkorrektur',
zeugnis: 'Zeugniserstellung',
material: 'Unterrichtsmaterial',
curriculum: 'Lehrplan',
other: 'Sonstiges',
unknown: 'Unbekannt',
}
const handleIngest = async () => {
setIngesting(true)
setIngestMessage(null)
try {
const res = await fetch(`${API_BASE}/api/v1/admin/rag/collections/${collection.name}/ingest?incremental=true`, {
method: 'POST',
})
if (res.ok) {
const data = await res.json()
setIngestMessage(data.message || 'Indexierung gestartet')
} else {
const error = await res.json()
setIngestMessage(error.detail || 'Fehler beim Starten')
}
} catch (err) {
setIngestMessage('Netzwerkfehler')
} finally {
setIngesting(false)
}
}
const handleReindex = async () => {
setShowReindexConfirm(false)
setReindexing(true)
setIngestMessage('Starte Re-Indexierung...')
try {
// Start the reindex
const res = await fetch(
`${API_BASE}/api/v1/admin/rag/collections/${collection.name}/reindex?chunking_strategy=${chunkingStrategy}`,
{ method: 'POST' }
)
if (!res.ok) {
const error = await res.json()
setIngestMessage(error.detail || 'Fehler beim Starten')
setReindexing(false)
return
}
// Poll for progress
const pollProgress = async () => {
try {
const progressRes = await fetch(`${API_BASE}/api/v1/admin/rag/reindex/progress`)
if (progressRes.ok) {
const progress = await progressRes.json()
if (progress.phase === 'deleting') {
setIngestMessage('Lösche alte Chunks...')
} else if (progress.phase === 'indexing') {
const pct = progress.total_docs > 0
? Math.round((progress.current_doc / progress.total_docs) * 100)
: 0
setIngestMessage(
`Indexiere: ${progress.current_doc}/${progress.total_docs} (${pct}%) - ${progress.current_filename}`
)
} else if (progress.phase === 'complete') {
setIngestMessage(
`Fertig: ${progress.documents_processed} Dokumente, ` +
`${progress.chunks_created} neue Chunks (${progress.old_chunks_deleted} alte gelöscht)`
)
setReindexing(false)
return // Stop polling
} else if (progress.phase === 'failed') {
setIngestMessage(`Fehler: ${progress.error || 'Unbekannter Fehler'}`)
setReindexing(false)
return // Stop polling
}
// Continue polling if still running
if (progress.running) {
setTimeout(pollProgress, 1000)
} else {
setReindexing(false)
}
}
} catch (err) {
// Ignore polling errors, will retry
setTimeout(pollProgress, 2000)
}
}
// Start polling after a short delay
setTimeout(pollProgress, 500)
} catch (err) {
setIngestMessage('Netzwerkfehler bei Re-Indexierung')
setReindexing(false)
}
}
return (
<div className="bg-white rounded-lg border border-slate-200 p-6 hover:shadow-md transition-shadow">
<div className="flex items-start justify-between">
<div className="flex items-center gap-4">
<div className="w-12 h-12 bg-primary-100 rounded-lg flex items-center justify-center">
<svg className="w-6 h-6 text-primary-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
</svg>
</div>
<div>
<h3 className="text-lg font-semibold text-slate-900">{collection.displayName}</h3>
<p className="text-sm text-slate-500 font-mono">{collection.name}</p>
{collection.description && (
<p className="text-sm text-slate-600 mt-1">{collection.description}</p>
)}
</div>
</div>
<div className="flex flex-col items-end gap-2">
<span className={`px-3 py-1 rounded-full text-xs font-medium ${statusColors[collection.status]}`}>
{statusLabels[collection.status]}
</span>
<span className="px-2 py-1 bg-slate-100 text-slate-600 text-xs rounded">
{useCaseLabels[collection.useCase] || collection.useCase}
</span>
</div>
</div>
<div className="mt-4 grid grid-cols-2 md:grid-cols-4 gap-4">
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Chunks</p>
<p className="text-lg font-semibold text-slate-900">
{collection.chunkCount.toLocaleString()}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Jahre</p>
<p className="text-lg font-semibold text-slate-900">
{collection.years?.length > 0
? `${Math.min(...collection.years)}-${Math.max(...collection.years)}`
: '-'
}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Fächer</p>
<p className="text-lg font-semibold text-slate-900">
{collection.subjects?.length > 0 ? collection.subjects.length : '-'}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Bundesland</p>
<p className="text-lg font-semibold text-slate-900">
{collection.bundesland}
</p>
</div>
</div>
{collection.subjects.length > 0 && (
<div className="mt-4 flex flex-wrap gap-2">
{collection.subjects.slice(0, 8).map((subject) => (
<span
key={subject}
className="px-2 py-1 bg-slate-100 text-slate-700 text-xs rounded-md"
>
{subject}
</span>
))}
{collection.subjects.length > 8 && (
<span className="px-2 py-1 bg-slate-100 text-slate-500 text-xs rounded-md">
+{collection.subjects.length - 8} weitere
</span>
)}
</div>
)}
{/* Ingestion Buttons */}
<div className="mt-4 pt-4 border-t border-slate-200">
<div className="flex items-center gap-2 flex-wrap">
<button
onClick={handleIngest}
disabled={ingesting || reindexing}
className="px-4 py-2 text-sm font-medium text-primary-700 bg-primary-50 border border-primary-200 rounded-lg hover:bg-primary-100 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
>
{ingesting ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-primary-600"></div>
Wird gestartet...
</>
) : (
<>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
Neue indexieren
</>
)}
</button>
<button
onClick={() => setShowReindexConfirm(true)}
disabled={ingesting || reindexing || collection.chunkCount === 0}
className="px-4 py-2 text-sm font-medium text-amber-700 bg-amber-50 border border-amber-200 rounded-lg hover:bg-amber-100 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
title="Alle Dokumente mit neuem Chunking-Algorithmus neu indexieren"
>
{reindexing ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-amber-600"></div>
Re-Indexierung...
</>
) : (
<>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
Neu-Chunking
</>
)}
</button>
</div>
{ingestMessage && (
<p className="mt-2 text-sm text-slate-600">{ingestMessage}</p>
)}
</div>
{/* Re-Index Confirmation Modal */}
{showReindexConfirm && (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white rounded-xl p-6 max-w-md w-full mx-4 shadow-xl">
<h3 className="text-lg font-semibold text-slate-900 mb-4">
Collection neu indexieren?
</h3>
<p className="text-slate-600 mb-4">
Dies löscht alle {collection.chunkCount.toLocaleString()} bestehenden Chunks
und erstellt sie mit dem gewählten Chunking-Algorithmus neu.
</p>
<div className="mb-4">
<label className="block text-sm font-medium text-slate-700 mb-2">
Chunking-Strategie
</label>
<div className="flex gap-4">
<label className="flex items-center gap-2 cursor-pointer">
<input
type="radio"
name="chunkingStrategy"
value="semantic"
checked={chunkingStrategy === 'semantic'}
onChange={() => setChunkingStrategy('semantic')}
className="text-primary-600"
/>
<span className="text-sm">
<strong>Semantisch</strong>
<span className="text-slate-500 ml-1">(empfohlen)</span>
</span>
</label>
<label className="flex items-center gap-2 cursor-pointer">
<input
type="radio"
name="chunkingStrategy"
value="recursive"
checked={chunkingStrategy === 'recursive'}
onChange={() => setChunkingStrategy('recursive')}
className="text-primary-600"
/>
<span className="text-sm">Rekursiv (legacy)</span>
</label>
</div>
<p className="text-xs text-slate-500 mt-2">
Semantisches Chunking respektiert Satzgrenzen und verbessert die Suchqualität.
</p>
</div>
<div className="flex justify-end gap-3">
<button
onClick={() => setShowReindexConfirm(false)}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-slate-100 rounded-lg hover:bg-slate-200"
>
Abbrechen
</button>
<button
onClick={handleReindex}
className="px-4 py-2 text-sm font-medium text-white bg-amber-600 rounded-lg hover:bg-amber-700"
>
Neu indexieren
</button>
</div>
</div>
</div>
)}
</div>
)
}
// ============================================================================
// Upload Tab
// ============================================================================
export { CollectionsTab, CollectionCard }

View File

@@ -0,0 +1,425 @@
'use client'
import { useState, useEffect, useCallback } from 'react'
import type {
Collection,
IndexedDocument,
IndexedDocumentsData,
DocumentContent
} from '../types'
const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'
interface DocumentsTabProps {
collections: Collection[]
}
function DocumentsTab({ collections }: DocumentsTabProps) {
const [documents, setDocuments] = useState<IndexedDocumentsData | null>(null)
const [loading, setLoading] = useState(true)
const [selectedCollection, setSelectedCollection] = useState('bp_nibis_eh')
const [yearFilter, setYearFilter] = useState<string>('')
const [subjectFilter, setSubjectFilter] = useState<string>('')
const [currentPage, setCurrentPage] = useState(0)
const [deleting, setDeleting] = useState<string | null>(null)
const [deleteConfirm, setDeleteConfirm] = useState<string | null>(null)
const [viewingDoc, setViewingDoc] = useState<DocumentContent | null>(null)
const [loadingContent, setLoadingContent] = useState(false)
const pageSize = 20
const fetchDocuments = useCallback(async () => {
setLoading(true)
try {
const params = new URLSearchParams({
collection: selectedCollection,
limit: pageSize.toString(),
offset: (currentPage * pageSize).toString(),
})
if (yearFilter) params.append('year', yearFilter)
if (subjectFilter) params.append('subject', subjectFilter)
const res = await fetch(`${API_BASE}/api/v1/admin/rag/files/indexed?${params}`)
if (res.ok) {
const data = await res.json()
setDocuments(data)
}
} catch (err) {
console.error('Failed to fetch documents:', err)
} finally {
setLoading(false)
}
}, [selectedCollection, yearFilter, subjectFilter, currentPage])
useEffect(() => {
fetchDocuments()
}, [fetchDocuments])
const handleDelete = async (docId: string, deleteSource: boolean = false) => {
setDeleting(docId)
try {
const params = new URLSearchParams()
if (deleteSource) params.append('delete_source', 'true')
const res = await fetch(
`${API_BASE}/api/v1/admin/rag/files/${selectedCollection}/${encodeURIComponent(docId)}?${params}`,
{ method: 'DELETE' }
)
if (res.ok) {
// Refresh the list
await fetchDocuments()
setDeleteConfirm(null)
}
} catch (err) {
console.error('Failed to delete document:', err)
} finally {
setDeleting(null)
}
}
const handleViewDocument = async (docId: string) => {
setLoadingContent(true)
try {
const res = await fetch(
`${API_BASE}/api/v1/admin/rag/files/${selectedCollection}/${encodeURIComponent(docId)}/content`
)
if (res.ok) {
const data = await res.json()
setViewingDoc(data)
}
} catch (err) {
console.error('Failed to load document content:', err)
} finally {
setLoadingContent(false)
}
}
const handleDownloadPdf = (docId: string) => {
window.open(
`${API_BASE}/api/v1/admin/rag/files/${selectedCollection}/${encodeURIComponent(docId)}/download`,
'_blank'
)
}
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h2 className="text-lg font-semibold text-slate-900">Indexierte Dokumente</h2>
<p className="text-sm text-slate-500">
Übersicht aller indexierten Dokumente mit Möglichkeit zum Löschen
</p>
</div>
<button
onClick={fetchDocuments}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"
>
Aktualisieren
</button>
</div>
{/* Filters */}
<div className="bg-white rounded-lg border border-slate-200 p-4">
<div className="flex flex-wrap gap-4">
<div className="w-64">
<label className="block text-sm font-medium text-slate-700 mb-1">Sammlung</label>
<select
value={selectedCollection}
onChange={(e) => {
setSelectedCollection(e.target.value)
setCurrentPage(0)
}}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
>
{collections.length > 0 ? (
collections.map((col) => (
<option key={col.name} value={col.name}>
{col.displayName}
</option>
))
) : (
<option value="bp_nibis_eh">Niedersachsen - Klausurkorrektur</option>
)}
</select>
</div>
{documents && (
<>
<div className="w-32">
<label className="block text-sm font-medium text-slate-700 mb-1">Jahr</label>
<select
value={yearFilter}
onChange={(e) => {
setYearFilter(e.target.value)
setCurrentPage(0)
}}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
>
<option value="">Alle Jahre</option>
{documents.years?.map((y) => (
<option key={y} value={y}>{y}</option>
))}
</select>
</div>
<div className="w-48">
<label className="block text-sm font-medium text-slate-700 mb-1">Fach</label>
<select
value={subjectFilter}
onChange={(e) => {
setSubjectFilter(e.target.value)
setCurrentPage(0)
}}
className="w-full px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500"
>
<option value="">Alle Fächer</option>
{documents.subjects?.map((s) => (
<option key={s} value={s}>{s}</option>
))}
</select>
</div>
</>
)}
</div>
{documents && (
<div className="mt-4 flex items-center gap-6 text-sm text-slate-600">
<span>
<strong>{documents.total_documents ?? documents.totalCount}</strong> Dokumente
</span>
<span>
<strong>{(documents.total_chunks ?? 0).toLocaleString()}</strong> Chunks
</span>
</div>
)}
</div>
{/* Documents List */}
{loading ? (
<div className="flex items-center justify-center py-12">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600"></div>
</div>
) : documents && documents.documents.length > 0 ? (
<div className="bg-white rounded-lg border border-slate-200 overflow-hidden">
<table className="w-full">
<thead className="bg-slate-50 border-b border-slate-200">
<tr>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Jahr
</th>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Fach
</th>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Niveau
</th>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Typ
</th>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Chunks
</th>
<th className="px-4 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
Vorschau
</th>
<th className="px-4 py-3 text-right text-xs font-medium text-slate-500 uppercase tracking-wider">
Aktionen
</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-200">
{documents.documents.map((doc) => (
<tr key={doc.doc_id} className="hover:bg-slate-50">
<td className="px-4 py-3 text-sm font-medium text-slate-900">
{doc.year || '-'}
</td>
<td className="px-4 py-3 text-sm text-slate-700">
{doc.subject || '-'}
</td>
<td className="px-4 py-3 text-sm text-slate-600">
{doc.niveau || '-'}
</td>
<td className="px-4 py-3">
<span className="px-2 py-1 bg-slate-100 text-slate-700 text-xs rounded">
{doc.doc_type || '-'}
</span>
</td>
<td className="px-4 py-3 text-sm text-slate-600 font-mono">
{doc.chunk_count}
</td>
<td className="px-4 py-3 text-xs text-slate-500 max-w-xs truncate">
{doc.sample_text || '-'}
</td>
<td className="px-4 py-3 text-right">
{deleteConfirm === doc.doc_id ? (
<div className="flex items-center justify-end gap-2">
<button
onClick={() => handleDelete(doc.doc_id, false)}
disabled={deleting === doc.doc_id}
className="px-2 py-1 text-xs text-red-700 bg-red-100 rounded hover:bg-red-200"
>
Nur Index
</button>
<button
onClick={() => handleDelete(doc.doc_id, true)}
disabled={deleting === doc.doc_id}
className="px-2 py-1 text-xs text-red-900 bg-red-200 rounded hover:bg-red-300"
>
+ Datei
</button>
<button
onClick={() => setDeleteConfirm(null)}
className="px-2 py-1 text-xs text-slate-600 bg-slate-100 rounded hover:bg-slate-200"
>
Abbrechen
</button>
</div>
) : (
<div className="flex items-center justify-end gap-1">
<button
onClick={() => handleViewDocument(doc.doc_id)}
className="p-1 text-slate-400 hover:text-primary-600"
title="Dokument anzeigen"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
</svg>
</button>
<button
onClick={() => handleDownloadPdf(doc.doc_id)}
className="p-1 text-slate-400 hover:text-blue-600"
title="PDF herunterladen"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
</button>
<button
onClick={() => setDeleteConfirm(doc.doc_id)}
className="p-1 text-slate-400 hover:text-red-600"
title="Dokument löschen"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
)}
</td>
</tr>
))}
</tbody>
</table>
{/* Pagination */}
{documents.pagination && (
<div className="px-4 py-3 border-t border-slate-200 flex items-center justify-between bg-slate-50">
<span className="text-sm text-slate-600">
Zeige {documents.pagination.offset + 1} bis{' '}
{Math.min(documents.pagination.offset + pageSize, documents.total_documents ?? documents.totalCount)} von{' '}
{documents.total_documents ?? documents.totalCount}
</span>
<div className="flex gap-2">
<button
onClick={() => setCurrentPage((p) => Math.max(0, p - 1))}
disabled={currentPage === 0}
className="px-3 py-1 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded hover:bg-slate-50 disabled:opacity-50 disabled:cursor-not-allowed"
>
Zurück
</button>
<button
onClick={() => setCurrentPage((p) => p + 1)}
disabled={!documents.pagination.has_more}
className="px-3 py-1 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded hover:bg-slate-50 disabled:opacity-50 disabled:cursor-not-allowed"
>
Weiter
</button>
</div>
</div>
)}
</div>
) : (
<div className="bg-slate-50 rounded-lg p-8 text-center">
<svg className="w-12 h-12 text-slate-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<h3 className="text-lg font-medium text-slate-900 mb-2">Keine Dokumente gefunden</h3>
<p className="text-slate-500">
{yearFilter || subjectFilter
? 'Keine Dokumente mit den gewählten Filtern.'
: 'Diese Sammlung enthält noch keine indexierten Dokumente.'}
</p>
</div>
)}
{/* Document Viewer Modal */}
{(viewingDoc || loadingContent) && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
<div className="bg-white rounded-xl shadow-xl w-full max-w-4xl max-h-[90vh] flex flex-col">
{/* Header */}
<div className="p-4 border-b border-slate-200 flex items-center justify-between">
<div>
<h3 className="text-lg font-semibold text-slate-900">
{viewingDoc?.metadata?.filename || 'Dokument'}
</h3>
{viewingDoc?.metadata && (
<div className="flex items-center gap-3 mt-1 text-sm text-slate-500">
{viewingDoc.metadata.year && <span>{viewingDoc.metadata.year}</span>}
{viewingDoc.metadata.subject && <span>{viewingDoc.metadata.subject}</span>}
{viewingDoc.metadata.niveau && <span>{viewingDoc.metadata.niveau}</span>}
<span>{viewingDoc?.chunk_count} Chunks</span>
</div>
)}
</div>
<div className="flex items-center gap-2">
{viewingDoc && (
<button
onClick={() => handleDownloadPdf(viewingDoc.doc_id)}
className="px-3 py-1.5 text-sm font-medium text-blue-700 bg-blue-50 rounded-lg hover:bg-blue-100 flex items-center gap-1"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
PDF
</button>
)}
<button
onClick={() => setViewingDoc(null)}
className="p-1.5 text-slate-400 hover:text-slate-600 rounded-lg hover:bg-slate-100"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-6">
{loadingContent ? (
<div className="flex items-center justify-center py-12">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600"></div>
</div>
) : viewingDoc ? (
<div className="prose prose-slate max-w-none">
<pre className="whitespace-pre-wrap font-sans text-sm text-slate-700 bg-slate-50 p-4 rounded-lg">
{viewingDoc.text}
</pre>
</div>
) : null}
</div>
</div>
</div>
)}
</div>
)
}
// ============================================================================
// Search Tab
// ============================================================================
export { DocumentsTab }

View File

@@ -0,0 +1,512 @@
'use client'
import { useState, useEffect, useCallback } from 'react'
import type {
Collection,
IngestionStatus,
LiveProgress,
IndexedStats,
PendingFile,
PendingFilesData,
IngestionHistoryEntry
} from '../types'
const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'
interface IngestionTabProps {
status: IngestionStatus | null
onRefresh: () => void
}
function IngestionTab({
status,
onRefresh
}: IngestionTabProps) {
const [starting, setStarting] = useState(false)
const [liveProgress, setLiveProgress] = useState<LiveProgress | null>(null)
const [indexedStats, setIndexedStats] = useState<IndexedStats | null>(null)
const [pendingFiles, setPendingFiles] = useState<PendingFilesData | null>(null)
const [ingestionHistory, setIngestionHistory] = useState<IngestionHistoryEntry[]>([])
const [showHistory, setShowHistory] = useState(false)
const [showPending, setShowPending] = useState(false)
// Fetch indexed stats, pending files, and history on mount
useEffect(() => {
const fetchStats = async () => {
try {
const res = await fetch(`${API_BASE}/api/v1/admin/nibis/stats`)
if (res.ok) {
const data = await res.json()
setIndexedStats(data)
}
} catch (err) {
console.error('Failed to fetch stats:', err)
}
}
const fetchPending = async () => {
try {
const res = await fetch(`${API_BASE}/api/v1/admin/rag/files/pending`)
if (res.ok) {
const data = await res.json()
setPendingFiles(data)
}
} catch (err) {
console.error('Failed to fetch pending files:', err)
}
}
const fetchHistory = async () => {
try {
const res = await fetch(`${API_BASE}/api/v1/admin/rag/ingestion/history?limit=20`)
if (res.ok) {
const data = await res.json()
setIngestionHistory(data.history || [])
}
} catch (err) {
console.error('Failed to fetch history:', err)
}
}
fetchStats()
fetchPending()
fetchHistory()
}, [])
// Poll for live progress when running
useEffect(() => {
let interval: NodeJS.Timeout | null = null
const fetchProgress = async () => {
try {
const res = await fetch(`${API_BASE}/api/v1/admin/nibis/progress`)
if (res.ok) {
const data = await res.json()
setLiveProgress(data)
// Refresh stats when complete
if (!data.running && data.phase === 'complete') {
onRefresh()
// Also refresh indexed stats
const statsRes = await fetch(`${API_BASE}/api/v1/admin/nibis/stats`)
if (statsRes.ok) {
setIndexedStats(await statsRes.json())
}
}
}
} catch (err) {
console.error('Failed to fetch progress:', err)
}
}
// Start polling immediately and every 1.5 seconds
fetchProgress()
interval = setInterval(fetchProgress, 1500)
return () => {
if (interval) clearInterval(interval)
}
}, [onRefresh])
const startIngestion = async () => {
setStarting(true)
try {
await fetch(`${API_BASE}/api/v1/admin/nibis/ingest`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ewh_only: true, incremental: true }),
})
onRefresh()
} catch (err) {
console.error('Failed to start ingestion:', err)
} finally {
setStarting(false)
}
}
const phaseLabels: Record<string, string> = {
idle: 'Bereit',
extracting: 'Entpacke ZIP-Dateien...',
discovering: 'Suche Dokumente...',
indexing: 'Indexiere Dokumente...',
complete: 'Abgeschlossen',
}
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h2 className="text-lg font-semibold text-slate-900">Ingestion Status</h2>
<p className="text-sm text-slate-500">
Übersicht über laufende und vergangene Indexierungsvorgänge
</p>
</div>
<div className="flex gap-3">
<button
onClick={onRefresh}
className="px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"
>
Aktualisieren
</button>
<button
onClick={startIngestion}
disabled={status?.running || starting}
className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
{starting ? 'Startet...' : 'Ingestion starten'}
</button>
</div>
</div>
{/* Live Progress (when running) */}
{liveProgress?.running && (
<div className="bg-primary-50 rounded-lg border border-primary-200 p-6">
<div className="flex items-center gap-4 mb-4">
<div className="w-3 h-3 bg-primary-500 rounded-full animate-pulse"></div>
<span className="text-lg font-medium text-primary-900">
{liveProgress.phase ? (phaseLabels[liveProgress.phase] || liveProgress.phase) : 'Läuft...'}
</span>
<span className="ml-auto text-sm text-primary-700 font-mono">
{(liveProgress.percent ?? 0).toFixed(1)}%
</span>
</div>
{/* Progress Bar */}
<div className="h-3 bg-primary-200 rounded-full overflow-hidden mb-4">
<div
className="h-full bg-primary-600 transition-all duration-300"
style={{ width: `${liveProgress.percent ?? 0}%` }}
/>
</div>
{/* Current File */}
{liveProgress.current_filename && (
<p className="text-sm text-primary-700 mb-4 truncate">
<span className="font-medium">[{liveProgress.current_doc}/{liveProgress.total_docs}]</span>{' '}
{liveProgress.current_filename}
</p>
)}
{/* Live Stats */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm">
<div className="bg-white/50 rounded p-2">
<p className="text-primary-600 text-xs uppercase">Indexiert</p>
<p className="text-lg font-semibold text-primary-900">{liveProgress.documents_indexed}</p>
</div>
<div className="bg-white/50 rounded p-2">
<p className="text-primary-600 text-xs uppercase">Übersprungen</p>
<p className="text-lg font-semibold text-primary-900">{liveProgress.documents_skipped}</p>
</div>
<div className="bg-white/50 rounded p-2">
<p className="text-primary-600 text-xs uppercase">Chunks</p>
<p className="text-lg font-semibold text-primary-900">{liveProgress.chunks_created}</p>
</div>
<div className="bg-white/50 rounded p-2">
<p className="text-primary-600 text-xs uppercase">Fehler</p>
<p className={`text-lg font-semibold ${(liveProgress.errors_count ?? 0) > 0 ? 'text-red-600' : 'text-primary-900'}`}>
{liveProgress.errors_count ?? 0}
</p>
</div>
</div>
</div>
)}
{/* Indexed Data Overview - Always visible */}
{indexedStats?.indexed && (
<div className="bg-gradient-to-r from-green-50 to-emerald-50 rounded-lg border border-green-200 p-6">
<h3 className="text-lg font-semibold text-green-900 mb-4 flex items-center gap-2">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
Indexierte Daten (Gesamt)
</h3>
<div className="grid grid-cols-2 md:grid-cols-4 gap-6 mb-4">
<div className="bg-white/60 rounded-lg p-3">
<p className="text-xs text-green-700 uppercase tracking-wider">Chunks gesamt</p>
<p className="text-2xl font-bold text-green-900">
{(indexedStats.total_chunks ?? 0).toLocaleString()}
</p>
</div>
<div className="bg-white/60 rounded-lg p-3">
<p className="text-xs text-green-700 uppercase tracking-wider">Jahre</p>
<p className="text-2xl font-bold text-green-900">
{indexedStats.years?.length ?? 0}
</p>
<p className="text-xs text-green-600 mt-1">
{(indexedStats.years?.length ?? 0) > 0
? `${Math.min(...indexedStats.years!)} - ${Math.max(...indexedStats.years!)}`
: '-'
}
</p>
</div>
<div className="bg-white/60 rounded-lg p-3">
<p className="text-xs text-green-700 uppercase tracking-wider">Fächer</p>
<p className="text-2xl font-bold text-green-900">
{indexedStats.subjects?.length || 0}
</p>
</div>
<div className="bg-white/60 rounded-lg p-3">
<p className="text-xs text-green-700 uppercase tracking-wider">Status</p>
<p className="text-lg font-bold text-green-600">Bereit</p>
</div>
</div>
{/* Years breakdown */}
{indexedStats.years && indexedStats.years.length > 0 && (
<div className="flex flex-wrap gap-2">
{indexedStats.years.sort((a, b) => a - b).map((year) => (
<span
key={year}
className="px-3 py-1 bg-white/80 text-green-800 text-sm rounded-full font-medium"
>
{year}
</span>
))}
</div>
)}
</div>
)}
{/* Last Run Status */}
{!liveProgress?.running && (
<div className="bg-white rounded-lg border border-slate-200 p-6">
<h3 className="text-sm font-medium text-slate-700 mb-4">Letzter Indexierungslauf</h3>
<div className="flex items-center gap-4 mb-4">
<div className="w-3 h-3 bg-green-500 rounded-full"></div>
<span className="text-lg font-medium text-slate-900">
{liveProgress?.phase === 'complete' ? 'Abgeschlossen' : 'Bereit'}
</span>
</div>
{status && (
<div className="grid grid-cols-2 md:grid-cols-4 gap-6">
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Zeitpunkt</p>
<p className="text-lg font-semibold text-slate-900">
{status.lastRun
? new Date(status.lastRun).toLocaleString('de-DE')
: 'Noch nie'
}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Neu indexiert</p>
<p className="text-lg font-semibold text-slate-900">
{status.documentsIndexed ?? '-'}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Neue Chunks</p>
<p className="text-lg font-semibold text-slate-900">
{status.chunksCreated ?? '-'}
</p>
</div>
<div>
<p className="text-xs text-slate-500 uppercase tracking-wider">Fehler</p>
<p className={`text-lg font-semibold ${status.errors.length > 0 ? 'text-red-600' : 'text-slate-900'}`}>
{status.errors.length}
</p>
</div>
</div>
)}
{/* Show skipped info from live progress */}
{liveProgress && (liveProgress.documents_skipped ?? 0) > 0 && (
<p className="mt-3 text-sm text-slate-500">
{liveProgress.documents_skipped} Dokumente übersprungen (bereits indexiert)
</p>
)}
{status?.errors && status.errors.length > 0 && (
<div className="mt-4 p-4 bg-red-50 rounded-lg">
<h4 className="text-sm font-medium text-red-800 mb-2">Fehler</h4>
<ul className="text-sm text-red-700 space-y-1">
{status.errors.slice(0, 5).map((error, i) => (
<li key={i}>{error}</li>
))}
{status.errors.length > 5 && (
<li className="text-red-500">... und {status.errors.length - 5} weitere</li>
)}
</ul>
</div>
)}
</div>
)}
{/* Pending Files Section */}
{pendingFiles && (pendingFiles.pending_count ?? 0) > 0 && (
<div className="bg-amber-50 rounded-lg border border-amber-200 p-6">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-3">
<svg className="w-6 h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
<div>
<h3 className="text-lg font-semibold text-amber-900">
{pendingFiles.pending_count} Dateien warten auf Indexierung
</h3>
<p className="text-sm text-amber-700">
{pendingFiles.indexed_count} von {pendingFiles.total_files} Dateien sind indexiert
</p>
</div>
</div>
<button
onClick={() => setShowPending(!showPending)}
className="text-sm text-amber-700 hover:text-amber-900 font-medium"
>
{showPending ? 'Ausblenden' : 'Details anzeigen'}
</button>
</div>
{/* Pending by year summary */}
{pendingFiles.by_year && Object.keys(pendingFiles.by_year).length > 0 && (
<div className="flex flex-wrap gap-2 mb-4">
{Object.entries(pendingFiles.by_year)
.sort(([a], [b]) => Number(b) - Number(a))
.map(([year, count]) => (
<span
key={year}
className="px-3 py-1 bg-amber-100 text-amber-800 text-sm rounded-full font-medium"
>
{year}: {count} Dateien
</span>
))}
</div>
)}
{/* Pending files list */}
{showPending && pendingFiles.pending_files && (
<div className="mt-4 max-h-64 overflow-y-auto">
<table className="w-full text-sm">
<thead className="text-left text-amber-700 border-b border-amber-200">
<tr>
<th className="pb-2">Dateiname</th>
<th className="pb-2">Jahr</th>
<th className="pb-2">Fach</th>
<th className="pb-2">Niveau</th>
</tr>
</thead>
<tbody className="divide-y divide-amber-100">
{pendingFiles.pending_files.map((file) => (
<tr key={file.id} className="text-amber-900">
<td className="py-2 font-mono text-xs">{file.filename}</td>
<td className="py-2">{file.year}</td>
<td className="py-2">{file.subject}</td>
<td className="py-2">{file.niveau}</td>
</tr>
))}
</tbody>
</table>
{(pendingFiles.pending_count ?? 0) > 100 && (
<p className="mt-2 text-xs text-amber-600">
Zeige 100 von {pendingFiles.pending_count} Dateien
</p>
)}
</div>
)}
</div>
)}
{/* Ingestion History Section */}
<div className="bg-white rounded-lg border border-slate-200 p-6">
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-semibold text-slate-900">Indexierungs-Historie</h3>
<button
onClick={() => setShowHistory(!showHistory)}
className="text-sm text-slate-600 hover:text-slate-900 font-medium"
>
{showHistory ? 'Ausblenden' : `Alle anzeigen (${ingestionHistory.length})`}
</button>
</div>
{ingestionHistory.length === 0 ? (
<p className="text-slate-500 text-sm">Noch keine Indexierungsläufe durchgeführt.</p>
) : (
<div className="space-y-3">
{(showHistory ? ingestionHistory : ingestionHistory.slice(0, 3)).map((entry) => (
<div
key={entry.id}
className={`rounded-lg p-4 ${
entry.status === 'success'
? 'bg-green-50 border border-green-200'
: 'bg-red-50 border border-red-200'
}`}
>
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
{entry.status === 'success' ? (
<svg className="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
) : (
<svg className="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
)}
<span className="font-medium text-slate-900">
{new Date(entry.started_at || entry.startedAt).toLocaleString('de-DE')}
</span>
</div>
<span className="text-xs text-slate-500 font-mono">{entry.collection}</span>
</div>
{entry.stats && (
<div className="grid grid-cols-4 gap-4 text-sm">
<div>
<span className="text-slate-500">Gefunden:</span>{' '}
<span className="font-medium">{entry.stats.documents_found}</span>
</div>
<div>
<span className="text-slate-500">Indexiert:</span>{' '}
<span className="font-medium text-green-700">{entry.stats.documents_indexed}</span>
</div>
<div>
<span className="text-slate-500">Übersprungen:</span>{' '}
<span className="font-medium">{entry.stats.documents_skipped}</span>
</div>
<div>
<span className="text-slate-500">Chunks:</span>{' '}
<span className="font-medium">{entry.stats.chunks_created}</span>
</div>
</div>
)}
{entry.filters && (
<div className="mt-2 flex flex-wrap gap-2">
{entry.filters.year && (
<span className="px-2 py-0.5 bg-slate-200 text-slate-700 text-xs rounded">
Jahr: {entry.filters.year}
</span>
)}
{entry.filters.subject && (
<span className="px-2 py-0.5 bg-slate-200 text-slate-700 text-xs rounded">
Fach: {entry.filters.subject}
</span>
)}
{entry.filters.incremental && (
<span className="px-2 py-0.5 bg-blue-100 text-blue-700 text-xs rounded">
Inkrementell
</span>
)}
</div>
)}
{entry.error && (
<p className="mt-2 text-sm text-red-700">{entry.error}</p>
)}
</div>
))}
</div>
)}
</div>
</div>
)
}
// ============================================================================
// Documents Tab
// ============================================================================
export { IngestionTab }

View File

@@ -0,0 +1,327 @@
'use client'
import { useState, useCallback } from 'react'
import type { Collection, UploadResult } from '../types'
const API_BASE = process.env.NEXT_PUBLIC_KLAUSUR_SERVICE_URL || 'http://localhost:8086'
interface UploadTabProps {
collections: Collection[]
onUploadComplete: () => void
}
function UploadTab({
collections,
onUploadComplete
}: UploadTabProps) {
const [isDragging, setIsDragging] = useState(false)
const [files, setFiles] = useState<File[]>([])
const [uploading, setUploading] = useState(false)
const [uploadResults, setUploadResults] = useState<UploadResult[]>([])
const [currentFile, setCurrentFile] = useState<string>('')
const [selectedCollection, setSelectedCollection] = useState('bp_nibis_eh')
const handleDrop = useCallback((e: React.DragEvent) => {
e.preventDefault()
setIsDragging(false)
const droppedFiles = Array.from(e.dataTransfer.files)
const validFiles = droppedFiles.filter(
f => f.name.endsWith('.zip') || f.name.endsWith('.pdf')
)
setFiles(prev => [...prev, ...validFiles])
}, [])
const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
const selectedFiles = Array.from(e.target.files)
setFiles(prev => [...prev, ...selectedFiles])
}
}, [])
const removeFile = useCallback((index: number) => {
setFiles(prev => prev.filter((_, i) => i !== index))
}, [])
const handleUpload = async () => {
if (files.length === 0) return
setUploading(true)
setUploadResults([])
const results: UploadResult[] = []
try {
for (const file of files) {
setCurrentFile(file.name)
const formData = new FormData()
formData.append('file', file)
formData.append('collection', selectedCollection)
try {
const res = await fetch(`${API_BASE}/api/v1/admin/rag/upload`, {
method: 'POST',
body: formData,
})
if (res.ok) {
const data = await res.json()
results.push({
filename: file.name,
status: data.status,
pdfs_extracted: data.pdfs_extracted,
pdfs_skipped: data.pdfs_skipped,
duplicates: data.duplicates || [],
target_year: data.target_year,
message: data.message,
})
} else {
results.push({
filename: file.name,
status: 'error',
pdfs_extracted: 0,
pdfs_skipped: 0,
duplicates: [],
target_year: 0,
message: 'Upload fehlgeschlagen',
})
}
} catch {
results.push({
filename: file.name,
status: 'error',
pdfs_extracted: 0,
pdfs_skipped: 0,
duplicates: [],
target_year: 0,
message: 'Netzwerkfehler',
})
}
setUploadResults([...results])
}
setFiles([])
setCurrentFile('')
onUploadComplete()
} catch (err) {
console.error('Upload failed:', err)
} finally {
setUploading(false)
}
}
return (
<div className="space-y-6">
<div>
<h2 className="text-lg font-semibold text-slate-900">Dokumente hochladen</h2>
<p className="text-sm text-slate-500">
ZIP-Archive oder einzelne PDFs hochladen. ZIPs werden automatisch entpackt.
</p>
</div>
{/* Collection Selector */}
<div>
<label className="block text-sm font-medium text-slate-700 mb-2">
Ziel-Sammlung
</label>
<select
value={selectedCollection}
onChange={(e) => setSelectedCollection(e.target.value)}
className="w-full md:w-96 px-3 py-2 border border-slate-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500"
>
{collections.length > 0 ? (
collections.map((col) => (
<option key={col.name} value={col.name}>
{col.displayName} ({col.chunkCount.toLocaleString()} Chunks)
</option>
))
) : (
<option value="bp_nibis_eh">Niedersachsen - Klausurkorrektur</option>
)}
</select>
{collections.length === 0 && (
<p className="text-xs text-slate-500 mt-1">
Keine Sammlungen vorhanden. Erstellen Sie zuerst eine Sammlung im Tab "Sammlungen".
</p>
)}
</div>
{/* Drop Zone */}
<div
onDragOver={(e) => { e.preventDefault(); setIsDragging(true) }}
onDragLeave={() => setIsDragging(false)}
onDrop={handleDrop}
className={`
border-2 border-dashed rounded-lg p-12 text-center transition-colors
${isDragging
? 'border-primary-500 bg-primary-50'
: 'border-slate-300 hover:border-slate-400'
}
`}
>
<svg className="w-12 h-12 text-slate-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
</svg>
<p className="text-lg font-medium text-slate-700 mb-2">
ZIP-Datei oder Ordner hierher ziehen
</p>
<p className="text-sm text-slate-500 mb-4">
oder
</p>
<label className="cursor-pointer">
<span className="px-4 py-2 bg-white border border-slate-300 rounded-lg text-sm font-medium text-slate-700 hover:bg-slate-50">
Dateien auswählen
</span>
<input
type="file"
multiple
accept=".zip,.pdf"
onChange={handleFileSelect}
className="hidden"
/>
</label>
<p className="text-xs text-slate-400 mt-4">
Unterstützt: .zip, .pdf
</p>
</div>
{/* File Queue */}
{files.length > 0 && (
<div className="space-y-3">
<h3 className="text-sm font-medium text-slate-700">Upload-Queue ({files.length})</h3>
{files.map((file, index) => (
<div
key={index}
className="flex items-center justify-between bg-slate-50 rounded-lg p-3"
>
<div className="flex items-center gap-3">
<svg className="w-5 h-5 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<div>
<p className="text-sm font-medium text-slate-900">{file.name}</p>
<p className="text-xs text-slate-500">
{(file.size / 1024 / 1024).toFixed(2)} MB
</p>
</div>
</div>
<button
onClick={() => removeFile(index)}
className="p-1 text-slate-400 hover:text-red-500"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
))}
<button
onClick={handleUpload}
disabled={uploading}
className="w-full py-3 bg-primary-600 text-white rounded-lg font-medium hover:bg-primary-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
>
{uploading ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
{currentFile ? `Lade ${currentFile}...` : 'Wird hochgeladen...'}
</>
) : (
<>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
</svg>
Hochladen
</>
)}
</button>
</div>
)}
{/* Upload Results */}
{uploadResults.length > 0 && (
<div className="space-y-3">
<h3 className="text-sm font-medium text-slate-700">Upload-Ergebnisse</h3>
{uploadResults.map((result, index) => (
<div
key={index}
className={`rounded-lg p-4 ${
result.status === 'success'
? 'bg-green-50 border border-green-200'
: result.status === 'all_duplicates'
? 'bg-yellow-50 border border-yellow-200'
: result.status === 'partial_duplicates'
? 'bg-blue-50 border border-blue-200'
: 'bg-red-50 border border-red-200'
}`}
>
<div className="flex items-start justify-between">
<div className="flex items-center gap-2">
{result.status === 'success' && (
<svg className="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
)}
{result.status === 'all_duplicates' && (
<svg className="w-5 h-5 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
)}
{result.status === 'partial_duplicates' && (
<svg className="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
)}
{result.status === 'error' && (
<svg className="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
)}
<span className="font-medium text-slate-900">{result.filename}</span>
</div>
<span className="text-sm text-slate-500">Jahr {result.target_year}</span>
</div>
<p className={`mt-1 text-sm ${
result.status === 'success' ? 'text-green-700' :
result.status === 'all_duplicates' ? 'text-yellow-700' :
result.status === 'partial_duplicates' ? 'text-blue-700' :
'text-red-700'
}`}>
{result.message}
</p>
{(result.pdfs_extracted ?? 0) > 0 && (
<p className="mt-1 text-xs text-slate-500">
{result.pdfs_extracted} neue PDFs extrahiert
</p>
)}
{(result.pdfs_skipped ?? 0) > 0 && (result.duplicates?.length ?? 0) > 0 && (
<details className="mt-2">
<summary className="text-xs text-slate-500 cursor-pointer hover:text-slate-700">
{result.pdfs_skipped} Duplikate anzeigen
</summary>
<ul className="mt-1 text-xs text-slate-500 pl-4 max-h-32 overflow-y-auto">
{result.duplicates?.map((dup, i) => (
<li key={i} className="truncate">{dup}</li>
))}
</ul>
</details>
)}
</div>
))}
<button
onClick={() => setUploadResults([])}
className="text-sm text-slate-500 hover:text-slate-700"
>
Ergebnisse ausblenden
</button>
</div>
)}
</div>
)
}
// ============================================================================
// Ingestion Tab
// ============================================================================
export { UploadTab }

View File

@@ -0,0 +1,8 @@
/**
* RAG Admin Page Components
*/
export { CollectionsTab, CollectionCard } from './CollectionsTab'
export { UploadTab } from './UploadTab'
export { IngestionTab } from './IngestionTab'
export { DocumentsTab } from './DocumentsTab'