Skip to content

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

43.9 KB
Raw Blame
// First-party Android phone MCP server: physical USB devices only (no emulators, wireless or network ADB).
import { spawn } from "node:child_process";
import { accessSync, constants, existsSync } from "node:fs";
import { delimiter, join } from "node:path";
import { homedir } from "node:os";

import { PHONE_STEP_TOOLS, type PhoneStepTool } from "../../shared/phone-steps.ts";
import { CONTROL_ACT_WITHIN_MS, CONTROL_WAIT_MS } from "../control-client.ts";
import { readStdioLines } from "../stdio-lines.ts";

type Json = Record<string, unknown>;
type Device = { serial: string; state: string; model: string; connection: "usb" | "network" | "emulator" };
type UiNode = { text: string; description: string; id: string; className: string; bounds: [number, number, number, number] };
type ClaimOutcome = { ok: true } | { ok: false; message: string };
/** `midCall`: the call has already claimed the phone once, so it has no time left to wait for a person to hand it back. */
type EnsureClaim = (midCall?: boolean) => Promise<ClaimOutcome>;
type Adb = typeof runAdb;
type OnDevice = (serial: string, args: string[], binary?: boolean) => Promise<Buffer>;

const MAX_ADB_OUTPUT = 16 * 1024 * 1024;
const PHONE_BUSY_TEXT = "Another thread is using the phone. This call was not performed. Pause phone work until that thread finishes, then read the screen again before acting.";
const PHONE_PERSON_TEXT = "A person has taken control of the phone, so this call was NOT performed. Do not act until they hand it back. " +
  `To wait, call read_screen: each phone call waits up to ${CONTROL_WAIT_MS / 1000} seconds and returns as soon as they hand the phone back. ` +
  "Meanwhile you may work on anything that does not need the phone.";
const PHONE_HANDED_BACK_TEXT = "The person has handed the phone back. This call was NOT performed: the screen may have changed. Call read_screen, then continue the task.";
const PHONE_WAITED_TEXT = "The phone is free now, but this call had to wait for it, so it was NOT performed. " +
  "Call again: the next call goes through at once. If you meant to tap or type from an earlier read_screen, call read_screen again first: the screen may have changed while you waited.";
/** Past the harness's longest answer, so a claim it still legitimately holds is never cut off. */
const CLAIM_TIMEOUT_MS = CONTROL_WAIT_MS + 10_000;
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",
  down: "KEYCODE_DPAD_DOWN",
  enter: "KEYCODE_ENTER",
  escape: "KEYCODE_BACK",
  home: "KEYCODE_HOME",
  left: "KEYCODE_DPAD_LEFT",
  recent: "KEYCODE_APP_SWITCH",
  return: "KEYCODE_ENTER",
  right: "KEYCODE_DPAD_RIGHT",
  sleep: "KEYCODE_SLEEP",
  space: "KEYCODE_SPACE",
  tab: "KEYCODE_TAB",
  up: "KEYCODE_DPAD_UP",
  wakeup: "KEYCODE_WAKEUP",
};
const KNOWN_PACKAGES: Record<string, string> = {
  skyscanner: "net.skyscanner.android.main",
  uber: "com.ubercab",
};

function executableName(platform = process.platform) {
  return platform === "win32" ? "adb.exe" : "adb";
}

export function resolveAdbPath(env: NodeJS.ProcessEnv = process.env, platform = process.platform): string | null {
  const executable = executableName(platform);
  const home = homedir();
  const candidates = [
    env.OMB_ADB_PATH,
    env.OMB_RESOURCES_PATH && join(env.OMB_RESOURCES_PATH, "android-platform-tools", platform, executable),
    ...(env.PATH ?? "").split(delimiter).map((entry) => entry && join(entry, executable)),
    platform === "darwin" && join(home, "Library/Android/sdk/platform-tools/adb"),
    platform === "darwin" && "/opt/homebrew/bin/adb",
    platform === "darwin" && "/usr/local/bin/adb",
    platform === "linux" && join(home, "Android/Sdk/platform-tools/adb"),
  ].filter((candidate): candidate is string => Boolean(candidate));
  for (const candidate of candidates) {
    try {
      accessSync(candidate, constants.X_OK);
      return candidate;
    } catch {}
  }
  return null;
}

async function runAdb(args: string[], options: { binary?: boolean; timeoutMs?: number } = {}): Promise<Buffer> {
  const adb = resolveAdbPath();
  if (!adb) throw new Error("Android platform tools are unavailable. Reopen OpenMausBot or install adb.");
  return new Promise((resolve, reject) => {
    const child = spawn(adb, args, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
    const stdout: Buffer[] = [];
    const stderr: Buffer[] = [];
    let size = 0;
    const timer = setTimeout(() => {
      child.kill();
      reject(new Error("The Android device command timed out"));
    }, options.timeoutMs ?? 12_000);
    child.stdout.on("data", (chunk: Buffer) => {
      size += chunk.length;
      if (size > MAX_ADB_OUTPUT) child.kill();
      else stdout.push(chunk);
    });
    child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk));
    child.on("error", (error) => {
      clearTimeout(timer);
      reject(error);
    });
    child.on("close", (code) => {
      clearTimeout(timer);
      if (size > MAX_ADB_OUTPUT) return reject(new Error("The Android device returned too much data"));
      if (code !== 0) return reject(new Error(Buffer.concat(stderr).toString("utf8").trim() || `adb exited ${code}`));
      resolve(Buffer.concat(stdout));
    });
  });
}

