-
Notifications
You must be signed in to change notification settings - Fork 42
feat(cdp-proxy): add /json endpoint for Playwright connectOverCDP support #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…port The CDP proxy on port 9222 previously only implemented /json/version, which returned the WebSocket URL but didn't support target discovery. Playwright's connectOverCDP() fetches /json to discover browser targets before establishing a WebSocket connection. Without this endpoint, using `http://127.0.0.1:9222` with agent-browser or Playwright would fail, even though direct WebSocket connections (ws://127.0.0.1:9222) worked fine. This change: - Adds /json and /json/list endpoints that proxy to Chrome's /json - Rewrites webSocketDebuggerUrl and devtoolsFrontendUrl in the response to use the proxy's host instead of Chrome's internal host - Enables `agent-browser --cdp http://127.0.0.1:9222` to work correctly
Add comprehensive e2e tests verifying that agent-browser can connect to Chrome through the CDP proxy on port 9222. Tests validate: - /json endpoint returns targets with URLs rewritten to proxy port - /json/list endpoint works correctly with URL rewriting - /json/version endpoint continues to work - agent-browser works with various --cdp argument formats: - port only (9222) - http URL (http://127.0.0.1:9222) - localhost:port - 127.0.0.1:port - agent-browser snapshot and navigation commands work via proxy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
Rewrite the test to focus on validating the /json and /json/list endpoints added by the PR: - Test that /json returns targets with webSocketDebuggerUrl rewritten from port 9223 (Chrome) to port 9222 (proxy) - Test that /json/list works the same way - Test that /json/version continues to work - Add comparison test showing Chrome's direct /json on 9223 has unrewritten URLs Remove agent-browser tests since they required additional setup and the core functionality is validated by the JSON endpoint tests.
The TestUpstreamManagerDetectsChromiumAndRestart test was flaky in CI because Chromium's stderr output was fully buffered when connected to a file. The "DevTools listening on ws://..." line would sit in a buffer and never be flushed until the buffer filled or the process exited, causing the test to timeout after 20 seconds. Fix by using stdbuf -oL -eL to force line buffering on both stdout and stderr, ensuring each line is flushed immediately after the newline. This also improves test speed from ~17-20s to ~0.5s since we no longer wait for buffered output.
1. Add HTTP status code check before decoding JSON response from Chrome's /json endpoint. Previously, 4xx/5xx responses would result in confusing JSON decode errors like "invalid character '<'". 2. Fix devtoolsFrontendUrl rewriting for URLs with ws= query parameter. Chrome's devtoolsFrontendUrl often has the format: https://chrome-devtools-frontend.appspot.com/.../inspector.html?ws=127.0.0.1:9223/... The previous code only replaced the URL's host field, but in this format the Chrome host appears in the ws= query parameter. Now we handle both cases: direct host replacement and ws= query param replacement.
…2e test Playwright's connectOverCDP requests /json/version/ with a trailing slash, which was falling through to the WebSocket handler and returning a 426 "Upgrade Required" error. This prevented agent-browser (and other Playwright-based tools) from connecting via the CDP proxy on port 9222. - Register trailing-slash variants for /json, /json/, /json/list, /json/list/, /json/version, and /json/version/ endpoints - Add comprehensive e2e test for agent-browser that: - Installs agent-browser globally in the container - Tests CDP connection with port number format (--cdp 9222) - Tests CDP connection with http:// URL format (--cdp http://127.0.0.1:9222) - Verifies navigation, snapshot, get url, and get title commands This enables natural usage of agent-browser within containers: agent-browser --cdp 9222 open https://example.com agent-browser --cdp 9222 snapshot --json
Sayan-
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed the CDP proxy JSON endpoint changes:
-
main.go: New
/jsonand/json/listhandlers that proxy to Chrome and rewritewebSocketDebuggerUrlto point to the proxy (9222) instead of Chrome directly (9223). Also handlesws=query params indevtoolsFrontendUrl. Trailing slash variants added for Playwright compatibility. -
e2e test: Comprehensive tests covering endpoint responses, URL rewriting verification, and full agent-browser integration through the proxy.
-
proxy_test.go: Reliability fix using
stdbufto prevent test flakiness from buffered stderr in CI.
The two bugbot comments have been addressed in the current code. LGTM.
Summary
This PR enables running
agent-browser(and other Playwright-based tools) colocated on the VM itself, connecting through the CDP proxy on port 9222 instead of needing to use Chrome's internal port 9223.Motivation
When running
agent-browserdirectly inside a Kernel browser VM, users should be able to use the natural, documented approach:Previously this failed because:
connectOverCDP()fetches/jsonfor target discovery, which wasn't implemented/json/version/with a trailing slash, which fell through to the WebSocket handler and returned a 426 "Upgrade Required" errorChanges
/jsonand/json/listendpoints: Proxy to Chrome's/jsonand rewritewebSocketDebuggerUrlanddevtoolsFrontendUrlto point back to the proxy (port 9222 instead of 9223)/json/versionand/json/version/(and other endpoints) to handle Playwright's URL formatdevtoolsFrontendUrlrewriting: Handle thews=query parameter format used in DevTools frontend URLsagent-browserin the container and verifies CDP connectivity with multiple command formatsBefore
After
Test plan
go test ./lib/devtoolsproxy/...go test -run TestCDPProxy ./e2e/...go test -run TestAgentBrowserCDPProxy ./e2e/...