Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// The computer a bot routine keeps from the chat it was made in, and the pin each of its runs starts with.
import { ROUTINE_COMPUTERS, type RoutineComputer } from "../shared/routines.ts";
import { chosenPin } from "./surface.ts";
import type { Routine, RoutineRun } from "./routines.ts";
/** A routine's own computer as it may hold one: only a bot routine that runs on this server keeps one. */
export function routineComputer(value: unknown, routine: { target?: Routine["target"] | string; runOn?: Routine["runOn"] | string }): RoutineComputer | undefined {
if (routine.target !== "bot" || routine.runOn === "cloud") return undefined;
return ROUTINE_COMPUTERS.find((computer) => computer === value);
}
/** A bot on its team's computer works there on every turn, so its routines may keep only the cloud computer. */
export const TEAM_COMPUTER_REFUSAL = "This bot works on its team's cloud computer, so its routines can't keep another computer. Clear the computer, or take the bot off the team computer first.";
/** Throws when a routine keeps a computer other than the cloud one for a bot on its team's computer. */
export function refuseKeptComputer(routine: { botId: string; computer?: RoutineComputer }, onTeamComputer?: (botId: string) => boolean): void {
if (routine.computer && routine.computer !== "cloud" && onTeamComputer?.(routine.botId)) throw new Error(TEAM_COMPUTER_REFUSAL);
}
/** The computer the person chose for a chat; a place a turn picked on its own (Auto) is not kept, nor any by a bot on its team's computer. */
export function chatComputer(task: { surface?: RoutineComputer; surfaceSource?: "user" | "auto" } | undefined, onTeamComputer = false): RoutineComputer | undefined {
return onTeamComputer ? undefined : chosenPin(task) ?? undefined;
}
/** The pin a run's own new conversation starts with, so its turns work where the routine's chat did. */
export function runComputerPin(run: { computer?: unknown; runOn?: RoutineRun["runOn"] | string; target: RoutineRun["target"] | string }): { surface: RoutineComputer; surfaceSource: "user" } | undefined {
const computer = routineComputer(run.computer, run);
return computer ? { surface: computer, surfaceSource: "user" } : undefined;
}
export interface RunCloudComputerDeps<B> {
bot(botId: string): B | null | undefined;
/** `pinned`: the kept computer of a run that starts a conversation; `threadId`: the conversation a run continues. */
provider(bot: B, runOn: string | undefined, pinned: RoutineComputer | undefined, threadId?: string): string | null;
owner(bot: B): string;
}
/** The owner of the cloud computer a run (or a routine's next run) needs, so its start check waits for it and it stays
* awake. The kept computer decides before the bot's Works on setting, as it does for the run's turns. A run that continues
* a conversation (`joined`) works where that conversation is pinned, as its turns do, so that pin decides instead. */
export function runCloudComputerOwner<B>(run: { botId: string; computer?: unknown; runOn?: RoutineRun["runOn"] | string; target?: RoutineRun["target"] | string }, deps: RunCloudComputerDeps<B>, joined?: string | null): string | null {
const bot = deps.bot(run.botId);
if (!bot) return null;
const provider = joined ? deps.provider(bot, run.runOn, undefined, joined) : deps.provider(bot, run.runOn, routineComputer(run.computer, run));
return provider === "box" ? deps.owner(bot) : null;
}