'use client' import { useState } from 'react' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Badge } from '@/components/ui/badge' import { ScrollArea } from '@/components/ui/scroll-area' import { Separator } from '@/components/ui/separator' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { useAsk } from '@/lib/hooks/use-ask' import { useModels } from '@/lib/hooks/use-models' import { LoadingSpinner } from '@/components/common/LoadingSpinner' import { Sparkles, Brain, Lightbulb, Network } from 'lucide-react' import ReactMarkdown from 'react-markdown' interface KnowledgeGraphInsightsPanelProps { notebookId: string graphStats: { nodeCount: number edgeCount: number topConcepts?: Array<{ label: string mentions: number type?: string importance?: number }> nodeTypes?: Record relationshipTypes?: Record allNodes?: Array<{ label: string type: string mentions: number }> } } export function KnowledgeGraphInsightsPanel({ notebookId, graphStats }: KnowledgeGraphInsightsPanelProps) { const [question, setQuestion] = useState('') const [selectedStrategyModel, setSelectedStrategyModel] = useState('') const [selectedAnswerModel, setSelectedAnswerModel] = useState('') const [selectedFinalModel, setSelectedFinalModel] = useState('') const askHook = useAsk() const askState = { isStreaming: askHook.isStreaming, strategy: askHook.strategy, answers: askHook.answers, finalAnswer: askHook.finalAnswer, error: askHook.error } const { sendAsk, sendDirectAsk } = askHook const { data: models, isLoading: modelsLoading } = useModels() const handleAsk = async () => { if (!question.trim()) return // Build context from knowledge graph const graphContext = buildGraphContext() const contextualQuestion = `${graphContext}\n\nQuestion: ${question}` if (!selectedStrategyModel || !selectedAnswerModel || !selectedFinalModel) { // Use first available model as default const defaultModel = models?.[0]?.id await sendAsk(contextualQuestion, { strategy: selectedStrategyModel || defaultModel || 'gpt-4o', answer: selectedAnswerModel || defaultModel || 'gpt-4o', finalAnswer: selectedFinalModel || defaultModel || 'gpt-4o', }) } else { await sendAsk(contextualQuestion, { strategy: selectedStrategyModel, answer: selectedAnswerModel, finalAnswer: selectedFinalModel, }) } } const handleDirectAsk = async () => { if (!question.trim()) return // Build context from knowledge graph const graphContext = buildGraphContext() const contextualQuestion = `${graphContext}\n\nQuestion: ${question}` await sendDirectAsk(contextualQuestion, selectedAnswerModel) } // Build context string from graph statistics const buildGraphContext = () => { const { nodeCount, edgeCount, topConcepts, nodeTypes, relationshipTypes, allNodes } = graphStats let context = `You are analyzing a Knowledge Graph with the following structure:\n\n` context += `## Graph Statistics:\n` context += `- Total Nodes: ${nodeCount}\n` context += `- Total Connections: ${edgeCount}\n\n` // Node types breakdown if (nodeTypes && Object.keys(nodeTypes).length > 0) { context += `## Node Types Distribution:\n` Object.entries(nodeTypes).forEach(([type, count]) => { context += `- ${type}: ${count} nodes\n` }) context += `\n` } // Top concepts with details if (topConcepts && topConcepts.length > 0) { context += `## Top ${Math.min(10, topConcepts.length)} Most Important Concepts:\n` topConcepts.forEach((concept, index) => { context += `${index + 1}. **${concept.label}**` if (concept.type) context += ` [${concept.type}]` context += ` - ${concept.mentions} mentions` if (concept.importance) context += ` (importance: ${Math.round(concept.importance * 100)}%)` context += `\n` }) context += `\n` } // Relationship types if (relationshipTypes && Object.keys(relationshipTypes).length > 0) { context += `## Relationship Types:\n` Object.entries(relationshipTypes) .sort(([, a], [, b]) => b - a) .slice(0, 10) .forEach(([type, count]) => { context += `- ${type}: ${count} connections\n` }) context += `\n` } // All concepts (for comprehensive understanding) if (allNodes && allNodes.length > 0 && allNodes.length <= 50) { context += `## All Concepts in Graph:\n` allNodes.forEach(node => { context += `- ${node.label} [${node.type}]\n` }) context += `\n` } context += `Please answer the following question based on this knowledge graph structure and content.\n` return context } const suggestedQuestions = [ "What are the main concepts in this knowledge graph?", "How are the key ideas connected?", "Summarize the relationships between the top concepts", "What patterns can you identify in the graph?", "Explain the most important nodes and their connections" ] return (
Knowledge Graph Insights {graphStats.nodeCount} nodes

Ask AI questions about your knowledge graph

{/* Quick Stats */}
{graphStats.nodeCount}
Nodes
{graphStats.edgeCount}
Connections
{graphStats.topConcepts?.length || 0}
Top Concepts
{/* Ask AI Section */}
{/* Model Selection */}
{/* Question Input */}