Browse Source

#5406 AI Agents revamp: cmts tools

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

+ 90 - 0
inc/classes/BxDolAIToolCmtsAdd.php

@@ -0,0 +1,90 @@
+<?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\ToolProperty;;
+
+class BxDolAIToolCmtsAdd extends BxDolAITool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'comments_add',
+            'Use this tool to post comments.',
+        );
+    }
+
+    protected function properties(): array
+    {
+        return [
+            new ToolProperty(
+                name: 'module',
+                type: PropertyType::STRING,
+                description: 'Module name of the content_id.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'content_id',
+                type: PropertyType::INTEGER,
+                description: 'Content ID where to to post comments.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'comment_text',
+                type: PropertyType::STRING,
+                description: 'Comments text to post, in HTML format.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'parent_comment_id',
+                type: PropertyType::INTEGER,
+                description: 'Parent comment ID, for comment replies.',
+                required: false
+            ),
+            new ToolProperty(
+                name: 'author_profile_id',
+                type: PropertyType::INTEGER,
+                description: 'Author profile ID of the posted comment.',
+                required: false
+            ),
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id, string $comment_text, int $parent_comment_id = 0, int $author_profile_id = 0): array
+    {
+        if ($author_profile_id) {
+            $GLOBALS['glForceCurrentProfileId'] = $author_profile_id;
+        }
+
+        $a = [];
+        $o = BxDolCmts::getObjectInstance($module, $content_id);
+        if ($o) {
+            $aData = [
+                'sys' => $module,
+                'id' => $content_id,
+                'cmt_parent_id' => $parent_comment_id,
+                // 'cmt_image' => [],
+                'cmt_text' => $comment_text,
+            ];
+            // if ($author_profile_id)
+            //    $aData['cmt_author_id'] = $author_profile_id;
+
+            $a = $o->add($aData);
+            unset($a['count']);
+            unset($a['countf']);
+        }
+        else {
+            $a = ['msg' => '_sys_txt_not_found', 'code' => 404];
+        }
+
+        $GLOBALS['glForceCurrentProfileId'] = 0;
+
+        return $a;
+    }
+}

+ 61 - 0
inc/classes/BxDolAIToolCmtsDelete.php

@@ -0,0 +1,61 @@
+<?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\ToolProperty;
+
+class BxDolAIToolCmtsDelete extends BxDolAITool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'comments_delete',
+            'Use this tool to delete comments.',
+        );
+    }
+
+    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 post comments for.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'comment_id',
+                type: PropertyType::INTEGER,
+                description: 'Comment ID to update.',
+                required: true
+            ),
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id, int $comment_id): array
+    {
+        $a = [];
+        $o = BxDolCmts::getObjectInstance($module, $content_id);
+        if ($o) {
+            $a = $o->remove($comment_id);
+            unset($a['count']);
+            unset($a['countf']);
+        }
+        else {
+            $a = ['msg' => '_sys_txt_not_found', 'code' => 404];
+        }
+        return $a;
+    }
+}

+ 72 - 0
inc/classes/BxDolAIToolCmtsGet.php

@@ -0,0 +1,72 @@
+<?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\ToolProperty;
+
+class BxDolAIToolCmtsGet extends BxDolAITool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'comments_get',
+            'Retrieve content\'s comments for a given module and content id. If needed, prepend base URL to links: ' . BX_DOL_URL_ROOT,
+        );
+    }
+    
+    protected function properties(): array
+    {
+        return [
+            new ToolProperty(
+                name: 'module',
+                type: PropertyType::STRING,
+                description: 'The module name to retrieve content info from. Use "system" for site accounts. ',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'content_id',
+                type: PropertyType::INTEGER,
+                description: 'The ID of the content item to retrieve info for.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'per_page',
+                type: PropertyType::INTEGER,
+                description: 'Pagination, number of comments per page. Default is 10.',
+                required: false
+            ),
+            new ToolProperty(
+                name: 'start',
+                type: PropertyType::INTEGER,
+                description: 'Pagination, start showing comments starting from this number. Default is 0.',
+                required: false
+            ),
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id, int $per_page = 10, int $start = 0): array
+    {
+        $o = BxDolCmts::getObjectInstance($module, $content_id);
+        if ($o->isEnabled()) {
+            $a = $o->serviceGetAll([
+                'type' => 'latest',
+                'object_id' => $content_id,
+                'start' => $start,
+                'per_page' => $per_page
+            ]);
+            foreach ($a as $k => $r) {
+                $a[$k] = $o->getDataAPI($r);
+            }
+            return $a;
+        } 
+        else {
+            return ['msg' => '_sys_txt_not_found', 'code' => 404];
+        }
+    }
+}

