Просмотр исходного кода

#5406 AI Agents revamp: agents (message trigger)

Aleksandr Trofimov 7 месяцев назад
Родитель
Сommit
7d4db13f48

+ 12 - 6
inc/classes/BxDolAI.php

@@ -700,23 +700,24 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         return $aAutomators;
     }
 
-    public function callAgent($sType, $aAgent, $aParams = [])
+    public function callAgent($sType, $aAgent, $mixedParams = [])
     {
+        $sParams = is_string($mixedParams) ? $mixedParams : json_encode($mixedParams);
+
         // update sample data
         $a = ['alert' => 'alert_sample', 'webhook' => 'webhook_sample'];
         if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
-            $this->updateAgentField($aAgent['id'], $a[$sType], json_encode($aParams));
+            $this->updateAgentField($aAgent['id'], $a[$sType], $sParams);
         }
 
+        // call agent
         $o = self::getAgentInstance($aAgent['id']);
         if (!$o)
             return false;
 
-        $o->chat(new UserMessage($aParams));
-
-        $o->run();
+        $oMessage = $o->chat(new NeuronAI\Chat\Messages\UserMessage($sParams))->getMessage();
 
-        return $o->getMessage();
+        return $oMessage->getContent();
     }
 
     public function getAgentsByAlertUnitAndAction($sUnit, $sAction)
@@ -724,6 +725,11 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
         return $this->_oDb->getAgentsByAlertUnitAndAction($sUnit, $sAction);
     }
 
+    public function getAgentsByProfileId($iProfileId)
+    {
+        return $this->_oDb->getAgentsByProfileId($iProfileId);
+    }
+
     public function callAutomator($sType, $aParams = [])
     {
         $sMethod = '_callAutomator' . bx_gen_method_name($sType);

+ 5 - 0
inc/classes/BxDolAIQuery.php

@@ -896,6 +896,11 @@ class BxDolAIQuery extends BxDolDb
         return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `alert_unit` = :unit AND `alert_action` = :action", ['unit' => $sUnit, 'action' => $sAction]);
     }
 
