Преглед изворни кода

#5406 AI Agents revamp: AI Providers/models

Aleksandr Trofimov пре 7 месеци
родитељ
комит
0a4d033544

+ 8 - 0
inc/classes/BxDolAIQuery.php

@@ -73,6 +73,14 @@ class BxDolAIQuery extends BxDolDb
         return $aAssistant;
     }
 
+    public function insertModel($aModel)
+    {
+        if(empty($aModel))
+            return false;
+
+        return (int)$this->query("INSERT INTO `sys_agents_models` SET " . $this->arrayToSQL($aModel));
+    }
+
     public function getModelsBy($aParams = [])
     {
         $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];

+ 1 - 1
inc/classes/BxDolDb.php

@@ -1206,7 +1206,7 @@ class BxDolDb extends BxDolFactory implements iBxDolSingleton
                 throw new Exception('Invalid field name in arrayToSQL method');
             if ($bWildcardSpaceChars)
                 $v = preg_replace('/[\p{Zs}\p{Cc}\p{Pd}]/', '_', $v);
-            $s .= "`{$k}` {$sOperator} " . $this->escape($v) . $sDiv;
+            $s .= "`{$k}` {$sOperator} " . (is_null($v) ? 'NULL' : $this->escape($v)) . $sDiv;
         }
         return trim($s, $sDiv);
     }

+ 7 - 6
install/sql/system.sql

@@ -5633,7 +5633,7 @@ INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon
 
 -- GRID: Agents Models
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `responsive`, `show_total_count`, `override_class_name`, `override_class_file`) VALUES
-('sys_studio_agents_models', 'Sql', 'SELECT * FROM `sys_agents_models` WHERE 1 ', 'sys_agents_models', 'id', 'changed', 'active', '', 20, NULL, 'start', '', 'type,model,title,capabilities', '', 'like', 'type,model,title,capabilities', '', 2147483647, 1, 1, 'BxTemplStudioAgentsModels', '');
+('sys_studio_agents_models', 'Sql', 'SELECT * FROM `sys_agents_models` WHERE 1 ', 'sys_agents_models', 'id', 'changed', 'active', '', 20, NULL, 'start', '', 'type,model,title,capabilities', '', 'like', 'type,model,title,capabilities', '', 128, 1, 1, 'BxTemplStudioAgentsModels', '');
 
 INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `hidden_on`, `order`) VALUES
 ('sys_studio_agents_models', 'checkbox', '', '2%', 0, 0, '', '', 10),
