# Embed reference

[中文](embed.zh-CN.md)

This page lists the messages between an artifact's Shell (or the home page) and the app that shows it, and the API of
`@openartifactsai/harness-kit/embed`. The types are in `packages/protocol/src/shell.ts` (`HostToShell`, `ShellToHost`,
`HomeToHost`). For how to use them, read [Showing pages in your UI](../integrating/embedding.md).

## Channels

| Mode | How messages travel |
| --- | --- |
| Frame | `window.postMessage` between your page and the Shell's iframe. The Shell sends `shell_hello` to `*`; every later message goes to the host's origin only. |
| Top-level view | Through the daemon. The Shell, loaded with `?oa_view=<ticket>`, claims the ticket; what it says reaches your adapter as `shell.event`; you answer with `shell.command`. A host whose origin cannot be declared (a `file://` page) may load the ticketed address in its own iframe: that Shell also talks through the daemon, and neither greets nor hears its parent frame. |

Every message is an object with the key `openArtifacts` naming it. Host protocol version: `1` (`HOST_PROTOCOL`). Text
fields are cut to 8000 characters (`MAX_HOST_TEXT`).

Before the rename (0.22 and earlier) the key was `openArtifact`. The Shell, the home page and the kit send both keys, and
read a message that has only `openArtifact` as if it had `openArtifacts`, so a host on an older kit and a newer Shell (or
the other way round) still understand each other. Write `openArtifacts`; a Shell from before the rename may also send an
artifact as `open-artifact://artifact/<id>`.

## Host → Shell

| Message | Fields | |
| --- | --- | --- |
| `host_hello` | `caps` (`sessions?`, `toolbar?`, `artifacts?`, `switcher?`), `agentName?`, `harness?`, `protocol?`, `theme?` (`scheme`, `accent?`), `destinations?` (`id`, `label`; at most 8), `library?` (`harness?`, `workspace?`) | the answer to `shell_hello` or `home_hello`; send it again when the theme changes (a theme without `accent` takes back the accent an earlier one set) |
| `goto` | `hash` | show this place; the Shell answers with `place` |

