فهرست منبع

Ticket #4711 - Automations.

AntonLV 2 سال پیش
والد
کامیت
a1b0390b13

+ 166 - 0
ai/instructions/common.html

@@ -0,0 +1,166 @@
+Basic Concept
+Modules
+
+The basic unit is the module (class BxDolModule)
+
+Creating a module object: $oModule = BxDolModule::getInstance($sModuleName);
+Modules can inherit interfaces: profile, context, content; one module can inherit several interfaces:
+•	To check whether a module inherits profile: bx_srv('system', 'is_module_profile', $sModuleName)
+•	To check whether a module inherits context: bx_srv('system', 'is_module_ context', $sModuleName)
+•	To check whether a module inherits content: bx_srv('system', 'is_module_content', $sModuleName)
+
+Example class declaration BxPostsModule extends BxBaseModTextModule
+
+There are several basic modules, you cannot make objects from them, they contain common functions for all objects that inherit them:
+•	class BxBaseModGeneralModule extends BxDolModule
+•	class BxBaseModGroupsModule extends BxBaseModProfileModule
+•	class BxBaseModNotificationsModule extends BxBaseModGeneralModule
+•	class BxBaseModProfileModule extends BxBaseModGeneralModule implements
+•	iBxDolContentInfoService, iBxDolProfileService
+•	class BxBaseModTextModule extends BxBaseModGeneralModule implements iBxDolContentInfoService
+
+Available modules
+•	bx_timeline
+•	bx_spaces
+•	bx_posts
+•	bx_polls
+•	bx_photos
+•	bx_persons
+•	bx_notifications
+•	bx_messenger
+•	bx_invites
+•	bx_groups
+•	bx_files
+•	bx_events
+•	bx_channels
+
+available fields for bx_persons module :
+- id
+- fullname
+- last_name
+- gender
+- birthday (date)
+- added(timestamp)
+
+available fields for bx_organisations module:
+
+- id
+- org_name
+- added(timestamp)
+
+BxBaseModGeneralModule functions
+BxBaseModProfileModule functions
+•	serviceGetContentInfoByProfileId($iProfileId) – return array  with all object fields, example for post
+[‘title’ =>, ‘created’ => ‘text’ => ]
+
+Accounts/Profiles
+The basic unit of the system user is the account: class BxDolAccount
+Creating an account object: $oAccount = BxDolAccount::getInstance($iAccountId);
+
+Methods of the BxDolAccount class object
+•	getInfo($iAccountId=false)
+return array with account  info :
+- id, 
+- name 
+- email  -  
+- email_confirmed (bool)
+- added (timestamp)
+
+•	id()
+•	isConfirmed($iAccountId=false)
+•	updateEmailConfirmed($isConfirmed,$isAutoSendConfrmationEmail=true,$iAccountId=false)
+•	updatePhoneConfirmed($isConfirmed,$iAccountId=false)
+•	updatePassword($sPassword,$iAccountId=false)
+•	updatePhone($sPhone,$iAccountId=false)
+•	updateProfileContextAuto($iProfileIdFilterOut=false,$iAccountId=false)
+•	sendConfirmationEmail($iAccountId=false)
+•	getDisplayName($iAccountId=false)
+•	getUrl($iAccountId=false)
+•	getUnit($iAccountId=false,$aParams=array())
+•	getPicture($iAccountId=false)
+•	getAvatarBig($iAccountId=false)
+•	getAvatar($iAccountId=false)
+•	getThumb($iAccountId=false)
+•	getIcon($iAccountId=false)
+•	getEmail($iAccountId=false)
+•	getLanguageId($iAccountId=false)
+•	isOnline($iAccountId=false)
+•	isProfilesLimitReached()
+•	getProfilesNumber($isFilterNonSwitchableProfiles=true)
+•	getProfilesIds($isFilterNonSwitchableProfiles=true,$isFilterSystemProfiles=true)
+•	getProfiles($isFilterNonSwitchableProfiles=true,$isFilterSystemProfiles=true)
+•	delete($bDeleteWithContent=false)
+•	addInformerPermanentMessages($oInformer)
+•	getUnsubscribeLink($iEmailType,$iAccountId=false)
+
+One account can have many profiles (class BxDolProfile) of different types, for example, one person profile (bx_persons module) and one organization profile (bx_organisations module).
+
+Creating a profile object: $oProfile = BxDolProfile::getInstance($iProfileId);
+
+Get the current $iProfileId for a user: bx_get_logged_profile_id()
+
+Methods of the BxDolProfile class object
+•	getInfo ($iProfileId=0)
+return array with profile info :
+- id, 
+- account_id 
+- type  -  content module name for profile 
+- content_id – id from content module
+- status, can be active/suspended, pending
+
+•	id()
+•	getAccountId ($iProfileId=false)
+•	getAccountObject ($iProfileId=false)
+•	getContentId()
+•	isActive ($iProfileId=false)
+•	isOnline ($iProfileId=false)
+•	isActAsProfile ($iProfileId=false)
+•	getStatus ($iProfileId=false)
+•	getModule ($iProfileId=false)
+•	getDisplayName ($iProfileId=0)
+•	getUrl ($iProfileId=0)
+•	getUnit ($iProfileId=0, $aParams=array())
+•	getUnitAPI ($iProfileId=0, $aParams=array())
+•	getBadges ($iProfileId=0)
+•	hasImage ($iProfileId=0)
+•	getPicture ($iProfileId=0)
+•	getAvatar ($iProfileId=0)
+•	getAvatarBig ($iProfileId=0)
+•	getCover ($iProfileId=0)
+•	getUnitCover ($iProfileId=0)
+•	getThumb ($iProfileId=0)
+•	getIcon ($iProfileId=0)
+•	getIconModule ($iProfileId=0)
+•	getEditUrl ($iProfileId=0)
+•	checkAllowedProfileView ($iProfileId=0)
+•	checkAllowedProfileContact ($iProfileId=0)
+•	checkAllowedPostInProfile ($iProfileId=0, $sPostModule='')
+•	delete ($ID=false, $bDeleteWithContent=false, $bForceDelete=false)
+•	activate ($iAction, $iProfileId=0, $bSendEmailNotification=true)
+•	approve ($iAction, $iProfileId=0, $bSendEmailNotification=true)
+•	disapprove ($iAction, $iProfileId=0, $bSendEmailNotification=true)
+•	move ($iAccountId, $iProfileId=0)
+•	suspend ($iAction, $iProfileId=0, $bSendEmailNotification=true)
+•	checkSwitchToProfile ($oTemplate=null, $iViewerAccountId=false, $iViewerProfileId=false)
+•	addInformerPermanentMessages ($oInformer)
+
+
+System functions
+/**
+ * Send email function
+ ** @param $mRecipientEmails - Email where email should be send, can be array, string with one email or comma separated
+ * @param $sMailSubject - subject of the message
+ * @param $sMailBody - Body of the message
+ * @param $iRecipientID - ID of recipient profile
+ * @param $aPlus - Array of additional information
+ * @param $iEmailType - email message type: BX_EMAIL_SYSTEM, BX_EMAIL_NOTIFY or BX_EMAIL_MASS
+ * @param $sEmailFlag - use 'html' for HTML email message
+ * @param $isDisableAlert - disable alert
+ * @param $aCustomHeaders - custom email headers
+ * @param $bAddToQueue - add message to email queue
+ * @return true if message was send or false otherwise
+ */
+function sendMail($mRecipientEmails, $sMailSubject, $sMailBody, $iRecipientID = 0, $aPlus = array(), $iEmailType = BX_EMAIL_NOTIFY, $sEmailFlag = 'html', $isDisableAlert = false, $aCustomHeaders = array(), $bAddToQueue = false)
+
+
+

