1
0
Эх сурвалжийг харах

#5406 AI Agents revamp: form input trigger

Aleksandr Trofimov 3 сар өмнө
parent
commit
db2c174796

+ 27 - 0
inc/classes/BxDolAIQuery.php

@@ -975,6 +975,33 @@ class BxDolAIQuery extends BxDolDb
         return $aValues;
     }
 
+    public function getFormDisplay($sObject)
+    {
+        $aDisplays = $this->getColumn("SELECT `display_name` FROM `sys_form_displays` WHERE `object` = :object", ['object' => $sObject]);
+        return $aDisplays ? current($aDisplays) : null;
+    }
+
+    public function getFormObjects()
+    {
+        $aValues = [];
+        $aForms = $this->getAll("SELECT `f`.`object`, `f`.`title`, `f`.`module`, `m`.`title` AS `module_title` FROM `sys_objects_form` AS f INNER JOIN `sys_modules` as `m` ON `m`.`name` = `f`.`module` ORDER BY `f`.`module`, `f`.`object` ASC");
+        foreach ($aForms as $r) {
+            $sDisplay = $this->getFormDisplay($r['object']);
+            if (!$sDisplay) 
+                continue;
+            $oForm = BxDolForm::getObjectInstance($r['object'], $sDisplay);
+            $sFormId = $oForm->getId();
+            $aValues[$r['object']] = [
+                'form_id' => $sFormId,
+                'form_object' => $r['object'],
+                'module' => $r['module'],
+                'module_title' => $r['module_title'],
+                'form_title' => _t($r['title']),
+            ];
+        }
+        return $aValues;
+    }
+
     static public function getAlertDesc($sAlert) 
     {
         if (!$sAlert)

+ 3 - 1
install/sql/system.sql

@@ -7193,12 +7193,14 @@ CREATE TABLE `sys_agents_agents` (
   `tools_max_run` int(11) NOT NULL DEFAULT 10,
   `chat_history_context` int(11) NOT NULL DEFAULT 50000,
   `vector_store_id` int(11) NOT NULL,
-  `trigger` enum('alert','scheduler','webhook','manual','agent','message') NOT NULL DEFAULT 'message',
+  `trigger` enum('alert','scheduler','webhook','manual','agent','message','form-field') NOT NULL DEFAULT 'message',
   `async` tinyint(4) NOT NULL DEFAULT 0,
   `alert` varchar(192) NOT NULL DEFAULT '',
   `scheduler_cron` varchar(64) NOT NULL,
   `webhook_key` varchar(255) NOT NULL,
   `webhook_sample` text NOT NULL,
+  `form_object` varchar(128) NOT NULL,
+  `form_input` varchar(128) NOT NULL,
   `agent_sample` text NOT NULL,
   `message_profile_id` int(11) NOT NULL,
   `added` int(11) UNSIGNED NOT NULL DEFAULT 0,

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

@@ -2313,7 +2313,7 @@
     <string name="_sys_agents_automators_field_type_scheduler"><![CDATA[Scheduler]]></string>
     <string name="_sys_agents_automators_field_type_webhook"><![CDATA[Webhook]]></string>
     <string name="_sys_agents_field_trigger"><![CDATA[Trigger]]></string>
-    <string name="_sys_agents_field_trigger_info"><![CDATA[Trigger which will initiate agent to run. Alert — system event notification. Scheduler — time-based scheduled execution. Webhook — external HTTP trigger. Manual — user-initiated run. Agent — triggered by another agent. Message — triggered by chat message.]]></string>
+    <string name="_sys_agents_field_trigger_info"><![CDATA[Trigger which will initiate agent to run. Alert — system event notification. Scheduler — time-based scheduled execution. Webhook — external HTTP trigger. Manual — user-initiated run. Agent — triggered by another agent. Message — triggered by chat message. Form field — compose form field value.]]></string>
     <string name="_sys_agents_field_alert_sample"><![CDATA[Alert: payload]]></string>
     <string name="_sys_agents_waiting_for_sample_data_alert"><![CDATA[Please select an alert to see its payload.]]></string>
     <string name="_sys_agents_waiting_for_sample_data_webhook"><![CDATA[Trigger webhook to see its payload.]]></string>

+ 137 - 2
studio/template/scripts/BxBaseStudioAgentsAgents.php

@@ -37,6 +37,19 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
         ]);
     }
 
+    public function performActionGetFormInputs()
+    {
+        $aFormInputs = $this->_getFormInputs(bx_get('form_object'));
+        if (!$aFormInputs) {
+            echoJson(['code' => 404, 'msg' => _t('_sys_not_available')]);
+            return;
+        }
+        echoJson([
+            'code' => $aFormInputs ? 200 : 404,
+            'form_inputs' => $aFormInputs,
+        ]);
+    }
+
     public function performActionManual()
     {
         $iId = $this->_getId();        
@@ -343,7 +356,7 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                     'type' => 'select',
                     'name' => 'trigger',
                     'caption' => _t('_sys_agents_field_trigger'),
-                    'info' => _t('_sys_agents_field_trigger_info'),                    
+                    'info' => _t('_sys_agents_field_trigger_info'),
                     'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
                     'values' => [
                         'alert' => 'alert',
@@ -352,6 +365,7 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                         'manual' => 'manual',
                         'agent' => 'agent',
                         'message' => 'message',
+                        'form-field' => 'form field',
                     ],
                     'attrs' => [
                         'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
@@ -466,7 +480,36 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                     ],
                 ],
 
