BxDolAIQuery.php 30 KB

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