diff --git a/.changeset/good-impalas-applaud.md b/.changeset/good-impalas-applaud.md new file mode 100644 index 000000000..df0a6b077 --- /dev/null +++ b/.changeset/good-impalas-applaud.md @@ -0,0 +1,8 @@ +--- +"@assistant-ui/react-playground": patch +"@assistant-ui/react-langgraph": patch +"@assistant-ui/react-ai-sdk": patch +"@assistant-ui/react": patch +--- + +fix: do not export internal Runtime types diff --git a/packages/react-playground/src/lib/playground-runtime.ts b/packages/react-playground/src/lib/playground-runtime.ts index 28bfa5a9d..960ff0778 100644 --- a/packages/react-playground/src/lib/playground-runtime.ts +++ b/packages/react-playground/src/lib/playground-runtime.ts @@ -22,6 +22,8 @@ import { fromCoreMessage, INTERNAL, ThreadSuggestion, + ThreadRuntime, + AssistantRuntime, } from "@assistant-ui/react"; import { LanguageModelV1FunctionTool } from "@ai-sdk/provider"; import { useMemo, useState } from "react"; @@ -517,7 +519,14 @@ export class PlaygroundThreadRuntimeCore implements INTERNAL.ThreadRuntimeCore { } } -class PlaygroundThreadRuntime extends ThreadRuntimeImpl { +type PlaygroundThreadRuntime = ThreadRuntime & { + setRequestData: (options: EdgeRuntimeRequestOptions) => void; +}; + +class PlaygroundThreadRuntimeImpl + extends ThreadRuntimeImpl + implements PlaygroundThreadRuntime +{ constructor(private binding: INTERNAL.ThreadRuntimeCoreBinding) { super(binding); } @@ -529,6 +538,29 @@ class PlaygroundThreadRuntime extends ThreadRuntimeImpl { } } +export type PlaygroundRuntime = AssistantRuntime & { + thread: PlaygroundThreadRuntime; +}; + +class PlaygroundRuntimeImpl + extends AssistantRuntimeImpl + implements PlaygroundRuntime +{ + public override get thread() { + return super.thread as PlaygroundThreadRuntime; + } + + public static override create(_core: PlaygroundRuntimeCore) { + return new PlaygroundRuntimeImpl( + _core, + AssistantRuntimeImpl.createThreadRuntime( + _core, + PlaygroundThreadRuntimeImpl, + ), + ) as PlaygroundRuntime; + } +} + export const usePlaygroundRuntime = ({ initialMessages, maxToolRoundtrips, @@ -545,8 +577,5 @@ export const usePlaygroundRuntime = ({ ), ); - return useMemo( - () => new AssistantRuntimeImpl(runtime, PlaygroundThreadRuntime), - [runtime], - ); + return useMemo(() => PlaygroundRuntimeImpl.create(runtime), [runtime]); }; diff --git a/packages/react/src/api/AssistantRuntime.ts b/packages/react/src/api/AssistantRuntime.ts index ca3c9d07d..69d31eca7 100644 --- a/packages/react/src/api/AssistantRuntime.ts +++ b/packages/react/src/api/AssistantRuntime.ts @@ -1,7 +1,11 @@ import { AssistantRuntimeCore } from "../runtimes/core/AssistantRuntimeCore"; import { NestedSubscriptionSubject } from "./subscribable/NestedSubscriptionSubject"; import { ModelConfigProvider } from "../types/ModelConfigTypes"; -import { ThreadRuntime, ThreadRuntimeCoreBinding } from "./ThreadRuntime"; +import { + ThreadRuntime, + ThreadRuntimeCoreBinding, + ThreadRuntimeImpl, +} from "./ThreadRuntime"; import { Unsubscribe } from "../types"; export type AssistantRuntime = { @@ -23,26 +27,17 @@ export type AssistantRuntime = { subscribe(callback: () => void): Unsubscribe; }; -export class AssistantRuntimeImpl< - TThreadRuntime extends ThreadRuntime = ThreadRuntime, - > +export class AssistantRuntimeImpl implements AssistantRuntimeCore, AssistantRuntime { - constructor( - private _core: AssistantRuntimeCore, - CustomThreadRuntime: new ( - binding: ThreadRuntimeCoreBinding, - ) => TThreadRuntime, - ) { - this.thread = new CustomThreadRuntime( - new NestedSubscriptionSubject({ - getState: () => this._core.thread, - subscribe: (callback) => this._core.subscribe(callback), - }), - ); - } + protected constructor( + private readonly _core: AssistantRuntimeCore, + private readonly _thread: ThreadRuntime, + ) {} - public readonly thread; + public get thread() { + return this._thread; + } public switchToNewThread() { return this._core.switchToNewThread(); @@ -68,4 +63,30 @@ export class AssistantRuntimeImpl< public subscribe(callback: () => void) { return this._core.subscribe(callback); } + + protected static createThreadRuntime( + _core: AssistantRuntimeCore, + CustomThreadRuntime: new ( + binding: ThreadRuntimeCoreBinding, + ) => ThreadRuntime = ThreadRuntimeImpl, + ) { + return new CustomThreadRuntime( + new NestedSubscriptionSubject({ + getState: () => _core.thread, + subscribe: (callback) => _core.subscribe(callback), + }), + ); + } + + public static create( + _core: AssistantRuntimeCore, + CustomThreadRuntime: new ( + binding: ThreadRuntimeCoreBinding, + ) => ThreadRuntime = ThreadRuntimeImpl, + ) { + return new AssistantRuntimeImpl( + _core, + AssistantRuntimeImpl.createThreadRuntime(_core, CustomThreadRuntime), + ) as AssistantRuntime; + } } diff --git a/packages/react/src/runtimes/external-store/useExternalStoreRuntime.tsx b/packages/react/src/runtimes/external-store/useExternalStoreRuntime.tsx index 824fa87f6..a077603bf 100644 --- a/packages/react/src/runtimes/external-store/useExternalStoreRuntime.tsx +++ b/packages/react/src/runtimes/external-store/useExternalStoreRuntime.tsx @@ -12,7 +12,7 @@ export const useExternalStoreRuntime = (store: ExternalStoreAdapter) => { }); return useMemo( - () => new AssistantRuntimeImpl(runtime, ThreadRuntimeImpl), + () => AssistantRuntimeImpl.create(runtime, ThreadRuntimeImpl), [runtime], ); }; diff --git a/packages/react/src/runtimes/local/useLocalRuntime.tsx b/packages/react/src/runtimes/local/useLocalRuntime.tsx index 6d55e0a50..cf12ee4ce 100644 --- a/packages/react/src/runtimes/local/useLocalRuntime.tsx +++ b/packages/react/src/runtimes/local/useLocalRuntime.tsx @@ -4,17 +4,35 @@ import { useInsertionEffect, useMemo, useState } from "react"; import type { ChatModelAdapter } from "./ChatModelAdapter"; import { LocalRuntimeCore } from "./LocalRuntimeCore"; import { LocalRuntimeOptions } from "./LocalRuntimeOptions"; -import { AssistantRuntimeImpl } from "../../api/AssistantRuntime"; -import { ThreadRuntimeImpl } from "../../api/ThreadRuntime"; +import { + AssistantRuntime, + AssistantRuntimeImpl, +} from "../../api/AssistantRuntime"; +import { ThreadRuntimeImpl } from "../../internal"; +import { ThreadRuntime } from "../../api"; -export class LocalRuntime extends AssistantRuntimeImpl { - constructor(private core: LocalRuntimeCore) { - super(core, ThreadRuntimeImpl); +export type LocalRuntime = AssistantRuntime & { + reset: (options?: Parameters[0]) => void; +}; + +class LocalRuntimeImpl extends AssistantRuntimeImpl implements LocalRuntime { + private constructor( + private core: LocalRuntimeCore, + thread: ThreadRuntime, + ) { + super(core, thread); } public reset(options?: Parameters[0]) { this.core.reset(options); } + + public static override create(_core: LocalRuntimeCore) { + return new LocalRuntimeImpl( + _core, + AssistantRuntimeImpl.createThreadRuntime(_core, ThreadRuntimeImpl), + ) as LocalRuntime; + } } export const useLocalRuntime = ( @@ -28,5 +46,5 @@ export const useLocalRuntime = ( runtime.thread.options = options; }); - return useMemo(() => new LocalRuntime(runtime), [runtime]); + return useMemo(() => LocalRuntimeImpl.create(runtime), [runtime]); };