Skip to content

Commit

Permalink
feat: DangerousInBrowserRuntime (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Aug 22, 2024
1 parent 02150b8 commit edb5a16
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-bears-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: DangerousInBrowserRuntime
5 changes: 4 additions & 1 deletion packages/react/src/edge.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { createEdgeRuntimeAPI, getEdgeRuntimeResponse } from "./runtimes/edge/createEdgeRuntimeAPI";
export {
createEdgeRuntimeAPI,
getEdgeRuntimeResponse,
} from "./runtimes/edge/createEdgeRuntimeAPI";
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ChatModelAdapter, ChatModelRunOptions } from "../local";
import { toCoreMessages } from "../edge/converters/toCoreMessages";
import { toLanguageModelTools } from "../edge/converters/toLanguageModelTools";
import { EdgeRuntimeRequestOptions } from "../edge/EdgeRuntimeRequestOptions";
import { runResultStream } from "../edge/streams/runResultStream";
import { toolResultStream } from "../edge/streams/toolResultStream";
import { asAsyncIterable } from "../edge/EdgeChatAdapter";
import {
CreateEdgeRuntimeAPIOptions,
getEdgeRuntimeStream,
} from "../edge/createEdgeRuntimeAPI";

export type DangerousInBrowserAdapterOptions = CreateEdgeRuntimeAPIOptions;

export class DangerousInBrowserAdapter implements ChatModelAdapter {
constructor(private options: DangerousInBrowserAdapterOptions) {}

async *run({ messages, abortSignal, config }: ChatModelRunOptions) {
const res = await getEdgeRuntimeStream({
options: this.options,
abortSignal,
requestData: {
system: config.system,
messages: toCoreMessages(messages),
tools: config.tools ? toLanguageModelTools(config.tools) : [],
...config.callSettings,
...config.config,
} satisfies EdgeRuntimeRequestOptions,
});

const stream = res
.pipeThrough(toolResultStream(config.tools, abortSignal))
.pipeThrough(runResultStream());

for await (const update of asAsyncIterable(stream)) {
yield update;
}
}
}
4 changes: 4 additions & 0 deletions packages/react/src/runtimes/dangerous-in-browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
useDangerousInBrowserRuntime,
type DangerousInBrowserRuntimeOptions,
} from "./useDangerousInBrowserRuntime";
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LocalRuntimeOptions, useLocalRuntime } from "..";
import { useState } from "react";
import {
DangerousInBrowserAdapter,
DangerousInBrowserAdapterOptions,
} from "./DangerousInBrowserAdapter";

export type DangerousInBrowserRuntimeOptions =
DangerousInBrowserAdapterOptions & LocalRuntimeOptions;

export const useDangerousInBrowserRuntime = ({
initialMessages,
...options
}: DangerousInBrowserRuntimeOptions) => {
const [adapter] = useState(() => new DangerousInBrowserAdapter(options));
return useLocalRuntime(adapter, { initialMessages });
};
4 changes: 2 additions & 2 deletions packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type LanguageModelCreator = (
config: LanguageModelConfig,
) => Promise<LanguageModelV1> | LanguageModelV1;

type CreateEdgeRuntimeAPIOptions = LanguageModelV1CallSettings & {
export type CreateEdgeRuntimeAPIOptions = LanguageModelV1CallSettings & {
model: LanguageModelV1 | LanguageModelCreator;
system?: string;
tools?: Record<string, Tool<any, any>>;
Expand All @@ -63,7 +63,7 @@ type GetEdgeRuntimeStreamOptions = {
options: CreateEdgeRuntimeAPIOptions;
};

const getEdgeRuntimeStream = async ({
export const getEdgeRuntimeStream = async ({
abortSignal,
requestData: unsafeRequest,
options: {
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/runtimes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./core";
export * from "./local";
export * from "./edge";
export * from "./external-store";
export * from "./dangerous-in-browser";

0 comments on commit edb5a16

Please sign in to comment.