BxBaseStudioAgentsAsstChats.php 11 KB

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