# Agent plan

An agent's plan: a card to review before it runs, then watch run with live timing and a retry, or a turn's to-dos folded behind their count.

Live variants: https://design.arcadia.is/components/agent-plan

```tsx
import {
  AgentPlan, planProgress, workStateDot, workProgress,
} from "@arcadia/ui";
```

```tsx
import { useState, useEffect } from "react";
import { AgentPlan } from "@arcadia/ui";

function Review() {
  const review: PlanStep[] = [
    { id: "read", title: "Read the Q2 numbers", detail: "finance/q2.csv", tool: "read_file" },
    { id: "draft", title: "Draft the board memo", tool: "write_file" },
    { id: "send", title: "Send it for review", tool: "send_email" },
  ];
  const [steps, setSteps] = useState<PlanStep[]>(review);
  const [status, setStatus] = useState<AgentPlanStatus>("draft");

  useEffect(() => {
    if (status !== "running") return;
    const at = steps.findIndex((step) => step.status === "running");
    if (at < 0) return;
    const timer = setTimeout(() => {
      setSteps((current) => current.map((step, index): PlanStep =>
        index === at ? { ...step, status: "done", duration: 1_800 }
          : index === at + 1 ? { ...step, status: "running", duration: undefined }
            : step));
      if (at + 1 >= steps.length) setStatus("done");
    }, 1_800);
    return () => clearTimeout(timer);
  }, [status, steps]);

  return (
    <AgentPlan
      title="Prepare the Q3 board update"
      estimate="~2 min"
      steps={steps}
      status={status}
      onChange={setSteps}
      onRun={() => {
        setSteps((current) => current.map((step, index): PlanStep => ({
          ...step,
          status: index === 0 ? "running" : "queued",
          duration: undefined,
          error: undefined,
        })));
        setStatus("running");
      }}
      onCancel={() => {
        setSteps(review);
        setStatus("draft");
      }}
    />
  );
}
```

## Accessibility

The steps are an ordered list labelled by the plan's title, or Plan when it has none. In a draft each step is a stop: Tab moves between the steps, the remove buttons, Add step and the tray, and the arrow keys move between steps. Alt with an arrow moves the focused step and keeps focus on it. Enter renames and Escape leaves the name as it was; both put focus back on the step. Delete removes the step and puts focus on the next one. The numbers, the marks and the rail are for the eye alone: the list numbers every step and the row says its state in words.

Each change is announced in a polite live region: a move with its new position, a rename, a removal, an addition, and a step that starts, finishes, fails or is skipped. In a run the live step carries `aria-current="step"`; the grip is for the pointer, and the keyboard move is its equal.

In the fold the head is the button: it reads the plan, the count and the step in progress, and `aria-expanded` says whether the steps show. Each step's mark is named in words before it, and a done step is struck through as well as checked, so a state never rests on colour alone.

## API reference

### AgentPlan

- `frame` (`"card"`): The card.
- `status` (`"draft" | "running" | "done"`): Draft is editable, running is locked and live, done shows what each step took.
- `onChange` (`(steps: PlanStep[]) => void`): Every edit in a draft: rename, remove, reorder, add.
- `onRun` (`(steps: PlanStep[]) => void`): Run plan or Run again pressed, with the steps in their final order. The plan starts only here.
- `onCancel` (`() => void`): Cancel beside the run action, in a draft and while it runs.
- `onRetry` (`(stepId: string) => void`): Retry from here under a failed step, with that step id.
- `title` (`string`): The plan goal: its head, and the list label.
- `estimate` (`string`): The run estimated time, beside the head while the plan is a draft.
- `rootStyle` (`AgentPlanStyle`): Typed StyleX override: where the plan sits and its knobs.
- `steps` (`readonly PlanStep[]`): The steps, controlled: every edit leaves through onChange.
- `defaultSteps` (`readonly PlanStep[]`): The steps a draft starts with when uncontrolled: edits stay inside the component, and onChange still hears them.

Rest props go to `HTMLAttributes<HTMLDivElement>`.

### AgentPlan fold

- `frame` (`"fold"`, required)
- `title` (`string`, default `To-dos`): The head's name: the plan, or what it is.
- `open` (`boolean`): Open state, controlled.
- `defaultOpen` (`boolean`, default `false`): Whether it starts open.
- `onOpenChange` (`(open: boolean, details: OpenChangeDetails) => void`): Called when the head folds or unfolds the steps.
- `size` (`"xs" | "sm" | "md"`): The head's text style: sm (Body, a card) by default; md (Headline) in a conversation.
- `rootStyle` (`RootStyle<CollapsibleKnobs>`): Typed StyleX override: where the plan sits and the fold's knobs, which the head and the rows inherit.
- `steps` (`readonly PlanStep[]`): The steps, controlled: every edit leaves through onChange.
- `defaultSteps` (`readonly PlanStep[]`): The steps a draft starts with when uncontrolled: edits stay inside the component, and onChange still hears them.

Rest props go to `HTMLAttributes<HTMLDivElement>`.
