# How it works

> The parts of the system, who talks to whom, and what runs where.

## The parts

```text
 Your web page  (https://www.your-site.com)
 ├─ aimigo-embed.js          one script tag; creates the frame and the JavaScript API (window.Aimigo)
 │    │  postMessage: { type: 'call', tool, input }  ──►
 │    │  ◄── { type: 'started' | 'ready' | 'result' | 'event' | 'error' | 'contact' }
 │    ▼
 └─ <iframe>  Aimigo player   (Aimigo player host, its own origin)
      ├─ aimigo-player.js    loads the 3D engine, relays commands, shared download cache, personal links
      └─ 3D engine (Unity WebGL)
           └─ command registry ── every function is an MCP command with a minimum role
                                   (character, scene, transparency, audio, settings, chat, …)

 Aimigo API  (https://aimigo-api-8d940f.azurewebsites.net)
      └─ personal links: contact profile, greeting audio, AI notes  ──►  your Google Sheet (service account)
```

- **Your page** never loads engine files itself and needs no server change. It talks to the player only through `postMessage`, and the player only accepts messages from the page that created it.
- **The player** runs on the Aimigo player host in its own frame. It downloads the engine and the character once and keeps them in the browser cache (see [performance](https://aimigo.live/docs/performance.md)).
- **The engine** renders the character, plays audio with lip sync, and executes **MCP commands**. The same commands are used by your page, by the HTML UI inside the apps, and by AI agents. Each command has a minimum role (see [permissions](https://aimigo.live/docs/permissions.md)).
- **The Aimigo API** is only involved for personal links: it reads a contact from your Google Sheet and writes what the AI learned back. The Google key is stored only on that server.

## What happens when a visitor opens your page

| Time | What happens | What your page sees |
|---|---|---|
| 0 s | The script tag creates the frame | `Aimigo.default` exists |
| first visit: some seconds; later visits: faster | The player downloads and starts the engine (cached afterwards) | message `started` |
| shortly after | The character and the stage (transparent or 3D room) load | message `ready` → `await dh.ready` resolves |
| any time | Your page calls commands (`speak`, `setCharacter`, `call(...)`) | `result` for each call |
| asynchronous | Long operations finish (character loaded, mode changed) | `event` with `name` and `success` |

## Two ways to start a function

1. **Directly from your page**: `dh.speak(...)`, `dh.setCharacter(...)` or the general `dh.call(name, input)`. These calls run with the role **User**: whatever a visitor's browser does, the visitor could also do through the browser's developer tools.
2. **Through an AI agent** that knows the commands: it discovers them with `aimigo.commands.list` and `aimigo.commands.search`, fetches instructions with `aimigo.skills.get`, and calls them like your page does. See [MCP commands](https://aimigo.live/docs/mcp.md).

## Privacy in short

- The frame gets no cookies or data of your page. It knows only what your page sends to it.
- Without a personal link no visitor data is sent to Aimigo apart from what any embedded resource causes (IP address, browser).
- With a personal link the Aimigo API reads only the columns needed for the greeting and the conversation, never e-mail addresses or credits, and writes only the AI columns (see personal links).
