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 { actionLine, actionView, computerLabel, keysLine } from "./computer.ts";
import { computerActionProblem, type ComputerAction } from "./format.ts";
const APP = { name: "TextEdit" };
describe("a computer step in words", () => {
it("writes keys as a person would on each computer", () => {
expect(keysLine({ keys: ["cmd", "s"] }, "yourComputer")).toBe("⌘S");
expect(keysLine({ keys: ["cmd", "shift", "z"] }, "yourComputer")).toBe("⌘⇧Z");
expect(keysLine({ key: "ctrl+s" }, "cloud")).toBe("Ctrl+S");
expect(keysLine({ key: "return", modifiers: ["alt"] }, "localVm")).toBe("Alt+Return");
});
it("labels the step by its actions, inputs by name and long runs counted", () => {
const save: ComputerAction = { tool: "click", app: APP, window: { title: "Untitled" }, element: { role: "AXButton", label: "Save", path: [] } };
const type: ComputerAction = { tool: "type_text", app: APP, window: { title: "Untitled" }, text: { $arg: "recipient" } };
expect(actionLine(save, "yourComputer")).toBe("Click “Save” in TextEdit");
expect(actionLine(type, "yourComputer")).toBe("Type {recipient}");
expect(actionLine({ tool: "click", point: { x: 10, y: 20 }, screen: { width: 1, height: 1 } }, "cloud")).toBe("Click at 10,20");
expect(computerLabel([save, type], "yourComputer")).toBe("Click “Save” in TextEdit, type {recipient}");
expect(computerLabel(Array.from({ length: 20 }, () => save), "yourComputer").length).toBeLessThanOrEqual(120);
expect(computerLabel(Array.from({ length: 20 }, () => save), "yourComputer")).toMatch(/and \d+ more$/);
expect(actionView(type, "yourComputer")).toEqual({ kind: "type", on: { window: "Untitled" }, input: "recipient", app: "TextEdit" });
expect(actionView({ ...type, text: "hello" }, "yourComputer").input).toBe("text");
});
it("refuses an action that could only be replayed blind", () => {
expect(computerActionProblem({ tool: "click", app: APP, point: { x: 1, y: 1 }, window: { title: "W" } }, "yourComputer")).toMatch(/size/);
expect(computerActionProblem({ tool: "click", app: APP }, "yourComputer")).toMatch(/element or a point/);
expect(computerActionProblem({ tool: "click", point: { x: 1, y: 1 } }, "cloud")).toMatch(/screen/);
expect(computerActionProblem({ tool: "click", app: APP, point: { x: 1, y: 1 }, screen: { width: 9, height: 9 } }, "cloud")).toMatch(/only a point/);
expect(computerActionProblem({ tool: "hotkey", app: APP, keys: ["cmd", "s"] }, "yourComputer")).toMatch(/window/);
expect(computerActionProblem({ tool: "set_value", app: APP, window: { title: "W" }, element: { role: "AXTextField", label: "To", path: [] } }, "localVm")).toMatch(/text/);
expect(computerActionProblem({ tool: "click", app: APP, window: { title: "W", width: 10, height: 10 }, point: { x: 1, y: 1 } }, "localVm")).toBeNull();
});
});