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: refuse to clear when no field is taking input
Xiaolei Shi7bff4d5fc0f6
diff --git a/apps/docs/content/docs/mobile/android-control.mdx b/apps/docs/content/docs/mobile/android-control.mdxindex 9558026edb..00f60d6979 100644--- a/apps/docs/content/docs/mobile/android-control.mdx+++ b/apps/docs/content/docs/mobile/android-control.mdx@@ -23,4 +23,4 @@ A bot can turn the screen on and off. It never unlocks a PIN, pattern, or passwoThe bot types letters, digits, spaces, and `_.,@-` through Android's own input. For other text, such as Chinese, emoji, or `+`, install [ADBKeyBoard](https://github.com/senzhk/ADBKeyBoard) on the phone and choose it as the keyboard. ADBKeyBoard shows no on-screen keyboard, so switch back to your usual keyboard when you type on the phone yourself. It also takes typing from any app on the phone, not only from OpenMausBot, so use it only on a phone whose apps you trust.-With ADBKeyBoard 2.0 or later chosen as the keyboard, the bot can also empty a field before it types, for example to replace the words in a search box. With another keyboard it has to remove old text one character at a time.+With ADBKeyBoard 2.0 or later chosen as the keyboard, the bot can also empty a field before it types, for example to replace the words in a search box. It taps the field first. With another keyboard it has to remove old text one character at a time.diff --git a/server/drivers/phone-proxy.test.ts b/server/drivers/phone-proxy.test.tsindex 4afd833322..0fbcd07274 100644--- a/server/drivers/phone-proxy.test.ts+++ b/server/drivers/phone-proxy.test.ts@@ -55,11 +55,14 @@ describe("phone MCP text input", () => {const ADB_KEYBOARD = "com.android.adbkeyboard/.AdbIME";const VENDOR_KEYBOARD = "com.baidu.input_huawei/.ImeService";const SENT = "Sent the text to ADBKeyBoard. Use read_screen to check that it reached the field.";- const device = (keyboard: string) => {+ // Lines of `dumpsys input_method` on a Huawei Mate 9 (Android 9): a text field taking input, and the home screen.+ const FIELD = " mServedInputConnectionWrapper=ControlledInputConnectionWrapper{connection=com.android.internal.widget.EditableInputConnection@c54836f finished=false mParentInputMethodManager.mActive=true}\n";+ const NO_FIELD = " mServedInputConnectionWrapper=null\n";+ const device = (keyboard: string, inputState = FIELD) => {const commands: string[][] = [];const shell = async (...command: string[]) => {commands.push(command);- return command[0] === "settings" ? `${keyboard}\n` : "";+ return command[0] === "settings" ? `${keyboard}\n` : command[0] === "dumpsys" ? inputState : "";};return { commands, shell };};@@ -106,11 +109,24 @@ describe("phone MCP text input", () => {await expect(typePhoneText(phone.shell, text, true)).resolves.toBe("Asked ADBKeyBoard to clear the field and sent the text. Use read_screen to check that the field holds only this text.");expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"],+ ["dumpsys", "input_method"],CLEAR,["am", "broadcast", "-a", "ADB_INPUT_B64", "-p", "com.android.adbkeyboard", "--es", "msg", base64],]);});+ it("clears nothing and types nothing when no field is taking input", async () => {+ const phone = device(ADB_KEYBOARD, NO_FIELD);+ await expect(typePhoneText(phone.shell, "二手", true)).rejects.toThrow("No text field is taking input, so nothing was cleared or typed. Tap the field first, then call type_text again.");+ expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"], ["dumpsys", "input_method"]]);+ });++ it("still clears on a phone whose input state it cannot read", async () => {+ const phone = device(ADB_KEYBOARD, "");+ await expect(typePhoneText(phone.shell, "二手", true)).resolves.toContain("Asked ADBKeyBoard to clear the field");+ expect(phone.commands.map((command) => command[3])).toEqual(["default_input_method", undefined, "ADB_CLEAR_TEXT", "ADB_INPUT_B64"]);+ });+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);@@ -124,10 +140,11 @@ describe("phone MCP text input", () => {const shell = async (...command: string[]) => {commands.push(command);if (command[0] === "settings") return `${ADB_KEYBOARD}\n`;+ if (command[0] === "dumpsys") return FIELD;throw new Error("error: closed");};await expect(typePhoneText(shell, "二手", true)).rejects.toThrow("error: closed");- expect(commands).toEqual([["settings", "get", "secure", "default_input_method"], CLEAR]);+ expect(commands).toEqual([["settings", "get", "secure", "default_input_method"], ["dumpsys", "input_method"], CLEAR]);});it("offers clear as an optional plain boolean beside the text", () => {diff --git a/server/drivers/phone-proxy.ts b/server/drivers/phone-proxy.tsindex 3f62f17044..30fe46fbdb 100644--- a/server/drivers/phone-proxy.ts+++ b/server/drivers/phone-proxy.ts@@ -157,11 +157,18 @@ export async function typePhoneText(shell: Shell, text: string, clear = false):}// -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);- // ADB_CLEAR_TEXT exists from ADBKeyBoard 2.0; a cleared field's text then goes the same way so the two arrive in order.- if (clear) await toKeyboard("ADB_CLEAR_TEXT");+ if (clear) {+ // ADBKeyBoard 2.0 and 2.1 crash on ADB_CLEAR_TEXT when the app in front has no field taking input.+ // Android 9 then dumps that app's mServedInputConnectionWrapper as null; 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.");+ }+ // A cleared field's text goes through ADBKeyBoard too, so the two arrive in order.+ await toKeyboard("ADB_CLEAR_TEXT");+ }// ADBKeyBoard reads msg as Base64 UTF-8; Base64 also keeps the text out of the device shell's syntax.await toKeyboard("ADB_INPUT_B64", "--es", "msg", Buffer.from(text, "utf8").toString("base64"));- // ADBKeyBoard drops a broadcast when no field has focus, and the broadcast reports success either way.+ // ADBKeyBoard drops the text when no field has focus, and the broadcast reports success either way.return clear? "Asked ADBKeyBoard to clear the field and sent the text. Use read_screen to check that the field holds only this text.": "Sent the text to ADBKeyBoard. Use read_screen to check that it reached the field.";