/** * Web search + scrape for the Worker/x402 side — TypeScript equivalent of * skillforge/research.py's provider router (Tavily -> Brave -> keyless * fallback for search; Firecrawl -> keyless fetch for scrape). * * skillforge/research.py drives its paid providers through a local MCP * server process (skillforge/mcp_client.py) — that mechanism doesn't exist * in a Cloudflare Worker (no subprocess/MCP protocol support), so this * calls each provider's plain REST API directly with fetch(). Deliberately * narrower than the Python router: Bright Data and Apify's real REST * contracts (as opposed to their MCP tool wrappers) weren't confirmed * against primary sources, so scrape here is Firecrawl -> keyless only, * same "real, not guessed" standard as everywhere else in this repo. No * quota guard either (skillforge/research.py's MONTHLY_QUOTA is a durable * file-backed counter — the Worker has no equivalent durable store wired * up); each provider's own account-level rate limit is the real backstop. */ export interface ResearchEnv { TAVILY_API_KEY?: string; BRAVE_API_KEY?: string; FIRECRAWL_API_KEY?: string; } export interface SearchResult { provider: string; results: Array<{ title: string; url: string; snippet: string }>; degraded?: boolean; } // ── minimal, portable HTML tokenizer ──────────────────────────────────────── // worker/src runs on BOTH Cloudflare Workers and Deno (worker/deno/deno-entry.ts // — see worker/README.md's "Cloudflare + Deno Deploy" section: "src/index.ts // has zero Cloudflare-specific code"), so Cloudflare's own HTMLRewriter global // isn't usable here — Deno's `deno check` has no declaration for it. This is // a real, hand-written single-pass tokenizer instead: it never re-scans // already-consumed input (unlike an iterative regex replace, which is exactly // the "incomplete multi-character sanitization" class of bug CodeQL flags — // e.g. stripping "