Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

132 KB
Raw Blame
//! MCP stdio 服务。工具名、参数、返回文本与 iphone-control-toolkit 一致(docs/decisions.md D10),
//! 切换时 agent 侧不用改。本 crate 不直接碰设备,全部经守护进程控制接口(Unix socket)。
//!
//! 手写 `ServerHandler`(不用宏):参数 schema 直接写 JSON,省掉 schemars;
//! 参数到请求的转换([`to_request`])是纯函数,可单测。

use std::path::PathBuf;
use std::time::Duration;

use ictl_protocol::{self as protocol, Delivery, FailureStage, MAX_TAP_COUNT, Request, Response};
use rmcp::{
    ErrorData, RoleServer, ServerHandler, ServiceExt,
    model::{
        CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, InitializeResult, ListToolsResult,
        PaginatedRequestParams, ServerCapabilities, Tool, ToolAnnotations,
    },
    service::RequestContext,
};
use serde_json::{Map, Value, json};

pub mod task;

/// 服务说明(发给客户端的 instructions):toolkit 的那段,加上自动解锁范围、多设备的说明
pub fn instructions() -> &'static str {
    ictl_l10n::tr!(
        "控制一台通过 USB 连接的真实 iPhone(或 iPad)。坐标单位是点(pt),与 iphone_screenshot 图像像素一致。\
操作前先用 iphone_screenshot 或 iphone_list_elements 看清屏幕;能按名字点就优先用 iphone_tap_element。\
游戏、地图等界面没有元素树,只能看截图按坐标操作;小图标、小字看不清时用 iphone_screenshot 的区域放大。\
配置了锁屏密码时,截图、读元素和操作类工具会先自动解锁(iphone_status、列 App、列设备、切换设备和开发者工具不会,按 lock 键也不会)。每次操作后再截图确认结果。\
涉及付款、删除、发送消息、修改账号或隐私设置的操作,先向用户确认。\
省时间:操作类工具可带 then=elements 或 then=screenshot,等界面稳定后顺带返回操作后的界面,不用再单独调一次;\
几步确定的连续操作(按键、逐级点菜单、填表)用 iphone_batch 一次发完;等页面加载用 iphone_wait_for,不要反复截图。\
接着多台 iPhone/iPad 时同一时间只控制一台:用 iphone_list_devices 查看,iphone_select_device 切换。",
        "Controls a real iPhone (or iPad) connected over USB. Coordinates are in points (pt), the same as the pixels of an iphone_screenshot image. Before acting, look at the screen with iphone_screenshot or iphone_list_elements; prefer iphone_tap_element whenever the target has text. Games, maps and similar screens have no element tree: work from screenshots and coordinates, and zoom into small icons or text with an iphone_screenshot region. If a passcode is configured, screenshots, reading elements and the action tools unlock the phone first (iphone_status, listing apps or devices, switching devices and the developer tools don't, nor does pressing lock). Check the result with a screenshot after each action. Ask the user before anything that pays, deletes, sends a message, or changes account or privacy settings. Save time: action tools accept then=elements or then=screenshot to return the screen once it has settled, without a separate call; send a few predictable steps in a row (keys, menu levels, forms) as one iphone_batch; wait for a page with iphone_wait_for instead of repeated screenshots. With several iPhones/iPads connected only one is controlled at a time: see them with iphone_list_devices, switch with iphone_select_device."
    )
}

fn num(desc: &str) -> Value {
    json!({"type": "number", "description": desc})
}

fn schema(props: Value, required: &[&str]) -> Map<String, Value> {
    let v = json!({"type": "object", "properties": props, "required": required});
    v.as_object().cloned().unwrap_or_default()
}

/// 操作后顺带返回界面时最多列多少个元素(每步都带太多会占满上下文)
pub const THEN_LIMIT: usize = 80;
/// 顺带返回界面前等界面稳定的上限
pub const THEN_SETTLE_MS: u64 = 1500;
/// 一次批量最多几步
pub const BATCH_MAX_STEPS: usize = 20;
/// reuse_elements 时按名字点击复用多旧的元素树(毫秒)
pub const BATCH_REUSE_MS: u64 = 10_000;
/// 批量里能用的工具:改变界面的操作和等待;看界面用 then
pub const BATCH_TOOLS: [&str; 11] = [
    "iphone_tap",
    "iphone_tap_element",
    "iphone_long_press",
    "iphone_swipe",
    "iphone_type_text",
    "iphone_press_button",
    "iphone_launch_app",
    "iphone_terminate_app",
    "iphone_open_url",
    "iphone_wait_for",
    "wait",
];

/// 操作类工具共有的可选参数:操作后顺带返回界面
fn then_props() -> Value {
    json!({
        "then": {"type": "string", "enum": ["none", "elements", "screenshot"], "default": "none",
                 "description": ictl_l10n::tr!("操作后等界面稳定,再顺带返回界面:elements=元素列表(最多 80 个),screenshot=截图。省一次单独的调用。操作前看过界面(截图、读元素)时,还会说明和操作前相比界面变没变;没变说明操作可能没生效", "After the action, wait for the screen to settle and return it: elements = element list (up to 80), screenshot = screenshot. Saves a separate call. If the screen was looked at before the action (screenshot, element list), it also says whether the screen changed since; no change means the action may not have taken effect")},
        "then_filter": {"type": "string", "default": "", "description": ictl_l10n::tr!("then=elements 时只列包含这段文字的元素", "With then=elements: only list elements containing this text")},
        "then_region": {"type": "array", "items": {"type": "number"}, "minItems": 4, "maxItems": 4,
                        "description": ictl_l10n::tr!("then=screenshot 时只返回这块区域的放大图 [x, y, width, height](点),例如游戏棋盘", "With then=screenshot: only return this region, enlarged, as [x, y, width, height] (points), e.g. a game board")},
        "then_settle": {"type": "number", "default": 1.5,
                        "description": ictl_l10n::tr!("顺带返回界面前最多等几秒让界面稳定(不超过 10)。游戏、地图按画面是否静止判断,消除连锁等长动画可调大,画面一静止就返回", "Maximum seconds to wait for the screen to settle before returning it (up to 10). Games and maps are judged by whether the picture is still; raise it for long animations such as chain reactions, it returns as soon as the picture is still")}
    })
}

/// 在参数 schema 里加上 then / then_filter
fn with_then(props: Value) -> Value {
    let mut p = props;
    if let (Some(o), Value::Object(extra)) = (p.as_object_mut(), then_props()) {
        o.extend(extra);
    }
    p
}

/// 操作类工具描述的共同结尾:返回什么、then 能做什么、自动解锁。
/// `alert`:不带 then 时会不会查系统弹窗(见 [`wants_alert_check`])
fn action_desc(body: &str, example: &str, alert: bool) -> String {
    let mut s = ictl_l10n::trf!(
        "{body}\n\n返回:一行结果文字,例如「{example}」。",
        "{body}\n\nReturns: one line of result text, e.g. “{example}”."
    );
    if alert {
        s += ictl_l10n::tr!(
            "不带 then 时,如果系统弹窗挡着屏幕,结果里会提醒(这时操作落在了弹窗上)。",
            " Without then, the result warns if a system alert covers the screen (the action then landed on the alert)."
        );
    }
    s += ictl_l10n::tr!(
        "传 then=elements 或 then=screenshot 会等界面稳定后附上操作后的元素列表或截图,省一次单独的调用。\
配置了锁屏密码时会先自动解锁。",
        " With then=elements or then=screenshot it waits for the screen to settle and attaches the element list or screenshot after the action, saving a separate call. If a passcode is configured, the phone is unlocked first."
    );
    s
}

/// 只看不改的工具
fn looking(title: &str) -> ToolAnnotations {
    ToolAnnotations::with_title(title).read_only(true).open_world(false)
}

/// 改变手机状态的工具。open_world:点击、输入可能经 App 触达外部(发消息、下单),所以都标 true
fn acting(title: &str, destructive: bool, idempotent: bool) -> ToolAnnotations {
    ToolAnnotations::with_title(title).read_only(false).destructive(destructive).idempotent(idempotent).open_world(true)
}

/// Names of the developer tools, listed only with [`dev_tools_enabled`]
pub const DEV_TOOLS: [&str; 6] = [
    "iphone_app_logs",
    "iphone_crash_reports",
    "iphone_install_app",
    "iphone_uninstall_app",
    "iphone_web_targets",
    "iphone_web_eval",
];

