v0.11 U6: Bring your own key für die KI-Auswertung (Anthropic, OpenRouter)
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m11s

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-11-03 14:19:13 +01:00
1 parent 6f9aa46943
commit 6862dec201
20 files changed
+752 -95

No files matched your search

+183
View File
@@ -0,0 +1,183 @@
<?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\AiAccess;
use App\Enums\TenantServiceKey;
use App\Filament\Pages\AiConnection;
use App\Models\InboundMessage;
use App\Models\InboxMessage;
use App\Models\Property;
use App\Models\TenantService;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Support\Settings;
use App\Tenancy\TenantFeatures;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
/**
* v0.11, bring your own key for the extraction: each landlord configures provider (Anthropic or OpenRouter), key and
* both models himself; there is no operator key, no built-in model and no free endpoint. The key is stored encrypted,
* never shown again and never logged; a rejected access switches the extraction off and leaves messages to the
* landlord.
*/
class AiAccessTest extends TestCase
{
use RefreshDatabase;
private const EXTRACTION = ['facts' => [], 'change_requests' => [], 'intent' => 'answer', 'uncertainties' => []];
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
Mail::fake();
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true]);
app(TenantFeatures::class)->forget();
}
public function test_without_the_landlords_own_complete_access_nothing_is_sent(): void
{
Http::fake();
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message()));
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => ''], key: 'sk-or-1');
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message('<2@x>', 'zwei@example.org', '2026-11-20', '2026-11-23')), 'both models are required');
Http::assertNothingSent();
}
public function test_openrouter_is_called_at_its_fixed_endpoint_with_the_landlords_key_and_a_strict_schema(): void
{
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => 'vendor/large'], key: 'sk-or-v1-geheim');
Http::fake(['https://openrouter.ai/api/v1/chat/completions' => Http::response(['choices' => [['finish_reason' => 'stop', 'message' => ['content' => json_encode(self::EXTRACTION)]]], 'model' => 'vendor/small'])]);
$this->assertSame('done', app(ExtractMessageFacts::class)->handle($this->message()));
Http::assertSent(function (Request $request): bool {
$body = $request->data();
return $request->url() === 'https://openrouter.ai/api/v1/chat/completions'
&& $request->header('Authorization')[0] === 'Bearer sk-or-v1-geheim'
&& $body['model'] === 'vendor/small'
&& $body['response_format']['type'] === 'json_schema'
&& $body['response_format']['json_schema']['strict'] === true
&& $body['provider']['require_parameters'] === true
&& $body['messages'][0]['role'] === 'system';
});
}
public function test_a_rejected_key_switches_the_extraction_off_and_tells_the_landlord(): void
{
$this->access(['provider' => 'openrouter', 'small_model' => 'vendor/small', 'review_model' => 'vendor/large'], key: 'sk-or-v1-abgelaufen');
Http::fake(['*' => Http::response(['error' => ['code' => 401, 'message' => 'Invalid key sk-or-v1-abgelaufen']], 401)]);
$first = $this->message();
$this->assertSame('failed', app(ExtractMessageFacts::class)->handle($first));
Http::assertSentCount(1);
$service = AiAccess::service(Settings::tenant());
$this->assertSame(AiAccess::INVALID, $service->check_status);
$this->assertStringNotContainsString('abgelaufen', (string) $service->check_message, 'the key is never stored in a message');
$this->assertStringNotContainsString('abgelaufen', (string) $first->fresh()->extraction['error']);
$this->assertSame(1, InboxMessage::query()->where('kind', 'ai_access_rejected')->count());
$this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->message('<2@x>', 'zwei@example.org', '2026-11-20', '2026-11-23')));
Http::assertSentCount(1);
}
public function test_the_landlord_configures_the_access_the_key_is_encrypted_and_never_shown(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(AiConnection::class)
->fillForm(['provider' => 'anthropic', 'api_key' => 'sk-ant-geheim', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'])
->call('save')->assertHasNoFormErrors();
$service = AiAccess::service(Settings::tenant());
$this->assertSame('sk-ant-geheim', $service->api_key);
$this->assertStringNotContainsString('sk-ant-geheim', (string) DB::table('tenant_services')->where('id', $service->id)->value('api_key'), 'encrypted at rest');
$this->assertArrayNotHasKey('api_key', $service->toArray());
Livewire::test(AiConnection::class)
->assertDontSee('sk-ant-geheim')
->assertSee('gespeichert – leer lassen')
->fillForm(['provider' => 'anthropic', 'api_key' => '', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-sonnet-5-5'])
->call('save');
$this->assertSame(['sk-ant-geheim', 'claude-sonnet-5-5'], [$service->fresh()->api_key, $service->fresh()->settings['review_model']]);
}
public function test_the_check_tries_both_models_and_records_the_result(): void
{
$this->actingAs(User::factory()->create());
$this->access(['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'], key: 'sk-ant-1');
$broken = false;
Http::fake(['https://api.anthropic.com/v1/messages' => function () use (&$broken) {
return $broken
? Http::response(['error' => ['type' => 'not_found_error', 'message' => 'model: claude-falsch']], 404)
: Http::response(['content' => [['type' => 'text', 'text' => '{"ok":true}']], 'stop_reason' => 'end_turn', 'model' => 'x']);
}]);
Livewire::test(AiConnection::class)->call('check')->assertNotified('Verbindung in Ordnung');
Http::assertSentCount(2);
$this->assertSame(AiAccess::OK, AiAccess::service(Settings::tenant())->check_status);
$broken = true;
AiAccess::service(Settings::tenant())->update(['settings' => ['provider' => 'anthropic', 'small_model' => 'claude-falsch', 'review_model' => 'claude-opus-5-5']]);
Livewire::test(AiConnection::class)->call('check')->assertNotified('Prüfung fehlgeschlagen');
$this->assertSame(AiAccess::INVALID, AiAccess::service(Settings::tenant())->check_status);
}
public function test_the_page_exists_only_when_the_operator_switched_the_service_on(): void
{
$this->actingAs(User::factory()->create());
TenantService::query()->update(['is_enabled' => false]);
app(TenantFeatures::class)->forget();
$this->assertFalse(AiConnection::canAccess());
}
public function test_endpoints_cannot_be_redirected_by_configuration(): void
{
config(['fewo.ai.endpoint' => 'https://evil.example/v1']);
$this->access(['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5'], key: 'sk-ant-1');
Http::fake(['*' => Http::response(['content' => [['type' => 'text', 'text' => json_encode(self::EXTRACTION)]], 'stop_reason' => 'end_turn'])]);
app(ExtractMessageFacts::class)->handle($this->message());
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://api.anthropic.com/v1/messages');
Http::assertNotSent(fn (Request $request): bool => str_contains($request->url(), 'evil.example'));
}
/**
* @param array<string, string> $settings
*/
private function access(array $settings, string $key): void
{
TenantService::query()->update(['settings' => json_encode($settings), 'api_key' => encrypt($key, false)]);
}
private function message(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(Property::sole(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => $email]);
return app(ReceiveInboundMessage::class)->handle(new PortalMail($id, $email, 'Re: '.$booking->reference, 'Strandweg 1, 18225 Kühlungsborn', CarbonImmutable::now()))->fresh();
}
}
+3 -3
View File
@@ -62,8 +62,8 @@ protected function setUp(): void
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]);
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true, 'api_key' => 'test-key',
'settings' => ['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5']]);
app(TenantFeatures::class)->forget();
}
@@ -214,7 +214,7 @@ 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();
$this->artisan('ai:evaluate', ['tenant' => Settings::tenant()->id])->expectsOutputToContain('Übersehene Angaben:')->assertSuccessful();
}
public function test_the_prompt_is_versioned_and_every_placeholder_is_filled(): void
+9 -7
View File
@@ -56,7 +56,7 @@ protected function setUp(): void
$this->property = Property::sole();
Storage::fake('local');
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
config(['mail.default' => 'array', 'fewo.spam.min_seconds' => 0, 'fewo.ai.api_key' => 'test-key']);
config(['mail.default' => 'array', 'fewo.spam.min_seconds' => 0]);
Mail::swap(new MailManager($this->app));
Settings::tenant()->update([
'bank_iban' => 'DE02120300000000202051',
@@ -64,7 +64,9 @@ protected function setUp(): void
'process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic'],
]);
Mailbox::query()->create(['tenant_id' => Settings::tenant()->id, 'host' => 'imap.example.org', 'username' => 'anfragen@example.org', 'password' => 'x', 'mode' => Mailbox::FULL, 'address' => 'anfragen@example.org', 'is_active' => true]);
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true]);
// Bring your own key: the landlord's own access (here OpenRouter), no operator key.
TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true, 'api_key' => 'sk-or-test',
'settings' => ['provider' => 'openrouter', 'small_model' => 'vendor/small-model', 'review_model' => 'vendor/large-model']]);
app(TenantFeatures::class)->forget();
}
@@ -138,10 +140,10 @@ public function test_a_binding_portal_booking_stays_binding_and_is_only_complete
*/
private function fakeExtraction(array $facts): void
{
Http::fake(fn (Request $request) => Http::response([
'id' => 'msg', 'type' => 'message', 'role' => 'assistant', 'model' => $request->data()['model'],
'content' => [['type' => 'text', 'text' => json_encode(['facts' => $facts, 'change_requests' => [], 'intent' => 'answer', 'uncertainties' => []])]],
'stop_reason' => 'end_turn', 'usage' => ['input_tokens' => 10, 'output_tokens' => 10],
]));
Http::fake(['https://openrouter.ai/api/v1/chat/completions' => fn (Request $request) => Http::response([
'id' => 'gen', 'model' => $request->data()['model'],
'choices' => [['finish_reason' => 'stop', 'message' => ['role' => 'assistant', 'content' => json_encode(['facts' => $facts, 'change_requests' => [], 'intent' => 'answer', 'uncertainties' => []])]]],
'usage' => ['prompt_tokens' => 10, 'completion_tokens' => 10],
])]);
}
}