BxBaseStudioAgentsAssistants.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 BxBaseStudioAgentsAssistants extends BxDolStudioAgentsAssistants
  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=assistants';
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionAdd()
  25. {
  26. $sAction = 'add';
  27. $oAI = BxDolAI::getInstance();
  28. $aForm = $this->_getForm($sAction);
  29. $oForm = new BxTemplFormView($aForm);
  30. $oForm->initChecker();
  31. if($oForm->isSubmittedAndValid()) {
  32. $aValsToAdd = ['added' => time()];
  33. $sName = $oForm->getCleanValue($this->_sFieldName);
  34. $sName = $this->_getAssistantName($sName);
  35. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  36. $iProfileId = $oForm->getCleanValue('profile_id');
  37. if(empty($iProfileId)) {
  38. $iProfileId = (int)getParam('sys_agents_profile');
  39. if(empty($iProfileId))
  40. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  41. $aValsToAdd['profile_id'] = $iProfileId;
  42. }
  43. $oAIModel = $oAI->getModelObject($oForm->getCleanValue('model_id'));
  44. if(($aAssistant = $oAIModel->getAssistant(['name' => $sName, 'prompt' => $oForm->getCleanValue('prompt')])) !== false)
  45. $aValsToAdd = array_merge($aValsToAdd, [
  46. 'ai_vs_id' => $aAssistant['vector_store_id'],
  47. 'ai_asst_id' => $aAssistant['assistant_id']
  48. ]);
  49. if(($iId = $oForm->insert($aValsToAdd)) !== false)
  50. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  51. else
  52. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  53. return echoJson($aRes);
  54. }
  55. $sFormId = $oForm->getId();
  56. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_assistants_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  57. 'form_id' => $sFormId,
  58. 'form' => $oForm->getCode(true),
  59. 'object' => $this->_sObject,
  60. 'action' => $sAction
  61. ]));
  62. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  63. }
  64. public function performActionEdit()
  65. {
  66. $sAction = 'edit';
  67. $iId = $this->_getId();
  68. $aHelper = $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $iId]);
  69. $aForm = $this->_getFormEdit($sAction, $aHelper);
  70. $oForm = new BxTemplFormView($aForm);
  71. $oForm->initChecker();
  72. if($oForm->isSubmittedAndValid()) {
  73. $aValsToAdd = [];
  74. $sName = $oForm->getCleanValue($this->_sFieldName);
  75. if($aHelper[$this->_sFieldName] != $sName) {
  76. $sName = $this->_getAssistantName($sName);
  77. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  78. }
  79. $iProfileId = $oForm->getCleanValue('profile_id');
  80. if(empty($iProfileId)) {
  81. $iProfileId = (int)getParam('sys_agents_profile');
  82. if(empty($iProfileId))
  83. $iProfileId = current(bx_srv('system', 'get_options_agents_profile', [false], 'TemplServices'))['key'];
  84. $aValsToAdd['profile_id'] = $iProfileId;
  85. }
  86. if($oForm->update($iId, $aValsToAdd) !== false)
  87. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  88. else
  89. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  90. return echoJson($aRes);
  91. }
  92. $sFormId = $oForm->getId();
  93. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_assistants_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  94. 'form_id' => $sFormId,
  95. 'form' => $oForm->getCode(true),
  96. 'object' => $this->_sObject,
  97. 'action' => $sAction
  98. ]));
  99. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  100. }
  101. public function performActionCodes()
  102. {
  103. $sAction = 'codes';
  104. $iId = $this->_getId();
  105. $sContent = BxTemplStudioFunctions::getInstance()->transBox('bx_std_agents_assistants_codes', $this->_oTemplate->parseHtmlByName('agents_assistant_codes.html', [
  106. 'code_onclick' => '{onclick}',
  107. 'code_button' => htmlspecialchars($this->_oTemplate->parseLink('javascript:void(0)', _t('_sys_agents_assistants_txt_add_knowledge'), [
  108. 'class' => 'bx-btn',
  109. 'onclick' => '{onclick}'
  110. ]), ENT_NOQUOTES)
  111. ]));
  112. $sContent = bx_replace_markers($sContent, ['onclick' => "javascript:bx_agents_action(this, 'asst', 'add_knowledge', {id: " . $iId . "})"]);
  113. return echoJson(['popup' => ['html' => $sContent, 'options' => []]]);
  114. }
  115. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  116. {
  117. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  118. if(!empty($aModel) && is_array($aModel))
  119. $mixedValue = $aModel['title'];
  120. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  121. }
  122. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  123. {
  124. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  125. }
  126. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  127. {
  128. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  129. }
  130. protected function _getActionChats($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  131. {
  132. $a['attr'] = array_merge($a['attr'], [
  133. "onclick" => "window.open('" . $this->_sUrlPage . '&spage=chats&aid=' . $aRow['id'] . "', '_self');"
  134. ]);
  135. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  136. }
  137. protected function _getActionFiles($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  138. {
  139. $a['attr'] = array_merge($a['attr'], [
  140. "onclick" => "window.open('" . $this->_sUrlPage . '&spage=files&aid=' . $aRow['id'] . "', '_self');"
  141. ]);
  142. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  143. }
  144. protected function _addJsCss()
  145. {
  146. parent::_addJsCss();
  147. $this->_oTemplate->addJs(['jquery.form.min.js']);
  148. $oForm = new BxTemplStudioFormView([]);
  149. $oForm->addCssJs();
  150. }
  151. protected function _isCheckboxDisabled($aRow)
  152. {
  153. return false;
  154. }
  155. protected function _getActionsDisabledBehavior($aRow)
  156. {
  157. return false;
  158. }
  159. protected function _delete ($mixedId)
  160. {
  161. $iAssistantId = (int)$mixedId;
  162. $aAssistant = $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $iAssistantId]);
  163. $mixedResult = parent::_delete($mixedId);
  164. if($mixedResult) {
  165. $oAi = BxDolAI::getInstance();
  166. $oAIModel = $oAi->getModelObject($aAssistant['model_id']);
  167. $aChats = $this->_oDb->getChatsBy(['sample' => 'assistant_id', 'assistant_id' => $iAssistantId]);
  168. foreach($aChats as $aChat) {
  169. if(($oCmts = $oAi->getAssistantChatCmtsObject($aChat['id'])) !== false)
  170. $oCmts->onObjectDelete();
  171. if(!empty($aChat['ai_file_id'])) {
  172. $oAIModel->callVectorStoresFilesDelete($aAssistant['ai_vs_id'], $aChat['ai_file_id']);
  173. $oAIModel->callFilesDelete($aChat['ai_file_id']);
  174. }
  175. }
  176. $this->_oDb->deleteChats(['assistant_id' => $iAssistantId]);
  177. $this->_oDb->deleteAutomatorAssistants(['assistant_id' => $iAssistantId]);
  178. $oAIModel->callVectorStoresDelete($aAssistant['ai_vs_id']);
  179. $oAIModel->callAssistantsDelete($aAssistant['ai_asst_id']);
  180. }
  181. return $mixedResult;
  182. }
  183. protected function _getFormEdit($sAction, $aHelper = [])
  184. {
  185. $aForm = $this->_getForm($sAction, $aHelper);
  186. $aForm['form_attrs']['action'] .= '&id=' . $aHelper['id'];
  187. return $aForm;
  188. }
  189. protected function _getForm($sAction, $aHelper = [])
  190. {
  191. $sJsObject = $this->getPageJsObject();
  192. $aForm = [
  193. 'form_attrs' => [
  194. 'id' => 'bx_std_agents_assistants_' . $sAction,
  195. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => 'sys_studio_agents_assistants', 'a' => $sAction]),
  196. 'method' => 'post',
  197. ],
  198. 'params' => [
  199. 'db' => [
  200. 'table' => 'sys_agents_assistants',
  201. 'key' => 'id',
  202. 'submit_name' => 'do_submit',
  203. ],
  204. ],
  205. 'inputs' => [
  206. 'name' => [
  207. 'type' => 'text',
  208. 'name' => 'name',
  209. 'required' => '1',
  210. 'caption' => _t('_sys_agents_assistants_field_name'),
  211. 'value' => isset($aHelper['name']) ? $aHelper['name'] : '',
  212. 'required' => '1',
  213. 'checker' => [
  214. 'func' => 'Avail',
  215. 'params' => [],
  216. 'error' => _t('_sys_agents_form_field_err_enter'),
  217. ],
  218. 'db' => [
  219. 'pass' => 'Xss',
  220. ]
  221. ],
  222. 'model_id' => [
  223. 'type' => 'select',
  224. 'name' => 'model_id',
  225. 'caption' => _t('_sys_agents_assistants_field_model_id'),
  226. 'info' => '',
  227. 'value' => isset($aHelper['model_id']) ? $aHelper['model_id'] : BxDolAI::getInstance()->getDefaultModel(),
  228. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'for_asst' => 1]),
  229. 'required' => '1',
  230. 'db' => [
  231. 'pass' => 'Int',
  232. ]
  233. ],
  234. 'profile_id' => [
  235. 'type' => 'select',
  236. 'name' => 'profile_id',
  237. 'caption' => _t('_sys_agents_assistants_field_profile_id'),
  238. 'info' => _t('_sys_agents_assistants_field_profile_id_inf'),
  239. 'value' => isset($aHelper['profile_id']) ? $aHelper['profile_id'] : 0,
  240. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  241. 'required' => '0',
  242. 'db' => [
  243. 'pass' => 'Int',
  244. ]
  245. ],
  246. 'description' => [
  247. 'type' => 'textarea',
  248. 'name' => 'description',
  249. 'caption' => _t('_sys_agents_assistants_field_description'),
  250. 'value' => isset($aHelper['description']) ? $aHelper['description'] : '',
  251. 'required' => '1',
  252. 'checker' => [
  253. 'func' => 'Avail',
  254. 'params' => [],
  255. 'error' => _t('_sys_agents_form_field_err_enter'),
  256. ],
  257. 'db' => [
  258. 'pass' => 'Xss',
  259. ]
  260. ],
  261. 'prompt' => [
  262. 'type' => 'textarea',
  263. 'name' => 'prompt',
  264. 'caption' => _t('_sys_agents_assistants_field_prompt'),
  265. 'value' => isset($aHelper['prompt']) ? $aHelper['prompt'] : '',
  266. 'required' => '1',
  267. 'checker' => [
  268. 'func' => 'Avail',
  269. 'params' => [],
  270. 'error' => _t('_sys_agents_assistants_field_prompt_err'),
  271. ],
  272. 'db' => [
  273. 'pass' => 'Xss',
  274. ]
  275. ],
  276. 'submit' => [
  277. 'type' => 'input_set',
  278. 0 => [
  279. 'type' => 'submit',
  280. 'name' => 'do_submit',
  281. 'value' => _t('_sys_submit'),
  282. ],
  283. 1 => [
  284. 'type' => 'reset',
  285. 'name' => 'close',
  286. 'value' => _t('_sys_close'),
  287. 'attrs' => ['class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'],
  288. ],
  289. ],
  290. ],
  291. ];
  292. return $aForm;
  293. }
  294. protected function _getId()
  295. {
  296. $aIds = bx_get('ids');
  297. if(!empty($aIds) && is_array($aIds))
  298. return array_shift($aIds);
  299. $iId = (int)bx_get('id');
  300. if(!$iId)
  301. return false;
  302. return $iId;
  303. }
  304. protected function _getAssistantName($sName)
  305. {
  306. return uriGenerate($sName, 'sys_agents_assistants', 'name', ['lowercase' => false]);
  307. }
  308. }
  309. /** @} */