Skip to content

fix(frontend): preserve path prefix in WebSocket URL for proxy deployments#12284

Merged
amanape merged 12 commits intoOpenHands:mainfrom
zxkane:fix/websocket-url-preserve-path-prefix
Jan 9, 2026
Merged

fix(frontend): preserve path prefix in WebSocket URL for proxy deployments#12284
amanape merged 12 commits intoOpenHands:mainfrom
zxkane:fix/websocket-url-preserve-path-prefix

Conversation

@zxkane
Copy link
Copy Markdown
Contributor

@zxkane zxkane commented Jan 7, 2026

Summary

When deploying OpenHands behind a reverse proxy that routes /runtime/{port}/... requests to agent-server containers, the V1 WebSocket connection fails because buildWebSocketUrl() only extracts the host from conversationUrl, losing the required path prefix.

Example:

  • conversationUrl: https://example.com/runtime/55313/api/conversations/abc123
  • Before: wss://example.com/sockets/events/abc123 ❌ (request goes to main app, not agent-server)
  • After: wss://example.com/runtime/55313/sockets/events/abc123 ✅ (correctly routed to agent-server)

Changes

  1. Added extractPathPrefix() helper function - Extracts the path before /api/conversations from the conversation URL (e.g., /runtime/55313)

  2. Updated buildWebSocketUrl() - Now includes the path prefix when building WebSocket URLs

  3. Updated buildHttpBaseUrl() - Also includes path prefix for consistency with other API calls

  4. Added comprehensive unit tests - Tests cover all scenarios including:

    • Standard URLs without path prefix
    • Proxy deployment URLs with path prefix
    • Fallback behavior for invalid/missing URLs

Context

This fix aligns V1 WebSocket behavior with the existing V0 socket.io provider in ws-client-provider.tsx, which correctly handles path prefixes:

// V0 implementation that works correctly
const pathBeforeApi = u.pathname.split("/api/conversations")[0] || "/";
socketPath = `${pathBeforeApi.replace(/\/$/, "")}/socket.io`;

Test Plan

  • Unit tests added for all affected functions
  • Verified WebSocket connection works in proxy deployment environment
  • Tested standard deployment (without path prefix) still works

Related Issues

Fixes #12283
Related to #12271 (App View never worked - same root cause for port accessibility)

@zxkane zxkane requested review from amanape and hieptl as code owners January 7, 2026 07:58
@amanape amanape self-assigned this Jan 7, 2026
Copy link
Copy Markdown
Contributor

@amanape amanape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to open a PR! These changes make sense, thanks for adding test coverage too. I just have a few requests before merging:

Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
Comment thread frontend/__tests__/utils/websocket-url.test.ts Outdated
@nileio
Copy link
Copy Markdown

nileio commented Jan 7, 2026

+1

zxkane and others added 10 commits January 9, 2026 10:22
…ments

When deploying OpenHands behind a reverse proxy that routes
`/runtime/{port}/...` requests to agent-server containers, the V1
WebSocket connection fails because `buildWebSocketUrl()` only extracts
the host from `conversationUrl`, losing the required path prefix.

This fix:
- Adds `extractPathPrefix()` helper to extract the path before
  `/api/conversations` from the conversation URL
- Updates `buildWebSocketUrl()` to include the path prefix
- Updates `buildHttpBaseUrl()` to include the path prefix for consistency

Example:
- conversationUrl: `https://example.com/runtime/55313/api/conversations/abc`
- Before: `wss://example.com/sockets/events/abc` (WRONG)
- After: `wss://example.com/runtime/55313/sockets/events/abc` (CORRECT)

This matches the behavior of the V0 socket.io provider in
`ws-client-provider.tsx` which correctly handles path prefixes.

Fixes OpenHands#12283
Tests cover:
- extractBaseHost: host extraction from various URL formats
- extractPathPrefix: path prefix extraction before /api/conversations
- buildHttpBaseUrl: HTTP base URL construction with path prefix
- buildWebSocketUrl: WebSocket URL construction with path prefix

All tests include scenarios for:
- Standard URLs without path prefix
- Proxy deployment URLs with path prefix (e.g., /runtime/{port}/)
- Fallback behavior for invalid/missing URLs
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
…l test

The OpenHands project uses `#/*` as the path alias (configured in tsconfig.json),
not `@/*`. This fix updates the import statement to use the correct alias.
@zxkane zxkane force-pushed the fix/websocket-url-preserve-path-prefix branch from 800529f to 5337e9e Compare January 9, 2026 02:22
@zxkane zxkane requested a review from amanape January 9, 2026 09:13
Copy link
Copy Markdown
Contributor

@amanape amanape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

@amanape amanape merged commit f3aaebd into OpenHands:main Jan 9, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: V1 WebSocket URL loses path prefix from conversationUrl in proxy deployments

3 participants