import React, { useEffect, useState } from 'react'; import { useSession } from '@/hooks/useSession'; import ConfigPanel from './components/ConfigPanel'; import Sidebar from './components/Sidebar'; import Timeline from './components/Timeline'; import ProgressSidebar from './components/ProgressSidebar'; import ScenarioConfigPage from './components/ScenarioConfigPage'; import './MopeChat.css'; /** * 从 localStorage 读取用户偏好的主题色,默认为 light */ function getInitialTheme() { if (typeof window === 'undefined') return 'light'; return window.localStorage.getItem('mope-theme') || 'light'; } /** * 应用根组件 * * 负责整个页面的布局切换: * - idle 状态:显示配置面板或场景配置页(启动前) * - running / done / error 状态:显示三栏分析界面(左侧进度 → 中间时间线 → 右侧状态) * * 通过 useSession hook 管理所有分析会话的状态和事件流 */ export default function MopeChat() { const session = useSession(); const [taskContext, setTaskContext] = useState(''); const [idlePage, setIdlePage] = useState('home'); const [theme, setTheme] = useState(getInitialTheme); // 解构 session hook 返回的所有状态和方法 // status: 'idle' | 'running' | 'waiting_human' | 'done' | 'error' // phase: 'starting' | 'diverging' | 'closing' | 'done' → 当前分析阶段 // coordinatorMode: 'auto' | 'human_review' → 协调者运行模式 const { status, phase, coordinatorMode, currentRound, totalRounds, divergeRounds, reportSchema, agents, agentStates, timeline, coordinatorUpdates, latestDraft, sharedMemory, finalReportPartial, currentFinalField, pendingReview, reviewSubmitting, reviewRevising, errors, startSession, stopSession, approveCoordinator, reviseCoordinator, reset, } = session; // 主题切换时同步到 DOM 和 localStorage useEffect(() => { document.documentElement.dataset.theme = theme; window.localStorage.setItem('mope-theme', theme); }, [theme]); /** * 用户点击"开始分析"时触发 * @param {Object} config - 表单配置(任务背景、模板、智能体等) * @param {string} backendUrl - 后端 API 地址 */ const handleStart = (config, backendUrl) => { setTaskContext(config.task_context || ''); startSession(config, backendUrl); }; /** 用户点击"停止"时触发,中断当前 SSE 连接 */ const handleStop = () => { stopSession(); }; /** 用户点击"重新开始"时触发,重置所有状态回到 idle 页 */ const handleReset = () => { reset(); setTaskContext(''); }; /** 切换明/暗主题 */ const handleThemeToggle = () => { setTheme(current => (current === 'light' ? 'dark' : 'light')); }; // ========== idle 状态下的页面 ========== // 场景配置页(idle 下的二级页面) if (status === 'idle') { if (idlePage === 'scenario_config') { return (