BxBaseStudioAgentsAsstFiles.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  96. {
  97. if((int)$aRow['locked'] != 0)
  98. return '';
  99. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  100. }
  101. protected function _delete ($mixedId)
  102. {
  103. $iFileId = (int)$mixedId;
  104. $aFile = $this->_oDb->getFilesBy(['sample' => 'id', 'id' => $iFileId]);
  105. $mixedResult = parent::_delete($mixedId);
  106. if($mixedResult && !empty($aFile['ai_file_id'])) {
  107. $oAi = BxDolAI::getInstance();
  108. $oAIModel = $oAi->getModelObject($this->_aAssistantInfo['model_id']);
  109. $oAIModel->callVectorStoresFilesDelete($this->_aAssistantInfo['ai_vs_id'], $aFile['ai_file_id']);
  110. $oAIModel->callFilesDelete($aFile['ai_file_id']);
  111. }
  112. return $mixedResult;
  113. }
  114. protected function _getForm($sAction, $aFiles = [])
  115. {
  116. $sJsObject = $this->getPageJsObject();
  117. $aForm = array(
  118. 'form_attrs' => array(
  119. 'id' => 'bx_std_agents_assistants_files_' . $sAction,
  120. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => 'sys_studio_agents_assistants_files', 'a' => $sAction, 'aid' => $this->_iAssistantId]),
  121. 'method' => 'post',
  122. ),
  123. 'params' => array (
  124. 'db' => array(
  125. 'table' => 'sys_agents_assistants_files',
  126. 'key' => 'id',
  127. 'submit_name' => 'do_submit',
  128. ),
  129. ),
  130. 'inputs' => array(
  131. 'file' => [
  132. 'type' => 'file',
  133. 'name' => 'file',
  134. 'required' => '1',
  135. 'caption' => _t('_sys_agents_assistants_files_field_file'),
  136. 'value' => '',
  137. 'checker' => [
  138. 'func' => 'Avail',
  139. 'params' => '',
  140. 'error' => _t('_sys_agents_form_field_err_enter'),
  141. ]
  142. ],
  143. 'submit' => $this->_getFormControls(),
  144. ),
  145. );
  146. return $aForm;
  147. }
  148. }
  149. /** @} */