From f3fe30380c6176eda93888d7998dafdeb5094fca Mon Sep 17 00:00:00 2001 From: Tat Dat Duong Date: Tue, 11 Feb 2025 09:01:39 -0800 Subject: [PATCH] Make sure we clear when switching threads --- libs/sdk-js/src/react/stream.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libs/sdk-js/src/react/stream.tsx b/libs/sdk-js/src/react/stream.tsx index 1726b5920..047eff812 100644 --- a/libs/sdk-js/src/react/stream.tsx +++ b/libs/sdk-js/src/react/stream.tsx @@ -145,6 +145,7 @@ function fetchHistory>( function useThreadHistory>( threadId: string | undefined | null, client: Client, + clearCallbackRef: MutableRefObject<(() => void) | undefined>, submittingRef: MutableRefObject, ) { const [history, setHistory] = useState[]>([]); @@ -161,6 +162,7 @@ function useThreadHistory>( } setHistory([]); + clearCallbackRef.current?.(); return Promise.resolve([]); }, [], @@ -238,9 +240,20 @@ export function useStream< [], ); + const clearCallbackRef = useRef<() => void>(null!); + clearCallbackRef.current = () => { + setStreamError(undefined); + setStreamValues(null); + }; + // TODO: this should be done on the server to avoid pagination // TODO: should we permit adapter? SWR / React Query? - const history = useThreadHistory(threadId, client, submittingRef); + const history = useThreadHistory( + threadId, + client, + clearCallbackRef, + submittingRef, + ); const getMessages = useMemo(() => { if (withMessages == null) return undefined;