export function parseDevices(output: string): Device[] {
  const devices: Device[] = [];
  for (const raw of output.split(/\r?\n/)) {
    const line = raw.trim();
    if (!line || line.startsWith("List of devices attached") || line.startsWith("* daemon")) continue;
    const [serial, state, ...fields] = line.split(/\s+/);
    if (!serial || !state) continue;
    const connection = serial.startsWith("emulator-")
      ? "emulator"
      : serial.includes(":") || serial.startsWith("adb-")
        ? "network"
        : "usb";
    const properties = Object.fromEntries(fields.flatMap((field) => {
      const split = field.indexOf(":");
      return split > 0 ? [[field.slice(0, split), field.slice(split + 1)]] : [];
    }));
    devices.push({
      serial,
      state,
      connection,
      model: String(properties.model ?? properties.product ?? "Android device").replaceAll("_", " "),
    });
  }
  return devices;
}

async function usbDevices(adb: Adb) {
  if (!resolveAdbPath()) return [];
  return parseDevices((await adb(["devices", "-l"])).toString("utf8")).filter((device) => device.connection === "usb");
}

async function readySerial(adb: Adb, requested?: unknown): Promise<string> {
  const devices = await usbDevices(adb);
  const serial = typeof requested === "string" && requested ? requested : process.env.PH_ANDROID_SERIAL;
  const candidates = serial ? devices.filter((device) => device.serial === serial) : devices;
  if (!candidates.length) throw new Error("No physical USB Android device is connected");
  if (candidates.length > 1 && !serial) throw new Error("Multiple USB Android devices are connected; pass serial");
  const device = candidates[0];
  if (device.state === "unauthorized") throw new Error("Unlock the phone and accept Allow USB debugging");
  if (device.state !== "device") throw new Error(`The Android device is ${device.state}`);
  return device.serial;
}

// A call runs several commands and a person may take the phone between two of them, so each one asks again.
const claimedDevice = (ensureClaim: EnsureClaim, adb: Adb): OnDevice => async (serial, args, binary = false) => {
  const claim = await ensureClaim(true);
  if (!claim.ok) throw new Error(claim.message);
  return adb(["-s", serial, ...args], { binary });
};

type Shell = (...command: string[]) => Promise<string>;
const deviceShell = (onDevice: OnDevice, serial: string): Shell => async (...command) => (await onDevice(serial, ["shell", ...command])).toString("utf8");

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]?.split("Input method service state for ")[0] ?? "")?.[1] === "true";
// The same output says `mInputShown=false` until a field's keyboard is up, 0.5 to 0.8 s after the tap on Android 9.
const noKeyboardUp = (inputState: string) => /\bmInputShown=false\b/.test(inputState);

async function waitForInputState(shell: Shell, ready: (inputState: string) => boolean): Promise<void> {
  for (let read = 1; !ready(await shell("dumpsys", "input_method")); read += 1) {
    if (read === 10) throw new Error(NO_FIELD_TEXT);
    await new Promise((resolve) => setTimeout(resolve, 200));
  }
}

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";
  }
  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 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.");
  }
  // A keyboard switch sent while a tapped field's keyboard is still coming up can leave the field with no keyboard at all (seen on Android 9).
  await waitForInputState(shell, (inputState) => !noKeyboardUp(inputState));
  // 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.
    await waitForInputState(shell, adbKeyboardShown);
    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_FIELD_TEXT);
    }
    // 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 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.";
}

function normalize(value: string) {
  return value.toLowerCase().replace(/[^a-z0-9]/g, "");
}

export function parseLaunchablePackages(output: string): string[] {
  const packages: string[] = [];
  for (const raw of output.split(/\r?\n/)) {
    const match = COMPONENT.exec(raw.trim());
    if (match && !packages.includes(match[1])) packages.push(match[1]);
  }
  return packages;
}

export function findPackages(name: string, packages: string[]) {
  if (PACKAGE.test(name) && packages.includes(name)) return [name];
  const needle = normalize(name);
  const known = KNOWN_PACKAGES[needle];
  if (known && packages.includes(known)) return [known];
  if (needle.length < 4) return [];
  const ranked = [
    (token: string) => token === needle,
    (token: string) => token.startsWith(needle),
    (token: string) => token.includes(needle),
  ];
  for (const matches of ranked) {
    const found = packages.filter((pkg) => pkg.split(".").some((token) => matches(normalize(token))));
    if (found.length) return found;
  }
  return [];
}

