Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

7.6 KB
Raw Blame
import { afterAll, beforeAll, expect, it } from "vitest";
import { launchVerificationServer, runControlOmb, type VerificationServer } from "../scripts/control-omb.ts";

const sonnet = { instanceId: "claude", model: "claude-sonnet-5" };
const opus = { instanceId: "claude", model: "claude-opus-5" };
const haiku = { instanceId: "claude", model: "claude-haiku-4-5" };
let session: VerificationServer;

beforeAll(async () => {
  session = await launchVerificationServer(process.env, undefined, undefined, undefined, undefined, undefined, ["codex"]);
}, 60_000);
afterAll(async () => { await session?.close(); });

async function api(method: string, path: string, body?: unknown, token?: string) {
  const response = await fetch(session.info.url + path, {
    method, headers: { "content-type": "application/json", ...(token ? { authorization: `Bearer ${token}` } : { origin: session.info.url }) },
    ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(30_000),
  });
  return { status: response.status, body: await response.json() as any };
}

async function ok(method: string, path: string, body?: unknown) {
  const result = await api(method, path, body);
  expect(result.status, `${method} ${path}: ${JSON.stringify(result.body)}`).toBeLessThan(300);
  return result.body;
}

async function newBot(name: string) {
  const { bot } = await runControlOmb(["new-bot", "--name", name, "--url", session.info.url]) as { bot: { id: string } };
  await ok("PATCH", `/api/bots/${bot.id}/tasks/${(await profile(bot.id)).threadId}`, { modelSelection: sonnet, updateBotDefault: true });
  return bot.id;
}
const profile = async (botId: string) => (await ok("GET", "/api/bots")).bots.find((bot: any) => bot.id === botId);
const thread = async (botId: string, threadId: string) => (await profile(botId)).tasks.find((task: any) => task.threadId === threadId);
const newThread = async (botId: string, title: string) => (await ok("POST", `/api/bots/${botId}/tasks`, { title, select: false })).task.threadId as string;

it("keeps a pick in a thread to that thread, starts new threads on it, and lets a thread go back to the bot's model", async () => {
  const botId = await newBot("Pick keeper");
  const a = await newThread(botId, "A");
  const b = await newThread(botId, "B");

  await ok("PATCH", `/api/bots/${botId}/tasks/${a}`, { modelSelection: opus });
  expect(await thread(botId, a)).toMatchObject({ modelSelection: opus, followsBotModel: false });
  expect(await thread(botId, b)).toMatchObject({ modelSelection: sonnet, followsBotModel: true });
  expect(await profile(botId)).toMatchObject({ modelSelection: sonnet, newThreadModel: opus });

  const c = await newThread(botId, "C");
  expect(await thread(botId, c)).toMatchObject({ modelSelection: opus, followsBotModel: false });
  expect(await thread(botId, b)).toMatchObject({ modelSelection: sonnet, followsBotModel: true });

  // A reasoning pick is a pick too.
  await ok("PATCH", `/api/bots/${botId}/tasks/${c}`, { modelSelection: { ...opus, effort: "high" } });
  expect((await profile(botId)).newThreadModel).toEqual({ ...opus, effort: "high" });
  expect(await thread(botId, a)).toMatchObject({ modelSelection: opus });

  // "Use the bot's model": the thread follows again, and so do new threads.
  await ok("PATCH", `/api/bots/${botId}/tasks/${c}`, { modelSelection: sonnet });
  expect(await thread(botId, c)).toMatchObject({ modelSelection: sonnet, followsBotModel: true });
  expect((await profile(botId)).newThreadModel).toBeUndefined();
  const d = await newThread(botId, "D");
  expect(await thread(botId, d)).toMatchObject({ modelSelection: sonnet, followsBotModel: true });
}, 60_000);

