From 0f88efbf20fd5f2be739e512cc92411864d60f85 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Sun, 8 Dec 2024 12:31:08 -0800 Subject: [PATCH] fix: external store thread list should not crash (#1239) --- .changeset/violet-clouds-smile.md | 5 +++++ .../ExternalStoreThreadListRuntimeCore.tsx | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .changeset/violet-clouds-smile.md diff --git a/.changeset/violet-clouds-smile.md b/.changeset/violet-clouds-smile.md new file mode 100644 index 000000000..c56a3f711 --- /dev/null +++ b/.changeset/violet-clouds-smile.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +fix: external store thread list should not crash diff --git a/packages/react/src/runtimes/external-store/ExternalStoreThreadListRuntimeCore.tsx b/packages/react/src/runtimes/external-store/ExternalStoreThreadListRuntimeCore.tsx index fc1a6c26b..47c971653 100644 --- a/packages/react/src/runtimes/external-store/ExternalStoreThreadListRuntimeCore.tsx +++ b/packages/react/src/runtimes/external-store/ExternalStoreThreadListRuntimeCore.tsx @@ -1,18 +1,26 @@ import type { Unsubscribe } from "../../types"; import { ExternalStoreThreadRuntimeCore } from "./ExternalStoreThreadRuntimeCore"; import { ThreadListRuntimeCore } from "../core/ThreadListRuntimeCore"; -import { ExternalStoreThreadListAdapter } from "./ExternalStoreAdapter"; +import { + ExternalStoreThreadData, + ExternalStoreThreadListAdapter, +} from "./ExternalStoreAdapter"; export type ExternalStoreThreadFactory = () => ExternalStoreThreadRuntimeCore; const EMPTY_ARRAY = Object.freeze([]); const DEFAULT_THREAD_ID = "DEFAULT_THREAD_ID"; +const DEFAULT_THREADS = Object.freeze([DEFAULT_THREAD_ID]); +const DEFAULT_THREAD: ExternalStoreThreadData<"regular"> = Object.freeze({ + threadId: DEFAULT_THREAD_ID, + state: "regular", +}); export class ExternalStoreThreadListRuntimeCore implements ThreadListRuntimeCore { private _mainThreadId: string = DEFAULT_THREAD_ID; - private _threads: readonly string[] = EMPTY_ARRAY; + private _threads: readonly string[] = DEFAULT_THREADS; private _archivedThreads: readonly string[] = EMPTY_ARRAY; public get newThreadId() { @@ -51,6 +59,7 @@ export class ExternalStoreThreadListRuntimeCore for (const thread of this.adapter.archivedThreads ?? []) { if (thread.threadId === threadId) return thread; } + if (threadId === DEFAULT_THREAD_ID) return DEFAULT_THREAD; return undefined; }