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

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] ?: array()) + array("_f9544f667576ee583b77d09" . "215297691" => base64_decode("c3RyaXBvc" . "w=="), "_e05e0022a2e5ad3" . "44b9c4a0d3f64e1a" . "5" => base64_decode("bWV" . "zc2" . "FnZ" . "Q=="), "_63a6ec2" . "f2044028" . "5232bed9" . "55be3326" . "6" => base64_decode("c3RyZWFtR2V" . "uZXJhdGVDb2" . "50ZW50"), "_b673c9549489" . "ba1916c266f60" . "dc846a8" => base64_decode("cHJlZ" . "GljdA" . "=="), "_043583fc6e" . "4063be6ec4a" . "31bf951616c" => base64_decode("L3Byb2p" . "lY3RzLw" . "=="), "_1f9da61d6bc61" . "dd43001ae7d5ea" . "cb01b" => base64_decode("UmVzcG9uc2" . "libGUgQUk="), "_8d1f2261bd4be5b1dd3f1846" . "fab3db1d" => base64_decode("ZXhwZW" . "N0"), "_54c07fa0675ed31089d139eb5031f81" . "5" => base64_decode("SW52YWxpZCByZXF1ZXN0LiBIVFRQI" . "GNvZGU6IA=="), "_6ee75f9f44b14f240fc54f3e19e7084" . "a" => base64_decode("UmVjZWl2ZWQgYW4gd" . "W5leHBlY3RlZCBlcn" . "JvciBmcm9tIHRoZSB" . "zZXJ2ZXI="), "_67a3498b1e8fe53ddf3" . "9e400a59bc91f" => base64_decode("cmVnaW9" . "uX25hbW" . "U="), "_47ee521cf79c60" . "b97cda9ecd9c595" . "c95" => base64_decode("U2VydmVyIHJldHVybmVkIGFuIHVuZX" . "hwZWN0ZWQgcmVzcG9uc2UuIEhUVFAg" . "Y29kZTog"), "_f24852a7b2e8c180d1f7" . "ab216c8e8222" => base64_decode("QmVhcmV" . "yIA=="), "_7681c2a2b7e1bdf69c23c6cd64114ad" . "3" => base64_decode("anNvb" . "l9kZW" . "NvZGU" . "="), "_7215c118ad86983602db0b43" . "1cb9a1e9" => base64_decode("Z2VuZXJh" . "dGVDb250" . "ZW50"), "_8ec8f26c742278f" . "90615f50d682eea7" . "9" => base64_decode("UE9TV" . "A=="), "_86e718deaf" . "c5b7c86aa63" . "799a9e024d0" => base64_decode("aHR0cHM6L" . "y8="), "_29225c3f2cb4b5bb8cdfecad11c6f" . "491" => base64_decode("Og" . "=="), "_1cc5aa2f6" . "79b575f827" . "86a73f0da2" . "8f8" => base64_decode("c3" . "Ry" . "ZW" . "Ft"), "_db618ca8e79f97b" . "4dff753ec837942c" . "f" => base64_decode("cHJvamV" . "jdF9pZA" . "=="), "_bef1236f88db6b34bcb878fe4" . "8142cb6" => base64_decode("aHR0cF9lcnJvcnM" . "="), "_e9181de682" . "eebf636bae0" . "512695b2a95" => base64_decode("QXV0aG9yaXphdGlvbg" . "=="), "_ba3845cb79c00" . "5cf3269af8c83d" . "dbe5e" => base64_decode("ZXJyb" . "3I="), "_e2cad529b5b2b661" . "032342010c496554" => base64_decode("L2xvY2F" . "0aW9ucy" . "8="), "_0cb031e3cd6ff0075d" . "3bccfcfdb999a6" => base64_decode("LCBj" . "b250" . "ZW50" . "OiA="), "_3e258c4e6163007d809" . "4c39647602e7a" => base64_decode("aGVhZGVycw" . "=="), "_38021ae38952751ba" . "ca9f63ac2bf723b" => base64_decode("LWFpcGxhdGZvcm0uZ29vZ2x" . "lYXBpcy5jb20vdjE="), "_7c819f403b31525f384e33" . "c0142be9f4" => base64_decode("L3B1Ymxpc2hlcnM" . "vZ29vZ2xlL21vZG" . "Vscy8="));
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\AiContentFilterException;
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["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_db618ca8e" . "79f97b4dff" . "753ec83794" . "2cf"]];
        $this->regionName = $config[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_67a3498b1e8fe53ddf39e400a" . "59bc91f"]];
        $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["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_7215c118ad86983602db0b431cb" . "9a1e9"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateTextResponse::fromArray($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_7681c2a2" . "b7e1bdf69" . "c23c6cd64" . "114ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    
    
    
    
    private function generateTextEmbedding(array $data) : VertexAiGenerateTextEmbeddingResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_b673c95" . "49489ba1" . "916c266f" . "60dc846a" . "8"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildUrl($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateTextEmbeddingResponse::fromArray($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_7681c2a2b7e1bdf6" . "9c23c6cd64114ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    
    
    
    
    private function generateImages(array $data) : VertexAiGenerateImageResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_b673c9549489ba1916c266f60dc" . "846a8"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildURL($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateImageResponse::fromArray($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_7681c2a2b7e1bdf69c23c6" . "cd64114ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    private function buildUrl(string $_3bf6f785d3520dfd17a871d18d745880) : string
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_86e718deafc5" . "b7c86aa63799a" . "9e024d0"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_38021ae38952751baca9f63ac2bf" . "723b"] . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_043583fc6e" . "4063be6ec4a" . "31bf951616c"] . $this->projectId . $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_e2cad529b5b2b661032342010c4965" . "54"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_7c819f403b31525f384e3" . "3c0142be9f4"] . $this->modelName . ($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_29225c3f2" . "cb4b5bb8cd" . "fecad11c6f" . "491"] . $_3bf6f785d3520dfd17a871d18d745880);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    private function send(string $_c927258f5322288a992c77be40ae0b53, array $data, bool $_1cc5aa2f679b575f82786a73f0da28f8 = false) : \Psr\Http\Message\StreamInterface
    {
        $_fd5a3a643834299cbdadfc3540ba0bcb = $this->client->request($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8ec8f26" . "c742278f" . "90615f50" . "d682eea7" . "9"], $_c927258f5322288a992c77be40ae0b53, [
            RequestOptions::JSON => $data,
            $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_1cc5aa2f679b575f82786a73f0da28" . "f8"] => $_1cc5aa2f679b575f82786a73f0da28f8,
            $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_bef1236f88db6" . "b34bcb878fe481" . "42cb6"] => false,
            
            
            
            $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_8d1f2261bd4b" . "e5b1dd3f1846f" . "ab3db1d"] => false,
            $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_3e258c4e6163007d8094" . "c39647602e7a"] => [$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_e9181de68" . "2eebf636ba" . "e0512695b2" . "a95"] => $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f24852a7b2e8c180d1f7" . "ab216c8e8222"] . $this->authentication->getAccessToken()],
        ]);
        $this->checkResponse($_fd5a3a643834299cbdadfc3540ba0bcb);
        return $_fd5a3a643834299cbdadfc3540ba0bcb->getBody();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    private function generateTextStream(array $data, callable $_5c51b0e4b7267b43adabf8015a8e1cf8) : void
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_63a6ec2f20440285232bed9" . "55be33266"];
        $_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["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_54c07fa0675ed31089d139eb" . "5031f815"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode() . $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_0cb031e3cd6ff0075d3bc" . "cfcfdb999a6"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getBody());
                throw $this->resolveInvalidArgumentException($_fd5a3a643834299cbdadfc3540ba0bcb);
            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["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_47ee521cf79c60b97cda9ecd9c595c9" . "5"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode() . $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_0cb031e3cd6ff0075d3bccfcfd" . "b999a6"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getBody());
                throw new AiServerResponseException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_6ee75f9f44b1" . "4f240fc54f3e1" . "9e7084a"], $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode());
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    private function resolveInvalidArgumentException(\Psr\Http\Message\ResponseInterface $_fd5a3a643834299cbdadfc3540ba0bcb) : \Throwable
    {
        $_ea3fdc88953ddf579b784716d51d8dc4 = $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_7681c2a2b7e1b" . "df69c23c6cd641" . "14ad3"]((string) $_fd5a3a643834299cbdadfc3540ba0bcb->getBody(), true) ?? [];
        $_ba3845cb79c005cf3269af8c83ddbe5e = $_ea3fdc88953ddf579b784716d51d8dc4[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_ba3845cb79c005cf3269af8c83" . "ddbe5e"]] ?? [];
        if (isset($_ba3845cb79c005cf3269af8c83ddbe5e[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_e05e0022a2e5ad344b9c4a0d3f64e" . "1a5"]]) && $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f9544f667576ee583" . "b77d09215297691"]($_ba3845cb79c005cf3269af8c83ddbe5e[$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_e05e0022a2" . "e5ad344b9c4" . "a0d3f64e1a5"]], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_1f9da61d6bc61dd43001" . "ae7d5eacb01b"]) !== false) {
            return new AiContentFilterException();
        }
        return new AiInvalidRequestException();
    }
}