BxBaseStudioAgentsAutomators.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 getPageJsObject()
  19. {
  20. return 'oBxDolStudioPageAgents';
  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. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  168. {
  169. if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  170. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  171. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  172. }
  173. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  174. {
  175. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  176. if(!empty($aModel) && is_array($aModel))
  177. $mixedValue = $aModel['name'];
  178. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  179. }
  180. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  181. {
  182. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  183. }
  184. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  185. {
  186. return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
  187. }
  188. protected function _getCellData($sKey, $aField, $aRow)
  189. {
  190. if($sKey == 'message_id' && ($iCmtId = (int)$aRow[$sKey]) != 0 && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $aRow['id'])) !== null) {
  191. $aCmt = $oCmts->getCommentSimple($iCmtId);
  192. if(!empty($aCmt) && is_array($aCmt))
  193. $aRow[$sKey] = $aCmt['cmt_text'];
  194. }
  195. return parent::_getCellData($sKey, $aField, $aRow);
  196. }
  197. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  198. {
  199. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  200. }
  201. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  202. {
  203. return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
  204. }
  205. protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  206. {
  207. $a['attr'] = array_merge($a['attr'], [
  208. "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
  209. ]);
  210. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  211. }
  212. protected function _addJsCss()
  213. {
  214. parent::_addJsCss();
  215. $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
  216. $oForm = new BxTemplStudioFormView([]);
  217. $oForm->addCssJs();
  218. }
  219. protected function _isCheckboxDisabled($aRow)
  220. {
  221. return false;
  222. }
  223. protected function _getActionsDisabledBehavior($aRow)
  224. {
  225. return false;
  226. }
  227. protected function _getFormEdit($sAction, $aAutomator = [])
  228. {
  229. $aForm = $this->_getForm($sAction, $aAutomator);
  230. $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
  231. unset($aForm['inputs']['message']);
  232. return $aForm;
  233. }
  234. protected function _getForm($sAction, $aAutomator = [])
  235. {
  236. $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
  237. if(!empty($aAutomator['params']))
  238. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  239. $aForm = array(
  240. 'form_attrs' => array(
  241. 'id' => 'bx_std_agents_automators_' . $sAction,
  242. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
  243. 'method' => 'post',
  244. ),
  245. 'params' => array (
  246. 'db' => array(
  247. 'table' => 'sys_agents_automators',
  248. 'key' => 'id',
  249. 'submit_name' => 'do_submit',
  250. ),
  251. ),
  252. 'inputs' => array(
  253. 'model_id' => [
  254. 'type' => 'select',
  255. 'name' => 'model_id',
  256. 'caption' => _t('_sys_agents_automators_field_model_id'),
  257. 'info' => '',
  258. 'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : 0,
  259. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
  260. 'required' => '1',
  261. 'db' => [
  262. 'pass' => 'Int',
  263. ]
  264. ],
  265. 'profile_id' => [
  266. 'type' => 'select',
  267. 'name' => 'profile_id',
  268. 'caption' => _t('_sys_agents_automators_field_profile_id'),
  269. 'info' => _t('_sys_agents_automators_field_profile_id_inf'),
  270. 'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
  271. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  272. 'required' => '0',
  273. 'db' => [
  274. 'pass' => 'Int',
  275. ]
  276. ],
  277. 'type' => [
  278. 'type' => 'select',
  279. 'name' => 'type',
  280. 'caption' => _t('_sys_agents_automators_field_type'),
  281. 'info' => '',
  282. 'value' => $sType,
  283. 'values' => [
  284. 'event' => _t('_sys_agents_automators_field_type_event'),
  285. 'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
  286. 'webhook' => _t('_sys_agents_automators_field_type_webhook'),
  287. ],
  288. 'attrs' => [
  289. 'onchange' => $this->getPageJsObject() . '.onChangeType(this)',
  290. ],
  291. 'required' => '1',
  292. 'db' => [
  293. 'pass' => 'Xss',
  294. ]
  295. ],
  296. 'alert_unit' => [
  297. 'type' => 'hidden',
  298. 'name' => 'alert_unit',
  299. 'caption' => _t('_sys_agents_automators_field_alert_unit'),
  300. 'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
  301. 'tr_attrs' => [
  302. 'style' => $sType != 'event' ? 'display:none' : ''
  303. ],
  304. 'db' => [
  305. 'pass' => 'Xss',
  306. ],
  307. ],
  308. 'alert_action' => [
  309. 'type' => 'hidden',
  310. 'name' => 'alert_action',
  311. 'caption' => _t('_sys_agents_automators_field_alert_action'),
  312. 'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
  313. 'tr_attrs' => [
  314. 'style' => $sType != 'event' ? 'display:none' : ''
  315. ],
  316. 'db' => [
  317. 'pass' => 'Xss',
  318. ],
  319. ],
  320. 'scheduler_time' => [
  321. 'type' => 'hidden',
  322. 'name' => 'scheduler_time',
  323. 'caption' => _t('_sys_agents_automators_field_scheduler_time'),
  324. 'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
  325. 'tr_attrs' => [
  326. 'style' => $sType != 'scheduler' ? 'display:none' : ''
  327. ],
  328. ],
  329. 'message' => [
  330. 'type' => 'textarea',
  331. 'name' => 'message',
  332. 'caption' => _t('_sys_agents_automators_field_message'),
  333. ],
  334. 'submit' => array(
  335. 'type' => 'input_set',
  336. 0 => array (
  337. 'type' => 'submit',
  338. 'name' => 'do_submit',
  339. 'value' => _t('_sys_submit'),
  340. ),
  341. 1 => array (
  342. 'type' => 'reset',
  343. 'name' => 'close',
  344. 'value' => _t('_sys_close'),
  345. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  346. ),
  347. ),
  348. ),
  349. );
  350. return $aForm;
  351. }
  352. protected function _getId()
  353. {
  354. $aIds = bx_get('ids');
  355. if(!empty($aIds) && is_array($aIds))
  356. return array_shift($aIds);
  357. $iId = (int)bx_get('id');
  358. if(!$iId)
  359. return false;
  360. return $iId;
  361. }
  362. }
  363. /** @} */