fix: rehydrate canvas chat history reliably on initial open
This commit is contained in:
parent
2ce9ef0d1f
commit
d8d62edf53
@ -597,6 +597,7 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
|||||||
const autoContextRef = useRef<string | null>(null);
|
const autoContextRef = useRef<string | null>(null);
|
||||||
const pendingLaunchRef = useRef<CanvasLaunchPayload | null>(null);
|
const pendingLaunchRef = useRef<CanvasLaunchPayload | null>(null);
|
||||||
const sessionMenuRef = useRef<HTMLDivElement>(null);
|
const sessionMenuRef = useRef<HTMLDivElement>(null);
|
||||||
|
const historyScrollTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
// 🆕 标签悬停预览状态
|
// 🆕 标签悬停预览状态
|
||||||
const [tagPreview, setTagPreview] = useState<TagPreviewInfo | null>(null);
|
const [tagPreview, setTagPreview] = useState<TagPreviewInfo | null>(null);
|
||||||
@ -1597,15 +1598,41 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
|||||||
}
|
}
|
||||||
}, [selectedModelId, status, sendMessage, clearSelection]);
|
}, [selectedModelId, status, sendMessage, clearSelection]);
|
||||||
|
|
||||||
// 当历史消息加载完成后,更新消息列表
|
// 当历史消息加载完成后,更新消息列表(支持同一会话的增量刷新)
|
||||||
const hasLoadedHistory = useRef(false);
|
const loadedHistorySignatureRef = useRef<string>("");
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (historyData && !hasLoadedHistory.current) {
|
if (historyScrollTimerRef.current) {
|
||||||
hasLoadedHistory.current = true;
|
clearTimeout(historyScrollTimerRef.current);
|
||||||
setMessages(historyData.messages || []);
|
historyScrollTimerRef.current = null;
|
||||||
console.log('[CanvasChat] 已加载历史消息:', historyData.messages?.length || 0);
|
|
||||||
}
|
}
|
||||||
}, [historyData, setMessages]);
|
|
||||||
|
if (!historyData || !rawChatId) return;
|
||||||
|
if (status === "streaming" || status === "submitted") return;
|
||||||
|
|
||||||
|
const historyMessages = historyData.messages || [];
|
||||||
|
const lastMessageId = historyMessages.at(-1)?.id || "none";
|
||||||
|
const signature = `${rawChatId}:${historyMessages.length}:${lastMessageId}`;
|
||||||
|
|
||||||
|
if (loadedHistorySignatureRef.current === signature) return;
|
||||||
|
|
||||||
|
loadedHistorySignatureRef.current = signature;
|
||||||
|
setMessages(historyMessages);
|
||||||
|
console.log("[CanvasChat] 已同步历史消息:", historyMessages.length, signature);
|
||||||
|
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "auto", block: "end" });
|
||||||
|
});
|
||||||
|
historyScrollTimerRef.current = setTimeout(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "auto", block: "end" });
|
||||||
|
historyScrollTimerRef.current = null;
|
||||||
|
}, 160);
|
||||||
|
return () => {
|
||||||
|
if (historyScrollTimerRef.current) {
|
||||||
|
clearTimeout(historyScrollTimerRef.current);
|
||||||
|
historyScrollTimerRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [historyData, rawChatId, setMessages, status]);
|
||||||
|
|
||||||
// 监听消息变化,提取图片并添加到画布
|
// 监听消息变化,提取图片并添加到画布
|
||||||
// 只处理新生成的图片,不处理历史消息中的图片
|
// 只处理新生成的图片,不处理历史消息中的图片
|
||||||
@ -1613,7 +1640,7 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
|||||||
const isInitialLoad = useRef(true);
|
const isInitialLoad = useRef(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
hasLoadedHistory.current = false;
|
loadedHistorySignatureRef.current = "";
|
||||||
processedMessageIds.current.clear();
|
processedMessageIds.current.clear();
|
||||||
isInitialLoad.current = true;
|
isInitialLoad.current = true;
|
||||||
hiddenPromptRef.current = null;
|
hiddenPromptRef.current = null;
|
||||||
@ -3175,6 +3202,14 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
|||||||
}
|
}
|
||||||
return part;
|
return part;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const fallbackContent =
|
||||||
|
typeof (message as any).content === "string"
|
||||||
|
? (message as any).content.trim()
|
||||||
|
: "";
|
||||||
|
if (cleanedParts.length === 0 && fallbackContent) {
|
||||||
|
cleanedParts.push({ type: "text", text: fallbackContent } as any);
|
||||||
|
}
|
||||||
|
|
||||||
// 检查清理后是否有实际内容(更严格的检查)
|
// 检查清理后是否有实际内容(更严格的检查)
|
||||||
let hasContent = false;
|
let hasContent = false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user