BxDolStudioAgentsAutomatorsCmts.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 $_iProfileIdAi;
  13. protected $_bAuto;
  14. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  15. {
  16. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  17. $this->_bLiveUpdates = false;
  18. $this->_oQueryAgents = new BxDolStudioAgentsQuery();
  19. $this->_iProfileIdAi = BxDolAI::getInstance()->getProfileId();
  20. $this->_bAuto = false;
  21. }
  22. public function actionGetCmt ()
  23. {
  24. if(!$this->isEnabled())
  25. return echoJson([]);
  26. if($this->isViewAllowed() !== CHECK_ACTION_RESULT_ALLOWED)
  27. return echoJson([]);
  28. $mixedCmtId = bx_process_input(bx_get('Cmt'));
  29. $sCmtBrowse = ($sCmtBrowse = bx_get('CmtBrowse')) !== false ? bx_process_input($sCmtBrowse, BX_DATA_TEXT) : '';
  30. $sCmtDisplay = ($sCmtDisplay = bx_get('CmtDisplay')) !== false ? bx_process_input($sCmtDisplay, BX_DATA_TEXT) : '';
  31. $aCmtIds = strpos($mixedCmtId, ',') !== false ? explode(',', $mixedCmtId) : [$mixedCmtId];
  32. $sContent = '';
  33. foreach($aCmtIds as $iCmtId)
  34. $sContent .= $this->getComment((int)$iCmtId, ['type' => $sCmtBrowse], ['type' => $sCmtDisplay, 'dynamic_mode' => true]);
  35. $aCmt = $this->getCommentRow((int)reset($aCmtIds));
  36. echoJson([
  37. 'parent_id' => $aCmt['cmt_parent_id'],
  38. 'vparent_id' => $aCmt['cmt_parent_id'],
  39. 'content' => $sContent
  40. ]);
  41. }
  42. public function getCommentsBlock($aBp = [], $aDp = [])
  43. {
  44. $aComments = parent::getCommentsBlock($aBp, ['in_designbox' => false]);
  45. if(empty($aComments['content']))
  46. return MsgBox(_t('_error occured'));
  47. $aAutomator = $this->_oQueryAgents->getAutomatorsBy(['sample' => 'id', 'id' => (int)$this->getId()]);
  48. if(empty($aAutomator) || !is_array($aAutomator))
  49. return MsgBox(_t('_error occured'));
  50. $aTmplVarsEvent = [];
  51. $bTmplVarsEvent = $aAutomator['type'] == 'event';
  52. if($bTmplVarsEvent)
  53. $aTmplVarsEvent = [
  54. 'unit' => $aAutomator['alert_unit'],
  55. 'action' => $aAutomator['alert_action']
  56. ];
  57. $aTmplVarsScheduler = [];
  58. $bTmplVarsScheduler = $aAutomator['type'] == 'scheduler';
  59. if($bTmplVarsScheduler) {
  60. $aParams = [];
  61. if(!empty($aAutomator['params']))
  62. $aParams = json_decode($aAutomator['params'], true);
  63. $aTmplVarsScheduler = [
  64. 'time' => $aParams['scheduler_time'],
  65. ];
  66. }
  67. $sAutomator = $this->_oTemplate->parseHtmlByName('agents_automator_info.html', [
  68. 'type' => _t('_sys_agents_automators_field_type_' . $aAutomator['type']),
  69. 'bx_if:show_event' => [
  70. 'condition' => $bTmplVarsEvent,
  71. 'content' => $aTmplVarsEvent
  72. ],
  73. 'bx_if:show_scheduler' => [
  74. 'condition' => $bTmplVarsScheduler,
  75. 'content' => $aTmplVarsScheduler
  76. ],
  77. 'code' => $aAutomator['code']
  78. ]);
  79. return [
  80. $sAutomator,
  81. $aComments['content']
  82. ];
  83. }
  84. public function getFormBoxPost($aBp = [], $aDp = [])
  85. {
  86. $aComments = $this->_oQuery->getCommentsBy(['type' => 'latest', 'start' => 0, 'per_page' => 1]);
  87. if(!empty($aComments) && is_array($aComments)) {
  88. $aLast = current($aComments);
  89. if($aLast['cmt_author_id'] != $this->_iProfileIdAi)
  90. return '';
  91. }
  92. return parent::getFormBoxPost($aBp, $aDp);
  93. }
  94. public function isAttachImageEnabled()
  95. {
  96. return false;
  97. }
  98. public function addAuto($aValues)
  99. {
  100. $this->_bAuto = true;
  101. $mixedResult = $this->add($aValues);
  102. $this->_bAuto = false;
  103. return $mixedResult;
  104. }
  105. public function onPostAfter($iCmtId, $aDp = [])
  106. {
  107. $mixedResult = parent::onPostAfter($iCmtId, $aDp);
  108. if($this->_bAuto || $mixedResult === false)
  109. return $mixedResult;
  110. $iObjId = (int)$this->getId();
  111. $oAI = BxDolAI::getInstance();
  112. $aMessages = [
  113. ['role' => 'system', 'content' => $oAI->getAutomatorInstructions($aAutomator['type'], true)],
  114. ];
  115. $aAutomator = $oAI->getAutomator($iObjId, true);
  116. $aComments = $this->_oQuery->getCommentsBy(['type' => 'object_id', 'object_id' => $iObjId]);
  117. if($aAutomator['type'] == BX_DOL_AI_AUTOMATOR_EVENT && !empty($aAutomator['params']['trigger']))
  118. $aComments[0]['cmt_text'] .= $aAutomator['params']['trigger'];
  119. foreach($aComments as $aComment)
  120. $aMessages[] = [
  121. 'role' => (int)$aComment['cmt_author_id'] == $this->_iProfileIdAi ? 'assistant' : 'user',
  122. 'content' => $aComment['cmt_text']
  123. ];
  124. $sResponse = $oAI->chat($aAutomator['model_url'], $aAutomator['model_model'], $aAutomator['model_key'], $aAutomator['model_params'], $aMessages);
  125. if($sResponse != 'false') {
  126. $mixedResultAuto = $this->addAuto([
  127. 'cmt_author_id' => $this->_iProfileIdAi,
  128. 'cmt_parent_id' => 0,
  129. 'cmt_text' => $sResponse
  130. ]);
  131. if($mixedResultAuto !== false)
  132. $mixedResult['id'] .= ',' . $mixedResultAuto['id'];
  133. }
  134. return $mixedResult;
  135. }
  136. protected function _getActionsBox(&$aCmt, $aBp = [], $aDp = [])
  137. {
  138. return parent::_getActionsBox($aCmt, $aBp, array_merge($aDp, ['view_only' => true]));
  139. }
  140. protected function _getFormBox($sType, $aBp, $aDp)
  141. {
  142. return parent::_getFormBox($sType, $aBp, array_merge($aDp, ['min_post_form' => false]));
  143. }
  144. }
  145. /** @} */