# Pilot

How Firm Pilot shows its work in chat: each tool call as a trace line, its reasoning collapsed to one line, a turn's steps on a timing waterfall, and the cards it asks the lawyer to act on.

- Beta: ready to use; its API can still change in a minor release.
- Category: Patterns · Firm Pilot

```tsx
import { PilotToolCall, PilotReasoning, PilotTrace, PilotApprovalCard, PilotPermissionRequest } from "@leap-crm/design-system";
```

## Best practices

- Do: Write a tool call's `title` in its tense: “Searching contacts” while it runs, “Searched contacts” once it's done.
- Do: Use `PilotPermissionRequest` for a write Firm Pilot holds for approval, so the held tool row sits directly above its card. Use `PilotApprovalCard` alone for a choice that holds no write.
- Do: Mark one option `primary`, the action the card exists for, and a delete or discard `destructive`. The rest render as outline buttons.
- Do: Return a Promise from `onSelect`, so the chosen button spins until it settles and then shows a tick or a cross.
- Do: Set `readOnly` without `selectedValue` when a card has gone stale. It swaps its buttons for a line saying so, since faded buttons invite a tap that does nothing.
- Don't: Open `PilotReasoning` by default once Firm Pilot has finished. It collapses to one line, like “Reasoned for 4s”, so the answer leads and the lawyer opens the reasoning when they want it.

## Keyboard

| Keys | What they do |
| --- | --- |
| Enter, Space | Toggle the focused section and keep focus on its trigger. |
| Tab | Moves from an open trigger into its panel, and skips a closed one. |

## Related

- [Firm Pilot Mark](./firm-pilot-mark.md)
- [Conflict Status](./conflict-status.md)
- [Collapsible](./collapsible.md)

## Examples

What the examples import:

```tsx
import { FileText, Pencil, Search } from "lucide-react";
import { useState } from "react";
import { Button, PilotApprovalCard, PilotPermissionRequest, PilotReasoning, PilotToolCall, PilotTrace } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const options = [
  { value: "approve", label: "Approve", variant: "primary" as const },
  { value: "edit", label: "Edit first" },
  { value: "discard", label: "Discard", variant: "destructive" as const },
];

const details = [
  { label: "Email", value: "jane@citizenlegal.com.au" },
  { label: "Phone", value: "0400 000 000" },
  { label: "Source", value: "Signature in her 14 Nov email" },
];

const wait = (ms: number) =>
  new Promise<void>(resolve => {
    setTimeout(resolve, ms);
  });

function ApprovalDemo() {
  const [picked, setPicked] = useState<string>();
  // Firm Pilot takes a moment to apply the pick; the button spins, then ticks.
  const select = async (value: string) => {
    await wait(900);
    setPicked(value);
  };
  return (
    <div className="flex flex-wrap items-start gap-4">
      <PilotApprovalCard
        title="Update Jane Citizen"
        details={details}
        options={options}
        onSelect={select}
        readOnly={picked !== undefined}
        selectedValue={picked}
      />
      <PilotApprovalCard
        title="Update Jane Citizen"
        details={details}
        options={options}
        onSelect={() => undefined}
        readOnly
      />
    </div>
  );
}

type Step = "running" | "done" | "failed";

function SettlingDemo() {
  const [search, setSearch] = useState<Step>("done");
  const [write, setWrite] = useState<Step>("failed");
  const run = () => {
    setSearch("running");
    setWrite("running");
    setTimeout(() => setSearch("done"), 1400);
    setTimeout(() => setWrite("failed"), 2000);
  };
  return (
    <div className="flex flex-col items-start gap-3">
      <div className="flex flex-col">
        <PilotToolCall
          title={
            search === "running" ? "Searching contacts" : "Searched contacts"
          }
          icon={<Search />}
          state={search}
          duration={search === "running" ? undefined : 1.4}
        />
        <PilotToolCall
          title={
            write === "running"
              ? "Reading the costs agreement"
              : "Couldn't read the costs agreement"
          }
          icon={<FileText />}
          state={write}
          duration={write === "running" ? undefined : 2}
        />
      </div>
      <Button intent="outline" size="sm" onClick={run}>
        Run again
      </Button>
    </div>
  );
}
```

### Tool call

Running shimmers the title and breathes the icon. Done shows the duration. Failed turns the line red and wraps. Held waits for the lawyer's OK. A retried first try reads Retried. A row with detail opens to it, on a line down its left.

```tsx
<div className="flex max-w-120 flex-col">
  <PilotToolCall
    title="Searching contacts"
    icon={<Search />}
    state="running"
  />
  <PilotToolCall
    title="Searched contacts"
    icon={<Search />}
    state="done"
    duration={1.2}
  >
    <span>Query: “Citizen”</span>
    <span>
      Found 3 contacts — Jane Citizen, Citizen Holdings Pty Ltd, Tom
      Citizen
    </span>
  </PilotToolCall>
  <PilotToolCall
    title="Couldn’t update Agency Purchase — LEAP refused the matter type"
    icon={<Pencil />}
    state="failed"
    duration={0.8}
  />
  <PilotToolCall
    title="Update Jane Citizen’s email"
    icon={<Pencil />}
    state="held"
  />
  <PilotToolCall
    title="Reading the matter"
    icon={<FileText />}
    state="recovered"
  />
</div>
```

### Reasoning

Reasoning... while Firm Pilot thinks, with the brain breathing; Reasoned for 4s once done, collapsed by default.

```tsx
<div className="flex max-w-90 flex-col">
  <PilotReasoning streaming />
  <PilotReasoning duration={4}>
    Jane is the client on two open opportunities. The newer one has no
    costs agreement, so the conflict check comes first, then the
    agreement.
  </PilotReasoning>
</div>
```

