| title | useCopilotAction |
|---|---|
| description | A hook for providing actions the Copilot can call. |
useCopilotAction is a React hook that lets you integrate
actionable functions in the Copilot chat. The Copilot can then call these
actions to trigger actions in your application, allowing for interactive
and dynamic behavior controlled by the Copilot.
You can define actions with parameters of any complexity. CopilotKit will relay these definitions to the LLM and subsequently invoke your action handler with the parameters you've defined.
In addition, the types of all parameters in your action handler are automatically inferred from the action definition. This enables the use of TypeScript for ensuring type safety and enabling autocompletion in your action handlers.
useCopilotAction takes an optional render() function that lets you render a
custom component or string instead of the default message component.
In the simplest case, you can provide a string to render a text message.
{
// ...
render: "Hello, world!",
}You can also provide a function to dynamically set the content of the message.
{
// ...
render: ({status, args, result}) => status === "complete" ? "Done!" : "Processing...",
}In this case, status is the execution status of the action, args are the
possibly incomplete, streaming arguments, and result is the result of the action.
Finally, you can return a React component to render a custom component.
{
// ...
render: ({status, args, result}) => (
<div>
{status === "complete" ? "Done!" : "Processing..."}
</div>
),
}<ResponseField name="type" type="'string' | 'number' | 'boolean' | 'object' | 'string[]' | 'number[]' | 'boolean[]' | 'object[]'" required
The type of the argument.
A description of the argument. This is used to instruct the Copilot on what this argument is used for. Wether or not the argument is required. Defaults to true. If the argument is of a complex type, i.e. `object` or `object[]`, this field lets you define the attributes of the object. For example: ```js { name: "addresses", description: "The addresses extracted from the text.", type: "object[]", attributes: [ { name: "street", type: "string", description: "The street of the address.", }, { name: "city", type: "string", description: "The city of the address.", }, // ... ], } ````