Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Cloud's page asks: a session older than 8 s is read again first, so every 10 s ask is fresh
milind-soni77bf29ba4027
diff --git a/electron/cloud-operation.d.mts b/electron/cloud-operation.d.mtsindex 31ecdfd649..3b45df21c0 100644--- a/electron/cloud-operation.d.mts+++ b/electron/cloud-operation.d.mts@@ -5,6 +5,7 @@export interface CloudOperation { kind: string; since: number; until: number; expectedAt?: number; backup?: true; bigger?: true }export declare const CLOUD_OPERATION_KINDS: readonly string[];export declare const OPERATION_REFRESH_MS: number;+export declare const OPERATION_FRESH_MS: number;export declare function parseCloudOperation(input: unknown): CloudOperation | null;export declare function cloudOperationAt(state: unknown, origin: string | null | undefined): CloudOperation | null;export declare function anyOperation(machine: { operation?: CloudOperation } | null | undefined, clouds: unknown): boolean;diff --git a/electron/cloud-operation.mjs b/electron/cloud-operation.mjsindex be8d0a9954..ada7193063 100644--- a/electron/cloud-operation.mjs+++ b/electron/cloud-operation.mjs@@ -6,6 +6,9 @@export const CLOUD_OPERATION_KINDS = Object.freeze(["update", "resize", "move", "disk", "restore", "start"]);/** While the Admin says an operation has a Cloud down, its session is read this often, so the window follows it closely. */export const OPERATION_REFRESH_MS = 15_000;+/** The Cloud's own page asks every 10 s while its live stream is down (src/lib/cloud-work.ts): a session older than this is read+ * again first, so each ask gets a fresh word. */+export const OPERATION_FRESH_MS = 8_000;const KIND = /^[a-z][a-z-]{0,23}$/;const record = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);diff --git a/electron/cloud-operation.node-test.mjs b/electron/cloud-operation.node-test.mjsindex a5ea7ef8c7..ca77745ef5 100644--- a/electron/cloud-operation.node-test.mjs+++ b/electron/cloud-operation.node-test.mjs@@ -79,10 +79,13 @@ test("the Cloud's own page asks what has it down: the Admin's word for the placeassert.deepEqual(await w.home.operation(), UPDATE);w.state = { ...connected(), verifiedAt: 1_000_000 };assert.equal(await w.home.operation(), null);- // An old read is read again first when the account can (this fixture's can't): it still answers what it has, never throws.- const stale = world({ state: { ...connected({ operation: UPDATE }), verifiedAt: 1 } });+ assert.equal(w.refreshes, 0, "a fresh read is not read again");+ // An old read is read again first, so the page gets the Admin's word as of now: here the update has just begun.+ const stale = world({ state: { ...connected(), verifiedAt: 1 } });await stale.home.refresh();+ stale.refreshed = { ...connected({ operation: UPDATE }), verifiedAt: stale.clock };assert.deepEqual(await stale.home.operation(), UPDATE);+ assert.equal(stale.refreshes, 1);const signedOut = world({ state: { status: "signed-out" } });assert.equal(await signedOut.home.operation(), null);});diff --git a/electron/fixtures/home-window-world.mjs b/electron/fixtures/home-window-world.mjsindex 5cb6d40129..73f855ab18 100644--- a/electron/fixtures/home-window-world.mjs+++ b/electron/fixtures/home-window-world.mjs@@ -26,7 +26,7 @@ export function world({ clean = null, journal = done(), marker = null, state = cteam: { down: false, signedIn: true, version: "0.1.130", health: 200, gone: false, ...team },/** Another Cloud of the person's own, answering at CLIENT; `gone`: the Admin's pairing says it is no longer theirs. */client: { down: false, signedIn: true, version: "0.1.130", health: 200, gone: false, ...client },- went: [], calls: [], fetched: [], pairs: 0, teamPairs: [], ownPairs: [], wakes: 0, waking: [], splash: [], published: [], local: [], reloads: 0, signIns: [],+ went: [], calls: [], fetched: [], pairs: 0, refreshes: 0, teamPairs: [], ownPairs: [], wakes: 0, waking: [], splash: [], published: [], local: [], reloads: 0, signIns: [],/** The servers this computer's bots moved to, asked for again (`reopen`), and what main answers. `reopenGate`: a* recovery whose navigation is still loading. */reopened: [], reopenAnswer: true, reopenGate: null,@@ -47,6 +47,8 @@ export function world({ clean = null, journal = done(), marker = null, state = cw.window = { isDestroyed: () => false, webContents: contents };const account = {state: () => w.state,+ /** The session read again (cloud-account.mjs `refresh`): `w.refreshed`, when set, is what the Admin says then. */+ refresh: async () => { w.refreshes++; if (w.refreshed) w.state = w.refreshed; return w.state; },power: () => w.power,powerOf: (origin) => (origin === CLOUD ? w.power : (w.state?.clouds ?? []).find((cloud) => cloud.origin === origin)?.power ?? null),async wakeHome() { w.wakes++; await w.cloud.wakeGate; if (w.cloud.wakes) w.power = { ...w.power, state: "awake" }; return "awake"; },diff --git a/electron/home-window.mjs b/electron/home-window.mjsindex f078ce5e61..467087a5d5 100644--- a/electron/home-window.mjs+++ b/electron/home-window.mjs@@ -10,7 +10,7 @@// Admin's team pairing (its claims are the Admin's), never an address a page supplied.import { cloudHomeConnectUrl, cloudSetupOf, cloudUnreachableCause, cloudUnreachableText, machineSetup, readyCloudOrigin } from "./cloud-home.mjs";import { cloudMachine, cloudState, myCloudOf, otherOwnClouds, ownCloud } from "./cloud-list.mjs";-import { cloudOperationAt, OPERATION_REFRESH_MS } from "./cloud-operation.mjs";+import { cloudOperationAt, OPERATION_FRESH_MS, OPERATION_REFRESH_MS } from "./cloud-operation.mjs";import { asleepAnswer, BACKGROUND_HEADERS, openSleepingCloud, SIGNED_IN_MS } from "./cloud-power.mjs";import nativeText from "./native-text.cjs";import { createPairWait, nameNotFound } from "./home-pairing.mjs";@@ -852,11 +852,11 @@ export function createHomeWindow(deps) {homePage,/** The operation the Admin says has the place on screen down now (cloud-operation.mjs), else null: its own page asks while* its live stream is down, so it can say "Updating My Cloud" instead of "unable to connect". The session is read again- * first when the last read is OPERATION_REFRESH_MS old or more (the Admin is the one that knows; the place doesn't answer). */+ * first when the last read is OPERATION_FRESH_MS old or more (the Admin is the one that knows; the place doesn't answer). */async operation() {const origin = placeOrigin(), current = state();if (!origin || current?.status !== "connected") return null;- if (!(now() - (current.verifiedAt ?? 0) < OPERATION_REFRESH_MS)) await Promise.resolve(account()?.refresh?.()).catch(() => {});+ if (!(now() - (current.verifiedAt ?? 0) < OPERATION_FRESH_MS)) await Promise.resolve(account()?.refresh?.()).catch(() => {});return cloudOperationAt(state(), origin);},updating(event) {