+    public function getAgentsByProfileId($iProfileId)
+    {
+        return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `profile_id` = :profile", ['profile' => $iProfileId]);
+    }
+
     public function updateAgentField($iId, $sField, $sValue)
     {
         return $this->query("UPDATE `sys_agents_agents` SET `$sField` = :value WHERE `id` = :id", ['value' => $sValue, 'id' => $iId]);

+ 64 - 0
inc/classes/BxDolAiAlertResponse.php

@@ -0,0 +1,64 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    BaseGeneral Base classes for modules
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxDolAiAlertResponse extends BxDolAlertsResponse
+{
+    public function response($oAlert)
+    {        
+        if ('bx_messenger' == $oAlert->sUnit && 'got_jot' == $oAlert->sAction) {
+            $iProfileIdSender = $oAlert->iSender;
+            $this->processMesssage ($oAlert->iSender, $oAlert->aExtras['recipient_id'], $oAlert->iObject, $oAlert->aExtras['subobject_id'], $oAlert->aExtras['subobject_info']);
+        }
+    }
+
+    protected function processMesssage ($iSender, $iRecipient, $iLotId, $iJotId, $aJotInfo) 
+    {
+        if ($iSender == $iRecipient)
+            return;
+
+        // call agents
+        $oAi = BxDolAI::getInstance();
+        if($aAgents = $oAi->getAgentsByProfileId($iRecipient)) {            
+            foreach($aAgents as $a) {
+                if (!$a['message_profile_id'] || $iSender == $a['message_profile_id']) {
+                    $sMessage = $oAi->callAgent('message', $a, [
+                        'trigger' => 'message',
+                        'sender_profile_id' => $iSender,
+                        'recipient_profile_id' => $iRecipient,
+                        'message_lot_id' => $iLotId, 
+                        'message_id' => $iJotId,
+                        'message_text' => $aJotInfo['message'],
+                        'message_info' => $aJotInfo,
+                    ]);
+                    $this->sendAutoMessage($a['profile_id'], $iSender, $sMessage);
+                }
+            }
+        }
+    }
+
+    function sendAutoMessage ($iSender, $iRecipient, $sMsg) 
+    {        
+        $oMessengerModule = BxDolModule::getInstance('bx_messenger');
+
+        $aAutoReplyData = [
+            'message' => $sMsg,
+            'participants' => [$iSender, $iRecipient],
+        ];
+
+        $iSaveProfileId = $oMessengerModule->setProfileId($iSender);
+        $a = $oMessengerModule->sendMessage($aAutoReplyData, $iRecipient, $iSender);
+        $oMessengerModule->setProfileId($iSaveProfileId);
+
+        return $a;
+    }
+}
+
+/** @} */

+ 3 - 2
inc/classes/BxDolAlerts.php

@@ -104,8 +104,9 @@ class BxDolAlerts extends BxDol
     public function alert()
     {
         if (isset($this->_aAlerts[$this->sUnit]) && isset($this->_aAlerts[$this->sUnit][$this->sAction]))
-            foreach($this->_aAlerts[$this->sUnit][$this->sAction] as $iHandlerId) {
-                $aHandler = $this->_aHandlers[$iHandlerId];
+
+            foreach($this->_aAlerts[$this->sUnit][$this->sAction] as $iHandlerId) {                
+                $aHandler = $this->_aHandlers[$iHandlerId];                
                 if(!$aHandler['active'])
                     continue;
 

+ 9 - 1
install/sql/system.sql

@@ -2291,6 +2291,13 @@ INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
 ('sys_cmts_images', 'file_deleted', @iIdHandler);
 
 
+INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`) VALUES
+('sys_agents', 'BxDolAiAlertResponse', 'inc/classes/BxDolAiAlertResponse.php');
+SET @iIdHandler = LAST_INSERT_ID();
+
+INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
+('bx_messenger', 'got_jot', @iIdHandler);
+
 -- --------------------------------------------------------
 
 
@@ -7127,11 +7134,12 @@ CREATE TABLE `sys_agents_agents` (
   `webhook_key` varchar(255) NOT NULL,
   `webhook_sample` text NOT NULL,
   `agent_sample` text NOT NULL,
-  `message_profile_ids` int(11) NOT NULL,
+  `message_profile_id` int(11) NOT NULL,
   `added` int(11) UNSIGNED NOT NULL DEFAULT 0,
   `active` tinyint(4) NOT NULL DEFAULT 0,
   PRIMARY KEY (`id`),
   KEY `alert` (`alert_unit`,`alert_action`),
+  KEY `profile_id` (`profile_id`,`message_profile_id`),
   KEY `webhook_key` (`webhook_key`(192))
 );
 

+ 8 - 1
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -4153,9 +4153,16 @@ class BxBaseModGeneralModule extends BxDolModule
         return $oActions->{bx_is_api() ? 'getCodeAPI' : 'getCode'}();
     }
 
+    public function setProfileId($i)
+    {
+        $iOldProfileId = $this->_iProfileId;
+        $this->_iProfileId = $i;
+        return $iOldProfileId;
+    }
+    
     public function getProfileId()
     {
-    	return bx_get_logged_profile_id();
+        return $this->_iProfileId;
     }
 
     public function getProfileInfo($iUserId = false)

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

@@ -452,12 +452,12 @@ class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
                         'pass' => 'Xss',
                     ],
                 ],
-                'message_profile_ids' => [
+                'message_profile_id' => [
                     'type' => 'select',
-                    'name' => 'message_profile_ids',
+                    'name' => 'message_profile_id',
                     'caption' => _t('_sys_agents_field_message_profile'),
                     'info' => _t('_sys_agents_field_message_profile_info'),
-                    'value' => isset($aAgent['message_profile_ids']) ? $aAgent['message_profile_ids'] : '',
+                    'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
                     // 'tr_attrs' => [
                     //     'style' => $sType != 'message' ? 'display:none' : ''
                     // ],