diff --git a/.env.example b/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..88d19119e2e588d0610e730e85b6dd116de92af2 --- /dev/null +++ b/.env.example @@ -0,0 +1,18 @@ +# KnowledgeBridge Environment Configuration +# Copy this file to .env and fill in your API keys + +# Nebius AI Configuration (Required for AI features) +NEBIUS_API_KEY=your_nebius_api_key_here + +# Modal Configuration (Optional - for distributed processing) +MODAL_TOKEN_ID=your_modal_token_id +MODAL_TOKEN_SECRET=your_modal_token_secret +MODAL_BASE_URL=your_modal_endpoint + +# GitHub Configuration (Optional - for repository search) +GITHUB_TOKEN=your_github_personal_access_token + +# Node Environment +NODE_ENV=production +PORT=7860 +EOF < /dev/null \ No newline at end of file diff --git a/.gitattributes copy b/.gitattributes copy new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/.gitattributes copy @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..c1931370b05cd86c678cd881c5ae6803806b8220 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +node_modules +# dist - commented out for Hugging Face Spaces deployment +.DS_Store +server/public +vite.config.ts.* +*.tar.gz +attached_assets +server.log + +# Environment files +.env +.env.local +.env.production +.env.development + +# Logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Virtual environments +venv/ +env/ +ENV/ +env.bak/ +venv.bak/ + +# IDE +.vscode/ +.idea/ +*.swp +*.swo + +# OS +.DS_Store +Thumbs.db + +# Test coverage +coverage/ +.coverage +.nyc_output +htmlcov/ + +# Temporary files +*.tmp +*.temp +temp/ +tmp/ \ No newline at end of file diff --git a/.replit b/.replit new file mode 100644 index 0000000000000000000000000000000000000000..5b4d8d7fc5abb97cc462f6ebfa2d3629bac9328e --- /dev/null +++ b/.replit @@ -0,0 +1,37 @@ +modules = ["nodejs-20", "web", "postgresql-16", "python-3.11"] +run = "npm run dev" +hidden = [".config", ".git", "generated-icon.png", "node_modules", "dist"] + +[nix] +channel = "stable-24_05" +packages = ["ffmpeg-full"] + +[deployment] +deploymentTarget = "autoscale" +build = ["npm", "run", "build"] +run = ["npm", "run", "start"] + +[[ports]] +localPort = 5000 +externalPort = 80 + +[workflows] +runButton = "Project" + +[[workflows.workflow]] +name = "Project" +mode = "parallel" +author = "agent" + +[[workflows.workflow.tasks]] +task = "workflow.run" +args = "Start application" + +[[workflows.workflow]] +name = "Start application" +author = "agent" + +[[workflows.workflow.tasks]] +task = "shell.exec" +args = "npm run dev" +waitForPort = 5000 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..910bc718fa3be6bc1c64164d60ac391059410c94 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +# Use Node.js 18 Alpine for smaller image size +FROM node:18-alpine + +# Set working directory +WORKDIR /app + +# Install system dependencies +RUN apk add --no-cache \ + python3 \ + make \ + g++ \ + curl \ + && ln -sf python3 /usr/bin/python + +# Copy package files +COPY package*.json ./ + +# Install dependencies +RUN npm ci --only=production + +# Copy source code +COPY . . + +# Build the application +RUN npm run build + +# Create non-root user for security +RUN addgroup -g 1001 -S nodejs && \ + adduser -S nextjs -u 1001 + +# Change ownership of the app directory +RUN chown -R nextjs:nodejs /app + +# Switch to non-root user +USER nextjs + +# Expose port +EXPOSE 7860 + +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD curl -f http://localhost:7860/api/health || exit 1 + +# Start the application +CMD ["npm", "start"] \ No newline at end of file diff --git a/README copy.md b/README copy.md new file mode 100644 index 0000000000000000000000000000000000000000..f9e4fabfbe610e8eb0ce1e58f2dd736fadd898e2 --- /dev/null +++ b/README copy.md @@ -0,0 +1,344 @@ +--- +title: KnowledgeBridge +emoji: šŸ“š +colorFrom: yellow +colorTo: red +sdk: docker +pinned: false +license: mit +short_description: 'A sophisticated AI-powered knowledge retrieval and analysis ' +tags: + - agent-demo-track +--- + +# KnowledgeBridge + +šŸš€ **An AI-Enhanced Knowledge Discovery Platform** + +A sophisticated AI-powered knowledge retrieval and analysis system that combines semantic search, real-time web integration, and intelligent document processing for research and information discovery. + +![Security Status](https://img.shields.io/badge/Security-Hardened-green) +![TypeScript](https://img.shields.io/badge/TypeScript-100%25-blue) +![AI Models](https://img.shields.io/badge/AI-Nebius%20DeepSeek-purple) +![License](https://img.shields.io/badge/License-MIT-yellow) + +## šŸŽÆ Hackathon Submission + +**šŸ¤– Track 3: Agentic Demo Showcase** + +**Submitted to**: [Hugging Face Agents-MCP-Hackathon](https://huggingface.co/Agents-MCP-Hackathon) + +**Live Demo**: [Try KnowledgeBridge on Hugging Face Spaces](https://huggingface.co/spaces/YOUR_USERNAME/KnowledgeBridge) + +### **šŸš€ "Show us the most incredible things that your agents can do!"** + +KnowledgeBridge demonstrates sophisticated AI agent orchestration through multi-modal knowledge discovery, intelligent query enhancement, and autonomous research synthesis. + +## šŸ¤– Agentic Capabilities Showcase + +### 🧠 **Multi-Agent Orchestration** +- **Coordinated Search Agents**: Simultaneous deployment across GitHub, Wikipedia, ArXiv, and web sources +- **Intelligent Load Balancing**: Agents dynamically distribute workload based on query type and source availability +- **Fallback Agent Strategy**: Backup agents activate when primary sources fail or timeout +- **Real-Time Coordination**: Agents communicate results and adapt search strategies collaboratively + +### šŸ” **Query Enhancement Agents** +- **Intent Recognition Agents**: AI agents analyze user intent and suggest optimal search strategies +- **Semantic Expansion Agents**: Agents enhance queries with related terms and concepts +- **Context-Aware Agents**: Agents consider previous searches and user preferences +- **Multi-Modal Query Agents**: Agents adapt search approach based on content type (code, academic, general) + +### šŸ“Š **Analysis & Synthesis Agents** +- **Document Processing Agents**: Autonomous analysis with configurable reasoning (summary, classification, key points) +- **Research Synthesis Agents**: AI agents combine insights from multiple sources into coherent analysis +- **Quality Assessment Agents**: Agents evaluate source credibility and content relevance +- **Format Adaptation Agents**: Agents dynamically adjust output format (markdown/plain text) based on user needs + +### šŸ›”ļø **Security & Validation Agents** +- **URL Validation Agents**: Intelligent agents verify link accessibility and content authenticity +- **Rate Limiting Agents**: Protective agents prevent API abuse (100 requests/15min, 10/min for sensitive endpoints) +- **Input Sanitization Agents**: Security agents validate and clean all user inputs +- **Error Recovery Agents**: Resilient agents handle failures gracefully and maintain system stability + +### 🌐 **Intelligent Integration Agents** +- **ArXiv Academic Agents**: Specialized agents for academic paper validation and retrieval +- **GitHub Repository Agents**: Code-focused agents with author filtering and relevance scoring +- **Wikipedia Knowledge Agents**: Authoritative content agents with intelligent caching strategies +- **Cross-Platform Synthesis Agents**: Agents that combine and rank results across all sources + +## šŸ—ļø Technical Architecture + +### **Frontend Stack** +- **React 18** with TypeScript for type-safe development +- **Wouter Router** for lightweight client-side routing +- **TanStack Query** for efficient data fetching and caching +- **Radix UI + Tailwind CSS** for accessible, modern components +- **Framer Motion** for smooth animations and transitions + +### **Backend Stack** +- **Node.js + Express** with comprehensive middleware +- **Nebius AI** integration with DeepSeek models +- **Modal** for distributed processing and scalability +- **Express Rate Limit** for API protection +- **Helmet.js** for security headers + +### **AI & Processing** +- **DeepSeek-R1-0528** for chat completions and document analysis +- **BAAI/bge-en-icl** for embedding generation +- **Modal Client** for distributed compute tasks +- **Smart Ingestion Service** for advanced document processing + +## šŸš€ Quick Start + +### **Environment Configuration** + +Create a `.env` file in the project root: + +```bash +# Nebius AI Configuration (Required) +NEBIUS_API_KEY=your_nebius_api_key_here + +# Modal Configuration (Optional - for advanced processing) +MODAL_TOKEN_ID=your_modal_token_id +MODAL_TOKEN_SECRET=your_modal_token_secret +MODAL_BASE_URL=your_modal_endpoint + +# GitHub Configuration (Optional - for repository search) +GITHUB_TOKEN=your_github_token_here + +# Node Environment +NODE_ENV=development +``` + +### **Development Setup** + +```bash +# Install dependencies +npm install + +# Start development server +npm run dev + +# Build for production +npm run build + +# Type checking +npm run check +``` + +The application will be available at `http://localhost:5000` + +## šŸŽÆ Usage Guide + +### **Search Interface** +1. **Basic Search**: Enter queries in natural language +2. **AI Enhancement**: Click the sparkle icon to improve your query +3. **Advanced Search**: Use the AI tools panel for document analysis +4. **Export Results**: Generate citations in multiple formats + +### **AI Tools** +- **Document Analysis**: Paste content for AI-powered analysis with configurable formatting +- **Embeddings**: Generate vector representations of text +- **Query Enhancement**: Get AI suggestions for better search queries + +### **Knowledge Graph** +- Interactive visualization of document relationships +- Filter by concepts, authors, and source types +- Explore connections between research papers and topics + +## šŸ”§ API Reference + +### **Search Endpoints** +```typescript +POST /api/search +{ + query: string; + searchType: "semantic" | "keyword" | "hybrid"; + limit: number; + filters?: { + sourceTypes?: string[]; + }; +} +``` + +### **AI Analysis Endpoints** +```typescript +POST /api/analyze-document +{ + content: string; + analysisType: "summary" | "classification" | "key_points" | "quality_score"; + useMarkdown?: boolean; +} + +POST /api/enhance-query +{ + query: string; + context?: string; +} + +POST /api/embeddings +{ + input: string; + model?: string; +} +``` + +### **Health Check** +```typescript +GET /api/health +// Returns comprehensive health status of all services +``` + +## šŸš€ Performance & Reliability + +### **Response Times** +- Local search: <100ms for semantic queries +- Document analysis: ~3-5 seconds depending on content length +- URL validation: <2 seconds per URL with concurrent processing +- Embedding generation: ~500ms-1s per request + +### **Scalability Features** +- Rate limiting prevents API abuse +- Concurrent URL validation with configurable limits +- Efficient caching for repeated queries +- Graceful degradation when external services are unavailable + +### **Error Handling** +- React Error Boundaries prevent UI crashes +- Comprehensive API error responses +- Automatic retry logic for network requests +- User-friendly error messages + +## šŸ”’ Security Features + +### **Input Protection** +- Request body size limits (10MB) +- Comprehensive input sanitization +- SQL injection prevention +- XSS protection with CSP headers + +### **API Security** +- Rate limiting on all endpoints +- Secure environment variable handling +- No hardcoded credentials +- Proper error logging without information disclosure + +### **Infrastructure Security** +- Helmet.js security headers +- CORS configuration +- Secure cookie handling +- Production-ready error handling + +## šŸ› ļø Development + +### **Code Quality** +- 100% TypeScript coverage +- ESLint + Prettier configuration +- Comprehensive error handling +- Type-safe API contracts with Zod validation + +### **Testing** +```bash +# Type checking +npm run check + +# Development server +npm run dev + +# Production build +npm run build +``` + +## šŸŽ‰ Recent Updates + +- āœ… **Security Hardening**: Removed all hardcoded credentials, added comprehensive security middleware +- āœ… **TypeScript Migration**: Achieved 100% type safety across the entire codebase +- āœ… **URL Validation**: Intelligent filtering of broken and invalid links +- āœ… **Error Handling**: React Error Boundaries and improved server error handling +- āœ… **AI Enhancement**: Nebius AI integration with configurable document analysis +- āœ… **Performance**: Rate limiting, input validation, and optimized processing + +## šŸ“š Architecture Highlights + +### **AI Integration** +- **Nebius AI**: Primary AI service for all language model tasks +- **DeepSeek Models**: State-of-the-art reasoning capabilities +- **Modal Integration**: Distributed processing for heavy workloads +- **Embedding Search**: Semantic similarity matching + +### **Data Flow** +1. User query → AI query enhancement (optional) +2. Parallel search: local storage + external sources +3. URL validation and content verification +4. Result ranking and relevance scoring +5. AI-powered analysis and synthesis + +### **Component Architecture** +- **Enhanced Search Interface**: Unified search and AI tools +- **Knowledge Graph**: Interactive data visualization +- **Result Cards**: Rich content display with citations +- **Error Boundaries**: Resilient error handling + +## šŸ† Track 3: Agentic Demo Showcase Features + +### **šŸ¤– "Show us the most incredible things that your agents can do!"** + +KnowledgeBridge demonstrates sophisticated multi-agent systems in action: + +### **🧠 Autonomous Agent Workflows** +- **Smart Agent Coordination**: Multiple specialized agents work together to fulfill complex research tasks +- **Adaptive Agent Behavior**: Agents dynamically adjust strategies based on query complexity and source availability +- **Multi-Modal Agent Processing**: Different agent types (search, analysis, validation) collaborate seamlessly +- **Intelligent Agent Fallbacks**: Backup agents activate automatically when primary agents encounter issues + +### **šŸ” Real-Time Agent Decision Making** +- **Query Analysis Agents**: Instantly determine optimal search strategies across 4+ sources +- **Load Balancing Agents**: Distribute workload intelligently based on API response times and rate limits +- **Quality Control Agents**: Evaluate and filter results in real-time for relevance and authenticity +- **Synthesis Agents**: Combine disparate information sources into coherent, actionable insights + +### **šŸ“Š Advanced Agent Orchestration** +- **Parallel Agent Execution**: Simultaneous deployment of search agents across GitHub, Wikipedia, ArXiv +- **Agent Communication Protocols**: Real-time coordination between agents for optimal resource utilization +- **Adaptive Agent Learning**: Agents improve performance based on user interactions and feedback +- **Error Recovery Agents**: Autonomous problem-solving when individual agents encounter failures + +### **šŸ›”ļø Production-Grade Agent Infrastructure** +- **Security Agent Monitoring**: Continuous protection against abuse with intelligent rate limiting +- **Validation Agent Networks**: Multi-layer content verification and URL authenticity checking +- **Performance Agent Optimization**: Automatic scaling and resource management for enterprise workloads +- **Resilience Agent Systems**: Graceful degradation and fault tolerance across all agent operations + +### **⚔ Agent Performance Metrics** +- **Sub-second Agent Response**: Query analysis and routing in <100ms +- **Concurrent Agent Processing**: 4+ agents working simultaneously on complex research tasks +- **Intelligent Agent Caching**: Smart result storage and retrieval for enhanced performance +- **Scalable Agent Architecture**: Horizontal scaling support for enterprise deployment + +## šŸ“„ License + +MIT License - see [LICENSE](LICENSE) file for details. + +## šŸ”— Related Resources + +- [Nebius AI Documentation](https://docs.nebius.ai/) +- [Modal Documentation](https://modal.com/docs) +- [React Query Documentation](https://tanstack.com/query/latest) +- [Radix UI Components](https://www.radix-ui.com/) + +--- + +## šŸš€ Agents-MCP-Hackathon Submission Summary + +**KnowledgeBridge** showcases the incredible power of AI agents through: + +šŸ¤– **Multi-Agent Orchestration** - Coordinated intelligence across search, analysis, and synthesis agents +šŸ” **Real-Time Decision Making** - Agents adapt strategies and optimize performance dynamically +šŸ“Š **Advanced Agent Workflows** - Complex multi-step processes handled autonomously +šŸ›”ļø **Production-Ready Agent Infrastructure** - Enterprise-grade security and resilience + +**Track 3: Agentic Demo Showcase** - Demonstrating what happens when sophisticated AI agents work together to revolutionize knowledge discovery and research workflows. + +**Built for the Hugging Face Agents-MCP-Hackathon** šŸ† + +Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..3ac82d670c7cb44efc3b2a0841155240ccb96194 --- /dev/null +++ b/app.py @@ -0,0 +1,27 @@ +""" +KnowledgeBridge - AI-Enhanced Knowledge Discovery Platform +Hugging Face Spaces compatibility layer +""" + +import os +import subprocess +import sys + +def main(): + """ + This file exists for Hugging Face Spaces compatibility. + The actual application runs via Docker and the Node.js server. + """ + print("šŸš€ KnowledgeBridge is running via Docker on port 7860") + print("Visit the app interface above\!") + print("\nšŸ“š Features:") + print("- AI-Enhanced Search with Nebius DeepSeek") + print("- Multi-source search (GitHub, Wikipedia, ArXiv)") + print("- Interactive Knowledge Graphs") + print("- Document Analysis with configurable output") + print("- Enterprise-grade security") + print("\nšŸ”— Built for Agents-MCP-Hackathon") + +if __name__ == "__main__": + main() +EOF < /dev/null \ No newline at end of file diff --git a/client/index.html b/client/index.html new file mode 100644 index 0000000000000000000000000000000000000000..4b4d09e3151713b7af7e8d38dd2bac2a27d5a7ee --- /dev/null +++ b/client/index.html @@ -0,0 +1,13 @@ + + + + + + + +
+ + + + + \ No newline at end of file diff --git a/client/src/App.tsx b/client/src/App.tsx new file mode 100644 index 0000000000000000000000000000000000000000..1bda47f9578353eeeb9a31bb705b4ef6502a2481 --- /dev/null +++ b/client/src/App.tsx @@ -0,0 +1,35 @@ +import { Switch, Route } from "wouter"; +import { queryClient } from "./lib/queryClient"; +import { QueryClientProvider } from "@tanstack/react-query"; +import { Toaster } from "@/components/ui/toaster"; +import { TooltipProvider } from "@/components/ui/tooltip"; +import { ThemeProvider } from "@/components/theme-provider"; +import ErrorBoundary from "@/components/ErrorBoundary"; +import KnowledgeBase from "@/pages/knowledge-base"; +import NotFound from "@/pages/not-found"; + +function Router() { + return ( + + + + + ); +} + +function App() { + return ( + + + + + + + + + + + ); +} + +export default App; diff --git a/client/src/components/ErrorBoundary.tsx b/client/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000000000000000000000000000000000000..7100473cc1f12bce7c6db1362945b708754cd773 --- /dev/null +++ b/client/src/components/ErrorBoundary.tsx @@ -0,0 +1,75 @@ +import React, { Component, ErrorInfo, ReactNode } from 'react'; + +interface Props { + children: ReactNode; +} + +interface State { + hasError: boolean; + error: Error | null; +} + +class ErrorBoundary extends Component { + public state: State = { + hasError: false, + error: null + }; + + public static getDerivedStateFromError(error: Error): State { + return { hasError: true, error }; + } + + public componentDidCatch(error: Error, errorInfo: ErrorInfo) { + console.error('Error caught by boundary:', error, errorInfo); + } + + public render() { + if (this.state.hasError) { + return ( +
+
+
+
+ + + +
+
+

+ Something went wrong +

+
+

The application encountered an unexpected error. Please refresh the page and try again.

+
+
+ +
+
+
+ {process.env.NODE_ENV === 'development' && this.state.error && ( +
+
+ + Error Details (Development) + +
+                    {this.state.error.stack}
+                  
+
+
+ )} +
+
+ ); + } + + return this.props.children; + } +} + +export default ErrorBoundary; \ No newline at end of file diff --git a/client/src/components/knowledge-base/ai-assistant.tsx b/client/src/components/knowledge-base/ai-assistant.tsx new file mode 100644 index 0000000000000000000000000000000000000000..b7ef978b8acbf81a57b0b8e99afc4de78fa52bf2 --- /dev/null +++ b/client/src/components/knowledge-base/ai-assistant.tsx @@ -0,0 +1,507 @@ +import { useState } from "react"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +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 { + Brain, + Sparkles, + FileText, + Search, + Loader2, + TrendingUp, + Lightbulb, + Target, + CheckCircle, + AlertCircle +} from "lucide-react"; + +interface AIAssistantProps { + 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; + }; +} + +interface ResearchSynthesis { + synthesis: string; + keyFindings: string[]; + gaps: string[]; + recommendations: string[]; +} + +export default function AIAssistant({ onDocumentSelect }: AIAssistantProps) { + const [query, setQuery] = useState(""); + const [selectedDocuments, setSelectedDocuments] = useState([]); + const [analysisText, setAnalysisText] = useState(""); + const queryClient = useQueryClient(); + + // Enhanced AI Search + const aiSearchMutation = useMutation({ + mutationFn: async (searchQuery: string): Promise => { + 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(); + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["/api/search"] }); + }, + }); + + // 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 }: { content: string; analysisType: string }) => { + const response = await fetch("/api/analyze-document", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ content, analysisType }), + }); + if (!response.ok) throw new Error("Document analysis failed"); + return response.json(); + }, + }); + + // Research Synthesis + const researchSynthesisMutation = useMutation({ + mutationFn: async ({ query, documentIds }: { query: string; documentIds: number[] }): Promise => { + const response = await fetch("/api/research-synthesis", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ query, documentIds }), + }); + if (!response.ok) throw new Error("Research synthesis 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); + }; + + const handleQueryEnhancement = () => { + if (!query.trim()) return; + queryEnhancementMutation.mutate(query); + }; + + const handleDocumentAnalysis = (analysisType: string) => { + if (!analysisText.trim()) return; + documentAnalysisMutation.mutate({ content: analysisText, analysisType }); + }; + + const handleResearchSynthesis = () => { + if (!query.trim() || selectedDocuments.length === 0) return; + researchSynthesisMutation.mutate({ query, documentIds: selectedDocuments }); + }; + + const handleGenerateEmbeddings = () => { + if (!query.trim()) return; + embeddingsMutation.mutate(query); + }; + + return ( +
+ + + + + AI Research Assistant + Powered by Nebius & Modal + + + + + + + + Smart Search + + + + Analysis + + + + Synthesis + + + + Embeddings + + + + {/* Enhanced Search Tab */} + +
+
+ setQuery(e.target.value)} + onKeyDown={(e) => e.key === "Enter" && handleEnhancedSearch()} + className="flex-1" + /> + +
+ +
+ +
+
+ + {/* Query Enhancement Results */} + {queryEnhancementMutation.data && ( + + +

Enhanced Query

+

+ {queryEnhancementMutation.data.enhancedQuery} +

+
+
+ Intent: + {queryEnhancementMutation.data.intent} +
+
+ Keywords: +
+ {queryEnhancementMutation.data.keywords.map((keyword: string, i: number) => ( + + {keyword} + + ))} +
+
+
+
+
+ )} + + {/* Enhanced Search Results */} + {aiSearchMutation.data && ( + + + + + AI-Enhanced Results + {aiSearchMutation.data.searchInsights && ( + + {aiSearchMutation.data.searchInsights.totalResults} results + + )} + + + + {aiSearchMutation.data.searchInsights && ( +
+
+ Avg Relevance: + + {(aiSearchMutation.data.searchInsights.avgRelevanceScore * 100).toFixed(1)}% + +
+
+ Modal Results: + {aiSearchMutation.data.searchInsights.modalResultsCount} +
+
+ Local Results: + {aiSearchMutation.data.searchInsights.localResultsCount} +
+
+ Total: + {aiSearchMutation.data.searchInsights.totalResults} +
+
+ )} + +
+ {aiSearchMutation.data.results.map((result: any, index: number) => ( + +
+
{result.title}
+
+ {result.relevanceScore && ( + + {(result.relevanceScore * 100).toFixed(0)}% + + )} + {result.aiExplanation && ( + + )} +
+
+

+ {result.snippet} +

+ {result.keyReasons && ( +
+ AI Analysis: +
    + {result.keyReasons.slice(0, 2).map((reason: string, i: number) => ( +
  • {reason}
  • + ))} +
+
+ )} +
+ ))} +
+
+
+ )} +
+ + {/* Document Analysis Tab */} + +
+