Skip to content

重构 Playwright 浏览器管理层,统一参数与页面调度#101

Open
Ebola-Chan-bot wants to merge 2 commits into
Aas-ee:mainfrom
Ebola-Chan-bot:feat/playwright-unified-management
Open

重构 Playwright 浏览器管理层,统一参数与页面调度#101
Ebola-Chan-bot wants to merge 2 commits into
Aas-ee:mainfrom
Ebola-Chan-bot:feat/playwright-unified-management

Conversation

@Ebola-Chan-bot

@Ebola-Chan-bot Ebola-Chan-bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

修改动机

  • fetchWebContent有时也会唤起浏览器,但有时因无关紧要的参数差异而不和search唤起的浏览器共享。因此将大多数参数统一到playwright层处理,应用层仅可要求将纯无头模式替换为隐藏有头模式(bing)。
  • headless=new实测不可靠,某些环境下,fetchWebContent仍有可能唤起可见窗口,因此需要和隐藏有头模式一样使用隐藏桌面。
  • 浏览器进程意外死亡时没有重启机制,导致MCP直接失败

这些问题牵扯浏览器的启动参数、生命周期、窗口等许多共享更改,难以拆分成多个PR,因此合并解决。

具体修改方案

浏览器启动参数收归 Playwright 层统一管理

  • buildLocalBrowserProcessArgs 接管所有反检测参数(Windows 极简 --no-first-run,非 Windows 15 项)
  • openPlaywrightBrowser 参数简化为可选 antiBot 标志,移除 launchArgs
  • Windows 本地 headless 模式自动使用隐藏桌面启动,配合 --headless=new 双重保障无窗口

页面池统一调度

  • 新增 retryOnBrowserCrash 通用重试函数,浏览器崩溃自动重建恢复
  • searchBingWithPlaywright 改用 retryOnBrowserCrash 简化重试逻辑
  • fetchPageHtmlWithBrowser 改用 acquirePooledPlaywrightPage 复用页面池,不再创建独立 context
  • 修复 getOrCreateLocalBrowserSession 跨 session 缓存销毁时机,支持 headless 复用 hidden-headed 浏览器

浏览器 profile 持久化

  • 浏览器 profile 目录改为固定路径(%TEMP%/open-websearch-browser-profiles/),不再每次随机创建
  • 引入 cleanupStaleProfileLocks 清理崩溃残留的 SingletonLock 等锁文件
  • closeLocalBrowserSession 和 forceKill 不再删除 profile 目录

WS/CDP 远程连接改进

  • release 不再关闭远程浏览器,仅断开 Playwright 连接

Bing 引擎简化

  • 删除 buildBrowserLaunchArgs、shouldUseHiddenHeadedBingBrowser 等反检测启动参数构建函数
  • 修复 waitForFunction 的 TypeScript 类型注解导致 tsx 注入 __name 变量报错的问题

新增测试

  • test-browser-sharing:双模式(无头+有头)search/fetch/search 全流程浏览器进程共享验证
  • test-no-window:通过 koffi Win32 API 高频轮询验证 headless 模式零窗口闪现
  • test-bing-parser:移除过时的 launchArgs 相关测试用例

浏览器启动参数收归 Playwright 层统一管理
- buildLocalBrowserProcessArgs 接管所有反检测参数(Windows 极简 --no-first-run,非 Windows 15 项)
- openPlaywrightBrowser 参数简化为可选 antiBot 标志,移除 launchArgs
- Windows 本地 headless 模式自动使用隐藏桌面启动,配合 --headless=new 双重保障无窗口
- 隐藏桌面浏览器支持 headless 参数透传

页面池统一调度
- 新增 retryOnBrowserCrash 通用重试函数,浏览器崩溃自动重建恢复
- searchBingWithPlaywright 改用 retryOnBrowserCrash 简化重试逻辑
- fetchPageHtmlWithBrowser 改用 acquirePooledPlaywrightPage 复用页面池,不再创建独立 context
- 修复 getOrCreateLocalBrowserSession 跨 session 缓存销毁时机,支持 headless 复用 hidden-headed 浏览器

