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:
LarsandClaude Opus 5.5 committed 2026-11-03 13:49:51 +01:00
1 parent dff9e2344e
commit 4170e3bd29
18 files changed
+1130 -7

No files matched your search

+188
View File
@@ -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()]);
}
}
}
+3 -1
View File
@@ -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;
}
}
+7 -5
View File
@@ -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)
+19
View File
@@ -104,6 +104,25 @@
| Mailbox that receives the portals' booking notifications (fast path for portal bookings).
| Read via IMAP every minute; messages are left unread. Empty host = disabled.
*/
/*
| v0.11 extraction of guest messages (CH-14): a small model extracts, rules check, a larger model reviews risky
| results. Thresholds are starting values to be calibrated with representative messages (go-live condition).
*/
'ai' => [
'endpoint' => env('FEWO_AI_ENDPOINT', 'https://api.anthropic.com/v1/messages'),
'api_version' => env('FEWO_AI_API_VERSION', '2026-10-21'),
'api_key' => env('FEWO_AI_API_KEY'),
'small_model' => env('FEWO_AI_SMALL_MODEL', 'claude-haiku-5-5'),
'review_model' => env('FEWO_AI_REVIEW_MODEL', 'claude-opus-5-5'),
'timeout' => (int) env('FEWO_AI_TIMEOUT', 40),
'prompt_version' => env('FEWO_AI_PROMPT_VERSION', 'v1'),
// Escalate to the review model when criticality × (1 − confidence) exceeds this value.
'escalate_above' => (float) env('FEWO_AI_ESCALATE_ABOVE', 0.2),
// Minimum confidence of the final result for taking a detail over automatically.
'min_confidence' => (float) env('FEWO_AI_MIN_CONFIDENCE', 0.85),
'per_run' => (int) env('FEWO_AI_PER_RUN', 10),
],
'mailbox' => [
'host' => env('FEWO_MAILBOX_HOST'),
'port' => (int) env('FEWO_MAILBOX_PORT', 993),
@@ -0,0 +1,30 @@
[
{
"id": "antwort-anschrift",
"subject": "Re: Ihre Anfrage 2026-0001: Bitte ergänzen Sie Ihre Angaben",
"body": "Hallo,\n\nhier unsere Anschrift: Lindenallee 12, 10115 Berlin, Deutschland. Telefon 030 1234567.\n\nViele Grüße\nGesa Gast",
"expected": {"guest.street": "Lindenallee 12", "guest.postal_code": "10115", "guest.city": "Berlin", "guest.country": "DE", "guest.phone": "030 1234567"},
"expected_changes": []
},
{
"id": "teilantwort-mit-wunsch",
"subject": "Re: Rückfrage",
"body": "Guten Tag, wir wohnen in 18225 Kühlungsborn, Strandweg 3. Können wir eventuell einen Tag länger bleiben, bis zum 11.11.2026?",
"expected": {"guest.street": "Strandweg 3", "guest.postal_code": "18225", "guest.city": "Kühlungsborn"},
"expected_changes": ["booking.departure"]
},
{
"id": "zitat-nicht-uebernehmen",
"subject": "Re: Ihre Buchung",
"body": "Danke, alles klar!\n\n> Bitte nennen Sie uns Ihre Anschrift, z. B. Musterstraße 1, 12345 Musterstadt.",
"expected": {},
"expected_changes": []
},
{
"id": "anreisezeit",
"subject": "Anreise",
"body": "Wir kommen voraussichtlich gegen 17 Uhr an. Herzliche Grüße, Familie Muster",
"expected": {"booking.arrival_time": "17:00"},
"expected_changes": []
}
]
+36
View File
@@ -0,0 +1,36 @@
{
"type": "object",
"additionalProperties": false,
"required": ["facts", "change_requests", "intent", "uncertainties"],
"properties": {
"facts": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["field", "value", "evidence", "confidence"],
"properties": {
"field": {"type": "string", "enum": ["guest.first_name", "guest.last_name", "guest.email", "guest.phone", "guest.street", "guest.postal_code", "guest.city", "guest.country", "booking.guests", "booking.arrival", "booking.departure", "booking.arrival_time"]},
"value": {"type": "string"},
"evidence": {"type": "string"},
"confidence": {"type": "number"}
}
}
},
"change_requests": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["field", "value", "evidence"],
"properties": {
"field": {"type": "string", "enum": ["booking.guests", "booking.arrival", "booking.departure", "booking.extras", "cancellation", "other"]},
"value": {"type": "string"},
"evidence": {"type": "string"}
}
}
},
"intent": {"type": "string", "enum": ["answer", "question", "change", "cancellation", "new_request", "other"]},
"uncertainties": {"type": "array", "items": {"type": "string"}}
}
}
+12
View File
@@ -0,0 +1,12 @@
You check another model's extraction from a guest's e-mail to a holiday apartment landlord. You see the original message and the first extraction. Correct it: remove values the message does not state, add details that were missed, move wishes to change something into `change_requests`, and lower the confidence of anything ambiguous. Keep `evidence` as an exact copy of the passage in the message. You only extract; you decide nothing about bookings, prices or contracts.
Context of the case (not from the guest):
- Stage of the case: {{phase}}
- Details the landlord is waiting for: {{requested_fields}}
- Details already on file (masked, for comparison only): {{known_fields}}
- Today's date: {{today}}
First extraction:
{{first_extraction}}
Return the corrected extraction in the same format. Note in `uncertainties` what you changed and why.
+16
View File
@@ -0,0 +1,16 @@
You read one e-mail that a guest of a holiday apartment sent to the landlord, and you extract structured details from it. You only extract; you never decide anything about bookings, prices or contracts.
Context of the case (not from the guest):
- Stage of the case: {{phase}}
- Details the landlord is waiting for: {{requested_fields}}
- Details already on file (masked, for comparison only): {{known_fields}}
- Today's date: {{today}}
Rules:
- Extract only what the guest states in this message about the guest or this stay. Ignore quoted earlier mails, signatures of other people and anything you would have to guess.
- `facts`: one entry per detail. `field` must be one of the allowed keys. `value` is written as the guest wrote it, normalised only as described below. `evidence` is the exact passage of the message that contains the value, copied character for character. `confidence` is your estimate between 0 and 1.
- Normalise: country as ISO 3166-1 alpha-2 code (e.g. DE), dates as YYYY-MM-DD, number of guests as a whole number, postal code without the country prefix.
- If the guest wants something to change (other dates, other number of guests, cancel, extras), put it into `change_requests` with the wished value – never into `facts`.
- `intent`: what the message mainly is.
- `uncertainties`: short notes about anything unclear, contradictory or possibly missed. Empty when there is nothing.
- If the message contains no details, return empty lists.
+3
View File
@@ -21,6 +21,9 @@
// v0.11: reminders of data requests (never extends a hold or a deadline of the case).
Schedule::command('cases:data-requests')->everyFiveMinutes()->withoutOverlapping();
// v0.11: evaluate assigned messages (limited per run; outside the minute-by-minute portal fast path).
Schedule::command('messages:extract')->everyFiveMinutes()->withoutOverlapping(10);
Schedule::command('bookings:resend-confirmations')->everyFifteenMinutes()->withoutOverlapping();
Schedule::command('channels:sync')->everyFiveMinutes()->withoutOverlapping(10);
+275
View File
@@ -0,0 +1,275 @@
<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Actions\ExtractMessageFacts;
use App\Actions\ReceiveInboundMessage;
use App\Channels\Mail\PortalMail;
use App\Communication\Extraction\MessageExtractor;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\FactSource;
use App\Enums\TenantServiceKey;
use App\Filament\Resources\InboundMessages\Pages\ListInboundMessages;
use App\Models\BookingTask;
use App\Models\CaseFact;
use App\Models\InboundMessage;
use App\Models\Property;
use App\Models\ServiceUsage;
use App\Models\TenantService;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Support\Content;
use App\Support\Settings;
use App\Tenancy\TenantFeatures;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* v0.11 package U6: risk-based two-stage extraction (CH-14, E-035, M05, M06). The model only proposes; rules decide
* whether a detail goes to the fact intake. Contradictions and failed checks are reviewed regardless of the model's
* confidence and never taken over automatically. No network: the API is faked.
*/
class MessageExtractionTest extends TestCase
{
use RefreshDatabase;
private const ANSWER = "Hallo,\nhier unsere Anschrift: Lindenallee 12, 10115 Berlin. Telefon 030 1234567.\nGesa Gast";
private Property $property;
/** @var list<array<string, mixed>> */
private array $calls = [];
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
$this->property = Property::sole();
Mail::fake();
Storage::fake('local');
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']);
config(['fewo.ai.api_key' => 'test-key']);
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true]);
app(TenantFeatures::class)->forget();
}
public function test_without_the_service_nothing_is_sent_to_a_provider(): void
{
TenantService::query()->update(['is_enabled' => false]);
app(TenantFeatures::class)->forget();
Http::fake();
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->reply()));
Http::assertNothingSent();
}
public function test_confident_checked_details_are_taken_over_through_the_fact_intake_without_review(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic']]);
$this->fakeModels(small: $this->extraction([
$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12'), $this->fact('guest.postal_code', '10115', '10115 Berlin'),
$this->fact('guest.city', 'Berlin', '10115 Berlin'), $this->fact('guest.country', 'DE', 'Lindenallee 12, 10115 Berlin', 0.9),
$this->fact('guest.phone', '030 1234567', 'Telefon 030 1234567'),
]));
$message = $this->reply();
$this->assertSame('done', app(ExtractMessageFacts::class)->handle($message));
$booking = $message->booking->fresh();
$this->assertSame('Lindenallee 12', $booking->guest->street);
$this->assertSame(FactSource::Extraction, CaseFact::query()->where('field', 'guest.street')->where('outcome', CaseFact::APPLIED)->sole()->source);
$this->assertSame(BookingStatus::OfferSent, $booking->status, 'complete now: the existing process sent the offer');
$this->assertCount(1, $this->calls, 'no review needed');
$this->assertSame(InboundMessage::PROCESSED, $message->fresh()->status);
$this->assertSame(1, ServiceUsage::query()->count());
}
/** Precision 2: a contradiction is reviewed even at high confidence and is never taken over automatically. */
public function test_a_contradiction_is_reviewed_regardless_of_the_confidence_and_only_proposed(): void
{
$this->fakeModels(
small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.99)]),
review: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.99)]),
);
$message = $this->reply(['street' => 'Alter Weg 2']);
app(ExtractMessageFacts::class)->handle($message);
$this->assertSame('Alter Weg 2', $message->booking->guest->fresh()->street);
$this->assertCount(2, $this->calls, 'the review model was asked');
$this->assertSame(['weicht vom gespeicherten Wert ab'], $message->fresh()->extraction['proposed'][0]['problems']);
}
public function test_a_value_without_evidence_in_the_message_is_not_taken_over(): void
{
$this->fakeModels(
small: $this->extraction([$this->fact('guest.street', 'Musterstraße 1', 'Musterstraße 1', 0.99)]),
review: $this->extraction([$this->fact('guest.street', 'Musterstraße 1', 'Musterstraße 1', 0.99)]),
);
$message = $this->reply();
app(ExtractMessageFacts::class)->handle($message);
$this->assertNull($message->booking->guest->fresh()->street);
$this->assertContains('Belegstelle nicht in der Nachricht', $message->fresh()->extraction['proposed'][0]['problems']);
}
public function test_two_models_reading_a_value_differently_leave_it_to_the_landlord(): void
{
$this->fakeModels(
small: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Telefon 030 1234567', 0.3)]),
review: $this->extraction([$this->fact('guest.phone', '030 123456', 'Telefon 030 1234567', 0.95)]),
);
$message = $this->reply();
app(ExtractMessageFacts::class)->handle($message);
$this->assertNull($message->booking->guest->fresh()->phone);
$this->assertContains('die beiden Modelle lesen den Wert unterschiedlich', $message->fresh()->extraction['proposed'][0]['problems']);
}
/** M06: a phone number is completed, a wish to stay longer only becomes a request. */
public function test_a_wish_to_change_the_stay_is_only_a_request(): void
{
$text = 'Unsere Nummer: 030 1234567. Können wir bis zum 11.11.2026 bleiben?';
$this->fakeModels(
small: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Unsere Nummer: 030 1234567')], [['field' => 'booking.departure', 'value' => '2026-11-11', 'evidence' => 'Können wir bis zum 11.11.2026 bleiben?']], 'change'),
review: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Unsere Nummer: 030 1234567')], [['field' => 'booking.departure', 'value' => '2026-11-11', 'evidence' => 'Können wir bis zum 11.11.2026 bleiben?']], 'change'),
);
$message = $this->reply(text: $text);
app(ExtractMessageFacts::class)->handle($message);
$booking = $message->booking->fresh();
$this->assertSame('030 1234567', $booking->guest->phone);
$this->assertSame('2026-11-10', $booking->departure->toDateString(), 'the stay is unchanged');
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ChangeRequest)->count());
}
public function test_an_answer_that_does_not_fit_the_schema_or_a_refusal_waits_for_the_landlord(): void
{
$this->fakeModels(small: ['facts' => 'kaputt']);
$broken = $this->reply();
$this->assertSame('failed', app(ExtractMessageFacts::class)->handle($broken));
$this->assertCount(2, $this->calls, 'one repetition');
$this->calls = [];
Http::fake(['*' => Http::response(['content' => [], 'stop_reason' => 'refusal', 'model' => 'claude-haiku-5-5'])]);
$refused = $this->reply(id: '<2@x>', email: 'zwei@example.org', arrival: '2026-11-20', departure: '2026-11-23');
$this->assertSame('failed', app(ExtractMessageFacts::class)->handle($refused));
$this->assertSame(1, BookingTask::query()->where('booking_id', $refused->booking_id)->where('kind', BookingTaskKind::InboundReview)->count(), 'the landlord still has the message to look at');
}
public function test_the_request_uses_structured_outputs_and_the_review_model_has_the_refusal_fallback(): void
{
$this->fakeModels(
small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.3)]),
review: $this->extraction([]),
);
app(ExtractMessageFacts::class)->handle($this->reply());
[$small, $review] = $this->calls;
$this->assertSame(['claude-haiku-5-5', 'json_schema'], [$small['body']['model'], $small['body']['output_config']['format']['type']]);
$this->assertArrayNotHasKey('fallbacks', $small['body']);
$this->assertSame(['claude-opus-5-5', 'default', 'server-side-fallback-2026-07-01'], [$review['body']['model'], $review['body']['fallbacks'], $review['beta']]);
$this->assertSame('2026-10-21', $small['version']);
$this->assertStringNotContainsString('{{', $small['body']['system']);
$this->assertStringContainsString('Lindenallee 12', $review['body']['system'], 'the review sees the first extraction');
}
public function test_the_landlord_takes_over_a_proposal_in_the_inbox(): void
{
$this->actingAs(User::factory()->create());
$this->fakeModels(
small: $this->extraction([$this->fact('guest.city', 'Berlin', '10115 Berlin', 0.4)]),
review: $this->extraction([$this->fact('guest.city', 'Berlin', '10115 Berlin', 0.6)]),
);
$message = $this->reply();
app(ExtractMessageFacts::class)->handle($message);
$this->assertNull($message->booking->guest->fresh()->city);
Livewire::test(ListInboundMessages::class)->callAction(TestAction::make('applyProposals')->table($message->fresh()), ['fields' => ['guest.city']]);
$this->assertSame('Berlin', $message->booking->guest->fresh()->city);
$this->assertSame(FactSource::Landlord, CaseFact::query()->where('field', 'guest.city')->latest('id')->first()->source);
}
/** M05: wrong values and missed details are counted separately. */
public function test_the_evaluation_reports_missed_details(): void
{
$this->fakeModels(small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12')]));
$this->artisan('ai:evaluate')->expectsOutputToContain('Übersehene Angaben:')->assertSuccessful();
}
public function test_the_prompt_is_versioned_and_every_placeholder_is_filled(): void
{
$booking = $this->reply()->booking;
$prompt = app(MessageExtractor::class)->prompt('', $booking);
$this->assertSame('v1', MessageExtractor::promptVersion());
$this->assertStringContainsString('non-binding request', $prompt);
$this->assertStringContainsString('guest.street', $prompt);
$this->assertStringContainsString('guest.email=g', $prompt, 'known values are masked');
$this->assertStringNotContainsString('gesa@example.org', $prompt);
}
/**
* @param array<string, string> $guest
*/
private function reply(array $guest = [], string $text = self::ANSWER, string $id = '<1@x>', string $email = 'gesa@example.org', string $arrival = '2026-11-07', string $departure = '2026-11-10'): InboundMessage
{
$booking = app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => $email, ...$guest]);
return app(ReceiveInboundMessage::class)->handle(new PortalMail($id, $email, 'Re: Ihre Anfrage '.$booking->reference, $text, CarbonImmutable::now()))->fresh();
}
/**
* @return array<string, mixed>
*/
private function fact(string $field, string $value, string $evidence, float $confidence = 0.95): array
{
return ['field' => $field, 'value' => $value, 'evidence' => $evidence, 'confidence' => $confidence];
}
/**
* @param list<array<string, mixed>> $facts
* @param list<array<string, mixed>> $changes
* @return array<string, mixed>
*/
private function extraction(array $facts, array $changes = [], string $intent = 'answer'): array
{
return ['facts' => $facts, 'change_requests' => $changes, 'intent' => $intent, 'uncertainties' => []];
}
/**
* @param array<string, mixed> $small
* @param array<string, mixed>|null $review
*/
private function fakeModels(array $small, ?array $review = null): void
{
Http::fake(function (Request $request) use ($small, $review) {
$body = $request->data();
$this->calls[] = ['body' => $body, 'beta' => $request->header('anthropic-beta')[0] ?? null, 'version' => $request->header('anthropic-version')[0] ?? null];
$data = str_starts_with((string) $body['model'], 'claude-opus') ? ($review ?? $small) : $small;
return Http::response(['id' => 'msg', 'type' => 'message', 'role' => 'assistant', 'model' => $body['model'], 'content' => [['type' => 'text', 'text' => json_encode($data)]], 'stop_reason' => 'end_turn', 'usage' => ['input_tokens' => 10, 'output_tokens' => 10]]);
});
}
}
+3 -1
View File
@@ -80,9 +80,11 @@ public function test_without_a_landlord_the_configuration_of_the_installation_ap
public function test_only_services_the_application_really_asks_about_are_offered(): void
{
$this->assertSame(
[TenantServiceKey::PortalSync, TenantServiceKey::PortalMail],
[TenantServiceKey::PortalSync, TenantServiceKey::PortalMail, TenantServiceKey::AiGuestMail],
TenantServiceKey::wired(),
);
// v0.11: the guest message extraction sends data to a provider – only after the operator switched it on.
$this->assertFalse(TenantServiceKey::AiGuestMail->enabledByDefault());
foreach (TenantServiceKey::cases() as $service) {
if (! $service->isWired()) {