浏览代码

Merge branch 'master' of https://github.com/unaio/una

AntonLV 2 年之前
父节点
当前提交
fb4f43a7fa
共有 3 个文件被更改,包括 87 次插入 和 16 次删除
  1. 52 7
      inc/classes/BxDolAI.php
  2. 35 8
      studio/classes/BxDolStudioAgentsAutomatorsCmts.php
  3. 0 1
      studio/template/css/agents.css

+ 52 - 7
inc/classes/BxDolAI.php

@@ -149,19 +149,64 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
     {
         if(!isset($aParams['automator'], $aParams['alert']) || !is_a($aParams['alert'], 'BxDolAlerts'))
             return false;
-
+        
         $oAlert = &$aParams['alert'];
 
-        //TODO: Call the code in appropriate way using data from $oAlert
-        eval($aParams['automator']['code']);
+        $this->evalCode($aParams['automator'], true, ['alert' => $oAlert]);
     }
 
     protected function _callAutomatorScheduler($aParams = [])
     {
         if(!isset($aParams['automator']))
             return false;
-
-        //TODO: Call the code in appropriate way
-        eval($aParams['automator']['code']);
+        
+        $this->evalCode($aParams['automator'], true);
+    }
+    
+    
+    public function evalCode($aAutomator, $isWriteLog = true, $aParams = null)
+    {
+        ob_start();
+        set_error_handler("evalErrorHandler");
+        try{
+             $sCode = '';
+             if ($aAutomator['type'] == 'scheduler'){
+                 $sCode = $aAutomator['code'] . '; onCron();';
+             }
+             
+             if ($aAutomator['type'] == 'event'){
+                 $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';  
+             }
+             
+             
+             eval($sCode);
+            
+        }
+        catch (Exception $oException) {
+            if ($isWriteLog){
+                $this->writeLog($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
+            }
+            return $oException->getMessage();
+        } finally {
+             $sOutput = ob_get_clean();
+             if ($sOutput != '')
+                return "Eval error: " . $sOutput;
+            restore_error_handler();
+        }
+    }
+    
+    private function writeLog($sString)
+	{
+        bx_log('sys_agents', $sString);
+	}
+}
+
+class EvalException extends Exception {}
+
+function evalErrorHandler($errno, $errstr, $errfile, $errline) {
+    if (!(error_reporting() & $errno)) {
+        // This error code is not included in error_reporting
+        return false;
     }
-}
+    throw new EvalException($errstr, $errno);
+}

+ 35 - 8
studio/classes/BxDolStudioAgentsAutomatorsCmts.php

@@ -14,11 +14,18 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
     protected $_iProfileIdAi;
 
     protected $_bAuto;
+    
+    protected $_oAI;
 
     public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
 
+        if ($oTemplate)
+            $this->_oTemplate = $oTemplate;
+        else
+            $this->_oTemplate = BxDolStudioTemplate::getInstance();
+        
         $this->_sTmplNameItemContent = 'agents_comment_content.html';
         $this->_bLiveUpdates = false;
 
@@ -27,6 +34,8 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
         $this->_iProfileIdAi = BxDolAI::getInstance()->getProfileId();
 
         $this->_bAuto = false;
+        
+        $this->_oAI = BxDolAI::getInstance();
     }
 
     public function actionGetCmt ()
@@ -64,6 +73,19 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
         $aCmt = $this->getCommentRow($iCmt);
         if(empty($aCmt) || !is_array($aCmt))
             return echoJson([]);
+        
+        $iObjId = (int)$this->getId();
+        
+        $aAutomator = $this->_oAI->getAutomator($iObjId, true);
+        $aAutomator['code'] = $aCmt['cmt_text'];
+        
+        $oAlert = new BxDolAlerts('test', 'test', -1, -1, []);
+        $sRv = $this->_oAI->evalCode($aAutomator, false, ['alert' => $oAlert]);
+        
+        
+        if ($sRv != ''){
+            return echoJson(['msg' => 'Error in code: ' . $sRv]);
+        }
 
         if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
             return echoJson([]);
@@ -158,15 +180,13 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
             return $mixedResult;
         
         $iObjId = (int)$this->getId();
-
-        $oAI = BxDolAI::getInstance();
+        
+        $aAutomator = $this->_oAI->getAutomator($iObjId, true);
 
         $aMessages = [
-            ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($aAutomator['type'], true)],
+            ['role' => 'system', 'content' => $this->_oAI->getAutomatorInstructions($aAutomator['type'], true)],
         ];
 
-        $aAutomator = $oAI->getAutomator($iObjId, true);
-
         $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
         if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
             $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
@@ -180,7 +200,7 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
             ];
         }
 
-        $sResponse = $oAI->chat($aAutomator['model_url'], $aAutomator['model_model'], $aAutomator['model_key'], $aAutomator['model_params'], $aMessages);
+        $sResponse = $this->_oAI->chat($aAutomator['model_url'], $aAutomator['model_model'], $aAutomator['model_key'], $aAutomator['model_params'], $aMessages);
         if($sResponse != 'false') {
             $mixedResultAuto = $this->addAuto([
                 'cmt_author_id' => $this->_iProfileIdAi,
@@ -201,7 +221,7 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
             return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
 
         return $this->_oTemplate->parseLink('javascript:void(0)', _t('_sys_agents_automators_btn_approve'), [
-            'class' => 'bx-btn bx-btn-small',
+            'class' => 'bx-btn bx-btn-small bx-btn-primary',
             'onclick' => $this->getPageJsObject() . '.approveCode(this, ' . $aCmt['cmt_id'] . ')',
         ]);
     }
@@ -220,12 +240,19 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
     {
         $aResult = parent::_getTmplVarsText($aCmt);
 
-        //TODO: Custom template for AI responces.
         if((int)$aCmt['cmt_author_id'] == $this->_iProfileIdAi)
             $aResult['text'] = '<pre>' . $aResult['text'] . '</pre>';
 
         return $aResult;
     }
+    
+    protected function _getForm($sAction, $iId)
+    {
+        $aResult = parent::_getForm($sAction, $iId);
+
+        $aResult->aInputs['cmt_text']['db']['pass'] = 'xss';
+        return $aResult;
+    }
 }
 
 /** @} */

+ 0 - 1
studio/template/css/agents.css

@@ -7,7 +7,6 @@
 }
 .bx-def-vanilla-html pre,
 .cmt-body.bx-def-vanilla-html pre{
-    max-width: 60%;
     max-height: 20rem;
 
     border-width: 0;