BxBaseStudioAgentsAgents.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. $aAutomator = $oAI->getAutomator($iId);
  76. $aAutomator['providers'] = $this->_oDb->getAutomatorsBy(['sample' => 'providers_by_id_pairs', 'id' => $iId]);
  77. $aAutomator['helpers'] = $this->_oDb->getAutomatorsBy(['sample' => 'helpers_by_id_pairs', 'id' => $iId]);
  78. $aForm = $this->_getFormEdit($sAction, $aAutomator);
  79. $oForm = new BxTemplFormView($aForm);
  80. $oForm->initChecker();
  81. if($oForm->isSubmittedAndValid()) {
  82. $aValsToAdd = [];
  83. $sName = $oForm->getCleanValue($this->_sFieldName);
  84. if($aAutomator[$this->_sFieldName] != $sName) {
  85. $sName = $this->_getUniqName($sName);
  86. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  87. }
  88. /**
  89. * Process Providers
  90. */
  91. $aProvidersIds = $oForm->getCleanValue('providers_ids');
  92. $bProvidersIds = !empty($aProvidersIds) && is_array($aProvidersIds);
  93. $aProvidersValues = $oForm->getCleanValue('providers');
  94. $bProvidersValues = !empty($aProvidersValues) && is_array($aProvidersValues);
  95. //--- Providers: Remove deleted
  96. if(!empty($aAutomator['providers']) && is_array($aAutomator['providers']))
  97. $this->_oDb->deleteAutomatorProvidersById(array_diff(array_keys($aAutomator['providers']), $bProvidersIds ? $aProvidersIds : []));
  98. //--- Providers: Update existed
  99. if($bProvidersIds)
  100. foreach($aProvidersIds as $iIndex => $iApId)
  101. $this->_oDb->updateAutomatorProvider(['provider_id' => (int)$aProvidersValues[$iIndex]], ['id' => (int)$iApId]);
  102. //--- Providers: Add new
  103. $iProvidersIds = $bProvidersIds ? count($aProvidersIds) : 0;
  104. $iProvidersValues = $bProvidersValues ? count($aProvidersValues) : 0;
  105. if($iProvidersValues > $iProvidersIds) {
  106. $aProvidersValues = array_slice($aProvidersValues, $iProvidersIds);
  107. foreach($aProvidersValues as $iProvidersValue)
  108. $this->_oDb->insertAutomatorProvider([
  109. 'automator_id' => $iId,
  110. 'provider_id' => (int)$iProvidersValue,
  111. ]);
  112. }
  113. /**
  114. * Process Helpers
  115. */
  116. $aHelpersIds = $oForm->getCleanValue('helpers_ids');
  117. $bHelpersIds = !empty($aHelpersIds) && is_array($aHelpersIds);
  118. $aHelpersValues = $oForm->getCleanValue('helpers');
  119. $bHelpersValues = !empty($aHelpersValues) && is_array($aHelpersValues);
  120. //--- Helpers: Remove deleted
  121. if(!empty($aAutomator['helpers']) && is_array($aAutomator['helpers']))
  122. $this->_oDb->deleteAutomatorHelpersById(array_diff(array_keys($aAutomator['helpers']), $bHelpersIds ? $aHelpersIds : []));
  123. //--- Helpers: Update existed
  124. if($bHelpersIds)
  125. foreach($aHelpersIds as $iIndex => $iAhId)
  126. $this->_oDb->updateAutomatorHelper(['helper_id' => (int)$aHelpersValues[$iIndex]], ['id' => (int)$iAhId]);
  127. //--- Helpers: Add new
  128. $iHelpersIds = $bHelpersIds ? count($aHelpersIds) : 0;
  129. $iHelpersValues = $bHelpersValues ? count($aHelpersValues) : 0;
  130. if($iHelpersValues > $iHelpersIds) {
  131. $aHelpersValues = array_slice($aHelpersValues, $iHelpersIds);
  132. foreach($aHelpersValues as $iHelpersValue)
  133. $this->_oDb->insertAutomatorHelper([
  134. 'automator_id' => $iId,
  135. 'helper_id' => (int)$iHelpersValue,
  136. ]);
  137. }
  138. /**
  139. * Process Assistants
  140. */
  141. $aAssistantsIds = $oForm->getCleanValue('assistants_ids');
  142. $bAssistantsIds = !empty($aAssistantsIds) && is_array($aAssistantsIds);
  143. $aAssistantsValues = $oForm->getCleanValue('assistants');
  144. $bAssistantsValues = !empty($aAssistantsValues) && is_array($aAssistantsValues);
  145. //--- Assistants: Remove deleted
  146. if(!empty($aAutomator['assistants']) && is_array($aAutomator['assistants']))
  147. $this->_oDb->deleteAutomatorAssistantsById(array_diff(array_keys($aAutomator['assistants']), $bAssistantsIds ? $aAssistantsIds : []));
  148. //--- Assistants: Update existed
  149. if($bAssistantsIds)
  150. foreach($aAssistantsIds as $iIndex => $iAhId)
  151. $this->_oDb->updateAutomatorAssistant(['assistant_id' => (int)$aAssistantsValues[$iIndex]], ['id' => (int)$iAhId]);
  152. //--- Assistants: Add new
  153. $iAssistantsIds = $bAssistantsIds ? count($aAssistantsIds) : 0;
  154. $iAssistantsValues = $bAssistantsValues ? count($aAssistantsValues) : 0;
  155. if($iAssistantsValues > $iAssistantsIds) {
  156. $aAssistantsValues = array_slice($aAssistantsValues, $iAssistantsIds);
  157. foreach($aAssistantsValues as $iAssistantsValue)
  158. $this->_oDb->insertAutomatorAssistant([
  159. 'automator_id' => $iId,
  160. 'assistant_id' => (int)$iAssistantsValue,
  161. ]);
  162. }
  163. $iProfileId = $oForm->getCleanValue('profile_id');
  164. if(empty($iProfileId)) {
  165. $iProfileId = (int)getParam('sys_agents_profile');
  166. if(empty($iProfileId))
  167. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  168. $aValsToAdd['profile_id'] = $iProfileId;
  169. }
  170. $sSchedulerTime = $oForm->getCleanValue('scheduler_time');
  171. if(!empty($sSchedulerTime))
  172. $aValsToAdd['params'] = json_encode(['scheduler_time' => $sSchedulerTime]);
  173. if($oForm->update($iId, $aValsToAdd) !== false) {
  174. if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($iId, $oTemplate)) !== null) {
  175. $sInstructions = $oAI->getAutomatorInstruction('profile', $iProfileId);
  176. $aProviders = $this->_oDb->getAutomatorsBy(['sample' => 'providers_by_id_pairs', 'id' => $iId]);
  177. if(!empty($aProviders) && is_array($aProviders))
  178. $sInstructions .= $oAI->getAutomatorInstruction('providers', array_values($aProviders));
  179. $aHelpers = $this->_oDb->getAutomatorsBy(['sample' => 'helpers_by_id_pairs', 'id' => $iId]);
  180. if(!empty($aHelpers) && is_array($aHelpers))
  181. $sInstructions .= $oAI->getAutomatorInstruction('helpers', array_values($aHelpers));
  182. $aAssistants = $this->_oDb->getAutomatorsBy(['sample' => 'assistants_by_id_pairs', 'id' => $iId]);
  183. if(!empty($aAssistants) && is_array($aAssistants))
  184. $sInstructions .= $oAI->getAutomatorInstruction('assistants', array_values($aAssistants));
  185. $oCmts->addAuto([
  186. 'cmt_author_id' => $iProfileId,
  187. 'cmt_parent_id' => 0,
  188. 'cmt_text' => $sInstructions
  189. ]);
  190. if(($oMessage = new BxDolAIMessageString('hb', $sInstructions)) && ($sResponse = $oAI->getModelObject($aAutomator['model_id'])->getResponse($aAutomator['type'], $oMessage, $aAutomator['params'])) !== false) {
  191. sleep(1);
  192. $oCmts->addAuto([
  193. 'cmt_author_id' => $this->_iProfileIdAi,
  194. 'cmt_parent_id' => 0,
  195. 'cmt_text' => $sResponse
  196. ]);
  197. }
  198. }
  199. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  200. }
  201. else
  202. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  203. return echoJson($aRes);
  204. }
  205. $sFormId = $oForm->getId();
  206. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_automators_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  207. 'form_id' => $sFormId,
  208. 'form' => $oForm->getCode(true),
  209. 'object' => $this->_sObject,
  210. 'action' => $sAction
  211. ]));
  212. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  213. }
  214. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  215. {
  216. // if(empty($aRow['code']) || $aRow['status'] != BX_DOL_AI_AUTOMATOR_STATUS_READY)
  217. // return parent::_getCellDefault('', $sKey, $aField, $aRow);
  218. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  219. }
  220. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  221. {
  222. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  223. }
  224. protected function _delete ($mixedId)
  225. {
  226. $mixedResult = parent::_delete($mixedId);
  227. if($mixedResult) {
  228. // $this->_oDb->deleteAutomatorProviders(['automator_id' => (int)$mixedId]);
  229. // $this->_oDb->deleteAutomatorHelpers(['automator_id' => (int)$mixedId]);
  230. // $this->_oDb->deleteAutomatorAssistants(['automator_id' => (int)$mixedId]);
  231. // if(($oCmts = BxDolAI::getInstance()->getAutomatorCmtsObject($mixedId)) !== false)
  232. // $oCmts->onObjectDelete();
  233. }
  234. return $mixedResult;
  235. }
  236. protected function _getFormEdit($sAction, $aAgent = [])
  237. {
  238. $aForm = $this->_getForm($sAction, $aAgent);
  239. $aForm['form_attrs']['action'] .= '&id=' . $aAgent['id'];
  240. unset($aForm['inputs']['type']);
  241. unset($aForm['inputs']['message']);
  242. return $aForm;
  243. }
  244. protected function _getForm($sAction, $aAgent = [])
  245. {
  246. $aForm = array(
  247. 'form_attrs' => array(
  248. 'id' => 'bx_std_agents_automators_' . $sAction,
  249. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=sys_studio_agents_agents&a=' . $sAction,
  250. 'method' => 'post',
  251. ),
  252. 'params' => array (
  253. 'db' => array(
  254. 'table' => 'sys_agents_agents',
  255. 'key' => 'id',
  256. 'submit_name' => 'do_submit',
  257. ),
  258. ),
  259. 'inputs' => array(
  260. 'name' => [
  261. 'type' => 'text',
  262. 'name' => 'name',
  263. 'caption' => _t('_sys_agents_automators_field_name'),
  264. 'info' => _t('_sys_agents_automators_field_name_info'),
  265. 'value' => isset($aAgent['name']) ? $aAgent['name'] : '',
  266. 'required' => '1',
  267. 'checker' => [
  268. 'func' => 'Avail',
  269. 'params' => [],
  270. 'error' => _t('_sys_agents_form_field_err_enter'),
  271. ],
  272. 'db' => [
  273. 'pass' => 'Xss',
  274. ],
  275. ],
  276. 'model_id' => [
  277. 'type' => 'select',
  278. 'name' => 'model_id',
  279. 'caption' => _t('_sys_agents_automators_field_model_id'),
  280. 'info' => _t('_sys_agents_automators_field_model_id_info'),
  281. 'value' => isset($aAgent['model_id']) ? $aAgent['model_id'] : BxDolAI::getDefaultModel(),
  282. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'active' => 1, 'capabilities' => ['chatvlm', 'chatllm']]),
  283. 'required' => '1',
  284. 'checker' => [
  285. 'func' => 'Avail',
  286. 'params' => [],
  287. 'error' => _t('_sys_agents_form_field_err_select'),
  288. ],
  289. 'db' => [
  290. 'pass' => 'Int',
  291. ],
  292. ],
  293. 'profile_id' => [
  294. 'type' => 'select',
  295. 'name' => 'profile_id',
  296. 'caption' => _t('_sys_agents_field_profile_id'),
  297. 'info' => _t('_sys_agents_field_profile_id_inf'),
  298. 'value' => isset($aAgent['profile_id']) ? $aAgent['profile_id'] : 0,
  299. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  300. 'required' => '0',
  301. 'db' => [
  302. 'pass' => 'Int',
  303. ]
  304. ],
  305. 'trigger' => [
  306. 'type' => 'select',
  307. 'name' => 'trigger',
  308. 'caption' => _t('_sys_agents_automators_field_trigger'),
  309. 'info' => _t('_sys_agents_automators_field_trigger_info'),
  310. 'value' => isset($aAgent['trigger']) ? $aAgent['trigger'] : 0,
  311. 'values' => [
  312. 'alert' => 'alert',
  313. 'scheduler' => 'scheduler',
  314. 'webhook' => 'webhook',
  315. 'manual' => 'manual',
  316. 'agent' => 'agent',
  317. 'message' => 'message',
  318. ],
  319. 'attrs' => [
  320. 'onchange' => $this->getPageJsObject() . '.onChangeAgentTrigger(this)',
  321. ],
  322. 'required' => '1',
  323. 'db' => [
  324. 'pass' => 'Xss',
  325. ]
  326. ],
  327. 'alert_sample' => [
  328. 'type' => 'custom',
  329. 'name' => 'alert_sample',
  330. 'caption' => _t('_sys_agents_field_alert_sample'),
  331. 'content' => isset($aAgent['alert_sample']) ? $aAgent['alert_sample'] : _t('_sys_agents_waiting_for_sample_data'),
  332. // 'tr_attrs' => [
  333. // 'style' => $sType != 'alert' ? 'display:none' : ''
  334. // ],
  335. ],
  336. 'alert_unit' => [
  337. 'type' => 'text',
  338. 'name' => 'alert_unit',
  339. 'caption' => _t('_sys_agents_field_alert_unit'),
  340. 'info' => _t('_sys_agents_field_alert_unit_info'),
  341. 'value' => isset($aAgent['alert_unit']) ? $aAgent['alert_unit'] : '',
  342. // 'tr_attrs' => [
  343. // 'style' => $sType != 'alert' ? 'display:none' : ''
  344. // ],
  345. 'db' => [
  346. 'pass' => 'Xss',
  347. ],
  348. ],
  349. 'alert_action' => [
  350. 'type' => 'text',
  351. 'name' => 'alert_action',
  352. 'caption' => _t('_sys_agents_field_alert_action'),
  353. 'value' => isset($aAgent['alert_action']) ? $aAgent['alert_action'] : '',
  354. // 'tr_attrs' => [
  355. // 'style' => $sType != 'alert' ? 'display:none' : ''
  356. // ],
  357. 'db' => [
  358. 'pass' => 'Xss',
  359. ],
  360. ],
  361. 'scheduler_cron' => [
  362. 'type' => 'text',
  363. 'name' => 'scheduler_cron',
  364. 'caption' => _t('_sys_agents_automators_field_scheduler_cron'),
  365. 'info' => _t('_sys_agents_automators_field_scheduler_cron_info'),
  366. 'value' => isset($aAgent['scheduler_cron']) ? $aAgent['scheduler_cron'] : '',
  367. // 'tr_attrs' => [
  368. // 'style' => $sType != 'scheduler' ? 'display:none' : ''
  369. // ],
  370. ],
  371. 'webhook_sample' => [
  372. 'type' => 'custom',
  373. 'name' => 'webhook_sample',
  374. 'caption' => _t('_sys_agents_field_webhook_sample'),
  375. 'content' => isset($aAgent['webhook_sample']) ? $aAgent['webhook_sample'] : _t('_sys_agents_waiting_for_sample_data'),
  376. // 'tr_attrs' => [
  377. // 'style' => $sType != 'webhook' ? 'display:none' : ''
  378. // ],
  379. ],
  380. 'webhook_key' => [
  381. 'type' => 'text',
  382. 'name' => 'webhook_key',
  383. 'caption' => _t('_sys_agents_automators_field_webhook_key'),
  384. 'info' => _t('_sys_agents_automators_field_webhook_key_info'),
  385. 'value' => isset($aAgent['webhook_key']) ? $aAgent['webhook_key'] : '',
  386. // 'tr_attrs' => [
  387. // 'style' => $sType != 'webhook' ? 'display:none' : ''
  388. // ],
  389. ],
  390. 'message_profile_id' => [
  391. 'type' => 'select',
  392. 'name' => 'message_profile_id',
  393. 'caption' => _t('_sys_agents_automators_field_message_profile'),
  394. 'info' => _t('_sys_agents_automators_field_message_profile_info'),
  395. 'value' => isset($aAgent['message_profile_id']) ? $aAgent['message_profile_id'] : '',
  396. // 'tr_attrs' => [
  397. // 'style' => $sType != 'message' ? 'display:none' : ''
  398. // ],
  399. ],
  400. 'vector_store_id' => [
  401. 'type' => 'select',
  402. 'name' => 'vector_store_id',
  403. 'caption' => _t('_sys_agents_automators_field_vector_store_id'),
  404. 'info' => _t('_sys_agents_automators_field_vector_store_id_info'),
  405. 'value' => isset($aAgent['vector_store_id']) ? $aAgent['vector_store_id'] : 0,
  406. 'values' => $this->_oDb->getVectorStores(),
  407. ],
  408. 'tools' => [
  409. 'type' => 'custom',
  410. 'name' => 'tools',
  411. 'caption' => _t('_sys_agents_automators_field_tools'),
  412. 'info' => _t('_sys_agents_automators_field_tools_info'),
  413. 'value' => '',
  414. ],
  415. 'prompt_system' => [
  416. 'type' => 'textarea',
  417. 'name' => 'prompt_system',
  418. 'caption' => _t('_sys_agents_automators_field_prompt_system'),
  419. 'info' => _t('_sys_agents_automators_field_prompt_system_info'),
  420. 'value' => isset($aAgent['prompt_system']) ? $aAgent['prompt_system'] : '',
  421. ],
  422. 'prompt_steps' => [
  423. 'type' => 'textarea',
  424. 'name' => 'prompt_steps',
  425. 'caption' => _t('_sys_agents_automators_field_prompt_steps'),
  426. 'info' => _t('_sys_agents_automators_field_prompt_steps_info'),
  427. 'value' => isset($aAgent['prompt_steps']) ? $aAgent['prompt_steps'] : '',
  428. ],
  429. 'prompt_output' => [
  430. 'type' => 'textarea',
  431. 'name' => 'prompt_output',
  432. 'caption' => _t('_sys_agents_automators_field_prompt_output'),
  433. 'info' => _t('_sys_agents_automators_field_prompt_output_info'),
  434. 'value' => isset($aAgent['prompt_output']) ? $aAgent['prompt_output'] : '',
  435. ],
  436. 'prompt_tools' => [
  437. 'type' => 'textarea',
  438. 'name' => 'prompt_tools',
  439. 'caption' => _t('_sys_agents_automators_field_prompt_tools'),
  440. 'info' => _t('_sys_agents_automators_field_prompt_tools_info'),
  441. 'value' => isset($aAgent['prompt_tools']) ? $aAgent['prompt_tools'] : '',
  442. ],
  443. 'submit' => $this->_getFormControls(),
  444. ),
  445. );
  446. $this->_getMultiField('tools', $aAgent, 'getTools', 'toolAdd', 'agents_agents_form_tools.html', $aForm);
  447. return $aForm;
  448. }
  449. protected function _getUniqName($sName)
  450. {
  451. return uriGenerate($sName, 'sys_agents_automators', 'name', ['lowercase' => false]);
  452. }
  453. protected function _getMultiField($sField, $aAgent, $sGetValuesMethod, $sJsAddMethod, $sTemplateName, &$aForm)
  454. {
  455. if(!isset($aForm['inputs']['tools']))
  456. return;
  457. $sJsObject = $this->getPageJsObject();
  458. $oForm = new BxTemplFormView([]);
  459. $aValues = ['0' => _t('_sys_please_select')];
  460. $a = $this->_oDb->$sGetValuesMethod();
  461. foreach ($a as $k => $v) {
  462. $aValues[$k] = $v;
  463. }
  464. $aTmplVars = [];
  465. if(!empty($aAgent[$sField])) {
  466. $a = explode(',', $aAgent[$sField]);
  467. foreach($a as $v) {
  468. $aInputSelect = [
  469. 'type' => 'select',
  470. 'name' => $sField . '[]',
  471. 'values' => $aValues,
  472. 'value' => $v,
  473. 'attrs' => [
  474. 'class' => 'bx-def-margin-sec-top-auto'
  475. ]
  476. ];
  477. $sInput = $oForm->genInput($aInputSelect);
  478. $aInputHidden = [
  479. 'type' => 'hidden',
  480. 'name' => $sField . '_ids[]',
  481. 'value' => $v,
  482. ];
  483. $sInput .= $oForm->genInput($aInputHidden);
  484. $aTmplVars[] = ['js_object' => $sJsObject, 'input_select' => $sInput];
  485. }
  486. }
  487. else {
  488. $aInputSelect = [
  489. 'type' => 'select',
  490. 'name' => $sField . '[]',
  491. 'values' => $aValues,
  492. 'value' => '',
  493. 'attrs' => [
  494. 'class' => 'bx-def-margin-sec-top-auto'
  495. ]
  496. ];
  497. $aTmplVars = [
  498. ['js_object' => $sJsObject, 'input_select' => $oForm->genInput($aInputSelect)],
  499. ];
  500. }
  501. $aInputButton = [
  502. 'type' => 'button',
  503. 'name' => $sField . '_add',
  504. 'value' => _t('_adm_nav_btn_items_add'),
  505. 'attrs' => [
  506. 'class' => 'bx-def-margin-sec-top',
  507. 'onclick' => $sJsObject . ".$sJsAddMethod(this, '" . $sField . "');"
  508. ]
  509. ];
  510. $aForm['inputs'][$sField]['content'] = $this->_oTemplate->parseHtmlByName($sTemplateName, [
  511. "bx_repeat:$sField" => $aTmplVars,
  512. 'btn_add' => $oForm->genInputButton($aInputButton)
  513. ]);
  514. }
  515. }
  516. /** @} */