Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { createServer } from "node:http";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { expect, it } from "vitest";
import { launchVerificationServer } from "../scripts/control-omb.ts";
import { fixtureApi } from "../scripts/testing/preview-fixture.ts";
import { hostTimeout } from "./testing/host-timeout.ts";
import { openSse } from "./testing/sse.ts";
import { isTitleCall } from "./testing/title-call.ts";
it("Mistral keys persist, rotate, clear and reach real HTTP turns without leaking into public state", async () => {
const received: string[] = [];
let reply = 0;
const provider = createServer(async (req, res) => {
if (req.url === "/v1/models") {
// Deliberately public: this must never be presented as authenticated.
res.setHeader("content-type", "application/json");
res.end(JSON.stringify({ data: [{ id: "fixture-model" }] }));
return;
}
if (req.url !== "/v1/chat/completions") { res.writeHead(404).end(); return; }
let body = "";
for await (const chunk of req) body += chunk;
if (isTitleCall(body)) { res.writeHead(500).end(); return; }
const key = req.headers.authorization ?? "";
received.push(key);
if (!["Bearer fixture-first-key", "Bearer fixture-replacement-key"].includes(key)) {
res.writeHead(401, { "content-type": "application/json" });
res.end(JSON.stringify({ error: { message: "Invalid fixture credential" } }));
return;
}
res.writeHead(200, { "content-type": "text/event-stream" });
res.end(`data: ${JSON.stringify({ choices: [{ delta: { content: `fixture reply ${++reply}` } }] })}\n\ndata: [DONE]\n\n`);
});
await new Promise<void>((resolve) => provider.listen(0, "127.0.0.1", resolve));
const address = provider.address();
if (!address || typeof address === "string") throw new Error("fixture provider has no port");
const fixture = await launchVerificationServer();
const api = fixtureApi(fixture.info.url);
const sse = await openSse(`${fixture.info.url}/api/events`);
try {
const missing = await fetch(`${fixture.info.url}/api/keys/test`, {
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ provider: "mistral" }),
});
expect(missing.status).toBe(400);
const configPath = join(fixture.info.dataDir, "config.json");
const config = JSON.parse(readFileSync(configPath, "utf8"));
config.instances.mistral = { driver: "mistral", config: { url: `http://127.0.0.1:${address.port}/v1` } };
writeFileSync(configPath, JSON.stringify(config));
const status = await api("PUT", "/api/config", {
mistral: { key: "fixture-first-key" },
});
expect(status.mistral.configured).toBe(true);
expect(JSON.stringify(status)).not.toContain("fixture-first-key");
expect(await api("POST", "/api/keys/test", { provider: "mistral", url: `http://127.0.0.1:${address.port}/v1` }))
.toEqual({ ok: true, check: "models", models: ["fixture-model"] });
for (const key of ["", " ", 123]) {
const response = await fetch(`${fixture.info.url}/api/keys/test`, {
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ provider: "mistral", key }),
});
expect(response.status).toBe(400);
}
const { bot } = await api("POST", "/api/bots", {
name: "Mistral fixture", modelSelection: { instanceId: "mistral", model: "fixture-model" },
});
const send = async (expectedReply: string) => {
await api("POST", `/api/bots/${bot.id}/messages`, { text: "Reply briefly for the credential test." });
const replyFrame = await sse.until((frame) => frame.kind === "message" && frame.threadId === bot.threadId
&& frame.message?.role === "bot" && frame.message?.text === expectedReply);
const { bots } = await api("GET", "/api/bots?messages=10");
const current = bots.find((candidate: { id: string }) => candidate.id === bot.id);
// Tool cleanup may keep the turn busy after its reply: await a newer idle status before saving.
if (current.busy) await sse.until((frame) => frame.kind === "bot" && frame.bot?.id === bot.id && !frame.bot.busy
&& frame.seq > replyFrame.seq);
};
await send("fixture reply 1");
// The first save installed an env value and created the runtime; a second must replace both.
await api("PUT", "/api/config", { mistral: { key: "fixture-replacement-key" } });
await send("fixture reply 2");
expect(received).toEqual(["Bearer fixture-first-key", "Bearer fixture-replacement-key"]);
const disk = JSON.parse(readFileSync(join(fixture.info.dataDir, "config.json"), "utf8"));
expect(disk.mistral.key).toBe("fixture-replacement-key");
const cleared = await api("PUT", "/api/config", { mistral: { key: "" } });
expect(cleared.mistral.configured).toBe(false);
const { instances } = await api("GET", "/api/instances");
expect(instances.find((instance: { instanceId: string }) => instance.instanceId === "mistral").snapshot.state).toBe("unavailable");
const publicState = JSON.stringify([await api("GET", "/api/bots"), await api("GET", "/api/config"), sse.frames]);
const logs = readFileSync(fixture.info.logPath, "utf8");
for (const key of ["fixture-first-key", "fixture-replacement-key"]) {
expect(publicState).not.toContain(key);
expect(logs).not.toContain(key);
}
} finally {
sse.close();
await fixture.close();
await new Promise<void>((resolve) => provider.close(() => resolve()));
}
}, hostTimeout(60_000));