Files
deer-flow/frontend/src/app/workspace/chats/[thread_id]/page.tsx

226 lines
7.4 KiB
TypeScript
Raw Normal View History

2026-01-16 09:15:04 +08:00
"use client";
2026-01-17 11:02:33 +08:00
import { FilesIcon, XIcon } from "lucide-react";
2026-01-16 09:15:04 +08:00
import { useParams, useRouter } from "next/navigation";
import { useCallback, useEffect, useMemo, useState } from "react";
2026-01-17 15:09:44 +08:00
import { ConversationEmptyState } from "@/components/ai-elements/conversation";
2026-01-17 11:02:33 +08:00
import { Button } from "@/components/ui/button";
2026-01-17 00:02:03 +08:00
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
2026-01-17 11:02:33 +08:00
import { useSidebar } from "@/components/ui/sidebar";
2026-01-17 00:02:03 +08:00
import {
2026-01-17 00:05:19 +08:00
ArtifactFileDetail,
ArtifactFileList,
2026-01-17 00:02:03 +08:00
useArtifacts,
2026-01-17 00:05:19 +08:00
} from "@/components/workspace/artifacts";
2026-01-16 09:15:04 +08:00
import { InputBox } from "@/components/workspace/input-box";
2026-01-17 00:05:19 +08:00
import { MessageList } from "@/components/workspace/messages";
2026-01-17 17:37:12 +08:00
import { ThreadTitle } from "@/components/workspace/thread-title";
2026-01-17 11:02:33 +08:00
import { Tooltip } from "@/components/workspace/tooltip";
2026-01-17 19:46:02 +08:00
import { Welcome } from "@/components/workspace/welcome";
2026-01-16 09:55:02 +08:00
import { useLocalSettings } from "@/core/settings";
2026-01-17 11:02:33 +08:00
import { type AgentThread } from "@/core/threads";
2026-01-16 19:51:39 +08:00
import { useSubmitThread, useThreadStream } from "@/core/threads/hooks";
import { pathOfThread, titleOfThread } from "@/core/threads/utils";
2026-01-16 09:15:04 +08:00
import { uuid } from "@/core/utils/uuid";
2026-01-17 11:02:33 +08:00
import { cn } from "@/lib/utils";
2026-01-16 09:15:04 +08:00
export default function ChatPage() {
2026-01-17 11:02:33 +08:00
const router = useRouter();
const [settings, setSettings] = useLocalSettings();
const { setOpen: setSidebarOpen } = useSidebar();
const {
artifacts,
2026-01-17 11:02:33 +08:00
open: artifactsOpen,
setOpen: setArtifactsOpen,
setArtifacts,
2026-01-17 11:02:33 +08:00
selectedArtifact,
} = useArtifacts();
2026-01-16 09:15:04 +08:00
const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>();
const isNewThread = useMemo(
() => threadIdFromPath === "new",
[threadIdFromPath],
);
const [threadId, setThreadId] = useState<string | null>(null);
useEffect(() => {
if (threadIdFromPath !== "new") {
setThreadId(threadIdFromPath);
} else {
setThreadId(uuid());
}
}, [threadIdFromPath]);
2026-01-17 11:02:33 +08:00
2026-01-16 14:03:34 +08:00
const thread = useThreadStream({
isNewThread,
threadId,
2026-01-16 09:15:04 +08:00
});
2026-01-17 15:48:43 +08:00
const title = useMemo(() => {
let result = isNewThread
? ""
: titleOfThread(thread as unknown as AgentThread);
if (result === "Untitled") {
result = "";
}
return result;
}, [thread, isNewThread]);
2026-01-17 00:02:03 +08:00
useEffect(() => {
setArtifacts(thread.values.artifacts);
}, [setArtifacts, thread.values.artifacts]);
2026-01-16 19:51:39 +08:00
const handleSubmit = useSubmitThread({
isNewThread,
threadId,
thread,
2026-01-16 23:03:39 +08:00
threadContext: settings.context,
2026-01-16 19:51:39 +08:00
afterSubmit() {
router.push(pathOfThread(threadId!));
2026-01-16 09:15:04 +08:00
},
2026-01-16 19:51:39 +08:00
});
2026-01-16 09:15:04 +08:00
const handleStop = useCallback(async () => {
await thread.stop();
}, [thread]);
2026-01-17 11:02:33 +08:00
2026-01-16 09:15:04 +08:00
return (
2026-01-17 00:02:03 +08:00
<ResizablePanelGroup orientation="horizontal">
2026-01-17 11:02:33 +08:00
<ResizablePanel
className="relative"
defaultSize={artifactsOpen ? 46 : 100}
minSize={30}
>
<div className="relative flex size-full min-h-0 justify-between">
2026-01-17 20:32:27 +08:00
<header className="bg-background/80 absolute top-0 right-0 left-0 z-30 flex h-12 shrink-0 items-center px-4 backdrop-blur">
2026-01-17 11:02:33 +08:00
<div className="flex w-full items-center text-sm font-medium">
2026-01-17 17:37:12 +08:00
{threadId && title !== "Untitled" && (
<ThreadTitle threadId={threadId} threadTitle={title} />
)}
2026-01-17 11:02:33 +08:00
</div>
<div>
{artifacts?.length && !artifactsOpen && (
<Tooltip content="Show artifacts of this conversation">
2026-01-17 11:02:33 +08:00
<Button
variant="ghost"
onClick={() => {
setArtifactsOpen(true);
setSidebarOpen(false);
}}
>
<FilesIcon />
Artifacts
2026-01-17 11:02:33 +08:00
</Button>
</Tooltip>
)}
</div>
</header>
<main className="flex min-h-0 grow flex-col">
<div className="flex size-full justify-center">
2026-01-17 15:09:44 +08:00
<MessageList
className="size-full"
threadId={threadId!}
thread={thread}
/>
2026-01-17 11:02:33 +08:00
</div>
2026-01-17 21:34:32 +08:00
<div className="absolute right-0 bottom-0 left-0 z-30 flex justify-center px-4">
2026-01-17 19:46:02 +08:00
<div
className={cn(
2026-01-17 19:46:02 +08:00
"relative w-full",
isNewThread && "-translate-y-[calc(50vh-120px)]",
2026-01-17 19:46:02 +08:00
isNewThread
? "max-w-(--container-width-sm)"
: "max-w-(--container-width-md)",
)}
2026-01-17 19:46:02 +08:00
>
<div
className={cn(
"absolute right-0 bottom-[148px] left-0 flex",
isNewThread ? "" : "pointer-events-none opacity-0",
)}
>
<Welcome />
</div>
<InputBox
2026-01-17 21:34:32 +08:00
className={cn("w-full")}
2026-01-17 19:46:02 +08:00
autoFocus={isNewThread}
status={thread.isLoading ? "streaming" : "ready"}
context={settings.context}
onContextChange={(context) => setSettings("context", context)}
onSubmit={handleSubmit}
onStop={handleStop}
/>
</div>
2026-01-17 11:02:33 +08:00
</div>
</main>
</div>
</ResizablePanel>
<ResizableHandle
className={cn(
"transition-opacity duration-300",
!artifactsOpen && "pointer-events-none opacity-0",
)}
/>
<ResizablePanel
className={cn(
"transition-all duration-300 ease-in-out",
!artifactsOpen && "opacity-0",
)}
defaultSize={artifactsOpen ? 64 : 0}
minSize={0}
>
<div
className={cn(
"h-full transition-transform duration-300 ease-in-out",
artifactsOpen ? "translate-x-0" : "translate-x-full",
)}
>
{selectedArtifact ? (
<ArtifactFileDetail
className="size-full"
filepath={selectedArtifact}
2026-01-17 15:09:44 +08:00
threadId={threadId!}
2026-01-17 11:02:33 +08:00
/>
) : (
<div className="relative flex size-full justify-center">
2026-01-17 15:09:44 +08:00
<div className="absolute top-1 right-1 z-30">
<Button
size="icon-sm"
variant="ghost"
onClick={() => {
setArtifactsOpen(false);
}}
>
<XIcon />
</Button>
</div>
{thread.values.artifacts?.length === 0 ? (
<ConversationEmptyState
icon={<FilesIcon />}
title="No artifact selected"
description="Select an artifact to view its details"
/>
) : (
<div className="flex size-full max-w-(--container-width-sm) flex-col justify-center p-4 pt-8">
<header className="shrink-0">
<h2 className="text-lg font-medium">Artifacts</h2>
</header>
<main className="min-h-0 grow">
<ArtifactFileList
className="max-w-(--container-width-sm) p-4 pt-12"
files={thread.values.artifacts ?? []}
threadId={threadId!}
/>
</main>
</div>
)}
2026-01-17 11:02:33 +08:00
</div>
)}
2026-01-17 00:02:03 +08:00
</div>
</ResizablePanel>
</ResizablePanelGroup>
2026-01-16 09:15:04 +08:00
);
}