BxBaseStudioAgentsAgents.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
  11. {
  12. protected $_sUrlPage;
  13. protected $_sFieldName;
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=agents';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionGetAlert()
  25. {
  26. $aAlert = $this->_getAlertPayload(bx_get('alert'));
  27. echoJson([
  28. 'code' => $aAlert ? 200 : 404,
  29. 'desc' => $aAlert ? $this->_oDb->getAlertDesc(bx_get('alert')) : '',
  30. 'payload' => $aAlert,
  31. ]);
  32. }
  33. public function performActionGetFormInputs()
  34. {
  35. $aFormInputs = $this->_getFormInputs(bx_get('form_object'));
  36. if (!$aFormInputs) {
  37. echoJson(['code' => 404, 'msg' => _t('_sys_not_available')]);
  38. return;
  39. }
  40. echoJson([
  41. 'code' => $aFormInputs ? 200 : 404,
  42. 'form_inputs' => $aFormInputs,
  43. ]);
  44. }
  45. public function performActionManual()
  46. {
  47. $iId = $this->_getId();
  48. $aAgent = BxDolAiQuery::getAgentObject($iId);
  49. if (!$aAgent) {
  50. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  51. return;
  52. }
  53. if (!$aAgent['active']) {
  54. echoJson(['msg' => _t('_sys_txt_agent_inactive')]);
  55. return;
  56. }
  57. $oAi = BxDolAI::getInstance();
  58. $sResponse = $oAi->callAgent('manual', $aAgent);
  59. $oParsedown = new Parsedown();
  60. $oParsedown->setSafeMode(true);
  61. $sMessageHtml = $oParsedown->text($sResponse);
  62. $oTemplate = BxDolStudioTemplate::getInstance();
  63. $sHtml = $oTemplate->parseHtmlByName('agents_manual_response.html', [
  64. 'response' => $sMessageHtml
  65. ]);
  66. echoJson(['msg' => $sHtml]);
  67. }
  68. public function performActionWipeChatHistory()
  69. {
  70. $iId = $this->_getId();
  71. $aAgent = BxDolAiQuery::getAgentObject($iId);
  72. if (!$aAgent) {
  73. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  74. return;
  75. }
  76. $oDb = new BxDolAIQuery();
  77. $oDb->wipeAgentChatHistory($aAgent);
  78. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  79. echoJson($aRes);
  80. }
  81. public function performActionAdd()
  82. {
  83. $sAction = 'add';
  84. $oTemplate = BxDolStudioTemplate::getInstance();
  85. $oAI = BxDolAI::getInstance();
  86. $aForm = $this->_getForm($sAction);
  87. $oForm = new BxTemplFormView($aForm);
  88. $oForm->initChecker();
  89. if ($oForm->isSubmitted() && $oForm->getCleanValue('trigger') == 'message') {
  90. $aAgent = $this->_getAgentWithProfile($oForm->getCleanValue('profile_id'));
  91. if ($aAgent) {
  92. $oForm->aInputs['profile_id']['error'] = _t('_sys_agents_form_field_err_profile_has_agent', $aAgent['name']);
  93. $oForm->setValid(false);
  94. }
  95. }
  96. if($oForm->isSubmittedAndValid()) {
  97. $aValsToAdd = [
  98. 'added' => time()
  99. ];
  100. $sName = $oForm->getCleanValue($this->_sFieldName);
  101. $sName = $this->_getUniqName($sName);
  102. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  103. $iProfileId = $oForm->getCleanValue('profile_id');
  104. if(empty($iProfileId)) {
  105. $iProfileId = (int)getParam('sys_agents_profile');
  106. if(empty($iProfileId))
  107. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  108. $aValsToAdd['profile_id'] = $iProfileId;
  109. }
  110. $iModel = $oForm->getCleanValue('model_id');
  111. $sTrigger = $oForm->getCleanValue('trigger');
  112. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  113. $aValsToAdd['tools'] = $sTools;
  114. $bIsValid = true;
  115. if($bIsValid) {
  116. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  117. $this->_oDb->cleanCache('sys_agents_with_alert');
  118. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  119. }
  120. else
  121. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  122. return echoJson($aRes);
  123. }
  124. }
  125. $sFormId = $oForm->getId();
  126. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  127. 'form_id' => $sFormId,
  128. 'form' => $oForm->getCode(true),
  129. 'object' => $this->_sObject,
  130. 'action' => $sAction
  131. ]));
  132. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  133. }
  134. public function performActionEdit()
  135. {
  136. $sAction = 'edit';
  137. $oTemplate = BxDolStudioTemplate::getInstance();
  138. $oAI = BxDolAI::getInstance();
  139. $iId = $this->_getId();
  140. $aAgent = BxDolAiQuery::getAgentObject($iId);
  141. $aForm = $this->_getFormEdit($sAction, $aAgent);
  142. $oForm = new BxTemplFormView($aForm);
  143. $oForm->initChecker();
  144. if ($oForm->isSubmitted() && $oForm->getCleanValue('trigger') == 'message') {
  145. $aAgent = $this->_getAgentWithProfile($oForm->getCleanValue('profile_id'), $iId);
  146. if ($aAgent) {
  147. $oForm->aInputs['profile_id']['error'] = _t('_sys_agents_form_field_err_profile_has_agent', $aAgent['name']);
  148. $oForm->setValid(false);
  149. }
  150. }
  151. if($oForm->isSubmittedAndValid()) {
  152. $aValsToAdd = [];
  153. $sName = $oForm->getCleanValue($this->_sFieldName);
  154. $sName = $this->_getUniqName($sName);
  155. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  156. $iProfileId = $oForm->getCleanValue('profile_id');
  157. if(empty($iProfileId)) {
  158. $iProfileId = (int)getParam('sys_agents_profile');
  159. if(empty($iProfileId))
  160. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  161. $aValsToAdd['profile_id'] = $iProfileId;
  162. }
  163. $iModel = $oForm->getCleanValue('model_id');
  164. $sTrigger = $oForm->getCleanValue('trigger');
  165. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  166. $aValsToAdd['tools'] = $sTools;
  167. $bIsValid = true;
  168. if($bIsValid) {
  169. if(($iId = $oForm->update($iId, $aValsToAdd)) !== false) {
  170. $this->_oDb->cleanCache('sys_agents_with_alert');
  171. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  172. }
  173. else
  174. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  175. return echoJson($aRes);
  176. }
  177. }
  178. $sFormId = $oForm->getId();
  179. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_edit', $aAgent['name']), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  180. 'form_id' => $sFormId,
  181. 'form' => $oForm->getCode(true),
  182. 'object' => $this->_sObject,
  183. 'action' => $sAction
  184. ]));
  185. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  186. }
  187. public function performActionLogs()
  188. {
  189. $iId = $this->_getId();
  190. $aAgent = BxDolAiQuery::getAgentObject($iId);
  191. if (!$aAgent) {
  192. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  193. return;
  194. }
  195. $oGrid = BxDolGrid::getObjectInstance('sys_studio_agents_logs');
  196. $oGrid->addMarkers(['agent_id' => $iId]);
  197. $oGrid->setBrowseParams(['agent_id' => $iId]);
  198. $sGrid = $oGrid->getCode();
  199. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('popup_files_' . $iId, _t('_sys_agents_logs_popup', $aAgent['name']), $this->_oTemplate->parseHtmlByName('agents_popup_grid.html', [
  200. 'grid' => $sGrid,
  201. ]));
  202. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  203. }
  204. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  205. {
  206. // if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  207. // return parent::_getCellDefault('', $sKey, $aField, $aRow);
  208. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  209. }
  210. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  211. {
  212. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  213. }
  214. protected function _delete ($mixedId)
  215. {
  216. $mixedResult = parent::_delete($mixedId);
  217. $this->_oDb->cleanCache('sys_agents_with_alert');
  218. if($mixedResult) {
  219. // $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  220. // $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
  221. // $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
  222. // if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
  223. // $oCmts->onObjectDelete();
  224. }
  225. return $mixedResult;
  226. }
  227. protected function _getFormEdit($sAction, $aAgent = [])
  228. {
  229. $aForm = $this->_getForm($sAction, $aAgent);
  230. $aForm['form_attrs']['action'] .= '&id=' . $aAgent['id'];
  231. unset($aForm['inputs']['name']);
  232. return $aForm;
  233. }
  234. protected function _getForm($sAction, $aAgent = [])
  235. {
  236. $aForm = array(
  237. 'form_attrs' => array(
  238. 'id' => 'bx_std_agents_' . $sAction,
  239. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_agents&a=' . $sAction,
  240. 'method' => 'post',
  241. ),
  242. 'params' => array (
  243. 'db' => array(
  244. 'table' => 'sys_agents_agents',
  245. 'key' => 'id',
  246. 'submit_name' => 'do_submit',
  247. ),
  248. ),
  249. 'inputs' => array(
  250. 'name' => [
  251. 'type' => 'text',
  252. 'name' => 'name',
  253. 'caption' => _t('_sys_agents_field_name'),
  254. 'info' => _t('_sys_agents_field_name_info'),
  255. 'value' => isset($aAgent['name']) ? $aAgent['name'] : '',
  256. 'required' => '1',
  257. 'checker' => [
  258. 'func' => 'Avail',
  259. 'error' => _t('_sys_agents_form_field_err_enter'),
  260. ],
  261. 'db' => [
  262. 'pass' => 'Xss',
  263. ],
  264. ],
  265. 'model_id' => [
  266. 'type' => 'select',
  267. 'name' => 'model_id',
  268. 'caption' => _t('_sys_agents_field_model_id'),
  269. 'info' => _t('_sys_agents_field_model_id_info'),
  270. 'value' => isset($aAgent['model_id']) ? $aAgent['model_id'] : BxDolAI::getDefaultModel(),
  271. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'capabilities' => ['chatvlm', 'chatllm']]),
  272. 'required' => '1',
  273. 'checker' => [
  274. 'func' => 'Avail',
  275. 'error' => _t('_sys_agents_form_field_err_select'),
  276. ],
  277. 'db' => [
  278. 'pass' => 'Int',
  279. ],
  280. ],
  281. 'profile_id' => [
  282. 'type' => 'select',
  283. 'name' => 'profile_id',
  284. 'caption' => _t('_sys_agents_field_profile_id'),
  285. 'info' => _t('_sys_agents_field_profile_id_inf'),
  286. 'value' => isset($aAgent['profile_id']) ? $aAgent['profile_id'] : 0,
  287. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  288. 'required' => '1',
  289. 'required' => '1',
  290. 'checker' => [
  291. 'func' => 'Avail',
  292. 'error' => _t('_sys_agents_form_field_err_select'),
  293. ],
  294. 'db' => [
  295. 'pass' => 'Int',
  296. ]
  297. ],
  298. 'trigger' => [
  299. 'type' => 'select',
  300. 'name' => 'trigger',
  301. 'caption' => _t('_sys_agents_field_trigger'),
  302. 'info' => _t('_sys_agents_field_trigger_info'),
  303. 'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
  304. 'values' => [
  305. 'alert' => 'alert',
  306. 'scheduler' => 'scheduler',
  307. 'webhook' => 'webhook',
  308. 'manual' => 'manual',
  309. 'agent' => 'agent',
  310. 'message' => 'message',
  311. 'form-input' => 'form input',
  312. ],
  313. // 'attrs' => [
  314. // 'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
  315. // ],
  316. 'required' => '1',
  317. 'db' => [
  318. 'pass' => 'Xss',
  319. ]
  320. ],
  321. 'async' => [
  322. 'type' => 'checkbox',
  323. 'name' => 'async',
  324. 'caption' => _t('_sys_agents_field_async'),
  325. 'info' => _t('_sys_agents_field_async_inf'),
  326. 'value' => 1,
  327. 'checked' => isset($aAgent['async']) ? $aAgent['async'] : 0,
  328. 'db' => [
  329. 'pass' => 'Int',
  330. ]
  331. ],
  332. 'alert_section' => array(
  333. 'type' => 'block_header',
  334. 'caption' => 'Trigger - alert',
  335. 'collapsable' => true,
  336. 'collapsed' => true,
  337. ),
  338. 'alert_sample' => [
  339. 'type' => 'custom',
  340. 'name' => 'alert_sample',
  341. 'caption' => _t('_sys_agents_field_alert_sample'),
  342. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_alerts_payload.html', [
  343. 'text-visibility' => empty($aAgent['alert']) ? 'block' : 'none',
  344. 'text' => _t('_sys_agents_waiting_for_sample_data_alert'),
  345. 'payload-visibility' => !empty($aAgent['alert']) ? 'block' : 'none',
  346. 'desc' => $aAgent ? $this->_oDb->getAlertDesc($aAgent['alert']) : '',
  347. 'payload' => !empty($aAgent['alert']) ? json_encode($this->_getAlertPayload($aAgent['alert']), JSON_PRETTY_PRINT) : '',
  348. ]),
  349. ],
  350. 'alert' => [
  351. 'type' => 'custom',
  352. 'name' => 'alert',
  353. 'caption' => _t('_sys_agents_field_alert'),
  354. 'info' => _t('_sys_agents_field_alert_info'),
  355. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_alerts_select.html', $this->_getAlertValues(isset($aAgent['alert']) ? $aAgent['alert'] : '')),
  356. 'db' => [
  357. 'pass' => 'Xss',
  358. ],
  359. ],
  360. 'scheduler_section' => [
  361. 'type' => 'block_header',
  362. 'caption' => 'Trigger - scheduler',
  363. 'collapsable' => true,
  364. 'collapsed' => true,
  365. ],
  366. 'scheduler_cron' => [
  367. 'type' => 'text',
  368. 'name' => 'scheduler_cron',
  369. 'caption' => _t('_sys_agents_field_scheduler_cron'),
  370. 'info' => _t('_sys_agents_field_scheduler_cron_info'),
  371. 'value' => isset($aAgent['scheduler_cron']) ? $aAgent['scheduler_cron'] : '',
  372. 'db' => [
  373. 'pass' => 'Xss',
  374. ],
  375. ],
  376. 'webhook_section' => array(
  377. 'type' => 'block_header',
  378. 'caption' => 'Trigger - webhook',
  379. 'collapsable' => true,
  380. 'collapsed' => true,
  381. ),
  382. 'webhook_sample' => [
  383. 'type' => 'custom',
  384. 'name' => 'webhook_sample',
  385. 'caption' => _t('_sys_agents_field_webhook_sample'),
  386. 'content' => isset($aAgent['webhook_sample']) ? $aAgent['webhook_sample'] : _t('_sys_agents_waiting_for_sample_data_webhook'),
  387. ],
  388. 'webhook_key' => [
  389. 'type' => 'text',
  390. 'name' => 'webhook_key',
  391. 'caption' => _t('_sys_agents_field_webhook_key'),
  392. 'info' => _t('_sys_agents_field_webhook_key_info'),
  393. 'value' => isset($aAgent['webhook_key']) ? $aAgent['webhook_key'] : '',
  394. 'db' => [
  395. 'pass' => 'Xss',
  396. ],
  397. ],
  398. 'message_section' => array(
  399. 'type' => 'block_header',
  400. 'caption' => 'Trigger - message',
  401. 'collapsable' => true,
  402. 'collapsed' => true,
  403. ),
  404. 'message_profile_id' => [
  405. 'type' => 'select',
  406. 'name' => 'message_profile_id',
  407. 'caption' => _t('_sys_agents_field_message_profile'),
  408. 'info' => _t('_sys_agents_field_message_profile_info'),
  409. 'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
  410. 'values' => bx_srv('system', 'get_options_agents_profile', [true, '_adm_nav_txt_items_visible_for_all'], 'TemplServices'),
  411. 'db' => [
  412. 'pass' => 'Xss',
  413. ],
  414. ],
  415. 'form_field_section' => array(
  416. 'type' => 'block_header',
  417. 'caption' => 'Trigger - form field',
  418. 'collapsable' => true,
  419. 'collapsed' => true,
  420. ),
  421. 'form_object' => [
  422. 'type' => 'custom',
  423. 'name' => 'form_object',
  424. 'caption' => _t('_sys_agents_form_object'),
  425. 'info' => _t('_sys_agents_form_object_info'),
  426. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_form_object_select.html', $this->_getFormObjectValues(isset($aAgent['form_object']) ? $aAgent['form_object'] : '')),
  427. 'db' => [
  428. 'pass' => 'Xss',
  429. ],
  430. ],
  431. 'form_input' => [
  432. 'type' => 'custom',
  433. 'name' => 'form_input',
  434. 'caption' => _t('_sys_agents_form_input'),
  435. 'info' => _t('_sys_agents_form_input_info'),
  436. 'content' => $this->_oTemplate->parseHtmlByName('agents_agents_form_input_select.html', $this->_getFormInputValues(isset($aAgent['form_object']) ? $aAgent['form_object'] : '', isset($aAgent['form_input']) ? $aAgent['form_input'] : '')),
  437. 'db' => [
  438. 'pass' => 'Xss',
  439. ],
  440. ],
  441. 'form_field_section_end' => array(
  442. 'type' => 'block_end',
  443. ),
  444. 'vector_store_id' => [
  445. 'type' => 'select',
  446. 'name' => 'vector_store_id',
  447. 'caption' => _t('_sys_agents_field_vector_store_id'),
  448. 'info' => _t('_sys_agents_field_vector_store_id_info'),
  449. 'value' => isset($aAgent['vector_store_id']) ? $aAgent['vector_store_id'] : 0,
  450. 'values' => $this->_oDb->getVectorStores(),
  451. 'required' => '1',
  452. 'checker' => [
  453. 'func' => 'Avail',
  454. 'error' => _t('_sys_agents_form_field_err_select'),
  455. ],
  456. 'db' => [
  457. 'pass' => 'Xss',
  458. ],
  459. ],
  460. 'chat_history_context' => [
  461. 'type' => 'text',
  462. 'name' => 'chat_history_context',
  463. 'caption' => _t('_sys_agents_field_chat_history_context'),
  464. 'info' => _t('_sys_agents_field_chat_history_context_info'),
  465. 'value' => isset($aAgent['chat_history_context']) ? $aAgent['chat_history_context'] : 50000,
  466. 'db' => [
  467. 'pass' => 'Int',
  468. ],
  469. ],
  470. 'tools' => [
  471. 'type' => 'custom',
  472. 'name' => 'tools',
  473. 'caption' => _t('_sys_agents_field_tools'),
  474. 'info' => _t('_sys_agents_field_tools_info'),
  475. 'value' => '',
  476. ],
  477. 'tools_max_run' => [
  478. 'type' => 'text',
  479. 'name' => 'tools_max_run',
  480. 'caption' => _t('_sys_agents_field_tools_max_run'),
  481. 'info' => _t('_sys_agents_field_tools_max_run_info'),
  482. 'value' => isset($aAgent['tools_max_run']) ? $aAgent['tools_max_run'] : '10',
  483. 'required' => '1',
  484. 'db' => [
  485. 'pass' => 'Int',
  486. ],
  487. ],
  488. 'prompt_system' => [
  489. 'type' => 'textarea',
  490. 'name' => 'prompt_system',
  491. 'caption' => _t('_sys_agents_field_prompt_system'),
  492. 'info' => _t('_sys_agents_field_prompt_system_info'),
  493. 'value' => isset($aAgent['prompt_system']) ? $aAgent['prompt_system'] : '',
  494. 'db' => [
  495. 'pass' => 'Xss',
  496. ],
  497. ],
  498. 'prompts_section' => array(
  499. 'type' => 'block_header',
  500. 'caption' => 'Other prompts',
  501. 'collapsable' => true,
  502. 'collapsed' => true,
  503. ),
  504. 'prompt_steps' => [
  505. 'type' => 'textarea',
  506. 'name' => 'prompt_steps',
  507. 'caption' => _t('_sys_agents_field_prompt_steps'),
  508. 'info' => _t('_sys_agents_field_prompt_steps_info'),
  509. 'value' => isset($aAgent['prompt_steps']) ? $aAgent['prompt_steps'] : '',
  510. 'db' => [
  511. 'pass' => 'Xss',
  512. ],
  513. ],
  514. 'prompt_output' => [
  515. 'type' => 'textarea',
  516. 'name' => 'prompt_output',
  517. 'caption' => _t('_sys_agents_field_prompt_output'),
  518. 'info' => _t('_sys_agents_field_prompt_output_info'),
  519. 'value' => isset($aAgent['prompt_output']) ? $aAgent['prompt_output'] : '',
  520. 'db' => [
  521. 'pass' => 'Xss',
  522. ],
  523. ],
  524. 'prompt_tools' => [
  525. 'type' => 'textarea',
  526. 'name' => 'prompt_tools',
  527. 'caption' => _t('_sys_agents_field_prompt_tools'),
  528. 'info' => _t('_sys_agents_field_prompt_tools_info'),
  529. 'value' => isset($aAgent['prompt_tools']) ? $aAgent['prompt_tools'] : '',
  530. 'db' => [
  531. 'pass' => 'Xss',
  532. ],
  533. ],
  534. 'prompts_section_end' => array(
  535. 'type' => 'block_end',
  536. ),
  537. 'submit' => $this->_getFormControls(),
  538. ),
  539. );
  540. $this->_getMultiField('tools', $aAgent, 'getTools', 'toolAdd', 'agents_agents_form_tools.html', $aForm);
  541. return $aForm;
  542. }
  543. protected function _getAlertPayload($s)
  544. {
  545. $aAlert = $this->_oDb->getAlert($s);
  546. if (!$aAlert)
  547. return '';
  548. return [
  549. 'object_id' => $aAlert['object'],
  550. 'sender_profile_id' => $aAlert['sender'],
  551. 'extra' => json_decode($aAlert['extra'], true),
  552. 'extra_modifiable_keys' => json_decode($aAlert['extra_refs'], true),
  553. ];
  554. }
  555. protected function _getAlertValues($sValue)
  556. {
  557. $aAlerts = $this->_oDb->getAlerts();
  558. $a = [
  559. 'sys_stat_not_available' => _t('_sys_not_available'),
  560. 'grid_object' => $this->_sObject,
  561. 'bx_repeat:alerts' => [
  562. [
  563. 'name' => _t('_sys_please_select'),
  564. 'value' => '',
  565. 'desc' => '',
  566. 'count' => 0,
  567. 'count_per_req' => 0,
  568. 'bx_if:sel' => [
  569. 'condition' => false,
  570. 'content' => ['sel' => '']
  571. ]
  572. ]
  573. ]
  574. ];
  575. foreach ($aAlerts as $k => $r) {
  576. $a['bx_repeat:alerts'][] = [
  577. 'name' => $r['name'],
  578. 'value' => $r['key'],
  579. 'desc' => bx_html_attribute($r['desc']),
  580. 'count' => bx_html_attribute(_t('_sys_agents_count_today', $r['counter_24h'])),
  581. 'count_per_req' => bx_html_attribute(_t('_sys_agents_count_per_req', $r['counter_per_request'])),
  582. 'bx_if:sel' => [
  583. 'condition' => $sValue == $r['key'],
  584. 'content' => [
  585. 'sel' => $sValue == $r['key'] ? 'selected' : '',
  586. ]
  587. ]
  588. ];
  589. }
  590. return $a;
  591. }
  592. protected function _getFormObjectValues($sValue)
  593. {
  594. $aFormObjects = $this->_oDb->getFormObjects();
  595. // add "Please select" option
  596. $a = [
  597. 'sys_stat_not_available' => _t('_sys_not_available'),
  598. 'grid_object' => $this->_sObject,
  599. 'bx_repeat:form_objects' => [
  600. [
  601. 'title' => _t('_sys_please_select'),
  602. 'title_attr' => _t('_sys_please_select'),
  603. 'value' => '',
  604. 'bx_if:sel' => [
  605. 'condition' => false,
  606. 'content' => ['sel' => '']
  607. ]
  608. ]
  609. ]
  610. ];
  611. // add form objects
  612. $bSelected = false;
  613. foreach ($aFormObjects as $k => $r) {
  614. if ($sValue == $r['form_object'])
  615. $bSelected = true;
  616. $a['bx_repeat:form_objects'][] = [
  617. 'title' => $r['module_title'] . ': ' . $r['form_title'],
  618. 'title_attr' => bx_html_attribute($r['module_title'] . ': ' . $r['form_title']),
  619. 'value' => $r['form_object'],
  620. // 'form_id' => $r['form_id'],
  621. // 'module' => $r['module'],
  622. // 'module_title' => $r['module_title'],
  623. // 'form_title' => $r['form_title'],
  624. 'bx_if:sel' => [
  625. 'condition' => $sValue == $r['form_object'],
  626. 'content' => ['sel' => $sValue == $r['form_object'] ? 'selected' : ''],
  627. ]
  628. ];
  629. }
  630. // add selected form object if not found in form objects
  631. if ($sValue && !$bSelected) {
  632. $a['bx_repeat:form_objects'][] = [
  633. 'title' => $sValue,
  634. 'title_attr' => bx_html_attribute($sValue),
  635. 'value' => $sValue,
  636. 'bx_if:sel' => [
  637. 'condition' => true,
  638. 'content' => ['sel' => 'selected'],
  639. ]
  640. ];
  641. }
  642. return $a;
  643. }
  644. protected function _getFormInputs($sFormObject)
  645. {
  646. $sDisplay = $this->_oDb->getFormDisplay($sFormObject);
  647. $oForm = $sDisplay ? BxDolForm::getObjectInstance($sFormObject, $sDisplay) : null;
  648. $aFormInputs = $oForm ? $oForm->aInputs : [];
  649. if (!$aFormInputs)
  650. return [];
  651. $aRet = [];
  652. foreach ($aFormInputs as $k => $r) {
  653. if ($r['type'] == 'text' || $r['type'] == 'textarea') {
  654. $aRet[$r['name']] = [
  655. 'name' => $r['name'],
  656. 'caption' => $r['caption'],
  657. ];
  658. }
  659. }
  660. return $aRet;
  661. }
  662. protected function _getFormInputValues($sFormObject, $sFormInput)
  663. {
  664. $aFormInputs = $this->_getFormInputs($sFormObject);
  665. // add "Please select" option
  666. $a = [
  667. 'bx_repeat:form_inputs' => [
  668. [
  669. 'title' => _t('_sys_please_select'),
  670. 'title_attr' => _t('_sys_please_select'),
  671. 'value' => '',
  672. 'bx_if:sel' => [
  673. 'condition' => false,
  674. 'content' => ['sel' => '']
  675. ]
  676. ]
  677. ]
  678. ];
  679. // add form inputs
  680. $bSelected = false;
  681. if ($aFormInputs) {
  682. foreach ($aFormInputs as $k => $r) {
  683. if ($sFormInput == $r['name'])
  684. $bSelected = true;
  685. $a['bx_repeat:form_inputs'][] = [
  686. 'title' => $r['name'] . ' - ' . $r['caption'],
  687. 'title_attr' => bx_html_attribute($r['name'] . (!empty($r['caption']) ? ' - ' . $r['caption'] : '')),
  688. 'value' => $r['name'],
  689. 'bx_if:sel' => [
  690. 'condition' => $sFormInput == $r['name'],
  691. 'content' => [
  692. 'sel' => $sFormInput == $r['name'] ? 'selected' : '',
  693. ]
  694. ]
  695. ];
  696. }
  697. }
  698. // add selected input if not found in form inputs
  699. if ($sFormInput && !$bSelected) {
  700. $a['bx_repeat:form_inputs'][] = [
  701. 'title' => $sFormInput,
  702. 'title_attr' => bx_html_attribute($sFormInput),
  703. 'value' => $sFormInput,
  704. 'bx_if:sel' => [
  705. 'condition' => true,
  706. 'content' => ['sel' => 'selected'],
  707. ]
  708. ];
  709. }
  710. return $a;
  711. }
  712. protected function _getUniqName($sName)
  713. {
  714. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  715. }
  716. protected function _getMultiField($sField, $aAgent, $sGetValuesMethod, $sJsAddMethod, $sTemplateName, &$aForm)
  717. {
  718. if(!isset($aForm['inputs']['tools']))
  719. return;
  720. $sJsObject = $this->getPageJsObject();
  721. $oForm = new BxTemplFormView([]);
  722. $aValues = ['0' => _t('_sys_please_select')];
  723. $a = $this->_oDb->$sGetValuesMethod();
  724. foreach ($a as $k => $v) {
  725. $aValues[$k] = $v;
  726. }
  727. $aTmplVars = [];
  728. if(!empty($aAgent[$sField])) {
  729. $a = explode(',', $aAgent[$sField]);
  730. foreach($a as $v) {
  731. $aInputSelect = [
  732. 'type' => 'select',
  733. 'name' => $sField . '[]',
  734. 'values' => $aValues,
  735. 'value' => $v,
  736. 'attrs' => [
  737. 'class' => 'bx-def-margin-sec-top-auto'
  738. ]
  739. ];
  740. $sInput = $oForm->genInput($aInputSelect);
  741. $aInputHidden = [
  742. 'type' => 'hidden',
  743. 'name' => $sField . '_ids[]',
  744. 'value' => $v,
  745. ];
  746. $sInput .= $oForm->genInput($aInputHidden);
  747. $aTmplVars[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  748. }
  749. }
  750. else {
  751. $aInputSelect = [
  752. 'type' => 'select',
  753. 'name' => $sField . '[]',
  754. 'values' => $aValues,
  755. 'value' => '',
  756. 'attrs' => [
  757. 'class' => 'bx-def-margin-sec-top-auto'
  758. ]
  759. ];
  760. $aTmplVars = [
  761. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  762. ];
  763. }
  764. $aInputButton = [
  765. 'type' => 'button',
  766. 'name' => $sField . '_add',
  767. 'value' => _t('_adm_nav_btn_items_add'),
  768. 'attrs' => [
  769. 'class' => 'bx-def-margin-sec-top',
  770. 'onclick' => $sJsObject . ".$sJsAddMethod(this, '" . $sField . "');"
  771. ]
  772. ];
  773. $aForm['inputs'][$sField]['content'] = $this->_oTemplate->parseHtmlByName($sTemplateName, [
  774. "bx_repeat:$sField" => $aTmplVars,
  775. 'btn_add' => $oForm->genInputButton($aInputButton)
  776. ]);
  777. }
  778. protected function _getActionManual ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  779. {
  780. if ($aRow['trigger'] != 'manual' && $aRow['trigger'] != 'scheduler')
  781. return '';
  782. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  783. }
  784. protected function _getAgentWithProfile($iProfileId, $iExcludeAgentId = 0)
  785. {
  786. $aAgents = BxDolAi::getInstance()->getAgentsByProfileId($iProfileId);
  787. foreach($aAgents as $aAgent) {
  788. if($aAgent['id'] != $iExcludeAgentId)
  789. return $aAgent;
  790. }
  791. return null;
  792. }
  793. protected function _addJsCss()
  794. {
  795. parent::_addJsCss();
  796. $this->_oTemplate->addJs(['select2/js/select2.min.js']);
  797. $this->_oTemplate->addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'select2/css/|select2.min.css']);
  798. }
  799. }
  800. /** @} */