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

#5406 AI Agents revamp: content change tools

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

+ 62 - 0
inc/classes/BxDolAIToolContentAdd.php

@@ -0,0 +1,62 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+use NeuronAI\Tools\PropertyType;
+use NeuronAI\Tools\Tool;
+use NeuronAI\Tools\ToolProperty;
+use NeuronAI\Tools\ArrayProperty;
+use NeuronAI\Tools\ObjectProperty;
+
+class BxDolAIToolContentAdd extends Tool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'content_add',
+            'Use this tool to add content to content modules. Always use "content_structure" tool to get knowledge about content modules fields. Available modules: ' . bx_srv('system', 'modules_list', [true, true], 'TemplServiceContent') . '.',
+        );
+    }
+
+    protected function properties(): array
+    {
+        return [
+            new ToolProperty(
+                name: 'module',
+                type: PropertyType::STRING,
+                description: 'The module name to perform content changes on. Use "system" for site accounts. ',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'data',
+                type: PropertyType::OBJECT,
+                description: 'The data to add, as key-value pairs.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'profile_id',
+                type: PropertyType::INTEGER,
+                description: 'Author profile ID.',
+                required: true
+            )
+        ];
+    }
+
+    public function __invoke(string $module, array $data, int $profile_id): string
+    {
+        if ($profile_id) {
+            $GLOBALS['glForceCurrentProfileId'] = $profile_id;
+        }
+
+        $a = bx_srv('system', 'add', [$module, $data], 'TemplServiceContent');
+
+        $GLOBALS['glForceCurrentProfileId'] = 0;
+
+        return json_encode($a);
+    }
+}

+ 0 - 92
inc/classes/BxDolAIToolContentChange.php

@@ -1,92 +0,0 @@
-<?php defined('BX_DOL') or die('hack attempt');
-/**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup    UnaCore UNA Core
- * @{
- */
-
-use NeuronAI\Tools\PropertyType;
-use NeuronAI\Tools\Tool;
-use NeuronAI\Tools\ToolProperty;
-use NeuronAI\Tools\ArrayProperty;
-use NeuronAI\Tools\ObjectProperty;
-
-class BxDolAIToolContentChange extends Tool
-{
-    public function __construct()
-    {
-        parent::__construct(
-            'content_change',
-            'Use this tool to add, update or delete content. Always use "content_structure" tool if it\'s available to get knowledge about content modules fields for "add" and "update" actions. ALL module fields MUST be placed inside data - no exceptions, flat fields are invalid. Available modules: ' . self::getModules() . '.',
-        );
-    }
-    
-    static public function getModules(): string
-    {
-        $aModules = $aContentModules = bx_srv('system', 'modules_list', [true], 'TemplServiceContent');
-        return implode(',', array_keys($aModules));
-    }
-
-    protected function properties(): array
-    {
-        return [
-            new ToolProperty(
-                name: 'action',
-                type: PropertyType::STRING,
-                description: 'The action to perform on the content. Options are "update", "create" or "delete". Returns array with code = 0 on success, or array with code != 0 and error message on failure.',
-                required: true
-            ),
-            new ToolProperty(
-                name: 'module',
-                type: PropertyType::STRING,
-                description: 'The module name to perform content changes on. Use "system" for site accounts. ',
-                required: true
-            ),
-            new ToolProperty(
-                name: 'content_id',
-                type: PropertyType::INTEGER,
-                description: 'The ID of the content item to delete or update, use 0 for creating new content, never pass 0 for update and delete actions.',
-                required: true
-            ),
-            new ToolProperty(
-                name: 'data',
-                type: PropertyType::OBJECT,
-                description: 'The data to update the content item with, as key-value pairs, where key is the field name and value is the value. Skip this parameter for delete action, for "create" and "update" actions this is mandatory.',
-                required: false
-            ),
-            new ToolProperty(
-                name: 'profile_id',
-                type: PropertyType::INTEGER,
-                description: 'The ID of the profile to perform the action on behalf of.',
-                required: false
-            )
-        ];
-    }
-
-    public function __invoke(string $action, string $module, ?int $content_id = 0, ?array $data = null, ?int $profile_id = 0): string
-    {
-        if ($profile_id) {
-            $GLOBALS['glForceCurrentProfileId'] = $profile_id;
-        }
-
-        switch ($action) {
-            case 'update':
-                $a = bx_srv('system', 'update', [$module, $content_id, $data], 'TemplServiceContent');
-                break;
-            case 'create':
-                $a = bx_srv('system', 'add', [$module, $data], 'TemplServiceContent');
-                break;
-            case 'delete':
-                $a = bx_srv('system', 'delete', [$module, $content_id], 'TemplServiceContent');
-                break;
-            default:
-                $a = ['code' => 400, 'error' => 'Error: Invalid action'];
-        }
-
-        $GLOBALS['glForceCurrentProfileId'] = 0;
-
-        return json_encode($a);
-    }
-}

+ 49 - 0
inc/classes/BxDolAIToolContentDelete.php

