Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import type { IncomingMessage, ServerResponse } from "node:http";
import { join } from "node:path";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { DATA_DIR, engineLaunches, instanceConfigs, loadConfig, type AppConfig } from "../config.ts";
import { desktopMainMover } from "../move-back.ts";
import type { RequestAuth } from "../request-auth.ts";
import { CONFIG_RELOAD_PATH, createConfigReloadRoutes } from "./config-reload.ts";
import { PASS } from "./pass.ts";
const FILE = join(DATA_DIR, "config.json");
const SECRET = "sk-route-test-hidden-41";
const desktopMain: RequestAuth = { kind: "loopback", scopes: ["admin"], desktopMain: true };
const page: RequestAuth = { kind: "loopback", scopes: ["admin"] };
const phone: RequestAuth = { kind: "loopback", scopes: ["admin"], desktopMain: true, companionDevice: "phone-1" };
beforeEach(() => mkdirSync(DATA_DIR, { recursive: true }));
afterEach(() => rmSync(FILE, { force: true }));
function route(cfg: AppConfig, place = { desktopManaged: true, managed: false }) {
const changed = vi.fn();
const handler = createConfigReloadRoutes({
cfg, file: FILE, load: loadConfig, engines: (config) => engineLaunches(instanceConfigs(config)),
allowed: (auth) => desktopMainMover(auth, place), changed,
});
const call = async (auth: RequestAuth, method = "POST", path = CONFIG_RELOAD_PATH) => {
const json = vi.fn();
const out = await handler({ req: {} as IncomingMessage, res: {} as ServerResponse, url: new URL(`http://127.0.0.1${path}`), path, method, auth, json, readBody: vi.fn() });
return { out, status: json.mock.calls[0]?.[1], body: json.mock.calls[0]?.[2] };
};
return { call, changed };
}
it("reloads for the desktop app's own main process, and tells open pages once something was applied", async () => {
writeFileSync(FILE, JSON.stringify({ everyBotRules: "Be brief." }));
const cfg = loadConfig();
writeFileSync(FILE, JSON.stringify({ everyBotRules: "Be kind." }));
const { call, changed } = route(cfg);
expect(await call(desktopMain)).toMatchObject({ status: 200, body: { ok: true, applied: ["everyBotRules"], restart: [] } });
expect(cfg.everyBotRules).toBe("Be kind.");
expect(changed).toHaveBeenCalledOnce();
expect(await call(desktopMain)).toMatchObject({ status: 200, body: { ok: true, applied: [], restart: [] } });
expect(changed).toHaveBeenCalledOnce();
});
it("answers a bad file with 422 and its line, keeps the settings, and never echoes the file", async () => {
writeFileSync(FILE, JSON.stringify({ everyBotRules: "Be brief." }));
const cfg = loadConfig();
writeFileSync(FILE, `{\n "everyBotRules": "Be kind.",\n "anthropic": { "key": "${SECRET}" }\n "rooms": {}\n}\n`);
const { call, changed } = route(cfg);
const { status, body } = await call(desktopMain);
expect(status).toBe(422);
expect(body).toEqual({ ok: false, code: "json", error: "config.json line 4, column 3: unexpected quote (a comma before it may be missing)" });
expect(JSON.stringify(body)).not.toContain(SECRET);
expect(cfg.everyBotRules).toBe("Be brief.");
expect(changed).not.toHaveBeenCalled();
});
it("refuses a page, a phone, a shared or hosted server and a server the desktop does not run", async () => {
writeFileSync(FILE, JSON.stringify({ everyBotRules: "Be brief." }));
const cfg = loadConfig();
writeFileSync(FILE, JSON.stringify({ everyBotRules: "Be kind." }));
for (const [auth, place] of [
[page, { desktopManaged: true, managed: false }],
[phone, { desktopManaged: true, managed: false }],
[{ ...desktopMain, trust: "service" } as RequestAuth, { desktopManaged: true, managed: false }],
[desktopMain, { desktopManaged: true, managed: true }],
[desktopMain, { desktopManaged: false, managed: false }],
] as const) {
expect((await route(cfg, place).call(auth)).status).toBe(403);
}
expect(cfg.everyBotRules).toBe("Be brief.");
});
it("takes only POST on its own path", async () => {
const { call } = route(loadConfig());
expect((await call(desktopMain, "GET")).status).toBe(405);
expect((await call(desktopMain, "POST", "/api/config")).out).toBe(PASS);
});