diff --git a/packages/react/src/experimental.ts b/packages/react/src/experimental.ts index 89dd95895..0e9dc53e9 100644 --- a/packages/react/src/experimental.ts +++ b/packages/react/src/experimental.ts @@ -8,3 +8,4 @@ export type { ModelConfigProvider } from "./utils/ModelConfigTypes"; export * from "./context"; export { useAssistantInstructions } from "./model-config/useAssistantInstructions"; +export { useAssistantTool } from "./model-config/useAssistantTool"; diff --git a/packages/react/src/model-config/useAssistantInstructions.tsx b/packages/react/src/model-config/useAssistantInstructions.tsx index fb91b4de6..fdc279b30 100644 --- a/packages/react/src/model-config/useAssistantInstructions.tsx +++ b/packages/react/src/model-config/useAssistantInstructions.tsx @@ -5,16 +5,16 @@ import { useAssistantContext } from "../context/AssistantContext"; export const useAssistantInstructions = (instruction: string) => { const { useModelConfig } = useAssistantContext(); - const addContextProvider = useModelConfig( + const registerModelConfigProvider = useModelConfig( (s) => s.registerModelConfigProvider, ); useEffect( () => - addContextProvider(() => { + registerModelConfigProvider(() => { return { system: instruction, }; }), - [addContextProvider, instruction], + [registerModelConfigProvider, instruction], ); }; diff --git a/packages/react/src/model-config/useAssistantTool.tsx b/packages/react/src/model-config/useAssistantTool.tsx new file mode 100644 index 000000000..f2f878c36 --- /dev/null +++ b/packages/react/src/model-config/useAssistantTool.tsx @@ -0,0 +1,23 @@ +"use client"; + +import { useEffect } from "react"; +import { useAssistantContext } from "../context/AssistantContext"; +import type { Tool } from "../utils/ModelConfigTypes"; + +export const useAssistantTool = (tool: Tool) => { + const { useModelConfig } = useAssistantContext(); + const registerModelConfigProvider = useModelConfig( + (s) => s.registerModelConfigProvider, + ); + useEffect( + () => + registerModelConfigProvider(() => { + return { + tools: { + [tool.name]: tool, + }, + }; + }), + [registerModelConfigProvider, tool], + ); +}; diff --git a/packages/react/src/utils/ModelConfigTypes.ts b/packages/react/src/utils/ModelConfigTypes.ts index 1cde2208f..e34e8c714 100644 --- a/packages/react/src/utils/ModelConfigTypes.ts +++ b/packages/react/src/utils/ModelConfigTypes.ts @@ -2,6 +2,7 @@ import type { z } from "zod"; export type Tool = { + name: string; description: string; parameters: z.ZodSchema; execute: (args: TArgs) => Promise; // TODO return type