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

$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"] ?: array()) + array("_ea8229a1212" . "85ee649a72fd" . "880bac39b" => base64_decode("YXJyYX" . "lfZmls" . "dGVy"), "_a007326ef17d829cd8" . "88f0d359000fbc" => base64_decode("QUkgY29udmVyc2F0aW9" . "uIGV4Y2VlZGVkIG1heG" . "ltdW0gdG9vbCBjYWxsa" . "W5nIGRlcHRo"), "_acac1f196e0" . "e59d3cca218a" . "173109cfe" => base64_decode("RXJyb3I6IFRvb2wgbm90IGZv" . "dW5k"), "_eab1120b01f30694dc930b0781e" . "8f708" => base64_decode("RXJyb3I6I" . "A=="), "_b12c8bd5a6ec8f0aa9f1fd99" . "46237385" => base64_decode("QUkgYXR0ZW1wdGVkIHRvIGNhbGwgYSB0b29sLCBidXQgbm8gY29uZml" . "ndXJhdGlvbiB3YXMgcHJvdmlkZWQ="));
use Claromentis\Core\Ai\AiResponseInterface;
use Claromentis\Core\Ai\AiTextGenerationConfiguration;
use Claromentis\Core\Ai\Parts\FunctionCallPart;
use Claromentis\Core\Ai\Parts\FunctionCallResponse;
use Claromentis\Core\Ai\Parts\PartInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
class AiToolInvoker
{
    use LoggerAwareTrait;
    public function __construct(?LoggerInterface $logger)
    {
        $this->setLogger($logger ?? new NullLogger());
    }
    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["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_ea8229a121285ee649a72" . "fd880bac39b"]($_20aace3fac43f462dff3a1aed39560a7, fn($p) => $p instanceof FunctionCallPart);
            if (empty($_01137ccb470f015ece4033a6c233b021)) {
                return $_fd5a3a643834299cbdadfc3540ba0bcb->getResponse();
            }
            if ($config === null) {
                throw new RuntimeException($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_b12c8bd5a6ec8f0aa9f1fd99462373" . "85"]);
            }
            $_9961f986823b4be7f5c4cc8379c6b228 = [...$_9961f986823b4be7f5c4cc8379c6b228, ...$_20aace3fac43f462dff3a1aed39560a7];
            foreach ($_01137ccb470f015ece4033a6c233b021 as $_340e1bcbbb0667c62854ad5d47aea71a) {
                $_9961f986823b4be7f5c4cc8379c6b228[] = $this->executeTool($_340e1bcbbb0667c62854ad5d47aea71a, $config);
            }
        }
        throw new RuntimeException($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_a007326ef17d829cd888f0d35" . "9000fbc"]);
    }
    private function executeTool(FunctionCallPart $_340e1bcbbb0667c62854ad5d47aea71a, AiTextGenerationConfiguration $config) : FunctionCallResponse
    {
        $_f5541d132a41089a60ef382afdec6d63 = $config->getToolByName($_340e1bcbbb0667c62854ad5d47aea71a->getName());
        if (!$_f5541d132a41089a60ef382afdec6d63) {
            return new FunctionCallResponse($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_acac1f196e0e59d3cca218a1" . "73109cfe"]);
        }
        try {
            $args = $_340e1bcbbb0667c62854ad5d47aea71a->getArgs();
            $_1f24efd4735b7a11b391147435e7d35d = $_f5541d132a41089a60ef382afdec6d63->execute($args);
            return new FunctionCallResponse($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $_1f24efd4735b7a11b391147435e7d35d, $_340e1bcbbb0667c62854ad5d47aea71a->getMetadata());
        } catch (\Exception $_fe441aa3ef350bfb1a1a1ad8520dd530) {
            
            
            return new FunctionCallResponse($_340e1bcbbb0667c62854ad5d47aea71a->getName(), $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_eab1120b01f306" . "94dc930b0781e8f" . "708"] . $_fe441aa3ef350bfb1a1a1ad8520dd530->getMessage());
        }
    }
}