import { useEffect, useState, useCallback, useRef } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useAppStore } from '@/store'; import { useChat } from '@/hooks/useChat'; import { ChatMessage } from './ChatMessage'; import { ChatInput } from './ChatInput'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; import { RefreshCw, Loader2 } from 'lucide-react'; import { api } from '@/lib/api'; import type { Account } from '@/types'; export function ChatView() { const { conversationId } = useParams<{ conversationId?: string }>(); const navigate = useNavigate(); const { setConversations, selectedAccountId, setSelectedAccountId } = useAppStore(); const { messages, connected, thinking, toolStatus, send, createConversation } = useChat(conversationId || null); const [manifestPath, setManifestPath] = useState(null); const [accounts, setAccounts] = useState([]); const creatingRef = useRef(false); useEffect(() => { api.listAccounts().then(setAccounts).catch(() => {}); }, []); useEffect(() => { fetch('/api/pipeline/conversations') .then((r) => r.json()) .then(setConversations) .catch(() => {}); }, [messages]); useEffect(() => { const toolMsgs = messages.filter((m) => m.role === 'tool'); if (toolMsgs.length > 0) { try { const lastTool = JSON.parse(toolMsgs[toolMsgs.length - 1].content); if (lastTool.manifest) setManifestPath(lastTool.manifest); } catch {} } }, [messages]); // Inject account context when account is selected useEffect(() => { if (selectedAccountId && conversationId && connected && messages.length === 0) { const account = accounts.find((a) => a.id === selectedAccountId); if (account) { const ctx = `已选择账号「${account.name}」:${account.description || ''}。生图:${account.imageModel} 视频:${account.videoModel} 画幅:${account.defaultFormat}`; send(ctx); } } }, [selectedAccountId, conversationId, connected]); const handleNewConversation = useCallback(() => { if (creatingRef.current) return; creatingRef.current = true; createConversation('新对话', selectedAccountId || undefined); setTimeout(() => { fetch('/api/pipeline/conversations') .then((r) => r.json()) .then((list) => { setConversations(list); if (list.length > 0) { navigate(`/chat/${list[0].id}`); } creatingRef.current = false; }); }, 500); }, [createConversation, selectedAccountId, setConversations, navigate]); const handleResume = async () => { if (!manifestPath) return; try { await fetch('/api/pipeline/resume', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ manifest: manifestPath }), }); } catch (e) { console.error('Resume failed:', e); } }; if (!conversationId) { return (
💬

开始新对话

选择左侧对话或创建新的创作会话

{accounts.length > 0 && ( )}
); } return (
{connected ? '在线' : '连接中'}
{manifestPath && ( )}
{messages.map((msg) => ( ))} {thinking && (
{toolStatus || '思考中...'}
)}
); }