BxDolAIQuery.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 getAssistantObject($iId)
  40. {
  41. $oDb = BxDolDb::getInstance();
  42. $aAssistant = $oDb->getRow("SELECT * FROM `sys_agents_assistants` WHERE `id` = :id", ['id' => $iId]);
  43. if(!$aAssistant || !is_array($aAssistant))
  44. return false;
  45. return $aAssistant;
  46. }
  47. public function insertModel($aModel)
  48. {
  49. if(empty($aModel))
  50. return false;
  51. return (int)$this->query("INSERT INTO `sys_agents_models` SET " . $this->arrayToSQL($aModel));
  52. }
  53. public function getModelsBy($aParams = [])
  54. {
  55. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  56. $sWhereClause = "";
  57. switch($aParams['sample']) {
  58. case 'id':
  59. $aMethod['name'] = 'getRow';
  60. $aMethod['params'][1] = [
  61. 'id' => $aParams['id']
  62. ];
  63. $sWhereClause .= " AND `id`=:id";
  64. break;
  65. case 'all_pairs':
  66. $aMethod['name'] = 'getPairs';
  67. $aMethod['params'][1] = 'id';
  68. $aMethod['params'][2] = 'title';
  69. $aMethod['params'][3] = [];
  70. if(isset($aParams['active'])) {
  71. $aMethod['params'][3]['active'] = $aParams['active'];
  72. $sWhereClause .= " AND `active`=:active";
  73. }
  74. if(isset($aParams['capabilities'])) {
  75. if (is_array($aParams['capabilities']))
  76. {
  77. $sWhereClause .= " AND `capabilities` IN (" . $this->implode_escape($aParams['capabilities']) . ")";
  78. }
  79. else {
  80. $aMethod['params'][3]['capabilities'] = $aParams['capabilities'];
  81. $sWhereClause .= " AND `capabilities` = :capabilities";
  82. }
  83. }
  84. break;
  85. }
  86. $aMethod['params'][0] = "SELECT *
  87. FROM `sys_agents_models`
  88. WHERE 1" . $sWhereClause;
  89. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  90. }
  91. public function getVectorStores (): mixed
  92. {
  93. $sQuery = "SELECT `id`, `title` FROM `sys_agents_vector_store` WHERE `active` = 1 ORDER BY `title` ASC";
  94. return $this->getPairs($sQuery, 'id', 'title');
  95. }
  96. public function getVectorStoreById (int $iId): mixed
  97. {
  98. $sQuery = "SELECT * FROM `sys_agents_vector_store` WHERE `id` = :id";
  99. return $this->getRow($sQuery, ['id' => $iId]);
  100. }
  101. public function insertVectorStore($aVectorStore)
  102. {
  103. if(empty($aVectorStore) || !is_array($aVectorStore))
  104. return false;
  105. return (int)$this->query("INSERT INTO `sys_agents_vector_store` SET " . $this->arrayToSQL($aVectorStore));
  106. }
  107. public function getVectorStoreDataNum (int $iVectorStoreId): int
  108. {
  109. $sQuery = "SELECT COUNT(*) FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = :vector_store_id";
  110. return (int)$this->getOne($sQuery, ['vector_store_id' => $iVectorStoreId]);
  111. }
  112. public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
  113. {
  114. $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET
  115. `vector_store_id` = :vector_store_id,
  116. `type` = :type,
  117. `name` = :name,
  118. `size` = :size,
  119. `metadata` = :metadata,
  120. `settings` = :settings,
  121. `content` = :content,
  122. `added` = :ts
  123. ";
  124. return $this->query($sQuery, [
  125. 'vector_store_id' => $iVectorStoreId,
  126. 'type' => $sType,
  127. 'name' => $sName,
  128. 'size' => $iFileSize,
  129. 'metadata' => $sMetadata,
  130. 'settings' => $sSettings,
  131. 'content' => $sContent,
  132. 'ts' => time()]) > 0;
  133. }
  134. public function getVectorStoreDataById (int $iId): mixed
  135. {
  136. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `id` = :id";
  137. return $this->getRow($sQuery, ['id' => $iId]);
  138. }
  139. static public function getVectorStorePendingData (int $iLimit = 1): mixed
  140. {
  141. $oDb = BxDolDb::getInstance();
  142. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT :limit";
  143. return $oDb->getAll($sQuery, ['limit' => $iLimit]);
  144. }
  145. static public function updateVectorStoreDataStatus (int $iId, string $sStatus): mixed
  146. {
  147. $oDb = BxDolDb::getInstance();
  148. $sQuery = "UPDATE `sys_agents_vector_store_data` SET `status` = :status WHERE `id` = :id";
  149. return $oDb->query($sQuery, ['status' => $sStatus, 'id' => $iId]);
  150. }
  151. public function getAgentsWithAlert($bActiveOnly = true)
  152. {
  153. return $this->fromCache('sys_agents_with_alert', 'getAll', "SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'alert' AND `active` = :active", ['active' => $bActiveOnly ? 1 : 0]);
  154. }
  155. public function getAgentsByProfileId($iProfileId, $bActiveOnly = true)
  156. {
  157. return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'message' AND `profile_id` = :profile AND `active` = :active", ['profile' => $iProfileId, 'active' => $bActiveOnly ? 1 : 0]);
  158. }
  159. public function getAgentsByFormObject($sFormObject, $bActiveOnly = true)
  160. {
  161. return $this->fromMemory('sys_agents_with_form_' . $sFormObject, 'getAll', "SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'form-input' AND `form_object` = :form_object AND `active` = :active", ['form_object' => $sFormObject, 'active' => $bActiveOnly ? 1 : 0]);
  162. }
  163. public function getAgentsBy($aParams = [])
  164. {
  165. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  166. $sWhereClause = "";
  167. switch($aParams['sample']) {
  168. case 'id':
  169. $aMethod['name'] = 'getRow';
  170. $aMethod['params'][1] = [
  171. 'id' => $aParams['id']
  172. ];
  173. $sWhereClause .= " AND `id`=:id";
  174. break;
  175. case 'all':
  176. if(isset($aParams['active'])) {
  177. $aMethod['params'][3]['active'] = $aParams['active'];
  178. $sWhereClause .= " AND `active`=:active";
  179. }
  180. break;
  181. }
  182. $aMethod['params'][0] = "SELECT *
  183. FROM `sys_agents_agents`
  184. WHERE 1" . $sWhereClause;
  185. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  186. }
  187. public function getAgentById(int $iId): mixed
  188. {
  189. return $this->getAgentsBy(['sample' => 'id', 'id' => $iId]);
  190. }
  191. public function getAgentByTriggerWebhookKey($sKey, $bActiveOnly = true)
  192. {
  193. return $this->getRow("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'scheduler' AND `webhook_key` = :key AND `active` = :active", ['key' => $sKey, 'active' => $bActiveOnly ? 1 : 0]);
  194. }
  195. public function getAgentsByTriggerType($sTrigger, $bActiveOnly = true)
  196. {
  197. return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = :trigger AND `active` = :active", ['trigger' => $sTrigger, 'active' => $bActiveOnly ? 1 : 0]);
  198. }
  199. public function updateAgentField($iId, $sField, $sValue)
  200. {
  201. return $this->query("UPDATE `sys_agents_agents` SET `$sField` = :value WHERE `id` = :id", ['value' => $sValue, 'id' => $iId]);
  202. }
  203. public function getToolById (int $iId): mixed
  204. {
  205. $sQuery = "SELECT * FROM `sys_agents_tools` WHERE `id` = :id";
  206. return $this->getRow($sQuery, ['id' => $iId]);
  207. }
  208. public function insertTool(array $aFields): int
  209. {
  210. if(empty($aFields) || !is_array($aFields))
  211. return false;
  212. return (int)$this->query("INSERT INTO `sys_agents_tools` SET " . $this->arrayToSQL($aFields));
  213. }
  214. public function getTools()
  215. {
  216. $sQuery = "SELECT `id`, `title` FROM `sys_agents_tools` WHERE `active` = 1 ORDER BY `title` ASC";
  217. return $this->getPairs($sQuery, 'id', 'title');
  218. }
  219. public function wipeAgentChatHistory($aAgent)
  220. {
  221. $sQuery = "DELETE FROM `sys_agents_chat_history` WHERE `thread_id` LIKE :val";
  222. $iAffected = 0;
  223. $iAffected += $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
  224. $iAffected += $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id']]);
  225. return $iAffected;
  226. }
  227. public function getAgentChatHistoryRows($aAgent)
  228. {
  229. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  230. $sFields = "`id`, `thread_id`, `messages`, `created_at`, `updated_at`";
  231. if ($this->isFieldExists('sys_agents_chat_history', 'closed_reason'))
  232. $sFields .= ", `closed_reason`";
  233. return $this->getAll("
  234. SELECT $sFields
  235. FROM `sys_agents_chat_history`
  236. WHERE `thread_id` = :exact OR `thread_id` LIKE :prefix
  237. ORDER BY `updated_at` DESC
  238. LIMIT 200
  239. ", [
  240. 'exact' => $sExact,
  241. 'prefix' => $sExact . ':%',
  242. ]);
  243. }
  244. public function getChatArtifactsByHistoryIds($aIds)
  245. {
  246. $aIds = array_values(array_unique(array_filter(array_map('intval', (array)$aIds))));
  247. if (!$aIds)
  248. return [];
  249. $sIn = implode(',', $aIds);
  250. $aRows = $this->getAll("
  251. SELECT `history_id`, `field_name`, `field_value`
  252. FROM `sys_agents_chat_artifacts`
  253. WHERE `history_id` IN ($sIn)
  254. ORDER BY `id` ASC
  255. ");
  256. return $this->mapChatArtifactRows($aRows);
  257. }
  258. public function getChatArtifactsByThreadId($sThreadId)
  259. {
  260. $sThreadId = (string)$sThreadId;
  261. if ($sThreadId === '')
  262. return [];
  263. $aRows = $this->getAll("
  264. SELECT `a`.`history_id`, `a`.`field_name`, `a`.`field_value`
  265. FROM `sys_agents_chat_artifacts` AS `a`
  266. INNER JOIN `sys_agents_chat_history` AS `h` ON `h`.`id` = `a`.`history_id`
  267. WHERE `h`.`thread_id` = :t
  268. ORDER BY `a`.`id` ASC
  269. ", [
  270. 't' => $sThreadId,
  271. ]);
  272. $aMap = $this->mapChatArtifactRows($aRows);
  273. if (!$aMap)
  274. return [];
  275. return (array)reset($aMap);
  276. }
  277. public function getChatArtifactsForAgent($aAgent)
  278. {
  279. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  280. if ($sExact === ':')
  281. return [];
  282. $aRows = $this->getAll("
  283. SELECT `a`.`history_id`, `a`.`field_name`, `a`.`field_value`
  284. FROM `sys_agents_chat_artifacts` AS `a`
  285. INNER JOIN `sys_agents_chat_history` AS `h` ON `h`.`id` = `a`.`history_id`
  286. WHERE `h`.`thread_id` = :exact OR `h`.`thread_id` LIKE :prefix
  287. ORDER BY `a`.`id` ASC
  288. ", [
  289. 'exact' => $sExact,
  290. 'prefix' => $sExact . ':%',
  291. ]);
  292. return $this->mapChatArtifactRows($aRows);
  293. }
  294. protected function mapChatArtifactRows($aRows)
  295. {
  296. if (!is_array($aRows) || !$aRows)
  297. return [];
  298. $aOut = [];
  299. foreach ($aRows as $aRow) {
  300. if (!is_array($aRow))
  301. continue;
  302. $iHistoryId = (int)($aRow['history_id'] ?? 0);
  303. $sName = trim((string)($aRow['field_name'] ?? ''));
  304. if ($iHistoryId <= 0 || $sName === '')
  305. continue;
  306. $aOut[$iHistoryId][] = [
  307. 'field_name' => $sName,
  308. 'field_value' => (string)($aRow['field_value'] ?? ''),
  309. ];
  310. }
  311. return $aOut;
  312. }
  313. public function getExpiredAgentChatHistoryIds($aAgent, $iTtlMin)
  314. {
  315. if (!$this->isFieldExists('sys_agents_chat_history', 'closed_reason'))
  316. return [];
  317. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  318. $iTtlMin = (int)$iTtlMin;
  319. if ($sExact === ':' || $iTtlMin <= 0)
  320. return [];
  321. return $this->getColumn("
  322. SELECT `id` FROM `sys_agents_chat_history`
  323. WHERE (`thread_id` = :exact OR `thread_id` LIKE :prefix)
  324. AND `messages` IS NOT NULL AND `messages` != '' AND `messages` != '[]'
  325. AND (`closed_reason` = '' OR `closed_reason` IS NULL)
  326. AND `updated_at` <= DATE_SUB(NOW(), INTERVAL :ttl MINUTE)
  327. ", [
  328. 'exact' => $sExact,
  329. 'prefix' => $sExact . ':%',
  330. 'ttl' => $iTtlMin,
  331. ]);
  332. }
  333. /**
  334. * Rename this agent's guest threads (`…:{sessionId}`) onto a profile id.
  335. * If the member thread already has messages, drop the guest copy.
  336. */
  337. public function adoptGuestChatHistory($aAgent, $sSessionId, $iProfileId)
  338. {
  339. $sSessionId = (string)$sSessionId;
  340. $iProfileId = (int)$iProfileId;
  341. if ($sSessionId === '' || !$iProfileId || empty($aAgent['id']))
  342. return 0;
  343. $sPrefix = $aAgent['trigger'] . ':' . $aAgent['id'] . ':';
  344. $sOldSuffix = ':' . $sSessionId;
  345. $sNewSuffix = ':' . $iProfileId;
  346. $iLen = strlen($sOldSuffix);
  347. $aBindings = [
  348. 'p' => $sPrefix . '%',
  349. 'o' => $sOldSuffix,
  350. 'n' => $sNewSuffix,
  351. ];
  352. $this->query("
  353. DELETE `g` FROM `sys_agents_chat_history` AS `g`
  354. INNER JOIN `sys_agents_chat_history` AS `m`
  355. ON `m`.`thread_id` = CONCAT(LEFT(`g`.`thread_id`, CHAR_LENGTH(`g`.`thread_id`) - $iLen), :n)
  356. WHERE `g`.`thread_id` LIKE :p AND RIGHT(`g`.`thread_id`, $iLen) = :o
  357. AND `m`.`messages` IS NOT NULL AND `m`.`messages` != '' AND `m`.`messages` != '[]'
  358. ", $aBindings);
  359. $this->query("
  360. DELETE `m` FROM `sys_agents_chat_history` AS `m`
  361. INNER JOIN `sys_agents_chat_history` AS `g`
  362. ON `g`.`thread_id` LIKE :p AND RIGHT(`g`.`thread_id`, $iLen) = :o
  363. AND `m`.`thread_id` = CONCAT(LEFT(`g`.`thread_id`, CHAR_LENGTH(`g`.`thread_id`) - $iLen), :n)
  364. WHERE `m`.`messages` = '' OR `m`.`messages` = '[]' OR `m`.`messages` IS NULL
  365. ", $aBindings);
  366. $iAffected = (int)$this->query("
  367. UPDATE `sys_agents_chat_history`
  368. SET `thread_id` = CONCAT(LEFT(`thread_id`, CHAR_LENGTH(`thread_id`) - $iLen), :n)
  369. WHERE `thread_id` LIKE :p AND RIGHT(`thread_id`, $iLen) = :o
  370. ", $aBindings);
  371. return $iAffected;
  372. }
  373. static public function getAlert($s) {
  374. if (!$s)
  375. return false;
  376. $a = explode(':', $s);
  377. $oDb = BxDolDb::getInstance();
  378. return $oDb->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
  379. 'unit' => $a[0] ?? '',
  380. 'action' => $a[1] ?? '',
  381. ]);
  382. }
  383. public function getAlerts()
  384. {
  385. $aValues = [];
  386. $aAlerts = $this->getAll("SELECT `unit`, `action`, `counter_24h`, `counter_per_request` FROM `sys_alerts_log` ORDER BY `unit`, `action`");
  387. foreach ($aAlerts as $a) {
  388. $sKey = $a['unit'] . ':' . $a['action'];
  389. $aValues[$sKey] = [
  390. 'key' => $sKey,
  391. 'unit' => $a['unit'],
  392. 'action' => $a['action'],
  393. 'name' => $a['unit'] . ' - ' . $a['action'],
  394. 'desc' => $this->getAlertDesc($a['unit'] . ':' . $a['action']),
  395. 'counter_24h' => $a['counter_24h'],
  396. 'counter_per_request' => $a['counter_per_request'],
  397. ];
  398. }
  399. return $aValues;
  400. }
  401. public function getFormDisplay($sObject)
  402. {
  403. $aDisplays = $this->getColumn("SELECT `display_name` FROM `sys_form_displays` WHERE `object` = :object", ['object' => $sObject]);
  404. return $aDisplays ? current($aDisplays) : null;
  405. }
  406. public function getFormObjects()
  407. {
  408. $aValues = [];
  409. $aForms = $this->getAll("SELECT `f`.`object`, `f`.`title`, `f`.`module`, `m`.`title` AS `module_title` FROM `sys_objects_form` AS f INNER JOIN `sys_modules` as `m` ON `m`.`name` = `f`.`module` ORDER BY `f`.`module`, `f`.`object` ASC");
  410. foreach ($aForms as $r) {
  411. if ('system' != $r['module']) {
  412. $oModule = BxDolModule::getInstance($r['module']);
  413. if (!$oModule || !$oModule->isEnabled())
  414. continue;
  415. }
  416. $sDisplay = $this->getFormDisplay($r['object']);
  417. if (!$sDisplay)
  418. continue;
  419. $oForm = BxDolForm::getObjectInstance($r['object'], $sDisplay);
  420. $sFormId = $oForm->getId();
  421. $aValues[$r['object']] = [
  422. 'form_id' => $sFormId,
  423. 'form_object' => $r['object'],
  424. 'module' => $r['module'],
  425. 'module_title' => $r['module_title'],
  426. 'form_title' => _t($r['title']),
  427. ];
  428. }
  429. return $aValues;
  430. }
  431. static public function getAlertDesc($sAlert)
  432. {
  433. if (!$sAlert)
  434. return '';
  435. $oDb = BxDolDb::getInstance();
  436. [$sUnit, $sAction] = explode(':', $sAlert);
  437. $sDesc = $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
  438. 'unit' => $sUnit,
  439. 'action' => $sAction,
  440. ]);
  441. if (!$sDesc) {
  442. $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
  443. 'action' => $sAction,
  444. ]);
  445. }
  446. return $sDesc;
  447. }
  448. }
  449. /** @} */