BxBaseStudioAgentsAutomators.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
  11. {
  12. protected $_sUrlPage;
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct ($aOptions, $oTemplate);
  16. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
  17. }
  18. public function getJsObject()
  19. {
  20. return 'oBxDolStudioAgentsAutomators';
  21. }
  22. public function performActionAdd()
  23. {
  24. $sAction = 'add';
  25. $aForm = $this->_getForm($sAction);
  26. $oForm = new BxTemplFormView($aForm);
  27. $oForm->initChecker();
  28. if($oForm->isSubmittedAndValid()) {
  29. $aValsToAdd = ['added' => time()];
  30. $iProfileId = $oForm->getCleanValue('profile_id');
  31. if(empty($iProfileId)) {
  32. $iProfileId = (int)getParam('sys_agents_profile');
  33. if(empty($iProfileId))
  34. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  35. $aValsToAdd['profile_id'] = $iProfileId;
  36. }
  37. $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
  38. if(!empty($sSchedulerTime))
  39. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
  40. $iModel = $oForm->getCleanValue('model_id');
  41. $sType = $oForm->getCleanValue('type');
  42. $sMessage = $oForm->getCleanValue('message');
  43. $bMessage = !empty($sMessage);
  44. $sMessageAdd = '';
  45. $sMessageResponse = '';
  46. $oAI = BxDolAI::getInstance();
  47. $aMessages = [
  48. ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($sType . '_init')],
  49. ['role' => 'user', 'content' => $sMessage]
  50. ];
  51. $aModel = $oAI->getModel($iModel);
  52. $bIsValid = true;
  53. switch($sType) {
  54. case BX_DOL_AI_AUTOMATOR_EVENT:
  55. $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
  56. if($sResponse != 'false') {
  57. $aResponse = json_decode($sResponse, true);
  58. $aValsToAdd = array_merge($aValsToAdd, [
  59. 'alert_unit' => $aResponse['alert_unit'],
  60. 'alert_action' => $aResponse['alert_action'],
  61. 'params' => json_encode(['trigger' => $aResponse['trigger']])
  62. ]);
  63. $sMessageAdd = $aResponse['trigger'];
  64. }
  65. else {
  66. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_event_not_found');
  67. $bIsValid = false;
  68. }
  69. break;
  70. case BX_DOL_AI_AUTOMATOR_SCHEDULER:
  71. $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
  72. if($sResponse != 'false') {
  73. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sResponse]);
  74. }
  75. else {
  76. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_event_not_found');
  77. $bIsValid = false;
  78. }
  79. break;
  80. }
  81. if($bIsValid) {
  82. $aMessages = [
  83. ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($sType, true)],
  84. ['role' => 'user', 'content' => $sMessage . $sMessageAdd]
  85. ];
  86. $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
  87. if($sResponse != 'false') {
  88. $sMessageResponse = $sResponse;
  89. }
  90. else {
  91. $oForm->aInputs['message']['error'] = _t('_sys_agents_automators_err_cannot_get_code');
  92. $bIsValid = false;
  93. }
  94. }
  95. if($bIsValid) {
  96. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  97. if(($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
  98. if($bMessage) {
  99. $aResult = $oCmts->addAuto([
  100. 'cmt_author_id' => bx_get_logged_profile_id(),
  101. 'cmt_parent_id' => 0,
  102. 'cmt_text' => $sMessage
  103. ]);
  104. if(!empty($aResult['id']))
  105. $this->_oDb->updateAutomators(['message_id' => $aResult['id']], ['id' => $iId]);
  106. }
  107. if($sMessageResponse) {
  108. sleep(1);
  109. $oCmts->addAuto([
  110. 'cmt_author_id' => $this->_iProfileIdAi,
  111. 'cmt_parent_id' => 0,
  112. 'cmt_text' => $sMessageResponse
  113. ]);
  114. }
  115. }
  116. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  117. }
  118. else
  119. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  120. return echoJson($aRes);
  121. }
  122. }
  123. $sFormId = $oForm->getId();
  124. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  125. 'form_id' => $sFormId,
  126. 'form' => $oForm->getCode(true),
  127. 'object' => $this->_sObject,
  128. 'action' => $sAction
  129. ]));
  130. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  131. }
  132. public function performActionEdit()
  133. {
  134. $sAction = 'edit';
  135. $iId = $this->_getId();
  136. $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id', 'id' => $iId]);
  137. $aForm = $this->_getFormEdit($sAction, $aAutomator);
  138. $oForm = new BxTemplFormView($aForm);
  139. $oForm->initChecker();
  140. if($oForm->isSubmittedAndValid()) {
  141. $aValsToAdd = [];
  142. $iProfileId = $oForm->getCleanValue('profile_id');
  143. if(empty($iProfileId)) {
  144. $iProfileId = (int)getParam('sys_agents_profile');
  145. if(empty($iProfileId))
  146. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  147. $aValsToAdd['profile_id'] = $iProfileId;
  148. }
  149. $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
  150. if(!empty($sSchedulerTime))
  151. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
  152. if($oForm->update($iId, $aValsToAdd))
  153. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  154. else
  155. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  156. return echoJson($aRes);
  157. }
  158. $sFormId = $oForm->getId();
  159. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  160. 'form_id' => $sFormId,
  161. 'form' => $oForm->getCode(true),
  162. 'object' => $this->_sObject,
  163. 'action' => $sAction
  164. ]));
  165. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  166. }
  167. public function getCode($isDisplayHeader = true)
  168. {
  169. return $this->_oTemplate->parseHtmlByName('agents_automator.html', [
  170. 'content' => parent::getCode($isDisplayHeader),
  171. 'js_object' => $this->getJsObject(),
  172. 'page_url' => $this->_sUrlPage,
  173. 'grid_object' => $this->_sObject,
  174. 'params_divider' => $this->sParamsDivider
  175. ]);
  176. }
  177. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  178. {
  179. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  180. if(!empty($aModel) && is_array($aModel))
  181. $mixedValue = $aModel['name'];
  182. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  183. }
  184. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  185. {
  186. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  187. }
  188. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  189. {
  190. return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
  191. }
  192. protected function _getCellData($sKey, $aField, $aRow)
  193. {
  194. if($sKey == 'message_id' && ($iCmtId = (int)$aRow[$sKey]) != 0 && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $aRow['id'])) !== null) {
  195. $aCmt = $oCmts->getCommentSimple($iCmtId);
  196. if(!empty($aCmt) && is_array($aCmt))
  197. $aRow[$sKey] = $aCmt['cmt_text'];
  198. }
  199. return parent::_getCellData($sKey, $aField, $aRow);
  200. }
  201. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  202. {
  203. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  204. }
  205. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  206. {
  207. return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
  208. }
  209. protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  210. {
  211. $a['attr'] = array_merge($a['attr'], [
  212. "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
  213. ]);
  214. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  215. }
  216. protected function _addJsCss()
  217. {
  218. parent::_addJsCss();
  219. $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
  220. $oForm = new BxTemplStudioFormView([]);
  221. $oForm->addCssJs();
  222. }
  223. protected function _isCheckboxDisabled($aRow)
  224. {
  225. return false;
  226. }
  227. protected function _getActionsDisabledBehavior($aRow)
  228. {
  229. return false;
  230. }
  231. protected function _getFormEdit($sAction, $aAutomator = [])
  232. {
  233. $aForm = $this->_getForm($sAction, $aAutomator);
  234. $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
  235. unset($aForm['inputs']['message']);
  236. return $aForm;
  237. }
  238. protected function _getForm($sAction, $aAutomator = [])
  239. {
  240. $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
  241. if(!empty($aAutomator['params']))
  242. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  243. $aForm = array(
  244. 'form_attrs' => array(
  245. 'id' => 'bx_std_agents_automators_' . $sAction,
  246. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
  247. 'method' => 'post',
  248. ),
  249. 'params' => array (
  250. 'db' => array(
  251. 'table' => 'sys_agents_automators',
  252. 'key' => 'id',
  253. 'submit_name' => 'do_submit',
  254. ),
  255. ),
  256. 'inputs' => array(
  257. 'model_id' => [
  258. 'type' => 'select',
  259. 'name' => 'model_id',
  260. 'caption' => _t('_sys_agents_automators_field_model_id'),
  261. 'info' => '',
  262. 'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : 0,
  263. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
  264. 'required' => '1',
  265. 'db' => [
  266. 'pass' => 'Int',
  267. ]
  268. ],
  269. 'profile_id' => [
  270. 'type' => 'select',
  271. 'name' => 'profile_id',
  272. 'caption' => _t('_sys_agents_automators_field_profile_id'),
  273. 'info' => _t('_sys_agents_automators_field_profile_id_inf'),
  274. 'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
  275. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  276. 'required' => '0',
  277. 'db' => [
  278. 'pass' => 'Int',
  279. ]
  280. ],
  281. 'type' => [
  282. 'type' => 'select',
  283. 'name' => 'type',
  284. 'caption' => _t('_sys_agents_automators_field_type'),
  285. 'info' => '',
  286. 'value' => $sType,
  287. 'values' => [
  288. 'event' => _t('_sys_agents_automators_field_type_event'),
  289. 'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
  290. 'webhook' => _t('_sys_agents_automators_field_type_webhook'),
  291. ],
  292. 'attrs' => [
  293. 'onchange' => $this->getJsObject() . '.onChangeType(this)',
  294. ],
  295. 'required' => '1',
  296. 'db' => [
  297. 'pass' => 'Xss',
  298. ]
  299. ],
  300. 'alert_unit' => [
  301. 'type' => 'hidden',
  302. 'name' => 'alert_unit',
  303. 'caption' => _t('_sys_agents_automators_field_alert_unit'),
  304. 'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
  305. 'tr_attrs' => [
  306. 'style' => $sType != 'event' ? 'display:none' : ''
  307. ],
  308. 'db' => [
  309. 'pass' => 'Xss',
  310. ],
  311. ],
  312. 'alert_action' => [
  313. 'type' => 'hidden',
  314. 'name' => 'alert_action',
  315. 'caption' => _t('_sys_agents_automators_field_alert_action'),
  316. 'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
  317. 'tr_attrs' => [
  318. 'style' => $sType != 'event' ? 'display:none' : ''
  319. ],
  320. 'db' => [
  321. 'pass' => 'Xss',
  322. ],
  323. ],
  324. 'scheduler_time' => [
  325. 'type' => 'hidden',
  326. 'name' => 'scheduler_time',
  327. 'caption' => _t('_sys_agents_automators_field_scheduler_time'),
  328. 'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
  329. 'tr_attrs' => [
  330. 'style' => $sType != 'scheduler' ? 'display:none' : ''
  331. ],
  332. ],
  333. 'message' => [
  334. 'type' => 'textarea',
  335. 'name' => 'message',
  336. 'caption' => _t('_sys_agents_automators_field_message'),
  337. ],
  338. 'submit' => array(
  339. 'type' => 'input_set',
  340. 0 => array (
  341. 'type' => 'submit',
  342. 'name' => 'do_submit',
  343. 'value' => _t('_sys_submit'),
  344. ),
  345. 1 => array (
  346. 'type' => 'reset',
  347. 'name' => 'close',
  348. 'value' => _t('_sys_close'),
  349. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  350. ),
  351. ),
  352. ),
  353. );
  354. return $aForm;
  355. }
  356. protected function _getId()
  357. {
  358. $aIds = bx_get('ids');
  359. if(!empty($aIds) && is_array($aIds))
  360. return array_shift($aIds);
  361. $iId = (int)bx_get('id');
  362. if(!$iId)
  363. return false;
  364. return $iId;
  365. }
  366. }
  367. /** @} */