BxDolAIQuery.php 29 KB

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