mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-19 04:14:46 +08:00
Citations:
- In shouldShowCitationLoading, treat any unreplaced [cite-N] in cleanContent
as show-loading (no body). Fixes Ultra and other modes when refs arrive
before the <citations> block in the stream.
- Single rule: hasUnreplacedCitationRefs(cleanContent) => true forces loading;
then isLoading && hasCitationsBlock(rawContent) for streaming indicator.
SSE end state:
- When stream finishes, SDK may set isLoading=false before client state has
the final message content, so UI stayed wrong until refresh.
- Store onFinish(state) as finalState in chat page; clear when stream starts.
- Pass messagesOverride={finalState.messages} to MessageList when not loading
so the list uses server-complete messages right after SSE ends (no refresh).
Chore:
- Stop tracking .githooks/pre-commit; add .githooks/ to .gitignore (local only).
Co-authored-by: Cursor <cursoragent@cursor.com>
---
fix(前端): 杜绝半成品引用,SSE 结束时展示正确状态
引用:
- shouldShowCitationLoading 中只要 cleanContent 仍含未替换的 [cite-N] 就
只显示加载、不渲染正文,解决流式时引用块未到就出现 [cite-1] 的问题。
- 规则:hasUnreplacedCitationRefs(cleanContent) 为真则一律显示加载;
此外 isLoading && hasCitationsBlock 用于流式时显示「正在整理引用」。
SSE 结束状态:
- 流结束时 SDK 可能先置 isLoading=false,客户端 messages 尚未包含
最终内容,导致需刷新才显示正确。
- 在对话页保存 onFinish(state) 为 finalState,流开始时清空。
- 非加载时向 MessageList 传入 messagesOverride={finalState.messages},
列表在 SSE 结束后立即用服务端完整消息渲染,无需刷新。
杂项:
- 取消跟踪 .githooks/pre-commit,.gitignore 增加 .githooks/(仅本地)。
376 lines
13 KiB
TypeScript
376 lines
13 KiB
TypeScript
"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<string | null>(null);
|
|
useEffect(() => {
|
|
if (threadIdFromPath !== "new") {
|
|
setThreadId(threadIdFromPath);
|
|
} else {
|
|
setThreadId(uuid());
|
|
}
|
|
}, [threadIdFromPath]);
|
|
|
|
const { showNotification } = useNotification();
|
|
const [finalState, setFinalState] = useState<AgentThreadState | null>(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 (
|
|
<ThreadContext.Provider value={{ threadId, thread }}>
|
|
<ResizablePanelGroup orientation="horizontal">
|
|
<ResizablePanel
|
|
className="relative"
|
|
defaultSize={artifactPanelOpen ? 46 : 100}
|
|
minSize={artifactPanelOpen ? 30 : 100}
|
|
>
|
|
<div className="relative flex size-full min-h-0 justify-between">
|
|
<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"
|
|
: "bg-background/80 shadow-xs backdrop-blur",
|
|
)}
|
|
>
|
|
<div className="flex w-full items-center text-sm font-medium">
|
|
{title !== "Untitled" && (
|
|
<ThreadTitle threadId={threadId} threadTitle={title} />
|
|
)}
|
|
</div>
|
|
<div>
|
|
{artifacts?.length > 0 && !artifactsOpen && (
|
|
<Tooltip content="Show artifacts of this conversation">
|
|
<Button
|
|
className="text-muted-foreground hover:text-foreground"
|
|
variant="ghost"
|
|
onClick={() => {
|
|
setArtifactsOpen(true);
|
|
setSidebarOpen(false);
|
|
}}
|
|
>
|
|
<FilesIcon />
|
|
{t.common.artifacts}
|
|
</Button>
|
|
</Tooltip>
|
|
)}
|
|
</div>
|
|
</header>
|
|
<main className="flex min-h-0 max-w-full grow flex-col">
|
|
<div className="flex size-full justify-center">
|
|
<MessageList
|
|
className={cn("size-full", !isNewThread && "pt-10")}
|
|
threadId={threadId}
|
|
thread={thread}
|
|
messagesOverride={
|
|
!thread.isLoading && finalState?.messages
|
|
? (finalState.messages as Message[])
|
|
: undefined
|
|
}
|
|
paddingBottom={todoListCollapsed ? 160 : 280}
|
|
/>
|
|
</div>
|
|
<div className="absolute right-0 bottom-0 left-0 z-30 flex justify-center px-4">
|
|
<div
|
|
className={cn(
|
|
"relative w-full",
|
|
isNewThread && "-translate-y-[calc(50vh-96px)]",
|
|
isNewThread
|
|
? "max-w-(--container-width-sm)"
|
|
: "max-w-(--container-width-md)",
|
|
)}
|
|
>
|
|
<div className="absolute -top-4 right-0 left-0 z-0">
|
|
<div className="absolute right-0 bottom-0 left-0">
|
|
<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>
|
|
<InputBox
|
|
className={cn("bg-background/5 w-full -translate-y-4")}
|
|
isNewThread={isNewThread}
|
|
autoFocus={isNewThread}
|
|
status={thread.isLoading ? "streaming" : "ready"}
|
|
context={settings.context}
|
|
extraHeader={
|
|
isNewThread && <Welcome mode={settings.context.mode} />
|
|
}
|
|
disabled={env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true"}
|
|
onContextChange={(context) =>
|
|
setSettings("context", context)
|
|
}
|
|
onSubmit={handleSubmit}
|
|
onStop={handleStop}
|
|
/>
|
|
{env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && (
|
|
<div className="text-muted-foreground/67 w-full translate-y-12 text-center text-xs">
|
|
{t.common.notAvailableInDemoMode}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
</ResizablePanel>
|
|
<ResizableHandle
|
|
className={cn(
|
|
"opacity-33 hover:opacity-100",
|
|
!artifactPanelOpen && "pointer-events-none opacity-0",
|
|
)}
|
|
/>
|
|
<ResizablePanel
|
|
className={cn(
|
|
"transition-all duration-300 ease-in-out",
|
|
!artifactsOpen && "opacity-0",
|
|
)}
|
|
defaultSize={artifactPanelOpen ? 64 : 0}
|
|
minSize={0}
|
|
maxSize={artifactPanelOpen ? undefined : 0}
|
|
>
|
|
<div
|
|
className={cn(
|
|
"h-full p-4 transition-transform duration-300 ease-in-out",
|
|
artifactPanelOpen ? "translate-x-0" : "translate-x-full",
|
|
)}
|
|
>
|
|
{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>
|
|
{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>
|
|
);
|
|
}
|