The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore(scripts): add web ui debug script
Steven Enamakel508b74323e0c
diff --git a/scripts/debug/web-ui.mjs b/scripts/debug/web-ui.mjsnew file mode 100644index 0000000000..ccb5acf4aa--- /dev/null+++ b/scripts/debug/web-ui.mjs@@ -0,0 +1,262 @@+#!/usr/bin/env node+// `pnpm debug web` — drive the web SPA in Playwright Chromium against a+// throwaway, fully local stack: the shared mock backend (in-process), a fresh+// `openhuman-core serve` on a scratch workspace, and Vite with no file watcher.+// See USAGE in ./web-ui-lib.mjs.+//+// Why each piece is set up the way it is:+// - Vite runs with OPENHUMAN_VITE_NO_WATCH=1. A busy box can have its inotify+// watch limit used up by other processes, and the watcher's ENOSPC is fatal+// to the dev server; a scripted browser needs no HMR anyway.+// - The SPA is pointed at the core through Vite's `/__dev-connect` page, which+// seeds the RPC URL and bearer into localStorage.+// - Sign-in goes through the real provider button: the mock answers+// `/auth/<provider>/login?redirectUri=<vite>/__dev-auth` like the backend,+// so the app's own OAuth callback path runs.+import { spawn, spawnSync } from "node:child_process";+import fs from "node:fs";+import { createRequire } from "node:module";+import path from "node:path";+import process from "node:process";+import { fileURLToPath, pathToFileURL } from "node:url";++import {+ devConnectUrl,+ freePort,+ parseArgs,+ runStamp,+ USAGE,+ waitFor,+} from "./web-ui-lib.mjs";++const repo = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");+const log = (...args) => console.log("[debug:web]", ...args);++let opts;+try {+ opts = parseArgs(process.argv.slice(2));+} catch (err) {+ console.error(`[debug:web] ${err.message}\n\n${USAGE}`);+ process.exit(2);+}+if (opts.help) {+ console.log(USAGE);+ process.exit(0);+}++const logDir = path.join(repo, "target/debug-logs", `web-${runStamp()}`);+fs.mkdirSync(logDir, { recursive: true });+const children = [];+let mockModule = null;+let browser = null;+let shuttingDown = false;++async function shutdown(code) {+ if (shuttingDown) return;+ shuttingDown = true;+ await browser?.close().catch(() => {});+ for (const child of children) {+ // Each child leads its own process group, so pnpm/node grandchildren go too.+ try {+ process.kill(-child.pid, "SIGTERM");+ } catch {+ child.kill("SIGTERM");+ }+ }+ await mockModule?.stopMockServer().catch(() => {});+ log(`artifacts: ${path.relative(repo, logDir)}`);+ process.exit(code);+}+process.on("SIGINT", () => void shutdown(130));+process.on("SIGTERM", () => void shutdown(143));++function startChild(name, command, args, env) {+ const out = fs.openSync(path.join(logDir, `${name}.log`), "a");+ const child = spawn(command, args, {+ cwd: repo,+ env: { ...process.env, ...env },+ stdio: ["ignore", out, out],+ detached: true,+ });+ child.on("exit", status => {+ if (!shuttingDown) {+ console.error(`[debug:web] ${name} exited (${status}); see ${name}.log`);+ void shutdown(1);+ }+ });+ children.push(child);+ return child;+}++function resolveCore() {+ const core = path.resolve(repo, opts.core ?? "target/debug/openhuman-core");+ if (fs.existsSync(core)) return core;+ if (opts.core || !opts.build) {+ throw new Error(`core binary not found: ${core}`);+ }+ log("building openhuman-core (target/debug/openhuman-core is missing)…");+ const build = spawnSync(+ "cargo",+ ["build", "--manifest-path", "Cargo.toml", "-p", "openhuman-cli", "--bin", "openhuman-core"],+ { cwd: repo, stdio: "inherit" },+ );+ if (build.status !== 0) throw new Error("cargo build failed");+ return core;+}++function loadChromium() {+ const appRequire = createRequire(path.join(repo, "app/package.json"));+ const { chromium } = appRequire("@playwright/test");+ if (opts.headed && !fs.existsSync(chromium.executablePath())) {+ throw new Error(+ `headed mode needs the full Chromium build at ${chromium.executablePath()}.\n` ++ "Install it with: pnpm --filter openhuman-app exec playwright install chromium",+ );+ }+ return chromium;+}++async function signIn(page, appOrigin) {+ await page.getByText("Welcome to OpenHuman").waitFor({ timeout: 120_000 });+ log("signing in through the GitHub provider button (mock backend)…");+ await page.getByRole("button", { name: "GitHub" }).click();+ await waitFor(+ async () => {+ const url = new URL(page.url());+ return url.origin === appOrigin && /^#\/(home|chat)/.test(url.hash);+ },+ { timeoutMs: 60_000, what: "the signed-in home page" },+ );+}++async function main() {+ const core = resolveCore();+ const chromium = loadChromium();+ const [mockPort, corePort, vitePort] = await Promise.all([+ freePort(),+ freePort(),+ freePort(),+ ]);+ const token = `debug-web-${Math.random().toString(36).slice(2)}${Date.now()}`;+ const mockUrl = `http://127.0.0.1:${mockPort}`;+ const rpcUrl = `http://127.0.0.1:${corePort}/rpc`;+ const appOrigin = `http://localhost:${vitePort}`;+ const workspace = path.join(logDir, "workspace");+ fs.mkdirSync(workspace, { recursive: true });++ mockModule = await import(pathToFileURL(path.join(repo, "scripts/mock-api-core.mjs")));+ await mockModule.startMockServer(mockPort);+ log(`mock backend ${mockUrl}`);++ startChild("core", core, ["serve", "--port", String(corePort)], {+ OPENHUMAN_WORKSPACE: workspace,+ OPENHUMAN_CORE_TOKEN: token,+ BACKEND_URL: mockUrl,+ VITE_BACKEND_URL: mockUrl,+ OPENHUMAN_COMPOSIO_MODE: "disabled",+ OPENHUMAN_SANDBOX: "off",+ RUST_LOG: process.env.RUST_LOG ?? "info",+ });+ await waitFor(async () => (await fetch(`http://127.0.0.1:${corePort}/health`)).ok, {+ timeoutMs: 120_000,+ what: "the core's /health",+ });+ log(`core ${rpcUrl} (workspace ${path.relative(repo, workspace)})`);++ startChild(+ "vite",+ path.join(repo, "app/node_modules/.bin/vite"),+ ["--port", String(vitePort), "--strictPort"],+ {+ OPENHUMAN_VITE_NO_WATCH: "1",+ OPENHUMAN_DEV_PORT: String(vitePort),+ OPENHUMAN_CORE_TOKEN: token,+ VITE_OPENHUMAN_CORE_RPC_URL: rpcUrl,+ VITE_BACKEND_URL: mockUrl,+ VITE_DEV_SKIP_ONBOARDING: "true",+ },+ ).spawnargs; // cwd for vite must be app/+ await waitFor(async () => (await fetch(appOrigin)).ok, {+ timeoutMs: 120_000,+ what: "Vite",+ });+ log(`app ${appOrigin}`);++ browser = await chromium.launch({+ headless: !opts.headed,+ args: ["--disable-dev-shm-usage"],+ });+ const context = await browser.newContext({ viewport: { width: 1300, height: 900 } });+ const page = await context.newPage();+ const browserLog = fs.createWriteStream(path.join(logDir, "browser.log"));+ page.on("console", msg => browserLog.write(`[${msg.type()}] ${msg.text()}\n`));+ page.on("pageerror", err => browserLog.write(`[pageerror] ${err.stack ?? err.message}\n`));+ page.on("requestfailed", req =>+ browserLog.write(`[requestfailed] ${req.method()} ${req.url()} ${req.failure()?.errorText}\n`),+ );+ page.on("crash", () => browserLog.write("[crash] renderer crashed\n"));++ const screenshot = async name => {+ const file = path.join(logDir, `${name}.png`);+ await page.screenshot({ path: file, fullPage: true });+ return file;+ };++ // The first load compiles the app's modules on demand; give it room.+ page.setDefaultNavigationTimeout(120_000);+ await page.goto(devConnectUrl(appOrigin, rpcUrl, token));+ if (opts.signIn) {+ await signIn(page, appOrigin);+ log(`signed in ${page.url()}`);+ }+ await screenshot("ready");++ const rpc = async (method, params = {}) => {+ const res = await fetch(rpcUrl, {+ method: "POST",+ headers: { "content-type": "application/json", authorization: `Bearer ${token}` },+ body: JSON.stringify({ jsonrpc: "2.0", id: Date.now(), method, params }),+ });+ const body = await res.json();+ if (body.error) throw new Error(`${method}: ${body.error.message}`);+ return body.result;+ };+ const mock = {+ url: mockUrl,+ set: (key, value) =>+ mockModule.setMockBehavior(key, typeof value === "string" ? value : JSON.stringify(value)),+ reset: () => mockModule.resetMockBehavior(),+ requests: () => mockModule.getRequestLog(),+ };+ const urls = { app: appOrigin, rpc: rpcUrl, mock: mockUrl, token };++ if (!opts.script) {+ log("ready — the stack stays up until Ctrl-C");+ log(`open ${devConnectUrl(appOrigin, rpcUrl, token)} in any local browser to share this core`);+ await new Promise(() => {});+ }++ const scriptPath = path.resolve(process.cwd(), opts.script);+ log(`running ${path.relative(repo, scriptPath)}`);+ const { default: run } = await import(pathToFileURL(scriptPath));+ if (typeof run !== "function") throw new Error("--script must default-export a function");+ try {+ await run({ page, context, browser, mock, rpc, urls, logDir, screenshot, log });+ } catch (err) {+ await screenshot("failure").catch(() => {});+ throw err;+ }+ log("script finished");+ if (opts.keep) {+ log("--keep: the stack stays up until Ctrl-C");+ await new Promise(() => {});+ }+}++main().then(+ () => shutdown(0),+ async err => {+ console.error(`[debug:web] ${err.stack ?? err.message}`);+ await shutdown(1);+ },+);