<?php
namespace Claromentis\Core\Ai\Adapters\Vertex;

$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"] = (@$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"] ?: array()) + array("_e2cad529b5b" . "2b6610323420" . "10c496554" => base64_decode("L2xvY2F0aW9u" . "cy8="), "_38021ae38952751baca9f63ac2" . "bf723b" => base64_decode("LWFpcGxhdGZvcm0uZ2" . "9vZ2xlYXBpcy5jb20v" . "djE="), "_7215c118a" . "d86983602d" . "b0b431cb9a" . "1e9" => base64_decode("Z2VuZXJhdGVDb" . "250ZW50"), "_1cc5aa2f679b5" . "75f82786a73f0d" . "a28f8" => base64_decode("c3RyZWF" . "t"), "_3e258c4e61" . "63007d8094c" . "39647602e7a" => base64_decode("aGVhZGVy" . "cw=="), "_63a6ec2f20440" . "285232bed955be" . "33266" => base64_decode("c3RyZWFtR" . "2VuZXJhdG" . "VDb250ZW5" . "0"), "_7681c2a2b7e1" . "bdf69c23c6cd6" . "4114ad3" => base64_decode("anNvbl9kZWNvZGU" . "="), "_67a3498b1e8fe53ddf39e400a59bc91" . "f" => base64_decode("cmVna" . "W9uX2" . "5hbWU" . "="), "_6ee75f9f44b14f240fc54f3e19e7084" . "a" => base64_decode("UmVjZWl2ZWQgYW4gdW5leH" . "BlY3RlZCBlcnJvciBmcm9t" . "IHRoZSBzZXJ2ZXI="), "_29225c3f2cb4b5bb8cdfecad11c6" . "f491" => base64_decode("O" . "g" . "=" . "="), "_0cb031e3cd6ff" . "0075d3bccfcfdb" . "999a6" => base64_decode("LCBjb2" . "50ZW50" . "OiA="), "_86e718deafc5b7c86aa6379" . "9a9e024d0" => base64_decode("aHR0cH" . "M6Ly8="), "_bef1236f88db6b34bcb878fe4" . "8142cb6" => base64_decode("aHR0cF9lcn" . "JvcnM="), "_b673c954948" . "9ba1916c266f" . "60dc846a8" => base64_decode("cHJlZGljdA=" . "="), "_db618ca" . "8e79f97b" . "4dff753e" . "c837942c" . "f" => base64_decode("cHJvamVj" . "dF9pZA=="), "_f24852a7b2e8" . "c180d1f7ab216" . "c8e8222" => base64_decode("QmVhcm" . "VyIA=="), "_7c819f403b31525f384e33c01" . "42be9f4" => base64_decode("L3B1Ymxpc2hl" . "cnMvZ29vZ2xl" . "L21vZGVscy8="), "_47ee521cf79c60b97cda9ecd" . "9c595c95" => base64_decode("U2VydmVyIHJldHVybmVkIGFuIHVuZXhwZWN0ZWQgcmVzcG9uc2UuIEhUVF" . "AgY29kZTog"), "_54c07fa0675e" . "d31089d139eb5" . "031f815" => base64_decode("SW52YWxpZCBy" . "ZXF1ZXN0LiBI" . "VFRQIGNvZGU6" . "IA=="), "_e9181de682ee" . "bf636bae05126" . "95b2a95" => base64_decode("QXV0aG" . "9yaXph" . "dGlvbg" . "=="), "_8ec8f26c742278f90615f" . "50d682eea79" => base64_decode("UE9TVA=" . "="), "_043583fc6e4063be6ec4a31bf9" . "51616c" => base64_decode("L3Byb2pl" . "Y3RzLw=="));
use Claromentis\Core\Ai\Adapters\Vertex\Requests\Embedding\VertexAiTextEmbeddingRequest;
use Claromentis\Core\Ai\Adapters\Vertex\Requests\Image\VertexAiImageRequest;
use Claromentis\Core\Ai\Adapters\Vertex\Requests\Text\VertexAiTextRequest;
use Claromentis\Core\Ai\Adapters\Vertex\Requests\VertexAiClientAuthentication;
use Claromentis\Core\Ai\Adapters\Vertex\Responses\Image\VertexAiGenerateImageResponse;
use Claromentis\Core\Ai\Adapters\Vertex\Responses\Embedding\VertexAiGenerateTextEmbeddingResponse;
use Claromentis\Core\Ai\Adapters\Vertex\Responses\Text\VertexAiGenerateTextResponse;
use Claromentis\Core\Ai\Exceptions\AiException;
use Claromentis\Core\Ai\Exceptions\AiInvalidRequestException;
use Claromentis\Core\Ai\Exceptions\AiServerResponseException;
use Claromentis\Core\Ai\JsonParser;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Response;






