Files
fewo-stommer/app/Communication/Extraction/AiAccess.php
T
LarsandClaude Opus 5.5 6862dec201
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m11s
v0.11 U6: Bring your own key für die KI-Auswertung (Anthropic, OpenRouter)
- Jeder Mandant verbindet unter „System › KI-Anbindung“ seinen eigenen
  Zugang: Anbieter (Anthropic oder OpenRouter), API-Schlüssel, Modell für
  die erste Auswertung und für die Prüfung – keine Modellkennung im Code,
  kein zentraler Betreiberschlüssel (FEWO_AI_API_KEY entfällt).
- Gespeichert im bestehenden TenantService (ai_guest_mail): Schlüssel
  verschlüsselt, nie wieder angezeigt, nie in Meldungen; Anbieter/Modelle
  in settings, Prüfergebnis in check_status/check_message/checked_at.
- Feste Endpunkte je Anbieter (AiProvider), nicht konfigurierbar.
  OpenRouter mit response_format json_schema (strict) und
  provider.require_parameters.
- „Verbindung prüfen“ testet beide Modelle. Ein abgelehnter Zugang
  (401/402/403) schaltet die Auswertung für den Mandanten ab, meldet es im
  Postfach der Verwaltung; Nachrichten bleiben zur Prüfung im Posteingang.
- Zweistufige Extraktion und ihre Sicherheitsregeln unverändert.
- Zielarchitektur §3.8, Umsetzungsplan U6, Bericht, Drehbuch, Status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 14:19:13 +01:00

79 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Communication\Extraction;
use App\Enums\TenantServiceKey;
use App\Models\Tenant;
use App\Models\TenantService;
use App\Tenancy\TenantFeatures;
/**
* A landlord's own AI access (bring your own key): provider, key and the two models of the extraction. There is no
* central operator key and no built-in model – without a complete access, or after the provider rejected the key,
* the extraction is off and messages go to the landlord.
*/
final class AiAccess
{
public const OK = 'ok';
public const INVALID = 'invalid';
public function __construct(
public readonly AiProvider $provider,
#[\SensitiveParameter] public readonly string $key,
public readonly string $smallModel,
public readonly string $reviewModel,
) {}
/**
* The usable access of a landlord, or null (service off, incomplete, or rejected by the provider).
*/
public static function for(?Tenant $tenant): ?self
{
if ($tenant === null || ! app(TenantFeatures::class)->enabled(TenantServiceKey::AiGuestMail, $tenant)) {
return null;
}
$service = self::service($tenant);
if ($service === null || $service->check_status === self::INVALID) {
return null;
}
return self::fromService($service);
}
/**
* The access as configured, regardless of the last check (for the check itself).
*/
public static function fromService(TenantService $service): ?self
{
$settings = (array) $service->settings;
$provider = AiProvider::tryFrom((string) ($settings['provider'] ?? ''));
$small = trim((string) ($settings['small_model'] ?? ''));
$review = trim((string) ($settings['review_model'] ?? ''));
if ($provider === null || blank($service->api_key) || $small === '' || $review === '') {
return null;
}
return new self($provider, (string) $service->api_key, $small, $review);
}
public static function service(Tenant $tenant): ?TenantService
{
return TenantService::query()->where('tenant_id', $tenant->id)->where('service', TenantServiceKey::AiGuestMail->value)->first();
}
/**
* The provider rejected the key: the extraction stays off until the landlord saves or checks the access again.
*/
public static function markInvalid(Tenant $tenant, string $message): void
{
self::service($tenant)?->forceFill(['check_status' => self::INVALID, 'check_message' => mb_substr($message, 0, 500), 'checked_at' => now()])->save();
}
public function __debugInfo(): array
{
return ['provider' => $this->provider->value, 'key' => '***', 'smallModel' => $this->smallModel, 'reviewModel' => $this->reviewModel];
}
}