AntonLV 2 лет назад
Родитель
Сommit
886dda19ea

+ 4 - 0
studio/classes/BxDolStudioAgentsAutomators.php

@@ -13,6 +13,8 @@ class BxDolStudioAgentsAutomators extends BxTemplStudioGrid
 
     protected $_sCmts;
 
+    protected $_iProfileAi;
+
     public function __construct ($aOptions, $oTemplate = false)
     {
         parent::__construct ($aOptions, $oTemplate);
@@ -20,6 +22,8 @@ class BxDolStudioAgentsAutomators extends BxTemplStudioGrid
         $this->_oDb = new BxDolStudioAgentsQuery();
 
         $this->_sCmts = 'sys_agents_automators';
+
+        $this->_iProfileAi = (int)getParam('sys_profile_bot');
     }
 
     protected function _delete ($mixedId)

+ 26 - 4
studio/classes/BxDolStudioAgentsAutomatorsCmts.php

@@ -11,14 +11,16 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
 {
     protected $_oQueryAgents;
     protected $_iProfileAi;
+    protected $_bAuto;
 
     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'); 
+        $this->_bAuto = false;
     }
 
     public function getCommentsBlock($aBp = [], $aDp = [])
@@ -87,16 +89,36 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
         return false;
     }
 
+    public function addAuto($aValues)
+    {
+        $this->_bAuto = true;
+        $mixedResult = $this->add($aValues);
+
+        $this->_bAuto = false;
+        return $mixedResult;
+    }
+
     public function onPostAfter($iCmtId, $aDp = [])
     {
         $mixedResult = parent::onPostAfter($iCmtId, $aDp);
-        if($mixedResult !== false) {
+        if(!$this->_bAuto && $mixedResult !== false) {
             $iObjId = (int)$this->getId();
 
-            $aCmt = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
             $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id_full', 'id' => $iObjId]);
 
-            //TODO: Send to AI $aCmt['cmt_text']
+            $aMessages = [
+                ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_' . $aAutomator['type'] . '_instructions.html' ) . file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_common_instructions.html' )],
+            ];
+            
+            $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
+            foreach($aComments as $aComment) {
+                $aMessages[] = [
+                    'role' => (int)$aComment['cmt_author_id'] == $this->_iProfileAi ? 'assistant' : 'user',
+                    'content' => $aComment['cmt_text']
+                ];
+            }
+
+            //TODO: Send to AI $aMessages
         }
 
         return $mixedResult;

+ 24 - 12
studio/template/scripts/BxBaseStudioAgentsAutomators.php

@@ -10,9 +10,7 @@
 
 class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
 {
-    protected $_sUrlPage;
-
-    
+    protected $_sUrlPage;   
 
     public function __construct ($aOptions, $oTemplate = false)
     {
@@ -53,13 +51,14 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
             $sMessage = $oForm->getCleanValue('message');
             $bMessage = !empty($sMessage);
             $sMessageAdd = '';
+            $sMessageResponse = '';
 
             if($bMessage)
                 $aValsToAdd['messages'] = 1;
 
             $oAI = BxDolAI::getInstance();
-            
-            $iModel = $oForm->getCleanValue('model');
+
+            $iModel = $oForm->getCleanValue('model_id');
             $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iModel]);
             if(!empty($aModel['params']))
                 $aModel['params'] = json_decode($aModel['params'], true);
@@ -115,7 +114,7 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
                 $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
                 if(trim($sResponse) != 'false') {
                     $sResponse = str_replace(['```php', '```'], '', $sResponse);
-                    $aValsToAdd['code'] = $sResponse;
+                    $sMessageResponse = $sResponse;
                 }
                 else {
                     $oForm->aInputs['message']['err'] = _t('_sys_agents_automators_err_cannot_get_code');
@@ -125,12 +124,25 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
 
             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
-                        ]);
+                    if(($oCmts = BxDolCmts::getObjectInstance($this->_sCmts, $iId)) !== null) {
+                        if($bMessage)
+                            $oCmts->addAuto([
+                                'cmt_author_id' => bx_get_logged_profile_id(),
+                                'cmt_parent_id' => 0,
+                                'cmt_text' => $sMessage
+                            ]);
+
+                        if($sMessageResponse) {
+                            sleep(1);
+
+                            $oCmts->addAuto([
+                                'cmt_author_id' => $this->_iProfileAi,
+                                'cmt_parent_id' => 0,
+                                'cmt_text' => $sMessageResponse
+                            ]);
+
+                            $aValsToAdd['messages']++;
+                        }
                     }
 
                     $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];