BxDolAiQuery.php 18 KB

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