Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Routine kept computer: keep-awake check uses the kept computer too
AdityaUmalea74d3921b20b
diff --git a/server/host/cloud-computer-power.ts b/server/host/cloud-computer-power.tsindex e4ca62e5d7..6fa76169ce 100644--- a/server/host/cloud-computer-power.ts+++ b/server/host/cloud-computer-power.ts@@ -31,7 +31,7 @@ export const CLOUD_COMPUTER_NOT_ANSWERING_YET = "the cloud computer is awake butexport const CLOUD_COMPUTER_DISK_NOT_BACK_YET = "the cloud computer is awake but still restoring its disk";const REMOVAL_POLL_MS = 3_000;-type ScheduledRoutine = Pick<Routine, "botId" | "runOn" | "enabled" | "nextRunAt">;+type ScheduledRoutine = Pick<Routine, "botId" | "runOn" | "enabled" | "nextRunAt"> & Partial<Pick<Routine, "target" | "computer">>;export interface CloudComputerPowerDeps {cfg: AppConfig;diff --git a/server/index.ts b/server/index.tsindex 36a924c081..6a1d811e3e 100644--- a/server/index.ts+++ b/server/index.ts@@ -504,7 +504,7 @@ import { CONTROL_ACT_WITHIN_MS, CONTROL_HANDED_BACK, CONTROL_NOT_READY, CONTROL_import { computerFreeAfterText, computerParkedRefusal, computerParkedText, computerPersonAwayText, computerStoppedWaitingText, computerWaitingText, type ComputerHolder } from "./computer-wait.ts"; import { createComputerGateAnswers } from "./host/computer-gate-answers.ts";import { modelContextWindow } from "./model-context-window.ts";import { browserIsThePlace, chosenPin, cloudPlaceRefusal, computerSelectionPrompt, computerSkippedNote, computerToolsRefusal, handoffUsesOwnCloud, isHandedWork, failedOnHostPath, openBrowserBeside, ownCloudStateWithin, parseSurface, pinSource, PlaceUnavailableError, placeUnavailable, resolveSurface, surfaceLabel, surfaceOfComputerKind, surfacePrompt, thisComputerSwitch, whereWorksControl, type ComputerWant, type PlaceSource, type Surface } from "./surface.ts";-import { chatComputer, runCloudComputerOwner, runComputerPin } from "./routine-computer.ts"; import type { RoutineComputer } from "../shared/routines.ts";+import { chatComputer, runCloudComputerOwner, runComputerPin, type RunCloudComputerDeps } from "./routine-computer.ts"; import type { RoutineComputer } from "../shared/routines.ts";import { cloudRefusal, type PlaceRow } from "../shared/place-view.ts";import { cloudFailureOf, cloudLimitFields, cloudLimitText } from "./boat-limit.ts";import {@@ -5599,12 +5599,12 @@ const localVmIdleMs = () => localVmIdleTimeoutMinutes(cfg) * 60_000;const localVmIdles = new Map<string, ComputerIdleTimer>();const { activeVpsThreads, vpsThreadStarted, vpsThreadEnded, vpsIdles, vpsIdleMs, cancelVpsIdle, touchVpsIdle, vpsServerSwitched, recover: recoverVpsIdles, cancelAll: cancelAllVpsIdles } = createVpsIdleHost({ cfg, store, botHasActiveTurn, hasActiveRoutine: (id) => Boolean(routines?.activeRunForBot(id)), isSharedVmHeld: (id) => sharedVm.snapshot(id).held, hasComputerPreview: (id) => computerPreviews.has(id), isVpsConfigTransitioning: () => computerProviderConfigTransitions.has("vps"), isLifecycleBusy: (id) => boatLifecycleBusyBots.has(id), claimBotComputerLifecycle, isShuttingDown: () => companyShutdown });-const meetingsHost = createMeetingsHost({ cfg, dataDir: DATA_DIR, store, startTurn: (botId, text, opts) => startTurn(botId, text, opts), threadBusy, power: () => cloudComputerPower, computerOwner: (bot) => botComputerControlKey(bot), routines: () => routines, log: (line) => console.warn(line) }, () => ({ personName: personNameIn, sendsFreely: (botId, threadId) => roomTurnLevels.forRequest(threadId, botId, () => sourceApprovalMode(botId, threadId)) === "full", outbound: () => outboundHold, markOutside: (threadId) => outsideLineage.mark(threadId), folder: (botId, threadId) => messageFileRootsForThread(botId, threadId).filter((root) => root !== ATTACHMENTS_DIR)[0] ?? null })); const cloudComputerPower = createCloudComputerPower({+const runCloudDeps: RunCloudComputerDeps<BotRecord> = { bot: (botId) => store.bot(botId), provider: (bot, runOn, pinned) => turnProvider(bot, runOn as RoutineRunOn | undefined, undefined, pinned), owner: botComputerControlKey }; const meetingsHost = createMeetingsHost({ cfg, dataDir: DATA_DIR, store, startTurn: (botId, text, opts) => startTurn(botId, text, opts), threadBusy, power: () => cloudComputerPower, computerOwner: (bot) => botComputerControlKey(bot), routines: () => routines, log: (line) => console.warn(line) }, () => ({ personName: personNameIn, sendsFreely: (botId, threadId) => roomTurnLevels.forRequest(threadId, botId, () => sourceApprovalMode(botId, threadId)) === "full", outbound: () => outboundHold, markOutside: (threadId) => outsideLineage.mark(threadId), folder: (botId, threadId) => messageFileRootsForThread(botId, threadId).filter((root) => root !== ATTACHMENTS_DIR)[0] ?? null })); const cloudComputerPower = createCloudComputerPower({cfg, owners: managedBoatOwners, broadcast, log: line => console.warn(line), routines: () => routines?.listRoutines() ?? [], wakeTimedOut: (owner, boxId, ctx) => boatRecovery.wakeTimedOut(owner, boxId, ctx),- routineComputer: routine => { const bot = store.bot(routine.botId); return bot && turnProvider(bot, routine.runOn) === "box" ? botComputerControlKey(bot) : null; },+ routineComputer: routine => runCloudComputerOwner(routine, runCloudDeps),busy: owner => computerProviderConfigTransitions.has("box") || boatLifecycleBusyBots.has(owner) || cloudBrowserBusy(owner) || managedBoatOwners().some(entry => entry.botId === owner && entry.inUse) || meetingsHost.keepsAwake(owner),claim: owner => { const team = teamComputers.list().find(entry => teamComputerOwner(entry.id) === owner); return team ? claimTeamComputerLifecycle(team) : claimBotComputerLifecycle(owner); },-}); const boatRecovery = createBoatRecovery({ cfg: () => cfg, log: line => console.warn(line), eligible: owner => { const bot = store.bot(owner); return Boolean(bot) && botComputerControlKey(bot!) === owner; }, personActive: owner => computerControl.snapshot(owner).held || desktopViewer.viewing(owner) > 0, usedByOther: (owner, boxId, asking) => usedByAnotherBot(owner, boxId, { holder: resource => turnResources.holder(resource), botOfThread: threadId => store.botByThread(threadId)?.id ?? null }, asking), created: owner => cloudComputerPower.used(owner), turnWaiting: boxId => cloudComputerPower.waiting(boxId), tell: (owner, key, vars) => { const team = teamComputers.list().find(entry => teamComputerOwner(entry.id) === owner); if (team) { teamComputers.setProblem(team.id, serverLine(appLanguage(), CLOUD_COMPUTER_PERSON_LINES.teamNotResponding, { name: team.name })); return; } const bot = store.bot(owner); if (!bot) return; const text = serverLine(conversationLanguage(bot.threadId), CLOUD_COMPUTER_PERSON_LINES[key], { ...vars, name: bot.name }); store.appendMessage(bot.threadId, { role: "bot", kind: "activity", tool: { name: `recovery: ${text}`, ok: true } }); notify(buildNoticeNotification(bot, bot.threadId, text)); } }); const runComputers = createRunComputerGate({ computerOf: (run) => runCloudComputerOwner(run, { bot: (botId) => store.bot(botId), provider: (bot, runOn, pinned) => turnProvider(bot, runOn as RoutineRunOn | undefined, undefined, pinned), owner: botComputerControlKey }), find: (owner) => boat.findBoat(cfg, owner), wake: (owner, budgetMs) => boat.readyBoat(cfg, owner, budgetMs), awaitAnswer: (boxId, opts) => boatReadiness.awaitAnswer(cfg, boxId, opts), verdict: (owner, since) => boatRecovery.verdict(owner, since), recoveryState: (owner) => boatRecovery.status(owner)?.state ?? null, used: (owner) => cloudComputerPower.used(owner), disk: (boxId, signal) => cloudComputerDisk.beforeRun(cfg, boxId, signal), remember: runComputerMemory(join(DATA_DIR, "routine-computers.json")), routineOf: (botId, threadId) => { const run = routines?.activeRunForBot(botId); return run?.threadId === threadId ? run.routineId : null; }, log: (line) => console.warn(line) });+}); const boatRecovery = createBoatRecovery({ cfg: () => cfg, log: line => console.warn(line), eligible: owner => { const bot = store.bot(owner); return Boolean(bot) && botComputerControlKey(bot!) === owner; }, personActive: owner => computerControl.snapshot(owner).held || desktopViewer.viewing(owner) > 0, usedByOther: (owner, boxId, asking) => usedByAnotherBot(owner, boxId, { holder: resource => turnResources.holder(resource), botOfThread: threadId => store.botByThread(threadId)?.id ?? null }, asking), created: owner => cloudComputerPower.used(owner), turnWaiting: boxId => cloudComputerPower.waiting(boxId), tell: (owner, key, vars) => { const team = teamComputers.list().find(entry => teamComputerOwner(entry.id) === owner); if (team) { teamComputers.setProblem(team.id, serverLine(appLanguage(), CLOUD_COMPUTER_PERSON_LINES.teamNotResponding, { name: team.name })); return; } const bot = store.bot(owner); if (!bot) return; const text = serverLine(conversationLanguage(bot.threadId), CLOUD_COMPUTER_PERSON_LINES[key], { ...vars, name: bot.name }); store.appendMessage(bot.threadId, { role: "bot", kind: "activity", tool: { name: `recovery: ${text}`, ok: true } }); notify(buildNoticeNotification(bot, bot.threadId, text)); } }); const runComputers = createRunComputerGate({ computerOf: (run) => runCloudComputerOwner(run, runCloudDeps), find: (owner) => boat.findBoat(cfg, owner), wake: (owner, budgetMs) => boat.readyBoat(cfg, owner, budgetMs), awaitAnswer: (boxId, opts) => boatReadiness.awaitAnswer(cfg, boxId, opts), verdict: (owner, since) => boatRecovery.verdict(owner, since), recoveryState: (owner) => boatRecovery.status(owner)?.state ?? null, used: (owner) => cloudComputerPower.used(owner), disk: (boxId, signal) => cloudComputerDisk.beforeRun(cfg, boxId, signal), remember: runComputerMemory(join(DATA_DIR, "routine-computers.json")), routineOf: (botId, threadId) => { const run = routines?.activeRunForBot(botId); return run?.threadId === threadId ? run.routineId : null; }, log: (line) => console.warn(line) });function inheritedTeamComputer(bot: Pick<BotRecord, "section" | "computer" | "cloudBackend">): TeamComputerRecord | undefined {return teamComputers.forBot(bot);diff --git a/server/routine-computer.ts b/server/routine-computer.tsindex 9843bd4a84..f3bce1fadc 100644--- a/server/routine-computer.ts+++ b/server/routine-computer.ts@@ -4,7 +4,7 @@ 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 {+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);}@@ -20,13 +20,15 @@ export function runComputerPin(run: { computer?: unknown; runOn?: RoutineRun["rureturn computer ? { surface: computer, surfaceSource: "user" } : undefined;}-/** The owner of the cloud computer a run's start check waits for. The run's kept computer decides before the bot's Works on- * setting, as it does for the run's turns, so a run kept on another computer never waits for the bot's cloud computer. */-export function runCloudComputerOwner<B>(run: { botId: string; computer?: unknown; runOn?: RoutineRun["runOn"] | string; target: RoutineRun["target"] | string }, deps: {+export interface RunCloudComputerDeps<B> {bot(botId: string): B | null | undefined;provider(bot: B, runOn: string | undefined, pinned: RoutineComputer | undefined): string | null;owner(bot: B): string;-}): string | null {+}++/** 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. */+export function runCloudComputerOwner<B>(run: { botId: string; computer?: unknown; runOn?: RoutineRun["runOn"] | string; target?: RoutineRun["target"] | string }, deps: RunCloudComputerDeps<B>): string | null {const bot = deps.bot(run.botId);return bot && deps.provider(bot, run.runOn, routineComputer(run.computer, run)) === "box" ? deps.owner(bot) : null;}