Skip to content

Commit

Permalink
feat: useAssistantRuntime (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Aug 23, 2024
1 parent 033f6d2 commit 6985271
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/react/src/context/providers/AssistantProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { makeAssistantModelConfigStore } from "../stores/AssistantModelConfig";
import { makeAssistantToolUIsStore } from "../stores/AssistantToolUIs";
import { ThreadProvider } from "./ThreadProvider";
import { makeAssistantActionsStore } from "../stores/AssistantActions";
import {
AssistantRuntimeStore,
makeAssistantRuntimeStore,
} from "../stores/AssistantRuntime";
import { StoreApi } from "zustand";

type AssistantProviderProps = {
runtime: AssistantRuntime;
Expand All @@ -22,18 +27,32 @@ export const AssistantProvider: FC<
});

const [context] = useState(() => {
const useAssistantRuntime = makeAssistantRuntimeStore(runtime);
const useModelConfig = makeAssistantModelConfigStore();
const useToolUIs = makeAssistantToolUIsStore();
const useAssistantActions = makeAssistantActionsStore(runtimeRef);

return { useModelConfig, useToolUIs, useAssistantActions };
return {
useModelConfig,
useToolUIs,
useAssistantRuntime,
useAssistantActions,
};
});

const getModelConfig = context.useModelConfig();
useEffect(() => {
return runtime.registerModelConfigProvider(getModelConfig);
}, [runtime, getModelConfig]);

useEffect(
() =>
(
context.useAssistantRuntime as unknown as StoreApi<AssistantRuntimeStore>
).setState(runtime, true),
[runtime, context],
);

return (
<AssistantContext.Provider value={context}>
<ThreadProvider provider={runtime}>{children}</ThreadProvider>
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/react/AssistantContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import type { AssistantModelConfigState } from "../stores/AssistantModelConfig";
import type { AssistantToolUIsState } from "../stores/AssistantToolUIs";
import { ReadonlyStore } from "../ReadonlyStore";
import { AssistantActionsState } from "../stores/AssistantActions";
import { AssistantRuntime } from "../../runtimes";

export type AssistantContextValue = {
useModelConfig: ReadonlyStore<AssistantModelConfigState>;
useToolUIs: ReadonlyStore<AssistantToolUIsState>;
useAssistantRuntime: ReadonlyStore<AssistantRuntime>;
useAssistantActions: ReadonlyStore<AssistantActionsState>;
};

Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/context/stores/AssistantRuntime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { create } from "zustand";
import { AssistantRuntime } from "../../runtimes";

export type AssistantRuntimeStore = AssistantRuntime;

export const makeAssistantRuntimeStore = (runtime: AssistantRuntime) => {
return create<AssistantRuntimeStore>(() => runtime);
};

0 comments on commit 6985271

Please sign in to comment.