Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Own server: the connector checks the Docker API range it speaks at start
milind-sonica45aa9488a8
diff --git a/connector/src/docker.ts b/connector/src/docker.tsindex 407e1bd79b..d84db50fb3 100644--- a/connector/src/docker.ts+++ b/connector/src/docker.ts@@ -143,6 +143,26 @@ export function negotiateApi(version: Pick<DockerVersion, "ApiVersion" | "MinAPIreturn `${chosen[0]}.${chosen[1]}`;}+/**+ * Why the connector cannot talk to this Docker, with the fix, or null. Checked at start, so an engine that has dropped+ * the API versions the connector speaks gets one plain line instead of a connector that crashes and restarts forever.+ */+export async function dockerApiProblem(docker: Pick<Docker, "version">, socket: string): Promise<string | null> {+ let version: DockerVersion;+ try {+ version = await docker.version();+ } catch (error) {+ return `Docker does not answer on ${socket} (${error instanceof Error ? error.message : String(error)}). Start it with: sudo systemctl enable --now docker`;+ }+ if (negotiateApi(version)) return null;+ const speaks = `the connector speaks API ${MIN_API.join(".")} to ${MAX_API.join(".")}`;+ const engine = apiParts(version.ApiVersion);+ if (!engine || below(engine, MIN_API)) {+ return `Docker ${version.Version} is too old: it speaks API ${version.ApiVersion}, and ${speaks}. Update it with: curl -fsSL https://get.docker.com | sudo sh`;+ }+ return `Docker ${version.Version} no longer speaks an API this connector knows (it needs ${version.MinAPIVersion} or newer, and ${speaks}). Run the install command again to get the current connector.`;+}+/** Splits an image reference with a digest into what `POST /images/create` takes. */export function pullParams(image: string): { fromImage: string; tag: string } {const at = image.indexOf("@");diff --git a/connector/src/main.ts b/connector/src/main.tsindex e926e220ed..be0f88da1c 100644--- a/connector/src/main.ts+++ b/connector/src/main.ts@@ -3,7 +3,7 @@ import { readFileSync, statfsSync } from "node:fs";import { join } from "node:path";import { Agent } from "./agent.ts";-import { SocketDocker, type DockerInfo } from "./docker.ts";+import { SocketDocker, dockerApiProblem, type DockerInfo } from "./docker.ts";import { adminOrigin, enroll } from "./enroll.ts";import { readHealth } from "./home-health.ts";import { Home } from "./home.ts";@@ -95,6 +95,11 @@ async function run(): Promise<void> {log(`MausBot connector ${CONNECTOR_VERSION} starting`);// A connector removed with SIGKILL never said it went offline.state.saveStatus({ connected: false });+ for (let problem = await dockerApiProblem(docker, PATHS.docker); problem; problem = await dockerApiProblem(docker, PATHS.docker)) {+ log(problem);+ state.saveStatus({ lastError: problem });+ await sleep(10 * 60_000);+ }let record: AgentRecord | null = state.agent();if (!record) log("waiting for this server to be connected: run the install command with the code MausBot shows");while (!record) {@@ -206,6 +211,12 @@ function statusCommand(): void {*/async function probeCommand(): Promise<void> {const docker = new SocketDocker(PATHS.docker);+ const unusable = await dockerApiProblem(docker, PATHS.docker);+ if (unusable) {+ console.log(process.argv.includes("--json") ? JSON.stringify({ ok: false, checks: [{ id: "docker", ok: false, fix: unusable }] }) : `fail docker ${unusable}`);+ process.exitCode = 1;+ return;+ }const image = process.env.MAUSBOT_CONNECTOR_IMAGE;let box: BoxProbeResult | null = null;try { box = image ? await probeBox(docker, image, seccomp()) : null; } catch (error) { box = { ok: false, fix: errorText(error) }; }diff --git a/connector/test/host.test.ts b/connector/test/host.test.tsindex 0a0965e053..ab742d8274 100644--- a/connector/test/host.test.ts+++ b/connector/test/host.test.ts@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";-import { demuxLogs, negotiateApi, pullFailure, pullParams, type DockerInfo } from "../src/docker.ts";+import { demuxLogs, dockerApiProblem, negotiateApi, pullFailure, pullParams, type DockerInfo, type DockerVersion } from "../src/docker.ts";import { BOX_PROBE, boxProbeFix, hostChecks, hostFacts, hostHealth, probeBox, usernsAllowed, type HostReader } from "../src/host.ts";import { FakeDocker } from "./fake-docker.ts";@@ -87,6 +87,18 @@ describe("speaking to Docker", () => {expect(negotiateApi({ ApiVersion: "garbage" })).toBeNull();});+ it("says at start, in one line with its fix, when Docker cannot be spoken to, instead of crashing over and over", async () => {+ const engine = (version: Partial<DockerVersion> | Error) => ({ version: async () => { if (version instanceof Error) throw version; return { Version: "29.9.3", ApiVersion: "1.52", MinAPIVersion: "1.44", ...version }; } });+ expect(await dockerApiProblem(engine({}), "/var/run/docker.sock")).toBeNull();+ expect(await dockerApiProblem(engine({ Version: "20.10.24", ApiVersion: "1.41", MinAPIVersion: "1.12" }), "/var/run/docker.sock"))+ .toBe("Docker 20.10.24 is too old: it speaks API 1.41, and the connector speaks API 1.43 to 1.47. Update it with: curl -fsSL https://get.docker.com | sudo sh");+ // An engine that has raised its floor past what this connector knows: a newer connector is the fix.+ expect(await dockerApiProblem(engine({ Version: "31.0.0", ApiVersion: "1.60", MinAPIVersion: "1.50" }), "/var/run/docker.sock"))+ .toBe("Docker 31.0.0 no longer speaks an API this connector knows (it needs 1.50 or newer, and the connector speaks API 1.43 to 1.47). Run the install command again to get the current connector.");+ expect(await dockerApiProblem(engine(new Error("connect ENOENT /var/run/docker.sock")), "/var/run/docker.sock"))+ .toBe("Docker does not answer on /var/run/docker.sock (connect ENOENT /var/run/docker.sock). Start it with: sudo systemctl enable --now docker");+ });+it("pulls by digest, and reads a failure Docker reports inside a 200 stream", () => {const digest = `sha256:${"a".repeat(64)}`;expect(pullParams(`ghcr.io/milind-soni/openmausbot-cloud-home@${digest}`)).toEqual({ fromImage: "ghcr.io/milind-soni/openmausbot-cloud-home", tag: digest });