-                'message_section_end' => array(
+                'form_field_section' => array(
+                    'type' => 'block_header',
+                    'caption' => 'Trigger - form field',
+                    'collapsable' => true,
+                    'collapsed' => true,
+                ),
+
+                'form_object' => [
+                    'type' => 'custom',
+                    'name' => 'form_object',
+                    'caption' => _t('_sys_agents_form_object'),
+                    'info' => _t('_sys_agents_form_object_info'),
+                    'content' => $this->_oTemplate->parseHtmlByName('agents_agents_form_object_select.html', $this->_getFormObjectValues(isset($aAgent['form_object']) ? $aAgent['form_object'] : '')),
+                    'db' => [
+                        'pass' => 'Xss',
+                    ],
+                ],
+
+                'form_input' => [
+                    'type' => 'custom',
+                    'name' => 'form_input',
+                    'caption' => _t('_sys_agents_form_input'),
+                    'info' => _t('_sys_agents_form_input_info'),
+                    'content' => $this->_oTemplate->parseHtmlByName('agents_agents_form_input_select.html', $this->_getFormInputValues(isset($aAgent['form_id']) ? $aAgent['form_id'] : '', isset($aAgent['form_input']) ? $aAgent['form_input'] : '')),
+                    'db' => [
+                        'pass' => 'Xss',
+                    ],
+                ],
+
+                'form_field_section_end' => array(
                     'type' => 'block_end',
                 ),
 
@@ -626,6 +669,98 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
         return $a;
     }
 
+    protected function _getFormObjectValues($sValue)
+    {
+        $aFormObjects = $this->_oDb->getFormObjects();
+        $a = [
+            'sys_stat_not_available' => _t('_sys_not_available'),
+            'grid_object' => $this->_sObject,
+            'bx_repeat:form_objects' => [
+                [
+                    'title' => _t('_sys_please_select'), 
+                    'title_attr' => _t('_sys_please_select'), 
+                    'value' => '', 
+                    'bx_if:sel' => [
+                        'condition' => false,
+                        'content' => ['sel' => '']
+                    ]
+                ]
+            ]
+        ];
+        foreach ($aFormObjects as $k => $r) {
+            $a['bx_repeat:form_objects'][] = [
+                'title' => $r['module_title'] . ': ' . $r['form_title'],
+                'title_attr' => bx_html_attribute($r['module_title'] . ': ' . $r['form_title']),
+                'value' => $r['form_object'],
+                // 'form_id' => $r['form_id'],
+                // 'module' => $r['module'],
+                // 'module_title' => $r['module_title'],
+                // 'form_title' => $r['form_title'],
+                'bx_if:sel' => [
+                    'condition' => $sValue == $r['form_object'],
+                    'content' => ['sel' => $sValue == $r['form_object'] ? 'selected' : ''],
+                ]
+            ];
+        }
+        return $a;
+    }
+
+    protected function _getFormInputs($sFormObject)
+    {
+        $sDisplay = $this->_oDb->getFormDisplay($sFormObject);
+        $oForm = $sDisplay ? BxDolForm::getObjectInstance($sFormObject, $sDisplay) : null;
+        $aFormInputs = $oForm ? $oForm->aInputs : [];
+        if (!$aFormInputs)
+            return [];
+        $aRet = [];
+        foreach ($aFormInputs as $k => $r) {
+            if ($r['type'] == 'text' || $r['type'] == 'textarea') {
+                $aRet[$r['name']] = [
+                    'name' => $r['name'],
+                    'caption' => $r['caption'],
+                ];
+            }
+        }
+        return $aRet;
+    }
+
+    protected function _getFormInputValues($sFormObject, $sFormInput)
+    {
+        $aFormInputs = $this->_getFormInputs($sFormObject);
+
+        $a = [
+            'bx_repeat:form_inputs' => [
+                [
+                    'title' => _t('_sys_please_select'), 
+                    'title_attr' => _t('_sys_please_select'), 
+                    'value' => '', 
+                    'bx_if:sel' => [
+                        'condition' => false,
+                        'content' => ['sel' => '']
+                    ]
+                ]
+            ]
+        ];
+
+        if ($aFormInputs) {
+            foreach ($aFormInputs as $k => $r) {
+                $a['bx_repeat:form_ids'][] = [
+                    'title' => $r['name'] . ' - ' . $r['caption'],
+                    'title_attr' => bx_html_attribute($r['name'] . (!empty($r['caption']) ? ' - ' . $r['caption'] : '')),
+                    'value' => $r['name'],
+                    'bx_if:sel' => [
+                        'condition' => $sFormInput == $r['name'],
+                        'content' => [
+                            'sel' => $sFormInput == $r['name'] ? 'selected' : '',
+                        ]
+                    ]
+                ];
+            }
+        }
+
+        return $a;
+    }
+
     protected function _getUniqName($sName)
     {
         return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);