Description
The webfetch tool crashes with HTMLRewriter is not defined when called with format: "text" on HTML content in the Electron desktop app (OpenCode.exe).
Root Cause
The extractTextFromHTML() function in packages/opencode/src/tool/webfetch.ts calls new HTMLRewriter() as an un-imported global:
https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/tool/webfetch.ts
HTMLRewriter is a built-in global in Bun and Cloudflare Workers (workerd), but it does not exist in Node.js or Electron.
| Runtime |
HTMLRewriter available? |
| Bun (CLI/TUI mode) |
Yes, built-in global |
| Cloudflare Workers |
Yes, built-in global |
| Node.js |
No |
| Electron (desktop app) |
No |
The OpenCode desktop app (OpenCode.exe) runs on Electron, so HTMLRewriter is undefined at runtime.
OpenCode Version
- Desktop app version: 1.14.39 (also confirmed in app.asar as 1.1.0)
- Platform: Windows (Electron)
Steps to Reproduce
- Open OpenCode desktop app (Electron version)
- Use the
webfetch tool with format: "text" on any HTML page:
- e.g.,
webfetch with url https://github.com and format "text"
- Observe the error:
HTMLRewriter is not defined
Workaround
Use format: "markdown" (the default) instead of format: "text". The markdown path uses TurndownService (pure JavaScript) and works correctly in all runtimes.
Suggested Fix
The simplest fix would be to replace HTMLRewriter with a runtime-agnostic approach. Three options:
Option 1 — Use TurndownService for text extraction too (simplest):
The format: "markdown" path already uses TurndownService to convert HTML to markdown. For format: "text", the markdown output could be further stripped of formatting characters.
Option 2 — Use a simple regex-based approach:
Replace extractTextFromHTML with a regex-based tag stripper that removes <script>, <style>, <noscript> blocks and then strips remaining HTML tags. This would work in all JavaScript runtimes without dependencies.
Option 3 — Add a conditional polyfill:
Detect whether HTMLRewriter is available at runtime, and fall back to a regex-based approach when it is not. This preserves the Bun/Workers fast path while adding Electron compatibility.
Additional Context
- No existing issues appear to cover this specific bug (searched for "HTMLRewriter" and "webfetch" in issues)
- PR fix: resolve memory leaks and zombie processes from missing cleanup handlers #13186 previously patched an O(n²)
text += pattern in the same function but did not address the missing HTMLRewriter global
- The bug does not affect
format: "markdown" (default) or format: "html"
- The bug does not affect non-HTML content (JSON, plain text, API responses)
Operating System
Windows 11
Terminal
OpenCode Desktop (Electron)