`caps.sessions`: you can start and open sessions. `caps.toolbar`: you draw your own toolbar, so the Shell hides its
close button. `caps.switcher`: your app lists and switches artifacts itself, so the Shell's title menu has no
"Switch artifact" (with "All artifacts"). `caps.artifacts` (home page only): a click on an artifact sends `open_artifact` instead of navigating.
`library` (home page only): list only that harness's artifacts (by the owner session's harness), or that folder's: the
owner session works in it, the artifact's folder is under it, or the artifact was made for a project the folder is in
or holds (so an artifact kept in the person's Open Artifacts folder is still listed for its project).

## Shell → host

| Message | Fields | Declared host only |
| --- | --- | --- |
| `shell_hello` | `version`, `protocol`, `artifact`, `id`, `title`, `type` | |
| `shell_ready` | `artifact` | |
| `new_session` | `prompt`, `artifact` | `prompt` is empty to an undeclared host |
| `open_session` | `session` (or `null`), `harness` | |
| `close` | — | |
| `place` | `artifact`, `hash` | |
| `comment_summon` | `text` | `text` is empty to an undeclared host |
| `open_external` | `url` (`http(s)` only) | yes |
| `saved_file` | `path` (absolute) | yes |
| `saved` | `state` (`saved`, `saving`, `unsaved`, `reconnecting`, `readonly`, or `null`), `leaveLosesWork` | |
| `model_use` | `artifact`, `source`, `running`, `calls`, `tokens` (or `null`), `approx` | yes |
| `model_notice` | `artifact`, `source`, `scope` (`type` or `all`) | yes |

`place` comes after `shell_ready`, whenever the place changes, and in answer to `goto`. `saved` comes after
`host_hello` and whenever the page's save state or `leaveLosesWork` changes; `state` is `null` before the page reports
one and while another page loads. `leaveLosesWork: true` means closing now loses work: ask before you close the view.
An older Shell sends no `saved`.

`model_use` and `model_notice` come only while the Shell bar is hidden (`?toolbar=0`, an inline view; `caps.toolbar` alone does not hide it),
where the bar's AI mark and its notice cannot show. `model_use` comes when a page's model call starts and when it
ends: `source` is the model source by name, `running` the calls running now, `calls` and `tokens` today's for this
artifact (`tokens` is `null` when the source reported none), and `approx: true` when some of those token numbers are
estimated. `model_notice` comes when a page starts using a model without asking, because its type's permission
(`scope: 'type'`) or "Always allow" (`'all'`) let it; the bar would say so with "Manage" and "Not this page". Show it
your way, and send the person to the Shell page (its AI models panel) to change it. With the bar hidden and no
declared host, the Shell shows that notice over the top of the page instead.

## Home page → host

| Message | Fields |
| --- | --- |
| `home_hello` | — (sent to `*` on load) |
| `home_ready` | — |
| `open_artifact` | `url`, `id`, `title`, `type` |
| `open_session`, `new_session` | as from the Shell |

## Artifact page → Shell: initial view and saving

The page's `connectShell()` connection from `@openartifactsai/runtime` sends `ChromeReport` with
`report(...)` (chrome protocol 1). These reports are between the artifact page and the Shell, separately from the
host messages above.

- `ready: false` keeps the Shell's loading cover over the initial view. Send it before the frame loads, then
  `ready: true` once the view is restored. Omitting `ready` also shows the page. Once a document is shown, later
  `false` reports cannot cover it again; the Shell lifts the cover after `CHROME_READY_TIMEOUT_MS` (10 seconds)
  even if the page never becomes ready.
- `saved: 'saving'` includes edits waiting for a debounced write. The Shell shows “Saving…” only after 500 ms.
  Use `saved: 'unsaved'` for a failed save, and leave `saved` out when there is no save state to report.
- `shellArtifactId()` and `ShellConnection.artifactId` read the stable `open_artifacts_id` from the framed page's
  URL, or return `null` outside the Shell or without an id. Use it to key remembered page state instead of a
  cloud-host page path whose token changes on each reload; it is not an authorization credential.

## Who is a declared host

The Shell reads `GET /api/open-artifacts.embed` (`hosts`: the origins connected adapters declared in
`caps.open.origins`) and judges the origin that sent `host_hello`:

| Verdict | When | Effect |
| --- | --- | --- |
| declared | a connected adapter declared the origin, or the Shell runs in a view opened with a ticket (top-level or framed) | everything |
| refused | adapters declared origins, but not this one | `host_hello` is ignored; a host this origin made while no list could be read is dropped |
| undeclared | no adapter declared any origin, or no list could be read yet | empty `prompt` and `text`; no `open_external`, `saved_file` or destinations |

The list is read again for each `host_hello`. A read that fails after one succeeded is judged by the last list read,
so a declared host stays declared through a theme change while the daemon is slow. When two hellos are in flight, the
later one's answer stands.

## View tickets

| Rule | Value |
| --- | --- |
| URL parameter | `oa_view` |
| Use | once, by the connection it was issued to |
| Unclaimed after | 2 minutes |
| A Shell that reloads may come back within | 30 seconds |
| Commands queued before the Shell connects | 32 |
| Views per adapter connection | 64 |
| `shell.event` order | as the Shell said them: it sends one at a time, the next once the daemon took the last |

A ticket ends with its adapter connection.

## `embedArtifact(target, options)`

`target` is an iframe (frame mode) or a transport `{ send(command), onEvent(listener) }` (a view opened with a ticket, also when your iframe shows it).

| Option | |
| --- | --- |
| `url` | `viewUrl` (frame) or `embed_url` (view); set as the iframe's `src` when it differs |
| `agentName`, `harness`, `toolbar`, `switcher`, `theme`, `destinations` | what `host_hello` says |
| `onReady(info)` | `info`: `artifact`, `id`, `title`, `type`, `version`, `protocol` |
| `onOpenSession({ session, harness, artifact })` | sets `caps.sessions` |
| `onNewSession({ prompt, artifact })` | sets `caps.sessions` |
| `onCommentSent({ text })` | |
| `onPlace({ artifact, hash })` | |
| `onClose()` | |
| `onOpenExternal(url)` | default: `window.open` |
| `onSavedFile(path)` | |
| `onSaved({ state, leaveLosesWork })` | the `saved` message |
| `onModelUse({ artifact, source, running, calls, tokens, approx })` | the `model_use` message (bar hidden, declared host) |
| `onModelNotice({ artifact, source, scope })` | the `model_notice` message (bar hidden, declared host) |
| `capture()`, `saveThumbnail(jpeg, artifact)` | thumbnails: 2 s after ready, then after a place change at most once a minute |
| `onError(error)` | a callback or a thumbnail failed |
| `window` | the window to listen on |

Returns `{ artifact, place, ready, saved, goto(hash), setTheme(theme), url(), destroy() }`. `url()` is the Shell's address with
the last place and without a ticket.

## `embedLibrary(iframe, options)`

Options: `url`, `harness`, `workspace`, `agentName`, `theme`, `onOpenArtifact(artifact)` (sets `caps.artifacts`),
`onOpenSession({ session, harness })`, `onNewSession({ prompt, artifact })`, `onReady()`, `onError`, `window`. Returns
`{ setTheme(theme), destroy() }`.

## Snapshot view

For a page that mounts the Shell's `App` itself to show a read-only snapshot to anyone with a link
([how](../integrating/embedding.md#a-read-only-snapshot-for-anyone-with-a-link)). The boot's part is in
`packages/protocol/src/library.ts`; the actions' part in `packages/shell/src/shell/snapshot.ts`.

`ShellBoot.snapshot` (`ShellSnapshot`): present, the bar is a snapshot view and the Shell reads the boot as
`typeServer: { role: 'viewer' }`.

| Field | |
| --- | --- |
| `brand?` | `{ label, href, logoUrl? }`: the home link at the left, an icon the size of the bar's buttons (same tab). `logoUrl` absent: a house. `label` is its accessible name and tooltip, not shown beside it. `href` and `logoUrl` must be an `https:`/`http:` URL or a path on this origin, `label` 1–120 characters; otherwise there is no link |
| `updatedAt?` | ISO 8601: "Updated 27 minutes ago" as the first line of the title's menu (not a row: it takes no focus), the full date and time as its title; a time that does not parse is left out |

`setSnapshotActions({ main?, menu? })` sets what the bar offers beyond the boot; called again, it replaces both (a run
in progress is let go and the button is idle). No effect outside a snapshot view.

| Field | |
| --- | --- |
| `main?` | `{ label, icon, run({ progress }) }`, or `null`: the main action. `run` may return a promise; while it runs the button is busy (`aria-busy`, "Working…" or the last `progress(text)`) and a press does nothing; it settles idle, or failed when it throws or rejects (the error's message, else "Didn't work — try again"), and a press runs it again |
| `menu?` | rows of the menu the title opens, in order, under the updated line: `{ label, icon?, href }` (an `<a>` with `target="_blank"` and `rel="noopener noreferrer nofollow"`; an unsafe address drops the row), `{ label, icon?, run() }`, or `{ separator: true }` (kept only between two rows). With no rows and no `updatedAt`, the title is plain text |

The bar, left to right: the home link, the title, "Read-only" (quiet text, only for screen readers on a narrow bar),
then the main action at the right. The title is a button with a chevron that opens the menu below it, aligned to its
start; Escape closes the menu and gives the focus back to the title, and the arrow keys move through the rows.

Data attributes for tests: `header[data-shell-snapshot]`, `a[data-shell-snapshot-brand]`, `[data-shell-title]` (the
title button, or plain text when there is no menu; its text is the title), `button[data-shell-snapshot-menu]` (the same
button), `time[data-shell-snapshot-updated]` (in the open menu), `[data-shell-snapshot-readonly]`,
`[data-shell-snapshot-main="idle|busy|failed"]`.

## Remote pages

`remotePolicyOf`, `remoteGate` and the types `RemotePagePolicy`, `AllowRemote` are exported from the kit's main entry
for apps that gate remote pages themselves ([remote types](../integrating/remote-types.md#showing-a-remote-page)).
