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

$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"] = (@$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"] ?: array()) + array("_7c819f40" . "3b31525f3" . "84e33c014" . "2be9f4" => base64_decode("L3B1Ymxpc2hlcnMvZ29vZ2xl" . "L21vZGVscy8="), "_8ec8f26c742278f90615f50d68" . "2eea79" => base64_decode("UE" . "9T" . "VA" . "=="), "_38021ae389" . "52751baca9f" . "63ac2bf723b" => base64_decode("LWFpcGxhdGZvcm0uZ" . "29vZ2xlYXBpcy5jb2" . "0vdjE="), "_7215c118ad869836" . "02db0b431cb9a1e9" => base64_decode("Z2VuZXJhdGVDb25" . "0ZW50"), "_0cb031e3cd6ff0075d3b" . "ccfcfdb999a6" => base64_decode("LCBjb250ZW50" . "OiA="), "_b673c9549489b" . "a1916c266f60dc" . "846a8" => base64_decode("cHJlZGljdA" . "=="), "_3e258c4" . "e6163007" . "d8094c39" . "647602e7" . "a" => base64_decode("aGVhZGVycw" . "=="), "_f9544f667576ee583b77d0921529" . "7691" => base64_decode("c3RyaXB" . "vcw=="), "_7681c2a2b7e1bdf69c23c6cd64" . "114ad3" => base64_decode("anNvbl9k" . "ZWNvZGU="), "_043583fc6e" . "4063be6ec4a" . "31bf951616c" => base64_decode("L3Byb" . "2plY3" . "RzLw=" . "="), "_e9181de682eebf" . "636bae0512695b2" . "a95" => base64_decode("QXV0aG9ya" . "XphdGlvbg" . "=="), "_1cc5aa2f679b575f82786a73f0da" . "28f8" => base64_decode("c3RyZWF" . "t"), "_47ee521cf79c60b97cda9e" . "cd9c595c95" => base64_decode("U2VydmVyIHJldHVybmVkIGFuIHVuZXhwZWN0ZWQgcmVzcG9uc2UuIEhU" . "VFAgY29kZTog"), "_f24852a7b2e8c180d1f7ab21" . "6c8e8222" => base64_decode("QmV" . "hcm" . "VyI" . "A=="), "_bef1236f88db6b34bcb878fe" . "48142cb6" => base64_decode("aHR0cF9lcnJvcn" . "M="), "_db618ca8e79f9" . "7b4dff753ec837" . "942cf" => base64_decode("cHJvamVjdF9" . "pZA=="), "_8d1f2261bd4be" . "5b1dd3f1846fab" . "3db1d" => base64_decode("ZX" . "hw" . "ZW" . "N0"), "_63a6ec2f20440" . "285232bed955be" . "33266" => base64_decode("c3RyZWF" . "tR2VuZX" . "JhdGVDb" . "250ZW50"), "_67a3498" . "b1e8fe53" . "ddf39e40" . "0a59bc91" . "f" => base64_decode("cmVnaW9uX25" . "hbWU="), "_e2cad529b5b2b661032342010c4965" . "54" => base64_decode("L2xvY" . "2F0aW" . "9ucy8" . "="), "_6ee75f9f44b14f240fc54f3e19e" . "7084a" => base64_decode("UmVjZWl2ZWQgYW4gdW5leHBlY3R" . "lZCBlcnJvciBmcm9tIHRoZSBzZX" . "J2ZXI="), "_1f9da61d6bc6" . "1dd43001ae7d5" . "eacb01b" => base64_decode("UmVzcG9uc2libGUgQ" . "Uk="), "_ba3845cb79c005cf3269af8c83dd" . "be5e" => base64_decode("ZXJyb" . "3I="), "_86e718dea" . "fc5b7c86aa" . "63799a9e02" . "4d0" => base64_decode("aHR0c" . "HM6Ly" . "8="), "_29225c3f2cb4b5bb8c" . "dfecad11c6f491" => base64_decode("Og=" . "="), "_e05e0022a2" . "e5ad344b9c4" . "a0d3f64e1a5" => base64_decode("bWVz" . "c2Fn" . "ZQ=="));
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\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\Exceptions\VertexAiPromptException;
use Claromentis\Core\Ai\JsonParser;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Stream\StreamInterface;
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["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_db618ca8e79f97b4dff" . "753ec837942cf"]];
        $this->regionName = $config[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_67a3498b1e8fe53ddf39e400a59bc9" . "1f"]];
        $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->serialize();
        if ($request->isStreaming()) {
            $this->generateTextStream($_f4bd77b9d7574792bd46db73586ce19d, $_5c51b0e4b7267b43adabf8015a8e1cf8);
            return;
        }
        return $this->generateText($_f4bd77b9d7574792bd46db73586ce19d);
    }
    
    
    
    
    
    
    
    
    
    public function handleGenerateImageRequest(VertexAiImageRequest $request)
    {
        
        
        
        $this->modelName = $request->getModelName();
        $_f4bd77b9d7574792bd46db73586ce19d = $request->serialize();
        return $this->generateImages($_f4bd77b9d7574792bd46db73586ce19d);
    }
    
    
    
    
    
    
    
    private function generateText(array $data) : VertexAiGenerateTextResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_7215c118a" . "d86983602d" . "b0b431cb9a" . "1e9"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildURL($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateTextResponse::fromArray($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_7681c2a2b7e1bdf69c23c6cd64114" . "ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true), $this->modelName);
    }
    
    
    
    
    
    
    
    private function generateImages(array $data) : VertexAiGenerateImageResponse
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_b673c9549489ba1916c266f60dc846a" . "8"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildURL($_3bf6f785d3520dfd17a871d18d745880);
        return VertexAiGenerateImageResponse::fromArray($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_7681c2a2b7e1bdf69c23c6" . "cd64114ad3"]($this->send($_c927258f5322288a992c77be40ae0b53, $data), true));
    }
    
    
    
    
    
    
    
    public function buildURL(string $_3bf6f785d3520dfd17a871d18d745880) : string
    {
        return $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_86e718deafc5b7c86aa63799a9e024d" . "0"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_38021ae38952751bac" . "a9f63ac2bf723b"] . $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_043583fc6" . "e4063be6ec" . "4a31bf9516" . "16c"] . $this->projectId . $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_e2cad529b5b2b661032342010c496" . "554"] . $this->regionName . $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_7c819f403b" . "31525f384e3" . "3c0142be9f4"] . $this->modelName . ($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_29225c3f2cb4b5bb8" . "cdfecad11c6f491"] . $_3bf6f785d3520dfd17a871d18d745880);
    }
    
    
    
    
    
    
    
    
    
    
    
    private function send(string $_c927258f5322288a992c77be40ae0b53, array $data, bool $_1cc5aa2f679b575f82786a73f0da28f8 = false)
    {
        $_fd5a3a643834299cbdadfc3540ba0bcb = $this->client->request($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_8ec8f26c742" . "278f90615f50" . "d682eea79"], $_c927258f5322288a992c77be40ae0b53, [
            RequestOptions::JSON => $data,
            $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_1cc5aa2f679b575f82786a" . "73f0da28f8"] => $_1cc5aa2f679b575f82786a73f0da28f8,
            $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_bef1236f88db6b3" . "4bcb878fe48142cb" . "6"] => false,
            
            
            
            $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_8d1f2261bd4be5b" . "1dd3f1846fab3db1" . "d"] => false,
            $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_3e258c4e616300" . "7d8094c39647602" . "e7a"] => [$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_e9181de682eebf636b" . "ae0512695b2a95"] => $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_f24852a7b2e8c1" . "80d1f7ab216c8e8" . "222"] . $this->authentication->getAccessToken()],
        ]);
        $this->checkResponse($_fd5a3a643834299cbdadfc3540ba0bcb);
        return $_fd5a3a643834299cbdadfc3540ba0bcb->getBody();
    }
    
    
    
    
    
    
    
    
    private function generateTextStream(array $data, callable $_5c51b0e4b7267b43adabf8015a8e1cf8) : void
    {
        $_3bf6f785d3520dfd17a871d18d745880 = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_63a6ec2f20440285232bed955be332" . "66"];
        $_c927258f5322288a992c77be40ae0b53 = $this->buildURL($_3bf6f785d3520dfd17a871d18d745880);
        $_1cc5aa2f679b575f82786a73f0da28f8 = $this->send($_c927258f5322288a992c77be40ae0b53, $data, true);
        $_c37f5d387ecde7a206c24852e173b2fe = new JsonParser(function (array $_fd62bc4eaaf7d58dea9bada3e30443b9) use($_5c51b0e4b7267b43adabf8015a8e1cf8) {
            $_5c51b0e4b7267b43adabf8015a8e1cf8(VertexAiGenerateTextResponse::fromArray($_fd62bc4eaaf7d58dea9bada3e30443b9, $this->modelName));
        });
        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:
                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["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_47ee521cf79c60b97cd" . "a9ecd9c595c95"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode() . $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_0cb031e3cd6ff0075d3bccf" . "cfdb999a6"] . $_fd5a3a643834299cbdadfc3540ba0bcb->getBody());
                throw new AiServerResponseException($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_6ee75f9f44b14f2" . "40fc54f3e19e7084" . "a"], $_fd5a3a643834299cbdadfc3540ba0bcb->getStatusCode());
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    private function resolveInvalidArgumentException(\Psr\Http\Message\ResponseInterface $_fd5a3a643834299cbdadfc3540ba0bcb) : \Throwable
    {
        $_ea3fdc88953ddf579b784716d51d8dc4 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_7681c2a2" . "b7e1bdf69" . "c23c6cd64" . "114ad3"]((string) $_fd5a3a643834299cbdadfc3540ba0bcb->getBody(), true) ?? [];
        $_ba3845cb79c005cf3269af8c83ddbe5e = $_ea3fdc88953ddf579b784716d51d8dc4[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_ba3845cb79c005cf3269af" . "8c83ddbe5e"]] ?? [];
        if (isset($_ba3845cb79c005cf3269af8c83ddbe5e[$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_e05e0022a2e5ad344b9c4a" . "0d3f64e1a5"]]) && $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f9544f6" . "67576ee5" . "83b77d09" . "21529769" . "1"]($_ba3845cb79c005cf3269af8c83ddbe5e[$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_e05e0022a2e5" . "ad344b9c4a0d3" . "f64e1a5"]], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_1f9da61d6bc61dd43001ae" . "7d5eacb01b"]) !== false) {
            return new AiContentFilterException();
        }
        return new AiInvalidRequestException();
    }
}