From bcef683fe4529e3f14ad141f23de770f707a98c3 Mon Sep 17 00:00:00 2001 From: Anupam Kumar Date: Tue, 1 Oct 2024 20:43:00 +0530 Subject: [PATCH] fix: better error handling and some fixes Signed-off-by: Anupam Kumar --- lib/Service/LangRopeService.php | 29 +++++++++++++++------- lib/TaskProcessing/ContextChatProvider.php | 14 ++++++++++- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/Service/LangRopeService.php b/lib/Service/LangRopeService.php index 3115cfe..cae62e9 100644 --- a/lib/Service/LangRopeService.php +++ b/lib/Service/LangRopeService.php @@ -76,20 +76,20 @@ private function requestToExApp( $enabledResponse = $appApiFunctions->exAppRequest('context_chat_backend', '/enabled', $this->userId, 'GET'); if (is_array($enabledResponse) && isset($enabledResponse['error'])) { - throw new RuntimeException('Error during request to ExApp (context_chat_backend): ' . $enabledResponse['error']); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): ' . $enabledResponse['error']); } $enabledResponse = $enabledResponse->getBody(); if (!is_string($enabledResponse)) { - $this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a string', ['response' => $enabledResponse]); - throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a string'); + $this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a string', ['response' => $enabledResponse]); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a string'); } $enabledResponse = json_decode($enabledResponse, true); if ($enabledResponse === null) { - $this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a valid JSON', ['response' => $enabledResponse]); - throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a valid JSON'); + $this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a valid JSON', ['response' => $enabledResponse]); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a valid JSON'); } if (isset($enabledResponse['enabled']) && $enabledResponse['enabled'] === true) { @@ -134,15 +134,26 @@ private function requestToExApp( $options, ); if (is_array($response) && isset($response['error'])) { - throw new RuntimeException('Error during request to ExApp (context_chat_backend): ' . $response['error']); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): ' . $response['error']); + } + if (is_array($response)) { + // this should never happen since app_api only returns errors in an array + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response is not a valid response object'); + } + if (intval($response->getStatusCode() / 100) !== 2) { + $this->logger->error('Error during request to Context Chat Backend (ExApp)', [ + 'code' => $response->getStatusCode(), + 'response' => $response->getBody() + ]); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp)'); } $resContentType = $response->getHeader('Content-Type'); if (strpos($resContentType, 'application/json') !== false) { $body = $response->getBody(); if (!is_string($body)) { - $this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a string, but content type is application/json', ['response' => $response]); - throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a string, but content type is application/json'); + $this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a string, but content type is application/json', ['response' => $response]); + throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a string, but content type is application/json'); } return json_decode($body, true); @@ -225,7 +236,7 @@ public function indexSources(array $sources): void { * @return array * @throws RuntimeException */ - public function query(string $userId, string $prompt, bool $useContext = true, ?string $scopeType = null, ?array $scopeList = null): array { + public function query(string $userId, string $prompt, bool $useContext = false, ?string $scopeType = null, ?array $scopeList = null): array { $params = [ 'query' => $prompt, 'userId' => $userId, diff --git a/lib/TaskProcessing/ContextChatProvider.php b/lib/TaskProcessing/ContextChatProvider.php index f4115b4..62abeeb 100644 --- a/lib/TaskProcessing/ContextChatProvider.php +++ b/lib/TaskProcessing/ContextChatProvider.php @@ -120,7 +120,7 @@ public function process(?string $userId, array $input, callable $reportProgress) // scoped query $scopeList = array_unique($input['scopeList']); if (count($scopeList) === 0) { - throw new \RuntimeException('No sources found'); + throw new \RuntimeException('Empty scope list provided, use unscoped query instead'); } // index sources before the query, not needed for providers @@ -136,6 +136,10 @@ public function process(?string $userId, array $input, callable $reportProgress) throw new \InvalidArgumentException('Invalid scope type'); } + if (count($processedScopes) === 0) { + throw new \RuntimeException('No supported sources found in the scope list, extend the list or use unscoped query instead'); + } + $response = $this->langRopeService->query( $userId, $input['prompt'], @@ -164,6 +168,14 @@ private function processResponse(string $userId, array $response): array { throw new \RuntimeException('Invalid response from ContextChat, expected "output" and "sources" keys: ' . json_encode($response)); } + if (count($response['sources']) === 0) { + $this->logger->info('No sources found in the response', ['response' => $response]); + return [ + 'output' => $response['output'] ?? '', + 'sources' => [], + ]; + } + $jsonSources = array_filter(array_map( fn ($source) => json_encode($source), $this->metadataService->getEnrichedSources($userId, ...$response['sources'] ?? []),