class VertexAiClient implements LoggerAwareInterface
{
    use LoggerAwareTrait;
    
    
    
    
    
    
    
    
    
    public const ERROR_INVALID_ARGUMENT = Response::HTTP_BAD_REQUEST;
    public const ERROR_UNAUTHENTICATED = Response::HTTP_UNAUTHORIZED;
    public const ERROR_PERMISSION_DENIED = Response::HTTP_FORBIDDEN;
    public const ERROR_NOT_FOUND = Response::HTTP_NOT_FOUND;
    public const ERROR_RESOURCE_EXHAUSTED = Response::HTTP_TOO_MANY_REQUESTS;
    public const ERROR_CANCELLED = 499;
    
    
    public const ERROR_INTERNAL_ERROR = Response::HTTP_INTERNAL_SERVER_ERROR;
    public const ERROR_UNAVAILABLE = Response::HTTP_SERVICE_UNAVAILABLE;
    public const ERROR_DEADLINE_EXCEEDED = Response::HTTP_GATEWAY_TIMEOUT;
    
    
    
    
    private string $projectId;
    
    
    
    
    private string $regionName;
    
    
    
    
    private string $modelName;
    
    
    
    
    private VertexAiClientAuthentication $authentication;
    
    
    
    
    private ClientInterface $client;
    
    
    
    
    
    
    
    
    
