Jelajahi Sumber

Ticket #4711 - Automations.

AntonLV 2 tahun lalu
induk
melakukan
f8195328e6

+ 56 - 0
inc/classes/BxDolAI.php

@@ -0,0 +1,56 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolAI extends BxDolFactory implements iBxDolSingleton
+{
+    
+    protected function __construct()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
+
+        parent::__construct();
+    }
+
+    /**
+     * Prevent cloning the instance
+     */
+    public function __clone()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
+    }
+
+    /**
+     * Get singleton instance of the class
+     */
+    public static function getInstance()
+    {
+        if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
+            $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolAI();
+
+        return $GLOBALS['bxDolClasses'][__CLASS__];
+    }
+    
+    public function chat($sEndpoint, $sModel, $sApiKey, $aParams, $aMessages)
+    {
+         $aData = array_merge(
+             [
+                'model' => $sModel,
+                'messages' => $aMessages
+            ], 
+            $aParams
+        );
+        $sRv = bx_file_get_contents($sEndpoint, $aData, "post-json", ["Authorization: Bearer ".$sApiKey, 'Content-Type: application/json', 'OpenAI-Beta: assistants=v1']);
+
+        $aRv = json_decode($sRv, true);
+
+        return $aRv['choices'][0]['message']['content'];
+    }
+}

+ 2 - 2
modules/boonex/english/data/langs/system/en.xml

@@ -2150,8 +2150,8 @@
         <string name="_sys_agents_automators_field_status_auto"><![CDATA[Auto]]></string>
         <string name="_sys_agents_automators_field_status_manual"><![CDATA[Manual]]></string>
         <string name="_sys_agents_automators_field_status_ready"><![CDATA[Ready]]></string>
-        <string name=""><![CDATA[]]></string>
-        <string name=""><![CDATA[]]></string>
+        <string name="_sys_agents_automators_err_event_not_found"><![CDATA[Event isn't found, please rephrase your prompt.]]></string>
+        <string name="_sys_agents_automators_err_cannot_get_code"><![CDATA[Can't generate code, please rephrase your prompt.]]></string>
         <string name=""><![CDATA[]]></string>
         
 

+ 2 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -2125,6 +2125,8 @@
         <string name="_sys_agents_automators_field_status_auto"><![CDATA[Авто]]></string>
         <string name="_sys_agents_automators_field_status_manual"><![CDATA[Ручной]]></string>
         <string name="_sys_agents_automators_field_status_ready"><![CDATA[Готовый]]></string>
+        <string name="_sys_agents_automators_err_event_not_found"><![CDATA[Событие не найдено. Перефразируйте запрос.]]></string>
+        <string name="_sys_agents_automators_err_cannot_get_code"><![CDATA[Невозможно сгенерировать код. Перефразируйте запрос.]]></string>
         <string name=""><![CDATA[]]></string>
         <string name=""><![CDATA[]]></string>
         <string name=""><![CDATA[]]></string>

+ 66 - 2
studio/classes/BxDolStudioAgentsAutomatorsCmts.php

@@ -10,19 +10,83 @@
 class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
 {
     protected $_oQueryAgents;
+    protected $_iProfileAi;
 
     public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
         
         $this->_oQueryAgents = new BxDolStudioAgentsQuery();
+
+        $this->_iProfileAi = (int)getParam('sys_profile_bot'); 
     }
-    
+
+    public function getCommentsBlock($aBp = [], $aDp = [])
+    {
+        $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
+        if(empty($aComments['content']))
+            return MsgBox(_t('_error occured'));
+
+        $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
+        if(empty($aAutomator) || !is_array($aAutomator))
+            return MsgBox(_t('_error occured'));
+
+        $aTmplVarsEvent = [];
+        $bTmplVarsEvent = $aAutomator['type'] == 'event';
+        if($bTmplVarsEvent)
+            $aTmplVarsEvent = [
+                'unit' => $aAutomator['alert_unit'],
+                'action' => $aAutomator['alert_action']
+            ];
+        
+        $aTmplVarsScheduler = [];
+        $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
+        if($bTmplVarsScheduler) {
+            $aParams = [];
+            if(!empty($aAutomator['params']))
+                $aParams = json_decode($aAutomator['params'], true);
+
+            $aTmplVarsScheduler = [
+                'time' => $aParams['scheduler_time'],
+            ];
+        }
+
+        $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
+            'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
+            'bx_if:show_event' => [
+                'condition' => $bTmplVarsEvent,
+                'content' => $aTmplVarsEvent
+            ],
+            'bx_if:show_scheduler' => [
+                'condition' => $bTmplVarsScheduler,
+                'content' => $aTmplVarsScheduler
+            ],
+            'code' => $aAutomator['code']
+        ]);
+
+        return [
+            $sAutomator,
+            $aComments['content']
+        ];
+    }
+
+    public function getFormBoxPost($aBp = [], $aDp = [])
+    {
+        $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'start' => 0, 'per_page' => 1]);
+        if(!empty($aComments) && is_array($aComments)) {
+            $aLast = current($aComments);
+            if($aLast['cmt_author_id'] != $this->_iProfileAi)
+                return '';
+        }
+
+        return parent::getFormBoxPost($aBp, $aDp);
+    }
+
     public function isAttachImageEnabled()
     {
         return false;
     }
