Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Phone type_text: switch to ADBKeyBoard for the call when another keyboard is the default
Xiaolei Shie7e181459f93
diff --git a/server/drivers/phone-proxy.test.ts b/server/drivers/phone-proxy.test.tsindex 92438dd14e..281707fb56 100644--- a/server/drivers/phone-proxy.test.ts+++ b/server/drivers/phone-proxy.test.ts@@ -63,6 +63,55 @@ emulator-5554 device product:sdk model:Emulator transport_id:6});});+// Lines cut, in their order, from four `dumpsys input_method` outputs of a Huawei Mate 9 (Android 9). Three are from a switch from the Baidu keyboard to ADBKeyBoard with a search+// field taking input: before `ime set`, right after it (ADBKeyBoard is not bound yet, so the dump has no section for it) and once it shows. The fourth is the home screen with ADBKeyBoard as the keyboard.+const BEFORE_THE_SWITCH = [+ " mCurMethodId=com.baidu.input_huawei/.ImeService",+ " mCurId=com.baidu.input_huawei/.ImeService mHaveConnect=true mBoundToMethod=true",+ " mShowRequested=true mShowExplicitlyRequested=false mShowForced=false mInputShown=true",+ " mServedInputConnectionWrapper=ControlledInputConnectionWrapper{connection=com.android.internal.widget.EditableInputConnection@c49480f finished=false mParentInputMethodManager.mActive=true}",+ "Input method service state for com.baidu.input_huawei.ImeService@ca25e11:",+ " mWindowCreated=true mWindowAdded=true",+ " mWindowVisible=true mWindowWasVisible=true mInShowWindow=false",+ " mExtractedToken=0",+ " mIsInputViewShown=true mStatusIcon=0",+ "Last computed insets:",+ "",+].join("\n");+const SWITCHED_BUT_NOT_BOUND = [+ " mCurMethodId=com.android.adbkeyboard/.AdbIME",+ " mCurId=com.android.adbkeyboard/.AdbIME mHaveConnect=true mBoundToMethod=false",+ " mShowRequested=true mShowExplicitlyRequested=false mShowForced=false mInputShown=true",+ " mServedInputConnectionWrapper=ControlledInputConnectionWrapper{connection=com.android.internal.widget.EditableInputConnection@9c2709c finished=false mParentInputMethodManager.mActive=true}",+ "",+].join("\n");+const SWITCHED_AND_SHOWN = [+ " mCurMethodId=com.android.adbkeyboard/.AdbIME",+ " mCurId=com.android.adbkeyboard/.AdbIME mHaveConnect=true mBoundToMethod=true",+ " mShowRequested=true mShowExplicitlyRequested=false mShowForced=false mInputShown=true",+ " mServedInputConnectionWrapper=ControlledInputConnectionWrapper{connection=com.android.internal.widget.EditableInputConnection@9c2709c finished=false mParentInputMethodManager.mActive=true}",+ "Input method service state for com.android.adbkeyboard.AdbIME@a07f2f1:",+ " mWindowCreated=true mWindowAdded=true",+ " mWindowVisible=true mWindowWasVisible=true mInShowWindow=false",+ " mExtractedToken=0",+ " mIsInputViewShown=true mStatusIcon=0",+ "Last computed insets:",+ "",+].join("\n");+const ON_THE_HOME_SCREEN = [+ " mCurMethodId=com.android.adbkeyboard/.AdbIME",+ " mCurId=com.android.adbkeyboard/.AdbIME mHaveConnect=true mBoundToMethod=true",+ " mShowRequested=false mShowExplicitlyRequested=false mShowForced=false mInputShown=false",+ " mServedInputConnectionWrapper=null",+ "Input method service state for com.android.adbkeyboard.AdbIME@3f66c6f:",+ " mWindowCreated=false mWindowAdded=false",+ " mWindowVisible=false mWindowWasVisible=false mInShowWindow=false",+ " mExtractedToken=0",+ " mIsInputViewShown=false mStatusIcon=0",+ "Last computed insets:",+ "",+].join("\n");+describe("phone MCP text input", () => {const ADB_KEYBOARD = "com.android.adbkeyboard/.AdbIME";const VENDOR_KEYBOARD = "com.baidu.input_huawei/.ImeService";@@ -128,8 +177,8 @@ describe("phone MCP text input", () => {it("asks for ADBKeyBoard and types nothing when another keyboard is chosen", async () => {const phone = device(VENDOR_KEYBOARD);- await expect(typePhoneText(phone.shell, "二手 iPad")).rejects.toThrow("Only letters, digits, spaces and _.,@- can be typed until ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and chosen as the phone's keyboard");- expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"]]);+ await expect(typePhoneText(phone.shell, "二手 iPad")).rejects.toThrow("Only letters, digits, spaces and _.,@- can be typed until ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and switched on in the phone's keyboard list");+ expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"], ["ime", "list", "-s"]]);});const CLEAR = ["am", "broadcast", "-a", "ADB_CLEAR_TEXT", "-p", "com.android.adbkeyboard"];@@ -181,9 +230,9 @@ describe("phone MCP text input", () => {it.each(["二手 iPad", "iPad"])("asks for ADBKeyBoard and changes nothing when another keyboard cannot clear (%s)", async (text) => {const phone = device(VENDOR_KEYBOARD);const refused = typePhoneText(phone.shell, text, true);- await expect(refused).rejects.toThrow("A field can be cleared only when ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and chosen as the phone's keyboard");+ await expect(refused).rejects.toThrow("A field can be cleared only when ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and switched on in the phone's keyboard list");await expect(refused).rejects.toThrow("call type_text without clear and remove the old text with press delete");- expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"]]);+ expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"], ["ime", "list", "-s"]]);});it("sends no text after a clear that failed", async () => {@@ -211,6 +260,77 @@ describe("phone MCP text input", () => {expect(phone.commands).toEqual([]);}});++ describe("with another keyboard as the default", () => {+ const KEYBOARD = ["settings", "get", "secure", "default_input_method"];+ const KEYBOARDS = ["ime", "list", "-s"];+ const SWITCH = ["ime", "set", ADB_KEYBOARD];+ const INPUT_STATE = ["dumpsys", "input_method"];+ const INPUT = ["am", "broadcast", "-a", "ADB_INPUT_B64", "-p", "com.android.adbkeyboard", "--es", "msg", "5LqM5omLIGlQYWQ="];+ const RESTORE = ["ime", "set", VENDOR_KEYBOARD];+ /** A phone whose keyboard list holds `switchedOn`, and whose input state reads as each of `dumps` in turn, then as the last of them. */+ const switching = (dumps: string[], keyboard = VENDOR_KEYBOARD, switchedOn = [VENDOR_KEYBOARD, ADB_KEYBOARD]) => {+ const phone = device(keyboard);+ let reads = 0;+ const shell = async (...command: string[]) => {+ const reply = await phone.shell(...command);+ if (command[0] === "dumpsys") return dumps[Math.min(reads++, dumps.length - 1)];+ return command[1] === "list" ? `${switchedOn.join("\n")}\n` : reply;+ };+ return { commands: phone.commands, shell };+ };++ beforeEach(() => {+ vi.useFakeTimers();+ });+ afterEach(() => {+ vi.useRealTimers();+ });++ it.each([+ { clear: false, sends: [INPUT], says: SENT },+ { clear: true, sends: [INPUT_STATE, CLEAR, INPUT], says: "Asked ADBKeyBoard to clear the field and sent the text. Use read_screen to check that the field holds only this text." },+ ])("switches to ADBKeyBoard to type, then back to the person's keyboard (clear: $clear)", async ({ clear, sends, says }) => {+ const phone = switching([SWITCHED_AND_SHOWN]);+ await expect(typePhoneText(phone.shell, "二手 iPad", clear)).resolves.toBe(says);+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE, ...sends, RESTORE]);+ });++ it.each([+ ["ADBKeyBoard is not in the phone's keyboard list", VENDOR_KEYBOARD, [VENDOR_KEYBOARD]],+ ["the default keyboard is not in that list", "com.example.keys/.Ime", [VENDOR_KEYBOARD, ADB_KEYBOARD]],+ ["the default keyboard is not in that list and its id would run a command in the phone's shell", "a/b;reboot", [VENDOR_KEYBOARD, ADB_KEYBOARD]],+ ["the phone's shell would not read the default keyboard's id as written", "com.x/.Ime$Inner", ["com.x/.Ime$Inner", ADB_KEYBOARD]],+ ])("asks for ADBKeyBoard and switches nothing when %s", async (_case, keyboard, switchedOn) => {+ const phone = switching([SWITCHED_AND_SHOWN], keyboard, switchedOn);+ await expect(typePhoneText(phone.shell, "二手 iPad")).rejects.toThrow(new Error("Only letters, digits, spaces and _.,@- can be typed until ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and switched on in the phone's keyboard list. Ask the person to set it up, or type only those characters."));+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS]);+ });++ it("reads the input state again 200 ms later while ADBKeyBoard is not bound, and types once it shows", async () => {+ const phone = switching([SWITCHED_BUT_NOT_BOUND, SWITCHED_AND_SHOWN]);+ const typed = typePhoneText(phone.shell, "二手 iPad");+ await vi.advanceTimersByTimeAsync(199);+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE]);+ await vi.advanceTimersByTimeAsync(1);+ await expect(typed).resolves.toBe(SENT);+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE, INPUT_STATE, INPUT, RESTORE]);+ });++ it.each([+ ["is not bound", SWITCHED_BUT_NOT_BOUND],+ ["does not show, as on the home screen", ON_THE_HOME_SCREEN],+ ["has not taken over, though the other keyboard shows", BEFORE_THE_SWITCH],+ ])("types nothing and switches back when at the tenth read, 1.8 s after the switch, ADBKeyBoard still %s", async (_state, dump) => {+ const phone = switching([dump]);+ const refused = expect(typePhoneText(phone.shell, "二手 iPad")).rejects.toThrow(new Error("No text field is taking input, so nothing was cleared or typed. Tap the field first, then call type_text again."));+ await vi.advanceTimersByTimeAsync(1_799);+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS, SWITCH, ...Array(9).fill(INPUT_STATE)]);+ await vi.advanceTimersByTimeAsync(1);+ await refused;+ expect(phone.commands).toEqual([KEYBOARD, KEYBOARDS, SWITCH, ...Array(10).fill(INPUT_STATE), RESTORE]);+ });+ });});describe("phone MCP lazy claim gate", () => {@@ -463,6 +583,10 @@ describe("phone MCP tool surface", () => {}});+ it("tells the bot that other text needs ADBKeyBoard switched on, not chosen as the keyboard", () => {+ expect(tool("type_text").description).toContain("other text, such as Chinese, emoji or +, needs the ADBKeyBoard app switched on in the phone's keyboard list.");+ });+it("keeps serial out of a step's fields", () => {expect(tool("batch").inputSchema.properties.steps.items.properties).not.toHaveProperty("serial");});@@ -919,4 +1043,54 @@ describe("phone MCP calls claim before each command sent to the phone", () => {expect(commands.filter((command) => command.includes("input"))).toEqual([]);hold.stop();});++ const KEYBOARD = onPhone("shell", "settings", "get", "secure", "default_input_method");+ const KEYBOARDS = onPhone("shell", "ime", "list", "-s");+ const SWITCH = onPhone("shell", "ime", "set", "com.android.adbkeyboard/.AdbIME");+ const INPUT_STATE = onPhone("shell", "dumpsys", "input_method");+ const RESTORE = onPhone("shell", "ime", "set", "com.baidu.input_huawei/.ImeService");+ const OTHER_TEXT = { text: "二手" };++ /** adb for a phone whose default keyboard is Baidu's, with ADBKeyBoard switched on in its keyboard list and showing as soon as it is set.+ * `during` runs while adb runs each command. */+ function adbOfPhoneWithAnotherKeyboard(during: (command: string) => void) {+ const commands: string[][] = [];+ const adb = async (args: string[]) => {+ commands.push(args);+ const line = args.join(" ");+ during(line);+ if (line.includes("devices -l")) return Buffer.from(DEVICE_LIST);+ if (line.includes("default_input_method")) return Buffer.from("com.baidu.input_huawei/.ImeService\n");+ if (line.includes("ime list -s")) return Buffer.from("com.baidu.input_huawei/.ImeService\ncom.android.adbkeyboard/.AdbIME\n");+ return Buffer.from(line.includes("dumpsys input_method") ? SWITCHED_AND_SHOWN : "");+ };+ return { commands, adb };+ }++ const ONLY_THE_RESTORE = "only the command that puts the person's keyboard back";+ const STEP_REFUSED = `Step 1 failed: A person has taken control of the phone. Do not act until they hand it back. To wait, call read_screen: each phone call waits up to 45 seconds and returns as soon as they hand the phone back. Meanwhile you may work on anything that does not need the phone. Its effect is unknown; call read_screen or screenshot before trying it again. ${EARLIER_AND_LATER}`;+ it.each([+ ["type_text", "nothing more", "its read of the default keyboard", OTHER_TEXT, KEYBOARD, [DEVICES, KEYBOARD], PERSON_TEXT],+ ["type_text", ONLY_THE_RESTORE, "its read of the keyboard list", OTHER_TEXT, KEYBOARDS, [DEVICES, KEYBOARD, KEYBOARDS, RESTORE], PERSON_TEXT],+ ["type_text", ONLY_THE_RESTORE, "the switch to ADBKeyBoard", OTHER_TEXT, SWITCH, [DEVICES, KEYBOARD, KEYBOARDS, SWITCH, RESTORE], PERSON_TEXT],+ ["type_text", ONLY_THE_RESTORE, "the read that finds ADBKeyBoard shown", OTHER_TEXT, INPUT_STATE, [DEVICES, KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE, RESTORE], PERSON_TEXT],+ ["batch", ONLY_THE_RESTORE, "the read that finds ADBKeyBoard shown", { steps: [{ action: "type_text", ...OTHER_TEXT }] }, INPUT_STATE, [DEVICES, KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE, RESTORE], STEP_REFUSED],+ ])("%s sends %s once a person takes the phone during %s", async (name, _more, _moment, args, during, sent, says) => {+ let taken = false;+ const { commands, adb } = adbOfPhoneWithAnotherKeyboard((command) => {+ if (command === during.join(" ")) taken = true;+ });+ const result = await callPhoneTool(name, args, async () => (taken ? REFUSED : { ok: true }), adb);+ expect(result).toEqual({ content: [{ type: "text", text: says }], isError: true });+ expect(commands).toEqual(sent);+ });++ it("type_text says the phone is still on ADBKeyBoard when the command that puts the person's keyboard back fails", async () => {+ const { commands, adb } = adbOfPhoneWithAnotherKeyboard((command) => {+ if (command === RESTORE.join(" ")) throw new Error("error: closed");+ });+ const result = await callPhoneTool("type_text", OTHER_TEXT, async () => ({ ok: true }), adb);+ expect(result).toEqual({ content: [{ type: "text", text: "The phone is still on ADBKeyBoard, which shows no keys. Tell the person to choose their keyboard again in the phone's settings, and call read_screen to see whether the text arrived." }], isError: true });+ expect(commands).toEqual([DEVICES, KEYBOARD, KEYBOARDS, SWITCH, INPUT_STATE, onPhone("shell", "am", "broadcast", "-a", "ADB_INPUT_B64", "-p", "com.android.adbkeyboard", "--es", "msg", "5LqM5omL"), RESTORE]);+ });});diff --git a/server/drivers/phone-proxy.ts b/server/drivers/phone-proxy.tsindex d6ea3a720a..21831a1acb 100644--- a/server/drivers/phone-proxy.ts+++ b/server/drivers/phone-proxy.ts@@ -31,6 +31,8 @@ const PACKAGE = /^[A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)+$/;const COMPONENT = /^([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)+)\/\S+$/;const SAFE_TEXT = /^[A-Za-z0-9 _.,@-]+$/;const ADB_KEYBOARD = "com.android.adbkeyboard/.AdbIME";+// `adb shell` hands its arguments to the phone's shell as one line, so a keyboard id is sent back only when that shell reads each character of it literally.+const SAFE_KEYBOARD_ID = /^[\w.]+\/[\w.]+$/;const KEYCODES: Record<string, string> = {back: "KEYCODE_BACK",delete: "KEYCODE_DEL",@@ -162,24 +164,49 @@ const claimedDevice = (ensureClaim: EnsureClaim, adb: Adb): OnDevice => async (stype Shell = (...command: string[]) => Promise<string>;const deviceShell = (onDevice: OnDevice, serial: string): Shell => async (...command) => (await onDevice(serial, ["shell", ...command])).toString("utf8");-export async function typePhoneText(shell: Shell, text: string, clear = false): Promise<string> {+const NO_FIELD_TEXT = "No text field is taking input, so nothing was cleared or typed. Tap the field first, then call type_text again.";+// `dumpsys input_method` prints the current keyboard's own state under a header that names it (seen on Android 9).+const adbKeyboardShown = (inputState: string) =>+ /\bmIsInputViewShown=(\w+)/.exec(inputState.split("Input method service state for com.android.adbkeyboard.AdbIME@")[1] ?? "")?.[1] === "true";++export async function typePhoneText(shell: Shell, text: string, clear = false, unclaimed: Shell = shell): Promise<string> {if (!text || [...text].length > 256) throw new Error("Android text must be 1 to 256 characters");if (!clear && SAFE_TEXT.test(text)) {await shell("input", "text", text.replaceAll(" ", "%s"));return "Typed text into the focused Android field";}- if ((await shell("settings", "get", "secure", "default_input_method")).trim() !== ADB_KEYBOARD) {+ const keyboard = (await shell("settings", "get", "secure", "default_input_method")).trim();+ if (keyboard === ADB_KEYBOARD) return sendThroughAdbKeyboard(shell, text, clear);+ const switchedOn = (await shell("ime", "list", "-s")).split(/\s+/);+ if (!switchedOn.includes(ADB_KEYBOARD) || !switchedOn.includes(keyboard) || !SAFE_KEYBOARD_ID.test(keyboard)) {throw new Error(clear- ? "A field can be cleared only when ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and chosen as the phone's keyboard. Ask the person to set it up, or call type_text without clear and remove the old text with press delete."- : "Only letters, digits, spaces and _.,@- can be typed until ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and chosen as the phone's keyboard. Ask the person to set it up, or type only those characters.");+ ? "A field can be cleared only when ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and switched on in the phone's keyboard list. Ask the person to set it up, or call type_text without clear and remove the old text with press delete."+ : "Only letters, digits, spaces and _.,@- can be typed until ADBKeyBoard (https://github.com/senzhk/ADBKeyBoard) is installed and switched on in the phone's keyboard list. Ask the person to set it up, or type only those characters.");}+ // Known open risk: a proxy killed between the switch and the restore leaves the phone on ADBKeyBoard.+ try {+ await shell("ime", "set", ADB_KEYBOARD);+ // `ime set` returns before the keyboard is up (ADBKeyBoard took 0.2 to 0.4 s on Android 9), and it stays down while no field takes input.+ for (let read = 1; !adbKeyboardShown(await shell("dumpsys", "input_method")); read += 1) {+ if (read === 10) throw new Error(NO_FIELD_TEXT);+ await new Promise((resolve) => setTimeout(resolve, 200));+ }+ return await sendThroughAdbKeyboard(shell, text, clear);+ } finally {+ await unclaimed("ime", "set", keyboard).catch(() => {+ throw new Error("The phone is still on ADBKeyBoard, which shows no keys. Tell the person to choose their keyboard again in the phone's settings, and call read_screen to see whether the text arrived.");+ });+ }+}++async function sendThroughAdbKeyboard(shell: Shell, text: string, clear: boolean): Promise<string> {// -p delivers each broadcast to ADBKeyBoard alone, not to any other app listening for the action.const toKeyboard = (action: string, ...extras: string[]) => shell("am", "broadcast", "-a", action, "-p", "com.android.adbkeyboard", ...extras);if (clear) {// ADBKeyBoard 2.0 and 2.1 crash on ADB_CLEAR_TEXT when the app in front has no field taking input. Android then dumps// that app's served input connection as null (named ...Wrapper before Android 13); a dump without the line is not refused.if (/\bmServedInputConnection(?:Wrapper)?=(\S+)/.exec(await shell("dumpsys", "input_method"))?.[1] === "null") {- throw new Error("No text field is taking input, so nothing was cleared or typed. Tap the field first, then call type_text again.");+ throw new Error(NO_FIELD_TEXT);}// A cleared field's text goes through ADBKeyBoard too, so the two arrive in order.await toKeyboard("ADB_CLEAR_TEXT");@@ -298,7 +325,7 @@ const SINGLE_TOOLS = [{ name: "tap_text", description: "Tap visible Android text or an accessibility label.", inputSchema: { type: "object", properties: { text: { type: "string" }, exact: { type: "boolean" }, index: { type: "integer", minimum: 0 }, serial: { type: "string" } }, required: ["text"] } },{ name: "tap", description: "Tap Android screen pixel coordinates obtained from read_screen or screenshot.", inputSchema: { type: "object", properties: { x: { type: "number" }, y: { type: "number" }, serial: { type: "string" } }, required: ["x", "y"] } },{ name: "swipe", description: "Swipe the Android screen in a direction.", inputSchema: { type: "object", properties: { direction: { type: "string", enum: ["up", "down", "left", "right"] }, serial: { type: "string" } }, required: ["direction"] } },- { name: "type_text", description: "Type text into the focused Android field. Letters, digits, spaces and _.,@- always work; other text, such as Chinese, emoji or +, needs the ADBKeyBoard keyboard selected on the phone. Set clear to true to empty the field first, which also needs ADBKeyBoard. Never enter passwords or payment details. A one-time code only for a sign-in the person asked for, into the app it was sent for.", inputSchema: { type: "object", properties: { text: { type: "string", maxLength: 256 }, clear: { type: "boolean" }, serial: { type: "string" } }, required: ["text"] } },+ { name: "type_text", description: "Type text into the focused Android field. Letters, digits, spaces and _.,@- always work; other text, such as Chinese, emoji or +, needs the ADBKeyBoard app switched on in the phone's keyboard list. Set clear to true to empty the field first, which also needs ADBKeyBoard. Never enter passwords or payment details. A one-time code only for a sign-in the person asked for, into the app it was sent for.", inputSchema: { type: "object", properties: { text: { type: "string", maxLength: 256 }, clear: { type: "boolean" }, serial: { type: "string" } }, required: ["text"] } },{ name: "press", description: "Press an Android navigation or keyboard key. wakeup turns the screen on and sleep turns it off.", inputSchema: { type: "object", properties: { key: { type: "string", enum: Object.keys(KEYCODES) }, serial: { type: "string" } }, required: ["key"] } },] as const;@@ -510,8 +537,8 @@ async function wait(requested: unknown): Promise<string> {return seconds < Number(requested) ? `Waited ${seconds} s (a wait is at most ${MAX_WAIT_SECONDS} s)` : `Waited ${seconds} s`;}-const actOn = (onDevice: OnDevice) => async (serial: string, { action, ...fields }: PhoneStep): Promise<string> => {- const [first] = (await callToolOn(onDevice, serial, action, fields)).content;+const actOn = (onDevice: OnDevice, unclaimed: OnDevice) => async (serial: string, { action, ...fields }: PhoneStep): Promise<string> => {+ const [first] = (await callToolOn(onDevice, unclaimed, serial, action, fields)).content;return first.type === "text" ? first.text : "";};@@ -553,15 +580,18 @@ async function callTool(name: string, args: Json, adb: Adb, onDevice: OnDevice,const devices = available ? await usbDevices(adb) : [];return textResult(JSON.stringify({ available, devices }, null, 2));}+ // Security: this sends a command without asking for the phone claim, so a person's hold does not stop it.+ // Only type_text's keyboard restore may use it: it puts back what that call changed.+ const unclaimed: OnDevice = (serial, command) => adb(["-s", serial, ...command]);if (name === "batch") {const steps = parseSteps(args.steps);- return runSteps(await readySerial(adb, args.serial), steps, ensureClaim, actOn(onDevice));+ return runSteps(await readySerial(adb, args.serial), steps, ensureClaim, actOn(onDevice, unclaimed));}const serial = await readySerial(adb, args.serial);- return callToolOn(onDevice, serial, name, args);+ return callToolOn(onDevice, unclaimed, serial, name, args);}-async function callToolOn(onDevice: OnDevice, serial: string, name: string, args: Json): Promise<ToolResult> {+async function callToolOn(onDevice: OnDevice, unclaimed: OnDevice, serial: string, name: string, args: Json): Promise<ToolResult> {if (name === "read_screen") return textResult(nodeLines(await readNodes(onDevice, serial)));if (name === "screenshot") {const png = await onDevice(serial, ["exec-out", "screencap", "-p"], true);@@ -618,7 +648,7 @@ async function callToolOn(onDevice: OnDevice, serial: string, name: string, argsawait onDevice(serial, ["shell", "input", "swipe", ...point.map((value) => String(Math.round(value))), "260"]);return textResult(`Swiped ${direction}`);}- if (name === "type_text") return textResult(await typePhoneText(deviceShell(onDevice, serial), String(args.text ?? ""), args.clear === true));+ if (name === "type_text") return textResult(await typePhoneText(deviceShell(onDevice, serial), String(args.text ?? ""), args.clear === true, deviceShell(unclaimed, serial)));if (name === "press") {const key = String(args.key ?? "").toLowerCase();if (!KEYCODES[key]) throw new Error("Unsupported Android key");