/// `IPHONE_CONTROL_DEV_TOOLS=1` (or true/yes/on) adds the developer tools; off by default, so agents that don't
/// need them don't pay for their descriptions
pub fn dev_tools_enabled() -> bool {
    std::env::var(ictl_protocol::env::DEV_TOOLS)
        .is_ok_and(|v| matches!(v.trim().to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on"))
}

/// The tools this server lists: the 18 standard ones, plus the developer tools when switched on
pub fn tools() -> Vec<Tool> {
    tools_with(dev_tools_enabled())
}

pub fn tools_with(dev: bool) -> Vec<Tool> {
    let mut v = standard_tools();
    if dev {
        v.extend(dev_tools());
    }
    v
}

/// [`task::CONFIRM_TOOL`], listed only while working for a task (see [`task`])
pub fn confirm_tool() -> Tool {
    Tool::new(
        task::CONFIRM_TOOL,
        ictl_l10n::tr!(
            "请用户确认一步操作(在控制台页面或发起任务的地方),一直等到用户回答。\n\n何时用:付款、购买、下单、转账;删除或清空数据;发送消息、邮件、评论、帖子;\
修改账号、密码、隐私或安全设置;其他不可撤销的操作。在真正点下那一步之前调用。\n\n\
返回:用户同意时是普通结果,接着执行;用户拒绝时是错误结果,这时不要执行这一步,也不要换个方式绕过,停下来说明情况。",
            "Ask the user to confirm one action (on the control page or wherever the task was started), and wait for the answer.\n\nWhen to use: payments, purchases, orders, transfers; deleting or clearing data; sending messages, emails, comments, posts; changing account, password, privacy or security settings; anything else that can't be undone. Call it right before the step that does it.\n\nReturns: a normal result when the user approves, then go ahead; an error when the user rejects, then don't do that step or work around it: stop and explain."
        )
        .to_owned(),
        schema(
            json!({
                "action": {"type": "string", "description": ictl_l10n::tr!("要做的事,一句话,例如「发送消息」「删除照片」", "what is about to happen, in a few words, e.g. “Send message”, “Delete photo”")},
                "detail": {"type": "string", "description": ictl_l10n::tr!("具体内容:发给谁、发什么、删哪几项、付多少钱", "the specifics: to whom, what, which items, how much")}
            }),
            &["action", "detail"],
        ),
    )
    .with_annotations(ToolAnnotations::with_title(ictl_l10n::tr!("请用户确认", "Ask the user")).read_only(true).open_world(false))
}

fn dev_tools() -> Vec<Tool> {
    let t = |name: &'static str, desc: &str, s: Map<String, Value>, a: ToolAnnotations| {
        Tool::new(name, desc.to_owned(), s).with_annotations(a)
    };
    vec![
        t(
            "iphone_app_logs",
            ictl_l10n::tr!(
                "读手机的系统日志(os_log,默认、Error、Fault 级别;Debug、Info 不记录,记录它们会拖慢手机),可按 App 或进程、关键词、时间筛选,每行:多少秒前、进程[pid]、级别、子系统、内容。\n\n\
何时用:App 崩溃、报错、请求失败、界面没反应时,不靠截图找原因。第一次调用才开始记录(之前的拿不到):先调一次,复现问题,再调一次。\
记录在 15 分钟没有调用后自动停止。只读。",
                "Read the phone's system log (os_log, default, Error and Fault levels; Debug and Info aren't recorded, since streaming them slows the phone down), filtered by app or process, keyword and time; one line per entry: seconds ago, process[pid], level, subsystem, message.\n\nWhen to use: when an app crashes, shows an error, a request fails or the UI doesn't respond, to find the cause without screenshots. Recording starts with the first call (earlier lines aren't available): call it once, reproduce the problem, then call it again. Recording stops by itself after 15 minutes without a call. Read-only."
            ),
            schema(
                json!({
                    "process": {"type": "string", "default": "", "description": ictl_l10n::tr!("App 的显示名或 Bundle ID,或进程名(如 SpringBoard);空为全部", "app display name or bundle ID, or a process name (e.g. SpringBoard); empty for all")},
                    "grep": {"type": "string", "default": "", "description": ictl_l10n::tr!("只要内容或子系统包含这段文字的行(不分大小写)", "only lines whose message or subsystem contains this text (case-insensitive)")},
                    "since": {"type": "number", "default": 60, "description": ictl_l10n::tr!("只看最近多少秒,最多 3600", "only the last this many seconds, at most 3600")},
                    "limit": {"type": "integer", "default": 100, "description": ictl_l10n::tr!("最多返回多少行(最新的),1 到 1000", "at most this many lines (the newest), 1 to 1000")},
                    "errors_only": {"type": "boolean", "default": false, "description": ictl_l10n::tr!("只看 Error 和 Fault 级别", "only Error and Fault levels")}
                }),
                &[],
            ),
            looking(ictl_l10n::tr!("读日志", "Read logs")),
        ),
        t(
            "iphone_crash_reports",
            ictl_l10n::tr!(
                "列出手机上的崩溃报告(新的在前,最多 50 个),或用 name 读取其中一份(.ips,开头是异常类型、崩溃的线程和 App 版本)。\n\n\
何时用:App 闪退后找原因。只读。",
                "List the crash reports on the phone (newest first, at most 50), or read one with name (.ips; the start has the exception type, the crashed thread and the app version).\n\nWhen to use: to find out why an app quit unexpectedly. Read-only."
            ),
            schema(
                json!({
                    "filter": {"type": "string", "default": "", "description": ictl_l10n::tr!("只列文件名包含这段文字的(如进程名)", "only list file names containing this text (e.g. the process name)")},
                    "name": {"type": "string", "default": "", "description": ictl_l10n::tr!("要读取的报告文件名(来自列表);给了就返回内容", "file name of the report to read (from the list); if given, returns its content")}
                }),
                &[],
            ),
            looking(ictl_l10n::tr!("崩溃报告", "Crash reports")),
        ),
        t(
            "iphone_install_app",
            ictl_l10n::tr!(
                "安装签好名的 .ipa 或 .app(覆盖已装的同一 App)。path 是运行守护进程的机器上的路径。\
大的包要一两分钟。",
                "Install a signed .ipa or .app (replacing the same app if installed). path is on the machine that runs the daemon. Large packages take a minute or two."
            ),
            schema(
                json!({"path": {"type": "string", "description": ictl_l10n::tr!(".ipa 文件或 .app 目录的路径", "path of the .ipa file or .app directory")}}),
                &["path"],
            ),
            acting(ictl_l10n::tr!("安装 App", "Install app"), true, true),
        ),
        t(
            "iphone_uninstall_app",
            ictl_l10n::tr!(
                "卸载 App,连同它的数据。不会卸载 WebDriverAgent(iphone-control 靠它控制手机)。卸载前先问用户。",
                "Uninstall an app, together with its data. Refuses to remove WebDriverAgent, which iphone-control needs to control the phone. Ask the user before uninstalling."
            ),
            schema(
                json!({"app": {"type": "string", "description": ictl_l10n::tr!("Bundle ID 或显示名", "bundle ID or display name")}}),
                &["app"],
            ),
            acting(ictl_l10n::tr!("卸载 App", "Uninstall app"), true, true),
        ),
        t(
            "iphone_web_targets",
            ictl_l10n::tr!(
                "列出可检查的网页和 WebView(Web Inspector):Safari 标签页、App 里可检查的 WebView(Capacitor、Cordova、React Native 等的调试构建)。每行:id、类型、所属 App、标题和网址。\n\n\
何时用:页面内容在网页里时,用 iphone_web_eval 直接读 DOM,比截图和元素树快、省 token、也更准。需要手机上 设置 → Safari → 高级 → 网页检查器 打开。只读。",
                "List inspectable web pages and WebViews (Web Inspector): Safari tabs and inspectable WebViews in apps (debug builds of Capacitor, Cordova, React Native and similar). One line each: id, type, app, title and URL.\n\nWhen to use: when the content is a web page, read the DOM with iphone_web_eval instead; that is faster, cheaper in tokens and more accurate than screenshots or the element tree. Needs Settings → Safari → Advanced → Web Inspector on. Read-only."
            ),
            schema(json!({}), &[]),
            looking(ictl_l10n::tr!("列出网页", "List web pages")),
        ),
        t(
            "iphone_web_eval",
            ictl_l10n::tr!(
                "在网页或 WebView 里执行 JavaScript,返回结果的 JSON(Promise 会等它完成)。例如 document.title、\
[...document.querySelectorAll('button')].map(b => b.innerText)、document.querySelector('#email').value = '[email protected]'。\n\n\
何时用:读网页内容、表单状态、控制台变量,或直接操作页面。只取需要的部分,结果超过 5 万字会被截断。\
脚本能改页面(提交表单、发请求),和点击一样要小心。",
                "Run JavaScript in a web page or WebView and return the result as JSON (a promise is awaited). For example document.title, [...document.querySelectorAll('button')].map(b => b.innerText), document.querySelector('#email').value = '[email protected]'.\n\nWhen to use: to read page content, form state or JavaScript variables, or to act on the page directly. Select just what you need; results over 50,000 characters are cut. A script can change the page (submit forms, send requests), so take the same care as with taps."
            ),
            schema(
                json!({
                    "expression": {"type": "string", "description": ictl_l10n::tr!("要执行的 JavaScript 表达式", "the JavaScript expression to run")},
                    "target": {"type": "string", "default": "", "description": ictl_l10n::tr!("iphone_web_targets 列出的 id,或网址、标题里的一段文字;只有一个页面时可以不填", "an id from iphone_web_targets, or text in the page's URL or title; may be left out when there is only one page")},
                    "timeout": {"type": "number", "default": 10, "description": ictl_l10n::tr!("最多等多少秒,最多 60", "maximum seconds to wait, at most 60")}
                }),
                &["expression"],
            ),
            acting(ictl_l10n::tr!("执行 JavaScript", "Run JavaScript"), true, false),
        ),
    ]
}

/// 18 个工具的定义(toolkit 的 13 个 + iphone_wait_for、iphone_batch、iphone_list_devices、iphone_select_device、iphone_scroll_collect)。
/// 描述可以改写,但工具名、参数、默认返回不改(docs/decisions.md D10)
fn standard_tools() -> Vec<Tool> {
    let t = |name: &'static str, desc: String, s: Map<String, Value>, a: ToolAnnotations| {
        Tool::new(name, desc, s).with_annotations(a)
    };
    let app = || {
        json!({"app": {"type": "string",
            "description": ictl_l10n::tr!("Bundle ID(如 com.apple.Preferences)或显示名(如「设置」「微信」)", "Bundle ID (e.g. com.apple.Preferences) or display name (e.g. “Settings”)")}})
    };
    vec![
        t(
            "iphone_status",
            ictl_l10n::tr!("查看手机和服务的状态:WDA(WebDriverAgent)是否就绪、是否锁屏、能否自动解锁、当前前台 App 的 Bundle ID、\
屏幕尺寸(点)、守护进程状态。\n\n何时用:开始操作前确认手机可用;操作报错、怀疑 WDA 掉线或手机锁屏时排查。\
WDA 不可用时会说明哪些功能受影响(读元素、按名字点击、启动 App)。只读,不改变手机状态。", "Show the state of the phone and the service: whether WDA (WebDriverAgent) is ready, whether the phone is locked and can be auto-unlocked, the bundle ID of the foreground app, the screen size (points) and the daemon state.\n\nWhen to use: to check that the phone is usable before starting; to investigate when actions fail or WDA or the lock screen seem to be in the way. If WDA is unavailable it says which features are affected (reading elements, tapping by text, launching apps). Read-only, doesn't change the phone.")
                .into(),
            schema(json!({}), &[]),
            looking(ictl_l10n::tr!("查看状态", "Show status")),
        ),
        t(
            "iphone_screenshot",
            ictl_l10n::tr!("截取当前屏幕,返回 JPEG 图像和一行说明。图像已缩放到点尺寸,图中像素坐标可直接用于 iphone_tap、iphone_swipe。\n\n\
看不清小图标、小字时,传 x/y/width/height(点,四个要一起给)放大这块区域:从原始高清截图裁剪,小区域再额外放大。\
返回文字里有放大倍数,图中坐标换算回屏幕点:x + 像素/倍数。\n\n\
何时用:游戏、地图、图片等没有元素树的界面,或要确认画面效果时。只想知道有哪些按钮、文字,iphone_list_elements 更快更省上下文。\
操作后要看结果时,优先给操作工具带 then=screenshot。WDA 不可用时改用 CoreDevice 截图(iOS 27 起,说明里注明「HID」)。\
只读:不改变界面,但配置了锁屏密码时会先解锁。", "Capture the current screen and return a JPEG image plus a line of explanation. The image is scaled to points, so its pixel coordinates can be used directly with iphone_tap and iphone_swipe.\n\nTo read small icons or text, pass x/y/width/height (points, all four together) to zoom into that region: it is cropped from the full-resolution capture, and small regions are enlarged further. The text gives the zoom factor; convert image coordinates back to screen points with x + pixel / factor.\n\nWhen to use: screens without an element tree (games, maps, images), or to check how something looks. To find out which buttons and texts are there, iphone_list_elements is faster and uses less context. To see the result of an action, prefer then=screenshot on the action tool. If WDA is unavailable it uses a CoreDevice screenshot instead (iOS 27+, marked “HID”). Read-only: doesn't change the screen, but unlocks the phone first if a passcode is configured.")
                .into(),
            schema(
                json!({"x": num(ictl_l10n::tr!("区域左上角 x(点)", "left edge x of the region (points)")), "y": num(ictl_l10n::tr!("区域左上角 y(点)", "top edge y of the region (points)")), "width": num(ictl_l10n::tr!("区域宽(点)", "region width (points)")), "height": num(ictl_l10n::tr!("区域高(点)", "region height (points)"))}),
                &[],
            ),
            looking(ictl_l10n::tr!("截图", "Screenshot")),
        ),
        t(
            "iphone_list_elements",
            ictl_l10n::tr!("列出屏幕上带文字的界面元素,每行一个:类型、标签、名字、值、中心点坐标、尺寸(点)。\n\n\
何时用:找按钮、输入框、列表项的文字和位置,比截图快、省上下文;找到后用 iphone_tap_element 按文字点。\
游戏、地图等没有元素树的界面这里是空的,改用 iphone_screenshot。\n\n\
注意:按坐标判断是否在屏幕内,被弹窗遮住的元素也可能列出来,不确定时截图核对。需要 WDA。\
只读:不改变界面,但配置了锁屏密码时会先解锁。", "List the UI elements on screen that carry text, one per line: type, label, name, value, center point and size (points).\n\nWhen to use: to find buttons, text fields and list items with their text and position; faster than a screenshot and uses less context. Then tap one by its text with iphone_tap_element. Games, maps and other screens without an element tree return nothing here; use iphone_screenshot.\n\nNote: visibility is judged by position, so elements hidden behind an alert can still be listed; check with a screenshot when in doubt. Needs WDA. Read-only: doesn't change the screen, but unlocks the phone first if a passcode is configured.")
                .into(),
            schema(
                json!({
                    "filter": {"type": "string", "default": "",
                        "description": ictl_l10n::tr!("只返回类型/标签/名字/值里包含这段文字的元素(不区分大小写),例如 SearchField、Button、登录", "Only return elements whose type/label/name/value contains this text (case-insensitive), e.g. SearchField, Button, Login")},
                    "limit": {"type": "integer", "default": 150, "description": ictl_l10n::tr!("最多列几个,结构化结果包含匹配总数和截断标记", "Maximum number of elements to return; structured output reports the count and whether it was truncated")}
                }),
                &[],
            ),
            looking(ictl_l10n::tr!("列出界面元素", "List UI elements")),
        )
        .with_raw_output_schema(std::sync::Arc::new(elements_output_schema())),
        t(
            "iphone_scroll_collect",
            ictl_l10n::tr!("收集连续几屏里可见的文字行,合并相邻屏的重叠内容。可用 until 找到文字就停在当前屏。只滑动,不点击。返回 rows、scrolls、stop_reason,以及找到时当前屏上的 matched。",
                "Collect visible text rows across overlapping viewports. Use until to stop with a matching element on screen. Only scrolls, never taps. Returns rows, scrolls, stop_reason and the matched element on the final viewport.").into(),
            schema(json!({
                "scroll": {"type": "string", "enum": ["down", "up"], "default": "down", "description": ictl_l10n::tr!("往下或往上找", "look further down or up the list")},
                "until": {"type": "string", "description": ictl_l10n::tr!("可见元素的文字、标识或值包含这段文字时停止,不区分大小写", "stop at a visible label, name or value containing this text, case-insensitive")},
                "filter": {"type": "string", "description": ictl_l10n::tr!("只收集类型或文字包含它的行。与 until 独立", "collect rows whose type or text contains this string, independent of until")},
                "limit": {"type": "integer", "minimum": 1, "maximum": protocol::MAX_SCROLL_ROWS, "default": protocol::MAX_SCROLL_ROWS, "description": ictl_l10n::tr!("最多收集几行", "stop after collecting this many rows")},
                "max_scrolls": {"type": "integer", "minimum": 1, "maximum": protocol::MAX_SEARCH_SCROLLS, "default": 8, "description": ictl_l10n::tr!("最多滚动几次", "maximum number of scrolls")},
                "x": {"type": "number", "minimum": 0, "description": ictl_l10n::tr!("在哪个横坐标上下滑(点),默认屏幕正中。iPad 分栏界面(设置、邮件)只滚动手指所在的那一栏:要滚左栏,给左栏里的横坐标", "where to swipe across, in points from the left; the middle of the screen by default. An iPad split view (Settings, Mail) scrolls only the column swiped in: for the sidebar, an x inside it")}
            }), &[]),
            acting(ictl_l10n::tr!("滚动收集文字", "Scroll and collect text"), false, false),
        ),
        t(
            "iphone_tap",
            action_desc(
                ictl_l10n::tr!("点击屏幕上的一个坐标(单位是点,与 iphone_screenshot 全屏图的像素一致,左上角为原点)。\n\n\
何时用:目标没有文字(图标、图片)、在游戏或地图里,或 iphone_list_elements 里找不到它时。\
带文字的按钮、列表项优先用 iphone_tap_element,界面布局变了也点得准。按住用 iphone_long_press,滚动用 iphone_swipe。\n\n\
行为:单指按下约 0.05 秒后抬起,点一次(count 大于 1 时在同一点连点)。不检查坐标上是什么,点之前先截图确认位置。\
WDA 不可用时改用 CoreDevice HID(iOS 27 起;结果里注明「经 HID」)。", "Tap a point on the screen (in points, the same as the pixels of a full-screen iphone_screenshot, origin at the top left).\n\nWhen to use: the target has no text (icon, image), is in a game or map, or can't be found with iphone_list_elements. For buttons and list items with text prefer iphone_tap_element, which keeps working when the layout changes. Use iphone_long_press to hold and iphone_swipe to scroll.\n\nBehavior: one finger down for about 0.05 s, then up; a single tap (several at the same point with count above 1). It doesn't check what is at that point, so look at a screenshot first. If WDA is unavailable it uses CoreDevice HID instead (iOS 27+; the result says “via HID”)."),
                ictl_l10n::tr!("已点击 (120,300)", "Tapped (120,300)"),
                true,
            ),
            schema(
                with_then(json!({
                    "x": num(ictl_l10n::tr!("x(点)", "x (points)")),
                    "y": num(ictl_l10n::tr!("y(点)", "y (points)")),
                    "count": {"type": "integer", "default": 1, "minimum": 1, "maximum": MAX_TAP_COUNT,
                        "description": ictl_l10n::tr!("在这一点连点几下,一个手势里点完(每下间隔约 0.1 秒):2 是双击;也用于要在短时间内连点几下的入口。分开调用赶不上这类时间窗", "how many times to tap this point, all in one gesture (about 0.1 s apart): 2 is a double tap; also for entries that need several taps in a short time, which separate calls are too slow for")}
                })),
                &["x", "y"],
            ),
            acting(ictl_l10n::tr!("点击坐标", "Tap point"), true, false),
        ),
        t(
            "iphone_tap_element",
            action_desc(
                ictl_l10n::tr!("按文字点击界面元素:先找标签/名字与 text 完全相同的,找不到再找包含 text 的。\
中心被悬浮栏、键盘等盖住时改点没被盖住的部分,整个被盖住就不点。\n\n\
何时用:按钮、列表项、标签页等带文字的元素,优先用它而不是按坐标点。不确定文字怎么写时先 iphone_list_elements。\
没有文字的图标、游戏、地图用 iphone_tap。\n\n\
行为:没找到、index 超出匹配个数或整个被盖住时不点,返回原因(不算出错;在 iphone_batch 里会让批量停下)。需要 WDA。", "Tap a UI element by its text: first an exact match of label or name with text, otherwise one that contains text. If its center is covered by a floating bar, the keyboard or similar, it taps an uncovered part; if it is completely covered, it doesn't tap.\n\nWhen to use: buttons, list items, tabs and other elements with text; prefer this over coordinates. If unsure how the text is written, use iphone_list_elements first. For icons without text, games and maps use iphone_tap.\n\nBehavior: when nothing matches, index is beyond the number of matches, or the element is completely covered, it doesn't tap and says why (not an error; inside iphone_batch it stops the batch). Needs WDA."),
                ictl_l10n::tr!("已点击 Button \"通用\"", "Tapped Button \"General\""),
                true,
            ),
            schema(
                with_then(json!({
                    "text": {"type": "string", "description": ictl_l10n::tr!("元素的文字(标签或名字),先精确匹配再按包含匹配", "the element's text (label or name); exact match first, then substring")},
                    "index": {"type": "integer", "default": 0,
                        "description": ictl_l10n::tr!("匹配到多个时点第几个(从 0 开始,顺序同 iphone_list_elements)", "which match to tap if there are several (from 0, same order as iphone_list_elements)")},
                    "scroll": {"type": "string", "enum": ["down", "up"],
                        "description": ictl_l10n::tr!("当前屏幕上没有时边滑边找:down 往下找(看下面的内容),up 往上找。每次慢滑约三分之一屏、不带惯性,找到文字完全相同的就点(当前屏只有包含它的不算,接着滑;滑完都没有完全相同的才点包含它的),滑到底(界面不再变)或滑满 max_scrolls 次还没有就停下说明。用于长列表、设置页里不在当前屏的项;不给就只在当前屏幕上找", "If it isn't on the current screen, scroll and look again: down looks further down (the content below), up further up. Each scroll is a slow third of the screen without momentum; it taps as soon as the exact text is found (a row that only contains it doesn't stop the search; if no exact match turns up, one that contains it is tapped), and stops and says so at the end of the list (the screen no longer changes) or after max_scrolls. For items of long lists and settings pages that are off screen; without it only the current screen is searched")},
                    "max_scrolls": {"type": "integer", "default": 8, "minimum": 1, "maximum": ictl_protocol::MAX_SEARCH_SCROLLS,
                        "description": ictl_l10n::tr!("带 scroll 时最多滑几次,默认 8", "with scroll: at most this many scrolls, default 8")}
                })),
                &["text"],
            ),
            acting(ictl_l10n::tr!("按文字点击", "Tap by text"), true, false),
        ),
        t(
            "iphone_long_press",
            action_desc(
                ictl_l10n::tr!("在屏幕坐标(点)上单指按住 seconds 秒再抬起,手指不移动。\n\n\
何时用:要长按才出现的操作,例如主屏图标的快捷菜单、消息或图片的上下文菜单、选中文字。\
普通点击用 iphone_tap 或 iphone_tap_element;要移动手指(滚动、拖滑块)用 iphone_swipe。\n\n\
行为:只按一次。按得太短(不到约 0.5 秒)系统可能当成普通点击。长按弹出的菜单不会自己消失,\
下一步先看屏幕(或直接带 then=screenshot)。需要 WDA。", "Hold one finger still at a screen point (points) for seconds, then lift.\n\nWhen to use: actions that need a long press, such as home-screen quick actions, context menus on messages or images, or selecting text. For normal taps use iphone_tap or iphone_tap_element; to move the finger (scroll, drag a slider) use iphone_swipe.\n\nBehavior: a single press. Too short a press (under about 0.5 s) may count as a normal tap. Menus opened by a long press stay open, so look at the screen next (or pass then=screenshot). Needs WDA."),
                ictl_l10n::tr!("已长按 (200,400) 1 秒", "Long-pressed (200,400) for 1 s"),
                true,
            ),
            schema(
                with_then(json!({
                    "x": num(ictl_l10n::tr!("x(点)", "x (points)")),
                    "y": num(ictl_l10n::tr!("y(点)", "y (points)")),
                    "seconds": {"type": "number", "default": 1.0, "description": ictl_l10n::tr!("按住几秒,默认 1", "seconds to hold, default 1")}
                })),
                &["x", "y"],
            ),
            acting(ictl_l10n::tr!("长按", "Long press"), true, false),
        ),
        t(
            "iphone_swipe",
            action_desc(
                ictl_l10n::tr!("单指从 (from_x, from_y) 按下,用 seconds 秒移到 (to_x, to_y) 后抬起(坐标单位是点)。\n\n\
何时用:滚动列表或页面、左右翻页、拖动滑块。向上滑看下面的内容:from_y 大、to_y 小(例如 y 从 700 滑到 300);\
向左滑看右边的内容:from_x 大、to_x 小。点击用 iphone_tap,原地按住用 iphone_long_press。\n\n\
行为:seconds 越小滑得越快,快滑会带惯性多滚一段;要精确移动(滑块)把 seconds 调到 1 秒左右。\
滑完会等列表停止滚动(最多 2.5 秒)再返回,还在滚就在结果里说明。WDA 不可用时改用 CoreDevice HID(iOS 27 起)。", "One finger down at (from_x, from_y), moving to (to_x, to_y) over seconds, then up (coordinates in points).\n\nWhen to use: scroll a list or page, page left or right, drag a slider. To see content further down, swipe up: from_y large, to_y small (e.g. y from 700 to 300); to see content to the right, swipe left: from_x large, to_x small. Use iphone_tap to tap and iphone_long_press to hold in place.\n\nBehavior: the smaller seconds, the faster the swipe; a fast swipe keeps scrolling with momentum. For precise moves (sliders) use about 1 second. It waits until the list stops scrolling (up to 2.5 s) before returning and says so if it is still moving. If WDA is unavailable it uses CoreDevice HID instead (iOS 27+)."),
                ictl_l10n::tr!("已滑动 (215,700) → (215,300)", "Swiped (215,700) → (215,300)"),
                true,
            ),
            schema(
                with_then(json!({
                    "from_x": num(ictl_l10n::tr!("起点 x(点)", "start x (points)")),
                    "from_y": num(ictl_l10n::tr!("起点 y(点)", "start y (points)")),
                    "to_x": num(ictl_l10n::tr!("终点 x(点)", "end x (points)")),
                    "to_y": num(ictl_l10n::tr!("终点 y(点)", "end y (points)")),
                    "seconds": {"type": "number", "default": 0.3, "description": ictl_l10n::tr!("滑动用时(秒),默认 0.3;越小越快、惯性越大", "swipe duration (seconds), default 0.3; smaller is faster with more momentum")},
                    "hold": {"type": "number", "default": 0, "minimum": 0, "description": ictl_l10n::tr!("先在起点按住几秒再移动,默认 0(普通滑动)。拖动要先长按才拿得起来的东西时用,例如主屏幕图标、可排序的列表项、文件:hold 0.6~1,seconds 0.7 左右。少于 0.5 秒通常拿不起来,只会滑动或翻页", "seconds to hold still at the start before moving, default 0 (a plain swipe). For dragging things that have to be long-pressed before they can be picked up, such as home-screen icons, reorderable list rows or files: hold 0.6-1, seconds about 0.7. Under 0.5 s usually doesn't pick anything up and just swipes or pages")}
                })),
                &["from_x", "from_y", "to_x", "to_y"],
            ),
            acting(ictl_l10n::tr!("滑动", "Swipe"), false, false),
        ),
        t(
            "iphone_type_text",
            action_desc(
                ictl_l10n::tr!("在当前获得焦点的输入框里输入文字(支持中文),默认从光标处输入。replace=true 替换当前聚焦的普通输入框的全文(text 为空时清空),读回全文一致才按回车。submit=true 时末尾按回车。\n\n\
何时用:搜索框、表单、聊天框等。要先点中输入框让键盘弹出;屏幕上没有键盘时报错,什么都不输。\
回车可能会发送消息或提交表单,发送前先向用户确认。\n\n\
行为:输完会读回输入框核对,少了结尾几个字会自动补上;对不上就报错,也不按回车。\
WDA 不可用时改用 HID(iOS 27 起):美式键盘上的英文和符号直接按键;中文等经剪贴板粘贴,手机会弹「允许粘贴」,要截图后点它才粘得进去(剪贴板 45 秒后还原)。", "Type text into the focused text field (any language, including Chinese) at the cursor by default. replace=true replaces the contents of a readable focused field (empty text empties it), and presses Return only once the field reads back as exactly the text. With submit=true it presses Return at the end.\n\nWhen to use: search boxes, forms, chat fields. Tap the field first so the keyboard comes up; with no keyboard on screen it fails and types nothing. Return may send a message or submit a form, so ask the user before sending.\n\nBehavior: it reads the field back afterwards; dropped trailing characters are typed again. If it still doesn't match, it reports an error and doesn't press Return. If WDA is unavailable it uses HID instead (iOS 27+): English letters and symbols of a US keyboard are typed as keys, anything else is pasted through the pasteboard, and the phone asks to “Allow Paste”: take a screenshot and tap it to let the text in (the pasteboard is put back after 45 s)."),
                ictl_l10n::tr!("已输入 4 个字符,并按了回车", "Typed 4 characters and pressed Return"),
                true,
            ),
            schema(
                with_then(json!({
                    "text": {"type": "string", "description": ictl_l10n::tr!("要输入的文字", "the text to type")},
                    "replace": {"type": "boolean", "default": false, "description": ictl_l10n::tr!("替换当前聚焦输入框的全文,text 为空时清空输入框。仅普通可读输入框(不含密码框);经 WDA 时先清空再输入,失败时原来的文字已删掉;默认在光标处输入", "replace all text in the focused field; with empty text, empty it. Readable fields only (not password fields); through WDA the field is emptied first, so after a failure the old text is gone; defaults to typing at the cursor")},
                    "submit": {"type": "boolean", "default": false, "description": ictl_l10n::tr!("输完后按回车(搜索、发送、提交)", "press Return afterwards (search, send, submit)")}
                })),
                &["text"],
            ),
            acting(ictl_l10n::tr!("输入文字", "Type text"), false, false),
        ),
        t(
            "iphone_press_button",
            action_desc(
                ictl_l10n::tr!("按硬件键:home(回主屏幕)、volume_up、volume_down、lock(锁屏)。\n\n\
何时用:离开当前 App 回主屏幕用 home(App 留在后台,要彻底关掉用 iphone_terminate_app);调音量;锁屏。\
锁屏后配置了密码的话,下一个操作会先自动解锁。\n\n\
行为:系统弹窗挡着时按 home 不会生效,这时不按,并提示先点弹窗上的按钮。按 home 会等主屏真的到前台再返回。", "Press a hardware button: home (go to the home screen), volume_up, volume_down, lock (lock the screen).\n\nWhen to use: leave the current app with home (the app stays in the background; use iphone_terminate_app to quit it); change the volume; lock the phone. After locking, if a passcode is configured, the next action unlocks first.\n\nBehavior: while a system alert covers the screen home has no effect, so it isn't pressed and the result says to tap a button on the alert first. home waits until the home screen is really in front before returning."),
                ictl_l10n::tr!("已按 home", "Pressed home"),
                false,
            ),
            schema(
                with_then(json!({"button": {"type": "string", "enum": ["home", "volume_up", "volume_down", "lock"],
                    "description": ictl_l10n::tr!("home、volume_up、volume_down 或 lock", "home, volume_up, volume_down or lock")}})),
                &["button"],
            ),
            acting(ictl_l10n::tr!("按硬件键", "Press button"), false, false),
        ),
        t(
            "iphone_list_apps",
            ictl_l10n::tr!("列出已安装的 App,每行一个:显示名和 Bundle ID。\n\n\
何时用:iphone_launch_app、iphone_terminate_app 报「找不到名为…的 App」时,查准确的名字或 Bundle ID。\
默认只列用户装的 App;设置、相机等系统 App 要 include_system=true(要多花几秒)。结果有缓存。只读。", "List installed apps, one per line: display name and bundle ID.\n\nWhen to use: when iphone_launch_app or iphone_terminate_app report “No app named …”, to look up the exact name or bundle ID. By default only user-installed apps; system apps such as Settings or Camera need include_system=true (takes a few seconds longer). Results are cached. Read-only.")
                .into(),
            schema(
                json!({"include_system": {"type": "boolean", "default": false, "description": ictl_l10n::tr!("同时列出系统 App", "also list system apps")}}),
                &[],
            ),
            looking(ictl_l10n::tr!("列出 App", "List apps")),
        ),
        t(
            "iphone_launch_app",
            action_desc(
                ictl_l10n::tr!("启动 App 并切到前台。app 是显示名时按已安装的 App 查找,找不到会报错,提示用 iphone_list_apps 查看。\n\n\
何时用:打开某个 App。要直接进网页或 App 里的某个页面用 iphone_open_url。\
想从首页重新开始(而不是回到上次的页面),先用 iphone_terminate_app 结束它再启动。\n\n\
行为:App 已在后台运行时只切回前台,停在上次的页面;没在运行就冷启动。等 App 真的到前台才返回(最多约 8 秒);\
被系统弹窗(如权限请求)挡住或启动慢时,结果里会说明。需要 WDA。", "Launch an app and bring it to the foreground. A display name is looked up among the installed apps; if not found it fails and suggests iphone_list_apps.\n\nWhen to use: to open an app. To go straight to a web page or a screen inside an app, use iphone_open_url. To start from the app's first screen (instead of where it was), quit it with iphone_terminate_app first.\n\nBehavior: an app already running in the background is only brought back to the front, on the screen it was on; otherwise it cold-starts. It returns once the app is really in front (up to about 8 s); if a system alert (such as a permission prompt) blocks it or it starts slowly, the result says so. Needs WDA."),
                ictl_l10n::tr!("已启动 com.apple.Preferences", "Launched com.apple.Preferences"),
                true,
            ),
            schema(with_then(app()), &["app"]),
            acting(ictl_l10n::tr!("启动 App", "Launch app"), false, true),
        ),
        t(
            "iphone_terminate_app",
            action_desc(
                ictl_l10n::tr!("强制结束 App 进程,相当于在多任务界面里把它划掉。app 是显示名时按已安装的 App 查找,找不到会报错。\n\n\
何时用:App 卡住或状态不对,或者想让下次 iphone_launch_app 从首页冷启动。只想离开 App 用 iphone_press_button 按 home,App 留在后台。\n\n\
行为:App 里没保存的输入、正在播放的内容会丢失。App 本来没在运行也不报错。结束的是前台 App 时会回到主屏幕。需要 WDA。", "Force-quit an app, like swiping it away in the app switcher. A display name is looked up among the installed apps; if not found it fails.\n\nWhen to use: the app is stuck or in a wrong state, or the next iphone_launch_app should cold-start it on its first screen. To just leave the app, press home with iphone_press_button; the app stays in the background.\n\nBehavior: unsaved input and anything playing is lost. No error if the app wasn't running. Quitting the foreground app returns to the home screen. Needs WDA."),
                ictl_l10n::tr!("已结束 com.apple.Preferences", "Terminated com.apple.Preferences"),
                false,
            ),
            schema(with_then(app()), &["app"]),
            acting(ictl_l10n::tr!("结束 App", "Quit app"), true, true),
        ),
        t(
            "iphone_open_url",
            action_desc(
                ictl_l10n::tr!("用系统默认方式打开网址或 URL Scheme:http/https 网页在 Safari 打开,URL Scheme 打开对应的 App。\n\n\
何时用:直接进入某个网页或 App 里的某个页面,比启动 App 再一级级点快。只想打开 App 用 iphone_launch_app。\n\n\
行为:有的 URL Scheme 系统会先弹窗问要不要打开,带 then=screenshot 确认。需要 WDA。", "Open a web address or URL scheme the way the system does: http/https pages open in the default browser, URL schemes open their app.\n\nWhen to use: to go straight to a web page or a screen inside an app, faster than launching the app and tapping through. To just open an app use iphone_launch_app.\n\nBehavior: for some URL schemes the system first asks whether to open them; check with then=screenshot. Needs WDA."),
                ictl_l10n::tr!("已打开 https://example.com", "Opened https://example.com"),
                true,
            ),
            schema(
                with_then(json!({"url": {"type": "string", "description": ictl_l10n::tr!("完整网址(含 https://)或 URL Scheme", "full address (including https://) or URL scheme")}})),
                &["url"],
            ),
            acting(ictl_l10n::tr!("打开链接", "Open URL"), false, false),
        ),
        t(
            "iphone_wait_for",
            ictl_l10n::tr!("等含指定文字的元素出现或消失。默认等出现;state=disappeared 时等当前界面没有匹配项。超时报错。\n\n\
何时用:操作后要等加载时代替反复截图轮询,每 0.3 秒查一次,不占上下文。输入框里的字不算(刚输入的搜索词不会被当成结果)。\
只读:不改变界面,但配置了锁屏密码时会先解锁。", "Wait for visible UI text to appear (the default), or use state=disappeared to wait for a screen without a match. Returns an error on timeout.\n\nWhen to use: after an action, to wait for loading instead of polling with screenshots; it checks every 0.3 s and uses no context meanwhile. Text inside input fields doesn't count (a search term just typed isn't taken for a result). Read-only: doesn't change the screen, but unlocks the phone first if a passcode is configured.")
                .into(),
            schema(
                json!({
                    "text": {"type": "string", "description": ictl_l10n::tr!("要等的文字,元素的标签/名字/值包含它就算出现", "the text to wait for; it counts as appeared when an element's label/name/value contains it")},
                    "timeout": {"type": "number", "default": 5, "description": ictl_l10n::tr!("最多等几秒(不超过 30)", "maximum seconds to wait (up to 30)")},
                    "state": {"type": "string", "enum": ["appeared", "disappeared"], "default": "appeared", "description": ictl_l10n::tr!("等文字出现或从当前界面消失", "wait for the text to appear or disappear from the visible screen")}
                }),
                &["text"],
            ),
            looking(ictl_l10n::tr!("等文字出现", "Wait for text")),
        ),
        t(
            "iphone_list_devices",
            ictl_l10n::tr!("列出 USB 上接着的全部 iPhone/iPad,每行一台:设备名、型号、iOS 版本、UDID,标出正在控制哪台、有没有设自动解锁密码。\n\n\
何时用:接着多台设备、要确认操作落在哪台,或切换前查设备名和 UDID。同一时间只控制一台,其他工具都作用在正在控制的那台上。只读。", "List all iPhones/iPads connected over USB, one per line: device name, model, iOS version, UDID; marks the one being controlled and whether it has an auto-unlock passcode.\n\nWhen to use: with several devices connected, to check which one actions go to, or to look up names and UDIDs before switching. Only one device is controlled at a time; all other tools act on it. Read-only.")
                .into(),
            schema(json!({}), &[]),
            looking(ictl_l10n::tr!("列出设备", "List devices")),
        ),
        t(
            "iphone_select_device",
            ictl_l10n::tr!("改为控制另一台设备,之后所有工具都作用在它上面,守护进程重启后还记得。device 是 UDID 或设备名(设备名包含这段文字且只有一台也行),\
auto 表示取消选定、改回自动选(正在控制的那台断开才换)。\n\n\
何时用:接着多台设备、要操作的不是正在控制的那台。先用 iphone_list_devices 看有哪几台。\n\n\
行为:切换后要在新设备上拉起 WDA(10~60 秒),期间读界面、按名字点击等用不了,先用 iphone_status 确认就绪;\
这台从没装过 WDA 时起不来,要请用户运行 iphone-control setup。\
自动解锁按设备分开存密码,新设备没设就不会自动解锁。设了 IPHONE_UDID 时不能切换。\n\n\
返回:一行结果文字,例如「已切换到 Derrors 的 iPad(iPad11,1 iOS 26.6.1,00008020-…)」。", "Control another device from now on: all tools act on it, and the daemon remembers it across restarts. device is a UDID or device name (part of the name works if only one device matches); auto clears the selection and goes back to automatic (switches only when the controlled device disconnects).\n\nWhen to use: with several devices connected, when the one to operate isn't the one being controlled. Look at them with iphone_list_devices first.\n\nBehavior: after switching, WDA has to start on the new device (10-60 s); until then reading the screen, tapping by text etc. don't work, so check readiness with iphone_status. If WDA was never installed on that device it won't start; ask the user to run iphone-control setup. Auto-unlock passcodes are stored per device, so a new device without one isn't auto-unlocked. Not possible while IPHONE_UDID is set.\n\nReturns: one line of result text, e.g. “Switched to Derrors's iPad (iPad11,1 iOS 26.6.1, 00008020-…)”.")
                .into(),
            schema(
                json!({"device": {"type": "string", "description": ictl_l10n::tr!("UDID、设备名,或 auto(改回自动选)", "UDID, device name, or auto (back to automatic)")}}),
                &["device"],
            ),
            ToolAnnotations::with_title(ictl_l10n::tr!("切换设备", "Switch device")).read_only(false).destructive(false).idempotent(true).open_world(false),
        ),
        t(
            "iphone_batch",
            action_desc(
                ictl_l10n::tr!("按顺序执行几步操作,一次调用完成(最多 20 步),某一步出错就停下,说明停在第几步、后面几步没执行。\n\n\
steps 每项是 {\"tool\": 工具名, 其余是该工具的参数},可用:iphone_tap、iphone_tap_element、iphone_long_press、\
iphone_swipe、iphone_type_text、iphone_press_button、iphone_launch_app、iphone_terminate_app、iphone_open_url、\
iphone_wait_for,以及 {\"tool\": \"wait\", \"seconds\": 秒数}(不超过 5)。iphone_tap_element 没点到也会停下。\
执行前先检查全部参数,有错一步都不执行。\n\n\
何时用:结果确定的连续操作(按键、逐级点菜单、填表),每步省一轮往返。每步的结果不确定(点了以后不知道会跳到哪)时,还是一步步来。\
then 在全部做完(或停下)后返回一次界面。", "Run several steps in order in one call (up to 20); stops at the first error and says which step it stopped at and how many didn't run.\n\nEach item of steps is {\"tool\": tool name, plus that tool's parameters}; allowed: iphone_tap, iphone_tap_element, iphone_long_press, iphone_swipe, iphone_type_text, iphone_press_button, iphone_launch_app, iphone_terminate_app, iphone_open_url, iphone_wait_for, and {\"tool\": \"wait\", \"seconds\": n} (up to 5). An iphone_tap_element that taps nothing also stops it. All parameters are checked first; if any is wrong, no step runs.\n\nWhen to use: sequences with predictable results (keys, menu levels, forms), saving a round trip per step. When the outcome of a step is uncertain (you don't know where a tap leads), go step by step. then returns the screen once, after the last step (or where it stopped)."),
                ictl_l10n::tr!("1. iphone_launch_app → 已启动 com.apple.Preferences(每步一行)", "1. iphone_launch_app → Launched com.apple.Preferences (one line per step)"),
                true,
            ),
            schema(
                with_then(json!({
                    "steps": {"type": "array", "items": {"type": "object"},
                        "description": ictl_l10n::tr!("要执行的步骤,1 到 20 个,例如 [{\"tool\": \"iphone_tap_element\", \"text\": \"通用\"}]", "the steps to run, 1 to 20, e.g. [{\"tool\": \"iphone_tap_element\", \"text\": \"General\"}]")},
                    "reuse_elements": {"type": "boolean", "default": false,
                        "description": ictl_l10n::tr!("这批操作不改变界面布局时(例如在同一个键盘上连按)设为 true:按名字点击复用最近读到的元素树,每步省 0.3~0.8 秒。会跳转页面的不要用,否则可能点到旧位置", "Set to true when the steps don't change the layout (e.g. pressing keys on the same keypad): tapping by text reuses the most recent element tree, saving 0.3-0.8 s per step. Don't use it for steps that navigate, or it may tap where things used to be")},
                    "one_gesture": {"type": "boolean", "default": false,
                        "description": ictl_l10n::tr!("步骤全是 iphone_tap 时可设为 true:这些点在一个手势里依次点完(每个点一根手指,间隔约 0.04 秒),中间不等界面空闲。用于在同一个键盘上连按,例如输入框不接受 iphone_type_text 时逐个点数字键:7 个键约 1.9 秒,分开点约 3.6 秒。点的过程中不看界面,中途跳了页面后面的点也照样点下去;点完截图或读元素核对", "Only when every step is iphone_tap: set to true to tap the points one after another in a single gesture (one finger per point, about 0.04 s apart) without waiting for the interface in between. For pressing keys on the same keyboard, e.g. digit keys one by one when a field doesn't take iphone_type_text: 7 keys in about 1.9 s instead of about 3.6 s. Nothing is checked while it taps, so if a tap navigates, the remaining points still land; check the result with a screenshot or elements afterwards")}
                })),
                &["steps"],
            ),
            acting(ictl_l10n::tr!("批量操作", "Batch"), true, false),
        ),
    ]
}

