Repository navigation
Call Workers AI over HTTP instead of the ai binding - #73
Conversation
Cloudflare fails to attach the ai binding to new versions of this worker (code 10021), which blocks every deploy. Badge moderation now uses the Workers AI REST API with an AI_API_TOKEN secret, keeping the binding as the first choice if it is ever restored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017dqWu3YgeVRGwBt5pXDq4A
Reviewer's GuideThe worker no longer declares a Workers AI binding, avoiding the deployment failure, and instead uses an authenticated Workers AI REST client that matches the binding’s run interface. Existing bindings remain preferred for compatibility, while missing AI configuration fails closed during badge moderation; tests cover the HTTP contract, error handling, fallback selection, and refusal behavior. Sequence diagram for badge moderation AI selectionsequenceDiagram
participant Process
participant Binding as WorkersAI_binding
participant HTTP as WorkersAIOverHTTP
participant API as WorkersAI_REST_API
Process->>Process: Select Environment.AI or AI_API_TOKEN client
alt binding available
Process->>Binding: run(Model, Inputs)
Binding-->>Process: moderation result
else API token available
Process->>HTTP: run(Model, Inputs)
HTTP->>API: POST /accounts/{ACCOUNT_ID}/ai/run/{model}
API-->>HTTP: success and result
HTTP-->>Process: result
else neither available
Process->>Process: run throws
Process-->>Process: Refuse badge edit
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. This changes the production worker's AI integration and introduces a bearer-token HTTP dependency, so a configuration or endpoint mistake could disable moderation or send moderation inputs through an unintended API path. Reverting restores the binding-based behavior, but requests already sent and any associated credential or data exposure would not be undone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017dqWu3YgeVRGwBt5pXDq4A
Why
Every deploy since #72 fails: Cloudflare can't attach the
aibinding to a new version of this worker (binding AI of type ai failed to generate … [code: 10021]). It fails with wrangler 4.114 and 4.147, in Workers Builds and locally, and the dashboard's AI → Models page errors out too. Other accounts are hitting the same thing: cloudflare/workers-sdk#15973. The version already running can still call Workers AI; only creating new versions is broken.What
[ai]block fromwrangler.toml, so deploys go through again (including fix: stop logging raw PHPSESSID values #72's log fix)./accounts/{ACCOUNT_ID}/ai/run/{model}) using the existingAPI_TOKENsecret (the Analytics Engine token), or a separateAI_API_TOKENif one is set. The client has the samerun()shape as the binding and returns the binding's output (the API'sresult), so the moderation code and its parsing don't change.runthrows inside the existing try block, and the edit is refused with 「内容审核服务暂时不可用」. Unmoderated badges never get through.Before merging (merge auto-deploys)
API_TOKENand add Account → Workers AI → Read. Editing permissions keeps the token value, so the secret doesn't change.aibinding, and badge moderation keeps working without any gap.Tests
npm test: 144 pass. The 9 new tests cover the request URL, auth and body, HTTP errors,success: falsewith a 200, a non-JSON body, binding-first selection,AI_API_TOKENoverAPI_TOKEN, theAPI_TOKENfallback, and refusing the edit when there is no AI at all.wrangler deploy --dry-runbuilds with noAIbinding.🤖 Generated with Claude Code
https://claude.ai/code/session_017dqWu3YgeVRGwBt5pXDq4A
Summary by Sourcery
Replace the unavailable Workers AI binding with a resilient HTTP-based integration so deployments succeed and badge moderation remains enforced.
New Features:
Bug Fixes:
Enhancements:
Build:
Tests: