Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat: add automation backend integration with standalone ingress proxy (#127)

Rohit Malhotra988cfed5c60b
diff --git a/.gitignore b/.gitignore
index 9df99a5cfb..fed15ba17b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,8 @@ src/i18n/declaration.ts
.env
node_modules/
build/
+
+
/test-results/
/playwright-report/
/blob-report/
diff --git a/AGENTS.md b/AGENTS.md
index 8f4a1aa733..9c2a034a9f 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -88,6 +88,15 @@
- `OH_SECRET_KEY` β€” secret key for settings encryption; uses a default value for local dev, override for production
- Default: latest released version from PyPI
- `scripts/dev-safe.mjs` should fail fast if `uvx` cannot be spawned (for example missing PATH entries).
+- `npm run dev` now runs the full stack with automation by default (via `dev:automation`). Use `npm run dev:minimal` for agent-server + Vite only.
+- `scripts/dev-with-automation.mjs` runs the full stack: agent-server, automation backend (both via uvx), Vite dev server, and ingress proxy. Uses a standalone ingress proxy (`scripts/ingress.mjs`) to route traffic:
+ - `/api/automation/*` β†’ automation backend (:18001)
+ - `/api/*`, `/sockets`, etc. β†’ agent server (:18000)
+ - `/*` (default) β†’ Vite dev server (:3001)
+ - Environment variables: `PORT` (ingress port, default: 8000), `OH_AUTOMATION_GIT_REF` (default: `main`)
+ - Access points: `http://localhost:8000/` (main UI), `http://localhost:8000/api/automation/docs` (API docs)
+- `scripts/ingress.mjs` is a standalone HTTP reverse proxy that can be used independently to route traffic to multiple backends based on URL path prefix.
+- `scripts/dev-safe.mjs` (now `npm run dev:minimal`) runs just agent-server + Vite without automation.
- Vite dev mode can black-screen on first load with `504 Outdated Optimize Dep` if core client-entry deps are not prebundled; keep `react`, `react/jsx-runtime`, `react-dom/client`, and `react-router/dom` in `optimizeDeps.include`.
- Vercel deployment note: React Router builds for this repo must keep `build/client` intact on actual Vercel builds and include `presets: [vercelPreset()]` from `@vercel/react-router/vite`; flattening `build/client` during a Vercel build produces deployments with empty outputs (`routes: null`, no static files) and a production 404.
diff --git a/README.md b/README.md
index 0cff703e25..6c2fdedcce 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,21 @@ Then edit the values you need.
npm run dev
```
-This starts an isolated local `agent-server` for this checkout and the frontend on [http://localhost:3001](http://localhost:3001).
+This starts the full stack:
+- Agent server (via uvx)
+- Automation backend (via uvx)
+- Vite dev server
+- Ingress proxy (routes traffic to all backends)
+
+Access the UI at [http://localhost:8000](http://localhost:8000)
+
+#### Environment Variables
+
+| Variable | Description | Default |
+|----------|-------------|---------|
+| `PORT` | Ingress port | `8000` |
+| `OH_AUTOMATION_GIT_REF` | Git ref for automation backend | `main` |
+| `OH_AGENT_SERVER_GIT_REF` | Git ref for agent-server | `main` |
### 5. First-run sanity check
@@ -66,6 +80,18 @@ After the page opens:
- `/settings` should load
- configure a working LLM model + API key under `Settings > LLM` before running the first live task
- you should be able to open or create a conversation
+- `/api/automation/docs` should show the automation API docs
+
+### Alternative: Minimal Mode (without Automation)
+
+To run without the automation service:
+
+```sh
+npm run dev:minimal
+```
+
+This runs only agent-server + Vite (no automation backend or ingress).
+Access at `http://localhost:3001/`
## More documentation
diff --git a/__tests__/scripts/dev-with-automation.test.ts b/__tests__/scripts/dev-with-automation.test.ts
new file mode 100644
index 0000000000..8398103786
--- /dev/null
+++ b/__tests__/scripts/dev-with-automation.test.ts
@@ -0,0 +1,228 @@
+import { spawn } from "node:child_process";
+import { once } from "node:events";
+import { homedir } from "node:os";
+import path from "node:path";
+import { setTimeout as delay } from "node:timers/promises";
+import { fileURLToPath } from "node:url";
+import { describe, expect, it } from "vitest";
+import {
+ buildAutomationCommand,
+ buildConfig,
+ DEFAULT_AUTOMATION_REPO,
+ DEFAULT_AUTOMATION_GIT_REF,
+ DEFAULT_BACKEND_PORT,
+ DEFAULT_AUTOMATION_PORT,
+} from "../../scripts/dev-with-automation.mjs";
+
+const repoRoot = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ "../..",
+);
+
+describe("buildAutomationCommand", () => {
+ it("uses main branch by default", () => {
+ const cmd = buildAutomationCommand({});
+
+ expect(cmd.command).toBe("uvx");
+ expect(cmd.args).toContain("--from");
+ expect(cmd.args).toContain(
+ `git+${DEFAULT_AUTOMATION_REPO}@${DEFAULT_AUTOMATION_GIT_REF}`,
+ );
+ expect(cmd.args).toContain("uvicorn");
+ expect(cmd.args).toContain("automation.app:app");
+ expect(cmd.source).toBe(`git (${DEFAULT_AUTOMATION_GIT_REF})`);
+ });
+
+ it("uses custom git ref from OH_AUTOMATION_GIT_REF", () => {
+ const cmd = buildAutomationCommand({
+ OH_AUTOMATION_GIT_REF: "feat/my-feature",
+ });
+
+ expect(cmd.command).toBe("uvx");
+ expect(cmd.args).toContain("--from");
+ expect(cmd.args).toContain(
+ `git+${DEFAULT_AUTOMATION_REPO}@feat/my-feature`,
+ );
+ expect(cmd.source).toBe("git (feat/my-feature)");
+ });
+
+ it("uses custom repo from OH_AUTOMATION_REPO", () => {
+ const cmd = buildAutomationCommand({
+ OH_AUTOMATION_REPO: "https://github.com/MyOrg/my-automation",
+ });
+
+ expect(cmd.command).toBe("uvx");
+ expect(cmd.args).toContain(
+ `git+https://github.com/MyOrg/my-automation@${DEFAULT_AUTOMATION_GIT_REF}`,
+ );
+ });
+
+ it("uses both custom repo and ref together", () => {
+ const cmd = buildAutomationCommand({
+ OH_AUTOMATION_REPO: "https://github.com/MyOrg/my-automation",
+ OH_AUTOMATION_GIT_REF: "v1.0.0",
+ });
+
+ expect(cmd.command).toBe("uvx");
+ expect(cmd.args).toContain(
+ "git+https://github.com/MyOrg/[email protected]",
+ );
+ expect(cmd.source).toBe("git (v1.0.0)");
+ });
+
+ it("supports commit SHA as git ref", () => {
+ const cmd = buildAutomationCommand({
+ OH_AUTOMATION_GIT_REF: "abc123def456",
+ });
+
+ expect(cmd.command).toBe("uvx");
+ expect(cmd.args).toContain(
+ `git+${DEFAULT_AUTOMATION_REPO}@abc123def456`,
+ );
+ expect(cmd.source).toBe("git (abc123def456)");
+ });
+});
+
+describe("buildConfig", () => {
+ it("builds default config with correct ports", () => {
+ const config = buildConfig({}, {});
+
+ expect(config.ingressPort).toBe(8000);
+ expect(config.agentServerPort).toBe(DEFAULT_BACKEND_PORT);
+ expect(config.autoBackendPort).toBe(DEFAULT_AUTOMATION_PORT);
+ expect(config.vitePort).toBe(3001);
+ expect(config.vscodePort).toBe(DEFAULT_BACKEND_PORT + 1000);
+ });
+
+ it("respects port from args", () => {
+ const config = buildConfig({ port: 9000 }, {});
+
+ expect(config.ingressPort).toBe(9000);
+ });
+
+ it("respects PORT from env", () => {
+ const config = buildConfig({}, { PORT: "9001" });
+
+ expect(config.ingressPort).toBe(9001);
+ });
+
+ it("args.port takes precedence over env.PORT", () => {
+ const config = buildConfig({ port: 9002 }, { PORT: "9999" });
+
+ expect(config.ingressPort).toBe(9002);
+ });
+
+ it("applies automationGitRef from args to env", () => {
+ const env: Record<string, string> = {};
+ buildConfig({ automationGitRef: "my-branch" }, env);
+
+ expect(env.OH_AUTOMATION_GIT_REF).toBe("my-branch");
+ });
+
+ it("applies automationRepo from args to env", () => {
+ const env: Record<string, string> = {};
+ buildConfig({ automationRepo: "https://example.com/repo" }, env);
+
+ expect(env.OH_AUTOMATION_REPO).toBe("https://example.com/repo");
+ });
+
+ it("uses correct state directory path", () => {
+ const config = buildConfig({}, {});
+
+ expect(config.stateDir).toBe(
+ path.join(homedir(), ".openhands", "agent-canvas"),
+ );
+ });
+
+ it("passes verbose flag through", () => {
+ const config = buildConfig({ verbose: true }, {});
+
+ expect(config.verbose).toBe(true);
+ });
+});
+
+describe("default constants", () => {
+ it("has expected default automation repo", () => {
+ expect(DEFAULT_AUTOMATION_REPO).toBe(
+ "https://github.com/OpenHands/automation",
+ );
+ });
+
+ it("has expected default automation git ref", () => {
+ expect(DEFAULT_AUTOMATION_GIT_REF).toBe("main");
+ });
+
+ it("has expected default backend port", () => {
+ expect(DEFAULT_BACKEND_PORT).toBe(18000);
+ });
+
+ it("has expected default automation port", () => {
+ expect(DEFAULT_AUTOMATION_PORT).toBe(18001);
+ });
+});
+
+describe("dev-with-automation CLI", () => {
+ it("shows help with --help flag", async () => {
+ const child = spawn(
+ process.execPath,
+ ["scripts/dev-with-automation.mjs", "--help"],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ let output = "";
+ child.stdout.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+
+ const [code] = await once(child, "exit");
+
+ expect(code).toBe(0);
+ expect(output).toContain("Agent Canvas + Automation Development Stack");
+ expect(output).toContain("--port");
+ expect(output).toContain("--automation-ref");
+ expect(output).toContain("--automation-repo");
+ expect(output).toContain("OH_AUTOMATION_GIT_REF");
+ });
+
+ it("exits promptly when uvx is missing", async () => {
+ const child = spawn(
+ process.execPath,
+ ["scripts/dev-with-automation.mjs"],
+ {
+ cwd: repoRoot,
+ env: {
+ PATH: "",
+ },
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ let output = "";
+ child.stdout.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+ child.stderr.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+
+ const exitResult = await Promise.race([
+ once(child, "exit").then(([code, signal]) => ({
+ code,
+ signal,
+ timedOut: false,
+ })),
+ delay(4_000).then(() => ({ code: null, signal: null, timedOut: true })),
+ ]);
+
+ if (exitResult.timedOut) {
+ child.kill("SIGKILL");
+ }
+
+ expect(exitResult.timedOut).toBe(false);
+ expect(exitResult.code).toBe(1);
+ expect(output).toContain("uvx");
+ });
+});
diff --git a/__tests__/scripts/ingress.test.ts b/__tests__/scripts/ingress.test.ts
new file mode 100644
index 0000000000..2cda7a6ce1
--- /dev/null
+++ b/__tests__/scripts/ingress.test.ts
@@ -0,0 +1,304 @@
+import { createServer, type Server } from "node:http";
+import { spawn, type ChildProcess } from "node:child_process";
+import { once } from "node:events";
+import path from "node:path";
+import { setTimeout as delay } from "node:timers/promises";
+import { fileURLToPath } from "node:url";
+import { describe, expect, it, beforeAll, afterAll, afterEach } from "vitest";
+
+const repoRoot = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ "../..",
+);
+
+const ingressScript = path.join(repoRoot, "scripts", "ingress.mjs");
+
+describe("ingress.mjs CLI", () => {
+ it("shows help with --help flag", async () => {
+ const child = spawn(process.execPath, [ingressScript, "--help"], {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+
+ let output = "";
+ child.stdout.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+
+ const [code] = await once(child, "exit");
+
+ expect(code).toBe(0);
+ expect(output).toContain("Standalone Ingress / Reverse Proxy");
+ expect(output).toContain("--port");
+ expect(output).toContain("--route");
+ expect(output).toContain("--default");
+ });
+
+ it("exits with error when no routes configured", async () => {
+ const child = spawn(process.execPath, [ingressScript], {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+
+ let stderr = "";
+ child.stderr.on("data", (chunk) => {
+ stderr += chunk.toString();
+ });
+
+ const [code] = await once(child, "exit");
+
+ expect(code).toBe(1);
+ expect(stderr).toContain("No routes configured");
+ });
+
+ it("parses --port argument correctly", async () => {
+ const child = spawn(
+ process.execPath,
+ [ingressScript, "--port", "9999", "--default", "http://localhost:3000"],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ let output = "";
+ child.stdout.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+
+ // Wait for startup message
+ await delay(500);
+ child.kill("SIGTERM");
+
+ expect(output).toContain("9999");
+ });
+
+ it("parses --route arguments correctly", async () => {
+ const child = spawn(
+ process.execPath,
+ [
+ ingressScript,
+ "--port",
+ "9998",
+ "--route",
+ "/api=http://localhost:8000",
+ "--route",
+ "/static=http://localhost:3000",
+ ],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ let output = "";
+ child.stdout.on("data", (chunk) => {
+ output += chunk.toString();
+ });
+
+ await delay(500);
+ child.kill("SIGTERM");
+
+ expect(output).toContain("/api");
+ expect(output).toContain("http://localhost:8000");
+ expect(output).toContain("/static");
+ expect(output).toContain("http://localhost:3000");
+ });
+});
+
+describe("ingress proxy functionality", () => {
+ let backend1: Server;
+ let backend2: Server;
+ let ingressProcess: ChildProcess;
+ const backend1Port = 19001;
+ const backend2Port = 19002;
+ const ingressPort = 19000;
+
+ beforeAll(async () => {
+ // Create mock backend 1
+ backend1 = createServer((req, res) => {
+ res.writeHead(200, { "Content-Type": "application/json" });
+ res.end(JSON.stringify({ backend: 1, path: req.url }));
+ });
+ await new Promise<void>((resolve) => backend1.listen(backend1Port, resolve));
+
+ // Create mock backend 2
+ backend2 = createServer((req, res) => {
+ res.writeHead(200, { "Content-Type": "application/json" });
+ res.end(JSON.stringify({ backend: 2, path: req.url }));
+ });
+ await new Promise<void>((resolve) => backend2.listen(backend2Port, resolve));
+
+ // Start ingress
+ ingressProcess = spawn(
+ process.execPath,
+ [
+ ingressScript,
+ "--port",
+ ingressPort.toString(),
+ "--route",
+ `/api/v2=http://localhost:${backend2Port}`,
+ "--route",
+ `/api=http://localhost:${backend1Port}`,
+ "--default",
+ `http://localhost:${backend1Port}`,
+ ],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ // Wait for ingress to start
+ await delay(1000);
+ });
+
+ afterAll(async () => {
+ ingressProcess?.kill("SIGTERM");
+ await new Promise<void>((resolve) => backend1?.close(() => resolve()));
+ await new Promise<void>((resolve) => backend2?.close(() => resolve()));
+ });
+
+ it("routes /api requests to backend1", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/api/test`);
+ const data = await response.json();
+
+ expect(response.status).toBe(200);
+ expect(data.backend).toBe(1);
+ expect(data.path).toBe("/api/test");
+ });
+
+ it("routes /api/v2 requests to backend2 (more specific route)", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/api/v2/test`);
+ const data = await response.json();
+
+ expect(response.status).toBe(200);
+ expect(data.backend).toBe(2);
+ expect(data.path).toBe("/api/v2/test");
+ });
+
+ it("routes unmatched paths to default backend", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/other/path`);
+ const data = await response.json();
+
+ expect(response.status).toBe(200);
+ expect(data.backend).toBe(1);
+ expect(data.path).toBe("/other/path");
+ });
+
+ it("preserves query parameters", async () => {
+ const response = await fetch(
+ `http://localhost:${ingressPort}/api/test?foo=bar&baz=123`,
+ );
+ const data = await response.json();
+
+ expect(response.status).toBe(200);
+ expect(data.path).toBe("/api/test?foo=bar&baz=123");
+ });
+
+ it("returns 502 when backend is unavailable", async () => {
+ // Start a fresh ingress pointing to a non-existent backend
+ const badIngressPort = 19003;
+ const badIngress = spawn(
+ process.execPath,
+ [
+ ingressScript,
+ "--port",
+ badIngressPort.toString(),
+ "--default",
+ "http://localhost:59999", // Non-existent port
+ ],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ await delay(500);
+
+ try {
+ const response = await fetch(`http://localhost:${badIngressPort}/test`);
+ expect(response.status).toBe(502);
+ const text = await response.text();
+ expect(text).toContain("Bad Gateway");
+ } finally {
+ badIngress.kill("SIGTERM");
+ }
+ });
+});
+
+describe("ingress route matching", () => {
+ let backend: Server;
+ let ingressProcess: ChildProcess;
+ const backendPort = 19011;
+ const ingressPort = 19010;
+
+ beforeAll(async () => {
+ backend = createServer((req, res) => {
+ res.writeHead(200, { "Content-Type": "text/plain" });
+ res.end(req.url);
+ });
+ await new Promise<void>((resolve) => backend.listen(backendPort, resolve));
+
+ ingressProcess = spawn(
+ process.execPath,
+ [
+ ingressScript,
+ "--port",
+ ingressPort.toString(),
+ "--route",
+ `/api/automation=http://localhost:${backendPort}`,
+ "--route",
+ `/api=http://localhost:${backendPort}`,
+ "--route",
+ `/sockets=http://localhost:${backendPort}`,
+ ],
+ {
+ cwd: repoRoot,
+ stdio: ["ignore", "pipe", "pipe"],
+ },
+ );
+
+ await delay(1000);
+ });
+
+ afterAll(async () => {
+ ingressProcess?.kill("SIGTERM");
+ await new Promise<void>((resolve) => backend?.close(() => resolve()));
+ });
+
+ it("matches exact path", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/api`);
+ expect(response.status).toBe(200);
+ });
+
+ it("matches path with trailing content", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/api/users`);
+ expect(response.status).toBe(200);
+ const text = await response.text();
+ expect(text).toBe("/api/users");
+ });
+
+ it("matches longer prefix before shorter", async () => {
+ const response = await fetch(
+ `http://localhost:${ingressPort}/api/automation/docs`,
+ );
+ expect(response.status).toBe(200);
+ const text = await response.text();
+ expect(text).toBe("/api/automation/docs");
+ });
+
+ it("matches path with query string", async () => {
+ const response = await fetch(
+ `http://localhost:${ingressPort}/api?foo=bar`,
+ );
+ expect(response.status).toBe(200);
+ const text = await response.text();
+ expect(text).toBe("/api?foo=bar");
+ });
+
+ it("returns 503 for unmatched routes with no default", async () => {
+ const response = await fetch(`http://localhost:${ingressPort}/unknown`);
+ expect(response.status).toBe(503);
+ });
+});
diff --git a/package.json b/package.json
index 070b4b40f5..6f18cba499 100644
--- a/package.json
+++ b/package.json
@@ -50,8 +50,9 @@
"zustand": "5.0.12"
},
"scripts": {
- "dev": "npm run dev:safe",
- "dev:safe": "node --env-file-if-exists=.env scripts/dev-safe.mjs",
+ "dev": "npm run dev:automation",
+ "dev:automation": "node --env-file-if-exists=.env scripts/dev-with-automation.mjs",
+ "dev:minimal": "node --env-file-if-exists=.env scripts/dev-safe.mjs",
"dev:frontend": "npm run make-i18n && cross-env VITE_MOCK_API=false react-router dev",
"dev:mock": "npm run make-i18n && cross-env VITE_MOCK_API=true react-router dev",
"build": "npm run build:app",
diff --git a/scripts/dev-with-automation.mjs b/scripts/dev-with-automation.mjs
new file mode 100644
index 0000000000..d14b8358aa
--- /dev/null
+++ b/scripts/dev-with-automation.mjs
@@ -0,0 +1,570 @@
+#!/usr/bin/env node
+/**
+ * Development Stack with Automation Service
+ *
+ * Extends agent-canvas's dev-safe.mjs to additionally run the OpenHands Automation
+ * backend via uvx. No cloning required - runs directly from git reference.
+ *
+ * Uses a standalone ingress proxy to route traffic to multiple backends.
+ *
+ * Architecture:
+ * β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ * β”‚ http://localhost:8000 (Ingress Proxy) β”‚
+ * β”‚ /api/automation/* β†’ Automation Backend β”‚
+ * β”‚ /api/*, /sockets β†’ Agent Server β”‚
+ * β”‚ /* β†’ Vite Dev Server β”‚
+ * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ * β”‚ β”‚ β”‚
+ * β–Ό β–Ό β–Ό
+ * β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
+ * β”‚ Vite β”‚ β”‚ Agent Server β”‚ β”‚ Automation β”‚
+ * β”‚ :3001 β”‚ β”‚ (uvx) :18000 β”‚ β”‚ Backend (uvx) β”‚
+ * β”‚ β”‚ β”‚ β”‚ β”‚ :18001 β”‚
+ * β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
+ *
+ * Usage:
+ * node scripts/dev-with-automation.mjs
+ * node scripts/dev-with-automation.mjs --automation-ref feat/my-branch
+ * node scripts/dev-with-automation.mjs --port 12000
+ *
+ * Environment variables:
+ * - PORT: Ingress port (default: 8000)
+ * - OH_AUTOMATION_GIT_REF: Git ref for automation (default: main)
+ * - OH_AGENT_SERVER_GIT_REF: Git ref for agent-server
+ */
+
+import { spawn, execSync } from "node:child_process";
+import { mkdirSync } from "node:fs";
+import { join, resolve, dirname } from "node:path";
+import { fileURLToPath } from "node:url";
+import { homedir } from "node:os";
+import { setTimeout as delay } from "node:timers/promises";
+import process from "node:process";
+
+import {
+ buildAgentServerCommand,
+ buildSafeDevConfig,
+ buildAgentServerEnv,
+ buildNpmScriptCommand,
+ formatMissingUvxGuidance,
+} from "./dev-safe.mjs";
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+const projectRoot = resolve(__dirname, "..");
+
+const DEFAULT_AUTOMATION_REPO = "https://github.com/OpenHands/automation";
+const DEFAULT_AUTOMATION_GIT_REF = "main";
+const DEFAULT_BACKEND_PORT = 18000;
+const DEFAULT_AUTOMATION_PORT = 18001;
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Terminal Styling
+// ═══════════════════════════════════════════════════════════════════════════
+
+const c = {
+ reset: "\x1b[0m",
+ bold: "\x1b[1m",
+ dim: "\x1b[2m",
+ red: "\x1b[31m",
+ green: "\x1b[32m",
+ yellow: "\x1b[33m",
+ blue: "\x1b[34m",
+ magenta: "\x1b[35m",
+ cyan: "\x1b[36m",
+};
+
+function logService(name, message, color = c.reset) {
+ const ts = new Date().toISOString().split("T")[1].split(".")[0];
+ console.log(`${c.dim}${ts}${c.reset} ${color}[${name}]${c.reset} ${message}`);
+}
+
+function logStep(step, message) {
+ console.log(`${c.cyan}[${step}]${c.reset} ${message}`);
+}
+
+function logSuccess(message) {
+ console.log(`${c.green}βœ“${c.reset} ${message}`);
+}
+
+function logError(message) {
+ console.error(`${c.red}βœ—${c.reset} ${message}`);
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Configuration
+// ═══════════════════════════════════════════════════════════════════════════
+
+function parseArgs() {
+ const args = process.argv.slice(2);
+ const config = {
+ port: null,
+ automationGitRef: null,
+ automationRepo: null,
+ verbose: false,
+ };
+
+ for (let i = 0; i < args.length; i++) {
+ switch (args[i]) {
+ case "-p":
+ case "--port":
+ config.port = parseInt(args[++i], 10);
+ break;
+ case "--automation-ref":
+ config.automationGitRef = args[++i];
+ break;
+ case "--automation-repo":
+ config.automationRepo = args[++i];
+ break;
+ case "-v":
+ case "--verbose":
+ config.verbose = true;
+ break;
+ case "-h":
+ case "--help":
+ showHelp();
+ process.exit(0);
+ }
+ }
+
+ return config;
+}
+
+function showHelp() {
+ console.log(`
+Agent Canvas + Automation Development Stack
+
+Runs agent-canvas with the automation backend (via uvx, no clone needed).
+Uses a standalone ingress proxy to route traffic.
+
+USAGE:
+ node scripts/dev-with-automation.mjs [options]
+
+OPTIONS:
+ -p, --port <port> Ingress port (default: 8000)
+ --automation-ref <ref> Git ref for automation (branch/tag/SHA, default: main)
+ --automation-repo <url> Git repo URL (default: ${DEFAULT_AUTOMATION_REPO})
+ -v, --verbose Show detailed output
+ -h, --help Show this help
+
+ENVIRONMENT VARIABLES:
+ PORT Alternative to --port
+ OH_AUTOMATION_GIT_REF Alternative to --automation-ref
+ OH_AGENT_SERVER_GIT_REF Git ref for agent-server SDK
+ OH_SECRET_KEY Secret key for sessions
+
+ACCESS POINTS:
+ Main UI: http://localhost:PORT/
+ API Docs: http://localhost:PORT/api/automation/docs
+`);
+}
+
+/**
+ * Build the uvx command for running automation backend.
+ */
+function buildAutomationCommand(env = process.env) {
+ const gitRef = env.OH_AUTOMATION_GIT_REF || DEFAULT_AUTOMATION_GIT_REF;
+ const repoUrl = env.OH_AUTOMATION_REPO || DEFAULT_AUTOMATION_REPO;
+
+ // Build git URL with ref
+ const gitUrl = `git+${repoUrl}@${gitRef}`;
+
+ return {
+ command: "uvx",
+ args: [
+ "--from", gitUrl,
+ "uvicorn", "automation.app:app",
+ ],
+ source: `git (${gitRef})`,
+ };
+}
+
+function buildConfig(args, env = process.env) {
+ // Apply args to env for buildAutomationCommand
+ if (args.automationGitRef) {
+ env.OH_AUTOMATION_GIT_REF = args.automationGitRef;
+ }
+ if (args.automationRepo) {
+ env.OH_AUTOMATION_REPO = args.automationRepo;
+ }
+
+ const ingressPort = args.port || parseInt(env.PORT, 10) || 8000;
+ const backendPort = DEFAULT_BACKEND_PORT;
+ const automationPort = DEFAULT_AUTOMATION_PORT;
+ const vitePort = 3001;
+ const vscodePort = backendPort + 1000;
+
+ return {
+ // Ingress port (main entry point)
+ ingressPort,
+
+ // Service ports (internal)
+ agentServerPort: backendPort,
+ autoBackendPort: automationPort,
+ vitePort,
+ vscodePort,
+
+ // Paths
+ canvasPath: projectRoot,
+
+ // Data directories (same as dev-safe.mjs)
+ stateDir: join(homedir(), ".openhands", "agent-canvas"),
+
+ verbose: args.verbose,
+ };
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Prerequisites & Setup
+// ═══════════════════════════════════════════════════════════════════════════
+
+function commandExists(cmd) {
+ try {
+ execSync(`command -v ${cmd}`, { stdio: "pipe" });
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+function checkPrerequisites() {
+ logStep("1/2", "Checking prerequisites...");
+
+ if (!commandExists("uvx")) {
+ console.error(formatMissingUvxGuidance(projectRoot));
+ process.exit(1);
+ }
+ logSuccess("uvx found");
+
+ if (!commandExists("npm")) {
+ logError("npm is required but not found");
+ process.exit(1);
+ }
+ logSuccess("npm found");
+}
+
+function ensureDirectories(config) {
+ const dirs = [
+ config.stateDir,
+ join(config.stateDir, "tmux"),
+ join(config.stateDir, "conversations"),
+ join(config.stateDir, "workspaces"),
+ join(config.stateDir, "bash_events"),
+ join(config.stateDir, "storage"),
+ ];
+
+ for (const dir of dirs) {
+ mkdirSync(dir, { recursive: true });
+ }
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Process Management
+// ═══════════════════════════════════════════════════════════════════════════
+
+const processes = new Map();
+
+function spawnService(name, command, args, options = {}) {
+ const proc = spawn(command, args, {
+ stdio: ["ignore", "pipe", "pipe"],
+ env: { ...process.env, ...options.env },
+ cwd: options.cwd,
+ shell: process.platform === "win32",
+ });
+
+ const color = options.color || c.reset;
+
+ proc.stdout.on("data", (data) => {
+ data
+ .toString()
+ .split("\n")
+ .filter(Boolean)
+ .forEach((line) => {
+ logService(name, line.trim(), color);
+ });
+ });
+
+ proc.stderr.on("data", (data) => {
+ data
+ .toString()
+ .split("\n")
+ .filter(Boolean)
+ .forEach((line) => {
+ logService(name, line.trim(), c.yellow);
+ });
+ });
+
+ proc.on("error", (error) => {
+ logError(`${name} failed to start: ${error.message}`);
+ });
+
+ proc.on("exit", (code, signal) => {
+ if (code !== 0 && code !== null && !shuttingDown) {
+ logService(name, `Exited with code ${code}`, c.red);
+ }
+ processes.delete(name);
+ });
+
+ processes.set(name, proc);
+ return proc;
+}
+
+async function waitForService(name, url, timeoutMs = 30000) {
+ const start = Date.now();
+
+ while (Date.now() - start < timeoutMs) {
+ try {
+ const res = await fetch(url);
+ if (res.ok) {
+ logService(name, `Ready at ${url}`, c.green);
+ return true;
+ }
+ } catch {
+ // Keep trying
+ }
+ await delay(500);
+ }
+
+ logService(name, `Timeout waiting for ${url}`, c.red);
+ return false;
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Service Starters
+// ═══════════════════════════════════════════════════════════════════════════
+
+function startAgentServer(config) {
+ logService("agent-server", `Starting on port ${config.agentServerPort}...`, c.blue);
+
+ const agentServerCmd = buildAgentServerCommand(process.env);
+ logService("agent-server", `Using ${agentServerCmd.source}`, c.dim);
+
+ // Build safe config for agent-server env vars
+ const safeConfig = buildSafeDevConfig(config.canvasPath, {
+ ...process.env,
+ OH_CANVAS_SAFE_STATE_DIR: config.stateDir,
+ OH_CANVAS_SAFE_BACKEND_PORT: config.agentServerPort.toString(),
+ OH_CANVAS_SAFE_VSCODE_PORT: config.vscodePort.toString(),
+ });
+
+ const agentServerEnv = buildAgentServerEnv(safeConfig);
+
+ spawnService(
+ "agent-server",
+ agentServerCmd.command,
+ [...agentServerCmd.args, "--host", "127.0.0.1", "--port", String(config.agentServerPort)],
+ {
+ cwd: safeConfig.workspacesPath,
+ env: agentServerEnv,
+ color: c.blue,
+ }
+ );
+}
+
+function startAutomationBackend(config) {
+ logService("automation", `Starting on port ${config.autoBackendPort}...`, c.green);
+
+ const automationCmd = buildAutomationCommand(process.env);
+ logService("automation", `Using ${automationCmd.source}`, c.dim);
+
+ spawnService(
+ "automation",
+ automationCmd.command,
+ [
+ ...automationCmd.args,
+ "--host", "127.0.0.1",
+ "--port", config.autoBackendPort.toString(),
+ ],
+ {
+ cwd: config.stateDir,
+ env: {
+ AUTOMATION_AGENT_SERVER_URL: `http://localhost:${config.agentServerPort}`,
+ AUTOMATION_DB_URL: `sqlite+aiosqlite:///${join(config.stateDir, "automations.db")}`,
+ AUTOMATION_BASE_URL: `http://localhost:3001`,
+ AUTOMATION_WORKSPACE_BASE: join(config.stateDir, "workspaces"),
+ AUTOMATION_AUTH_DISABLED: "true",
+ FILE_STORE: "local",
+ LOCAL_STORAGE_PATH: join(config.stateDir, "storage"),
+ OPENHANDS_SUPPRESS_BANNER: "1",
+ },
+ color: c.green,
+ }
+ );
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Main
+// ═══════════════════════════════════════════════════════════════════════════
+
+let shuttingDown = false;
+
+function shutdown() {
+ if (shuttingDown) return;
+ shuttingDown = true;
+
+ console.log("");
+ console.log(`${c.yellow}Shutting down...${c.reset}`);
+
+ for (const [name, proc] of processes) {
+ logService(name, "Stopping...", c.dim);
+ proc.kill("SIGTERM");
+ }
+
+ setTimeout(() => {
+ for (const [, proc] of processes) {
+ if (!proc.killed) {
+ proc.kill("SIGKILL");
+ }
+ }
+ process.exit(0);
+ }, 3000);
+}
+
+process.on("SIGINT", shutdown);
+process.on("SIGTERM", shutdown);
+
+function startIngress(config) {
+ logService("ingress", `Starting on port ${config.ingressPort}...`, c.yellow);
+
+ const ingressScript = join(projectRoot, "scripts", "ingress.mjs");
+
+ spawnService(
+ "ingress",
+ "node",
+ [
+ ingressScript,
+ "--port", config.ingressPort.toString(),
+ "--route", `/api/automation=http://localhost:${config.autoBackendPort}`,
+ "--route", `/api=http://localhost:${config.agentServerPort}`,
+ "--route", `/sockets=http://localhost:${config.agentServerPort}`,
+ "--route", `/server_info=http://localhost:${config.agentServerPort}`,
+ "--route", `/health=http://localhost:${config.agentServerPort}`,
+ "--route", `/ready=http://localhost:${config.agentServerPort}`,
+ "--route", `/alive=http://localhost:${config.agentServerPort}`,
+ "--default", `http://localhost:${config.vitePort}`,
+ ],
+ {
+ cwd: projectRoot,
+ color: c.yellow,
+ }
+ );
+}
+
+function startVite(config) {
+ logService("vite", `Starting on port ${config.vitePort}...`, c.magenta);
+
+ const frontendCommand = buildNpmScriptCommand("dev:frontend");
+
+ spawnService("vite", frontendCommand.command, frontendCommand.args, {
+ cwd: config.canvasPath,
+ env: {
+ // Point Vite at the ingress (so client-side fetches work)
+ VITE_BACKEND_HOST: `127.0.0.1:${config.ingressPort}`,
+ VITE_BACKEND_BASE_URL: `http://127.0.0.1:${config.ingressPort}`,
+ VITE_WORKING_DIR: join(config.stateDir, "workspaces"),
+ VITE_FRONTEND_PORT: config.vitePort.toString(),
+ },
+ color: c.magenta,
+ });
+}
+
+function printBanner(config) {
+ console.log("");
+ console.log(
+ `${c.green}${c.bold}╔══════════════════════════════════════════════════════════════╗${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•‘${c.reset} ${c.bold}Agent Canvas + Automation Stack${c.reset} ${c.green}${c.bold}β•‘${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}╠══════════════════════════════════════════════════════════════╣${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•‘${c.reset} ${c.green}${c.bold}β•‘${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•‘${c.reset} Main UI: ${c.cyan}http://localhost:${config.ingressPort}/${c.reset}`.padEnd(75) + `${c.green}${c.bold}β•‘${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•‘${c.reset} API Docs: ${c.cyan}http://localhost:${config.ingressPort}/api/automation/docs${c.reset}`.padEnd(75) + `${c.green}${c.bold}β•‘${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•‘${c.reset} ${c.green}${c.bold}β•‘${c.reset}`
+ );
+ console.log(
+ `${c.green}${c.bold}β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•${c.reset}`
+ );
+ console.log("");
+ console.log(`${c.dim}State directory: ${config.stateDir}${c.reset}`);
+ console.log(`${c.dim}Press Ctrl+C to stop${c.reset}`);
+ console.log("");
+}
+
+async function main() {
+ const args = parseArgs();
+ const config = buildConfig(args);
+
+ console.log("");
+ console.log(`${c.cyan}${c.bold}Agent Canvas + Automation Development Stack${c.reset}`);
+ console.log("");
+
+ // Setup phase
+ checkPrerequisites();
+ ensureDirectories(config);
+
+ // Start services phase
+ logStep("2/2", "Starting services...");
+
+ // 1. Start agent-server first (other services depend on it)
+ startAgentServer(config);
+ await waitForService(
+ "agent-server",
+ `http://localhost:${config.agentServerPort}/server_info`
+ );
+
+ // 2. Start automation backend
+ startAutomationBackend(config);
+
+ // 3. Start Vite dev server (no proxy config needed - ingress handles routing)
+ startVite(config);
+
+ // 4. Wait for services to be ready
+ await delay(2000);
+
+ // 5. Start ingress proxy (routes traffic to all backends)
+ startIngress(config);
+
+ // Wait for ingress to start
+ await delay(1000);
+
+ printBanner(config);
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Exports for testing
+// ═══════════════════════════════════════════════════════════════════════════
+
+export {
+ buildAutomationCommand,
+ buildConfig,
+ DEFAULT_AUTOMATION_REPO,
+ DEFAULT_AUTOMATION_GIT_REF,
+ DEFAULT_BACKEND_PORT,
+ DEFAULT_AUTOMATION_PORT,
+};
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Main entry point (only when run directly, not when imported)
+// ═══════════════════════════════════════════════════════════════════════════
+
+// Check if this module is the main entry point
+const isMainModule = import.meta.url === `file://${process.argv[1]}`;
+
+if (isMainModule) {
+ main().catch((err) => {
+ logError(`Fatal error: ${err.message}`);
+ if (err.stack) {
+ console.error(c.dim + err.stack + c.reset);
+ }
+ process.exit(1);
+ });
+}
diff --git a/scripts/ingress.mjs b/scripts/ingress.mjs
new file mode 100644
index 0000000000..fdbb205875
--- /dev/null
+++ b/scripts/ingress.mjs
@@ -0,0 +1,313 @@
+#!/usr/bin/env node
+/**
+ * Standalone Ingress / Reverse Proxy
+ *
+ * A minimal HTTP reverse proxy that routes requests to multiple backends
+ * based on URL path. Completely independent of any backend implementation.
+ *
+ * Usage:
+ * node scripts/ingress.mjs [options]
+ * node scripts/ingress.mjs --port 8000 --route "/api/automation=http://localhost:18001" --route "/api=http://localhost:18000" --default "http://localhost:3001"
+ *
+ * Environment variables:
+ * INGRESS_PORT - Port to listen on (default: 8000)
+ * INGRESS_ROUTES - JSON object of path prefix -> backend URL
+ * INGRESS_DEFAULT - Default backend for unmatched routes
+ *
+ * Route matching:
+ * - Routes are matched by longest prefix first
+ * - More specific routes take precedence (e.g., /api/automation before /api)
+ */
+
+import { createServer, request as httpRequest } from "node:http";
+import process from "node:process";
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Configuration
+// ═══════════════════════════════════════════════════════════════════════════
+
+function parseArgs() {
+ const args = process.argv.slice(2);
+ const config = {
+ port: 8000,
+ routes: {},
+ defaultBackend: null,
+ };
+
+ for (let i = 0; i < args.length; i++) {
+ switch (args[i]) {
+ case "-p":
+ case "--port":
+ config.port = parseInt(args[++i], 10);
+ break;
+ case "-r":
+ case "--route":
+ // Format: "/path=http://host:port"
+ const [path, url] = args[++i].split("=");
+ config.routes[path] = url;
+ break;
+ case "-d":
+ case "--default":
+ config.defaultBackend = args[++i];
+ break;
+ case "-h":
+ case "--help":
+ showHelp();
+ process.exit(0);
+ }
+ }
+
+ return config;
+}
+
+function showHelp() {
+ console.log(`
+Standalone Ingress / Reverse Proxy
+
+Routes HTTP requests to multiple backends based on URL path prefix.
+
+USAGE:
+ node scripts/ingress.mjs [options]
+
+OPTIONS:
+ -p, --port <port> Port to listen on (default: 8000)
+ -r, --route <path=url> Add a route (can be repeated)
+ -d, --default <url> Default backend for unmatched routes
+ -h, --help Show this help
+
+ENVIRONMENT VARIABLES:
+ INGRESS_PORT Port to listen on
+ INGRESS_ROUTES JSON object: {"path": "url", ...}
+ INGRESS_DEFAULT Default backend URL
+
+EXAMPLES:
+ # Basic setup with agent server and automation
+ node scripts/ingress.mjs \\
+ --port 8000 \\
+ --route "/api/automation=http://localhost:18001" \\
+ --route "/api=http://localhost:18000" \\
+ --route "/sockets=http://localhost:18000" \\
+ --default "http://localhost:3001"
+
+ # Using environment variables
+ INGRESS_PORT=8000 \\
+ INGRESS_ROUTES='{"/ api/automation":"http://localhost:18001","/api":"http://localhost:18000"}' \\
+ INGRESS_DEFAULT="http://localhost:3001" \\
+ node scripts/ingress.mjs
+
+ROUTE MATCHING:
+ Routes are sorted by path length (longest first), so more specific
+ routes like /api/automation will match before /api.
+`);
+}
+
+function buildConfig(args, env = process.env) {
+ let routes = { ...args.routes };
+
+ // Merge env routes
+ if (env.INGRESS_ROUTES) {
+ try {
+ const envRoutes = JSON.parse(env.INGRESS_ROUTES);
+ routes = { ...routes, ...envRoutes };
+ } catch (e) {
+ console.error("Failed to parse INGRESS_ROUTES:", e.message);
+ }
+ }
+
+ return {
+ port: args.port || parseInt(env.INGRESS_PORT, 10) || 8000,
+ routes,
+ defaultBackend: args.defaultBackend || env.INGRESS_DEFAULT || null,
+ };
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Router
+// ═══════════════════════════════════════════════════════════════════════════
+
+function createRouter(routes, defaultBackend) {
+ // Sort routes by path length (longest first) for most-specific matching
+ const sortedRoutes = Object.entries(routes).sort(
+ ([a], [b]) => b.length - a.length
+ );
+
+ return function route(url) {
+ for (const [prefix, backend] of sortedRoutes) {
+ if (url === prefix || url.startsWith(prefix + "/") || url.startsWith(prefix + "?")) {
+ return backend;
+ }
+ }
+ return defaultBackend;
+ };
+}
+
+function parseBackendUrl(backendUrl) {
+ const url = new URL(backendUrl);
+ return {
+ hostname: url.hostname,
+ port: parseInt(url.port, 10) || (url.protocol === "https:" ? 443 : 80),
+ protocol: url.protocol,
+ };
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Proxy
+// ═══════════════════════════════════════════════════════════════════════════
+
+function proxyRequest(req, res, backendUrl) {
+ const backend = parseBackendUrl(backendUrl);
+
+ const options = {
+ hostname: backend.hostname,
+ port: backend.port,
+ path: req.url,
+ method: req.method,
+ headers: {
+ ...req.headers,
+ host: `${backend.hostname}:${backend.port}`,
+ },
+ };
+
+ const proxyReq = httpRequest(options, (proxyRes) => {
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
+ proxyRes.pipe(res, { end: true });
+ });
+
+ proxyReq.on("error", (err) => {
+ console.error(`Proxy error for ${req.url}:`, err.message);
+ if (!res.headersSent) {
+ res.writeHead(502);
+ res.end(`Bad Gateway: ${err.message}`);
+ }
+ });
+
+ req.pipe(proxyReq, { end: true });
+}
+
+function proxyWebSocket(req, socket, head, backendUrl) {
+ const backend = parseBackendUrl(backendUrl);
+
+ const options = {
+ hostname: backend.hostname,
+ port: backend.port,
+ path: req.url,
+ method: req.method,
+ headers: {
+ ...req.headers,
+ host: `${backend.hostname}:${backend.port}`,
+ },
+ };
+
+ const proxyReq = httpRequest(options);
+
+ proxyReq.on("upgrade", (proxyRes, proxySocket, proxyHead) => {
+ socket.write(
+ `HTTP/${proxyRes.httpVersion} ${proxyRes.statusCode} ${proxyRes.statusMessage}\r\n`
+ );
+ for (let i = 0; i < proxyRes.rawHeaders.length; i += 2) {
+ socket.write(`${proxyRes.rawHeaders[i]}: ${proxyRes.rawHeaders[i + 1]}\r\n`);
+ }
+ socket.write("\r\n");
+
+ if (proxyHead.length > 0) {
+ socket.write(proxyHead);
+ }
+
+ proxySocket.pipe(socket, { end: true });
+ socket.pipe(proxySocket, { end: true });
+ });
+
+ proxyReq.on("error", (err) => {
+ console.error(`WebSocket proxy error for ${req.url}:`, err.message);
+ socket.destroy();
+ });
+
+ proxyReq.end();
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Server
+// ═══════════════════════════════════════════════════════════════════════════
+
+function startIngress(config) {
+ const route = createRouter(config.routes, config.defaultBackend);
+
+ const server = createServer((req, res) => {
+ const backend = route(req.url);
+
+ if (!backend) {
+ res.writeHead(503);
+ res.end("No backend configured for this route");
+ return;
+ }
+
+ proxyRequest(req, res, backend);
+ });
+
+ // Handle WebSocket upgrades
+ server.on("upgrade", (req, socket, head) => {
+ const backend = route(req.url);
+
+ if (!backend) {
+ socket.destroy();
+ return;
+ }
+
+ proxyWebSocket(req, socket, head, backend);
+ });
+
+ server.listen(config.port, () => {
+ console.log("");
+ console.log("╔═══════════════════════════════════════════════════════════════╗");
+ console.log("β•‘ Ingress Proxy β•‘");
+ console.log("╠═══════════════════════════════════════════════════════════════╣");
+ console.log(`β•‘ Listening on: http://localhost:${config.port}/`.padEnd(66) + "β•‘");
+ console.log("╠═══════════════════════════════════════════════════════════════╣");
+ console.log("β•‘ Routes: β•‘");
+
+ const sortedRoutes = Object.entries(config.routes).sort(
+ ([a], [b]) => b.length - a.length
+ );
+ for (const [path, backend] of sortedRoutes) {
+ const line = ` ${path} β†’ ${backend}`;
+ console.log(`β•‘ ${line.padEnd(61)}β•‘`);
+ }
+
+ if (config.defaultBackend) {
+ const line = ` * (default) β†’ ${config.defaultBackend}`;
+ console.log(`β•‘ ${line.padEnd(61)}β•‘`);
+ }
+
+ console.log("β•‘ β•‘");
+ console.log("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•");
+ console.log("");
+ });
+
+ return server;
+}
+
+// ═══════════════════════════════════════════════════════════════════════════
+// Main
+// ═══════════════════════════════════════════════════════════════════════════
+
+const args = parseArgs();
+const config = buildConfig(args);
+
+if (Object.keys(config.routes).length === 0 && !config.defaultBackend) {
+ console.error("Error: No routes configured. Use --route or --default options.");
+ console.error("Run with --help for usage information.");
+ process.exit(1);
+}
+
+startIngress(config);
+
+// Handle graceful shutdown
+process.on("SIGINT", () => {
+ console.log("\nShutting down...");
+ process.exit(0);
+});
+
+process.on("SIGTERM", () => {
+ console.log("\nShutting down...");
+ process.exit(0);
+});