| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- <?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
- * @{
- */
- class BxDolAIQuery extends BxDolDb
- {
- public function __construct()
- {
- parent::__construct();
- }
- static protected function _getObject(int $iId, string $sTable): array|bool
- {
- $oDb = BxDolDb::getInstance();
- $a = $oDb->getRow("SELECT * FROM `$sTable` WHERE `id` = :id", ['id' => $iId]);
- if(!$a || !is_array($a))
- return false;
- return $a;
- }
- static public function getModelObject(int $iId): array|bool
- {
- return self::_getObject($iId, 'sys_agents_models');
- }
- static public function getVectorStoreObject(int $iId): array|bool
- {
- return self::_getObject($iId, 'sys_agents_vector_store');
- }
- static public function getAgentObject(int $iId): array|bool
- {
- return self::_getObject($iId, 'sys_agents_agents');
- }
- static public function getToolObject(int $iId): array|bool
- {
- return self::_getObject($iId, 'sys_agents_tools');
- }
- static public function getProviderObject(int $iId)
- {
- $oDb = BxDolDb::getInstance();
- $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
- if(!$aProvider || !is_array($aProvider))
- return false;
- // get type
- $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
- if(!$aProviderType || !is_array($aProviderType))
- return false;
- $aProvider['type'] = $aProviderType;
- // get options
- $sQuery = "SELECT
- `tpo`.`name` AS `name`,
- `tpv`.`value` AS `value`
- FROM `sys_agents_provider_options` AS `tpo`
- LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
- WHERE 1 ORDER BY `tpo`.`order`";
- $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
- 'provider_id' => $iId
- ]);
- return $aProvider;
- }
- static public function getProviderIdByName($sName)
- {
- return (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `sys_agents_providers` WHERE `name`=:name LIMIT 1", [
- 'name' => $sName
- ]);
- }
-
- static public function getAssistantObject($iId)
- {
- $oDb = BxDolDb::getInstance();
- $aAssistant = $oDb->getRow("SELECT * FROM `sys_agents_assistants` WHERE `id` = :id", ['id' => $iId]);
- if(!$aAssistant || !is_array($aAssistant))
- return false;
- return $aAssistant;
- }
- public function insertModel($aModel)
- {
- if(empty($aModel))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_models` SET " . $this->arrayToSQL($aModel));
- }
- public function getModelsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'title';
- $aMethod['params'][3] = [];
- if(isset($aParams['active'])) {
- $aMethod['params'][3]['active'] = $aParams['active'];
- $sWhereClause .= " AND `active`=:active";
- }
- if(isset($aParams['capabilities'])) {
- if (is_array($aParams['capabilities']))
- {
- $sWhereClause .= " AND `capabilities` IN (" . $this->implode_escape($aParams['capabilities']) . ")";
- }
- else {
- $aMethod['params'][3]['capabilities'] = $aParams['capabilities'];
- $sWhereClause .= " AND `capabilities` = :capabilities";
- }
- }
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_models`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getAutomatorsBy($aParams = [], $bFromCache = true)
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`taa`.*";
- $sPostfix = $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `taa`.`id`=:id";
- break;
- case 'id_full':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
- $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
- $sWhereClause .= " AND `taa`.`id`=:id";
- break;
- case 'type':
- $sPostfix .= '_' . $aParams['type'];
- $aMethod['params'][1] = [
- 'type' => $aParams['type']
- ];
- $sWhereClause .= " AND `taa`.`type`=:type";
- if(isset($aParams['active'])) {
- $iActive = (int)$aParams['active'];
- $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
- $aMethod['params'][1]['active'] = $iActive;
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
- case 'events':
- $aBindings = [
- 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
- 'alert_unit' => $aParams['alert_unit'],
- 'alert_action' => $aParams['alert_action']
- ];
- $sPostfix .= '_' . implode('_', $aBindings);
- $aMethod['params'][1] = $aBindings;
- $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
- if(isset($aParams['active'])) {
- $iActive = (int)$aParams['active'];
- $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
- $aMethod['params'][1]['active'] = $iActive;
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
- case 'schedulers':
- $aMethod['params'][1] = [
- 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
- ];
- $sWhereClause .= " AND `taa`.`type`=:type";
- if(isset($aParams['active'])) {
- $aMethod['params'][1]['active'] = (int)$aParams['active'];
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
- case 'webhooks':
- $aMethod['params'][1] = [
- 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
- ];
- $sWhereClause .= " AND `taa`.`type`=:type";
- if(isset($aParams['provider_id'])) {
- $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
- $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
- $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
- }
- if(isset($aParams['active'])) {
- $aMethod['params'][1]['active'] = (int)$aParams['active'];
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
- case 'providers_by_id_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'provider_id';
- $aMethod['params'][3] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`taap`.`id`, `taap`.`provider_id`";
- $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `taap` ON `taa`.`id`=`taap`.`automator_id`";
- $sWhereClause = " AND `taa`.`id`=:id";
- break;
-
- case 'helpers_by_id_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'helper_id';
- $aMethod['params'][3] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`taah`.`id`, `taah`.`helper_id`";
- $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `taah` ON `taa`.`id`=`taah`.`automator_id`";
- $sWhereClause = " AND `taa`.`id`=:id";
- break;
-
- case 'assistants_by_id_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'assistant_id';
- $aMethod['params'][3] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`taaa`.`id`, `taaa`.`assistant_id`";
- $sJoinClause = "INNER JOIN `sys_agents_automators_assistants` AS `taaa` ON `taa`.`id`=`taaa`.`automator_id`";
- $sWhereClause = " AND `taa`.`id`=:id";
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- if(!$bFromCache || empty($sPostfix))
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- return call_user_func_array([$this, 'fromMemory'], array_merge(['sys_automators' . $sPostfix, $aMethod['name']], $aMethod['params']));
- }
- public function updateAutomators($aSetClause, $aWhereClause)
- {
- if(empty($aSetClause) || empty($aWhereClause))
- return false;
- return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
- }
- public function insertAutomatorProvider($aParamsSet)
- {
- if(empty($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet) || empty($aParamsWhere))
- return false;
- return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
- }
- public function deleteAutomatorProviders($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
- }
-
- public function deleteAutomatorProvidersById($mixedId)
- {
- if(!is_array($mixedId))
- $mixedId = [$mixedId];
- return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
- }
- public function insertAutomatorHelper($aParamsSet)
- {
- if(empty($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet) || empty($aParamsWhere))
- return false;
- return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
- }
- public function deleteAutomatorHelpers($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
- }
- public function deleteAutomatorHelpersById($mixedId)
- {
- if(!is_array($mixedId))
- $mixedId = [$mixedId];
- return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
- }
- public function insertAutomatorAssistant($aParamsSet)
- {
- if(empty($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateAutomatorAssistant($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet) || empty($aParamsWhere))
- return false;
- return $this->query("UPDATE `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
- }
- public function deleteAutomatorAssistants($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
- }
-
- public function deleteAutomatorAssistantsById($mixedId)
- {
- if(!is_array($mixedId))
- $mixedId = [$mixedId];
- return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
- }
- public function getProviderTypesBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'name' => $aParams['name']
- ];
- $sWhereClause .= " AND `name`=:name";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'title';
- if(isset($aParams['active'])) {
- $aMethod['params'][3] = [
- 'active' => $aParams['active']
- ];
- $sWhereClause = " AND `active`=:active";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_provider_types`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getProviderOptionsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'provider_type_id':
- $aMethod['params'][1] = [
- 'provider_type_id' => $aParams['provider_type_id']
- ];
- $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_provider_options`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getProvidersBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`tp`.*";
- $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause = " AND `tp`.`id`=:id";
- break;
- case 'ids':
- $sSelectClause .= ", `tpt`.`name` AS `type_name`";
- $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
- $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
- break;
-
- case 'options_by_id':
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
- $sJoinClause = "LEFT JOIN `sys_agents_provider_options` AS `tpo` ON `tp`.`type_id`=`tpo`.`provider_type_id` LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tp`.`id`=`tpv`.`provider_id` AND `tpo`.`id`=`tpv`.`option_id`";
- $sWhereClause = " AND `tp`.`id`=:id";
- break;
-
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'name';
- if(isset($aParams['active'])) {
- $aMethod['params'][3] = [
- 'active' => $aParams['active']
- ];
- $sWhereClause = " AND `tp`.`active`=:active";
- }
- break;
- }
- $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
- $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
- $aMethod['params'][0] = "SELECT
- " . $sSelectClause . "
- FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
- WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- public function insertProviderValue($aParamsSet)
- {
- if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
- 'value' => $aParamsSet['value']
- ]) > 0 ? (int)$this->lastId() : false;
- }
- public function updateProviderValue($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet))
- return false;
- $sWhereClause = "1";
- if(!empty($aParamsWhere) && is_array($aParamsWhere))
- $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
- return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
- }
- public function deleteProviderValues($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
- }
-
- public function getHelpersBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`th`.*";
- $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `th`.`id`=:id";
- break;
- case 'name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'name' => $aParams['name']
- ];
- $sWhereClause .= " AND `th`.`name`=:name";
- break;
- case 'ids':
- $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'name';
- if(isset($aParams['active'])) {
- $aMethod['params'][3] = [
- 'active' => $aParams['active']
- ];
- $sWhereClause = " AND `th`.`active`=:active";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getAssistantsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`ta`.*";
- $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `ta`.`id`=:id";
- break;
- case 'name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'name' => $aParams['name']
- ];
- $sWhereClause .= " AND `ta`.`name`=:name";
- break;
- case 'ids':
- $sWhereClause = " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'name';
- $aMethod['params'][3] = [];
- if(isset($aParams['active'])) {
- $aMethod['params'][3]['active'] = $aParams['active'];
- $sWhereClause .= " AND `ta`.`active`=:active";
- }
- if(isset($aParams['hidden'])) {
- $aMethod['params'][3]['hidden'] = $aParams['hidden'];
- $sWhereClause .= " AND `ta`.`hidden`=:hidden";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_assistants` AS `ta` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
-
- public function updateAssistants($aSetClause, $aWhereClause)
- {
- if(empty($aSetClause) || empty($aWhereClause))
- return false;
- return (int)$this->query("UPDATE `sys_agents_assistants` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
- }
- public function getChatsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`tac`.*";
- $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `tac`.`id`=:id";
- break;
- case 'name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'name' => $aParams['name']
- ];
- $sWhereClause .= " AND `tac`.`name`=:name";
- break;
- case 'assistant_id':
- $aMethod['params'][1] = [
- 'assistant_id' => $aParams['assistant_id']
- ];
- $sWhereClause .= " AND `tac`.`assistant_id`=:assistant_id";
- break;
- case 'type':
- $aMethod['params'][1] = [
- 'type' => $aParams['type']
- ];
- $sWhereClause .= " AND `tac`.`type`=:type";
-
- if(isset($aParams['lifetime']) && (int)$aParams['lifetime'] > 0) {
- $aMethod['params'][1]['lifetime'] = (int)$aParams['lifetime'];
- $sWhereClause .= " AND (UNIX_TIMESTAMP() - `tac`.`added`) >= :lifetime";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_assistants_chats` AS `tac` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
-
- public function insertChat($aParamsSet)
- {
- if(empty($aParamsSet) || !is_array($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateChats($aSetClause, $aWhereClause)
- {
- if(empty($aSetClause) || empty($aWhereClause))
- return false;
- return (int)$this->query("UPDATE `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
- }
-
- public function deleteChats($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_assistants_chats` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
- }
-
- public function getFilesBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`taf`.*";
- $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `taf`.`id`=:id";
- break;
- case 'assistant_id':
- $aMethod['params'][1] = [
- 'assistant_id' => $aParams['assistant_id']
- ];
- $sWhereClause .= " AND `taf`.`assistant_id`=:assistant_id";
- if(!empty($aParams['name'])) {
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1]['name'] = $aParams['name'];
- $sWhereClause .= " AND `taf`.`name`=:name";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_assistants_files` AS `taf` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function insertFile($aParamsSet)
- {
- if(empty($aParamsSet) || !is_array($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_assistants_files` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateFiles($aSetClause, $aWhereClause)
- {
- if(empty($aSetClause) || empty($aWhereClause))
- return false;
- return (int)$this->query("UPDATE `sys_agents_assistants_files` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
- }
- public function deleteFiles($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_assistants_files` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
- }
- public function getVectorStores (): mixed
- {
- $sQuery = "SELECT `id`, `title` FROM `sys_agents_vector_store` WHERE `active` = 1 ORDER BY `title` ASC";
- return $this->getPairs($sQuery, 'id', 'title');
- }
- public function getVectorStoreById (int $iId): mixed
- {
- $sQuery = "SELECT * FROM `sys_agents_vector_store` WHERE `id` = :id";
- return $this->getRow($sQuery, ['id' => $iId]);
- }
- public function insertVectorStore($aVectorStore)
- {
- if(empty($aVectorStore) || !is_array($aVectorStore))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_vector_store` SET " . $this->arrayToSQL($aVectorStore));
- }
- public function getVectorStoreDataNum (int $iVectorStoreId): int
- {
- $sQuery = "SELECT COUNT(*) FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = :vector_store_id";
- return (int)$this->getOne($sQuery, ['vector_store_id' => $iVectorStoreId]);
- }
- public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
- {
- $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET
- `vector_store_id` = :vector_store_id,
- `type` = :type,
- `name` = :name,
- `size` = :size,
- `metadata` = :metadata,
- `settings` = :settings,
- `content` = :content,
- `added` = :ts
- ";
- return $this->query($sQuery, [
- 'vector_store_id' => $iVectorStoreId,
- 'type' => $sType,
- 'name' => $sName,
- 'size' => $iFileSize,
- 'metadata' => $sMetadata,
- 'settings' => $sSettings,
- 'content' => $sContent,
- 'ts' => time()]) > 0;
- }
- public function getVectorStoreDataById (int $iId): mixed
- {
- $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `id` = :id";
- return $this->getRow($sQuery, ['id' => $iId]);
- }
- static public function getVectorStorePendingData (int $iLimit = 1): mixed
- {
- $oDb = BxDolDb::getInstance();
- $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT :limit";
- return $oDb->getAll($sQuery, ['limit' => $iLimit]);
- }
- static public function updateVectorStoreDataStatus (int $iId, string $sStatus): mixed
- {
- $oDb = BxDolDb::getInstance();
- $sQuery = "UPDATE `sys_agents_vector_store_data` SET `status` = :status WHERE `id` = :id";
- return $oDb->query($sQuery, ['status' => $sStatus, 'id' => $iId]);
- }
- public function getAgentsByAlertUnitAndAction($sUnit, $sAction, $bActiveOnly = true)
- {
- return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'alert' AND `alert` = :alert AND `active` = :active", ['alert' => $sUnit . ':' . $sAction, 'active' => $bActiveOnly ? 1 : 0]);
- }
- public function getAgentsByProfileId($iProfileId, $bActiveOnly = true)
- {
- return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'message' AND `profile_id` = :profile AND `active` = :active", ['profile' => $iProfileId, 'active' => $bActiveOnly ? 1 : 0]);
- }
- public function getAgentByTriggerWebhookKey($sKey, $bActiveOnly = true)
- {
- return $this->getRow("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'scheduler' AND `webhook_key` = :key AND `active` = :active", ['key' => $sKey, 'active' => $bActiveOnly ? 1 : 0]);
- }
-
- public function getAgentsByTriggerType($sTrigger, $bActiveOnly = true)
- {
- return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = :trigger AND `active` = :active", ['trigger' => $sTrigger, 'active' => $bActiveOnly ? 1 : 0]);
- }
- public function updateAgentField($iId, $sField, $sValue)
- {
- return $this->query("UPDATE `sys_agents_agents` SET `$sField` = :value WHERE `id` = :id", ['value' => $sValue, 'id' => $iId]);
- }
- public function getToolById (int $iId): mixed
- {
- $sQuery = "SELECT * FROM `sys_agents_tools` WHERE `id` = :id";
- return $this->getRow($sQuery, ['id' => $iId]);
- }
- public function insertTool(array $aFields): int
- {
- if(empty($aFields) || !is_array($aFields))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_tools` SET " . $this->arrayToSQL($aFields));
- }
- public function getTools()
- {
- $sQuery = "SELECT `id`, `title` FROM `sys_agents_tools` WHERE `active` = 1 ORDER BY `title` ASC";
- return $this->getPairs($sQuery, 'id', 'title');
- }
- public function wipeAgentChatHistory($aAgent)
- {
- $sQuery = "DELETE FROM `sys_agents_chat_history` WHERE `thread_id` LIKE :val";
- return $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
- }
- static public function getAlert($s) {
- if (!$s)
- return false;
- $a = explode(':', $s);
- $oDb = BxDolDb::getInstance();
- return $oDb->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
- 'unit' => $a[0] ?? '',
- 'action' => $a[1] ?? '',
- ]);
- }
- public function getAlerts()
- {
- $aValues = [];
- $aAlerts = $this->getAll("SELECT `unit`, `action`, `counter_24h` FROM `sys_alerts_log` ORDER BY `unit`, `action`");
- foreach ($aAlerts as $a) {
- $sKey = $a['unit'] . ':' . $a['action'];
- $aValues[$sKey] = [
- 'key' => $sKey,
- 'unit' => $a['unit'],
- 'action' => $a['action'],
- 'name' => $a['unit'] . ' - ' . $a['action'],
- 'desc' => $this->getAlertDesc($a['unit'] . ':' . $a['action']),
- 'counter_24h' => $a['counter_24h'],
- ];
- }
- return $aValues;
- }
- static public function getAlertDesc($sAlert)
- {
- if (!$sAlert)
- return '';
- $oDb = BxDolDb::getInstance();
- [$sUnit, $sAction] = explode(':', $sAlert);
- $sDesc = $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
- 'unit' => $sUnit,
- 'action' => $sAction,
- ]);
- if (!$sDesc) {
- $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
- 'action' => $sAction,
- ]);
- }
- return $sDesc;
- }
- }
- /** @} */
|