Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import assert from "node:assert/strict";
import test from "node:test";
import { reopenSavedServer, reopenSavedServerAtLaunch, savedServerLook, serverAnswers, serverOpens } from "./saved-server.mjs";
const office = { id: "vps", name: "pro cloud", origin: "https://omb-u-gone.fly.dev" };
const answering = (status) => async () => new Response("{}", { status });
const gone = async () => { throw new TypeError("fetch failed"); };
const asked = (responses) => {
const shown = [];
return { shown, dialog: { showMessageBox: async (options) => { shown.push(options); return { response: responses.shift() ?? 2 }; } } };
};
test("a server answers with any status but a proxy's 5xx; a name that doesn't resolve, a refused or a slow one doesn't", async () => {
for (const status of [200, 401, 404]) assert.equal(await serverAnswers(office.origin, answering(status)), true, String(status));
for (const status of [502, 503]) assert.equal(await serverAnswers(office.origin, answering(status)), false, String(status));
assert.equal(await serverAnswers(office.origin, gone), false);
const slow = (_url, init) => new Promise((_, fail) => init.signal.addEventListener("abort", () => fail(init.signal.reason)));
assert.equal(await serverAnswers(office.origin, slow, 20), false, "no answer within the look");
});
test("the window goes to a server that answers without asking anything", async () => {
const { shown, dialog } = asked([]);
assert.equal(await serverOpens(office, { fetch: answering(200), dialog, forget: () => assert.fail("nothing to forget") }), true);
assert.deepEqual(shown, []);
});
test("one that doesn't answer is the person's choice: Cancel stays put, Remove forgets it, Try again looks once more", async () => {
const cancel = asked([2]);
assert.equal(await serverOpens(office, { fetch: gone, dialog: cancel.dialog, forget: () => assert.fail("not asked to") }), false);
assert.equal(cancel.shown[0].message, "pro cloud isn't answering");
assert.match(cancel.shown[0].detail, /omb-u-gone\.fly\.dev/);
assert.deepEqual(cancel.shown[0].buttons, ["Try again", "Remove from list", "Cancel"]);
const forgotten = [];
const remove = asked([1]);
assert.equal(await serverOpens(office, { fetch: gone, dialog: remove.dialog, forget: (id) => forgotten.push(id) }), false);
assert.deepEqual(forgotten, ["vps"]);
let tries = 0;
const back = async () => (++tries < 2 ? gone() : new Response("{}", { status: 200 }));
const retry = asked([0]);
assert.equal(await serverOpens(office, { fetch: back, dialog: retry.dialog, forget: () => assert.fail("not asked to") }), true, "it answered the second time");
assert.equal(retry.shown.length, 1);
});
test("the way back opens a saved server that answers, and gives up on one that left the list or a newer show() took over", async () => {
const entry = { id: "vps", origin: office.origin };
let opened = null;
const open = (more = {}) => ({ saved: () => [entry], fetch: answering(200), opened: (found) => { opened = found; return true; }, ...more });
assert.equal(await reopenSavedServer({ origin: office.origin }, open()), true, "saved here and answering: it opens");
assert.equal(opened.id, "vps");
assert.equal(await reopenSavedServer({ origin: "https://elsewhere.example.test" }, open()), false, "not saved here any more");
assert.equal(await reopenSavedServer({ origin: office.origin }, open({ fetch: gone })), false, "nothing answers at it");
assert.equal(await reopenSavedServer({ origin: office.origin }, open({ still: () => false })), false, "a newer show() took over");
assert.equal(await reopenSavedServer({ origin: null }, open()), false, "no server named");
let looks = 0;
opened = null;
const left = () => { looks += 1; return looks === 1 ? [entry] : []; };
assert.equal(await reopenSavedServer({ origin: office.origin }, { saved: left, fetch: answering(200), opened: (found) => { opened = found; return true; } }), false, "forgotten while it was asked");
assert.equal(opened, null, "never opened the environment that is now active");
});
/** A server at `office.origin`: its descriptor's id, and what its session check says to this app's request. */
function server({ id = "env-office", session = { status: 401, body: { error: "unauthorized: this browser is not signed in" } }, down = false } = {}) {
const asked = [];
const fetch = async (url, init) => {
asked.push([new URL(url).pathname, init.credentials]);
if (down) throw new TypeError("fetch failed");
if (url.endsWith("/.well-known/openmausbot/environment")) return Response.json({ environmentId: id, label: "DESKTOP-1" });
return Response.json(session.body, { status: session.status });
};
return { asked, fetch };
}
const own = (id) => async () => id;
test("at launch a saved server is in only with this app's session there; this computer's own and a pairing page are not", async () => {
const signedIn = server({ session: { status: 200, body: { kind: "session", id: "s-1", environmentId: "env-office" } } });
assert.equal(await savedServerLook(office.origin, { fetch: signedIn.fetch, ownEnvironmentId: own("env-mine") }), "in");
assert.deepEqual(signedIn.asked.sort(), [["/.well-known/openmausbot/environment", "omit"], ["/api/auth/session", "include"]], "the session check carries this app's cookie");
assert.equal(await savedServerLook(office.origin, { fetch: server().fetch, ownEnvironmentId: own("env-mine") }), "sign-in", "the sign-in gate");
assert.equal(await savedServerLook(office.origin, { fetch: server({ session: { status: 403, body: { error: "forbidden: loopback host required" } } }).fetch, ownEnvironmentId: own("env-mine") }), "sign-in");
const loopback = server({ session: { status: 200, body: { kind: "loopback", environmentId: "env-other" } } });
assert.equal(await savedServerLook(office.origin, { fetch: loopback.fetch, ownEnvironmentId: own("env-mine") }), "sign-in",
"another server on this machine lets this process in as loopback, but the window's page would still have to pair");
assert.equal(await savedServerLook(office.origin, { fetch: server({ id: "env-mine" }).fetch, ownEnvironmentId: own("env-mine") }), "own", "this computer's own, by its descriptor");
const mineSignedIn = server({ id: "env-mine", session: { status: 200, body: { kind: "session", id: "s-2", environmentId: "env-mine" } } });
assert.equal(await savedServerLook(office.origin, { fetch: mineSignedIn.fetch, ownEnvironmentId: own("env-mine") }), "own", "even with a session there, it is never a place of its own");
assert.equal(await savedServerLook(office.origin, { fetch: server({ id: "env-mine" }).fetch, ownEnvironmentId: async () => { throw new Error("not ready"); } }), "sign-in",
"this computer's server not ready: not known as its own, still not opened unasked");
assert.equal(await savedServerLook(office.origin, { fetch: server({ down: true }).fetch, ownEnvironmentId: own("env-mine") }), "gone");
assert.equal(await savedServerLook(office.origin, { fetch: answering(503), ownEnvironmentId: own("env-mine") }), "gone");
});
test("the launch opens a saved server only where it is in; otherwise it says why, after main puts the window back on This computer", async () => {
const saved = () => [office];
const run = async (fetch, list = saved) => {
const said = [];
const result = await reopenSavedServerAtLaunch(office, { saved: list, fetch, ownEnvironmentId: own("env-mine"), opened: async (entry) => { said.push(`opened:${entry.id}`); return true; }, declined: () => said.push("declined") });
return { result, said };
};
assert.deepEqual(await run(server({ session: { status: 200, body: { kind: "session", id: "s-1" } } }).fetch), { result: true, said: ["opened:vps"] });
assert.deepEqual(await run(server().fetch), { result: "sign-in", said: ["declined"] });
assert.deepEqual(await run(server({ id: "env-mine" }).fetch), { result: "own", said: ["declined"] });
assert.deepEqual(await run(server({ down: true }).fetch), { result: false, said: [] }, "nothing answers: the old fallback");
assert.deepEqual(await run(server().fetch, () => []), { result: false, said: [] }, "forgotten meanwhile");
});