feat: support browser-level CDP websocket fallback#408
feat: support browser-level CDP websocket fallback#408coolxll wants to merge 10 commits intojackwener:mainfrom
Conversation
27ee008 to
3644d37
Compare
|
Good direction — browser-level CDP fallback aligns with Chrome's newer debugging model. Found some issues in the current implementation: CriticalCreated tabs are never cleaned up
The option is registered on all Suggestion: only register Warnings
Fallback chain skips
Host whitelist missing IPv6 loopback — |
# Conflicts: # src/commanderAdapter.ts
|
Follow-up update:
Verification completed on the consolidated implementation before sync:
PR #406 has been closed as superseded by this PR. 后续更新:
合并前验证:
PR #406 已关闭,并由当前 PR 接续。 |
|
@Astro-Han followed up on your feedback here:
Verification on the consolidated implementation before sync:
If you have time for a re-check, this branch is ready for another pass. |
|
Thanks for the thorough follow-up — 6 out of 8 fixes verified cleanly. The blank tab cleanup, page-only targeting, /json/version fallback, verbose logging, shared helper extraction, and IPv6 support all look good. Two remaining items I noticed, both around desktop adapter scoping:
The field is correctly derived at registration time via Default derivation might miss non-localhost desktop apps The condition checks Everything else looks solid. Nice work consolidating #406 into this branch. |
|
Thanks for the follow-up review. I fixed both remaining desktop-scoping issues in this branch.
Also added regression coverage for:
Verification after the follow-up fix:
Please take another look when you have time. 感谢跟进 review。我已经把这两个剩余的 desktop scope 问题修掉了。
另外补了回归测试,覆盖:
修复后的验证:
有空的话麻烦再帮忙看一轮。 |
|
修复确认了,manifest 序列化和推导逻辑都没问题,回归测试也补上了。辛苦! |
|
Follow-up fix for the latest review items:
Verification:
最新一轮 review 的两点也已修复:
验证:
|
# Conflicts: # src/commanderAdapter.test.ts
Summary / 摘要
This PR improves Chrome CDP connectivity for newer remote debugging flows and adds a practical direct-CDP mode for browser-backed commands.
这个 PR 一方面增强了 opencli 对新版 Chrome 远程调试流程的兼容性,另一方面为浏览器型命令补齐了可实际使用的直连 CDP 模式。
What This PR Includes / 本 PR 包含的内容
Browser-level websocket fallback when classic
/jsondiscovery is unavailable.Explicit
--browser-cdpmode for browser-backed commands to bypass the daemon / extension path.Global
OPENCLI_BROWSER_CDP=1default to enable the same behavior without repeating the flag.Safer auto mode that opens a fresh page by default, instead of accidentally attaching to an unrelated existing tab.
当经典
/json发现路径不可用时,回退到浏览器级 websocket。为浏览器型命令增加显式
--browser-cdp模式,直接绕过 daemon / extension 链路。增加全局
OPENCLI_BROWSER_CDP=1,无需每次重复传 flag。改进 auto 模式,默认新开干净页面,避免误附着到不相关的现有标签页。
Official Reference / 官方背书
This change is aligned with Chrome's newer DevTools / MCP direction documented by the Chrome team:
https://developer.chrome.com/blog/chrome-devtools-mcp-debug-your-browser-session?hl=zh-cn
The official post shows that newer Chrome debugging sessions can rely on browser-session level connectivity rather than the older
/jsondiscovery path alone.这个改动和 Chrome 官方介绍的新 DevTools / MCP 方向是一致的:
https://developer.chrome.com/blog/chrome-devtools-mcp-debug-your-browser-session?hl=zh-cn
官方文章说明,较新的 Chrome 调试会话可以围绕浏览器会话级连接来工作,而不再只依赖旧的
/json发现路径。Why / 背景与原因
In newer Chrome remote debugging setups, the debugging port can be active while classic endpoints like
/jsonreturn404.In that situation, opencli's previous CDP implementation could not connect to ordinary Chrome pages even though the browser debugging session was actually available.
Also, for normal Chrome website automation, users may want an explicit or global command mode that clearly routes through browser CDP and does not depend on whether the daemon / extension path is working.
During local testing, auto-attaching to an existing browser tab could also land on the wrong page context, which made website commands fail with unrelated page errors.
在较新的 Chrome 远程调试模式下,调试端口可能已经开启,但经典的
/json接口会返回404。这种情况下,opencli 之前的 CDP 实现即使面对真实可用的浏览器调试会话,也无法连接普通 Chrome 页面。
另外,在普通 Chrome 网站自动化链路里,用户既需要显式模式,也需要全局默认模式,明确要求走浏览器 CDP,而不是依赖 daemon / extension 是否可用。
同时在本地测试中,auto 模式如果附着到已有标签页,也可能落到错误页面上下文,导致网站命令报出与目标站点无关的页面错误。
Changes / 修改内容
add fallback logic from
http://.../jsonto browser-level websocket discovery viaDevToolsActivePortsupport selecting inspectable targets from
Target.getTargetsresults even when they do not includewebSocketDebuggerUrlattach to the chosen page target via
Target.attachToTarget(flatten: true)and route subsequent commands through the returned session idadd explicit
--browser-cdpsupport for built-in browser commands and browser-backed registry commandsadd global
OPENCLI_BROWSER_CDP=1support so browser-backed commands can default to direct CDP modeadd
--no-browser-cdpso one command can opt out even when the global default is enabledwhen browser CDP auto-discovery is used without an explicit target hint, open a fresh blank page first instead of attaching to a random existing tab
keep the existing explicit
OPENCLI_CDP_ENDPOINTpath unchanged when users already configure itadd unit tests for browser websocket detection, auto-discovery parsing, runtime override behavior, and CLI option registration
增加从
http://.../json回退到DevToolsActivePort浏览器级 websocket 发现的逻辑支持从
Target.getTargets返回的目标中选择可附着页面,即使这些目标没有webSocketDebuggerUrl通过
Target.attachToTarget(flatten: true)附着到目标页面,并将后续命令路由到对应 session id为内置浏览器命令和 browser-backed registry commands 增加显式
--browser-cdp增加全局
OPENCLI_BROWSER_CDP=1,让浏览器命令默认走 direct CDP 模式增加
--no-browser-cdp,即使全局默认开启也可以单次关闭当 auto-discovery 且没有显式 target hint 时,默认先新开空白页,而不是附着到随机现有标签页
对已经显式设置
OPENCLI_CDP_ENDPOINT的场景保持原有行为不变增加浏览器 websocket 检测、自动发现解析、runtime 覆盖逻辑和 CLI 选项注册的单元测试
Verification / 验证
npm run typechecknpm testnpm run buildlocal smoke test with Chrome on
127.0.0.1:9222verified that
opencli ... --browser-cdpreaches browser command execution without falling back to the daemon / extension error pathverified that
OPENCLI_BROWSER_CDP=1enables the same path globallyverified a real local command:
opencli linux-do categories --browser-cdpverified the same command also works with only
OPENCLI_BROWSER_CDP=1npm run typechecknpm testnpm run build在本地
127.0.0.1:9222Chrome 会话上完成 smoke test已验证
opencli ... --browser-cdp可以进入浏览器命令执行流程,不再掉回 daemon / extension 报错链路已验证
OPENCLI_BROWSER_CDP=1可以全局开启同一路径已验证本地真实命令:
opencli linux-do categories --browser-cdp也已验证只设置
OPENCLI_BROWSER_CDP=1时同一命令可正常运行Screenshot / 截图
Example / 示例
opencli cascade https://news.ycombinator.com --browser-cdp opencli explore https://linux.do --browser-cdp export OPENCLI_BROWSER_CDP=1 opencli linux-do categories opencli linux-do categories --no-browser-cdp