diff --git a/.changeset/violet-clouds-smile.md b/.changeset/violet-clouds-smile.md new file mode 100644 index 0000000000..c56a3f711b --- /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 fc1a6c26b2..47c971653b 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; }