Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/AiSettingsPage.php
Friederich Loheide 856a5c30af
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m24s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m15s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m10s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m8s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m23s
Vorlesen und Leichte Sprache mit KI-Vorschlag (0.34.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:24:56 +00:00

125 lines
5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Filament\Pages;
use App\Ai\AiAssistant;
use App\Support\Audit;
use App\Support\Settings;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Validation\ValidationException;
use Throwable;
use UnitEnum;
/**
* KI-Assistent: API-Schlüssel und Modell, z. B. für Vorschläge in Leichter Sprache.
*/
class AiSettingsPage extends Page
{
protected string $view = 'filament.pages.settings-form';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedSparkles;
protected static string|UnitEnum|null $navigationGroup = 'System';
protected static ?int $navigationSort = 6;
protected static ?string $navigationLabel = 'KI-Assistent';
protected static ?string $title = 'KI-Assistent';
protected static ?string $slug = 'ki-assistent';
/** @var array<string, mixed>|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();
}),
];
}
}