BxBaseStudioAgentsAgents.php 21 KB

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