"use client"; import { FilesIcon, ListTodoIcon, XIcon } from "lucide-react"; import { useRouter } from "next/navigation"; import { useCallback, useEffect, useMemo, useState } from "react"; import { ConversationEmptyState } from "@/components/ai-elements/conversation"; import { Button } from "@/components/ui/button"; import { DevDialog, DevDialogContent, DevDialogFooter, DevDialogHeader, DevDialogTitle, } from "@/components/ui/dev-dialog"; import { useSidebar } from "@/components/ui/sidebar"; import { ArtifactFileDetail, ArtifactFileList, useArtifacts, } from "@/components/workspace/artifacts"; import { useThreadChat } from "@/components/workspace/chats"; // import { DevTodoList } from "@/components/workspace/dev-todo-list"; import { InputBox } from "@/components/workspace/input-box"; import { MessageList } from "@/components/workspace/messages"; import { ThreadContext } from "@/components/workspace/messages/context"; import { ThreadTitle } from "@/components/workspace/thread-title"; import { Tooltip } from "@/components/workspace/tooltip"; import { useSpecificChatMode } from "@/components/workspace/use-chat-mode"; import { Welcome } from "@/components/workspace/welcome"; import { useI18n } from "@/core/i18n/hooks"; import { POST_MESSAGE_TYPES, sendToParent } from "@/core/iframe-messages"; import { useNotification } from "@/core/notification/hooks"; import { useLocalSettings } from "@/core/settings"; import { useThreadStream } from "@/core/threads/hooks"; import { textOfMessage } from "@/core/threads/utils"; import { env } from "@/env"; import { useSelectedSkillListener } from "@/hooks/use-selected-skill-listener"; import { cn } from "@/lib/utils"; import { IframeTestPanel } from "@/components/workspace/iframe-test-panel"; export default function ChatPage() { const { t } = useI18n(); useSpecificChatMode(); const [settings, setSettings] = useLocalSettings(); const { setOpen: setSidebarOpen } = useSidebar(); const router = useRouter(); const { artifacts, open: artifactsOpen, setOpen: setArtifactsOpen, setArtifacts, select: selectArtifact, selectedArtifact, deselect: deselectArtifact, setFullscreen: setArtifactsFullscreen, fullscreen, } = useArtifacts(); const { threadId, isNewThread, setIsNewThread, isMock, showWelcomeStyle, } = useThreadChat(); // 新逻辑:历史渲染和新会话仅由路由 /chats/new 控制,不再读取 isnew/is_chatting 参数。 const shouldRenderHistory = !showWelcomeStyle; const createNewSession = useMemo(() => isNewThread, [isNewThread]); const safeThreadId = useMemo(() => { if (!threadId || threadId === "new") { return undefined; } return threadId; }, [threadId]); const streamThreadId = useMemo(() => { if (isNewThread && createNewSession) { return undefined; } return safeThreadId; }, [createNewSession, isNewThread, safeThreadId]); const { showNotification } = useNotification(); // 监听宿主页 selectedSkill 消息 const { skillError: selectedSkillError, clearSkillError: clearSelectedSkillError, isBootstrapping: isSelectedSkillBootstrapping, } = useSelectedSkillListener({ threadId: safeThreadId ?? null }); // 对话行为控制器 const [thread, sendMessage, isUploading] = useThreadStream({ threadId: streamThreadId, context: settings.context, createNewSession, isMock, // 发送消息后跳转的逻辑 onStart: (currentThreadId) => { setIsNewThread(false); // if (!shouldStayOnNewRoute) { // Keep /new in history so router.back() can return to it. router.replace(`/workspace/chats/${currentThreadId}?is_chatting=true`); // } // history.pushState(null, "", pathOfThread(currentThreadId)); }, onFinish: (state) => { if (document.hidden || !document.hasFocus()) { let body = "Conversation finished"; const lastMessage = state.messages.at(-1); if (lastMessage) { const textContent = textOfMessage(lastMessage); if (textContent) { body = textContent.length > 200 ? textContent.substring(0, 200) + "..." : textContent; } } showNotification(state.title, { body }); } }, }); const title = useMemo(() => { const result = thread.values?.title ?? ""; return result === "Untitled" ? "" : result; }, [thread.values?.title]); const [hasSubmitted, setHasSubmitted] = useState(false); const [historyCutoff, setHistoryCutoff] = useState(null); useEffect(() => { if (shouldRenderHistory) { setHistoryCutoff(null); return; } if (historyCutoff === null && !thread.isThreadLoading) { setHistoryCutoff(thread.messages.length); } }, [ historyCutoff, shouldRenderHistory, thread.isThreadLoading, thread.messages.length, ]); useEffect(() => { const pageTitle = isNewThread ? t.pages.newChat : thread.values?.title && thread.values.title !== "Untitled" ? thread.values.title : t.pages.untitled; if (thread.isThreadLoading) { document.title = `Loading... - ${t.pages.appName}`; } else { document.title = `${pageTitle} - ${t.pages.appName}`; } }, [ isNewThread, t.pages.newChat, t.pages.untitled, t.pages.appName, thread.values?.title, thread.isThreadLoading, ]); const [autoSelectFirstArtifact, setAutoSelectFirstArtifact] = useState(true); useEffect(() => { setArtifacts(thread.values.artifacts); if ( env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && autoSelectFirstArtifact ) { if (thread?.values?.artifacts?.length > 0) { setAutoSelectFirstArtifact(false); selectArtifact(thread.values.artifacts[0]!); } } }, [ autoSelectFirstArtifact, selectArtifact, setArtifacts, thread.values.artifacts, ]); const artifactPanelOpen = useMemo(() => { if (env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true") { return artifactsOpen && artifacts?.length > 0; } return artifactsOpen; }, [artifactsOpen, artifacts]); const todoListCollapsed = true; const [showExitDialog, setShowExitDialog] = useState(false); const isStreaming = isUploading || thread.isLoading; const handleSubmit = useCallback( (message: Parameters[1]) => { if (isSelectedSkillBootstrapping) { return; } setHasSubmitted(true); void sendMessage(threadId, message); }, [isSelectedSkillBootstrapping, sendMessage, threadId], ); const handleStop = useCallback(async () => { await thread.stop(); }, [thread]); const resetNewSessionState = useCallback(() => { setIsNewThread(true); setHasSubmitted(false); setHistoryCutoff(null); setArtifacts([]); deselectArtifact(); setArtifactsOpen(false); setArtifactsFullscreen(false); }, [ deselectArtifact, setArtifacts, setArtifactsFullscreen, setArtifactsOpen, setIsNewThread, ]); return (
{title !== "Untitled" && ( )}
{/* 取消TodoList */} {/*
{selectedArtifact ? ( ) : (
{thread.values.artifacts?.length === 0 ? ( } title="No artifact selected" description="Select an artifact to view its details" /> ) : (

{t.common.artifacts}

)}
)}
{/* Fixed 底部居中输入框容器 */}
{!(showWelcomeStyle && thread.isThreadLoading) ? ( {showWelcomeStyle && !hasSubmitted && ( )}
} disabled={ env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" || isSelectedSkillBootstrapping || isUploading } onContextChange={(context) => setSettings("context", context)} onSubmit={handleSubmit} onStop={handleStop} /> ) : ( // '' )} {/* {isSelectedSkillBootstrapping && (
正在初始化 Skill 文件...
)} */} {env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && (
{t.common.notAvailableInDemoMode}
)}
{/* 退出确认对话框 */} 提示

(测试中:计划销毁但是现在没有销毁) 退出后,当前会话结束并销毁,请先下载保存当前结果!

{/* selectedSkill 失败:错误弹窗 */} { if (!open) clearSelectedSkillError(); }} > ⚠️ {selectedSkillError?.title ?? "技能加载失败"}

{selectedSkillError?.message ?? "发生了未知错误,请稍后重试。"}

{/* MARK: 开发测试:iframe 通信功能测试面板 */} {process.env.NODE_ENV !== "production" && }
); }