BxBaseStudioAgentsAsstChats.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 BxBaseStudioAgentsAsstChats extends BxDolStudioAgentsAsstChats
  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&spage=chats&aid=' . $this->_iAssistantId;
  18. $this->_sFieldName = 'name';
  19. }
  20. public function getPageJsObject()
  21. {
  22. return 'oBxDolStudioPageAgents';
  23. }
  24. public function performActionAdd()
  25. {
  26. $sAction = 'add';
  27. $aForm = $this->_getForm($sAction);
  28. $oForm = new BxTemplFormView($aForm);
  29. $oForm->initChecker();
  30. if($oForm->isSubmittedAndValid()) {
  31. $aValsToAdd = ['assistant_id' => $this->_iAssistantId, 'added' => time()];
  32. $sName = $oForm->getCleanValue($this->_sFieldName);
  33. $sName = BxDolAIAssistant::getChatName($sName);
  34. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  35. if(($iId = $oForm->insert($aValsToAdd)) !== false)
  36. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  37. else
  38. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  39. return echoJson($aRes);
  40. }
  41. $sFormId = $oForm->getId();
  42. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_assistants_chats_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  43. 'form_id' => $sFormId,
  44. 'form' => $oForm->getCode(true),
  45. 'object' => $this->_sObject,
  46. 'action' => $sAction
  47. ]));
  48. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  49. }
  50. public function performActionEdit()
  51. {
  52. $sAction = 'edit';
  53. $iId = $this->_getId();
  54. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $iId]);
  55. $aForm = $this->_getFormEdit($sAction, $aChat);
  56. $oForm = new BxTemplFormView($aForm);
  57. $oForm->initChecker();
  58. if($oForm->isSubmittedAndValid()) {
  59. $aValsToAdd = [];
  60. $sName = $oForm->getCleanValue($this->_sFieldName);
  61. if($aChat[$this->_sFieldName] != $sName) {
  62. $sName = BxDolAIAssistant::getChatName($sName);
  63. BxDolForm::setSubmittedValue($this->_sFieldName, $sName, $oForm->aFormAttrs['method']);
  64. }
  65. if($oForm->update($iId, $aValsToAdd) !== false)
  66. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  67. else
  68. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  69. return echoJson($aRes);
  70. }
  71. $sFormId = $oForm->getId();
  72. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_assistants_chats_popup_edit'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  73. 'form_id' => $sFormId,
  74. 'form' => $oForm->getCode(true),
  75. 'object' => $this->_sObject,
  76. 'action' => $sAction
  77. ]));
  78. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  79. }
  80. public function performActionStore()
  81. {
  82. $sAction = 'store';
  83. $oAI = BxDolAI::getInstance();
  84. $iId = $this->_getId();
  85. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $iId]);
  86. $aAssistant = $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $aChat['assistant_id']]);
  87. $aComments = $oAI->getAssistantChatCmtsObject($iId)->getCommentsBy(['type' => 'object_id', 'object_id' => $iId]);
  88. $aMessages = [];
  89. foreach($aComments as $aComment)
  90. $aMessages[] = [
  91. 'role' => (int)$aComment['cmt_author_id'] == $this->_iProfileIdAi ? "assistant" : "user",
  92. 'content' => $aComment['cmt_text']
  93. ];
  94. $oAIModel = $oAI->getModelObject($aAssistant['model_id']);
  95. if(!empty($aChat['ai_file_id'])) {
  96. $aResponse = $oAIModel->callVectorStoresFilesDelete($aAssistant['ai_vs_id'], $aChat['ai_file_id']);
  97. if(!$aResponse)
  98. return echoJson([]);
  99. $aResponse = $oAIModel->callFilesDelete($aChat['ai_file_id']);
  100. if(!$aResponse)
  101. return echoJson([]);
  102. $this->_oDb->updateChats(['ai_file_id' => '', 'stored' => 0], ['id' => $iId]);
  103. }
  104. $aRes = [];
  105. if(($sFile = $aChat['name'] . '.json') && ($aFile = $oAIModel->callFiles(['content' => json_encode($aMessages), 'name' => $sFile, 'mime' => 'application/json'])) !== false) {
  106. if(($aResponse = $oAIModel->callVectorStoresFiles($aAssistant['ai_vs_id'], ['file_id' => $aFile['id']])) !== false) {
  107. $iNow = time();
  108. $this->_oDb->insertFile(['name' => $sFile, 'assistant_id' => $aAssistant['id'], 'added' => $iNow, 'ai_file_id' => $aFile['id'], 'locked' => 1]);
  109. $this->_oDb->updateChats(['ai_file_id' => $aFile['id'], 'stored' => $iNow], ['id' => $iId]);
  110. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  111. }
  112. }
  113. return echoJson($aRes);
  114. }
  115. public function performActionUnstore()
  116. {
  117. $sAction = 'unstore';
  118. $oAI = BxDolAI::getInstance();
  119. $iId = $this->_getId();
  120. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $iId]);
  121. if(empty($aChat['ai_file_id']))
  122. return echoJson([]);
  123. $aAssistant = $this->_oDb->getAssistantsBy(['sample' => 'id', 'id' => $aChat['assistant_id']]);
  124. $oAIModel = $oAI->getModelObject($aAssistant['model_id']);
  125. if(!$oAIModel->callVectorStoresFilesDelete($aAssistant['ai_vs_id'], $aChat['ai_file_id']))
  126. return echoJson([]);
  127. if(!$oAIModel->callFilesDelete($aChat['ai_file_id']))
  128. return echoJson([]);
  129. $this->_oDb->deleteFiles(['ai_file_id' => $aChat['ai_file_id']]);
  130. $this->_oDb->updateChats(['ai_file_id' => '', 'stored' => 0], ['id' => $iId]);
  131. echoJson(['grid' => $this->getCode(false), 'blink' => $iId]);
  132. }
  133. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  134. {
  135. return parent::_getCellDefault(_t('_sys_agents_assistants_chats_txt_type_' . $mixedValue), $sKey, $aField, $aRow);
  136. }
  137. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  138. {
  139. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  140. }
  141. protected function _getCellStored($mixedValue, $sKey, $aField, $aRow)
  142. {
  143. return parent::_getCellDefault($mixedValue ? bx_time_js($mixedValue) : '', $sKey, $aField, $aRow);
  144. }
  145. protected function _getActionChat($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  146. {
  147. $a['attr'] = array_merge($a['attr'], [
  148. "onclick" => "window.open('" . $this->_sUrlPage . "&cid=" . $aRow['id'] . "', '_self');"
  149. ]);
  150. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  151. }
  152. protected function _getActionStore($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  153. {
  154. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $aRow['id']]);
  155. if(!empty($aChat['ai_file_id']))
  156. return '';
  157. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  158. }
  159. protected function _getActionUnstore($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  160. {
  161. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => $aRow['id']]);
  162. if(empty($aChat['ai_file_id']))
  163. return '';
  164. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  165. }
  166. protected function _addJsCss()
  167. {
  168. parent::_addJsCss();
  169. $this->_oTemplate->addJs(['jquery.form.min.js']);
  170. $oForm = new BxTemplStudioFormView([]);
  171. $oForm->addCssJs();
  172. }
  173. protected function _isCheckboxDisabled($aRow)
  174. {
  175. return false;
  176. }
  177. protected function _getActionsDisabledBehavior($aRow)
  178. {
  179. return false;
  180. }
  181. protected function _delete ($mixedId)
  182. {
  183. $aChat = $this->_oDb->getChatsBy(['sample' => 'id', 'id' => (int)$mixedId]);
  184. $mixedResult = parent::_delete($mixedId);
  185. if($mixedResult)
  186. BxDolAIAssistant::getObjectInstance($aChat['assistant_id'])->deleteChat($aChat);
  187. return $mixedResult;
  188. }
  189. protected function _getFormEdit($sAction, $aChat = [])
  190. {
  191. $aForm = $this->_getForm($sAction, $aChat);
  192. $aForm['form_attrs']['action'] .= '&id=' . $aChat['id'];
  193. return $aForm;
  194. }
  195. protected function _getForm($sAction, $aChat = [])
  196. {
  197. $sJsObject = $this->getPageJsObject();
  198. $aForm = array(
  199. 'form_attrs' => array(
  200. 'id' => 'bx_std_agents_assistants_' . $sAction,
  201. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => 'sys_studio_agents_assistants_chats', 'a' => $sAction, 'aid' => $this->_iAssistantId]),
  202. 'method' => 'post',
  203. ),
  204. 'params' => array (
  205. 'db' => array(
  206. 'table' => 'sys_agents_assistants_chats',
  207. 'key' => 'id',
  208. 'submit_name' => 'do_submit',
  209. ),
  210. ),
  211. 'inputs' => array(
  212. 'name' => [
  213. 'type' => 'text',
  214. 'name' => 'name',
  215. 'required' => '1',
  216. 'caption' => _t('_sys_agents_assistants_chats_field_name'),
  217. 'value' => isset($aChat['name']) ? $aChat['name'] : '',
  218. 'required' => '1',
  219. 'checker' => [
  220. 'func' => 'Avail',
  221. 'params' => [],
  222. 'error' => _t('_sys_agents_form_field_err_enter'),
  223. ],
  224. 'db' => [
  225. 'pass' => 'Xss',
  226. ]
  227. ],
  228. 'description' => [
  229. 'type' => 'textarea',
  230. 'name' => 'description',
  231. 'caption' => _t('_sys_agents_assistants_chats_field_description'),
  232. 'value' => isset($aChat['description']) ? $aChat['description'] : '',
  233. 'required' => '1',
  234. 'checker' => [
  235. 'func' => 'Avail',
  236. 'params' => [],
  237. 'error' => _t('_sys_agents_form_field_err_enter'),
  238. ],
  239. 'db' => [
  240. 'pass' => 'Xss',
  241. ]
  242. ],
  243. 'submit' => array(
  244. 'type' => 'input_set',
  245. 0 => array (
  246. 'type' => 'submit',
  247. 'name' => 'do_submit',
  248. 'value' => _t('_sys_submit'),
  249. ),
  250. 1 => array (
  251. 'type' => 'reset',
  252. 'name' => 'close',
  253. 'value' => _t('_sys_close'),
  254. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  255. ),
  256. ),
  257. ),
  258. );
  259. return $aForm;
  260. }
  261. protected function _getId()
  262. {
  263. $aIds = bx_get('ids');
  264. if(!empty($aIds) && is_array($aIds))
  265. return array_shift($aIds);
  266. $iId = (int)bx_get('id');
  267. if(!$iId)
  268. return false;
  269. return $iId;
  270. }
  271. }
  272. /** @} */