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

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"] ?: array()) + array("_acac1f196e0e59d3cca2" . "18a173109cfe" => base64_decode("RXJyb3I6IFRvb2wgbm90IGZvdW5" . "k"), "_b12c8bd5a6ec8f0aa9f" . "1fd9946237385" => base64_decode("QUkgYXR0ZW1wdGVkIHRvIGNhbGwgYSB0b29sLCBidXQgbm8gY29uZmlndXJhdGlvbiB3YXMgcHJvdmlkZW" . "Q="), "_a007326ef1" . "7d829cd888f" . "0d359000fbc" => base64_decode("QUkgY29udmVyc2F0aW9uIGV4Y2VlZGVkIG1heGltdW" . "0gdG9vbCBjYWxsaW5nIGRlcHRo"), "_ea8229a12" . "1285ee649a" . "72fd880bac" . "39b" => base64_decode("YXJyYXl" . "fZmlsdG" . "Vy"), "_7a753ecc3dd2" . "8f91f97a7e60b" . "ba38471" => base64_decode("RXhlY3V0aW9uIEVycm" . "9yLiBDb3VsZCBub3Qg" . "ZXhlY3V0ZSB0aGUgdG" . "9vbA=="), "_f73dab606630b38" . "8645c4f7d3fbb13b" . "b" => base64_decode("YXJyYXlfbWVyZ2U" . "="));
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["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_ea8229a121285ee649a72fd880" . "bac39b"]($_20aace3fac43f462dff3a1aed39560a7, function ($p) {
                return $p instanceof FunctionCallPart;
            });
            if (empty($_01137ccb470f015ece4033a6c233b021)) {
                return $_fd5a3a643834299cbdadfc3540ba0bcb->getResponse();
            }
            if ($config === null) {
                throw new RuntimeException($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_b12c8bd5a6ec" . "8f0aa9f1fd994" . "6237385"]);
            }
            $_9961f986823b4be7f5c4cc8379c6b228 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_f73dab606630b388645c4f7d3fbb" . "13bb"]($_9961f986823b4be7f5c4cc8379c6b228, $_20aace3fac43f462dff3a1aed39560a7);
            foreach ($_01137ccb470f015ece4033a6c233b021 as $_340e1bcbbb0667c62854ad5d47aea71a) {
                $_9961f986823b4be7f5c4cc8379c6b228[] = $this->executeTool($_340e1bcbbb0667c62854ad5d47aea71a, $config);
            }
        }
        throw new RuntimeException($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_a007326ef17d829cd888f0d3590" . "00fbc"]);
    }
    private function executeTool(FunctionCallPart $_340e1bcbbb0667c62854ad5d47aea71a, AiTextGenerationConfiguration $config) : FunctionCallPart
    {
        $_f5541d132a41089a60ef382afdec6d63 = $config->getToolByName($_340e1bcbbb0667c62854ad5d47aea71a->getName());
        if (!$_f5541d132a41089a60ef382afdec6d63) {
            return new FunctionCallPart($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_acac1f196e0e59d3cca218" . "a173109cfe"]);
        }
        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["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_7a753ecc3d" . "d28f91f97a7" . "e60bba38471"]);
        }
    }
}