struct Args<'a>(&'a Map<String, Value>);

/// 数字,或写成字符串的数字(toolkit 的 pydantic 也接受 "3")
fn number(v: &Value) -> Option<f64> {
    v.as_f64().or_else(|| v.as_str()?.trim().parse().ok()).filter(|x: &f64| x.is_finite())
}

/// 参数给了但类型不对就报错,不再悄悄用默认值(例如 index: -1 原来会点第 0 个)
impl Args<'_> {
    fn get(&self, k: &str) -> Option<&Value> {
        self.0.get(k).filter(|v| !v.is_null())
    }
    fn f(&self, k: &str) -> Result<f64, String> {
        self.opt_f(k)?.ok_or_else(|| ictl_l10n::trf!("缺少数字参数 {k}", "missing number parameter {k}"))
    }
    fn f_or(&self, k: &str, d: f64) -> Result<f64, String> {
        Ok(self.opt_f(k)?.unwrap_or(d))
    }
    fn opt_f(&self, k: &str) -> Result<Option<f64>, String> {
        self.get(k)
            .map(|v| number(v).ok_or_else(|| ictl_l10n::trf!("参数 {k} 要是数字", "parameter {k} must be a number")))
            .transpose()
    }
    fn s(&self, k: &str) -> Result<String, String> {
        self.get(k)
            .and_then(Value::as_str)
            .map(str::to_owned)
            .ok_or_else(|| ictl_l10n::trf!("缺少文字参数 {k}", "missing text parameter {k}"))
    }
    fn s_or(&self, k: &str, d: &str) -> Result<String, String> {
        match self.get(k) {
            None => Ok(d.to_owned()),
            Some(v) => v
                .as_str()
                .map(str::to_owned)
                .ok_or_else(|| ictl_l10n::trf!("参数 {k} 要是文字", "parameter {k} must be text")),
        }
    }
    fn u_or(&self, k: &str, d: usize) -> Result<usize, String> {
        match self.get(k) {
            None => Ok(d),
            Some(v) => number(v).filter(|x| *x >= 0.0 && x.fract() == 0.0).map(|x| x as usize).ok_or_else(|| {
                ictl_l10n::trf!("参数 {k} 要是非负整数", "parameter {k} must be a non-negative integer")
            }),
        }
    }
    /// 连点次数:不给是 1,给了要是 1 到 [`MAX_TAP_COUNT`] 的整数
    fn count(&self, k: &str) -> Result<u32, String> {
        match self.u_or(k, 1)? {
            n @ 1.. if n <= MAX_TAP_COUNT as usize => Ok(n as u32),
            _ => Err(ictl_l10n::trf!(
                "参数 {k} 要是 1 到 {MAX_TAP_COUNT} 的整数",
                "parameter {k} must be a whole number from 1 to {MAX_TAP_COUNT}"
            )),
        }
    }
    fn b_or(&self, k: &str, d: bool) -> Result<bool, String> {
        match self.get(k) {
            None => Ok(d),
            Some(Value::Bool(b)) => Ok(*b),
            Some(Value::String(s)) if s.eq_ignore_ascii_case("true") => Ok(true),
            Some(Value::String(s)) if s.eq_ignore_ascii_case("false") => Ok(false),
            Some(_) => Err(ictl_l10n::trf!("参数 {k} 要是 true 或 false", "parameter {k} must be true or false")),
        }
    }
}

