Skip to content

[feat]: support client routing for worker gateway & cache requests - #3208

Merged
seanmcguire12 merged 6 commits into
bb-client-api/04-go-browserbase-transportfrom
bb-client-api/05-worker-browserbase-routing
Oct 18, 2026
Merged

seanmcguire12 merged 6 commits into
bb-client-api/04-go-browserbase-transportfrom
bb-client-api/05-worker-browserbase-routing

Conversation

@seanmcguire12

@seanmcguire12 seanmcguire12 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

why

this PR connects the worker's model gateway & cache clients to the new RPC transport. aisdk continues to build gateway requests, interpret responses, & decide when to retry

what changed

  • added a worker adapter that sends browserbase.request & reconstructs a native Response or the corresponding transport error for existing callers
  • added cancellable RPC sends using the existing request ID, including cleanup on abort, disconnect, & late replies
  • routed Gateway requests through the adapter when initialization selects client transport. automatic model selection, explicit headers, & AI SDK retry behavior remain in the worker
  • routed cache reads & writes through the same adapter. each cache request keeps one five-second budget across serialization, RPC, & HTTP response buffering
  • preserved direct-provider inference, custom LLM callbacks, batch overrides, & cache failure fallback

test plan

  • response-adapter tests cover HTTP status & headers, empty bodies, transport errors, cancellation ordering, disconnects, & late replies.
  • Gateway tests exercise both transports against the same header, response, retry, & backoff expectations.
  • cache tests cover hits, misses, read/write failures, & the shared timeout budget without adding retries
  • runtime tests cover ordinary & batch calls, custom callbacks, model overrides, close/drain behavior, & disconnects during inference

Summary by cubic

Connects the worker's Browserbase Model Gateway and cache clients to the new RPC transport, so inference and cache HTTP requests are sent by the connected SDK instead of direct HTTP from the worker.

  • Adds a worker adapter that sends browserbase.request over RPC and reconstructs a native Response or the matching transport error for existing callers.
  • Makes RPC sends cancellable via the existing request ID, with cleanup on abort, disconnect, and late replies.
  • Routes Gateway requests through the adapter when initialization selects client transport; AI SDK retry and backoff behavior, explicit headers, and automatic model selection stay in the worker.
  • Routes cache reads and writes through the same adapter, keeping one shared five-second budget per request across serialization, RPC, and response buffering.
  • Preserves direct-provider inference, custom LLM callbacks, batch overrides, and cache failure fallback.
  • Rebuilds the extension assets bundle, removes duplicated gateway tests, and updates the client/server version compatibility tests.

Written for commit 015eaf9. Summary will update on new commits.

View guided diff Turn on auto-fix

@changeset-bot

changeset-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 015eaf9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@browserbasehq/stagehand-protocol Minor
@browserbasehq/stagehand-extension Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 22 files

