Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { describe, expect, it } from "vitest";
import { gatedLocalComputer } from "../local-computer.ts";
import { SPAWNED_PROXIES } from "../proxy-paths.ts";
import { cloudOutcome, deskReach, placeOf } from "./desk-caller.ts";
const control = { url: "http://127.0.0.1:1/api/internal/computer-control?botId=b", token: "turn-token" };
const host = gatedLocalComputer({ command: "/x/cua-driver", args: ["mcp"], env: {}, platform: "darwin", scope: "local-computer" }, control, "desk");
const vm = { command: process.execPath, args: [SPAWNED_PROXIES.containerMcp, "docker", "omb-vm", "/run/user/1000/s.sock"], env: { OMB_CONTROL_URL: control.url, OMB_CONTROL_TOKEN: control.token } };
const vps = { command: process.execPath, args: [SPAWNED_PROXIES.vpsContainerMcp, "x"], env: { OMB_CONTROL_URL: control.url, OMB_CONTROL_TOKEN: control.token } };
const cloud = { command: process.execPath, args: [SPAWNED_PROXIES.harnessMcp, "computer"], env: { OMB_HARNESS_URL: "http://127.0.0.1:9", OMB_MCP_TOKEN_COMPUTER: "turn-cloud" } };
describe("a run's reach into its turn's computer", () => {
it("knows each computer a turn can mount, and none it can't drive", () => {
expect(placeOf(host)).toBe("yourComputer");
expect(placeOf(vm)).toBe("localVm");
expect(placeOf(cloud)).toBe("cloud");
expect(placeOf(vps)).toBeNull();
expect(placeOf({ ...host, env: { ...host.env, OMB_CONTROL_URL: "" } })).toBeNull();
});
it("allows a replay only at full access and within the turn's tool scope", () => {
let full = true;
const reach = deskReach({ generation: "g", computer: host, toolScope: { deny: ["mcp:computer:hotkey"] }, approvalFull: () => full })!;
expect(reach.allowed(["list_apps", "click"])).toBe(true);
expect(reach.allowed(["list_apps", "hotkey"])).toBe(false);
full = false;
expect(reach.allowed(["list_apps", "click"])).toBe(false);
expect(deskReach(null)).toBeNull();
expect(deskReach({ generation: "g", approvalFull: () => true })).toBeNull();
});
it("reaches the turn's computer with a replay token the server minted from the turn's own, or not at all", async () => {
const opened: Array<Record<string, string>> = [];
const openDriver = async (computer: { env: Record<string, string> }) => {
opened.push(computer.env);
return { tools: null, call: async () => ({ ok: true as const, structured: {} }), close: async () => {} };
};
const mint = (token: string) => (token === "turn-token" ? "replay-token" : null);
await deskReach({ generation: "g", computer: vm, approvalFull: () => true, replay: mint }, { openDriver })!.open(new AbortController().signal);
expect(opened).toEqual([{ ...vm.env, OMB_CONTROL_TOKEN: "replay-token" }]);
const refused = deskReach({ generation: "g", computer: vm, approvalFull: () => true, replay: () => null }, { openDriver })!;
await expect(refused.open(new AbortController().signal)).rejects.toThrow(/could not be reached/);
expect(opened).toHaveLength(1);
const sent: string[] = [];
const fetchImpl = (async (_url: string, init: RequestInit) => {
sent.push(String((init.headers as Record<string, string>).authorization));
return new Response(JSON.stringify({ result: { content: [] } }));
}) as unknown as typeof fetch;
const session = await deskReach({ generation: "g", computer: cloud, approvalFull: () => true, replay: (token) => `replay-${token}` }, { fetch: fetchImpl })!.open(new AbortController().signal);
await session.call("click", { x: 1, y: 1 }, new AbortController().signal);
expect(sent).toEqual(["Bearer replay-turn-cloud"]);
});
it("calls the cloud computer with the turn's own capability and reads its answer", async () => {
const sent: Array<{ url: string; auth: string; body: unknown }> = [];
const fetchImpl = (async (url: string, init: RequestInit) => {
sent.push({ url, auth: String((init.headers as Record<string, string>).authorization), body: JSON.parse(String(init.body)) });
return new Response(JSON.stringify({ result: { content: [{ type: "text", text: JSON.stringify({ ok: true, stdout: "1280 800\n" }) }] } }));
}) as unknown as typeof fetch;
const session = await deskReach({ generation: "g", computer: cloud, approvalFull: () => true }, { fetch: fetchImpl })!.open(new AbortController().signal);
expect(await session.call("get_screen_size", {}, new AbortController().signal)).toEqual({ ok: true, structured: { ok: true, stdout: "1280 800\n" } });
expect(sent).toEqual([{ url: "http://127.0.0.1:9/api/internal/computer/mcp", auth: "Bearer turn-cloud", body: { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "get_screen_size", arguments: {} } } }]);
expect(cloudOutcome({ result: { isError: true, content: [{ type: "text", text: "The cloud computer is not ready." }] } })).toEqual({ ok: false, error: "The cloud computer is not ready." });
expect(cloudOutcome({ error: "this turn has no cloud computer" })).toEqual({ ok: false, error: "this turn has no cloud computer" });
expect(cloudOutcome({ result: { content: [{ type: "text", text: JSON.stringify({ ok: false, stderr: "xdotool: failed" }) }] } })).toEqual({ ok: false, error: "xdotool: failed" });
});
});