<?php
namespace App\AI\Contexts; abstract class ContextAbstract { protected $functions = []; protected $developerMessages = []; protected $cacheObj; protected $idUser; protected $idOffice; protected $idDepartment; protected $listingParams = [ 'listing[paramsSlot]' => 'AIASSISTANT', 'listing[reset]' => '1', ]; public function __construct() { if (CURRENT_ID_USER && CURRENT_ID_OFFICE && CURRENT_ID_DEPT) { $this->idUser = CURRENT_ID_USER; $this->idOffice = CURRENT_ID_OFFICE; $this->idDepartment = CURRENT_ID_DEPT; } else { $userEnt = \App\OAuth2\ResourceServerValidator::getInstance()->getUser(); $this->idUser = $userEnt->getId(); $this->idOffice = $userEnt->idOffice; $this->idDepartment = $userEnt->idDepartment; } $this->initFunctions(); $developerMessages = $this->getDeveloperMessages(); if ($developerMessages) { $this->developerMessages = $developerMessages; } } public function setCache(\Engine\Cache\BaseInterface $cacheObj) { $this->cacheObj = $cacheObj; } public function getCache() { return $this->cacheObj; } public function initFunctions() { \Engine\Loader_Cfg::getInstance()->loadCustomFile('AI/contexts.cfg.php'); $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $contextcfg = $CFG['AI']['contexts']['\\'.get_called_class()]; if ($contextcfg) { foreach ($contextcfg['functions'] as $function) { $this->functions[] = [ 'name' => $function['name'], 'description' => $function['description'], 'strict' => !!$function['strict'], ]; } } } protected function getDeveloperMessages(): array { return []; } public function getFunctions(): array { return $this->functions; } public function functionExists(string $functionName): bool { return method_exists($this, $functionName); } public function checkPermission(string $functionName): bool { $checkPermFunctionName = "checkPermission_".$functionName; if ($this->functionExists($checkPermFunctionName)) { return $this->$checkPermFunctionName(); } return false; } public function executeFunction(string $functionName, string $arguments): array { $checkPermFunctionName = "checkPermission_".$functionName; $executeFunctionName = "executeFunction_".$functionName; if (!$this->functionExists($executeFunctionName)) { throw new \Exception("Function `$executeFunctionName` does not exist in context " . get_class($this)); } if (!$this->functionExists($checkPermFunctionName)) { throw new \Exception("Function `$checkPermFunctionName` does not exist in context " . get_class($this)); } if ($this->$checkPermFunctionName()) { return $this->$executeFunctionName($arguments); } else { return [ 'error'=>'You do not have permission to perform this operation.' ]; } } public function executeMcpTool(string $funcName, array $args) : ?array { $executeFunctionName = "executeFunction_".$funcName; return $this->$executeFunctionName(json_encode($args)); } public function developerMessage(string $functionName, string $arguments, array $response): string { $developerFunctionName = "developerMessage_".$functionName; if ($this->functionExists($developerFunctionName)) { return $this->$developerFunctionName($arguments, $response); } return ''; } public function generateDeveloperMessages(): array { $developerMessages = []; foreach ($this->developerMessages as $message) { $inputObj = new \App\AI\Api\ChatGpt\Responses\Input(); $inputObj->type = "message"; $inputObj->role = "developer"; $inputObj->content = $message; $developerMessages[] = $inputObj; } return $developerMessages; } public function generateFunctionObjects(): array { $functions = []; foreach ($this->functions as $function) { $functions[] = $this->generateFunctionObject($function['name'], $function['description'], false); } return $functions; } public function generateFunctionObject(string $funcName, string $description, bool $strict) : \App\AI\Api\ChatGpt\Responses\ToolFunction { $funcClass = "generateFunctionObject_".$funcName; $funcObj = new \App\AI\Api\ChatGpt\Responses\ToolFunction( $funcName, $description, $strict ); $funcObj = $this->$funcClass($funcObj); return $funcObj; } protected function executeCronjob(string $id): void { try { $client = new \GuzzleHttp\Client(); $request = new \GuzzleHttp\Psr7\Request('GET', \Engine\Mvc\Dispatcher::getInstance()->getRawAppUrl('Admin/CronJobs/Runner', 'run', ['job'=>$id, 'executionType'=>'actionExecuteTask'])); $client->send($request, ['timeout' => 1, 'connect_timeout' => 1]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { } } } 