Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// A saved burger: a workflow skill is SKILL.md (numbered steps any bot can follow) plus workflow.json (the exact steps,
// values and args a runner repeats without the AI). References are JSON values, never text templates: {"$arg": name}
// and {"$step": id, "path": "a.0.b"}. A shell step reads its args from OMB_ARG_<NAME> environment variables.
import { z } from "zod";
import { appMeta } from "./apps.ts";
import { actionLine } from "./computer.ts";
export const WORKFLOW_FILE = "workflow.json";
export const WORKFLOW_TAG = "workflow";
export const WORKFLOW_MAX_STEPS = 40;
export const WORKFLOW_MAX_BYTES = 256 * 1024;
export const SUMMARY_MAX = 160;
const ref = z.union([
z.object({ $arg: z.string().regex(/^[a-z][a-z0-9_]{0,39}$/) }).strict(),
z.object({ $step: z.string().regex(/^[a-z0-9_]{1,40}$/), path: z.string().max(200).optional() }).strict(),
]);
export type WorkflowRef = z.infer<typeof ref>;
/** Numeric gt/lt rules compare finite numbers or nonempty numeric strings. Other values do not match. */
const condition = z.object({
$step: z.string(), path: z.string().max(200).optional(), op: z.enum(["eq", "ne", "gt", "lt", "exists"]), value: z.unknown().optional(),
}).strict();
/** Text with values in it: plain text, or parts where an arg or an earlier step's output fills a gap. Never a template
* string, so a value can't change the words around it. */
const text = z.union([z.string().min(1).max(8_000), z.array(z.union([z.string().max(8_000), ref])).min(1).max(40)]);
export type WorkflowText = z.infer<typeof text>;
/** What "done" means for a step: `says` in words, and, where a rule on the step's own output can tell, that rule. A step
* with a rule fails when the rule doesn't hold; one with words only is checked by the bot. `true` is an older file's
* "check this step", read as "does this look right". */
export const checkSchema = z.union([
z.boolean(),
z.object({
says: z.string().min(1).max(1_000),
rule: z.object({ path: z.string().max(200).optional(), op: z.enum(["eq", "ne", "gt", "lt", "exists"]), value: z.unknown().optional() }).strict().optional(),
}).strict(),
]);
export type WorkflowCheck = z.infer<typeof checkSchema>;
/** A teammate a delegation goes to: its id where it ran, its name and role so another bot can take its place. */
const teammate = z.object({ id: z.string().min(1).max(128).optional(), name: z.string().min(1).max(80), role: z.string().max(120).optional() }).strict();
export type WorkflowTeammate = z.infer<typeof teammate>;
export const DELEGATE_MAX_ROUNDS = 3;
const common = {
id: z.string().regex(/^[a-z0-9_]{1,40}$/),
label: z.string().min(1).max(120),
when: condition.optional(),
timeoutMs: z.number().int().min(1_000).max(600_000).optional(),
check: checkSchema.optional(),
/** Where it ran, so a replay runs it in the same place: an app, the browser, the bot's computer, a cloud computer (VM), the person's computer. */
where: z.enum(["app", "browser", "bot", "vm", "yourComputer"]).optional(),
};
/** A case's name: a few plain words, as the person or the bot would say them ("support", "a refund"). */
export const caseName = z.string().trim().min(1).max(40).regex(/^[\p{L}\p{N}][\p{L}\p{N} '&-]*$/u);
/** One rule of a decide step: when an earlier step's output matches, the run takes `case`. */
const caseRule = condition.extend({ case: caseName });
const PLACE: Record<string, string> = { vm: "on the cloud computer", yourComputer: "on the person's own computer", browser: "in the browser" };
/** The desktop a computer step acts on: the person's own computer or the Local VM (both through Cua Driver, by element),
* or the cloud computer (pixels only). */
export const COMPUTER_PLACES = ["yourComputer", "localVm", "cloud"] as const;
export type ComputerPlace = typeof COMPUTER_PLACES[number];
export const COMPUTER_ACTION_TOOLS = ["click", "double_click", "right_click", "type_text", "set_value", "press_key", "hotkey", "scroll", "drag", "invoke_menu", "launch_app", "bring_to_front"] as const;
export type ComputerActionTool = typeof COMPUTER_ACTION_TOOLS[number];
export const COMPUTER_MAX_ACTIONS = 24;
const COMPUTER_PLACE: Record<ComputerPlace, string> = { yourComputer: "on the person's own computer", localVm: "on the Local VM", cloud: "on the cloud computer" };
const coordinate = z.number().finite().min(0).max(32_767);
const point = z.object({ x: coordinate, y: coordinate }).strict();
const extent = z.number().int().min(1).max(32_767);
/** One recorded desktop action. A driver target is found again by `app`, then `window` by title, then `element` by role,
* label and ancestor path; `point` is window-local and only replays in a window of the recorded width and height. On the
* cloud computer `point` is on the screen and only replays on a screen of the recorded `screen` size. `text` is what
* type_text types or set_value sets. */
export const computerActionSchema = z.object({
tool: z.enum(COMPUTER_ACTION_TOOLS),
app: z.object({ bundleId: z.string().min(1).max(255).optional(), name: z.string().min(1).max(255).optional() }).strict().optional(),
window: z.object({ title: z.string().max(500), width: extent.optional(), height: extent.optional() }).strict().optional(),
element: z.object({ role: z.string().min(1).max(100), label: z.string().min(1).max(500), path: z.array(z.string().max(600)).max(40) }).strict().optional(),
point: point.optional(),
to: point.optional(),
screen: z.object({ width: extent, height: extent }).strict().optional(),
text: z.union([z.string().max(4_000), ref]).optional(),
key: z.string().min(1).max(100).optional(),
keys: z.array(z.string().min(1).max(40)).min(1).max(8).optional(),
modifiers: z.array(z.string().min(1).max(40)).max(6).optional(),
button: z.enum(["left", "right", "middle"]).optional(),
count: z.number().int().min(1).max(3).optional(),
direction: z.enum(["up", "down", "left", "right"]).optional(),
amount: z.number().int().min(1).max(50).optional(),
by: z.enum(["line", "page"]).optional(),
menuPath: z.array(z.string().min(1).max(200)).min(1).max(8).optional(),
foreground: z.literal(true).optional(),
}).strict();
export type ComputerAction = z.infer<typeof computerActionSchema>;
const NEEDS_TEXT = new Set<ComputerActionTool>(["type_text", "set_value"]);
const POINTER = new Set<ComputerActionTool>(["click", "double_click", "right_click", "scroll", "drag"]);
/** What is wrong with an action for its place, or null: a driver action names its app; an element or a point needs its
* window, a point the window's size; a cloud action has no app, window or element and a point needs the screen's size. */
export function computerActionProblem(action: ComputerAction, on: ComputerPlace): string | null {
if (NEEDS_TEXT.has(action.tool) && action.text === undefined) return `${action.tool} needs text`;
if (action.tool === "press_key" && !action.key) return "press_key needs a key";
if (action.tool === "hotkey" && !action.keys) return "hotkey needs keys";
if (action.tool === "drag" && (!action.point || !action.to)) return "drag needs a start and an end";
if (action.tool === "scroll" && !action.direction) return "scroll needs a direction";
if (on === "cloud") {
if (action.app || action.window || action.element || action.menuPath) return "a cloud computer action has only a point on the screen";
if (["set_value", "invoke_menu", "launch_app", "bring_to_front", "hotkey"].includes(action.tool)) return `the cloud computer has no ${action.tool}`;
if (POINTER.has(action.tool) && (!action.point || !action.screen)) return `${action.tool} needs a point and the screen's size`;
return null;
}
if (!action.app?.bundleId && !action.app?.name) return "the action does not name its app";
if (action.tool === "invoke_menu" && (!action.menuPath || !action.window)) return "invoke_menu needs its window and menu path";
if (action.element && !action.window) return "an element needs its window";
if (action.point && !(action.window?.width && action.window.height)) return "a point needs its window's size";
if (POINTER.has(action.tool) && !action.element && !action.point) return `${action.tool} needs an element or a point`;
if (["press_key", "hotkey"].includes(action.tool) && !action.element && !action.window) return "a key needs its window";
return null;
}
export const workflowStepSchema = z.discriminatedUnion("kind", [
z.object({ ...common, kind: z.literal("shell"), run: z.string().min(1).max(20_000), cwd: z.string().max(1_000).optional(), stdin: ref.optional() }).strict(),
z.object({ ...common, kind: z.literal("app"), app: z.string().min(1).max(64), tool: z.string().min(1).max(200), args: z.record(z.string(), z.unknown()), account: z.string().max(200).optional() }).strict(),
z.object({ ...common, kind: z.literal("browser"), action: z.literal("open"), url: z.union([z.string().url(), ref]) }).strict(),
z.object({ ...common, kind: z.literal("file"), path: z.string().min(1).max(1_000), content: z.union([z.string(), ref]) }).strict(),
z.object({
...common, kind: z.literal("ai"), deterministic: z.literal(false), prompt: z.string().min(1).max(4_000),
input: ref.optional(), example: z.string().max(4_000).optional(),
}).strict(),
// A fork: rules on an earlier step's output pick a case, or the AI picks one by judgement. Steps for a case carry
// `when: {"$step": <decide id>, "path": "case", "op": "eq", "value": <case>}`; the run takes only that case's steps.
// Work handed to teammates at once, each with its own message: the run waits for every reply. `review` lets the bot
// send a reply that falls short of its done-when back for up to `rounds` more rounds; `example` is what the extra round
// asked last time.
z.object({
...common, kind: z.literal("delegate"),
tasks: z.array(z.object({ bot: teammate, message: text, doneWhen: z.string().max(1_000).optional() }).strict()).min(1).max(8),
consult: z.boolean().optional(),
review: z.object({ rounds: z.number().int().min(1).max(DELEGATE_MAX_ROUNDS), example: z.string().max(2_000).optional() }).strict().optional(),
}).strict(),
// Desktop actions in a row on one computer, each found again on the screen before it acts; one that can't be found as
// recorded hands the rest of the step to the bot.
z.object({ ...common, kind: z.literal("computer"), on: z.enum(COMPUTER_PLACES), actions: z.array(computerActionSchema).min(1).max(COMPUTER_MAX_ACTIONS) }).strict(),
z.object({
...common, kind: z.literal("decide"), cases: z.array(caseName).min(1).max(8),
rules: z.array(caseRule).max(16).optional(), otherwise: caseName.optional(),
prompt: z.string().min(1).max(2_000).optional(), input: ref.optional(),
}).strict(),
]);
export type WorkflowStep = z.infer<typeof workflowStepSchema>;
export const workflowSchema = z.object({
version: z.literal(1),
summary: z.string().min(1).max(SUMMARY_MAX),
args: z.record(z.string().regex(/^[a-z][a-z0-9_]{0,39}$/), z.object({ default: z.unknown().optional(), label: z.string().max(80).optional() }).strict()),
steps: z.array(workflowStepSchema).min(1).max(WORKFLOW_MAX_STEPS),
}).strict().superRefine((workflow, issue) => {
const seen = new Map<string, WorkflowStep>();
for (const step of workflow.steps) {
if (seen.has(step.id)) issue.addIssue({ code: "custom", message: `step id ${step.id} is used twice` });
if (step.kind === "decide" && !step.rules?.length && !step.prompt) issue.addIssue({ code: "custom", message: `decide step ${step.id} needs rules or a prompt` });
if (step.kind === "computer") {
for (const action of step.actions) {
const problem = computerActionProblem(action, step.on);
if (problem) issue.addIssue({ code: "custom", message: `computer step ${step.id}: ${problem}` });
}
}
const fork = caseOf(step);
if (fork) {
const decide = seen.get(fork.decide);
if (decide?.kind !== "decide") issue.addIssue({ code: "custom", message: `step ${step.id} follows a case of ${fork.decide}, which is no earlier decide step` });
else if (!decide.cases.includes(fork.case)) issue.addIssue({ code: "custom", message: `step ${step.id} follows case "${fork.case}", which ${fork.decide} doesn't have` });
}
seen.set(step.id, step);
}
});
export type Workflow = z.infer<typeof workflowSchema>;
/** The decide step and case a step belongs to, read from its `when`; null for a step every run takes. */
export function caseOf(step: Pick<WorkflowStep, "when">): { decide: string; case: string } | null {
const when = step.when;
if (!when || when.path !== "case" || when.op !== "eq" || typeof when.value !== "string") return null;
return { decide: when.$step, case: when.value };
}
/** The `when` that puts a step on one case of a decide step. */
export const onCase = (decide: string, name: string) => ({ $step: decide, path: "case", op: "eq" as const, value: name });
/** A skill name from the summary's first words: lowercase, hyphens, unique among `taken`. */
export function workflowSkillName(summary: string, taken: ReadonlySet<string>): string {
const words = summary.toLowerCase().replace(/['’]/gu, "").normalize("NFKD").replace(/[^\p{L}\p{N}\s-]/gu, " ").replace(/[^a-z0-9\s-]/g, "").split(/[\s-]+/).filter(Boolean);
const stem = words.slice(0, 6).join("-").slice(0, 48).replace(/-+$/, "") || "saved-workflow";
if (!taken.has(stem)) return stem;
for (let n = 2; ; n += 1) if (!taken.has(`${stem}-${n}`)) return `${stem}-${n}`;
}
const yamlScalar = (value: string) => JSON.stringify(value.replace(/\s+/g, " ").trim());
/** "Kiwi (accessibility tester), Rio and Miso". */
export function teammateNames(tasks: ReadonlyArray<{ bot: WorkflowTeammate }>, roles = true): string {
const names = [...new Map(tasks.map((task) => [task.bot.name, task.bot])).values()].map((bot) => (roles && bot.role ? `${bot.name} (${bot.role})` : bot.name));
return names.length > 1 ? `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}` : names[0] ?? "";
}
function argsUsed(step: WorkflowStep, args: Workflow["args"]): string {
const used = Object.keys(args).filter((name) => JSON.stringify(step).includes(`"$arg":"${name}"`));
return used.length ? ` (${used.map((name) => `${name}${args[name]!.default === undefined ? "" : `, default ${JSON.stringify(args[name]!.default)}`}`).join("; ")})` : "";
}
/** A text's words with each gap named: "QA-test {site}". */
export function textLine(value: WorkflowText): string {
return typeof value === "string" ? value : value.map((part) => (typeof part === "string" ? part : "$arg" in part ? `{${part.$arg}}` : `{${part.$step}}`)).join("");
}
function stepLine(step: WorkflowStep, args: Workflow["args"]): string {
if (step.kind === "delegate") {
const done = [...new Set(step.tasks.map((task) => task.doneWhen).filter(Boolean))];
return [
`Teammates: ${teammateNames(step.tasks)} each get their part at once (coordinate_bots); wait for every reply${argsUsed(step, args)}`,
...step.tasks.map((task) => ` - ${task.bot.name}: ${textLine(task.message).replace(/\s+/g, " ").slice(0, 300)}`),
...(done.length ? [` Done when: ${done.join(" / ")}`] : []),
...(step.review ? [` A reply that falls short goes back, up to ${step.review.rounds} more round${step.review.rounds > 1 ? "s" : ""}${step.review.example ? `; last time: ${step.review.example.replace(/\s+/g, " ").slice(0, 200)}` : ""}`] : []),
].join("\n");
}
if (step.kind === "decide") return `Decide: ${step.label} (${step.cases.map((one) => `"${one}"`).join(", ")})${step.prompt ? " (your judgement)" : ""}`;
if (step.kind === "computer") {
const fork = caseOf(step);
return [
`${fork ? `[only when ${fork.decide} is "${fork.case}"] ` : ""}Computer ${COMPUTER_PLACE[step.on]}: ${step.label}${argsUsed(step, args)}`,
...step.actions.map((action) => ` - ${actionLine(action, step.on)}`),
" Find each target again before acting (app, window by title, then the element by its role, label and path); if one isn't there, look at the screen and do the rest yourself.",
].join("\n");
}
const where = step.kind === "app" ? appMeta(step.app).label : step.kind === "ai" ? "AI" : step.kind === "shell" ? "Command" : step.kind === "file" ? "File" : "Browser";
const used = Object.keys(args).filter((name) => JSON.stringify(step).includes(`"$arg":"${name}"`) || (step.kind === "shell" && step.run.includes(`OMB_ARG_${name.toUpperCase()}`)));
const detail = used.map((name) => `${args[name]!.label ?? name}: ${name}${args[name]!.default === undefined ? "" : `, default ${JSON.stringify(args[name]!.default)}`}`);
const what = step.kind === "shell" ? ` \`${step.run.split("\n", 1)[0]!.slice(0, 120)}\`` : step.kind === "app" ? ` (${step.tool})` : "";
const place = step.where && PLACE[step.where] && !(step.kind === "browser" && step.where === "browser") ? ` ${PLACE[step.where]}` : "";
const fork = caseOf(step);
const done = step.check && typeof step.check === "object" ? ` (done when: ${step.check.says.replace(/\s+/g, " ").slice(0, 200)})` : "";
return `${fork ? `[only when ${fork.decide} is "${fork.case}"] ` : ""}${where}${place}: ${step.label}${what}${detail.length ? ` (${detail.join("; ")})` : ""}${step.kind === "ai" ? " (your judgement)" : ""}${done}`;
}
/** SKILL.md for a saved burger: the steps in words, so a bot can follow them even where nothing replays workflow.json. */
export function renderWorkflowSkillMd(name: string, workflow: Workflow): string {
const title = workflow.summary.replace(/[.!?]+$/, "");
const lines = [
"---",
`name: ${name}`,
`description: ${yamlScalar(workflow.summary)}`,
`tags: ${WORKFLOW_TAG}`,
"---",
"",
`# ${title}`,
"",
`A workflow saved from a run in chat. Run it with the run_workflow tool (name "${name}"), putting values from the request in its args. Where that tool isn't available, do these steps in order with the values in ${WORKFLOW_FILE} beside this file; a step marked as your judgement is yours to do well, the rest repeat exactly.`,
"",
...workflow.steps.map((step, index) => `${index + 1}. ${stepLine(step, workflow.args)}`),
"",
];
return lines.join("\n");
}