1
0

BxBaseStudioAgentsAssistants.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. protected function _getCellModelId($mixedValue, $sKey, $aField, $aRow)
  102. {
  103. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $mixedValue]);
  104. if(!empty($aModel) && is_array($aModel))
  105. $mixedValue = $aModel['title'];
  106. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  107. }
  108. protected function _getCellProfileId($mixedValue, $sKey, $aField, $aRow)
  109. {
  110. return parent::_getCellDefault(BxDolProfile::getInstanceMagic($mixedValue)->getDisplayName(), $sKey, $aField, $aRow);
  111. }
  112. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  113. {
  114. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  115. }
  116. protected function _getActionChats($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  117. {
  118. $a['attr'] = array_merge($a['attr'], [
  119. "onclick" => "window.open('" . $this->_sUrlPage . '&spage=chats&aid=' . $aRow['id'] . "', '_self');"
  120. ]);
  121. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  122. }
  123. protected function _getActionFiles($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  124. {
  125. $a['attr'] = array_merge($a['attr'], [
  126. "onclick" => "window.open('" . $this->_sUrlPage . '&spage=files&aid=' . $aRow['id'] . "', '_self');"
  127. ]);
  128. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  129. }
  130. protected function _addJsCss()
  131. {
  132. parent::_addJsCss();
  133. $this->_oTemplate->addJs(['jquery.form.min.js']);
  134. $oForm = new BxTemplStudioFormView([]);
  135. $oForm->addCssJs();
  136. }
  137. protected function _isCheckboxDisabled($aRow)
  138. {
  139. return false;
  140. }
  141. protected function _getActionsDisabledBehavior($aRow)
  142. {
  143. return false;
  144. }
  145. protected function _delete ($mixedId)
  146. {
  147. $iAssistantId = (int)$mixedId;
  148. $aAssistant = $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $iAssistantId]);
  149. $mixedResult = parent::_delete($mixedId);
  150. if($mixedResult) {
  151. $oAi = BxDolAI::getInstance();
  152. $oAIModel = $oAi->getModelObject($aAssistant['model_id']);
  153. $aChats = $this->_oDb->getChatsBy(['sample' => 'assistant_id', 'assistant_id' => $iAssistantId]);
  154. foreach($aChats as $aChat) {
  155. if(($oCmts = $oAi->getAssistantChatCmtsObject($aChat['id'])) !== false)
  156. $oCmts->onObjectDelete();
  157. if(!empty($aChat['ai_file_id'])) {
  158. $oAIModel->callVectorStoresFilesDelete($aAssistant['ai_vs_id'], $aChat['ai_file_id']);
  159. $oAIModel->callFilesDelete($aChat['ai_file_id']);
  160. }
  161. }
  162. $this->_oDb->deleteChats(['assistant_id' => $iAssistantId]);
  163. $this->_oDb->deleteAutomatorAssistants(['assistant_id' => $iAssistantId]);
  164. $oAIModel->callVectorStoresDelete($aAssistant['ai_vs_id']);
  165. $oAIModel->callAssistantsDelete($aAssistant['ai_asst_id']);
  166. }
  167. return $mixedResult;
  168. }
  169. protected function _getFormEdit($sAction, $aHelper = [])
  170. {
  171. $aForm = $this->_getForm($sAction, $aHelper);
  172. $aForm['form_attrs']['action'] .= '&id=' . $aHelper['id'];
  173. return $aForm;
  174. }
  175. protected function _getForm($sAction, $aHelper = [])
  176. {
  177. $sJsObject = $this->getPageJsObject();
  178. $aForm = [
  179. 'form_attrs' => [
  180. 'id' => 'bx_std_agents_assistants_' . $sAction,
  181. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => 'sys_studio_agents_assistants', 'a' => $sAction]),
  182. 'method' => 'post',
  183. ],
  184. 'params' => [
  185. 'db' => [
  186. 'table' => 'sys_agents_assistants',
  187. 'key' => 'id',
  188. 'submit_name' => 'do_submit',
  189. ],
  190. ],
  191. 'inputs' => [
  192. 'name' => [
  193. 'type' => 'text',
  194. 'name' => 'name',
  195. 'required' => '1',
  196. 'caption' => _t('_sys_agents_assistants_field_name'),
  197. 'value' => isset($aHelper['name']) ? $aHelper['name'] : '',
  198. 'required' => '1',
  199. 'checker' => [
  200. 'func' => 'Avail',
  201. 'params' => [],
  202. 'error' => _t('_sys_agents_form_field_err_enter'),
  203. ],
  204. 'db' => [
  205. 'pass' => 'Xss',
  206. ]
  207. ],
  208. 'model_id' => [
  209. 'type' => 'select',
  210. 'name' => 'model_id',
  211. 'caption' => _t('_sys_agents_assistants_field_model_id'),
  212. 'info' => '',
  213. 'value' => isset($aHelper['model_id']) ? $aHelper['model_id'] : BxDolAI::getInstance()->getDefaultModel(),
  214. 'values' => $this->_oDb->getModelsBy(['sample' => 'all_pairs', 'for_asst' => 1]),
  215. 'required' => '1',
  216. 'db' => [
  217. 'pass' => 'Int',
  218. ]
  219. ],
  220. 'profile_id' => [
  221. 'type' => 'select',
  222. 'name' => 'profile_id',
  223. 'caption' => _t('_sys_agents_assistants_field_profile_id'),
  224. 'info' => _t('_sys_agents_assistants_field_profile_id_inf'),
  225. 'value' => isset($aHelper['profile_id']) ? $aHelper['profile_id'] : 0,
  226. 'values' => bx_srv('system', 'get_options_agents_profile', [], 'TemplServices'),
  227. 'required' => '0',
  228. 'db' => [
  229. 'pass' => 'Int',
  230. ]
  231. ],
  232. 'description' => [
  233. 'type' => 'textarea',
  234. 'name' => 'description',
  235. 'caption' => _t('_sys_agents_assistants_field_description'),
  236. 'value' => isset($aHelper['description']) ? $aHelper['description'] : '',
  237. 'required' => '1',
  238. 'checker' => [
  239. 'func' => 'Avail',
  240. 'params' => [],
  241. 'error' => _t('_sys_agents_form_field_err_enter'),
  242. ],
  243. 'db' => [
  244. 'pass' => 'Xss',
  245. ]
  246. ],
  247. 'prompt' => [
  248. 'type' => 'textarea',
  249. 'name' => 'prompt',
  250. 'caption' => _t('_sys_agents_assistants_field_prompt'),
  251. 'value' => isset($aHelper['prompt']) ? $aHelper['prompt'] : '',
  252. 'required' => '1',
  253. 'checker' => [
  254. 'func' => 'Avail',
  255. 'params' => [],
  256. 'error' => _t('_sys_agents_assistants_field_prompt_err'),
  257. ],
  258. 'db' => [
  259. 'pass' => 'Xss',
  260. ]
  261. ],
  262. 'submit' => [
  263. 'type' => 'input_set',
  264. 0 => [
  265. 'type' => 'submit',
  266. 'name' => 'do_submit',
  267. 'value' => _t('_sys_submit'),
  268. ],
  269. 1 => [
  270. 'type' => 'reset',
  271. 'name' => 'close',
  272. 'value' => _t('_sys_close'),
  273. 'attrs' => ['class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'],
  274. ],
  275. ],
  276. ],
  277. ];
  278. return $aForm;
  279. }
  280. protected function _getId()
  281. {
  282. $aIds = bx_get('ids');
  283. if(!empty($aIds) && is_array($aIds))
  284. return array_shift($aIds);
  285. $iId = (int)bx_get('id');
  286. if(!$iId)
  287. return false;
  288. return $iId;
  289. }
  290. protected function _getAssistantName($sName)
  291. {
  292. return uriGenerate($sName, 'sys_agents_assistants', 'name', ['lowercase' => false]);
  293. }
  294. }
  295. /** @} */