Frag das Portal (0.64.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Failing after 19m38s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 24m17s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 6m4s

Antworten mit Quellen aus Seiten, QM-Dokumenten, Glossar und Neuigkeiten –
nur aus Sichtbarem, über den KI-Assistenten (Anthropic, OpenAI, Ollama).
Wissensquellen als Registry für Plugins, Auswertung ohne Personenbezug.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-10-07 18:08:35 +00:00
parent 372f0656f9
commit 9d8bdc08ce
168 changed files with 1751 additions and 383 deletions

120
app/Ask/Knowledge.php Normal file
View file

@ -0,0 +1,120 @@
<?php
namespace App\Ask;
use App\Models\User;
use App\Plugins\PluginAccess;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* Wissensquellen für „Frag das Portal“. Kern und Plugins melden an, worin gesucht
* wird – z. B. Handbuch-Seiten, QM-Dokumente, Glossar, Neuigkeiten. Jede Quelle
* liefert nur, was die fragende Person sehen darf, und dazu den vollen Text
* (die Antwort entsteht aus diesen Texten).
*
* Treffer: ['title' => …, 'url' => …, 'text' => Klartext, 'meta' => ?string]
*/
class Knowledge
{
/** Höchstens so viel Text je Treffer – ausgewählt wird später ohnehin nur ein Ausschnitt */
public const MAX_TEXT = 30000;
/** @var array<string, array{label: string, search: callable}> */
private array $sources = [];
/**
* Die Quelle bekommt Person, Suchbegriffe (klein geschrieben; ein Treffer enthält
* mindestens einen) und Höchstzahl.
*
* @param callable(User, list<string>, int): list<array<string, mixed>> $search
*/
public function add(string $key, string $label, callable $search): void
{
$this->sources[$key] = compact('label', 'search');
}
public function remove(string $key): void
{
unset($this->sources[$key]);
}
/**
* @return array<string, string> Schlüssel => Bezeichnung
*/
public function labels(): array
{
return array_map(fn (array $source) => $source['label'], $this->sources);
}
/**
* @param list<string> $terms
* @return list<array{source: string, label: string, title: string, url: string, text: string, meta: ?string}>
*/
public function gather(User $user, array $terms, int $perSource = 8): array
{
if ($terms === []) {
return [];
}
$documents = [];
foreach ($this->sources as $key => $source) {
if (! app(PluginAccess::class)->allows($key, $user)) {
continue;
}
try {
foreach (array_slice(($source['search'])($user, $terms, $perSource), 0, $perSource) as $hit) {
$documents[] = [
'source' => $key,
'label' => $source['label'],
'title' => (string) $hit['title'],
'url' => (string) $hit['url'],
'text' => mb_substr((string) $hit['text'], 0, self::MAX_TEXT),
'meta' => $hit['meta'] ?? null,
];
}
} catch (Throwable $e) {
// Eine fehlerhafte Quelle darf die Antwort nicht verhindern.
Log::error("Wissensquelle {$key} fehlgeschlagen: ".$e->getMessage(), ['exception' => $e]);
}
}
return $documents;
}
/**
* Mindestens einer der Begriffe in mindestens einer der Spalten.
*
* @template T of Builder|QueryBuilder
*
* @param T $query
* @param list<string> $columns
* @param list<string> $terms
* @return T
*/
public static function whereAny(Builder|QueryBuilder $query, array $columns, array $terms): Builder|QueryBuilder
{
return $query->where(function ($q) use ($columns, $terms) {
foreach ($terms as $term) {
$like = '%'.str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $term).'%';
foreach ($columns as $column) {
$q->orWhere($column, 'like', $like);
}
}
});
}
/** HTML in lesbaren Klartext (Absätze bleiben als Zeilenumbrüche erhalten) */
public static function plain(?string $html): string
{
$text = preg_replace('#<(br|/p|/li|/h[1-6]|/tr|/div)\b[^>]*>#i', "\n", (string) $html) ?? '';
$text = html_entity_decode(strip_tags($text), ENT_QUOTES | ENT_HTML5);
return trim(preg_replace(["/[ \t]+/u", "/\n\s*\n+/u"], [' ', "\n"], $text) ?? '');
}
}