Architecture diagram
sequenceDiagram
    participant SDK as Connected SDK
    participant Worker as Stagehand Worker
    participant Router as RPC Router
    participant RPC as RPC Client
    participant Adapter as Browserbase Adapter
    participant Cache as Cache Client
    participant Gateway as Gateway Client
    participant AI SDK as AI SDK
    participant Upstream as Browserbase API

    Note over SDK,Upstream: Client transport mode (browserbase: "client")

    alt Legacy transport (API key configured)
        Worker->>Gateway: buildGatewayContext(apiKey + sessionId)
        Worker->>Cache: buildCacheContext(apiKey + sessionId)
        Gateway->>Upstream: Direct HTTP POST /llm/responses
        Cache->>Upstream: Direct HTTP POST /cache/get|set
    else Client transport (browserbase: "client")
        Worker->>Gateway: buildGatewayContext(sessionId + adapter)
        Worker->>Cache: buildCacheContext(sessionId + adapter)
        Worker->>AI SDK: createGatewayLanguageModel(config, gateway)
    end

    Note over AI SDK,SDK: Gateway inference flow (client transport)

    AI SDK->>Gateway: generateText() with model
    Gateway->>Adapter: clientGatewayFetch() - operation: gateway.responses
    Adapter->>RPC: send(browserbase.request, params, {signal, onAbort})
    RPC->>Router: Dispatch request with request ID
    Router->>SDK: browserbase.request (RPC)
    SDK->>Upstream: HTTP POST /llm/responses (Buffered)
    Upstream-->>SDK: HTTP Response
    SDK-->>Router: Response payload
    Router-->>RPC: Response payload
    RPC-->>Adapter: Reconstruct native Response
    Adapter-->>AI SDK: Response object
    AI SDK-->>Gateway: Model result
    Gateway-->>Worker: Inference result

    alt Upstream network error
        Adapter->>Adapter: Throw TypeError("fetch failed")
        AI SDK->>AI SDK: Retry with backoff (ownership in worker)
    end

    alt Request aborted (caller signal)
        Adapter->>RPC: notify(browserbase.cancel, {requestId, reason})
        RPC-->>RPC: Cleanup pending + listeners
    end

    Note over Cache,SDK: Cache request flow (client transport)

    Worker->>Cache: get(request) with 5s budget
    Cache->>Cache: Serialize request body (budget continues)
    Cache->>Adapter: requestBrowserbase() - operation: cache.get
    Adapter->>RPC: send(browserbase.request, params, {signal, onAbort})
    RPC->>Router: Dispatch request with request ID
    Router->>SDK: browserbase.request (RPC)
    SDK->>Upstream: HTTP POST /cache/get
    Upstream-->>SDK: HTTP Response
    SDK-->>Router: Response payload
    Router-->>RPC: Response payload
    RPC-->>Adapter: Reconstruct native Response
    Adapter-->>Cache: Response object
    Cache->>Cache: Validate envelope + schema
    Cache-->>Worker: Parsed cache result

    alt Cache read failure
        Cache-->>Worker: Error
        Worker->>Worker: Fallback to live inference
    end

    alt Cache request timeout (5s budget)
        Cache->>Adapter: Abort signal (TimeoutError)
        Adapter->>RPC: notify(browserbase.cancel, {requestId, reason: "timeout"})
        Cache-->>Worker: TimeoutError
    end

    Note over RPC,SDK: Cancellation handling

    alt RPC dispatch in-flight + abort
        RPC->>RPC: Reject pending immediately
        RPC-->>RPC: Mark aborted flag
        RPC-->>RPC: Delayed onAbort notification after dispatch
    else Abort before dispatch
        RPC->>RPC: throwIfAborted() - no dispatch
    else Disconnect
        RPC-->>Adapter: "Browserbase connection is disconnected"
    end

    Note over Worker,SDK: Runtime adapters wiring

    Worker->>Worker: createStagehandRuntime(clientBrowserbaseRequest)
    Worker->>Controller: stagehandController with adapter
    Controller->>Gateway: Pass adapter to buildGatewayContext
    Controller->>Cache: Pass adapter to buildCacheContext
    Worker->>ServiceWorker: startStagehandServiceWorker registers adapter
    ServiceWorker->>RPC: Verify RPC client connected
    RPC-->>ServiceWorker: requestBrowserbase() callable
    ServiceWorker-->>Worker: Adapter wired to runtime
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

