Spaces:
Sleeping
Sleeping
File size: 21,015 Bytes
aa63765 345b8ff aa63765 345b8ff aa63765 345b8ff 73fd1fc aa63765 345b8ff aa63765 73fd1fc aa63765 345b8ff 73fd1fc aa63765 73fd1fc 345b8ff 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc 345b8ff aa63765 345b8ff aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 73fd1fc aa63765 345b8ff aa63765 |
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
"use client";
import { useState, useRef, useEffect } from "react";
import Link from "next/link";
import { useTenant } from "@/contexts/TenantContext";
type SearchResult = {
text: string;
similarity?: number;
relevance?: number;
};
type Document = {
id: number;
text: string;
created_at?: string;
};
type SourceType = "raw_text" | "url" | "pdf" | "docx" | "txt" | "markdown";
const API_BASE =
process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, "") || "http://localhost:8000";
export function KnowledgeBasePanel() {
const { tenantId, isLoading: tenantLoading } = useTenant();
const [searchQuery, setSearchQuery] = useState("");
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
const [isSearching, setIsSearching] = useState(false);
const [ingestContent, setIngestContent] = useState("");
const [sourceType, setSourceType] = useState<SourceType>("raw_text");
const [filename, setFilename] = useState("");
const [url, setUrl] = useState("");
const [isIngesting, setIsIngesting] = useState(false);
const [ingestStatus, setIngestStatus] = useState<string | null>(null);
const [searchError, setSearchError] = useState<string | null>(null);
const [documents, setDocuments] = useState<Document[]>([]);
const [isLoadingDocs, setIsLoadingDocs] = useState(false);
const [isDeleting, setIsDeleting] = useState<number | null>(null);
const [isDeletingAll, setIsDeletingAll] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
async function handleSearch() {
if (!searchQuery.trim() || isSearching) return;
setIsSearching(true);
setSearchError(null);
setSearchResults([]);
try {
const response = await fetch(`${API_BASE}/rag/search`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-tenant-id": tenantId,
},
body: JSON.stringify({ query: searchQuery }),
});
if (!response.ok) {
throw new Error(`Search failed: ${response.status}`);
}
const data = await response.json();
setSearchResults(data.results || []);
} catch (err) {
console.error(err);
setSearchError(
err instanceof Error
? err.message
: "Failed to search knowledge base. Is the RAG MCP server running?",
);
} finally {
setIsSearching(false);
}
}
async function handleFileUpload(event: React.ChangeEvent<HTMLInputElement>) {
const file = event.target.files?.[0];
if (!file) return;
// Detect file type from extension
const ext = file.name.split('.').pop()?.toLowerCase();
let detectedType: SourceType = "raw_text";
if (ext === "pdf") detectedType = "pdf";
else if (ext === "docx" || ext === "doc") detectedType = "docx";
else if (ext === "txt" || ext === "text") detectedType = "txt";
else if (ext === "md" || ext === "markdown") detectedType = "markdown";
setSourceType(detectedType);
setFilename(file.name);
// For binary files (PDF, DOCX), upload directly to server
if (detectedType === "pdf" || detectedType === "docx") {
await handleFileIngest(file);
return;
}
// For text files, read and show in textarea
const reader = new FileReader();
reader.onload = async (e) => {
const text = e.target?.result as string;
setIngestContent(text);
};
reader.readAsText(file);
}
async function handleFileIngest(file: File) {
setIsIngesting(true);
setIngestStatus(null);
try {
const formData = new FormData();
formData.append("file", file);
const response = await fetch(`${API_BASE}/rag/ingest-file`, {
method: "POST",
headers: {
"x-tenant-id": tenantId,
},
body: formData,
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(
errorData.detail || `File ingestion failed: ${response.status}`,
);
}
const data = await response.json();
setIngestStatus(
`✅ ${data.message || `Successfully ingested ${data.chunks_stored || 0} chunk(s)`}`,
);
setFilename("");
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
// Reload documents after successful ingestion
loadDocuments();
} catch (err) {
console.error(err);
setIngestStatus(
err instanceof Error
? `❌ Error: ${err.message}`
: "Failed to ingest file. Is the RAG MCP server running?",
);
} finally {
setIsIngesting(false);
}
}
async function handleIngest() {
if (!ingestContent.trim() || isIngesting) return;
setIsIngesting(true);
setIngestStatus(null);
try {
// Prepare metadata
const metadata: Record<string, string> = {};
if (filename) metadata.filename = filename;
if (url || sourceType === "url") {
const ingestUrl = url || ingestContent.trim();
metadata.url = ingestUrl;
}
if (filename) {
// Generate doc_id from filename
metadata.doc_id = filename.replace(/[^a-zA-Z0-9]/g, "_").toLowerCase();
}
// Use the new enhanced endpoint
const response = await fetch(`${API_BASE}/rag/ingest-document`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-tenant-id": tenantId,
},
body: JSON.stringify({
action: "ingest_document",
tenant_id: tenantId,
source_type: sourceType,
content: sourceType === "url" ? (url || ingestContent.trim()) : ingestContent,
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(
errorData.detail || `Ingestion failed: ${response.status}`,
);
}
const data = await response.json();
setIngestStatus(
`✅ ${data.message || `Successfully ingested ${data.chunks_stored || 0} chunk(s)`}`,
);
setIngestContent("");
setFilename("");
setUrl("");
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
// Reload documents after successful ingestion
loadDocuments();
} catch (err) {
console.error(err);
setIngestStatus(
err instanceof Error
? `❌ Error: ${err.message}`
: "Failed to ingest content. Is the RAG MCP server running?",
);
} finally {
setIsIngesting(false);
}
}
async function loadDocuments() {
// Guard against empty tenant ID
if (!tenantId || !tenantId.trim() || isLoadingDocs) return;
setIsLoadingDocs(true);
try {
const response = await fetch(`${API_BASE}/rag/list?limit=10&offset=0`, {
method: "GET",
headers: {
"x-tenant-id": tenantId,
},
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
const errorMsg = errorData.detail || errorData.message || `Failed to load documents (${response.status})`;
if (response.status === 400) {
// Missing tenant ID - silently fail, user will see empty list
console.warn("Cannot load documents: Missing tenant ID");
setDocuments([]);
return;
} else if (response.status === 503) {
console.error("Cannot connect to RAG MCP server");
setDocuments([]);
return;
} else {
throw new Error(errorMsg);
}
}
const data = await response.json();
setDocuments(data.documents || []);
} catch (err) {
console.error(err);
setDocuments([]);
// Don't show error in status for document loading - it's not critical
} finally {
setIsLoadingDocs(false);
}
}
async function handleDeleteDocument(documentId: number) {
if (!tenantId.trim() || isDeleting !== null) return;
setIsDeleting(documentId);
try {
const response = await fetch(`${API_BASE}/rag/delete/${documentId}`, {
method: "DELETE",
headers: {
"x-tenant-id": tenantId,
},
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
const errorMsg = errorData.detail || `Failed to delete document: ${response.status}`;
throw new Error(errorMsg);
}
// Remove from local state
setDocuments(docs => docs.filter(doc => doc.id !== documentId));
setIngestStatus("✅ Document deleted successfully");
} catch (err) {
console.error(err);
setIngestStatus(
err instanceof Error
? `❌ Error: ${err.message}`
: "Failed to delete document",
);
} finally {
setIsDeleting(null);
}
}
async function handleDeleteAll() {
if (!tenantId.trim() || isDeletingAll) return;
if (!confirm("Are you sure you want to delete ALL documents for this tenant? This action cannot be undone.")) {
return;
}
setIsDeletingAll(true);
try {
const response = await fetch(`${API_BASE}/rag/delete-all`, {
method: "DELETE",
headers: {
"x-tenant-id": tenantId,
},
});
if (!response.ok) {
throw new Error(`Failed to delete all documents: ${response.status}`);
}
const data = await response.json();
setDocuments([]);
setIngestStatus(`✅ Deleted ${data.deleted_count || 0} document(s)`);
} catch (err) {
console.error(err);
setIngestStatus(
err instanceof Error
? `❌ Error: ${err.message}`
: "Failed to delete all documents",
);
} finally {
setIsDeletingAll(false);
}
}
// Load documents on mount and when tenant changes
useEffect(() => {
// Wait for tenant context to finish loading, then load documents if tenant ID is available
if (!tenantLoading && tenantId && tenantId.trim()) {
loadDocuments();
}
}, [tenantId, tenantLoading]);
return (
<section
id="knowledge-base"
className="gradient-border relative rounded-[28px] p-1 text-white"
>
<div className="glass-panel relative rounded-[26px] p-6">
<div className="flex flex-wrap items-center justify-between gap-4">
<div>
<p className="text-sm uppercase tracking-[0.5em] text-cyan-200/70">
Knowledge Base
</p>
<h2 className="mt-2 text-3xl font-semibold">
Search & ingest documents
</h2>
</div>
<Link
href="/knowledge-base"
className="rounded-full border border-cyan-500/50 bg-cyan-500/10 px-5 py-2.5 text-sm font-semibold text-cyan-300 transition hover:bg-cyan-500/20"
>
View All Documents →
</Link>
</div>
{/* Search Section */}
<div className="mt-6">
<div className="flex flex-col gap-3 md:flex-row">
<input
type="text"
placeholder="Search knowledge base (e.g., 'HR policy', 'refund procedure')..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
className="flex-1 rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-white outline-none focus:border-cyan-200/80"
/>
<button
onClick={handleSearch}
disabled={isSearching}
className="min-w-[140px] rounded-2xl bg-gradient-to-r from-sky-400 to-cyan-500 px-6 py-3 font-semibold text-slate-950 shadow-lg shadow-cyan-500/30 transition hover:-translate-y-0.5 disabled:cursor-not-allowed disabled:opacity-60"
>
{isSearching ? "Searching…" : "Search"}
</button>
</div>
{searchError && (
<p className="mt-3 rounded-2xl border border-red-500/40 bg-red-500/10 px-4 py-3 text-sm text-red-200">
{searchError}
</p>
)}
{searchResults.length > 0 && (
<div className="mt-4 space-y-3">
<p className="text-sm uppercase tracking-widest text-slate-400">
Found {searchResults.length} result(s)
</p>
{searchResults.map((result, idx) => (
<div
key={idx}
className="rounded-2xl border border-white/10 bg-slate-950/40 p-4"
>
<div className="flex items-start justify-between gap-3">
<p className="flex-1 text-sm text-slate-200">
{result.text}
</p>
{(result.similarity !== undefined ||
result.relevance !== undefined) && (
<span className="text-xs text-cyan-300">
{(
result.similarity ?? result.relevance ?? 0
).toFixed(2)}
</span>
)}
</div>
</div>
))}
</div>
)}
</div>
{/* Ingest Section */}
<div className="mt-8 rounded-2xl border border-white/10 bg-slate-950/40 p-4">
<p className="text-sm uppercase tracking-[0.5em] text-slate-400">
Add to Knowledge Base
</p>
<p className="mt-2 text-sm text-slate-300">
Upload files (PDF, DOCX, TXT, MD), paste text, or provide URLs. Content will be chunked, embedded, and stored.
</p>
{/* Source Type Selector */}
<div className="mt-4 flex flex-wrap gap-2">
{(["raw_text", "url", "pdf", "docx", "txt", "markdown"] as SourceType[]).map((type) => (
<button
key={type}
onClick={() => {
setSourceType(type);
if (type !== "url") setUrl("");
}}
className={`rounded-full px-4 py-2 text-xs font-semibold uppercase tracking-wider transition ${
sourceType === type
? "bg-cyan-500 text-slate-950"
: "bg-white/5 text-slate-300 hover:bg-white/10"
}`}
>
{type.replace("_", " ")}
</button>
))}
</div>
{/* File Upload */}
<div className="mt-4">
<input
ref={fileInputRef}
type="file"
accept=".pdf,.docx,.doc,.txt,.md,.markdown"
onChange={handleFileUpload}
className="hidden"
id="file-upload"
/>
<label
htmlFor="file-upload"
className="inline-flex cursor-pointer items-center gap-2 rounded-full border border-white/10 bg-white/5 px-4 py-2 text-sm text-slate-300 transition hover:bg-white/10"
>
📄 Upload File (PDF, DOCX, TXT, MD)
</label>
{filename && (
<span className="ml-3 text-sm text-cyan-300">{filename}</span>
)}
</div>
{/* URL Input (when source type is URL) */}
{sourceType === "url" && (
<input
type="url"
placeholder="Enter URL to fetch content from..."
value={url}
onChange={(e) => setUrl(e.target.value)}
className="mt-4 w-full rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-white outline-none focus:border-cyan-200/80"
/>
)}
{/* Content Textarea */}
<textarea
placeholder={
sourceType === "url"
? "Or paste URL here..."
: "Paste document content here (e.g., policy text, procedures, documentation, FAQs)..."
}
value={ingestContent}
onChange={(e) => setIngestContent(e.target.value)}
className="mt-4 w-full rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-white outline-none focus:border-cyan-200/80"
rows={6}
/>
{/* Filename Input (optional) */}
{sourceType !== "url" && (
<input
type="text"
placeholder="Filename (optional, e.g., policy.pdf)"
value={filename}
onChange={(e) => setFilename(e.target.value)}
className="mt-3 w-full rounded-2xl border border-white/10 bg-white/5 px-4 py-3 text-sm text-white outline-none focus:border-cyan-200/80"
/>
)}
<div className="mt-4 flex items-center gap-3">
<button
onClick={handleIngest}
disabled={
isIngesting ||
(!ingestContent.trim() && !url.trim() && sourceType === "url")
}
className="rounded-2xl bg-gradient-to-r from-emerald-400 to-teal-500 px-6 py-2.5 font-semibold text-slate-950 shadow-lg shadow-emerald-500/30 transition hover:-translate-y-0.5 disabled:cursor-not-allowed disabled:opacity-60"
>
{isIngesting ? "Ingesting…" : `Ingest as ${sourceType.replace("_", " ")}`}
</button>
{ingestStatus && (
<p className="text-sm text-slate-300">{ingestStatus}</p>
)}
</div>
</div>
{/* Manage Documents Section */}
<div className="mt-8 rounded-2xl border border-white/10 bg-slate-950/40 p-4">
<div className="flex items-center justify-between mb-4">
<div>
<p className="text-sm uppercase tracking-[0.5em] text-slate-400">
Manage Documents
</p>
<p className="mt-2 text-sm text-slate-300">
View and delete your ingested documents
</p>
</div>
<div className="flex items-center gap-3">
<button
onClick={loadDocuments}
disabled={isLoadingDocs}
className="rounded-full border border-white/10 bg-white/5 px-4 py-2 text-xs font-semibold text-slate-300 transition hover:bg-white/10 disabled:opacity-60"
>
{isLoadingDocs ? "Loading…" : "Refresh"}
</button>
{documents.length > 0 && (
<button
onClick={handleDeleteAll}
disabled={isDeletingAll}
className="rounded-full border border-red-500/50 bg-red-500/10 px-4 py-2 text-xs font-semibold text-red-300 transition hover:bg-red-500/20 disabled:opacity-60"
>
{isDeletingAll ? "Deleting…" : "Delete All"}
</button>
)}
</div>
</div>
{documents.length === 0 && !isLoadingDocs && (
<p className="text-sm text-slate-400 text-center py-4">
No documents found. Ingest some content to get started.
</p>
)}
{isLoadingDocs && (
<p className="text-sm text-slate-400 text-center py-4">
Loading documents…
</p>
)}
{documents.length > 0 && (
<div className="space-y-2 max-h-96 overflow-y-auto">
{documents.map((doc) => (
<div
key={doc.id}
className="flex items-start justify-between gap-3 rounded-xl border border-white/10 bg-white/5 p-3"
>
<div className="flex-1 min-w-0">
<p className="text-xs text-slate-400 mb-1">
ID: {doc.id}
{doc.created_at && (
<span className="ml-2">
• {new Date(doc.created_at).toLocaleDateString()}
</span>
)}
</p>
<p className="text-sm text-slate-200 line-clamp-2">
{doc.text.length > 150
? `${doc.text.substring(0, 150)}...`
: doc.text}
</p>
</div>
<button
onClick={() => handleDeleteDocument(doc.id)}
disabled={isDeleting === doc.id}
className="flex-shrink-0 rounded-lg border border-red-500/50 bg-red-500/10 px-3 py-1.5 text-xs font-semibold text-red-300 transition hover:bg-red-500/20 disabled:opacity-60"
>
{isDeleting === doc.id ? "Deleting…" : "Delete"}
</button>
</div>
))}
</div>
)}
</div>
</div>
</section>
);
}
|