Files
prautoblogger/includes/providers/class-image-nsfw-blocked.php
peptiderepo fb1451c4dc feat(images): retry Cloudflare NSFW-blocked prompts with a sanitized fallback
Cloudflare's Workers AI FLUX.1 schnell content filter sometimes rejects
peptide-comic prompts as NSFW (HTTP 400, errors[].code === 3030). On
2026-04-20 this produced a silent publish-without-featured-image for
post #650 ("Semaglutide vs Tirzepatide") — the LLM-rewritten scene
tripped the filter and we had no recovery path.

New behaviour: on a 3030 response, the Cloudflare provider throws a
typed PRAutoBlogger_Image_NSFW_Blocked exception. The batch wrapper
catches it and tags the per-slot result with
`error_type: nsfw_blocked`. Image_Pipeline hands the results to a new
PRAutoBlogger_Image_NSFW_Retry helper, which reruns the blocked slot
once with a sanitized fallback prompt (title + style suffix, no LLM
rewrite). On success the retry result replaces the error entry; on a
second block, a WARNING is logged and the pipeline publishes without
that image — same terminal behaviour we had before, just explicit.

Files:

- New providers/class-image-nsfw-blocked.php — typed exception.
- providers/class-cloudflare-image-support.php — is_nsfw_error() +
  throw_if_nsfw() helpers.
- providers/class-cloudflare-image-provider.php — calls throw_if_nsfw
  inside the 4xx branch; batch catches the typed exception and marks
  the result with error_type.
- core/class-image-prompt-builder.php — new public build_fallback_prompt
  shim so the retry layer can reach the rule-based fallback without
  going through the LLM path.
- New core/class-image-nsfw-retry.php — single-shot retry helper.
- core/class-image-pipeline.php — invokes the retry between batch
  completion and per-slot processing.
- admin/class-settings-fields-extended.php — new toggle
  `prautoblogger_image_nsfw_retry` (default on) in the Images section.

Tests:

- CloudflareImageProviderTest — 3030 response throws NSFW exception;
  batch tags the per-slot result with `error_type: nsfw_blocked`.
- ImagePipelineTest — retry succeeds when provider returns a good
  result on the second call; retry is skipped when the setting is off.

Cost impact: worst-case an extra Cloudflare call per NSFW-blocked slot
(~$0.000834). Current incidence ~2% of generations; ~+$0.00002 per
generation amortized. Immaterial.

Uninstall: new option covered by the existing prautoblogger_% LIKE
sweep in uninstall.php.

Part of the v0.8.0 image bundle (issues from thread image-mime-bug).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 01:06:59 +07:00

46 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* Exception raised when an image provider rejects a prompt as NSFW.
*
* Cloudflare Workers AI returns HTTP 400 with an `errors[].code === 3030`
* body when its content filter blocks a prompt. We catch that shape in
* the provider and raise this typed exception so the image pipeline can
* distinguish a content-filter rejection from a generic 4xx and retry
* once with a sanitized fallback prompt.
*
* Triggered by: PRAutoBlogger_Cloudflare_Image_Provider on NSFW 4xx.
* Dependencies: None.
*
* @see providers/class-cloudflare-image-provider.php — Raises this.
* @see core/class-image-nsfw-retry.php — Catches + rebuilds prompt.
* @see https://developers.cloudflare.com/workers-ai/models/flux-1-schnell/ — Upstream error codes.
*/
class PRAutoBlogger_Image_NSFW_Blocked extends \RuntimeException {
/**
* The raw upstream response body, kept verbatim for log forensics.
*
* @var string
*/
private string $upstream_body;
/**
* @param string $message Human-readable summary.
* @param string $upstream_body Raw response body from the provider.
* @param \Throwable|null $previous Optional previous exception.
*/
public function __construct( string $message, string $upstream_body = '', ?\Throwable $previous = null ) {
parent::__construct( $message, 0, $previous );
$this->upstream_body = $upstream_body;
}
/**
* @return string Raw upstream body (may be empty).
*/
public function get_upstream_body(): string {
return $this->upstream_body;
}
}