@@ -5645,7 +5645,7 @@ INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable
 ('sys_studio_agents_models', 'actions', '', '50%', 0, 0, '', '', 70);
 
 INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon_only`, `confirm`, `active`, `order`) VALUES
-('sys_studio_agents_models', 'independent', 'add', '_sys_agents_models_btn_add', '', 0, 0, 1, 1),
+('sys_studio_agents_models', 'single', 'duplicate', '_Duplicate', 'copy', 1, 0, 1, 1),
 ('sys_studio_agents_models', 'single', 'edit', '_Edit', 'pencil-alt', 1, 0, 1, 2),
 ('sys_studio_agents_models', 'single', 'delete', '_Delete', 'remove', 1, 1, 1, 3),
 ('sys_studio_agents_models', 'bulk', 'delete', '_Delete', '', 0, 1, 1, 1);
@@ -5779,7 +5779,7 @@ INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon
 
 -- GRID: Agents Vector Store
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `responsive`, `show_total_count`, `override_class_name`, `override_class_file`) VALUES
-('sys_studio_agents_vector_store', 'Sql', 'SELECT * FROM `sys_agents_vector_store` WHERE 1 ', 'sys_agents_vector_store', 'id', 'id', 'active', '', 20, NULL, 'start', '', '', '', 'like', '', '', 2147483647, 1, 1, 'BxTemplStudioAgentsVectorStore', '');
+('sys_studio_agents_vector_store', 'Sql', 'SELECT * FROM `sys_agents_vector_store` WHERE 1 ', 'sys_agents_vector_store', 'id', 'id', 'active', '', 20, NULL, 'start', '', '', '', 'like', '', '', 128, 1, 1, 'BxTemplStudioAgentsVectorStore', '');
 
 INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `hidden_on`, `order`) VALUES
 ('sys_studio_agents_vector_store', 'checkbox', '', '2%', 0, 0, '', '', 1),
@@ -5798,7 +5798,7 @@ INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon
 
 -- GRID: Agents Vector Store Data
 INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `responsive`, `show_total_count`, `override_class_name`, `override_class_file`) VALUES
-('sys_studio_agents_vector_store_data', 'Sql', 'SELECT * FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = ''{vector_store_id}'' ', 'sys_agents_vector_store_data', 'id', 'id', '', '', 10, NULL, 'start', '', 'name', '', 'like', '', '', 2147483647, 1, 1, 'BxTemplStudioAgentsVectorStoreData', '');
+('sys_studio_agents_vector_store_data', 'Sql', 'SELECT * FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = ''{vector_store_id}'' ', 'sys_agents_vector_store_data', 'id', 'id', '', '', 10, NULL, 'start', '', 'name', '', 'like', '', '', 128, 1, 1, 'BxTemplStudioAgentsVectorStoreData', '');
 
 INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `hidden_on`, `order`) VALUES
 ('sys_studio_agents_vector_store_data', 'checkbox', '', '2%', 0, 0, '', '', 10),
@@ -6721,7 +6721,7 @@ CREATE TABLE `sys_api_origins` (
 -- --------------------------------------------------------
 
 CREATE TABLE `sys_agents_models` (
-  `id` int(11) NOT NULL,
+  `id` int(11) NOT NULL AUTO_INCREMENT,
   `type` varchar(64) NOT NULL,
   `model` varchar(64) NOT NULL,
   `title` varchar(64) NOT NULL DEFAULT '',
@@ -6732,7 +6732,8 @@ CREATE TABLE `sys_agents_models` (
   `capabilities` enum('chatllm','chatvlm','embeddings') NOT NULL DEFAULT 'chatllm',
   `duplicate` tinyint(4) NOT NULL DEFAULT 1,
   `active` tinyint(4) NOT NULL DEFAULT 1,
-  `changed` int(11) UNSIGNED NOT NULL DEFAULT 0
+  `changed` int(11) UNSIGNED NOT NULL DEFAULT 0,
+  PRIMARY KEY (`id`)
 );
 
 SET @j = JSON_OBJECT(

+ 64 - 47
studio/template/scripts/BxBaseStudioAgentsModels.php

@@ -24,38 +24,23 @@ class BxBaseStudioAgentsModels extends BxDolStudioAgentsModels
         return 'oBxDolStudioPageAgents';
     }
 
-    public function performActionAdd()
+    public function performActionDuplicate()
     {
-        $sAction = 'add';
-
-        $aForm = $this->_getForm($sAction);
-        $oForm = new BxTemplFormView($aForm);
-        $oForm->initChecker();
-
-        if($oForm->isSubmittedAndValid()) {
-            $aValsToAdd = ['added' => time()];
-
-            $bIsValid = true;
-            if($bIsValid) {
-                if(($iId = $oForm->insert($aValsToAdd)) !== false) {
-                    $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
-                }
-                else
-                    $aRes = ['msg' => _t('_sys_txt_error_occured')];
-
+        $iId = $this->_getId();
+        $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
+        if (!empty($aModel)) {
+            unset($aModel['id']);
+            $aModel['title'] .= ' (Copy)';
+            $aModel['duplicate'] = 1;
+            $aModel['changed'] = time();
+            $iNewId = $this->_oDb->insertModel($aModel);
+            if ($iNewId) {
+                $aRes = ['grid' => $this->getCode(false), 'blink' => $iNewId];
                 return echoJson($aRes);
             }
         }
-
-        $sFormId = $oForm->getId();
-        $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_models_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
-            'form_id' => $sFormId,
-            'form' => $oForm->getCode(true),
-            'object' => $this->_sObject,
-            'action' => $sAction
-        ]));
-
-        return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
+        $aRes = ['msg' => _t('_sys_txt_error_occured')];
+        return echoJson($aRes);
     }
 
     public function performActionEdit()
@@ -103,6 +88,16 @@ class BxBaseStudioAgentsModels extends BxDolStudioAgentsModels
     {
         $sJsObject = $this->getPageJsObject();
 
+        $sParams = $aModel['params'];
+        if (!empty($aModel['params_user']))
+            $sParams = $aModel['params_user'];
+        $s = json_decode($sParams, true);
+        $sParams = json_encode($s, JSON_PRETTY_PRINT);
+
+        $oParsedown = new Parsedown();
+        $oParsedown->setSafeMode(false);
+        $sDocs = $oParsedown->text($aModel['docs']);
+
         $aForm = array(
             'form_attrs' => array(
                 'id' => 'bx_std_agents_models_' . $sAction,
@@ -117,12 +112,18 @@ class BxBaseStudioAgentsModels extends BxDolStudioAgentsModels
                 ),
             ),
             'inputs' => array(
-                'name' => [
+                'docs' => [
+                    'type' => 'custom',
+                    'name' => 'docs',
+                    'caption' => '',
+                    'content' => $sDocs,
+                ],
+                'title' => [
                     'type' => 'text',
-                    'name' => 'name',
+                    'name' => 'title',
                     'required' => '1',
-                    'caption' => _t('_sys_agents_models_field_name'),
-                    'value' => isset($aModel['name']) ? $aModel['name'] : '',
+                    'caption' => _t('_sys_agents_models_field_title'),
+                    'value' => isset($aModel['title']) ? $aModel['title'] : '',
                     'required' => '1',
                     'checker' => [
                         'func' => 'Avail',
@@ -133,35 +134,36 @@ class BxBaseStudioAgentsModels extends BxDolStudioAgentsModels
                         'pass' => 'Xss',
                     ]
                 ],
+                'model' => [
+                    'type' => 'text',
+                    'name' => 'model',
+                    'caption' => _t('_sys_agents_models_field_model'),
+                    'value' => isset($aModel['model']) ? $aModel['model'] : '',
+                    'db' => [
+                        'pass' => 'Xss',
+                    ]
+                ],
                 'key' => [
                     'type' => 'text',
                     'name' => 'key',
-                    'required' => '1',
                     'caption' => _t('_sys_agents_models_field_key'),
                     'value' => isset($aModel['key']) ? $aModel['key'] : '',
-                    'required' => '1',
-                    'checker' => [
-                        'func' => 'Avail',
-                        'params' => [],
-                        'error' => _t('_sys_agents_form_field_err_enter'),
-                    ],
                     'db' => [
                         'pass' => 'Xss',
                     ]
                 ],
-                'params' => [
+                'params_user' => [
                     'type' => 'textarea',
-                    'name' => 'params',
+                    'name' => 'params_user',
                     'caption' => _t('_sys_agents_models_field_params'),
-                    'value' => isset($aModel['params']) ? $aModel['params'] : '',
-                    'required' => '1',
+                    'value' => $sParams,
                     'checker' => [
-                        'func' => 'Avail',
-                        'params' => [],
-                        'error' => _t('_sys_agents_form_field_err_enter'),
+                        'func' => 'Json',
+                        'params' => ['allow_empty' => true],
+                        'error' => _t('_sys_agents_json_field_err'),
                     ],
                     'db' => [
-                        'pass' => 'Xss',
+                        'pass' => 'All'
                     ]
                 ],
                 'submit' => $this->_getFormControls(),
@@ -170,6 +172,21 @@ class BxBaseStudioAgentsModels extends BxDolStudioAgentsModels
 
         return $aForm;
     }
+
+    protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
+    {
+        if ($sType == 'single' && $aRow['duplicate'] == 0)
+            return '';
+        return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
+    }
+
+    protected function _delete ($mixedId)
+    {
+        $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedId]);
+        if (empty($aModel) || $aModel['duplicate'] == 0)
+            return false;
+        return parent::_delete($mixedId);
+    }
 }
 
 /** @} */

+ 1 - 1
studio/template/scripts/BxBaseStudioAgentsVectorStore.php

@@ -190,7 +190,7 @@ class BxBaseStudioAgentsVectorStore extends BxDolStudioAgentsVectorStore
                     'checker' => [
                         'func' => 'Json',
                         'params' => ['allow_empty' => true],
-                        'error' => _t('_sys_agents_vector_store_json_field_err'),
+                        'error' => _t('_sys_agents_json_field_err'),
                     ],
                     'db' => [
                         'pass' => 'All'