BxBaseStudioAgentsAsstFiles.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 BxBaseStudioAgentsAsstFiles extends BxDolStudioAgentsAsstFiles
  11. {
  12. protected $_sUrlPage;
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct ($aOptions, $oTemplate);
  16. $this->_sUrlPage = BX_DOL_URL_STUDIO . 'agents.php?page=assistants&spage=files&aid=' . $this->_iAssistantId;
  17. }
  18. public function getPageJsObject()
  19. {
  20. return 'oBxDolStudioPageAgents';
  21. }
  22. public function performActionAdd()
  23. {
  24. $sAction = 'add';
  25. $aForm = $this->_getForm($sAction);
  26. $oForm = new BxTemplFormView($aForm);
  27. $oForm->initChecker();
  28. if($oForm->isSubmittedAndValid()) {
  29. $aValsToAdd = ['assistant_id' => $this->_iAssistantId, 'added' => time()];
  30. if(!empty($_FILES['file']['tmp_name'])) {
  31. $sName = $_FILES['file']['name'];
  32. $sMime = $_FILES['file']['type'];
  33. $iSize = $_FILES['file']['size'];
  34. $oHandle = fopen($_FILES['file']['tmp_name'], "r");
  35. $sContents = fread($oHandle, $iSize);
  36. fclose($oHandle);
  37. $oAIModel = BxDolAI::getInstance()->getModelObject($this->_aAssistantInfo['model_id']);
  38. if(($aFile = $oAIModel->callFiles(['content' => $sContents, 'name' => $sName, 'mime' => $sMime])) !== false) {
  39. if(($aResponse = $oAIModel->callVectorStoresFiles($this->_aAssistantInfo['ai_vs_id'], ['file_id' => $aFile['id']])) !== false) {
  40. $aValsToAdd = array_merge($aValsToAdd, [
  41. 'name' => $sName,
  42. 'ai_file_id' => $aFile['id'],
  43. 'ai_file_size' => $iSize
  44. ]);
  45. }
  46. }
  47. }
  48. if(($iId = $oForm->insert($aValsToAdd)) !== false)
  49. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  50. else
  51. $aRes = ['msg' => _t('_sys_txt_error_occured')];
  52. return echoJson($aRes);
  53. }
  54. $sFormId = $oForm->getId();
  55. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($sFormId . '_popup', _t('_sys_agents_assistants_files_popup_add'), $this->_oTemplate->parseHtmlByName('agents_automator_form.html', [
  56. 'form_id' => $sFormId,
  57. 'form' => $oForm->getCode(true),
  58. 'object' => $this->_sObject,
  59. 'action' => $sAction
  60. ]));
  61. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  62. }
  63. public function performActionSync()
  64. {
  65. $sAction = 'sync';
  66. $oAIModel = BxDolAI::getInstance()->getModelObject($this->_aAssistantInfo['model_id']);
  67. if(($aFiles = $oAIModel->callVectorStoresFilesList($this->_aAssistantInfo['ai_vs_id'])) === false)
  68. return echoJson([]);
  69. foreach($aFiles as $aFile) {
  70. $aFileInfo = $oAIModel->callFilesRetrieve($aFile['id']);
  71. $this->_oDb->updateFiles([
  72. 'ai_file_size' => is_array($aFileInfo) && !empty($aFileInfo['bytes']) ? $aFileInfo['bytes'] : 0,
  73. 'ai_file_status' => $aFile['status']
  74. ], ['ai_file_id' => $aFile['id']]);
  75. }
  76. return echoJson(['grid' => $this->getCode(false)]);
  77. }
  78. public function performActionDelete()
  79. {
  80. $sAction = 'delete';
  81. $iId = $this->_getId();
  82. $aFile = $this->_oDb->getFilesBy(['sample' => 'id', 'id' => $iId]);
  83. if(empty($aFile) || !is_array($aFile))
  84. return echoJson([]);
  85. return parent::performActionDelete();
  86. }
  87. protected function _getCellSize($mixedValue, $sKey, $aField, $aRow)
  88. {
  89. return parent::_getCellDefault(!empty($aRow['ai_file_size']) ? _t_format_size($aRow['ai_file_size']) : _t('_undefined'), $sKey, $aField, $aRow);
  90. }
  91. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  92. {
  93. return parent::_getCellDefault(_t('_sys_agents_assistants_files_txt_status_' . $aRow['ai_file_status']), $sKey, $aField, $aRow);
  94. }
  95. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  96. {
  97. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  98. }
  99. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  100. {
  101. if((int)$aRow['locked'] != 0)
  102. return '';
  103. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  104. }
  105. protected function _addJsCss()
  106. {
  107. parent::_addJsCss();
  108. $this->_oTemplate->addJs(['jquery.form.min.js']);
  109. $oForm = new BxTemplStudioFormView([]);
  110. $oForm->addCssJs();
  111. }
  112. protected function _isCheckboxDisabled($aRow)
  113. {
  114. return false;
  115. }
  116. protected function _getActionsDisabledBehavior($aRow)
  117. {
  118. return false;
  119. }
  120. protected function _delete ($mixedId)
  121. {
  122. $iFileId = (int)$mixedId;
  123. $aFile = $this->_oDb->getFilesBy(['sample' => 'id', 'id' => $iFileId]);
  124. $mixedResult = parent::_delete($mixedId);
  125. if($mixedResult && !empty($aFile['ai_file_id'])) {
  126. $oAi = BxDolAI::getInstance();
  127. $oAIModel = $oAi->getModelObject($this->_aAssistantInfo['model_id']);
  128. $oAIModel->callVectorStoresFilesDelete($this->_aAssistantInfo['ai_vs_id'], $aFile['ai_file_id']);
  129. $oAIModel->callFilesDelete($aFile['ai_file_id']);
  130. }
  131. return $mixedResult;
  132. }
  133. protected function _getForm($sAction, $aFiles = [])
  134. {
  135. $sJsObject = $this->getPageJsObject();
  136. $aForm = array(
  137. 'form_attrs' => array(
  138. 'id' => 'bx_std_agents_assistants_files_' . $sAction,
  139. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => 'sys_studio_agents_assistants_files', 'a' => $sAction, 'aid' => $this->_iAssistantId]),
  140. 'method' => 'post',
  141. ),
  142. 'params' => array (
  143. 'db' => array(
  144. 'table' => 'sys_agents_assistants_files',
  145. 'key' => 'id',
  146. 'submit_name' => 'do_submit',
  147. ),
  148. ),
  149. 'inputs' => array(
  150. 'file' => [
  151. 'type' => 'file',
  152. 'name' => 'file',
  153. 'required' => '1',
  154. 'caption' => _t('_sys_agents_assistants_files_field_file'),
  155. 'value' => '',
  156. 'checker' => [
  157. 'func' => 'Avail',
  158. 'params' => '',
  159. 'error' => _t('_sys_agents_form_field_err_enter'),
  160. ]
  161. ],
  162. 'submit' => array(
  163. 'type' => 'input_set',
  164. 0 => array (
  165. 'type' => 'submit',
  166. 'name' => 'do_submit',
  167. 'value' => _t('_sys_submit'),
  168. ),
  169. 1 => array (
  170. 'type' => 'reset',
  171. 'name' => 'close',
  172. 'value' => _t('_sys_close'),
  173. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  174. ),
  175. ),
  176. ),
  177. );
  178. return $aForm;
  179. }
  180. protected function _getId()
  181. {
  182. $aIds = bx_get('ids');
  183. if(!empty($aIds) && is_array($aIds))
  184. return array_shift($aIds);
  185. $iId = (int)bx_get('id');
  186. if(!$iId)
  187. return false;
  188. return $iId;
  189. }
  190. }
  191. /** @} */