Files
2026-05-14 16:51:32 +08:00

216 lines
7.0 KiB
React

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 (
<div className="app-idle app-idle-scroll">
<button className="theme-toggle" onClick={handleThemeToggle}>
{theme === 'light' ? '切换暗色' : '切换亮色'}
</button>
<ScenarioConfigPage onBack={() => setIdlePage('home')} />
</div>
);
}
// 首页:配置表单
return (
<div className="app-idle">
<button className="theme-toggle" onClick={handleThemeToggle}>
{theme === 'light' ? '切换暗色' : '切换亮色'}
</button>
<ConfigPanel
onStart={handleStart}
isRunning={false}
onStop={handleStop}
onOpenScenarioConfig={() => setIdlePage('scenario_config')}
/>
</div>
);
}
// ========== 运行中 / 已完成 / 错误 状态下的三栏布局 ==========
return (
<div className="app-running">
{/* 主题切换按钮(运行状态下固定在 header 区域) */}
<button className="theme-toggle theme-toggle-running" onClick={handleThemeToggle}>
{theme === 'light' ? '切换暗色' : '切换亮色'}
</button>
{/* 顶部状态栏:显示项目名、任务摘要、运行模式、操作按钮 */}
<header className="app-header">
<span className="app-header-logo">MOPE</span>
<span className="app-header-task">
{taskContext ? `"${taskContext.slice(0, 80)}${taskContext.length > 80 ? '…' : ''}"` : '分析中…'}
</span>
<span className="badge badge-default">
{coordinatorMode === 'human_review' ? '人工确认' : '全自动'}
</span>
{status === 'waiting_human' && (
<span className="badge badge-diverging">等待人工确认</span>
)}
<div className="app-header-actions">
{(status === 'running' || status === 'waiting_human') && (
<button className="btn-danger" onClick={handleStop}>
停止
</button>
)}
{(status === 'done' || status === 'error') && (
<button className="btn-secondary" onClick={handleReset}>
重新开始
</button>
)}
</div>
</header>
{/* 三栏主体区域 */}
<div className="app-body">
{/* 左侧栏:阶段进度 + 智能体列表(可点击跳转到对应输出) */}
<aside className="app-sidebar">
<Sidebar
agents={agents}
agentStates={agentStates}
phase={phase}
currentRound={currentRound}
divergeRounds={divergeRounds}
totalRounds={totalRounds}
/>
</aside>
{/* 中间主区域:错误横幅 + 时间线(所有 agent 输出和协调者指令按时间排列) */}
<main className="app-main">
{errors.length > 0 && errors.map((e, i) => (
<div key={i} className="error-banner">
{e.agent_id ? `[${e.agent_id}] ` : ''}{e.error}
</div>
))}
<Timeline
timeline={timeline}
agentStates={agentStates}
reportSchema={reportSchema}
pendingReview={pendingReview}
onApproveReview={approveCoordinator}
onReviseReview={reviseCoordinator}
reviewSubmitting={reviewSubmitting}
reviewRevising={reviewRevising}
/>
</main>
{/* 右侧栏:方案进度、共享记忆、字段/角色记忆 */}
<aside className="app-progress-sidebar">
<ProgressSidebar
phase={phase}
status={status}
currentRound={currentRound}
divergeRounds={divergeRounds}
reportSchema={reportSchema}
coordinatorUpdates={coordinatorUpdates}
pendingReview={pendingReview}
latestDraft={latestDraft}
sharedMemory={sharedMemory}
finalReportPartial={finalReportPartial}
currentFinalField={currentFinalField}
agentStates={agentStates}
/>
</aside>
</div>
</div>
);
}