|null */ public ?array $data = []; public static function canAccess(): bool { return auth()->user()?->can('ai.manage') ?? false; } public function mount(Settings $settings): void { $this->form->fill([ 'enabled' => (bool) $settings->get('ai.enabled'), 'model' => $settings->get('ai.model') ?: AiAssistant::DEFAULT_MODEL, 'api_key' => null, ]); } public function form(Schema $schema): Schema { $hasKey = filled(app(Settings::class)->get('ai.api_key')); return $schema ->components([ Section::make('Datenschutz') ->schema([ Text::make('Wenn der Assistent eingeschaltet ist, können Redakteurinnen und Redakteure Vorschläge in Leichter Sprache anfordern. Dazu wird der Text des Beitrags bzw. der Seite an Anthropic übertragen und dort verarbeitet. Nutzen Sie den Assistenten nur mit einem Vertrag zur Auftragsverarbeitung und nicht für vertrauliche Inhalte. Übertragen wird nur, was alle Lesenden sehen – keine Blöcke für bestimmte Zielgruppen.'), ]), Section::make('Zugang') ->columns(2) ->schema([ Toggle::make('enabled')->label('KI-Assistent einschalten')->columnSpanFull(), TextInput::make('api_key')->label('API-Schlüssel (Anthropic)')->password()->revealable()->maxLength(300)->autocomplete('new-password') ->helperText($hasKey ? 'Ein Schlüssel ist gespeichert. Leer lassen, um ihn zu behalten.' : 'Erhalten Sie in der Anthropic Console unter „API Keys“.'), TextInput::make('model')->label('Modell')->required()->maxLength(100)->placeholder(AiAssistant::DEFAULT_MODEL) ->helperText('Standard: '.AiAssistant::DEFAULT_MODEL), ]), ]) ->statePath('data'); } public function save(AiAssistant $ai, Settings $settings): void { $state = $this->form->getState(); if ($state['enabled'] && blank($state['api_key']) && blank($settings->get('ai.api_key'))) { throw ValidationException::withMessages(['data.api_key' => 'Bitte einen API-Schlüssel eingeben.']); } $ai->save($state); $this->data['api_key'] = null; Audit::log('ai.settings_updated', ['enabled' => (bool) $state['enabled'], 'model' => $state['model']]); Notification::make()->success()->title('KI-Assistent gespeichert')->send(); } protected function getHeaderActions(): array { return [ Action::make('test') ->label('Verbindung testen') ->icon(Heroicon::OutlinedSignal) ->color('gray') ->action(function (AiAssistant $ai) { try { $ai->test(); Notification::make()->success()->title('Verbindung klappt')->body('Modell: '.$ai->model())->send(); } catch (Throwable $e) { Notification::make()->danger()->title('Verbindung fehlgeschlagen')->body($e->getMessage())->persistent()->send(); } }), Action::make('forget') ->label('Schlüssel löschen') ->icon(Heroicon::OutlinedTrash) ->color('danger') ->requiresConfirmation() ->visible(fn () => filled(app(Settings::class)->get('ai.api_key'))) ->action(function (AiAssistant $ai) { $ai->forgetKey(); $this->data['enabled'] = false; Audit::log('ai.key_removed'); Notification::make()->success()->title('Schlüssel gelöscht, Assistent ausgeschaltet')->send(); }), ]; } }