Skip to content

Commit

Permalink
fix: external store thread list should not crash (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Dec 8, 2024
1 parent 8923944 commit 0f88efb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-clouds-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: external store thread list should not crash
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 0f88efb

Please sign in to comment.