/** * App Component * * 响应式布局:支持桌面端和移动端 */ import { useState, useEffect, useCallback } from 'react'; import { Sidebar, FileBrowser, ConfigPanel, CommandPanel, MCPPanel, HooksPanel, AgentsPanel, CheckpointPanel, Toaster, listSessions, createSession, type Session, } from '@ai-assistant/ui'; import { ChatPage } from './pages/Chat'; export function App() { const [currentSessionId, setCurrentSessionId] = useState(null); const [isInitializing, setIsInitializing] = useState(true); const [showFileBrowser, setShowFileBrowser] = useState(false); const [showConfig, setShowConfig] = useState(false); const [showCommands, setShowCommands] = useState(false); const [showMCP, setShowMCP] = useState(false); const [showHooks, setShowHooks] = useState(false); const [showAgents, setShowAgents] = useState(false); const [showCheckpoints, setShowCheckpoints] = useState(false); const [sessionTitleUpdate, setSessionTitleUpdate] = useState<{ sessionId: string; name: string } | null>(null); // 初始化:加载或创建会话 useEffect(() => { async function init() { try { const { data: sessions } = await listSessions(); if (sessions.length > 0) { // 选择最近的会话 setCurrentSessionId(sessions[0].id); } else { // 创建新会话 const { data: newSession } = await createSession(); setCurrentSessionId(newSession.id); } } catch (error) { console.error('Failed to initialize:', error); } finally { setIsInitializing(false); } } init(); }, []); const handleSelectSession = (id: string) => { setCurrentSessionId(id); }; const handleCreateSession = (session: Session) => { setCurrentSessionId(session.id); }; // 会话不存在时自动创建新会话 const handleSessionNotFound = useCallback(async () => { try { const { data: newSession } = await createSession(); setCurrentSessionId(newSession.id); } catch (error) { console.error('Failed to create new session:', error); } }, []); // 会话标题更新回调 const handleSessionUpdated = useCallback((sessionId: string, name: string) => { setSessionTitleUpdate({ sessionId, name }); }, []); if (isInitializing) { return (

Initializing...

); } return (
{/* 主内容区域 */}
{/* 聊天区域 */}
{currentSessionId ? ( setShowFileBrowser(!showFileBrowser)} onOpenConfig={() => setShowConfig(true)} onOpenCommands={() => setShowCommands(true)} onOpenMCP={() => setShowMCP(true)} onOpenHooks={() => setShowHooks(true)} onOpenAgents={() => setShowAgents(true)} onOpenCheckpoints={() => setShowCheckpoints(true)} /> ) : (

Select or create a session

)}
{/* 文件浏览器 - 桌面端侧边栏,移动端全屏覆盖 */} {showFileBrowser && ( <> {/* 移动端: 全屏覆盖 */}
Files
{ console.log('Selected file:', path); }} />
{/* 桌面端: 侧边栏 */}
{ console.log('Selected file:', path); }} />
)}
{/* 配置面板 */} {showConfig && setShowConfig(false)} responsive />} {/* 命令面板 */} {showCommands && setShowCommands(false)} responsive />} {/* MCP 面板 */} {showMCP && setShowMCP(false)} responsive />} {/* Hooks 面板 */} {showHooks && setShowHooks(false)} responsive />} {/* Agents 面板 */} {showAgents && setShowAgents(false)} responsive />} {/* Checkpoints 面板 */} {showCheckpoints && setShowCheckpoints(false)} responsive />} {/* 移动端底部文件按钮 */} {/* Toast 通知 */}
); }