Browse Source

#5406 AI Agents revamp: alerts trigger

Aleksandr Trofimov 5 tháng trước cách đây
mục cha
commit
4cdf90cf78

+ 1 - 1
inc/classes/BxDolAI.php

@@ -379,7 +379,7 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         $sParams = is_string($mixedParams) ? $mixedParams : json_encode($mixedParams);
         $sParams = is_string($mixedParams) ? $mixedParams : json_encode($mixedParams);
 
 
         // update sample data
         // update sample data
-        $a = ['alert' => 'alert_sample', 'webhook' => 'webhook_sample'];
+        $a = ['webhook' => 'webhook_sample'];
         if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
         if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
             $this->_oDb->updateAgentField($aAgent['id'], $a[$sType], $sParams);
             $this->_oDb->updateAgentField($aAgent['id'], $a[$sType], $sParams);
         }
         }

+ 8 - 6
inc/classes/BxDolAIQuery.php

@@ -942,11 +942,12 @@ class BxDolAIQuery extends BxDolDb
         return $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
         return $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
     }
     }
 
 
-    public function getAlert($s) {
+    static public function getAlert($s) {
         if (!$s)
         if (!$s)
-            return false;
+            return false;        
         $a = explode(':', $s);
         $a = explode(':', $s);
-        return $this->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
+        $oDb = BxDolDb::getInstance();
+        return $oDb->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
             'unit' => $a[0] ?? '',
             'unit' => $a[0] ?? '',
             'action' => $a[1] ?? '',
             'action' => $a[1] ?? '',
         ]);
         ]);
@@ -969,15 +970,16 @@ class BxDolAIQuery extends BxDolDb
         return $aValues;
         return $aValues;
     }
     }
 
 
-    public function getAlertDesc($sAlert) 
+    static public function getAlertDesc($sAlert) 
     {
     {
+        $oDb = BxDolDb::getInstance();
         [$sUnit, $sAction] = explode(':', $sAlert);
         [$sUnit, $sAction] = explode(':', $sAlert);
-        $sDesc = $this->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
+        $sDesc = $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
             'unit' => $sUnit,
             'unit' => $sUnit,
             'action' => $sAction,
             'action' => $sAction,
         ]);
         ]);
         if (!$sDesc) {
         if (!$sDesc) {
-            $this->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
+            $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
                 'action' => $sAction,
                 'action' => $sAction,
             ]);
             ]);
         }
         }

+ 0 - 1
inc/classes/BxDolAIToolContentSearch.php

@@ -56,7 +56,6 @@ class BxDolAIToolContentSearch extends BxDolAITool
 
 
     public function __invoke(string $keyword, array $sections, ?int $limit = 5): array
     public function __invoke(string $keyword, array $sections, ?int $limit = 5): array
     {
     {
-echoDbgLog("Invoking content search tool with keyword: {$keyword}, sections: " . implode(',', $sections) . ", limit: {$limit}");
         $a = bx_srv('system', 'search', [$keyword, $sections, $limit], 'TemplServiceContent');
         $a = bx_srv('system', 'search', [$keyword, $sections, $limit], 'TemplServiceContent');
         if (!$a)
         if (!$a)
             return ["error" => "No content items found"];
             return ["error" => "No content items found"];

+ 35 - 6
inc/classes/BxDolAiAgent.php

@@ -38,14 +38,27 @@ class BxDolAiAgent extends RAG
 
 
     protected function instructions(): string
     protected function instructions(): string
     {
     {
-        $aToolsUsage = !empty($this->aAgent['prompt_tools']) ? [$this->aAgent['prompt_tools']] : [];
-        $aToolsUsage[] = "Always use agent profile id = {$this->aAgent['profile_id']} as author ('profile_id' tool parameter), unless explicitly specified in the user instructions, no other variants strictly.";
+        $aPromptSystem = !empty($this->aAgent['prompt_system']) ? [$this->aAgent['prompt_system']] : [];
+        $aPromptSteps = !empty($this->aAgent['prompt_steps']) ? [$this->aAgent['prompt_steps']] : [];
+        $aPromptTools = !empty($this->aAgent['prompt_tools']) ? [$this->aAgent['prompt_tools']] : [];
+        $aPromptOutput = !empty($this->aAgent['prompt_output']) ? [$this->aAgent['prompt_output']] : [];
+
+        $aPromptTools[] = "Always use agent profile id = {$this->aAgent['profile_id']} as author ('profile_id' tool parameter), unless explicitly specified in the user instructions, no other variants strictly.";
+
+        if ('alert' == $this->aAgent['trigger']) {
+            $aPromptTools[] = "Return array only, modified version of 'extra' array. Modifyable keys: " . $this->getAlertTriggerModifyableKeys() . ".";             
+
+            $sDesc = trim(BxDolAiQuery::getAlertDesc($this->aAgent['alert']));
+            if ('.' != mb_substr($sDesc, -1))
+                $sDesc .= '.';
+            $aPromptSystem[] = $sDesc;
+        }
 
 
         $oPrompt = new NeuronAI\Agent\SystemPrompt(
         $oPrompt = new NeuronAI\Agent\SystemPrompt(
-            background: [$this->aAgent['prompt_system']],
-            steps: !empty($this->aAgent['prompt_steps']) ? [$this->aAgent['prompt_steps']] : [],
-            output: !empty($this->aAgent['prompt_output']) ? [$this->aAgent['prompt_output']] : [],
-            toolsUsage: $aToolsUsage
+            background: $aPromptSystem,
+            steps: $aPromptSteps,
+            output: $aPromptOutput,
+            toolsUsage: $aPromptTools
         );
         );
         return (string) $oPrompt;
         return (string) $oPrompt;
     }
     }
@@ -101,4 +114,20 @@ class BxDolAiAgent extends RAG
             $s .=  ':' . $this->aParams['chat_history_subindex'];
             $s .=  ':' . $this->aParams['chat_history_subindex'];
         return $s;
         return $s;
     }
     }
