Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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;
Expand Down