@@ -891,10 +891,11 @@ public function updateExpImgProcessingTime(int $runtime): void {
891891 * @param array $params Query parameters (key/val pairs)
892892 * @param string $method HTTP query method
893893 * @param string|null $contentType
894+ * @param bool $logErrors if set to false error logs will be suppressed
894895 * @return array decoded request result or error
895896 * @throws Exception
896897 */
897- public function request (?string $ userId , string $ endPoint , array $ params = [], string $ method = 'GET ' , ?string $ contentType = null ): array {
898+ public function request (?string $ userId , string $ endPoint , array $ params = [], string $ method = 'GET ' , ?string $ contentType = null , bool $ logErrors = true ): array {
898899 try {
899900 $ serviceUrl = $ this ->openAiSettingsService ->getServiceUrl ();
900901 if ($ serviceUrl === '' ) {
@@ -1000,10 +1001,12 @@ public function request(?string $userId, string $endPoint, array $params = [], s
10001001 } catch (ClientException |ServerException $ e ) {
10011002 $ responseBody = $ e ->getResponse ()->getBody ();
10021003 $ parsedResponseBody = json_decode ($ responseBody , true );
1003- if ($ e ->getResponse ()->getStatusCode () === 404 ) {
1004- $ this ->logger ->debug ('API request error : ' . $ e ->getMessage (), ['response_body ' => $ responseBody , 'exception ' => $ e ]);
1005- } else {
1006- $ this ->logger ->warning ('API request error : ' . $ e ->getMessage (), ['response_body ' => $ responseBody , 'exception ' => $ e ]);
1004+ if ($ logErrors ) {
1005+ if ($ e ->getResponse ()->getStatusCode () === 404 ) {
1006+ $ this ->logger ->debug ('API request error : ' . $ e ->getMessage (), ['response_body ' => $ responseBody , 'exception ' => $ e ]);
1007+ } else {
1008+ $ this ->logger ->warning ('API request error : ' . $ e ->getMessage (), ['response_body ' => $ responseBody , 'exception ' => $ e ]);
1009+ }
10071010 }
10081011 throw new Exception (
10091012 $ this ->l10n ->t ('API request error: ' ) . (
@@ -1019,4 +1022,86 @@ public function request(?string $userId, string $endPoint, array $params = [], s
10191022 );
10201023 }
10211024 }
1025+
1026+ /**
1027+ * Check if the T2I provider is available
1028+ *
1029+ * @return bool whether the T2I provider is available
1030+ */
1031+ public function isT2IAvailable (): bool {
1032+ if ($ this ->isUsingOpenAi ()) {
1033+ return true ;
1034+ }
1035+ try {
1036+ $ params = [
1037+ 'prompt ' => 'a ' ,
1038+ 'model ' => 'invalid-model ' ,
1039+ ];
1040+ $ this ->request (null , 'images/generations ' , $ params , 'POST ' , logErrors: false );
1041+ } catch (Exception $ e ) {
1042+ return $ e ->getCode () !== Http::STATUS_NOT_FOUND && $ e ->getCode () !== Http::STATUS_UNAUTHORIZED ;
1043+ }
1044+ return true ;
1045+ }
1046+
1047+ /**
1048+ * Check if the STT provider is available
1049+ *
1050+ * @return bool whether the STT provider is available
1051+ */
1052+ public function isSTTAvailable (): bool {
1053+ if ($ this ->isUsingOpenAi ()) {
1054+ return true ;
1055+ }
1056+ try {
1057+ $ params = [
1058+ 'model ' => 'invalid-model ' ,
1059+ 'file ' => 'a ' ,
1060+ ];
1061+ $ this ->request (null , 'audio/translations ' , $ params , 'POST ' , 'multipart/form-data ' , logErrors: false );
1062+ } catch (Exception $ e ) {
1063+ return $ e ->getCode () !== Http::STATUS_NOT_FOUND && $ e ->getCode () !== Http::STATUS_UNAUTHORIZED ;
1064+ }
1065+ return true ;
1066+ }
1067+
1068+ /**
1069+ * Check if the TTS provider is available
1070+ *
1071+ * @return bool whether the TTS provider is available
1072+ */
1073+ public function isTTSAvailable (): bool {
1074+ if ($ this ->isUsingOpenAi ()) {
1075+ return true ;
1076+ }
1077+ try {
1078+ $ params = [
1079+ 'input ' => 'a ' ,
1080+ 'voice ' => 'invalid-voice ' ,
1081+ 'model ' => 'invalid-model ' ,
1082+ 'response_format ' => 'mp3 ' ,
1083+ ];
1084+
1085+ $ this ->request (null , 'audio/speech ' , $ params , 'POST ' , logErrors: false );
1086+ } catch (Exception $ e ) {
1087+ return $ e ->getCode () !== Http::STATUS_NOT_FOUND && $ e ->getCode () !== Http::STATUS_UNAUTHORIZED ;
1088+ }
1089+ return true ;
1090+ }
1091+
1092+ /**
1093+ * Updates the admin config with the availability of the providers
1094+ *
1095+ * @return array the updated config
1096+ * @throws Exception
1097+ */
1098+ public function autoDetectFeatures (): array {
1099+ $ config = [];
1100+ $ config ['t2i_provider_enabled ' ] = $ this ->isT2IAvailable ();
1101+ $ config ['stt_provider_enabled ' ] = $ this ->isSTTAvailable ();
1102+ $ config ['tts_provider_enabled ' ] = $ this ->isTTSAvailable ();
1103+ $ this ->openAiSettingsService ->setAdminConfig ($ config );
1104+ $ config ['analyze_image_provider_enabled ' ] = $ this ->openAiSettingsService ->getAnalyzeImageProviderEnabled ();
1105+ return $ config ;
1106+ }
10221107}
0 commit comments