Decision-Update Abschnitt 12 (E15, S24): Sichtung auf offensichtliche Probleme, keine Rechteprüfung, öffentlich weiterhin „automatisierte Prüfung". - ImagePrecheckManager mit openai- und deterministic-Treiber (Muster der Text-Prüfstrecke), Bild auf 1024 px, Metadaten gestrippt, JPEG als Data-URI, Antwortschema 12.4; Fehler → Ausgang `error`, durchlassen mit Vermerk. - Auswertung 12.3: reject (Stock-Wasserzeichen, Nacktheit/Gewalt, Dokument ab 0,85), queue (Screenshot, fremdes Logo, Kennzeichen ab 0,6 sowie Ablehnungskategorien 0,6–0,85), hint (Personen, „wirkt KI" nur bei „ohne KI", technischer Mangel). Stufe 1 wertet nur das Wasserzeichen; die KI setzt nie die Kennzeichnung (E9). Herabstufung per Config (12.8). - Uploader: Check vor jedem Schreiben, Ablehnung speichert nichts und landet als ki_audits `image_precheck` an der PM; Hash-Bindung zwischen Vorprüfung im Formular und Speichern; Spalten precheck*, Snapshot `precheck_snapshot` im Bestätigungsprotokoll, Archiv-Payload erweitert. - Queue: Bild zurückgehalten (withheld_at), Meldung läuft weiter; Admin entscheidet mit Vermerk (Freigabe/Ablehnung → ki_audits `image_precheck_review`); Ansicht „Bild-Sichtung" in der PM-Liste, Sichtungsblock auf der PM-Seite, Systemstatus-Zähler. - Formular v2: Ablehnungsgrund unter dem Dateifeld, Hinweise nach der Dateiauswahl, „wird kurz gesichtet" an der Karte; API 422 mit Grund, Warnung `image_under_review`. - Config images.precheck (IMAGE_PRECHECK aus, STAGE 1); 17 Tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
139 lines
4.8 KiB
PHP
139 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Image\Precheck;
|
|
|
|
use App\Enums\ImagePrecheckCategory;
|
|
|
|
/**
|
|
* Rohbefund des Vorab-Checks (DU 12.4): je Kategorie `hit`, `confidence`
|
|
* und bei technischen Mängeln eine `note`. `status` ist `ok` oder `error`;
|
|
* bei `error` sind die Kategorien leer und `errorMessage` beschreibt den
|
|
* Grund (Timeout, unbrauchbare Antwort, kein Schlüssel).
|
|
*/
|
|
final readonly class ImagePrecheckResult
|
|
{
|
|
/**
|
|
* @param array<string, array{hit: bool, confidence: float, note: string|null}> $categories
|
|
* @param array<string, mixed> $raw
|
|
*/
|
|
public function __construct(
|
|
public string $status,
|
|
public array $categories,
|
|
public string $provider,
|
|
public ?string $model,
|
|
public ?string $fileSha256,
|
|
public string $checkedAt,
|
|
public ?string $errorMessage = null,
|
|
public array $raw = [],
|
|
) {}
|
|
|
|
/**
|
|
* @param array<string, array{hit: bool, confidence: float, note: string|null}> $categories
|
|
* @param array<string, mixed> $raw
|
|
*/
|
|
public static function ok(array $categories, string $provider, ?string $model, ?string $fileSha256, array $raw = []): self
|
|
{
|
|
return new self('ok', self::normalize($categories), $provider, $model, $fileSha256, now()->toIso8601String(), null, $raw);
|
|
}
|
|
|
|
public static function error(string $message, string $provider, ?string $model, ?string $fileSha256): self
|
|
{
|
|
return new self('error', [], $provider, $model, $fileSha256, now()->toIso8601String(), $message);
|
|
}
|
|
|
|
public function isError(): bool
|
|
{
|
|
return $this->status !== 'ok';
|
|
}
|
|
|
|
public function confidence(ImagePrecheckCategory $category): float
|
|
{
|
|
$entry = $this->categories[$category->value] ?? null;
|
|
|
|
if ($entry === null || ! $entry['hit']) {
|
|
return 0.0;
|
|
}
|
|
|
|
return $entry['confidence'];
|
|
}
|
|
|
|
public function note(ImagePrecheckCategory $category): ?string
|
|
{
|
|
return $this->categories[$category->value]['note'] ?? null;
|
|
}
|
|
|
|
public function withFileSha256(string $sha256): self
|
|
{
|
|
return new self($this->status, $this->categories, $this->provider, $this->model, $sha256, $this->checkedAt, $this->errorMessage, $this->raw);
|
|
}
|
|
|
|
/**
|
|
* @return array{status: string, categories: array<string, array{hit: bool, confidence: float, note: string|null}>, provider: string, model: string|null, file_sha256: string|null, checked_at: string, error: string|null}
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'status' => $this->status,
|
|
'categories' => $this->categories,
|
|
'provider' => $this->provider,
|
|
'model' => $this->model,
|
|
'file_sha256' => $this->fileSha256,
|
|
'checked_at' => $this->checkedAt,
|
|
'error' => $this->errorMessage,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Gegenstück zu toArray() für den Komponentenzustand (Hash-Bindung
|
|
* zwischen Vorprüfung und Speichern).
|
|
*
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public static function fromArray(array $data): ?self
|
|
{
|
|
if (! isset($data['status'], $data['provider'], $data['checked_at']) || ! is_string($data['status'])) {
|
|
return null;
|
|
}
|
|
|
|
return new self(
|
|
$data['status'],
|
|
self::normalize(is_array($data['categories'] ?? null) ? $data['categories'] : []),
|
|
(string) $data['provider'],
|
|
isset($data['model']) ? (string) $data['model'] : null,
|
|
isset($data['file_sha256']) ? (string) $data['file_sha256'] : null,
|
|
(string) $data['checked_at'],
|
|
isset($data['error']) ? (string) $data['error'] : null,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Nur bekannte Kategorien, Konfidenz auf 0…1 begrenzt, Notiz gekürzt.
|
|
*
|
|
* @param array<string, mixed> $categories
|
|
* @return array<string, array{hit: bool, confidence: float, note: string|null}>
|
|
*/
|
|
private static function normalize(array $categories): array
|
|
{
|
|
$normalized = [];
|
|
|
|
foreach (ImagePrecheckCategory::cases() as $category) {
|
|
$entry = $categories[$category->value] ?? null;
|
|
|
|
if (! is_array($entry)) {
|
|
continue;
|
|
}
|
|
|
|
$confidence = isset($entry['confidence']) && is_numeric($entry['confidence']) ? (float) $entry['confidence'] : 0.0;
|
|
$hit = (bool) ($entry['hit'] ?? false);
|
|
$note = isset($entry['note']) && is_string($entry['note']) && trim($entry['note']) !== '' ? mb_substr(trim($entry['note']), 0, 200) : null;
|
|
|
|
$normalized[$category->value] = [
|
|
'hit' => $hit,
|
|
'confidence' => max(0.0, min(1.0, round($confidence, 3))),
|
|
'note' => $note,
|
|
];
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
}
|