fix(provider): scope AI Gateway token to first-party Workers AI models - #33597
fix(provider): scope AI Gateway token to first-party Workers AI models#33597keefetang wants to merge 3 commits into
Conversation
anomalyco#32052 fixed anomalyco#32051 (Workers AI 401s) by passing apiKey to createUnified, but applied it to every model — so the Cloudflare API token was sent as the upstream Authorization header for third-party providers (OpenAI, Anthropic), causing them to 401 with "Invalid API Key". Scope token forwarding to be model-aware: attach the Cloudflare token only for first-party Workers AI models, whose upstream is Cloudflare itself. The Unified API addresses Workers AI both as "workers-ai/..." and as bare "@cf/..." ids, so match both; "@cf/" is Cloudflare's reserved namespace, so this never matches a third-party model. Other providers receive no upstream Authorization and fall back to the gateway's stored/BYOK keys. Applied in both the v1 provider (provider.ts) and v2 plugin (core/.../cloudflare-ai-gateway.ts) paths. Tests assert both directions, including that third-party sub-requests carry no upstream authorization header. Reapplies and extends the approach from anomalyco#33407.
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
|
this fell under the radar will go over this today |
|
closing in favor of larger fix pr that is linked, tagged u as coauthor I think the provider needs a lot more work like moving off the legacy endpoint We can better address it in v2 |
Issue for this PR
Closes #32051
Closes #40344
Reapplies and extends #33407 (auto-closed on a PR-template technicality).
Fixes the third-party regression introduced by #32052.
Type of change
What does this PR do?
#32052 fixed #32051 (Workers AI models 401'd through the gateway) by passing
apiKeytocreateUnified. That apiKey was applied to every model, so theCloudflare API token was sent as the upstream
Authorizationheader forthird-party providers (OpenAI, Anthropic) too — causing them to 401 with
"Invalid API Key".
This scopes token forwarding to be model-aware. The Cloudflare token is only
attached for first-party Workers AI models, whose upstream is Cloudflare
itself; everything else relies on the gateway's stored/BYOK credentials (or
Unified Billing):
Both Workers AI id forms the Unified API accepts are matched: the explicit
workers-ai/...prefix and the bare@cf/...form.@cf/is Cloudflare'sreserved namespace, so this never matches a third-party model.
Applied in both the v1 provider path (
provider.ts) and the v2 plugin path(
core/.../cloudflare-ai-gateway.ts). The gateway-level auth(
createAiGateway({ apiKey }), thecf-aig-authorizationheader) is unchanged.How did you verify your code works?
Added regression tests in
packages/opencode/test/provider/cf-ai-gateway-e2e.test.tsthat capture the real forwarded headers and assert both directions:
upstream sub-request carries no
authorizationheader (fix(provider): pass apiKey to createUnified for Cloudflare AI Gateway #32052 regression)workers-ai/...and bare@cf/...models do forward it (cloudflare-ai-gateway provider returns 401 for Workers AI models #32051)Screenshots / recordings
N/A — no UI changes.
Checklist