Skip to content

refactor: extract GatewayHTTPHandler from WebSocketChannel#4115

Draft
chengyongru wants to merge 7 commits into
mainfrom
refactor/extract-ws-http-api
Draft

refactor: extract GatewayHTTPHandler from WebSocketChannel#4115
chengyongru wants to merge 7 commits into
mainfrom
refactor/extract-ws-http-api

Conversation

@chengyongru
Copy link
Copy Markdown
Collaborator

Summary

Extract all HTTP route handling from WebSocketChannel into a new GatewayHTTPHandler class in nanobot/channels/ws_http.py.

This is the first step toward decoupling WebUI, WebSocket transport, and AgentLoop — and a prerequisite for future hot-reload capabilities.

What changed

  • New file: nanobot/channels/ws_http.pyGatewayHTTPHandler (731 lines)

    • Token management (issue + API tokens)
    • Bootstrap endpoint
    • Session routes (list, messages, delete, webui-thread)
    • Media routes + HMAC signing/staging
    • Commands, sidebar state, workspaces
    • Static SPA serving
    • Delegates settings routes to existing WebUISettingsRouter
  • Modified: nanobot/channels/websocket.py — 1907 → 1372 lines (-28%)

    • _dispatch_http() now only handles WS upgrade directly, delegates everything else to GatewayHTTPHandler
    • send() delegates media helpers to handler
    • WS handshake auth delegates token validation to handler
    • Backwards-compat property aliases for test compatibility
  • Shared utilities: HTTP helper functions (_parse_query, _http_error, _bearer_token, etc.) moved to ws_http.py with websocket.py importing from there

What didn't change

  • Zero behavior change — all HTTP responses identical
  • All 3730 tests pass, 0 failures
  • Config schema unchanged
  • No breaking changes to external interfaces

Test plan

  • pytest tests/channels/ — 204 passed
  • pytest tests/ — 3730 passed, 27 skipped, 0 failed
  • ruff check — clean

@chengyongru chengyongru force-pushed the refactor/extract-ws-http-api branch from f2cc227 to b4ac502 Compare May 31, 2026 05:47
Extract all HTTP route handling (bootstrap, sessions, settings, media,
commands, sidebar state, static serving, token management) into a new
GatewayHTTPHandler class in nanobot/channels/ws_http.py.

WebSocketChannel is reduced from 1907 to 1372 lines (-28%), retaining
only WebSocket connection management and message dispatch.

No behavior change. 3730 tests pass, 0 failures.

Shared HTTP utility functions (path parsing, response builders, auth
helpers) now live in ws_http.py with websocket.py importing from there,
avoiding circular dependencies.

Backwards-compat property aliases on WebSocketChannel ensure existing
tests continue to work without modification.
@chengyongru chengyongru force-pushed the refactor/extract-ws-http-api branch from 4800fd9 to 74560fa Compare May 31, 2026 11:44
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.

1 participant