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

$GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"] ?: array()) + array("_acac1f196e0e59d3" . "cca218a173109cfe" => base64_decode("RXJyb3I6IFRvb2wgbm90IGZvd" . "W5k"), "_b12c8bd5a6ec" . "8f0aa9f1fd994" . "6237385" => base64_decode("QUkgYXR0ZW1wdGVkIHRvIGNhbGwgYSB0" . "b29sLCBidXQgbm8gY29uZmlndXJhdGlv" . "biB3YXMgcHJvdmlkZWQ="), "_f73dab606630b388645" . "c4f7d3fbb13bb" => base64_decode("YXJyYXlfbWVyZ2U" . "="), "_ea8229a12128" . "5ee649a72fd88" . "0bac39b" => base64_decode("YXJyY" . "XlfZm" . "lsdGV" . "y"), "_a007326ef17d829cd888f" . "0d359000fbc" => base64_decode("QUkgY29udmVyc2F0aW9uIGV4Y2VlZGVkIG1heGltdW" . "0gdG9vbCBjYWxsaW5nIGRlcHRo"), "_7a753ecc3dd" . "28f91f97a7e6" . "0bba38471" => base64_decode("RXhlY3V0aW9uIEVyc" . "m9yLiBDb3VsZCBub3" . "QgZXhlY3V0ZSB0aGU" . "gdG9vbA=="));
use Claromentis\Core\Ai\AiResponseInterface;
use Claromentis\Core\Ai\AiTextGenerationConfiguration;
use Claromentis\Core\Ai\Parts\FunctionCallPart;
use Claromentis\Core\Ai\Parts\PartInterface;
use RuntimeException;
class AiToolInvoker
{
    public function invoke(callable $_978ff3ecc92c5d222904161650f08e07, array $_9961f986823b4be7f5c4cc8379c6b228, ?AiTextGenerationConfiguration $config) : string
    {
        $_c2fe869bcefc5bf6b130ff49f98ba974 = 5;
        for ($_644606ca74c2c5245559d17aac5ed202 = 0; $_644606ca74c2c5245559d17aac5ed202 < $_c2fe869bcefc5bf6b130ff49f98ba974; $_644606ca74c2c5245559d17aac5ed202++) {
            
            
            
            
            $_fd5a3a643834299cbdadfc3540ba0bcb = $_978ff3ecc92c5d222904161650f08e07($_9961f986823b4be7f5c4cc8379c6b228);
            $_20aace3fac43f462dff3a1aed39560a7 = $_fd5a3a643834299cbdadfc3540ba0bcb->getParts();
            $_01137ccb470f015ece4033a6c233b021 = $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_ea8229a121285ee649a72fd880bac3" . "9b"]($_20aace3fac43f462dff3a1aed39560a7, function ($p) {
                return $p instanceof FunctionCallPart;
            });
            if (empty($_01137ccb470f015ece4033a6c233b021)) {
                return $_fd5a3a643834299cbdadfc3540ba0bcb->getResponse();
            }
            if ($config === null) {
                throw new RuntimeException($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_b12c8bd5a6ec8" . "f0aa9f1fd99462" . "37385"]);
            }
            $_9961f986823b4be7f5c4cc8379c6b228 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_f73dab606630b388645c4f7d3fbb13" . "bb"]($_9961f986823b4be7f5c4cc8379c6b228, $_20aace3fac43f462dff3a1aed39560a7);
            foreach ($_01137ccb470f015ece4033a6c233b021 as $_340e1bcbbb0667c62854ad5d47aea71a) {
                $_9961f986823b4be7f5c4cc8379c6b228[] = $this->executeTool($_340e1bcbbb0667c62854ad5d47aea71a, $config);
            }
        }
        throw new RuntimeException($GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_a007326ef17d829cd" . "888f0d359000fbc"]);
    }
    private function executeTool(FunctionCallPart $_340e1bcbbb0667c62854ad5d47aea71a, AiTextGenerationConfiguration $config) : FunctionCallPart
    {
        $_f5541d132a41089a60ef382afdec6d63 = $config->getToolByName($_340e1bcbbb0667c62854ad5d47aea71a->getName());
        if (!$_f5541d132a41089a60ef382afdec6d63) {
            return new FunctionCallPart($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_acac1f196e0e59d3cca21" . "8a173109cfe"]);
        }
        try {
            
            
            
            $_1f24efd4735b7a11b391147435e7d35d = $_f5541d132a41089a60ef382afdec6d63->execute($_340e1bcbbb0667c62854ad5d47aea71a->getArgs());
            return new FunctionCallPart($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $_1f24efd4735b7a11b391147435e7d35d, PartInterface::PART_ROLE_USER, $_340e1bcbbb0667c62854ad5d47aea71a->getMetadata());
        } catch (\Exception $_fe441aa3ef350bfb1a1a1ad8520dd530) {
            
            
            return new FunctionCallPart($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_7a753ecc3dd28f91f97a7e60bba" . "38471"]);
        }
    }
}