BxDolAIQuery.php 30 KB

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