feat(workers-ai-provider): add custom fetch option for credentials mode#480
Merged
threepointone merged 1 commit intomainfrom Apr 5, 2026
Merged
feat(workers-ai-provider): add custom fetch option for credentials mode#480threepointone merged 1 commit intomainfrom
threepointone merged 1 commit intomainfrom
Conversation
Expose an optional fetch parameter when using accountId+apiKey credentials so callers can intercept or provide a custom fetch implementation (useful for testing). Pass options.fetch into createRun, use it as fetchFn (falling back to globalThis.fetch), and update run to call fetchFn for normal and retry requests. Added unit tests for custom fetch behavior and a changeset entry documenting the change.
🦋 Changeset detectedLatest commit: 1c6bdad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
commit: |
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
Adds an optional
fetchparameter toworkers-ai-providerwhen using credentials mode (accountId + apiKey). This matches the pattern used by@ai-sdk/openaiand@ai-sdk/anthropic, enabling request interception for testing, middleware, and advanced use cases like durable response buffering.Motivation
The Agents SDK is building a durable response buffer that survives DO eviction during long-running inference calls (RFC #1257). The buffer works by intercepting the provider's HTTP call and routing it through a proxy that persists the streaming response. For OpenAI and Anthropic, this is done via their
fetchoption. Workers AI lacked this option, requiring a workaround (fake AI binding whoserun()routes to the buffer).With this change, Workers AI in credentials mode supports the same clean pattern:
Changes
src/index.tsfetch?: typeof globalThis.fetchto the credentials branch ofWorkersAISettings(not the binding branch — bindings don't use HTTP)fetchthrough tocreateRun()fetchcan only be used with credentials, not with bindingssrc/utils.tsfetch?toCreateRunConfigcreateRunresolvesfetchFn = config.fetch ?? globalThis.fetchand uses it for both the main request and the streaming retry fallbackcreateRunBinaryis unchanged (used for transcription, separate concern)test/utils.test.tscreateRunretries withstream: false— both calls go through the custom fetch)Type safety
The
fetchoption is in the credentials branch of the discriminated union, so TypeScript rejects{ binding: env.AI, fetch: myFetch }at the type level. This prevents confusion — bindings bypass HTTP entirely, so a custom fetch would have no effect.Test plan
npx vitest run test/utils.test.ts)npx tsc --noEmit)globalThis.fetchis NOT calledMade with Cursor