-    
+
     public function onPostAfter($iCmtId, $aDp = [])
     {
         $mixedResult = parent::onPostAfter($iCmtId, $aDp);

+ 7 - 7
studio/js/agents_automators.js

@@ -22,28 +22,28 @@ BxDolStudioAgentsAutomators.prototype.onChangeType = function(oSelect) {
     var aShow = [];
     switch($(oSelect).val()) {
         case 'event':
-            aHide = ['scheduler_time'];
-            aShow = ['alert_unit', 'alert_action'];
+            aHide = []; //['scheduler_time'];
+            aShow = []; //['alert_unit', 'alert_action'];
             break;
 
         case 'scheduler':
-            aHide = ['alert_unit', 'alert_action'];
-            aShow = ['scheduler_time'];
+            aHide = []; //['alert_unit', 'alert_action'];
+            aShow = []; //['scheduler_time'];
             break;
             
         default:
-            aHide = ['alert_unit', 'alert_action', 'scheduler_time'];
+            aHide = []; //['alert_unit', 'alert_action', 'scheduler_time'];
             aShow = [];
     }
 
     var sHide = '';
     aHide.forEach((sItem) => {
-        sHide += "#bx_std_agents_automators #bx-form-element-" + sItem + ",";
+        sHide += ".bx-form-advanced #bx-form-element-" + sItem + ",";
     });
 
     var sShow = '';
     aShow.forEach((sItem) => {
-        sShow += "#bx_std_agents_automators #bx-form-element-" + sItem + ",";
+        sShow += ".bx-form-advanced #bx-form-element-" + sItem + ",";
     });
 
     $(sHide.substring(0, sHide.length - 1)).bx_anim('hide', this.sAnimationEffect, 0);

+ 4 - 3
studio/template/scripts/BxBaseStudioAgents.php

@@ -72,10 +72,11 @@ class BxBaseStudioAgents extends BxDolStudioAgents
 
     protected function getAutomators()
     {
+        $oTemplate = BxDolStudioTemplate::getInstance();
+
         if(($iId = bx_get('id')) !== false) {
-            $oCmts = BxDolCmts::getObjectInstance('sys_agents_automators', (int)$iId);
-            $aCmts = $oCmts->getCommentsBlock([], ['in_designbox' => false]);
-            return $aCmts['content'];
+            $oCmts = BxDolCmts::getObjectInstance('sys_agents_automators', (int)$iId, true, $oTemplate);
+            return $oCmts->getCommentsBlock();
         }
 
         return $this->getGrid($this->aGridObjects[BX_DOL_STUDIO_AGENTS_TYPE_AUTOMATORS]);

+ 413 - 346
studio/template/scripts/BxBaseStudioAgentsAutomators.php

@@ -1,346 +1,413 @@
-<?php defined('BX_DOL') or die('hack attempt');
-/**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup    UnaView UNA Studio Representation classes
- * @ingroup     UnaStudio
- * @{
- */
-
-class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
-{
-    protected $_sUrlPage;
-
-    
-
-    public function __construct ($aOptions, $oTemplate = false)
-    {
-        parent::__construct ($aOptions, $oTemplate);
-
-        $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
-    }
-
-    public function getJsObject()
-    {
-        return 'oBxDolStudioAgentsAutomators';
-    }
-
-    public function performActionAdd()
-    {
-        $sAction = 'add';
-
-        $aForm = $this->_getForm($sAction);
-        $oForm = new BxTemplFormView($aForm);
-        $oForm->initChecker();
-
-        if($oForm->isSubmittedAndValid()) {
-            $aValsToAdd = [];
-            
-            $iProfileId = $oForm->getCleanValue('profile_id');
-            if(empty($iProfileId)) {
-                $iProfileId = (int)getParam('sys_agents_profile');
-                if(empty($iProfileId))
-                    $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
-
-                $aValsToAdd['profile_id'] = $iProfileId;
-            }
-
-            $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
-            if(!empty($sSchedulerTime))
-                $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
-
-            $sMessage = $oForm->getCleanValue('message');
-            $bMessage = !empty($sMessage);
-
-            if($bMessage)
-                $aValsToAdd['messages'] = 1;
-
-            if(($iId = $oForm->insert($aValsToAdd)) !== false) {
-                if($bMessage && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
-                    $oCmts->add([
-                        'cmt_author_id' => bx_get_logged_profile_id(),
-                        'cmt_parent_id' => 0,
-                        'cmt_text' => $sMessage
-                    ]);
-
-                    //TODO: Send to AI
-                }                
-
-                $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
-            }
-            else
-                $aRes = ['msg' => _t('_sys_txt_error_occured')];
-
-            return echoJson($aRes);
-        } 
-
-        $sFormId = $oForm->getId();
-        $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
-            'form_id' => $sFormId,
-            'form' => $oForm->getCode(true),
-            'object' => $this->_sObject,
-            'action' => $sAction
-        ]));
-
-        return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
-    }
-
-    public function performActionEdit()
-    {
-        $sAction = 'edit';
-
-        $iId = $this->_getId();
-        $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id', 'id' => $iId]);
-
-        $aForm = $this->_getFormEdit($sAction, $aAutomator);
-        $oForm = new BxTemplFormView($aForm);
-        $oForm->initChecker();
-
-        if($oForm->isSubmittedAndValid()) {
-            $aValsToAdd = [];
-            
-            $iProfileId = $oForm->getCleanValue('profile_id');
-            if(empty($iProfileId)) {
-                $iProfileId = (int)getParam('sys_agents_profile');
-                if(empty($iProfileId))
-                    $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
-
-                $aValsToAdd['profile_id'] = $iProfileId;
-            }
-
-            $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
-            if(!empty($sSchedulerTime))
-                $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
-
-            if($oForm->update($iId, $aValsToAdd))
-                $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
-            else
-                $aRes = ['msg' => _t('_sys_txt_error_occured')];
-
-            return echoJson($aRes);
-        } 
-
-        $sFormId = $oForm->getId();
-        $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
-            'form_id' => $sFormId,
-            'form' => $oForm->getCode(true),
-            'object' => $this->_sObject,
-            'action' => $sAction
-        ]));
-
-        return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
-    }
-
-    public function getCode($isDisplayHeader = true)
-    {
-        return $this->_oTemplate->parseHtmlByName('agents_automator.html', [
-            'content' => parent::getCode($isDisplayHeader),
-            'js_object' => $this->getJsObject(),
-            'page_url' => $this->_sUrlPage,
-            'grid_object' => $this->_sObject,
-            'params_divider' => $this->sParamsDivider
-        ]);
-    }
-
-    protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
-    {
-        $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
-        if(!empty($aModel) && is_array($aModel))
-            $mixedValue = $aModel['name'];
-
-        return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
-    }
-
-    protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
-    {
-        return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
-    }
-
-    protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
-    {
-        return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
-    }
-
-    protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
-    {
-        return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
-    }
-
-    protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
-    {
-    	$a['attr'] = array_merge($a['attr'], [
-            "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
-    	]);
-
-    	return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
-    }
-
-    protected function _addJsCss()
-    {
-        parent::_addJsCss();
-
-        $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
-
-        $oForm = new BxTemplStudioFormView([]);
-        $oForm->addCssJs();
-    }
-    
-    protected function _isCheckboxDisabled($aRow)
-    {
-        return false;
-    }
-
-    protected function _getActionsDisabledBehavior($aRow)
-    {
-        return false;
-    }
-
-    protected function _getFormEdit($sAction, $aAutomator = [])
-    {
-        $aForm = $this->_getForm($sAction, $aAutomator);
-        $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
-
-        unset($aForm['inputs']['message']);
-
-        return $aForm;
-    }
-
-    protected function _getForm($sAction, $aAutomator = [])
-    {
-        $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
-
-        if(!empty($aAutomator['params']))
-            $aAutomator['params'] = json_decode($aAutomator['params'], true);
-
-        $aForm = array(
-            'form_attrs' => array(
-                'id' => 'bx_std_agents_automators_' . $sAction,
-                'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
-                'method' => 'post',
-            ),
-            'params' => array (
-                'db' => array(
-                    'table' => 'sys_agents_automators',
-                    'key' => 'id',
-                    'submit_name' => 'do_submit',
-                ),
-            ),
-            'inputs' => array(
-                'model_id' => [
-                    'type' => 'select',
-                    'name' => 'model_id',
-                    'caption' => _t('_sys_agents_automators_field_model_id'),
-                    'info' => '',
-                    'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : 0,
-                    'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
-                    'required' => '1',
-                    'db' => [
-                        'pass' => 'Int',
-                    ]
-                ],
-                'profile_id' => [
-                    'type' => 'select',
-                    'name' => 'profile_id',
-                    'caption' => _t('_sys_agents_automators_field_profile_id'),
-                    'info' => _t('_sys_agents_automators_field_profile_id_inf'),
-                    'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
-                    'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
-                    'required' => '0',
-                    'db' => [
-                        'pass' => 'Int',
-                    ]
-                ],
-                'type' => [
-                    'type' => 'select',
-                    'name' => 'type',
-                    'caption' => _t('_sys_agents_automators_field_type'),
-                    'info' => '',
-                    'value' => $sType,
-                    'values' => [
-                        'event' => _t('_sys_agents_automators_field_type_event'),
-                        'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
-                        'webhook' => _t('_sys_agents_automators_field_type_webhook'),
-                    ],
-                    'attrs' => [
-                        'onchange' => $this->getJsObject() . '.onChangeType(this)',
-                    ],
-                    'required' => '1',
-                    'db' => [
-                        'pass' => 'Xss',
-                    ]
-                ],
-                'alert_unit' => [
-                    'type' => 'text',
-                    'name' => 'alert_unit',
-                    'caption' => _t('_sys_agents_automators_field_alert_unit'),
-                    'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
-                    'tr_attrs' => [
-                        'style' => $sType != 'event' ? 'display:none' : ''
-                    ],
-                    'db' => [
-                        'pass' => 'Xss',
-                    ],
-                ],
-                'alert_action' => [
-                    'type' => 'text',
-                    'name' => 'alert_action',
-                    'caption' => _t('_sys_agents_automators_field_alert_action'),
-                    'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
-                    'tr_attrs' => [
-                        'style' => $sType != 'event' ? 'display:none' : ''
-                    ],
-                    'db' => [
-                        'pass' => 'Xss',
-                    ],
-                ],
-                'scheduler_time' => [
-                    'type' => 'text',
-                    'name' => 'scheduler_time',
-                    'caption' => _t('_sys_agents_automators_field_scheduler_time'),
-                    'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
-                    'tr_attrs' => [
-                        'style' => $sType != 'scheduler' ? 'display:none' : ''
-                    ],
-                ],
-                'message' => [
-                    'type' => 'textarea',
-                    'name' => 'message',
-                    'caption' => _t('_sys_agents_automators_field_message'),
-                ],
-                'submit' => array(
-                    'type' => 'input_set',
-                    0 => array (
-                        'type' => 'submit',
-                        'name' => 'do_submit',
-                        'value' => _t('_sys_submit'),
-                    ),
-                    1 => array (
-                        'type' => 'reset',
-                        'name' => 'close',
-                        'value' => _t('_sys_close'),
-                        'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
-                    ),
-                ),
-
-            ),
-        );
-
-        return $aForm;
-    }
-    
-    protected function _getId()
-    {
-        $aIds = bx_get('ids');
-        if(!empty($aIds) && is_array($aIds))
-            return array_shift($aIds);
-
-        $iId = (int)bx_get('id');
-        if(!$iId)
-            return false;
-
-        return $iId;
-    }
-}
-
-/** @} */
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaView UNA Studio Representation classes
+ * @ingroup     UnaStudio
+ * @{
+ */
+
+class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
+{
+    protected $_sUrlPage;
+
+    
+
+    public function __construct ($aOptions, $oTemplate = false)
+    {
+        parent::__construct ($aOptions, $oTemplate);
+
+        $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
+    }
+
+    public function getJsObject()
+    {
+        return 'oBxDolStudioAgentsAutomators';
+    }
+
+    public function performActionAdd()
+    {
+        $sAction = 'add';
+
+        $aForm = $this->_getForm($sAction);
+        $oForm = new BxTemplFormView($aForm);
+        $oForm->initChecker();
+
+        if($oForm->isSubmittedAndValid()) {
+            $aValsToAdd = [];
+            
+            $iProfileId = $oForm->getCleanValue('profile_id');
+            if(empty($iProfileId)) {
+                $iProfileId = (int)getParam('sys_agents_profile');
+                if(empty($iProfileId))
+                    $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
+
+                $aValsToAdd['profile_id'] = $iProfileId;
+            }
+
+            $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
+            if(!empty($sSchedulerTime))
+                $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
+
+            $sMessage = $oForm->getCleanValue('message');
+            $bMessage = !empty($sMessage);
+            $sMessageAdd = '';
+
+            if($bMessage)
+                $aValsToAdd['messages'] = 1;
+
+            $oAI = BxDolAI::getInstance();
+            
+            $iModel = $oForm->getCleanValue('model');
+            $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iModel]);
+            if(!empty($aModel['params']))
+                $aModel['params'] = json_decode($aModel['params'], true);
+
+            $bIsValid = true;
+            $sType = $oForm->getCleanValue('type');
+            switch($sType) {
+                case 'event':
+                    $aMessages = [
+                        ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_alert_instructions.html' )],
+                        ['role' => 'user', 'content' => $sMessage]
+                    ];
+
+                    $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
+                    if(trim($sResponse) != 'false') {
+                        $sResponse = str_replace(['```json', '```'], '', $sResponse);
+                        $aResponse = json_decode($sResponse, true);
+                        $aValsToAdd = array_merge($aValsToAdd, [
+                            'alert_unit' => $aResponse['alert_unit'],
+                            'alert_action' => $aResponse['alert_action']
+                        ]);
+                        $sMessageAdd = $aResponse['trigger'];
+                    }
+                    else {
+                        $oForm->aInputs['message']['err'] = _t('_sys_agents_automators_err_event_not_found');
+                        $bIsValid = false;
+                    }
+                    break;
+
+                case 'scheduler':
+                    $aMessages = [
+                        ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_cron_instructions.html' )],
+                        ['role' => 'user', 'content' => $sMessage]
+                    ];
+
+                    $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
+                    if(trim($sResponse) != 'false') {
+                        $aValsToAdd['params'] = json_encode(['scheduler_time' => $aResponse]);
+                    }
+                    else {
+                        $oForm->aInputs['message']['err'] = _t('_sys_agents_automators_err_event_not_found');
+                        $bIsValid = false;
+                    }
+                    break;
+            }
+
+            if($bIsValid) {
+                $aMessages = [
+                    ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_' . $sType . '_instructions.html' ) . file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_common_instructions.html' )],
+                    ['role' => 'user', 'content' => $sMessage . $sMessageAdd]
+                ];
+
+                $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
+                if(trim($sResponse) != 'false') {
+                    $sResponse = str_replace(['```php', '```'], '', $sResponse);
+                    $aValsToAdd['code'] = $sResponse;
+                }
+                else {
+                    $oForm->aInputs['message']['err'] = _t('_sys_agents_automators_err_cannot_get_code');
+                    $bIsValid = false;
+                }
+            }
+
+            if($bIsValid) {
+                if(($iId = $oForm->insert($aValsToAdd)) !== false) {
+                    if($bMessage && ($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
+                        $oCmts->add([
+                            'cmt_author_id' => bx_get_logged_profile_id(),
+                            'cmt_parent_id' => 0,
+                            'cmt_text' => $sMessage
+                        ]);
+                    }
+
+                    $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
+                }
+                else
+                    $aRes = ['msg' => _t('_sys_txt_error_occured')];
+
+                return echoJson($aRes);
+            }
+        }
+
+        $sFormId = $oForm->getId();
+        $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
+            'form_id' => $sFormId,
+            'form' => $oForm->getCode(true),
+            'object' => $this->_sObject,
+            'action' => $sAction
+        ]));
+
+        return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
+    }
+
+    public function performActionEdit()
+    {
+        $sAction = 'edit';
+
+        $iId = $this->_getId();
+        $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id', 'id' => $iId]);
+
+        $aForm = $this->_getFormEdit($sAction, $aAutomator);
+        $oForm = new BxTemplFormView($aForm);
+        $oForm->initChecker();
+
+        if($oForm->isSubmittedAndValid()) {
+            $aValsToAdd = [];
+            
+            $iProfileId = $oForm->getCleanValue('profile_id');
+            if(empty($iProfileId)) {
+                $iProfileId = (int)getParam('sys_agents_profile');
+                if(empty($iProfileId))
+                    $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
+
+                $aValsToAdd['profile_id'] = $iProfileId;
+            }
+
+            $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
+            if(!empty($sSchedulerTime))
+                $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
+
+            if($oForm->update($iId, $aValsToAdd))
+                $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
+            else
+                $aRes = ['msg' => _t('_sys_txt_error_occured')];
+
+            return echoJson($aRes);
+        } 
+
+        $sFormId = $oForm->getId();
+        $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
+            'form_id' => $sFormId,
+            'form' => $oForm->getCode(true),
+            'object' => $this->_sObject,
+            'action' => $sAction
+        ]));
+
+        return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
+    }
+
+    public function getCode($isDisplayHeader = true)
+    {
+        return $this->_oTemplate->parseHtmlByName('agents_automator.html', [
+            'content' => parent::getCode($isDisplayHeader),
+            'js_object' => $this->getJsObject(),
+            'page_url' => $this->_sUrlPage,
+            'grid_object' => $this->_sObject,
+            'params_divider' => $this->sParamsDivider
+        ]);
+    }
+
+    protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
+    {
+        $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
+        if(!empty($aModel) && is_array($aModel))
+            $mixedValue = $aModel['name'];
+
+        return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
+    {
+        return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
+    {
+        return parent::_getCellDefault(_t('_sys_agents_automators_field_type_' . $mixedValue), $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
+    {
+        return parent::_getCellDefault(_t('_sys_agents_automators_field_status_' . $mixedValue), $sKey, $aField, $aRow);
+    }
+
+    protected function _getActionTune($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
+    {
+    	$a['attr'] = array_merge($a['attr'], [
+            "onclick" => "window.open('" . $this->_sUrlPage . '&id=' . $aRow['id'] . "', '_self');"
+    	]);
+
+    	return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
+    }
+
+    protected function _addJsCss()
+    {
+        parent::_addJsCss();
+
+        $this->_oTemplate->addJs(['jquery.form.min.js', 'agents_automators.js']);
+
+        $oForm = new BxTemplStudioFormView([]);
+        $oForm->addCssJs();
+    }
+    
+    protected function _isCheckboxDisabled($aRow)
+    {
+        return false;
+    }
+
+    protected function _getActionsDisabledBehavior($aRow)
+    {
+        return false;
+    }
+
+    protected function _getFormEdit($sAction, $aAutomator = [])
+    {
+        $aForm = $this->_getForm($sAction, $aAutomator);
+        $aForm['form_attrs']['action'] .= '&id=' . $aAutomator['id'];
+
+        unset($aForm['inputs']['message']);
+
+        return $aForm;
+    }
+
+    protected function _getForm($sAction, $aAutomator = [])
+    {
+        $sType = isset($aAutomator['type']) ? $aAutomator['type'] : 'event';
+
+        if(!empty($aAutomator['params']))
+            $aAutomator['params'] = json_decode($aAutomator['params'], true);
+
+        $aForm = array(
+            'form_attrs' => array(
+                'id' => 'bx_std_agents_automators_' . $sAction,
+                'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_automators&a=' . $sAction,
+                'method' => 'post',
+            ),
+            'params' => array (
+                'db' => array(
+                    'table' => 'sys_agents_automators',
+                    'key' => 'id',
+                    'submit_name' => 'do_submit',
+                ),
+            ),
+            'inputs' => array(
+                'model_id' => [
+                    'type' => 'select',
+                    'name' => 'model_id',
+                    'caption' => _t('_sys_agents_automators_field_model_id'),
+                    'info' => '',
+                    'value' => isset($aAutomator['model_id']) ? $aAutomator['model_id'] : 0,
+                    'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs']),
+                    'required' => '1',
+                    'db' => [
+                        'pass' => 'Int',
+                    ]
+                ],
+                'profile_id' => [
+                    'type' => 'select',
+                    'name' => 'profile_id',
+                    'caption' => _t('_sys_agents_automators_field_profile_id'),
+                    'info' => _t('_sys_agents_automators_field_profile_id_inf'),
+                    'value' => isset($aAutomator['profile_id']) ? $aAutomator['profile_id'] : 0,
+                    'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
+                    'required' => '0',
+                    'db' => [
+                        'pass' => 'Int',
+                    ]
+                ],
+                'type' => [
+                    'type' => 'select',
+                    'name' => 'type',
+                    'caption' => _t('_sys_agents_automators_field_type'),
+                    'info' => '',
+                    'value' => $sType,
+                    'values' => [
+                        'event' => _t('_sys_agents_automators_field_type_event'),
+                        'scheduler' => _t('_sys_agents_automators_field_type_scheduler'),
+                        'webhook' => _t('_sys_agents_automators_field_type_webhook'),
+                    ],
+                    'attrs' => [
+                        'onchange' => $this->getJsObject() . '.onChangeType(this)',
+                    ],
+                    'required' => '1',
+                    'db' => [
+                        'pass' => 'Xss',
+                    ]
+                ],
+                'alert_unit' => [
+                    'type' => 'hidden',
+                    'name' => 'alert_unit',
+                    'caption' => _t('_sys_agents_automators_field_alert_unit'),
+                    'value' => isset($aAutomator['alert_unit']) ? $aAutomator['alert_unit'] : '',
+                    'tr_attrs' => [
+                        'style' => $sType != 'event' ? 'display:none' : ''
+                    ],
+                    'db' => [
+                        'pass' => 'Xss',
+                    ],
+                ],
+                'alert_action' => [
+                    'type' => 'hidden',
+                    'name' => 'alert_action',
+                    'caption' => _t('_sys_agents_automators_field_alert_action'),
+                    'value' => isset($aAutomator['alert_action']) ? $aAutomator['alert_action'] : '',
+                    'tr_attrs' => [
+                        'style' => $sType != 'event' ? 'display:none' : ''
+                    ],
+                    'db' => [
+                        'pass' => 'Xss',
+                    ],
+                ],
+                'scheduler_time' => [
+                    'type' => 'hidden',
+                    'name' => 'scheduler_time',
+                    'caption' => _t('_sys_agents_automators_field_scheduler_time'),
+                    'value' => isset($aAutomator['params']['scheduler_time']) ? $aAutomator['params']['scheduler_time'] : '',
+                    'tr_attrs' => [
+                        'style' => $sType != 'scheduler' ? 'display:none' : ''
+                    ],
+                ],
+                'message' => [
+                    'type' => 'textarea',
+                    'name' => 'message',
+                    'caption' => _t('_sys_agents_automators_field_message'),
+                ],
+                'submit' => array(
+                    'type' => 'input_set',
+                    0 => array (
+                        'type' => 'submit',
+                        'name' => 'do_submit',
+                        'value' => _t('_sys_submit'),
+                    ),
+                    1 => array (
+                        'type' => 'reset',
+                        'name' => 'close',
+                        'value' => _t('_sys_close'),
+                        'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
+                    ),
+                ),
+
+            ),
+        );
+
+        return $aForm;
+    }
+    
+    protected function _getId()
+    {
+        $aIds = bx_get('ids');
+        if(!empty($aIds) && is_array($aIds))
+            return array_shift($aIds);
+
+        $iId = (int)bx_get('id');
+        if(!$iId)
+            return false;
+
+        return $iId;
+    }
+}
+
+/** @} */