feat(images): route Cloudflare Workers AI via AI Gateway by default
Direct probe on 2026-04-21 confirmed the Cloudflare AI Gateway route to Workers AI (`gateway.ai.cloudflare.com/.../workers-ai/@cf/...`) is no longer 403ing — the upstream regression noted in v0.8.0's ARCHITECTURE.md #16 and in thread image-pipeline seq 6 has been resolved. Routing via the gateway gives us the same benefits we already get for OpenRouter LLM calls: response caching, cost/latency dashboard, rate limiting. Changes: - `Cloudflare_Image_Support::build_endpoint_url()` now prefers the gateway URL derived from the existing `prautoblogger_ai_gateway_base_url` option. Direct API remains the fallback. - New private helper `build_gateway_workers_ai_url()` strips the OpenRouter path suffix from the configured gateway URL and appends `/workers-ai/{model}`. Returns empty string (fallback to direct) when: no gateway URL configured, the new toggle is off, or the URL isn't a valid `https://gateway.ai.cloudflare.com/...` shape. - New admin toggle `prautoblogger_cf_image_via_gateway` (default on) in the Images section. Operators can disable to force direct API without clearing the LLM-path gateway URL. - ARCHITECTURE.md #16 rewritten to reflect the v0.8.2+ gateway routing. - CHANGELOG.md v0.8.2 entry under Changed. Tests (new `CloudflareImageSupportTest`): - Direct URL when gateway not configured. - Gateway URL derived correctly when configured + toggle on. - Toggle off forces direct URL even with gateway configured. - Malformed gateway URL falls back to direct (defensive). - `is_nsfw_error()` only matches error code 3030 (pinned separately so a tolerance regression fails early). Cost impact: near-zero. Gateway adds a few ms of latency and enables response caching — could *reduce* cost on identical prompts within the configurable cache TTL. Uninstall: new option covered by existing prautoblogger_% LIKE sweep. Implements Commit 1b (narrow AI-Gateway-migration scope) per thread image-pipeline seq 7. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -533,8 +533,8 @@ Reddit rejected our OAuth API application (April 2026). We initially switched to
|
||||
|
||||
The admin model picker (v1) needs to list OpenRouter models with pricing and capabilities. We fetch `https://openrouter.ai/api/v1/models` (free, unauthenticated) once daily and store the normalized payload in a WP option fronted by a 24h transient. On stale-and-fetch-fails, we serve last-good + surface a warning. The registry class (`class-open-router-model-registry.php`) takes all config via constructor (option name, transient name, endpoint URL) — no PRAUTOBLOGGER_* constants inside the class body. Phase 2 lifts it into a shared Composer package with zero internal edits. Phase 3 adds a parallel `Cloudflare_WorkersAI_Model_Registry` behind the same interface. Capability vocabulary: `text→text`, `text+image→text`, `text+audio→text`, `text→image`, `text→audio`, `text→video`, `text→embedding`. Cost: $0/month.
|
||||
|
||||
### #16: Image generation via Cloudflare Workers AI (FLUX.1), direct (not via AI Gateway)
|
||||
The image-generation layer (started 2026-04-15, tracked in `convo/prautoblogger/threads/2026-04-image-pipeline/`) uses FLUX.1 [schnell] on Cloudflare Workers AI as its default model, chosen for its ~$0.0011/MP cost and 2–3 sec latency. FLUX.1 [dev] is a ~4× cost upgrade exposed as a dropdown for specific posts that warrant it. Calls go directly to `https://api.cloudflare.com/client/v4/accounts/{id}/ai/run/@cf/black-forest-labs/...`, *not* through the Cloudflare AI Gateway that we use for OpenRouter — the gateway route to Workers AI currently 403s (pre-existing open issue). The provider lives behind `PRAutoBlogger_Image_Provider_Interface`, so the decision is trivially reversible: switching to a different image API (DALL-E, Replicate, a future AI Gateway route) is a new class implementation plus a one-line swap in the pipeline wiring. Trade-off: we run two different Cloudflare integration paths (gateway for OpenRouter LLMs, direct for Workers AI images) until the gateway route stabilizes — minor cognitive overhead, zero functional downside.
|
||||
### #16: Image generation via Cloudflare Workers AI (FLUX.1), via AI Gateway (v0.8.2+)
|
||||
The image-generation layer uses FLUX.1 [schnell] on Cloudflare Workers AI as its default model, chosen for its ~$0.0011/MP cost and 2–3 sec latency. FLUX.1 [dev] is a ~4× cost upgrade exposed as a dropdown for specific posts that warrant it. **v0.8.2+**: calls route through the Cloudflare AI Gateway (same gateway we already use for OpenRouter — `gateway.ai.cloudflare.com/v1/{account}/{gateway}/workers-ai/@cf/black-forest-labs/...`) for response caching, unified cost/latency telemetry, and rate limiting. The 2026-04-15 regression that caused the gateway route to 403 on Workers AI has been resolved upstream; verified by direct probe on 2026-04-21. A `prautoblogger_cf_image_via_gateway` toggle (default on) falls back to the direct `api.cloudflare.com/client/v4/accounts/{id}/ai/run/...` URL if the gateway regresses again. The provider lives behind `PRAutoBlogger_Image_Provider_Interface`, so switching to a different image API (DALL-E, Replicate) is a new class implementation plus a one-line swap in the pipeline wiring.
|
||||
|
||||
### #15: Optional Cloudflare AI Gateway in front of OpenRouter
|
||||
We already use Cloudflare for DNS/CDN on peptiderepo.com, so layering AI Gateway in front of OpenRouter is zero marginal infrastructure. It gives us response caching (meaningful for repeated classification/scoring calls), a unified cost/latency dashboard, rate limiting, and provider fallback — all of which we would otherwise have to build ourselves to satisfy the CTO cost-tracking rules. Kept as an opt-in URL setting (`prautoblogger_ai_gateway_base_url`) so the plugin still works unchanged out of the box and can be bypassed instantly if the gateway misbehaves. The gateway is a transparent OpenRouter-compatible proxy; no new provider class is needed, and the response parsing path (`usage`, `choices[0].message.content`) is unchanged.
|
||||
|
||||
Reference in New Issue
Block a user