<?php
namespace Claromentis\Core\Ai;

$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] ?: array()) + array("_ea8229a121285ee64" . "9a72fd880bac39b" => base64_decode("YXJyYXlfZm" . "lsdGVy"), "_bd71c3b5c201fc11db82e" . "d14ce424de5" => base64_decode("aW5fYXJyY" . "Xk="), "_e65d9ebe03c7b68f03597f9bf0f297d" . "8" => base64_decode("RWFjaCBlbGVtZW50IG11c3QgYmUgYW4gaW5zdGFuY2" . "Ugb2Yg"), "_24124167882dd54c5fd27989982de70" . "7" => base64_decode("QUkgcGx1Z2lu" . "cyBtdXN0IGlt" . "cGxlbWVudCA="), "_075c6c4172eaf58cfa84828a3582" . "d19a" => base64_decode("YXJyYXlfdW5pc" . "XVl"), "_a70b8baf1a135547a271002" . "00b31ba5e" => base64_decode("c29yd" . "A=="), "_2e384775c6854f15e613f8da" . "88db608d" => base64_decode("Y2ZnX2FpX2VuYWJsZWRfbW9kdW" . "xlcw=="), "_49c6565477ee" . "9f822b499a53e" . "6d2995f" => base64_decode("YXJyYXlfa2V5X2V4aX" . "N0cw=="), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_34725a20914b07a28de7045ef89ffe" . "6e" => base64_decode("SW52YWxp" . "ZCBwbHVn" . "aW4gdHlw" . "ZQ=="), "_4298577e41894bcce23f896cd3" . "65aea4" => base64_decode("Z" . "W" . "4" . "="), "_4d707fab93bebb0" . "0bbe69e8159697c9" . "0" => base64_decode(""));
use Claromentis\Core\Ai\Events\AiEvent;
use Claromentis\Core\Ai\Events\AiEventType;
use Claromentis\Core\Ai\Parts\PartInterface;
use Claromentis\Core\Ai\Tools\AiToolInvoker;
use Claromentis\Core\Ai\Util\Util;
use Claromentis\Core\Config\Config;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webmozart\Assert\Assert;




class Ai
{
    
    
    
    
    protected AiAdapterInterface $adapter;
    protected Config $config;
    
    
    
    
    protected EventDispatcherInterface $dispatcher;
    
    
    
    
    
    
    protected array $plugins;
    private AiToolInvoker $invoker;
    
    
    
    
    
    
    
    public function __construct(AiAdapterInterface $_fff71005fefc432e322a63468ff7fd0c, Config $config, EventDispatcherInterface $_b497ce6f2af10f67516aed0ecf34d8cf, array $_a99e51514fd0fcca58579cb5552a6035 = [])
    {
        Assert::allIsInstanceOf($_a99e51514fd0fcca58579cb5552a6035, AiPluginInterface::class, $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_24124167882dd54c5f" . "d27989982de707"] . AiPluginInterface::class);
        $this->adapter = $_fff71005fefc432e322a63468ff7fd0c;
        $this->config = $config;
        $this->dispatcher = $_b497ce6f2af10f67516aed0ecf34d8cf;
        $this->plugins = $_a99e51514fd0fcca58579cb5552a6035;
        $this->invoker = new AiToolInvoker();
    }
    public function generateText($_9961f986823b4be7f5c4cc8379c6b228, ?AiTextGenerationConfiguration $_91cc42f5289e2e63eeb6b3317a65abbd = null) : string
    {
        $this->dispatchEvent(AiEventType::TEXT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228);
        return $this->invoker->invoke(function ($_876a795d28af608a9e0d1966e7a11739) use($_91cc42f5289e2e63eeb6b3317a65abbd) {
            return $this->adapter->generateText($_876a795d28af608a9e0d1966e7a11739, $_91cc42f5289e2e63eeb6b3317a65abbd);
        }, Util::ingestTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228), $_91cc42f5289e2e63eeb6b3317a65abbd);
        return $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_4d707fa" . "b93bebb0" . "0bbe69e8" . "159697c9" . "0"];
    }
    public function generateChat(array $_9961f986823b4be7f5c4cc8379c6b228, ?string $_dd7bdbcd8b58669a07aa0241bcad827b = null, ?AiTextGenerationConfiguration $_91cc42f5289e2e63eeb6b3317a65abbd = null) : string
    {
        Assert::allIsInstanceOf($_9961f986823b4be7f5c4cc8379c6b228, PartInterface::class, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_e65d9ebe03c7b68f03597f9b" . "f0f297d8"] . PartInterface::class);
        $event = $this->dispatchEvent(AiEventType::CHAT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228, $_dd7bdbcd8b58669a07aa0241bcad827b);
        return $this->invoker->invoke(function ($_876a795d28af608a9e0d1966e7a11739) use($_91cc42f5289e2e63eeb6b3317a65abbd, $_dd7bdbcd8b58669a07aa0241bcad827b) {
            $_fd5a3a643834299cbdadfc3540ba0bcb = $this->adapter->generateChat($_876a795d28af608a9e0d1966e7a11739, $_91cc42f5289e2e63eeb6b3317a65abbd);
            $this->dispatchEvent(AiEventType::CHAT_PROMPT, $_fd5a3a643834299cbdadfc3540ba0bcb->getParts(), $_dd7bdbcd8b58669a07aa0241bcad827b);
            return $_fd5a3a643834299cbdadfc3540ba0bcb;
        }, Util::ingestTextGenerationParts($event->getParts()), $_91cc42f5289e2e63eeb6b3317a65abbd);
    }
    public function generateTextStream(callable $_5c51b0e4b7267b43adabf8015a8e1cf8, $_9961f986823b4be7f5c4cc8379c6b228) : void
    {
        Util::assertTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228);
        $this->dispatchEvent(AiEventType::TEXT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228);
        $this->adapter->generateTextStream($_5c51b0e4b7267b43adabf8015a8e1cf8, $_9961f986823b4be7f5c4cc8379c6b228);
    }
    public function generateImages(string $_a51236f5e7f12cf84a776152098d24a9, ?AiImageConfiguration $_96a90e63b3679cfbc97e4f9bc15d4d2a = null) : array
    {
        $this->dispatchEvent(AiEventType::IMAGE_PROMPT, $_a51236f5e7f12cf84a776152098d24a9);
        return $this->adapter->generateImages($_a51236f5e7f12cf84a776152098d24a9, $_96a90e63b3679cfbc97e4f9bc15d4d2a);
    }
    public function generateTextEmbedding($_9961f986823b4be7f5c4cc8379c6b228) : array
    {
        Util::assertTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228);
        return $this->adapter->generateTextEmbedding($_9961f986823b4be7f5c4cc8379c6b228);
    }
    
    
    
    
    
    
    
    public function isModuleEnabled(string $module) : bool
    {
        return $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_49c6565477ee9f822b" . "499a53e6d2995f"]($module, $this->config[$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_2e384775c6854f15e613f8d" . "a88db608d"]]) && $this->config[$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_2e384775c6854" . "f15e613f8da88d" . "b608d"]][$module];
    }
    
    
    
    
    
    
    public function getModules()
    {
        $_59eea0afb98cc6c83c49d730362a45fd = [];
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            $_59eea0afb98cc6c83c49d730362a45fd[] = $_38198dbb7190fed0f875632ecef1ac6f->getModule();
        }
        $_59eea0afb98cc6c83c49d730362a45fd = $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_075c6c4172eaf58cfa84828a3582" . "d19a"]($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_ea8229a121285ee649a72fd880" . "bac39b"]($_59eea0afb98cc6c83c49d730362a45fd));
        $GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_a70b8baf1a135547a27100200b31ba" . "5e"]($_59eea0afb98cc6c83c49d730362a45fd);
        return $_59eea0afb98cc6c83c49d730362a45fd;
    }
    
    
    
    
    
    
    
    public function getModulePlugins(string $module) : array
    {
        $_a99e51514fd0fcca58579cb5552a6035 = [];
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            if ($_38198dbb7190fed0f875632ecef1ac6f->getModule() === $module) {
                $_a99e51514fd0fcca58579cb5552a6035[] = $_38198dbb7190fed0f875632ecef1ac6f;
            }
        }
        return $_a99e51514fd0fcca58579cb5552a6035;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function getInitializationParts(AiPluginInterface $_38198dbb7190fed0f875632ecef1ac6f, ?string $_a05cd0d81b0d993868f75f877961db96 = null, ?array $_b255ebafd5b8bb45110cd2db0604fa44 = null) : array
    {
        
        
        $_a05cd0d81b0d993868f75f877961db96 = $this->getSupportedLanguage($_a05cd0d81b0d993868f75f877961db96);
        return $_38198dbb7190fed0f875632ecef1ac6f->getParts($_a05cd0d81b0d993868f75f877961db96, $_b255ebafd5b8bb45110cd2db0604fa44);
    }
    
    
    
    
    
    
    
    
    public function getModule(string $module, string $_2236a44cb0a254e187e94523cbb4ccbb) : ?AiPluginInterface
    {
        if (!$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_bd71c3b5c201f" . "c11db82ed14ce4" . "24de5"]($_2236a44cb0a254e187e94523cbb4ccbb, AiPluginInterface::TYPES)) {
            throw new \InvalidArgumentException($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_34725a20914b07a28de704" . "5ef89ffe6e"]);
        }
        $_38198dbb7190fed0f875632ecef1ac6f = null;
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            if ($_38198dbb7190fed0f875632ecef1ac6f->getModule() === $module && $_38198dbb7190fed0f875632ecef1ac6f->getType() === $_2236a44cb0a254e187e94523cbb4ccbb) {
                return $_38198dbb7190fed0f875632ecef1ac6f;
            }
        }
        return $_38198dbb7190fed0f875632ecef1ac6f;
    }
    
    
    
    
    
    
    
    
    
    
    
    protected function getSupportedLanguage(?string $language) : string
    {
        $language = \Languages::DetectLanguage($language);
        $_3a9ee8592d2799f4e3ba31426430d858 = $this->isLanguageSupported($language);
        return $_3a9ee8592d2799f4e3ba31426430d858 ? $language : $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_4298577e41" . "894bcce23f8" . "96cd365aea4"];
    }
    
    
    
    
    
    
    
    public function isLanguageSupported(string $language) : bool
    {
        return $this->adapter->isLanguageSupported($language);
    }
    
    
    
    
    
    
    
    
    public function getInformation() : AiInformation
    {
        return $this->adapter->getInformation();
    }
    
    
    
    
    
    
    
    
    
    protected function dispatchEvent(string $_18c3ae9a896c5ec6d2e49d0d9d3852f2, $_9961f986823b4be7f5c4cc8379c6b228, ?string $_dd7bdbcd8b58669a07aa0241bcad827b = null) : AiEvent
    {
        $event = new AiEvent($_9961f986823b4be7f5c4cc8379c6b228);
        $event->setArgument($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["i" . "d"], $_dd7bdbcd8b58669a07aa0241bcad827b);
        return $this->dispatcher->dispatch($event, $_18c3ae9a896c5ec6d2e49d0d9d3852f2);
    }
}