Skip to content

Commit 42c656d

Browse files
committed
refactor: simplify thread message handling
1 parent f2dcb6f commit 42c656d

File tree

2 files changed

+6
-93
lines changed

2 files changed

+6
-93
lines changed

frontend/src/app/(dashboard)/projects/[projectId]/thread/_hooks/useThreadData.ts

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ export function useThreadData(threadId: string, projectId: string): UseThreadDat
4141
const agentRunsCheckedRef = useRef(false);
4242
const hasInitiallyScrolled = useRef<boolean>(false);
4343

44-
// Store messages by thread ID to preserve across navigation
45-
const threadMessagesRef = useRef<Map<string, UnifiedMessage[]>>(new Map());
46-
// Track the last active thread id to correctly cache on navigation
47-
const lastThreadIdRef = useRef<string>(threadId);
4844

4945
const threadQuery = useThreadQuery(threadId);
5046
const messagesQuery = useMessagesQuery(threadId);
@@ -56,28 +52,13 @@ export function useThreadData(threadId: string, projectId: string): UseThreadDat
5652
useEffect(() => {
5753
let isMounted = true;
5854

59-
// Store current messages before switching threads using the actual last thread id
60-
const previousThreadId = lastThreadIdRef.current;
61-
if (previousThreadId && previousThreadId !== threadId && messages.length > 0) {
62-
threadMessagesRef.current.set(previousThreadId, [...messages]);
63-
}
64-
6555
// Reset refs when thread changes
66-
// (debug logs removed)
6756
agentRunsCheckedRef.current = false;
6857
messagesLoadedRef.current = false;
6958
initialLoadCompleted.current = false;
7059

71-
// Restore cached messages for this thread if available
72-
const cachedMessages = threadMessagesRef.current.get(threadId);
73-
if (cachedMessages && cachedMessages.length > 0) {
74-
setMessages(cachedMessages);
75-
} else {
76-
setMessages([]);
77-
}
78-
79-
// Update last thread id tracker to the new thread
80-
lastThreadIdRef.current = threadId;
60+
// Clear messages on thread change; fresh data will set messages
61+
setMessages([]);
8162

8263
async function initializeData() {
8364
if (!initialLoadCompleted.current) setIsLoading(true);
@@ -136,9 +117,7 @@ export function useThreadData(threadId: string, projectId: string): UseThreadDat
136117
});
137118

138119
setMessages(mergedMessages);
139-
// Update cache for this thread
140-
threadMessagesRef.current.set(threadId, [...mergedMessages]);
141-
// (debug logs removed)
120+
// Messages set only from server merge; no cross-thread cache
142121
messagesLoadedRef.current = true;
143122

144123
if (!hasInitiallyScrolled.current) {
@@ -257,9 +236,7 @@ export function useThreadData(threadId: string, projectId: string): UseThreadDat
257236
return aTime - bTime;
258237
});
259238

260-
// Update cache for this thread
261-
threadMessagesRef.current.set(threadId, [...merged]);
262-
// (debug logs removed)
239+
// Messages set only from server merge; no cross-thread cache
263240
return merged;
264241
});
265242
} else {
@@ -268,19 +245,9 @@ export function useThreadData(threadId: string, projectId: string): UseThreadDat
268245
}
269246
}, [messagesQuery.data, messagesQuery.status, isLoading, messages.length, threadId]);
270247

271-
// Wrap setMessages to also update the cache
272-
const setMessagesWithCache = (messagesOrUpdater: UnifiedMessage[] | ((prev: UnifiedMessage[]) => UnifiedMessage[])) => {
273-
setMessages((prev) => {
274-
const newMessages = typeof messagesOrUpdater === 'function' ? messagesOrUpdater(prev) : messagesOrUpdater;
275-
// Update cache whenever messages change
276-
threadMessagesRef.current.set(threadId, [...newMessages]);
277-
return newMessages;
278-
});
279-
};
280-
281248
return {
282249
messages,
283-
setMessages: setMessagesWithCache,
250+
setMessages,
284251
project,
285252
sandboxId,
286253
projectName,

frontend/src/hooks/useAgentStream.ts

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -116,59 +116,9 @@ export function useAgentStream(
116116
textContentRef.current = textContent;
117117
}, [textContent]);
118118

119-
// Update refs if threadId or setMessages changes
119+
// Update refs if threadId changes (no persistence across navigation)
120120
useEffect(() => {
121-
const previousThreadId = threadIdRef.current;
122-
123-
// If we're switching threads and have an active stream, persist it and clean up
124-
if (previousThreadId && previousThreadId !== threadId && streamCleanupRef.current) {
125-
// Use refs to get current state values
126-
const currentState = {
127-
status: statusRef.current,
128-
agentRunId: agentRunIdRef.current,
129-
textContent: textContentRef.current,
130-
timestamp: Date.now()
131-
};
132-
sessionStorage.setItem(`stream_state_${previousThreadId}`, JSON.stringify(currentState));
133-
134-
// Clean up current stream
135-
streamCleanupRef.current();
136-
streamCleanupRef.current = null;
137-
setStatus('idle');
138-
setTextContent([]);
139-
setToolCall(null);
140-
setAgentRunId(null);
141-
currentRunIdRef.current = null;
142-
}
143-
144121
threadIdRef.current = threadId;
145-
146-
// Check if we have persisted stream state for this thread
147-
const persistedKey = `stream_state_${threadId}`;
148-
const persistedState = sessionStorage.getItem(persistedKey);
149-
150-
if (persistedState && previousThreadId !== threadId) {
151-
try {
152-
const parsed = JSON.parse(persistedState);
153-
const stateAge = Date.now() - (parsed.timestamp || 0);
154-
155-
// Only restore if state is recent (< 5 minutes) and was streaming
156-
if (parsed.status === 'streaming' && parsed.agentRunId && stateAge < 5 * 60 * 1000) {
157-
setStatus('streaming');
158-
setAgentRunId(parsed.agentRunId);
159-
currentRunIdRef.current = parsed.agentRunId;
160-
setTextContent(parsed.textContent || []);
161-
162-
// Clear the persisted state since we've restored it
163-
sessionStorage.removeItem(persistedKey);
164-
} else {
165-
sessionStorage.removeItem(persistedKey);
166-
}
167-
} catch (e) {
168-
console.warn('Failed to parse persisted stream state:', e);
169-
sessionStorage.removeItem(persistedKey);
170-
}
171-
}
172122
}, [threadId]);
173123

174124
useEffect(() => {
@@ -240,10 +190,6 @@ export function useAgentStream(
240190
setAgentRunId(null);
241191
currentRunIdRef.current = null;
242192

243-
// Clear persisted state when stream completes
244-
const persistedKey = `stream_state_${currentThreadId}`;
245-
sessionStorage.removeItem(persistedKey);
246-
247193
// Message refetch disabled - optimistic messages will handle updates
248194

249195
// If the run was stopped or completed, try to get final status to update nonRunning set (keep this)

0 commit comments

Comments
 (0)