<?php
namespace Claromentis\Core\Ai\Controller;

$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"] = (@$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"] ?: array()) + array("da" . "ta" => base64_decode("ZG" . "F0" . "YQ" . "=="), "langua" . "ge" => base64_decode("bGFuZ3VhZ" . "2U="), "_bd71c3b5c201fc11db82ed14ce424d" . "e5" => base64_decode("aW5f" . "YXJy" . "YXk="), "_54faabe694dfa8d0" . "5dc6c0827a214f82" => base64_decode("aW1h" . "Z2U="), "_cfab5a4a68" . "04604f4998a" . "7244eba86bd" => base64_decode("Cgo" . "="), "_475c4ddf787" . "3ea4e9044df1" . "0ae31d01e" => base64_decode("QUkgaW1hZ2UgZ2VuZXJhdGlvbiBpcyBkaXNhYmxlZA" . "=="), "st" . "at" . "us" => base64_decode("c3R" . "hdH" . "Vz"), "_b2ffc45fa7bfe7aac72" . "914a57d0f9162" => base64_decode("SW52YWxp" . "ZCBtb2R1" . "bGUgJw=="), "_839809c6" . "beb4465bd" . "21e037814" . "ee4683" => base64_decode("Y29tbW9uLmFpLmludmFsaWRfcH" . "JvbXB0"), "_d1071a032215f0" . "eb5c4a8ef175f70" . "49d" => base64_decode("cm9s" . "ZQ=="), "_f083878fb61180fb" . "f68ba3a77b8d7de3" => base64_decode("aW1hZ" . "2Vz"), "_ea8229a12128" . "5ee649a72fd88" . "0bac39b" => base64_decode("YXJyYXlfZmlsdGV" . "y"), "_4d707fab93bebb00bbe69e81596" . "97c90" => base64_decode(""), "_967e161ed500fcf11d56175ec79897" . "dc" => base64_decode("QUkgaXMgbm90I" . "GVuYWJsZWQgZm" . "9yIGFueSBtb2R" . "1bGVz"), "_d962cfd17" . "35adb2dfba" . "46f69c7f15" . "cb8" => base64_decode("cGF" . "ydA" . "=="), "_c2900f14e63513f00ce80ed" . "bb045506c" => base64_decode("b2Jf" . "Zmx1" . "c2g="), "_a7442cd7a4d3c557bf" . "6b6b4b0d724559" => base64_decode("cHJvb" . "XB0"), "_1ab2691b0149e6c0f5bef2" . "e034d68c11" => base64_decode("JywgJw=" . "="), "_2e384775c6" . "854f15e613f" . "8da88db608d" => base64_decode("Y2ZnX2FpX2VuYWJsZWRfb" . "W9kdWxlcw=="), "_5159b3572969ec" . "a46acd1b2e14184" . "59c" => base64_decode("aW1wb" . "G9kZQ" . "=="), "_f73dab606630b388645c4f7d3" . "fbb13bb" => base64_decode("YXJyYXlfbWV" . "yZ2U="), "_eac6791bbe0decdc57613078" . "38b277de" => base64_decode("Y29tbW9uLmFpLnVuZXhwZWN0ZWRfcm" . "VzcG9uc2U="), "_df34c069de" . "43011ade22c" . "720a3318f60" => base64_decode("aGlzd" . "G9yeV" . "td"), "_1128df95be7b7" . "ba48e931c0662b" . "158c1" => base64_decode("Zm" . "x1" . "c2" . "g="), "mo" . "du" . "le" => base64_decode("bW9" . "kdW" . "xl"), "_655a561f4e34bb" . "dfa745dc1339347" . "1b3" => base64_decode("bWltZV90eX" . "Bl"), "_e956b2bd945b" . "591c54805c958" . "2d15a3b" => base64_decode("b3B0aW" . "9uc1td"), "_321258308ef09879" . "cd184188c6cd2744" => base64_decode("dGVybWluY" . "XRlZA=="), "_e05e0022a2e5ad344b9c4a" . "0d3f64e1a5" => base64_decode("bWVz" . "c2Fn" . "ZQ=="), "_7aefd482de7885f4293" . "c2926df28c235" => base64_decode("anNvbl9lbmNv" . "ZGU="), "te" . "xt" => base64_decode("dG" . "V4" . "dA" . "=="), "_e608e5b5c4c34a6" . "7ba97ef84c1e1619" . "9" => base64_decode("Jw=" . "="), "_a1b32522d3c9a793e0e" . "4307466b172d9" => base64_decode("ZGF0Y" . "Tog"), "_8dacf526543365460" . "ea3a88a3a0904a1" => base64_decode("JzsgdmFsaWQgbW9kdWxl" . "cyBhcmUg"), "_7669a84" . "31d2368b" . "1dc09581" . "496aea68" . "e" => base64_decode("Zmlu" . "aXNo"), "_38f12d8b8c71" . "9a8ce0fc2faef" . "c225f35" => base64_decode("YXJyYX" . "lfbWFw"), "_9a122dd9e0255ca418d47" . "3b4ba68f14b" => base64_decode("bWV0YWRh" . "dGFbXQ=="));
use Claromentis\Core\Ai\Adapters\Vertex\Responses\Text\VertexAiGenerateTextResponse;
use Claromentis\Core\Ai\Ai;
use Claromentis\Core\Ai\AiImageConfiguration;
use Claromentis\Core\Ai\AiPluginInterface;
use Claromentis\Core\Ai\Exceptions\AiInvalidRequestException;
use Claromentis\Core\Ai\Exceptions\AiInvalidResponseException;
use Claromentis\Core\Ai\Exceptions\AiServerResponseException;
use Claromentis\Core\Ai\Parts\FilePart;
use Claromentis\Core\Ai\Parts\PartInterface;
use Claromentis\Core\Ai\Parts\TextPart;
use Claromentis\Core\Config\Config;
use Claromentis\Core\Http\JsonPrettyResponse;
use Claromentis\Core\Http\RequestData;
use Claromentis\Core\Http\ResponseFactory;
use Claromentis\Core\Localization\Lmsg;
use Claromentis\Core\TextUtil\ClaText;
use Parsedown;
use RestExBadRequest;
use RestExError;
use Symfony\Component\HttpFoundation\Response;
use Throwable;