@seanmcguire12
seanmcguire12 added this pull request to stack #3211 October 15, 2026 22:12
@seanmcguire12
seanmcguire12 force-pushed the bb-client-api/05-worker-browserbase-routing branch from 3853acb to af16fc4 Compare October 15, 2026 23:43
@seanmcguire12
seanmcguire12 force-pushed the bb-client-api/05-worker-browserbase-routing branch from af16fc4 to 015eaf9 Compare October 16, 2026 00:06
if (!this.transport.request) throw new Error("Client Browserbase transport is unavailable");
response = await this.transport.request(
{
operation: path === "/cache/get" ? "cache.get" : "cache.set",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full tree goes out even when the project has caching disabled, since the server only checks that after receiving the body. A caching-enabled flag at init, or remembering not_enabled for the session, would skip most of that traffic.

@seanmcguire12
seanmcguire12 merged commit 4a7c637 into main Oct 18, 2026
42 of 64 checks passed
@seanmcguire12
seanmcguire12 deleted the bb-client-api/05-worker-browserbase-routing branch October 18, 2026 20:26
miguelg719 pushed a commit that referenced this pull request Oct 20, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @browserbasehq/[email protected]

### Minor Changes

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3253](#3253)
[`6bf4146`](6bf4146)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
optional `extensionCdpUrl` to `localBrowser.connect` to allow
configuring the in-browser CDP url

- [#3263](#3263)
[`ecb2a6b`](ecb2a6b)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`headers` to `localBrowser.connect` so CDP gateways can be authenticated
without credentials in the URL

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`page.waitForFunction()` to wait until a predicate in the page returns a
truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Major Changes

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

### Minor Changes

- [#3208](#3208)
[`4a7c637`](4a7c637)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Support
routing Browserbase Model Gateway and cache requests through the
connected SDK.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add the
`page.wait_for_function` method.
## @browserbasehq/[email protected]

### Minor Changes

- [#3208](#3208)
[`4a7c637`](4a7c637)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Support
routing Browserbase Model Gateway and cache requests through the
connected SDK.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add the
`page.wait_for_function` method.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3257](#3257)
[`113ad81`](113ad81)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - honour a
changed extension CDP WS URL when creating a new Stagehand instance
after closing the previous one

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.
## @browserbasehq/[email protected]

### Minor Changes

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3265](#3265)
[`a31e2da`](a31e2da)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`Headers` to `LocalBrowserConnectOptions` so CDP gateways can be
authenticated without credentials in the URL

- [#3254](#3254)
[`9eac212`](9eac212)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
optional `ExtensionCDPURL` to `LocalBrowserConnectOptions` to allow
configuring the in-browser CDP url.

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`Page.WaitForFunction` to wait until a predicate in the page returns a
truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Minor Changes

- [#3260](#3260)
[`2859fd5`](2859fd5)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`headers` to `local_browser.connect` so CDP gateways can be
authenticated without credentials in the URL

- [#3267](#3267)
[`cb9a1d1`](cb9a1d1)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
extension archive getter

- [#3252](#3252)
[`21e0363`](21e0363)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add
`extension_cdp_url` to `local_browser.connect` for clients that reach
Chrome through a proxy the extension cannot use

- [#3183](#3183)
[`901eb27`](901eb27)
Thanks [@supremeboxlogos](https://github.com/supremeboxlogos)! - Add
`page.wait_for_function()` to wait until a predicate in the page returns
a truthy value.

### Patch Changes

- [#3229](#3229)
[`c6b6f27`](c6b6f27)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip
caching for request bodies larger than 25 MB.

- [#3198](#3198)
[`10418f9`](10418f9)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable
WebMCP by default for local chromium versions newer than 157

- [#3210](#3210)
[`1c40ba7`](1c40ba7)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Require
client transport for Browserbase Model Gateway and cache requests.

- [#3209](#3209)
[`17de6ea`](17de6ea)
Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Route
Browserbase Model Gateway and cache requests through the connected SDK.
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
##
@browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies
[[`c6b6f27`](c6b6f27),
[`cb9a1d1`](cb9a1d1),
[`10418f9`](10418f9),
[`1c40ba7`](1c40ba7),
[`17de6ea`](17de6ea),
[`6bf4146`](6bf4146),
[`ecb2a6b`](ecb2a6b),
[`901eb27`](901eb27)]:
  - @browserbasehq/[email protected]
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]
## @browserbasehq/[email protected]

### Patch Changes

- Updated dependencies []:
  - @browserbasehq/[email protected]

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants