Skip to content

Commit

Permalink
fix: thread initialization (#1269)
Browse files Browse the repository at this point in the history
fixes #1263
  • Loading branch information
Yonom authored Dec 23, 2024
1 parent ee77267 commit 1b16dce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-cows-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: thread initialization
6 changes: 2 additions & 4 deletions packages/react/src/runtimes/local/LocalRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ export class LocalRuntimeCore extends BaseAssistantRuntimeCore {

this._options = options;

this.threadList = new LocalThreadListRuntimeCore((data) => {
const thread = new LocalThreadRuntimeCore(
this.threadList = new LocalThreadListRuntimeCore(() => {
return new LocalThreadRuntimeCore(
this._proxyConfigProvider,
this._options,
);
thread.import(data);
return thread;
});

if (initialMessages) {
Expand Down
12 changes: 3 additions & 9 deletions packages/react/src/runtimes/local/LocalThreadListRuntimeCore.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Unsubscribe } from "../../types";
import { ThreadListRuntimeCore } from "../core/ThreadListRuntimeCore";
import { ExportedMessageRepository } from "../utils/MessageRepository";
import { generateId } from "../../utils/idUtils";
import { LocalThreadRuntimeCore } from "./LocalThreadRuntimeCore";

Expand All @@ -13,13 +12,9 @@ export type LocalThreadData = {
readonly state: "new" | "regular" | "archived";
readonly threadId: string;
readonly title?: string | undefined;

dispose(): void;
};

export type LocalThreadFactory = (
data: ExportedMessageRepository,
) => LocalThreadRuntimeCore;
export type LocalThreadFactory = () => LocalThreadRuntimeCore;

export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
private _threadData = new Map<string, LocalThreadData>();
Expand Down Expand Up @@ -79,8 +74,9 @@ export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
threadId = generateId();
} while (this._threadData.has(threadId));

const runtime = this._threadFactory({ messages: [] });
const runtime = this._threadFactory();
const dispose = runtime.unstable_on("initialize", () => {
dispose();
const data = this._threadData.get(threadId);
if (!data) throw new Error("Thread not found");

Expand All @@ -90,7 +86,6 @@ export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
runtime,
state: "new",
threadId,
dispose,
});
this._newThreadId = threadId;
}
Expand Down Expand Up @@ -141,7 +136,6 @@ export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {

case "deleted":
this._threadData.delete(threadId);
data.dispose();
break;

default: {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/runtimes/local/useLocalRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const useLocalRuntime = (

useEffect(() => {
runtime.threadList.getMainThreadRuntimeCore().__internal_setOptions(opt);
});
}, [runtime, opt]);

return useMemo(() => LocalRuntimeImpl.create(runtime), [runtime]);
};

0 comments on commit 1b16dce

Please sign in to comment.