class RestAiController
{
    
    
    
    
    private Ai $ai;
    
    
    
    
    protected Config $config;
    
    
    
    
    private ResponseFactory $responseFactory;
    
    
    
    
    private ClaText $claText;
    
    
    
    
    private Lmsg $lmsg;
    
    
    
    
    
    
    
    
    public function __construct(Ai $ai, Config $config, ResponseFactory $responseFactory, ClaText $cla_text, Lmsg $lmsg)
    {
        $this->ai = $ai;
        $this->config = $config;
        $this->responseFactory = $responseFactory;
        $this->claText = $cla_text;
        $this->lmsg = $lmsg;
    }
    
    
    
    
    
    
    
    public function generateTextStream(RequestData $requestData) : Response
    {
        $_a7442cd7a4d3c557bf6b6b4b0d724559 = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_a7442cd7a4d3c557bf6b6b4b0d" . "724559"]);
        $module = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["modu" . "le"], $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_4d707fab93bebb00bbe69e8159697c" . "90"]);
        $language = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["la" . "ng" . "ua" . "ge"]);
        $this->validateModule($module);
        $_a7442cd7a4d3c557bf6b6b4b0d724559 = $this->initializePrompt($module, AiPluginInterface::TYPE_TEXT_GENERATION, $language, $_a7442cd7a4d3c557bf6b6b4b0d724559);
        $_f9aef021fb8e50005cc6aa3a9d7e2117 = $this->responseFactory->GetEventStreamResponse(function () use($_a7442cd7a4d3c557bf6b6b4b0d724559) {
            $_e696d5598f94156853fd8b977b8c344c = new Parsedown();
            $_77ab044ee5ed2012c760c0fbb7bd34f9 = $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_4d707fab93beb" . "b00bbe69e81596" . "97c90"];
            $this->ai->generateTextStream(function (VertexAiGenerateTextResponse $_fd5a3a643834299cbdadfc3540ba0bcb) use($_e696d5598f94156853fd8b977b8c344c, &$_77ab044ee5ed2012c760c0fbb7bd34f9) {
                if ($_fd5a3a643834299cbdadfc3540ba0bcb->hasTerminated()) {
                    $this->flush($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_321258308ef098" . "79cd184188c6cd2" . "744"]);
                } else {
                    try {
                        $_77ab044ee5ed2012c760c0fbb7bd34f9 .= $_fd5a3a643834299cbdadfc3540ba0bcb->getGeneratedText();
                    } catch (Throwable $throwable) {
                        $this->handleException($throwable);
                    }
                    $this->flush($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_e05e0022a2e5ad34" . "4b9c4a0d3f64e1a5"], $this->claText->SanitizeHtml($_e696d5598f94156853fd8b977b8c344c->text($_77ab044ee5ed2012c760c0fbb7bd34f9)));
                }
            }, $_a7442cd7a4d3c557bf6b6b4b0d724559);
            $this->flush($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_7669a8431d2368b1dc0958149" . "6aea68e"]);
        });
        return $_f9aef021fb8e50005cc6aa3a9d7e2117;
    }
    
    
    
    
    
    
    
    
    
    public function generateText(RequestData $requestData) : Response
    {
        $_a7442cd7a4d3c557bf6b6b4b0d724559 = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_a7442cd7a4d3c557bf6b6b4" . "b0d724559"]);
        $module = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["mo" . "du" . "le"], $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_4d707fab9" . "3bebb00bbe" . "69e8159697" . "c90"]);
        $language = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["languag" . "e"]);
        $this->validateModule($module);
        $_a7442cd7a4d3c557bf6b6b4b0d724559 = $this->initializePrompt($module, AiPluginInterface::TYPE_TEXT_GENERATION, $language, $_a7442cd7a4d3c557bf6b6b4b0d724559);
        try {
            $_fd5a3a643834299cbdadfc3540ba0bcb = $this->ai->generateText($_a7442cd7a4d3c557bf6b6b4b0d724559);
        } catch (Throwable $throwable) {
            $this->handleException($throwable);
        }
        return $this->responseFactory->GetJsonPrettyResponse([$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["t" . "e" . "x" . "t"] => $this->claText->SanitizeHtml($_fd5a3a643834299cbdadfc3540ba0bcb)]);
    }
    
    
    
    
    
    
    
    public function generateImages(RequestData $requestData) : Response
    {
        $_a7442cd7a4d3c557bf6b6b4b0d724559 = $requestData->get($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_a7442cd7a4d3c557bf6b6b" . "4b0d724559"]);
        $options = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_e956b2bd945b591c54805c9582d1" . "5a3b"]);
        if (!($this->config->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_2e384775c6854f1" . "5e613f8da88db608" . "d"])[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_54faabe694dfa8d05dc" . "6c0827a214f82"]] ?? false)) {
            throw new RestExBadRequest($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_475c4ddf7873ea4e90" . "44df10ae31d01e"]);
        }
        try {
            $_f083878fb61180fbf68ba3a77b8d7de3 = $this->ai->generateImages($_a7442cd7a4d3c557bf6b6b4b0d724559, new AiImageConfiguration($options ?? []));
        } catch (Throwable $throwable) {
            $this->handleException($throwable);
        }
        return $this->responseFactory->GetJsonPrettyResponse([$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_f083878fb61" . "180fbf68ba3a" . "77b8d7de3"] => $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_38f12d8b8c719a8ce0fc2" . "faefc225f35"](function ($_54faabe694dfa8d05dc6c0827a214f82) {
            return [$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_655a561f4e34b" . "bdfa745dc13393" . "471b3"] => $_54faabe694dfa8d05dc6c0827a214f82->getMimeType(), $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["dat" . "a"] => $_54faabe694dfa8d05dc6c0827a214f82->getValue()];
        }, $_f083878fb61180fbf68ba3a77b8d7de3)]);
    }
    
    
    
    
    
    
    
    public function generateChat(RequestData $requestData) : JsonPrettyResponse
    {
        $_a82e7c9cba13670954fbe01d604c242a = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_df34c069de430" . "11ade22c720a33" . "18f60"], []);
        $module = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["modu" . "le"], $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_4d707fab93be" . "bb00bbe69e815" . "9697c90"]);
        $language = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["langu" . "age"]);
        $metadata = $requestData->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_9a122dd9e0255" . "ca418d473b4ba6" . "8f14b"], []);
        $this->validateModule($module);
        $_6f77ebd0332e42368c37efd859b74de7 = $this->ai->getInitializationPrompt($module, AiPluginInterface::TYPE_CHAT_GENERATION, $language, $metadata);
        $_9961f986823b4be7f5c4cc8379c6b228 = $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_38f12d8b8c7" . "19a8ce0fc2fa" . "efc225f35"](function ($_9258ac851c9bc3180b251966de4a02bc) {
            if (isset($_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["dat" . "a"]], $_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_655a561f4e34bbdfa745" . "dc13393471b3"]], $_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_d1071a032215f0eb5c4a8ef175f" . "7049d"]])) {
                return new FilePart($_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["d" . "a" . "t" . "a"]], $_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_655a561f4e34bbdfa" . "745dc13393471b3"]], $_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_d1071a032215f0eb5c4a" . "8ef175f7049d"]]);
            }
            return new TextPart($_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["t" . "e" . "x" . "t"]], $_9258ac851c9bc3180b251966de4a02bc[$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_d1071a032215f0eb5c4a8ef175f7049" . "d"]]);
        }, $_a82e7c9cba13670954fbe01d604c242a);
        if ($_6f77ebd0332e42368c37efd859b74de7) {
            $_9961f986823b4be7f5c4cc8379c6b228 = $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_f73dab6066" . "30b388645c4" . "f7d3fbb13bb"]($_6f77ebd0332e42368c37efd859b74de7, $_9961f986823b4be7f5c4cc8379c6b228);
        }
        try {
            $text = $this->ai->generateChat($_9961f986823b4be7f5c4cc8379c6b228);
        } catch (Throwable $throwable) {
            $this->handleException($throwable);
        }
        $_e696d5598f94156853fd8b977b8c344c = new Parsedown();
        return $this->responseFactory->GetJsonPrettyResponse([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_d962cfd1735adb2dfba46f69c7f15cb" . "8"] => [$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_d1071a032215f0eb5c4a" . "8ef175f7049d"] => PartInterface::PART_ROLE_MODEL, $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["te" . "xt"] => $this->claText->SanitizeHtml($_e696d5598f94156853fd8b977b8c344c->text($text))]]);
    }
    
    
    
    
    
    
    
    
    
    
    private function initializePrompt(string $module, string $pluginType, ?string $languageCode = null, ?string $userPrompt = null, ?array $metadata = null) : ?array
    {
        $_cec480cefdb389e4790714f70ba7809c = $this->ai->getInitializationPrompt($module, $pluginType, $languageCode, $metadata);
        return $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_ea8229a121285ee649a72fd8" . "80bac39b"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_f73dab60663" . "0b388645c4f7" . "d3fbb13bb"]($_cec480cefdb389e4790714f70ba7809c, [new TextPart($userPrompt)]), function ($_59cdb5ce92fcf760d09d3c6ed5b03aaa) {
            return $_59cdb5ce92fcf760d09d3c6ed5b03aaa !== null && !empty($_59cdb5ce92fcf760d09d3c6ed5b03aaa->getValue());
        });
    }
    
    
    
    
    
    
    
    
    private function flush(string $status, string $text = '')
    {
        echo $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_a1b32522d3c9a793" . "e0e4307466b172d9"] . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_7aefd48" . "2de7885f" . "4293c292" . "6df28c23" . "5"]([$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["sta" . "tus"] => $status, $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["t" . "e" . "x" . "t"] => $text]) . $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_cfab5a4a6804604f4998a7244eba8" . "6bd"];
        $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_c2900f14e63513f00ce80edbb04" . "5506c"]();
        $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_1128df95be7b7b" . "a48e931c0662b15" . "8c1"]();
    }
    
    
    
    
    
    
    
    
    
    
    private function handleException($throwable) : void
    {
        if ($throwable instanceof AiInvalidRequestException) {
            throw new RestExBadRequest(($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_839809c6beb44" . "65bd21e037814e" . "e4683"]), $throwable->getCode(), $throwable);
        } elseif ($throwable instanceof AiInvalidResponseException || $throwable instanceof AiServerResponseException) {
            throw new RestExError(($this->lmsg)($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_eac6791bbe0decdc5761307838" . "b277de"], $throwable->getCode()), $throwable->getCode(), $throwable);
        }
        throw new RestExError($throwable->getMessage(), $throwable->getCode(), $throwable);
    }
    
    
    
    
    
    
    private function validateModule(string $module) : void
    {
        $_7e868a0f1e76721d67a3c57a1406d916 = $this->ai->getModules();
        if (empty($_7e868a0f1e76721d67a3c57a1406d916)) {
            throw new RestExBadRequest($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_967e161ed500f" . "cf11d56175ec79" . "897dc"]);
        }
        if (!$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_bd71c3b5c201fc11" . "db82ed14ce424de5"]($module, $_7e868a0f1e76721d67a3c57a1406d916)) {
            $_de3bcb12af58b547f987c780da4b95d3 = $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_e608e5b5c4c34a" . "67ba97ef84c1e16" . "199"] . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_5159b3572969ec" . "a46acd1b2e14184" . "59c"]($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_1ab2691b0149e6c0f5bef2e" . "034d68c11"], $_7e868a0f1e76721d67a3c57a1406d916) . $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_e608e5b5c4c34a67ba97ef84c1e16" . "199"];
            throw new RestExBadRequest($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_b2ffc45fa7bfe7aac72914a5" . "7d0f9162"] . $module . $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_8dacf526543" . "365460ea3a88" . "a3a0904a1"] . $_de3bcb12af58b547f987c780da4b95d3);
        }
    }
}