Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { Children, createElement, isValidElement, type ReactElement, type ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Bot, ConfigStatus, Group, Message } from "@/state/store";
const fixture = vi.hoisted(() => ({ config: null as ConfigStatus | null, dispatch: vi.fn() }));
vi.mock("@/state/store", async (original) => ({
...await original<typeof import("@/state/store")>(),
useStore: () => ({ state: { config: fixture.config }, dispatch: fixture.dispatch }),
}));
// The selector reads the same fixture, so a component can still be called as a plain function below.
vi.mock("@/state/store-select", async (original) => ({
...await original<typeof import("@/state/store-select")>(),
useStoreSelector: <T,>(select: (state: { config: ConfigStatus | null }) => T) => select({ config: fixture.config }),
useStoreDispatch: () => fixture.dispatch,
}));
vi.mock("./DesktopCapabilities", () => ({ useDesktopCapabilities: () => ({}) }));
import { Transcript } from "./GroupView";
import { DefaultResponderSelect } from "./RoomHeader";
import { Select } from "./Select";
import { RoutedByLine } from "./RoomQueueList";
import { loadLocale, setLocale } from "@/lib/i18n";
const members = [
{ id: "maya", name: "Maya" },
{ id: "theo", name: "Theo" },
] as Bot[];
const room = (defaultResponder: Group["defaultResponder"]) => ({ id: "room", defaultResponder } as Group);
const jev = (enabled: boolean): ConfigStatus => ({ decider: { provider: "jev", configured: enabled, enabled, jobs: { roomRouting: true } } } as ConfigStatus);
type Node = ReactElement<Record<string, unknown> & { children?: ReactNode }>;
function nodes(value: ReactNode): Node[] {
return Children.toArray(value).flatMap((child) => {
if (!isValidElement(child)) return [];
const node = child as Node;
return [node, ...nodes(node.props.children)];
});
}
beforeEach(() => {
fixture.config = jev(true);
fixture.dispatch.mockReset();
});
describe("room responder selector", () => {
it.each(["jev", "workersai", "openrouter", "custom"] as const)("describes automatic routing without naming a different provider (%s)", (provider) => {
fixture.config = { ...jev(true), decider: { ...jev(true).decider, provider } } as ConfigStatus;
const markup = renderToStaticMarkup(createElement(DefaultResponderSelect, { group: room({ kind: "auto" }), members }));
expect(markup).toContain('<option value="auto" selected="">Auto</option>');
expect(markup).toContain("The decision model picks who answers each plain message");
});
it("reads plain Auto, and names who answers, while the decision model is off", () => {
fixture.config = jev(false);
const markup = renderToStaticMarkup(createElement(DefaultResponderSelect, { group: room({ kind: "auto", fallbackBotId: "theo" }), members }));
expect(markup).toContain('<option value="auto" selected="">Auto</option>');
expect(markup).toContain("The decision model is off, so plain messages go to Theo");
});
it("uses the neutral wording in the Chinese selector with Workers AI", async () => {
fixture.config = { ...jev(true), decider: { ...jev(true).decider, provider: "workersai" } } as ConfigStatus;
await loadLocale("zh");
try {
const markup = renderToStaticMarkup(createElement(DefaultResponderSelect, { group: room({ kind: "auto" }), members }));
expect(markup).toContain('<option value="auto" selected="">自动</option>');
expect(markup).toContain("由决策模型挑选谁回复每一条普通消息");
expect(markup).not.toContain("Jev");
} finally {
setLocale("en");
}
});
it("switching a lead room to Auto keeps the lead as its fallback", () => {
const tree = DefaultResponderSelect({ group: room({ kind: "member", botId: "theo" }), members });
const select = nodes(tree).find((node) => node.type === Select)!;
(select.props.onChange as (event: { target: { value: string } }) => void)({ target: { value: "auto" } });
expect(fixture.dispatch).toHaveBeenCalledWith({ type: "patchGroup", groupId: "room", patch: { defaultResponder: { kind: "auto", fallbackBotId: "theo" } } });
});
});
describe("routed-by line", () => {
it("says Jev picked the speaker, with how sure it was", () => {
expect(renderToStaticMarkup(createElement(RoutedByLine, { routedBy: { provider: "jev", probability: 0.94 } }))).toContain("Picked by Jev · 94%");
});
it("names OpenRouter when that provider picked the speaker", () => {
expect(renderToStaticMarkup(createElement(RoutedByLine, { routedBy: { provider: "openrouter", probability: 0.81 } }))).toContain("Picked by OpenRouter · 81%");
});
it("appears under a Jev-routed reply only", () => {
const reply = (id: string, extra: Partial<Message> = {}): Message =>
({ id, role: "bot", kind: "text", text: `reply ${id}`, at: 1_000, from: { botId: "theo", name: "Theo", color: "green" }, ...extra });
const messages = [reply("routed", { routedBy: { provider: "jev", probability: 0.94 } }), reply("plain", { at: 2_000 })];
vi.stubGlobal("window", {});
const markup = renderToStaticMarkup(createElement(Transcript, {
group: { ...room({ kind: "auto" }), threadId: "thread", messages } as Group, members, locale: "en",
messages, transcript: messages, onReply: () => undefined,
}));
expect(markup.match(/Picked by Jev/g)).toHaveLength(1);
expect(markup.indexOf("Picked by Jev")).toBeGreaterThan(markup.indexOf("reply routed"));
expect(markup.indexOf("Picked by Jev")).toBeLessThan(markup.indexOf("reply plain"));
vi.unstubAllGlobals();
});
});