Canonical document: [docs/protocol.md](../protocol.md) (Chinese). For anyone modifying the relay or writing another client. All replies are JSON; errors are `{"error": "plain sentence"}` with an HTTP status.
Reachable only through the relay (or on the LAN with the phone password). Highlights: `GET /api/state?since=` (bots, activity, pending approvals; long-poll ≤20 s), `GET /api/bots/<id>`, `POST /api/bots/<id>/<action>` (`send`, `stop`, `answer`, `read`, `new`, `upload`…; `send` may carry `"memories": [...]`, the memories ticked above the composer — empty means none, absent means the brain chooses), `POST /api/memory` (owner only) and `POST /api/sync/memory` (mirror) for the memory page and composer suggestions (`{op, bot, …}`; reads `suggest`/`search`/`list`/`stats`/`overview`, writes `add`/`pin`/`edit`/`forget`/`restore`/`merge`/`tidy`/`empty_trash`; see `docs/memory.md`), `GET/POST /api/screen/mac` (drive this Mac from the phone, if enabled), `/screen` + `/api/vnc` (view the bot's computer, noVNC), and the brain-sync family: `GET /api/sync?since=`, `POST /api/sync/edit` (`{ops, client, seq}`), `/api/sync/act`, `/api/sync/upload/<bot>`, `GET /api/sync/file`, `POST /api/sync/models`, `POST/GET /api/sync/screen` (thumbnails; `{kind:"hold"|"wake"}`).
Sending between devices (`src/drops.rs`; owner's phone at `/api/drop/<what>`, the owner's screens at `/api/sync/drop/<what>`): `GET targets` → `{targets: [{id, name, online, kind}], you}`; `GET list` → `{items}` (last 7 days); `POST new``{name, bytes, to}` or `{text, to}` → `{id, chunk}`; `POST chunk?id=&at=` (raw bytes, 2 MB a piece) → `{have}` (a piece that isn't the next one is not written; the sender continues from `have`); `POST done``{id}`; `GET file?id=&at=&len=` (phone only: a piece of a file waiting for the phone); `POST remove` / `retry``{id}`. The brain pushes a parcel to a computer through `/api/device/run` with `tool: "drop_put"` and `args``{id, name, bytes, from}` plus `{probe: true}` (how much do you have?), `{at, data}` (one base64 piece) or `{text}`; the answer is `{ok, have, done, text}`. A computer accepts `drop_put` only from its own brain, with or without 裝置連結.
WebSocket binary frames, each message: `[4-byte big-endian header length][header JSON][raw body]` — bodies are not base64'd; `z: true` means the body is deflate-compressed, `z_ok: true` says the asker understands compressed replies.
| `t` | Direction | Header fields | Meaning |
|---|---|---|---|
| `hello` | AnyBot → relay | `brain`, `hands` | which brain this machine follows; is it hands |
| `job` | relay → AnyBot | `id, method, path, query, user, role, from, z, z_ok` | a request for this machine |
| `reply` | AnyBot → relay | `id, status, ctype, headers, z` | answer to a job |
Both loops poll every 2 ms while busy, 40 ms idle (`wire::Pace`), then flush queued messages; keepalive via WebSocket ping/pong. On disconnect the relay answers pending jobs 503, the AnyBot fails pending calls.
## Sync ops (`/api/sync/edit`)
`set` (one key; `next_*` counters only grow), `patch` (map/list item-level), `merge` (providers/skills/mcp_servers on move), `bot_add` (id collision → new id), `bot_del` (primary bot protected), `bot_set`, `bot_patch` (routines keep the brain's run times), `bot_read`, `bot_order`.
`patch` shapes: `{"value": …}` replaces; `{"map": {"put": {…}, "del": […]}}`; `{"list": {"put": […], "del": [keys], "order": [keys]}}` where keys are item `id`s (or `name`, raw values as-is). Items without id/name, or duplicate keys → send the whole value instead.