Files
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

144 lines
6.7 KiB
PHP

<?php
namespace App\Communication\Extraction;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Throwable;
/**
* The model connection of the extraction with the landlord's own access (bring your own key, v0.11): Anthropic's
* Messages API or OpenRouter's chat completions, both over HTTP to the provider's fixed endpoint (AiProvider), no SDK
* dependency. One call: system prompt, the message, and a JSON schema the answer must follow (structured outputs).
* The key travels only in the request header; it is never part of an exception, a log line or a stored result.
*/
class LlmClient
{
/** Server-side refusal fallback on Anthropic (opt-in default for the Claude Opus family). */
private const ANTHROPIC_FALLBACK_BETA = 'server-side-fallback-2026-07-01';
/**
* @param array<string, mixed> $schema
* @return array{data: array<string, mixed>, model: string, usage: array<string, mixed>}
*
* @throws AccessRejected when the provider rejects the access
* @throws ExtractionFailed for every other unusable outcome
*/
public function structured(AiAccess $access, string $model, string $system, string $message, array $schema, string $effort = 'medium'): array
{
[$headers, $body] = match ($access->provider) {
AiProvider::Anthropic => $this->anthropic($access, $model, $system, $message, $schema, $effort),
AiProvider::OpenRouter => $this->openRouter($access, $model, $system, $message, $schema),
};
try {
$response = Http::withHeaders($headers)
->timeout((int) config('fewo.ai.timeout', 40))
// Only passing problems are repeated; a rejected key or a bad request is not.
->retry(2, 2000, fn (Throwable $exception): bool => $exception instanceof ConnectionException || ($exception instanceof RequestException && in_array($exception->response->status(), [429, 500, 502, 503, 529], true)), throw: false)
->post($access->provider->endpoint(), $body);
} catch (ConnectionException) {
throw new ExtractionFailed('Keine Verbindung zu '.$access->provider->getLabel().'.');
}
if (in_array($response->status(), [401, 402, 403], true)) {
throw new AccessRejected($access->provider->getLabel().' lehnt den Zugang ab ('.$response->status().': '.$this->providerMessage($response, $access).').');
}
if (! $response->successful()) {
throw new ExtractionFailed($access->provider->getLabel().' antwortet mit '.$response->status().' ('.$this->providerMessage($response, $access).').');
}
$payload = (array) $response->json();
$text = $access->provider === AiProvider::Anthropic ? $this->anthropicText($payload) : $this->openRouterText($payload);
$data = json_decode($text, true);
if (! is_array($data)) {
throw new ExtractionFailed('Die Antwort ist kein gültiges JSON.');
}
return ['data' => $data, 'model' => (string) ($payload['model'] ?? $model), 'usage' => (array) ($payload['usage'] ?? [])];
}
/**
* @param array<string, mixed> $schema
* @return array{0: array<string, string>, 1: array<string, mixed>}
*/
private function anthropic(AiAccess $access, string $model, string $system, string $message, array $schema, string $effort): array
{
$headers = ['x-api-key' => $access->key, 'anthropic-version' => (string) config('fewo.ai.anthropic_version'), 'content-type' => 'application/json'];
$body = [
'model' => $model,
'max_tokens' => 4096,
'system' => $system,
'messages' => [['role' => 'user', 'content' => $message]],
'output_config' => ['effort' => $effort, 'format' => ['type' => 'json_schema', 'schema' => $schema]],
];
if (str_starts_with($model, 'claude-opus-')) {
$body['fallbacks'] = 'default';
$headers['anthropic-beta'] = self::ANTHROPIC_FALLBACK_BETA;
}
return [$headers, $body];
}
/**
* @param array<string, mixed> $schema
* @return array{0: array<string, string>, 1: array<string, mixed>}
*/
private function openRouter(AiAccess $access, string $model, string $system, string $message, array $schema): array
{
return [
['Authorization' => 'Bearer '.$access->key, 'content-type' => 'application/json', 'HTTP-Referer' => (string) config('app.url'), 'X-OpenRouter-Title' => 'FewoPortal'],
[
'model' => $model,
'max_tokens' => 4096,
'messages' => [['role' => 'system', 'content' => $system], ['role' => 'user', 'content' => $message]],
'response_format' => ['type' => 'json_schema', 'json_schema' => ['name' => 'extraction', 'strict' => true, 'schema' => $schema]],
// Only providers that honour the schema; otherwise it would merely be a hint.
'provider' => ['require_parameters' => true],
],
];
}
/**
* @param array<string, mixed> $payload
*/
private function anthropicText(array $payload): string
{
match ($payload['stop_reason'] ?? null) {
'refusal' => throw new ExtractionFailed('Der Anbieter hat die Auswertung abgelehnt.'),
'max_tokens' => throw new ExtractionFailed('Die Antwort ist unvollständig.'),
default => null,
};
return collect((array) ($payload['content'] ?? []))->where('type', 'text')->pluck('text')->implode('');
}
/**
* @param array<string, mixed> $payload
*/
private function openRouterText(array $payload): string
{
$choice = (array) ($payload['choices'][0] ?? []);
match ($choice['finish_reason'] ?? null) {
'content_filter' => throw new ExtractionFailed('Der Anbieter hat die Auswertung abgelehnt.'),
'length' => throw new ExtractionFailed('Die Antwort ist unvollständig.'),
'error' => throw new ExtractionFailed('Der Anbieter meldet einen Fehler.'),
default => null,
};
return (string) ($choice['message']['content'] ?? '');
}
/**
* The provider's own short message, cut and without anything that looks like a key.
*/
private function providerMessage(Response $response, AiAccess $access): string
{
$message = str_replace($access->key, '***', (string) ($response->json('error.message') ?? $response->json('error.type') ?? ''));
return mb_substr((string) preg_replace('/(sk-[A-Za-z0-9_\-]{6})[A-Za-z0-9_\-]+/', '$1…', $message), 0, 200) ?: 'ohne Angabe';
}
}