Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/site/docs/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Visual localization does not depend on the UI rendering stack, so the same API w
<AgentPlatforms hideHeading />
</div>

Beyond built-in platforms, you can use [`@midscene/connect`](./midscene-connect) to connect any UI over HTTP—including car screens, embedded panels, and physical devices driven by robotic arms or HID modules. See [midscene-test-everything](https://github.com/web-infra-dev/midscene-test-everything) for real-world hardware integrations.

### Model options and cost {#model-options}

<div className="rp-not-doc introduction-block">
Expand Down
6 changes: 6 additions & 0 deletions apps/site/docs/en/integrate-with-any-interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import SetupEnv from './common/setup-env.mdx';

# Integrate with any interface

:::info Prefer Midscene Connect for connecting custom devices
For new device integrations, use [Midscene Connect](./midscene-connect) (`@midscene/connect`). You can describe a device in one `.mjs` file, serve it over HTTP, and drive it from the Agent SDK, CLI, or Playground without implementing `AbstractInterface`.

Implementing `AbstractInterface` directly is no longer the recommended path for connecting custom devices. This page remains available for maintaining existing `AbstractInterface` implementations and referencing low-level APIs such as `defineAction`.
:::

You can use Midscene Agent to control any interface—such as IoT devices, in-house apps, and in-vehicle displays—by implementing a UI operation class that conforms to `AbstractInterface`.

After implementing the UI operation class, you get the full capabilities of Midscene Agent:
Expand Down
179 changes: 179 additions & 0 deletions apps/site/docs/en/midscene-connect.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import SetupEnv from './common/setup-env.mdx';

# Connect any UI

Use [`@midscene/connect`](https://github.com/web-infra-dev/midscene/tree/main/packages/connect) to connect any screen or custom hardware to Midscene—such as robotic arms, capture cards with HID modules, in-vehicle displays, IoT devices, or remote machines.

Describe the device in one `.mjs` file, serve it over HTTP, and drive it with the Agent SDK, the CLI, or the Playground without writing an `AbstractInterface` class.

## Real-world examples

See the [midscene-test-everything](https://github.com/web-infra-dev/midscene-test-everything) repository for real-world hardware and platform integrations built on `@midscene/connect`:

- **Robot arm × physical touchscreen** (`mg400-service`, `arm-kit`, `robotics-vision`): camera capture, ArUco screen calibration, coordinate mapping, and a DOBOT MG400 reference setup for car screens and industrial panels.
- **Bluetooth HID × physical iPhone** (`ios-airlink`): driving physical iPhones from macOS over Bluetooth HID with USB, AirPlay, or camera screen capture, without jailbreaking or enabling Developer Mode.

## Set up API keys for model

<SetupEnv />

## 1. Define a device

One `.mjs` file describes one kind of device, and `options` selects a concrete instance. Keys in `actions` that match a built-in input primitive are plain functions; any other key is a custom action.

```js
// car-screen.mjs
export default {
name: 'Car Screen',
description: 'In-car touchscreen driven by a robot arm', // added to aiAct context
pointerType: 'touch', // required with `swipe`: 'touch' | 'mouse'
options: {
type: 'object',
properties: { armId: { type: 'string' } },
required: ['armId'],
},
async connect({ armId }) {
const arm = await openArm(armId);
return {
screenshot: () => camera.jpeg(), // PNG/JPEG Buffer, data URL, or base64
actions: {
tap: (p) => arm.tap(p.x, p.y),
swipe: (from, to, opts) => arm.swipe(from, to, opts?.duration),
homeButton: () => arm.pressHome(),
wakeVoiceAssistant: {
description: 'Wake up the voice assistant with a phrase',
params: {
type: 'object',
properties: {
phrase: { type: 'string' },
button: { 'x-midscene-locate': true, description: 'The mic button' },
},
required: ['phrase'],
},
call: ({ phrase, button }) => arm.tapAndSay(button.center, phrase),
},
},
destroy: () => arm.close(),
};
},
};
```

`import { defineDevice } from '@midscene/connect'` adds editor type hints, but a plain default export works without it.

`size()` is optional in the object returned by `connect()`. When omitted, screen dimensions are read from the current screenshot.

### Built-in primitives

The function signatures match `@midscene/core`'s `InputPrimitives`. Coordinates are in screenshot pixels.

| Group | Primitives | Rule |
| --- | --- | --- |
| Pointer | `tap`, `doubleClick`, `rightClick`, `middleClick`, `hover`, `longPress`, `dragAndDrop` | Every other pointer primitive requires `tap` |
| Swipe | `swipe`, `pinch` | `swipe` requires `pointerType` (a `mouse` swipe also requires `tap`); `pinch` requires `swipe` with `pointerType: 'touch'` |
| Keyboard | `typeText`, `keyboardPress`, `clearInput`, `cursorMove` | `typeText`, `keyboardPress`, and `clearInput` must be provided together |
| Scroll | `scroll` | |
| System | `backButton`, `homeButton`, `recentAppsButton` | |

### Parameter schemas

Custom action `params` and device `options` use a JSON Schema subset:

- `string` (`enum`, `minLength`, `maxLength`)
- `number` / `integer` (`minimum`, `maximum`)
- `boolean`
- `array` (`items`, `minItems`, `maxItems`)
- `object` (`properties`, `required`)
- `description`

A top-level property marked `{ "x-midscene-locate": true }` is an element located by the AI model. Your `call` function receives `{ description, center, rect }`.

`serve` rejects any keyword outside this subset at startup. Request bodies are validated strictly, so unknown keys fail.

## 2. Serve the device

Start the device service first. It keeps the device connected and exposes an HTTP API with Swagger UI at `/api/docs/`:

```bash
npx @midscene/connect serve ./car-screen.mjs --arm-id mg400-1
# ✔ Car Screen is ready
# Connect URL: http://127.0.0.1:5820
# API docs: http://127.0.0.1:5820/api/docs/
```

- Each `serve` process holds one device instance. Pass `--port` to serve additional devices.
- When `--host` is not a loopback address, the server requires bearer token authentication. Pass `--token <token>`, or use the token generated and printed at startup.

## 3. Drive the device

### Agent SDK

Create an Agent connected to the served device with `agentOverConnectProtocol`:

```ts
import { agentOverConnectProtocol } from '@midscene/connect';

const agent = await agentOverConnectProtocol({
url: 'http://127.0.0.1:5820',
// token: '...', // or set MIDSCENE_CONNECT_TOKEN
// agentOptions: { ... }, // standard Agent options
});

await agent.aiAct('Open the music app and search for "Jay Chou"');
await agent.aiAssert('Search results are visible');
```

- `url` defaults to `MIDSCENE_CONNECT_URL`, then `http://127.0.0.1:5820`.
- `token` defaults to `MIDSCENE_CONNECT_TOKEN`.
- If the device defines `description` and `agentOptions` does not set an `aiAct` context, `description` is automatically added to `aiContexts.aiAct`.

### CLI and Playground

The CLI builds its subcommands dynamically from the served device's action space:

```bash
export MIDSCENE_CONNECT_URL=http://127.0.0.1:5820 # or pass --url / --token
npx @midscene/connect act --prompt "Open the music app"
npx @midscene/connect tap --locate "the search box"
npx @midscene/connect wakevoiceassistant --phrase "hello"
npx @midscene/connect playground
```

`playground` supports `--url`, `--token`, `--port` (default `5800`), and `--no-open`.

## Embed in an existing server

To mount the Connect protocol inside an existing Node HTTP server:

```js
import http from 'node:http';
import { createConnectHandler } from '@midscene/connect/server';

const handler = createConnectHandler(
{ name: 'Phone', screenshot, actions },
{
basePath: '/devices/phone-1/api',
token,
},
);

http.createServer((req, res) => handler.handle(req, res) || next(req, res));
// Clients connect with url: http://host:port/devices/phone-1
```

## HTTP API

| Method | Path | Description |
| --- | --- | --- |
| `GET` | `/api/info` | Protocol version, name, description, `pointerType`, primitives, and custom actions |
| `GET` | `/api/screenshot` | Returns `{ image: dataURL, width, height }` |
| `GET` | `/api/size` | Returns `{ width, height }` |
| `POST` | `/api/primitives/{name}` | Runs a built-in primitive |
| `POST` | `/api/actions/{name}` | Runs a custom action and returns `{ ok, result? }` |
| `GET` | `/api/openapi.json`, `/api/docs/` | OpenAPI specification and Swagger UI |

The server executes actions one at a time. Errors return `{ error, code }`.

## Low-level `AbstractInterface` integration

If you maintain an existing custom interface class or need the low-level `AbstractInterface` and `defineAction` reference, see [Integrate with any interface](./integrate-with-any-interface).
2 changes: 1 addition & 1 deletion apps/site/docs/en/skills.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Control any platform with Skills
# Control with Skills

[Agent Skills](https://github.com/anthropics/skills) are a format for extending AI coding agents with specialized capabilities. Midscene provides Agent Skills that let AI coding tools (like Claude Code, Cline, etc.) drive UI automation through CLI commands.

Expand Down
2 changes: 2 additions & 0 deletions apps/site/docs/zh/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ E2E 测试的初衷是从用户视角验证完整流程,所以 Midscene 按用
<AgentPlatforms hideHeading />
</div>

除了内置平台,还可以通过 [`@midscene/connect`](./midscene-connect) 以 HTTP 协议接入任意界面,例如车机屏、嵌入式面板,以及通过机械臂或 HID 模块驱动的物理设备。真实硬件接入案例见 [midscene-test-everything](https://github.com/web-infra-dev/midscene-test-everything)。

### 模型选择与成本 {#model-options}

<div className="rp-not-doc introduction-block">
Expand Down
6 changes: 6 additions & 0 deletions apps/site/docs/zh/integrate-with-any-interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import SetupEnv from './common/setup-env.mdx';

# 与任意界面集成

:::info 接入自定义设备请优先使用 Midscene Connect
新接入自定义设备推荐使用 [Midscene Connect](./midscene-connect)(`@midscene/connect`):在一个 `.mjs` 文件中描述设备并通过 HTTP 启动服务,即可直接使用 Agent SDK、CLI 或 Playground 驱动,无需编写 `AbstractInterface` 类。

直接实现 `AbstractInterface` 的方式已不再作为接入自定义设备的首选推荐。本页继续保留,供维护已有 `AbstractInterface` 实现以及查阅 `defineAction` 等底层 API 参考。
:::

你可以使用 Midscene 的 Agent 来控制任意界面,比如 IoT 设备、内部应用、车载显示器等,只需要实现一个符合 `AbstractInterface` 定义的 UI 操作类。

在实现了 UI 操作类之后,你可以获得 Midscene Agent 的全部特性:
Expand Down
Loading
Loading