+ 67 - 0
inc/classes/BxDolAIToolCmtsUpdate.php

@@ -0,0 +1,67 @@
+<?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\ToolProperty;
+
+class BxDolAIToolCmtsUpdate extends BxDolAITool
+{
+    public function __construct()
+    {
+        parent::__construct(
+            'comments_update',
+            'Use this tool to change comments.',
+        );
+    }
+
+    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 post comments for.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'comment_text',
+                type: PropertyType::STRING,
+                description: 'New comments text, in HTML format.',
+                required: true
+            ),
+            new ToolProperty(
+                name: 'comment_id',
+                type: PropertyType::INTEGER,
+                description: 'Comment ID to update.',
+                required: true
+            ),
+        ];
+    }
+
+    public function __invoke(string $module, int $content_id, string $comment_text, int $comment_id): array
+    {
+        $a = [];
+        $o = BxDolCmts::getObjectInstance($module, $content_id);
+        if ($o) {
+            $a = $o->edit(['cmt_text' => $comment_text], $comment_id);
+            unset($a['content']);
+        }
+        else {
+            $a = ['msg' => '_sys_txt_not_found', 'code' => 404];
+        }
+
+        return $a;
+    }
+}

+ 9 - 20
inc/classes/BxDolAIToolFactory.php

@@ -41,27 +41,16 @@ class BxDolAIToolFactory extends BxDolFactory
                     pdo: BxDolDb::getInstance()->getLink(),
                 );
                 break;
-            case 'content_structure':
-                $o = BxDolAIToolContentStructure::make();
-                break;
-            case 'content_get':
-                $o = BxDolAIToolContentGet::make();
-                break;
-            case 'content_search':
-                $o = BxDolAIToolContentSearch::make();
-                break;
-            case 'content_add':
-                $o = BxDolAIToolContentAdd::make();
-                break;
-            case 'content_update':
-                $o = BxDolAIToolContentUpdate::make();
-                break;
-            case 'content_delete':
-                $o = BxDolAIToolContentDelete::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");
+                if (!empty($a['class_name'])) {
+                    if (!empty($aObject['class_file']))
+                        require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
+                    $o = $a['class_name']::make();
+                }
+                else {
+                    bx_log('sys_agents', "Tool type {$a['type']} is not supported", BX_LOG_ERR);
+                    throw new Exception("Tool type {$a['type']} is not supported");
+                }
         }
 
         $GLOBALS['bxDolClasses'][__CLASS__ . '_AiAgentTool_' . $iId] = $o;

+ 18 - 10
install/sql/system.sql

@@ -7305,19 +7305,27 @@ CREATE TABLE IF NOT EXISTS `sys_agents_tools` (
   `duplicate` tinyint(4) NOT NULL DEFAULT 1,
   `changed` int(11) NOT NULL DEFAULT 0,
   `active` tinyint(4) NOT NULL DEFAULT 0,
+  `class_name` varchar(255) NOT NULL DEFAULT '',
+  `class_file` varchar(255) NOT NULL DEFAULT '',
   PRIMARY KEY (`id`)
 );
 
-INSERT INTO `sys_agents_tools` (`type`, `title`, `docs`, `params`, `params_user`, `duplicate`, `changed`, `active`) VALUES
-('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', '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_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);
+INSERT INTO `sys_agents_tools` (`type`, `title`, `docs`, `params`, `params_user`, `duplicate`, `changed`, `active`, `class_name`, `class_file`) VALUES
+('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', '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, 'BxDolAIToolContentStructure', ''),
+('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, 'BxDolAIToolContentGet', ''),
+('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, 'BxDolAIToolContentSearch', ''),
+('content_delete', 'Content modules delete', 'This tool allows agents to delete content. ', '{}', NULL, 0, 0, 1, 'BxDolAIToolContentDelete', ''),
+('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, 'BxDolAIToolContentAdd', ''),
+('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, 'BxDolAIToolContentUpdate', ''),
+
+('comments_get', 'Comments get', 'This tool allows agents to get comments for specific content by module name and content id.', '{}', NULL, 0, 0, 1, 'BxDolAIToolCmtsGet', ''),
+('comments_add', 'Comments add', 'This tool allows agents to post comments for specific content.', '{}', NULL, 0, 0, 1, 'BxDolAIToolCmtsAdd', ''),
+('comments_update', 'Comments update', 'This tool allows agents to edit comments.', '{}', NULL, 0, 0, 1, 'BxDolAIToolCmtsUpdate', ''),
+('comments_delete', 'Comments delete', 'This tool allows agents to delete comments.', '{}', NULL, 0, 0, 1, 'BxDolAIToolCmtsDelete', '');
 
 CREATE TABLE IF NOT EXISTS `sys_agents_chat_history` (
   `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,