BxDolStudioAgentsAutomatorsCmts.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioAgentsAutomatorsCmts extends BxTemplCmts
  10. {
  11. protected $_oQueryAgents;
  12. protected $_sUrlPageAgents;
  13. protected $_iProfileIdAi;
  14. protected $_bAuto;
  15. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  16. {
  17. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  18. $this->_sTmplNameItemContent = 'agents_comment_content.html';
  19. $this->_bLiveUpdates = false;
  20. $this->_oQueryAgents = new BxDolStudioAgentsQuery();
  21. $this->_sUrlPageAgents = BX_DOL_URL_STUDIO . 'agents.php?page=automators';
  22. $this->_iProfileIdAi = BxDolAI::getInstance()->getProfileId();
  23. $this->_bAuto = false;
  24. }
  25. public function actionGetCmt ()
  26. {
  27. if(!$this->isEnabled())
  28. return echoJson([]);
  29. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  30. return echoJson([]);
  31. $mixedCmtId = bx_process_input(bx_get('Cmt'));
  32. $sCmtBrowse = ($sCmtBrowse = bx_get('CmtBrowse')) !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  33. $sCmtDisplay = ($sCmtDisplay = bx_get('CmtDisplay')) !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  34. $aCmtIds = strpos($mixedCmtId, ',') !== false ? explode(',', $mixedCmtId) : [$mixedCmtId];
  35. $sContent = '';
  36. foreach($aCmtIds as $iCmtId)
  37. $sContent .= $this->getComment((int)$iCmtId, ['type' => $sCmtBrowse], ['type' => $sCmtDisplay, 'dynamic_mode' => true]);
  38. $aCmt = $this->getCommentRow((int)reset($aCmtIds));
  39. echoJson([
  40. 'parent_id' => $aCmt['cmt_parent_id'],
  41. 'vparent_id' => $aCmt['cmt_parent_id'],
  42. 'content' => $sContent
  43. ]);
  44. }
  45. public function actionApproveCode()
  46. {
  47. if(!$this->isEnabled())
  48. return echoJson([]);
  49. $iCmt = bx_process_input(bx_get('Cmt'), BX_DATA_INT);
  50. $aCmt = $this->getCommentRow($iCmt);
  51. if(empty($aCmt) || !is_array($aCmt))
  52. return echoJson([]);
  53. if(!$this->_oQueryAgents->updateAutomators(['code' => $aCmt['cmt_text'], 'status' => 'ready'], ['id' => (int)$this->getId()]))
  54. return echoJson([]);
  55. return echoJson(['redirect' => $this->_sUrlPageAgents]);
  56. }
  57. public function getPageJsObject()
  58. {
  59. return 'oBxDolStudioPageAgents';
  60. }
  61. public function getCommentsBlock($aBp = [], $aDp = [])
  62. {
  63. $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
  64. if(empty($aComments['content']))
  65. return MsgBox(_t('_error occured'));
  66. $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
  67. if(empty($aAutomator) || !is_array($aAutomator))
  68. return MsgBox(_t('_error occured'));
  69. $aTmplVarsEvent = [];
  70. $bTmplVarsEvent = $aAutomator['type'] == 'event';
  71. if($bTmplVarsEvent)
  72. $aTmplVarsEvent = [
  73. 'unit' => $aAutomator['alert_unit'],
  74. 'action' => $aAutomator['alert_action']
  75. ];
  76. $aTmplVarsScheduler = [];
  77. $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
  78. if($bTmplVarsScheduler) {
  79. $aParams = [];
  80. if(!empty($aAutomator['params']))
  81. $aParams = json_decode($aAutomator['params'], true);
  82. $aTmplVarsScheduler = [
  83. 'time' => $aParams['scheduler_time'],
  84. ];
  85. }
  86. $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
  87. 'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
  88. 'bx_if:show_event' => [
  89. 'condition' => $bTmplVarsEvent,
  90. 'content' => $aTmplVarsEvent
  91. ],
  92. 'bx_if:show_scheduler' => [
  93. 'condition' => $bTmplVarsScheduler,
  94. 'content' => $aTmplVarsScheduler
  95. ],
  96. 'code' => $aAutomator['code']
  97. ]);
  98. return [
  99. $sAutomator,
  100. $aComments['content']
  101. ];
  102. }
  103. public function getFormBoxPost($aBp = [], $aDp = [])
  104. {
  105. $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'start' => 0, 'per_page' => 1]);
  106. if(!empty($aComments) && is_array($aComments)) {
  107. $aLast = current($aComments);
  108. if($aLast['cmt_author_id'] != $this->_iProfileIdAi)
  109. return '';
  110. }
  111. return parent::getFormBoxPost($aBp, $aDp);
  112. }
  113. public function isAttachImageEnabled()
  114. {
  115. return false;
  116. }
  117. public function addAuto($aValues)
  118. {
  119. $this->_bAuto = true;
  120. $mixedResult = $this->add($aValues);
  121. $this->_bAuto = false;
  122. return $mixedResult;
  123. }
  124. public function onPostAfter($iCmtId, $aDp = [])
  125. {
  126. $mixedResult = parent::onPostAfter($iCmtId, $aDp);
  127. if($this->_bAuto || $mixedResult === false)
  128. return $mixedResult;
  129. $iObjId = (int)$this->getId();
  130. $oAI = BxDolAI::getInstance();
  131. $aMessages = [
  132. ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($aAutomator['type'], true)],
  133. ];
  134. $aAutomator = $oAI->getAutomator($iObjId, true);
  135. $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
  136. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
  137. $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
  138. foreach($aComments as $aComment) {
  139. $bAuthorAi = (int)$aComment['cmt_author_id'] == $this->_iProfileIdAi;
  140. $aMessages[] = [
  141. 'role' => $bAuthorAi ? 'assistant' : 'user',
  142. 'content' => $aComment['cmt_text'] . ($bAuthorAi ? '' : ' return only function, without explanation or false if you havn`t enougth data')
  143. ];
  144. }
  145. $sResponse = $oAI->chat($aAutomator['model_url'], $aAutomator['model_model'], $aAutomator['model_key'], $aAutomator['model_params'], $aMessages);
  146. if($sResponse != 'false') {
  147. $mixedResultAuto = $this->addAuto([
  148. 'cmt_author_id' => $this->_iProfileIdAi,
  149. 'cmt_parent_id' => 0,
  150. 'cmt_text' => $sResponse
  151. ]);
  152. if($mixedResultAuto !== false)
  153. $mixedResult['id'] .= ',' . $mixedResultAuto['id'];
  154. }
  155. return $mixedResult;
  156. }
  157. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  158. {
  159. if((int)$aCmt['cmt_author_id'] != $this->_iProfileIdAi)
  160. return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
  161. return $this->_oTemplate->parseLink('javascript:void(0)', _t('_sys_agents_automators_btn_approve'), [
  162. 'class' => 'bx-btn bx-btn-small',
  163. 'onclick' => $this->getPageJsObject() . '.approveCode(this, ' . $aCmt['cmt_id'] . ')',
  164. ]);
  165. }
  166. protected function _getCountersBox(&$aCmt, $aBp = [], $aDp = [])
  167. {
  168. return '';
  169. }
  170. protected function _getFormBox($sType, $aBp, $aDp)
  171. {
  172. return parent::_getFormBox($sType, $aBp, array_merge($aDp, ['min_post_form' => false]));
  173. }
  174. protected function _getTmplVarsText($aCmt)
  175. {
  176. $aResult = parent::_getTmplVarsText($aCmt);
  177. //TODO: Custom template for AI responces.
  178. if((int)$aCmt['cmt_author_id'] == $this->_iProfileIdAi)
  179. $aResult['text'] = '<pre>' . $aResult['text'] . '</pre>';
  180. return $aResult;
  181. }
  182. }
  183. /** @} */