/// 工具调用 → 守护进程请求
/// `iphone_tap_element` 的 `scroll` / `max_scrolls`:没给 scroll 时不滑
fn scroll_search(a: &Args) -> Result<Option<ictl_protocol::ScrollSearch>, String> {
    let dir = a.s_or("scroll", "")?;
    let max = a.u_or("max_scrolls", 8)?;
    if dir.is_empty() {
        return Ok(None);
    }
    checked_scroll(&dir, max).map(Some)
}

fn checked_scroll(dir: &str, max: usize) -> Result<protocol::ScrollSearch, String> {
    let down = match dir {
        "down" => true,
        "up" => false,
        _ => return Err(ictl_l10n::trf!("参数 scroll 要是 down 或 up", "parameter scroll must be down or up")),
    };
    if max == 0 || max > ictl_protocol::MAX_SEARCH_SCROLLS as usize {
        return Err(ictl_l10n::trf!(
            "参数 max_scrolls 要在 1 到 {} 之间",
            "parameter max_scrolls must be between 1 and {}",
            ictl_protocol::MAX_SEARCH_SCROLLS
        ));
    }
    Ok(protocol::ScrollSearch { down, max: max as u32 })
}

pub fn to_request(name: &str, args: &Map<String, Value>) -> Result<Request, String> {
    let a = Args(args);
    Ok(match name {
        "iphone_status" => Request::Status,
        "iphone_screenshot" => {
            let r = [a.opt_f("x")?, a.opt_f("y")?, a.opt_f("width")?, a.opt_f("height")?];
            let region = match r {
                [None, None, None, None] => None,
                [Some(x), Some(y), Some(w), Some(h)] => Some((x, y, w, h)),
                _ => {
                    return Err(ictl_l10n::tr!(
                        "放大区域要同时给出 x、y、width、height",
                        "a zoom region needs all of x, y, width and height"
                    )
                    .into());
                }
            };
            Request::Screenshot { region }
        }
        "iphone_list_elements" => Request::ListElements {
            filter: a.s_or("filter", "")?,
            limit: a.u_or("limit", 150)?,
            max_age_ms: 0,
            keep_locked: false,
        },
        "iphone_scroll_collect" => {
            let limit = a.u_or("limit", protocol::MAX_SCROLL_ROWS)?;
            if !(1..=protocol::MAX_SCROLL_ROWS).contains(&limit) {
                return Err(ictl_l10n::trf!(
                    "limit 要在 1 到 {} 之间",
                    "limit must be between 1 and {}",
                    protocol::MAX_SCROLL_ROWS
                ));
            }
            let scroll = checked_scroll(&a.s_or("scroll", "down")?, a.u_or("max_scrolls", 8)?)?;
            let x = a.opt_f("x")?;
            if x.is_some_and(|x| !x.is_finite() || x < 0.0) {
                return Err(ictl_l10n::tr!("x 不能是负数", "x can't be negative").into());
            }
            Request::ScrollCollect(protocol::ScrollCollect {
                down: scroll.down,
                max_scrolls: scroll.max,
                until: a.s_or("until", "")?,
                filter: a.s_or("filter", "")?,
                limit,
                x,
            })
        }
        "iphone_tap" => Request::Tap { x: a.f("x")?, y: a.f("y")?, count: a.count("count")? },
        "iphone_tap_element" => Request::TapElement {
            text: a.s("text")?,
            index: a.u_or("index", 0)?,
            max_age_ms: 0,
            scroll: scroll_search(&a)?,
        },
        "iphone_long_press" => Request::LongPress { x: a.f("x")?, y: a.f("y")?, seconds: a.f_or("seconds", 1.0)? },
        "iphone_swipe" => Request::Swipe {
            from: (a.f("from_x")?, a.f("from_y")?),
            to: (a.f("to_x")?, a.f("to_y")?),
            seconds: a.f_or("seconds", 0.3)?,
            hold: a.f_or("hold", 0.0)?,
        },
        "iphone_type_text" => {
            let text = a.s("text")?;
            let submit = a.b_or("submit", false)?;
            if a.b_or("replace", false)? {
                Request::ReplaceText { text, submit }
            } else {
                Request::TypeText { text, submit }
            }
        }
        "iphone_press_button" => Request::PressButton { button: a.s("button")? },
        "iphone_list_apps" => Request::ListApps { include_system: a.b_or("include_system", false)? },
        "iphone_launch_app" => Request::LaunchApp { app: a.s("app")? },
        "iphone_terminate_app" => Request::TerminateApp { app: a.s("app")? },
        "iphone_open_url" => Request::OpenUrl { url: a.s("url")? },
        "iphone_list_devices" => Request::ListDevices,
        "iphone_select_device" => Request::SelectDevice { device: a.s("device")? },
        "iphone_app_logs" => Request::AppLogs {
            process: a.s_or("process", "")?,
            grep: a.s_or("grep", "")?,
            since_secs: a.u_or("since", 60)? as u64,
            limit: a.u_or("limit", 100)?,
            errors_only: a.b_or("errors_only", false)?,
        },
        "iphone_crash_reports" => Request::CrashReports { filter: a.s_or("filter", "")?, name: a.s_or("name", "")? },
        "iphone_install_app" => Request::InstallApp { path: a.s("path")? },
        "iphone_uninstall_app" => Request::UninstallApp { app: a.s("app")? },
        "iphone_web_targets" => Request::WebTargets,
        "iphone_web_eval" => {
            let secs = a.f_or("timeout", 10.0)?;
            if !(0.0..=60.0).contains(&secs) {
                return Err(
                    ictl_l10n::tr!("timeout 要在 0 到 60 秒之间", "timeout must be between 0 and 60 seconds").into()
                );
            }
            Request::WebEval {
                target: a.s_or("target", "")?,
                expression: a.s("expression")?,
                timeout_ms: (secs * 1000.0) as u64,
            }
        }
        "iphone_wait_for" => {
            let secs = a.f_or("timeout", 5.0)?;
            if !(0.0..=30.0).contains(&secs) {
                return Err(
                    ictl_l10n::tr!("timeout 要在 0 到 30 秒之间", "timeout must be between 0 and 30 seconds").into()
                );
            }
            let text = a.s("text")?;
            let timeout_ms = (secs * 1000.0) as u64;
            match a.s_or("state", "appeared")?.as_str() {
                "appeared" => Request::WaitFor { text, timeout_ms },
                "disappeared" => Request::WaitForDisappearance { text, timeout_ms },
                _ => {
                    return Err(ictl_l10n::tr!(
                        "state 只能是 appeared 或 disappeared",
                        "state must be appeared or disappeared"
                    )
                    .into());
                }
            }
        }
        other => return Err(ictl_l10n::trf!("未知工具 {other}", "unknown tool {other}")),
    })
}