浏览器 profile 持久化
- 浏览器 profile 目录改为固定路径,不再每次随机创建
- 引入 cleanupStaleProfileLocks 清理崩溃残留的 SingletonLock 等锁文件
- closeLocalBrowserSession 和 forceKill 不再删除 profile 目录

WS/CDP 远程连接改进
- release 不再关闭远程浏览器,仅断开 Playwright 连接

Bing 引擎简化
- 删除 buildBrowserLaunchArgs、shouldUseHiddenHeadedBingBrowser 等反检测启动参数构建函数
- 移除废弃的 getEffectiveBingPlaywrightHeadless 函数
- 修复 waitForFunction 的类型注解导致 tsx 注入 __name 变量报错的问题

新增测试
- test-browser-sharing:双模式(无头+有头)search/fetch/search 全流程浏览器进程共享验证
- test-no-window:通过 koffi Win32 API 高频轮询验证 headless 模式零窗口闪现
- test-bing-parser:移除过时的 launchArgs 相关测试用例
Copilot AI review requested due to automatic review settings July 5, 2026 08:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

本 PR 旨在重构并统一 Playwright 本地/远程浏览器管理层:将启动参数与生命周期管理下沉到 playwrightClient,引入浏览器崩溃自动重试能力,并将 fetch 场景接入页面池以提升跨功能的浏览器/页面复用一致性;同时补充 Windows 下“无窗口闪现”和“跨 search/fetch 共享进程”的回归脚本。

Changes:

  • Playwright 本地会话:统一启动参数构建、引入持久化 profile 目录与锁文件清理,并调整本地 session 缓存/复用逻辑
  • Playwright 操作可靠性:新增 retryOnBrowserCrash,Bing Playwright 搜索接入通用崩溃重试
  • fetch HTML:改用 acquirePooledPlaywrightPage 复用页面池;新增 Windows 相关回归脚本测试

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/utils/playwrightClient.ts 统一本地浏览器启动/复用/恢复逻辑,引入持久化 profile 与崩溃重试入口,并简化 openPlaywrightBrowser 参数
src/utils/browserCookies.ts fetchPageHtmlWithBrowser 接入页面池以复用页面/浏览器会话
src/engines/bing/bing.ts Bing Playwright 搜索改为通过 openPlaywrightBrowser({ antiBot: true }) + retryOnBrowserCrash,并调整 waitForFunction 注入方式
src/test/test-no-window.ts 新增 Windows 下 headless 零窗口闪现回归脚本
src/test/test-browser-sharing.ts 新增 Windows 下 search/fetch/search 共享浏览器进程回归脚本(含有头/无头两种模式)
src/test/test-bing-parser.ts 移除与旧 launchArgs/反检测参数相关的过时测试断言

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils/playwrightClient.ts Outdated
Comment thread src/engines/bing/bing.ts Outdated
Comment thread src/utils/playwrightClient.ts
Comment thread src/utils/playwrightClient.ts
Comment thread src/utils/playwrightClient.ts
Comment thread src/test/test-no-window.ts
Comment thread src/test/test-browser-sharing.ts
Comment thread src/utils/browserCookies.ts
采纳的审查意见
- 移除 playwrightClient.ts 和 bing.ts 的 UTF-8 BOM
- WS/CDP 远程连接 release 改用 browser.close() 断开 Playwright 连接
- 恢复 antiBot 参数的精确语义:仅 bing 用它将无头改为隐藏有头(headless=false)
- test-browser-sharing 和 test-no-window 增加平台守卫和 OPEN_WEBSEARCH_INTEGRATION_TESTS 环境变量

修正浏览器模式逻辑
- 普通无头(fetchWebContent 等)自动走隐藏桌面 + --headless=new
- bing 传 antiBot 时将无头改为隐藏有头(headless=false,不加 --headless=new)
- 两种模式的 hideWindow 独立判断,互不干扰

其他改进
- test-browser-sharing 测试前增加 shutdownLocalPlaywrightBrowserSessions 清理残留缓存
- test-no-window 简化为直接测试 fetchWebContent 窗口检测
- browserCookies fetchPageHtmlWithBrowser 添加 Cookie 隔离取舍的注释说明
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.

2 participants