# toasterAgent

> The Toaster's adapter for the agent bridge, @cloudlet/toast/agent — its records, verbs, permissions, refusals and the changes it reports.

Source: https://cloudlet.dev/docs/agents/api/toast

`toasterAgent` connects a Toaster controller to [`@cloudlet/agent`](https://cloudlet.dev/docs/agents/api/agent). It lives in the package as the subpath `@cloudlet/toast/agent`, from `1.4.0`; the main entry never loads it. Like the bridge it is **experimental**: its records may change before the bridge reaches `1.0`. The guide is [Agents](https://cloudlet.dev/docs/agents).

```ts title="signature"
import type { CancellationSignal, CloudletController } from '@cloudlet/toast';
import type { ToasterAgent, ToasterAgentOptions } from '@cloudlet/toast/agent';

declare function toasterAgent<Slot, Signal extends CancellationSignal>(
  controller: Omit<CloudletController<Slot, Signal>, 'track'>,
  options?: ToasterAgentOptions,
): ToasterAgent;
```

| Name | Type | Description |
|---|---|---|
| `controller` | `CloudletController` | A controller of this copy of `@cloudlet/toast`; any other throws a `TypeError`. |
| `options.name` | `string` | Optional. The component's name and the prefix of its refs, `[a-z][a-z0-9-]*`; default `toaster`. Two controllers on one agent need two names. |
| returns | `ToasterAgent` | The object to pass to `agent.connect`. |

The adapter touches no DOM, starts no timers, never takes the host from a `Toaster` and never moves focus. It speaks protocol `1`, so any `@cloudlet/agent` 0.x that understands protocol `1` works with it; the two packages do not depend on each other.

## Records

The snapshot holds a `ToasterEntry` for the Toaster, then a `NotificationEntry` per notification in presentation order; together they are `ToasterAgentEntry`. The Toaster's `ref` is its name (`toaster`), a notification's is the name and a number (`toaster:3`). A number is never reused; a new adapter starts counting again.

`ToasterEntry`:

| Field | Type | Description |
|---|---|---|
| `component` | `'toaster'` | |
| `actions` | `readonly string[]` | `open`, `close`, `clear`, `acknowledge`, by state. |
| `island` | `{ open, current }` | Whether the Island is open, and the `ref` of the notification it shows (null while closed). |
| `guest` | `{ ref } \| null` | The app's own content holding the Island instead of notifications, such as a [tour in the Island](https://cloudlet.dev/docs/components/onboarding#island); `ref` is the record to steer it through (`IslandGuideContent.ref`, else its `label`), when it named one. Meanwhile `open` and `show` are refused with "A tour is in progress in the Island" and new notifications wait. Null when notifications have the Island. |
| `count` | `number` | Notifications in the stack. |
| `overflow` | `{ count, pending }` | Notifications that did not fit, and whether that is still unacknowledged. |
| `mounted` | `boolean` | A `Toaster` renders this controller. |

`NotificationEntry`:

| Field | Type | Description |
|---|---|---|
| `component` | `'notification'` | |
| `actions` | `readonly string[]` | `dismiss`, `show` (with a mounted `Toaster`), `press`, `fill`, `submit`, `cancel`, `retry`, by state. |
| `label` | `string` | The title. |
| `text` | `string \| null` | The description as text; for rich content, its announcement. |
| `kind`, `priority`, `occurrences`, `location` | as in [`NotificationSnapshot`](https://cloudlet.dev/docs/components/toaster/api/types#notificationsnapshot) | |
| `remainingMs`, `pauseReasons` | `number \| null`, `readonly PauseReason[]` | Time left, and why it is paused. |
| `progress` | `{ kind, value, label } \| null` | |
| `draft` | `boolean` | Its form holds text that was not sent. |
| `operation` | `OperationEntry \| null` | `{ status, attempt, canCancel, canRetry, cancelRequested }`, without the attempt token. |
| `action` | `ActionEntry \| null` | The primary action, below. |

`ActionEntry` is `{ kind, label, status, message, agent?, href?, fields?, error? }`: `kind` is `command`, `link` or `form`; `status` is `idle`, `pending` or `failed`; `agent` is the action's [marker](#verbs-and-permissions) (`confirm` when the app set none; a link has none); `href` only for a link. A form's `fields` are `FormFieldEntry` records, `{ name, label, kind, required, maxLength, inputMode, filled, filledBy, error }`: `filled` says whether a field holds non-blank text and `filledBy` who wrote it last (`app` for its default, `user`, `agent`, or null), never what. Form values, defaults and placeholders never reach the snapshot. A failed form's `message`, and a field or form `error` that came from the form's own `validate`, are fixed phrases (`Action failed`, `Not accepted by the form's own check`), because the app's text may quote what the person typed; the built-in `required` and `maxLength` messages come as they are.

## Verbs and permissions

| Record | Verb | Args | Needs | What it does |
|---|---|---|---|---|
| Toaster | `open` | — | `allow` | Opens the Island on the notification it shows first. Needs a mounted `Toaster`; refused while the person is using it. |
| Toaster | `close` | — | `allow` | Closes the open Island; refused over a form someone started filling. |
| Toaster | `clear` | — | `allow` | Closes every notification. |
| Toaster | `acknowledge` | — | `allow` | Marks the overflow as seen: `overflow.pending` turns false. |
| Notification | `dismiss` | — | `allow` | Closes it; the record leaves the snapshot (`gone`). |
| Notification | `show` | — | `allow` | Opens the Island on it, like `open`. |
| Notification | `press` | — | the marker | Runs the command and waits: success closes the notification, `pending` means it is still working. |
| Notification | `fill` | `[name, text]` | `allow` | Writes a form field as the agent, by the field's `name`, never over text the person typed. |
| Notification | `submit` | — | the marker | Sends the form and waits, like `press`; a refusal names the fields that were not accepted. |
| Notification | `cancel` | — | `allow` | Asks the operation to stop; result `requested`. |
| Notification | `retry` | — | `allow` | Starts the failed operation again; result `started`. |

`allow` means the step runs unless the bridge's `allow` says no. `press` and `submit` follow the `agent` marker of the command or form (`agent: 'allow' | 'confirm' | 'deny'` on [`CommandAction` and `FormAction`](https://cloudlet.dev/docs/components/toaster/api/types#primaryaction)): `confirm`, the default, needs a person's approval; `deny` removes the verb from `actions`, while `fill` stays. A link has no verb at all: the bridge never opens one, its address is `href`.

**Needs a person.** A `press` or `submit` that nobody approved is brought to the person: `summon` opens the Island on that notification at once, or after the attention delay once they stop using the Toaster, and never moves focus. The agent reads `Needs a person: …` with one of `The Island is open on it.`, `The Island will open on it once they stop using it.` or `No Toaster is on screen, so tell them what it does.`

**The person comes first.** While they use the Toaster (hover, focus, a gesture, a draft in the open Island, the agent's own included), `open` and `show` answer `The person is using the Island. Try again later.`, and `close` over a draft is refused.

## Refusals

The adapter words these itself; the bridge words the rest from the record's `actions`. Labels and titles are quoted with JSON quotes.

| When | Message |
|---|---|
| `open` or `show` without a mounted `Toaster` | `No Toaster is on screen: "open" needs one.` |
| Arguments where none are taken | `"dismiss" on "toaster:3" takes no arguments.` |
| `fill` without a name and a value | `"fill" on "toaster:3" takes a field name and a value.` |
| An unknown field | `No field "x" on "toaster:3". Fields: "reply".` |
| A value that is not a string | `The value for "reply" must be a string.` |
| Too long | `"reply" takes at most 80 characters.` |
| The person typed there | `The person typed in "reply". An agent can't overwrite it.` |
| The form refused | `"Send" was not sent. …` with the fields' messages |
| Already running | `"Send" is already running.` |
| The action failed | `"Send" failed: …` |
| Operations | `This operation has already finished.`, `Cancelling this operation was already requested.`, `This operation can't be cancelled.`, `This operation is still running, so it can't be retried yet.`, `This operation can't be retried.` |

## Changes

`subscribe` reports what changed after every change of the controller, with `source` from the Toaster's own [`ChangeSource`](https://cloudlet.dev/docs/components/toaster/api/types#changesource). `label` is the notification's title.

| Record | Key | Values |
|---|---|---|
| Notification | `present` | `true` when it appears, `false` when it closes, with the [`CloseReason`](https://cloudlet.dev/docs/components/toaster/api/types#closereason) in `reason`. |
| Notification | `content` | The app updated it; both values are null. |
| Notification | `action` | `idle`, `pending`, `failed`. |
| Notification | `operation` | `pending`, `cancel-requested`, `fulfilled`, `rejected`, `cancelled`. |
| Notification | `field:<name>` | Who wrote the field last: `app`, `user`, `agent` or null; only when the author changes, never the text. |
| Toaster | `island` | The `ref` of the notification the Island shows, or null. |
| Toaster | `overflow` | The missed count, after it was acknowledged. |

Two sources follow from the rule rather than from a hand: `action` going back to `idle` because an operation's outcome replaced the content is `system`, and `field:<name>` turning null because the app replaced the form is `app`. A report never holds more than 1000 changes, which is the bridge's limit; a pause or a timer tick changes the snapshot with an empty report.

## ToasterAgent

`ToasterAgent` is the `AgentComponent` the adapter returns: `protocol`, `name`, `snapshot` (the same frozen array until the controller changes, empty after `dispose`), `subscribe`, `check`, `run`, `summon` and `verbs`, the text of each verb above for a model.

The subpath also exports the shapes of the bridge's protocol it implements, so that `@cloudlet/toast` needs no dependency on `@cloudlet/agent`, even for types: `AgentEntryShape`, `AgentSignalShape`, `AgentValueShape`, `AgentPermissionShape`, `AgentStepCallShape`, `AgentStepNamesShape`, `AgentStepCheckShape`, `AgentRunContextShape`, `AgentRunOutcomeShape`, `AgentSummonOutcomeShape`, `AgentChangeShape`, `AgentVerbsShape` and `AgentComponentShape`. They match `AgentEntry`, `AgentSignal` and the rest of the [bridge's types](https://cloudlet.dev/docs/agents/api/agent#writing-an-adapter) one for one; you never write them yourself.
