| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- define('BX_DOL_AI_ASSISTANT', 'assistant');
- define('BX_DOL_AI_AUTOMATOR_EVENT', 'event');
- define('BX_DOL_AI_AUTOMATOR_SCHEDULER', 'scheduler');
- define('BX_DOL_AI_AUTOMATOR_WEBHOOK', 'webhook');
- define('BX_DOL_AI_AUTOMATOR_STATUS_AUTO', 'auto');
- define('BX_DOL_AI_AUTOMATOR_STATUS_MANUAL', 'manual');
- define('BX_DOL_AI_AUTOMATOR_STATUS_READY', 'ready');
- class BxDolAI extends BxDolFactory implements iBxDolSingleton
- {
- protected $_oDb;
- protected $_iProfileId;
-
- protected $_aExcludeAlertUnits;
- protected $_sCmtsAutomators;
- protected $_sCmtsAssistantsChats;
- protected $_bWriteLog;
- protected function __construct()
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
- parent::__construct();
- $this->_oDb = new BxDolAIQuery();
- $this->_iProfileId = (int)getParam('sys_profile_bot');
- $this->_aExcludeAlertUnits = [
- 'system', 'module_template_method_call'
- ];
- $this->_sCmtsAutomators = 'sys_agents_automators';
- $this->_sCmtsAssistantsChats = 'sys_agents_assistants_chats';
- $this->_bWriteLog = true;
- }
- /**
- * Prevent cloning the instance
- */
- public function __clone()
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
- }
- /**
- * Get singleton instance of the class
- */
- public static function getInstance()
- {
- if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
- $GLOBALS['bxDolClasses'][__CLASS__] = BxDolDb::getInstance()->isTableExists('sys_agents_agents') ? new BxDolAI() : null;
- }
- return $GLOBALS['bxDolClasses'][__CLASS__];
- }
- public static function getAgentInstance(int $iId): NeuronAI\Agent\Agent
- {
- if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId]))
- return $GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId];
- $a = BxDolAIQuery::getAgentObject($iId);
- if (!$a) {
- $s = "Agent with id {$iId} not found";
- bx_log('sys_agents', $s, BX_LOG_ERR);
- throw new Exception($s);
- }
- if (!$a['active']) {
- $s = "Agent with id {$iId} isn't active";
- bx_log('sys_agents', $s, BX_LOG_ERR);
- throw new Exception($s);
- }
- if (!$a['prompt_system'] || !$a['model_id']) {
- $s = "Agent with id {$iId} can't be used because it haven't prompt or AI model";
- bx_log('sys_agents', $s, BX_LOG_ERR);
- throw new Exception($s);
- }
- $o = BxDolAiAgent::make($a);
- if ($a['vector_store_id']) {
- $aVectorStore = BxDolAIQuery::getVectorStoreObject($a['vector_store_id']);
- if ($aVectorStore && $aVectorStore['embedding_provider_id']) {
- $oEmbedder = self::getAiEmbeddingsProviderInstance($aVectorStore['embedding_provider_id']);
- $o->setEmbeddingsProvider($oEmbedder);
- }
- }
- $GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId] = $o;
- return $o;
- }
- public static function getAiProviderInstance(int $iId):NeuronAI\Providers\AIProviderInterface
- {
- return self::getModelInstance($iId);
- }
- public static function getAiEmbeddingsProviderInstance(int $iId):NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
- {
- return self::getModelInstance($iId);
- }
- public static function getModelInstance(int $iId): NeuronAI\Providers\AIProviderInterface | NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
- {
- if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId]))
- return $GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId];
- $aProvidersWithKey = ['anthropic'];
- $a = BxDolAIQuery::getModelObject($iId);
- if (!$a) {
- bx_log('sys_agents', "Agent AI Model with id {$iId} not found", BX_LOG_ERR);
- throw new Exception("Agent AI Model with id {$iId} not found");
- }
- if (in_array($a['type'], $aProvidersWithKey) && empty($a['key'])) {
- bx_log('sys_agents', "Model with id {$iId} has empty key, can't be used", BX_LOG_ERR);
- throw new Exception("Model with id {$iId} has empty key, can't be used");
- }
- if (!$a['active']) {
- bx_log('sys_agents', "Model with id {$iId} is not active, can't be used", BX_LOG_ERR);
- throw new Exception("Model with id {$iId} is not active, can't be used");
- }
- $aParameters = !empty($a['params']) ? json_decode($a['params'], true) : [];
- // replace markers {key} {model} in $aParameters recoursively
- $aParameters = bx_replace_markers($aParameters, [
- 'key' => $a['key'],
- 'model' => $a['model']
- ]);
- switch($a['type']) {
- // regular AI providers ------------------------
- case 'anthropic':
- $o = new NeuronAI\Providers\Anthropic\Anthropic(
- key: $a['key'],
- model: $a['model'],
- version: $aParameters['version'] ?? '2023-06-01',
- max_tokens: $aParameters['max_tokens'] ?? 8192,
- parameters: $aParameters['parameters'] ?? [],
- );
- break;
- case 'openai-responses':
- $o = new NeuronAI\Providers\OpenAI\Responses\OpenAIResponses(
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'azure-openai':
- $o = new NeuronAI\Providers\OpenAI\AzureOpenAI(
- key: $a['key'],
- model: $a['model'],
- endpoint: $aParameters['endpoint'],
- version: $aParameters['version'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'openai-like':
- $o = new NeuronAI\Providers\OpenAILike(
- baseUri: $aParameters['baseUri'],
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'ollama':
- $o = new NeuronAI\Providers\Ollama\Ollama(
- url: $aParameters['url'] ?? 'http://localhost:11434/api',
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- );
- break;
- case 'gemini':
- $o = new NeuronAI\Providers\Gemini\Gemini(
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- );
- break;
- case 'mistral':
- $o = new NeuronAI\Providers\Mistral\Mistral(
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'huggingface':
- $o = new NeuronAI\Providers\HuggingFace\HuggingFace(
- key: $a['key'],
- model: $a['model'],
- inferenceProvider: $aParameters['inferenceProvider'] ?? 'hf-inference/models', // cohere, groq, etc - https://github.com/neuron-core/neuron-ai/blob/3.x/src/Providers/HuggingFace/InferenceProvider.php
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'deepseek':
- $o = new NeuronAI\Providers\DeepSeek\DeepSeek(
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'grok':
- $o = new NeuronAI\Providers\XAI\Grok(
- key: $a['key'],
- model: $a['model'],
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- case 'aws-bedrock':
- $oClient = new Aws\BedrockRuntime\BedrockRuntimeClient($aParameters['client_params']);
- $o = new NeuronAI\Providers\AWS\BedrockRuntime(
- client: $oClient,
- model: $a['model'],
- inferenceConfig: $aParameters['inferenceConfig'] ?? [],
- );
- break;
- case 'cohere':
- $o = new NeuronAI\Providers\Cohere\Cohere(
- key: $a['key'],
- model: $a['model'],
- baseUri: $aParameters['baseUri'] ?? 'https://api.cohere.ai/v2',
- parameters: $aParameters['parameters'] ?? [],
- strict_response: $aParameters['strict_response'] ?? false,
- );
- break;
- // embeddings models ------------------------
- case 'ollama-embeddings':
- $o = new NeuronAI\RAG\Embeddings\OllamaEmbeddingsProvider(
- model: $a['model'],
- url: $aParameters['url'] ?? 'http://localhost:11434/api',
- parameters: $aParameters['parameters'] ?? [],
- );
- break;
- case 'voyageai-embeddings':
- $o = new NeuronAI\RAG\Embeddings\VoyageEmbeddingsProvider(
- key: $a['key'],
- model: $a['model'],
- dimensions: !empty($aParameters['dimensions']) ? (int)$aParameters['dimensions'] : null
- );
- break;
- case 'openai-embeddings':
- $o = new NeuronAI\RAG\Embeddings\OpenAIEmbeddingsProvider(
- key: $a['key'],
- model: $a['model'],
- dimensions: !empty($aParameters['dimensions']) ? (int)$aParameters['dimensions'] : 1024
- );
- break;
- case 'openai-like-embeddings':
- $o = new NeuronAI\RAG\Embeddings\OpenAILikeEmbeddings(
- baseUri: $aParameters['baseUri'],
- key: $a['key'],
- model: $a['model'],
- dimensions: !empty($aParameters['dimensions']) ? (int)$aParameters['dimensions'] : 1024
- );
- break;
- case 'aws-bedrock-embeddings':
- $oClient = new Aws\BedrockRuntime\BedrockRuntimeClient($aParameters['client_params']);
- $o = new NeuronAI\RAG\Embeddings\AwsBedrockEmbeddingsProvider(
- client: $oClient,
- model: $a['model']
- );
- break;
- default:
- bx_log('sys_agents', "Model type {$a['type']} is not supported", BX_LOG_ERR);
- throw new Exception("Model type {$a['type']} is not supported");
- }
- $GLOBALS['bxDolClasses'][__CLASS__ . '_Model_' . $iId] = $o;
- return $o;
- }
- public static function getVectorStoreInstance(int $iId): NeuronAI\RAG\VectorStore\VectorStoreInterface
- {
- if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId]))
- return $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId];
- $a = BxDolAIQuery::getVectorStoreObject($iId);
- if (!$a) {
- bx_log('sys_agents', "Vector store with id {$iId} not found", BX_LOG_ERR);
- throw new Exception("Vector store with id {$iId} not found");
- }
- $aParameters = !empty($a['params']) ? json_decode($a['params'], true) : [];
- // replace marker {topk} in $aParameters recoursively
- $aParameters = bx_replace_markers($aParameters, [
- 'topk' => $a['topk'],
- ]);
-
- // TODO: fix params
- switch($a['type']) {
- case 'file':
- $o = new NeuronAI\RAG\VectorStore\FileVectorStore(
- directory: BX_DIRECTORY_STORAGE . 'vector_stores/' . $iId,
- topK: $a['topk'] ?? 4
- );
- break;
- case 'pinecon':
- $o = new NeuronAI\RAG\VectorStore\PineconeVectorStore(
- key: $a['key'],
- indexUrl: $aParameters['indexUrl'],
- topK: $a['topk'] ?? 4,
- version: $aParameters['version'] ?? '2025-04',
- namespace: $aParameters['namespace'] ?? '__default__'
- );
- break;
- case 'elasticsearch':
- $oClient = Elastic\Elasticsearch\ClientBuilder::create()
- ->setHosts([$aParameters['client_params']['endpoint']])
- ->setApiKey($aParameters['client_params']['key'])
- ->build();
-
- $o = new NeuronAI\RAG\VectorStore\ElasticsearchVectorStore(
- client: $oClient,
- index: $aParameters['index'],
- topK: $a['topk'] ?? 4
- );
- break;
- case 'opensearch':
- $oClient = (new OpenSearch\GuzzleClientFactory())->create([
- 'base_uri' => $aParameters['client_params']['endpoint'] ?? 'http://localhost:9200',
- ]);
-
- $o = NeuronAI\RAG\VectorStore\OpenSearchVectorStore(
- client: $oClient,
- index: $aParameters['index'],
- topK: $a['topk'] ?? 4,
- );
- break;
- case 'typesense':
- $oClient = new \Typesense\Client($aParameters['client_params']);
- $o = new NeuronAI\RAG\VectorStore\TypesenseVectorStore(
- client: $oClient,
- collection: $aParameters['collection'],
- vectorDimension: $aParameters['vectorDimension'] ?? 1024,
- topK: $a['topk'] ?? 4,
- );
- break;
- case 'qdrant':
- $o = new NeuronAI\RAG\VectorStore\QdrantVectorStore(
- collectionUrl: $aParameters['collectionUrl'],
- key: $a['key'],
- topK: $a['topk'] ?? 4,
- dimension: $aParameters['dimension'] ?? 1024
- );
- break;
- case 'chromadb':
- $o = new NeuronAI\RAG\VectorStore\ChromaVectorStore(
- collection: $aParameters['collection'],
- host: $aParameters['host'] ?? 'http://localhost:8000',
- tenant: $aParameters['tenant'] ?? 'default_tenant',
- database: $aParameters['database'] ?? 'default_database',
- key: $a['key'] ?? null,
- topK: $a['topk'] ?? 4
- );
- break;
- case 'meilisearch':
- $o = new NeuronAI\RAG\VectorStore\MeilisearchVectorStore(
- indexUid: $aParameters['indexUid'],
- host: $aParameters['host'] ?? 'http://localhost:8000',
- key: $a['key'] ?? null,
- embedder: $aParameters['embedder'] ?? 'default',
- topK: $a['topk'] ?? 4,
- dimension: $aParameters['dimension'] ?? 1024
- );
- break;
- default:
- bx_log('sys_agents', "Vector store type {$a['type']} is not supported", BX_LOG_ERR);
- throw new Exception("Vector store type {$a['type']} is not supported");
- }
- $GLOBALS['bxDolClasses'][__CLASS__ . '_VectorStore_' . $iId] = $o;
- return $o;
- }
- public static function getToolInstance(int $iId): NeuronAI\Tools\ToolInterface
- {
- if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId]))
- return $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId];
- $a = BxDolAIQuery::getToolObject($iId);
- if (!$a) {
- bx_log('sys_agents', "Tool with id {$iId} not found", BX_LOG_ERR);
- throw new Exception("Tool with id {$iId} not found");
- }
- $aParameters = !empty($a['params']) ? json_decode($a['params'], true) : [];
-
- switch($a['type']) {
- case 'mysql_schema':
- $o = NeuronAI\Tools\Toolkits\MySQL\MySQLSchemaTool::make(
- pdo: BxDolDb::getInstance()->getLink(),
- tables: $aParameters['tables'] ?? null
- );
- break;
- case 'mysql_select':
- $o = NeuronAI\Tools\Toolkits\MySQL\MySQLSelectTool::make(
- pdo: BxDolDb::getInstance()->getLink(),
- );
- break;
- case 'mysql_write':
- $o = NeuronAI\Tools\Toolkits\MySQL\MySQLWriteTool::make(
- pdo: BxDolDb::getInstance()->getLink(),
- );
- break;
- default:
- bx_log('sys_agents', "Tool type {$a['type']} is not supported", BX_LOG_ERR);
- throw new Exception("Tool type {$a['type']} is not supported");
- }
- $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId] = $o;
- return $o;
- }
- public static function callHelper($mixedHelper, $sMessage)
- {
- $oAI = BxDolAI::getInstance();
- if (is_numeric($mixedHelper))
- $aHelper = $oAI->getHelperById($mixedHelper);
- else
- $aHelper = $oAI->getHelperByName($mixedHelper);
- $oAIModel = $oAI->getModelObject($aHelper['model_id']);
- return $oAIModel->getResponseText($aHelper['prompt'], $sMessage);
- }
- public static function pruning()
- {
- BxDolAIAssistant::pruning();
- }
- public static function getDefaultApiKey()
- {
- return getParam('sys_agents_api_key');
- }
- public static function getDefaultModel()
- {
- return (int)getParam('sys_agents_model');
- }
- public static function getAssistantForStudio()
- {
- return ($iId = (int)getParam('sys_agents_studio_assistant')) != 0 ? $iId : 0;
- }
- public static function getAssistantForLiveSearch()
- {
- return ($iId = (int)getParam('sys_agents_live_search_assistant')) != 0 ? $iId : 0;
- }
- public static function getAssistantForAskBlock()
- {
- return ($iId = (int)getParam('sys_agents_ask_block_assistant')) != 0 ? $iId : 0;
- }
- public function getProfileId()
- {
- return $this->_iProfileId;
- }
- public function getModels($aParams = [])
- {
- $aParamsDb = ['sample' => 'all_pairs'];
- if(isset($aParams['active']))
- $aParamsDb['active'] = $aParams['active'] === true ? 1 : 0;
- if(isset($aParams['capabilities']))
- $aParamsDb['capabilities'] = $aParams['capabilities'];
- return $aModel = $this->_oDb->getModelsBy($aParamsDb);
- }
- public function getModel($iId)
- {
- $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
- if(!empty($aModel['params']))
- $aModel['params'] = json_decode($aModel['params'], true);
- return $aModel;
- }
- public function getModelObject($iId)
- {
- if(!$iId)
- $iId = $this->getDefaultModel();
- if(!$iId)
- return false;
- return BxDolAIModel::getObjectInstance($iId);
- }
-
- public function getProviderObject($iId)
- {
- if(!$iId)
- return false;
- return BxDolAIProvider::getObjectInstance($iId);
- }
- public function getAssistants($aParams = [])
- {
- $aParamsDb = ['sample' => 'all_pairs'];
- if(isset($aParams['active']))
- $aParamsDb['active'] = $aParams['active'] === true ? 1 : 0;
- if(isset($aParams['hidden']))
- $aParamsDb['hidden'] = $aParams['hidden'] === true ? 1 : 0;
- return $aModel = $this->_oDb->getAssistantsBy($aParamsDb);
- }
- public function getAssistantById($iId)
- {
- return $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $iId]);
- }
- public function getAssistantByName($sName)
- {
- return $this->_oDb->getAssistantsBy(['sample' => 'name', 'name' => $sName]);
- }
- public function getAssistantChatById($iId)
- {
- return $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $iId]);
- }
- public function getAssistantChatsTransient($iLifetime = 0)
- {
- return $this->_oDb->getChatsBy(['sample' => 'type', 'type' => BX_DOL_AI_ASST_TYPE_TRANSIENT, 'lifetime' => $iLifetime]);
- }
- public function updateAssistantChatById($iId, $aSet)
- {
- return $this->_oDb->updateChats($aSet, ['id' => $iId]);
- }
- public function getAssistantChatCmts()
- {
- return $this->_sCmtsAssistantsChats;
- }
- public function getAssistantChatCmtsObject($iId, $oTemplate = false)
- {
- $oCmts = BxDolCmts::getObjectInstance($this->_sCmtsAssistantsChats, (int)$iId, true, $oTemplate);
- if(!$oCmts || !$oCmts->isEnabled())
- return false;
- return $oCmts;
- }
- public function getHelperById($iId)
- {
- return $this->_oDb->getHelpersBy(['sample' => 'id', 'id' => $iId]);
- }
-
- public function getHelperByName($sName)
- {
- return $this->_oDb->getHelpersBy(['sample' => 'name', 'name' => $sName]);
- }
- public function getAutomator($iId, $bFullInfo = false)
- {
- $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id' . ($bFullInfo ? '_full' : ''), 'id' => $iId]);
- if(!empty($aAutomator['params']))
- $aAutomator['params'] = json_decode($aAutomator['params'], true);
- if($bFullInfo && !empty($aAutomator['model_params']))
- $aAutomator['model_params'] = json_decode($aAutomator['model_params'], true);
- return $aAutomator;
- }
- public function getAutomatorInstruction($sType, $mixedParams = false)
- {
- $mixedResult = '';
- switch($sType) {
- case 'profile':
- $mixedResult = "\n ProfileId for system actions = " . $mixedParams;
- break;
- case 'providers':
- $aProviders = $this->_oDb->getProvidersBy(['sample' => 'ids', 'ids' => $mixedParams]);
- if(!empty($aProviders) && is_array($aProviders)) {
- $mixedResult = "\n Proividers list = [";
- foreach($aProviders as $aProvider)
- $mixedResult .= "\n {'ProviderName' => '" . $aProvider['name'] . "', 'ProviderType' => '" . $aProvider['type_name'] . "'}";
- $mixedResult .= "\n ]";
- }
- break;
- case 'helpers':
- $aHelpers = $this->_oDb->getHelpersBy(['sample' => 'ids', 'ids' => $mixedParams]);
- if(!empty($aHelpers) && is_array($aHelpers)) {
- $mixedResult = "\n Helpers list = [";
- foreach($aHelpers as $aHelper)
- $mixedResult .= "\n {'" . $aHelper['name'] . "', 'HelperDescription' => '" . $aHelper['description'] . "'}";
- $mixedResult .= "\n ]";
- }
- break;
- case 'assistants':
- $aAssistants = $this->_oDb->getAssistantsBy(['sample' => 'ids', 'ids' => $mixedParams]);
- if(!empty($aAssistants) && is_array($aAssistants)) {
- $mixedResult = "\n Assistants list = [";
- foreach($aAssistants as $aAssistant)
- $mixedResult .= "\n {'" . $aAssistant['name'] . "', 'AssistantDescription' => '" . $aAssistant['description'] . "'}";
- $mixedResult .= "\n ]";
- }
- break;
- }
- return $mixedResult;
- }
- public function getAutomatorCmts()
- {
- return $this->_sCmtsAutomators;
- }
- public function getAutomatorCmtsObject($iId, $oTemplate = false)
- {
- $oCmts = BxDolCmts::getObjectInstance($this->_sCmtsAutomators, (int)$iId, true, $oTemplate);
- if(!$oCmts || !$oCmts->isEnabled())
- return false;
- return $oCmts;
- }
- public function hasAutomators($sType, $bActive = null)
- {
- $aParams = [
- 'sample' => 'type',
- 'type' => $sType
- ];
- if($bActive !== null)
- $aParams['active'] = $bActive;
- return ($aAutomators = $this->_oDb->getAutomatorsBy($aParams)) && is_array($aAutomators);
- }
- public function getAutomatorsEvent($sUnit, $sAction)
- {
- if(in_array($sUnit, $this->_aExcludeAlertUnits))
- return [];
- return $this->_oDb->getAutomatorsBy([
- 'sample' => 'events',
- 'alert_unit' => $sUnit,
- 'alert_action' => $sAction,
- 'active' => true
- ]);
- }
- public function getAutomatorsScheduler()
- {
- $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'schedulers', 'active' => true]);
- foreach($aAutomators as &$aAutomator)
- if(!empty($aAutomator['params']))
- $aAutomator['params'] = json_decode($aAutomator['params'], true);
- return $aAutomators;
- }
- public function getAutomatorsWebhook($iProviderId)
- {
- $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'webhooks', 'provider_id' => $iProviderId, 'active' => true]);
- foreach($aAutomators as &$aAutomator)
- if(!empty($aAutomator['params']))
- $aAutomator['params'] = json_decode($aAutomator['params'], true);
- return $aAutomators;
- }
- public function callAgent($sType, $aAgent, $aParams = [])
- {
- // update sample data
- $a = ['alert' => 'alert_sample', 'webhook' => 'webhook_sample'];
- if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
- $this->updateAgentField($aAgent['id'], $a[$sType], json_encode($aParams));
- }
- $o = self::getAgentInstance($aAgent['id']);
- if (!$o)
- return false;
- $o->chat(new UserMessage($aParams));
- $o->run();
- return $o->getMessage();
- }
- public function getAgentsByAlertUnitAndAction($sUnit, $sAction)
- {
- return $this->_oDb->getAgentsByAlertUnitAndAction($sUnit, $sAction);
- }
- public function callAutomator($sType, $aParams = [])
- {
- $sMethod = '_callAutomator' . bx_gen_method_name($sType);
- if(!method_exists($this, $sMethod))
- return false;
- return $this->$sMethod($aParams);
- }
- protected function _callAutomatorEvent($aParams = [])
- {
- if(!isset($aParams['automator'], $aParams['alert']) || !is_a($aParams['alert'], 'BxDolAlerts'))
- return false;
-
- $oAlert = &$aParams['alert'];
- $this->evalCode($aParams['automator'], ['alert' => $oAlert]);
- }
- protected function _callAutomatorScheduler($aParams = [])
- {
- if(!isset($aParams['automator']))
- return false;
-
- $this->evalCode($aParams['automator']);
- }
- protected function _callAutomatorWebhook($aParams = [])
- {
- if(!isset($aParams['automator']))
- return false;
- $this->evalCode($aParams['automator']);
- }
- public function evalCode($aAutomator, $aParams = [])
- {
- try {
- $this->_evalCode($aAutomator, $aParams);
- }
- catch (Exception $oException) {
- $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
- }
- catch (Error $oError) {
- $this->log($oError->getFile() . ':' . $oError->getLine() . ' ' . $oError->getMessage());
- }
- }
- public function emulCode($aAutomator, $aParams = [])
- {
- ob_start();
- try {
- $this->_evalCode($aAutomator, $aParams);
- }
- catch (Exception $oException) {
- return $oException->getMessage();
- }
- catch (Error $oError) {
- return $oError->getMessage();
- }
- finally {
- $sOutput = ob_get_clean();
- if(!empty($sOutput))
- return $sOutput;
- }
- }
- public function log($mixedContents, $sSection = '')
- {
- if(!$this->_bWriteLog)
- return;
- if(is_array($mixedContents))
- $mixedContents = var_export($mixedContents, true);
- else if(is_object($mixedContents))
- $mixedContents = json_encode($mixedContents);
- if(empty($sSection))
- $sSection = "Core";
- bx_log('sys_agents', ":\n[" . $sSection . "] " . $mixedContents, BX_LOG_ERR);
- }
- protected function _evalCode($aAutomator, $aParams = [])
- {
- $sCode = '';
- switch($aAutomator['type']) {
- case BX_DOL_AI_AUTOMATOR_EVENT:
- $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
- break;
- case BX_DOL_AI_AUTOMATOR_SCHEDULER:
- $sCode = $aAutomator['code'] . '; onCron();';
- break;
- case BX_DOL_AI_AUTOMATOR_WEBHOOK:
- $sCode = $aAutomator['code'] . '; onHook();';
- break;
- }
- eval($sCode);
- }
- }
- class BxDolAIMessage
- {
- /**
- * @var string - message Type with following values: hb, ai
- */
- protected $_sType;
-
- /**
- * @var mixed - an array of message parts (text, image_url) or a string.
- */
- protected $_mixedContent;
-
- /**
- * @var array - an array of of files attached to the message.
- */
- protected $_aAttachments;
- public function __construct($sType)
- {
- $this->_sType = $sType;
- }
- public function isAi()
- {
- return $this->_sType == 'ai';
- }
- public function getContent()
- {
- return $this->_mixedContent;
- }
- public function getAttachments()
- {
- return $this->_aAttachments;
- }
- }
- class BxDolAIMessageString extends BxDolAIMessage
- {
- public function __construct($sType, $sContent)
- {
- parent::__construct($sType);
- $this->_mixedContent = is_string($sContent) ? $sContent : '';
- }
- }
- class BxDolAIMessageArray extends BxDolAIMessage
- {
- public function __construct($sType, $aContent = '')
- {
- parent::__construct($sType);
- $this->_mixedContent = is_array($aContent) ? $aContent : [];
- }
- public function addText($sText)
- {
- $this->_mixedContent[] = [
- 'type' => 'text',
- 'text' => $sText
- ];
- }
- public function addImageUrl($sUrl, $sDetail = 'high')
- {
- $this->_mixedContent[] = [
- 'type' => 'image_url',
- 'image_url' => [
- 'url' => $sUrl,
- 'detail' => $sDetail
- ]
- ];
- }
- public function addAttachments($mixedAttachments, $mixedTools = false)
- {
- if(!is_array($mixedAttachments))
- $mixedAttachments = [$mixedAttachments];
- if(!$mixedTools)
- $mixedTools = [['type' => 'file_search']];
- foreach($mixedAttachments as $sAttachment)
- $this->_aAttachments[] = [
- 'file_id' => $sAttachment,
- 'tools' => $mixedTools
- ];
- }
- }
- class BxDolAIMessages
- {
- /**
- * @var array - an array of items (messages)
- */
- protected $_aItems;
- public function __construct($aItems = [])
- {
- $this->_aItems = !empty($aItems) && is_array($aItems) ? $aItems : [];
- }
- public function add($sType, $mixedMessage)
- {
- $sClass = 'BxDolAIMessage' . (is_string($mixedMessage) ? 'String' : 'Array');
- $this->_aItems[] = new $sClass($sType, $mixedMessage);
- }
- public function getAll()
- {
- return $this->_aItems;
- }
- public function getLast()
- {
- return end($this->_aItems);
- }
- }
|