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

294 lines
10 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-18 17:13:15 +08:00
import { ThreadContext } from "@/components/workspace/messages/context";
2026-01-17 17:37:12 +08:00
import { ThreadTitle } from "@/components/workspace/thread-title";
2026-01-22 00:26:11 +08:00
import { TodoList } from "@/components/workspace/todo-list";
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-20 14:06:47 +08:00
import { useI18n } from "@/core/i18n/hooks";
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-24 18:01:27 +08:00
import { env } from "@/env";
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-20 14:06:47 +08:00
const { t } = useI18n();
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-24 23:51:11 +08:00
select: selectArtifact,
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
2026-01-24 23:59:41 +08:00
const [autoSelectFirstArtifact, setAutoSelectFirstArtifact] = useState(true);
useEffect(() => {
setArtifacts(thread.values.artifacts);
2026-01-24 23:59:41 +08:00
if (
env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" &&
autoSelectFirstArtifact
) {
2026-01-24 23:51:11 +08:00
if (thread?.values?.artifacts?.length > 0) {
2026-01-24 23:59:41 +08:00
setAutoSelectFirstArtifact(false);
2026-01-24 23:51:11 +08:00
selectArtifact(thread.values.artifacts[0]!);
}
}
2026-01-24 23:59:41 +08:00
}, [
autoSelectFirstArtifact,
selectArtifact,
setArtifacts,
thread.values.artifacts,
]);
2026-01-25 21:57:57 +08:00
const artifactPanelOpen = useMemo(() => {
if (env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true") {
return artifactsOpen && artifacts?.length > 0;
}
return artifactsOpen;
}, [artifactsOpen, artifacts]);
2026-01-24 23:51:11 +08:00
const [todoListCollapsed, setTodoListCollapsed] = useState(
env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true",
);
2026-01-22 00:26:11 +08:00
2026-01-16 19:51:39 +08:00
const handleSubmit = useSubmitThread({
isNewThread,
threadId,
thread,
threadContext: {
...settings.context,
thinking_enabled: settings.context.mode !== "flash",
is_plan_mode: settings.context.mode === "pro",
},
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-18 17:13:15 +08:00
if (!threadId) {
return null;
}
2026-01-16 09:15:04 +08:00
return (
2026-01-18 17:13:15 +08:00
<ThreadContext.Provider value={{ threadId, thread }}>
<ResizablePanelGroup orientation="horizontal">
<ResizablePanel
className="relative"
2026-01-25 21:57:57 +08:00
defaultSize={artifactPanelOpen ? 46 : 100}
minSize={artifactPanelOpen ? 30 : 100}
2026-01-18 17:13:15 +08:00
>
<div className="relative flex size-full min-h-0 justify-between">
2026-01-21 10:31:54 +08:00
<header
className={cn(
"absolute top-0 right-0 left-0 z-30 flex h-12 shrink-0 items-center px-4",
isNewThread
? "bg-background/0 backdrop-blur-none"
2026-01-27 13:15:49 +08:00
: "bg-background/80 shadow-xs backdrop-blur",
2026-01-21 10:31:54 +08:00
)}
>
2026-01-18 17:13:15 +08:00
<div className="flex w-full items-center text-sm font-medium">
{title !== "Untitled" && (
<ThreadTitle threadId={threadId} threadTitle={title} />
)}
</div>
<div>
{artifacts?.length > 0 && !artifactsOpen && (
2026-01-18 17:13:15 +08:00
<Tooltip content="Show artifacts of this conversation">
<Button
variant="ghost"
onClick={() => {
setArtifactsOpen(true);
setSidebarOpen(false);
}}
>
<FilesIcon />
2026-01-20 14:06:47 +08:00
{t.common.artifacts}
2026-01-18 17:13:15 +08:00
</Button>
</Tooltip>
)}
2026-01-18 17:13:15 +08:00
</div>
</header>
<main className="flex min-h-0 grow flex-col">
<div className="flex size-full justify-center">
<MessageList
2026-01-27 13:15:49 +08:00
className={cn("size-full", !isNewThread && "pt-10")}
2026-01-18 17:13:15 +08:00
threadId={threadId}
thread={thread}
2026-01-22 00:26:11 +08:00
paddingBottom={todoListCollapsed ? 160 : 280}
2026-01-18 17:13:15 +08:00
/>
</div>
<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-18 17:13:15 +08:00
"relative w-full",
2026-01-22 13:43:45 +08:00
isNewThread && "-translate-y-[calc(50vh-160px)]",
2026-01-18 17:13:15 +08:00
isNewThread
? "max-w-(--container-width-sm)"
: "max-w-(--container-width-md)",
2026-01-17 19:46:02 +08:00
)}
>
2026-01-22 09:41:01 +08:00
<div className="absolute -top-4 right-0 left-0 z-0">
2026-01-22 11:42:25 +08:00
<div className="absolute right-0 bottom-0 left-0">
2026-01-22 09:41:01 +08:00
<TodoList
className="bg-background/5"
todos={thread.values.todos ?? []}
collapsed={todoListCollapsed}
hidden={
!thread.values.todos ||
thread.values.todos.length === 0
}
onToggle={() =>
setTodoListCollapsed(!todoListCollapsed)
}
/>
</div>
</div>
2026-01-18 17:13:15 +08:00
<InputBox
2026-01-22 00:26:11 +08:00
className={cn("bg-background/5 w-full -translate-y-4")}
2026-01-22 14:28:10 +08:00
isNewThread={isNewThread}
2026-01-18 17:13:15 +08:00
autoFocus={isNewThread}
status={thread.isLoading ? "streaming" : "ready"}
context={settings.context}
2026-01-22 13:43:45 +08:00
extraHeader={isNewThread && <Welcome />}
2026-01-24 18:01:27 +08:00
disabled={env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true"}
2026-01-18 17:13:15 +08:00
onContextChange={(context) =>
setSettings("context", context)
}
onSubmit={handleSubmit}
onStop={handleStop}
/>
2026-01-24 18:01:27 +08:00
{env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && (
<div className="text-muted-foreground/67 w-full -translate-y-2 text-center text-xs">
{t.common.notAvailableInDemoMode}
</div>
)}
2026-01-17 19:46:02 +08:00
</div>
</div>
2026-01-18 17:13:15 +08:00
</main>
</div>
</ResizablePanel>
<ResizableHandle
2026-01-17 11:02:33 +08:00
className={cn(
2026-01-21 08:50:15 +08:00
"opacity-33 hover:opacity-100",
2026-01-25 21:57:57 +08:00
!artifactPanelOpen && "pointer-events-none opacity-0",
2026-01-17 11:02:33 +08:00
)}
2026-01-18 17:13:15 +08:00
/>
<ResizablePanel
className={cn(
"transition-all duration-300 ease-in-out",
!artifactsOpen && "opacity-0",
)}
2026-01-25 21:57:57 +08:00
defaultSize={artifactPanelOpen ? 64 : 0}
2026-01-18 17:13:15 +08:00
minSize={0}
2026-01-25 21:57:57 +08:00
maxSize={artifactPanelOpen ? undefined : 0}
2026-01-17 11:02:33 +08:00
>
2026-01-18 17:13:15 +08:00
<div
className={cn(
2026-01-21 08:50:15 +08:00
"h-full p-4 transition-transform duration-300 ease-in-out",
2026-01-25 21:57:57 +08:00
artifactPanelOpen ? "translate-x-0" : "translate-x-full",
2026-01-18 17:13:15 +08:00
)}
>
{selectedArtifact ? (
<ArtifactFileDetail
className="size-full"
filepath={selectedArtifact}
threadId={threadId}
/>
) : (
<div className="relative flex size-full justify-center">
<div className="absolute top-1 right-1 z-30">
<Button
size="icon-sm"
variant="ghost"
onClick={() => {
setArtifactsOpen(false);
}}
>
<XIcon />
</Button>
</div>
2026-01-18 17:13:15 +08:00
{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>
)}
</div>
)}
</div>
</ResizablePanel>
</ResizablePanelGroup>
</ThreadContext.Provider>
2026-01-16 09:15:04 +08:00
);
}