Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Add scripts/sync-phone-protocol.mjs: copy the phone protocol at one commit (#5779)
Milind Soniee37891a0424
diff --git a/scripts/sync-phone-protocol.mjs b/scripts/sync-phone-protocol.mjsnew file mode 100644index 0000000000..fcef38322a--- /dev/null+++ b/scripts/sync-phone-protocol.mjs@@ -0,0 +1,68 @@+#!/usr/bin/env node+// Copies the phone protocol into another checkout, as one commit of this repository holds it:+//+// node scripts/sync-phone-protocol.mjs --out <dir> [--ref <commit>]+//+// Files are read from git, never the working tree, so the copy is exactly what SOURCE names and the same commit always+// gives the same bytes: the phone repository's CI re-runs this at SOURCE and fails on any difference.+import { execFileSync } from "node:child_process";+import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";+import { dirname, join, resolve } from "node:path";+import { fileURLToPath } from "node:url";++const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");+const PACKAGE = "packages/phone-protocol";++/** Where each copied file comes from, by its path under the output directory. */+export function syncedFiles(ref, root = ROOT) {+ if (typeof ref !== "string" || !ref || ref.startsWith("-")) throw new Error(`not a commit: ${ref}`);+ const git = (...args) => execFileSync("git", args, { cwd: root, encoding: "utf8" });+ const commit = git("rev-parse", "--verify", `${ref}^{commit}`).trim();+ const source = git("ls-tree", "-r", "--name-only", "-z", commit, "--", `${PACKAGE}/src`).split("\0")+ .filter((path) => path.endsWith(".ts") && !path.endsWith(".test.ts"));+ const files = new Map([+ ["package.json", `${PACKAGE}/package.json`],+ ...source.map((path) => [path.slice(PACKAGE.length + 1), path]),+ ["vectors/v1.json", "companion/src/e2e/vectors/v1.json"],+ ["app-host.html", "shared/app-frame/app-host.html"],+ ]);+ return { commit, files };+}++/** The top-level names this script owns in the output directory: only these are replaced. */+const OWNED = ["SOURCE", "package.json", "src", "vectors", "app-host.html"];++export function syncPhoneProtocol(out, ref = "HEAD", root = ROOT) {+ const { commit, files } = syncedFiles(ref, root);+ const target = resolve(out);+ if (existsSync(target) && readdirSync(target).length > 0 && !existsSync(join(target, "SOURCE"))) {+ throw new Error(`${target} is not empty and has no SOURCE: refusing to write into a directory this script did not make`);+ }+ for (const name of OWNED) rmSync(join(target, name), { recursive: true, force: true });+ for (const [path, from] of files) {+ const bytes = execFileSync("git", ["cat-file", "blob", `${commit}:${from}`], { cwd: root, maxBuffer: 64 * 1024 * 1024 });+ mkdirSync(dirname(join(target, path)), { recursive: true });+ writeFileSync(join(target, path), bytes);+ }+ writeFileSync(join(target, "SOURCE"), `${commit}\n`);+ return { commit, count: files.size };+}++if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ const flag = (name) => {+ const at = process.argv.indexOf(name);+ return at === -1 ? undefined : process.argv[at + 1];+ };+ const out = flag("--out");+ if (!out) {+ console.error("Usage: node scripts/sync-phone-protocol.mjs --out <dir> [--ref <commit>]");+ process.exit(2);+ }+ try {+ const { commit, count } = syncPhoneProtocol(out, flag("--ref") ?? "HEAD");+ console.log(`copied ${count} files from ${commit} to ${resolve(out)}`);+ } catch (error) {+ console.error(error instanceof Error ? error.message : String(error));+ process.exit(1);+ }+}diff --git a/scripts/sync-phone-protocol.test.mjs b/scripts/sync-phone-protocol.test.mjsnew file mode 100644index 0000000000..1bb881d69a--- /dev/null+++ b/scripts/sync-phone-protocol.test.mjs@@ -0,0 +1,88 @@+import { execFileSync } from "node:child_process";+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join, relative } from "node:path";+import { fileURLToPath } from "node:url";+import { afterEach, describe, expect, it } from "vitest";++import { syncPhoneProtocol } from "./sync-phone-protocol.mjs";++const ROOT = fileURLToPath(new URL("..", import.meta.url));+const SCRIPT = fileURLToPath(new URL("./sync-phone-protocol.mjs", import.meta.url));+const git = (...args) => execFileSync("git", args, { cwd: ROOT, encoding: "utf8" }).trim();+const blob = (path) => execFileSync("git", ["cat-file", "blob", `HEAD:${path}`], { cwd: ROOT });++const made = [];+const scratch = () => {+ const dir = mkdtempSync(join(tmpdir(), "omb-phone-protocol-sync-"));+ made.push(dir);+ return dir;+};+afterEach(() => {+ for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });+});++function tree(dir) {+ const files = {};+ const walk = (at) => {+ for (const entry of readdirSync(at, { withFileTypes: true })) {+ const path = join(at, entry.name);+ if (entry.isDirectory()) walk(path);+ else files[relative(dir, path).split("\\").join("/")] = readFileSync(path).toString("base64");+ }+ };+ walk(dir);+ return files;+}++describe("sync-phone-protocol", () => {+ it("copies the package's sources, the wire vectors and the app host page from HEAD, and names the commit", () => {+ const out = join(scratch(), "protocol");+ execFileSync(process.execPath, [SCRIPT, "--out", out], { cwd: ROOT, stdio: "pipe" });++ const head = git("rev-parse", "HEAD");+ expect(readFileSync(join(out, "SOURCE"), "utf8")).toBe(`${head}\n`);+ const sources = git("ls-tree", "-r", "--name-only", "HEAD", "--", "packages/phone-protocol/src").split("\n")+ .filter((path) => !path.endsWith(".test.ts"));+ expect(sources).toContain("packages/phone-protocol/src/wire.ts");+ expect(Object.keys(tree(out)).sort()).toEqual([+ "SOURCE", "app-host.html", "package.json", "vectors/v1.json",+ ...sources.map((path) => path.slice("packages/phone-protocol/".length)),+ ].sort());+ for (const path of sources) expect(readFileSync(join(out, path.slice("packages/phone-protocol/".length)))).toEqual(blob(path));+ expect(readFileSync(join(out, "vectors/v1.json"))).toEqual(blob("companion/src/e2e/vectors/v1.json"));+ expect(readFileSync(join(out, "app-host.html"))).toEqual(blob("shared/app-frame/app-host.html"));+ expect(readFileSync(join(out, "package.json"))).toEqual(blob("packages/phone-protocol/package.json"));+ });++ it("gives the same bytes on every run, and replaces a stale or hand-edited copy", () => {+ const first = join(scratch(), "a");+ const second = join(scratch(), "b");+ syncPhoneProtocol(first);+ syncPhoneProtocol(second);+ const expected = tree(first);+ expect(tree(second)).toEqual(expected);++ writeFileSync(join(second, "src", "wire.ts"), "export type Edited = true;\n");+ writeFileSync(join(second, "src", "removed-upstream.ts"), "export {};\n");+ writeFileSync(join(second, "SOURCE"), "0000000000000000000000000000000000000000\n");+ syncPhoneProtocol(second);+ expect(tree(second)).toEqual(expected);+ });++ it("refuses a directory it did not make and leaves it alone", () => {+ const out = scratch();+ writeFileSync(join(out, "notes.txt"), "mine\n");+ mkdirSync(join(out, "src"));+ writeFileSync(join(out, "src", "keep.ts"), "export {};\n");+ expect(() => syncPhoneProtocol(out)).toThrow(/has no SOURCE/);+ expect(tree(out)).toEqual({ "notes.txt": Buffer.from("mine\n").toString("base64"), "src/keep.ts": Buffer.from("export {};\n").toString("base64") });+ });++ it("refuses a ref that is not a commit, or reads as an option", () => {+ const out = join(scratch(), "protocol");+ expect(() => syncPhoneProtocol(out, "--output=/tmp/x")).toThrow(/not a commit/);+ expect(() => syncPhoneProtocol(out, "no-such-ref-anywhere")).toThrow();+ expect(existsSync(out)).toBe(false);+ });+});