Skip to content

Commit

Permalink
feat: add thread runtime threadId (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Sep 6, 2024
1 parent d2b11a5 commit fb8e58f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/mean-cameras-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@assistant-ui/react-playground": patch
"@assistant-ui/react": patch
---

feat: add thread runtime threadId
1 change: 1 addition & 0 deletions packages/react-playground/src/lib/playground-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class PlaygroundThreadRuntime implements ReactThreadRuntime {

public tools: Record<string, Tool<any, any>> = {};

public readonly threadId = generateId();
public readonly isDisabled = false;
public readonly capabilities = CAPABILITIES;

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/runtimes/core/ThreadRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ThreadRuntime = ThreadActionsState &
Readonly<{
composer: ThreadRuntime.Composer;
capabilities: Readonly<RuntimeCapabilities>;
threadId: string;
isDisabled: boolean;
messages: readonly ThreadMessage[];
subscribe: (callback: () => void) => Unsubscribe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class ExternalStoreRuntime extends BaseAssistantRuntime<ExternalStoreThre
super(new ExternalStoreThreadRuntime(store));
}

public get store() {
return this.thread.store;
}

public set store(store: ExternalStoreAdapter<any>) {
this.thread.store = store;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getAutoStatus, isAutoStatus } from "./auto-status";
import { fromThreadMessageLike } from "./ThreadMessageLike";
import { RuntimeCapabilities } from "../../context/stores/Thread";
import { getThreadMessageText } from "../../utils/getThreadMessageText";
import { generateId } from "../../internal";

export const hasUpcomingMessage = (
isRunning: boolean,
Expand All @@ -38,8 +39,9 @@ export class ExternalStoreThreadRuntime implements ReactThreadRuntime {
return this._capabilities;
}

public messages: ThreadMessage[] = [];
public isDisabled = false;
public threadId!: string;
public messages!: ThreadMessage[];
public isDisabled!: boolean;
public converter = new ThreadMessageConverter();

private _store!: ExternalStoreAdapter<any>;
Expand All @@ -56,9 +58,14 @@ export class ExternalStoreThreadRuntime implements ReactThreadRuntime {
this.store = store;
}

public get store() {
return this._store;
}

public set store(store: ExternalStoreAdapter<any>) {
if (this._store === store) return;

this.threadId = store.threadId ?? this.threadId ?? generateId();
const isRunning = store.isRunning ?? false;
this.isDisabled = store.isDisabled ?? false;

Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/runtimes/local/LocalThreadRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class LocalThreadRuntime implements ThreadRuntime {
speak: false,
};

public readonly threadId: string;
public readonly isDisabled = false;

public get messages() {
Expand All @@ -52,6 +53,7 @@ export class LocalThreadRuntime implements ThreadRuntime {
public adapter: ChatModelAdapter,
{ initialMessages, ...options }: LocalRuntimeOptions,
) {
this.threadId = generateId();
this.options = options;
if (initialMessages) {
let parentId: string | null = null;
Expand Down

0 comments on commit fb8e58f

Please sign in to comment.