export function launchTarget(name: string, packages: string[]) {
  const matches = findPackages(name, packages);
  if (matches.length === 1) return matches[0];
  throw new Error(matches.length
    ? `App name is ambiguous: ${matches.join(", ")}`
    : `No installed launchable app matches ${JSON.stringify(name)}. open_app knows English names and package names; call list_apps without a query and pass the package name.`);
}

async function launchablePackages(onDevice: OnDevice, serial: string) {
  const output = await onDevice(serial, [
    "shell", "cmd", "package", "query-activities", "--brief",
    "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER",
  ]);
  return parseLaunchablePackages(output.toString("utf8"));
}

function decodeXml(value: string) {
  return value
    .replaceAll("&quot;", '"')
    .replaceAll("&apos;", "'")
    .replaceAll("&lt;", "<")
    .replaceAll("&gt;", ">")
    .replaceAll("&amp;", "&");
}

export function parseUiNodes(xml: string): UiNode[] {
  const nodes: UiNode[] = [];
  for (const match of xml.matchAll(/<node\s+([^>]+?)\/?\s*>/g)) {
    const attributes: Record<string, string> = {};
    for (const attribute of match[1].matchAll(/([\w:-]+)="([^"]*)"/g)) {
      attributes[attribute[1]] = decodeXml(attribute[2]);
    }
    const bounds = /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/.exec(attributes.bounds ?? "");
    if (!bounds) continue;
    const text = attributes.text ?? "";
    const description = attributes["content-desc"] ?? "";
    if (!text && !description && !attributes["resource-id"]) continue;
    nodes.push({
      text,
      description,
      id: attributes["resource-id"] ?? "",
      className: attributes.class ?? "",
      bounds: [Number(bounds[1]), Number(bounds[2]), Number(bounds[3]), Number(bounds[4])],
    });
  }
  return nodes;
}

async function readNodes(onDevice: OnDevice, serial: string) {
  const streamed = (await onDevice(serial, ["shell", "uiautomator", "dump", "/dev/tty"])).toString("utf8");
  const streamedStart = streamed.indexOf("<?xml");
  if (streamedStart >= 0) return parseUiNodes(streamed.slice(streamedStart));

  const remotePath = "/data/local/tmp/openmaus-window.xml";
  const saved = (await onDevice(serial, ["shell", `rm -f ${remotePath}; uiautomator dump ${remotePath} >/dev/null && cat ${remotePath}; status=$?; rm -f ${remotePath}; exit $status`])).toString("utf8");
  const savedStart = saved.indexOf("<?xml");
  return parseUiNodes(savedStart >= 0 ? saved.slice(savedStart) : saved);
}

function nodeLines(nodes: UiNode[]) {
  return nodes.slice(0, 250).map((node, index) => {
    const label = [node.text, node.description].filter(Boolean).join(" · ");
    return `${index}: ${JSON.stringify(label || node.id)} bounds=${node.bounds.join(",")} class=${node.className}`;
  }).join("\n") || "No accessible Android UI text is visible.";
}

