From 93ff6262cc45ec5e6fae7b29145809401dc84219 Mon Sep 17 00:00:00 2001 From: team 1 Date: Sat, 25 Apr 2026 18:37:29 +0200 Subject: [PATCH] optimize error handling if shop api error --- src/Agent/AgentRunner.php | 28 +++++++++++++++++++++++++++- src/Commerce/ShopSearchService.php | 16 +++------------- src/Controller/AskSseController.php | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Agent/AgentRunner.php b/src/Agent/AgentRunner.php index cc8cdb9..06bc43e 100644 --- a/src/Agent/AgentRunner.php +++ b/src/Agent/AgentRunner.php @@ -144,15 +144,21 @@ final readonly class AgentRunner $commerceHistoryContext ); $primaryShopSearchHadSystemFailure = $this->shopSearchService->hadLastSearchSystemFailure(); + $primaryShopSearchFailureReason = $this->shopSearchService->getLastSearchFailureReason(); if ($primaryShopSearchHadSystemFailure) { $this->agentLogger->warning('Shop repair skipped after Store API system failure', [ 'userId' => $userId, 'commerceIntent' => $commerceIntent, 'shopSearchQuery' => $shopSearchQuery, - 'failureReason' => $this->shopSearchService->getLastSearchFailureReason(), + 'failureReason' => $primaryShopSearchFailureReason, ]); + yield $this->systemMsg( + $this->buildShopUnavailableMessage($primaryShopSearchFailureReason), + 'info' + ); + $repairPayload = [ 'results' => $primaryShopResults, 'attemptedRepair' => false, @@ -268,6 +274,7 @@ final readonly class AgentRunner 'usedShopRepair' => $usedShopRepair, 'shopRepairQueries' => $shopRepairQueries, 'primaryShopSearchHadSystemFailure' => $primaryShopSearchHadSystemFailure, + 'primaryShopSearchFailureReason' => $primaryShopSearchFailureReason ?? null, 'knowledgeChunkCount' => count($knowledgeChunks), 'knowledgeRetrievalPrompt' => $knowledgeRetrievalPrompt, 'usedFollowUpRetrievalContext' => $usedFollowUpRetrievalContext, @@ -783,6 +790,25 @@ final readonly class AgentRunner } } + private function buildShopUnavailableMessage(?string $reason): string + { + $reason = trim((string) $reason); + + if ($reason === '') { + $reason = 'keine Detailmeldung vom Shopware-Server'; + } + + $reason = preg_replace('/\s+/u', ' ', $reason) ?? $reason; + + if (mb_strlen($reason, 'UTF-8') > 260) { + $reason = rtrim(mb_substr($reason, 0, 257, 'UTF-8')) . '...'; + } + + return '⚠️ Shopsystem aktuell nicht erreichbar oder fehlerhaft. Grund: ' + . htmlspecialchars($reason, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') + . '. Ich antworte ohne Shopdaten weiter.'; + } + private function buildUserErrorMessage(Throwable $e): string { if (!$this->debug) { diff --git a/src/Commerce/ShopSearchService.php b/src/Commerce/ShopSearchService.php index 458e157..306d07c 100644 --- a/src/Commerce/ShopSearchService.php +++ b/src/Commerce/ShopSearchService.php @@ -103,13 +103,14 @@ final class ShopSearchService ); if ($this->lastSearchHadSystemFailure) { - $this->logger->warning('Shop search stopped after Store API system failure during reference probe', [ + $this->logger->warning('Shop search reference probe failed, continuing with primary search', [ 'commerceIntent' => $commerceIntent, 'originalPrompt' => $originalPrompt, 'failureReason' => $this->lastSearchFailureReason, ]); - //return []; + // A reference probe is only an enrichment step. It must not block the primary shop search. + $this->resetLastSearchFailure(); } $rankedProducts = $this->executeSearch( @@ -238,17 +239,6 @@ final class ShopSearchService false ); - if ($this->lastSearchHadSystemFailure) { - $this->logger->warning('Shop reference probe stopped after Store API system failure', [ - 'commerceIntent' => $commerceIntent, - 'originalPrompt' => $originalPrompt, - 'referenceSearchText' => $referenceSearchText, - 'failureReason' => $this->lastSearchFailureReason, - ]); - - break; - } - if ($results !== []) { $allResults = array_merge($allResults, $results); } diff --git a/src/Controller/AskSseController.php b/src/Controller/AskSseController.php index 8458785..6bee4a0 100644 --- a/src/Controller/AskSseController.php +++ b/src/Controller/AskSseController.php @@ -14,7 +14,7 @@ use Symfony\Component\Routing\Annotation\Route; final readonly class AskSseController { - private const JOB_TTL_SECONDS = 30; + private const JOB_TTL_SECONDS = 900; public function __construct( private AgentRunner $agentRunner,