+ 11 - 0
ai/instructions/event.html

@@ -0,0 +1,11 @@
+When someone gives you automation instructions, 
+Convert the following rule into PHP code function named "onAlert" with input parameters 
+($iObjectId, $iSender, $aExtras)
+where $iObjectId = 3d paremeter from input bx_alert
+where $iSender= 4d paremeter from input bx_alert
+where $aExtras= 5d paremeter from input bx_alert
+
+use only described functions, if needed fuction can't be found return false
+dont use ' symbol in return 
+
+return only function, without explanation

+ 12 - 0
ai/instructions/event_init.html

@@ -0,0 +1,12 @@
+When someone gives you automation instructions, 
+select most suitable trigger from trigger list and return triggers code without any explanations, if no suitable triggers found return false
+
+trigger list:
+bx_alert('account', 'login',  $iId);
+
+bx_alert('profile', 'add', $iProfileId, 0, array('module' => $sType, 'content' => $iContentId, 'account' => $iAccountId, 'status' => $sStatus, 'action' => $iAction, 'profile_id' => &$iProfileId));
+
+bx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'type' => $aProfileInfo['type']));
+
+return shoud be valid json string {'alert_unit':'profile', 'alert_action':'delete', 'trigger':'bx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'type' => $aProfileInfo['type'])) '}  for trigger  bbx_alert('profile', 'delete', $ID, 0, array('delete_with_content' => $bDeleteWithContent, 'type' => $aProfileInfo['type'])) or false 
+