const SINGLE_TOOLS = [
  { name: "status", description: "Check physical USB Android devices and USB-debugging authorization before any Android phone task.", inputSchema: { type: "object", properties: {} } },
  { name: "read_screen", description: "Read visible text, accessibility labels, resource ids, and pixel bounds from the connected Android screen. Use it before choosing what to act on. To act and see the result in one call, put the action and read_screen in one batch.", inputSchema: { type: "object", properties: { serial: { type: "string" } } } },
  { name: "screenshot", description: "Capture the connected Android screen when accessibility text is insufficient. Returns a PNG image.", inputSchema: { type: "object", properties: { serial: { type: "string" } } } },
  { name: "list_apps", description: "List installed launchable Android package names, optionally filtered by a human app name. Prefer open_app first.", inputSchema: { type: "object", properties: { query: { type: "string" }, serial: { type: "string" } } } },
  { name: "open_app", description: "Open an installed Android app directly by its English name, such as Uber or Skyscanner, or by its package name. Do not scan the app drawer first.", inputSchema: { type: "object", properties: { name: { type: "string" }, serial: { type: "string" } }, required: ["name"] } },
  { 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. Add duration_ms to hold the tap; about 1000 is a long press.", inputSchema: { type: "object", properties: { x: { type: "number" }, y: { type: "number" }, duration_ms: { type: "integer" }, serial: { type: "string" } }, required: ["x", "y"] } },
  { name: "swipe", description: "Swipe the Android screen in a direction. By default the finger moves half the screen through the centre in 260 ms. To scroll a list, a pane or a picker wheel by a chosen amount, give x and y (where the finger starts), distance (pixels it moves) and duration_ms (longer is slower).", inputSchema: { type: "object", properties: { direction: { type: "string", enum: ["up", "down", "left", "right"] }, x: { type: "number" }, y: { type: "number" }, distance: { type: "number" }, duration_ms: { type: "integer" }, 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 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;

type StepAction = PhoneStepTool | "wait";
export type PhoneStep = Json & { action: StepAction };
type StepRule = { ok: (step: Json) => boolean; needs: string; example?: Json };
type FieldSchema = { type?: string; enum?: readonly string[]; [keyword: string]: unknown };

const STEP_ACTIONS: readonly StepAction[] = [...PHONE_STEP_TOOLS, "wait"];
const MAX_STEPS = 10;
const DEFAULT_WAIT_SECONDS = 2;
const MAX_WAIT_SECONDS = 60;
const STEP_FIELD_NOTES: Record<string, string> = {
  name: "the app's English name or package name",
  text: "the visible text to tap, or the text to type",
  exact: "true to match the whole text, not part of it",
  index: "which match of the text, from 0",
  x: "pixel x from read_screen or screenshot",
  y: "pixel y from read_screen or screenshot",
  direction: "which way to swipe",
  distance: "pixels the finger moves, 50 to 10000, default half the screen",
  duration_ms: "milliseconds a tap is held or a swipe takes, 50 to 2000; without it a tap is not held and a swipe takes 260",
  key: "the key to press",
  seconds: `how long to wait, 0 to ${MAX_WAIT_SECONDS}, default ${DEFAULT_WAIT_SECONDS}`,
};
const STEP_EXAMPLES: Record<StepAction, Json> = {
  open_app: { action: "open_app", name: "Settings" },
  tap_text: { action: "tap_text", text: "Search" },
  tap: { action: "tap", x: 540, y: 1200 },
  swipe: { action: "swipe", direction: "up" },
  type_text: { action: "type_text", text: "iPad mini" },
  press: { action: "press", key: "enter" },
  read_screen: { action: "read_screen" },
  wait: { action: "wait", seconds: 3 },
};
const inPixels = (value: unknown) => Number.isFinite(value) && Number(value) >= 0 && Number(value) <= 10_000;
const given = (value: unknown) => value !== undefined && value !== null && value !== "";
const rounded = (value: unknown) => Math.round(toNumber(value));
const absentOrWithin = (value: unknown, min: number, max: number) => !given(value) || (rounded(value) >= min && rounded(value) <= max);
const DURATION_NEEDS = "duration_ms from 50 to 2000";
const HELD_TAP_EXAMPLE = { x: 540, y: 900, duration_ms: 1000 };
const durationOk = (args: Json) => absentOrWithin(args.duration_ms, 50, 2000);
const SWIPE_NEEDS = `x and y from 0 to 10000, distance from 50 to 10000 and ${DURATION_NEEDS}`;
const SWIPE_EXAMPLE = { direction: "up", x: 300, y: 1400, distance: 400, duration_ms: 800 };
const swipeFieldsOk = (args: Json) => absentOrWithin(args.x, 0, 10_000) && absentOrWithin(args.y, 0, 10_000) && absentOrWithin(args.distance, 50, 10_000) && durationOk(args);
const STEP_RULES: Partial<Record<StepAction, StepRule[]>> = {
  open_app: [{ ok: (step) => String(step.name).trim() !== "", needs: "name, an app's English name or package name" }],
  tap_text: [
    { ok: (step) => String(step.text).trim() !== "", needs: "text that is not blank" },
    { ok: (step) => step.index === undefined || (Number.isInteger(step.index) && Number(step.index) >= 0), needs: "index as a whole number from 0", example: { action: "tap_text", text: "Search", index: 1 } },
  ],
  tap: [
    { ok: (step) => inPixels(step.x) && inPixels(step.y), needs: "x and y from 0 to 10000" },
    { ok: durationOk, needs: DURATION_NEEDS, example: { action: "tap", ...HELD_TAP_EXAMPLE } },
  ],
  swipe: [{ ok: swipeFieldsOk, needs: SWIPE_NEEDS, example: { action: "swipe", ...SWIPE_EXAMPLE } }],
  type_text: [{ ok: (step) => [...String(step.text)].length <= 256, needs: "text of at most 256 characters" }],
  wait: [{ ok: (step) => step.seconds === undefined || Number(step.seconds) >= 0, needs: `seconds as a number from 0 to ${MAX_WAIT_SECONDS}` }],
};

function stepSchema(action: StepAction): { properties: Record<string, FieldSchema>; required?: readonly string[] } {
  if (action === "wait") return { properties: { seconds: { type: "number", minimum: 0 } } };
  return SINGLE_TOOLS.find((tool) => tool.name === action)!.inputSchema;
}

const stepFields = (action: StepAction) => Object.keys(stepSchema(action).properties).filter((field) => field !== "serial");
const fieldOwners = (field: string) => STEP_ACTIONS.filter((action) => stepFields(action).includes(field));

function stepProperties() {
  const properties: Record<string, FieldSchema> = {
    action: { type: "string", enum: STEP_ACTIONS, description: "What the step does: a phone tool's name, or wait." },
  };
  for (const action of STEP_ACTIONS) {
    for (const field of stepFields(action)) {
      const owners = fieldOwners(field).join(" and ");
      const note = STEP_FIELD_NOTES[field];
      properties[field] ??= { ...stepSchema(action).properties[field], description: note ? `${owners}: ${note}.` : owners };
    }
  }
  return properties;
}

const STEP_PROPERTIES = stepProperties();

const lower = (value: unknown) => String(value).toLowerCase();
const toNumber = (value: unknown) => (typeof value === "number" ? value : typeof value === "string" && value.trim() !== "" ? Number(value) : Number.NaN);

function coerce(field: string, value: unknown): unknown {
  const { type, enum: choices } = STEP_PROPERTIES[field];
  if (type === "number" || type === "integer") return toNumber(value);
  if (type === "boolean") return value === "true" || value === "false" ? value === "true" : value;
  if (type === "string") return choices ? lower(value) : typeof value === "boolean" ? value : String(value);
  return value;
}

function typeRules(action: StepAction): StepRule[] {
  return stepFields(action).flatMap((field): StepRule[] => {
    const { type, enum: choices } = stepSchema(action).properties[field];
    if (type === "boolean") {
      return [{ ok: (step) => step[field] === undefined || typeof step[field] === "boolean", needs: `${field} as true or false`, example: { ...STEP_EXAMPLES[action], [field]: true } }];
    }
    if (choices) return [{ ok: (step) => step[field] === undefined || choices.includes(String(step[field])), needs: `${field} as one of ${choices.join(", ")}` }];
    if (type === "string") return [{ ok: (step) => step[field] === undefined || typeof step[field] === "string", needs: `${field} as a string, not true or false` }];
    return [];
  });
}

const BATCH_HINT = "To do several steps, or to act and then read the screen, in one call, use batch.";
const takesBatchHint = (name: string) => name !== "read_screen" && (PHONE_STEP_TOOLS as readonly string[]).includes(name);

export const TOOLS = [
  ...SINGLE_TOOLS.map((tool) => (takesBatchHint(tool.name) ? { ...tool, description: `${tool.description} ${BATCH_HINT}` } : tool)),
  {
    name: "batch",
    description: `Run up to ${MAX_STEPS} phone steps in order in one call. A step is one of open_app, tap_text, tap, swipe, type_text, press, read_screen or wait, with that tool's fields: {action, ...fields}. Add read_screen as the last step to see the result; the batch returns the screen only at a read_screen step. A wait step (0 to ${MAX_WAIT_SECONDS} s) gives content time to load. It stops at the first failing step. End a batch where the next screen is uncertain, such as after a step that may open a dialog, and read it before acting further. Example: {"steps":[{"action":"tap_text","text":"Search"},{"action":"type_text","text":"iPad mini"},{"action":"press","key":"enter"},{"action":"wait","seconds":3},{"action":"read_screen"}]}`,
    inputSchema: {
      type: "object",
      properties: {
        steps: { type: "array", maxItems: MAX_STEPS, items: { type: "object", properties: STEP_PROPERTIES, required: ["action"] } },
        serial: { type: "string" },
      },
      required: ["steps"],
    },
  },
] as const;

type ToolResult = { content: Array<{ type: "text"; text: string } | { type: "image"; data: string; mimeType: string }>; isError?: boolean };
const textResult = (text: string, isError = false): ToolResult => ({ content: [{ type: "text", text }], ...(isError ? { isError: true } : {}) });

const isStepAction = (value: unknown): value is StepAction => STEP_ACTIONS.includes(value as StepAction);
const refusal = (problem: string) => new Error(`${problem}. No step was performed.`);

function parseJson(value: unknown): unknown {
  if (typeof value !== "string") return value;
  try {
    return JSON.parse(value);
  } catch {
    return value;
  }
}

export function parseSteps(raw: unknown): PhoneStep[] {
  const steps = parseJson(raw);
  if (!Array.isArray(steps) || !steps.length) {
    throw refusal(`steps needs a list of 1 to ${MAX_STEPS} steps, e.g. {"steps":[{"action":"tap_text","text":"Search"},{"action":"read_screen"}]}`);
  }
  if (steps.length > MAX_STEPS) throw refusal(`A batch runs at most ${MAX_STEPS} steps, not ${steps.length}; split them into several batches`);
  return steps.map((step, index) => parseStep(parseJson(step), index + 1));
}

function parseStep(raw: unknown, n: number): PhoneStep {
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw refusal(`Step ${n} needs to be an object such as ${JSON.stringify(STEP_EXAMPLES.tap_text)}`);
  const { serial } = raw as Json;
  if (serial !== undefined && serial !== null && serial !== "") {
    const named = typeof serial === "string" || typeof serial === "number" ? String(serial) : "<serial>";
    throw refusal(`Step ${n} names a serial, but a batch runs on one phone; set serial once beside steps, e.g. ${JSON.stringify({ serial: named, steps: [STEP_EXAMPLES.read_screen] })}`);
  }
  const step: Json = {};
  for (const [field, value] of Object.entries(raw)) {
    if (!Object.hasOwn(STEP_PROPERTIES, field) || value === null || value === "") continue;
    step[field] = typeof value === "object" ? value : coerce(field, value);
  }
  const { action } = step;
  if (action === "screenshot") throw refusal(`Step ${n}: screenshot returns an image and cannot be a batch step; use read_screen, or call screenshot after the batch`);
  if (action === "batch") throw refusal(`Step ${n}: a batch cannot be a batch step; put its steps in this list`);
  if (!isStepAction(action)) throw refusal(`Step ${n} needs an action from ${STEP_ACTIONS.join(", ")}, e.g. ${JSON.stringify(STEP_EXAMPLES.tap_text)}`);
  const example = JSON.stringify(STEP_EXAMPLES[action]);
  const foreign = Object.keys(step).find((field) => field !== "action" && !stepFields(action).includes(field));
  if (foreign === "seconds") {
    const seconds = Number.isFinite(step.seconds) && Number(step.seconds) >= 0 ? step.seconds : STEP_EXAMPLES.wait.seconds;
    throw refusal(`Step ${n} (${action}) does not take seconds; put a separate ${JSON.stringify({ action: "wait", seconds })} step after it`);
  }
  if (foreign) throw refusal(`Step ${n} (${action}) does not take ${foreign}, which belongs to ${fieldOwners(foreign).join(" or ")}; write it like ${example}`);
  const missing = (stepSchema(action).required ?? []).filter((field) => step[field] === undefined);
  if (missing.length) throw refusal(`Step ${n} (${action}) needs ${missing.join(" and ")}, e.g. ${example}`);
  const nested = Object.keys(step).find((field) => typeof step[field] === "object");
  if (nested) throw refusal(`Step ${n} (${action}) needs ${nested} as a single value, not a list or an object, e.g. ${example}`);
  const broken = [...(STEP_RULES[action] ?? []), ...typeRules(action)].find((rule) => !rule.ok(step));
  if (broken) throw refusal(`Step ${n} (${action}) needs ${broken.needs}, e.g. ${JSON.stringify(broken.example ?? STEP_EXAMPLES[action])}`);
  return { ...step, action };
}

// The harness reserves the phone per turn from the first real tool call. Retained MCP processes can
// outlive their turn, so every call revalidates the claim.
export function createPhoneClaim(
  env: NodeJS.ProcessEnv = process.env,
  fetchImpl: typeof fetch = fetch,
): EnsureClaim {
  const url = env.OMB_HARNESS_URL?.trim();
  const token = env.OMB_PHONE_TOKEN?.trim();
  if (!url || !token) {
    return async () => ({ ok: true });
  }
  return async (midCall = false) => {
    try {
      const asked = Date.now();
      const response = await fetchImpl(new URL("/api/internal/phone/claim", url), {
        method: "POST",
        redirect: "error",
        headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
        body: midCall ? '{"wait":false}' : "{}",
        signal: AbortSignal.timeout(CLAIM_TIMEOUT_MS),
      });
      if (response.ok) return Date.now() - asked > CONTROL_ACT_WITHIN_MS ? { ok: false, message: PHONE_WAITED_TEXT } : { ok: true };
      if (response.status === 409) return { ok: false, message: PHONE_BUSY_TEXT };
      if (response.status === 423) {
        const reason = ((await response.json().catch(() => null)) as { reason?: unknown } | null)?.reason;
        if (reason === "person") return { ok: false, message: PHONE_PERSON_TEXT };
        if (reason === "handed-back") return { ok: false, message: PHONE_HANDED_BACK_TEXT };
      }
      if (response.status === 401 || response.status === 403) {
        return { ok: false, message: "This turn no longer has phone access. Start a new turn to use the phone." };
      }
      return { ok: false, message: "OpenMausBot could not reserve the phone for this call. This call was not performed; try again, or start a new turn if it keeps failing." };
    } catch {
      return { ok: false, message: "OpenMausBot could not be reached to reserve the phone. This call was not performed; try again." };
    }
  };
}

async function wait(requested: unknown): Promise<string> {
  const seconds = Math.min(requested === undefined ? DEFAULT_WAIT_SECONDS : Number(requested), MAX_WAIT_SECONDS);
  await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
  return seconds < Number(requested) ? `Waited ${seconds} s (a wait is at most ${MAX_WAIT_SECONDS} s)` : `Waited ${seconds} s`;
}

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 : "";
};

// Mid-batch, earlier steps did run, so a claim text's words that the call was not performed would be false.
const midBatchClaimReason = (message: string) => message.replace(/ This call was (?:not|NOT) performed[^.]*\.|, so (?:this call|it) was NOT performed/, "").replace(/\.$/, "");
// A step fails with a claim text when the phone is refused between two of its commands.
const reason = (problem: unknown) => midBatchClaimReason(problem instanceof Error ? problem.message : String(problem));
const EARLIER_AND_LATER_STEPS = "Steps before it were done; do not repeat them. Later steps were not performed.";

export async function runSteps(
  serial: string,
  steps: PhoneStep[],
  ensureClaim: EnsureClaim,
  perform: (serial: string, step: PhoneStep) => Promise<string>,
): Promise<ToolResult> {
  const lines: string[] = [];
  for (const [index, step] of steps.entries()) {
    const n = index + 1;
    // Stopping the turn revokes its phone claim while this process lives on, so each later step rechecks it.
    if (n > 1) {
      const claim = await ensureClaim(true);
      if (!claim.ok) return textResult([...lines, `Stopped before step ${n}: ${midBatchClaimReason(claim.message)}. ${EARLIER_AND_LATER_STEPS}`].join("\n"));
    }
    let output: string;
    try {
      output = step.action === "wait" ? await wait(step.seconds) : await perform(serial, step);
    } catch (error) {
      // An error flag leads models to redo the whole call, so it marks only a first-step failure, where no finished step is redone.
      return textResult([...lines, `Step ${n} failed: ${reason(error)}. Its effect is unknown; call read_screen or screenshot before trying it again. ${EARLIER_AND_LATER_STEPS}`].join("\n"), n === 1);
    }
    lines.push(step.action === "read_screen" ? `${n}. read_screen:\n${output}` : `${n}. ${step.action}: ${output}`);
  }
  return textResult(lines.join("\n"));
}

async function callTool(name: string, args: Json, adb: Adb, onDevice: OnDevice, ensureClaim: EnsureClaim): Promise<ToolResult> {
  if (name === "status") {
    const available = Boolean(resolveAdbPath());
    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, unclaimed));
  }
  const serial = await readySerial(adb, args.serial);
  return callToolOn(onDevice, unclaimed, serial, name, args);
}

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);
    if (!png.subarray(0, 8).equals(Buffer.from("89504e470d0a1a0a", "hex"))) throw new Error("Android returned an invalid screenshot");
    return { content: [{ type: "text", text: `Android screenshot from ${serial}` }, { type: "image", data: png.toString("base64"), mimeType: "image/png" }] };
  }
  if (name === "list_apps") {
    const packages = await launchablePackages(onDevice, serial);
    const query = typeof args.query === "string" ? args.query : "";
    return textResult((query ? findPackages(query, packages) : packages).join("\n") || "No matching launchable app found");
  }
  if (name === "open_app") {
    const appName = String(args.name ?? "").trim();
    if (!appName) throw new Error("name is required");
    const target = launchTarget(appName, await launchablePackages(onDevice, serial));
    await onDevice(serial, ["shell", "monkey", "-p", target, "-c", "android.intent.category.LAUNCHER", "1"]);
    return textResult(`Opened ${appName} (${target})`);
  }
  if (name === "tap_text") {
    const query = String(args.text ?? "").trim().toLowerCase();
    if (!query) throw new Error("text is required");
    const exact = args.exact === true;
    const hits = (await readNodes(onDevice, serial)).filter((node) => {
      const labels = [node.text, node.description].filter(Boolean).map((value) => value.toLowerCase());
      return labels.some((label) => exact ? label === query : label.includes(query));
    });
    const index = Number.isInteger(args.index) ? Number(args.index) : 0;
    const hit = hits[index];
    if (!hit) throw new Error(`No visible Android text matches ${JSON.stringify(args.text)}`);
    const [x1, y1, x2, y2] = hit.bounds;
    await onDevice(serial, ["shell", "input", "tap", String(Math.round((x1 + x2) / 2)), String(Math.round((y1 + y2) / 2))]);
    return textResult(`Tapped ${JSON.stringify(hit.text || hit.description)}`);
  }
  if (name === "tap") {
    const x = Number(args.x); const y = Number(args.y);
    if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || y < 0 || x > 10000 || y > 10000) throw new Error("Invalid Android coordinates");
    if (!durationOk(args)) throw new Error(`tap needs ${DURATION_NEEDS}, e.g. ${JSON.stringify(HELD_TAP_EXAMPLE)}`);
    const at = [String(Math.round(x)), String(Math.round(y))];
    const held = given(args.duration_ms) ? String(rounded(args.duration_ms)) : undefined;
    // Android's `input` has no touch long press: a swipe that ends where it starts holds the finger there (seen on Android 9).
    await onDevice(serial, ["shell", "input", ...(held ? ["swipe", ...at, ...at, held] : ["tap", ...at])]);
    return textResult(held ? `Pressed ${at.join(",")} for ${held} ms` : `Tapped ${at.join(",")}`);
  }
  if (name === "swipe") {
    const direction = String(args.direction ?? "");
    if (!swipeFieldsOk(args)) throw new Error(`swipe needs ${SWIPE_NEEDS}, e.g. ${JSON.stringify(SWIPE_EXAMPLE)}`);
    const size = (await onDevice(serial, ["shell", "wm", "size"])).toString("utf8");
    const match = /(\d+)x(\d+)/.exec(size);
    if (!match) throw new Error("Could not read Android screen size");
    const width = Number(match[1]); const height = Number(match[2]);
    const moves: Record<string, { x: number; y: number; dx: number; dy: number; distance: number }> = {
      up: { x: width / 2, y: height * 0.75, dx: 0, dy: -1, distance: height / 2 },
      down: { x: width / 2, y: height * 0.25, dx: 0, dy: 1, distance: height / 2 },
      left: { x: width * 0.75, y: height / 2, dx: -1, dy: 0, distance: width / 2 },
      right: { x: width * 0.25, y: height / 2, dx: 1, dy: 0, distance: width / 2 },
    };
    const move = Object.hasOwn(moves, direction) ? moves[direction] : undefined;
    if (!move) throw new Error("Invalid swipe direction");
    const or = (value: unknown, fallback: number) => (given(value) ? rounded(value) : fallback);
    const x = or(args.x, move.x); const y = or(args.y, move.y); const distance = or(args.distance, move.distance);
    const sent = [x, y, x + move.dx * distance, y + move.dy * distance, or(args.duration_ms, 260)].map((value) => String(Math.round(value)));
    await onDevice(serial, ["shell", "input", "swipe", ...sent]);
    const chosen = [args.x, args.y, args.distance, args.duration_ms].some(given);
    return textResult(chosen ? `Swiped from ${sent[0]},${sent[1]} to ${sent[2]},${sent[3]} in ${sent[4]} ms` : `Swiped ${direction}`);
  }
  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");
    await onDevice(serial, ["shell", "input", "keyevent", KEYCODES[key]]);
    return textResult(`Pressed ${key}`);
  }
  throw new Error(`Unknown phone tool: ${name}`);
}