+
+    protected function getAlertTriggerModifyableKeys(): string
+    {
+        if ('alert' != $this->aAgent['trigger'])
+            return 'none';
+
+        $aAlert = BxDolAiQuery::getAlert($this->aAgent['alert']);
+        if (!$aAlert)
+            return 'none';
+
+        $aKeys = json_decode($aAlert['extra_refs']);
+        if (!$aKeys)
+            return 'none';
+
+        return implode(',', $aKeys);
+    }
 }
 }

+ 15 - 4
inc/classes/BxDolAlerts.php

@@ -152,14 +152,25 @@ class BxDolAlerts extends BxDol
         $oAi = BxDolAI::getInstance();
         $oAi = BxDolAI::getInstance();
         if($aAgents = $oAi->getAgentsByAlertUnitAndAction($this->sUnit, $this->sAction)) {
         if($aAgents = $oAi->getAgentsByAlertUnitAndAction($this->sUnit, $this->sAction)) {
             foreach($aAgents as $a) {
             foreach($aAgents as $a) {
-                $oAi->callAgent('alert', $a, [
+                $sExtraJSON = $oAi->callAgent('alert', $a, [
                     'trigger' => 'alert',
                     'trigger' => 'alert',
-                    'object' => $this->iObject,
-                    'sender' => $this->iSender,
+                    'object_id' => $this->iObject,
+                    'sender_profile_id' => $this->iSender,
                     'unit' => $this->sUnit, 
                     'unit' => $this->sUnit, 
                     'action' => $this->sAction,
                     'action' => $this->sAction,
-                    'extra' => $this->aExtras
+                    'extra' => $this->aExtras,
                 ]);
                 ]);
+
+                $aExtra = [];
+                if ($sExtraJSON && 0 === bx_mb_strpos($sExtraJSON, '{'))
+                    $aExtra = json_decode($sExtraJSON, true);
+                
+                if ($aExtra && is_array($aExtra)) {
+                    foreach ($aExtra as $k => $v) {
+                        if (isset($this->aExtras[$k]))
+                            $this->aExtras[$k] = $v;
+                    }
+                }
             }
             }
         }
         }
 
 

+ 1 - 1
studio/template/scripts/BxBaseStudioAgentsAgents.php

@@ -375,7 +375,7 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                         'text' => _t('_sys_agents_waiting_for_sample_data'),
                         'text' => _t('_sys_agents_waiting_for_sample_data'),
 
 
                         'payload-visibility' => !empty($aAgent['alert']) ? 'block' : 'none',
                         'payload-visibility' => !empty($aAgent['alert']) ? 'block' : 'none',
-                        'desc' => $this->_oDb->getAlertDesc($aAgent['alert']),
+                        'desc' => $aAgent ? $this->_oDb->getAlertDesc($aAgent['alert']) : '',
                         'payload' => !empty($aAgent['alert']) ? json_encode($this->_getAlertPayload($aAgent['alert']), JSON_PRETTY_PRINT) : '',
                         'payload' => !empty($aAgent['alert']) ? json_encode($this->_getAlertPayload($aAgent['alert']), JSON_PRETTY_PRINT) : '',
                     ]),
                     ]),
                 ],
                 ],