/**
* Level 1: HTTP Fetch Backend (Decoupled)
*
* Uses plain fetch() with configurable User-Agent, HTML parsing via
* node-html-parser, and Markdown conversion via turndown.
* No JavaScript execution — fastest path for static content.
*
* When called directly via the `webFetch()` entry point, this backend:
* - Fetches and converts HTML → Markdown
* - Runs JS-shell detection inline
* - Runs bot-detection heuristics inline
* - Caps content for inline display + spills to temp files when large
*
*/
import { writeFileSync, rmSync } from "node:fs";
import { createHash } from "node:crypto";
import {
BROWSER_TEMP_DIR,
safeTaskId,
ensureBrowserTempDir,
formatBytes,
} from "./shared/paths.js";
import TurndownService from "turndown";
import { parse as parseHtml } from "node-html-parser";
import { checkPage } from "./shared/bot-detection.js";
const DEFAULT_USER_AGENT =
"Mozilla/5.0 (compatible; PiBrowser/1.0; +https://pi.ai)";
const turndown = new TurndownService({
headingStyle: "atx",
codeBlockStyle: "fenced",
emDelimiter: "*",
});
/**
* Detect whether a page is a JS-only shell (empty
,
* mostly