Преглед изворни кода

Ticket #4711 - Automations. Test evaluation.

AntonLV пре 2 година
родитељ
комит
f28fff7187

+ 57 - 35
inc/classes/BxDolAI.php

@@ -21,6 +21,8 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
     protected $_iProfileId;
     
     protected $_aExcludeAlertUnits;
+    
+    protected $_bWriteLog;
 
     protected function __construct()
     {
@@ -36,6 +38,8 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         $this->_aExcludeAlertUnits = [
             'system', 'module_template_method_call'
         ];
+
+        $this->_bWriteLog = true;
     }
 
     /**
@@ -214,7 +218,7 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         
         $oAlert = &$aParams['alert'];
 
-        $this->evalCode($aParams['automator'], true, ['alert' => $oAlert]);
+        $this->evalCode($aParams['automator'], ['alert' => $oAlert]);
     }
 
     protected function _callAutomatorScheduler($aParams = [])
@@ -222,7 +226,7 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         if(!isset($aParams['automator']))
             return false;
         
-        $this->evalCode($aParams['automator'], true);
+        $this->evalCode($aParams['automator']);
     }
 
     protected function _callAutomatorWebhook($aParams = [])
@@ -230,48 +234,67 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         if(!isset($aParams['automator']))
             return false;
 
-        $this->evalCode($aParams['automator'], true);
+        $this->evalCode($aParams['automator']);
     }
 
-    public function evalCode($aAutomator, $isWriteLog = true, $aParams = null)
+    public function evalCode($aAutomator, $aParams = [])
     {
-        ob_start();
-        set_error_handler("evalErrorHandler");
         try {
-            $sCode = '';
-            switch($aAutomator['type']) {
-                case BX_DOL_AI_AUTOMATOR_EVENT:
-                    $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
-                    break;
-                
-                case BX_DOL_AI_AUTOMATOR_SCHEDULER:
-                    $sCode = $aAutomator['code'] . '; onCron();';
-                    break;
-
-                case BX_DOL_AI_AUTOMATOR_WEBHOOK:
-                    $sCode = $aAutomator['code'] . '; onHook();';
-                    break;
-            }
-
-            eval($sCode);
+            $this->_evalCode($aAutomator, $aParams);
         }
         catch (Exception $oException) {
-            if ($isWriteLog){
-                $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
-            }
+            $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
+
+            return $oException->getMessage();
+        }
+    }
+
+    public function emulCode($aAutomator, $aParams = [])
+    {
+        set_error_handler("evalErrorHandler");
+        ob_start();
+
+        try {
+            $this->_evalCode($aAutomator, $aParams);
+        }
+        catch (ParseError $oException) {
             return $oException->getMessage();
-        } 
+        }
         finally {
             $sOutput = ob_get_clean();
-            if($sOutput != '')
-                return "Eval error: " . $sOutput;
-
             restore_error_handler();
+
+            if(!empty($sOutput))
+                return $sOutput;
+        }
+    }
+
+    protected function _evalCode($aAutomator, $aParams = [])
+    {
+        $sCode = '';
+
+        switch($aAutomator['type']) {
+            case BX_DOL_AI_AUTOMATOR_EVENT:
+                $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
+                break;
+
+            case BX_DOL_AI_AUTOMATOR_SCHEDULER:
+                $sCode = $aAutomator['code'] . '; onCron();';
+                break;
+
+            case BX_DOL_AI_AUTOMATOR_WEBHOOK:
+                $sCode = $aAutomator['code'] . '; onHook();';
+                break;
         }
+
+        eval($sCode);
     }
 
     public function log($mixedContents, $sSection = '')
     {
+        if(!$this->_bWriteLog)
+            return;
+
         if(is_array($mixedContents))
             $mixedContents = var_export($mixedContents, true);	
         else if(is_object($mixedContents))
@@ -284,12 +307,11 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
     }
 }
 
-class EvalException extends Exception {}
+class BxEvalException 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);
+    if(!(error_reporting() & $errno))
+        return false; // This error code is not included in error_reporting
+
+    throw new BxEvalException($errstr, $errno);
 }

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

@@ -2175,6 +2175,7 @@
     <string name="_sys_agents_automators_field_status_ready"><![CDATA[Ready]]></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="_sys_agents_automators_txt_eval_response"><![CDATA[Response:<br />]]></string>
     <string name="_sys_agents_providers_txt_active"><![CDATA[Active]]></string>
     <string name="_sys_agents_providers_txt_provider_name"><![CDATA[Name]]></string>
     <string name="_sys_agents_providers_txt_provider_type"><![CDATA[Type]]></string>
@@ -2220,9 +2221,6 @@
     <string name="_sys_agents_helpers_field_result"><![CDATA[Result]]></string>
     <string name="_sys_agents_helpers_popup_add"><![CDATA[Add helper]]></string>
     <string name="_sys_agents_helpers_popup_edit"><![CDATA[Edit helper]]></string>
-    <string name=""><![CDATA[]]></string>
-    <string name=""><![CDATA[]]></string>
-    <string name=""><![CDATA[]]></string>
 
   <string name="_sys_audit_content_info_popup_title"><![CDATA[Information]]></string>
   <string name="_sys_audit_action_edit_content"><![CDATA[Content edited]]></string>

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

@@ -2150,6 +2150,7 @@
         <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="_sys_agents_automators_txt_eval_response"><![CDATA[Ответ:<br />]]></string>
         <string name="_sys_agents_providers_txt_active"><![CDATA[Активный]]></string>
         <string name="_sys_agents_providers_txt_provider_name"><![CDATA[Название]]></string>
         <string name="_sys_agents_providers_txt_provider_type"><![CDATA[Тип]]></string>

+ 6 - 9
studio/classes/BxDolStudioAgentsAutomatorsCmts.php

@@ -79,15 +79,12 @@ class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
         $aAutomator = $this->_oAI->getAutomator($iObjId, true);
         $aAutomator['code'] = $aCmt['cmt_text'];
 
-/*
- * TODO: Improve code evaluation.
- * 
-        $oAlert = new BxDolAlerts('test', 'test', -1, -1, []);
-        $sRv = $this->_oAI->evalCode($aAutomator, false, ['alert' => $oAlert]);
-        if ($sRv != ''){
-            return echoJson(['msg' => 'Error in code: ' . $sRv]);
-        }
-*/
+        $aParams = [];
+        if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT)
+            $aParams['alert'] = new BxDolAlerts('test', 'test', 0);
+
+        if(($sResult = $this->_oAI->emulCode($aAutomator, $aParams)) != '')
+            return echoJson(['msg' => _t('_sys_agents_automators_txt_eval_response') . $sResult]);
 
         if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
             return echoJson([]);