export async function callPhoneTool(name: string, args: Json, ensureClaim: EnsureClaim, adb: Adb = runAdb): Promise<ToolResult> {
  const claim = await ensureClaim();
  if (!claim.ok) return textResult(claim.message, true);
  try {
    return await callTool(name, args, adb, claimedDevice(ensureClaim, adb), ensureClaim);
  } catch (error) {
    return textResult(error instanceof Error ? error.message : String(error), true);
  }
}

const send = (message: Json) => process.stdout.write(`${JSON.stringify(message)}\n`);
const ok = (id: unknown, result: unknown) => send({ jsonrpc: "2.0", id, result });
const rpcError = (id: unknown, code: number, message: string) => send({ jsonrpc: "2.0", id, error: { code, message } });

// MCP: a client that gives up on a call sends notifications/cancelled with that call's request id.
const callsCancelled = new Map<unknown, boolean>();

async function handle(message: Json, ensureClaim: EnsureClaim) {
  const id = message.id;
  const method = message.method;
  const params = (message.params ?? {}) as Json;
  if (method === "initialize") return ok(id, { protocolVersion: String(params.protocolVersion ?? "2024-11-05"), capabilities: { tools: {} }, serverInfo: { name: "openmausbot-phone", version: "1" } });
  if (method === "notifications/cancelled" && callsCancelled.has(params.requestId)) callsCancelled.set(params.requestId, true);
  if (method === "notifications/initialized" || method === "notifications/cancelled") return;
  if (method === "ping") return ok(id, {});
  if (method === "tools/list") return ok(id, { tools: TOOLS });
  if (method === "tools/call") {
    const name = String(params.name ?? "");
    if (!TOOLS.some((tool) => tool.name === name)) return rpcError(id, -32602, `Unknown tool: ${name}`);
    if (name === "batch") callsCancelled.set(id, false);
    const stillWanted: EnsureClaim = async (midCall) => {
      const stillClaimed = await ensureClaim(midCall);
      return callsCancelled.get(id) ? { ok: false, message: "The client cancelled this call." } : stillClaimed;
    };
    try {
      return ok(id, await callPhoneTool(name, (params.arguments ?? {}) as Json, stillWanted));
    } finally {
      callsCancelled.delete(id);
    }
  }
  if (id !== undefined) rpcError(id, -32601, `Method not found: ${String(method)}`);
}

if (process.argv[1] && existsSync(process.argv[1]) && /phone-proxy\.(?:ts|js)$/.test(process.argv[1])) {
  const ensureClaim = createPhoneClaim();
  readStdioLines(process.stdin, (line) => {
    try {
      const message = JSON.parse(line) as Json;
      void handle(message, ensureClaim).catch((error) => rpcError(message.id, -32603, error instanceof Error ? error.message : String(error)));
    } catch {}
  });
}