From 0418c7370063ecd78f4cd451399f63940b3fd8b7 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Fri, 26 Jul 2024 16:27:48 -0700 Subject: [PATCH] fix(runtimes/external-store): invalidate cache when isRunning changes during autoStatus (#584) --- .changeset/chatty-balloons-film.md | 5 +++++ .../react/src/runtimes/external-store/auto-status.tsx | 5 ----- .../runtimes/external-store/useExternalStoreSync.tsx | 11 ++--------- 3 files changed, 7 insertions(+), 14 deletions(-) create mode 100644 .changeset/chatty-balloons-film.md diff --git a/.changeset/chatty-balloons-film.md b/.changeset/chatty-balloons-film.md new file mode 100644 index 000000000..3f0c616e1 --- /dev/null +++ b/.changeset/chatty-balloons-film.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +fix(runtimes/external-store): invalidate cache when isRunning changes during autoStatus diff --git a/packages/react/src/runtimes/external-store/auto-status.tsx b/packages/react/src/runtimes/external-store/auto-status.tsx index 58023e498..cdc247d29 100644 --- a/packages/react/src/runtimes/external-store/auto-status.tsx +++ b/packages/react/src/runtimes/external-store/auto-status.tsx @@ -1,13 +1,8 @@ -import { MessageStatus } from "../../types"; - const AUTO_STATUS_RUNNING = Object.freeze({ type: "running" }); const AUTO_STATUS_COMPLETE = Object.freeze({ type: "complete", reason: "unknown", }); -export const isAutoStatus = (status: MessageStatus) => - status === AUTO_STATUS_RUNNING || status === AUTO_STATUS_COMPLETE; - export const getAutoStatus = (isLast: boolean, isRunning: boolean) => isLast && isRunning ? AUTO_STATUS_RUNNING : AUTO_STATUS_COMPLETE; diff --git a/packages/react/src/runtimes/external-store/useExternalStoreSync.tsx b/packages/react/src/runtimes/external-store/useExternalStoreSync.tsx index 945973684..a376424f3 100644 --- a/packages/react/src/runtimes/external-store/useExternalStoreSync.tsx +++ b/packages/react/src/runtimes/external-store/useExternalStoreSync.tsx @@ -6,7 +6,7 @@ import { } from "./ThreadMessageConverter"; import { ThreadMessage } from "../../types"; import { symbolInnerMessage } from "./getExternalStoreMessage"; -import { getAutoStatus, isAutoStatus } from "./auto-status"; +import { getAutoStatus } from "./auto-status"; import { fromThreadMessageLike } from "./ThreadMessageLike"; type UpdateDataCallback = ( @@ -32,16 +32,9 @@ export const useExternalStoreSync = ( adapterRef.current.isRunning ?? false, ); - if ( - cache && - (cache.role !== "assistant" || - !isAutoStatus(cache.status) || - cache.status.type === autoStatus.type) - ) + if (cache && (cache.role !== "assistant" || cache.status === autoStatus)) return cache; - if (cache) return cache; - const newMessage = fromThreadMessageLike( converter(m, idx), idx.toString(),