BxBaseStudioAgentsAgents.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioAgentsAgents extends BxDolStudioAgentsAgents
  11. {
  12. protected $_sUrlPage;
  13. protected $_sFieldName;
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=agents';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionManual()
  25. {
  26. $iId = $this->_getId();
  27. $aAgent = BxDolAiQuery::getAgentObject($iId);
  28. if (!$aAgent) {
  29. echoJson(['msg' => _t('_sys_txt_error_occured')]);
  30. return;
  31. }
  32. if (!$aAgent['active']) {
  33. echoJson(['msg' => _t('_sys_txt_agent_inactive')]);
  34. return;
  35. }
  36. $oAi = BxDolAI::getInstance();
  37. $sResponse = $oAi->callAgent('manual', $aAgent);
  38. $oParsedown = new Parsedown();
  39. $oParsedown->setSafeMode(true);
  40. $sMessageHtml = $oParsedown->text($sResponse);
  41. $oTemplate = BxDolStudioTemplate::getInstance();
  42. $sHtml = $oTemplate->parseHtmlByName('agents_manual_response.html', [
  43. 'response' => $sMessageHtml
  44. ]);
  45. echoJson(['msg' => $sHtml]);
  46. }
  47. public function performActionAdd()
  48. {
  49. $sAction = 'add';
  50. $oTemplate = BxDolStudioTemplate::getInstance();
  51. $oAI = BxDolAI::getInstance();
  52. $aForm = $this->_getForm($sAction);
  53. $oForm = new BxTemplFormView($aForm);
  54. $oForm->initChecker();
  55. if($oForm->isSubmittedAndValid()) {
  56. $aValsToAdd = [
  57. 'added' => time()
  58. ];
  59. $sName = $oForm->getCleanValue($this->_sFieldName);
  60. $sName = $this->_getUniqName($sName);
  61. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  62. $iProfileId = $oForm->getCleanValue('profile_id');
  63. if(empty($iProfileId)) {
  64. $iProfileId = (int)getParam('sys_agents_profile');
  65. if(empty($iProfileId))
  66. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  67. $aValsToAdd['profile_id'] = $iProfileId;
  68. }
  69. $iModel = $oForm->getCleanValue('model_id');
  70. $sTrigger = $oForm->getCleanValue('trigger');
  71. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  72. $aValsToAdd['tools'] = $sTools;
  73. $bIsValid = true;
  74. if($bIsValid) {
  75. if(($iId = $oForm->insert($aValsToAdd)) !== false) {
  76. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  77. }
  78. else
  79. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  80. return echoJson($aRes);
  81. }
  82. }
  83. $sFormId = $oForm->getId();
  84. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  85. 'form_id' => $sFormId,
  86. 'form' => $oForm->getCode(true),
  87. 'object' => $this->_sObject,
  88. 'action' => $sAction
  89. ]));
  90. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  91. }
  92. public function performActionEdit()
  93. {
  94. $sAction = 'edit';
  95. $oTemplate = BxDolStudioTemplate::getInstance();
  96. $oAI = BxDolAI::getInstance();
  97. $iId = $this->_getId();
  98. $aAgent = BxDolAiQuery::getAgentObject($iId);
  99. $aForm = $this->_getFormEdit($sAction, $aAgent);
  100. $oForm = new BxTemplFormView($aForm);
  101. $oForm->initChecker();
  102. if($oForm->isSubmittedAndValid()) {
  103. $aValsToAdd = [];
  104. $sName = $oForm->getCleanValue($this->_sFieldName);
  105. $sName = $this->_getUniqName($sName);
  106. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  107. $iProfileId = $oForm->getCleanValue('profile_id');
  108. if(empty($iProfileId)) {
  109. $iProfileId = (int)getParam('sys_agents_profile');
  110. if(empty($iProfileId))
  111. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  112. $aValsToAdd['profile_id'] = $iProfileId;
  113. }
  114. $iModel = $oForm->getCleanValue('model_id');
  115. $sTrigger = $oForm->getCleanValue('trigger');
  116. $sTools = is_array($oForm->getCleanValue('tools')) ? implode(',', $oForm->getCleanValue('tools')) : '';
  117. $aValsToAdd['tools'] = $sTools;
  118. $bIsValid = true;
  119. if($bIsValid) {
  120. if(($iId = $oForm->update($iId, $aValsToAdd)) !== false) {
  121. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  122. }
  123. else
  124. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  125. return echoJson($aRes);
  126. }
  127. }
  128. $sFormId = $oForm->getId();
  129. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_agents_popup_edit', $aAgent['name']), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  130. 'form_id' => $sFormId,
  131. 'form' => $oForm->getCode(true),
  132. 'object' => $this->_sObject,
  133. 'action' => $sAction
  134. ]));
  135. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  136. }
  137. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  138. {
  139. // if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  140. // return parent::_getCellDefault('', $sKey, $aField, $aRow);
  141. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  142. }
  143. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  144. {
  145. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  146. }
  147. protected function _delete ($mixedId)
  148. {
  149. $mixedResult = parent::_delete($mixedId);
  150. if($mixedResult) {
  151. // $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  152. // $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
  153. // $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
  154. // if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
  155. // $oCmts->onObjectDelete();
  156. }
  157. return $mixedResult;
  158. }
  159. protected function _getFormEdit($sAction, $aAgent = [])
  160. {
  161. $aForm = $this->_getForm($sAction, $aAgent);
  162. $aForm['form_attrs']['action'] .= '&id=' . $aAgent['id'];
  163. unset($aForm['inputs']['name']);
  164. return $aForm;
  165. }
  166. protected function _getForm($sAction, $aAgent = [])
  167. {
  168. $aForm = array(
  169. 'form_attrs' => array(
  170. 'id' => 'bx_std_agents_' . $sAction,
  171. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_agents&a=' . $sAction,
  172. 'method' => 'post',
  173. ),
  174. 'params' => array (
  175. 'db' => array(
  176. 'table' => 'sys_agents_agents',
  177. 'key' => 'id',
  178. 'submit_name' => 'do_submit',
  179. ),
  180. ),
  181. 'inputs' => array(
  182. 'name' => [
  183. 'type' => 'text',
  184. 'name' => 'name',
  185. 'caption' => _t('_sys_agents_field_name'),
  186. 'info' => _t('_sys_agents_field_name_info'),
  187. 'value' => isset($aAgent['name']) ? $aAgent['name'] : '',
  188. 'required' => '1',
  189. 'checker' => [
  190. 'func' => 'Avail',
  191. 'error' => _t('_sys_agents_form_field_err_enter'),
  192. ],
  193. 'db' => [
  194. 'pass' => 'Xss',
  195. ],
  196. ],
  197. 'model_id' => [
  198. 'type' => 'select',
  199. 'name' => 'model_id',
  200. 'caption' => _t('_sys_agents_field_model_id'),
  201. 'info' => _t('_sys_agents_field_model_id_info'),
  202. 'value' => isset($aAgent['model_id']) ? $aAgent['model_id'] : BxDolAI::getDefaultModel(),
  203. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'capabilities' => ['chatvlm', 'chatllm']]),
  204. 'required' => '1',
  205. 'checker' => [
  206. 'func' => 'Avail',
  207. 'error' => _t('_sys_agents_form_field_err_select'),
  208. ],
  209. 'db' => [
  210. 'pass' => 'Int',
  211. ],
  212. ],
  213. 'profile_id' => [
  214. 'type' => 'select',
  215. 'name' => 'profile_id',
  216. 'caption' => _t('_sys_agents_field_profile_id'),
  217. 'info' => _t('_sys_agents_field_profile_id_inf'),
  218. 'value' => isset($aAgent['profile_id']) ? $aAgent['profile_id'] : 0,
  219. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  220. 'required' => '1',
  221. 'required' => '1',
  222. 'checker' => [
  223. 'func' => 'Avail',
  224. 'error' => _t('_sys_agents_form_field_err_select'),
  225. ],
  226. 'db' => [
  227. 'pass' => 'Int',
  228. ]
  229. ],
  230. 'trigger' => [
  231. 'type' => 'select',
  232. 'name' => 'trigger',
  233. 'caption' => _t('_sys_agents_field_trigger'),
  234. 'info' => _t('_sys_agents_field_trigger_info'),
  235. 'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
  236. 'values' => [
  237. 'alert' => 'alert',
  238. 'scheduler' => 'scheduler',
  239. 'webhook' => 'webhook',
  240. 'manual' => 'manual',
  241. 'agent' => 'agent',
  242. 'message' => 'message',
  243. ],
  244. 'attrs' => [
  245. 'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
  246. ],
  247. 'required' => '1',
  248. 'db' => [
  249. 'pass' => 'Xss',
  250. ]
  251. ],
  252. 'alert_section' => array(
  253. 'type' => 'block_header',
  254. 'caption' => 'Trigger - alert',
  255. 'collapsable' => true,
  256. 'collapsed' => true,
  257. ),
  258. 'alert_sample' => [
  259. 'type' => 'custom',
  260. 'name' => 'alert_sample',
  261. 'caption' => _t('_sys_agents_field_alert_sample'),
  262. 'content' => isset($aAgent['alert_sample']) ? $aAgent['alert_sample'] : _t('_sys_agents_waiting_for_sample_data'),
  263. ],
  264. 'alert_unit' => [
  265. 'type' => 'text',
  266. 'name' => 'alert_unit',
  267. 'caption' => _t('_sys_agents_field_alert_unit'),
  268. 'info' => _t('_sys_agents_field_alert_unit_info'),
  269. 'value' => isset($aAgent['alert_unit']) ? $aAgent['alert_unit'] : '',
  270. 'db' => [
  271. 'pass' => 'Xss',
  272. ],
  273. ],
  274. 'alert_action' => [
  275. 'type' => 'text',
  276. 'name' => 'alert_action',
  277. 'caption' => _t('_sys_agents_field_alert_action'),
  278. 'value' => isset($aAgent['alert_action']) ? $aAgent['alert_action'] : '',
  279. 'db' => [
  280. 'pass' => 'Xss',
  281. ],
  282. ],
  283. 'scheduler_section' => array(
  284. 'type' => 'block_header',
  285. 'caption' => 'Trigger - scheduler',
  286. 'collapsable' => true,
  287. 'collapsed' => true,
  288. ),
  289. 'scheduler_cron' => [
  290. 'type' => 'text',
  291. 'name' => 'scheduler_cron',
  292. 'caption' => _t('_sys_agents_field_scheduler_cron'),
  293. 'info' => _t('_sys_agents_field_scheduler_cron_info'),
  294. 'value' => isset($aAgent['scheduler_cron']) ? $aAgent['scheduler_cron'] : '',
  295. 'db' => [
  296. 'pass' => 'Xss',
  297. ],
  298. ],
  299. 'webhook_section' => array(
  300. 'type' => 'block_header',
  301. 'caption' => 'Trigger - webhook',
  302. 'collapsable' => true,
  303. 'collapsed' => true,
  304. ),
  305. 'webhook_sample' => [
  306. 'type' => 'custom',
  307. 'name' => 'webhook_sample',
  308. 'caption' => _t('_sys_agents_field_webhook_sample'),
  309. 'content' => isset($aAgent['webhook_sample']) ? $aAgent['webhook_sample'] : _t('_sys_agents_waiting_for_sample_data'),
  310. ],
  311. 'webhook_key' => [
  312. 'type' => 'text',
  313. 'name' => 'webhook_key',
  314. 'caption' => _t('_sys_agents_field_webhook_key'),
  315. 'info' => _t('_sys_agents_field_webhook_key_info'),
  316. 'value' => isset($aAgent['webhook_key']) ? $aAgent['webhook_key'] : '',
  317. 'db' => [
  318. 'pass' => 'Xss',
  319. ],
  320. ],
  321. 'message_section' => array(
  322. 'type' => 'block_header',
  323. 'caption' => 'Trigger - message',
  324. 'collapsable' => true,
  325. 'collapsed' => true,
  326. ),
  327. 'message_profile_id' => [
  328. 'type' => 'select',
  329. 'name' => 'message_profile_id',
  330. 'caption' => _t('_sys_agents_field_message_profile'),
  331. 'info' => _t('_sys_agents_field_message_profile_info'),
  332. 'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
  333. 'values' => bx_srv('system', 'get_options_agents_profile', [true, '_adm_nav_txt_items_visible_for_all'], 'TemplServices'),
  334. 'db' => [
  335. 'pass' => 'Xss',
  336. ],
  337. ],
  338. 'message_section_end' => array(
  339. 'type' => 'block_end',
  340. ),
  341. 'vector_store_id' => [
  342. 'type' => 'select',
  343. 'name' => 'vector_store_id',
  344. 'caption' => _t('_sys_agents_field_vector_store_id'),
  345. 'info' => _t('_sys_agents_field_vector_store_id_info'),
  346. 'value' => isset($aAgent['vector_store_id']) ? $aAgent['vector_store_id'] : 0,
  347. 'values' => $this->_oDb->getVectorStores(),
  348. 'required' => '1',
  349. 'checker' => [
  350. 'func' => 'Avail',
  351. 'error' => _t('_sys_agents_form_field_err_select'),
  352. ],
  353. 'db' => [
  354. 'pass' => 'Xss',
  355. ],
  356. ],
  357. 'chat_history_context' => [
  358. 'type' => 'text',
  359. 'name' => 'chat_history_context',
  360. 'caption' => _t('_sys_agents_field_chat_history_context'),
  361. 'info' => _t('_sys_agents_field_chat_history_context_info'),
  362. 'value' => isset($aAgent['chat_history_context']) ? $aAgent['chat_history_context'] : 50000,
  363. 'db' => [
  364. 'pass' => 'Int',
  365. ],
  366. ],
  367. 'tools' => [
  368. 'type' => 'custom',
  369. 'name' => 'tools',
  370. 'caption' => _t('_sys_agents_field_tools'),
  371. 'info' => _t('_sys_agents_field_tools_info'),
  372. 'value' => '',
  373. ],
  374. 'prompt_system' => [
  375. 'type' => 'textarea',
  376. 'name' => 'prompt_system',
  377. 'caption' => _t('_sys_agents_field_prompt_system'),
  378. 'info' => _t('_sys_agents_field_prompt_system_info'),
  379. 'value' => isset($aAgent['prompt_system']) ? $aAgent['prompt_system'] : '',
  380. 'db' => [
  381. 'pass' => 'Xss',
  382. ],
  383. ],
  384. 'prompts_section' => array(
  385. 'type' => 'block_header',
  386. 'caption' => 'Other prompts',
  387. 'collapsable' => true,
  388. 'collapsed' => true,
  389. ),
  390. 'prompt_steps' => [
  391. 'type' => 'textarea',
  392. 'name' => 'prompt_steps',
  393. 'caption' => _t('_sys_agents_field_prompt_steps'),
  394. 'info' => _t('_sys_agents_field_prompt_steps_info'),
  395. 'value' => isset($aAgent['prompt_steps']) ? $aAgent['prompt_steps'] : '',
  396. 'db' => [
  397. 'pass' => 'Xss',
  398. ],
  399. ],
  400. 'prompt_output' => [
  401. 'type' => 'textarea',
  402. 'name' => 'prompt_output',
  403. 'caption' => _t('_sys_agents_field_prompt_output'),
  404. 'info' => _t('_sys_agents_field_prompt_output_info'),
  405. 'value' => isset($aAgent['prompt_output']) ? $aAgent['prompt_output'] : '',
  406. 'db' => [
  407. 'pass' => 'Xss',
  408. ],
  409. ],
  410. 'prompt_tools' => [
  411. 'type' => 'textarea',
  412. 'name' => 'prompt_tools',
  413. 'caption' => _t('_sys_agents_field_prompt_tools'),
  414. 'info' => _t('_sys_agents_field_prompt_tools_info'),
  415. 'value' => isset($aAgent['prompt_tools']) ? $aAgent['prompt_tools'] : '',
  416. 'db' => [
  417. 'pass' => 'Xss',
  418. ],
  419. ],
  420. 'prompts_section_end' => array(
  421. 'type' => 'block_end',
  422. ),
  423. 'submit' => $this->_getFormControls(),
  424. ),
  425. );
  426. $this->_getMultiField('tools', $aAgent, 'getTools', 'toolAdd', 'agents_agents_form_tools.html', $aForm);
  427. return $aForm;
  428. }
  429. protected function _getUniqName($sName)
  430. {
  431. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  432. }
  433. protected function _getMultiField($sField, $aAgent, $sGetValuesMethod, $sJsAddMethod, $sTemplateName, &$aForm)
  434. {
  435. if(!isset($aForm['inputs']['tools']))
  436. return;
  437. $sJsObject = $this->getPageJsObject();
  438. $oForm = new BxTemplFormView([]);
  439. $aValues = ['0' => _t('_sys_please_select')];
  440. $a = $this->_oDb->$sGetValuesMethod();
  441. foreach ($a as $k => $v) {
  442. $aValues[$k] = $v;
  443. }
  444. $aTmplVars = [];
  445. if(!empty($aAgent[$sField])) {
  446. $a = explode(',', $aAgent[$sField]);
  447. foreach($a as $v) {
  448. $aInputSelect = [
  449. 'type' => 'select',
  450. 'name' => $sField . '[]',
  451. 'values' => $aValues,
  452. 'value' => $v,
  453. 'attrs' => [
  454. 'class' => 'bx-def-margin-sec-top-auto'
  455. ]
  456. ];
  457. $sInput = $oForm->genInput($aInputSelect);
  458. $aInputHidden = [
  459. 'type' => 'hidden',
  460. 'name' => $sField . '_ids[]',
  461. 'value' => $v,
  462. ];
  463. $sInput .= $oForm->genInput($aInputHidden);
  464. $aTmplVars[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  465. }
  466. }
  467. else {
  468. $aInputSelect = [
  469. 'type' => 'select',
  470. 'name' => $sField . '[]',
  471. 'values' => $aValues,
  472. 'value' => '',
  473. 'attrs' => [
  474. 'class' => 'bx-def-margin-sec-top-auto'
  475. ]
  476. ];
  477. $aTmplVars = [
  478. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  479. ];
  480. }
  481. $aInputButton = [
  482. 'type' => 'button',
  483. 'name' => $sField . '_add',
  484. 'value' => _t('_adm_nav_btn_items_add'),
  485. 'attrs' => [
  486. 'class' => 'bx-def-margin-sec-top',
  487. 'onclick' => $sJsObject . ".$sJsAddMethod(this, '" . $sField . "');"
  488. ]
  489. ];
  490. $aForm['inputs'][$sField]['content'] = $this->_oTemplate->parseHtmlByName($sTemplateName, [
  491. "bx_repeat:$sField" => $aTmplVars,
  492. 'btn_add' => $oForm->genInputButton($aInputButton)
  493. ]);
  494. }
  495. protected function _getActionManual ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  496. {
  497. if ($aRow['trigger'] != 'manual' && $aRow['trigger'] != 'scheduler')
  498. return '';
  499. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  500. }
  501. }
  502. /** @} */