    public function __construct(VertexAiClientAuthentication $_961ccc1b377e30c70011809b405b9c07, array $config, LoggerInterface $logger = null, ClientInterface $_2f664bcc4a0a32ffda2885fb12940265 = null)
    {
        $this->projectId = $config[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_db618ca8e79f97b4d" . "ff753ec837942cf"]];
        $this->regionName = $config[$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_67a3498b1e8fe53ddf39e400a59bc91" . "f"]];
        $this->authentication = $_961ccc1b377e30c70011809b405b9c07;
        $this->logger = $logger ?? new NullLogger();
        $this->client = $_2f664bcc4a0a32ffda2885fb12940265 ?? new Client();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function handleGenerateTextRequest(VertexAiTextRequest $request, ?callable $_5c51b0e4b7267b43adabf8015a8e1cf8 = null)
    {
        
        
        $this->modelName = $request->getModelName();
        $_f4bd77b9d7574792bd46db73586ce19d = $request->toArray();
        if ($request->isStreaming()) {
            $this->generateTextStream($_f4bd77b9d7574792bd46db73586ce19d, $_5c51b0e4b7267b43adabf8015a8e1cf8);
            return;
        }
        return $this->generateText($_f4bd77b9d7574792bd46db73586ce19d);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function handleTextEmbeddingRequest(VertexAiTextEmbeddingRequest $request) : VertexAiGenerateTextEmbeddingResponse
    {
        
        
        $this->modelName = $request->getModelName();
        $_f4bd77b9d7574792bd46db73586ce19d = $request->toArray();
        return $this->generateTextEmbedding($_f4bd77b9d7574792bd46db73586ce19d);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function handleGenerateImageRequest(VertexAiImageRequest $request) : VertexAiGenerateImageResponse
    {
        
        
        
        $this->modelName = $request->getModelName();
        $_f4bd77b9d7574792bd46db73586ce19d = $request->toArray();
        return $this->generateImages($_f4bd77b9d7574792bd46db73586ce19d);
    }
    
    
    
    
    
    
    
    
    
    
    
    private function generateText(array $data) : VertexAiGenerateTextResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_7215c118ad8" . "6983602db0b4" . "31cb9a1e9"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateTextResponse::fromArray($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_7681c2a2b7e1bdf69c23c6cd6411" . "4ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    
    
    
    
    private function generateTextEmbedding(array $data) : VertexAiGenerateTextEmbeddingResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_b673c9549489ba1916c266f60dc8" . "46a8"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateTextEmbeddingResponse::fromArray($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_7681c2a2b7e1bdf69c23c6cd64114a" . "d3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    
    
    
    
    private function generateImages(array $data) : VertexAiGenerateImageResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_b673c9549" . "489ba1916c" . "266f60dc84" . "6a8"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateImageResponse::fromArray($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_7681c2a2b7e1bdf69c23c6cd6" . "4114ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    private function buildUrl(string $_3bf6f785d3520dfd17a871d18d745880) : string
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_86e718deafc5b7c86a" . "a63799a9e024d0"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_38021ae389527" . "51baca9f63ac2b" . "f723b"] . $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_043583fc" . "6e4063be6" . "ec4a31bf9" . "51616c"] . $this->projectId . $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_e2cad529b5b2b6610323" . "42010c496554"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_7c819f403b31525f384e33c0142be" . "9f4"] . $this->modelName . ($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_29225c3" . "f2cb4b5b" . "b8cdfeca" . "d11c6f49" . "1"] . $_3bf6f785d3520dfd17a871d18d745880);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    private function send(string $_c927258f5322288a992c77be40ae0b53, array $data, bool $_1cc5aa2f679b575f82786a73f0da28f8 = false) : \Psr\Http\Message\StreamInterface
    {
        $_fd5a3a643834299cbdadfc3540ba0bcb = $this->client->request($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_8ec8f26c742278f90615f50d682e" . "ea79"], $_c927258f5322288a992c77be40ae0b53, [RequestOptions::JSON => $data, $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_1cc5aa2f679b575f82786a73f0da2" . "8f8"] => $_1cc5aa2f679b575f82786a73f0da28f8, $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_bef1236f88db6b34bcb878fe4" . "8142cb6"] => false, $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_3e258c4e6163007d8" . "094c39647602e7a"] => [$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_e9181de682eebf636" . "bae0512695b2a95"] => $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_f24852a7b2e8" . "c180d1f7ab216" . "c8e8222"] . $this->authentication->getAccessToken()]]);
        $this->checkResponse($_fd5a3a643834299cbdadfc3540ba0bcb);
        return $_fd5a3a643834299cbdadfc3540ba0bcb->getBody();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function generateTextStream(array $data, callable $_5c51b0e4b7267b43adabf8015a8e1cf8) : void
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_63a6ec2f20440285232be" . "d955be33266"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        $_1cc5aa2f679b575f82786a73f0da28f8 = $this->send($_c927258f5322288a992c77be40ae0b53, $data, true);
        $_c37f5d387ecde7a206c24852e173b2fe = new JsonParser(function (array $_fd62bc4eaaf7d58dea9bada3e30443b9) use($_5c51b0e4b7267b43adabf8015a8e1cf8) {
            $_5c51b0e4b7267b43adabf8015a8e1cf8(VertexAiGenerateTextResponse::fromArray($_fd62bc4eaaf7d58dea9bada3e30443b9));
        });
        while (!$_1cc5aa2f679b575f82786a73f0da28f8->eof()) {
            $_c37f5d387ecde7a206c24852e173b2fe->consume($_1cc5aa2f679b575f82786a73f0da28f8->Read(1024));
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    private function checkResponse(\Psr\Http\Message\ResponseInterface $_fd5a3a643834299cbdadfc3540ba0bcb)
    {
        switch ($_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode()) {
            
            
            
            case self::ERROR_UNAUTHENTICATED:
            case self::ERROR_PERMISSION_DENIED:
                throw AiException::invalidCredentials();
            case self::ERROR_INVALID_ARGUMENT:
                $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_54c07fa0675ed31089d139" . "eb5031f815"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode() . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_0cb031e3cd6ff0075d3bcc" . "fcfdb999a6"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getBody());
                throw new AiInvalidRequestException();
            case self::ERROR_NOT_FOUND:
            case self::ERROR_RESOURCE_EXHAUSTED:
            case self::ERROR_CANCELLED:
            case self::ERROR_DEADLINE_EXCEEDED:
            case self::ERROR_INTERNAL_ERROR:
            case self::ERROR_UNAVAILABLE:
                $this->logger->error($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_47ee521cf79c60b97cda" . "9ecd9c595c95"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode() . $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_0cb031e3cd6ff0075d3bcc" . "fcfdb999a6"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getBody());
                throw new AiServerResponseException($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_6ee75f9f" . "44b14f240" . "fc54f3e19" . "e7084a"], $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode());
        }
    }
}