BxDolAIQuery.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIQuery extends BxDolDb
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. static protected function _getObject(int $iId, string $sTable): array|bool
  16. {
  17. $oDb = BxDolDb::getInstance();
  18. $a = $oDb->getRow("SELECT * FROM `$sTable` WHERE `id` = :id", ['id' => $iId]);
  19. if(!$a || !is_array($a))
  20. return false;
  21. return $a;
  22. }
  23. static public function getModelObject(int $iId): array|bool
  24. {
  25. return self::_getObject($iId, 'sys_agents_models');
  26. }
  27. static public function getVectorStoreObject(int $iId): array|bool
  28. {
  29. return self::_getObject($iId, 'sys_agents_vector_store');
  30. }
  31. static public function getAgentObject(int $iId): array|bool
  32. {
  33. return self::_getObject($iId, 'sys_agents_agents');
  34. }
  35. static public function getToolObject(int $iId): array|bool
  36. {
  37. return self::_getObject($iId, 'sys_agents_tools');
  38. }
  39. static public function getProviderObject(int $iId)
  40. {
  41. $oDb = BxDolDb::getInstance();
  42. $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
  43. if(!$aProvider || !is_array($aProvider))
  44. return false;
  45. // get type
  46. $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
  47. if(!$aProviderType || !is_array($aProviderType))
  48. return false;
  49. $aProvider['type'] = $aProviderType;
  50. // get options
  51. $sQuery = "SELECT
  52. `tpo`.`name` AS `name`,
  53. `tpv`.`value` AS `value`
  54. FROM `sys_agents_provider_options` AS `tpo`
  55. LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
  56. WHERE 1 ORDER BY `tpo`.`order`";
  57. $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
  58. 'provider_id' => $iId
  59. ]);
  60. return $aProvider;
  61. }
  62. static public function getProviderIdByName($sName)
  63. {
  64. return (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `sys_agents_providers` WHERE `name`=:name LIMIT 1", [
  65. 'name' => $sName
  66. ]);
  67. }
  68. static public function getAssistantObject($iId)
  69. {
  70. $oDb = BxDolDb::getInstance();
  71. $aAssistant = $oDb->getRow("SELECT * FROM `sys_agents_assistants` WHERE `id` = :id", ['id' => $iId]);
  72. if(!$aAssistant || !is_array($aAssistant))
  73. return false;
  74. return $aAssistant;
  75. }
  76. public function insertModel($aModel)
  77. {
  78. if(empty($aModel))
  79. return false;
  80. return (int)$this->query("INSERT INTO `sys_agents_models` SET " . $this->arrayToSQL($aModel));
  81. }
  82. public function getModelsBy($aParams = [])
  83. {
  84. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  85. $sWhereClause = "";
  86. switch($aParams['sample']) {
  87. case 'id':
  88. $aMethod['name'] = 'getRow';
  89. $aMethod['params'][1] = [
  90. 'id' => $aParams['id']
  91. ];
  92. $sWhereClause .= " AND `id`=:id";
  93. break;
  94. case 'all_pairs':
  95. $aMethod['name'] = 'getPairs';
  96. $aMethod['params'][1] = 'id';
  97. $aMethod['params'][2] = 'title';
  98. $aMethod['params'][3] = [];
  99. if(isset($aParams['active'])) {
  100. $aMethod['params'][3]['active'] = $aParams['active'];
  101. $sWhereClause .= " AND `active`=:active";
  102. }
  103. if(isset($aParams['capabilities'])) {
  104. if (is_array($aParams['capabilities']))
  105. {
  106. $sWhereClause .= " AND `capabilities` IN (" . $this->implode_escape($aParams['capabilities']) . ")";
  107. }
  108. else {
  109. $aMethod['params'][3]['capabilities'] = $aParams['capabilities'];
  110. $sWhereClause .= " AND `capabilities` = :capabilities";
  111. }
  112. }
  113. break;
  114. }
  115. $aMethod['params'][0] = "SELECT *
  116. FROM `sys_agents_models`
  117. WHERE 1" . $sWhereClause;
  118. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  119. }
  120. public function getAutomatorsBy($aParams = [], $bFromCache = true)
  121. {
  122. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  123. $sSelectClause = "`taa`.*";
  124. $sPostfix = $sJoinClause = $sWhereClause = "";
  125. switch($aParams['sample']) {
  126. case 'id':
  127. $aMethod['name'] = 'getRow';
  128. $aMethod['params'][1] = [
  129. 'id' => $aParams['id']
  130. ];
  131. $sWhereClause .= " AND `taa`.`id`=:id";
  132. break;
  133. case 'id_full':
  134. $aMethod['name'] = 'getRow';
  135. $aMethod['params'][1] = [
  136. 'id' => $aParams['id']
  137. ];
  138. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  139. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  140. $sWhereClause .= " AND `taa`.`id`=:id";
  141. break;
  142. case 'type':
  143. $sPostfix .= '_' . $aParams['type'];
  144. $aMethod['params'][1] = [
  145. 'type' => $aParams['type']
  146. ];
  147. $sWhereClause .= " AND `taa`.`type`=:type";
  148. if(isset($aParams['active'])) {
  149. $iActive = (int)$aParams['active'];
  150. $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
  151. $aMethod['params'][1]['active'] = $iActive;
  152. $sWhereClause .= " AND `taa`.`active`=:active";
  153. }
  154. break;
  155. case 'events':
  156. $aBindings = [
  157. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  158. 'alert_unit' => $aParams['alert_unit'],
  159. 'alert_action' => $aParams['alert_action']
  160. ];
  161. $sPostfix .= '_' . implode('_', $aBindings);
  162. $aMethod['params'][1] = $aBindings;
  163. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  164. if(isset($aParams['active'])) {
  165. $iActive = (int)$aParams['active'];
  166. $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
  167. $aMethod['params'][1]['active'] = $iActive;
  168. $sWhereClause .= " AND `taa`.`active`=:active";
  169. }
  170. break;
  171. case 'schedulers':
  172. $aMethod['params'][1] = [
  173. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  174. ];
  175. $sWhereClause .= " AND `taa`.`type`=:type";
  176. if(isset($aParams['active'])) {
  177. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  178. $sWhereClause .= " AND `taa`.`active`=:active";
  179. }
  180. break;
  181. case 'webhooks':
  182. $aMethod['params'][1] = [
  183. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  184. ];
  185. $sWhereClause .= " AND `taa`.`type`=:type";
  186. if(isset($aParams['provider_id'])) {
  187. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  188. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  189. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  190. }
  191. if(isset($aParams['active'])) {
  192. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  193. $sWhereClause .= " AND `taa`.`active`=:active";
  194. }
  195. break;
  196. case 'providers_by_id_pairs':
  197. $aMethod['name'] = 'getPairs';
  198. $aMethod['params'][1] = 'id';
  199. $aMethod['params'][2] = 'provider_id';
  200. $aMethod['params'][3] = [
  201. 'id' => $aParams['id']
  202. ];
  203. $sSelectClause = "`taap`.`id`, `taap`.`provider_id`";
  204. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `taap` ON `taa`.`id`=`taap`.`automator_id`";
  205. $sWhereClause = " AND `taa`.`id`=:id";
  206. break;
  207. case 'helpers_by_id_pairs':
  208. $aMethod['name'] = 'getPairs';
  209. $aMethod['params'][1] = 'id';
  210. $aMethod['params'][2] = 'helper_id';
  211. $aMethod['params'][3] = [
  212. 'id' => $aParams['id']
  213. ];
  214. $sSelectClause = "`taah`.`id`, `taah`.`helper_id`";
  215. $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `taah` ON `taa`.`id`=`taah`.`automator_id`";
  216. $sWhereClause = " AND `taa`.`id`=:id";
  217. break;
  218. case 'assistants_by_id_pairs':
  219. $aMethod['name'] = 'getPairs';
  220. $aMethod['params'][1] = 'id';
  221. $aMethod['params'][2] = 'assistant_id';
  222. $aMethod['params'][3] = [
  223. 'id' => $aParams['id']
  224. ];
  225. $sSelectClause = "`taaa`.`id`, `taaa`.`assistant_id`";
  226. $sJoinClause = "INNER JOIN `sys_agents_automators_assistants` AS `taaa` ON `taa`.`id`=`taaa`.`automator_id`";
  227. $sWhereClause = " AND `taa`.`id`=:id";
  228. break;
  229. }
  230. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  231. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  232. WHERE 1" . $sWhereClause;
  233. if(!$bFromCache || empty($sPostfix))
  234. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  235. return call_user_func_array([$this, 'fromMemory'], array_merge(['sys_automators' . $sPostfix, $aMethod['name']], $aMethod['params']));
  236. }
  237. public function updateAutomators($aSetClause, $aWhereClause)
  238. {
  239. if(empty($aSetClause) || empty($aWhereClause))
  240. return false;
  241. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  242. }
  243. public function insertAutomatorProvider($aParamsSet)
  244. {
  245. if(empty($aParamsSet))
  246. return false;
  247. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  248. }
  249. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  250. {
  251. if(empty($aParamsSet) || empty($aParamsWhere))
  252. return false;
  253. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  254. }
  255. public function deleteAutomatorProviders($aParamsWhere)
  256. {
  257. if(empty($aParamsWhere))
  258. return false;
  259. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  260. }
  261. public function deleteAutomatorProvidersById($mixedId)
  262. {
  263. if(!is_array($mixedId))
  264. $mixedId = [$mixedId];
  265. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  266. }
  267. public function insertAutomatorHelper($aParamsSet)
  268. {
  269. if(empty($aParamsSet))
  270. return false;
  271. return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  272. }
  273. public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
  274. {
  275. if(empty($aParamsSet) || empty($aParamsWhere))
  276. return false;
  277. return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  278. }
  279. public function deleteAutomatorHelpers($aParamsWhere)
  280. {
  281. if(empty($aParamsWhere))
  282. return false;
  283. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  284. }
  285. public function deleteAutomatorHelpersById($mixedId)
  286. {
  287. if(!is_array($mixedId))
  288. $mixedId = [$mixedId];
  289. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  290. }
  291. public function insertAutomatorAssistant($aParamsSet)
  292. {
  293. if(empty($aParamsSet))
  294. return false;
  295. return (int)$this->query("INSERT INTO `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  296. }
  297. public function updateAutomatorAssistant($aParamsSet, $aParamsWhere)
  298. {
  299. if(empty($aParamsSet) || empty($aParamsWhere))
  300. return false;
  301. return $this->query("UPDATE `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  302. }
  303. public function deleteAutomatorAssistants($aParamsWhere)
  304. {
  305. if(empty($aParamsWhere))
  306. return false;
  307. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  308. }
  309. public function deleteAutomatorAssistantsById($mixedId)
  310. {
  311. if(!is_array($mixedId))
  312. $mixedId = [$mixedId];
  313. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  314. }
  315. public function getProviderTypesBy($aParams = [])
  316. {
  317. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  318. $sWhereClause = "";
  319. switch($aParams['sample']) {
  320. case 'id':
  321. $aMethod['name'] = 'getRow';
  322. $aMethod['params'][1] = [
  323. 'id' => $aParams['id']
  324. ];
  325. $sWhereClause .= " AND `id`=:id";
  326. break;
  327. case 'name':
  328. $aMethod['name'] = 'getRow';
  329. $aMethod['params'][1] = [
  330. 'name' => $aParams['name']
  331. ];
  332. $sWhereClause .= " AND `name`=:name";
  333. break;
  334. case 'all_pairs':
  335. $aMethod['name'] = 'getPairs';
  336. $aMethod['params'][1] = 'id';
  337. $aMethod['params'][2] = 'title';
  338. if(isset($aParams['active'])) {
  339. $aMethod['params'][3] = [
  340. 'active' => $aParams['active']
  341. ];
  342. $sWhereClause = " AND `active`=:active";
  343. }
  344. break;
  345. }
  346. $aMethod['params'][0] = "SELECT *
  347. FROM `sys_agents_provider_types`
  348. WHERE 1" . $sWhereClause;
  349. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  350. }
  351. public function getProviderOptionsBy($aParams = [])
  352. {
  353. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  354. $sWhereClause = "";
  355. switch($aParams['sample']) {
  356. case 'id':
  357. $aMethod['name'] = 'getRow';
  358. $aMethod['params'][1] = [
  359. 'id' => $aParams['id']
  360. ];
  361. $sWhereClause .= " AND `id`=:id";
  362. break;
  363. case 'provider_type_id':
  364. $aMethod['params'][1] = [
  365. 'provider_type_id' => $aParams['provider_type_id']
  366. ];
  367. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  368. break;
  369. }
  370. $aMethod['params'][0] = "SELECT *
  371. FROM `sys_agents_provider_options`
  372. WHERE 1" . $sWhereClause;
  373. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  374. }
  375. public function getProvidersBy($aParams = [])
  376. {
  377. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  378. $sSelectClause = "`tp`.*";
  379. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  380. switch($aParams['sample']) {
  381. case 'id':
  382. $aMethod['name'] = 'getRow';
  383. $aMethod['params'][1] = [
  384. 'id' => $aParams['id']
  385. ];
  386. $sWhereClause = " AND `tp`.`id`=:id";
  387. break;
  388. case 'ids':
  389. $sSelectClause .= ", `tpt`.`name` AS `type_name`";
  390. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  391. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  392. break;
  393. case 'options_by_id':
  394. $aMethod['params'][1] = [
  395. 'id' => $aParams['id']
  396. ];
  397. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  398. $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`";
  399. $sWhereClause = " AND `tp`.`id`=:id";
  400. break;
  401. case 'all_pairs':
  402. $aMethod['name'] = 'getPairs';
  403. $aMethod['params'][1] = 'id';
  404. $aMethod['params'][2] = 'name';
  405. if(isset($aParams['active'])) {
  406. $aMethod['params'][3] = [
  407. 'active' => $aParams['active']
  408. ];
  409. $sWhereClause = " AND `tp`.`active`=:active";
  410. }
  411. break;
  412. }
  413. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  414. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  415. $aMethod['params'][0] = "SELECT
  416. " . $sSelectClause . "
  417. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  418. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  419. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  420. }
  421. public function insertProviderValue($aParamsSet)
  422. {
  423. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  424. return false;
  425. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  426. 'value' => $aParamsSet['value']
  427. ]) > 0 ? (int)$this->lastId() : false;
  428. }
  429. public function updateProviderValue($aParamsSet, $aParamsWhere)
  430. {
  431. if(empty($aParamsSet))
  432. return false;
  433. $sWhereClause = "1";
  434. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  435. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  436. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  437. }
  438. public function deleteProviderValues($aParamsWhere)
  439. {
  440. if(empty($aParamsWhere))
  441. return false;
  442. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  443. }
  444. public function getHelpersBy($aParams = [])
  445. {
  446. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  447. $sSelectClause = "`th`.*";
  448. $sJoinClause = $sWhereClause = "";
  449. switch($aParams['sample']) {
  450. case 'id':
  451. $aMethod['name'] = 'getRow';
  452. $aMethod['params'][1] = [
  453. 'id' => $aParams['id']
  454. ];
  455. $sWhereClause .= " AND `th`.`id`=:id";
  456. break;
  457. case 'name':
  458. $aMethod['name'] = 'getRow';
  459. $aMethod['params'][1] = [
  460. 'name' => $aParams['name']
  461. ];
  462. $sWhereClause .= " AND `th`.`name`=:name";
  463. break;
  464. case 'ids':
  465. $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  466. break;
  467. case 'all_pairs':
  468. $aMethod['name'] = 'getPairs';
  469. $aMethod['params'][1] = 'id';
  470. $aMethod['params'][2] = 'name';
  471. if(isset($aParams['active'])) {
  472. $aMethod['params'][3] = [
  473. 'active' => $aParams['active']
  474. ];
  475. $sWhereClause = " AND `th`.`active`=:active";
  476. }
  477. break;
  478. }
  479. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  480. FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
  481. WHERE 1" . $sWhereClause;
  482. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  483. }
  484. public function getAssistantsBy($aParams = [])
  485. {
  486. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  487. $sSelectClause = "`ta`.*";
  488. $sJoinClause = $sWhereClause = "";
  489. switch($aParams['sample']) {
  490. case 'id':
  491. $aMethod['name'] = 'getRow';
  492. $aMethod['params'][1] = [
  493. 'id' => $aParams['id']
  494. ];
  495. $sWhereClause .= " AND `ta`.`id`=:id";
  496. break;
  497. case 'name':
  498. $aMethod['name'] = 'getRow';
  499. $aMethod['params'][1] = [
  500. 'name' => $aParams['name']
  501. ];
  502. $sWhereClause .= " AND `ta`.`name`=:name";
  503. break;
  504. case 'ids':
  505. $sWhereClause = " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  506. break;
  507. case 'all_pairs':
  508. $aMethod['name'] = 'getPairs';
  509. $aMethod['params'][1] = 'id';
  510. $aMethod['params'][2] = 'name';
  511. $aMethod['params'][3] = [];
  512. if(isset($aParams['active'])) {
  513. $aMethod['params'][3]['active'] = $aParams['active'];
  514. $sWhereClause .= " AND `ta`.`active`=:active";
  515. }
  516. if(isset($aParams['hidden'])) {
  517. $aMethod['params'][3]['hidden'] = $aParams['hidden'];
  518. $sWhereClause .= " AND `ta`.`hidden`=:hidden";
  519. }
  520. break;
  521. }
  522. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  523. FROM `sys_agents_assistants` AS `ta` " . $sJoinClause . "
  524. WHERE 1" . $sWhereClause;
  525. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  526. }
  527. public function updateAssistants($aSetClause, $aWhereClause)
  528. {
  529. if(empty($aSetClause) || empty($aWhereClause))
  530. return false;
  531. return (int)$this->query("UPDATE `sys_agents_assistants` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  532. }
  533. public function getChatsBy($aParams = [])
  534. {
  535. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  536. $sSelectClause = "`tac`.*";
  537. $sJoinClause = $sWhereClause = "";
  538. switch($aParams['sample']) {
  539. case 'id':
  540. $aMethod['name'] = 'getRow';
  541. $aMethod['params'][1] = [
  542. 'id' => $aParams['id']
  543. ];
  544. $sWhereClause .= " AND `tac`.`id`=:id";
  545. break;
  546. case 'name':
  547. $aMethod['name'] = 'getRow';
  548. $aMethod['params'][1] = [
  549. 'name' => $aParams['name']
  550. ];
  551. $sWhereClause .= " AND `tac`.`name`=:name";
  552. break;
  553. case 'assistant_id':
  554. $aMethod['params'][1] = [
  555. 'assistant_id' => $aParams['assistant_id']
  556. ];
  557. $sWhereClause .= " AND `tac`.`assistant_id`=:assistant_id";
  558. break;
  559. case 'type':
  560. $aMethod['params'][1] = [
  561. 'type' => $aParams['type']
  562. ];
  563. $sWhereClause .= " AND `tac`.`type`=:type";
  564. if(isset($aParams['lifetime']) && (int)$aParams['lifetime'] > 0) {
  565. $aMethod['params'][1]['lifetime'] = (int)$aParams['lifetime'];
  566. $sWhereClause .= " AND (UNIX_TIMESTAMP() - `tac`.`added`) >= :lifetime";
  567. }
  568. break;
  569. }
  570. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  571. FROM `sys_agents_assistants_chats` AS `tac` " . $sJoinClause . "
  572. WHERE 1" . $sWhereClause;
  573. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  574. }
  575. public function insertChat($aParamsSet)
  576. {
  577. if(empty($aParamsSet) || !is_array($aParamsSet))
  578. return false;
  579. return (int)$this->query("INSERT INTO `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  580. }
  581. public function updateChats($aSetClause, $aWhereClause)
  582. {
  583. if(empty($aSetClause) || empty($aWhereClause))
  584. return false;
  585. return (int)$this->query("UPDATE `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  586. }
  587. public function deleteChats($aParamsWhere)
  588. {
  589. if(empty($aParamsWhere))
  590. return false;
  591. return (int)$this->query("DELETE FROM `sys_agents_assistants_chats` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  592. }
  593. public function getFilesBy($aParams = [])
  594. {
  595. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  596. $sSelectClause = "`taf`.*";
  597. $sJoinClause = $sWhereClause = "";
  598. switch($aParams['sample']) {
  599. case 'id':
  600. $aMethod['name'] = 'getRow';
  601. $aMethod['params'][1] = [
  602. 'id' => $aParams['id']
  603. ];
  604. $sWhereClause .= " AND `taf`.`id`=:id";
  605. break;
  606. case 'assistant_id':
  607. $aMethod['params'][1] = [
  608. 'assistant_id' => $aParams['assistant_id']
  609. ];
  610. $sWhereClause .= " AND `taf`.`assistant_id`=:assistant_id";
  611. if(!empty($aParams['name'])) {
  612. $aMethod['name'] = 'getRow';
  613. $aMethod['params'][1]['name'] = $aParams['name'];
  614. $sWhereClause .= " AND `taf`.`name`=:name";
  615. }
  616. break;
  617. }
  618. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  619. FROM `sys_agents_assistants_files` AS `taf` " . $sJoinClause . "
  620. WHERE 1" . $sWhereClause;
  621. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  622. }
  623. public function insertFile($aParamsSet)
  624. {
  625. if(empty($aParamsSet) || !is_array($aParamsSet))
  626. return false;
  627. return (int)$this->query("INSERT INTO `sys_agents_assistants_files` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  628. }
  629. public function updateFiles($aSetClause, $aWhereClause)
  630. {
  631. if(empty($aSetClause) || empty($aWhereClause))
  632. return false;
  633. return (int)$this->query("UPDATE `sys_agents_assistants_files` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  634. }
  635. public function deleteFiles($aParamsWhere)
  636. {
  637. if(empty($aParamsWhere))
  638. return false;
  639. return (int)$this->query("DELETE FROM `sys_agents_assistants_files` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  640. }
  641. public function getVectorStores (): mixed
  642. {
  643. $sQuery = "SELECT `id`, `title` FROM `sys_agents_vector_store` WHERE `active` = 1 ORDER BY `title` ASC";
  644. return $this->getPairs($sQuery, 'id', 'title');
  645. }
  646. public function getVectorStoreById (int $iId): mixed
  647. {
  648. $sQuery = "SELECT * FROM `sys_agents_vector_store` WHERE `id` = :id";
  649. return $this->getRow($sQuery, ['id' => $iId]);
  650. }
  651. public function insertVectorStore($aVectorStore)
  652. {
  653. if(empty($aVectorStore) || !is_array($aVectorStore))
  654. return false;
  655. return (int)$this->query("INSERT INTO `sys_agents_vector_store` SET " . $this->arrayToSQL($aVectorStore));
  656. }
  657. public function getVectorStoreDataNum (int $iVectorStoreId): int
  658. {
  659. $sQuery = "SELECT COUNT(*) FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = :vector_store_id";
  660. return (int)$this->getOne($sQuery, ['vector_store_id' => $iVectorStoreId]);
  661. }
  662. public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
  663. {
  664. $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET
  665. `vector_store_id` = :vector_store_id,
  666. `type` = :type,
  667. `name` = :name,
  668. `size` = :size,
  669. `metadata` = :metadata,
  670. `settings` = :settings,
  671. `content` = :content,
  672. `added` = :ts
  673. ";
  674. return $this->query($sQuery, [
  675. 'vector_store_id' => $iVectorStoreId,
  676. 'type' => $sType,
  677. 'name' => $sName,
  678. 'size' => $iFileSize,
  679. 'metadata' => $sMetadata,
  680. 'settings' => $sSettings,
  681. 'content' => $sContent,
  682. 'ts' => time()]) > 0;
  683. }
  684. public function getVectorStoreDataById (int $iId): mixed
  685. {
  686. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `id` = :id";
  687. return $this->getRow($sQuery, ['id' => $iId]);
  688. }
  689. static public function getVectorStorePendingData (int $iLimit = 1): mixed
  690. {
  691. $oDb = BxDolDb::getInstance();
  692. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT :limit";
  693. return $oDb->getAll($sQuery, ['limit' => $iLimit]);
  694. }
  695. static public function updateVectorStoreDataStatus (int $iId, string $sStatus): mixed
  696. {
  697. $oDb = BxDolDb::getInstance();
  698. $sQuery = "UPDATE `sys_agents_vector_store_data` SET `status` = :status WHERE `id` = :id";
  699. return $oDb->query($sQuery, ['status' => $sStatus, 'id' => $iId]);
  700. }
  701. public function getAgentsByAlertUnitAndAction($sUnit, $sAction)
  702. {
  703. return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `alert_unit` = :unit AND `alert_action` = :action", ['unit' => $sUnit, 'action' => $sAction]);
  704. }
  705. public function updateAgentField($iId, $sField, $sValue)
  706. {
  707. return $this->query("UPDATE `sys_agents_agents` SET `$sField` = :value WHERE `id` = :id", ['value' => $sValue, 'id' => $iId]);
  708. }
  709. public function getToolById (int $iId): mixed
  710. {
  711. $sQuery = "SELECT * FROM `sys_agents_tools` WHERE `id` = :id";
  712. return $this->getRow($sQuery, ['id' => $iId]);
  713. }
  714. public function insertTool(array $aFields): int
  715. {
  716. if(empty($aFields) || !is_array($aFields))
  717. return false;
  718. return (int)$this->query("INSERT INTO `sys_agents_tools` SET " . $this->arrayToSQL($aFields));
  719. }
  720. public function getTools()
  721. {
  722. return [];
  723. }
  724. }
  725. /** @} */