# 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](/migrate/v2) and [use the complete export map](/reference/v1/export-map).

{
 /*
  * ATTENTION! DO NOT MODIFY THIS FILE!
  * This page is auto-generated. If you want to make any changes to this page, changes must be made at:
  * packages/react-core/src/v1-deprecated/lib/copilot-task.ts
  */
}
<Callout type="info">
  `CopilotTask` has no direct v2 replacement. See the [v1 to v2 export map](/reference/v1/export-map).
</Callout>

This class is used to execute one-off tasks, for example on button press. It can use the context available via [useCopilotReadable](/reference/v1/hooks/useCopilotReadable) and the actions provided by [useCopilotAction](/reference/v1/hooks/useCopilotAction), or you can provide your own context and actions.

## Example [#example]
In the simplest case, use CopilotTask in the context of your app by giving it instructions on what to do.

```tsx
import { useState } from "react";
import { CopilotTask, useCopilotContext } from "@copilotkit/react-core";

export function MyComponent() {
  const context = useCopilotContext();
  const [message, setMessage] = useState("");

  const task = new CopilotTask({
    instructions: "Set a random message",
    actions: [
      {
        name: "setMessage",
        description: "Set the message.",
        parameters: [
          {
            name: "message",
            type: "string",
            description: "A message to display.",
            required: true,
          },
        ],
        handler: ({ message }) => setMessage(message),
      },
    ],
  });

  const executeTask = async () => {
    await task.run(context);
  };

  return (
    <>
      <button onClick={executeTask}>Execute task</button>
      <p>{message}</p>
    </>
  );
}
```

Have a look at the [Presentation Example App](https://github.com/CopilotKit/CopilotKit/blob/main/examples/v1/next-openai/src/app/presentation/page.tsx) for a more complete example.

## Constructor Parameters [#constructor-parameters]

<PropertyReference name="instructions" type="string" required > 
The instructions to be given to the assistant.
</PropertyReference>

<PropertyReference name="actions" type="FrontendAction<any>[]"  > 
An array of action definitions that can be called.
</PropertyReference>

<PropertyReference name="includeCopilotReadable" type="boolean"  > 
Whether to include the copilot readable context in the task.
</PropertyReference>

<PropertyReference name="includeCopilotActions" type="boolean"  > 
Whether to include actions defined via useCopilotAction in the task.
</PropertyReference>

<PropertyReference name="forwardedParameters" type="ForwardedParametersInput"  > 
The forwarded parameters to use for the task.
</PropertyReference>

<PropertyReference name="run" type="context: CopilotContextParams, data?: T">
Run the task.

  <PropertyReference name="context" type="CopilotContextParams" required>
  The CopilotContext to use for the task. Use `useCopilotContext` to obtain the current context.
  </PropertyReference>

  <PropertyReference name="data" type="T" >
  The data to use for the task.
  </PropertyReference>

</PropertyReference>
