"use client"; import { useState, useRef, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Markdown } from "@/components/ui/markdown"; import { Send, Loader2, Brain, User, BookMarked, MessageCircle, RotateCcw, Sparkles, } from "lucide-react"; import { MentorAskResponse, KBSearchResult } from "@/types"; import { RAGCitationCard } from "./rag-citation-card"; interface ChatMessage { role: "user" | "mentor"; content: string; sources?: KBSearchResult[]; followups?: string[]; agentRole?: string; agentTeam?: string; journalEntriesUsed?: number; } interface MentorChatProps { onAsk: (question: string, conversationId?: string) => Promise; isLoading: boolean; } export function MentorChat({ onAsk, isLoading }: MentorChatProps) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [conversationId, setConversationId] = useState(null); const [expandedSources, setExpandedSources] = useState(null); const bottomRef = useRef(null); const inputRef = useRef(null); // Auto-scroll to the newest message using a sentinel div + scrollIntoView useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages, isLoading]); const handleSubmit = async (question?: string) => { const q = question || input.trim(); if (!q || isLoading) return; // Add user message setMessages((prev) => [...prev, { role: "user", content: q }]); setInput(""); try { const response = await onAsk(q, conversationId ?? undefined); // Save conversation ID for follow-ups setConversationId(response.conversation_id); // Add mentor response setMessages((prev) => [ ...prev, { role: "mentor", content: response.answer, sources: response.sources, followups: response.suggested_followups, agentRole: response.agent_role, agentTeam: response.agent_team, journalEntriesUsed: response.journal_entries_used, }, ]); } catch { // Error handled by parent } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleSubmit(); } }; const handleNewChat = () => { setMessages([]); setConversationId(null); setExpandedSources(null); inputRef.current?.focus(); }; const handleFollowUp = (question: string) => { handleSubmit(question); }; // Empty state if (messages.length === 0 && !isLoading) { return (
{/* Empty state */}

AI Mentor

Your personalized mentor that knows your role, searches your journals, and provides tailored guidance. Start a conversation below.

Role-aware Personal context Multi-turn chat
{/* Input at bottom */}