@@ -0,0 +1,49 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+use NeuronAI\Tools\PropertyType;
+use NeuronAI\Tools\Tool;
+use NeuronAI\Tools\ToolProperty;
+use NeuronAI\Tools\ArrayProperty;
+use NeuronAI\Tools\ObjectProperty;
+
+class BxDolAIToolContentChange extends Tool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'content_delete',
+            'Use this tool to delete content. Available modules: ' . bx_srv('system', 'modules_list', [true, true], 'TemplServiceContent') . '.',
+        );
+    }
+
+    protected function properties(): array
+    {
+        return [
+            new ToolProperty(
+                name: 'module',
+                type: PropertyType::STRING,
+                description: 'The module name to perform content changes on. Use "system" for site accounts. ',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'content_id',
+                type: PropertyType::INTEGER,
+                description: 'Content ID to delete.',
+                required: true
+            ),
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id = 0): string
+    {        
+        $a = bx_srv('system', 'delete', [$module, $content_id], 'TemplServiceContent');
+        return json_encode($a);
+    }
+}

+ 68 - 0
inc/classes/BxDolAIToolContentUpdate.php

@@ -0,0 +1,68 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+use NeuronAI\Tools\PropertyType;
+use NeuronAI\Tools\Tool;
+use NeuronAI\Tools\ToolProperty;
+use NeuronAI\Tools\ArrayProperty;
+use NeuronAI\Tools\ObjectProperty;
+
+class BxDolAIToolContentUpdate extends Tool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'content_update',
+            'Use this tool to update content. Always use "content_structure" tool to get knowledge about content modules fields. Available modules: ' . bx_srv('system', 'modules_list', [true, true], 'TemplServiceContent') . '.',
+        );
+    }
+
+    protected function properties(): array
+    {
+        return [
+            new ToolProperty(
+                name: 'module',
+                type: PropertyType::STRING,
+                description: 'The module name to perform content changes on. Use "system" for site accounts. ',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'content_id',
+                type: PropertyType::INTEGER,
+                description: 'Content ID to update.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'data',
+                type: PropertyType::OBJECT,
+                description: 'The data to update, as key-value pairs.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'profile_id',
+                type: PropertyType::INTEGER,
+                description: 'Author profile ID.',
+                required: true
+            )
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id, array $data, int $profile_id): string
+    {
+        if ($profile_id) {
+            $GLOBALS['glForceCurrentProfileId'] = $profile_id;
+        }
+
+        $a = bx_srv('system', 'update', [$module, $content_id, $data], 'TemplServiceContent');
+
+        $GLOBALS['glForceCurrentProfileId'] = 0;
+
+        return $a;
+    }
+}

+ 4 - 2
install/sql/system.sql

@@ -7279,8 +7279,10 @@ INSERT INTO `sys_agents_tools` (`type`, `title`, `docs`, `params`, `params_user`
 ('mysql_write', 'MySQL Write', 'Use this tool to make your agent able to performs write operations against the database (INSERT, UPDATE, DELETE). It works the best in par of MySQL Schema tool.   \r\nNo parameters are needed for this tool.  \r\n**USE WITH CAUTION**', '{}', NULL, 0, 0, 0),
 ('content_structure', 'Content modules structure', 'This tool allows agents to understand the structure of content modules fields,  allowing them to call content adding and editing with correct data. It provide knowledge of what content modules are available and what the fields in them, event if fields are changed in fields builder.', '{}', NULL, 0, 0, 1),
 ('content_get', 'Content modules get', 'This tool allows agents to get info about specific content by module name and content id.', '{}', NULL, 0, 0, 1),
-('content_change', 'Content modules change', 'This tool allows agents to add, update or delete content. It works with the "content_structure" tool to get knowledge about content modules fields for add and update actions.', '{}', NULL, 0, 0, 1),
-('content_search', 'Content modules search', 'This tool allows agents to search for content items based on a keyword and optional sections.', '{}', NULL, 0, 0, 1);
+('content_search', 'Content modules search', 'This tool allows agents to search for content items based on a keyword and optional sections.', '{}', NULL, 0, 0, 1),
+('content_delete', 'Content modules delete', 'This tool allows agents to delete content. ', '{}', NULL, 0, 0, 1),
+('content_add', 'Content modules add', 'This tool allows agents to add new content. It works with the "content_structure" tool to get knowledge about content modules fields for add actions.', '{}', NULL, 0, 0, 1),
+('content_update', 'Content modules update', 'This tool allows agents to update existing content. It works with the "content_structure" tool to get knowledge about content modules fields for update actions.', '{}', NULL, 0, 0, 1);
 
 CREATE TABLE IF NOT EXISTS `sys_agents_chat_history` (
   `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,

+ 3 - 2
template/scripts/BxBaseServiceContent.php

@@ -129,6 +129,7 @@ class BxBaseServiceContent extends BxDol
      * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/modules_list/TemplServiceContent" @endcode
      * 
      * @param $bContentModulesOnly if true - return only modules with content objects, if false - return all modules
+     * @param $bNamesOnly if true - return only module names, if false - return full module info
      * @return content objects info array 
      * 
      * @see BxBaseServiceContent::serviceModulesList
@@ -136,7 +137,7 @@ class BxBaseServiceContent extends BxDol
     /** 
      * @ref bx_system_general_cnt-modules_list "Get modules list"
      */
-    public function serviceModulesList ($bContentModulesOnly = false)
+    public function serviceModulesList ($bContentModulesOnly = false, $bNamesOnly = false)
     {
         $aAllowedKeys = ['title', 'type', 'uri', 'db_prefix'];
         $a = BxDolModuleQuery::getInstance()->getModules();
@@ -158,7 +159,7 @@ class BxBaseServiceContent extends BxDol
             }  
         }
 
-        return $aModules;
+        return $bNamesOnly ? array_keys($aModules) : $aModules;
     }
 
     /**