diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e29f902..b1b465a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ Neueste Version oben. Jeder Abschnitt beginnt mit `## MAJOR.MINOR.PATCH` und enthält kurze Stichpunkte für Anwenderinnen und Anwender – keine Commit-Texte. Der Self-Updater zeigt diese Abschnitte bei neuen Versionen an. +## 0.35.0 + +- KI-Assistent: Neben Anthropic jetzt auch OpenAI bzw. OpenAI-kompatible Dienste (z. B. Mistral, IONOS, OpenRouter) und Ollama auf einem eigenen Server – dann bleibt der Text im eigenen Netz +- Anbieter, Adresse und Modell unter „System → KI-Assistent“ wählbar; „Verbindung testen“ nennt Anbieter und Modell, fehlende Ollama-Modelle werden mit Hinweis gemeldet +- Ein gespeicherter API-Schlüssel gilt nur für den Dienst, für den er eingetragen wurde – bei Wechsel von Anbieter oder Adresse muss er neu eingegeben werden + ## 0.34.0 - Vorlesen: Beiträge und Seiten lassen sich mit der Sprachausgabe des Geräts vorlesen – mit Pause und Stopp, ohne dass Text das Portal verlässt diff --git a/app/Accessibility/EasyLanguage.php b/app/Accessibility/EasyLanguage.php index 44b8646f..514eeb3c 100644 --- a/app/Accessibility/EasyLanguage.php +++ b/app/Accessibility/EasyLanguage.php @@ -83,6 +83,7 @@ class EasyLanguage 'html' => $html, 'can_edit' => $canEdit, 'ai' => $canEdit && app(AiAssistant::class)->available(), + 'ai_external' => app(AiAssistant::class)->isExternal(), ]; } } diff --git a/app/Ai/AiAssistant.php b/app/Ai/AiAssistant.php index e3bfe479..7a84d6f0 100644 --- a/app/Ai/AiAssistant.php +++ b/app/Ai/AiAssistant.php @@ -2,49 +2,109 @@ namespace App\Ai; +use App\Support\SafeOutbound; use App\Support\Settings; +use Illuminate\Http\Client\PendingRequest; +use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Http; +use Illuminate\Validation\ValidationException; use RuntimeException; use Throwable; /** - * KI-Assistent (derzeit Anthropic Claude) – z. B. für Vorschläge in Leichter Sprache. - * Nur aktiv, wenn die Administration ihn eingerichtet hat. Die Texte werden dazu an - * den Anbieter übertragen; darauf weist die Oberfläche hin. + * KI-Assistent, z. B. für Vorschläge in Leichter Sprache. Anbieter wählbar: + * Anthropic (Claude), OpenAI bzw. OpenAI-kompatible Dienste und Ollama auf einem + * eigenen Server. Nur aktiv, wenn die Administration ihn eingerichtet hat. */ class AiAssistant { + public const PROVIDERS = [ + 'anthropic' => '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 { - return (bool) $this->settings->get('ai.enabled') && filled($this->settings->get('ai.api_key')); + 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') ?: self::DEFAULT_MODEL); + 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, model: ?string, api_key: ?string} $values + * @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); @@ -52,7 +112,35 @@ class AiAssistant public function forgetKey(): void { - $this->settings->setMany(['ai.api_key' => null, 'ai.enabled' => false]); + $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.'); + } + } } /** @@ -81,7 +169,7 @@ class AiAssistant - Gliedern mit kurzen Zwischenüberschriften und Listen. Antworte ausschließlich mit HTML aus diesen Elementen:

,

,