Skip to content

Commit

Permalink
fix: do not export internal Runtime types (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 12, 2024
1 parent 47a0829 commit c3806f8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 30 deletions.
8 changes: 8 additions & 0 deletions .changeset/good-impalas-applaud.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 34 additions & 5 deletions packages/react-playground/src/lib/playground-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -545,8 +577,5 @@ export const usePlaygroundRuntime = ({
),
);

return useMemo(
() => new AssistantRuntimeImpl(runtime, PlaygroundThreadRuntime),
[runtime],
);
return useMemo(() => PlaygroundRuntimeImpl.create(runtime), [runtime]);
};
57 changes: 39 additions & 18 deletions packages/react/src/api/AssistantRuntime.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useExternalStoreRuntime = <T,>(store: ExternalStoreAdapter<T>) => {
});

return useMemo(
() => new AssistantRuntimeImpl(runtime, ThreadRuntimeImpl),
() => AssistantRuntimeImpl.create(runtime, ThreadRuntimeImpl),
[runtime],
);
};
30 changes: 24 additions & 6 deletions packages/react/src/runtimes/local/useLocalRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalRuntimeCore["reset"]>[0]) => void;
};

class LocalRuntimeImpl extends AssistantRuntimeImpl implements LocalRuntime {
private constructor(
private core: LocalRuntimeCore,
thread: ThreadRuntime,
) {
super(core, thread);
}

public reset(options?: Parameters<LocalRuntimeCore["reset"]>[0]) {
this.core.reset(options);
}

public static override create(_core: LocalRuntimeCore) {
return new LocalRuntimeImpl(
_core,
AssistantRuntimeImpl.createThreadRuntime(_core, ThreadRuntimeImpl),
) as LocalRuntime;
}
}

export const useLocalRuntime = (
Expand All @@ -28,5 +46,5 @@ export const useLocalRuntime = (
runtime.thread.options = options;
});

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

0 comments on commit c3806f8

Please sign in to comment.