feat(webfetch): route through fetch-use when BROWSER_USE_API_KEY is set#49
Open
Alezander9 wants to merge 2 commits intomainfrom
Open
feat(webfetch): route through fetch-use when BROWSER_USE_API_KEY is set#49Alezander9 wants to merge 2 commits intomainfrom
Alezander9 wants to merge 2 commits intomainfrom
Conversation
Adds @browser-use/bcode-browser/fetch-use Effect service that POSTs to https://fetch.browser-use.com/fetch with the X-Browser-Use-API-Key header. The webfetch tool consults fetchUse.enabled and swaps the HTTP call when the key is present, preserving the existing native HttpClient + cloudflare-retry path otherwise. Opt-out: BCODE_NO_FETCH_USE=1. Decisions ref: decisions.md S3.3 / ROADMAP B1+B2. No webfetch schema changes (session_id / output_format / proxy_country deferred until evals show demand). B3 retired by Phase H.
There was a problem hiding this comment.
3 issues found across 8 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/opencode/test/tool/webfetch.test.ts">
<violation number="1" location="packages/opencode/test/tool/webfetch.test.ts:36">
P2: Don’t enable FetchUse unconditionally in this test harness; it makes the suite environment-dependent and can break when BROWSER_USE_API_KEY is present.</violation>
</file>
<file name="packages/bcode-browser/test/fetch-use.test.ts">
<violation number="1" location="packages/bcode-browser/test/fetch-use.test.ts:26">
P2: Skip the live fetch-use test when the opt-out env var is set too, not just when the API key is missing.</violation>
</file>
<file name="packages/opencode/src/tool/webfetch.ts">
<violation number="1" location="packages/opencode/src/tool/webfetch.ts:59">
P1: The fetch-use branch ignores the fetched page status code, so HTTP error pages (e.g. 404/500) are treated as successful content instead of failing like the native path.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| const { arrayBuffer, contentType } = yield* (fetchUse.enabled | ||
| ? fetchUse | ||
| .fetch(params.url, { timeoutMs: timeout }) | ||
| .pipe(Effect.map((r) => ({ arrayBuffer: r.body, contentType: r.contentType }))) |
There was a problem hiding this comment.
P1: The fetch-use branch ignores the fetched page status code, so HTTP error pages (e.g. 404/500) are treated as successful content instead of failing like the native path.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/tool/webfetch.ts, line 59:
<comment>The fetch-use branch ignores the fetched page status code, so HTTP error pages (e.g. 404/500) are treated as successful content instead of failing like the native path.</comment>
<file context>
@@ -47,61 +49,77 @@ export const WebFetchTool = Tool.define(
+ const { arrayBuffer, contentType } = yield* (fetchUse.enabled
+ ? fetchUse
+ .fetch(params.url, { timeoutMs: timeout })
+ .pipe(Effect.map((r) => ({ arrayBuffer: r.body, contentType: r.contentType })))
+ : Effect.gen(function* () {
+ // Build Accept header based on requested format with q parameters for fallbacks
</file context>
Suggested change
| .pipe(Effect.map((r) => ({ arrayBuffer: r.body, contentType: r.contentType }))) | |
| .pipe( | |
| Effect.flatMap((r) => | |
| r.statusCode >= 200 && r.statusCode < 300 | |
| ? Effect.succeed({ arrayBuffer: r.body, contentType: r.contentType }) | |
| : Effect.fail(new Error(`Request failed with status ${r.statusCode}`)), | |
| ), | |
| ) |
| expect(enabled).toBe(haveKey && process.env.BCODE_NO_FETCH_USE !== "1") | ||
| }) | ||
|
|
||
| test.skipIf(!haveKey)("live: fetches httpbin and returns body + content-type", async () => { |
There was a problem hiding this comment.
P2: Skip the live fetch-use test when the opt-out env var is set too, not just when the API key is missing.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/bcode-browser/test/fetch-use.test.ts, line 26:
<comment>Skip the live fetch-use test when the opt-out env var is set too, not just when the API key is missing.</comment>
<file context>
@@ -0,0 +1,40 @@
+ expect(enabled).toBe(haveKey && process.env.BCODE_NO_FETCH_USE !== "1")
+})
+
+test.skipIf(!haveKey)("live: fetches httpbin and returns body + content-type", async () => {
+ const result = await Effect.gen(function* () {
+ const svc = yield* FetchUse.Service
</file context>
…ten service - Opt-out is now experimental.fetch_use=false in opencode.json (was BCODE_NO_FETCH_USE env var). One field added to the existing experimental config struct. - FetchUse service tightened from ~95 to ~55 LOC: inline interface, single object Service.of, dropped statusCode from FetchResult (unused). Behavior unchanged. - webfetch.ts opt-out check moves from service-internal env read to call-site config check. FetchUse.enabled now reflects only key presence. - Tests updated; live + native paths verified.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Routes the
webfetchtool through Browser Use'sfetch-usecloud (Chrome JA4 fingerprint, HTTP/2 header order, session-based cookie persistence, proxy pool) wheneverBROWSER_USE_API_KEYis set. Falls back to the existing nativeHttpClient+ cloudflare-retry path otherwise.Implements ROADMAP B1 + B2. B3 retired by Phase H (Python harness goes away).
What changed
packages/bcode-browser/src/fetch-use.ts(~80 LOC): Effect service. ReadsBROWSER_USE_API_KEYonce at layer init, exposesenabledgetter andfetch(url, opts) -> {body, contentType, statusCode}method. POSTs tohttps://fetch.browser-use.com/fetchwithX-Browser-Use-API-Key.packages/opencode/src/tool/webfetch.ts: one branch inexecuteswaps the request whenfetchUse.enabled. Existing post-processing (image-attachment / markdown / text / html conversion) is unchanged. No schema changes.packages/opencode/src/tool/registry.ts: addsFetchUse.Serviceto the layer requirements +Layer.provide(FetchUse.layer)to defaultLayer.FetchUse.layerwhere ToolRegistry/WebFetchTool is wired.Why direct Level-3 mod, not a plugin hook or upstream PR
We own browsercode/. Reducing diff against
anomalyco/opencodematters for upstream-pull friction, not approval. Adding plugin-hook indirection inside our fork would touch more files (webfetch.ts + plugin types + registration) for a single fetch backend. The direct branch is smallest-total-code.Opt-out
BCODE_NO_FETCH_USE=1env var. Chose env overopencode.jsonconfig because it keeps the diff bounded to one upstream file.Schema deferred
Originally planned
session_id/output_format/proxy_countryschema additions on thewebfetchtool are deferred. They add prompt verbosity for features evals havent shown we need. Easy to add later if demand emerges.Verified
bun typecheckclean acrossbcode-browserandopencode.packages/opencode: 3/3 webfetch tests pass (native fallback path unchanged).packages/bcode-browser/test/fetch-use.test.ts:enabledreflects env vars correctly.https://httpbin.org/getreturned 200 withapplication/jsonand the expected body. Skipped whenBROWSER_USE_API_KEYis unset.Notes
packages/bcode-browser/harness/is unrelated untracked content and not part of this PR.