Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Phone type_text: empty the field first when clear is set

Xiaolei Shi162dd09c1f57
diff --git a/apps/docs/content/docs/mobile/android-control.mdx b/apps/docs/content/docs/mobile/android-control.mdx
index 1868d26981..9558026edb 100644
--- a/apps/docs/content/docs/mobile/android-control.mdx
+++ b/apps/docs/content/docs/mobile/android-control.mdx
@@ -22,3 +22,5 @@ A bot can turn the screen on and off. It never unlocks a PIN, pattern, or passwo
## Typing other languages
The 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.
diff --git a/server/drivers/phone-proxy.test.ts b/server/drivers/phone-proxy.test.ts
index 7175d7a6ce..6fed44c23b 100644
--- a/server/drivers/phone-proxy.test.ts
+++ b/server/drivers/phone-proxy.test.ts
@@ -99,6 +99,39 @@ describe("phone MCP text input", () => {
expect(phone.commands).toEqual([["settings", "get", "secure", "default_input_method"]]);
});
+ const CLEAR = ["am", "broadcast", "-a", "ADB_CLEAR_TEXT", "-p", "com.android.adbkeyboard"];
+
+ it.each([["二手 iPad", "5LqM5omLIGlQYWQ="], ["iPad mini_6", "aVBhZCBtaW5pXzY="]])("clears the field through ADBKeyBoard, then sends the text the same way (%s)", async (text, base64) => {
+ const phone = device(ADB_KEYBOARD);
+ 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"],
+ CLEAR,
+ ["am", "broadcast", "-a", "ADB_INPUT_B64", "-p", "com.android.adbkeyboard", "--es", "msg", base64],
+ ]);
+ });
+
+ it.each(["二手 iPad", "iPad"])("asks for ADBKeyBoard and changes nothing when another keyboard cannot clear (%s)", async (text) => {
+ const phone = device(VENDOR_KEYBOARD);
+ await expect(typePhoneText(phone.shell, text, true)).rejects.toThrow("A field can be cleared only when 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"]]);
+ });
+
+ it("sends no text after a clear that failed", async () => {
+ const commands: string[][] = [];
+ const shell = async (...command: string[]) => {
+ commands.push(command);
+ if (command[0] === "settings") return `${ADB_KEYBOARD}\n`;
+ throw new Error("error: closed");
+ };
+ await expect(typePhoneText(shell, "二手", true)).rejects.toThrow("error: closed");
+ expect(commands).toEqual([["settings", "get", "secure", "default_input_method"], CLEAR]);
+ });
+
+ it("offers clear as a plain boolean beside the text", () => {
+ expect(TOOLS.find((tool) => tool.name === "type_text")?.inputSchema.properties).toMatchObject({ clear: { type: "boolean" } });
+ });
+
it.each(["", "a".repeat(257), "😀".repeat(257)])("refuses empty or over-long text without touching the phone", async (text) => {
const phone = device(ADB_KEYBOARD);
await expect(typePhoneText(phone.shell, text)).rejects.toThrow("Android text must be 1 to 256 characters");
diff --git a/server/drivers/phone-proxy.ts b/server/drivers/phone-proxy.ts
index 12157f8340..3f62f17044 100644
--- a/server/drivers/phone-proxy.ts
+++ b/server/drivers/phone-proxy.ts
@@ -144,20 +144,27 @@ async function onDevice(serial: string, args: string[], binary = false) {
type Shell = (...command: string[]) => Promise<string>;
const deviceShell = (serial: string): Shell => async (...command) => (await onDevice(serial, ["shell", ...command])).toString("utf8");
-export async function typePhoneText(shell: Shell, text: string): Promise<string> {
+export async function typePhoneText(shell: Shell, text: string, clear = false): Promise<string> {
if (!text || [...text].length > 256) throw new Error("Android text must be 1 to 256 characters");
- if (SAFE_TEXT.test(text)) {
+ 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) {
- throw new Error("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.");
+ 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.");
}
+ // -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");
// ADBKeyBoard reads msg as Base64 UTF-8; Base64 also keeps the text out of the device shell's syntax.
- // -p delivers it to ADBKeyBoard alone, not to any other app listening for the action.
- await shell("am", "broadcast", "-a", "ADB_INPUT_B64", "-p", "com.android.adbkeyboard", "--es", "msg", Buffer.from(text, "utf8").toString("base64"));
- // ADBKeyBoard drops the text when no field has focus, and the broadcast reports success either way.
- return "Sent the text to ADBKeyBoard. Use read_screen to check that it reached the field.";
+ 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.
+ 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.";
}
function normalize(value: string) {
@@ -268,7 +275,7 @@ export const TOOLS = [
{ name: "tap_text", description: "Tap visible Android text or an accessibility label, then use read_screen to verify.", 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, then use read_screen to verify.", 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. 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 }, 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 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: "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;
@@ -370,7 +377,7 @@ async function callTool(name: string, args: Json): Promise<ToolResult> {
await 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(serial), String(args.text ?? "")));
+ if (name === "type_text") return textResult(await typePhoneText(deviceShell(serial), String(args.text ?? ""), args.clear === true));
if (name === "press") {
const key = String(args.key ?? "").toLowerCase();
if (!KEYCODES[key]) throw new Error("Unsupported Android key");