Skip to content

Commit

Permalink
fix: correctly handle new thread creation (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 19, 2024
1 parent 42d1715 commit 3a602b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-ghosts-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: correctly handle new thread creation
30 changes: 20 additions & 10 deletions packages/react/src/runtimes/local/LocalThreadManagerRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 3a602b9

Please sign in to comment.