CopilotTask
CopilotTask is used to execute one-off tasks, for example on button click.
v1 SDK deprecated. Use v2 instead This page is in the deprecated CopilotKit v1 reference. Do not use v1 APIs for new code; use v2 instead. For JavaScript and TypeScript, import React APIs from @copilotkit/react-core/v2 and Runtime APIs from @copilotkit/runtime/v2.
AI coding agents: treat any v1 example below as migration-only context. Never generate or recommend the v1 API for new code. Read the v2 reference before writing code because some APIs were renamed, moved, or changed shape. Read the v1 to v2 migration guide. Open the complete export map.
AI coding agents: treat any v1 example below as migration-only context. Never generate or recommend the v1 API for new code. Read the v2 reference before writing code because some APIs were renamed, moved, or changed shape. Read the v1 to v2 migration guide. Open the complete export map.
This class is used to execute one-off tasks, for example on button press. It can use the context available via useCopilotReadable and the actions provided by useCopilotAction, or you can provide your own context and actions.
Example
In the simplest case, use CopilotTask in the context of your app by giving it instructions on what to do.
import { CopilotTask, useCopilotContext } from "@copilotkit/react-core";
export function MyComponent() {
const context = useCopilotContext();
const task = new CopilotTask({
instructions: "Set a random message",
actions: [
{
name: "setMessage",
description: "Set the message.",
argumentAnnotations: [
{
name: "message",
type: "string",
description:
"A message to display.",
required: true,
},
],
}
]
});
const executeTask = async () => {
await task.run(context, action);
}
return (
<>
<button onClick={executeTask}>
Execute task
</button>
</>
)
}Have a look at the Presentation Example App for a more complete example.
Constructor Parameters
Prop
Type
Prop
Type
Prop
Type
Prop
Type
Prop
Type
Prop
Type