/// 操作后顺带返回什么
#[derive(Debug, Clone, PartialEq)]
pub enum Then {
    None,
    Elements(String),
    Screenshot(Option<(f64, f64, f64, f64)>),
}

/// then 带的选项:返回什么 + 最多等多久让界面稳定
#[derive(Debug, Clone, PartialEq)]
pub struct ThenOpts {
    pub then: Then,
    pub settle_ms: u64,
}

pub fn parse_then(args: &Map<String, Value>) -> Result<ThenOpts, String> {
    let a = Args(args);
    let then = match a.s_or("then", "none")?.as_str() {
        "none" | "" => Then::None,
        "elements" => Then::Elements(a.s_or("then_filter", "")?),
        "screenshot" => {
            let region =
                match a.get("then_region") {
                    None => None,
                    Some(v) => {
                        let xs: Option<Vec<f64>> = v.as_array().map(|arr| arr.iter().filter_map(number).collect());
                        match xs.as_deref() {
                            Some([x, y, w, h]) if *w > 0.0 && *h > 0.0 => Some((*x, *y, *w, *h)),
                            _ => return Err(ictl_l10n::tr!(
                                "then_region 要是 [x, y, width, height] 四个数,宽高大于 0",
                                "then_region must be four numbers [x, y, width, height], with width and height above 0"
                            )
                            .into()),
                        }
                    }
                };
            Then::Screenshot(region)
        }
        other => {
            return Err(ictl_l10n::trf!(
                "then 只能是 none / elements / screenshot,不是 {other}",
                "then must be none / elements / screenshot, not {other}"
            ));
        }
    };
    let secs = a.f_or("then_settle", THEN_SETTLE_MS as f64 / 1000.0)?;
    if !(0.0..=10.0).contains(&secs) {
        return Err(
            ictl_l10n::tr!("then_settle 要在 0 到 10 秒之间", "then_settle must be between 0 and 10 seconds").into()
        );
    }
    Ok(ThenOpts { then, settle_ms: (secs * 1000.0) as u64 })
}

/// Each tool's argument names: the properties of its input schema, the developer tools' and
/// [`task::CONFIRM_TOOL`]'s included
static ARG_NAMES: std::sync::LazyLock<std::collections::HashMap<String, Vec<String>>> =
    std::sync::LazyLock::new(|| {
        tools_with(true)
            .into_iter()
            .chain([confirm_tool()])
            .map(|t| {
                let names = t
                    .input_schema
                    .get("properties")
                    .and_then(Value::as_object)
                    .map(|p| p.keys().cloned().collect())
                    .unwrap_or_default();
                (t.name.to_string(), names)
            })
            .collect()
    });

/// Refuses an argument `tool` doesn't take, before anything is done. Unknown arguments used to be dropped: a call
/// with a misspelt name, or with one this version doesn't have yet, ran with the default instead and reported success
/// (`iphone_wait_for` with `state: "disappeared"` answered “appeared” at once, #158). `extra`: names taken besides the
/// schema's (a batch step's `tool`); `then` and the like are only taken where the schema lists them, so not in a
/// batch's steps (the batch's own then applies). A tool this server doesn't know is left to the call to refuse
pub fn check_args(tool: &str, args: &Map<String, Value>, extra: &[&str], then: bool) -> Result<(), String> {
    let Some(names) = ARG_NAMES.get(tool) else { return Ok(()) };
    let then_names = ["then", "then_filter", "then_region", "then_settle"];
    let takes = |k: &str| (names.iter().any(|n| n == k) && (then || !then_names.contains(&k))) || extra.contains(&k);
    let mut unknown: Vec<&str> = args.keys().map(String::as_str).filter(|k| !takes(k)).collect();
    if unknown.is_empty() {
        return Ok(());
    }
    unknown.sort_unstable();
    let mut known: Vec<&str> =
        names.iter().map(String::as_str).filter(|k| takes(k)).chain(extra.iter().copied()).collect();
    known.sort_unstable();
    known.dedup();
    let sep = ictl_l10n::tr!("、", ", ");
    let known = if known.is_empty() { ictl_l10n::tr!("(无)", "(none)").to_owned() } else { known.join(sep) };
    Err(ictl_l10n::trf!(
        "{tool} 没有参数 {},什么都没做;它的参数:{known}",
        "{tool} has no argument {}, nothing was done; its arguments: {known}",
        unknown.join(sep)
    ))
}

/// 批量里的一步
#[derive(Debug, Clone, PartialEq)]
pub enum Step {
    Request { tool: String, req: Request },
    Wait(std::time::Duration),
}

/// 检查并转换批量步骤(一步都不执行就先把参数错误全挡掉)
pub fn batch_steps(args: &Map<String, Value>) -> Result<Vec<Step>, String> {
    let steps = args
        .get("steps")
        .and_then(Value::as_array)
        .ok_or(ictl_l10n::tr!("缺少 steps(数组)", "missing steps (array)"))?;
    let reuse = Args(args).b_or("reuse_elements", false)?;
    let one_gesture = Args(args).b_or("one_gesture", false)?;
    if steps.is_empty() || steps.len() > BATCH_MAX_STEPS {
        return Err(ictl_l10n::trf!(
            "steps 要有 1 到 {BATCH_MAX_STEPS} 步",
            "steps must have 1 to {BATCH_MAX_STEPS} steps"
        ));
    }
    let steps: Vec<Step> = steps
        .iter()
        .enumerate()
        .map(|(i, s)| {
            let n = i + 1;
            let obj = s.as_object().ok_or_else(|| ictl_l10n::trf!("第 {n} 步不是对象", "step {n} is not an object"))?;
            let tool = obj
                .get("tool")
                .and_then(Value::as_str)
                .ok_or_else(|| ictl_l10n::trf!("第 {n} 步缺少 tool", "step {n} has no tool"))?;
            if !BATCH_TOOLS.contains(&tool) {
                return Err(ictl_l10n::trf!(
                    "第 {n} 步:批量里不能用 {tool},可用:{}",
                    "step {n}: {tool} can't be used in a batch; allowed: {}",
                    BATCH_TOOLS.join(ictl_l10n::tr!("、", ", "))
                ));
            }
            if tool == "wait" {
                if let Some(k) = obj.keys().find(|k| !matches!(k.as_str(), "tool" | "seconds")) {
                    return Err(ictl_l10n::trf!(
                        "第 {n} 步:wait 没有参数 {k},它的参数:tool、seconds",
                        "step {n}: wait has no argument {k}; its arguments: tool, seconds"
                    ));
                }
                let secs = Args(obj).f("seconds").map_err(|e| ictl_l10n::trf!("第 {n} 步:{e}", "step {n}: {e}"))?;
                if !(0.0..=5.0).contains(&secs) {
                    return Err(ictl_l10n::trf!(
                        "第 {n} 步:wait 的 seconds 要在 0 到 5 之间",
                        "step {n}: wait's seconds must be between 0 and 5"
                    ));
                }
                return Ok(Step::Wait(std::time::Duration::from_secs_f64(secs)));
            }
            let req = match check_args(tool, obj, &["tool"], false)
                .and_then(|()| to_request(tool, obj))
                .map_err(|e| ictl_l10n::trf!("第 {n} 步({tool}):{e}", "step {n} ({tool}): {e}"))?
            {
                Request::TapElement { text, index, scroll, .. } if reuse => {
                    Request::TapElement { text, index, max_age_ms: BATCH_REUSE_MS, scroll }
                }
                other => other,
            };
            req.check_required_text()
                .map_err(|e| ictl_l10n::trf!("第 {n} 步({tool}):{e}", "step {n} ({tool}): {e}"))?;
            Ok(Step::Request { tool: tool.to_owned(), req })
        })
        .collect::<Result<_, String>>()?;
    if !one_gesture {
        return Ok(steps);
    }
    // 全是坐标点击才能并成一个手势:别的步骤(按名字点、等待、输入)都要在点之间看界面或花时间
    let points = steps
        .iter()
        .map(|s| match s {
            Step::Request { req: Request::Tap { x, y, count }, .. } => Some((*x, *y, *count)),
            _ => None,
        })
        .collect::<Option<Vec<_>>>()
        .ok_or(ictl_l10n::tr!(
            "one_gesture 只能用于步骤全是 iphone_tap(按坐标点)的批量",
            "one_gesture only works when every step is iphone_tap (tap by coordinates)"
        ))?;
    // 一个手势里每个点只点一下;要连点用 iphone_tap 的 count,不悄悄当成点一下
    if points.iter().any(|p| p.2 != 1) {
        return Err(ictl_l10n::tr!(
            "one_gesture 里的 iphone_tap 不能带 count;要在一个点连点,单独调用 iphone_tap 并给 count",
            "iphone_tap steps under one_gesture can't take count; to tap one point several times, call iphone_tap on its own with count"
        )
        .into());
    }
    let points = points.into_iter().map(|(x, y, _)| (x, y)).collect();
    Ok(vec![Step::Request { tool: "iphone_tap".into(), req: Request::TapSequence { points } }])
}

/// tap_element 的结果是不是「没点到」(没找到、index 超出匹配个数、或整个被别的东西盖住)
pub fn tap_missed(text: &str, outcome: Option<protocol::Outcome>) -> bool {
    protocol::tapped_nothing(text, outcome)
}

/// 不带 then 的操作做完后要不要查系统弹窗(弹窗挡着时操作都落在弹窗上,WDA 照样报成功)。
/// 按键自己处理(Home 先查弹窗;锁屏后不用查),结束 App 后前台是主屏,不会冒出新弹窗
/// Every variant is listed (no `_`), so a new request type has to be decided here
pub fn wants_alert_check(req: &Request) -> bool {
    match req {
        Request::Tap { .. }
        | Request::TapSequence { .. }
        | Request::TapElement { .. }
        | Request::LongPress { .. }
        | Request::Swipe { .. }
        | Request::TypeText { .. }
        | Request::ReplaceText { .. }
        | Request::LaunchApp { .. }
        | Request::OpenUrl { .. } => true,
        // 按键见 PressButton 自己的弹窗处理;切换设备后新设备的 WDA 还没起来,查不了弹窗;
        // 安装、卸载、Web Inspector 不碰屏幕(Web Inspector 也不该为它排队等设备锁)
        // the web viewer's finger isn't an MCP tool; the person watching sees any alert
        Request::PressButton { .. }
        | Request::Touch { .. }
        | Request::TerminateApp { .. }
        | Request::SelectDevice { .. }
        | Request::InstallApp { .. }
        | Request::UninstallApp { .. }
        | Request::WebEval { .. }
        | Request::Status
        | Request::ScreenStream
        | Request::Screenshot { .. }
        | Request::ScrollCollect(_)
        | Request::ListElements { .. }
        | Request::ListApps { .. }
        | Request::Settle { .. }
        | Request::AlertCheck
        | Request::WaitFor { .. }
        | Request::WaitForDisappearance { .. }
        | Request::Daemon
        | Request::Doctor
        | Request::Resign { .. }
        | Request::UnlockReset
        | Request::ListDevices
        | Request::AppLogs { .. }
        | Request::CrashReports { .. }
        | Request::WebTargets
        // tasks: not tools, nothing on the screen
        | Request::TaskOpen { .. }
        | Request::TaskEvents { .. }
        | Request::TaskList
        | Request::TaskControl { .. }
        | Request::TaskAgent
        | Request::TaskCheck
        | Request::TaskStep { .. }
        | Request::TaskStepDone { .. }
        | Request::TaskConfirm { .. }
        | Request::TaskNote { .. }
        | Request::TaskFinish { .. } => false,
    }
}

/// The shape of [`with_element_data`]' structured content, declared as `iphone_list_elements`' output schema (action
/// tools give it with then=elements, but not always: a declared schema would oblige every result to carry it).
/// `matched_count` and `truncated` are left out by a daemon older than them
fn elements_output_schema() -> Map<String, Value> {
    let element = json!({
        "type": "object",
        "properties": {
            "type": {"type": "string"}, "label": {"type": "string"},
            "x": {"type": "number"}, "y": {"type": "number"}, "width": {"type": "number"}, "height": {"type": "number"}
        }
    });
    let v = json!({
        "type": "object",
        "properties": {
            "type": {"const": "elements"},
            "elements": {"type": "array", "items": element,
                "description": "the elements listed, at most limit; x/y the top left corner in points"},
            "matched_count": {"type": "integer", "description": "how many elements matched filter in this read, before limit"},
            "truncated": {"type": "boolean", "description": "matched_count is more than limit: some weren't listed"}
        },
        "required": ["elements"]
    });
    v.as_object().cloned().unwrap_or_default()
}

fn with_element_data(mut result: CallToolResult, data: Option<protocol::Data>) -> CallToolResult {
    if let Some(data @ (protocol::Data::Elements { .. } | protocol::Data::ScrollCollection { .. })) = data {
        result.structured_content = serde_json::to_value(data).ok();
    }
    result
}

/// 守护进程响应 → MCP 结果。错误走工具错误(is_error),文字原样给客户端
pub fn to_result(r: Response) -> CallToolResult {
    match r {
        Response::Text { text, data, .. } => {
            with_element_data(CallToolResult::success(vec![ContentBlock::text(text)]), data)
        }
        Response::Image { jpeg_base64, caption } => {
            CallToolResult::success(vec![ContentBlock::image(jpeg_base64, "image/jpeg"), ContentBlock::text(caption)])
        }
        Response::Error { message, .. } => CallToolResult::error(vec![ContentBlock::text(message)]),
    }
}

/// Tools that expose delivery of their primary tap, gesture, or text input.
pub fn reports_input(name: &str) -> bool {
    matches!(name, "iphone_tap" | "iphone_tap_element" | "iphone_type_text" | "iphone_swipe" | "iphone_long_press")
}

fn input_error(
    message: impl Into<String>,
    name: &str,
    delivery: Delivery,
    stage: Option<FailureStage>,
) -> CallToolResult {
    let mut message = message.into();
    if !reports_input(name) {
        return to_result(Response::error(message));
    }
    if delivery == Delivery::Unknown {
        message += ictl_l10n::tr!(
            "。输入可能已送达,请先查看屏幕再决定下一步。",
            ". Input may have reached the phone. Inspect the screen before retrying."
        );
    }
    let mut result = to_result(Response::error(message));
    result.structured_content =
        Some(json!({"delivery": delivery, "readback": {"screen": "not_requested", "settle": "not_requested"}}));
    if let Some(stage) = stage {
        result.structured_content.as_mut().unwrap()["stage"] = json!(stage);
    }
    result
}

fn add_delivery_hint(message: &mut String, delivery: Delivery) {
    let lower = message.to_ascii_lowercase();
    let already_guided =
        ["inspect the screen", "take a screenshot", "check with a screenshot", "查看屏幕", "截图确认", "截图看看"]
            .iter()
            .any(|hint| lower.contains(hint));
    message.truncate(message.trim_end().trim_end_matches(['.', '。']).len());
    message.push_str(match delivery {
        Delivery::NotSent => ictl_l10n::tr!("。目标输入未发出。", ". The requested input was not sent."),
        Delivery::Unknown => ictl_l10n::tr!("。输入可能已送达。", ". Input may have reached the phone."),
        Delivery::Sent => ictl_l10n::tr!("。输入已发出。", ". Input was sent."),
    });
    if !already_guided && delivery != Delivery::NotSent {
        message.push_str(ictl_l10n::tr!("请先查看屏幕再决定下一步。", " Inspect the screen before retrying."));
    }
}

