Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import "../testing/scrub-ambient-omb-env.ts";
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
import type { AddressInfo } from "node:net";
import { tmpdir } from "node:os";
import { basename, dirname, join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fromMarkdown } from "mdast-util-from-markdown";
import { htmlFallbackText } from "../../shared/app.ts";
import { parseWidgetInput, widgetToText } from "../../shared/widget.ts";
import { WIDGET_SAMPLES } from "../../shared/widget-samples.ts";
import { removeTempDir } from "../testing/cleanup.ts";
import type { HeadlessIo } from "./output.ts";
import { safeName } from "./export.ts";
import { runHeadless } from "./main.ts";
const dir = mkdtempSync(join(tmpdir(), "omb-export-"));
let server: Server;
let origin: string;
const LEAD = "lead-thread-0001";
const PRIYA = "priya-thread-0002";
const KEY = "sk-ant-api03-abcdefghijklmnopqrstuvwxyz0123456789";
const BIG = Buffer.alloc(3_000, 7);
const REPORTS: Record<string, Record<string, string>> = {
"project-a/report/": { "index.html": '<!doctype html><link rel="stylesheet" href="style.css"><p>First report</p>', "style.css": "body { color: red; }\n" },
"project-b/report/": { "index.html": '<!doctype html><link rel="stylesheet" href="style.css"><p>Second report</p>', "style.css": "body { color: blue; }\n" },
};
const messages: Record<string, unknown[]> = {
[LEAD]: [
{ id: "u1", role: "user", kind: "text", text: `Rehearse it. The key is ${KEY}` },
{ id: "m1", role: "bot", kind: "text", text: "Report: [report](report.md), [raw](big.bin), [shots](shots/), [secret](../../etc/passwd)" },
{ id: "s1", role: "bot", kind: "screen" },
{ id: "s2", role: "bot", kind: "screen" },
],
[PRIYA]: [{ id: "p1", role: "bot", kind: "text", text: "Pricing was hidden.\n\n" }],
};
function answer(req: IncomingMessage, res: ServerResponse, raw: string) {
const url = new URL(req.url ?? "/", "http://localhost");
const send = (status: number, body: unknown, headers: Record<string, string> = {}) => {
res.writeHead(status, { "content-type": "application/json", ...headers });
res.end(JSON.stringify(body));
};
if (url.pathname === "/api/bots") return send(200, { bots: [{ id: "lead", name: "Room Lead", threadId: LEAD, tasks: [{ threadId: LEAD }] }], groups: [] });
if (url.pathname === `/api/threads/${LEAD}/job`) {
return send(200, { threadId: LEAD, state: "completed", reason: "Room Lead finished", since: 0, version: "v1", nodes: [
{ id: "r1", rootId: "r1", botName: "Room Lead", threadId: LEAD, status: "completed", result: "Report" },
{ id: "a1", rootId: "r1", botName: "Priya", threadId: PRIYA, threadTitle: "Visit", status: "completed", result: `Saw ${KEY}` },
] });
}
const exported = /^\/api\/threads\/([\w-]+)\/export$/.exec(url.pathname);
if (exported && url.searchParams.get("format") === "json") return send(200, { name: "Rehearsal", threadId: exported[1], messages: messages[exported[1]!] ?? [] });
const image = /^\/api\/threads\/([\w-]+)\/messages\/(s\d)\/image$/.exec(url.pathname);
if (image) {
// s2's pixels are gone: the server no longer keeps them
if (image[2] === "s2") return send(404, { error: "no image on that message" });
res.writeHead(200, { "content-type": "image/png", "content-length": "4" });
return res.end("PNG!");
}
const file = /^\/api\/threads\/([\w-]+)\/messages\/([\w-]+)\/file$/.exec(url.pathname);
if (file && req.method === "POST") {
const body = JSON.parse(raw) as { path: string; entry?: string; listFolder?: boolean };
if (body.path === "plain/report") {
res.writeHead(200, { "content-type": "text/plain", "content-disposition": 'attachment; filename="report"' });
return res.end("A standalone report");
}
const report = REPORTS[body.path];
if (report && body.entry && report[body.entry] !== undefined) {
const text = report[body.entry]!;
res.writeHead(200, { "content-type": "text/plain", "content-length": String(Buffer.byteLength(text)) });
return res.end(text);
}
if (report && body.listFolder) {
return send(200, { name: "report", entries: Object.entries(report).map(([name, text]) => ({ name, bytes: Buffer.byteLength(text) })), truncated: false }, { "x-openmausbot-folder": "1" });
}
if (body.path === "old.md") {
res.writeHead(200, { "content-type": "text/markdown", "content-length": "3" });
return res.end("old");
}
if (body.path === "report.md") {
res.writeHead(200, { "content-type": "text/markdown", "content-length": "15", "content-disposition": "attachment; filename=\"report.md\"" });
return res.end("# Launch report");
}
if (body.path === "priya-1.png") {
res.writeHead(200, { "content-type": "image/png", "content-length": "3" });
return res.end("png");
}
if (body.path === "big.bin") {
// no content-length: sent in chunks, as a proxy may
res.writeHead(200, { "content-type": "application/octet-stream" });
res.write(BIG.subarray(0, 1_500));
return res.end(BIG.subarray(1_500));
}
if (body.path === "shots/" && body.entry) {
res.writeHead(200, { "content-type": "image/png", "content-length": String(body.entry.length) });
return res.end(body.entry);
}
if (body.path === "shots/" && body.listFolder) {
return send(200, { name: "shots", entries: [{ name: "a.png", bytes: 5 }, { name: "b.png", bytes: 5 }], truncated: false }, { "x-openmausbot-folder": "1" });
}
return send(403, { error: "that bot message does not link to this file" });
}
send(404, { error: "not found" });
}
beforeAll(async () => {
server = createServer(async (req, res) => {
let raw = "";
for await (const chunk of req) raw += chunk;
answer(req, res, raw);
});
await new Promise<void>((ready) => server.listen(0, "127.0.0.1", ready));
origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
});
afterAll(async () => {
await new Promise((done) => server.close(done));
await removeTempDir(dir);
});
const run = async (argv: string[]) => {
const out: string[] = [];
const err: string[] = [];
const io: HeadlessIo = { out: (text) => out.push(text), err: (text) => err.push(text), readStdin: async () => "" };
const code = await runHeadless(argv, { io, env: { OPENMAUSBOT_URL: origin } });
return { code, result: out.length ? JSON.parse(out.join("\n")) : undefined, err };
};
async function exportReports(out: string, force = false, standalone?: "first" | "last") {
const links = Object.keys(REPORTS).map((path) => `[Report](${path})`);
if (standalone === "first") links.unshift("[Standalone](plain/report)");
if (standalone === "last") links.push("[Standalone](plain/report)");
const previous = messages[LEAD]!;
messages[LEAD] = [{ id: "reports", role: "bot", kind: "text", text: links.join("\n\n") }];
try {
return await run(["export", "--thread", LEAD, "--out", out, "--json", ...(force ? ["--force"] : [])]);
} finally {
messages[LEAD] = previous;
}
}
function checkReportFiles(out: string, result: { files: Array<{ link: string; entry?: string; file: string }> }): string[] {
const folders: string[] = [];
for (const [link, expected] of Object.entries(REPORTS)) {
const files = result.files.filter((file) => file.link === link);
expect(files.map((file) => file.entry)).toEqual(Object.keys(expected));
for (const file of files) {
expect(basename(file.file)).toBe(file.entry);
expect(readFileSync(join(out, file.file), "utf8")).toBe(expected[file.entry!]);
}
const html = files.find((file) => file.entry === "index.html")!;
const folder = dirname(html.file);
const stylesheet = /href="([^"]+)"/.exec(readFileSync(join(out, html.file), "utf8"))![1]!;
expect(readFileSync(join(out, folder, stylesheet), "utf8")).toBe(expected["style.css"]);
folders.push(folder);
}
expect(new Set(folders).size).toBe(2);
expect(JSON.parse(readFileSync(join(out, "manifest.json"), "utf8")).files).toEqual(result.files);
return folders;
}
describe("export", () => {
it.each(["```js\nconst answer = 42;", "~~~js\nconst answer = 42;"])("exports a follow-up after an unfinished code block: %s", async (text) => {
const previous = messages[LEAD]!;
messages[LEAD] = [
{ id: "partial", role: "bot", kind: "text", text },
{ id: "follow-up", role: "user", kind: "text", text: "Please use Python instead." },
];
try {
const out = mkdtempSync(join(dir, "unfinished-code-"));
const { code } = await run(["export", "--thread", LEAD, "--out", out, "--json"]);
expect(code).toBe(0);
const nodes = fromMarkdown(readFileSync(join(out, "transcripts/conversation-lead-thr.md"), "utf8")).children;
expect(nodes.filter((node) => node.type === "code").map((node) => node.value)).toEqual(["const answer = 42;"]);
expect(nodes.some((node) => node.type === "paragraph" && node.children.some((child) => child.type === "text" && child.value === "Please use Python instead."))).toBe(true);
} finally {
messages[LEAD] = previous;
}
});
it("keeps later messages readable when the transcript limit cuts a complete code block", async () => {
const previous = messages[LEAD]!;
const text = `\`\`\`text\n${"a line of output\n".repeat(4_000)}\`\`\``;
messages[LEAD] = [
{ id: "large-reply", role: "bot", kind: "text", text },
{ id: "follow-up", role: "user", kind: "text", text: "Summarize this output." },
];
try {
const out = mkdtempSync(join(dir, "truncated-code-"));
const { code } = await run(["export", "--thread", LEAD, "--out", out, "--json"]);
expect(code).toBe(0);
const saved = JSON.parse(readFileSync(join(out, "transcripts/conversation-lead-thr.json"), "utf8")) as { messages: Array<{ text: string }> };
expect(saved.messages[0]!.text).toBe(`${text.slice(0, 50_000)}…`);
const nodes = fromMarkdown(readFileSync(join(out, "transcripts/conversation-lead-thr.md"), "utf8")).children;
expect(nodes.find((node) => node.type === "code")?.value).toBe(fromMarkdown(saved.messages[0]!.text).children.find((node) => node.type === "code")?.value);
expect(nodes.at(-1)).toMatchObject({ type: "paragraph", children: [{ type: "text", value: "Summarize this output." }] });
} finally {
messages[LEAD] = previous;
}
});
it("keeps same-named linked report folders and their relative assets separate", async () => {
const out = join(dir, "report-folders");
const { code, result } = await exportReports(out);
expect(code).toBe(0);
checkReportFiles(out, result);
});
it.each(["first", "last"] as const)("keeps a same-named standalone file when it is linked %s", async (standalone) => {
const out = join(dir, `report-${standalone}`);
const { code, result } = await exportReports(out, false, standalone);
expect(code).toBe(0);
const folders = checkReportFiles(out, result);
const file = result.files.find((entry: { link: string }) => entry.link === "plain/report");
expect(folders).not.toContain(file.file);
expect(readFileSync(join(out, file.file), "utf8")).toBe("A standalone report");
});
it("keeps user files across repeated forced exports of whole report folders", async () => {
const out = join(dir, "report-force");
const occupied = join(out, "files/conversation-lead-thr/report");
mkdirSync(occupied, { recursive: true });
writeFileSync(join(occupied, "index.html"), "My existing report");
writeFileSync(join(occupied, "style.css"), "My existing stylesheet");
const first = await exportReports(out, true);
expect(first.code).toBe(0);
expect(readFileSync(join(occupied, "index.html"), "utf8")).toBe("My existing report");
const firstFolders = checkReportFiles(out, first.result);
const again = await exportReports(out, true);
expect(again.code).toBe(0);
expect(checkReportFiles(out, again.result)).toEqual(firstFolders);
const added = join(out, firstFolders[0]!, "notes.txt");
writeFileSync(added, "Keep these notes");
const updated = await exportReports(out, true);
expect(updated.code).toBe(0);
expect(checkReportFiles(out, updated.result)).not.toContain(firstFolders[0]);
expect(readFileSync(added, "utf8")).toBe("Keep these notes");
expect(readFileSync(join(occupied, "index.html"), "utf8")).toBe("My existing report");
expect(readFileSync(join(occupied, "style.css"), "utf8")).toBe("My existing stylesheet");
expect(readdirSync(join(out, firstFolders[0]!))).toEqual(["notes.txt"]);
});
it("keeps a trailing standalone file when forced exports retain report notes", async () => {
const out = join(dir, "report-force-standalone");
const first = await exportReports(out, false, "last");
expect(first.code).toBe(0);
const firstFolders = checkReportFiles(out, first.result);
const original = first.result.files.find((file: { link: string }) => file.link === "plain/report");
expect(original.file).toBe("files/conversation-lead-thr/report (3)");
const notes = join(out, firstFolders[0]!, "notes.txt");
writeFileSync(notes, "Keep these notes");
const updated = await exportReports(out, true, "last");
expect(updated.code, JSON.stringify(updated.result)).toBe(0);
const nextFolders = checkReportFiles(out, updated.result);
expect(nextFolders).not.toContain(firstFolders[0]);
const standalone = updated.result.files.find((file: { link: string }) => file.link === "plain/report");
expect(standalone.file).toBe("files/conversation-lead-thr/report (4)");
expect(readFileSync(join(out, standalone.file), "utf8")).toBe("A standalone report");
expect(readFileSync(notes, "utf8")).toBe("Keep these notes");
expect(readdirSync(join(out, firstFolders[0]!))).toEqual(["notes.txt"]);
const again = await exportReports(out, true, "last");
expect(again.code).toBe(0);
expect(checkReportFiles(out, again.result)).toEqual(nextFolders);
expect(again.result.files.find((file: { link: string }) => file.link === "plain/report").file).toBe(standalone.file);
expect(readFileSync(notes, "utf8")).toBe("Keep these notes");
});
it("writes the job, every transcript, the linked files and folders and the screens, within the byte limit", async () => {
const out = join(dir, "first");
const { code, result } = await run(["export", "--thread", LEAD, "--out", out, "--max-bytes", "2KB", "--json"]);
expect(code).toBe(0);
const files = (result.files as Array<{ file: string; link: string; entry?: string; bytes: number }>).map(({ file, link, entry, bytes }) => ({ file, link, entry, bytes }));
expect(files).toEqual([
{ file: "files/conversation-lead-thr/report.md", link: "report.md", entry: undefined, bytes: 15 },
{ file: "files/conversation-lead-thr/shots/a.png", link: "shots/", entry: "a.png", bytes: 5 },
{ file: "files/conversation-lead-thr/shots/b.png", link: "shots/", entry: "b.png", bytes: 5 },
{ file: "files/priya-visit-priya-th/priya-1.png", link: "priya-1.png", entry: undefined, bytes: 3 },
]);
expect(readFileSync(join(out, "files/conversation-lead-thr/report.md"), "utf8")).toBe("# Launch report");
expect(result.skipped).toEqual([
{ threadId: LEAD, messageId: "m1", link: "big.bin", reason: "more than --max-bytes 2000" },
{ threadId: LEAD, messageId: "m1", link: "../../etc/passwd", reason: "that bot message does not link to this file" },
]);
expect(readdirSync(join(out, "files/conversation-lead-thr"))).toEqual(["report.md", "shots"]);
expect(result.screens).toEqual([{ threadId: LEAD, messageId: "s1", file: "screens/conversation-lead-thr/001-s1.png", bytes: 4 }]);
expect(result.bytes).toBe(32);
expect(result.transcripts.map((transcript: { file: string }) => transcript.file)).toEqual([
"transcripts/conversation-lead-thr.json", "transcripts/priya-visit-priya-th.json"]);
for (const file of ["transcripts/conversation-lead-thr.json", "transcripts/conversation-lead-thr.md", "job.json", "manifest.json"]) {
const text = readFileSync(join(out, file), "utf8");
expect(text, file).not.toContain(KEY);
}
expect(JSON.parse(readFileSync(join(out, "manifest.json"), "utf8"))).toMatchObject({ ok: true, threadId: LEAD, state: "completed" });
});
it("refuses a folder with files in it before reading anything, and --force replaces an earlier export's files", async () => {
const out = join(dir, "again");
expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
const refused = await run(["export", "--thread", LEAD, "--out", out, "--json"]);
expect(refused).toMatchObject({ code: 2, result: { ok: false, error: `${out} already has files in it` } });
const forced = await run(["export", "--thread", LEAD, "--out", out, "--force", "--json"]);
expect(forced.code).toBe(0);
expect(readdirSync(join(out, "files/conversation-lead-thr")).sort()).toEqual(["big.bin", "report.md", "shots"]);
expect(existsSync(join(out, "files/conversation-lead-thr/report (2).md"))).toBe(false);
});
it("leaves nothing of an earlier job beside the new one under --force, and nothing it did not write is touched", async () => {
const out = join(dir, "replaced");
const lead = messages[LEAD]!;
messages[LEAD] = [...lead, { id: "m2", role: "bot", kind: "text", text: "Earlier: [old](old.md)" }, { id: "s3", role: "bot", kind: "screen" }];
try {
expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
} finally {
messages[LEAD] = lead;
}
expect(existsSync(join(out, "files/conversation-lead-thr/old.md"))).toBe(true);
expect(existsSync(join(out, "screens/conversation-lead-thr/002-s3.png"))).toBe(true);
writeFileSync(join(out, "notes.txt"), "mine");
const forced = await run(["export", "--thread", LEAD, "--out", out, "--force", "--json"]);
expect(forced.code).toBe(0);
expect(existsSync(join(out, "files/conversation-lead-thr/old.md"))).toBe(false);
expect(readdirSync(join(out, "screens/conversation-lead-thr"))).toEqual(["001-s1.png"]);
expect(readFileSync(join(out, "notes.txt"), "utf8")).toBe("mine");
const manifest = JSON.parse(readFileSync(join(out, "manifest.json"), "utf8"));
expect(manifest.files.map((file: { file: string }) => file.file)).not.toContain("files/conversation-lead-thr/old.md");
// a manifest naming something outside the folder never removes it
writeFileSync(join(dir, "outside.txt"), "not the export's");
writeFileSync(join(out, "manifest.json"), JSON.stringify({ ...manifest, files: [...manifest.files, { file: "../outside.txt" }, { file: join(dir, "outside.txt") }] }));
expect((await run(["export", "--thread", LEAD, "--out", out, "--force", "--json"])).code).toBe(0);
expect(readFileSync(join(dir, "outside.txt"), "utf8")).toBe("not the export's");
});
it("writes each transcript's .md from the same messages as its .json: the latest 2,000, secrets removed", async () => {
const out = join(dir, "long");
const lead = messages[LEAD]!;
messages[LEAD] = [{ id: "first", role: "user", kind: "text", text: "The very first message" },
...Array.from({ length: 2_050 }, (_, i) => ({ id: `n${i}`, role: i % 2 ? "bot" : "user", kind: "text", text: `message ${i}`, ...(i % 2 ? { from: { name: "Room Lead" } } : {}) })),
{ id: "last", role: "bot", kind: "text", text: `Launch report, sent with ${KEY}` }];
try {
expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
} finally {
messages[LEAD] = lead;
}
const json = JSON.parse(readFileSync(join(out, "transcripts/conversation-lead-thr.json"), "utf8"));
expect(json.messages).toHaveLength(2_000);
const markdown = readFileSync(join(out, "transcripts/conversation-lead-thr.md"), "utf8");
expect(markdown.startsWith("# Rehearsal\n")).toBe(true);
expect(markdown).toContain("**Room Lead:**\n\nLaunch report, sent with «redacted");
expect(markdown).not.toContain(KEY);
expect(markdown).not.toContain("The very first message");
expect(markdown).toContain("**User:**\n\nmessage 52");
});
it("names every file so any system can write it: no path, no device name, no trailing dot", () => {
expect(safeName("../../etc/passwd")).toBe(".._.._etc_passwd");
expect(safeName("report.md")).toBe("report.md");
// Windows reads these as devices, and drops a trailing dot or space
expect(["con.md", "AUX", "nul.txt", "com1.log", "lpt9"].map(safeName)).toEqual(["_con.md", "_AUX", "_nul.txt", "_com1.log", "_lpt9"]);
expect(["notes.", "draft ", "..", "."].map(safeName)).toEqual(["notes", "draft", "file", "file"]);
expect(safeName("console.md")).toBe("console.md");
});
it.each([".pdf", ".xlsx"])("keeps %s when shortening a long exported file name", (extension) => {
const name = safeName(`${"annual-report-".repeat(12)}${extension}`);
expect(name.endsWith(extension)).toBe(true);
expect(Buffer.byteLength(name)).toBeLessThanOrEqual(120);
});
it("shortens multilingual names on complete characters while keeping their extension", () => {
const name = safeName(`${"季度报告📊".repeat(12)}.xlsx`);
expect(name.endsWith(".xlsx")).toBe(true);
expect(Buffer.byteLength(name)).toBeLessThanOrEqual(120);
expect(Buffer.from(name).toString("utf8")).toBe(name);
expect(name).not.toContain("�");
});
it.each(["widget", "app"])("keeps %s fallback text in the Markdown transcript", async (kind) => {
const parsed = parseWidgetInput(WIDGET_SAMPLES.pullRequests);
if (!parsed.ok) throw new Error(parsed.error);
const text = kind === "widget" ? widgetToText(parsed.value) : htmlFallbackText("Sales by month", "<h1>Sales</h1><p>Up 12%</p>");
const out = join(dir, kind);
const lead = messages[LEAD]!;
messages[LEAD] = [
{ id: "before", role: "user", kind: "text", text: "Show the result" },
{ id: "card", role: "bot", kind, text },
{ id: "after", role: "user", kind: "text", text: "Thanks for the result" },
];
try {
expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
} finally {
messages[LEAD] = lead;
}
const transcript = JSON.parse(readFileSync(join(out, "transcripts/conversation-lead-thr.json"), "utf8"));
expect(transcript.messages[1]).toMatchObject({ kind, text });
const markdown = readFileSync(join(out, "transcripts/conversation-lead-thr.md"), "utf8");
expect(markdown).toContain(`**Room Lead:**\n\n${text}`);
expect(markdown.indexOf(text)).toBeGreaterThan(markdown.indexOf("Show the result"));
expect(markdown.indexOf("Thanks for the result")).toBeGreaterThan(markdown.indexOf(text));
});
it("keeps one job of the conversation with --root, and says so when there is none", async () => {
const missing = await run(["export", "--thread", LEAD, "--out", join(dir, "none"), "--root", "nope", "--json"]);
expect(missing).toMatchObject({ code: 2, result: { error: "no job with root nope in this conversation" } });
});
it("leaves an earlier export intact when --force is paired with an unknown job root", async () => {
const out = join(dir, "invalid-root");
expect((await run(["export", "--thread", LEAD, "--out", out, "--json"])).code).toBe(0);
const saved = [
"manifest.json", "job.json", "transcripts/conversation-lead-thr.json", "transcripts/conversation-lead-thr.md",
"files/conversation-lead-thr/report.md", "screens/conversation-lead-thr/001-s1.png",
].map((file) => ({ file, bytes: readFileSync(join(out, file)) }));
const refused = await run(["export", "--thread", LEAD, "--out", out, "--force", "--root", "nope", "--json"]);
expect(refused).toMatchObject({ code: 2, result: { error: "no job with root nope in this conversation" } });
for (const { file, bytes } of saved) expect(readFileSync(join(out, file))).toEqual(bytes);
});
});