'Anthropic (Claude)', 'openai' => 'OpenAI oder OpenAI-kompatibler Dienst', 'ollama' => 'Ollama (eigener Server)', ]; public const ENDPOINT = 'https://api.anthropic.com/v1/messages'; public const OPENAI_URL = 'https://api.openai.com/v1'; public const OLLAMA_URL = 'http://localhost:11434'; public const DEFAULT_MODEL = 'claude-sonnet-5'; public const MAX_INPUT = 20000; public function __construct(private readonly Settings $settings) {} public function provider(): string { $provider = (string) $this->settings->get('ai.provider', 'anthropic'); return isset(self::PROVIDERS[$provider]) ? $provider : 'anthropic'; } public function available(): bool { if (! $this->settings->get('ai.enabled') || ($this->provider() !== 'anthropic' && blank($this->settings->get('ai.model')))) { return false; } // Ollama braucht keinen Schlüssel return $this->provider() === 'ollama' || filled($this->settings->get('ai.api_key')); } public function model(): string { return (string) ($this->settings->get('ai.model') ?: ($this->provider() === 'anthropic' ? self::DEFAULT_MODEL : '')); } public function baseUrl(): string { return rtrim((string) ($this->settings->get('ai.base_url') ?: match ($this->provider()) { 'openai' => self::OPENAI_URL, 'ollama' => self::OLLAMA_URL, default => 'https://api.anthropic.com', }), '/'); } /** Verlässt der Text bei diesem Anbieter das eigene Netz? */ public function isExternal(): bool { return $this->provider() !== 'ollama'; } /** * @param array{enabled: bool, provider?: ?string, base_url?: ?string, model: ?string, api_key: ?string} $values */ public function save(array $values): void { $provider = isset(self::PROVIDERS[$values['provider'] ?? '']) ? $values['provider'] : 'anthropic'; $baseUrl = $provider === 'anthropic' || blank($values['base_url'] ?? null) ? null : rtrim(trim($values['base_url']), '/'); if ($baseUrl !== null) { self::assertAllowedUrl($provider, $baseUrl); } // Gespeicherter Schlüssel gilt nur für dasselbe Ziel – sonst ließe er sich an einen eigenen Server umleiten $destinationChanged = $provider !== $this->provider() || $baseUrl !== ($this->settings->get('ai.base_url') ?: null); $keepKey = blank($values['api_key'] ?? null) && ! $destinationChanged; if (blank($values['api_key'] ?? null) && $destinationChanged && filled($this->settings->get('ai.api_key')) && $provider !== 'ollama' && ($values['enabled'] ?? false)) { throw ValidationException::withMessages(['data.api_key' => 'Anbieter oder Adresse wurden geändert – bitte den API-Schlüssel erneut eingeben.']); } $data = [ 'ai.enabled' => (bool) $values['enabled'], 'ai.provider' => $provider, 'ai.base_url' => $baseUrl, 'ai.model' => filled($values['model'] ?? null) ? trim($values['model']) : null, ]; if (filled($values['api_key'] ?? null)) { $data['ai.api_key'] = Crypt::encryptString(trim($values['api_key'])); } elseif (! $keepKey) { $data['ai.api_key'] = null; } $this->settings->setMany($data); } public function forgetKey(): void { $this->settings->setMany(['ai.api_key' => null, 'ai.enabled' => $this->provider() === 'ollama' && (bool) $this->settings->get('ai.enabled')]); } /** * OpenAI-kompatible Dienste nur über HTTPS und öffentliche Adressen. Ollama darf im * eigenen Netz laufen – aber nie an Link-Local-Adressen (Metadaten-Dienste der Cloud). */ public static function assertAllowedUrl(string $provider, string $url): void { $scheme = strtolower((string) parse_url($url, PHP_URL_SCHEME)); $host = strtolower(trim((string) parse_url($url, PHP_URL_HOST), '[]')); $fail = fn (string $message) => throw ValidationException::withMessages(['data.base_url' => $message]); if ($host === '' || parse_url($url, PHP_URL_USER) !== null || parse_url($url, PHP_URL_QUERY) !== null) { $fail('Bitte eine gültige Adresse angeben, z. B. '.($provider === 'ollama' ? self::OLLAMA_URL : self::OPENAI_URL).'.'); } if ($provider === 'openai' && ($scheme !== 'https' || ! SafeOutbound::isPublicUrl($url, ['https']))) { $fail('OpenAI-kompatible Dienste nur über HTTPS und eine öffentliche Adresse. Für einen Server im eigenen Netz „Ollama“ wählen.'); } if ($provider === 'ollama') { $ips = filter_var($host, FILTER_VALIDATE_IP) ? [$host] : (gethostbynamel($host) ?: []); if (! in_array($scheme, ['http', 'https'], true) || $host === 'metadata.google.internal' || collect($ips)->contains(fn ($ip) => str_starts_with($ip, '169.254.') || str_starts_with(strtolower($ip), 'fe80:'))) { $fail('Diese Adresse ist nicht erlaubt.'); } } } /** * Vorschlag in Leichter Sprache als einfaches HTML (h2, p, ul/li, strong). */ public function easyLanguage(string $title, string $text): string { $text = mb_substr(trim($text), 0, self::MAX_INPUT); if ($text === '') { throw new RuntimeException('Der Inhalt enthält keinen Text, der übertragen werden könnte.'); } return $this->complete( <<<'PROMPT' Du übersetzt Texte aus dem Intranet eines Unternehmens in Leichte Sprache nach den Regeln des Netzwerks Leichte Sprache. Regeln: - Kurze Sätze. Ein Gedanke pro Satz. Keine Nebensätze, wenn es sich vermeiden lässt. - Einfache, bekannte Wörter. Fachwörter und Abkürzungen erklären oder ersetzen. - Aktive Formulierungen, keine Verneinungen wie „nicht unwichtig“. - Die Leserinnen und Leser direkt ansprechen („Sie“). - Zahlen als Ziffern, Datum ausgeschrieben (z. B. „am 3. Oktober“). - Lange Wörter mit Bindestrich trennen (z. B. „Mitarbeiter-Versammlung“). - Wichtiges zuerst. Nichts hinzuerfinden, nichts Wichtiges weglassen. - Gliedern mit kurzen Zwischenüberschriften und Listen. Antworte ausschließlich mit HTML aus diesen Elementen:
,