struct InputResult<'a> {
    delivery: Delivery,
    stage: Option<FailureStage>,
    field: Option<&'static str>,
    udid: Option<&'a str>,
}

#[derive(Debug, Clone)]
pub struct McpServer {
    pub socket: PathBuf,
    /// This process works for a task (`IPHONE_CONTROL_TASK_TOKEN`, see [`task`]); over HTTP a request may instead
    /// carry a task's token in [`protocol::TASK_TOKEN_HEADER`] ([`request_token`])
    pub task: bool,
}

/// The task token an HTTP request carries ([`protocol::TASK_TOKEN_HEADER`]); `None` over stdio
fn request_token(context: &RequestContext<RoleServer>) -> Option<String> {
    let parts = context.extensions.get::<http::request::Parts>()?;
    let t = parts.headers.get(protocol::TASK_TOKEN_HEADER)?.to_str().ok()?.trim();
    (!t.is_empty()).then(|| t.to_owned())
}

/// MCP over HTTP (Streamable HTTP, one session per client) for the viewer's `/mcp`
pub type HttpService = rmcp::transport::streamable_http_server::StreamableHttpService<
    McpServer,
    rmcp::transport::streamable_http_server::session::local::LocalSessionManager,
>;

/// The service behind `/mcp`. It doesn't check `Host` or `Origin`: the viewer does, with the names of its page
pub fn http_service(socket: PathBuf) -> HttpService {
    use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
    let config = StreamableHttpServerConfig::default().with_allowed_hosts(Vec::<String>::new());
    StreamableHttpService::new(
        move || Ok(McpServer { socket: socket.clone(), task: false }),
        std::sync::Arc::default(),
        config,
    )
}

impl ServerHandler for McpServer {
    fn get_info(&self) -> InitializeResult {
        InitializeResult::new(ServerCapabilities::builder().enable_tools().build()).with_instructions(instructions())
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, ErrorData> {
        let mut t = tools();
        if self.task || request_token(&context).is_some() {
            t.push(confirm_tool());
        }
        Ok(ListToolsResult::with_all_items(t))
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        let token = request_token(&context);
        if !self.task && token.is_none() {
            return self.call_tool_cancellable(request, &context).await;
        }
        // every request of this call goes out with the HTTP request's token (over stdio: the process's)
        protocol::as_task(token, None, self.call_tool_in_task(request, context)).await
    }
}

impl McpServer {
    /// [`ServerHandler::call_tool`] for a task's agent
    async fn call_tool_in_task(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        // working for a task: the daemon holds or refuses the calls; announce each so the page shows the tool
        let name = request.name.clone();
        let args = request.arguments.clone().unwrap_or_default();
        let work = async {
            if request.name == task::CONFIRM_TOOL {
                if let Err(e) = check_args(&request.name, &args, &[], false) {
                    return Ok(CallToolResult::error(vec![ContentBlock::text(e)]).into());
                }
                let r = match task::confirm(&self.socket, &args).await {
                    Ok(m) => CallToolResult::success(vec![ContentBlock::text(m)]),
                    Err(m) => CallToolResult::error(vec![ContentBlock::text(m)]),
                };
                return Ok(r.into());
            }
            match task::before(&self.socket, &request.name, &args).await {
                task::Before::Go(step) => {
                    let t0 = std::time::Instant::now();
                    let r = protocol::as_task(None, Some(step), self.call_tool_inner(request)).await;
                    if let Ok(CallToolResponse::Complete(c)) = &r {
                        let v = serde_json::to_value(c).unwrap_or_default();
                        task::after(&self.socket, step, &v, t0.elapsed().as_millis() as u64).await;
                    }
                    r
                }
                task::Before::Refuse(m) => {
                    Ok(input_error(m, &name, Delivery::NotSent, Some(FailureStage::Preflight)).into())
                }
            }
        };
        tokio::select! {
            r = work => r,
            _ = context.ct.cancelled() => Ok(input_error(ictl_l10n::tr!("调用已取消", "Call cancelled"), &name, Delivery::Unknown, None).into()),
        }
    }

    async fn call_tool_cancellable(
        &self,
        request: CallToolRequestParams,
        context: &RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        // rmcp 收到取消通知只是置这个令牌,不会中止处理:自己停下。丢掉进行中的调用会断开到守护进程的连接,
        // 守护进程那边还在排队的请求随之放弃(protocol::call),iphone_batch 剩下的步骤也不再执行
        let name = request.name.clone();
        tokio::select! {
            r = self.call_tool_inner(request) => r,
            _ = context.ct.cancelled() => Ok(input_error(ictl_l10n::tr!("调用已取消", "Call cancelled"), &name, Delivery::Unknown, None).into()),
        }
    }

    async fn call_tool_inner(&self, request: CallToolRequestParams) -> Result<CallToolResponse, ErrorData> {
        let args = request.arguments.unwrap_or_default();
        if let Err(e) = check_args(&request.name, &args, &[], true) {
            return Ok(input_error(e, &request.name, Delivery::NotSent, Some(FailureStage::Preflight)).into());
        }
        let then = match parse_then(&args) {
            Ok(t) => t,
            Err(e) => return Ok(input_error(e, &request.name, Delivery::NotSent, Some(FailureStage::Preflight)).into()),
        };
        if request.name == "iphone_batch" {
            return Ok(self.batch(&args, then).await.into());
        }
        if DEV_TOOLS.contains(&&*request.name) && !dev_tools_enabled() {
            let name = &request.name;
            return Ok(to_result(Response::error(ictl_l10n::trf!(
                "{name} 是开发者工具,要先设 IPHONE_CONTROL_DEV_TOOLS=1",
                "{name} is a developer tool; set IPHONE_CONTROL_DEV_TOOLS=1 first"
            )))
            .into());
        }
        let req = match to_request(&request.name, &args) {
            Ok(r) => r,
            Err(e) => return Ok(input_error(e, &request.name, Delivery::NotSent, Some(FailureStage::Preflight)).into()),
        };
        // then 只对操作类工具有意义;看界面的工具本身就是返回界面
        // A single input and its readbacks must all apply to the same device. Tasks already pin theirs.
        let udid = if req.reports_delivery() && !protocol::in_task() {
            match self.controlled_udid().await {
                Ok(id) => Some(id),
                Err((message, stage)) => {
                    return Ok(input_error(message, &request.name, Delivery::NotSent, Some(stage)).into());
                }
            }
        } else {
            None
        };
        let resp = self.call_on(&req, udid.as_deref()).await;
        let input = req.reports_delivery().then(|| InputResult {
            delivery: resp.delivery(&req),
            stage: resp.failure_stage(),
            field: resp.field_readback(&req),
            udid: udid.as_deref(),
        });
        if matches!(req, Request::ScrollCollect(_)) {
            return Ok(to_result(resp).into());
        }
        Ok(match (resp, input) {
            (Response::Error { mut message, .. }, Some(input)) => {
                add_delivery_hint(&mut message, input.delivery);
                self.finish(message, true, then, false, false, Some(input)).await
            }
            (Response::Text { text, outcome, .. }, input) if req.is_device_action() => {
                // 结果里已经说了有弹窗(启动 App 被挡)就不再查
                let check = wants_alert_check(&req) && !protocol::alert_reported(&text, outcome);
                // 按名字没点到东西:界面没动过,不用等它稳定,直接给界面(按名字点击刚读过的树)
                let settle = !tap_missed(&text, outcome);
                self.finish(text, false, then, check, settle, input).await
            }
            (resp, _) => to_result(resp),
        }
        .into())
    }

    async fn call(&self, req: &Request) -> Response {
        self.call_on(req, None).await
    }

    /// `expect_udid`:守护进程正在控制的不是这台就不执行(见 [`protocol::EXPECT_UDID`])
    async fn call_on(&self, req: &Request, expect_udid: Option<&str>) -> Response {
        // 守护进程正忙(例如在自动解锁)时会排队;最多等 150 秒,免得 agent 一直挂着。An install uploads the package and
        // can take several minutes on its own
        let wait = if matches!(req, Request::InstallApp { .. }) { 660 } else { 150 };
        // in a task the daemon holds calls while the user has paused or taken over, as long as that takes
        let wait = (!protocol::in_task()).then(|| Duration::from_secs(wait));
        protocol::exchange_on(&self.socket, req, wait, expect_udid, None, None).await.unwrap_or_else(|e| {
            let reason = e.source;
            let message = if e.failure.stage == FailureStage::Connect {
                ictl_l10n::trf!(
                    "连不上守护进程({}):{reason};确认守护进程在运行(iphone-control status)",
                    "Can't reach the daemon ({}): {reason}; make sure the daemon is running (iphone-control status)",
                    self.socket.display()
                )
            } else {
                ictl_l10n::trf!("守护进程请求失败: {reason}", "Daemon request failed: {reason}")
            };
            Response::error(message).with_failure(e.failure.delivery, e.failure.stage)
        })
    }

    /// 操作完成后:按 then 等界面稳定,再附上元素列表或截图(等稳定时顺带提醒弹窗)。
    /// 不带 then 时,`alert_check` 为真就查一下有没有系统弹窗挡着。`settle` 为假(什么都没点)时不等稳定
    async fn finish(
        &self,
        mut text: String,
        failed: bool,
        opts: ThenOpts,
        alert_check: bool,
        settle: bool,
        input: Option<InputResult<'_>>,
    ) -> CallToolResult {
        let expect_udid = input.as_ref().and_then(|i| i.udid);
        let mut elements = None;
        let mut readback = json!({"settle": "not_requested", "screen": "not_requested"});
        let mut image = None;
        if opts.then == Then::None {
            if alert_check {
                match self.call_on(&Request::AlertCheck, expect_udid).await {
                    Response::Text { text: t, .. } => {
                        text += &t;
                        readback["alert"] = "ok".into();
                    }
                    Response::Error { message, .. } if input.is_some() => {
                        text += &ictl_l10n::trf!("\n(检查弹窗失败:{message})", "\n(alert check failed: {message})");
                        readback["alert"] = "failed".into();
                    }
                    _ => readback["alert"] = "failed".into(),
                }
            }
        } else {
            if settle {
                match self.call_on(&Request::Settle { max_ms: opts.settle_ms }, expect_udid).await {
                    Response::Text { text: t, .. } => {
                        text += &format!("\n{t}");
                        readback["settle"] = "ok".into();
                    }
                    Response::Error { message, .. } => {
                        text += &ictl_l10n::trf!(
                            "\n(等界面稳定失败:{message})",
                            "\n(waiting for the screen to settle failed: {message})"
                        );
                        readback["settle"] = "failed".into();
                    }
                    _ => readback["settle"] = "failed".into(),
                }
            }
            let read = match opts.then {
                Then::Elements(filter) => {
                    Request::ListElements { filter, limit: THEN_LIMIT, max_age_ms: THEN_SETTLE_MS, keep_locked: false }
                }
                Then::Screenshot(region) => Request::Screenshot { region },
                Then::None => unreachable!(),
            };
            let screen = match self.call_on(&read, expect_udid).await {
                Response::Text { text: t, data, .. } if matches!(read, Request::ListElements { .. }) => {
                    elements = data;
                    text += &ictl_l10n::trf!("\n--- 操作后的界面 ---\n{t}", "\n--- Screen after the action ---\n{t}");
                    "ok"
                }
                Response::Image { jpeg_base64, caption } if matches!(read, Request::Screenshot { .. }) => {
                    image = Some((jpeg_base64, caption));
                    "ok"
                }
                Response::Error { message, .. } => {
                    text += &ictl_l10n::trf!("\n(读界面失败:{message})", "\n(reading the screen failed: {message})");
                    "failed"
                }
                _ => {
                    text += ictl_l10n::tr!("\n(未收到请求的界面)", "\n(The requested screen was not returned.)");
                    "failed"
                }
            };
            readback["screen"] = screen.into();
        }
        let read_failed = readback.as_object().unwrap().values().any(|v| v == "failed");
        if input.is_some() && read_failed && !failed {
            text += ictl_l10n::tr!(
                "\n请单独读取屏幕确认结果,避免重复之前的输入。",
                "\nRead the screen separately before repeating any input."
            );
        }
        let mut blocks = vec![ContentBlock::text(text)];
        if let Some((jpeg, caption)) = image {
            blocks.push(ContentBlock::image(jpeg, "image/jpeg"));
            blocks.push(ContentBlock::text(caption));
        }
        let mut result = if failed { CallToolResult::error(blocks) } else { CallToolResult::success(blocks) };
        result = with_element_data(result, elements);
        if let Some(input) = input {
            let data = result.structured_content.get_or_insert_with(|| json!({}));
            data["delivery"] = json!(input.delivery);
            data["readback"] = readback;
            if let Some(field) = input.field {
                data["readback"]["field"] = field.into();
            }
            if let Some(stage) = input.stage.or_else(|| (read_failed && !failed).then_some(FailureStage::Readback)) {
                data["stage"] = json!(stage);
            }
        }
        result
    }

    /// The cached daemon snapshot includes device.udid on older daemons as well.
    async fn controlled_udid(&self) -> Result<String, (String, FailureStage)> {
        match self.call(&Request::Daemon).await {
            Response::Text { text, .. } => {
                if let Ok(snapshot) = serde_json::from_str::<Value>(&text)
                    && let Some(id) = snapshot["device"]["udid"].as_str().filter(|id| !id.is_empty())
                {
                    return Ok(id.to_owned());
                }
            }
            Response::Error { message, failure, .. } => {
                return Err((message, failure.map_or(FailureStage::Preflight, |f| f.stage)));
            }
            Response::Image { .. } => {}
        }
        Err((
            ictl_l10n::tr!(
                "无法确定当前控制的设备,操作未执行",
                "Cannot identify the controlled device; action not sent"
            )
            .into(),
            FailureStage::Preflight,
        ))
    }

