"use client"; import { FilesIcon, XIcon } from "lucide-react"; import { useParams, 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 { 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 { ThreadTitle } from "@/components/workspace/thread-title"; import { Tooltip } from "@/components/workspace/tooltip"; import { Welcome } from "@/components/workspace/welcome"; import { useLocalSettings } from "@/core/settings"; import { type AgentThread } from "@/core/threads"; import { useSubmitThread, useThreadStream } from "@/core/threads/hooks"; import { pathOfThread, titleOfThread } from "@/core/threads/utils"; import { uuid } from "@/core/utils/uuid"; import { cn } from "@/lib/utils"; export default function ChatPage() { const router = useRouter(); const [settings, setSettings] = useLocalSettings(); const { setOpen: setSidebarOpen } = useSidebar(); const { artifacts, open: artifactsOpen, setOpen: setArtifactsOpen, setArtifacts, selectedArtifact, } = useArtifacts(); const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>(); const isNewThread = useMemo( () => threadIdFromPath === "new", [threadIdFromPath], ); const [threadId, setThreadId] = useState(null); useEffect(() => { if (threadIdFromPath !== "new") { setThreadId(threadIdFromPath); } else { setThreadId(uuid()); } }, [threadIdFromPath]); const thread = useThreadStream({ isNewThread, threadId, }); const title = useMemo(() => { let result = isNewThread ? "" : titleOfThread(thread as unknown as AgentThread); if (result === "Untitled") { result = ""; } return result; }, [thread, isNewThread]); useEffect(() => { setArtifacts(thread.values.artifacts); }, [setArtifacts, thread.values.artifacts]); const handleSubmit = useSubmitThread({ isNewThread, threadId, thread, threadContext: settings.context, afterSubmit() { router.push(pathOfThread(threadId!)); }, }); const handleStop = useCallback(async () => { await thread.stop(); }, [thread]); return (
{threadId && title !== "Untitled" && ( )}
{artifacts?.length && !artifactsOpen && ( )}
setSettings("context", context)} onSubmit={handleSubmit} onStop={handleStop} />
{selectedArtifact ? ( ) : (
{thread.values.artifacts?.length === 0 ? ( } title="No artifact selected" description="Select an artifact to view its details" /> ) : (

Artifacts

)}
)}
); }