"use client"; import { FilesIcon, XIcon } from "lucide-react"; import { useParams, useRouter, useSearchParams } from "next/navigation"; import { useCallback, useEffect, useMemo, useState } from "react"; import { ConversationEmptyState } from "@/components/ai-elements/conversation"; import { usePromptInputController } from "@/components/ai-elements/prompt-input"; import { Button } from "@/components/ui/button"; import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable"; import { useSidebar } from "@/components/ui/sidebar"; import { ArtifactFileDetail, ArtifactFileList, useArtifacts, } from "@/components/workspace/artifacts"; 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 { TodoList } from "@/components/workspace/todo-list"; import { Tooltip } from "@/components/workspace/tooltip"; import { Welcome } from "@/components/workspace/welcome"; import { useI18n } from "@/core/i18n/hooks"; import { useNotification } from "@/core/notification/hooks"; import { useLocalSettings } from "@/core/settings"; import { type AgentThread, type AgentThreadState } from "@/core/threads"; import { useSubmitThread, useThreadStream } from "@/core/threads/hooks"; import type { Message } from "@langchain/langgraph-sdk"; import { pathOfThread, textOfMessage, titleOfThread, } from "@/core/threads/utils"; import { uuid } from "@/core/utils/uuid"; import { env } from "@/env"; import { cn } from "@/lib/utils"; export default function ChatPage() { const { t } = useI18n(); const router = useRouter(); const [settings, setSettings] = useLocalSettings(); const { setOpen: setSidebarOpen } = useSidebar(); const { artifacts, open: artifactsOpen, setOpen: setArtifactsOpen, setArtifacts, select: selectArtifact, selectedArtifact, } = useArtifacts(); const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>(); const searchParams = useSearchParams(); const promptInputController = usePromptInputController(); const inputInitialValue = useMemo(() => { if (threadIdFromPath !== "new" || searchParams.get("mode") !== "skill") { return undefined; } return t.inputBox.createSkillPrompt; }, [threadIdFromPath, searchParams, t.inputBox.createSkillPrompt]); useEffect(() => { if (inputInitialValue) { setTimeout(() => { promptInputController.textInput.setInput(inputInitialValue); const textarea = document.querySelector("textarea"); if (textarea) { textarea.focus(); textarea.selectionStart = textarea.value.length; textarea.selectionEnd = textarea.value.length; } }, 100); } }, [inputInitialValue, promptInputController.textInput]); const isNewThread = useMemo( () => threadIdFromPath === "new", [threadIdFromPath], ); const [threadId, setThreadId] = useState(null); useEffect(() => { if (threadIdFromPath !== "new") { setThreadId(threadIdFromPath); } else { setThreadId(uuid()); } }, [threadIdFromPath]); const { showNotification } = useNotification(); const [finalState, setFinalState] = useState(null); const thread = useThreadStream({ isNewThread, threadId, onFinish: (state) => { setFinalState(state); if (document.hidden || !document.hasFocus()) { let body = "Conversation finished"; const lastMessage = state.messages[state.messages.length - 1]; if (lastMessage) { const textContent = textOfMessage(lastMessage); if (textContent) { if (textContent.length > 200) { body = textContent.substring(0, 200) + "..."; } else { body = textContent; } } } showNotification(state.title, { body, }); } }, }); useEffect(() => { if (thread.isLoading) setFinalState(null); }, [thread.isLoading]); const title = useMemo(() => { let result = isNewThread ? "" : titleOfThread(thread as unknown as AgentThread); if (result === "Untitled") { result = ""; } return result; }, [thread, isNewThread]); 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, setTodoListCollapsed] = useState(true); const handleSubmit = useSubmitThread({ isNewThread, threadId, thread, threadContext: { ...settings.context, thinking_enabled: settings.context.mode !== "flash", is_plan_mode: settings.context.mode === "pro" || settings.context.mode === "ultra", subagent_enabled: settings.context.mode === "ultra", }, afterSubmit() { router.push(pathOfThread(threadId!)); }, }); const handleStop = useCallback(async () => { await thread.stop(); }, [thread]); if (!threadId) { return null; } return (
{title !== "Untitled" && ( )}
{artifacts?.length > 0 && !artifactsOpen && ( )}
} disabled={env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true"} onContextChange={(context) => setSettings("context", context) } onSubmit={handleSubmit} onStop={handleStop} /> {env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && (
{t.common.notAvailableInDemoMode}
)}
{selectedArtifact ? ( ) : (
{thread.values.artifacts?.length === 0 ? ( } title="No artifact selected" description="Select an artifact to view its details" /> ) : (

Artifacts

)}
)}
); }