| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaView UNA Studio Representation classes
- * @ingroup UnaStudio
- * @{
- */
- class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
- {
- protected $_sUrlPage;
- protected $_sFieldName;
- public function __construct ($aOptions, $oTemplate = false)
- {
- parent::__construct ($aOptions, $oTemplate);
- $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=agents';
- $this->_sFieldName = 'name';
- }
- public function getPageJsObject()
- {
- return 'oBxDolStudioPageAgents';
- }
- public function performActionManual()
- {
- $iId = $this->_getId();
- $aAgent = BxDolAiQuery::getAgentObject($iId);
- if (!$aAgent) {
- echoJson(['msg' => _t('_sys_txt_error_occured')]);
- return;
- }
- if (!$aAgent['active']) {
- echoJson(['msg' => _t('_sys_txt_agent_inactive')]);
- return;
- }
- $oAi = BxDolAI::getInstance();
- $sResponse = $oAi->callAgent('manual', $aAgent);
- $oParsedown = new Parsedown();
- $oParsedown->setSafeMode(true);
- $sMessageHtml = $oParsedown->text($sResponse);
- $oTemplate = BxDolStudioTemplate::getInstance();
- $sHtml = $oTemplate->parseHtmlByName('agents_manual_response.html', [
- 'response' => $sMessageHtml
- ]);
- echoJson(['msg' => $sHtml]);
- }
- public function performActionAdd()
- {
- $sAction = 'add';
- $oTemplate = BxDolStudioTemplate::getInstance();
- $oAI = BxDolAI::getInstance();
- $aForm = $this->_getForm($sAction);
- $oForm = new BxTemplFormView($aForm);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $aValsToAdd = [
- 'added' => time()
- ];
- $sName = $oForm->getCleanValue($this->_sFieldName);
- $sName = $this->_getUniqName($sName);
- BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
- $iProfileId = $oForm->getCleanValue('profile_id');
- if(empty($iProfileId)) {
- $iProfileId = (int)getParam('sys_agents_profile');
- if(empty($iProfileId))
- $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
- $aValsToAdd['profile_id'] = $iProfileId;
- }
-
- $iModel = $oForm->getCleanValue('model_id');
- $sTrigger = $oForm->getCleanValue('trigger');
- $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
- $aValsToAdd['tools'] = $sTools;
- $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')];
- return echoJson($aRes);
- }
- }
- $sFormId = $oForm->getId();
- $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_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]]]);
- }
- public function performActionEdit()
- {
- $sAction = 'edit';
- $oTemplate = BxDolStudioTemplate::getInstance();
- $oAI = BxDolAI::getInstance();
- $iId = $this->_getId();
- $aAgent = BxDolAiQuery::getAgentObject($iId);
- $aForm = $this->_getFormEdit($sAction, $aAgent);
- $oForm = new BxTemplFormView($aForm);
- $oForm->initChecker();
- if($oForm->isSubmittedAndValid()) {
- $aValsToAdd = [];
- $sName = $oForm->getCleanValue($this->_sFieldName);
- $sName = $this->_getUniqName($sName);
- BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
- $iProfileId = $oForm->getCleanValue('profile_id');
- if(empty($iProfileId)) {
- $iProfileId = (int)getParam('sys_agents_profile');
- if(empty($iProfileId))
- $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
- $aValsToAdd['profile_id'] = $iProfileId;
- }
-
- $iModel = $oForm->getCleanValue('model_id');
- $sTrigger = $oForm->getCleanValue('trigger');
- $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
- $aValsToAdd['tools'] = $sTools;
- $bIsValid = true;
- if($bIsValid) {
- if(($iId = $oForm->update($iId, $aValsToAdd)) !== false) {
- $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
- }
- else
- $aRes = ['msg' => _t('_sys_txt_error_occured')];
- return echoJson($aRes);
- }
- }
- $sFormId = $oForm->getId();
- $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_edit', $aAgent['name']), $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]]]);
- }
- protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
- {
- // if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
- // return parent::_getCellDefault('', $sKey, $aField, $aRow);
- return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
- }
- protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
- {
- return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
- }
-
- protected function _delete ($mixedId)
- {
- $mixedResult = parent::_delete($mixedId);
- if($mixedResult) {
- // $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
- // $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
- // $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
- // if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
- // $oCmts->onObjectDelete();
- }
- return $mixedResult;
- }
- protected function _getFormEdit($sAction, $aAgent = [])
- {
- $aForm = $this->_getForm($sAction, $aAgent);
- $aForm['form_attrs']['action'] .= '&id=' . $aAgent['id'];
- unset($aForm['inputs']['name']);
- return $aForm;
- }
- protected function _getForm($sAction, $aAgent = [])
- {
- $aForm = array(
- 'form_attrs' => array(
- 'id' => 'bx_std_agents_' . $sAction,
- 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_agents&a=' . $sAction,
- 'method' => 'post',
- ),
- 'params' => array (
- 'db' => array(
- 'table' => 'sys_agents_agents',
- 'key' => 'id',
- 'submit_name' => 'do_submit',
- ),
- ),
- 'inputs' => array(
- 'name' => [
- 'type' => 'text',
- 'name' => 'name',
- 'caption' => _t('_sys_agents_field_name'),
- 'info' => _t('_sys_agents_field_name_info'),
- 'value' => isset($aAgent['name']) ? $aAgent['name'] : '',
- 'required' => '1',
- 'checker' => [
- 'func' => 'Avail',
- 'error' => _t('_sys_agents_form_field_err_enter'),
- ],
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'model_id' => [
- 'type' => 'select',
- 'name' => 'model_id',
- 'caption' => _t('_sys_agents_field_model_id'),
- 'info' => _t('_sys_agents_field_model_id_info'),
- 'value' => isset($aAgent['model_id']) ? $aAgent['model_id'] : BxDolAI::getDefaultModel(),
- 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'capabilities' => ['chatvlm', 'chatllm']]),
- 'required' => '1',
- 'checker' => [
- 'func' => 'Avail',
- 'error' => _t('_sys_agents_form_field_err_select'),
- ],
- 'db' => [
- 'pass' => 'Int',
- ],
- ],
- 'profile_id' => [
- 'type' => 'select',
- 'name' => 'profile_id',
- 'caption' => _t('_sys_agents_field_profile_id'),
- 'info' => _t('_sys_agents_field_profile_id_inf'),
- 'value' => isset($aAgent['profile_id']) ? $aAgent['profile_id'] : 0,
- 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
- 'required' => '1',
- 'required' => '1',
- 'checker' => [
- 'func' => 'Avail',
- 'error' => _t('_sys_agents_form_field_err_select'),
- ],
- 'db' => [
- 'pass' => 'Int',
- ]
- ],
- 'trigger' => [
- 'type' => 'select',
- 'name' => 'trigger',
- 'caption' => _t('_sys_agents_field_trigger'),
- 'info' => _t('_sys_agents_field_trigger_info'),
- 'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
- 'values' => [
- 'alert' => 'alert',
- 'scheduler' => 'scheduler',
- 'webhook' => 'webhook',
- 'manual' => 'manual',
- 'agent' => 'agent',
- 'message' => 'message',
- ],
- 'attrs' => [
- 'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
- ],
- 'required' => '1',
- 'db' => [
- 'pass' => 'Xss',
- ]
- ],
- 'alert_section' => array(
- 'type' => 'block_header',
- 'caption' => 'Trigger - alert',
- 'collapsable' => true,
- 'collapsed' => true,
- ),
- 'alert_sample' => [
- 'type' => 'custom',
- 'name' => 'alert_sample',
- 'caption' => _t('_sys_agents_field_alert_sample'),
- 'content' => isset($aAgent['alert_sample']) ? $aAgent['alert_sample'] : _t('_sys_agents_waiting_for_sample_data'),
- ],
- 'alert_unit' => [
- 'type' => 'text',
- 'name' => 'alert_unit',
- 'caption' => _t('_sys_agents_field_alert_unit'),
- 'info' => _t('_sys_agents_field_alert_unit_info'),
- 'value' => isset($aAgent['alert_unit']) ? $aAgent['alert_unit'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'alert_action' => [
- 'type' => 'text',
- 'name' => 'alert_action',
- 'caption' => _t('_sys_agents_field_alert_action'),
- 'value' => isset($aAgent['alert_action']) ? $aAgent['alert_action'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'scheduler_section' => array(
- 'type' => 'block_header',
- 'caption' => 'Trigger - scheduler',
- 'collapsable' => true,
- 'collapsed' => true,
- ),
- 'scheduler_cron' => [
- 'type' => 'text',
- 'name' => 'scheduler_cron',
- 'caption' => _t('_sys_agents_field_scheduler_cron'),
- 'info' => _t('_sys_agents_field_scheduler_cron_info'),
- 'value' => isset($aAgent['scheduler_cron']) ? $aAgent['scheduler_cron'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'webhook_section' => array(
- 'type' => 'block_header',
- 'caption' => 'Trigger - webhook',
- 'collapsable' => true,
- 'collapsed' => true,
- ),
- 'webhook_sample' => [
- 'type' => 'custom',
- 'name' => 'webhook_sample',
- 'caption' => _t('_sys_agents_field_webhook_sample'),
- 'content' => isset($aAgent['webhook_sample']) ? $aAgent['webhook_sample'] : _t('_sys_agents_waiting_for_sample_data'),
- ],
- 'webhook_key' => [
- 'type' => 'text',
- 'name' => 'webhook_key',
- 'caption' => _t('_sys_agents_field_webhook_key'),
- 'info' => _t('_sys_agents_field_webhook_key_info'),
- 'value' => isset($aAgent['webhook_key']) ? $aAgent['webhook_key'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'message_section' => array(
- 'type' => 'block_header',
- 'caption' => 'Trigger - message',
- 'collapsable' => true,
- 'collapsed' => true,
- ),
- 'message_profile_id' => [
- 'type' => 'select',
- 'name' => 'message_profile_id',
- 'caption' => _t('_sys_agents_field_message_profile'),
- 'info' => _t('_sys_agents_field_message_profile_info'),
- 'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
- 'values' => bx_srv('system', 'get_options_agents_profile', [true, '_adm_nav_txt_items_visible_for_all'], 'TemplServices'),
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'message_section_end' => array(
- 'type' => 'block_end',
- ),
- 'vector_store_id' => [
- 'type' => 'select',
- 'name' => 'vector_store_id',
- 'caption' => _t('_sys_agents_field_vector_store_id'),
- 'info' => _t('_sys_agents_field_vector_store_id_info'),
- 'value' => isset($aAgent['vector_store_id']) ? $aAgent['vector_store_id'] : 0,
- 'values' => $this->_oDb->getVectorStores(),
- 'required' => '1',
- 'checker' => [
- 'func' => 'Avail',
- 'error' => _t('_sys_agents_form_field_err_select'),
- ],
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'chat_history_context' => [
- 'type' => 'text',
- 'name' => 'chat_history_context',
- 'caption' => _t('_sys_agents_field_chat_history_context'),
- 'info' => _t('_sys_agents_field_chat_history_context_info'),
- 'value' => isset($aAgent['chat_history_context']) ? $aAgent['chat_history_context'] : 50000,
- 'db' => [
- 'pass' => 'Int',
- ],
- ],
- 'tools' => [
- 'type' => 'custom',
- 'name' => 'tools',
- 'caption' => _t('_sys_agents_field_tools'),
- 'info' => _t('_sys_agents_field_tools_info'),
- 'value' => '',
- ],
- 'prompt_system' => [
- 'type' => 'textarea',
- 'name' => 'prompt_system',
- 'caption' => _t('_sys_agents_field_prompt_system'),
- 'info' => _t('_sys_agents_field_prompt_system_info'),
- 'value' => isset($aAgent['prompt_system']) ? $aAgent['prompt_system'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'prompts_section' => array(
- 'type' => 'block_header',
- 'caption' => 'Other prompts',
- 'collapsable' => true,
- 'collapsed' => true,
- ),
- 'prompt_steps' => [
- 'type' => 'textarea',
- 'name' => 'prompt_steps',
- 'caption' => _t('_sys_agents_field_prompt_steps'),
- 'info' => _t('_sys_agents_field_prompt_steps_info'),
- 'value' => isset($aAgent['prompt_steps']) ? $aAgent['prompt_steps'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'prompt_output' => [
- 'type' => 'textarea',
- 'name' => 'prompt_output',
- 'caption' => _t('_sys_agents_field_prompt_output'),
- 'info' => _t('_sys_agents_field_prompt_output_info'),
- 'value' => isset($aAgent['prompt_output']) ? $aAgent['prompt_output'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'prompt_tools' => [
- 'type' => 'textarea',
- 'name' => 'prompt_tools',
- 'caption' => _t('_sys_agents_field_prompt_tools'),
- 'info' => _t('_sys_agents_field_prompt_tools_info'),
- 'value' => isset($aAgent['prompt_tools']) ? $aAgent['prompt_tools'] : '',
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'prompts_section_end' => array(
- 'type' => 'block_end',
- ),
- 'submit' => $this->_getFormControls(),
- ),
- );
-
- $this->_getMultiField('tools', $aAgent, 'getTools', 'toolAdd', 'agents_agents_form_tools.html', $aForm);
- return $aForm;
- }
- protected function _getUniqName($sName)
- {
- return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
- }
- protected function _getMultiField($sField, $aAgent, $sGetValuesMethod, $sJsAddMethod, $sTemplateName, &$aForm)
- {
- if(!isset($aForm['inputs']['tools']))
- return;
- $sJsObject = $this->getPageJsObject();
- $oForm = new BxTemplFormView([]);
- $aValues = ['0' => _t('_sys_please_select')];
- $a = $this->_oDb->$sGetValuesMethod();
- foreach ($a as $k => $v) {
- $aValues[$k] = $v;
- }
- $aTmplVars = [];
- if(!empty($aAgent[$sField])) {
- $a = explode(',', $aAgent[$sField]);
- foreach($a as $v) {
- $aInputSelect = [
- 'type' => 'select',
- 'name' => $sField . '[]',
- 'values' => $aValues,
- 'value' => $v,
- 'attrs' => [
- 'class' => 'bx-def-margin-sec-top-auto'
- ]
- ];
- $sInput = $oForm->genInput($aInputSelect);
- $aInputHidden = [
- 'type' => 'hidden',
- 'name' => $sField . '_ids[]',
- 'value' => $v,
- ];
- $sInput .= $oForm->genInput($aInputHidden);
- $aTmplVars[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
- }
- }
- else {
- $aInputSelect = [
- 'type' => 'select',
- 'name' => $sField . '[]',
- 'values' => $aValues,
- 'value' => '',
- 'attrs' => [
- 'class' => 'bx-def-margin-sec-top-auto'
- ]
- ];
- $aTmplVars = [
- ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
- ];
- }
-
- $aInputButton = [
- 'type' => 'button',
- 'name' => $sField . '_add',
- 'value' => _t('_adm_nav_btn_items_add'),
- 'attrs' => [
- 'class' => 'bx-def-margin-sec-top',
- 'onclick' => $sJsObject . ".$sJsAddMethod(this, '" . $sField . "');"
- ]
- ];
- $aForm['inputs'][$sField]['content'] = $this->_oTemplate->parseHtmlByName($sTemplateName, [
- "bx_repeat:$sField" => $aTmplVars,
- 'btn_add' => $oForm->genInputButton($aInputButton)
- ]);
- }
- protected function _getActionManual ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
- {
- if ($aRow['trigger'] != 'manual' && $aRow['trigger'] != 'scheduler')
- return '';
- return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
- }
- }
- /** @} */
|