optimize cleanup search query shop api extends

This commit is contained in:
team2
2026-04-25 22:05:35 +02:00
parent 4823752b3e
commit 6cf8aac872
4 changed files with 327 additions and 5 deletions

View File

@@ -61,6 +61,7 @@ final readonly class AgentRunner
$usedShopRepair = false;
$shopRepairQueries = [];
$primaryShopSearchHadSystemFailure = false;
$historyNotices = [];
$this->agentLogger->info('Agent run started', [
'userId' => $userId,
@@ -170,10 +171,15 @@ final readonly class AgentRunner
'failureReason' => $primaryShopSearchFailureReason,
]);
$shopUnavailableMessage = $this->buildShopUnavailableMessage($primaryShopSearchFailureReason);
yield $this->systemMsg(
$this->buildShopUnavailableMessage($primaryShopSearchFailureReason),
$shopUnavailableMessage,
'err'
);
$historyNotices[] = $this->buildHistoryNotice(
'Shopdaten konnten nicht geladen werden',
$primaryShopSearchFailureReason
);
$repairPayload = [
'results' => $primaryShopResults,
@@ -271,11 +277,13 @@ final readonly class AgentRunner
yield $this->systemMsg($finalPrompt, 'debug');
}
if ($fullOutput !== '') {
$historyResponse = $this->buildHistoryResponse($fullOutput, $historyNotices);
if ($historyResponse !== '') {
$this->contextService->appendHistory(
$userId,
$prompt,
$fullOutput
$historyResponse
);
}
@@ -307,7 +315,17 @@ final readonly class AgentRunner
'exception' => $e,
]);
yield $this->systemMsg($this->buildUserErrorMessage($e), 'err');
$userErrorMessage = $this->buildUserErrorMessage($e);
yield $this->systemMsg($userErrorMessage, 'err');
$historyResponse = $this->buildHistoryResponse('', array_merge(
$historyNotices,
[$this->buildHistoryNotice('Antwort konnte nicht abgeschlossen werden', $e->getMessage())]
));
if ($historyResponse !== '') {
$this->contextService->appendHistory($userId, $prompt, $historyResponse);
}
}
}
@@ -806,6 +824,66 @@ final readonly class AgentRunner
}
}
/**
* @param string[] $notices
*/
private function buildHistoryResponse(string $fullOutput, array $notices): string
{
$parts = [];
foreach ($notices as $notice) {
$notice = trim($notice);
if ($notice !== '') {
$parts[] = $notice;
}
}
$fullOutput = trim($fullOutput);
if ($fullOutput !== '') {
$parts[] = $fullOutput;
} else {
$noLlmMessage = $this->plainTextFromHtml($this->agentRunnerConfig->getNoLlmDataReceivedMessage());
if ($noLlmMessage === '') {
$noLlmMessage = 'Es wurden keine Daten vom LLM empfangen.';
}
$parts[] = 'Systemhinweis: ' . $noLlmMessage;
}
return trim(implode("\n\n", $parts));
}
private function buildHistoryNotice(string $title, ?string $detail): string
{
$title = $this->normalizeOneLine($this->plainTextFromHtml($title));
$detail = $this->normalizeOneLine($this->plainTextFromHtml((string) $detail));
if ($title === '') {
$title = 'Systemhinweis';
}
if ($detail === '') {
return 'Systemhinweis: ' . $title . '.';
}
if (mb_strlen($detail, 'UTF-8') > 500) {
$detail = rtrim(mb_substr($detail, 0, 497, 'UTF-8')) . '...';
}
return 'Systemhinweis: ' . $title . '. Ursache: ' . $detail;
}
private function plainTextFromHtml(string $value): string
{
$value = html_entity_decode(strip_tags($value), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$value = preg_replace('/\s+/u', ' ', $value) ?? $value;
return trim($value);
}
private function buildShopSearchMetaMessage(
string $query,
string $commerceIntent,