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

#5406 AI Agents revamp: content change tool

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

+ 75 - 0
inc/classes/BxDolAIToolContentChange.php

@@ -0,0 +1,75 @@
+<?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.  If needed, use info from "content_structure" tool to get knowledge about content modules fields for add and update actions.',
+        );
+    }
+    
+    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.',
+                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 required.',
+                required: false
+            )
+        ];
+    }
+
+    public function __invoke(string $action, string $module, ?int $content_id = 0, ?array $data = null): string
+    {
+        echoDbgLog("content_change tool called with action: {$action}, module: {$module}, content_id: {$content_id}, data: " . json_encode($data));
+        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:
+                return "Error: Invalid action";
+        }
+
+        return json_encode($a);
+    }
+}

+ 2 - 2
inc/classes/BxDolAIToolContentGet.php

@@ -16,8 +16,8 @@ class BxDolAIToolContentGet extends Tool
     public function __construct()
     {
         parent::__construct(
-            'get_content_info',
-            'Retrieve content info for a given module and content id.',
+            'content_get',
+            'Retrieve content info for a given module and content id, including author info and URL. If needed, prepend base URL to links: ' . BX_DOL_URL_ROOT,
         );
     }
     

+ 3 - 1
inc/classes/BxDolAIToolContentStructure.php

@@ -16,7 +16,7 @@ class BxDolAIToolContentStructure extends Tool
     public function __construct()
     {
         parent::__construct(
-            'get_content_fields',
+            'content_structure',
             'Retrieve the structure of content fields for a given module.',
         );
     }
@@ -35,6 +35,8 @@ class BxDolAIToolContentStructure extends Tool
 
     public function __invoke(string $module): string
     {        
+        echoDbgLog("content_structure tool called with module: {$module}");
+
         $aContentModules = bx_srv('system', 'modules_list', [true], 'TemplServiceContent');
         $s = $this->formatModulesOutputForLLM($aContentModules);
 

+ 3 - 0
inc/classes/BxDolAIToolFactory.php

@@ -47,6 +47,9 @@ class BxDolAIToolFactory extends BxDolFactory
             case 'content_get':
                 $o = BxDolAIToolContentGet::make();
                 break;
+            case 'content_change':
+                $o = BxDolAIToolContentChange::make();
+                break;
             default:
                 bx_log('sys_agents', "Tool type {$a['type']} is not supported", BX_LOG_ERR);
                 throw new Exception("Tool type {$a['type']} is not supported");

+ 3 - 2
install/sql/system.sql

@@ -7279,8 +7279,9 @@ INSERT INTO `sys_agents_tools` (`type`, `title`, `docs`, `params`, `params_user`
 ('mysql_schema', 'MySQL Schema', 'This tool allows agents to understand the structure of the database, enabling them to construct intelligent queries without requiring you to hardcode table structures or relationships into the prompts. This tool essentially gives your agent the equivalent of a database administrator’s understanding of your schema, allowing it to craft queries that respect your data model and take advantage of existing indexes.  \r\nIt reads all tables by default, but it\'s possible to limit it to a certain number of tables by providing the list in the `tables` parameter. By limiting the schema scope, you can create specialized agents that focus on specific areas of the site.', '{\"tables\":[]}', NULL, 0, 0, 1),
 ('mysql_select', 'MySQL Select', 'Use this tool to make your agent able to run SELECT query against the database. It\'s like read-only access without ability to change anything in the DB. It works the best in par of MySQL Schema tool.  \r\nNo parameters are needed for this tool.', '{}', NULL, 0, 0, 1),
 ('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', 'Site content modules fields 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', 'Site content modules get content', 'This tool allows agents to get info about specific content by module name and content id.', '{}', NULL, 0, 0, 1);
+('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);
 
 CREATE TABLE IF NOT EXISTS `sys_agents_chat_history` (
   `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,