# Player protocol (custom hosts)

> Frame the player yourself and talk to it with postMessage, without aimigo-embed.js.

Use this only if the [embed script](https://aimigo.live/docs/embed.md) does not fit, for example because your site already has its own avatar host script. The embed script implements exactly this protocol.

## The frame

```html
<iframe src="https://player.aimigo.live/player/latest/aimigo-player.html?origin=https%3A%2F%2Fwww.your-site.com&character=Ben&mode=transparent"
        title="Aimigo digital human"
        allow="autoplay; fullscreen; xr-spatial-tracking"
        allowtransparency="true"
        style="border:0;background:transparent;width:420px;height:620px"></iframe>
```

For a transparent stage over your page, give the frame `pointer-events: none` unless visitors should be able to click the character.

### Query parameters

| Parameter | Meaning |
|---|---|
| `origin` | origin of your page. The player sends messages only to it and accepts commands only from it. **Always set it.** Without it the player locks to the first page that talks to it. |
| `channel` | value of the `source` field in every message; default `aimigo` |
| `character` | character name or id loaded at start |
| `mode` | `transparent` or `room`; `scene` = 3D room for `room` |
| `sharedCache` | `0` keeps the download cache per embedding site (default: shared across sites where the browser allows it, see [performance](https://aimigo.live/docs/performance.md)) |
| `affiliate` | personal link id (personal links); `salesSheet` = sheet key; `greeting=0` = no spoken greeting |
| `debug` | `1` shows the player's status text inside the frame |

## Messages from your page to the player

```js
frame.contentWindow.postMessage(
  { source: 'aimigo', type: 'call', id: 'c1', tool: 'aimigo.character.set', input: { characterId: 'Ben' } },
  'https://player.aimigo.live');
```

`id` is yours; the answer carries the same `id`. `tool` is any [MCP command](https://aimigo.live/docs/mcp-reference.md) the role User may use, plus the player's own tools:

| Tool | Input | Purpose |
|---|---|---|
| `audio.play` | `audioData` (base64), `contentType` (`audio/wav`, `audio/mpeg`), `wordTimestamps?` (JSON string of the list in [embed](https://aimigo.live/docs/embed.md#word-timestamps)) | speak with lip sync |
| `ai.triggers` | `triggerCodes`, `audioFollowing`, `audioTimeoutSeconds`, `responseText` | gestures and expressions for the next audio (needs a loaded character) |
| `contact.get` | – | the contact of a personal link |
| `contact.update` | `interestAccordingAI`, `focus`, `aiNotes`, `generalFeedback`, `bookedMeetingDate` | store what the AI learned |

WAV (16-bit PCM) works on every platform; the embed script converts any browser-decodable audio to 22.05 kHz mono WAV before sending.

## Messages from the player to your page

Listen on `window` and check origin and source:

```js
window.addEventListener('message', (event) => {
  if (event.origin !== 'https://player.aimigo.live' || event.source !== frame.contentWindow) return;
  const m = event.data;
  if (!m || m.source !== 'aimigo') return;
  // m.type: started | progress | ready | result | event | error | contact
});
```

| `type` | Fields | Meaning |
|---|---|---|
| `progress` | `progress` (0…1) | engine download and start-up |
| `started` | `version`, `graphics`, `storage` (`shared`, `partitioned`, `no-handle`, `top-level`, `off`) | the engine runs |
| `ready` | `version`, `graphics` | character and stage are loaded, commands are accepted |
| `result` | `id`, `tool`, `success`, `data`, `error?`, `errorCode?` | answer to one `call` |
| `event` | `detail` = `{ type: 'event', name, data }` | asynchronous outcome or state change (names in [embed](https://aimigo.live/docs/embed.md#events)) |
| `error` | `message` | the player could not start or the initial stage failed |
| `contact` | `contact` = `{ contactId, displayName, greeting, language, formal, fields }` | personal-link contact loaded |

Send commands only after `ready`. Commands sent earlier are queued by the player, but a character command can be superseded by the initial stage transition.

## Rules

- Mode changes and character changes start a transition and answer at once (`operation: 'started'`). Wait for the matching `event` before you rely on the result.
- One transition at a time: when two arrive, the later one wins. To load the transparent stage with a character, send one `aimigo.transparency.set { enabled: true, characterId }` instead of two commands.
- `ai.triggers` needs a loaded character; send it after `ready`.
- There is no command to interrupt audio yet (**planned**); keep audio clips short if your page needs to react quickly.
