Let an AI model use a real iPhone with a $7 ESP32: AirPlay eyes, USB HID hands, any OpenAI-compatible vision model. No Mac, no developer account, no jailbreak.
Add README in English and Chinese
Henryda099a51e68c
diff --git a/LICENSE b/LICENSEnew file mode 100644index 0000000..796553d--- /dev/null+++ b/LICENSE@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2026 Henry Yuan++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/README.md b/README.mdnew file mode 100644index 0000000..2a503e5--- /dev/null+++ b/README.md@@ -0,0 +1,228 @@+<div align="center">++# ESP32 iPhone Agent++**Let an AI model use a real iPhone — with a $7 ESP32 and no developer account, no Xcode, no jailbreak.**++English · [简体中文](README.zh-CN.md)++[](https://github.com/onehumanbeing/esp32-iphone-agent/actions/workflows/ci.yml)+[](LICENSE)++++</div>++<!-- demo image: docs/images/demo.png -->++The agent sees the phone through AirPlay screen mirroring and touches it through a tiny ESP32-S3 board that+plugs into the phone's USB port as a mouse and keyboard. Any OpenAI-compatible vision model can be the brain —+a cloud API, or a model you run yourself.++## Why++Most iPhone automation drives the phone through XCTest / WebDriverAgent. That needs a Mac, Xcode, a signed+helper app and an Apple developer account (a free account's signature expires every 7 days). This project+uses the same paths a person uses — the screen and a pointer — so the phone needs nothing installed.++| | ESP32 iPhone Agent | XCTest / WebDriverAgent tools |+|----------------------------------|---------------------------|-------------------------------|+| Apple developer account | **Not needed** | Needed (to sign the runner) |+| Xcode | **Not needed** | Needed |+| Computer | Mac **or Linux** | Mac |+| App installed on the phone | **None** | Test runner app |+| Jailbreak | Not needed | Not needed |+| Extra hardware | ESP32-S3 board (~$7) | USB cable |+| Works in any app, incl. games | Yes (it only sees pixels) | Mostly (depends on the accessibility tree) |++## How it works++- **Eyes — AirPlay mirroring.** The phone mirrors its screen to [UxPlay](https://github.com/FDH2/UxPlay) running+ on your computer; a GStreamer pipeline turns the stream into JPEG frames for the agent and the web page.+- **Hands — a USB HID bridge.** An ESP32-S3 plugged into the phone shows up as a standard USB mouse and keyboard.+ With iOS AssistiveTouch on, the mouse moves an on-screen pointer, so a click is a tap and a drag is a swipe.+ The board connects to the computer over Wi-Fi and executes simple line commands (`move`, `click`, `type`, …);+ all logic stays on the computer.+- **Brain — a planner and an actor.** A planner model writes a short plan from the goal and the current screen.+ An actor model then looks at each new screenshot and picks the next action: tap, swipe, draw a path, type, press+ a key, go home, or finish. Both roles can be the same model.+- **Closing the loop.** The pointer is relative, so the server tracks where it is. Before each tap it wiggles the+ pointer, finds it on the mirrored screen and corrects any miss, refining the calibration as it goes. After each+ action it checks whether the screen actually changed and tells the model, so a missed tap is noticed and retried+ instead of built upon. Repeated actions trigger a re-plan; a dropped link or a bad reply is recovered from.++## Hardware++| Part | Notes |+|---|---|+| **ESP32-S3 board** | Tested with a bare [Seeed Studio XIAO ESP32S3](https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/) (~$7, no camera add-on needed). Any ESP32-S3 board whose USB-C port is wired to the S3's native USB works. |+| **iPhone** | USB-C models plug in directly. Lightning models need a Lightning to USB Camera Adapter. The phone powers the board. |+| **Computer** | macOS or Linux, on the same network as the phone; runs UxPlay and the server. |+| **Vision model** | Any OpenAI-compatible chat API that accepts images. Running a model locally is optional. |++Use a USB-C hub with power pass-through if the phone should keep charging while the board is plugged in.++## Quick start++> **Set up with an AI coding agent.** Paste this into Claude Code, Codex or a similar agent on the computer the+> board will connect to:+>+> ```text+> Clone https://github.com/onehumanbeing/esp32-iphone-agent and follow its README to set it up on this machine:+> flash the ESP32-S3 plugged in over USB, install UxPlay and the server, write server/.env for my model API, start+> the server and give me the web UI address. Ask me for the Wi-Fi name/password and the model API key.+> ```++### 1. Flash the board++Install [arduino-cli](https://arduino.github.io/arduino-cli/) and the ESP32 core (3.x), then:++```bash+cp hardware/hid_bridge/secrets.example.h hardware/hid_bridge/secrets.h+# edit secrets.h: 2.4 GHz Wi-Fi name/password and the IP address of the computer running the server+hardware/flash.sh # optional arguments: [serial port] [--debug]+```++### 2. Install and start the server++```bash+# UxPlay — macOS:+brew install uxplay+# UxPlay — Debian / Ubuntu:+sudo apt install uxplay gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-libav++cd server+python3 -m venv .venv && .venv/bin/pip install -e .+cp .env.example .env # set LLM_BASE_URL / LLM_MODEL / LLM_API_KEY+set -a && . ./.env && set +a && .venv/bin/python -m hidagent+```++Open `http://<computer-ip>:8890` in a browser.++### 3. Set up the phone (once)++1. **Settings → Accessibility → Touch → AssistiveTouch**: turn it on.+2. Plug the board into the phone. A round pointer appears; the web page shows *controller connected*.+3. **Control Center → Screen Mirroring → iPhone Agent**. The live screen appears in the web page.+4. In the web page, open **Manual controls → Calibrate**, then click exactly on the pointer in the live screen.++### 4. Give it a task++Type a goal (for example *"Open Settings and go to Wi-Fi"*) and press **Start**, or click one of the examples.++## Web UI++- **Live phone** — click to tap, drag to swipe, scroll to scroll; manual Home, Back, typing and keys.+- **Live log** — every plan, thought and action as it happens, with timings.+- **Step wall** — the screenshot the model decided on for every step, with its tap or swipe drawn on top, plus the+ model's reasoning and how long thinking and acting took. Click a card to zoom in.+- **Emergency stop** (or <kbd>Esc</kbd>) stops the agent and releases the pointer. **Clear history** empties the log.+- English and Simplified Chinese interface.++## Configuration++Set in `server/.env` (see [`.env.example`](server/.env.example)):++| Variable | Default | Meaning |+|---|---|---|+| `LLM_BASE_URL` | `http://localhost:8000/v1` | OpenAI-compatible endpoint |+| `LLM_MODEL` / `LLM_API_KEY` | — | Model name and API key |+| `LLM_EXTRA_BODY` | `{}` | JSON merged into every request (e.g. to turn off a model's thinking mode) |+| `ACTOR_*` / `PLANNER_*` | — | Per-role overrides of any `LLM_*` variable |+| `AIRPLAY_NAME` | `iPhone Agent` | Receiver name in the phone's Screen Mirroring list |+| `PORT` / `DEVICE_PORT` | `8890` / `7300` | Web UI port / port the board connects to |+| `HIDAGENT_DATA` | `~/.hidagent` | Calibration and debug traces |+| `HIDAGENT_DEBUG` | `0` | Debug mode, see below |++### Local model (optional)++A cloud API is enough. If you want everything on your own hardware, this is the setup the project was developed+and timed with:++- Model: [nvidia/Qwen3.8-Flash-Next-NVFP4](https://huggingface.co/nvidia/Qwen3.8-Flash-Next-NVFP4)+- Serving: [Qwen3.8-Flash-Dual-DGX-Sparks-TensorFold](https://github.com/MiaAI-Lab/Qwen3.8-Flash-Dual-DGX-Sparks-TensorFold)+ — two NVIDIA DGX Sparks with tensor parallelism (TP=2), OpenAI-compatible API+- Speed: about 1.3 s per action and 2 s per plan, with thinking mode off:++```bash+LLM_BASE_URL=http://<dgx-spark-ip>:8888/v1+LLM_MODEL=Qwen3.8-Flash-Next+LLM_EXTRA_BODY='{"chat_template_kwargs": {"enable_thinking": false}}'+```++Any other OpenAI-compatible server (vLLM, SGLang, llama.cpp, Ollama, …) with a vision model works the same way.++## Debug mode++Set `HIDAGENT_DEBUG=1` to log every board command with its latency and to save one folder per agent run:++```+~/.hidagent/traces/20261012-005207/+├── steps.jsonl # every model call (prompt, raw reply, latency) and every action+├── 002.jpg # the screenshot each model call saw+└── ...+```++`hardware/flash.sh --debug` builds firmware that echoes each command and its duration over USB serial.++## Board protocol++The board holds no logic. The server sends one command per line over TCP; the board answers `ok` or `err <reason>`.++| Command | Effect |+|---|---|+| `ping` | Health check |+| `home` | Push the pointer into the top-left corner (a known position) |+| `move <dx> <dy>` | Relative pointer move, at a constant pace so iOS pointer acceleration stays predictable |+| `press` / `release` / `click` | Left button down / up / both |+| `scroll <v> <h>` | Scroll wheel |+| `type <text>` | Type ASCII text |+| `key <combo>` | Press keys, e.g. `enter`, `cmd+h`, `cmd+space` |++If the Wi-Fi link drops, the board releases all buttons and reconnects on its own.++## Project layout++```+hardware/ ESP32-S3 firmware (Arduino) and flash script+server/ agent, web UI, screen capture and board link (Python, FastAPI)+ serve/: optional minimal server to host a Hugging Face vision model yourself+```++## Roadmap++- [ ] Test a fully local setup with DiffusionGemma (`server/serve/` has an experimental OpenAI-compatible server)+- [ ] Correct the pointer only when needed, to make taps faster+- [ ] More example tasks across popular apps++## Limitations++- iOS does not mirror some screens (passcode entry, Apple Pay, DRM video); the agent cannot see them.+- Mirroring stops when the phone locks; keep auto-lock off while the agent works.+- Keyboard input is ASCII only.+- Re-calibrate after changing AssistiveTouch tracking speed.+- On Linux machines with several network interfaces, limit Avahi (mDNS) to the interface on the phone's network+ if the phone does not list the receiver.++## Responsible use++Only automate devices and accounts you own or are allowed to use, and respect the terms of the apps involved. The+agent acts with your hands: watch it, and keep the emergency stop within reach.++## Contributing++Issues and pull requests are welcome. Run the checks before sending a change:++```bash+cd server && pip install -e ".[dev]" && ruff check . && pytest+```++## Acknowledgements++[UxPlay](https://github.com/FDH2/UxPlay) (AirPlay receiver) ·+[Arduino ESP32 core](https://github.com/espressif/arduino-esp32) and [TinyUSB](https://github.com/hathach/tinyusb) (USB HID) ·+[FastAPI](https://fastapi.tiangolo.com/)++## License++[MIT](LICENSE)diff --git a/README.zh-CN.md b/README.zh-CN.mdnew file mode 100644index 0000000..df8d7b2--- /dev/null+++ b/README.zh-CN.md@@ -0,0 +1,219 @@+<div align="center">++# ESP32 iPhone Agent++**让 AI 操作一部真实的 iPhone:只要一块约 50 元的 ESP32,不需要开发者账号、不需要 Xcode、不用越狱。**++[English](README.md) · 简体中文++[](https://github.com/onehumanbeing/esp32-iphone-agent/actions/workflows/ci.yml)+[](LICENSE)++++</div>++<!-- demo image: docs/images/demo.png -->++Agent 通过 AirPlay 屏幕镜像"看"手机,通过一块插在手机 USB 口上的 ESP32-S3 小板子"摸"手机:板子对手机来说就是一个+USB 鼠标加键盘。大脑可以是任何兼容 OpenAI 接口的视觉模型,云端 API 或自己部署的模型都行。++## 为什么做这个++大多数 iPhone 自动化方案通过 XCTest / WebDriverAgent 控制手机,需要 Mac、Xcode、一个签过名的辅助 App,还要 Apple+开发者账号(免费账号的签名 7 天就过期)。本项目走的是人用手机的路径:看屏幕、动指针,所以手机上什么都不用装。++| | ESP32 iPhone Agent | XCTest / WebDriverAgent 类工具 |+|--------------------------|------------------------|--------------------------------|+| Apple 开发者账号 | **不需要** | 需要(给测试程序签名) |+| Xcode | **不需要** | 需要 |+| 电脑 | Mac **或 Linux** | Mac |+| 手机上安装 App | **不需要** | 需要装测试程序 |+| 越狱 | 不需要 | 不需要 |+| 额外硬件 | ESP32-S3 开发板(约 50 元) | USB 线 |+| 任意 App(包括游戏) | 可以(只看像素) | 大多可以(取决于无障碍控件树) |++## 工作原理++- **眼:AirPlay 镜像。** 手机把屏幕镜像到电脑上运行的 [UxPlay](https://github.com/FDH2/UxPlay),GStreamer 把视频流+ 转成一帧帧 JPEG,供 Agent 和网页使用。+- **手:USB HID 桥。** 插在手机上的 ESP32-S3 被识别为标准的 USB 鼠标和键盘。打开 iOS 的辅助触控(AssistiveTouch)后,+ 鼠标会控制屏幕上的一个指针:点击就是点按,拖动就是滑动。板子通过 Wi-Fi 连接电脑,只执行简单的行命令+ (`move`、`click`、`type` 等),所有逻辑都在电脑上。+- **脑:规划模型 + 执行模型。** 规划模型根据目标和当前屏幕写一个简短的计划;执行模型每次看最新截图,选出下一个动作:+ 点按、滑动、画一笔、输入、按键、回主屏幕或结束。两个角色可以用同一个模型。+- **闭环校验。** 鼠标是相对移动的,所以服务端会记录指针位置。每次点按前,它会轻推一下指针,在镜像画面里找到它的真实位置,+ 偏了就先纠正,同时修正校准。每个动作之后检查屏幕是否真的变化了,并告诉模型。点空了会被发现并重试,而不是将错就错。+ 同一个动作重复多次会触发重新规划;断线或模型回复出错都能自动恢复。++## 硬件++| 部件 | 说明 |+|---|---|+| **ESP32-S3 开发板** | 实测使用 [Seeed Studio XIAO ESP32S3](https://wiki.seeedstudio.com/cn/xiao_esp32s3_getting_started/) 裸板(约 50 元,不需要摄像头扩展板)。任何 USB-C 口直连 S3 原生 USB 的 ESP32-S3 板子都可以。 |+| **iPhone** | USB-C 机型直接插;Lightning 机型用 Lightning 转 USB 相机转换器。板子由手机供电。 |+| **电脑** | macOS 或 Linux,和手机在同一个局域网;运行 UxPlay 和服务端。 |+| **视觉模型** | 任何支持图片输入、兼容 OpenAI 接口的对话模型。本地部署是可选的。 |++如果希望插着板子时手机还能充电,用一个支持 PD 充电直通的 USB-C 扩展坞。++## 快速开始++> **让 AI 编程助手帮你装。** 在板子要连接的那台电脑上,把下面这段话发给 Claude Code、Codex 或类似的 Agent:+>+> ```text+> Clone https://github.com/onehumanbeing/esp32-iphone-agent and follow its README to set it up on this machine:+> flash the ESP32-S3 plugged in over USB, install UxPlay and the server, write server/.env for my model API, start+> the server and give me the web UI address. Ask me for the Wi-Fi name/password and the model API key.+> ```++### 1. 刷固件++安装 [arduino-cli](https://arduino.github.io/arduino-cli/) 和 ESP32 core(3.x),然后:++```bash+cp hardware/hid_bridge/secrets.example.h hardware/hid_bridge/secrets.h+# 编辑 secrets.h:填 2.4 GHz Wi-Fi 名称和密码,以及运行服务端的电脑的 IP+hardware/flash.sh # 可选参数:[串口] [--debug]+```++### 2. 安装并启动服务端++```bash+# UxPlay,macOS:+brew install uxplay+# UxPlay,Debian / Ubuntu:+sudo apt install uxplay gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-libav++cd server+python3 -m venv .venv && .venv/bin/pip install -e .+cp .env.example .env # 填 LLM_BASE_URL / LLM_MODEL / LLM_API_KEY+set -a && . ./.env && set +a && .venv/bin/python -m hidagent+```++浏览器打开 `http://<电脑 IP>:8890`。++### 3. 设置手机(只需一次)++1. **设置 → 辅助功能 → 触控 → 辅助触控**:打开。+2. 把板子插到手机上,屏幕上会出现一个圆形指针,网页显示控制器已连接。+3. **控制中心 → 屏幕镜像 → iPhone Agent**,网页里出现实时画面。+4. 在网页里打开 **手动操控 → 校准**,然后在实时画面里精确点一下指针的位置。++### 4. 下达任务++输入一个目标(例如"打开设置,进入 Wi-Fi"),点 **开始**;也可以直接点下面的示例。++## 网页界面++- **实时手机画面**:点击 = 点按,拖动 = 滑动,滚轮 = 滚动;也可以手动回主屏幕、返回、输入文字和按键。+- **实时日志**:每一个计划、思考和动作实时显示,带耗时。+- **步骤截图墙**:每一步模型做决定时看到的截图,上面画出点按或滑动的位置,附带模型的理由和思考、执行各用了多久;+ 点卡片可以放大。+- **紧急停止**(或按 <kbd>Esc</kbd>):立即停止 Agent 并松开指针。**清空记录**:清空日志和截图墙。+- 界面支持英文和简体中文。++## 配置++写在 `server/.env` 里(参考 [`.env.example`](server/.env.example)):++| 变量 | 默认值 | 说明 |+|---|---|---|+| `LLM_BASE_URL` | `http://localhost:8000/v1` | 兼容 OpenAI 的接口地址 |+| `LLM_MODEL` / `LLM_API_KEY` | — | 模型名称和 API Key |+| `LLM_EXTRA_BODY` | `{}` | 合并进每次请求的 JSON(例如关闭模型的思考模式) |+| `ACTOR_*` / `PLANNER_*` | — | 单独覆盖执行模型 / 规划模型的任意 `LLM_*` 配置 |+| `AIRPLAY_NAME` | `iPhone Agent` | 手机"屏幕镜像"列表里显示的名字 |+| `PORT` / `DEVICE_PORT` | `8890` / `7300` | 网页端口 / 板子连接的端口 |+| `HIDAGENT_DATA` | `~/.hidagent` | 校准数据和调试记录 |+| `HIDAGENT_DEBUG` | `0` | 调试模式,见下文 |++### 本地模型(可选)++用云端 API 就够了。如果想全部跑在自己的硬件上,下面是本项目开发和测速时用的配置:++- 模型:[nvidia/Qwen3.8-Flash-Next-NVFP4](https://huggingface.co/nvidia/Qwen3.8-Flash-Next-NVFP4)+- 部署:[Qwen3.8-Flash-Dual-DGX-Sparks-TensorFold](https://github.com/MiaAI-Lab/Qwen3.8-Flash-Dual-DGX-Sparks-TensorFold),+ 两台 NVIDIA DGX Spark 张量并行(TP=2),提供 OpenAI 兼容接口+- 速度:关闭思考模式后,每个动作约 1.3 秒,每次规划约 2 秒:++```bash+LLM_BASE_URL=http://<dgx-spark-ip>:8888/v1+LLM_MODEL=Qwen3.8-Flash-Next+LLM_EXTRA_BODY='{"chat_template_kwargs": {"enable_thinking": false}}'+```++其他兼容 OpenAI 接口的服务(vLLM、SGLang、llama.cpp、Ollama 等)配上视觉模型,用法完全一样。++## 调试模式++设置 `HIDAGENT_DEBUG=1` 后,每条板子命令及其耗时都会写进日志,每次 Agent 运行会单独保存一个文件夹:++```+~/.hidagent/traces/20261012-005207/+├── steps.jsonl # 每次模型调用(提示词、原始回复、耗时)和每个动作+├── 002.jpg # 每次模型调用看到的截图+└── ...+```++`hardware/flash.sh --debug` 会编译一个调试版固件,通过 USB 串口回显每条命令和耗时。++## 板子协议++板子本身不含任何逻辑。服务端通过 TCP 每行发一条命令,板子回复 `ok` 或 `err <原因>`。++| 命令 | 作用 |+|---|---|+| `ping` | 健康检查 |+| `home` | 把指针推到左上角(一个已知位置) |+| `move <dx> <dy>` | 相对移动指针,匀速移动,让 iOS 的指针加速度可预测 |+| `press` / `release` / `click` | 左键按下 / 松开 / 点击 |+| `scroll <v> <h>` | 滚轮 |+| `type <text>` | 输入 ASCII 文字 |+| `key <combo>` | 按键,例如 `enter`、`cmd+h`、`cmd+space` |++Wi-Fi 断开时,板子会松开所有按键并自动重连。++## 目录结构++```+hardware/ ESP32-S3 固件(Arduino)和刷写脚本+server/ Agent、网页界面、屏幕采集和板子连接(Python,FastAPI)+ serve/:可选的极简服务,用来自己部署 Hugging Face 视觉模型+```++## 路线图++- [ ] 测试基于 DiffusionGemma 的纯本地方案(`server/serve/` 里有实验性的 OpenAI 兼容服务)+- [ ] 指针校正改为按需执行,让点按更快+- [ ] 更多常用 App 的示例任务++## 限制++- iOS 不镜像部分界面(输入密码、Apple Pay、有版权保护的视频),Agent 看不到这些画面。+- 手机锁屏后镜像会断开,Agent 工作时请关闭自动锁定。+- 键盘只支持 ASCII 输入。+- 修改辅助触控的跟踪速度后需要重新校准。+- 在有多个网卡的 Linux 机器上,如果手机列表里看不到接收端,把 Avahi(mDNS)限制在手机所在网络的网卡上。++## 合理使用++只自动化你自己拥有或被允许使用的设备和账号,并遵守相关 App 的条款。Agent 是在替你动手:请看着它,紧急停止按钮随时备好。++## 参与贡献++欢迎提 Issue 和 Pull Request。提交前请先跑一遍检查:++```bash+cd server && pip install -e ".[dev]" && ruff check . && pytest+```++## 致谢++[UxPlay](https://github.com/FDH2/UxPlay)(AirPlay 接收端) ·+[Arduino ESP32 core](https://github.com/espressif/arduino-esp32) 和 [TinyUSB](https://github.com/hathach/tinyusb)(USB HID) ·+[FastAPI](https://fastapi.tiangolo.com/)++## 许可证++[MIT](LICENSE)diff --git a/docs/images/.gitkeep b/docs/images/.gitkeepnew file mode 100644index 0000000..e69de29