diff --git a/lib/Service/OpenAiAPIService.php b/lib/Service/OpenAiAPIService.php index 050d82d6..50e1fd2f 100644 --- a/lib/Service/OpenAiAPIService.php +++ b/lib/Service/OpenAiAPIService.php @@ -72,6 +72,26 @@ public function getServiceName(): string { } } + /** + * @param mixed $models + * @return boolean + */ + private function isModelListValid($models): bool { + if (!is_array($models) || !array_is_list($models)) { + return false; + } + if (count($models) === 0) { + return false; + } + foreach ($models as $model) { + if (!isset($model['id'])) { + return false; + } + } + + return true; + } + /** * @param string $userId * @return array|string[] @@ -111,11 +131,22 @@ public function getModels(string $userId): array { $this->areCredsValid = false; throw $e; } - if (!isset($modelsResponse['data'])) { + if (isset($modelsResponse['error'])) { $this->logger->warning('Error retrieving models: ' . \json_encode($modelsResponse)); $this->areCredsValid = false; - throw new Exception($this->l10n->t('Unknown models error'), Http::STATUS_INTERNAL_SERVER_ERROR); + throw new Exception($modelsResponse['error'], Http::STATUS_INTERNAL_SERVER_ERROR); + } + if (!isset($modelsResponse['data'])) { + // also consider responses without 'data' as valid + $modelsResponse = ['data' => $modelsResponse]; } + + if (!$this->isModelListValid($modelsResponse['data'])) { + $this->logger->warning('Invalid models response: ' . \json_encode($modelsResponse)); + $this->areCredsValid = false; + throw new Exception($this->l10n->t('Invalid models response received'), Http::STATUS_INTERNAL_SERVER_ERROR); + } + $cache->set($cacheKey, $modelsResponse, Application::MODELS_CACHE_TTL); $this->modelsMemoryCache = $modelsResponse; $this->areCredsValid = true;