Skip to content

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

2.8 KB
Raw Blame
// A stdio bridge into Cua Driver's MCP server in the Local VM.
import { cuaExecArgs } from "./container-computer.ts";
import { runMcpBridge } from "./mcp-bridge.ts";
import { bridgeJournal } from "./saved-steps/journal-tap.ts";

const [runtime, container, socket] = process.argv.slice(2);
if (!runtime || !["docker", "podman", "container"].includes(runtime)) {
  process.stderr.write("invalid Local VM runtime\n");
  process.exit(2);
}
if (!container || !/^[a-zA-Z0-9_.-]+$/.test(container) || !socket?.startsWith("/run/user/1000/")) {
  process.stderr.write("invalid Local VM connection\n");
  process.exit(2);
}

// The who-is-driving pair rides in env, not argv, which `ps` shows to everyone; the token guards a loopback endpoint.
const controlUrl = process.env.OMB_CONTROL_URL ?? "";
const controlToken = process.env.OMB_CONTROL_TOKEN ?? "";
const journal = bridgeJournal(process.env.OMB_COMPUTER_JOURNAL, { url: controlUrl, token: controlToken }, "vm");

// Cua Driver 0.37.0 can report an AT-SPI write as attempted when Firefox ESR leaves the field empty.
function textInputResultLine(line: string): string {
  if (!line.includes("unverifiable") && !line.includes("suspected_noop")) return line;
  let frame: { result?: { structuredContent?: { effect?: string; route?: string }; content?: Array<{ type?: string; text?: string }> } } | null;
  try {
    frame = JSON.parse(line);
  } catch {
    return line;
  }
  const result = frame?.result;
  const outcome = result?.structuredContent;
  if (outcome?.route !== "accessibility" || (outcome.effect !== "unverifiable" && outcome.effect !== "suspected_noop")) return line;
  const content = result?.content;
  if (!Array.isArray(content)) return line;
  const [first, ...rest] = content;
  if (first?.type !== "text" || typeof first.text !== "string" || !/^(?:Typed \d+ character\(s\) \(via targeted AT-SPI\)\.$|Set value of element \[)/u.test(first.text)) return line;
  const text = `${first.text} The accessibility action could not confirm that the field changed. In Firefox ESR on the Local VM this route can leave a field empty. Check the field with get_window_state before trying again. If it is unchanged, use type_text with the field's window-relative x,y and delivery_mode: foreground, or focus the field and use clipboard_write followed by Ctrl+V. Check the resulting value.`;
  return JSON.stringify({ ...frame, result: { ...result, content: [{ ...first, text }, ...rest] } });
}

runMcpBridge({
  command: runtime,
  args: cuaExecArgs(["mcp", "--socket", socket], { container, interactive: true }),
  label: "Cua Driver",
  rewriteResponse: textInputResultLine,
  consoleChild: true,
  // No liveness watchdog: the runtime CLI talks to a local daemon and fails fast on its own.
  ...(controlUrl && controlToken ? { gate: { url: controlUrl, token: controlToken } } : {}),
  ...(journal ? { journal } : {}),
});