    /// 按顺序执行;某一步出错就停,说明是第几步、后面几步没执行。出错时也按 then 附上当前界面,方便看停在哪
    async fn batch(&self, args: &Map<String, Value>, then: ThenOpts) -> CallToolResult {
        let steps = match batch_steps(args) {
            Ok(s) => s,
            Err(e) => return to_result(Response::error(e)),
        };
        let total = steps.len();
        // 每一步都要求还是开始时控制的那台:自动选设备时拔掉它会改控另一台,剩下的步骤不能落到那台上
        let udid = self.controlled_udid().await.ok();
        let mut lines = Vec::new();
        // 最后一步是不是需要查弹窗的操作
        let mut check = false;
        for (i, step) in steps.into_iter().enumerate() {
            let n = i + 1;
            match step {
                Step::Wait(d) => {
                    tokio::time::sleep(d).await;
                    lines.push(ictl_l10n::trf!("{n}. wait → 等了 {} 秒", "{n}. wait → waited {} s", d.as_secs_f64()));
                }
                Step::Request { tool, req } => match self.call_on(&req, udid.as_deref()).await {
                    // 按名字没点到(toolkit 兼容:单独调用时是普通结果)在批量里要停下:后面的步骤都建立在它点中的前提上
                    Response::Text { text, outcome, .. }
                        if tool == "iphone_tap_element" && tap_missed(&text, outcome) =>
                    {
                        lines.push(format!("{n}. {tool} → {text}"));
                        if n < total {
                            lines.push(ictl_l10n::trf!(
                                "第 {n} 步没点到,后面 {} 步没有执行",
                                "Step {n} tapped nothing, the remaining {} step(s) didn't run",
                                total - n
                            ));
                        }
                        return self.finish(lines.join("\n"), true, then, false, true, None).await;
                    }
                    Response::Text { text, outcome, .. } => {
                        check = wants_alert_check(&req) && !protocol::alert_reported(&text, outcome);
                        lines.push(format!("{n}. {tool} → {text}"));
                    }
                    Response::Image { caption, .. } => lines.push(format!("{n}. {tool} → {caption}")),
                    Response::Error { message, .. } => {
                        lines.push(ictl_l10n::trf!("{n}. {tool} → 出错:{message}", "{n}. {tool} → error: {message}"));
                        if n < total {
                            lines.push(ictl_l10n::trf!(
                                "第 {n} 步出错,后面 {} 步没有执行",
                                "Step {n} failed, the remaining {} step(s) didn't run",
                                total - n
                            ));
                        }
                        return self.finish(lines.join("\n"), true, then, true, true, None).await;
                    }
                },
            }
        }
        self.finish(lines.join("\n"), false, then, check, true, None).await
    }
}

/// 在 stdin/stdout 上运行 MCP 服务直到客户端断开
pub async fn serve_stdio(socket: PathBuf) -> anyhow::Result<()> {
    let running = McpServer { socket, task: task::join_from_env() }.serve(rmcp::transport::stdio()).await?;
    running.waiting().await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn args(v: Value) -> Map<String, Value> {
        v.as_object().cloned().unwrap()
    }

    #[tokio::test]
    async fn input_delivery_is_independent_of_field_and_screen_readback() {
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
        let socket = PathBuf::from(format!("/tmp/ictl-single-input-{}.sock", std::process::id()));
        let server = McpServer { socket: socket.clone(), task: false };
        let call = |name, args: Value| {
            let req = CallToolRequestParams::new(name).with_arguments(args.as_object().unwrap().clone());
            let server = &server;
            async move {
                let CallToolResponse::Complete(result) = server.call_tool_inner(req).await.unwrap() else { panic!() };
                result
            }
        };
        let tap = json!({"x": 1, "y": 2});
        let unavailable = call("iphone_tap", tap.clone()).await;
        let data = unavailable.structured_content.unwrap();
        assert_eq!(data["delivery"], "not_sent");
        assert_eq!(data["stage"], "connect");

        let listener = tokio::net::UnixListener::bind(&socket).unwrap();
        let exchanges = [
            ("tap", None), // request reached daemon, reply lost
            ("tap", Some(Response::text("Tapped"))),
            ("settle", Some(Response::text("Settled"))),
            ("screenshot", Some(Response::error("controlled device changed to phone-B"))),
            ("tap", Some(Response::text("Tapped"))),
            ("settle", Some(Response::error("lost settle"))),
            (
                "list_elements",
                Some(Response::text_data(
                    "items",
                    Default::default(),
                    Some(protocol::Data::Elements { elements: vec![], matched_count: Some(3), truncated: Some(true) }),
                )),
            ),
            (
                "type_text",
                Some(Response::text_with(
                    "Typed",
                    protocol::Outcome { field_verified: Some(false), ..Default::default() },
                )),
            ),
            ("alert_check", Some(Response::text(""))),
            ("type_text", Some(Response::error("field mismatch").with_failure(Delivery::Sent, FailureStage::Readback))),
            (
                "tap_element",
                Some(Response::text_with("No match", protocol::Outcome { tapped_nothing: true, ..Default::default() })),
            ),
            ("alert_check", Some(Response::text(""))),
        ];
        let daemon = async {
            let mut first_snapshot = true;
            for (op, reply) in exchanges {
                let mut peer = loop {
                    let (mut peer, _) = listener.accept().await.unwrap();
                    let mut line = String::new();
                    BufReader::new(&mut peer).read_line(&mut line).await.unwrap();
                    let request: Value = serde_json::from_str(&line).unwrap();
                    if request["op"] == "daemon" {
                        // Old daemon snapshot: only the controlled device's UDID is needed.
                        let snapshot = if std::mem::take(&mut first_snapshot) {
                            Response::text(r#"{"device":null}"#)
                        } else {
                            Response::text(r#"{"device":{"udid":"phone-A"}}"#)
                        };
                        peer.write_all(format!("{}\n", json!(snapshot)).as_bytes()).await.unwrap();
                        continue;
                    }
                    assert_eq!(request["op"], op);
                    assert_eq!(request[protocol::EXPECT_UDID], "phone-A", "{request}");
                    break peer;
                };
                if let Some(reply) = reply {
                    peer.write_all(format!("{}\n", json!(reply)).as_bytes()).await.unwrap();
                }
            }
        };
        let actions = async {
            let missing = call("iphone_tap", tap.clone()).await;
            let data = missing.structured_content.unwrap();
            assert_eq!(data["delivery"], "not_sent");
            assert_eq!(data["stage"], "preflight");

            let lost = call("iphone_tap", tap).await;
            assert_eq!(lost.is_error, Some(true));
            let data = lost.structured_content.unwrap();
            assert_eq!(data["delivery"], "unknown");
            assert_eq!(data["stage"], "reply");

            let tapped = call("iphone_tap", json!({"x":1,"y":2,"then":"screenshot"})).await;
            assert_ne!(tapped.is_error, Some(true));
            let data = tapped.structured_content.unwrap();
            assert_eq!(data["delivery"], "sent");
            assert_eq!(data["stage"], "readback");
            assert_eq!(data["readback"], json!({"settle":"ok", "screen":"failed"}));

            let listed = call("iphone_tap", json!({"x":1,"y":2,"then":"elements"})).await;
            assert_ne!(listed.is_error, Some(true));
            let data = listed.structured_content.unwrap();
            assert_eq!(data["delivery"], "sent");
            assert_eq!(data["readback"], json!({"settle":"failed", "screen":"ok"}));
            assert_eq!(data["matched_count"], 3);
            assert_eq!(data["truncated"], true);

            let text = json!({"text":"x"});
            let typed = call("iphone_type_text", text.clone()).await.structured_content.unwrap();
            assert_eq!(typed["delivery"], "sent");
            assert_eq!(typed["readback"]["field"], "unverified");
            let mismatch = call("iphone_type_text", text.clone()).await;
            assert_eq!(mismatch.is_error, Some(true));
            let data = mismatch.structured_content.unwrap();
            assert_eq!(data["delivery"], "sent");
            assert_eq!(data["readback"]["field"], "failed");

            let missed = call("iphone_tap_element", text).await;
            assert_ne!(missed.is_error, Some(true));
            assert_eq!(missed.structured_content.unwrap()["delivery"], "not_sent");
        };
        tokio::time::timeout(Duration::from_secs(4), async { tokio::join!(daemon, actions) }).await.unwrap();
        std::fs::remove_file(socket).unwrap();
    }

    fn has_cjk(s: &str) -> bool {
        s.chars().any(|c| matches!(c, '\u{3000}'..='\u{303f}' | '\u{4e00}'..='\u{9fff}' | '\u{ff00}'..='\u{ffef}'))
    }

    /// With IPHONE_CONTROL_LANG=en nothing the agent reads up front is Chinese: instructions, tool
    /// descriptions, titles and parameter docs (only examples of Chinese input may stay)
    #[test]
    fn delivery_hints_do_not_repeat_punctuation_or_existing_screen_guidance() {
        ictl_l10n::with_lang(ictl_l10n::Lang::En, || {
            let mut message = "Focus changed. Inspect the screen.".to_owned();
            add_delivery_hint(&mut message, Delivery::Sent);
            assert_eq!(message, "Focus changed. Inspect the screen. Input was sent.");
        });
        ictl_l10n::with_lang(ictl_l10n::Lang::Zh, || {
            let mut message = "请求超时,请先截图确认。".to_owned();
            add_delivery_hint(&mut message, Delivery::Unknown);
            assert_eq!(message, "请求超时,请先截图确认。输入可能已送达。");
        });
    }

    #[test]
    fn english_tool_definitions_have_no_chinese() {
        use ictl_l10n::{Lang, with_lang};
        with_lang(Lang::En, || {
            assert!(!has_cjk(instructions()), "{}", instructions());
            for t in tools_with(true) {
                let json = serde_json::to_string(&t).unwrap();
                assert!(!has_cjk(&json), "{}: {json}", t.name);
            }
        });
        with_lang(Lang::Zh, || assert!(has_cjk(instructions())));
    }

    /// scroll 是可选的:不给就和以前一样只看当前屏幕;方向只认 down / up,次数 1 到上限
    #[test]
    fn tap_element_scroll_search_is_optional_and_checked() {
        let req = |v: Value| to_request("iphone_tap_element", &args(v));
        let plain = req(json!({"text": "隐私"})).unwrap();
        assert_eq!(plain, Request::TapElement { text: "隐私".into(), index: 0, max_age_ms: 0, scroll: None });
        let down = req(json!({"text": "隐私", "scroll": "down"})).unwrap();
        let s = |down, max| Some(protocol::ScrollSearch { down, max });
        assert_eq!(down, Request::TapElement { text: "隐私".into(), index: 0, max_age_ms: 0, scroll: s(true, 8) });
        let up = req(json!({"text": "隐私", "scroll": "up", "max_scrolls": 3})).unwrap();
        assert_eq!(up, Request::TapElement { text: "隐私".into(), index: 0, max_age_ms: 0, scroll: s(false, 3) });
        for bad in [
            json!({"text": "x", "scroll": "left"}),
            json!({"text": "x", "scroll": "down", "max_scrolls": 0}),
            json!({"text": "x", "scroll": "down", "max_scrolls": 16}),
            json!({"text": "x", "scroll": true}),
        ] {
            assert!(req(bad).is_err());
        }
        // reused in a batch, the scroll search stays
        let steps = batch_steps(&args(
            json!({"reuse_elements": true, "steps": [{"tool": "iphone_tap_element", "text": "x", "scroll": "down"}]}),
        ));
        assert!(matches!(&steps.unwrap()[0], Step::Request { req: Request::TapElement { scroll: Some(_), .. }, .. }));
    }

    /// hold defaults to 0 (a plain swipe, same request as before) and is passed through when given
    #[test]
    fn swipe_hold_defaults_to_zero() {
        let plain = to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4}))).unwrap();
        assert_eq!(plain, Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 });
        let held = json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4, "seconds": 0.7, "hold": 0.6});
        let held = to_request("iphone_swipe", &args(held)).unwrap();
        assert_eq!(held, Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.7, hold: 0.6 });
        assert!(
            to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4, "hold": "x"})))
                .is_err()
        );
    }

    /// one_gesture 把全是坐标点击的批量并成一个 TapSequence;混了别的步骤就报错,一步都不执行
    #[test]
    fn one_gesture_turns_taps_into_one_sequence() {
        let steps = batch_steps(&args(json!({"one_gesture": true, "steps": [
            {"tool": "iphone_tap", "x": 60, "y": 548},
            {"tool": "iphone_tap", "x": 23, "y": 548.5}
        ]})));
        let req = Request::TapSequence { points: vec![(60.0, 548.0), (23.0, 548.5)] };
        assert_eq!(steps, Ok(vec![Step::Request { tool: "iphone_tap".into(), req }]));
        for other in [
            json!({"tool": "iphone_tap_element", "text": "1"}),
            json!({"tool": "wait", "seconds": 0.1}),
            json!({"tool": "iphone_type_text", "text": "1"}),
        ] {
            let steps = json!({"one_gesture": true, "steps": [{"tool": "iphone_tap", "x": 1, "y": 2}, other]});
            assert!(batch_steps(&args(steps)).is_err());
        }
        // without it a batch of taps stays step by step
        let steps = batch_steps(&args(json!({"steps": [{"tool": "iphone_tap", "x": 1, "y": 2}]}))).unwrap();
        assert_eq!(
            steps,
            vec![Step::Request { tool: "iphone_tap".into(), req: Request::Tap { x: 1.0, y: 2.0, count: 1 } }]
        );
        // a step's count can't go into one gesture: an error, not one tap
        let steps = json!({"one_gesture": true, "steps": [{"tool": "iphone_tap", "x": 1, "y": 2, "count": 2}]});
        assert!(batch_steps(&args(steps)).is_err());
    }

    /// iphone_tap 不带 count 是点一下;count 要是 1 到 MAX_TAP_COUNT 的整数,给错了报错、不点,不悄悄改成 1
    #[test]
    fn tap_count_defaults_to_one_and_rejects_out_of_range() {
        let tap = |v: Value| to_request("iphone_tap", &args(v));
        assert_eq!(tap(json!({"x": 1, "y": 2})), Ok(Request::Tap { x: 1.0, y: 2.0, count: 1 }));
        assert_eq!(tap(json!({"x": 1, "y": 2, "count": 5})), Ok(Request::Tap { x: 1.0, y: 2.0, count: 5 }));
        for bad in [json!(0), json!(11), json!(2.5), json!(-1), json!("two")] {
            assert!(tap(json!({"x": 1, "y": 2, "count": bad})).is_err(), "{bad}");
        }
        // a batch step takes it too
        let steps = batch_steps(&args(json!({"steps": [{"tool": "iphone_tap", "x": 1, "y": 2, "count": 2}]})));
        let req = Request::Tap { x: 1.0, y: 2.0, count: 2 };
        assert_eq!(steps, Ok(vec![Step::Request { tool: "iphone_tap".into(), req }]));
    }

    #[test]
    fn scroll_collection_is_bounded_and_changes_the_viewport() {
        let req = |v| {
            let args = args(v);
            check_args("iphone_scroll_collect", &args, &[], true)?;
            to_request("iphone_scroll_collect", &args)
        };
        let r =
            req(json!({"scroll": "up", "max_scrolls": 1, "filter": "Cell", "until": "General", "limit": 3})).unwrap();
        assert_eq!(
            r,
            Request::ScrollCollect(protocol::ScrollCollect {
                down: false,
                max_scrolls: 1,
                filter: "Cell".into(),
                until: "General".into(),
                limit: 3,
                x: None,
            })
        );
        // an iPad split view's sidebar
        let Request::ScrollCollect(scan) = req(json!({"x": 160})).unwrap() else { panic!() };
        assert_eq!(scan.x, Some(160.0));
        assert!(r.is_device_action());
        assert!(req(json!({})).is_ok());
        for bad in [
            json!({"scroll":"left"}),
            json!({"max_scrolls":0}),
            json!({"max_scrolls":16}),
            json!({"limit":0}),
            json!({"limit":151}),
            json!({"limit":2.5}),
            json!({"x":-1}),
            json!({"x":"sidebar"}),
            json!({"max_scroll":2}),
        ] {
            assert!(req(bad.clone()).is_err(), "{bad}");
        }
        let result = to_result(Response::text_data(
            "row limit reached",
            Default::default(),
            Some(protocol::Data::ScrollCollection {
                stop_reason: protocol::ScrollStop::Limit,
                scrolls: 2,
                rows: vec![protocol::ScrollRow {
                    kind: "Cell".into(),
                    label: "One".into(),
                    name: "".into(),
                    value: "".into(),
                }],
                matched: None,
            }),
        ));
        let data = result.structured_content.unwrap();
        assert_eq!(data["stop_reason"], "limit");
        assert_eq!(data["rows"][0]["label"], "One");
        assert!(data["matched"].is_null());
    }

    #[test]
    fn alert_check_after_actions_but_not_buttons_or_reads() {
        assert!(wants_alert_check(&Request::Swipe { from: (0.0, 0.0), to: (1.0, 1.0), seconds: 0.3, hold: 0.0 }));
        assert!(wants_alert_check(&Request::LaunchApp { app: "微信".into() }));
        assert!(!wants_alert_check(&Request::PressButton { button: "home".into() }));
        assert!(!wants_alert_check(&Request::TerminateApp { app: "微信".into() }));
        assert!(!wants_alert_check(&Request::Status));
    }

    #[test]
    fn toolkit_tools_plus_wait_for_batch_and_devices() {
        let names: Vec<_> = tools().iter().map(|t| t.name.to_string()).collect();
        assert_eq!(names.len(), 18);
        // the developer tools only with IPHONE_CONTROL_DEV_TOOLS
        let dev: Vec<_> = tools_with(true).iter().skip(18).map(|t| t.name.to_string()).collect();
        assert_eq!(dev, DEV_TOOLS);
        assert_eq!(tools_with(false).len(), 18);
        for n in [
            "iphone_status",
            "iphone_screenshot",
            "iphone_scroll_collect",
            "iphone_tap_element",
            "iphone_open_url",
            "iphone_list_devices",
            "iphone_select_device",
        ] {
            assert!(names.contains(&n.to_owned()), "{n}");
        }
        for t in tools_with(true).into_iter().filter(|t| t.name != "iphone_batch") {
            let no_required = t.input_schema.get("required").unwrap().as_array().unwrap().is_empty();
            assert_eq!(to_request(&t.name, &Map::new()).is_ok(), no_required, "{}", t.name);
        }
    }

    /// 注解与工具实际行为一致:看界面的只读,改界面的不是;每个参数都有说明
    #[test]
    fn annotations_and_param_docs() {
        for t in tools_with(true) {
            let a = t.annotations.as_ref().unwrap_or_else(|| panic!("{} 没有注解", t.name));
            assert!(a.title.is_some(), "{}", t.name);
            let probe = || {
                let props = t.input_schema["properties"].as_object().unwrap();
                let mut m = Map::new();
                for k in t.input_schema["required"].as_array().unwrap() {
                    let k = k.as_str().unwrap();
                    let v = match props[k]["type"].as_str() {
                        Some("number") => json!(1),
                        Some("array") => json!([{"tool": "wait", "seconds": 0}]),
                        _ if k == "button" => json!("home"),
                        _ => json!("x"),
                    };
                    m.insert(k.to_owned(), v);
                }
                m
            };
            let read_only = match to_request(&t.name, &probe()) {
                Ok(r) => !r.is_device_action(),
                Err(_) => false, // iphone_batch
            };
            assert_eq!(a.read_only_hint, Some(read_only), "{}", t.name);
            for (k, v) in t.input_schema["properties"].as_object().unwrap() {
                let d = v["description"].as_str().unwrap_or_default();
                assert!(!d.is_empty(), "{}.{k} 缺少说明", t.name);
            }
        }
    }

    #[test]
    fn defaults_match_toolkit() {
        assert_eq!(
            to_request("iphone_list_elements", &Map::new()).unwrap(),
            Request::ListElements { filter: String::new(), limit: 150, max_age_ms: 0, keep_locked: false }
        );
        assert_eq!(
            to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": 3, "to_y": 4}))).unwrap(),
            Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 }
        );
        assert_eq!(
            to_request("iphone_long_press", &args(json!({"x": 1, "y": 2}))).unwrap(),
            Request::LongPress { x: 1.0, y: 2.0, seconds: 1.0 }
        );
        assert_eq!(
            to_request("iphone_type_text", &args(json!({"text": "设置"}))).unwrap(),
            Request::TypeText { text: "设置".into(), submit: false }
        );
    }

    #[test]
    fn then_is_offered_on_actions_only() {
        let has_then = |name: &str| {
            let t = tools().into_iter().find(|t| t.name == name).unwrap();
            t.input_schema["properties"].get("then").is_some()
        };
        for n in [
            "iphone_tap",
            "iphone_tap_element",
            "iphone_swipe",
            "iphone_type_text",
            "iphone_launch_app",
            "iphone_batch",
        ] {
            assert!(has_then(n), "{n}");
        }
        for n in ["iphone_status", "iphone_screenshot", "iphone_list_elements", "iphone_wait_for"] {
            assert!(!has_then(n), "{n}");
        }
        let then = |v: Value| parse_then(&args(v)).map(|o| o.then);
        assert_eq!(parse_then(&Map::new()).unwrap(), ThenOpts { then: Then::None, settle_ms: THEN_SETTLE_MS });
        assert_eq!(then(json!({"then": "elements", "then_filter": "设置"})).unwrap(), Then::Elements("设置".into()));
        assert_eq!(then(json!({"then": "screenshot"})).unwrap(), Then::Screenshot(None));
        assert_eq!(
            then(json!({"then": "screenshot", "then_region": [0, 295, 430, 460]})).unwrap(),
            Then::Screenshot(Some((0.0, 295.0, 430.0, 460.0)))
        );
        assert!(then(json!({"then": "screenshot", "then_region": [0, 1, 2]})).is_err());
        assert!(then(json!({"then": "screenshot", "then_region": [0, 1, 0, 5]})).is_err());
        assert!(then(json!({"then": "video"})).is_err());
        assert_eq!(parse_then(&args(json!({"then": "screenshot", "then_settle": 5}))).unwrap().settle_ms, 5000);
        assert!(parse_then(&args(json!({"then_settle": 30}))).is_err());
    }

    /// #158: an argument the tool doesn't take is refused, not dropped (a wait for disappearance ran as a wait for
    /// appearance and answered at once)
    #[test]
    fn unknown_arguments_are_refused() {
        // an argument from another version (here: misspelt) is refused and the tool's own are listed
        let e = check_args("iphone_wait_for", &args(json!({"text": "飞行模式", "until": "disappeared"})), &[], true)
            .unwrap_err();
        assert!(e.contains("until") && e.contains("state") && e.contains("timeout"), "{e}");
        assert!(check_args("iphone_wait_for", &args(json!({"text": "x", "state": "disappeared"})), &[], true).is_ok());
        assert!(check_args("iphone_type_text", &args(json!({"text": "", "replace": true})), &[], true).is_ok());
        // misspelt
        assert!(check_args("iphone_type_text", &args(json!({"text": "a", "sumbit": true})), &[], true).is_err());
        // every documented argument, then on an action, null for a default
        assert!(
            check_args(
                "iphone_type_text",
                &args(json!({"text": "a", "submit": true, "then": "elements",
            "then_filter": "x", "then_settle": 1})),
                &[],
                true
            )
            .is_ok()
        );
        assert!(check_args("iphone_list_elements", &args(json!({"limit": null})), &[], true).is_ok());
        // then only where the schema lists it
        assert!(check_args("iphone_screenshot", &args(json!({"then": "elements"})), &[], true).is_err());
        assert!(check_args("iphone_status", &args(json!({})), &[], true).is_ok());
        assert!(check_args("iphone_batch", &args(json!({"steps": [], "then": "screenshot"})), &[], true).is_ok());
        // developer tools and the confirm tool have their own; a tool this server doesn't know is left alone
        assert!(check_args("iphone_web_eval", &args(json!({"expression": "1", "timeout": 2})), &[], true).is_ok());
        assert!(
            check_args(task::CONFIRM_TOOL, &args(json!({"action": "a", "detail": "b", "x": 1})), &[], false).is_err()
        );
        assert!(check_args("no_such_tool", &args(json!({"x": 1})), &[], true).is_ok());
    }

    #[test]
    fn batch_steps_refuse_unknown_arguments() {
        let bad = |step: Value| batch_steps(&args(json!({"steps": [step]}))).unwrap_err();
        let e = bad(json!({"tool": "iphone_wait_for", "text": "x", "until": "disappeared"}));
        assert!(e.contains("第 1 步") && e.contains("until"), "{e}");
        // a step's then would be ignored (the batch's then applies): refused, not dropped
        assert!(bad(json!({"tool": "iphone_tap", "x": 1, "y": 2, "then": "elements"})).contains("then"));
        assert!(bad(json!({"tool": "wait", "seconds": 1, "secs": 2})).contains("secs"));
    }

    #[test]
    fn element_lists_declare_their_output_schema() {
        let tools = standard_tools();
        let declared: Vec<_> = tools.iter().filter(|t| t.output_schema.is_some()).map(|t| &*t.name).collect();
        assert_eq!(declared, ["iphone_list_elements"]);
        let schema = tools.iter().find(|t| t.name == "iphone_list_elements").and_then(|t| t.output_schema.clone());
        assert_eq!(schema.unwrap()["required"], json!(["elements"]));
    }

    #[test]
    fn batch_steps_are_checked_before_anything_runs() {
        let steps = batch_steps(&args(json!({"steps": [
            {"tool": "iphone_tap_element", "text": "One"},
            {"tool": "wait", "seconds": 0.5},
            {"tool": "iphone_tap", "x": 1, "y": 2},
            {"tool": "iphone_wait_for", "text": "完成", "timeout": 3}
        ]})))
        .unwrap();
        assert_eq!(steps.len(), 4);
        assert_eq!(steps[1], Step::Wait(std::time::Duration::from_millis(500)));
        assert_eq!(
            steps[3],
            Step::Request {
                tool: "iphone_wait_for".into(),
                req: Request::WaitFor { text: "完成".into(), timeout_ms: 3000 }
            }
        );
        // 第 2 步参数错:一步都不执行,报出是第几步
        let e = batch_steps(&args(
            json!({"steps": [{"tool": "iphone_tap", "x": 1, "y": 2}, {"tool": "iphone_tap", "x": 1}]}),
        ))
        .unwrap_err();
        assert!(e.contains("第 2 步"), "{e}");
        // 看界面的工具、未知工具、批量套批量都不行
        for bad in ["iphone_screenshot", "iphone_batch", "rm"] {
            assert!(batch_steps(&args(json!({"steps": [{"tool": bad}]}))).is_err(), "{bad}");
        }
        assert!(batch_steps(&args(json!({"steps": [{"tool": "wait", "seconds": 9}]}))).is_err());
        assert!(batch_steps(&args(json!({"steps": []}))).is_err());
        let many: Vec<Value> = (0..21).map(|_| json!({"tool": "wait", "seconds": 0})).collect();
        assert!(batch_steps(&args(json!({"steps": many}))).is_err());
    }

    #[test]
    fn reuse_elements_only_affects_tap_element_steps() {
        let steps = |reuse: bool| {
            batch_steps(&args(json!({"reuse_elements": reuse, "steps": [
                {"tool": "iphone_tap_element", "text": "One"},
                {"tool": "iphone_tap", "x": 1, "y": 2}
            ]})))
            .unwrap()
        };
        assert_eq!(
            steps(true)[0],
            Step::Request {
                tool: "iphone_tap_element".into(),
                req: Request::TapElement { text: "One".into(), index: 0, max_age_ms: BATCH_REUSE_MS, scroll: None }
            }
        );
        assert!(matches!(&steps(false)[0], Step::Request { req: Request::TapElement { max_age_ms: 0, .. }, .. }));
        assert_eq!(steps(true)[1], steps(false)[1]);
    }

    #[test]
    fn missed_tap_texts_are_recognised() {
        // 旧版本守护进程:没有 outcome,只能认中文开头
        assert!(tap_missed("没找到包含 \"x\" 的元素,先用 iphone_list_elements 或截图看看屏幕", None));
        assert!(tap_missed("只匹配到 1 个:\nButton \"x\"", None));
        assert!(!tap_missed("已点击 Button \"x\"", None));
        assert!(tap_missed("被挡住:Button \"x\" 整个被「浮层」盖住,没有点", None));
        assert!(!tap_missed("已点击 Button \"x\";它的中心被「搜索」盖住,改点没被盖住的 (215,848)", None));
        // 新版本:看 outcome,与语言无关
        let missed = protocol::Outcome { tapped_nothing: true, ..Default::default() };
        assert!(tap_missed("Covered: Button \"x\" is completely covered", Some(missed)));
        assert!(!tap_missed("Tapped Button \"Not found\"", Some(protocol::Outcome::default())));
    }

    #[test]
    fn wait_for_timeout_is_bounded() {
        assert_eq!(
            to_request("iphone_wait_for", &args(json!({"text": "x"}))).unwrap(),
            Request::WaitFor { text: "x".into(), timeout_ms: 5000 }
        );
        assert!(to_request("iphone_wait_for", &args(json!({"text": "x", "timeout": 60}))).is_err());
    }

    #[test]
    fn text_replacement_is_forwarded_only_when_requested() {
        let changed = to_request("iphone_type_text", &args(json!({"text": "蓝牙", "replace": true}))).unwrap();
        assert_eq!(serde_json::to_value(changed).unwrap()["op"], "replace_text");
        let default = to_request("iphone_type_text", &args(json!({"text": "蓝牙"}))).unwrap();
        assert_eq!(serde_json::to_value(default).unwrap()["op"], "type_text");
    }

    #[test]
    fn wait_for_accepts_disappeared_and_rejects_unknown_state() {
        let req = to_request("iphone_wait_for", &args(json!({"text": "设置", "state": "disappeared"}))).unwrap();
        assert_eq!(serde_json::to_value(req).unwrap()["op"], "wait_for_disappearance");
        assert!(to_request("iphone_wait_for", &args(json!({"text": "设置", "state": "any"}))).is_err());
    }

    #[test]
    fn element_limit_is_reported_in_structured_content() {
        let reply: Response = serde_json::from_value(json!({
            "kind": "text", "text": "one element, 16 more",
            "data": {
                "type": "elements",
                "elements": [{"type":"Button","label":"设置","x":16,"y":16,"width":44,"height":44}],
                "matched_count": 17, "truncated": true
            }
        }))
        .unwrap();
        let result = serde_json::to_value(to_result(reply)).unwrap();
        assert_eq!(result["structuredContent"]["elements"].as_array().unwrap().len(), 1);
        assert_eq!(result["structuredContent"]["matched_count"], 17);
        assert_eq!(result["structuredContent"]["truncated"], true);
    }

    #[test]
    fn wrong_typed_parameters_are_errors_not_defaults() {
        let args = |v: Value| v.as_object().unwrap().clone();
        assert!(to_request("iphone_tap_element", &args(json!({"text": "设置", "index": -1}))).is_err());
        assert!(to_request("iphone_list_elements", &args(json!({"limit": 1.5}))).is_err());
        assert!(to_request("iphone_swipe", &args(json!({"from_x": 1, "from_y": 2, "to_x": "x", "to_y": 4}))).is_err());
        // 写成字符串的数字照收(toolkit 的 pydantic 也收)
        assert_eq!(
            to_request("iphone_tap_element", &args(json!({"text": "设置", "index": "2"}))).unwrap(),
            Request::TapElement { text: "设置".into(), index: 2, max_age_ms: 0, scroll: None }
        );
        assert_eq!(
            to_request("iphone_list_elements", &args(json!({"limit": null}))).unwrap(),
            Request::ListElements { filter: String::new(), limit: 150, max_age_ms: 0, keep_locked: false }
        );
    }

    #[test]
    fn screenshot_region_needs_all_four() {
        assert_eq!(to_request("iphone_screenshot", &Map::new()).unwrap(), Request::Screenshot { region: None });
        assert_eq!(
            to_request("iphone_screenshot", &args(json!({"x": 0, "y": 55, "width": 430, "height": 40}))).unwrap(),
            Request::Screenshot { region: Some((0.0, 55.0, 430.0, 40.0)) }
        );
        assert_eq!(
            to_request("iphone_screenshot", &args(json!({"x": 10, "y": 10}))).unwrap_err(),
            "放大区域要同时给出 x、y、width、height"
        );
    }

    /// 回归用例 #17:预期内的错误要作为工具错误、带可读文字返回
    #[test]
    fn errors_become_tool_errors_with_text() {
        let r = to_result(Response::error("button 只能是 home / volume_up / volume_down / lock"));
        assert_eq!(r.is_error, Some(true));
        assert!(matches!(&r.content[0], ContentBlock::Text(t) if t.text.contains("button 只能是")));
        let img = to_result(Response::Image { jpeg_base64: "AAAA".into(), caption: "430x932 点".into() });
        assert_eq!(img.content.len(), 2);
        assert!(to_request("iphone_nope", &Map::new()).is_err());
    }
}