From d0db6022cc163c8c02e1ba0ad04d1c8026a0010e Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Fri, 4 Oct 2024 13:17:44 -0700 Subject: [PATCH] fix: useDangerousInBrowserRuntime correct options forwarding (#950) --- .changeset/late-timers-yell.md | 5 +++++ .../useDangerousInBrowserRuntime.ts | 14 ++++++++------ .../react/src/runtimes/edge/useEdgeRuntime.ts | 18 ++++++------------ .../src/runtimes/local/LocalRuntimeOptions.tsx | 10 ++++++++++ 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 .changeset/late-timers-yell.md diff --git a/.changeset/late-timers-yell.md b/.changeset/late-timers-yell.md new file mode 100644 index 0000000000..4ddb59a9e8 --- /dev/null +++ b/.changeset/late-timers-yell.md @@ -0,0 +1,5 @@ +--- +"@assistant-ui/react": patch +--- + +fix: useDangerousInBrowserRuntime correct options forwarding diff --git a/packages/react/src/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.ts b/packages/react/src/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.ts index e06949b206..c18f78a351 100644 --- a/packages/react/src/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.ts +++ b/packages/react/src/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.ts @@ -4,14 +4,16 @@ import { DangerousInBrowserAdapter, DangerousInBrowserAdapterOptions, } from "./DangerousInBrowserAdapter"; +import { splitLocalRuntimeOptions } from "../local/LocalRuntimeOptions"; export type DangerousInBrowserRuntimeOptions = DangerousInBrowserAdapterOptions & LocalRuntimeOptions; -export const useDangerousInBrowserRuntime = ({ - initialMessages, - ...options -}: DangerousInBrowserRuntimeOptions) => { - const [adapter] = useState(() => new DangerousInBrowserAdapter(options)); - return useLocalRuntime(adapter, { initialMessages }); +export const useDangerousInBrowserRuntime = ( + options: DangerousInBrowserRuntimeOptions, +) => { + const { localRuntimeOptions, otherOptions } = + splitLocalRuntimeOptions(options); + const [adapter] = useState(() => new DangerousInBrowserAdapter(otherOptions)); + return useLocalRuntime(adapter, localRuntimeOptions); }; diff --git a/packages/react/src/runtimes/edge/useEdgeRuntime.ts b/packages/react/src/runtimes/edge/useEdgeRuntime.ts index 78f3741ec0..7e717d1ce7 100644 --- a/packages/react/src/runtimes/edge/useEdgeRuntime.ts +++ b/packages/react/src/runtimes/edge/useEdgeRuntime.ts @@ -1,19 +1,13 @@ import { LocalRuntimeOptions, useLocalRuntime } from ".."; import { useState } from "react"; import { EdgeChatAdapterOptions, EdgeChatAdapter } from "./EdgeChatAdapter"; +import { splitLocalRuntimeOptions } from "../local/LocalRuntimeOptions"; export type EdgeRuntimeOptions = EdgeChatAdapterOptions & LocalRuntimeOptions; -export const useEdgeRuntime = ({ - initialMessages, - maxToolRoundtrips, - adapters, - ...options -}: EdgeRuntimeOptions) => { - const [adapter] = useState(() => new EdgeChatAdapter(options)); - return useLocalRuntime(adapter, { - initialMessages, - maxToolRoundtrips, - adapters, - }); +export const useEdgeRuntime = (options: EdgeRuntimeOptions) => { + const { localRuntimeOptions, otherOptions } = + splitLocalRuntimeOptions(options); + const [adapter] = useState(() => new EdgeChatAdapter(otherOptions)); + return useLocalRuntime(adapter, localRuntimeOptions); }; diff --git a/packages/react/src/runtimes/local/LocalRuntimeOptions.tsx b/packages/react/src/runtimes/local/LocalRuntimeOptions.tsx index 90ca9aae33..a695418f9e 100644 --- a/packages/react/src/runtimes/local/LocalRuntimeOptions.tsx +++ b/packages/react/src/runtimes/local/LocalRuntimeOptions.tsx @@ -14,3 +14,13 @@ export type LocalRuntimeOptions = { } | undefined; }; + +export const splitLocalRuntimeOptions = ( + options: T, +) => { + const { initialMessages, maxToolRoundtrips, adapters, ...rest } = options; + return { + localRuntimeOptions: { initialMessages, maxToolRoundtrips, adapters }, + otherOptions: rest, + }; +};