Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
Open the app's sockets against the server, so no proxy has to carry them (#421)
Vaibhav Zope8817249fecc9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex daf65045..3a19ed2a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -90,6 +90,18 @@ by itself.Nothing about existing rows changes. Every row already written, and every row a person's own clickwrites from now on, reads as a person, because that is what it was.+### The live screen and live channel updates work again, and one request can no longer end the app++The app opened its two WebSockets against its own address, so they travelled through Vite's `/api`+proxy. Vite is run through bun, and under bun that proxy does not carry a WebSocket: neither the+Bot's screen nor live channel updates ever connected, and the browser retried in a loop. Worse, an+upgrade the server answered with an ordinary HTTP response — a 503 when a Bot's computer is not+running, which is exactly when somebody opens the screen — crashed the process that served the app,+taking the server and the worker with it in development. Where Vite serves the app — the dev server+and the desktop's `vite preview` — both sockets now address the server directly, so nothing upgrades+through the proxy and the proxy no longer offers to carry one. In production the server serves the+app itself and answers the upgrade on the same origin, so the sockets stay on the browser's own+host, which is what an ingress terminating TLS on 443 requires and a fixed server port would break.## 0.0.8diff --git a/app/src/components/computer/live-screen.tsx b/app/src/components/computer/live-screen.tsxindex 356e0f2b..b9486f25 100644--- a/app/src/components/computer/live-screen.tsx+++ b/app/src/components/computer/live-screen.tsx@@ -1,5 +1,6 @@import { useCallback, useEffect, useRef, useState } from "react";import { pageCoordinates } from "./take-the-wheel";+import { socketUrl } from "@/lib/socket-url";/*** Low-latency screencast used while a human is driving the Bot's browser.@@ -57,10 +58,10 @@ export function LiveScreen({ computerId, driving, onProblem }: Props) {const [connected, setConnected] = useState(false);useEffect(() => {- // Same origin, so the scheme follows the page: wss when the app is served over https.- const scheme = window.location.protocol === "https:" ? "wss" : "ws";+ // The server's own address, so no proxy has to carry the upgrade. The scheme still follows+ // the page: wss when the app is served over https.const socket = new WebSocket(- `${scheme}://${window.location.host}/api/computers/${encodeURIComponent(computerId)}/stream`,+ socketUrl(`/api/computers/${encodeURIComponent(computerId)}/stream`),);socketRef.current = socket;let closed = false;diff --git a/app/src/lib/channels/use-channel-events.ts b/app/src/lib/channels/use-channel-events.tsindex ed714cc5..6381cc09 100644--- a/app/src/lib/channels/use-channel-events.ts+++ b/app/src/lib/channels/use-channel-events.ts@@ -2,6 +2,7 @@ import { useQueryClient } from "@tanstack/react-query";import { useRouter } from "@tanstack/react-router";import { useEffect } from "react";import { type ChannelPage, type ChannelSummary, channelKeys } from "./queries";+import { socketUrl as buildSocketUrl } from "@/lib/socket-url";/*** Keep the roster live.@@ -197,9 +198,7 @@ const FIRST_RETRY_MS = 500;const MAX_RETRY_MS = 30_000;function socketUrl() {- const url = new URL("/api/channels/events", window.location.href);- url.protocol = url.protocol === "https:" ? "wss:" : "ws:";- return url.toString();+ return buildSocketUrl("/api/channels/events");}export function useChannelEvents() {diff --git a/app/src/lib/socket-url.ts b/app/src/lib/socket-url.tsnew file mode 100644index 00000000..0822c0fb--- /dev/null+++ b/app/src/lib/socket-url.ts@@ -0,0 +1,44 @@+/**+ * A WebSocket's address, same-origin unless a Vite runtime says otherwise.+ *+ * In production the server serves the app and answers the `/api` WebSocket upgrade on the same+ * origin, so the browser's own host is the target — including behind an ingress that terminates TLS+ * on 443 and never exposes the container's port. Deriving the socket from `window.location` is the+ * standard shape for a reverse-proxied deployment, and it is the default here because it is the case+ * that must not break.+ *+ * Development and the desktop's `vite preview` are the exception. There Vite serves the app on+ * APP_PORT and proxies `/api` to the server on SERVER_PORT, a different origin, and under bun that+ * proxy cannot carry a WebSocket at all (oven-sh/bun#24127): the upgrade is answered with a plain+ * HTTP response and the socket never opens. So those two runtimes address the server directly, by+ * the port each announces on `window.__OPENBOT_WS_PORT__`. Nothing announces it in the built bundle+ * the server hands out, so a production page has no port to override with and stays same-origin.+ */+declare global {+ interface Window {+ __OPENBOT_WS_PORT__?: string;+ }+}++function announcedPort(): string {+ return typeof window !== "undefined" &&+ typeof window.__OPENBOT_WS_PORT__ === "string"+ ? window.__OPENBOT_WS_PORT__+ : "";+}++export function socketUrl(+ path: string,+ location: {+ protocol: string;+ hostname: string;+ host: string;+ } = window.location,+ port: string = announcedPort(),+): string {+ const scheme = location.protocol === "https:" ? "wss:" : "ws:";+ // A port only when a Vite runtime named one: the app is not same-origin with the server there.+ // Otherwise the browser's own host, which is the server's own origin in production.+ const authority = port ? `${location.hostname}:${port}` : location.host;+ return `${scheme}//${authority}${path}`;+}diff --git a/app/tests/socket-url.test.ts b/app/tests/socket-url.test.tsnew file mode 100644index 00000000..6ec3b542--- /dev/null+++ b/app/tests/socket-url.test.ts@@ -0,0 +1,64 @@+import { describe, expect, test } from "bun:test";+import { socketUrl } from "../src/lib/socket-url";++const at = (protocol: string, hostname: string, host: string) => ({+ protocol,+ hostname,+ host,+});++describe("where a socket is opened", () => {+ test("goes to the server's port rather than the page's, so no proxy carries it", () => {+ expect(+ socketUrl(+ "/api/channels/events",+ at("http:", "localhost", "localhost:3010"),+ "3001",+ ),+ ).toBe("ws://localhost:3001/api/channels/events");+ });++ test("keeps the host the person actually used, and changes only the port", () => {+ expect(+ socketUrl(+ "/api/channels/events",+ at("http:", "192.168.1.10", "192.168.1.10:3010"),+ "3001",+ ),+ ).toBe("ws://192.168.1.10:3001/api/channels/events");+ });++ test("stays on the page's own origin when no server port is announced", () => {+ expect(+ socketUrl(+ "/api/channels/events",+ at("https:", "openbot.example", "openbot.example"),+ "",+ ),+ ).toBe("wss://openbot.example/api/channels/events");+ });++ test("behind an ingress that only exposes 443, stays on 443, never the container port", () => {+ // The case that must not break, and the one a baked server port broke: production serves the+ // app and answers the upgrade same-origin, behind an ingress that terminates TLS on 443 and+ // never exposes the container's port. No Vite runtime announces a port there, so the socket+ // stays on the origin the browser loaded rather than being sent to a port nothing routes.+ expect(+ socketUrl(+ "/api/computers/a/stream",+ at("https:", "openbot.example", "openbot.example"),+ "",+ ),+ ).toBe("wss://openbot.example/api/computers/a/stream");+ });++ test("follows the page's scheme, so an https page never opens an insecure socket", () => {+ expect(+ socketUrl(+ "/api/computers/a/stream",+ at("https:", "openbot.example", "openbot.example:8443"),+ "3001",+ ),+ ).toBe("wss://openbot.example:3001/api/computers/a/stream");+ });+});diff --git a/app/vite.config.ts b/app/vite.config.tsindex 1379c8f2..aa32c26c 100644--- a/app/vite.config.ts+++ b/app/vite.config.ts@@ -1,10 +1,64 @@+import { readFileSync } from "node:fs";import path from "node:path";import tailwindcss from "@tailwindcss/vite";import { tanstackRouter } from "@tanstack/router-plugin/vite";import react from "@vitejs/plugin-react";-import { defineConfig } from "vite";+import { defineConfig, type Plugin } from "vite";import { listenPort } from "../shared/listen-port";+/*+ * Announce the server's port to the two runtimes that serve the app through Vite, and to no other.+ *+ * The dev server and the desktop's `vite preview` serve the app on APP_PORT and proxy `/api` to the+ * server, but under bun that proxy cannot carry a WebSocket (oven-sh/bun#24127), so the app's+ * sockets address the server directly on this port. `socketUrl` reads it off `window`.+ *+ * It is deliberately kept OUT of the built HTML. In production the server serves that same HTML on+ * its own origin and answers the upgrade there, so the socket is same-origin; a baked port would+ * point it at a container port an ingress terminating TLS on 443 does not expose, which is how a+ * build-time constant broke the fleet. The dev injection is gated on `ctx.server` so it never runs+ * during `vite build`; `vite preview` serves the static build unchanged and so is handled by its+ * own middleware below.+ */+function announceServerPort(port: number): Plugin {+ const tag = `<script>window.__OPENBOT_WS_PORT__=${JSON.stringify(String(port))};</script>`;+ const inject = (html: string) =>+ html.includes("__OPENBOT_WS_PORT__")+ ? html+ : html.replace("</head>", ` ${tag}\n </head>`);+ return {+ name: "openbot-announce-server-port",+ transformIndexHtml(html, ctx) {+ return ctx.server ? inject(html) : html;+ },+ configurePreviewServer(server) {+ const indexPath = path.resolve(__dirname, "dist", "index.html");+ server.middlewares.use((request, response, next) => {+ const requestPath = (request.url ?? "/").split("?")[0];+ // Only the SPA entry, and this middleware runs before Vite's own — so everything Vite must+ // still handle has to fall through: a non-GET, an `/api` call the proxy carries to the+ // server (extension-less GETs like `/api/bots` included), and an asset with a file+ // extension. What is left is a navigation, which gets the app shell with the port announced.+ if (+ request.method !== "GET" ||+ requestPath.startsWith("/api") ||+ /\.[^/]+$/.test(requestPath)+ ) {+ return next();+ }+ let html: string;+ try {+ html = readFileSync(indexPath, "utf8");+ } catch {+ return next();+ }+ response.setHeader("content-type", "text/html; charset=utf-8");+ response.end(inject(html));+ });+ },+ };+}+/** The same address and the same proxy whether this is the dev server or the preview of a build.*@@ -31,17 +85,19 @@ const serving = {port: appPort.port,strictPort: true,proxy: {- // `ws: true` is required for the live screen. Without it Vite answers the upgrade request with- // the app's HTML and the socket fails with an opaque error that looks like a server problem."/api": {target: `http://localhost:${apiPort.port}`,- ws: true,},},};export default defineConfig({- plugins: [tanstackRouter(), react(), tailwindcss()],+ plugins: [+ announceServerPort(apiPort.port),+ tanstackRouter(),+ react(),+ tailwindcss(),+ ],resolve: {alias: {"@": path.resolve(__dirname, "./src"),