mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-24 06:34: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/(仅本地)。
209 lines
7.4 KiB
TypeScript
209 lines
7.4 KiB
TypeScript
import type { UseStream } from "@langchain/langgraph-sdk/react";
|
|
|
|
import {
|
|
Conversation,
|
|
ConversationContent,
|
|
} from "@/components/ai-elements/conversation";
|
|
import { useI18n } from "@/core/i18n/hooks";
|
|
import {
|
|
extractContentFromMessage,
|
|
extractPresentFilesFromMessage,
|
|
extractTextFromMessage,
|
|
groupMessages,
|
|
hasContent,
|
|
hasPresentFiles,
|
|
hasReasoning,
|
|
} from "@/core/messages/utils";
|
|
import { useRehypeSplitWordsIntoSpans } from "@/core/rehype";
|
|
import type { Subtask } from "@/core/tasks";
|
|
import { useUpdateSubtask } from "@/core/tasks/context";
|
|
import type { AgentThreadState } from "@/core/threads";
|
|
import type { Message } from "@langchain/langgraph-sdk";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
import { ArtifactFileList } from "../artifacts/artifact-file-list";
|
|
import { StreamingIndicator } from "../streaming-indicator";
|
|
|
|
import { MessageGroup } from "./message-group";
|
|
import { MessageListItem } from "./message-list-item";
|
|
import { SafeCitationContent } from "./safe-citation-content";
|
|
import { MessageListSkeleton } from "./skeleton";
|
|
import { SubtaskCard } from "./subtask-card";
|
|
|
|
export function MessageList({
|
|
className,
|
|
threadId,
|
|
thread,
|
|
messagesOverride,
|
|
paddingBottom = 160,
|
|
}: {
|
|
className?: string;
|
|
threadId: string;
|
|
thread: UseStream<AgentThreadState>;
|
|
/** When set (e.g. from onFinish), use instead of thread.messages so SSE end shows complete state. */
|
|
messagesOverride?: Message[];
|
|
paddingBottom?: number;
|
|
}) {
|
|
const { t } = useI18n();
|
|
const rehypePlugins = useRehypeSplitWordsIntoSpans(thread.isLoading);
|
|
const updateSubtask = useUpdateSubtask();
|
|
const messages = messagesOverride ?? thread.messages;
|
|
if (thread.isThreadLoading) {
|
|
return <MessageListSkeleton />;
|
|
}
|
|
return (
|
|
<Conversation
|
|
className={cn("flex size-full flex-col justify-center", className)}
|
|
>
|
|
<ConversationContent className="mx-auto w-full max-w-(--container-width-md) gap-8 pt-12">
|
|
{groupMessages(messages, (group) => {
|
|
if (group.type === "human" || group.type === "assistant") {
|
|
return (
|
|
<MessageListItem
|
|
key={group.id}
|
|
message={group.messages[0]!}
|
|
isLoading={thread.isLoading}
|
|
/>
|
|
);
|
|
} else if (group.type === "assistant:clarification") {
|
|
const message = group.messages[0];
|
|
if (message && hasContent(message)) {
|
|
return (
|
|
<SafeCitationContent
|
|
key={group.id}
|
|
content={extractContentFromMessage(message)}
|
|
isLoading={thread.isLoading}
|
|
rehypePlugins={rehypePlugins}
|
|
/>
|
|
);
|
|
}
|
|
return null;
|
|
} else if (group.type === "assistant:present-files") {
|
|
const files: string[] = [];
|
|
for (const message of group.messages) {
|
|
if (hasPresentFiles(message)) {
|
|
const presentFiles = extractPresentFilesFromMessage(message);
|
|
files.push(...presentFiles);
|
|
}
|
|
}
|
|
return (
|
|
<div className="w-full" key={group.id}>
|
|
{group.messages[0] && hasContent(group.messages[0]) && (
|
|
<SafeCitationContent
|
|
content={extractContentFromMessage(group.messages[0])}
|
|
isLoading={thread.isLoading}
|
|
rehypePlugins={rehypePlugins}
|
|
className="mb-4"
|
|
/>
|
|
)}
|
|
<ArtifactFileList files={files} threadId={threadId} />
|
|
</div>
|
|
);
|
|
} else if (group.type === "assistant:subagent") {
|
|
const tasks = new Set<Subtask>();
|
|
for (const message of group.messages) {
|
|
if (message.type === "ai") {
|
|
for (const toolCall of message.tool_calls ?? []) {
|
|
if (toolCall.name === "task") {
|
|
const task: Subtask = {
|
|
id: toolCall.id!,
|
|
subagent_type: toolCall.args.subagent_type,
|
|
description: toolCall.args.description,
|
|
prompt: toolCall.args.prompt,
|
|
status: "in_progress",
|
|
};
|
|
updateSubtask(task);
|
|
tasks.add(task);
|
|
}
|
|
}
|
|
} else if (message.type === "tool") {
|
|
const taskId = message.tool_call_id;
|
|
if (taskId) {
|
|
const result = extractTextFromMessage(message);
|
|
if (result.startsWith("Task Succeeded. Result:")) {
|
|
updateSubtask({
|
|
id: taskId,
|
|
status: "completed",
|
|
result: result
|
|
.split("Task Succeeded. Result:")[1]
|
|
?.trim(),
|
|
});
|
|
} else if (result.startsWith("Task failed.")) {
|
|
updateSubtask({
|
|
id: taskId,
|
|
status: "failed",
|
|
error: result.split("Task failed.")[1]?.trim(),
|
|
});
|
|
} else if (result.startsWith("Task timed out")) {
|
|
updateSubtask({
|
|
id: taskId,
|
|
status: "failed",
|
|
error: result,
|
|
});
|
|
} else {
|
|
updateSubtask({
|
|
id: taskId,
|
|
status: "in_progress",
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const results: React.ReactNode[] = [];
|
|
for (const message of group.messages.filter(
|
|
(message) => message.type === "ai",
|
|
)) {
|
|
if (hasReasoning(message)) {
|
|
results.push(
|
|
<MessageGroup
|
|
key={"thinking-group-" + message.id}
|
|
messages={[message]}
|
|
isLoading={thread.isLoading}
|
|
/>,
|
|
);
|
|
}
|
|
results.push(
|
|
<div
|
|
key="subtask-count"
|
|
className="text-muted-foreground font-norma pt-2 text-sm"
|
|
>
|
|
{t.subtasks.executing(tasks.size)}
|
|
</div>,
|
|
);
|
|
const taskIds = message.tool_calls?.map(
|
|
(toolCall) => toolCall.id,
|
|
);
|
|
for (const taskId of taskIds ?? []) {
|
|
results.push(
|
|
<SubtaskCard
|
|
key={"task-group-" + taskId}
|
|
taskId={taskId!}
|
|
isLoading={thread.isLoading}
|
|
/>,
|
|
);
|
|
}
|
|
}
|
|
return (
|
|
<div
|
|
key={"subtask-group-" + group.id}
|
|
className="relative z-1 flex flex-col gap-2"
|
|
>
|
|
{results}
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<MessageGroup
|
|
key={"group-" + group.id}
|
|
messages={group.messages}
|
|
isLoading={thread.isLoading}
|
|
/>
|
|
);
|
|
})}
|
|
{thread.isLoading && <StreamingIndicator className="my-4" />}
|
|
<div style={{ height: `${paddingBottom}px` }} />
|
|
</ConversationContent>
|
|
</Conversation>
|
|
);
|
|
}
|