+ 7 - 0
ai/instructions/scheduler.html

@@ -0,0 +1,7 @@
+When someone gives you automation instructions, 
+Convert the following rule into PHP code function named "onCron" with no parameters 
+
+use only described functions, if needed fuction can't be found return false
+dont use ' symbol in return 
+
+return only function, without explanation

+ 2 - 0
ai/instructions/scheduler_init.html

@@ -0,0 +1,2 @@
+When someone gives you automation instructions, 
+return only cron setting like * * * * *

+ 22 - 9
inc/classes/BxDolAI.php

@@ -9,13 +9,16 @@
 
 class BxDolAI extends BxDolFactory implements iBxDolSingleton
 {
-    
+    protected $_sPathInst;
+
     protected function __construct()
     {
         if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
             trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
 
         parent::__construct();
+
+        $this->_sPathInst = BX_DIRECTORY_PATH_ROOT . 'ai/instructions/';
     }
 
     /**
@@ -37,16 +40,26 @@ class BxDolAI extends BxDolFactory implements iBxDolSingleton
 
         return $GLOBALS['bxDolClasses'][__CLASS__];
     }
-    
+
+    public function getAutomatorInstructions($sType, $bIncludeCommon = false)
+    {
+        $sResult = file_get_contents($this->_sPathInst . $sType . '.html');
+        if($bIncludeCommon)
+            $sResult .= file_get_contents($this->_sPathInst. 'common.html');
+
+        return $sResult;
+    }
+
     public function chat($sEndpoint, $sModel, $sApiKey, $aParams, $aMessages)
     {
-         $aData = array_merge(
-             [
-                'model' => $sModel,
-                'messages' => $aMessages
-            ], 
-            $aParams
-        );
+        $aData = [
+            'model' => $sModel,
+            'messages' => $aMessages
+        ];
+
+        if(!empty($aParams) && is_array($aParams))
+            $aData = array_merge($aData, $aParams);
+
         $sRv = bx_file_get_contents($sEndpoint, $aData, "post-json", ["Authorization: Bearer ".$sApiKey, 'Content-Type: application/json', 'OpenAI-Beta: assistants=v1']);
 
         $aRv = json_decode($sRv, true);

+ 7 - 12
studio/template/scripts/BxBaseStudioAgentsAutomators.php

@@ -48,6 +48,7 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
             if(!empty($sSchedulerTime))
                 $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
 
+            $sType = $oForm->getCleanValue('type');
             $sMessage = $oForm->getCleanValue('message');
             $bMessage = !empty($sMessage);
             $sMessageAdd = '';
@@ -57,6 +58,11 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
                 $aValsToAdd['messages'] = 1;
 
             $oAI = BxDolAI::getInstance();
+            
+            $aMessages = [
+                ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($sType . '_init')],
+                ['role' => 'user', 'content' => $sMessage]
+            ];
 
             $iModel = $oForm->getCleanValue('model_id');
             $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iModel]);
@@ -64,14 +70,8 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
                 $aModel['params'] = json_decode($aModel['params'], true);
 
             $bIsValid = true;
-            $sType = $oForm->getCleanValue('type');
             switch($sType) {
                 case 'event':
-                    $aMessages = [
-                        ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_alert_instructions.html' )],
-                        ['role' => 'user', 'content' => $sMessage]
-                    ];
-
                     $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
                     if(trim($sResponse) != 'false') {
                         $sResponse = str_replace(['```json', '```'], '', $sResponse);
@@ -89,11 +89,6 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
                     break;
 
                 case 'scheduler':
-                    $aMessages = [
-                        ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_cron_instructions.html' )],
-                        ['role' => 'user', 'content' => $sMessage]
-                    ];
-
                     $sResponse = $oAI->chat($aModel['url'], $aModel['model'], $aModel['key'], $aModel['params'], $aMessages);
                     if(trim($sResponse) != 'false') {
                         $aValsToAdd['params'] = json_encode(['scheduler_time' => $aResponse]);
@@ -107,7 +102,7 @@ class BxBaseStudioAgentsAutomators extends BxDolStudioAgentsAutomators
 
             if($bIsValid) {
                 $aMessages = [
-                    ['role' => 'system', 'content' => file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_' . $sType . '_instructions.html' ) . file_get_contents(BX_DIRECTORY_PATH_ROOT. '/ai_common_instructions.html' )],
+                    ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($sType, true)],
                     ['role' => 'user', 'content' => $sMessage . $sMessageAdd]
                 ];