BxBaseStudioAgentsAutomators.php 16 KB

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