fix retrieve final
This commit is contained in:
@@ -291,8 +291,9 @@ final readonly class AgentRunner
|
||||
|
||||
$history = $this->contextService->buildUserContextWithinBudget($userId, 3000);
|
||||
$previousQuestions = $this->extractRecentUserQuestions($history, 2);
|
||||
$referenceAnchors = $this->extractLatestAssistantReferenceAnchors($history);
|
||||
|
||||
if ($previousQuestions === []) {
|
||||
if ($previousQuestions === [] && $referenceAnchors === []) {
|
||||
return $prompt;
|
||||
}
|
||||
|
||||
@@ -302,6 +303,11 @@ final readonly class AgentRunner
|
||||
$lines[] = 'Vorherige Nutzerfrage: ' . $question;
|
||||
}
|
||||
|
||||
if ($referenceAnchors !== []) {
|
||||
$lines[] = 'Vorherige technische Referenzanker (nur zur Referenzauflösung, keine Faktenquelle): '
|
||||
. implode(' ', $referenceAnchors);
|
||||
}
|
||||
|
||||
$lines[] = 'Aktuelle Folgefrage: ' . $prompt;
|
||||
|
||||
return implode("\n", $lines);
|
||||
@@ -408,6 +414,101 @@ final readonly class AgentRunner
|
||||
return array_slice($questions, -$limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts stable reference anchors from the latest assistant answer.
|
||||
*
|
||||
* These anchors are only used to resolve follow-up references such as
|
||||
* "der Wert" or "welcher Indikator". They are not factual evidence for
|
||||
* the final answer. To avoid propagating wrong earlier answers, only the
|
||||
* first explicit Testomat model reference and the first explicit °dH value
|
||||
* are kept. Indicator names, reagent codes, prices, URLs and product
|
||||
* numbers are intentionally ignored here.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function extractLatestAssistantReferenceAnchors(string $history): array
|
||||
{
|
||||
$turn = $this->extractLatestHistoryTurn($history);
|
||||
|
||||
if ($turn === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$answer = preg_replace('/^Question:\s*.*(?:\R|$)/u', '', $turn, 1) ?? '';
|
||||
$answer = trim($answer);
|
||||
|
||||
if ($answer === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$anchors = [];
|
||||
|
||||
$model = $this->extractFirstTestomatModelAnchor($answer);
|
||||
if ($model !== '') {
|
||||
$anchors[] = $model;
|
||||
}
|
||||
|
||||
$hardnessValue = $this->extractFirstHardnessValueAnchor($answer);
|
||||
if ($hardnessValue !== '') {
|
||||
$anchors[] = $hardnessValue;
|
||||
}
|
||||
|
||||
return array_values(array_unique($anchors));
|
||||
}
|
||||
|
||||
private function extractLatestHistoryTurn(string $history): string
|
||||
{
|
||||
$history = trim($history);
|
||||
|
||||
if ($history === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$parts = preg_split('/(?=^Question:\s)/m', $history);
|
||||
|
||||
if ($parts === false || $parts === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$turns = array_values(array_filter(
|
||||
array_map(static fn(string $part): string => trim($part), $parts),
|
||||
static fn(string $part): bool => $part !== ''
|
||||
));
|
||||
|
||||
if ($turns === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string) end($turns);
|
||||
}
|
||||
|
||||
private function extractFirstTestomatModelAnchor(string $text): string
|
||||
{
|
||||
$pattern = '/\bTestomat(?:®)?\s+'
|
||||
. '(?:\d{3,4}|EVO(?:\s+[A-Z]{2,6})?|ECO(?:[-\s]?(?:PLUS|C))?|DUO(?:\s+\d{3,4})?|LAB(?:\s+[A-Z]{2,6})?)'
|
||||
. '\b/iu';
|
||||
|
||||
if (preg_match($pattern, $text, $matches) !== 1) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = $this->sanitizeHistoryQuestion((string) ($matches[0] ?? ''));
|
||||
$value = preg_replace('/\s+/u', ' ', $value) ?? $value;
|
||||
|
||||
return trim(str_replace('®', '', $value));
|
||||
}
|
||||
|
||||
private function extractFirstHardnessValueAnchor(string $text): string
|
||||
{
|
||||
if (preg_match('/\b\d+(?:[,.]\d+)?\s*°\s*dH\b/iu', $text, $matches) !== 1) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = preg_replace('/\s+/u', ' ', (string) ($matches[0] ?? '')) ?? '';
|
||||
|
||||
return trim($value);
|
||||
}
|
||||
|
||||
private function sanitizeHistoryQuestion(string $question): string
|
||||
{
|
||||
$question = trim((string) preg_replace('/\s+/u', ' ', $question));
|
||||
|
||||
Reference in New Issue
Block a user