// Main chat interface with message display and input import React, { useState, useRef, useEffect } from 'react'; import { useConversation } from '@/contexts/ConversationContext'; import { useInference } from '@/contexts/InferenceContext'; import { MessageList } from './MessageList'; import { MessageInput } from './MessageInput'; import { InferenceLogin } from './InferenceLogin'; export function ChatInterface() { const { activeConversationId, getConversation, sendMessage, stopAgentLoop, getAgentLoopState, } = useConversation(); const { currentProvider } = useInference(); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); const activeConversation = activeConversationId ? getConversation(activeConversationId) : undefined; const agentLoopState = activeConversationId ? getAgentLoopState(activeConversationId) : undefined; // Auto-scroll to bottom when new messages arrive useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [activeConversation?.messages]); const handleSendMessage = async (content: string) => { if (!activeConversationId || !currentProvider?.isAuthenticated) { return; } setIsLoading(true); try { await sendMessage(activeConversationId, content); } catch (error) { console.error('Failed to send message:', error); } finally { setIsLoading(false); } }; const handleStopGeneration = () => { if (activeConversationId) { stopAgentLoop(activeConversationId); } }; // Show empty state if no conversation is selected if (!activeConversationId || !activeConversation) { return (

Welcome to MCP Chat

Start a new conversation to begin chatting with AI.

The AI has access to both test tools and any connected MCP servers.

); } // Show auth prompt if not authenticated if (!currentProvider?.isAuthenticated) { return (
); } const isGenerating = agentLoopState?.isRunning || isLoading; return (
{/* Chat Header */}

{activeConversation.title}

Model: {currentProvider.selectedModel?.name || 'None selected'}

{isGenerating && ( )}
{/* Status Indicator */} {agentLoopState && (
{agentLoopState.isRunning && (
{agentLoopState.currentStep === 'inference' && 'Thinking...'} {agentLoopState.currentStep === 'tool_execution' && 'Using tools...'} {agentLoopState.currentStep === 'complete' && 'Complete'} (Step {agentLoopState.iteration + 1}/{agentLoopState.maxIterations})
)}
)}
{/* Messages Area */}
{/* Input Area */}
); }