fix: session history not loading + new session showing stale messages
Bug 1 - Session opens with empty messages: - loadChatHistory effect was re-running on every currentSessionId change, racing with openSessionById and overwriting loaded messages - Removed currentSessionId from effect dependencies (only runs on mount/postId) - Added recovery: if session has 0 messages but has post_id, try fetching from the post-based conversation endpoint as fallback Bug 2 - Start New Session shows old messages: - startNewConversation now sets isHydratingSessionRef=true before changing session state, preventing the persistence effect from saving stale data - Fully resets: messages, plan, agentMode, keyword suggestions, providerInfo - loadPostSessions called AFTER state reset to avoid stale renders Also fixed: - Legacy fallback now only fires when no session was resolved at all (prevents loading old post_meta data over session data)
This commit is contained in:
@@ -1136,6 +1136,11 @@
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const loadChatHistory = async () => {
|
const loadChatHistory = async () => {
|
||||||
|
// Skip if we already have a session loaded (e.g., from openSessionById)
|
||||||
|
if (messages.length > 0 || isHydratingSessionRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const headers = {
|
const headers = {
|
||||||
'X-WP-Nonce': wpAgenticWriter.nonce,
|
'X-WP-Nonce': wpAgenticWriter.nonce,
|
||||||
@@ -1148,7 +1153,7 @@
|
|||||||
if (sessions.length > 0) {
|
if (sessions.length > 0) {
|
||||||
if (sessions.length > 0) {
|
if (sessions.length > 0) {
|
||||||
let selected = sessions[0];
|
let selected = sessions[0];
|
||||||
const preferred = currentSessionId || (() => {
|
const preferred = (() => {
|
||||||
try {
|
try {
|
||||||
return window.localStorage.getItem(`wpawSessionId_${postId}`) || '';
|
return window.localStorage.getItem(`wpawSessionId_${postId}`) || '';
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -1185,8 +1190,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy endpoint fallback.
|
// Legacy endpoint fallback - only if no session found at all.
|
||||||
if (postId && historyMessages.length === 0) {
|
if (postId && historyMessages.length === 0 && !resolvedSessionId) {
|
||||||
const legacy = await fetch(`${wpAgenticWriter.apiUrl}/chat-history/${postId}`, {
|
const legacy = await fetch(`${wpAgenticWriter.apiUrl}/chat-history/${postId}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers,
|
headers,
|
||||||
@@ -1216,7 +1221,9 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadChatHistory();
|
loadChatHistory();
|
||||||
}, [postId, currentSessionId, sanitizeMessagesForStorage, hydrateSessionStateFromMessages]);
|
// Only run on mount / postId change — NOT on currentSessionId change
|
||||||
|
// Session switches are handled by openSessionById directly
|
||||||
|
}, [postId]);
|
||||||
|
|
||||||
const loadPostSessions = async () => {
|
const loadPostSessions = async () => {
|
||||||
const headers = {
|
const headers = {
|
||||||
@@ -1316,9 +1323,24 @@
|
|||||||
setCurrentSessionId(sessionId);
|
setCurrentSessionId(sessionId);
|
||||||
const sessionMessages = Array.isArray(data?.messages) ? data.messages : [];
|
const sessionMessages = Array.isArray(data?.messages) ? data.messages : [];
|
||||||
|
|
||||||
// If session has no messages but has a post_id, it may have been improperly persisted
|
// If session has no messages, try fetching from the conversations/post endpoint
|
||||||
if (sessionMessages.length === 0) {
|
// as a recovery mechanism (messages may be stored under post relationship)
|
||||||
wpawLog.warn('Session loaded with 0 messages:', sessionId);
|
if (sessionMessages.length === 0 && data?.post_id && Number(data.post_id) > 0) {
|
||||||
|
wpawLog.warn('Session has 0 messages, attempting post-based recovery:', sessionId);
|
||||||
|
try {
|
||||||
|
const postSessionRes = await fetch(`${wpAgenticWriter.apiUrl}/conversation/${data.post_id}`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
if (postSessionRes.ok) {
|
||||||
|
const postSessionData = await postSessionRes.json();
|
||||||
|
if (Array.isArray(postSessionData?.messages) && postSessionData.messages.length > 0) {
|
||||||
|
sessionMessages.push(...postSessionData.messages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Non-fatal recovery attempt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lastPersistedMessagesRef.current = JSON.stringify(sanitizeMessagesForStorage(sessionMessages));
|
lastPersistedMessagesRef.current = JSON.stringify(sanitizeMessagesForStorage(sessionMessages));
|
||||||
@@ -5627,13 +5649,27 @@
|
|||||||
throw new Error('Failed to create a new conversation');
|
throw new Error('Failed to create a new conversation');
|
||||||
}
|
}
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
// Fully reset state for clean slate
|
||||||
|
isHydratingSessionRef.current = true;
|
||||||
if (data?.session_id) {
|
if (data?.session_id) {
|
||||||
setCurrentSessionId(data.session_id);
|
setCurrentSessionId(data.session_id);
|
||||||
}
|
}
|
||||||
await loadPostSessions();
|
|
||||||
lastPersistedMessagesRef.current = JSON.stringify([]);
|
lastPersistedMessagesRef.current = JSON.stringify([]);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
|
currentPlanRef.current = null;
|
||||||
|
setAgentMode('chat');
|
||||||
setShowWelcome(false);
|
setShowWelcome(false);
|
||||||
|
setFocusKeywordSuggestions([]);
|
||||||
|
setSelectedFocusKeyword('');
|
||||||
|
setProviderInfo(null);
|
||||||
|
await loadPostSessions();
|
||||||
|
setTimeout(() => {
|
||||||
|
isHydratingSessionRef.current = false;
|
||||||
|
// Focus input
|
||||||
|
if (inputRef.current) {
|
||||||
|
inputRef.current.focus();
|
||||||
|
}
|
||||||
|
}, 50);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setMessages(prev => [...prev, {
|
setMessages(prev => [...prev, {
|
||||||
role: 'system',
|
role: 'system',
|
||||||
|
|||||||
Reference in New Issue
Block a user