Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Sync upstream: merge HKUDS/DeepTutor main (up to v1.3.10)

unknown3dd577106684
diff --cc deeptutor/api/main.py
index e2908a1ac,ab87722f9..c5f96ce3b
--- a/deeptutor/api/main.py
+++ b/deeptutor/api/main.py
@@@ -220,32 -287,78 +288,84 @@@ from deeptutor.api.routers import
unified_ws,
vision_solver,
)
+ from deeptutor.multi_user.router import router as multi_user_router # noqa: E402
+
+ # Auth router is public — login/logout/register/status require no token
+ app.include_router(auth.router, prefix="/api/v1/auth", tags=["auth"])
+
+ # All other routers require a valid session when AUTH_ENABLED=true.
+ # require_auth is a no-op when AUTH_ENABLED=false, so this is safe for local use.
+ from deeptutor.api.routers.auth import require_auth # noqa: E402
+
+ _auth = [Depends(require_auth)]
+
+ app.include_router(
+ multi_user_router,
+ prefix="/api/v1/multi-user",
+ tags=["multi-user"],
+ dependencies=_auth,
+ )
- # Include routers
- app.include_router(solve.router, prefix="/api/v1", tags=["solve"])
- app.include_router(chat.router, prefix="/api/v1", tags=["chat"])
- app.include_router(question.router, prefix="/api/v1/question", tags=["question"])
- app.include_router(knowledge.router, prefix="/api/v1/knowledge", tags=["knowledge"])
- app.include_router(dashboard.router, prefix="/api/v1/dashboard", tags=["dashboard"])
- app.include_router(guided_learning.router, prefix="/api/v1/learning", tags=["guided-learning"])
- app.include_router(co_writer.router, prefix="/api/v1/co_writer", tags=["co_writer"])
- app.include_router(notebook.router, prefix="/api/v1/notebook", tags=["notebook"])
- app.include_router(book.router, prefix="/api/v1/book", tags=["book"])
- app.include_router(memory.router, prefix="/api/v1/memory", tags=["memory"])
- app.include_router(sessions.router, prefix="/api/v1/sessions", tags=["sessions"])
+ app.include_router(solve.router, prefix="/api/v1", tags=["solve"], dependencies=_auth)
+ app.include_router(chat.router, prefix="/api/v1", tags=["chat"], dependencies=_auth)
+ app.include_router(
+ question.router, prefix="/api/v1/question", tags=["question"], dependencies=_auth
+ )
+ app.include_router(
+ knowledge.router, prefix="/api/v1/knowledge", tags=["knowledge"], dependencies=_auth
+ )
+ app.include_router(
+ dashboard.router, prefix="/api/v1/dashboard", tags=["dashboard"], dependencies=_auth
+ )
++app.include_router(
++ guided_learning.router,
++ prefix="/api/v1/learning",
++ tags=["guided-learning"],
++ dependencies=_auth,
++)
app.include_router(
- question_notebook.router, prefix="/api/v1/question-notebook", tags=["question-notebook"]
+ co_writer.router, prefix="/api/v1/co_writer", tags=["co_writer"], dependencies=_auth
)
- app.include_router(settings.router, prefix="/api/v1/settings", tags=["settings"])
- app.include_router(skills.router, prefix="/api/v1/skills", tags=["skills"])
- app.include_router(system.router, prefix="/api/v1/system", tags=["system"])
- app.include_router(plugins_api.router, prefix="/api/v1/plugins", tags=["plugins"])
- app.include_router(agent_config.router, prefix="/api/v1/agent-config", tags=["agent-config"])
- app.include_router(vision_solver.router, prefix="/api/v1", tags=["vision-solver"])
- app.include_router(tutorbot.router, prefix="/api/v1/tutorbot", tags=["tutorbot"])
- app.include_router(attachments.router, prefix="/api/attachments", tags=["attachments"])
-
- # Unified WebSocket endpoint
+ app.include_router(
+ notebook.router, prefix="/api/v1/notebook", tags=["notebook"], dependencies=_auth
+ )
+ app.include_router(book.router, prefix="/api/v1/book", tags=["book"], dependencies=_auth)
+ app.include_router(memory.router, prefix="/api/v1/memory", tags=["memory"], dependencies=_auth)
+ app.include_router(
+ sessions.router, prefix="/api/v1/sessions", tags=["sessions"], dependencies=_auth
+ )
+ app.include_router(
+ question_notebook.router,
+ prefix="/api/v1/question-notebook",
+ tags=["question-notebook"],
+ dependencies=_auth,
+ )
+ app.include_router(
+ settings.router, prefix="/api/v1/settings", tags=["settings"], dependencies=_auth
+ )
+ app.include_router(skills.router, prefix="/api/v1/skills", tags=["skills"], dependencies=_auth)
+ app.include_router(system.router, prefix="/api/v1/system", tags=["system"], dependencies=_auth)
+ app.include_router(
+ plugins_api.router, prefix="/api/v1/plugins", tags=["plugins"], dependencies=_auth
+ )
+ app.include_router(
+ agent_config.router, prefix="/api/v1/agent-config", tags=["agent-config"], dependencies=_auth
+ )
+ app.include_router(
+ vision_solver.router, prefix="/api/v1", tags=["vision-solver"], dependencies=_auth
+ )
+ app.include_router(
+ tutorbot.router, prefix="/api/v1/tutorbot", tags=["tutorbot"], dependencies=_auth
+ )
+ app.include_router(
+ attachments.router,
+ prefix="/api/attachments",
+ tags=["attachments"],
+ dependencies=_auth,
+ )
+
+ # Unified WebSocket endpoint — auth is checked inside the handler (WebSockets
+ # cannot use FastAPI dependencies in the standard way)
app.include_router(unified_ws.router, prefix="/api/v1", tags=["unified-ws"])
diff --cc web/app/(workspace)/chat/[[...sessionId]]/page.tsx
index ebf0fb0f9,11ac875ad..a933ebd74
--- a/web/app/(workspace)/chat/[[...sessionId]]/page.tsx
+++ b/web/app/(workspace)/chat/[[...sessionId]]/page.tsx
@@@ -1,1394 -1,1426 +1,1426 @@@
-"use client";
-
-import dynamic from "next/dynamic";
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
-import { useParams, useRouter } from "next/navigation";
-
-import {
- BarChart3,
- BrainCircuit,
- Clapperboard,
- Code2,
- Database,
- FileSearch,
- Globe,
- Lightbulb,
- MessageSquare,
- Microscope,
- PenLine,
- Sparkles,
- type LucideIcon,
-} from "lucide-react";
-import { useTranslation } from "react-i18next";
-import type { SelectedRecord } from "@/lib/notebook-selection-types";
-import type { SelectedHistorySession } from "@/components/chat/HistorySessionPicker";
-import type { SelectedQuestionEntry } from "@/components/chat/QuestionBankPicker";
-import ChatComposer from "@/components/chat/home/ChatComposer";
-import { ChatMessageList } from "@/components/chat/home/ChatMessages";
-// Imported eagerly so the drawer shell is always mounted off-screen —
-// clicking a chip becomes a single CSS class flip, no chunk fetch + double
-// render. The heavy renderers inside still load lazily.
-import FilePreviewDrawer from "@/components/chat/preview/FilePreviewDrawer";
-import {
- useUnifiedChat,
- type MessageAttachment,
- type MessageRequestSnapshot,
-} from "@/context/UnifiedChatContext";
-import { useAppShell } from "@/context/AppShellContext";
-import type { FilePreviewSource } from "@/components/chat/preview/previewerFor";
-import type { LLMSelection, StreamEvent } from "@/lib/unified-ws";
-import {
- extractBase64FromDataUrl,
- readFileAsDataUrl,
-} from "@/lib/file-attachments";
-import {
- classifyFile,
- isSvgFilename,
- MAX_ATTACHMENT_BYTES,
- MAX_TOTAL_ATTACHMENT_BYTES,
-} from "@/lib/doc-attachments";
-import { useChatAutoScroll } from "@/hooks/useChatAutoScroll";
-import { useMeasuredHeight } from "@/hooks/useMeasuredHeight";
-import {
- loadCapabilityPlaygroundConfigs,
- resolveCapabilityPlaygroundConfig,
- type CapabilityPlaygroundConfigMap,
-} from "@/lib/playground-config";
-import {
- DEFAULT_QUIZ_CONFIG,
- buildQuizWSConfig,
- type DeepQuestionFormConfig,
-} from "@/lib/quiz-types";
-import {
- DEFAULT_MATH_ANIMATOR_CONFIG,
- buildMathAnimatorWSConfig,
- type MathAnimatorFormConfig,
-} from "@/lib/math-animator-types";
-import {
- DEFAULT_VISUALIZE_CONFIG,
- buildVisualizeWSConfig,
- type VisualizeFormConfig,
-} from "@/lib/visualize-types";
-import {
- buildResearchWSConfig,
- createEmptyResearchConfig,
- validateResearchConfig,
- type DeepResearchFormConfig,
- type OutlineItem,
- type ResearchSource,
-} from "@/lib/research-types";
-import { listKnowledgeBases } from "@/lib/knowledge-api";
-import { listLLMOptions, type LLMOption } from "@/lib/llm-options";
-import { downloadChatMarkdown } from "@/lib/chat-export";
-import type { SpaceMemoryFile } from "@/lib/space-items";
-import {
- selectedBooksToPayload,
- type SelectedBookReference,
-} from "@/lib/book-references";
-
-const NotebookRecordPicker = dynamic(
- () => import("@/components/notebook/NotebookRecordPicker"),
- {
- ssr: false,
- },
-);
-const HistorySessionPicker = dynamic(
- () => import("@/components/chat/HistorySessionPicker"),
- {
- ssr: false,
- },
-);
-const QuestionBankPicker = dynamic(
- () => import("@/components/chat/QuestionBankPicker"),
- {
- ssr: false,
- },
-);
-const SkillsPicker = dynamic(() => import("@/components/chat/SkillsPicker"), {
- ssr: false,
-});
-const MemoryPicker = dynamic(() => import("@/components/chat/MemoryPicker"), {
- ssr: false,
-});
-const BookReferencePicker = dynamic(
- () => import("@/components/chat/BookReferencePicker"),
- {
- ssr: false,
- },
-);
-const SaveToNotebookModal = dynamic(
- () => import("@/components/notebook/SaveToNotebookModal"),
- {
- ssr: false,
- },
-);
-
-/* ------------------------------------------------------------------ */
-/* Type & data definitions */
-/* ------------------------------------------------------------------ */
-
-type ToolName =
- | "brainstorm"
- | "rag"
- | "web_search"
- | "code_execution"
- | "reason"
- | "paper_search";
-
-interface ToolDef {
- name: ToolName;
- label: string;
- icon: LucideIcon;
-}
-
-interface ResearchSourceDef {
- name: ResearchSource;
- label: string;
- icon: LucideIcon;
-}
-
-const ALL_TOOLS: ToolDef[] = [
- { name: "brainstorm", label: "Brainstorm", icon: Lightbulb },
- { name: "rag", label: "RAG", icon: Database },
- { name: "web_search", label: "Web Search", icon: Globe },
- { name: "code_execution", label: "Code", icon: Code2 },
- { name: "reason", label: "Reason", icon: Sparkles },
- { name: "paper_search", label: "Arxiv Search", icon: FileSearch },
-];
-
-const RESEARCH_SOURCES: ResearchSourceDef[] = [
- { name: "kb", label: "Knowledge Base", icon: Database },
- { name: "web", label: "Web", icon: Globe },
- { name: "papers", label: "Papers", icon: FileSearch },
-];
-
-interface CapabilityDef {
- value: string;
- label: string;
- description: string;
- icon: LucideIcon;
- allowedTools: ToolName[];
- defaultTools: ToolName[];
-}
-
-const CAPABILITIES: CapabilityDef[] = [
- {
- value: "",
- label: "Chat",
- description: "Flexible conversation with any tool",
- icon: MessageSquare,
- allowedTools: [
- "brainstorm",
- "rag",
- "web_search",
- "code_execution",
- "reason",
- "paper_search",
- ],
- defaultTools: [],
- },
- {
- value: "deep_solve",
- label: "Deep Solve",
- description: "Multi-step reasoning & problem solving",
- icon: BrainCircuit,
- allowedTools: ["rag", "web_search", "code_execution", "reason"],
- defaultTools: ["rag", "web_search", "code_execution", "reason"],
- },
- {
- value: "deep_question",
- label: "Quiz Generation",
- description: "Auto-validated question generation",
- icon: PenLine,
- allowedTools: ["rag", "web_search", "code_execution"],
- defaultTools: ["rag", "web_search", "code_execution"],
- },
- {
- value: "deep_research",
- label: "Deep Research",
- description: "Comprehensive multi-agent research",
- icon: Microscope,
- allowedTools: [],
- defaultTools: [],
- },
- {
- value: "math_animator",
- label: "Math Animator",
- description: "Generate math videos or storyboard images",
- icon: Clapperboard,
- allowedTools: [],
- defaultTools: [],
- },
- {
- value: "visualize",
- label: "Visualize",
- description: "Generate SVG, Chart.js, or Mermaid visualizations",
- icon: BarChart3,
- allowedTools: [],
- defaultTools: [],
- },
-];
-
-interface KnowledgeBase {
- name: string;
- is_default?: boolean;
-}
-
-interface PendingAttachment {
- type: string;
- filename: string;
- base64?: string;
- previewUrl?: string;
- size?: number;
- mimeType?: string;
-}
-
-/* ------------------------------------------------------------------ */
-/* Helpers */
-/* ------------------------------------------------------------------ */
-
-function getCapability(value: string | null): CapabilityDef {
- return CAPABILITIES.find((c) => c.value === (value || "")) ?? CAPABILITIES[0];
-}
-
-/* ------------------------------------------------------------------ */
-/* Chat page */
-/* ------------------------------------------------------------------ */
-
-export default function ChatPage() {
- const params = useParams<{ sessionId?: string[] }>();
- const router = useRouter();
- const { t } = useTranslation();
- const sessionIdParam = params.sessionId?.[0] ?? null;
- const { setActiveSessionId, language: appLanguage } = useAppShell();
-
- const {
- state,
- setTools,
- setCapability,
- setKBs,
- setLLMSelection,
- sendMessage,
- cancelStreamingTurn,
- regenerateLastMessage,
- newSession,
- loadSession,
- } = useUnifiedChat();
-
- const [knowledgeBases, setKnowledgeBases] = useState<KnowledgeBase[]>([]);
- const [llmOptions, setLLMOptions] = useState<LLMOption[]>([]);
- const [activeLLMDefault, setActiveLLMDefault] = useState<LLMSelection | null>(
- null,
- );
- const [llmOptionsLoading, setLLMOptionsLoading] = useState(true);
- const [llmOptionsError, setLLMOptionsError] = useState(false);
- const [capabilityConfigs, setCapabilityConfigs] =
- useState<CapabilityPlaygroundConfigMap>({});
- const [attachments, setAttachments] = useState<PendingAttachment[]>([]);
- const [dragging, setDragging] = useState(false);
- const [attachmentError, setAttachmentError] = useState<string | null>(null);
- const [previewSource, setPreviewSource] = useState<FilePreviewSource | null>(
- null,
- );
- const attachmentErrorTimer = useRef<ReturnType<typeof setTimeout> | null>(
- null,
- );
- const [capMenuOpen, setCapMenuOpen] = useState(false);
- const [quizConfig, setQuizConfig] = useState<DeepQuestionFormConfig>({
- ...DEFAULT_QUIZ_CONFIG,
- });
- const [quizPdf, setQuizPdf] = useState<File | null>(null);
- const [mathAnimatorConfig, setMathAnimatorConfig] =
- useState<MathAnimatorFormConfig>({
- ...DEFAULT_MATH_ANIMATOR_CONFIG,
- });
- const [visualizeConfig, setVisualizeConfig] = useState<VisualizeFormConfig>({
- ...DEFAULT_VISUALIZE_CONFIG,
- });
- const [researchConfig, setResearchConfig] = useState<DeepResearchFormConfig>(
- createEmptyResearchConfig(),
- );
- // Unified collapse state for the capability-specific config panel
- // (Quiz / Math Animator / Visualize / Deep Research). Default collapsed so
- // a fresh Chat / Deep Solve session has the shortest possible composer.
- const [panelCollapsed, setPanelCollapsed] = useState(true);
- const [showSaveModal, setShowSaveModal] = useState(false);
- const [showNotebookPicker, setShowNotebookPicker] = useState(false);
- const [showBookPicker, setShowBookPicker] = useState(false);
- const [showHistoryPicker, setShowHistoryPicker] = useState(false);
- const [showQuestionBankPicker, setShowQuestionBankPicker] = useState(false);
- const [showSkillsPicker, setShowSkillsPicker] = useState(false);
- const [showMemoryPicker, setShowMemoryPicker] = useState(false);
- const [toolMenuOpen, setToolMenuOpen] = useState(false);
- const [spaceMenuOpen, setSpaceMenuOpen] = useState(false);
- const [selectedNotebookRecords, setSelectedNotebookRecords] = useState<
- SelectedRecord[]
- >([]);
- const [selectedBookReferences, setSelectedBookReferences] = useState<
- SelectedBookReference[]
- >([]);
- const [selectedHistorySessions, setSelectedHistorySessions] = useState<
- SelectedHistorySession[]
- >([]);
- const [selectedQuestionEntries, setSelectedQuestionEntries] = useState<
- SelectedQuestionEntry[]
- >([]);
- const [selectedSkills, setSelectedSkills] = useState<string[]>([]);
- const [skillsAutoMode, setSkillsAutoMode] = useState(false);
- const [selectedMemoryFiles, setSelectedMemoryFiles] = useState<
- SpaceMemoryFile[]
- >([]);
- const dragCounter = useRef(0);
- const capMenuRef = useRef<HTMLDivElement>(null);
- const capBtnRef = useRef<HTMLButtonElement>(null);
- const toolMenuRef = useRef<HTMLDivElement>(null);
- const toolBtnRef = useRef<HTMLButtonElement>(null);
- const spaceMenuRef = useRef<HTMLDivElement>(null);
- const spaceBtnRef = useRef<HTMLButtonElement>(null);
- const initialLoadRef = useRef(false);
-
- const activeCap = useMemo(
- () => getCapability(state.activeCapability),
- [state.activeCapability],
- );
- const isQuizMode = activeCap.value === "deep_question";
- const isMathAnimatorMode = activeCap.value === "math_animator";
- const isVisualizeMode = activeCap.value === "visualize";
- const isResearchMode = activeCap.value === "deep_research";
- const selectedTools = useMemo(
- () => new Set(state.enabledTools),
- [state.enabledTools],
- );
- const ragActive = isResearchMode
- ? researchConfig.sources.includes("kb")
- : selectedTools.has("rag");
- const hasMessages = state.messages.length > 0;
- const { ref: composerRef, height: composerHeight } =
- useMeasuredHeight<HTMLDivElement>();
- const visibleTools = useMemo(
- () => ALL_TOOLS.filter((t) => activeCap.allowedTools.includes(t.name)),
- [activeCap.allowedTools],
- );
- const researchValidation = useMemo(
- () => validateResearchConfig(researchConfig),
- [researchConfig],
- );
- const notebookReferenceGroups = useMemo(() => {
- const groups = new Map<string, { notebookName: string; count: number }>();
- selectedNotebookRecords.forEach((record) => {
- const existing = groups.get(record.notebookId);
- if (existing) {
- existing.count += 1;
- } else {
- groups.set(record.notebookId, {
- notebookName: record.notebookName,
- count: 1,
- });
- }
- });
- return Array.from(groups.entries()).map(([notebookId, value]) => ({
- notebookId,
- ...value,
- }));
- }, [selectedNotebookRecords]);
- const notebookReferencesPayload = useMemo(() => {
- const grouped = new Map<string, string[]>();
- selectedNotebookRecords.forEach((record) => {
- const current = grouped.get(record.notebookId) || [];
- current.push(record.id);
- grouped.set(record.notebookId, current);
- });
- return Array.from(grouped.entries()).map(([notebook_id, record_ids]) => ({
- notebook_id,
- record_ids,
- }));
- }, [selectedNotebookRecords]);
- const bookReferencesPayload = useMemo(
- () => selectedBooksToPayload(selectedBookReferences),
- [selectedBookReferences],
- );
- const historyReferencesPayload = useMemo(
- () => selectedHistorySessions.map((session) => session.sessionId),
- [selectedHistorySessions],
- );
- const questionNotebookReferencesPayload = useMemo(
- () => selectedQuestionEntries.map((entry) => entry.id),
- [selectedQuestionEntries],
- );
- const memoryReferencesPayload = useMemo(
- () => [...selectedMemoryFiles],
- [selectedMemoryFiles],
- );
- const chatSaveMessages = useMemo(
- () =>
- state.messages.map((msg) => ({
- role: msg.role,
- content: msg.content,
- capability: msg.capability,
- })),
- [state.messages],
- );
- const chatSavePayload = useMemo(() => {
- if (!state.messages.length) return null;
- const title =
- state.messages
- .find((msg) => msg.role === "user")
- ?.content.trim()
- .slice(0, 80) || "Chat Session";
- return {
- recordType: "chat" as const,
- title,
- // The actual transcript / userQuery are rebuilt inside SaveToNotebookModal
- // from the user's selected subset of messages. We still provide a
- // sensible fallback for non-selection callers.
- userQuery: "",
- output: "",
- metadata: {
- source: "chat",
- capability: state.activeCapability || "chat",
- ui_language: state.language,
- session_id: state.sessionId,
- total_message_count: state.messages.length,
- },
- };
- }, [state.activeCapability, state.language, state.messages, state.sessionId]);
- const lastMessage = state.messages[state.messages.length - 1];
- const {
- containerRef: messagesContainerRef,
- endRef: messagesEndRef,
- shouldAutoScrollRef,
- handleScroll: handleMessagesScroll,
- } = useChatAutoScroll({
- hasMessages,
- isStreaming: state.isStreaming,
- composerHeight,
- messageCount: state.messages.length,
- lastMessageContent: lastMessage?.content,
- lastEventCount: lastMessage?.events?.length,
- });
- const copyAssistantMessage = useCallback(async (content: string) => {
- if (!content.trim()) return;
- try {
- await navigator.clipboard.writeText(content);
- } catch (error) {
- console.error("Failed to copy assistant message:", error);
- }
- }, []);
- const handleAnswerNow = useCallback(
- (
- snapshot?: MessageRequestSnapshot,
- assistantMsg?: { content: string; events?: StreamEvent[] },
- ) => {
- if (!snapshot || !state.isStreaming) return;
- const answerNowEvents = (assistantMsg?.events ?? []).map((event) => ({
- type: event.type,
- stage: event.stage,
- content: event.content,
- metadata: event.metadata ?? {},
- }));
- cancelStreamingTurn();
- // Preserve the original capability — each capability now owns its
- // own answer-now fast-path (deep_solve jumps to writing,
- // deep_question to direct quiz synthesis, math_animator to
- // code-gen + render, etc.). The backend orchestrator only falls
- // back to ``chat`` if the requested capability is missing.
- const answerNowSnapshot: MessageRequestSnapshot = {
- ...snapshot,
- language: appLanguage,
- config: {
- ...(snapshot.config || {}),
- answer_now_context: {
- original_user_message: snapshot.content,
- partial_response: assistantMsg?.content || "",
- events: answerNowEvents,
- },
- },
- };
- window.setTimeout(() => {
- sendMessage(
- answerNowSnapshot.content,
- answerNowSnapshot.attachments,
- answerNowSnapshot.config,
- answerNowSnapshot.notebookReferences,
- answerNowSnapshot.historyReferences,
- {
- displayUserMessage: false,
- persistUserMessage: false,
- requestSnapshotOverride: answerNowSnapshot,
- bookReferences: answerNowSnapshot.bookReferences,
- },
- answerNowSnapshot.questionNotebookReferences,
- answerNowSnapshot.skills,
- answerNowSnapshot.memoryReferences,
- );
- shouldAutoScrollRef.current = true;
- }, 0);
- },
- [
- appLanguage,
- cancelStreamingTurn,
- sendMessage,
- shouldAutoScrollRef,
- state.isStreaming,
- ],
- );
-
- /* ---- URL-driven session loading ---- */
- useEffect(() => {
- if (initialLoadRef.current) return;
- initialLoadRef.current = true;
- if (sessionIdParam) {
- void loadSession(sessionIdParam).catch(() => {
- router.replace("/chat", { scroll: false });
- });
- } else {
- newSession();
- }
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
-
- // When URL param changes (sidebar navigation), load the corresponding session
- const prevSessionIdParam = useRef(sessionIdParam);
- useEffect(() => {
- if (sessionIdParam === prevSessionIdParam.current) return;
- prevSessionIdParam.current = sessionIdParam;
- if (sessionIdParam) {
- if (sessionIdParam === state.sessionId) return;
- void loadSession(sessionIdParam).catch(() => {
- router.replace("/chat", { scroll: false });
- });
- } else {
- newSession();
- }
- }, [sessionIdParam, loadSession, newSession, router, state.sessionId]);
-
- // When a new session_id is assigned by the server, update the URL
- useEffect(() => {
- if (state.sessionId && !sessionIdParam) {
- router.replace(`/chat/${state.sessionId}`, { scroll: false });
- }
- }, [state.sessionId, sessionIdParam, router]);
-
- useEffect(() => {
- setActiveSessionId(state.sessionId || sessionIdParam || null);
- }, [state.sessionId, sessionIdParam, setActiveSessionId]);
-
- const refreshKnowledgeBases = useCallback(
- async (options?: { force?: boolean }) => {
- try {
- const list = await listKnowledgeBases({ force: options?.force });
- setKnowledgeBases(list);
- if (!state.knowledgeBases.length && list.length) {
- const def = list.find((k: KnowledgeBase) => k.is_default);
- setKBs([def?.name || list[0].name]);
- }
- } catch {
- setKnowledgeBases([]);
- }
- },
- [setKBs, state.knowledgeBases.length],
- );
-
- /* Load KBs */
- useEffect(() => {
- void refreshKnowledgeBases({ force: true });
- }, [refreshKnowledgeBases]);
-
- const refreshLLMOptions = useCallback(async () => {
- setLLMOptionsLoading(true);
- try {
- const payload = await listLLMOptions();
- setLLMOptions(payload.options);
- setActiveLLMDefault(payload.active);
- setLLMOptionsError(false);
- } catch {
- setLLMOptionsError(true);
- setLLMOptions([]);
- setActiveLLMDefault(null);
- } finally {
- setLLMOptionsLoading(false);
- }
- }, []);
-
- useEffect(() => {
- void refreshLLMOptions();
- }, [refreshLLMOptions]);
-
- useEffect(() => {
- if (state.llmSelection || !activeLLMDefault) return;
- setLLMSelection(activeLLMDefault);
- }, [activeLLMDefault, setLLMSelection, state.llmSelection]);
-
- useEffect(() => {
- if (typeof window === "undefined") return;
- const refresh = () => {
- void refreshKnowledgeBases({ force: true });
- void refreshLLMOptions();
- };
- const refreshWhenVisible = () => {
- if (document.visibilityState === "visible") refresh();
- };
- window.addEventListener("focus", refresh);
- window.addEventListener("pageshow", refresh);
- document.addEventListener("visibilitychange", refreshWhenVisible);
- return () => {
- window.removeEventListener("focus", refresh);
- window.removeEventListener("pageshow", refresh);
- document.removeEventListener("visibilitychange", refreshWhenVisible);
- };
- }, [refreshKnowledgeBases, refreshLLMOptions]);
-
- useEffect(() => {
- setCapabilityConfigs(loadCapabilityPlaygroundConfigs());
- }, []);
-
- /* URL query params (capability, tool) */
- useEffect(() => {
- if (typeof window === "undefined") return;
- const p = new URLSearchParams(window.location.search);
- const qc = p.get("capability");
- const qt = p.getAll("tool");
- if (qc !== null) handleSelectCapability(qc || "");
- else if (qt.length) {
- const valid = qt.filter((t): t is ToolName =>
- ALL_TOOLS.some((d) => d.name === t),
- );
- if (valid.length) setTools(Array.from(new Set(valid)));
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
-
- useEffect(() => {
- const handler = (e: MouseEvent) => {
- const t = e.target as Node;
- if (
- capMenuRef.current &&
- !capMenuRef.current.contains(t) &&
- capBtnRef.current &&
- !capBtnRef.current.contains(t)
- )
- setCapMenuOpen(false);
- if (
- toolMenuRef.current &&
- !toolMenuRef.current.contains(t) &&
- toolBtnRef.current &&
- !toolBtnRef.current.contains(t)
- )
- setToolMenuOpen(false);
- if (
- spaceMenuRef.current &&
- !spaceMenuRef.current.contains(t) &&
- spaceBtnRef.current &&
- !spaceBtnRef.current.contains(t)
- )
- setSpaceMenuOpen(false);
- };
- document.addEventListener("mousedown", handler);
- return () => document.removeEventListener("mousedown", handler);
- }, []);
-
- useEffect(() => {
- const allowed = new Set(visibleTools.map((tool) => tool.name));
- const nextTools = state.enabledTools.filter((tool) =>
- allowed.has(tool as ToolName),
- );
- if (nextTools.length !== state.enabledTools.length) setTools(nextTools);
- }, [setTools, state.enabledTools, visibleTools]);
-
- /* ---- handlers ---- */
-
- const handleSelectCapability = useCallback(
- (value: string) => {
- const cap =
- CAPABILITIES.find((c) => c.value === value) ?? CAPABILITIES[0];
- const storageKey = cap.value || "chat";
- const config = resolveCapabilityPlaygroundConfig(
- capabilityConfigs,
- storageKey,
- cap.allowedTools,
- );
- setCapability(cap.value || null);
- setTools(
- config.enabledTools.length > 0 || capabilityConfigs[storageKey]
- ? [...config.enabledTools]
- : [...cap.defaultTools],
- );
- if (config.enabledTools.includes("rag") && config.knowledgeBase)
- setKBs([config.knowledgeBase]);
- // Default-expand the per-capability settings panel right after a
- // capability switch so users immediately see the form. Sending a
- // message later will auto-collapse it (see handleSend).
- setPanelCollapsed(false);
- setCapMenuOpen(false);
- },
- [capabilityConfigs, setCapability, setKBs, setTools],
- );
-
- const toggleTool = useCallback(
- (tool: string) => {
- if (!activeCap.allowedTools.includes(tool as ToolName)) return;
- if (selectedTools.has(tool)) {
- setTools(state.enabledTools.filter((t) => t !== tool));
- } else {
- setTools([...state.enabledTools, tool]);
- }
- },
- [activeCap.allowedTools, selectedTools, setTools, state.enabledTools],
- );
-
- const toggleResearchSource = useCallback((source: ResearchSource) => {
- setResearchConfig((current) => ({
- ...current,
- sources: current.sources.includes(source)
- ? current.sources.filter((item) => item !== source)
- : [...current.sources, source],
- }));
- }, []);
-
- const fileToAttachment = useCallback(
- (f: File): Promise<PendingAttachment> =>
- new Promise((resolve, reject) => {
- readFileAsDataUrl(f)
- .then((raw) => {
- // SVG: treat as file (text extraction on server, vision models
- // reject SVG) but keep the data URL so the chip can render a
- // thumbnail via a raw <img> tag.
- const svg = isSvgFilename(f.name) || f.type === "image/svg+xml";
- const isImage = !svg && f.type.startsWith("image/");
- const b64 = extractBase64FromDataUrl(raw);
- resolve({
- type: isImage ? "image" : "file",
- filename: f.name,
- base64: b64,
- previewUrl: isImage || svg ? raw : undefined,
- size: f.size,
- mimeType: f.type || undefined,
- });
- })
- .catch(reject);
- }),
- [],
- );
-
- const showAttachmentError = useCallback((message: string) => {
- setAttachmentError(message);
- if (attachmentErrorTimer.current) {
- clearTimeout(attachmentErrorTimer.current);
- }
- attachmentErrorTimer.current = setTimeout(() => {
- setAttachmentError(null);
- attachmentErrorTimer.current = null;
- }, 4000);
- }, []);
-
- const filterAndReportFiles = useCallback(
- (files: File[]): File[] => {
- let runningTotal = attachments.reduce((s, a) => s + (a.size ?? 0), 0);
- const accepted: File[] = [];
- const rejected: {
- name: string;
- reason: "unsupported" | "too_large" | "quota";
- }[] = [];
- for (const f of files) {
- const kind = classifyFile(f);
- if (!kind) {
- rejected.push({ name: f.name, reason: "unsupported" });
- continue;
- }
- if (f.size > MAX_ATTACHMENT_BYTES) {
- rejected.push({ name: f.name, reason: "too_large" });
- continue;
- }
- if (runningTotal + f.size > MAX_TOTAL_ATTACHMENT_BYTES) {
- rejected.push({ name: f.name, reason: "quota" });
- break;
- }
- runningTotal += f.size;
- accepted.push(f);
- }
- if (rejected.length) {
- const first = rejected[0];
- let msg: string;
- if (first.reason === "too_large") {
- msg = t("File too large: {{name}}", { name: first.name });
- } else if (first.reason === "quota") {
- msg = t("Too many files, skipped some");
- } else {
- msg = t("Unsupported file type: {{name}}", { name: first.name });
- }
- showAttachmentError(msg);
- }
- return accepted;
- },
- [attachments, showAttachmentError, t],
- );
-
- const handlePaste = useCallback(
- async (event: React.ClipboardEvent) => {
- const items = Array.from(event.clipboardData.items);
- const files = items
- .filter((item) => item.kind === "file")
- .map((item) => item.getAsFile())
- .filter((f): f is File => f !== null);
- const accepted = filterAndReportFiles(files);
- if (!accepted.length) return;
- event.preventDefault();
- const next = await Promise.all(accepted.map(fileToAttachment));
- setAttachments((prev) => [...prev, ...next]);
- },
- [fileToAttachment, filterAndReportFiles],
- );
-
- const removeAttachment = useCallback((index: number) => {
- setAttachments((prev) => prev.filter((_, i) => i !== index));
- }, []);
-
- const handlePreviewPendingAttachment = useCallback(
- (index: number) => {
- const a = attachments[index];
- if (!a) return;
- setPreviewSource({
- filename: a.filename,
- mimeType: a.mimeType,
- type: a.type,
- base64: a.base64,
- size: a.size,
- });
- },
- [attachments],
- );
-
- const handlePreviewMessageAttachment = useCallback((a: MessageAttachment) => {
- setPreviewSource({
- filename: a.filename || "",
- mimeType: a.mime_type,
- type: a.type,
- url: a.url,
- base64: a.base64,
- extractedText: a.extracted_text,
- id: a.id,
- });
- }, []);
-
- const handleClosePreview = useCallback(() => {
- setPreviewSource(null);
- }, []);
-
- const handleDragEnter = useCallback((e: React.DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
- dragCounter.current += 1;
- if (e.dataTransfer.types.includes("Files")) setDragging(true);
- }, []);
-
- const handleDragLeave = useCallback((e: React.DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
- dragCounter.current -= 1;
- if (dragCounter.current === 0) setDragging(false);
- }, []);
-
- const handleDragOver = useCallback((e: React.DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
- }, []);
-
- const handleDrop = useCallback(
- async (e: React.DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
- setDragging(false);
- dragCounter.current = 0;
- const accepted = filterAndReportFiles(Array.from(e.dataTransfer.files));
- if (!accepted.length) return;
- const next = await Promise.all(accepted.map(fileToAttachment));
- setAttachments((prev) => [...prev, ...next]);
- },
- [fileToAttachment, filterAndReportFiles],
- );
-
- const handleAddFiles = useCallback(
- async (files: File[]) => {
- const accepted = filterAndReportFiles(files);
- if (!accepted.length) return;
- const next = await Promise.all(accepted.map(fileToAttachment));
- setAttachments((prev) => [...prev, ...next]);
- },
- [fileToAttachment, filterAndReportFiles],
- );
-
- const handleSend = useCallback(
- async (content: string) => {
- if (
- (!content &&
- !attachments.length &&
- !selectedBookReferences.length &&
- !selectedNotebookRecords.length &&
- !selectedHistorySessions.length &&
- !selectedQuestionEntries.length &&
- !selectedSkills.length &&
- !skillsAutoMode &&
- !selectedMemoryFiles.length) ||
- state.isStreaming
- )
- return;
-
- let extraAttachments = attachments.map((a) => ({
- type: a.type,
- filename: a.filename,
- base64: a.base64,
- mime_type: a.mimeType,
- }));
- let config: Record<string, unknown> | undefined;
-
- if (isQuizMode) {
- config = buildQuizWSConfig(quizConfig);
- if (quizConfig.mode === "mimic" && quizPdf) {
- const b64 = extractBase64FromDataUrl(
- await readFileAsDataUrl(quizPdf),
- );
- extraAttachments = [
- ...extraAttachments,
- {
- type: "pdf",
- filename: quizPdf.name,
- base64: b64,
- mime_type: "application/pdf",
- },
- ];
- }
- }
- if (isMathAnimatorMode)
- config = buildMathAnimatorWSConfig(mathAnimatorConfig);
- if (isVisualizeMode) config = buildVisualizeWSConfig(visualizeConfig);
- if (isResearchMode) config = buildResearchWSConfig(researchConfig);
-
- const skillsPayload = skillsAutoMode ? ["auto"] : [...selectedSkills];
- const memoryPayload = [...memoryReferencesPayload];
- const messageContent =
- content ||
- (selectedNotebookRecords.length ||
- selectedBookReferences.length ||
- selectedHistorySessions.length ||
- selectedQuestionEntries.length ||
- skillsPayload.length ||
- memoryPayload.length
- ? t("Please use the selected context to help with this request.")
- : "") ||
- (isMathAnimatorMode
- ? attachments.some((a) => a.type === "image")
- ? t(
- "Generate a math animation from the attached reference image(s).",
- )
- : ""
- : attachments.some((a) => a.type === "image")
- ? t("Please analyze the attached image(s).")
- : "");
- sendMessage(
- messageContent,
- extraAttachments,
- config,
- notebookReferencesPayload,
- historyReferencesPayload,
- { bookReferences: bookReferencesPayload },
- questionNotebookReferencesPayload,
- skillsPayload,
- memoryPayload,
- );
- shouldAutoScrollRef.current = true;
- // Auto-collapse the per-capability settings panel after sending so the
- // composer stays compact during conversation.
- setPanelCollapsed(true);
- setAttachments([]);
- setSelectedBookReferences([]);
- setSelectedNotebookRecords([]);
- setSelectedHistorySessions([]);
- setSelectedQuestionEntries([]);
- setSelectedSkills([]);
- setSkillsAutoMode(false);
- setSelectedMemoryFiles([]);
- },
- [
- attachments,
- bookReferencesPayload,
- historyReferencesPayload,
- isMathAnimatorMode,
- isQuizMode,
- isResearchMode,
- isVisualizeMode,
- mathAnimatorConfig,
- memoryReferencesPayload,
- notebookReferencesPayload,
- questionNotebookReferencesPayload,
- quizConfig,
- quizPdf,
- researchConfig,
- selectedHistorySessions.length,
- selectedMemoryFiles.length,
- selectedBookReferences.length,
- selectedNotebookRecords.length,
- selectedQuestionEntries.length,
- selectedSkills,
- skillsAutoMode,
- sendMessage,
- shouldAutoScrollRef,
- state.isStreaming,
- t,
- visualizeConfig,
- ],
- );
-
- const handleConfirmOutline = useCallback(
- (
- outline: OutlineItem[],
- _topic: string,
- originalConfig?: Record<string, unknown> | null,
- ) => {
- const config: Record<string, unknown> = {
- ...(originalConfig ?? {
- mode: researchConfig.mode,
- depth: researchConfig.depth,
- sources: [...researchConfig.sources],
- }),
- confirmed_outline: outline,
- };
- sendMessage(_topic, [], config, undefined, undefined, {
- displayUserMessage: false,
- persistUserMessage: false,
- });
- shouldAutoScrollRef.current = true;
- },
- [researchConfig, sendMessage, shouldAutoScrollRef],
- );
-
- const handleRegenerateMessage = useCallback(() => {
- regenerateLastMessage();
- }, [regenerateLastMessage]);
-
- const handleSetKB = useCallback(
- (kb: string) => {
- setKBs(kb ? [kb] : []);
- },
- [setKBs],
- );
- const handleSelectNotebookPicker = useCallback(() => {
- setShowNotebookPicker(true);
- }, []);
- const handleSelectBookPicker = useCallback(() => {
- setShowBookPicker(true);
- }, []);
- const handleSelectHistoryPicker = useCallback(() => {
- setShowHistoryPicker(true);
- }, []);
- const handleSelectQuestionBankPicker = useCallback(() => {
- setShowQuestionBankPicker(true);
- }, []);
- const handleSelectSkillsPicker = useCallback(() => {
- setShowSkillsPicker(true);
- }, []);
- const handleSelectMemoryPicker = useCallback(() => {
- setShowMemoryPicker(true);
- }, []);
- const handleRemoveHistory = useCallback((sessionId: string) => {
- setSelectedHistorySessions((prev) =>
- prev.filter((item) => item.sessionId !== sessionId),
- );
- }, []);
- const handleRemoveNotebook = useCallback((notebookId: string) => {
- setSelectedNotebookRecords((prev) =>
- prev.filter((record) => record.notebookId !== notebookId),
- );
- }, []);
- const handleRemoveBookReference = useCallback((bookId: string) => {
- setSelectedBookReferences((prev) =>
- prev.filter((record) => record.bookId !== bookId),
- );
- }, []);
- const handleRemoveQuestion = useCallback((entryId: number) => {
- setSelectedQuestionEntries((prev) =>
- prev.filter((entry) => entry.id !== entryId),
- );
- }, []);
- const handleToggleSkill = useCallback((name: string) => {
- setSkillsAutoMode(false);
- setSelectedSkills((prev) =>
- prev.includes(name) ? prev.filter((n) => n !== name) : [...prev, name],
- );
- }, []);
-
- const handleSetSkillsAuto = useCallback((auto: boolean) => {
- setSkillsAutoMode(auto);
- if (auto) setSelectedSkills([]);
- }, []);
-
- const handleToggleMemoryFile = useCallback((file: SpaceMemoryFile) => {
- setSelectedMemoryFiles((prev) =>
- prev.includes(file)
- ? prev.filter((item) => item !== file)
- : [...prev, file],
- );
- }, []);
-
- const handleTogglePanelCollapsed = useCallback(() => {
- setPanelCollapsed((prev) => !prev);
- }, []);
- const handleCloseNotebookPicker = useCallback(() => {
- setShowNotebookPicker(false);
- }, []);
- const handleCloseBookPicker = useCallback(() => {
- setShowBookPicker(false);
- }, []);
- const handleApplyBookReferences = useCallback(
- (references: SelectedBookReference[]) => {
- setSelectedBookReferences(references);
- },
- [],
- );
- const handleApplyNotebookRecords = useCallback(
- (records: SelectedRecord[]) => {
- setSelectedNotebookRecords(records);
- },
- [],
- );
- const handleCloseHistoryPicker = useCallback(() => {
- setShowHistoryPicker(false);
- }, []);
- const handleApplyHistorySessions = useCallback(
- (sessions: SelectedHistorySession[]) => {
- setSelectedHistorySessions(sessions);
- },
- [],
- );
- const handleCloseQuestionBankPicker = useCallback(() => {
- setShowQuestionBankPicker(false);
- }, []);
- const handleApplyQuestionEntries = useCallback(
- (entries: SelectedQuestionEntry[]) => {
- setSelectedQuestionEntries(entries);
- },
- [],
- );
- const handleCloseSkillsPicker = useCallback(() => {
- setShowSkillsPicker(false);
- }, []);
- const handleApplySkillsSelection = useCallback(
- (selection: { auto: boolean; skills: string[] }) => {
- setSkillsAutoMode(selection.auto);
- setSelectedSkills(selection.auto ? [] : selection.skills);
- },
- [],
- );
- const handleCloseMemoryPicker = useCallback(() => {
- setShowMemoryPicker(false);
- }, []);
- const handleApplyMemoryFiles = useCallback((files: SpaceMemoryFile[]) => {
- setSelectedMemoryFiles(files);
- }, []);
- const handleCloseSaveModal = useCallback(() => {
- setShowSaveModal(false);
- }, []);
-
- const handleNewChat = useCallback(() => {
- router.push("/chat");
- }, [router]);
-
- const handleDownloadMarkdown = useCallback(() => {
- if (!state.messages.length) return;
- const title =
- state.messages
- .find((msg) => msg.role === "user")
- ?.content.trim()
- .slice(0, 80) || "Chat Session";
- downloadChatMarkdown(state.messages, { title });
- }, [state.messages]);
-
- return (
- <div
- // When the preview drawer is open AND the viewport is wide enough,
- // push the chat content to the left by the drawer's width so the two
- // panels live side-by-side (matches Claude desktop). On smaller
- // screens the drawer overlays — squeezing a phone-width chat into
- // the remaining ~30 px would be useless. The actual padding +
- // transition lives in `chat-preview-shell` (globals.css) so we can
- // hand-tune it without fighting Tailwind's arbitrary-value parser.
- data-preview-open={previewSource ? "true" : "false"}
- className="chat-preview-shell flex h-full flex-col overflow-hidden bg-[var(--background)]"
- >
- <div className="mx-auto flex w-full max-w-[960px] items-center justify-between px-6 pt-3 pb-0">
- <span className="text-[15px] font-semibold tracking-[-0.01em] text-[var(--foreground)]">
- {t(activeCap.label)}
- </span>
- <div className="flex items-center gap-2">
- <button
- onClick={() => setShowSaveModal(true)}
- disabled={!chatSavePayload}
- className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-[var(--border)]/50 disabled:hover:text-[var(--muted-foreground)]"
- >
- {t("Save to Notebook")}
- </button>
- <button
- onClick={handleDownloadMarkdown}
- disabled={!state.messages.length}
- title={t("Download chat history as Markdown")}
- className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-[var(--border)]/50 disabled:hover:text-[var(--muted-foreground)]"
- >
- {t("Download Markdown")}
- </button>
- <button
- onClick={handleNewChat}
- className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)]"
- >
- {t("New chat")}
- </button>
- </div>
- </div>
- <div className="mx-auto flex w-full max-w-[960px] flex-1 min-h-0 flex-col overflow-hidden px-6">
- {!hasMessages ? (
- <div className="flex flex-1 min-h-0 flex-col items-center justify-center animate-fade-in">
- <div className="text-center">
- <h1 className="font-serif text-[36px] font-medium tracking-[-0.01em] text-[var(--foreground)]">
- {t("What would you like to learn?")}
- </h1>
- <p className="mt-4 text-[15px] text-[var(--muted-foreground)]">
- {t("Ask anything — I'm here to help you understand.")}
- </p>
- </div>
- </div>
- ) : (
- <div
- ref={messagesContainerRef}
- data-chat-scroll-root="true"
- onScroll={handleMessagesScroll}
- className={`mx-auto w-full flex-1 min-h-0 space-y-7 overflow-y-auto pr-4 [scrollbar-gutter:stable] ${hasMessages ? "pt-0" : "pt-2 pb-6"}`}
- style={
- hasMessages
- ? (() => {
- const maskImage =
- "linear-gradient(to bottom, transparent 0px, #000 32px, #000 calc(100% - 40px), transparent 100%)";
- return {
- paddingBottom: "4px",
- WebkitMaskImage: maskImage,
- maskImage,
- };
- })()
- : undefined
- }
- >
- <ChatMessageList
- messages={state.messages}
- isStreaming={state.isStreaming}
- sessionId={state.sessionId}
- language={state.language}
- onAnswerNow={handleAnswerNow}
- onCopyAssistantMessage={copyAssistantMessage}
- onRegenerateMessage={handleRegenerateMessage}
- onConfirmOutline={handleConfirmOutline}
- onPreviewAttachment={handlePreviewMessageAttachment}
- />
- <div ref={messagesEndRef} className="h-px w-full shrink-0" />
- </div>
- )}
-
- <ChatComposer
- composerRef={composerRef}
- capMenuRef={capMenuRef}
- capBtnRef={capBtnRef}
- toolMenuRef={toolMenuRef}
- toolBtnRef={toolBtnRef}
- spaceMenuRef={spaceMenuRef}
- spaceBtnRef={spaceBtnRef}
- dragCounter={dragCounter}
- dragging={dragging}
- capMenuOpen={capMenuOpen}
- toolMenuOpen={toolMenuOpen}
- spaceMenuOpen={spaceMenuOpen}
- hasMessages={hasMessages}
- attachments={attachments}
- attachmentError={attachmentError}
- activeCap={activeCap}
- visibleTools={visibleTools}
- selectedTools={selectedTools}
- ragActive={ragActive}
- knowledgeBases={knowledgeBases}
- llmOptions={llmOptions}
- activeLLMDefault={activeLLMDefault}
- llmSelection={state.llmSelection}
- llmOptionsLoading={llmOptionsLoading}
- llmOptionsError={llmOptionsError}
- selectedBookReferences={selectedBookReferences}
- selectedNotebookRecords={selectedNotebookRecords}
- selectedHistorySessions={selectedHistorySessions}
- selectedQuestionEntries={selectedQuestionEntries}
- notebookReferenceGroups={notebookReferenceGroups}
- selectedSkills={selectedSkills}
- skillsAutoMode={skillsAutoMode}
- selectedMemoryFiles={selectedMemoryFiles}
- stateKnowledgeBase={state.knowledgeBases[0] || ""}
- isStreaming={state.isStreaming}
- isResearchMode={isResearchMode}
- isQuizMode={isQuizMode}
- isMathAnimatorMode={isMathAnimatorMode}
- isVisualizeMode={isVisualizeMode}
- quizConfig={quizConfig}
- quizPdf={quizPdf}
- mathAnimatorConfig={mathAnimatorConfig}
- visualizeConfig={visualizeConfig}
- researchConfig={researchConfig}
- researchValidationErrors={researchValidation.errors}
- panelCollapsed={panelCollapsed}
- capabilities={CAPABILITIES}
- researchSources={RESEARCH_SOURCES}
- onSetCapMenuOpen={setCapMenuOpen}
- onSetToolMenuOpen={setToolMenuOpen}
- onSetSpaceMenuOpen={setSpaceMenuOpen}
- onSetKB={handleSetKB}
- onSelectLLM={setLLMSelection}
- onSelectNotebookPicker={handleSelectNotebookPicker}
- onSelectBookPicker={handleSelectBookPicker}
- onSelectHistoryPicker={handleSelectHistoryPicker}
- onSelectQuestionBankPicker={handleSelectQuestionBankPicker}
- onSelectSkillsPicker={handleSelectSkillsPicker}
- onSelectMemoryPicker={handleSelectMemoryPicker}
- onToggleTool={toggleTool}
- onToggleSkill={handleToggleSkill}
- onSetSkillsAuto={handleSetSkillsAuto}
- onToggleMemoryFile={handleToggleMemoryFile}
- onToggleResearchSource={toggleResearchSource}
- onSend={handleSend}
- onRemoveAttachment={removeAttachment}
- onPreviewAttachment={handlePreviewPendingAttachment}
- onRemoveHistory={handleRemoveHistory}
- onRemoveBookReference={handleRemoveBookReference}
- onRemoveNotebook={handleRemoveNotebook}
- onRemoveQuestion={handleRemoveQuestion}
- onDragEnter={handleDragEnter}
- onDragLeave={handleDragLeave}
- onDragOver={handleDragOver}
- onDrop={handleDrop}
- onPaste={handlePaste}
- onAddFiles={handleAddFiles}
- onSelectCapability={handleSelectCapability}
- onCancelStreaming={cancelStreamingTurn}
- onChangeQuizConfig={setQuizConfig}
- onUploadQuizPdf={setQuizPdf}
- onChangeMathAnimatorConfig={setMathAnimatorConfig}
- onChangeVisualizeConfig={setVisualizeConfig}
- onChangeResearchConfig={setResearchConfig}
- onTogglePanelCollapsed={handleTogglePanelCollapsed}
- />
- </div>
- <NotebookRecordPicker
- open={showNotebookPicker}
- onClose={handleCloseNotebookPicker}
- onApply={handleApplyNotebookRecords}
- />
- <BookReferencePicker
- open={showBookPicker}
- initialReferences={selectedBookReferences}
- onClose={handleCloseBookPicker}
- onApply={handleApplyBookReferences}
- />
- <HistorySessionPicker
- open={showHistoryPicker}
- onClose={handleCloseHistoryPicker}
- onApply={handleApplyHistorySessions}
- />
- <QuestionBankPicker
- open={showQuestionBankPicker}
- onClose={handleCloseQuestionBankPicker}
- onApply={handleApplyQuestionEntries}
- />
- <SkillsPicker
- open={showSkillsPicker}
- initialAuto={skillsAutoMode}
- initialSkills={selectedSkills}
- onClose={handleCloseSkillsPicker}
- onApply={handleApplySkillsSelection}
- />
- <MemoryPicker
- open={showMemoryPicker}
- initialFiles={selectedMemoryFiles}
- onClose={handleCloseMemoryPicker}
- onApply={handleApplyMemoryFiles}
- />
- <SaveToNotebookModal
- open={showSaveModal}
- payload={chatSavePayload}
- messages={chatSaveMessages}
- onClose={handleCloseSaveModal}
- />
- <FilePreviewDrawer
- open={previewSource !== null}
- source={previewSource}
- onClose={handleClosePreview}
- />
- </div>
- );
-}
+"use client";
+
+import dynamic from "next/dynamic";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import { useParams, useRouter } from "next/navigation";
+
+import {
+ BarChart3,
+ BrainCircuit,
+ Clapperboard,
+ Code2,
+ Database,
+ FileSearch,
+ Globe,
+ Lightbulb,
+ MessageSquare,
+ Microscope,
+ PenLine,
+ Sparkles,
+ type LucideIcon,
+} from "lucide-react";
+import { useTranslation } from "react-i18next";
+import type { SelectedRecord } from "@/lib/notebook-selection-types";
+import type { SelectedHistorySession } from "@/components/chat/HistorySessionPicker";
+import type { SelectedQuestionEntry } from "@/components/chat/QuestionBankPicker";
+import ChatComposer from "@/components/chat/home/ChatComposer";
+import { ChatMessageList } from "@/components/chat/home/ChatMessages";
+// Imported eagerly so the drawer shell is always mounted off-screen —
+// clicking a chip becomes a single CSS class flip, no chunk fetch + double
+// render. The heavy renderers inside still load lazily.
+import FilePreviewDrawer from "@/components/chat/preview/FilePreviewDrawer";
+import {
+ useUnifiedChat,
+ type MessageAttachment,
+ type MessageRequestSnapshot,
+} from "@/context/UnifiedChatContext";
+import { useAppShell } from "@/context/AppShellContext";
+import type { FilePreviewSource } from "@/components/chat/preview/previewerFor";
- import type { StreamEvent } from "@/lib/unified-ws";
++import type { LLMSelection, StreamEvent } from "@/lib/unified-ws";
+import {
+ extractBase64FromDataUrl,
+ readFileAsDataUrl,
+} from "@/lib/file-attachments";
+import {
+ classifyFile,
+ isSvgFilename,
+ MAX_ATTACHMENT_BYTES,
+ MAX_TOTAL_ATTACHMENT_BYTES,
+} from "@/lib/doc-attachments";
+import { useChatAutoScroll } from "@/hooks/useChatAutoScroll";
+import { useMeasuredHeight } from "@/hooks/useMeasuredHeight";
+import {
+ loadCapabilityPlaygroundConfigs,
+ resolveCapabilityPlaygroundConfig,
+ type CapabilityPlaygroundConfigMap,
+} from "@/lib/playground-config";
+import {
+ DEFAULT_QUIZ_CONFIG,
+ buildQuizWSConfig,
+ type DeepQuestionFormConfig,
+} from "@/lib/quiz-types";
+import {
+ DEFAULT_MATH_ANIMATOR_CONFIG,
+ buildMathAnimatorWSConfig,
+ type MathAnimatorFormConfig,
+} from "@/lib/math-animator-types";
+import {
+ DEFAULT_VISUALIZE_CONFIG,
+ buildVisualizeWSConfig,
+ type VisualizeFormConfig,
+} from "@/lib/visualize-types";
+import {
+ buildResearchWSConfig,
+ createEmptyResearchConfig,
+ validateResearchConfig,
+ type DeepResearchFormConfig,
+ type OutlineItem,
+ type ResearchSource,
+} from "@/lib/research-types";
+import { listKnowledgeBases } from "@/lib/knowledge-api";
++import { listLLMOptions, type LLMOption } from "@/lib/llm-options";
+import { downloadChatMarkdown } from "@/lib/chat-export";
+import type { SpaceMemoryFile } from "@/lib/space-items";
+import {
+ selectedBooksToPayload,
+ type SelectedBookReference,
+} from "@/lib/book-references";
+
+const NotebookRecordPicker = dynamic(
+ () => import("@/components/notebook/NotebookRecordPicker"),
+ {
+ ssr: false,
+ },
+);
+const HistorySessionPicker = dynamic(
+ () => import("@/components/chat/HistorySessionPicker"),
+ {
+ ssr: false,
+ },
+);
+const QuestionBankPicker = dynamic(
+ () => import("@/components/chat/QuestionBankPicker"),
+ {
+ ssr: false,
+ },
+);
+const SkillsPicker = dynamic(() => import("@/components/chat/SkillsPicker"), {
+ ssr: false,
+});
+const MemoryPicker = dynamic(() => import("@/components/chat/MemoryPicker"), {
+ ssr: false,
+});
+const BookReferencePicker = dynamic(
+ () => import("@/components/chat/BookReferencePicker"),
+ {
+ ssr: false,
+ },
+);
+const SaveToNotebookModal = dynamic(
+ () => import("@/components/notebook/SaveToNotebookModal"),
+ {
+ ssr: false,
+ },
+);
+
+/* ------------------------------------------------------------------ */
+/* Type & data definitions */
+/* ------------------------------------------------------------------ */
+
+type ToolName =
+ | "brainstorm"
+ | "rag"
+ | "web_search"
+ | "code_execution"
+ | "reason"
+ | "paper_search";
+
+interface ToolDef {
+ name: ToolName;
+ label: string;
+ icon: LucideIcon;
+}
+
+interface ResearchSourceDef {
+ name: ResearchSource;
+ label: string;
+ icon: LucideIcon;
+}
+
+const ALL_TOOLS: ToolDef[] = [
+ { name: "brainstorm", label: "Brainstorm", icon: Lightbulb },
+ { name: "rag", label: "RAG", icon: Database },
+ { name: "web_search", label: "Web Search", icon: Globe },
+ { name: "code_execution", label: "Code", icon: Code2 },
+ { name: "reason", label: "Reason", icon: Sparkles },
+ { name: "paper_search", label: "Arxiv Search", icon: FileSearch },
+];
+
+const RESEARCH_SOURCES: ResearchSourceDef[] = [
+ { name: "kb", label: "Knowledge Base", icon: Database },
+ { name: "web", label: "Web", icon: Globe },
+ { name: "papers", label: "Papers", icon: FileSearch },
+];
+
+interface CapabilityDef {
+ value: string;
+ label: string;
+ description: string;
+ icon: LucideIcon;
+ allowedTools: ToolName[];
+ defaultTools: ToolName[];
+}
+
+const CAPABILITIES: CapabilityDef[] = [
+ {
+ value: "",
+ label: "Chat",
+ description: "Flexible conversation with any tool",
+ icon: MessageSquare,
+ allowedTools: [
+ "brainstorm",
+ "rag",
+ "web_search",
+ "code_execution",
+ "reason",
+ "paper_search",
+ ],
+ defaultTools: [],
+ },
+ {
+ value: "deep_solve",
+ label: "Deep Solve",
+ description: "Multi-step reasoning & problem solving",
+ icon: BrainCircuit,
+ allowedTools: ["rag", "web_search", "code_execution", "reason"],
+ defaultTools: ["rag", "web_search", "code_execution", "reason"],
+ },
+ {
+ value: "deep_question",
+ label: "Quiz Generation",
+ description: "Auto-validated question generation",
+ icon: PenLine,
+ allowedTools: ["rag", "web_search", "code_execution"],
+ defaultTools: ["rag", "web_search", "code_execution"],
+ },
+ {
+ value: "deep_research",
+ label: "Deep Research",
+ description: "Comprehensive multi-agent research",
+ icon: Microscope,
+ allowedTools: [],
+ defaultTools: [],
+ },
+ {
+ value: "math_animator",
+ label: "Math Animator",
+ description: "Generate math videos or storyboard images",
+ icon: Clapperboard,
+ allowedTools: [],
+ defaultTools: [],
+ },
+ {
+ value: "visualize",
+ label: "Visualize",
+ description: "Generate SVG, Chart.js, or Mermaid visualizations",
- icon: BarChart3,
- allowedTools: [],
- defaultTools: [],
- },
- {
- value: "guided_learning",
- label: "Guided Learning",
- description: "Framework v1.8.2 — diagnostic, mastery-based study, spaced repetition",
- icon: Lightbulb,
- allowedTools: ["rag", "web_search", "code_execution"],
- defaultTools: ["rag"],
- },
- ];
++ icon: BarChart3,
++ allowedTools: [],
++ defaultTools: [],
++ },
++];
+
+interface KnowledgeBase {
+ name: string;
+ is_default?: boolean;
+}
+
+interface PendingAttachment {
+ type: string;
+ filename: string;
+ base64?: string;
+ previewUrl?: string;
+ size?: number;
+ mimeType?: string;
+}
+
+/* ------------------------------------------------------------------ */
+/* Helpers */
+/* ------------------------------------------------------------------ */
+
+function getCapability(value: string | null): CapabilityDef {
+ return CAPABILITIES.find((c) => c.value === (value || "")) ?? CAPABILITIES[0];
+}
+
+/* ------------------------------------------------------------------ */
+/* Chat page */
+/* ------------------------------------------------------------------ */
+
+export default function ChatPage() {
+ const params = useParams<{ sessionId?: string[] }>();
+ const router = useRouter();
+ const { t } = useTranslation();
+ const sessionIdParam = params.sessionId?.[0] ?? null;
+ const { setActiveSessionId, language: appLanguage } = useAppShell();
+
+ const {
+ state,
+ setTools,
+ setCapability,
+ setKBs,
++ setLLMSelection,
+ sendMessage,
+ cancelStreamingTurn,
+ regenerateLastMessage,
+ newSession,
+ loadSession,
+ } = useUnifiedChat();
+
+ const [knowledgeBases, setKnowledgeBases] = useState<KnowledgeBase[]>([]);
++ const [llmOptions, setLLMOptions] = useState<LLMOption[]>([]);
++ const [activeLLMDefault, setActiveLLMDefault] = useState<LLMSelection | null>(
++ null,
++ );
++ const [llmOptionsLoading, setLLMOptionsLoading] = useState(true);
++ const [llmOptionsError, setLLMOptionsError] = useState(false);
+ const [capabilityConfigs, setCapabilityConfigs] =
+ useState<CapabilityPlaygroundConfigMap>({});
+ const [attachments, setAttachments] = useState<PendingAttachment[]>([]);
+ const [dragging, setDragging] = useState(false);
+ const [attachmentError, setAttachmentError] = useState<string | null>(null);
+ const [previewSource, setPreviewSource] = useState<FilePreviewSource | null>(
+ null,
+ );
+ const attachmentErrorTimer = useRef<ReturnType<typeof setTimeout> | null>(
+ null,
+ );
+ const [capMenuOpen, setCapMenuOpen] = useState(false);
+ const [quizConfig, setQuizConfig] = useState<DeepQuestionFormConfig>({
+ ...DEFAULT_QUIZ_CONFIG,
+ });
+ const [quizPdf, setQuizPdf] = useState<File | null>(null);
+ const [mathAnimatorConfig, setMathAnimatorConfig] =
+ useState<MathAnimatorFormConfig>({
+ ...DEFAULT_MATH_ANIMATOR_CONFIG,
+ });
+ const [visualizeConfig, setVisualizeConfig] = useState<VisualizeFormConfig>({
+ ...DEFAULT_VISUALIZE_CONFIG,
+ });
+ const [researchConfig, setResearchConfig] = useState<DeepResearchFormConfig>(
+ createEmptyResearchConfig(),
+ );
+ // Unified collapse state for the capability-specific config panel
+ // (Quiz / Math Animator / Visualize / Deep Research). Default collapsed so
+ // a fresh Chat / Deep Solve session has the shortest possible composer.
+ const [panelCollapsed, setPanelCollapsed] = useState(true);
+ const [showSaveModal, setShowSaveModal] = useState(false);
+ const [showNotebookPicker, setShowNotebookPicker] = useState(false);
+ const [showBookPicker, setShowBookPicker] = useState(false);
+ const [showHistoryPicker, setShowHistoryPicker] = useState(false);
+ const [showQuestionBankPicker, setShowQuestionBankPicker] = useState(false);
+ const [showSkillsPicker, setShowSkillsPicker] = useState(false);
+ const [showMemoryPicker, setShowMemoryPicker] = useState(false);
+ const [toolMenuOpen, setToolMenuOpen] = useState(false);
+ const [spaceMenuOpen, setSpaceMenuOpen] = useState(false);
+ const [selectedNotebookRecords, setSelectedNotebookRecords] = useState<
+ SelectedRecord[]
+ >([]);
+ const [selectedBookReferences, setSelectedBookReferences] = useState<
+ SelectedBookReference[]
+ >([]);
+ const [selectedHistorySessions, setSelectedHistorySessions] = useState<
+ SelectedHistorySession[]
+ >([]);
+ const [selectedQuestionEntries, setSelectedQuestionEntries] = useState<
+ SelectedQuestionEntry[]
+ >([]);
+ const [selectedSkills, setSelectedSkills] = useState<string[]>([]);
+ const [skillsAutoMode, setSkillsAutoMode] = useState(false);
+ const [selectedMemoryFiles, setSelectedMemoryFiles] = useState<
+ SpaceMemoryFile[]
+ >([]);
+ const dragCounter = useRef(0);
+ const capMenuRef = useRef<HTMLDivElement>(null);
+ const capBtnRef = useRef<HTMLButtonElement>(null);
+ const toolMenuRef = useRef<HTMLDivElement>(null);
+ const toolBtnRef = useRef<HTMLButtonElement>(null);
+ const spaceMenuRef = useRef<HTMLDivElement>(null);
+ const spaceBtnRef = useRef<HTMLButtonElement>(null);
+ const initialLoadRef = useRef(false);
+
+ const activeCap = useMemo(
+ () => getCapability(state.activeCapability),
+ [state.activeCapability],
+ );
+ const isQuizMode = activeCap.value === "deep_question";
+ const isMathAnimatorMode = activeCap.value === "math_animator";
+ const isVisualizeMode = activeCap.value === "visualize";
+ const isResearchMode = activeCap.value === "deep_research";
+ const selectedTools = useMemo(
+ () => new Set(state.enabledTools),
+ [state.enabledTools],
+ );
+ const ragActive = isResearchMode
+ ? researchConfig.sources.includes("kb")
+ : selectedTools.has("rag");
+ const hasMessages = state.messages.length > 0;
+ const { ref: composerRef, height: composerHeight } =
+ useMeasuredHeight<HTMLDivElement>();
+ const visibleTools = useMemo(
+ () => ALL_TOOLS.filter((t) => activeCap.allowedTools.includes(t.name)),
+ [activeCap.allowedTools],
+ );
+ const researchValidation = useMemo(
+ () => validateResearchConfig(researchConfig),
+ [researchConfig],
+ );
+ const notebookReferenceGroups = useMemo(() => {
+ const groups = new Map<string, { notebookName: string; count: number }>();
+ selectedNotebookRecords.forEach((record) => {
+ const existing = groups.get(record.notebookId);
+ if (existing) {
+ existing.count += 1;
+ } else {
+ groups.set(record.notebookId, {
+ notebookName: record.notebookName,
+ count: 1,
+ });
+ }
+ });
+ return Array.from(groups.entries()).map(([notebookId, value]) => ({
+ notebookId,
+ ...value,
+ }));
+ }, [selectedNotebookRecords]);
+ const notebookReferencesPayload = useMemo(() => {
+ const grouped = new Map<string, string[]>();
+ selectedNotebookRecords.forEach((record) => {
+ const current = grouped.get(record.notebookId) || [];
+ current.push(record.id);
+ grouped.set(record.notebookId, current);
+ });
+ return Array.from(grouped.entries()).map(([notebook_id, record_ids]) => ({
+ notebook_id,
+ record_ids,
+ }));
+ }, [selectedNotebookRecords]);
+ const bookReferencesPayload = useMemo(
+ () => selectedBooksToPayload(selectedBookReferences),
+ [selectedBookReferences],
+ );
+ const historyReferencesPayload = useMemo(
+ () => selectedHistorySessions.map((session) => session.sessionId),
+ [selectedHistorySessions],
+ );
+ const questionNotebookReferencesPayload = useMemo(
+ () => selectedQuestionEntries.map((entry) => entry.id),
+ [selectedQuestionEntries],
+ );
+ const memoryReferencesPayload = useMemo(
+ () => [...selectedMemoryFiles],
+ [selectedMemoryFiles],
+ );
+ const chatSaveMessages = useMemo(
+ () =>
+ state.messages.map((msg) => ({
+ role: msg.role,
+ content: msg.content,
+ capability: msg.capability,
+ })),
+ [state.messages],
+ );
+ const chatSavePayload = useMemo(() => {
+ if (!state.messages.length) return null;
+ const title =
+ state.messages
+ .find((msg) => msg.role === "user")
+ ?.content.trim()
+ .slice(0, 80) || "Chat Session";
+ return {
+ recordType: "chat" as const,
+ title,
+ // The actual transcript / userQuery are rebuilt inside SaveToNotebookModal
+ // from the user's selected subset of messages. We still provide a
+ // sensible fallback for non-selection callers.
+ userQuery: "",
+ output: "",
+ metadata: {
+ source: "chat",
+ capability: state.activeCapability || "chat",
+ ui_language: state.language,
+ session_id: state.sessionId,
+ total_message_count: state.messages.length,
+ },
+ };
+ }, [state.activeCapability, state.language, state.messages, state.sessionId]);
+ const lastMessage = state.messages[state.messages.length - 1];
+ const {
+ containerRef: messagesContainerRef,
+ endRef: messagesEndRef,
+ shouldAutoScrollRef,
+ handleScroll: handleMessagesScroll,
+ } = useChatAutoScroll({
+ hasMessages,
+ isStreaming: state.isStreaming,
+ composerHeight,
+ messageCount: state.messages.length,
+ lastMessageContent: lastMessage?.content,
+ lastEventCount: lastMessage?.events?.length,
+ });
+ const copyAssistantMessage = useCallback(async (content: string) => {
+ if (!content.trim()) return;
+ try {
+ await navigator.clipboard.writeText(content);
+ } catch (error) {
+ console.error("Failed to copy assistant message:", error);
+ }
+ }, []);
+ const handleAnswerNow = useCallback(
+ (
+ snapshot?: MessageRequestSnapshot,
+ assistantMsg?: { content: string; events?: StreamEvent[] },
+ ) => {
+ if (!snapshot || !state.isStreaming) return;
+ const answerNowEvents = (assistantMsg?.events ?? []).map((event) => ({
+ type: event.type,
+ stage: event.stage,
+ content: event.content,
+ metadata: event.metadata ?? {},
+ }));
+ cancelStreamingTurn();
+ // Preserve the original capability — each capability now owns its
+ // own answer-now fast-path (deep_solve jumps to writing,
+ // deep_question to direct quiz synthesis, math_animator to
+ // code-gen + render, etc.). The backend orchestrator only falls
+ // back to ``chat`` if the requested capability is missing.
+ const answerNowSnapshot: MessageRequestSnapshot = {
+ ...snapshot,
+ language: appLanguage,
+ config: {
+ ...(snapshot.config || {}),
+ answer_now_context: {
+ original_user_message: snapshot.content,
+ partial_response: assistantMsg?.content || "",
+ events: answerNowEvents,
+ },
+ },
+ };
+ window.setTimeout(() => {
+ sendMessage(
+ answerNowSnapshot.content,
+ answerNowSnapshot.attachments,
+ answerNowSnapshot.config,
+ answerNowSnapshot.notebookReferences,
+ answerNowSnapshot.historyReferences,
+ {
+ displayUserMessage: false,
+ persistUserMessage: false,
+ requestSnapshotOverride: answerNowSnapshot,
+ bookReferences: answerNowSnapshot.bookReferences,
+ },
+ answerNowSnapshot.questionNotebookReferences,
+ answerNowSnapshot.skills,
+ answerNowSnapshot.memoryReferences,
+ );
+ shouldAutoScrollRef.current = true;
+ }, 0);
+ },
+ [
+ appLanguage,
+ cancelStreamingTurn,
+ sendMessage,
+ shouldAutoScrollRef,
+ state.isStreaming,
+ ],
+ );
+
+ /* ---- URL-driven session loading ---- */
+ useEffect(() => {
+ if (initialLoadRef.current) return;
+ initialLoadRef.current = true;
+ if (sessionIdParam) {
+ void loadSession(sessionIdParam).catch(() => {
+ router.replace("/chat", { scroll: false });
+ });
+ } else {
+ newSession();
+ }
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
+
+ // When URL param changes (sidebar navigation), load the corresponding session
+ const prevSessionIdParam = useRef(sessionIdParam);
+ useEffect(() => {
+ if (sessionIdParam === prevSessionIdParam.current) return;
+ prevSessionIdParam.current = sessionIdParam;
+ if (sessionIdParam) {
+ if (sessionIdParam === state.sessionId) return;
+ void loadSession(sessionIdParam).catch(() => {
+ router.replace("/chat", { scroll: false });
+ });
+ } else {
+ newSession();
+ }
+ }, [sessionIdParam, loadSession, newSession, router, state.sessionId]);
+
+ // When a new session_id is assigned by the server, update the URL
+ useEffect(() => {
+ if (state.sessionId && !sessionIdParam) {
+ router.replace(`/chat/${state.sessionId}`, { scroll: false });
+ }
+ }, [state.sessionId, sessionIdParam, router]);
+
+ useEffect(() => {
+ setActiveSessionId(state.sessionId || sessionIdParam || null);
+ }, [state.sessionId, sessionIdParam, setActiveSessionId]);
+
+ const refreshKnowledgeBases = useCallback(
+ async (options?: { force?: boolean }) => {
+ try {
+ const list = await listKnowledgeBases({ force: options?.force });
+ setKnowledgeBases(list);
+ if (!state.knowledgeBases.length && list.length) {
+ const def = list.find((k: KnowledgeBase) => k.is_default);
+ setKBs([def?.name || list[0].name]);
+ }
+ } catch {
+ setKnowledgeBases([]);
+ }
+ },
+ [setKBs, state.knowledgeBases.length],
+ );
+
+ /* Load KBs */
+ useEffect(() => {
+ void refreshKnowledgeBases({ force: true });
+ }, [refreshKnowledgeBases]);
+
++ const refreshLLMOptions = useCallback(async () => {
++ setLLMOptionsLoading(true);
++ try {
++ const payload = await listLLMOptions();
++ setLLMOptions(payload.options);
++ setActiveLLMDefault(payload.active);
++ setLLMOptionsError(false);
++ } catch {
++ setLLMOptionsError(true);
++ setLLMOptions([]);
++ setActiveLLMDefault(null);
++ } finally {
++ setLLMOptionsLoading(false);
++ }
++ }, []);
++
++ useEffect(() => {
++ void refreshLLMOptions();
++ }, [refreshLLMOptions]);
++
++ useEffect(() => {
++ if (state.llmSelection || !activeLLMDefault) return;
++ setLLMSelection(activeLLMDefault);
++ }, [activeLLMDefault, setLLMSelection, state.llmSelection]);
++
+ useEffect(() => {
+ if (typeof window === "undefined") return;
+ const refresh = () => {
+ void refreshKnowledgeBases({ force: true });
++ void refreshLLMOptions();
+ };
+ const refreshWhenVisible = () => {
+ if (document.visibilityState === "visible") refresh();
+ };
+ window.addEventListener("focus", refresh);
+ window.addEventListener("pageshow", refresh);
+ document.addEventListener("visibilitychange", refreshWhenVisible);
+ return () => {
+ window.removeEventListener("focus", refresh);
+ window.removeEventListener("pageshow", refresh);
+ document.removeEventListener("visibilitychange", refreshWhenVisible);
+ };
- }, [refreshKnowledgeBases]);
++ }, [refreshKnowledgeBases, refreshLLMOptions]);
+
+ useEffect(() => {
+ setCapabilityConfigs(loadCapabilityPlaygroundConfigs());
+ }, []);
+
+ /* URL query params (capability, tool) */
+ useEffect(() => {
+ if (typeof window === "undefined") return;
+ const p = new URLSearchParams(window.location.search);
+ const qc = p.get("capability");
+ const qt = p.getAll("tool");
+ if (qc !== null) handleSelectCapability(qc || "");
+ else if (qt.length) {
+ const valid = qt.filter((t): t is ToolName =>
+ ALL_TOOLS.some((d) => d.name === t),
+ );
+ if (valid.length) setTools(Array.from(new Set(valid)));
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ useEffect(() => {
+ const handler = (e: MouseEvent) => {
+ const t = e.target as Node;
+ if (
+ capMenuRef.current &&
+ !capMenuRef.current.contains(t) &&
+ capBtnRef.current &&
+ !capBtnRef.current.contains(t)
+ )
+ setCapMenuOpen(false);
+ if (
+ toolMenuRef.current &&
+ !toolMenuRef.current.contains(t) &&
+ toolBtnRef.current &&
+ !toolBtnRef.current.contains(t)
+ )
+ setToolMenuOpen(false);
+ if (
+ spaceMenuRef.current &&
+ !spaceMenuRef.current.contains(t) &&
+ spaceBtnRef.current &&
+ !spaceBtnRef.current.contains(t)
+ )
+ setSpaceMenuOpen(false);
+ };
+ document.addEventListener("mousedown", handler);
+ return () => document.removeEventListener("mousedown", handler);
+ }, []);
+
+ useEffect(() => {
+ const allowed = new Set(visibleTools.map((tool) => tool.name));
+ const nextTools = state.enabledTools.filter((tool) =>
+ allowed.has(tool as ToolName),
+ );
+ if (nextTools.length !== state.enabledTools.length) setTools(nextTools);
+ }, [setTools, state.enabledTools, visibleTools]);
+
+ /* ---- handlers ---- */
+
+ const handleSelectCapability = useCallback(
+ (value: string) => {
+ const cap =
+ CAPABILITIES.find((c) => c.value === value) ?? CAPABILITIES[0];
+ const storageKey = cap.value || "chat";
+ const config = resolveCapabilityPlaygroundConfig(
+ capabilityConfigs,
+ storageKey,
+ cap.allowedTools,
+ );
+ setCapability(cap.value || null);
+ setTools(
+ config.enabledTools.length > 0 || capabilityConfigs[storageKey]
+ ? [...config.enabledTools]
+ : [...cap.defaultTools],
+ );
+ if (config.enabledTools.includes("rag") && config.knowledgeBase)
+ setKBs([config.knowledgeBase]);
+ // Default-expand the per-capability settings panel right after a
+ // capability switch so users immediately see the form. Sending a
+ // message later will auto-collapse it (see handleSend).
+ setPanelCollapsed(false);
+ setCapMenuOpen(false);
+ },
+ [capabilityConfigs, setCapability, setKBs, setTools],
+ );
+
+ const toggleTool = useCallback(
+ (tool: string) => {
+ if (!activeCap.allowedTools.includes(tool as ToolName)) return;
+ if (selectedTools.has(tool)) {
+ setTools(state.enabledTools.filter((t) => t !== tool));
+ } else {
+ setTools([...state.enabledTools, tool]);
+ }
+ },
+ [activeCap.allowedTools, selectedTools, setTools, state.enabledTools],
+ );
+
+ const toggleResearchSource = useCallback((source: ResearchSource) => {
+ setResearchConfig((current) => ({
+ ...current,
+ sources: current.sources.includes(source)
+ ? current.sources.filter((item) => item !== source)
+ : [...current.sources, source],
+ }));
+ }, []);
+
+ const fileToAttachment = useCallback(
+ (f: File): Promise<PendingAttachment> =>
+ new Promise((resolve, reject) => {
+ readFileAsDataUrl(f)
+ .then((raw) => {
+ // SVG: treat as file (text extraction on server, vision models
+ // reject SVG) but keep the data URL so the chip can render a
+ // thumbnail via a raw <img> tag.
+ const svg = isSvgFilename(f.name) || f.type === "image/svg+xml";
+ const isImage = !svg && f.type.startsWith("image/");
+ const b64 = extractBase64FromDataUrl(raw);
+ resolve({
+ type: isImage ? "image" : "file",
+ filename: f.name,
+ base64: b64,
+ previewUrl: isImage || svg ? raw : undefined,
+ size: f.size,
+ mimeType: f.type || undefined,
+ });
+ })
+ .catch(reject);
+ }),
+ [],
+ );
+
+ const showAttachmentError = useCallback((message: string) => {
+ setAttachmentError(message);
+ if (attachmentErrorTimer.current) {
+ clearTimeout(attachmentErrorTimer.current);
+ }
+ attachmentErrorTimer.current = setTimeout(() => {
+ setAttachmentError(null);
+ attachmentErrorTimer.current = null;
+ }, 4000);
+ }, []);
+
+ const filterAndReportFiles = useCallback(
+ (files: File[]): File[] => {
+ let runningTotal = attachments.reduce((s, a) => s + (a.size ?? 0), 0);
+ const accepted: File[] = [];
+ const rejected: {
+ name: string;
+ reason: "unsupported" | "too_large" | "quota";
+ }[] = [];
+ for (const f of files) {
+ const kind = classifyFile(f);
+ if (!kind) {
+ rejected.push({ name: f.name, reason: "unsupported" });
+ continue;
+ }
+ if (f.size > MAX_ATTACHMENT_BYTES) {
+ rejected.push({ name: f.name, reason: "too_large" });
+ continue;
+ }
+ if (runningTotal + f.size > MAX_TOTAL_ATTACHMENT_BYTES) {
+ rejected.push({ name: f.name, reason: "quota" });
+ break;
+ }
+ runningTotal += f.size;
+ accepted.push(f);
+ }
+ if (rejected.length) {
+ const first = rejected[0];
+ let msg: string;
+ if (first.reason === "too_large") {
+ msg = t("File too large: {{name}}", { name: first.name });
+ } else if (first.reason === "quota") {
+ msg = t("Too many files, skipped some");
+ } else {
+ msg = t("Unsupported file type: {{name}}", { name: first.name });
+ }
+ showAttachmentError(msg);
+ }
+ return accepted;
+ },
+ [attachments, showAttachmentError, t],
+ );
+
+ const handlePaste = useCallback(
+ async (event: React.ClipboardEvent) => {
+ const items = Array.from(event.clipboardData.items);
+ const files = items
+ .filter((item) => item.kind === "file")
+ .map((item) => item.getAsFile())
+ .filter((f): f is File => f !== null);
+ const accepted = filterAndReportFiles(files);
+ if (!accepted.length) return;
+ event.preventDefault();
+ const next = await Promise.all(accepted.map(fileToAttachment));
+ setAttachments((prev) => [...prev, ...next]);
+ },
+ [fileToAttachment, filterAndReportFiles],
+ );
+
+ const removeAttachment = useCallback((index: number) => {
+ setAttachments((prev) => prev.filter((_, i) => i !== index));
+ }, []);
+
+ const handlePreviewPendingAttachment = useCallback(
+ (index: number) => {
+ const a = attachments[index];
+ if (!a) return;
+ setPreviewSource({
+ filename: a.filename,
+ mimeType: a.mimeType,
+ type: a.type,
+ base64: a.base64,
+ size: a.size,
+ });
+ },
+ [attachments],
+ );
+
+ const handlePreviewMessageAttachment = useCallback((a: MessageAttachment) => {
+ setPreviewSource({
+ filename: a.filename || "",
+ mimeType: a.mime_type,
+ type: a.type,
+ url: a.url,
+ base64: a.base64,
+ extractedText: a.extracted_text,
+ id: a.id,
+ });
+ }, []);
+
+ const handleClosePreview = useCallback(() => {
+ setPreviewSource(null);
+ }, []);
+
+ const handleDragEnter = useCallback((e: React.DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ dragCounter.current += 1;
+ if (e.dataTransfer.types.includes("Files")) setDragging(true);
+ }, []);
+
+ const handleDragLeave = useCallback((e: React.DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ dragCounter.current -= 1;
+ if (dragCounter.current === 0) setDragging(false);
+ }, []);
+
+ const handleDragOver = useCallback((e: React.DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ }, []);
+
+ const handleDrop = useCallback(
+ async (e: React.DragEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setDragging(false);
+ dragCounter.current = 0;
+ const accepted = filterAndReportFiles(Array.from(e.dataTransfer.files));
+ if (!accepted.length) return;
+ const next = await Promise.all(accepted.map(fileToAttachment));
+ setAttachments((prev) => [...prev, ...next]);
+ },
+ [fileToAttachment, filterAndReportFiles],
+ );
+
+ const handleAddFiles = useCallback(
+ async (files: File[]) => {
+ const accepted = filterAndReportFiles(files);
+ if (!accepted.length) return;
+ const next = await Promise.all(accepted.map(fileToAttachment));
+ setAttachments((prev) => [...prev, ...next]);
+ },
+ [fileToAttachment, filterAndReportFiles],
+ );
+
+ const handleSend = useCallback(
+ async (content: string) => {
+ if (
+ (!content &&
+ !attachments.length &&
+ !selectedBookReferences.length &&
+ !selectedNotebookRecords.length &&
+ !selectedHistorySessions.length &&
+ !selectedQuestionEntries.length &&
+ !selectedSkills.length &&
+ !skillsAutoMode &&
+ !selectedMemoryFiles.length) ||
+ state.isStreaming
+ )
+ return;
+
+ let extraAttachments = attachments.map((a) => ({
+ type: a.type,
+ filename: a.filename,
+ base64: a.base64,
+ mime_type: a.mimeType,
+ }));
+ let config: Record<string, unknown> | undefined;
+
+ if (isQuizMode) {
+ config = buildQuizWSConfig(quizConfig);
+ if (quizConfig.mode === "mimic" && quizPdf) {
+ const b64 = extractBase64FromDataUrl(
+ await readFileAsDataUrl(quizPdf),
+ );
+ extraAttachments = [
+ ...extraAttachments,
+ {
+ type: "pdf",
+ filename: quizPdf.name,
+ base64: b64,
+ mime_type: "application/pdf",
+ },
+ ];
+ }
+ }
+ if (isMathAnimatorMode)
+ config = buildMathAnimatorWSConfig(mathAnimatorConfig);
+ if (isVisualizeMode) config = buildVisualizeWSConfig(visualizeConfig);
+ if (isResearchMode) config = buildResearchWSConfig(researchConfig);
+
+ const skillsPayload = skillsAutoMode ? ["auto"] : [...selectedSkills];
+ const memoryPayload = [...memoryReferencesPayload];
+ const messageContent =
+ content ||
+ (selectedNotebookRecords.length ||
+ selectedBookReferences.length ||
+ selectedHistorySessions.length ||
+ selectedQuestionEntries.length ||
+ skillsPayload.length ||
+ memoryPayload.length
+ ? t("Please use the selected context to help with this request.")
+ : "") ||
+ (isMathAnimatorMode
+ ? attachments.some((a) => a.type === "image")
+ ? t(
+ "Generate a math animation from the attached reference image(s).",
+ )
+ : ""
+ : attachments.some((a) => a.type === "image")
+ ? t("Please analyze the attached image(s).")
+ : "");
+ sendMessage(
+ messageContent,
+ extraAttachments,
+ config,
+ notebookReferencesPayload,
+ historyReferencesPayload,
+ { bookReferences: bookReferencesPayload },
+ questionNotebookReferencesPayload,
+ skillsPayload,
+ memoryPayload,
+ );
+ shouldAutoScrollRef.current = true;
+ // Auto-collapse the per-capability settings panel after sending so the
+ // composer stays compact during conversation.
+ setPanelCollapsed(true);
+ setAttachments([]);
+ setSelectedBookReferences([]);
+ setSelectedNotebookRecords([]);
+ setSelectedHistorySessions([]);
+ setSelectedQuestionEntries([]);
+ setSelectedSkills([]);
+ setSkillsAutoMode(false);
+ setSelectedMemoryFiles([]);
+ },
+ [
+ attachments,
+ bookReferencesPayload,
+ historyReferencesPayload,
+ isMathAnimatorMode,
+ isQuizMode,
+ isResearchMode,
+ isVisualizeMode,
+ mathAnimatorConfig,
+ memoryReferencesPayload,
+ notebookReferencesPayload,
+ questionNotebookReferencesPayload,
+ quizConfig,
+ quizPdf,
+ researchConfig,
+ selectedHistorySessions.length,
+ selectedMemoryFiles.length,
+ selectedBookReferences.length,
+ selectedNotebookRecords.length,
+ selectedQuestionEntries.length,
+ selectedSkills,
+ skillsAutoMode,
+ sendMessage,
+ shouldAutoScrollRef,
+ state.isStreaming,
+ t,
+ visualizeConfig,
+ ],
+ );
+
+ const handleConfirmOutline = useCallback(
+ (
+ outline: OutlineItem[],
+ _topic: string,
+ originalConfig?: Record<string, unknown> | null,
+ ) => {
+ const config: Record<string, unknown> = {
+ ...(originalConfig ?? {
+ mode: researchConfig.mode,
+ depth: researchConfig.depth,
+ sources: [...researchConfig.sources],
+ }),
+ confirmed_outline: outline,
+ };
+ sendMessage(_topic, [], config, undefined, undefined, {
+ displayUserMessage: false,
+ persistUserMessage: false,
+ });
+ shouldAutoScrollRef.current = true;
+ },
+ [researchConfig, sendMessage, shouldAutoScrollRef],
+ );
+
+ const handleRegenerateMessage = useCallback(() => {
+ regenerateLastMessage();
+ }, [regenerateLastMessage]);
+
+ const handleSetKB = useCallback(
+ (kb: string) => {
+ setKBs(kb ? [kb] : []);
+ },
+ [setKBs],
+ );
+ const handleSelectNotebookPicker = useCallback(() => {
+ setShowNotebookPicker(true);
+ }, []);
+ const handleSelectBookPicker = useCallback(() => {
+ setShowBookPicker(true);
+ }, []);
+ const handleSelectHistoryPicker = useCallback(() => {
+ setShowHistoryPicker(true);
+ }, []);
+ const handleSelectQuestionBankPicker = useCallback(() => {
+ setShowQuestionBankPicker(true);
+ }, []);
+ const handleSelectSkillsPicker = useCallback(() => {
+ setShowSkillsPicker(true);
+ }, []);
+ const handleSelectMemoryPicker = useCallback(() => {
+ setShowMemoryPicker(true);
+ }, []);
+ const handleRemoveHistory = useCallback((sessionId: string) => {
+ setSelectedHistorySessions((prev) =>
+ prev.filter((item) => item.sessionId !== sessionId),
+ );
+ }, []);
+ const handleRemoveNotebook = useCallback((notebookId: string) => {
+ setSelectedNotebookRecords((prev) =>
+ prev.filter((record) => record.notebookId !== notebookId),
+ );
+ }, []);
+ const handleRemoveBookReference = useCallback((bookId: string) => {
+ setSelectedBookReferences((prev) =>
+ prev.filter((record) => record.bookId !== bookId),
+ );
+ }, []);
+ const handleRemoveQuestion = useCallback((entryId: number) => {
+ setSelectedQuestionEntries((prev) =>
+ prev.filter((entry) => entry.id !== entryId),
+ );
+ }, []);
+ const handleToggleSkill = useCallback((name: string) => {
+ setSkillsAutoMode(false);
+ setSelectedSkills((prev) =>
+ prev.includes(name) ? prev.filter((n) => n !== name) : [...prev, name],
+ );
+ }, []);
+
+ const handleSetSkillsAuto = useCallback((auto: boolean) => {
+ setSkillsAutoMode(auto);
+ if (auto) setSelectedSkills([]);
+ }, []);
+
+ const handleToggleMemoryFile = useCallback((file: SpaceMemoryFile) => {
+ setSelectedMemoryFiles((prev) =>
+ prev.includes(file)
+ ? prev.filter((item) => item !== file)
+ : [...prev, file],
+ );
+ }, []);
+
+ const handleTogglePanelCollapsed = useCallback(() => {
+ setPanelCollapsed((prev) => !prev);
+ }, []);
+ const handleCloseNotebookPicker = useCallback(() => {
+ setShowNotebookPicker(false);
+ }, []);
+ const handleCloseBookPicker = useCallback(() => {
+ setShowBookPicker(false);
+ }, []);
+ const handleApplyBookReferences = useCallback(
+ (references: SelectedBookReference[]) => {
+ setSelectedBookReferences(references);
+ },
+ [],
+ );
+ const handleApplyNotebookRecords = useCallback(
+ (records: SelectedRecord[]) => {
+ setSelectedNotebookRecords(records);
+ },
+ [],
+ );
+ const handleCloseHistoryPicker = useCallback(() => {
+ setShowHistoryPicker(false);
+ }, []);
+ const handleApplyHistorySessions = useCallback(
+ (sessions: SelectedHistorySession[]) => {
+ setSelectedHistorySessions(sessions);
+ },
+ [],
+ );
+ const handleCloseQuestionBankPicker = useCallback(() => {
+ setShowQuestionBankPicker(false);
+ }, []);
+ const handleApplyQuestionEntries = useCallback(
+ (entries: SelectedQuestionEntry[]) => {
+ setSelectedQuestionEntries(entries);
+ },
+ [],
+ );
+ const handleCloseSkillsPicker = useCallback(() => {
+ setShowSkillsPicker(false);
+ }, []);
+ const handleApplySkillsSelection = useCallback(
+ (selection: { auto: boolean; skills: string[] }) => {
+ setSkillsAutoMode(selection.auto);
+ setSelectedSkills(selection.auto ? [] : selection.skills);
+ },
+ [],
+ );
+ const handleCloseMemoryPicker = useCallback(() => {
+ setShowMemoryPicker(false);
+ }, []);
+ const handleApplyMemoryFiles = useCallback((files: SpaceMemoryFile[]) => {
+ setSelectedMemoryFiles(files);
+ }, []);
+ const handleCloseSaveModal = useCallback(() => {
+ setShowSaveModal(false);
+ }, []);
+
+ const handleNewChat = useCallback(() => {
+ router.push("/chat");
+ }, [router]);
+
+ const handleDownloadMarkdown = useCallback(() => {
+ if (!state.messages.length) return;
+ const title =
+ state.messages
+ .find((msg) => msg.role === "user")
+ ?.content.trim()
+ .slice(0, 80) || "Chat Session";
+ downloadChatMarkdown(state.messages, { title });
+ }, [state.messages]);
+
+ return (
+ <div
+ // When the preview drawer is open AND the viewport is wide enough,
+ // push the chat content to the left by the drawer's width so the two
+ // panels live side-by-side (matches Claude desktop). On smaller
+ // screens the drawer overlays — squeezing a phone-width chat into
+ // the remaining ~30 px would be useless. The actual padding +
+ // transition lives in `chat-preview-shell` (globals.css) so we can
+ // hand-tune it without fighting Tailwind's arbitrary-value parser.
+ data-preview-open={previewSource ? "true" : "false"}
+ className="chat-preview-shell flex h-full flex-col overflow-hidden bg-[var(--background)]"
+ >
+ <div className="mx-auto flex w-full max-w-[960px] items-center justify-between px-6 pt-3 pb-0">
+ <span className="text-[15px] font-semibold tracking-[-0.01em] text-[var(--foreground)]">
+ {t(activeCap.label)}
+ </span>
+ <div className="flex items-center gap-2">
+ <button
+ onClick={() => setShowSaveModal(true)}
+ disabled={!chatSavePayload}
+ className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-[var(--border)]/50 disabled:hover:text-[var(--muted-foreground)]"
+ >
+ {t("Save to Notebook")}
+ </button>
+ <button
+ onClick={handleDownloadMarkdown}
+ disabled={!state.messages.length}
+ title={t("Download chat history as Markdown")}
+ className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:border-[var(--border)]/50 disabled:hover:text-[var(--muted-foreground)]"
+ >
+ {t("Download Markdown")}
+ </button>
+ <button
+ onClick={handleNewChat}
+ className="rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)]"
+ >
+ {t("New chat")}
+ </button>
+ </div>
+ </div>
+ <div className="mx-auto flex w-full max-w-[960px] flex-1 min-h-0 flex-col overflow-hidden px-6">
+ {!hasMessages ? (
+ <div className="flex flex-1 min-h-0 flex-col items-center justify-center animate-fade-in">
+ <div className="text-center">
+ <h1 className="font-serif text-[36px] font-medium tracking-[-0.01em] text-[var(--foreground)]">
+ {t("What would you like to learn?")}
+ </h1>
+ <p className="mt-4 text-[15px] text-[var(--muted-foreground)]">
+ {t("Ask anything — I'm here to help you understand.")}
+ </p>
+ </div>
+ </div>
+ ) : (
+ <div
+ ref={messagesContainerRef}
+ data-chat-scroll-root="true"
+ onScroll={handleMessagesScroll}
+ className={`mx-auto w-full flex-1 min-h-0 space-y-7 overflow-y-auto pr-4 [scrollbar-gutter:stable] ${hasMessages ? "pt-0" : "pt-2 pb-6"}`}
+ style={
+ hasMessages
+ ? (() => {
+ const maskImage =
+ "linear-gradient(to bottom, transparent 0px, #000 32px, #000 calc(100% - 40px), transparent 100%)";
+ return {
+ paddingBottom: "4px",
+ WebkitMaskImage: maskImage,
+ maskImage,
+ };
+ })()
+ : undefined
+ }
+ >
+ <ChatMessageList
+ messages={state.messages}
+ isStreaming={state.isStreaming}
+ sessionId={state.sessionId}
+ language={state.language}
+ onAnswerNow={handleAnswerNow}
+ onCopyAssistantMessage={copyAssistantMessage}
+ onRegenerateMessage={handleRegenerateMessage}
+ onConfirmOutline={handleConfirmOutline}
+ onPreviewAttachment={handlePreviewMessageAttachment}
+ />
+ <div ref={messagesEndRef} className="h-px w-full shrink-0" />
+ </div>
+ )}
+
+ <ChatComposer
+ composerRef={composerRef}
+ capMenuRef={capMenuRef}
+ capBtnRef={capBtnRef}
+ toolMenuRef={toolMenuRef}
+ toolBtnRef={toolBtnRef}
+ spaceMenuRef={spaceMenuRef}
+ spaceBtnRef={spaceBtnRef}
+ dragCounter={dragCounter}
+ dragging={dragging}
+ capMenuOpen={capMenuOpen}
+ toolMenuOpen={toolMenuOpen}
+ spaceMenuOpen={spaceMenuOpen}
+ hasMessages={hasMessages}
+ attachments={attachments}
+ attachmentError={attachmentError}
+ activeCap={activeCap}
+ visibleTools={visibleTools}
+ selectedTools={selectedTools}
+ ragActive={ragActive}
+ knowledgeBases={knowledgeBases}
++ llmOptions={llmOptions}
++ activeLLMDefault={activeLLMDefault}
++ llmSelection={state.llmSelection}
++ llmOptionsLoading={llmOptionsLoading}
++ llmOptionsError={llmOptionsError}
+ selectedBookReferences={selectedBookReferences}
+ selectedNotebookRecords={selectedNotebookRecords}
+ selectedHistorySessions={selectedHistorySessions}
+ selectedQuestionEntries={selectedQuestionEntries}
+ notebookReferenceGroups={notebookReferenceGroups}
+ selectedSkills={selectedSkills}
+ skillsAutoMode={skillsAutoMode}
+ selectedMemoryFiles={selectedMemoryFiles}
+ stateKnowledgeBase={state.knowledgeBases[0] || ""}
+ isStreaming={state.isStreaming}
+ isResearchMode={isResearchMode}
+ isQuizMode={isQuizMode}
+ isMathAnimatorMode={isMathAnimatorMode}
+ isVisualizeMode={isVisualizeMode}
+ quizConfig={quizConfig}
+ quizPdf={quizPdf}
+ mathAnimatorConfig={mathAnimatorConfig}
+ visualizeConfig={visualizeConfig}
+ researchConfig={researchConfig}
+ researchValidationErrors={researchValidation.errors}
+ panelCollapsed={panelCollapsed}
+ capabilities={CAPABILITIES}
+ researchSources={RESEARCH_SOURCES}
+ onSetCapMenuOpen={setCapMenuOpen}
+ onSetToolMenuOpen={setToolMenuOpen}
+ onSetSpaceMenuOpen={setSpaceMenuOpen}
+ onSetKB={handleSetKB}
++ onSelectLLM={setLLMSelection}
+ onSelectNotebookPicker={handleSelectNotebookPicker}
+ onSelectBookPicker={handleSelectBookPicker}
+ onSelectHistoryPicker={handleSelectHistoryPicker}
+ onSelectQuestionBankPicker={handleSelectQuestionBankPicker}
+ onSelectSkillsPicker={handleSelectSkillsPicker}
+ onSelectMemoryPicker={handleSelectMemoryPicker}
+ onToggleTool={toggleTool}
+ onToggleSkill={handleToggleSkill}
+ onSetSkillsAuto={handleSetSkillsAuto}
+ onToggleMemoryFile={handleToggleMemoryFile}
+ onToggleResearchSource={toggleResearchSource}
+ onSend={handleSend}
+ onRemoveAttachment={removeAttachment}
+ onPreviewAttachment={handlePreviewPendingAttachment}
+ onRemoveHistory={handleRemoveHistory}
+ onRemoveBookReference={handleRemoveBookReference}
+ onRemoveNotebook={handleRemoveNotebook}
+ onRemoveQuestion={handleRemoveQuestion}
+ onDragEnter={handleDragEnter}
+ onDragLeave={handleDragLeave}
+ onDragOver={handleDragOver}
+ onDrop={handleDrop}
+ onPaste={handlePaste}
+ onAddFiles={handleAddFiles}
+ onSelectCapability={handleSelectCapability}
+ onCancelStreaming={cancelStreamingTurn}
+ onChangeQuizConfig={setQuizConfig}
+ onUploadQuizPdf={setQuizPdf}
+ onChangeMathAnimatorConfig={setMathAnimatorConfig}
+ onChangeVisualizeConfig={setVisualizeConfig}
+ onChangeResearchConfig={setResearchConfig}
+ onTogglePanelCollapsed={handleTogglePanelCollapsed}
+ />
+ </div>
+ <NotebookRecordPicker
+ open={showNotebookPicker}
+ onClose={handleCloseNotebookPicker}
+ onApply={handleApplyNotebookRecords}
+ />
+ <BookReferencePicker
+ open={showBookPicker}
+ initialReferences={selectedBookReferences}
+ onClose={handleCloseBookPicker}
+ onApply={handleApplyBookReferences}
+ />
+ <HistorySessionPicker
+ open={showHistoryPicker}
+ onClose={handleCloseHistoryPicker}
+ onApply={handleApplyHistorySessions}
+ />
+ <QuestionBankPicker
+ open={showQuestionBankPicker}
+ onClose={handleCloseQuestionBankPicker}
+ onApply={handleApplyQuestionEntries}
+ />
+ <SkillsPicker
+ open={showSkillsPicker}
+ initialAuto={skillsAutoMode}
+ initialSkills={selectedSkills}
+ onClose={handleCloseSkillsPicker}
+ onApply={handleApplySkillsSelection}
+ />
+ <MemoryPicker
+ open={showMemoryPicker}
+ initialFiles={selectedMemoryFiles}
+ onClose={handleCloseMemoryPicker}
+ onApply={handleApplyMemoryFiles}
+ />
+ <SaveToNotebookModal
+ open={showSaveModal}
+ payload={chatSavePayload}
+ messages={chatSaveMessages}
+ onClose={handleCloseSaveModal}
+ />
+ <FilePreviewDrawer
+ open={previewSource !== null}
+ source={previewSource}
+ onClose={handleClosePreview}
+ />
+ </div>
+ );
+}
diff --cc web/components/chat/home/ChatMessages.tsx
index 116acb47e,cbb9b9a59..7e08b4b0b
--- a/web/components/chat/home/ChatMessages.tsx
+++ b/web/components/chat/home/ChatMessages.tsx
@@@ -1,846 -1,845 +1,845 @@@
-"use client";
-
-import dynamic from "next/dynamic";
-import { memo, useCallback, useMemo, useState } from "react";
-import {
- BookOpen,
- Brain,
- ClipboardList,
- Coins,
- Copy,
- MessageSquare,
- RefreshCcw,
- Wand2,
- X,
- Zap,
- type LucideIcon,
-} from "lucide-react";
-import { useTranslation } from "react-i18next";
-import type { SelectedHistorySession } from "@/components/chat/HistorySessionPicker";
-import type { SelectedQuestionEntry } from "@/components/chat/QuestionBankPicker";
-import AssistantResponse from "@/components/common/AssistantResponse";
-import type {
- MessageAttachment,
- MessageRequestSnapshot,
-} from "@/context/UnifiedChatContext";
-import { apiUrl } from "@/lib/api";
-import { docIconFor } from "@/lib/doc-attachments";
-import { extractMathAnimatorResult } from "@/lib/math-animator-types";
-import { extractQuizQuestions } from "@/lib/quiz-types";
-import { extractVisualizeResult } from "@/lib/visualize-types";
-import type { StreamEvent } from "@/lib/unified-ws";
-import { hasVisibleMarkdownContent } from "@/lib/markdown-display";
-import type { SelectedBookReference } from "@/lib/book-references";
-import type { SpaceMemoryFile } from "@/lib/space-items";
-import { CallTracePanel } from "./TracePanels";
-
-const MathAnimatorViewer = dynamic(
- () => import("@/components/math-animator/MathAnimatorViewer"),
- { ssr: false },
-);
-const QuizViewer = dynamic(() => import("@/components/quiz/QuizViewer"), {
- ssr: false,
-});
-const ResearchOutlineEditor = dynamic(
- () => import("@/components/research/ResearchOutlineEditor"),
- { ssr: false },
-);
-const VisualizationViewer = dynamic(
- () => import("@/components/visualize/VisualizationViewer"),
- { ssr: false },
-);
-
-interface ChatMessageItem {
- role: "user" | "assistant" | "system";
- content: string;
- capability?: string;
- events?: StreamEvent[];
- attachments?: MessageAttachment[];
- requestSnapshot?: MessageRequestSnapshot;
-}
-
-interface NotebookReferenceGroup {
- notebookId: string;
- notebookName: string;
- count: number;
-}
-
-// Returns the i18n key (and a sensible fallback) for the capability badge
-// shown above the user's message. Callers must run `t(...)` on the result.
-function getModeBadgeLabel(capability?: string | null): string {
- if (!capability || capability === "chat") return "Chat";
- if (capability === "deep_solve") return "Deep Solve";
- if (capability === "deep_question") return "Quiz Generation";
- if (capability === "deep_research") return "Deep Research";
- if (capability === "math_animator") return "Math Animator";
- if (capability === "visualize") return "Visualize";
- return capability;
-}
-
-function imageSrcForAttachment(attachment: MessageAttachment): string | null {
- if (attachment.url) {
- if (
- attachment.url.startsWith("http") ||
- attachment.url.startsWith("blob:") ||
- attachment.url.startsWith("data:")
- ) {
- return attachment.url;
- }
- return apiUrl(attachment.url);
- }
-
- const base64 = attachment.base64?.trim();
- if (!base64) return null;
- if (base64.startsWith("data:")) return base64;
- return `data:${attachment.mime_type || "image/png"};base64,${base64}`;
-}
-
-const AssistantMessage = memo(function AssistantMessage({
- msg,
- isStreaming,
- outlineStatus,
- sessionId,
- language,
- onConfirmOutline,
- onAnswerNow,
-}: {
- msg: { content: string; capability?: string; events?: StreamEvent[] };
- isStreaming?: boolean;
- outlineStatus?: "editing" | "researching" | "done";
- sessionId?: string | null;
- language?: string;
- onConfirmOutline?: (
- outline: Array<{ title: string; overview: string }>,
- topic: string,
- researchConfig?: Record<string, unknown> | null,
- ) => void;
- onAnswerNow?: () => void;
-}) {
- const events = useMemo(() => msg.events ?? [], [msg.events]);
- const hasCallTrace = useMemo(
- () => events.some((event) => Boolean(event.metadata?.call_id)),
- [events],
- );
- const resultEvent = useMemo(
- () => msg.events?.find((event) => event.type === "result") ?? null,
- [msg.events],
- );
-
- const outlinePreview = useMemo(() => {
- if (msg.capability !== "deep_research" || !resultEvent) return null;
- const meta = resultEvent.metadata as Record<string, unknown> | undefined;
- if (!meta?.outline_preview) return null;
- return {
- sub_topics: (meta.sub_topics ?? []) as Array<{
- title: string;
- overview: string;
- }>,
- topic: String(meta.topic ?? ""),
- research_config: (meta.research_config ?? null) as Record<
- string,
- unknown
- > | null,
- };
- }, [msg.capability, resultEvent]);
-
- const quizQuestions = useMemo(() => {
- if (msg.capability !== "deep_question" || !resultEvent) return null;
- return extractQuizQuestions(resultEvent.metadata);
- }, [msg.capability, resultEvent]);
-
- const mathAnimatorResult = useMemo(() => {
- if (msg.capability !== "math_animator" || !resultEvent) return null;
- return extractMathAnimatorResult(resultEvent.metadata);
- }, [msg.capability, resultEvent]);
-
- const visualizeResult = useMemo(() => {
- if (msg.capability !== "visualize" || !resultEvent) return null;
- return extractVisualizeResult(resultEvent.metadata);
- }, [msg.capability, resultEvent]);
-
- return (
- <>
- {hasCallTrace ? (
- <CallTracePanel events={events} isStreaming={isStreaming} />
- ) : null}
- {isStreaming && onAnswerNow ? (
- <AnswerNowRow onAnswerNow={onAnswerNow} />
- ) : null}
- {outlinePreview && outlinePreview.sub_topics.length > 0 ? (
- <ResearchOutlineEditor
- outline={outlinePreview.sub_topics}
- topic={outlinePreview.topic}
- onConfirm={(items) =>
- onConfirmOutline?.(
- items,
- outlinePreview.topic,
- outlinePreview.research_config,
- )
- }
- status={outlineStatus}
- />
- ) : mathAnimatorResult ? (
- <MathAnimatorViewer result={mathAnimatorResult} />
- ) : visualizeResult ? (
- <VisualizationViewer result={visualizeResult} />
- ) : quizQuestions && quizQuestions.length > 0 ? (
- <QuizViewer
- questions={quizQuestions}
- sessionId={sessionId}
- language={language}
- />
- ) : (
- <AssistantResponse content={msg.content} />
- )}
- </>
- );
-});
-
-AssistantMessage.displayName = "AssistantMessage";
-
-/**
- * Inline "Answer now" affordance shown alongside the active assistant turn.
- * Lives outside the trace panel so it is visible as soon as the turn starts
- * — i.e. even before any tool / reasoning trace has been emitted, which is
- * the common case for the very first user message.
- */
-const AnswerNowRow = memo(function AnswerNowRow({
- onAnswerNow,
-}: {
- onAnswerNow: () => void;
-}) {
- const { t } = useTranslation();
- // Local single-shot guard: once the user has fired "answer now" we lock
- // the button so a second click can't queue a duplicate cancel + restart
- // race against the in-flight synthesis turn. The next assistant turn
- // mounts a fresh ``AnswerNowRow`` with its own state, so this naturally
- // resets per turn without any external bookkeeping.
- const [triggered, setTriggered] = useState(false);
- const handleClick = useCallback(() => {
- if (triggered) return;
- setTriggered(true);
- onAnswerNow();
- }, [triggered, onAnswerNow]);
-
- return (
- <div className="mt-1.5 mb-3 flex items-center">
- <button
- type="button"
- onClick={handleClick}
- disabled={triggered}
- title={t("Skip reasoning and answer now")}
- aria-disabled={triggered}
- className="group inline-flex items-center gap-1.5 rounded-md border border-[var(--border)]/60 bg-[var(--card)]/60 px-2.5 py-1 text-[11.5px] font-medium text-[var(--muted-foreground)] shadow-sm transition-colors hover:border-[var(--primary)]/40 hover:bg-[var(--primary)]/5 hover:text-[var(--primary)] disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:border-[var(--border)]/60 disabled:hover:bg-[var(--card)]/60 disabled:hover:text-[var(--muted-foreground)]"
- >
- <Zap
- size={12}
- strokeWidth={1.8}
- className="shrink-0 transition-colors group-hover:text-[var(--primary)] group-disabled:group-hover:text-[var(--muted-foreground)]"
- />
- <span>{triggered ? t("Answering…") : t("Answer now")}</span>
- </button>
- </div>
- );
-});
-
-AnswerNowRow.displayName = "AnswerNowRow";
-
-function CostFooter({
- cost,
- tokens,
- calls,
-}: {
- cost: number;
- tokens: number;
- calls: number;
-}) {
- const { t } = useTranslation();
- const formatCost = (usd: number) => {
- if (usd < 0.01) return `$${usd.toFixed(4)}`;
- return `$${usd.toFixed(2)}`;
- };
- const formatTokens = (n: number) => {
- if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
- return String(n);
- };
- return (
- <div className="flex items-center gap-2 text-[10px] text-[var(--muted-foreground)]/40">
- <Coins size={10} strokeWidth={1.5} className="shrink-0" />
- <span>{formatCost(cost)}</span>
- <span className="opacity-40">·</span>
- <span>
- {formatTokens(tokens)} {t("tokens")}
- </span>
- <span className="opacity-40">·</span>
- <span>
- {calls} {t("calls")}
- </span>
- </div>
- );
-}
-
-function RoughActionButton({
- icon: Icon,
- label,
- onClick,
- disabled,
-}: {
- icon: LucideIcon;
- label: string;
- onClick: () => void;
- disabled?: boolean;
-}) {
- return (
- <button
- type="button"
- onClick={onClick}
- disabled={disabled}
- className="inline-flex items-center gap-1 px-0.5 py-0.5 text-[11px] text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-35"
- >
- <Icon size={11} strokeWidth={1.5} />
- <span>{label}</span>
- </button>
- );
-}
-
-const UserMessage = memo(function UserMessage({
- msg,
- index,
- onPreviewAttachment,
-}: {
- msg: ChatMessageItem;
- index: number;
- onPreviewAttachment?: (attachment: MessageAttachment) => void;
-}) {
- const { t } = useTranslation();
- if (msg.content.startsWith("[Quiz Performance]")) return null;
-
- return (
- <div key={`${msg.role}-${index}`} className="flex justify-end">
- <div className="max-w-[75%] space-y-1.5">
- <div className="flex justify-end pr-1">
- <span className="text-[10px] tracking-wide text-[var(--muted-foreground)]">
- {t(getModeBadgeLabel(msg.capability))}
- </span>
- </div>
- {msg.attachments?.some((a) => a.type === "image") && (
- <div className="flex flex-wrap justify-end gap-2">
- {msg.attachments
- .filter((a) => a.type === "image" && (a.base64 || a.url))
- .map((a, ai) => {
- const src = imageSrcForAttachment(a);
- if (!src) return null;
- return (
- <button
- key={`img-${ai}`}
- type="button"
- onClick={() => onPreviewAttachment?.(a)}
- title={a.filename || t("image")}
- className="overflow-hidden rounded-2xl border border-[var(--border)] transition-shadow hover:shadow-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--primary)]/40"
- >
- {/* eslint-disable-next-line @next/next/no-img-element */}
- <img
- src={src}
- alt={a.filename || t("image")}
- className="max-h-48 max-w-[280px] rounded-2xl object-contain"
- />
- </button>
- );
- })}
- </div>
- )}
- {msg.attachments?.some((a) => a.type !== "image") && (
- <div className="flex flex-wrap justify-end gap-2">
- {msg.attachments
- .filter((a) => a.type !== "image")
- .map((a, ai) => {
- const filename = a.filename || t("Attachment");
- const spec = docIconFor(filename);
- const Icon = spec.Icon;
- const cardClass =
- "flex h-14 w-[220px] items-center gap-2.5 rounded-xl border border-[var(--border)] bg-[var(--card)] px-2.5 text-left shadow-sm transition-colors hover:border-[var(--primary)]/40 hover:bg-[var(--muted)]/30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--primary)]/40";
- return (
- <button
- key={`doc-${ai}`}
- type="button"
- onClick={() => onPreviewAttachment?.(a)}
- title={filename}
- className={cardClass}
- >
- <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-[var(--muted)]/60">
- <Icon size={20} strokeWidth={1.5} className={spec.tint} />
- </div>
- <div className="min-w-0 flex-1 text-left">
- <div className="truncate text-[12px] font-medium text-[var(--foreground)]">
- {filename}
- </div>
- <div className="truncate text-[10px] uppercase tracking-wide text-[var(--muted-foreground)]">
- {spec.label}
- </div>
- </div>
- </button>
- );
- })}
- </div>
- )}
- <div className="rounded-2xl bg-[var(--secondary)] px-4 py-2.5 text-[14px] leading-relaxed text-[var(--foreground)] shadow-sm">
- {(() => {
- const snap = msg.requestSnapshot;
- const hasNotebook = Boolean(snap?.notebookReferences?.length);
- const hasBooks = Boolean(snap?.bookReferences?.length);
- const hasHistory = Boolean(snap?.historyReferences?.length);
- const hasQuestions = Boolean(
- snap?.questionNotebookReferences?.length,
- );
- const hasSkills = Boolean(snap?.skills?.length);
- const hasMemory = Boolean(snap?.memoryReferences?.length);
- if (
- !hasNotebook &&
- !hasBooks &&
- !hasHistory &&
- !hasQuestions &&
- !hasSkills &&
- !hasMemory
- )
- return null;
- return (
- <div className="mb-2 flex flex-wrap gap-1.5">
- {snap?.notebookReferences?.map((ref) => (
- <span
- key={ref.notebook_id}
- className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
- >
- <BookOpen size={11} strokeWidth={1.8} />
- {t("Notebook")} · {ref.record_ids.length} {t("records")}
- </span>
- ))}
- {snap?.bookReferences?.map((ref) => (
- <span
- key={ref.book_id}
- className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
- >
- <BookOpen size={11} strokeWidth={1.8} />
- {t("Book")} · {ref.page_ids.length} {t("chapters")}
- </span>
- ))}
- {snap?.historyReferences?.map((sid) => (
- <span
- key={sid}
- className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
- >
- <MessageSquare size={11} strokeWidth={1.8} />
- {t("Chat History")}
- </span>
- ))}
- {hasQuestions && (
- <span className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]">
- <ClipboardList size={11} strokeWidth={1.8} />
- {t("Question Bank")} ·{" "}
- {snap?.questionNotebookReferences?.length} {t("items")}
- </span>
- )}
- {snap?.skills?.map((skill) => (
- <span
- key={skill}
- className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
- >
- <Wand2 size={11} strokeWidth={1.8} />
- {skill === "auto" ? t("Skills Auto") : skill}
- </span>
- ))}
- {snap?.memoryReferences?.map((file) => (
- <span
- key={file}
- className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
- >
- <Brain size={11} strokeWidth={1.8} />
- {t("Memory")} ·{" "}
- {file === "summary" ? t("Summary") : t("Profile")}
- </span>
- ))}
- </div>
- );
- })()}
- <div className="whitespace-pre-wrap">{msg.content}</div>
- </div>
- </div>
- </div>
- );
-});
-
-UserMessage.displayName = "UserMessage";
-
-export const SpaceContextChips = memo(function SpaceContextChips({
- historySessions,
- bookReferences,
- notebookGroups,
- questionEntries,
- selectedSkills,
- skillsAutoMode,
- memoryFiles,
- onRemoveHistory,
- onRemoveBookReference,
- onRemoveNotebook,
- onRemoveQuestion,
- onRemoveSkill,
- onClearSkillsAuto,
- onRemoveMemoryFile,
-}: {
- historySessions: SelectedHistorySession[];
- bookReferences: SelectedBookReference[];
- notebookGroups: NotebookReferenceGroup[];
- questionEntries: SelectedQuestionEntry[];
- selectedSkills: string[];
- skillsAutoMode: boolean;
- memoryFiles: SpaceMemoryFile[];
- onRemoveHistory: (sessionId: string) => void;
- onRemoveBookReference: (bookId: string) => void;
- onRemoveNotebook: (notebookId: string) => void;
- onRemoveQuestion: (entryId: number) => void;
- onRemoveSkill: (skill: string) => void;
- onClearSkillsAuto: () => void;
- onRemoveMemoryFile: (file: SpaceMemoryFile) => void;
-}) {
- const { t } = useTranslation();
- if (
- historySessions.length === 0 &&
- bookReferences.length === 0 &&
- notebookGroups.length === 0 &&
- questionEntries.length === 0 &&
- selectedSkills.length === 0 &&
- !skillsAutoMode &&
- memoryFiles.length === 0
- )
- return null;
-
- return (
- <div className="mb-3 flex flex-wrap gap-2">
- {historySessions.map((session) => (
- <span
- key={session.sessionId}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-sky-200 bg-sky-50 px-3 py-1.5 text-[12px] text-sky-800 shadow-sm dark:border-sky-900/60 dark:bg-sky-950/30 dark:text-sky-200"
- >
- <MessageSquare size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Chat History")}</span>
- <span className="truncate text-sky-700/90 dark:text-sky-200/90">
- {session.title}
- </span>
- <button
- onClick={() => onRemoveHistory(session.sessionId)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- {bookReferences.map((book) => (
- <span
- key={book.bookId}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-teal-200 bg-teal-50 px-3 py-1.5 text-[12px] text-teal-800 shadow-sm dark:border-teal-900/60 dark:bg-teal-950/30 dark:text-teal-200"
- >
- <BookOpen size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Book")}</span>
- <span className="truncate text-teal-700/90 dark:text-teal-200/90">
- {book.bookTitle} ({book.pages.length})
- </span>
- <button
- onClick={() => onRemoveBookReference(book.bookId)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- {notebookGroups.map((group) => (
- <span
- key={group.notebookId}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-[var(--border)] bg-[var(--background)] px-3 py-1.5 text-[12px] text-[var(--foreground)] shadow-sm"
- >
- <BookOpen size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Notebook")}</span>
- <span className="truncate text-[var(--muted-foreground)]">
- {group.notebookName} ({group.count})
- </span>
- <button
- onClick={() => onRemoveNotebook(group.notebookId)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- {questionEntries.map((entry) => (
- <span
- key={entry.id}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-amber-200 bg-amber-50 px-3 py-1.5 text-[12px] text-amber-800 shadow-sm dark:border-amber-900/60 dark:bg-amber-950/30 dark:text-amber-200"
- >
- <ClipboardList size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Question Bank")}</span>
- <span className="truncate text-amber-700/90 dark:text-amber-200/90">
- {entry.question.length > 40
- ? `${entry.question.slice(0, 40)}…`
- : entry.question}
- </span>
- <button
- onClick={() => onRemoveQuestion(entry.id)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- {skillsAutoMode && (
- <span className="inline-flex max-w-full items-center gap-2 rounded-xl border border-violet-200 bg-violet-50 px-3 py-1.5 text-[12px] text-violet-800 shadow-sm dark:border-violet-900/60 dark:bg-violet-950/30 dark:text-violet-200">
- <Wand2 size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Skills")}</span>
- <span className="truncate text-violet-700/90 dark:text-violet-200/90">
- {t("Auto")}
- </span>
- <button
- onClick={onClearSkillsAuto}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- )}
- {selectedSkills.map((skill) => (
- <span
- key={skill}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-violet-200 bg-violet-50 px-3 py-1.5 text-[12px] text-violet-800 shadow-sm dark:border-violet-900/60 dark:bg-violet-950/30 dark:text-violet-200"
- >
- <Wand2 size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Skill")}</span>
- <span className="truncate text-violet-700/90 dark:text-violet-200/90">
- {skill}
- </span>
- <button
- onClick={() => onRemoveSkill(skill)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- {memoryFiles.map((file) => (
- <span
- key={file}
- className="inline-flex max-w-full items-center gap-2 rounded-xl border border-emerald-200 bg-emerald-50 px-3 py-1.5 text-[12px] text-emerald-800 shadow-sm dark:border-emerald-900/60 dark:bg-emerald-950/30 dark:text-emerald-200"
- >
- <Brain size={12} strokeWidth={1.8} className="shrink-0" />
- <span className="shrink-0 font-medium">{t("Memory")}</span>
- <span className="truncate text-emerald-700/90 dark:text-emerald-200/90">
- {file === "summary" ? t("Summary") : t("Profile")}
- </span>
- <button
- onClick={() => onRemoveMemoryFile(file)}
- className="shrink-0 opacity-60 transition hover:opacity-100"
- >
- <X size={12} />
- </button>
- </span>
- ))}
- </div>
- );
-});
-
-SpaceContextChips.displayName = "SpaceContextChips";
-
-export const ChatMessageList = memo(function ChatMessageList({
- messages,
- isStreaming,
- sessionId,
- language,
- onAnswerNow,
- onCopyAssistantMessage,
- onRegenerateMessage,
- onConfirmOutline,
- onPreviewAttachment,
-}: {
- messages: ChatMessageItem[];
- isStreaming: boolean;
- sessionId?: string | null;
- language?: string;
- onAnswerNow: (
- snapshot?: MessageRequestSnapshot,
- assistantMsg?: { content: string; events?: StreamEvent[] },
- ) => void;
- onCopyAssistantMessage: (content: string) => void | Promise<void>;
- onRegenerateMessage: () => void;
- onConfirmOutline?: (
- outline: Array<{ title: string; overview: string }>,
- topic: string,
- researchConfig?: Record<string, unknown> | null,
- ) => void;
- onPreviewAttachment?: (attachment: MessageAttachment) => void;
-}) {
- const { t } = useTranslation();
- const outlineStatusByIndex = useMemo(() => {
- const map = new Map<number, "editing" | "researching" | "done">();
- for (let i = 0; i < messages.length; i++) {
- const msg = messages[i];
- if (msg.role !== "assistant" || msg.capability !== "deep_research")
- continue;
- const resultEv = msg.events?.find((e) => e.type === "result");
- const meta = resultEv?.metadata as Record<string, unknown> | undefined;
- if (!meta?.outline_preview) continue;
- const hasFollowup = messages
- .slice(i + 1)
- .some(
- (m) => m.role === "assistant" && m.capability === "deep_research",
- );
- if (hasFollowup) {
- const followup = messages
- .slice(i + 1)
- .find(
- (m) => m.role === "assistant" && m.capability === "deep_research",
- );
- const followupResult = followup?.events?.find(
- (e) => e.type === "result",
- );
- map.set(i, followupResult ? "done" : "researching");
- } else if (isStreaming) {
- map.set(i, "researching");
- } else {
- map.set(i, "editing");
- }
- }
- return map;
- }, [messages, isStreaming]);
-
- const messageRows = useMemo(() => {
- // System messages are backend grounding (e.g. quiz follow-up context) and
- // must never be rendered as a chat bubble. Filter them out defensively in
- // addition to the hydration-time filter in UnifiedChatContext.
- return messages
- .map((msg, index) => ({ msg, originalIndex: index }))
- .filter(({ msg }) => msg.role !== "system")
- .map(({ msg, originalIndex }) => {
- if (msg.role === "user") {
- return {
- msg,
- originalIndex,
- pairedUserMessage: null as ChatMessageItem | null,
- };
- }
- const pairedUserMessage =
- [...messages.slice(0, originalIndex)]
- .reverse()
- .find((previous) => previous.role === "user") ?? null;
- return { msg, originalIndex, pairedUserMessage };
- });
- }, [messages]);
-
- const lastAssistantIndex = useMemo(() => {
- for (let idx = messages.length - 1; idx >= 0; idx -= 1) {
- if (messages[idx].role === "assistant") return idx;
- }
- return -1;
- }, [messages]);
-
- return (
- <>
- {messageRows.map(({ msg, originalIndex, pairedUserMessage }) => {
- const i = originalIndex;
- if (msg.role === "user") {
- return (
- <UserMessage
- key={`${msg.role}-${i}`}
- msg={msg}
- index={i}
- onPreviewAttachment={onPreviewAttachment}
- />
- );
- }
-
- const isActiveAssistant = isStreaming && i === messages.length - 1;
- const msgDone = !isActiveAssistant;
- const showActions = msgDone && hasVisibleMarkdownContent(msg.content);
- const isLastAssistant = i === lastAssistantIndex;
- const showRegenerate =
- showActions &&
- !isStreaming &&
- isLastAssistant &&
- Boolean(pairedUserMessage) &&
- (!pairedUserMessage?.capability ||
- pairedUserMessage?.capability === "chat");
-
- // The "Answer now" affordance lives inside the trace panel for the
- // currently-streaming assistant turn. We hand the panel a thin
- // closure so it does not need to know about MessageRequestSnapshot.
- const handleTraceAnswerNow =
- isActiveAssistant && pairedUserMessage?.requestSnapshot
- ? () =>
- onAnswerNow(pairedUserMessage.requestSnapshot, {
- content: msg.content,
- events: msg.events,
- })
- : undefined;
-
- const costSummary = (() => {
- if (!msgDone) return null;
- const resultEv = msg.events?.find((e) => e.type === "result");
- if (!resultEv) return null;
- const meta = resultEv.metadata?.metadata as
- | Record<string, unknown>
- | undefined;
- const cs = meta?.cost_summary as
- | {
- total_cost_usd?: number;
- total_tokens?: number;
- total_calls?: number;
- }
- | undefined;
- if (!cs || !cs.total_calls) return null;
- return cs;
- })();
-
- return (
- <div key={`${msg.role}-${i}`} className="w-full">
- <AssistantMessage
- msg={msg}
- isStreaming={isActiveAssistant}
- outlineStatus={outlineStatusByIndex.get(i)}
- sessionId={sessionId}
- language={language}
- onConfirmOutline={onConfirmOutline}
- onAnswerNow={handleTraceAnswerNow}
- />
- {(showActions || costSummary) && (
- <div className="mt-2 flex items-center">
- {showActions && (
- <div className="flex gap-2">
- <RoughActionButton
- icon={Copy}
- label={t("Copy")}
- onClick={() => void onCopyAssistantMessage(msg.content)}
- />
- {showRegenerate && (
- <RoughActionButton
- icon={RefreshCcw}
- label={t("Regenerate")}
- onClick={() => onRegenerateMessage()}
- />
- )}
- </div>
- )}
- {costSummary && (
- <div className="ml-auto">
- <CostFooter
- cost={costSummary.total_cost_usd ?? 0}
- tokens={costSummary.total_tokens ?? 0}
- calls={costSummary.total_calls ?? 0}
- />
- </div>
- )}
- </div>
- )}
- </div>
- );
- })}
- </>
- );
-});
-
-ChatMessageList.displayName = "ChatMessageList";
+"use client";
+
+import dynamic from "next/dynamic";
+import { memo, useCallback, useMemo, useState } from "react";
+import {
+ BookOpen,
+ Brain,
+ ClipboardList,
+ Coins,
+ Copy,
+ MessageSquare,
+ RefreshCcw,
+ Wand2,
+ X,
+ Zap,
+ type LucideIcon,
+} from "lucide-react";
+import { useTranslation } from "react-i18next";
+import type { SelectedHistorySession } from "@/components/chat/HistorySessionPicker";
+import type { SelectedQuestionEntry } from "@/components/chat/QuestionBankPicker";
+import AssistantResponse from "@/components/common/AssistantResponse";
+import type {
+ MessageAttachment,
+ MessageRequestSnapshot,
+} from "@/context/UnifiedChatContext";
+import { apiUrl } from "@/lib/api";
+import { docIconFor } from "@/lib/doc-attachments";
+import { extractMathAnimatorResult } from "@/lib/math-animator-types";
+import { extractQuizQuestions } from "@/lib/quiz-types";
+import { extractVisualizeResult } from "@/lib/visualize-types";
+import type { StreamEvent } from "@/lib/unified-ws";
+import { hasVisibleMarkdownContent } from "@/lib/markdown-display";
+import type { SelectedBookReference } from "@/lib/book-references";
+import type { SpaceMemoryFile } from "@/lib/space-items";
+import { CallTracePanel } from "./TracePanels";
+
+const MathAnimatorViewer = dynamic(
+ () => import("@/components/math-animator/MathAnimatorViewer"),
+ { ssr: false },
+);
+const QuizViewer = dynamic(() => import("@/components/quiz/QuizViewer"), {
+ ssr: false,
+});
+const ResearchOutlineEditor = dynamic(
+ () => import("@/components/research/ResearchOutlineEditor"),
+ { ssr: false },
+);
+const VisualizationViewer = dynamic(
+ () => import("@/components/visualize/VisualizationViewer"),
+ { ssr: false },
+);
+
+interface ChatMessageItem {
+ role: "user" | "assistant" | "system";
+ content: string;
+ capability?: string;
+ events?: StreamEvent[];
+ attachments?: MessageAttachment[];
+ requestSnapshot?: MessageRequestSnapshot;
+}
+
+interface NotebookReferenceGroup {
+ notebookId: string;
+ notebookName: string;
+ count: number;
+}
+
+// Returns the i18n key (and a sensible fallback) for the capability badge
+// shown above the user's message. Callers must run `t(...)` on the result.
+function getModeBadgeLabel(capability?: string | null): string {
+ if (!capability || capability === "chat") return "Chat";
+ if (capability === "deep_solve") return "Deep Solve";
+ if (capability === "deep_question") return "Quiz Generation";
+ if (capability === "deep_research") return "Deep Research";
+ if (capability === "math_animator") return "Math Animator";
- if (capability === "visualize") return "Visualize";
- if (capability === "guided_learning") return "Guided Learning";
- return capability;
++ if (capability === "visualize") return "Visualize";
++ return capability;
+}
+
+function imageSrcForAttachment(attachment: MessageAttachment): string | null {
+ if (attachment.url) {
+ if (
+ attachment.url.startsWith("http") ||
+ attachment.url.startsWith("blob:") ||
+ attachment.url.startsWith("data:")
+ ) {
+ return attachment.url;
+ }
+ return apiUrl(attachment.url);
+ }
+
+ const base64 = attachment.base64?.trim();
+ if (!base64) return null;
+ if (base64.startsWith("data:")) return base64;
+ return `data:${attachment.mime_type || "image/png"};base64,${base64}`;
+}
+
+const AssistantMessage = memo(function AssistantMessage({
+ msg,
+ isStreaming,
+ outlineStatus,
+ sessionId,
+ language,
+ onConfirmOutline,
+ onAnswerNow,
+}: {
+ msg: { content: string; capability?: string; events?: StreamEvent[] };
+ isStreaming?: boolean;
+ outlineStatus?: "editing" | "researching" | "done";
+ sessionId?: string | null;
+ language?: string;
+ onConfirmOutline?: (
+ outline: Array<{ title: string; overview: string }>,
+ topic: string,
+ researchConfig?: Record<string, unknown> | null,
+ ) => void;
+ onAnswerNow?: () => void;
+}) {
+ const events = useMemo(() => msg.events ?? [], [msg.events]);
+ const hasCallTrace = useMemo(
+ () => events.some((event) => Boolean(event.metadata?.call_id)),
+ [events],
+ );
+ const resultEvent = useMemo(
+ () => msg.events?.find((event) => event.type === "result") ?? null,
+ [msg.events],
+ );
+
+ const outlinePreview = useMemo(() => {
+ if (msg.capability !== "deep_research" || !resultEvent) return null;
+ const meta = resultEvent.metadata as Record<string, unknown> | undefined;
+ if (!meta?.outline_preview) return null;
+ return {
+ sub_topics: (meta.sub_topics ?? []) as Array<{
+ title: string;
+ overview: string;
+ }>,
+ topic: String(meta.topic ?? ""),
+ research_config: (meta.research_config ?? null) as Record<
+ string,
+ unknown
+ > | null,
+ };
+ }, [msg.capability, resultEvent]);
+
+ const quizQuestions = useMemo(() => {
+ if (msg.capability !== "deep_question" || !resultEvent) return null;
+ return extractQuizQuestions(resultEvent.metadata);
+ }, [msg.capability, resultEvent]);
+
+ const mathAnimatorResult = useMemo(() => {
+ if (msg.capability !== "math_animator" || !resultEvent) return null;
+ return extractMathAnimatorResult(resultEvent.metadata);
+ }, [msg.capability, resultEvent]);
+
+ const visualizeResult = useMemo(() => {
+ if (msg.capability !== "visualize" || !resultEvent) return null;
+ return extractVisualizeResult(resultEvent.metadata);
+ }, [msg.capability, resultEvent]);
+
+ return (
+ <>
+ {hasCallTrace ? (
+ <CallTracePanel events={events} isStreaming={isStreaming} />
+ ) : null}
+ {isStreaming && onAnswerNow ? (
+ <AnswerNowRow onAnswerNow={onAnswerNow} />
+ ) : null}
+ {outlinePreview && outlinePreview.sub_topics.length > 0 ? (
+ <ResearchOutlineEditor
+ outline={outlinePreview.sub_topics}
+ topic={outlinePreview.topic}
+ onConfirm={(items) =>
+ onConfirmOutline?.(
+ items,
+ outlinePreview.topic,
+ outlinePreview.research_config,
+ )
+ }
+ status={outlineStatus}
+ />
+ ) : mathAnimatorResult ? (
+ <MathAnimatorViewer result={mathAnimatorResult} />
+ ) : visualizeResult ? (
+ <VisualizationViewer result={visualizeResult} />
+ ) : quizQuestions && quizQuestions.length > 0 ? (
+ <QuizViewer
+ questions={quizQuestions}
+ sessionId={sessionId}
+ language={language}
+ />
+ ) : (
+ <AssistantResponse content={msg.content} />
+ )}
+ </>
+ );
+});
+
+AssistantMessage.displayName = "AssistantMessage";
+
+/**
+ * Inline "Answer now" affordance shown alongside the active assistant turn.
+ * Lives outside the trace panel so it is visible as soon as the turn starts
+ * — i.e. even before any tool / reasoning trace has been emitted, which is
+ * the common case for the very first user message.
+ */
+const AnswerNowRow = memo(function AnswerNowRow({
+ onAnswerNow,
+}: {
+ onAnswerNow: () => void;
+}) {
+ const { t } = useTranslation();
+ // Local single-shot guard: once the user has fired "answer now" we lock
+ // the button so a second click can't queue a duplicate cancel + restart
+ // race against the in-flight synthesis turn. The next assistant turn
+ // mounts a fresh ``AnswerNowRow`` with its own state, so this naturally
+ // resets per turn without any external bookkeeping.
+ const [triggered, setTriggered] = useState(false);
+ const handleClick = useCallback(() => {
+ if (triggered) return;
+ setTriggered(true);
+ onAnswerNow();
+ }, [triggered, onAnswerNow]);
+
+ return (
+ <div className="mt-1.5 mb-3 flex items-center">
+ <button
+ type="button"
+ onClick={handleClick}
+ disabled={triggered}
+ title={t("Skip reasoning and answer now")}
+ aria-disabled={triggered}
+ className="group inline-flex items-center gap-1.5 rounded-md border border-[var(--border)]/60 bg-[var(--card)]/60 px-2.5 py-1 text-[11.5px] font-medium text-[var(--muted-foreground)] shadow-sm transition-colors hover:border-[var(--primary)]/40 hover:bg-[var(--primary)]/5 hover:text-[var(--primary)] disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:border-[var(--border)]/60 disabled:hover:bg-[var(--card)]/60 disabled:hover:text-[var(--muted-foreground)]"
+ >
+ <Zap
+ size={12}
+ strokeWidth={1.8}
+ className="shrink-0 transition-colors group-hover:text-[var(--primary)] group-disabled:group-hover:text-[var(--muted-foreground)]"
+ />
+ <span>{triggered ? t("Answering…") : t("Answer now")}</span>
+ </button>
+ </div>
+ );
+});
+
+AnswerNowRow.displayName = "AnswerNowRow";
+
+function CostFooter({
+ cost,
+ tokens,
+ calls,
+}: {
+ cost: number;
+ tokens: number;
+ calls: number;
+}) {
+ const { t } = useTranslation();
+ const formatCost = (usd: number) => {
+ if (usd < 0.01) return `$${usd.toFixed(4)}`;
+ return `$${usd.toFixed(2)}`;
+ };
+ const formatTokens = (n: number) => {
+ if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
+ return String(n);
+ };
+ return (
+ <div className="flex items-center gap-2 text-[10px] text-[var(--muted-foreground)]/40">
+ <Coins size={10} strokeWidth={1.5} className="shrink-0" />
+ <span>{formatCost(cost)}</span>
+ <span className="opacity-40">·</span>
+ <span>
+ {formatTokens(tokens)} {t("tokens")}
+ </span>
+ <span className="opacity-40">·</span>
+ <span>
+ {calls} {t("calls")}
+ </span>
+ </div>
+ );
+}
+
+function RoughActionButton({
+ icon: Icon,
+ label,
+ onClick,
+ disabled,
+}: {
+ icon: LucideIcon;
+ label: string;
+ onClick: () => void;
+ disabled?: boolean;
+}) {
+ return (
+ <button
+ type="button"
+ onClick={onClick}
+ disabled={disabled}
+ className="inline-flex items-center gap-1 px-0.5 py-0.5 text-[11px] text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)] disabled:cursor-not-allowed disabled:opacity-35"
+ >
+ <Icon size={11} strokeWidth={1.5} />
+ <span>{label}</span>
+ </button>
+ );
+}
+
+const UserMessage = memo(function UserMessage({
+ msg,
+ index,
+ onPreviewAttachment,
+}: {
+ msg: ChatMessageItem;
+ index: number;
+ onPreviewAttachment?: (attachment: MessageAttachment) => void;
+}) {
+ const { t } = useTranslation();
+ if (msg.content.startsWith("[Quiz Performance]")) return null;
+
+ return (
+ <div key={`${msg.role}-${index}`} className="flex justify-end">
+ <div className="max-w-[75%] space-y-1.5">
+ <div className="flex justify-end pr-1">
+ <span className="text-[10px] tracking-wide text-[var(--muted-foreground)]">
+ {t(getModeBadgeLabel(msg.capability))}
+ </span>
+ </div>
+ {msg.attachments?.some((a) => a.type === "image") && (
+ <div className="flex flex-wrap justify-end gap-2">
+ {msg.attachments
+ .filter((a) => a.type === "image" && (a.base64 || a.url))
+ .map((a, ai) => {
+ const src = imageSrcForAttachment(a);
+ if (!src) return null;
+ return (
+ <button
+ key={`img-${ai}`}
+ type="button"
+ onClick={() => onPreviewAttachment?.(a)}
+ title={a.filename || t("image")}
+ className="overflow-hidden rounded-2xl border border-[var(--border)] transition-shadow hover:shadow-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--primary)]/40"
+ >
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img
+ src={src}
+ alt={a.filename || t("image")}
+ className="max-h-48 max-w-[280px] rounded-2xl object-contain"
+ />
+ </button>
+ );
+ })}
+ </div>
+ )}
+ {msg.attachments?.some((a) => a.type !== "image") && (
+ <div className="flex flex-wrap justify-end gap-2">
+ {msg.attachments
+ .filter((a) => a.type !== "image")
+ .map((a, ai) => {
+ const filename = a.filename || t("Attachment");
+ const spec = docIconFor(filename);
+ const Icon = spec.Icon;
+ const cardClass =
+ "flex h-14 w-[220px] items-center gap-2.5 rounded-xl border border-[var(--border)] bg-[var(--card)] px-2.5 text-left shadow-sm transition-colors hover:border-[var(--primary)]/40 hover:bg-[var(--muted)]/30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--primary)]/40";
+ return (
+ <button
+ key={`doc-${ai}`}
+ type="button"
+ onClick={() => onPreviewAttachment?.(a)}
+ title={filename}
+ className={cardClass}
+ >
+ <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-[var(--muted)]/60">
+ <Icon size={20} strokeWidth={1.5} className={spec.tint} />
+ </div>
+ <div className="min-w-0 flex-1 text-left">
+ <div className="truncate text-[12px] font-medium text-[var(--foreground)]">
+ {filename}
+ </div>
+ <div className="truncate text-[10px] uppercase tracking-wide text-[var(--muted-foreground)]">
+ {spec.label}
+ </div>
+ </div>
+ </button>
+ );
+ })}
+ </div>
+ )}
+ <div className="rounded-2xl bg-[var(--secondary)] px-4 py-2.5 text-[14px] leading-relaxed text-[var(--foreground)] shadow-sm">
+ {(() => {
+ const snap = msg.requestSnapshot;
+ const hasNotebook = Boolean(snap?.notebookReferences?.length);
+ const hasBooks = Boolean(snap?.bookReferences?.length);
+ const hasHistory = Boolean(snap?.historyReferences?.length);
+ const hasQuestions = Boolean(
+ snap?.questionNotebookReferences?.length,
+ );
+ const hasSkills = Boolean(snap?.skills?.length);
+ const hasMemory = Boolean(snap?.memoryReferences?.length);
+ if (
+ !hasNotebook &&
+ !hasBooks &&
+ !hasHistory &&
+ !hasQuestions &&
+ !hasSkills &&
+ !hasMemory
+ )
+ return null;
+ return (
+ <div className="mb-2 flex flex-wrap gap-1.5">
+ {snap?.notebookReferences?.map((ref) => (
+ <span
+ key={ref.notebook_id}
+ className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
+ >
+ <BookOpen size={11} strokeWidth={1.8} />
+ {t("Notebook")} · {ref.record_ids.length} {t("records")}
+ </span>
+ ))}
+ {snap?.bookReferences?.map((ref) => (
+ <span
+ key={ref.book_id}
+ className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
+ >
+ <BookOpen size={11} strokeWidth={1.8} />
+ {t("Book")} · {ref.page_ids.length} {t("chapters")}
+ </span>
+ ))}
+ {snap?.historyReferences?.map((sid) => (
+ <span
+ key={sid}
+ className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
+ >
+ <MessageSquare size={11} strokeWidth={1.8} />
+ {t("Chat History")}
+ </span>
+ ))}
+ {hasQuestions && (
+ <span className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]">
+ <ClipboardList size={11} strokeWidth={1.8} />
+ {t("Question Bank")} ·{" "}
+ {snap?.questionNotebookReferences?.length} {t("items")}
+ </span>
+ )}
+ {snap?.skills?.map((skill) => (
+ <span
+ key={skill}
+ className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
+ >
+ <Wand2 size={11} strokeWidth={1.8} />
+ {skill === "auto" ? t("Skills Auto") : skill}
+ </span>
+ ))}
+ {snap?.memoryReferences?.map((file) => (
+ <span
+ key={file}
+ className="inline-flex items-center gap-1.5 rounded-md border border-[var(--border)] bg-[var(--background)]/60 px-2 py-1 text-[11px] font-medium text-[var(--muted-foreground)]"
+ >
+ <Brain size={11} strokeWidth={1.8} />
+ {t("Memory")} ·{" "}
+ {file === "summary" ? t("Summary") : t("Profile")}
+ </span>
+ ))}
+ </div>
+ );
+ })()}
- <div>{msg.content}</div>
++ <div className="whitespace-pre-wrap">{msg.content}</div>
+ </div>
+ </div>
+ </div>
+ );
+});
+
+UserMessage.displayName = "UserMessage";
+
+export const SpaceContextChips = memo(function SpaceContextChips({
+ historySessions,
+ bookReferences,
+ notebookGroups,
+ questionEntries,
+ selectedSkills,
+ skillsAutoMode,
+ memoryFiles,
+ onRemoveHistory,
+ onRemoveBookReference,
+ onRemoveNotebook,
+ onRemoveQuestion,
+ onRemoveSkill,
+ onClearSkillsAuto,
+ onRemoveMemoryFile,
+}: {
+ historySessions: SelectedHistorySession[];
+ bookReferences: SelectedBookReference[];
+ notebookGroups: NotebookReferenceGroup[];
+ questionEntries: SelectedQuestionEntry[];
+ selectedSkills: string[];
+ skillsAutoMode: boolean;
+ memoryFiles: SpaceMemoryFile[];
+ onRemoveHistory: (sessionId: string) => void;
+ onRemoveBookReference: (bookId: string) => void;
+ onRemoveNotebook: (notebookId: string) => void;
+ onRemoveQuestion: (entryId: number) => void;
+ onRemoveSkill: (skill: string) => void;
+ onClearSkillsAuto: () => void;
+ onRemoveMemoryFile: (file: SpaceMemoryFile) => void;
+}) {
+ const { t } = useTranslation();
+ if (
+ historySessions.length === 0 &&
+ bookReferences.length === 0 &&
+ notebookGroups.length === 0 &&
+ questionEntries.length === 0 &&
+ selectedSkills.length === 0 &&
+ !skillsAutoMode &&
+ memoryFiles.length === 0
+ )
+ return null;
+
+ return (
+ <div className="mb-3 flex flex-wrap gap-2">
+ {historySessions.map((session) => (
+ <span
+ key={session.sessionId}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-sky-200 bg-sky-50 px-3 py-1.5 text-[12px] text-sky-800 shadow-sm dark:border-sky-900/60 dark:bg-sky-950/30 dark:text-sky-200"
+ >
+ <MessageSquare size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Chat History")}</span>
+ <span className="truncate text-sky-700/90 dark:text-sky-200/90">
+ {session.title}
+ </span>
+ <button
+ onClick={() => onRemoveHistory(session.sessionId)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ {bookReferences.map((book) => (
+ <span
+ key={book.bookId}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-teal-200 bg-teal-50 px-3 py-1.5 text-[12px] text-teal-800 shadow-sm dark:border-teal-900/60 dark:bg-teal-950/30 dark:text-teal-200"
+ >
+ <BookOpen size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Book")}</span>
+ <span className="truncate text-teal-700/90 dark:text-teal-200/90">
+ {book.bookTitle} ({book.pages.length})
+ </span>
+ <button
+ onClick={() => onRemoveBookReference(book.bookId)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ {notebookGroups.map((group) => (
+ <span
+ key={group.notebookId}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-[var(--border)] bg-[var(--background)] px-3 py-1.5 text-[12px] text-[var(--foreground)] shadow-sm"
+ >
+ <BookOpen size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Notebook")}</span>
+ <span className="truncate text-[var(--muted-foreground)]">
+ {group.notebookName} ({group.count})
+ </span>
+ <button
+ onClick={() => onRemoveNotebook(group.notebookId)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ {questionEntries.map((entry) => (
+ <span
+ key={entry.id}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-amber-200 bg-amber-50 px-3 py-1.5 text-[12px] text-amber-800 shadow-sm dark:border-amber-900/60 dark:bg-amber-950/30 dark:text-amber-200"
+ >
+ <ClipboardList size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Question Bank")}</span>
+ <span className="truncate text-amber-700/90 dark:text-amber-200/90">
+ {entry.question.length > 40
+ ? `${entry.question.slice(0, 40)}…`
+ : entry.question}
+ </span>
+ <button
+ onClick={() => onRemoveQuestion(entry.id)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ {skillsAutoMode && (
+ <span className="inline-flex max-w-full items-center gap-2 rounded-xl border border-violet-200 bg-violet-50 px-3 py-1.5 text-[12px] text-violet-800 shadow-sm dark:border-violet-900/60 dark:bg-violet-950/30 dark:text-violet-200">
+ <Wand2 size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Skills")}</span>
+ <span className="truncate text-violet-700/90 dark:text-violet-200/90">
+ {t("Auto")}
+ </span>
+ <button
+ onClick={onClearSkillsAuto}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ )}
+ {selectedSkills.map((skill) => (
+ <span
+ key={skill}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-violet-200 bg-violet-50 px-3 py-1.5 text-[12px] text-violet-800 shadow-sm dark:border-violet-900/60 dark:bg-violet-950/30 dark:text-violet-200"
+ >
+ <Wand2 size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Skill")}</span>
+ <span className="truncate text-violet-700/90 dark:text-violet-200/90">
+ {skill}
+ </span>
+ <button
+ onClick={() => onRemoveSkill(skill)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ {memoryFiles.map((file) => (
+ <span
+ key={file}
+ className="inline-flex max-w-full items-center gap-2 rounded-xl border border-emerald-200 bg-emerald-50 px-3 py-1.5 text-[12px] text-emerald-800 shadow-sm dark:border-emerald-900/60 dark:bg-emerald-950/30 dark:text-emerald-200"
+ >
+ <Brain size={12} strokeWidth={1.8} className="shrink-0" />
+ <span className="shrink-0 font-medium">{t("Memory")}</span>
+ <span className="truncate text-emerald-700/90 dark:text-emerald-200/90">
+ {file === "summary" ? t("Summary") : t("Profile")}
+ </span>
+ <button
+ onClick={() => onRemoveMemoryFile(file)}
+ className="shrink-0 opacity-60 transition hover:opacity-100"
+ >
+ <X size={12} />
+ </button>
+ </span>
+ ))}
+ </div>
+ );
+});
+
+SpaceContextChips.displayName = "SpaceContextChips";
+
+export const ChatMessageList = memo(function ChatMessageList({
+ messages,
+ isStreaming,
+ sessionId,
+ language,
+ onAnswerNow,
+ onCopyAssistantMessage,
+ onRegenerateMessage,
+ onConfirmOutline,
+ onPreviewAttachment,
+}: {
+ messages: ChatMessageItem[];
+ isStreaming: boolean;
+ sessionId?: string | null;
+ language?: string;
+ onAnswerNow: (
+ snapshot?: MessageRequestSnapshot,
+ assistantMsg?: { content: string; events?: StreamEvent[] },
+ ) => void;
+ onCopyAssistantMessage: (content: string) => void | Promise<void>;
+ onRegenerateMessage: () => void;
+ onConfirmOutline?: (
+ outline: Array<{ title: string; overview: string }>,
+ topic: string,
+ researchConfig?: Record<string, unknown> | null,
+ ) => void;
+ onPreviewAttachment?: (attachment: MessageAttachment) => void;
+}) {
+ const { t } = useTranslation();
+ const outlineStatusByIndex = useMemo(() => {
+ const map = new Map<number, "editing" | "researching" | "done">();
+ for (let i = 0; i < messages.length; i++) {
+ const msg = messages[i];
+ if (msg.role !== "assistant" || msg.capability !== "deep_research")
+ continue;
+ const resultEv = msg.events?.find((e) => e.type === "result");
+ const meta = resultEv?.metadata as Record<string, unknown> | undefined;
+ if (!meta?.outline_preview) continue;
+ const hasFollowup = messages
+ .slice(i + 1)
+ .some(
+ (m) => m.role === "assistant" && m.capability === "deep_research",
+ );
+ if (hasFollowup) {
+ const followup = messages
+ .slice(i + 1)
+ .find(
+ (m) => m.role === "assistant" && m.capability === "deep_research",
+ );
+ const followupResult = followup?.events?.find(
+ (e) => e.type === "result",
+ );
+ map.set(i, followupResult ? "done" : "researching");
+ } else if (isStreaming) {
+ map.set(i, "researching");
+ } else {
+ map.set(i, "editing");
+ }
+ }
+ return map;
+ }, [messages, isStreaming]);
+
+ const messageRows = useMemo(() => {
+ // System messages are backend grounding (e.g. quiz follow-up context) and
+ // must never be rendered as a chat bubble. Filter them out defensively in
+ // addition to the hydration-time filter in UnifiedChatContext.
+ return messages
+ .map((msg, index) => ({ msg, originalIndex: index }))
+ .filter(({ msg }) => msg.role !== "system")
+ .map(({ msg, originalIndex }) => {
+ if (msg.role === "user") {
+ return {
+ msg,
+ originalIndex,
+ pairedUserMessage: null as ChatMessageItem | null,
+ };
+ }
+ const pairedUserMessage =
+ [...messages.slice(0, originalIndex)]
+ .reverse()
+ .find((previous) => previous.role === "user") ?? null;
+ return { msg, originalIndex, pairedUserMessage };
+ });
+ }, [messages]);
+
+ const lastAssistantIndex = useMemo(() => {
+ for (let idx = messages.length - 1; idx >= 0; idx -= 1) {
+ if (messages[idx].role === "assistant") return idx;
+ }
+ return -1;
+ }, [messages]);
+
+ return (
+ <>
+ {messageRows.map(({ msg, originalIndex, pairedUserMessage }) => {
+ const i = originalIndex;
+ if (msg.role === "user") {
+ return (
+ <UserMessage
+ key={`${msg.role}-${i}`}
+ msg={msg}
+ index={i}
+ onPreviewAttachment={onPreviewAttachment}
+ />
+ );
+ }
+
+ const isActiveAssistant = isStreaming && i === messages.length - 1;
+ const msgDone = !isActiveAssistant;
+ const showActions = msgDone && hasVisibleMarkdownContent(msg.content);
+ const isLastAssistant = i === lastAssistantIndex;
+ const showRegenerate =
+ showActions &&
+ !isStreaming &&
+ isLastAssistant &&
+ Boolean(pairedUserMessage) &&
+ (!pairedUserMessage?.capability ||
+ pairedUserMessage?.capability === "chat");
+
+ // The "Answer now" affordance lives inside the trace panel for the
+ // currently-streaming assistant turn. We hand the panel a thin
+ // closure so it does not need to know about MessageRequestSnapshot.
+ const handleTraceAnswerNow =
+ isActiveAssistant && pairedUserMessage?.requestSnapshot
+ ? () =>
+ onAnswerNow(pairedUserMessage.requestSnapshot, {
+ content: msg.content,
+ events: msg.events,
+ })
+ : undefined;
+
+ const costSummary = (() => {
+ if (!msgDone) return null;
+ const resultEv = msg.events?.find((e) => e.type === "result");
+ if (!resultEv) return null;
+ const meta = resultEv.metadata?.metadata as
+ | Record<string, unknown>
+ | undefined;
+ const cs = meta?.cost_summary as
+ | {
+ total_cost_usd?: number;
+ total_tokens?: number;
+ total_calls?: number;
+ }
+ | undefined;
+ if (!cs || !cs.total_calls) return null;
+ return cs;
+ })();
+
+ return (
+ <div key={`${msg.role}-${i}`} className="w-full">
+ <AssistantMessage
+ msg={msg}
+ isStreaming={isActiveAssistant}
+ outlineStatus={outlineStatusByIndex.get(i)}
+ sessionId={sessionId}
+ language={language}
+ onConfirmOutline={onConfirmOutline}
+ onAnswerNow={handleTraceAnswerNow}
+ />
+ {(showActions || costSummary) && (
+ <div className="mt-2 flex items-center">
+ {showActions && (
+ <div className="flex gap-2">
+ <RoughActionButton
+ icon={Copy}
+ label={t("Copy")}
+ onClick={() => void onCopyAssistantMessage(msg.content)}
+ />
+ {showRegenerate && (
+ <RoughActionButton
+ icon={RefreshCcw}
+ label={t("Regenerate")}
+ onClick={() => onRegenerateMessage()}
+ />
+ )}
+ </div>
+ )}
+ {costSummary && (
+ <div className="ml-auto">
+ <CostFooter
+ cost={costSummary.total_cost_usd ?? 0}
+ tokens={costSummary.total_tokens ?? 0}
+ calls={costSummary.total_calls ?? 0}
+ />
+ </div>
+ )}
+ </div>
+ )}
+ </div>
+ );
+ })}
+ </>
+ );
+});
+
+ChatMessageList.displayName = "ChatMessageList";