### Trace waterfall

A turn's steps with their timing on a shared scale. A retried first try's bar is grey, a failure's red. The bars are decoration; durations stay as text.

```tsx
<div className="max-w-140">
  <PilotTrace
    defaultOpen
    total={6.8}
    steps={[
      {
        id: "1",
        title: "Searched contacts",
        icon: <Search />,
        state: "done",
        start: 0,
        duration: 1.2,
      },
      {
        id: "2",
        title: "Read Agency Purchase",
        icon: <Search />,
        state: "done",
        start: 1.2,
        duration: 2.1,
      },
      {
        id: "3",
        title: "Reading the matter",
        icon: <FileText />,
        state: "recovered",
        start: 3.3,
        duration: 0.6,
      },
      {
        id: "4",
        title: "Read the matter",
        icon: <Search />,
        state: "done",
        start: 3.9,
        duration: 2.9,
      },
    ]}
  />
</div>
```

### Settling

When a call you watched running finishes, its icon turns into a tick that draws itself in (or a cross if it failed), holds a moment, then turns back into the tool's icon in its new tone. A call that arrives already settled just shows its icon, so a long trace doesn't flash.

```tsx
<SettlingDemo />
```

### Approval card

Primary for the one action the card exists for, Outline for others, Destructive for discarding. Pick one: it spins while onSelect's Promise runs, then resolves into a tick that stays on the chosen button, and the others fade. A stale card swaps its buttons for a line saying so.

```tsx
<ApprovalDemo />
```

### Permission request

A write Firm Pilot holds for approval: the held tool row directly above its card.

```tsx
<PilotPermissionRequest
  toolTitle="Update Jane Citizen’s email"
  toolIcon={<Pencil />}
  title="Update Jane Citizen"
  details={details}
  options={options}
  onSelect={() => undefined}
/>
```

## Props

### PilotToolCall

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `string` |  | What the call does, in its tense: "Searching contacts", "Searched contacts". |
| `icon` (required) | `ReactNode` |  | The tool's 14 px icon; it takes the state's tone. |
| `state` (required) | `"running" \| "done" \| "failed" \| "held" \| "recovered"` |  | running, done, failed, held (waiting on a person) or recovered. |
| `heldChange` | `string` |  | For a held write: what it will change, after the title. |
| `duration` | `number` |  | Seconds the call took. Not shown while it runs. |
| `children` | `ReactNode` |  | What the row shows when opened, on a line down its left. |
| `open` | `boolean` |  | Whether it's open, when you control it. |
| `defaultOpen` | `boolean` |  | Whether it starts open, when it controls itself. |
| `onOpenChange` | `(open: boolean) => void` |  | Runs when it opens or closes, with whether it's now open. |

### PilotReasoning

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `streaming` | `boolean` | `false` | Firm Pilot is still reasoning: the brain breathes and the label says so. |
| `duration` | `number` |  | Seconds it reasoned for, once done. |
| `children` | `ReactNode` |  | The reasoning, as prose. |
| `open` | `boolean` |  | Whether it's open, when you control it. |
| `defaultOpen` | `boolean` | `false` | Collapsed by default once done. |
| `onOpenChange` | `(open: boolean) => void` |  | Runs when it opens or closes, with whether it's now open. |

### PilotTrace

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `steps` (required) | `PilotTraceStep[]` |  | The tool calls, in order, each as a PilotToolCall's props. |
| `total` | `number` |  | The turn's whole time in seconds. Defaults to the end of the last step. |
| `open` | `boolean` |  | Whether it's open, when you control it. |
| `defaultOpen` | `boolean` | `false` | Whether it starts open, when it controls itself. |
| `onOpenChange` | `(open: boolean) => void` |  | Runs when it opens or closes, with whether it's now open. |

### PilotApprovalCard

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `ReactNode` |  | What Firm Pilot wants to do, as a question or statement. |
| `details` | `{ label: string; value: ReactNode }[]` | `[]` | Label and value rows under the title. |
| `options` (required) | `PilotCardOption[]` |  | The choices, as buttons. One is primary: the action the card exists for. |
| `onSelect` (required) | `(value: string) => unknown` |  | Runs with the chosen option's value. Return a Promise and the button spins until it settles, then draws a tick (or a cross if it fails). |
| `readOnly` | `boolean` | `false` | No longer actionable: after a pick, or when the card has gone stale. |
| `selectedValue` | `string` |  | The option the lawyer picked, once they have. |
| `children` | `ReactNode` |  | Extra content between the details and the actions, like an email draft. |

### PilotPermissionRequest

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `ReactNode` |  | What Firm Pilot wants to do, as a question or statement. |
| `details` | `{ label: string; value: ReactNode }[]` |  | Label and value rows under the title. |
| `options` (required) | `PilotCardOption[]` |  | The choices, as buttons. One is primary: the action the card exists for. |
| `onSelect` (required) | `(value: string) => unknown` |  | Runs with the chosen option's value. Return a Promise and the button spins until it settles, then draws a tick (or a cross if it fails). |
| `readOnly` | `boolean` |  | No longer actionable: after a pick, or when the card has gone stale. |
| `selectedValue` | `string` |  | The option the lawyer picked, once they have. |
| `children` | `ReactNode` |  | Extra content between the details and the actions, like an email draft. |
| `toolTitle` (required) | `string` |  | The held write's title, like "Update Jane Citizen's email". |
| `toolIcon` (required) | `ReactNode` |  | The icon of the tool asking, as on its tool call. |
| `heldChange` | `string` |  | What the held write will change, after the tool's title. |
