import React, { useState, useRef, useEffect } from 'react'; import { ArrowUp, X, RotateCcw, Sparkles, CornerDownRight, AlertTriangle } from 'lucide-react'; import { api } from '../lib/api'; import { PROVIDER_META, STATUS_TEXT, WARN_STATUSES, FAIL_STATUSES, shortModel, formatLatency } from '../lib/providers'; import EngineChain from './EngineChain'; import Markdown from './Markdown'; const MAX_CHIPS = 8; /** "Groq too large → Gemini 1.2s · flash-lite": where this answer actually came from. */ const RouteLine = ({ engine }) => { const attempts = (engine?.attempts || []).filter((a) => a.status !== 'unconfigured'); if (!attempts.length) return null; const winner = attempts.find((a) => a.status === 'ok'); return (
{attempts.map((a, i) => { const ok = a.status === 'ok'; const color = ok ? PROVIDER_META[a.provider]?.color : WARN_STATUSES.has(a.status) ? '#fb923c' : FAIL_STATUSES.has(a.status) ? '#f87171' : '#64748b'; return ( {i > 0 && →} {a.name} {ok ? formatLatency(a.latency_ms) : STATUS_TEXT[a.status] || a.status} ); })} {winner && · {shortModel(winner.model)}}
); }; const ChatInterface = ({ chain, lastEngine, documentName, suggestions = [], pendingQuestion, onEngine, onHighlightNodes, onFocusNode, onClose }) => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const scrollRef = useRef(null); const inputRef = useRef(null); const handledQuestionRef = useRef(null); // Auto-scroll to the latest message useEffect(() => { scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: 'smooth' }); }, [messages, isLoading]); // Grow the textarea with its content useEffect(() => { const el = inputRef.current; if (!el) return; el.style.height = 'auto'; // scrollHeight is 0 while the panel is hidden (mobile sheet closed); keep the natural height then if (el.scrollHeight) el.style.height = `${Math.min(el.scrollHeight, 160)}px`; }, [input]); const send = async (raw) => { const text = (raw ?? input).trim(); if (!text || isLoading) return; setMessages((prev) => [...prev, { role: 'user', content: text }]); setInput(''); setIsLoading(true); try { const { data } = await api.post('/chat', { message: text }); const highlighted = data.highlighted_nodes || []; setMessages((prev) => [...prev, { role: 'ai', content: data.response, engine: data.engine, highlighted, error: data.error }]); onHighlightNodes?.(highlighted); if (data.engine) onEngine?.(data.engine); } catch (error) { console.error('Chat error:', error); const content = error.response?.data?.detail || (error.message === 'Network Error' ? 'Network error. Please check your connection.' : "I'm having trouble processing that request. It might be too long or complex. Please try shortening it."); setMessages((prev) => [...prev, { role: 'ai', content: String(content), error: true }]); } finally { setIsLoading(false); } }; // Questions sent from elsewhere (e.g. the node inspector's "Ask about this") useEffect(() => { if (!pendingQuestion || handledQuestionRef.current === pendingQuestion.nonce) return; handledQuestionRef.current = pendingQuestion.nonce; send(pendingQuestion.text); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pendingQuestion]); const clearChat = () => { setMessages([]); onHighlightNodes?.([]); }; return (
{/* Header */}

Ask the graph

CogniChat

{messages.length > 0 && ( )}
{/* Messages */}
{messages.length === 0 && (

Ask anything about {documentName ? {documentName} : 'your document'}

Answers come only from the document. The concepts each answer uses light up in green on the graph.

{suggestions.length > 0 && (

Try asking

{suggestions.map((s) => ( ))}
)}
)} {messages.map((msg, idx) => msg.role === 'user' ? (
{msg.content}
) : (
{msg.error && }
{msg.highlighted?.length > 0 && (
Lit up {msg.highlighted.slice(0, MAX_CHIPS).map((id) => ( ))} {msg.highlighted.length > MAX_CHIPS && ( +{msg.highlighted.length - MAX_CHIPS} more )}
)}
) )} {/* Typing Indicator */} {isLoading && (
{[0, 150, 300].map((d) => ( ))} Tracing the graph…
)}
{/* Input Area */}