fix: avoid rehydrating historical chat images into canvas
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-19 14:03:03 +08:00
parent ddadac3a67
commit 06c0b63a27

View File

@ -1889,6 +1889,7 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
role: "user", role: "user",
parts, parts,
}); });
hasHydratedHistoryRef.current = true;
pendingLaunchRef.current = null; pendingLaunchRef.current = null;
setInput(""); setInput("");
@ -1919,6 +1920,12 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
if (loadedHistorySignatureRef.current === signature) return; if (loadedHistorySignatureRef.current === signature) return;
loadedHistorySignatureRef.current = signature; loadedHistorySignatureRef.current = signature;
processedMessageIds.current = new Set(
historyMessages
.filter((message) => message.role === "assistant")
.map((message) => message.id)
);
hasHydratedHistoryRef.current = true;
setMessages(historyMessages); setMessages(historyMessages);
setMessageViewportKey((prev) => prev + 1); setMessageViewportKey((prev) => prev + 1);
console.log("[CanvasChat] 已同步历史消息:", historyMessages.length, signature); console.log("[CanvasChat] 已同步历史消息:", historyMessages.length, signature);
@ -1939,14 +1946,14 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
}, [historyData, rawChatId, setMessages, status]); }, [historyData, rawChatId, setMessages, status]);
// 监听消息变化,提取图片并添加到画布 // 监听消息变化,提取图片并添加到画布
// 只处理新生成的图片,不处理历史消息中的图片 // 仅处理历史同步后的新消息,避免刷新后把历史图片重复回灌到画布
const processedMessageIds = useRef<Set<string>>(new Set()); const processedMessageIds = useRef<Set<string>>(new Set());
const isInitialLoad = useRef(true); const hasHydratedHistoryRef = useRef(false);
useEffect(() => { useEffect(() => {
loadedHistorySignatureRef.current = ""; loadedHistorySignatureRef.current = "";
processedMessageIds.current.clear(); processedMessageIds.current.clear();
isInitialLoad.current = true; hasHydratedHistoryRef.current = false;
hiddenPromptRef.current = null; hiddenPromptRef.current = null;
autoContextRef.current = null; autoContextRef.current = null;
pendingLaunchRef.current = null; pendingLaunchRef.current = null;
@ -2004,18 +2011,8 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
}, [activeChatId, messages]); }, [activeChatId, messages]);
useEffect(() => { useEffect(() => {
// 首次加载历史消息时,只记录已处理的消息 ID,不添加图片到画布 if (!hasHydratedHistoryRef.current) return;
// (画布数据应该从 canvasData 中加载,而不是从消息中提取)
if (isInitialLoad.current) {
messages.forEach(message => {
if (message.role === 'assistant') {
processedMessageIds.current.add(message.id);
}
});
isInitialLoad.current = false;
return;
}
// 只处理新消息 // 只处理新消息
messages.forEach(async (message, messageIndex) => { messages.forEach(async (message, messageIndex) => {
if (message.role === 'assistant' && !processedMessageIds.current.has(message.id)) { if (message.role === 'assistant' && !processedMessageIds.current.has(message.id)) {
@ -3548,6 +3545,7 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
role: "user", role: "user",
parts: parts, parts: parts,
}); });
hasHydratedHistoryRef.current = true;
// 清空状态 // 清空状态
setInput(""); setInput("");