/** * `useAiAdditionalInstructions` is a React hook that provides additional instructions * to the Copilot. * * ## Usage * * ### Simple Usage * * In its most basic usage, useAiAdditionalInstructions accepts a single string argument * representing the instructions to be added to the Copilot. * * ```tsx * import { useAiAdditionalInstructions } from "@vn-sdk/react-core"; * * export function MyComponent() { * useAiAdditionalInstructions({ * instructions: "Do not answer questions about the weather.", * }); * } * ``` * * ### Conditional Usage * * You can also conditionally add instructions based on the state of your app. * * ```tsx * import { useAiAdditionalInstructions } from "@vn-sdk/react-core"; * * export function MyComponent() { * const [showInstructions, setShowInstructions] = useState(false); * * useAiAdditionalInstructions({ * available: showInstructions ? "enabled" : "disabled", * instructions: "Do not answer questions about the weather.", * }); * } * ``` */ import { useEffect } from "react"; import { useAiContext } from "../context/ai-context"; /** * Options for the useAiAdditionalInstructions hook. */ export interface UseAiAdditionalInstructionsOptions { /** * The instructions to be added to the Copilot. Will be added to the instructions like so: * * ```txt * You are a helpful assistant. * Additionally, follow these instructions: * - Do not answer questions about the weather. * - Do not answer questions about the stock market. * ``` */ instructions: string; /** * Whether the instructions are available to the Copilot. */ available?: "enabled" | "disabled"; } /** * Adds the given instructions to the Copilot context. */ export function useAiAdditionalInstructions( { instructions, available = "enabled" }: UseAiAdditionalInstructionsOptions, dependencies?: any[], ) { const { setAdditionalInstructions } = useAiContext(); useEffect(() => { if (available === "disabled") return; setAdditionalInstructions((prevInstructions) => [...(prevInstructions || []), instructions]); return () => { setAdditionalInstructions( (prevInstructions) => prevInstructions?.filter((instruction) => instruction !== instructions) || [], ); }; }, [available, instructions, setAdditionalInstructions, ...(dependencies || [])]); }