v0.11 U6: risikobasierte KI-Extraktion von Gast- und Portalnachrichten
- Kleines Modell (claude-haiku-5-5) extrahiert Fakten mit Belegstelle und Konfidenz; programmatische Prüfung (Gültigkeit, Belegstelle im Original, Widerspruch zum gespeicherten Wert oder in der Nachricht, Plausibilität, Kritikalität nach Feld und Phase). Widerspruch oder fehlgeschlagene Prüfung → Prüfmodell (claude-opus-5-5, serverseitiger Fallback bei Ablehnung) unabhängig von der Konfidenz; solche Fakten nie automatisch. - Nur geprüfte, bisher fehlende Angaben gehen an den Fakteneingang (Quelle „llm“); Zeitraum/Personen nur als Änderungswunsch (M06); Rest als Vorschlag im Posteingang („Vorschläge übernehmen“). - Versionierte Prompts mit Platzhaltern, JSON-Schema mit eigener Validierung und einer Wiederholung; Ausfall/Ablehnung → sicherer Rückfall zur Vermieterin. HTTP-Client, kein SDK (laut Plan). - Dienst „KI: Gästekommunikation“ verdrahtet, standardmäßig aus; Schlüssel je Mandant oder FEWO_AI_API_KEY; Verbrauch in service_usages. - messages:extract alle fünf Minuten; ai:evaluate misst Fehlwerte und übersehene Angaben getrennt (M05). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dff9e2344e
commit
4170e3bd29
18 files changed
+1130
-7
No files matched your search
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Communication\Extraction\ExtractionFailed;
|
||||
use App\Communication\Extraction\MessageExtractor;
|
||||
use App\Communication\Extraction\RiskAssessment;
|
||||
use App\Enums\BookingTaskKind;
|
||||
use App\Enums\BookingTaskStatus;
|
||||
use App\Enums\FactSource;
|
||||
use App\Enums\TenantServiceKey;
|
||||
use App\Models\Booking;
|
||||
use App\Models\BookingTask;
|
||||
use App\Models\InboundMessage;
|
||||
use App\Models\ServiceUsage;
|
||||
use App\Models\TenantService;
|
||||
use App\Process\CaseLog;
|
||||
use App\Support\Settings;
|
||||
use App\Tenancy\TenantFeatures;
|
||||
|
||||
/**
|
||||
* The risk-based two-stage extraction of v0.11 (CH-14, E-035): the small model extracts facts with evidence and
|
||||
* confidence, programmatic rules check phase, stored values, contradictions and plausibility, and – when the risk is
|
||||
* high, something contradicts or a check fails – the review model looks at the original and the first extraction.
|
||||
* Only facts that pass every check go to the common fact intake (which applies its own rules); everything else is
|
||||
* proposed to the landlord in the inbox. Wishes to change the stay only become change requests. The extraction
|
||||
* decides nothing binding; without the service, a key or a usable answer the message simply waits for the landlord.
|
||||
*/
|
||||
class ExtractMessageFacts
|
||||
{
|
||||
public function __construct(private MessageExtractor $extractor, private RiskAssessment $risk, private ContributeCaseFacts $facts) {}
|
||||
|
||||
/**
|
||||
* @return string the extraction status: done, failed or disabled
|
||||
*/
|
||||
public function handle(InboundMessage $message): string
|
||||
{
|
||||
$booking = $message->booking()->with(['guest', 'property'])->first();
|
||||
if ($booking === null || $message->extraction_status !== null) {
|
||||
return (string) $message->extraction_status;
|
||||
}
|
||||
|
||||
$apiKey = $this->apiKey();
|
||||
if (! app(TenantFeatures::class)->enabled(TenantServiceKey::AiGuestMail) || $apiKey === null) {
|
||||
$message->update(['extraction_status' => 'disabled']);
|
||||
|
||||
return 'disabled';
|
||||
}
|
||||
|
||||
$record = ['prompt_version' => MessageExtractor::promptVersion()];
|
||||
try {
|
||||
$first = $this->extractor->extract($apiKey, $message, $booking);
|
||||
$this->meter();
|
||||
$record['first'] = $first;
|
||||
$data = $first['data'];
|
||||
$checked = $this->risk->facts($booking, (string) $message->body, $data);
|
||||
|
||||
if ($this->risk->needsReview($booking, $checked, $data)) {
|
||||
$review = $this->extractor->review($apiKey, $message, $booking, $data);
|
||||
$this->meter();
|
||||
$record['review'] = $review;
|
||||
$data = $review['data'];
|
||||
$checked = $this->markDisagreements($this->risk->facts($booking, (string) $message->body, $data), $first['data']);
|
||||
}
|
||||
} catch (ExtractionFailed $exception) {
|
||||
$message->update(['extraction_status' => 'failed', 'extraction' => [...$record, 'error' => $exception->getMessage()]]);
|
||||
CaseLog::record($booking, 'mail_in', 'Automatische Auswertung nicht möglich – bitte die Nachricht selbst lesen', actor: CaseLog::SYSTEM);
|
||||
|
||||
return 'failed';
|
||||
}
|
||||
|
||||
$automatic = array_values(array_filter($checked, fn (array $fact): bool => $this->risk->mayTakeOver($booking, $fact)));
|
||||
$proposed = array_values(array_filter($checked, fn (array $fact): bool => ! in_array($fact, $automatic, true)));
|
||||
$source = $message->status === InboundMessage::PORTAL ? FactSource::PortalMail : FactSource::Extraction;
|
||||
|
||||
if ($automatic !== []) {
|
||||
$this->facts->handle($booking, collect($automatic)->mapWithKeys(fn (array $fact): array => [$fact['field'] => $fact['value']])->all(), $source, [
|
||||
'inbound_message_id' => $message->id,
|
||||
'reference' => 'message:'.$message->id,
|
||||
'evidence' => collect($automatic)->mapWithKeys(fn (array $fact): array => [$fact['field'] => $fact['evidence']])->all(),
|
||||
'confidence' => collect($automatic)->mapWithKeys(fn (array $fact): array => [$fact['field'] => $fact['confidence']])->all(),
|
||||
'actor' => 'Auswertung der E-Mail (KI, geprüft nach Regeln)',
|
||||
]);
|
||||
}
|
||||
|
||||
$changes = $this->changeRequests($booking, $message, $data['change_requests']);
|
||||
$message->update([
|
||||
'extraction_status' => 'done',
|
||||
'extraction' => [...$record, 'intent' => $data['intent'], 'uncertainties' => $data['uncertainties'], 'applied' => $automatic, 'proposed' => $proposed, 'change_requests' => $changes],
|
||||
]);
|
||||
|
||||
$this->finish($message, $automatic, $proposed, $changes, $data['intent']);
|
||||
|
||||
return 'done';
|
||||
}
|
||||
|
||||
/**
|
||||
* Facts the two models disagree on are proposed, never taken over.
|
||||
*
|
||||
* @param list<array<string, mixed>> $checked
|
||||
* @param array{facts: list<array{field: string, value: string}>} $first
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function markDisagreements(array $checked, array $first): array
|
||||
{
|
||||
$firstValues = collect($first['facts'])->mapWithKeys(fn (array $fact): array => [$fact['field'] => mb_strtolower(trim($fact['value']))])->all();
|
||||
|
||||
return array_map(function (array $fact) use ($firstValues): array {
|
||||
if (isset($firstValues[$fact['field']]) && $firstValues[$fact['field']] !== mb_strtolower($fact['value'])) {
|
||||
$fact['problems'][] = 'die beiden Modelle lesen den Wert unterschiedlich';
|
||||
}
|
||||
|
||||
return $fact;
|
||||
}, $checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wishes to change the stay become change requests through the fact intake (dates, guests) or a task; nothing about
|
||||
* the booking changes (E-038).
|
||||
*
|
||||
* @param list<array{field: string, value: string, evidence: string}> $requests
|
||||
* @return list<array{field: string, value: string, evidence: string}>
|
||||
*/
|
||||
private function changeRequests(Booking $booking, InboundMessage $message, array $requests): array
|
||||
{
|
||||
$kept = array_values(array_filter($requests, fn (array $request): bool => $this->risk->evidenceFound((string) $message->body, $request['evidence'])));
|
||||
$stay = array_filter($kept, fn (array $request): bool => in_array($request['field'], ContributeCaseFacts::STAY_FIELDS, true));
|
||||
if ($stay !== []) {
|
||||
$this->facts->handle($booking, collect($stay)->mapWithKeys(fn (array $request): array => [$request['field'] => $request['value']])->all(), FactSource::Extraction, [
|
||||
'inbound_message_id' => $message->id, 'reference' => 'message-change:'.$message->id, 'advance' => false,
|
||||
'evidence' => collect($stay)->mapWithKeys(fn (array $request): array => [$request['field'] => $request['evidence']])->all(),
|
||||
]);
|
||||
}
|
||||
foreach (array_filter($kept, fn (array $request): bool => ! in_array($request['field'], ContributeCaseFacts::STAY_FIELDS, true)) as $request) {
|
||||
BookingTask::openOnce($booking, BookingTaskKind::ChangeRequest, 'message:'.$message->id.':'.$request['field'], [
|
||||
'due_on' => today(),
|
||||
'note' => 'Wunsch laut E-Mail ('.$request['field'].'): „'.mb_substr($request['evidence'], 0, 200).'“. Nichts wurde geändert.',
|
||||
]);
|
||||
}
|
||||
|
||||
return $kept;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $automatic
|
||||
* @param list<array<string, mixed>> $proposed
|
||||
* @param list<array<string, mixed>> $changes
|
||||
*/
|
||||
private function finish(InboundMessage $message, array $automatic, array $proposed, array $changes, string $intent): void
|
||||
{
|
||||
$booking = $message->booking;
|
||||
$task = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::InboundReview)->where('subject', 'm'.$message->id)->where('status', BookingTaskStatus::Open)->first();
|
||||
$parts = array_filter([
|
||||
$automatic !== [] ? count($automatic).' Angabe(n) übernommen' : null,
|
||||
$proposed !== [] ? count($proposed).' Vorschlag/Vorschläge zu prüfen' : null,
|
||||
$changes !== [] ? 'Änderungswunsch erkannt' : null,
|
||||
]);
|
||||
CaseLog::record($booking, 'mail_in', 'E-Mail ausgewertet: '.($parts === [] ? 'keine Angaben erkannt' : implode(', ', $parts)), actor: CaseLog::SYSTEM);
|
||||
|
||||
// Nothing left to look at: a pure answer whose details were all taken over.
|
||||
if ($proposed === [] && $changes === [] && $automatic !== [] && $intent === 'answer') {
|
||||
$task?->forceFill(['status' => BookingTaskStatus::Done, 'completed_at' => now(), 'completed_by' => CaseLog::SYSTEM, 'note' => trim($task->note.' · automatisch ausgewertet')])->save();
|
||||
$message->update(['status' => $message->status === InboundMessage::PORTAL ? InboundMessage::PORTAL : InboundMessage::PROCESSED, 'processed_at' => now()]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$task?->forceFill(['note' => '„'.mb_substr((string) $message->subject, 0, 120).'“ – '.(implode(', ', $parts) ?: 'keine Angaben erkannt').'. Im Posteingang ansehen.'])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* The landlord's own key of the service, otherwise the operator's key of the installation.
|
||||
*/
|
||||
private function apiKey(): ?string
|
||||
{
|
||||
$tenant = Settings::tenant();
|
||||
$own = $tenant === null ? null : TenantService::query()->where('tenant_id', $tenant->id)->where('service', TenantServiceKey::AiGuestMail->value)->first()?->api_key;
|
||||
|
||||
return filled($own) ? (string) $own : (filled(config('fewo.ai.api_key')) ? (string) config('fewo.ai.api_key') : null);
|
||||
}
|
||||
|
||||
private function meter(): void
|
||||
{
|
||||
if (($tenant = Settings::tenant()) !== null) {
|
||||
ServiceUsage::query()->create(['tenant_id' => $tenant->id, 'service' => TenantServiceKey::AiGuestMail->value, 'quantity' => 1, 'used_at' => now()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,9 @@ public function handle(PortalMail $mail, ?Mailbox $mailbox = null): InboundMessa
|
||||
// A portal's notification: the existing portal path decides, unchanged (CH-08).
|
||||
$record = $this->portalMail->handle($mail);
|
||||
$record?->forceFill(['inbound_message_id' => $message->id])->save();
|
||||
$message->update(['status' => InboundMessage::PORTAL, 'portal_email_id' => $record?->id, 'processed_at' => now()]);
|
||||
// CH-09/12: if the notification belongs to a case, the guest details it carries are evaluated for that case.
|
||||
$bookingId = $record?->blockedPeriod()->value('linked_booking_id');
|
||||
$message->update(['status' => InboundMessage::PORTAL, 'portal_email_id' => $record?->id, 'booking_id' => $bookingId, 'match_reason' => $bookingId ? 'portal_reference' : null, 'processed_at' => now()]);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Communication\Extraction;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* The extraction could not produce a usable result (no key, timeout, refusal, invalid output). The message then
|
||||
* waits for the landlord – the safe fallback of CH-14.
|
||||
*/
|
||||
class ExtractionFailed extends RuntimeException {}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Communication\Extraction;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
/**
|
||||
* The model connection for the extraction (Claude Messages API over HTTP – no SDK dependency, as approved in the
|
||||
* v0.11 plan). One call: system prompt, the message as user content, and a JSON schema the answer must follow
|
||||
* (structured outputs, `output_config.format`). Provider, endpoint and models come from config('fewo.ai').
|
||||
*/
|
||||
class LlmClient
|
||||
{
|
||||
/** Server-side refusal fallback (opt-in default for Claude Opus 5.5). */
|
||||
private const 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 ExtractionFailed
|
||||
*/
|
||||
public function structured(string $apiKey, string $model, string $system, string $message, array $schema, string $effort = 'medium'): array
|
||||
{
|
||||
$body = [
|
||||
'model' => $model,
|
||||
'max_tokens' => 4096,
|
||||
'system' => $system,
|
||||
'messages' => [['role' => 'user', 'content' => $message]],
|
||||
'output_config' => ['effort' => $effort, 'format' => ['type' => 'json_schema', 'schema' => $schema]],
|
||||
];
|
||||
$headers = ['x-api-key' => $apiKey, 'anthropic-version' => (string) config('fewo.ai.api_version'), 'content-type' => 'application/json'];
|
||||
if (str_starts_with($model, 'claude-opus-')) {
|
||||
$body['fallbacks'] = 'default';
|
||||
$headers['anthropic-beta'] = self::FALLBACK_BETA;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::withHeaders($headers)->timeout((int) config('fewo.ai.timeout', 40))->retry(2, 2000, throw: false)->post((string) config('fewo.ai.endpoint'), $body);
|
||||
} catch (ConnectionException $exception) {
|
||||
throw new ExtractionFailed('Keine Verbindung zum KI-Dienst: '.$exception->getMessage(), previous: $exception);
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
throw new ExtractionFailed('KI-Dienst antwortet mit '.$response->status().'.');
|
||||
}
|
||||
|
||||
$payload = (array) $response->json();
|
||||
$stop = $payload['stop_reason'] ?? null;
|
||||
if ($stop === 'refusal') {
|
||||
throw new ExtractionFailed('Der KI-Dienst hat die Auswertung abgelehnt.');
|
||||
}
|
||||
if ($stop === 'max_tokens') {
|
||||
throw new ExtractionFailed('Die Antwort des KI-Dienstes ist unvollständig.');
|
||||
}
|
||||
|
||||
$text = collect((array) ($payload['content'] ?? []))->where('type', 'text')->pluck('text')->implode('');
|
||||
$data = json_decode($text, true);
|
||||
if (! is_array($data)) {
|
||||
throw new ExtractionFailed('Die Antwort des KI-Dienstes ist kein gültiges JSON.');
|
||||
}
|
||||
|
||||
return ['data' => $data, 'model' => (string) ($payload['model'] ?? $model), 'usage' => (array) ($payload['usage'] ?? [])];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Communication\Extraction;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Models\InboundMessage;
|
||||
use App\Process\CaseRequirements;
|
||||
|
||||
/**
|
||||
* The model side of the extraction (v0.11, CH-14): versioned prompts with defined placeholders, a JSON schema for the
|
||||
* answer and its validation here (the answer is checked, not trusted). The small model extracts; the review model
|
||||
* sees the original and the first extraction. Both only return candidates – nothing is stored or changed here.
|
||||
*/
|
||||
class MessageExtractor
|
||||
{
|
||||
public function __construct(private LlmClient $client) {}
|
||||
|
||||
/**
|
||||
* @return array{data: array{facts: list<array{field: string, value: string, evidence: string, confidence: float|int}>, change_requests: list<array{field: string, value: string, evidence: string}>, intent: string, uncertainties: list<string>}, model: string}
|
||||
*
|
||||
* @throws ExtractionFailed
|
||||
*/
|
||||
public function extract(string $apiKey, InboundMessage $message, Booking $booking): array
|
||||
{
|
||||
return $this->call($apiKey, (string) config('fewo.ai.small_model'), $this->prompt('', $booking), $this->messageText($message), 'medium');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $first
|
||||
* @return array{data: array{facts: list<array{field: string, value: string, evidence: string, confidence: float|int}>, change_requests: list<array{field: string, value: string, evidence: string}>, intent: string, uncertainties: list<string>}, model: string}
|
||||
*
|
||||
* @throws ExtractionFailed
|
||||
*/
|
||||
public function review(string $apiKey, InboundMessage $message, Booking $booking, array $first): array
|
||||
{
|
||||
return $this->call($apiKey, (string) config('fewo.ai.review_model'), $this->prompt('-review', $booking, ['first_extraction' => json_encode($first, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]), $this->messageText($message), 'high');
|
||||
}
|
||||
|
||||
public static function promptVersion(): string
|
||||
{
|
||||
return (string) config('fewo.ai.prompt_version', 'v1');
|
||||
}
|
||||
|
||||
/**
|
||||
* The prompt of the configured version with every placeholder filled; an unknown placeholder is an error.
|
||||
*
|
||||
* @param array<string, string> $extra
|
||||
*/
|
||||
public function prompt(string $variant, Booking $booking, array $extra = []): string
|
||||
{
|
||||
$path = resource_path('prompts/extraction/'.self::promptVersion().$variant.'.md');
|
||||
if (! is_file($path)) {
|
||||
throw new ExtractionFailed('Prompt '.self::promptVersion().$variant.' fehlt.');
|
||||
}
|
||||
|
||||
$values = [
|
||||
'phase' => match (true) {
|
||||
$booking->isBinding() => 'binding booking (contract exists; changes are only wishes)',
|
||||
$booking->offer_version > 0 => 'binding offer sent, not yet accepted',
|
||||
default => 'non-binding request',
|
||||
},
|
||||
'requested_fields' => implode(', ', $booking->openDataRequest()?->fields ?? CaseRequirements::missing($booking)) ?: 'none',
|
||||
'known_fields' => $this->knownFields($booking),
|
||||
'today' => now()->toDateString(),
|
||||
...$extra,
|
||||
];
|
||||
|
||||
$prompt = preg_replace_callback('/\{\{\s*(\w+)\s*\}\}/', function (array $match) use ($values): string {
|
||||
if (! array_key_exists($match[1], $values)) {
|
||||
throw new ExtractionFailed('Unbekannter Platzhalter {{'.$match[1].'}}.');
|
||||
}
|
||||
|
||||
return $values[$match[1]];
|
||||
}, (string) file_get_contents($path));
|
||||
|
||||
return (string) $prompt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function schema(): array
|
||||
{
|
||||
return (array) json_decode((string) file_get_contents(resource_path('prompts/extraction/schema.json')), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{data: array{facts: list<array{field: string, value: string, evidence: string, confidence: float|int}>, change_requests: list<array{field: string, value: string, evidence: string}>, intent: string, uncertainties: list<string>}, model: string}
|
||||
*/
|
||||
private function call(string $apiKey, string $model, string $system, string $text, string $effort): array
|
||||
{
|
||||
$schema = self::schema();
|
||||
$lastError = null;
|
||||
// One repetition when the answer does not fit the schema, then the message goes to the landlord.
|
||||
for ($attempt = 1; $attempt <= 2; $attempt++) {
|
||||
$result = $this->client->structured($apiKey, $model, $system, $text, $schema, $effort);
|
||||
$lastError = $this->violation($result['data'], $schema);
|
||||
if ($lastError === null) {
|
||||
return ['data' => $result['data'], 'model' => $result['model']];
|
||||
}
|
||||
}
|
||||
|
||||
throw new ExtractionFailed('Antwort passt nicht zum Schema: '.$lastError);
|
||||
}
|
||||
|
||||
/**
|
||||
* The first deviation of the answer from the schema, or null.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, mixed> $schema
|
||||
*/
|
||||
private function violation(array $data, array $schema): ?string
|
||||
{
|
||||
foreach ($schema['required'] as $key) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
return 'Feld '.$key.' fehlt';
|
||||
}
|
||||
}
|
||||
if (! is_array($data['facts']) || ! is_array($data['change_requests']) || ! is_array($data['uncertainties']) || ! is_string($data['intent'])) {
|
||||
return 'falsche Typen';
|
||||
}
|
||||
if (! in_array($data['intent'], $schema['properties']['intent']['enum'], true)) {
|
||||
return 'unbekannte Absicht';
|
||||
}
|
||||
foreach (['facts', 'change_requests'] as $list) {
|
||||
$item = $schema['properties'][$list]['items'];
|
||||
foreach ($data[$list] as $entry) {
|
||||
if (! is_array($entry) || array_diff($item['required'], array_keys($entry)) !== [] || ! in_array($entry['field'], $item['properties']['field']['enum'], true)) {
|
||||
return 'ungültiger Eintrag in '.$list;
|
||||
}
|
||||
if (! is_string($entry['value']) || ! is_string($entry['evidence']) || ($list === 'facts' && ! is_numeric($entry['confidence']))) {
|
||||
return 'falscher Typ in '.$list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function messageText(InboundMessage $message): string
|
||||
{
|
||||
return 'Subject: '.$message->subject."\n\n".mb_substr((string) $message->body, 0, 20000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Values on file, masked: enough to see whether something differs, not to learn the data.
|
||||
*/
|
||||
private function knownFields(Booking $booking): string
|
||||
{
|
||||
$known = [];
|
||||
foreach ([...array_keys(CaseRequirements::FIELDS), ...array_keys(CaseRequirements::OPTIONAL_FIELDS)] as $field) {
|
||||
$value = (string) CaseRequirements::value($booking, $field);
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
$known[] = $field.'='.(str_starts_with($field, 'booking.') ? $value : mb_substr($value, 0, 1).str_repeat('*', max(1, mb_strlen($value) - 2)).mb_substr($value, -1));
|
||||
}
|
||||
|
||||
return $known === [] ? 'none' : implode('; ', $known);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace App\Communication\Extraction;
|
||||
|
||||
use App\Actions\ContributeCaseFacts;
|
||||
use App\Models\Booking;
|
||||
use App\Process\CaseRequirements;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The programmatic risk check of an extraction (v0.11, CH-14). The model's confidence is no calibrated probability:
|
||||
* it may trigger a review, never waive one. Criticality comes from the field, the phase and the kind of deviation;
|
||||
* contradictions and failed plausibility checks call for the review model regardless of the confidence, and are never
|
||||
* taken over automatically. Thresholds are configured and calibrated with test messages (go-live condition).
|
||||
*/
|
||||
class RiskAssessment
|
||||
{
|
||||
/** Details whose automatic completion is harmless in every phase. */
|
||||
private const ADMINISTRATIVE = ['guest.phone', 'booking.arrival_time'];
|
||||
|
||||
/**
|
||||
* Checks every fact and marks why it may not be taken over automatically.
|
||||
*
|
||||
* @param array{facts: list<array{field: string, value: string, evidence: string, confidence: float|int}>, change_requests: list<array{field: string, value: string, evidence: string}>, intent: string, uncertainties: list<string>} $extraction
|
||||
* @return list<array{field: string, value: string, evidence: string, confidence: float, criticality: float, problems: list<string>}>
|
||||
*/
|
||||
public function facts(Booking $booking, string $message, array $extraction): array
|
||||
{
|
||||
$checked = [];
|
||||
$values = [];
|
||||
foreach ($extraction['facts'] as $fact) {
|
||||
$values[$fact['field']][] = mb_strtolower(trim($fact['value']));
|
||||
}
|
||||
|
||||
foreach ($extraction['facts'] as $fact) {
|
||||
$field = $fact['field'];
|
||||
$value = trim($fact['value']);
|
||||
$problems = [];
|
||||
|
||||
if (! CaseRequirements::isValid($field, $value)) {
|
||||
$problems[] = 'Wert ungültig';
|
||||
}
|
||||
if (! $this->evidenceFound($message, $fact['evidence'])) {
|
||||
$problems[] = 'Belegstelle nicht in der Nachricht';
|
||||
}
|
||||
if (count(array_unique($values[$field] ?? [])) > 1) {
|
||||
$problems[] = 'widersprüchliche Angaben in derselben Nachricht';
|
||||
}
|
||||
$current = CaseRequirements::value($booking, $field);
|
||||
if (CaseRequirements::isValid($field, $current) && mb_strtolower(trim((string) $current)) !== mb_strtolower($value)) {
|
||||
$problems[] = 'weicht vom gespeicherten Wert ab';
|
||||
}
|
||||
$problems = [...$problems, ...$this->plausibility($booking, $field, $value)];
|
||||
|
||||
$checked[] = [
|
||||
'field' => $field,
|
||||
'value' => $value,
|
||||
'evidence' => $fact['evidence'],
|
||||
'confidence' => max(0.0, min(1.0, (float) $fact['confidence'])),
|
||||
'criticality' => $this->criticality($booking, $field),
|
||||
'problems' => $problems,
|
||||
];
|
||||
}
|
||||
|
||||
return $checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the review model has to look at the extraction: any contradiction or failed check, any wish to change
|
||||
* something in a binding phase, an uncertainty the model reported, or criticality × (1 − confidence) above the
|
||||
* configured threshold.
|
||||
*
|
||||
* @param list<array{criticality: float, confidence: float, problems: list<string>}> $facts
|
||||
* @param array{change_requests: list<mixed>, uncertainties: list<string>} $extraction
|
||||
*/
|
||||
public function needsReview(Booking $booking, array $facts, array $extraction): bool
|
||||
{
|
||||
if ($extraction['uncertainties'] !== [] || ($extraction['change_requests'] !== [] && $booking->offer_version > 0)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$threshold = (float) config('fewo.ai.escalate_above', 0.2);
|
||||
foreach ($facts as $fact) {
|
||||
if ($fact['problems'] !== [] || $threshold < $fact['criticality'] * (1 - $fact['confidence'])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a checked fact may go to the fact intake automatically: no problem, confident enough, the detail not
|
||||
* known yet, and no stay detail (those are change requests). The intake applies its own rules on top.
|
||||
*
|
||||
* @param array{field: string, confidence: float, problems: list<string>} $fact
|
||||
*/
|
||||
public function mayTakeOver(Booking $booking, array $fact): bool
|
||||
{
|
||||
return $fact['problems'] === []
|
||||
&& $fact['confidence'] >= (float) config('fewo.ai.min_confidence', 0.85)
|
||||
&& ! in_array($fact['field'], ContributeCaseFacts::STAY_FIELDS, true)
|
||||
&& ! CaseRequirements::isValid($fact['field'], CaseRequirements::value($booking, $fact['field']))
|
||||
&& (in_array($fact['field'], self::ADMINISTRATIVE, true) || str_starts_with($fact['field'], 'guest.'));
|
||||
}
|
||||
|
||||
public function evidenceFound(string $message, string $evidence): bool
|
||||
{
|
||||
$normalise = fn (string $text): string => mb_strtolower(trim((string) preg_replace('/\s+/u', ' ', $text)));
|
||||
$evidence = $normalise($evidence);
|
||||
|
||||
return $evidence !== '' && str_contains($normalise($message), $evidence);
|
||||
}
|
||||
|
||||
private function criticality(Booking $booking, string $field): float
|
||||
{
|
||||
return match (true) {
|
||||
in_array($field, self::ADMINISTRATIVE, true) => 0.3,
|
||||
in_array($field, ContributeCaseFacts::STAY_FIELDS, true), $field === 'guest.email' => 1.0,
|
||||
ContributeCaseFacts::isBinding($booking) => 0.8,
|
||||
default => 0.6,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function plausibility(Booking $booking, string $field, string $value): array
|
||||
{
|
||||
return match ($field) {
|
||||
'booking.guests' => (int) $value > (int) $booking->property?->max_guests ? ['mehr Personen als die Wohnung fasst'] : [],
|
||||
'booking.arrival', 'booking.departure' => $this->dateProblems($booking, $field, $value),
|
||||
default => [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function dateProblems(Booking $booking, string $field, string $value): array
|
||||
{
|
||||
$date = rescue(fn () => CarbonImmutable::parse($value), null, false);
|
||||
if ($date === null) {
|
||||
return ['Datum nicht lesbar'];
|
||||
}
|
||||
if ($date->isBefore(CarbonImmutable::today())) {
|
||||
return ['Datum liegt in der Vergangenheit'];
|
||||
}
|
||||
if ($field === 'booking.departure' && $booking->arrival !== null && ! $date->isAfter($booking->arrival)) {
|
||||
return ['Abreise nicht nach der Anreise'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Communication\Extraction\ExtractionFailed;
|
||||
use App\Communication\Extraction\MessageExtractor;
|
||||
use App\Models\Booking;
|
||||
use App\Models\InboundMessage;
|
||||
use App\Models\Property;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
/**
|
||||
* Measures the extraction on a set of anonymised sample messages (v0.11, M05): wrong values and missed details are
|
||||
* counted separately – no system guarantees that it finds every detail, so the rate of missed ones is reported, not
|
||||
* hidden. Calls the configured models (costs money); meant for calibrating prompts and thresholds before go-live.
|
||||
*/
|
||||
#[Signature('ai:evaluate {--path= : JSON file with samples} {--key= : API key (default: FEWO_AI_API_KEY)}')]
|
||||
#[Description('Evaluate the guest message extraction against sample messages (wrong values and missed details)')]
|
||||
class EvaluateExtraction extends Command
|
||||
{
|
||||
public function handle(MessageExtractor $extractor): int
|
||||
{
|
||||
$path = (string) ($this->option('path') ?: resource_path('prompts/extraction/evaluation/samples.json'));
|
||||
$key = (string) ($this->option('key') ?: config('fewo.ai.api_key'));
|
||||
if ($key === '') {
|
||||
$this->error('Kein API-Schlüssel (FEWO_AI_API_KEY oder --key).');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$samples = (array) json_decode((string) file_get_contents($path), true);
|
||||
$property = Property::query()->withoutGlobalScopes()->first() ?? new Property(['name' => 'Ferienwohnung', 'max_guests' => 4]);
|
||||
$totals = ['expected' => 0, 'found' => 0, 'missed' => 0, 'wrong' => 0, 'extra' => 0, 'changes_missed' => 0, 'failed' => 0];
|
||||
|
||||
foreach ($samples as $sample) {
|
||||
$booking = new Booking(['arrival' => now()->addDays(14)->toDateString(), 'departure' => now()->addDays(17)->toDateString(), 'guests' => 2, 'status' => 'reserved']);
|
||||
$booking->setRelation('property', $property);
|
||||
$booking->setRelation('guest', null);
|
||||
$message = new InboundMessage(['subject' => $sample['subject'], 'body' => $sample['body']]);
|
||||
|
||||
try {
|
||||
$result = $extractor->extract($key, $message, $booking)['data'];
|
||||
} catch (ExtractionFailed $exception) {
|
||||
$totals['failed']++;
|
||||
$this->warn("{$sample['id']}: {$exception->getMessage()}");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$found = collect($result['facts'])->mapWithKeys(fn (array $fact): array => [$fact['field'] => mb_strtolower(trim($fact['value']))])->all();
|
||||
foreach ($sample['expected'] as $field => $value) {
|
||||
$totals['expected']++;
|
||||
match (true) {
|
||||
! isset($found[$field]) => $totals['missed']++,
|
||||
$found[$field] !== mb_strtolower($value) => $totals['wrong']++,
|
||||
default => $totals['found']++,
|
||||
};
|
||||
}
|
||||
$totals['extra'] += count(array_diff_key($found, $sample['expected']));
|
||||
$changes = collect($result['change_requests'])->pluck('field')->all();
|
||||
$totals['changes_missed'] += count(array_diff($sample['expected_changes'], $changes));
|
||||
$this->line("{$sample['id']}: ".json_encode(['found' => $found, 'changes' => $changes], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
$this->table(array_keys($totals), [array_values($totals)]);
|
||||
$rate = $totals['expected'] > 0 ? round(100 * $totals['missed'] / $totals['expected'], 1) : 0;
|
||||
$this->info("Übersehene Angaben: {$rate} % · Prompt-Version ".MessageExtractor::promptVersion());
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Actions\ExtractMessageFacts;
|
||||
use App\Models\InboundMessage;
|
||||
use App\Models\Tenant;
|
||||
use App\Tenancy\TenantRunner;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Throwable;
|
||||
|
||||
#[Signature('messages:extract')]
|
||||
#[Description('Evaluate assigned guest and portal messages (v0.11 extraction) – a limited number per landlord and run')]
|
||||
class ExtractInboundMessages extends Command
|
||||
{
|
||||
public function handle(ExtractMessageFacts $extract, TenantRunner $runner): int
|
||||
{
|
||||
$runner->each(function (Tenant $tenant) use ($extract): void {
|
||||
$messages = InboundMessage::query()
|
||||
->whereNotNull('booking_id')
|
||||
->whereNull('extraction_status')
|
||||
->whereIn('status', [InboundMessage::ASSIGNED, InboundMessage::PORTAL])
|
||||
->orderBy('received_at')
|
||||
->limit((int) config('fewo.ai.per_run', 10))
|
||||
->get();
|
||||
|
||||
foreach ($messages as $message) {
|
||||
try {
|
||||
$this->line("{$tenant->name}: {$message->subject} → {$extract->handle($message)}");
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$message->update(['extraction_status' => 'failed']);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,8 @@ public function description(): string
|
||||
return match ($this) {
|
||||
self::PortalSync => 'Belegungen der Portale über iCal einlesen und den eigenen Kalender als Feed anbieten.',
|
||||
self::PortalMail => 'Buchungsmails der Portale aus dem Postfach lesen und Termine sofort sperren.',
|
||||
self::AiOffers, self::AiMarketing, self::AiGuestMail => 'Noch nicht gebaut.',
|
||||
self::AiGuestMail => 'Antworten und Nachrichten der Gäste auswerten: erkannte Angaben (Anschrift, Telefon …) werden vorgeschlagen und nur nach festen Regeln übernommen; nichts Verbindliches entscheidet die KI.',
|
||||
self::AiOffers, self::AiMarketing => 'Noch nicht gebaut.',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,8 +42,8 @@ public function description(): string
|
||||
public function isWired(): bool
|
||||
{
|
||||
return match ($this) {
|
||||
self::PortalSync, self::PortalMail => true,
|
||||
self::AiOffers, self::AiMarketing, self::AiGuestMail => false,
|
||||
self::PortalSync, self::PortalMail, self::AiGuestMail => true,
|
||||
self::AiOffers, self::AiMarketing => false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -52,11 +53,12 @@ public function isWired(): bool
|
||||
*/
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return $this->isWired();
|
||||
// The AI service sends guests' messages to an external provider: only after the operator switched it on.
|
||||
return $this->isWired() && $this !== self::AiGuestMail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the landlord's own API key is of any use. Nothing calls out yet, so no service has one.
|
||||
* Whether the landlord's own API key is of any use (the guest communication extraction calls out).
|
||||
*/
|
||||
public function usesApiKey(): bool
|
||||
{
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
namespace App\Filament\Resources\InboundMessages;
|
||||
|
||||
use App\Actions\ContributeCaseFacts;
|
||||
use App\Actions\CreateReservation;
|
||||
use App\Actions\RequestCaseData;
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\BookingTaskKind;
|
||||
use App\Enums\BookingTaskStatus;
|
||||
use App\Enums\FactSource;
|
||||
use App\Exceptions\ReservationException;
|
||||
use App\Filament\Concerns\FollowsAdminMode;
|
||||
use App\Filament\Resources\Bookings\BookingResource;
|
||||
@@ -18,10 +20,12 @@
|
||||
use App\Pricing\QuoteException;
|
||||
use App\Pricing\StayRequest;
|
||||
use App\Process\CaseLog;
|
||||
use App\Process\CaseRequirements;
|
||||
use BackedEnum;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\ActionGroup;
|
||||
use Filament\Forms\Components\CheckboxList;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
@@ -111,6 +115,14 @@ public static function table(Table $table): Table
|
||||
TextEntry::make('from')->label('Von')->state(fn (InboundMessage $record): string => trim(($record->from_name ? $record->from_name.' · ' : '').$record->from_address)),
|
||||
TextEntry::make('hints_text')->label('Hinweise')->state(fn (InboundMessage $record): string => collect((array) $record->hints)->map(fn (array $hint): string => $hint['reference'].': '.$hint['why'])->implode(' · ') ?: 'keine')
|
||||
->visible(fn (InboundMessage $record): bool => $record->status === InboundMessage::NEEDS_REVIEW),
|
||||
TextEntry::make('proposals_text')->label('Erkannte Angaben (KI-Vorschläge)')
|
||||
->state(fn (InboundMessage $record): HtmlString => new HtmlString(collect((array) ($record->extraction['proposed'] ?? []))
|
||||
->map(fn (array $fact): string => e(CaseRequirements::label($fact['field']).': „'.$fact['value'].'“').($fact['problems'] !== [] ? ' <em>('.e(implode(', ', $fact['problems'])).')</em>' : ' <em>(unsicher)</em>'))
|
||||
->implode('<br>') ?: '–'))
|
||||
->visible(fn (InboundMessage $record): bool => ($record->extraction['proposed'] ?? []) !== []),
|
||||
TextEntry::make('applied_text')->label('Automatisch übernommen')
|
||||
->state(fn (InboundMessage $record): string => collect((array) ($record->extraction['applied'] ?? []))->map(fn (array $fact): string => CaseRequirements::label($fact['field']).': „'.$fact['value'].'“')->implode(' · '))
|
||||
->visible(fn (InboundMessage $record): bool => ($record->extraction['applied'] ?? []) !== []),
|
||||
TextEntry::make('body_text')->label('Nachricht')->state(fn (InboundMessage $record): HtmlString => new HtmlString(nl2br(e((string) $record->body)))),
|
||||
]),
|
||||
Action::make('assign')->label('Vorgang zuordnen')->icon(Heroicon::OutlinedLink)
|
||||
@@ -158,6 +170,25 @@ public static function table(Table $table): Table
|
||||
$record->update(['status' => InboundMessage::PROCESSED, 'booking_id' => $booking->id, 'match_reason' => 'new_request', 'processed_at' => now()]);
|
||||
Notification::make()->title('Anfrage '.$booking->reference.' angelegt')->success()->send();
|
||||
}),
|
||||
Action::make('applyProposals')->label('Vorschläge übernehmen')->icon(Heroicon::OutlinedCheckBadge)
|
||||
->visible(fn (InboundMessage $record): bool => $record->booking_id !== null && ($record->extraction['proposed'] ?? []) !== [])
|
||||
->modalDescription('Nur übernehmen, was Sie in der Nachricht selbst geprüft haben. Zeitraum und Personenzahl ändern Sie über „Buchung ändern“ bzw. ein neues Angebot.')
|
||||
->schema([
|
||||
CheckboxList::make('fields')->label('Angaben')->required()
|
||||
->options(fn (InboundMessage $record): array => collect((array) ($record->extraction['proposed'] ?? []))
|
||||
->reject(fn (array $fact): bool => in_array($fact['field'], ContributeCaseFacts::STAY_FIELDS, true))
|
||||
->mapWithKeys(fn (array $fact): array => [$fact['field'] => CaseRequirements::label($fact['field']).': „'.$fact['value'].'“'])->all()),
|
||||
])
|
||||
->action(function (InboundMessage $record, array $data, ContributeCaseFacts $facts): void {
|
||||
$chosen = collect((array) ($record->extraction['proposed'] ?? []))->whereIn('field', $data['fields']);
|
||||
$result = $facts->handle($record->booking, $chosen->mapWithKeys(fn (array $fact): array => [$fact['field'] => $fact['value']])->all(), FactSource::Landlord, [
|
||||
'inbound_message_id' => $record->id,
|
||||
'reference' => 'message-landlord:'.$record->id,
|
||||
'evidence' => $chosen->mapWithKeys(fn (array $fact): array => [$fact['field'] => $fact['evidence']])->all(),
|
||||
]);
|
||||
$record->update(['extraction' => [...(array) $record->extraction, 'proposed' => collect((array) ($record->extraction['proposed'] ?? []))->whereNotIn('field', $data['fields'])->values()->all()]]);
|
||||
Notification::make()->title('Übernommen')->body($result->summary())->success()->send();
|
||||
}),
|
||||
ActionGroup::make([
|
||||
Action::make('done')->label('Als erledigt markieren')->icon(Heroicon::OutlinedCheck)
|
||||
->visible(fn (InboundMessage $record): bool => $record->status === InboundMessage::ASSIGNED)
|
||||
|
||||
Reference in new issue
Block a user