'use client'; import Link from 'next/link'; import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { ArrowRight, Building2, MessageSquare, Paperclip, Plus, Send, Trash2, X } from 'lucide-react'; import { OfficeStatusBadge } from '@/components/office/OfficeShared'; import { ChatWorkbench } from '@/components/chat-workbench/ChatWorkbench'; import { Button } from '@/components/ui/button'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Separator } from '@/components/ui/separator'; import { cancelDelegation, createSession, deleteSession, getSession, listCommands, listSessions, sendMessage, uploadFile, wsManager, } from '@/lib/api'; import { buildOfficeTaskList, isOfficeTaskTerminal } from '@/lib/office'; import { pickAppText } from '@/lib/i18n/core'; import { useAppI18n } from '@/lib/i18n/provider'; import { useChatStore } from '@/lib/store'; import type { ChatMessage, FileAttachment, SessionUpdatedEvent, SlashCommand, WsEvent } from '@/types'; function messageFingerprint(msg: ChatMessage): string { const attachmentKey = (msg.attachments ?? []) .map((a) => `${a.file_id ?? ''}:${a.name}:${a.content_type}:${a.size ?? ''}`) .join('|'); return `${msg.role}::${String(msg.content)}::${attachmentKey}`; } function mergeServerWithPendingUsers(serverMessages: ChatMessage[], localMessages: ChatMessage[]): ChatMessage[] { const counts = new Map(); for (const message of serverMessages) { const key = messageFingerprint(message); counts.set(key, (counts.get(key) ?? 0) + 1); } const pendingUsers: ChatMessage[] = []; for (const message of localMessages) { const key = messageFingerprint(message); const count = counts.get(key) ?? 0; if (count > 0) { counts.set(key, count - 1); continue; } if (message.role === 'user') { pendingUsers.push(message); } } return [...serverMessages, ...pendingUsers]; } function isSessionUpdatedEvent(data: WsEvent | Record): data is SessionUpdatedEvent { return data.type === 'session_updated' && typeof data.session_id === 'string'; } export default function ChatPage() { const { locale } = useAppI18n(); const { sessionId, messages, isLoading, isThinking, sessions, processRuns, processEvents, processArtifacts, selectedRunId, setSessionId, setMessages, addMessage, setIsLoading, clearMessages, setIsThinking, setSelectedRunId, } = useChatStore(); const [input, setInput] = useState(''); const [commands, setCommands] = useState([]); const [showCommandPicker, setShowCommandPicker] = useState(false); const [pickerIndex, setPickerIndex] = useState(0); const [pendingFiles, setPendingFiles] = useState>([]); const messagesEndRef = useRef(null); const messageViewportRef = useRef(null); const textareaRef = useRef(null); const pickerRef = useRef(null); const fileInputRef = useRef(null); const loadSessionReqSeq = useRef(0); const commandsLoadedRef = useRef(false); const refreshSessionOnReconnectRef = useRef(false); const hasConnectedRef = useRef(false); const shouldSnapToLatestRef = useRef(true); const wsStatus = useChatStore((state) => state.wsStatus); const filteredCommands = useMemo(() => { if (!input.startsWith('/') || input.includes(' ')) return []; const filter = input.slice(1).toLowerCase(); return commands.filter( (command) => command.name.startsWith(filter) || (filter === '' ? true : command.name.includes(filter)) ); }, [commands, input]); const sessionProcessRuns = useMemo( () => processRuns.filter((run) => run.session_id === sessionId), [processRuns, sessionId] ); const sessionRunIds = useMemo( () => new Set(sessionProcessRuns.map((run) => run.run_id)), [sessionProcessRuns] ); const sessionProcessEvents = useMemo( () => processEvents.filter((event) => sessionRunIds.has(event.run_id)), [processEvents, sessionRunIds] ); const sessionProcessArtifacts = useMemo( () => processArtifacts.filter((artifact) => sessionRunIds.has(artifact.run_id)), [processArtifacts, sessionRunIds] ); const selectedSessionRunId = selectedRunId && sessionRunIds.has(selectedRunId) ? selectedRunId : null; const officeTasks = useMemo( () => buildOfficeTaskList({ sessionId, sessions, processRuns, processEvents, processArtifacts, }, locale), [locale, processArtifacts, processEvents, processRuns, sessionId, sessions] ); const currentOfficeTask = officeTasks.find((task) => !isOfficeTaskTerminal(task.status)) ?? officeTasks[0] ?? null; const loadSessions = useCallback(async () => { try { const list = await listSessions(); useChatStore.getState().setSessions(list); } catch { // backend may be offline during first render } }, []); const loadSessionMessages = useCallback(async (key: string) => { const reqSeq = ++loadSessionReqSeq.current; const localSnapshot = useChatStore.getState().messages; const waitingForReply = useChatStore.getState().isLoading || useChatStore.getState().isThinking; try { const detail = await getSession(key); if (reqSeq !== loadSessionReqSeq.current) return; if (useChatStore.getState().sessionId !== key) return; const nextMessages = waitingForReply ? mergeServerWithPendingUsers(detail.messages, localSnapshot) : detail.messages; setMessages(nextMessages); shouldSnapToLatestRef.current = true; const last = nextMessages[nextMessages.length - 1]; if (last?.role === 'assistant') { setIsThinking(false); setIsLoading(false); } } catch { if (reqSeq !== loadSessionReqSeq.current) return; if (useChatStore.getState().sessionId !== key) return; } }, [setIsLoading, setIsThinking, setMessages]); const loadCommands = useCallback(async () => { if (commandsLoadedRef.current) return; commandsLoadedRef.current = true; try { const nextCommands = await listCommands(); setCommands(nextCommands); } catch { commandsLoadedRef.current = false; } }, []); useEffect(() => { if (input.startsWith('/') && !input.includes(' ')) { void loadCommands(); } }, [input, loadCommands]); useEffect(() => { setShowCommandPicker(filteredCommands.length > 0); setPickerIndex(0); }, [filteredCommands]); useEffect(() => { clearMessages(); setIsLoading(false); setIsThinking(false); void loadSessionMessages(sessionId); }, [clearMessages, loadSessionMessages, sessionId, setIsLoading, setIsThinking]); useEffect(() => { if (wsStatus === 'connected') { if (hasConnectedRef.current && refreshSessionOnReconnectRef.current) { refreshSessionOnReconnectRef.current = false; void loadSessionMessages(useChatStore.getState().sessionId); } hasConnectedRef.current = true; return; } if (wsStatus === 'disconnected' && hasConnectedRef.current) { refreshSessionOnReconnectRef.current = true; } }, [loadSessionMessages, wsStatus]); useEffect(() => { const unsubMessage = wsManager.onMessage((data) => { if (isSessionUpdatedEvent(data)) { void loadSessions(); if (data.session_id === useChatStore.getState().sessionId) { void loadSessionMessages(data.session_id); } return; } if (data.type === 'status' && data.status === 'thinking') { setIsThinking(true); } else if (data.type === 'message' && data.role === 'assistant') { setIsThinking(false); setIsLoading(false); addMessage({ role: 'assistant', content: typeof data.content === 'string' ? data.content : '', timestamp: new Date().toISOString(), attachments: Array.isArray(data.attachments) ? data.attachments : undefined, }); loadSessions(); } }); return () => { unsubMessage(); }; }, [addMessage, loadSessionMessages, loadSessions, setIsLoading, setIsThinking]); useEffect(() => { if (!isLoading && !isThinking) { return; } const timer = setInterval(() => { loadSessionMessages(useChatStore.getState().sessionId); }, 1500); return () => clearInterval(timer); }, [isLoading, isThinking, loadSessionMessages]); const scrollMessagesToLatest = useCallback((behavior: ScrollBehavior) => { const viewport = messageViewportRef.current; if (!viewport) return; messagesEndRef.current?.scrollIntoView({ block: 'end', behavior }); viewport.scrollTo({ top: viewport.scrollHeight, behavior }); }, []); const scheduleScrollToLatest = useCallback((behavior: ScrollBehavior) => { if (typeof window === 'undefined') { scrollMessagesToLatest(behavior); return; } window.requestAnimationFrame(() => { window.requestAnimationFrame(() => { scrollMessagesToLatest(behavior); }); }); }, [scrollMessagesToLatest]); useEffect(() => { shouldSnapToLatestRef.current = true; }, [sessionId]); useLayoutEffect(() => { if (messages.length === 0 && !isThinking && sessionProcessEvents.length === 0) { return; } scheduleScrollToLatest(shouldSnapToLatestRef.current ? 'auto' : 'smooth'); shouldSnapToLatestRef.current = false; }, [isThinking, messages.length, scheduleScrollToLatest, sessionProcessEvents.length]); useEffect(() => { if (!showCommandPicker || !pickerRef.current) return; const item = pickerRef.current.children[pickerIndex] as HTMLElement | undefined; item?.scrollIntoView({ block: 'nearest' }); }, [pickerIndex, showCommandPicker]); const selectCommand = useCallback((command: SlashCommand) => { setInput(command.argument_hint ? `/${command.name} ` : `/${command.name}`); setShowCommandPicker(false); textareaRef.current?.focus(); }, []); const handleSend = useCallback(async () => { const text = input.trim(); if ((!text && pendingFiles.length === 0) || isLoading) return; const readyFiles = pendingFiles.filter((p) => p.id && !p.error); const attachments: FileAttachment[] = readyFiles.map((item) => ({ file_id: item.id!, name: item.file.name, content_type: item.file.type || 'application/octet-stream', size: item.file.size, })); setInput(''); setPendingFiles([]); setShowCommandPicker(false); const msgContent = text || pickAppText(locale, '(仅附件)', '(Attachments only)'); addMessage({ role: 'user', content: msgContent, timestamp: new Date().toISOString(), attachments: attachments.length > 0 ? attachments : undefined, }); setIsLoading(true); setIsThinking(false); if (wsManager.getStatus() === 'connected') { const wsPayload: Record = { type: 'message', content: msgContent }; if (attachments.length > 0) { wsPayload.attachments = attachments; } wsManager.sendRaw(wsPayload); } else { try { const result = await sendMessage(msgContent, sessionId, attachments.length > 0 ? attachments : undefined); setIsThinking(false); setIsLoading(false); if (result.response) { if (useChatStore.getState().sessionId !== sessionId) { await loadSessions(); return; } addMessage({ role: 'assistant', content: result.response, timestamp: new Date().toISOString(), }); loadSessions(); } else { await loadSessionMessages(sessionId); loadSessions(); } } catch { setIsThinking(false); setIsLoading(false); if (useChatStore.getState().sessionId !== sessionId) { return; } addMessage({ role: 'assistant', content: pickAppText(locale, '发送失败,请检查后端服务是否正在运行。', 'Send failed. Please check whether the backend service is running.'), timestamp: new Date().toISOString(), }); } } }, [addMessage, input, isLoading, loadSessionMessages, loadSessions, locale, pendingFiles, sessionId, setIsLoading, setIsThinking]); const handleKeyDown = (e: React.KeyboardEvent) => { if (showCommandPicker && filteredCommands.length > 0) { if (e.key === 'ArrowUp') { e.preventDefault(); setPickerIndex((i) => (i <= 0 ? filteredCommands.length - 1 : i - 1)); return; } if (e.key === 'ArrowDown') { e.preventDefault(); setPickerIndex((i) => (i >= filteredCommands.length - 1 ? 0 : i + 1)); return; } if (e.key === 'Tab' || (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing)) { e.preventDefault(); selectCommand(filteredCommands[pickerIndex]); return; } if (e.key === 'Escape') { e.preventDefault(); setShowCommandPicker(false); return; } } if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); handleSend(); } }; const handleFileSelect = useCallback(async (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); if (!files.length) return; e.target.value = ''; for (const file of files) { if (file.size > 50 * 1024 * 1024) { setPendingFiles((prev) => [...prev, { file, progress: 0, error: pickAppText(locale, '文件过大(最大 50MB)', 'File is too large (max 50MB)') }]); continue; } setPendingFiles((prev) => [...prev, { file, progress: 0 }]); try { const result = await uploadFile(file, sessionId, (pct) => { setPendingFiles((prev) => prev.map((item) => (item.file === file ? { ...item, progress: pct } : item))); }); setPendingFiles((prev) => prev.map((item) => (item.file === file ? { ...item, id: result.file_id, progress: 100 } : item))); } catch (err: any) { setPendingFiles((prev) => prev.map((item) => (item.file === file ? { ...item, error: err.message || pickAppText(locale, '上传失败', 'Upload failed') } : item))); } } }, [locale, sessionId]); const handleNewSession = async () => { const id = `web:${Date.now()}`; setSessionId(id); setSelectedRunId(null); clearMessages(); useChatStore.getState().resetProcessState(); try { await createSession(id); } catch { // ignore transient create failures; first message can still create the session server-side } void loadSessions(); }; const handleDeleteSession = async (key: string, e: React.MouseEvent) => { e.stopPropagation(); try { await deleteSession(key); if (key === sessionId) { setSessionId('web:default'); clearMessages(); useChatStore.getState().resetProcessState(); } loadSessions(); } catch { // ignore transient errors } }; const handleSelectSession = (key: string) => { setSelectedRunId(null); setSessionId(key); }; const handleCancelRun = useCallback(async (runId: string) => { try { await cancelDelegation(runId); } catch (err: any) { addMessage({ role: 'assistant', content: pickAppText(locale, `取消任务 ${runId} 失败:${err.message || '未知错误'}`, `Failed to cancel task ${runId}: ${err.message || 'Unknown error'}`), timestamp: new Date().toISOString(), }); } }, [addMessage, locale]); const removePendingFile = useCallback((file: File) => { setPendingFiles((prev) => prev.filter((item) => item.file !== file)); }, []); const formatSessionName = (key: string) => { if (key.startsWith('web:')) { const id = key.slice(4); if (id === 'default') return pickAppText(locale, '默认', 'Default'); const numeric = Number(id); if (!Number.isNaN(numeric)) { return new Date(numeric).toLocaleDateString(locale, { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }); } return id; } return key; }; return (
{sessions.length === 0 && (

{pickAppText(locale, '暂无对话记录', 'No chat history yet')}

)} {sessions.map((session) => (
handleSelectSession(session.key)} className={`group flex items-center justify-between px-2 py-1.5 rounded-md cursor-pointer text-sm ${ session.key === sessionId ? 'bg-accent text-accent-foreground' : 'text-muted-foreground hover:bg-accent/50' }`} >
{formatSessionName(session.key)}
))}
{currentOfficeTask ? (
{pickAppText(locale, '当前任务现场', 'Current task floor')}
{currentOfficeTask.title} {pickAppText(locale, '主 Agent', 'Lead agent')}: {currentOfficeTask.rootActorName}
) : null}
setSelectedRunId(selectedSessionRunId === runId ? null : runId)} onCancelRun={handleCancelRun} />
{pendingFiles.length > 0 && (
{pendingFiles.map((item, index) => (
{item.file.name}{' '} ({(item.file.size / 1024).toFixed(0)}KB) {item.error ? ( {item.error} ) : item.progress < 100 ? (
) : ( {pickAppText(locale, '就绪', 'Ready')} )}
))}
)}
{showCommandPicker && filteredCommands.length > 0 && (
{filteredCommands.map((command, index) => ( ))}
)}