it("changes the bot's model, and every thread that follows it, only from the bot itself", async () => {
  const botId = await newBot("Bot-wide");
  const main = (await profile(botId)).threadId as string;
  const pinned = await newThread(botId, "Pinned");
  const follows = await newThread(botId, "Follows");
  await ok("PATCH", `/api/bots/${botId}/tasks/${pinned}`, { modelSelection: opus });
  expect((await profile(botId)).newThreadModel).toEqual(opus);

  await ok("PATCH", `/api/bots/${botId}/tasks/${main}`, { modelSelection: haiku, updateBotDefault: true });
  expect(await profile(botId)).toMatchObject({ modelSelection: haiku });
  expect((await profile(botId)).newThreadModel).toBeUndefined();
  expect(await thread(botId, follows)).toMatchObject({ modelSelection: haiku, followsBotModel: true });
  expect(await thread(botId, pinned)).toMatchObject({ modelSelection: opus, followsBotModel: false });
  expect(await thread(botId, await newThread(botId, "After"))).toMatchObject({ modelSelection: haiku, followsBotModel: true });

  await ok("PATCH", `/api/bots/${botId}/tasks/${pinned}`, { modelSelection: sonnet });
  await ok("PATCH", `/api/bots/${botId}`, { modelSelection: opus });
  expect((await profile(botId)).newThreadModel).toBeUndefined();
  expect(await thread(botId, await newThread(botId, "After settings"))).toMatchObject({ modelSelection: opus, followsBotModel: true });

  await ok("PATCH", `/api/bots/${botId}/tasks/${pinned}`, { modelSelection: haiku });
  await ok("PATCH", `/api/bots/${botId}/model`, sonnet);
  expect(await profile(botId)).toMatchObject({ modelSelection: sonnet });
  expect((await profile(botId)).newThreadModel).toBeUndefined();
}, 60_000);

it("starts a new thread on the bot's model while the last pick's engine cannot run", async () => {
  const botId = await newBot("Gone engine");
  const threadId = (await profile(botId)).threadId as string;
  const gone = { instanceId: "no-such-engine", model: "old-model" };
  await ok("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: gone, resetApprovalToAsk: true });
  expect((await profile(botId)).newThreadModel).toEqual(gone);
  expect(await thread(botId, await newThread(botId, "Still works"))).toMatchObject({ modelSelection: sonnet, followsBotModel: true });
}, 60_000);

it("never lets a chat-only session move where new threads start", async () => {
  const botId = await newBot("Shared");
  const threadId = (await profile(botId)).threadId as string;
  const code = (await ok("POST", "/api/auth/pairing", { scopes: ["client"] })).code;
  const token = (await ok("POST", "/api/auth/pair", { code })).token as string;
  const picked = await api("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: opus }, token);
  expect(picked.status, JSON.stringify(picked.body)).toBe(200);
  expect(await thread(botId, threadId)).toMatchObject({ modelSelection: opus, followsBotModel: false });
  expect((await profile(botId)).newThreadModel).toBeUndefined();
  expect(await thread(botId, await newThread(botId, "Owner's next"))).toMatchObject({ modelSelection: sonnet, followsBotModel: true });
}, 60_000);

it("starts a new thread on a picked engine its bot's level does not carry to at Ask", async () => {
  const botId = await newBot("Level keeper");
  const threadId = (await profile(botId)).threadId as string;
  await ok("PATCH", `/api/bots/${botId}`, { approvalMode: "edits" });
  expect((await profile(botId)).approvalMode).toBe("edits");
  const codex = { instanceId: "codex", model: (await ok("GET", "/api/instances")).instances.find((entry: any) => entry.instanceId === "codex").models.default };
  await ok("PATCH", `/api/bots/${botId}/tasks/${threadId}`, { modelSelection: codex, resetApprovalToAsk: true });
  expect((await profile(botId)).newThreadModel).toEqual(codex);
  const started = await thread(botId, await newThread(botId, "On Codex"));
  expect(started).toMatchObject({ modelSelection: codex, followsBotModel: false, approvalMode: "ask" });
  expect((await profile(botId)).approvalMode).toBe("edits");
}, 60_000);