File size: 24,759 Bytes
7c012de |
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 598 599 600 601 602 603 |
import { useState } from "react";
import { useMutation } from "@tanstack/react-query";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Checkbox } from "@/components/ui/checkbox";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import {
Search,
Loader2,
Brain,
Sparkles,
Target,
FileText,
Lightbulb,
ChevronDown,
ChevronUp,
Wand2,
AlertCircle
} from "lucide-react";
import { type SearchRequest } from "@shared/schema";
interface SearchInterfaceProps {
onSearch: (request: SearchRequest) => void;
onAISearch?: (query: string) => void;
isLoading?: boolean;
onDocumentSelect?: (documentId: number) => void;
}
interface EnhancedSearchResult {
results: any[];
enhancedQuery?: {
enhancedQuery: string;
intent: string;
keywords: string[];
suggestions: string[];
};
searchInsights?: {
totalResults: number;
avgRelevanceScore: number;
modalResultsCount: number;
localResultsCount: number;
};
}
export default function EnhancedSearchInterface({ onSearch, onAISearch, isLoading, onDocumentSelect }: SearchInterfaceProps) {
const [query, setQuery] = useState("");
const [searchType, setSearchType] = useState<"semantic" | "keyword" | "hybrid">("semantic");
const [sourceTypes, setSourceTypes] = useState<string[]>(["pdf", "web", "academic", "code"]);
const [showAITools, setShowAITools] = useState(false);
const [analysisText, setAnalysisText] = useState("");
const [selectedDocuments, setSelectedDocuments] = useState<number[]>([]);
const [useMarkdown, setUseMarkdown] = useState(true);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!query.trim()) return;
onSearch({
query: query.trim(),
searchType,
filters: {
sourceTypes: sourceTypes.length > 0 ? sourceTypes : undefined,
},
limit: 10,
offset: 0,
});
};
const handleSourceTypeChange = (sourceType: string, checked: boolean) => {
setSourceTypes(prev =>
checked
? [...prev, sourceType]
: prev.filter(type => type !== sourceType)
);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
} else if (e.key === "Escape") {
setQuery("");
}
};
// Enhanced AI Search
const aiSearchMutation = useMutation({
mutationFn: async (searchQuery: string): Promise<EnhancedSearchResult> => {
const response = await fetch("/api/ai-search", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: searchQuery,
maxResults: 10,
useQueryEnhancement: true
}),
});
if (!response.ok) throw new Error("Enhanced search failed");
return response.json();
},
});
// Query Enhancement
const queryEnhancementMutation = useMutation({
mutationFn: async (originalQuery: string) => {
const response = await fetch("/api/enhance-query", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: originalQuery }),
});
if (!response.ok) throw new Error("Query enhancement failed");
return response.json();
},
});
// Document Analysis
const documentAnalysisMutation = useMutation({
mutationFn: async ({ content, analysisType, useMarkdown }: { content: string; analysisType: string; useMarkdown?: boolean }) => {
const response = await fetch("/api/analyze-document", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content, analysisType, useMarkdown }),
});
if (!response.ok) throw new Error("Document analysis failed");
return response.json();
},
});
// Generate Embeddings
const embeddingsMutation = useMutation({
mutationFn: async (input: string) => {
const response = await fetch("/api/embeddings", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input }),
});
if (!response.ok) throw new Error("Embedding generation failed");
return response.json();
},
});
const handleEnhancedSearch = () => {
if (!query.trim()) return;
aiSearchMutation.mutate(query);
if (onAISearch) onAISearch(query);
};
const handleQueryEnhancement = () => {
if (!query.trim()) return;
queryEnhancementMutation.mutate(query);
};
const handleDocumentAnalysis = (analysisType: string) => {
if (!analysisText.trim()) return;
documentAnalysisMutation.mutate({
content: analysisText,
analysisType,
useMarkdown
});
};
const handleGenerateEmbeddings = () => {
if (!query.trim()) return;
embeddingsMutation.mutate(query);
};
const applyEnhancedQuery = (enhancedQuery: string) => {
setQuery(enhancedQuery);
onSearch({
query: enhancedQuery,
searchType,
filters: {
sourceTypes: sourceTypes.length > 0 ? sourceTypes : undefined,
},
limit: 10,
offset: 0,
});
};
return (
<div className="bg-white dark:bg-slate-800 rounded-xl shadow-sm border border-slate-200 dark:border-slate-700 p-6 mb-6">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-2">
<Brain className="w-5 h-5 text-blue-600" />
<h2 className="text-lg font-semibold text-slate-900 dark:text-slate-100">AI-Enhanced Search</h2>
<Badge variant="secondary" className="text-xs">Powered by Nebius & Modal</Badge>
</div>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => setShowAITools(!showAITools)}
className="flex items-center gap-1"
>
<Wand2 className="w-4 h-4" />
AI Tools
{showAITools ? <ChevronUp className="w-4 h-4" /> : <ChevronDown className="w-4 h-4" />}
</Button>
</div>
<form onSubmit={handleSubmit}>
<div className="flex flex-col lg:flex-row gap-4">
<div className="flex-1">
<Label htmlFor="knowledge-search" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
Search Knowledge Base
</Label>
<div className="relative">
<Input
id="knowledge-search"
type="text"
placeholder="Enter your query for AI-enhanced search... (Press Enter to search, Esc to clear)"
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={handleKeyDown}
className="pl-11 pr-12 focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
disabled={isLoading}
aria-label="Search knowledge base"
/>
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-slate-400 w-4 h-4" />
{query && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={handleQueryEnhancement}
disabled={queryEnhancementMutation.isPending}
className="absolute right-2 top-1/2 transform -translate-y-1/2 h-8 w-8 p-0"
title="Enhance query with AI"
>
{queryEnhancementMutation.isPending ? (
<Loader2 className="w-3 h-3 animate-spin" />
) : (
<Sparkles className="w-3 h-3 text-purple-500" />
)}
</Button>
)}
</div>
</div>
<div className="lg:w-auto">
<Label className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
Search Type
</Label>
<Select value={searchType} onValueChange={(value: any) => setSearchType(value)}>
<SelectTrigger className="w-full lg:w-40">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="semantic">Semantic Search</SelectItem>
<SelectItem value="keyword">Keyword Search</SelectItem>
<SelectItem value="hybrid">Hybrid Search</SelectItem>
</SelectContent>
</Select>
</div>
<div className="lg:w-auto flex items-end gap-2">
<Button
type="submit"
disabled={!query.trim() || isLoading}
className="px-6 py-3 bg-blue-600 hover:bg-blue-700 focus:ring-2 focus:ring-blue-600 focus:ring-offset-2"
aria-label={isLoading ? "Searching knowledge base" : "Search knowledge base"}
>
{isLoading ? (
<>
<Loader2 className="w-4 h-4 mr-2 animate-spin" aria-hidden="true" />
Searching...
</>
) : (
<>
<Search className="w-4 h-4 mr-2" aria-hidden="true" />
Search
</>
)}
</Button>
<Button
type="button"
variant="outline"
onClick={handleEnhancedSearch}
disabled={!query.trim() || aiSearchMutation.isPending}
className="px-4 py-3 border-purple-300 text-purple-700 hover:bg-purple-50 dark:border-purple-600 dark:text-purple-300 dark:hover:bg-purple-900/20"
title="AI-Enhanced Search"
>
{aiSearchMutation.isPending ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Brain className="w-4 h-4" />
)}
</Button>
</div>
</div>
{/* Search Filters */}
<div className="mt-4 pt-4 border-t border-slate-200 dark:border-slate-700">
<div className="flex flex-wrap gap-6">
{[
{ id: "pdf", label: "PDFs" },
{ id: "web", label: "Web Pages" },
{ id: "academic", label: "Academic Papers" },
{ id: "code", label: "Code Repositories" }
].map(({ id, label }) => (
<div key={id} className="flex items-center space-x-2">
<Checkbox
id={`filter-${id}`}
checked={sourceTypes.includes(id)}
onCheckedChange={(checked) => handleSourceTypeChange(id, !!checked)}
/>
<Label
htmlFor={`filter-${id}`}
className="text-sm text-slate-600 dark:text-slate-400 cursor-pointer"
>
{label}
</Label>
</div>
))}
</div>
</div>
</form>
{/* Query Enhancement Results */}
{queryEnhancementMutation.data && (
<div className="mt-4">
<Card className="bg-purple-50 dark:bg-purple-900/20 border-purple-200 dark:border-purple-800">
<CardContent className="pt-4">
<div className="flex items-center justify-between mb-3">
<h4 className="font-semibold text-purple-900 dark:text-purple-100 flex items-center gap-2">
<Sparkles className="w-4 h-4" />
Enhanced Query Suggestion
</h4>
<Button
size="sm"
onClick={() => applyEnhancedQuery(queryEnhancementMutation.data.enhancedQuery)}
className="bg-purple-600 hover:bg-purple-700"
>
Use This Query
</Button>
</div>
<p className="text-sm mb-3 font-mono bg-white dark:bg-gray-800 p-3 rounded border">
{queryEnhancementMutation.data.enhancedQuery}
</p>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3 text-xs">
<div>
<span className="font-medium text-purple-800 dark:text-purple-200">Intent:</span>
<span className="ml-2">{queryEnhancementMutation.data.intent}</span>
</div>
<div>
<span className="font-medium text-purple-800 dark:text-purple-200">Keywords:</span>
<div className="flex flex-wrap gap-1 mt-1">
{queryEnhancementMutation.data.keywords.map((keyword: string, i: number) => (
<Badge key={i} variant="outline" className="text-xs">
{keyword}
</Badge>
))}
</div>
</div>
</div>
</CardContent>
</Card>
</div>
)}
{/* AI Enhanced Search Results */}
{aiSearchMutation.data && (
<div className="mt-4">
<Card className="bg-green-50 dark:bg-green-900/20 border-green-200 dark:border-green-800">
<CardHeader className="pb-3">
<CardTitle className="flex items-center gap-2 text-lg">
<Brain className="w-5 h-5 text-green-600" />
AI-Enhanced Results
{aiSearchMutation.data.searchInsights && (
<Badge variant="secondary">
{aiSearchMutation.data.searchInsights.totalResults} results
</Badge>
)}
</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{aiSearchMutation.data.searchInsights && (
<div className="grid grid-cols-2 md:grid-cols-4 gap-3 p-3 bg-white dark:bg-gray-800 rounded-lg text-sm">
<div>
<span className="font-medium">Avg Relevance:</span>
<span className="ml-1 text-green-600">
{(aiSearchMutation.data.searchInsights.avgRelevanceScore * 100).toFixed(1)}%
</span>
</div>
<div>
<span className="font-medium">Modal Results:</span>
<span className="ml-1">{aiSearchMutation.data.searchInsights.modalResultsCount}</span>
</div>
<div>
<span className="font-medium">Local Results:</span>
<span className="ml-1">{aiSearchMutation.data.searchInsights.localResultsCount}</span>
</div>
<div>
<span className="font-medium">Total:</span>
<span className="ml-1">{aiSearchMutation.data.searchInsights.totalResults}</span>
</div>
</div>
)}
<div className="text-sm text-green-700 dark:text-green-300">
β¨ AI-enhanced search completed. Results are ranked by semantic relevance and include additional context.
</div>
</CardContent>
</Card>
</div>
)}
{/* Collapsible AI Tools */}
<Collapsible open={showAITools} onOpenChange={setShowAITools}>
<CollapsibleContent className="mt-4 pt-4 border-t border-slate-200 dark:border-slate-700">
<Tabs defaultValue="analysis" className="w-full">
<TabsList className="grid grid-cols-3 w-full mb-4">
<TabsTrigger value="analysis" className="flex items-center gap-2">
<FileText className="w-4 h-4" />
Analysis
</TabsTrigger>
<TabsTrigger value="embeddings" className="flex items-center gap-2">
<Sparkles className="w-4 h-4" />
Embeddings
</TabsTrigger>
<TabsTrigger value="tools" className="flex items-center gap-2">
<Target className="w-4 h-4" />
External Tools
</TabsTrigger>
</TabsList>
{/* Document Analysis Tab */}
<TabsContent value="analysis" className="space-y-4">
<div className="space-y-3">
<Label className="text-sm font-medium">Document Analysis</Label>
<Textarea
placeholder="Paste document content for AI analysis..."
value={analysisText}
onChange={(e) => setAnalysisText(e.target.value)}
className="min-h-24"
/>
{/* Formatting Option */}
<div className="flex items-center space-x-2 p-2 bg-gray-50 dark:bg-gray-800 rounded">
<Checkbox
id="use-markdown"
checked={useMarkdown}
onCheckedChange={(checked) => setUseMarkdown(!!checked)}
/>
<Label htmlFor="use-markdown" className="text-sm cursor-pointer">
Use markdown formatting (**bold**, bullet points, etc.)
</Label>
</div>
<div className="flex gap-2 flex-wrap">
{['summary', 'classification', 'key_points', 'quality_score'].map((type) => (
<Button
key={type}
variant="outline"
size="sm"
onClick={() => handleDocumentAnalysis(type)}
disabled={!analysisText.trim() || documentAnalysisMutation.isPending}
>
{documentAnalysisMutation.isPending ? (
<Loader2 className="w-3 h-3 animate-spin mr-1" />
) : (
<FileText className="w-3 h-3 mr-1" />
)}
{type.replace('_', ' ').toUpperCase()}
</Button>
))}
</div>
</div>
{documentAnalysisMutation.data && (
<Card className="bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800">
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<FileText className="w-5 h-5 text-blue-600" />
Analysis Result
</CardTitle>
</CardHeader>
<CardContent>
<div className="whitespace-pre-wrap text-sm">
{documentAnalysisMutation.data.analysis}
</div>
</CardContent>
</Card>
)}
</TabsContent>
{/* Embeddings Tab */}
<TabsContent value="embeddings" className="space-y-4">
<div className="space-y-3">
<Label className="text-sm font-medium">Generate Embeddings</Label>
<div className="flex gap-2">
<Input
placeholder="Text to generate embeddings..."
value={query}
onChange={(e) => setQuery(e.target.value)}
className="flex-1"
/>
<Button
onClick={handleGenerateEmbeddings}
disabled={!query.trim() || embeddingsMutation.isPending}
>
{embeddingsMutation.isPending ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Sparkles className="w-4 h-4" />
)}
</Button>
</div>
</div>
{embeddingsMutation.data && (
<Card className="bg-yellow-50 dark:bg-yellow-900/20 border-yellow-200 dark:border-yellow-800">
<CardContent className="pt-4 space-y-3">
<div className="grid grid-cols-2 gap-4 text-sm">
<div>
<span className="font-medium">Model:</span>
<span className="ml-2">{embeddingsMutation.data.model}</span>
</div>
<div>
<span className="font-medium">Dimensions:</span>
<span className="ml-2">{embeddingsMutation.data.data[0].embedding.length}</span>
</div>
</div>
<div>
<span className="font-medium text-sm">Vector (first 10 dimensions):</span>
<div className="font-mono text-xs bg-white dark:bg-gray-800 p-2 rounded mt-1 overflow-x-auto">
[{embeddingsMutation.data.data[0].embedding.slice(0, 10).map((val: number) => val.toFixed(4)).join(', ')}...]
</div>
</div>
</CardContent>
</Card>
)}
</TabsContent>
{/* External Tools Tab */}
<TabsContent value="tools" className="space-y-4">
<div>
<Label className="text-sm font-medium mb-3 block">AI Development Platforms</Label>
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
<Button
variant="outline"
className="h-auto p-4 hover:bg-blue-50 hover:border-blue-300 dark:hover:bg-blue-900/20"
onClick={() => window.open('https://studio.nebius.com/', '_blank')}
>
<div className="text-center">
<div className="text-2xl mb-1">π</div>
<div className="font-medium">Nebius Studio</div>
<div className="text-xs text-muted-foreground">AI model training & deployment</div>
</div>
</Button>
<Button
variant="outline"
className="h-auto p-4 hover:bg-green-50 hover:border-green-300 dark:hover:bg-green-900/20"
onClick={() => window.open('https://platform.openai.com/playground', '_blank')}
>
<div className="text-center">
<div className="text-2xl mb-1">π€</div>
<div className="font-medium">OpenAI Playground</div>
<div className="text-xs text-muted-foreground">Test and tune prompts</div>
</div>
</Button>
<Button
variant="outline"
className="h-auto p-4 hover:bg-orange-50 hover:border-orange-300 dark:hover:bg-orange-900/20"
onClick={() => window.open('https://huggingface.co/spaces', '_blank')}
>
<div className="text-center">
<div className="text-2xl mb-1">π€</div>
<div className="font-medium">HuggingFace</div>
<div className="text-xs text-muted-foreground">Open source AI models</div>
</div>
</Button>
</div>
</div>
</TabsContent>
</Tabs>
</CollapsibleContent>
</Collapsible>
{/* Error States */}
{(aiSearchMutation.error || documentAnalysisMutation.error || embeddingsMutation.error || queryEnhancementMutation.error) && (
<div className="mt-4">
<Card className="border-red-200 dark:border-red-800 bg-red-50 dark:bg-red-900/20">
<CardContent className="pt-4">
<div className="flex items-center gap-2 text-red-700 dark:text-red-300">
<AlertCircle className="w-4 h-4" />
<span className="font-medium">AI Operation Error</span>
</div>
<p className="text-sm text-red-600 dark:text-red-400 mt-1">
{(aiSearchMutation.error || documentAnalysisMutation.error || embeddingsMutation.error || queryEnhancementMutation.error)?.message}
</p>
</CardContent>
</Card>
</div>
)}
</div>
);
} |