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 (
setIdlePage('home')} />
); } // 首页:配置表单 return (
setIdlePage('scenario_config')} />
); } // ========== 运行中 / 已完成 / 错误 状态下的三栏布局 ========== return (
{/* 主题切换按钮(运行状态下固定在 header 区域) */} {/* 顶部状态栏:显示项目名、任务摘要、运行模式、操作按钮 */}
MOPE {taskContext ? `"${taskContext.slice(0, 80)}${taskContext.length > 80 ? '…' : ''}"` : '分析中…'} {coordinatorMode === 'human_review' ? '人工确认' : '全自动'} {status === 'waiting_human' && ( 等待人工确认 )}
{(status === 'running' || status === 'waiting_human') && ( )} {(status === 'done' || status === 'error') && ( )}
{/* 三栏主体区域 */}
{/* 左侧栏:阶段进度 + 智能体列表(可点击跳转到对应输出) */} {/* 中间主区域:错误横幅 + 时间线(所有 agent 输出和协调者指令按时间排列) */}
{errors.length > 0 && errors.map((e, i) => (
{e.agent_id ? `[${e.agent_id}] ` : ''}{e.error}
))}
{/* 右侧栏:方案进度、共享记忆、字段/角色记忆 */}
); }