feat: add web_search and web_fetch tools - #2
Conversation
…ng snippet execution
…et ReferenceErrors
When DevToolsActivePort is absent (Chrome started with --remote-debugging-port=9333), probe common fixed ports via /json/version to discover running instances. This makes session.connect() auto-detect existing browsers without manual wsUrl.
Port web search and fetch functionality from ds4 WebFetch to CDP. Adds two new Pi agent tools registered through a dedicated extension: - web_search: Google search via Chrome with consent dialog handling, up to 20 visible result links and text snapshot as markdown - web_fetch: URL page fetch with dynamic scrolling, semantic HTML extraction (headings, paragraphs, lists, code, blockquotes), up to 80 links, 900KB truncation Both tools share Chrome profile for session continuity and use profileDir to reuse an existing Chrome session. Includes: - src/web-fetch.ts: core web fetch/search logic (418 lines) - extensions/browser-execute-web.ts: Pi extension adapter (142 lines) - test/web-fetch.test.ts: 20 unit tests - test/browser-execute-web.test.ts: 9 integration tests - Updated README (EN + ZH), package.json, minor type fix in session.ts 59/60 tests pass (1 pre-existing failure: Chrome-on-port-9333)
… curl Make tool descriptions explicitly tell the LLM to prefer these tools over curl for web searches and page fetching. Add stronger guidance in promptGuidelines to use web_search/web_fetch instead of curl.
Change Target.createTarget from background: true to background: false so the user can see Chrome navigating during web search and fetch operations, matching the 'visible Chrome' design goal.
The promptSnippet was changed from 'Chrome' to 'curl' to emphasize the tool is a curl replacement. Update test accordingly.
Integration tests connect to Chrome on port 9333, navigate to pages, extract content, and leave tabs visible for 15 seconds so the user can visually verify tabs actually open during web_search/web_fetch.
The web_search and web_fetch tools now keep the Chrome tab visible for 15 seconds after extracting content, so the user can see what the extension did. The default is 15 seconds but can be configured via the keepTabVisibleMs option. Unit tests pass keepTabVisibleMs: 0 to avoid artificial delays.
- Update ensureConnected() to call session.connect() when profileDir is not provided, allowing detectBrowsers to find Chrome on common ports. - Fix whitespace-only query validation in webSearch. - Update tests to mock Session.connect for error cases to avoid connecting to real Chrome during unit tests. This ensures Chrome tabs open and stay visible for 15 seconds after extraction, even when profileDir is not explicitly provided.
The pi extension loader requires explicit file extensions in the manifest. Added .ts to browser-execute and browser-execute-web paths.
Add onProgress callback to web_search and web_fetch tools so the agent session can display status updates (query URL, navigating, extracting) to the user in real-time. This helps distinguish multiple concurrent browser calls in the tool execution log.
Add custom renderCall functions to web_search and web_fetch tools so the URL/query appears inline with the tool name in the agent UI, similar to how bash shows '$ command' inline. This helps distinguish multiple concurrent browser calls in the tool execution log.
Previously runPage() awaited a 15-second delay before closing the tab, blocking the entire tool call and preventing the pi agent from continuing to the next step. Now the tab closes in the background via setTimeout, so the tool returns immediately after content extraction. The Chrome tab stays visible for the full duration so the user can still see what happened, but the agent can proceed without waiting.
|
Thank you again for the continued work here. I really appreciate the energy and the concrete implementation. The idea of making Chrome-backed fetch/search visible to the user is interesting, and several infrastructure pieces here could be valuable for the project. I did a first pass review and also tested the branch in a temporary worktree. Validation results:
Main blockers before this can be merged:
For the user-facing
Thanks again. This is useful work; I just want to keep the merge path small and safe for existing users. |
Summary
Port web search and fetch functionality from ds4 WebFetch to CDP. Adds two new Pi agent tools registered through a dedicated extension.
New Tools
Both tools share Chrome profile for session continuity and use
profileDirto reuse an existing Chrome session.Files Changed
src/web-fetch.ts: core web fetch/search logic (418 lines)extensions/browser-execute-web.ts: Pi extension adapter (142 lines)test/web-fetch.test.ts: 20 unit teststest/browser-execute-web.test.ts: 9 integration testsREADME.md+README.zh-CN.md: documentation updatespackage.json: extension registrationsrc/cdp/session.ts: minor type fixTests