Skip to content

Commit

Permalink
feat: useAssistantTool (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 17, 2024
1 parent 8e01f1c commit 3c3eaaf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/react/src/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
6 changes: 3 additions & 3 deletions packages/react/src/model-config/useAssistantInstructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
};
23 changes: 23 additions & 0 deletions packages/react/src/model-config/useAssistantTool.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";

import { useEffect } from "react";
import { useAssistantContext } from "../context/AssistantContext";
import type { Tool } from "../utils/ModelConfigTypes";

export const useAssistantTool = <T,>(tool: Tool<T>) => {
const { useModelConfig } = useAssistantContext();
const registerModelConfigProvider = useModelConfig(
(s) => s.registerModelConfigProvider,
);
useEffect(
() =>
registerModelConfigProvider(() => {
return {
tools: {
[tool.name]: tool,
},
};
}),
[registerModelConfigProvider, tool],
);
};
1 change: 1 addition & 0 deletions packages/react/src/utils/ModelConfigTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { z } from "zod";

export type Tool<TArgs> = {
name: string;
description: string;
parameters: z.ZodSchema<TArgs>;
execute: (args: TArgs) => Promise<unknown>; // TODO return type
Expand Down

0 comments on commit 3c3eaaf

Please sign in to comment.