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