UNPKG

@copilotkit/react-core

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

104 lines (100 loc) 3.13 kB
import { Parameter } from '@copilotkit/shared'; import { FrontendAction, CatchAllFrontendAction } from '../types/frontend-action.js'; import '@copilotkit/runtime-client-gql'; import 'react'; /** * Example usage of useCopilotAction with complex parameters: * * @example * useCopilotAction({ * name: "myAction", * parameters: [ * { name: "arg1", type: "string", enum: ["option1", "option2", "option3"], required: false }, * { name: "arg2", type: "number" }, * { * name: "arg3", * type: "object", * attributes: [ * { name: "nestedArg1", type: "boolean" }, * { name: "xyz", required: false }, * ], * }, * { name: "arg4", type: "number[]" }, * ], * handler: ({ arg1, arg2, arg3, arg4 }) => { * const x = arg3.nestedArg1; * const z = arg3.xyz; * console.log(arg1, arg2, arg3); * }, * }); * * @example * // Simple action without parameters * useCopilotAction({ * name: "myAction", * handler: () => { * console.log("No parameters provided."); * }, * }); * * @example * // Interactive action with UI rendering and response handling * useCopilotAction({ * name: "handleMeeting", * description: "Handle a meeting by booking or canceling", * parameters: [ * { * name: "meeting", * type: "string", * description: "The meeting to handle", * required: true, * }, * { * name: "date", * type: "string", * description: "The date of the meeting", * required: true, * }, * { * name: "title", * type: "string", * description: "The title of the meeting", * required: true, * }, * ], * renderAndWaitForResponse: ({ args, respond, status }) => { * const { meeting, date, title } = args; * return ( * <MeetingConfirmationDialog * meeting={meeting} * date={date} * title={title} * onConfirm={() => respond('meeting confirmed')} * onCancel={() => respond('meeting canceled')} * /> * ); * }, * }); * * @example * // Catch all action allows you to render actions that are not defined in the frontend * useCopilotAction({ * name: "*", * render: ({ name, args, status, result, handler, respond }) => { * return <div>Rendering action: {name}</div>; * }, * }); */ /** * useCopilotAction is a legacy hook maintained for backwards compatibility. * * To avoid violating React's Rules of Hooks (which prohibit conditional hook calls), * we use a registration pattern: * 1. This hook registers the action configuration with the CopilotContext * 2. A renderer component in CopilotKit actually renders the appropriate hook wrapper * 3. React properly manages hook state since components are rendered, not conditionally called * * This allows action types to change between renders without corrupting React's hook state. */ declare function useCopilotAction<const T extends Parameter[] | [] = []>(action: FrontendAction<T> | CatchAllFrontendAction, dependencies?: any[]): void; export { useCopilotAction };