Fix WebSocket connection in dev mode (`bun run dev`)

AL
5mo ago
+15-6

Problem

When running the project with bun run dev, agent terminals never connect — you can't type into them and no output appears. This affects anyone developing against the repo (as opposed to the published single-server openui command, which works fine).

Root cause

In dev, the frontend runs on Vite (6969) and proxies to the Bun backend (6968). The HTTP /api proxy works, but the /ws proxy does not: Vite forwards the upgrade and Bun accepts it (the backend logs [ws] Connected), but Vite's underlying http-proxy cannot relay Bun.serve's 101 Switching Protocols response back to the browser. The socket never opens and the connection silently times out.

This is a known incompatibility between Vite's http-proxy and Bun.serve WebSockets — not specific to any agent.

Fix

  • client/src/components/Terminal.tsx — in dev, connect the terminal WebSocket directly to the backend (window.location.hostname + backend port) instead of through the Vite proxy. The backend port is configurable via VITE_BACKEND_PORT (defaults to 6968). Production/single-server mode is unchanged: there the backend serves the built client itself, so window.location.host already points at the backend.
  • client/vite.config.ts — remove the non-functional /ws proxy entry (with a comment explaining why). The /api proxy is untouched.

The backend performs no Origin check on upgrade, so the cross-port WebSocket from the dev page is accepted.

Verification

With the fix and bun run dev running:

  1. Created a session via the (proxied) /api.
  2. Opened the terminal WebSocket directly to the backend with a browser Origin: http://localhost:6969 header.
  3. Result: ws OPEN and command output received ✓.

Terminals now work in dev mode without falling back to single-server/production mode.

Notes

  • Two files changed, +10 / −5. No behavior change in production.
  • Independent of #6 (the Copilot agent PR); this is a pre-existing bug affecting all agents.

Summary by cubic

Fixes terminal WebSocket connections in dev by bypassing Vite’s proxy and connecting directly to the Bun backend. In dev, mirror the page protocol (ws/wss) to satisfy browser mixed-content rules; production/single-server is unchanged.

  • Bug Fixes
    • In client/src/components/Terminal.tsx, in dev connect directly to ${protocol}//${window.location.hostname}:${<VITE_BACKEND_PORT|6968>}/ws where protocol mirrors the page; production reuses the current host and protocol.
    • In client/vite.config.ts, remove the non-functional /ws proxy (keep /api), since Vite’s http-proxy can’t relay Bun.serve’s upgrade response.