Skip to content

Commit

Permalink
Ticket #4770 - Usage of assistants in Live Search and Studio Help.
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLV committed Aug 19, 2024
1 parent ee907d5 commit dab5ec9
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions studio/classes/BxDolStudioAgentsAsstChatsCmts.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,29 @@ public function getComment($mixedCmt, $aBp = [], $aDp = [])
if(!$aCmt)
return '';

$iCmt = (int)$aCmt['cmt_id'];
$iObjId = (int)$this->getId();
$iCmtId = (int)$aCmt['cmt_id'];
if($this->_isShowAnswerLoad($aCmt))
$this->_oQuery->updateComments(['cmt_text' => str_replace(self::$_sPrefixLoad, self::$_sPrefixRetrieve, $aCmt['cmt_text'])], ['cmt_id' => $iCmt]);
$this->_oQuery->updateComments(['cmt_text' => str_replace(self::$_sPrefixLoad, self::$_sPrefixRetrieve, $aCmt['cmt_text'])], ['cmt_id' => $iCmtId]);
else if($this->_isShowAnswerRetrieve($aCmt) && ($mixedAiData = $this->_getAiData()) !== false) {
list($iModelId, $sAssistantId, $sThreadId) = $mixedAiData;

$aCmtRequest = $this->getCommentRow((int)str_replace(self::$_sPrefixRetrieve, '', $aCmt['cmt_text']));

list($iModelId, $sAssistantId, $sThreadId) = $mixedAiData;
if(($oAIModel = $this->_oAI->getModelObject($iModelId)) !== false) {
if(empty($sThreadId) && ($aResponseInit = $oAIModel->getResponseInit(BX_DOL_AI_ASSISTANT, $aCmtRequest['cmt_text'], ['assistant_id' => $sAssistantId])) !== false) {
$sThreadId = $aResponseInit['params']['thread_id'];

$oAIModel = $this->_oAI->getModelObject($iModelId);
if(($sResponse = $oAIModel->getResponse(BX_DOL_AI_ASSISTANT, $aCmtRequest['cmt_text'], ['thread_id' => $sThreadId, 'assistant_id' => $sAssistantId])) !== false) {
$this->_oQuery->updateComments(['cmt_text' => $sResponse], ['cmt_id' => $iCmt]);
$this->_oAI->updateAssistantChatById($iObjId, [
'ai_thread_id' => $sThreadId
]);
}

$aCmt['cmt_text'] = $sResponse;
if(!empty($sThreadId) && ($sResponse = $oAIModel->getResponse(BX_DOL_AI_ASSISTANT, $aCmtRequest['cmt_text'], ['thread_id' => $sThreadId, 'assistant_id' => $sAssistantId])) !== false) {
$this->_oQuery->updateComments(['cmt_text' => $sResponse], ['cmt_id' => $iCmtId]);

$aCmt['cmt_text'] = $sResponse;
}
}
}

Expand Down Expand Up @@ -171,29 +181,6 @@ public function onPostAfter($iCmtId, $aDp = [])
if($this->_bAuto || $mixedResult === false)
return $mixedResult;

$iObjId = (int)$this->getId();

$mixedAiData = $this->_getAiData();
if($mixedAiData === false)
return $mixedResult;

list($iModelId, $sAssistantId, $sThreadId) = $mixedAiData;

$aComment = $this->_oQuery->getCommentSimple($iObjId, $iCmtId);
if(empty($aComment) || !is_array($aComment))
return $mixedResult;

$oAIModel = $this->_oAI->getModelObject($iModelId);
if(empty($sThreadId) && ($aResponseInit = $oAIModel->getResponseInit(BX_DOL_AI_ASSISTANT, $aComment['cmt_text'], ['assistant_id' => $sAssistantId])) !== false) {
$sThreadId = $aResponseInit['params']['thread_id'];

$this->_oAI->updateAssistantChatById($iObjId, [
'ai_thread_id' => $sThreadId
]);
}
if(empty($sThreadId))
return $mixedResult;

$mixedResultAuto = $this->addAuto([
'cmt_author_id' => $this->_iProfileIdAi,
'cmt_parent_id' => 0,
Expand Down Expand Up @@ -251,7 +238,7 @@ protected function _getTmplVarsText($aCmt)
$sText = nl2br($this->_prepareTextForOutput($sText, $iId));

if($bLoad)
$sText = _t('_sys_loading') . $this->_oTemplate->_wrapInTagJsCode($this->getJsObjectName() . "._getCmt('#cmt" . $iId . "', " . $iId . ");");
$sText = _t('_sys_loading') . $this->_oTemplate->_wrapInTagJsCode("var " . $this->_sJsObjName . "_interval = setInterval(function() {if(window['" . $this->_sJsObjName . "'] == undefined) return; clearInterval(" . $this->_sJsObjName . "_interval); " . $this->getJsObjectName() . "._getCmt('#cmt" . $iId . "', " . $iId . ");}, 200)");

if($bRetrieve)
$sText = _t('_sys_agents_assistants_chats_err_no_response');
Expand Down

0 comments on commit dab5ec9

Please sign in to comment.