From 3a602b95143ca665eed3974d38d5b27d8bd3b951 Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Fri, 18 Oct 2024 18:53:43 -0700 Subject: [PATCH] fix: correctly handle new thread creation (#1027) --- .changeset/olive-ghosts-visit.md | 5 ++++ .../local/LocalThreadManagerRuntimeCore.tsx | 30 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 .changeset/olive-ghosts-visit.md diff --git a/.changeset/olive-ghosts-visit.md b/.changeset/olive-ghosts-visit.md new file mode 100644 index 000000000..0a297b024 --- /dev/null +++ b/.changeset/olive-ghosts-visit.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +fix: correctly handle new thread creation diff --git a/packages/react/src/runtimes/local/LocalThreadManagerRuntimeCore.tsx b/packages/react/src/runtimes/local/LocalThreadManagerRuntimeCore.tsx index 22eea492d..a51dc1e95 100644 --- a/packages/react/src/runtimes/local/LocalThreadManagerRuntimeCore.tsx +++ b/packages/react/src/runtimes/local/LocalThreadManagerRuntimeCore.tsx @@ -39,7 +39,14 @@ export class LocalThreadManagerRuntimeCore implements ThreadManagerRuntimeCore { } constructor(private _threadFactory: LocalThreadFactory) { - this._mainThread = this._threadFactory(generateId(), { messages: [] }); + const threadId = generateId(); + this._threadData.set(threadId, { + data: { messages: [] }, + metadata: { threadId }, + isArchived: false, + }); + this._threads = [{ threadId }]; + this._mainThread = this._threadFactory(threadId, { messages: [] }); } public switchToThread(threadId: string): void { @@ -53,20 +60,23 @@ export class LocalThreadManagerRuntimeCore implements ThreadManagerRuntimeCore { } public switchToNewThread(): void { - if (!this._mainThread) return; - - const thread = this._threadFactory(generateId(), { messages: [] }); + const threadId = generateId(); + this._threadData.set(threadId, { + data: { messages: [] }, + metadata: { threadId }, + isArchived: false, + }); + this._threads = [{ threadId }]; + const thread = this._threadFactory(threadId, { messages: [] }); this._performThreadSwitch(thread); } private _performThreadSwitch(newThreadCore: LocalThreadRuntimeCore) { - if (this._mainThread) { - const data = this._threadData.get(this._mainThread.threadId); - if (!data) throw new Error("Thread not found"); + const data = this._threadData.get(this._mainThread.threadId); + if (!data) throw new Error("Thread not found"); - const exprt = this._mainThread.export(); - data.data = exprt; - } + const exprt = this._mainThread.export(); + data.data = exprt; this._mainThread._notifyEventSubscribers("switched-away"); this._mainThread = newThreadCore;