BxDolAI.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_AI_ASSISTANT', 'assistant');
  10. define('BX_DOL_AI_AUTOMATOR_EVENT', 'event');
  11. define('BX_DOL_AI_AUTOMATOR_SCHEDULER', 'scheduler');
  12. define('BX_DOL_AI_AUTOMATOR_WEBHOOK', 'webhook');
  13. define('BX_DOL_AI_AUTOMATOR_STATUS_AUTO', 'auto');
  14. define('BX_DOL_AI_AUTOMATOR_STATUS_MANUAL', 'manual');
  15. define('BX_DOL_AI_AUTOMATOR_STATUS_READY', 'ready');
  16. class BxDolAI extends BxDolFactory implements iBxDolSingleton
  17. {
  18. protected $_oDb;
  19. protected $_iProfileId;
  20. protected $_aExcludeAlertUnits;
  21. protected $_bWriteLog;
  22. protected $_aChatContext;
  23. protected function __construct()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. parent::__construct();
  28. $this->_oDb = new BxDolAIQuery();
  29. $this->_iProfileId = (int)getParam('sys_profile_bot');
  30. $this->_aExcludeAlertUnits = [
  31. 'system', 'module_template_method_call'
  32. ];
  33. $this->_bWriteLog = true;
  34. $this->_aChatContext = null;
  35. }
  36. /**
  37. * Prevent cloning the instance
  38. */
  39. public function __clone()
  40. {
  41. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  42. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  43. }
  44. /**
  45. * Get singleton instance of the class
  46. */
  47. public static function getInstance()
  48. {
  49. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  50. $GLOBALS['bxDolClasses'][__CLASS__] = BxDolDb::getInstance()->isTableExists('sys_agents_agents') ? new BxDolAI() : null;
  51. }
  52. return $GLOBALS['bxDolClasses'][__CLASS__];
  53. }
  54. public static function getAgentInstance(int $iId, array $aParams = []): NeuronAI\Agent\Agent
  55. {
  56. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId]))
  57. return $GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId];
  58. $a = BxDolAIQuery::getAgentObject($iId);
  59. if (!$a) {
  60. $s = "Agent with id {$iId} not found";
  61. bx_log('sys_agents', $s, BX_LOG_ERR);
  62. throw new Exception($s);
  63. }
  64. if (!$a['active']) {
  65. $s = "Agent with id {$iId} isn't active";
  66. bx_log('sys_agents', $s, BX_LOG_ERR);
  67. throw new Exception($s);
  68. }
  69. if (!$a['prompt_system'] || !$a['model_id']) {
  70. $s = "Agent with id {$iId} can't be used because it haven't prompt or AI model";
  71. bx_log('sys_agents', $s, BX_LOG_ERR);
  72. throw new Exception($s);
  73. }
  74. $o = BxDolAiAgent::make($a, $aParams);
  75. if ((int)$a['tools_max_run'] > 0)
  76. $o->toolMaxRuns($a['tools_max_run']);
  77. if ($a['vector_store_id']) {
  78. $aVectorStore = BxDolAIQuery::getVectorStoreObject($a['vector_store_id']);
  79. if ($aVectorStore && $aVectorStore['embedding_provider_id']) {
  80. $oEmbedder = self::getAiEmbeddingsProviderInstance($aVectorStore['embedding_provider_id']);
  81. $o->setEmbeddingsProvider($oEmbedder);
  82. }
  83. }
  84. $GLOBALS['bxDolClasses'][__CLASS__ . '_Agent_' . $iId] = $o;
  85. return $o;
  86. }
  87. public static function getAiProviderInstance(int $iId):NeuronAI\Providers\AIProviderInterface
  88. {
  89. return BxDolAIModelFactory::getModelInstance($iId);
  90. }
  91. public static function getAiEmbeddingsProviderInstance(int $iId):NeuronAI\RAG\Embeddings\EmbeddingsProviderInterface
  92. {
  93. return BxDolAIModelFactory::getModelInstance($iId);
  94. }
  95. public static function callHelper($mixedHelper, $sMessage)
  96. {
  97. $oAI = BxDolAI::getInstance();
  98. if (is_numeric($mixedHelper))
  99. $aHelper = $oAI->getHelperById($mixedHelper);
  100. else
  101. $aHelper = $oAI->getHelperByName($mixedHelper);
  102. $oAIModel = $oAI->getModelObject($aHelper['model_id']);
  103. return $oAIModel->getResponseText($aHelper['prompt'], $sMessage);
  104. }
  105. public static function pruning() {}
  106. public static function getDefaultApiKey()
  107. {
  108. return getParam('sys_agents_api_key');
  109. }
  110. public static function getDefaultModel()
  111. {
  112. return (int)getParam('sys_agents_model');
  113. }
  114. public function getProfileId()
  115. {
  116. return $this->_iProfileId;
  117. }
  118. public function getModels($aParams = [])
  119. {
  120. $aParamsDb = ['sample' => 'all_pairs'];
  121. if(isset($aParams['active']))
  122. $aParamsDb['active'] = $aParams['active'] === true ? 1 : 0;
  123. if(isset($aParams['capabilities']))
  124. $aParamsDb['capabilities'] = $aParams['capabilities'];
  125. return $this->_oDb->getModelsBy($aParamsDb);
  126. }
  127. public function getModel($iId)
  128. {
  129. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
  130. if(!empty($aModel['params']))
  131. $aModel['params'] = json_decode($aModel['params'], true);
  132. return $aModel;
  133. }
  134. public function callAgent($sType, $aAgent, $mixedParams = [])
  135. {
  136. if ($mixedParams)
  137. $sParams = is_string($mixedParams) ? $mixedParams : json_encode($mixedParams);
  138. else
  139. $sParams = 'START';
  140. // update sample data
  141. $a = ['webhook' => 'webhook_sample'];
  142. if (isset($a[$sType]) && empty($aAgent[$a[$sType]])) {
  143. $this->_oDb->updateAgentField($aAgent['id'], $a[$sType], $sParams);
  144. }
  145. // set additional params
  146. $aParams = [];
  147. if ('message' == $sType || 'form-input' == $sType || 'alert' == $sType) {
  148. $aParams = ['chat_history_subindex' => (int)($mixedParams['sender_profile_id'] ?? 0)];
  149. }
  150. if ('message' == $sType && is_array($mixedParams) && isset($mixedParams['message_text'])) {
  151. $mixedParams['message_text'] = $this->applyChatInputLimit((string)$mixedParams['message_text'], $aAgent);
  152. $sParams = json_encode($mixedParams);
  153. }
  154. else if (is_string($mixedParams) && in_array($sType, ['manual', 'message'], true)) {
  155. $sParams = $this->applyChatInputLimit($sParams, $aAgent);
  156. }
  157. if (in_array($sType, ['manual', 'message'], true) && $this->isChatTurnLimitReached($aAgent, $this->getChatUserTurnCount($aAgent['id'], $aParams))) {
  158. $this->emitConversationClosed('limit', '', $aAgent, $aParams);
  159. return $this->getChatLimitMessage($aAgent);
  160. }
  161. if (in_array($sType, ['manual', 'message'], true) && $this->isChatSessionRateLimited($aAgent, $aParams))
  162. throw new Exception($this->getChatSessionRateLimitError());
  163. $this->_aChatContext = ['agent' => $aAgent, 'params' => $aParams];
  164. // call agent
  165. $mixed = '';
  166. try {
  167. $o = self::getAgentInstance($aAgent['id'], $aParams);
  168. if (!$o)
  169. return false;
  170. $oMessage = $o->chat(new NeuronAI\Chat\Messages\UserMessage($sParams))->getMessage();
  171. $mixed = $oMessage->getContent();
  172. } catch (Exception $exception) {
  173. bx_log('sys_agents', "Exception in '{$aAgent['name']}' agent: " . $exception->getMessage() . " INPUT:" . $sParams, BX_LOG_ERR);
  174. $mixed = _t('_sys_agents_exception');
  175. }
  176. return $mixed;
  177. }
  178. /**
  179. * Whether the current (or given) member may interact with the agent
  180. * via chat, messenger, or form-input. Empty acl_levels means Nobody.
  181. * Site admins / Studio operators always may, so they can debug.
  182. */
  183. public function canInteract($aAgent, $iProfileId = false, $bAllowOperators = true)
  184. {
  185. if (!is_array($aAgent) || empty($aAgent['id']))
  186. return false;
  187. if ($bAllowOperators && isAdmin())
  188. return true;
  189. $iLevels = (int)($aAgent['acl_levels'] ?? 0);
  190. if (!$iLevels)
  191. return false;
  192. return (bool)BxDolAcl::getInstance()->isMemberLevelInSet($iLevels, $iProfileId);
  193. }
  194. /**
  195. * Direct HTTP chat (`sys-ai-chat`). Operators may talk to any trigger;
  196. * everyone else only `manual` and `message` agents they can interact with.
  197. */
  198. public function canChatDirectly($aAgent, $iProfileId = false)
  199. {
  200. if (!$this->canInteract($aAgent, $iProfileId))
  201. return false;
  202. if (isAdmin())
  203. return true;
  204. return in_array($aAgent['trigger'] ?? '', ['manual', 'message'], true);
  205. }
  206. /**
  207. * Runner limits from the agent row. 0 = no cap. Never put these in the prompt.
  208. */
  209. public function applyChatInputLimit($sPrompt, $aAgent)
  210. {
  211. $sPrompt = (string)$sPrompt;
  212. $iMax = (int)($aAgent['max_input_chars'] ?? 0);
  213. if ($iMax <= 0)
  214. return $sPrompt;
  215. if (function_exists('mb_strlen') && function_exists('mb_substr')) {
  216. if (mb_strlen($sPrompt, 'UTF-8') > $iMax)
  217. return mb_substr($sPrompt, 0, $iMax, 'UTF-8');
  218. return $sPrompt;
  219. }
  220. return strlen($sPrompt) > $iMax ? substr($sPrompt, 0, $iMax) : $sPrompt;
  221. }
  222. public function getChatLimitMessage($aAgent)
  223. {
  224. $s = trim((string)($aAgent['limit_message'] ?? ''));
  225. return $s !== '' ? $s : 'This chat has reached its limit.';
  226. }
  227. public function getChatSessionRateLimitError()
  228. {
  229. $s = _t('_sys_agents_err_session_rate_limit');
  230. return ($s !== '' && $s !== '_sys_agents_err_session_rate_limit')
  231. ? $s
  232. : 'Too many chat sessions from this IP. Try again later.';
  233. }
  234. public function countChatUserTurns($aUiMessages)
  235. {
  236. $i = 0;
  237. if (!is_array($aUiMessages))
  238. return 0;
  239. foreach ($aUiMessages as $aMessage) {
  240. if (is_array($aMessage) && ($aMessage['role'] ?? '') === 'user')
  241. $i++;
  242. }
  243. return $i;
  244. }
  245. public function isChatTurnLimitReached($aAgent, $iTurns, $iRequestUserTurns = 0)
  246. {
  247. $iMax = (int)($aAgent['max_turns'] ?? 0);
  248. if ($iMax <= 0)
  249. return false;
  250. if ((int)$iTurns >= $iMax)
  251. return true;
  252. // Client transcript includes the message being submitted.
  253. return (int)$iRequestUserTurns > $iMax;
  254. }
  255. public function getChatUserTurnCount($iAgentId, $aParams = [])
  256. {
  257. if (!isset($aParams['chat_history_subindex']))
  258. $aParams = array_merge($this->resolveChatHistoryParams($iAgentId), $aParams);
  259. try {
  260. $o = self::getAgentInstance((int)$iAgentId, $aParams);
  261. } catch (Throwable $oException) {
  262. return 0;
  263. }
  264. $i = 0;
  265. foreach ($o->getChatHistory()->getMessages() as $oMessage) {
  266. if ($oMessage instanceof NeuronAI\Chat\Messages\ToolCallMessage
  267. || $oMessage instanceof NeuronAI\Chat\Messages\ToolResultMessage
  268. ) {
  269. continue;
  270. }
  271. if ($oMessage instanceof NeuronAI\Chat\Messages\UserMessage)
  272. $i++;
  273. }
  274. return $i;
  275. }
  276. /**
  277. * New chat threads per IP for this agent. 0 on the agent = no cap.
  278. * Continuation of an existing thread is not a new session.
  279. */
  280. public function isChatSessionRateLimited($aAgent, $aParams = [])
  281. {
  282. $iHour = (int)($aAgent['max_sessions_per_hour'] ?? 0);
  283. $iDay = (int)($aAgent['max_sessions_per_day'] ?? 0);
  284. if ($iHour <= 0 && $iDay <= 0)
  285. return false;
  286. if (!isset($aParams['chat_history_subindex']))
  287. $aParams = array_merge($this->resolveChatHistoryParams((int)($aAgent['id'] ?? 0)), $aParams);
  288. $sThreadId = self::chatHistoryThreadId($aAgent, $aParams);
  289. if (!$this->isNewChatSession($sThreadId))
  290. return false;
  291. $sIp = $this->getVisitorIpHash();
  292. if ($sIp === '0' || $sIp === '')
  293. return false;
  294. $sPrefix = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  295. if ($sPrefix === ':')
  296. return false;
  297. if ($iHour > 0 && $this->countChatSessionsByIp($sIp, $sPrefix, 3600) >= $iHour)
  298. return true;
  299. if ($iDay > 0 && $this->countChatSessionsByIp($sIp, $sPrefix, 86400) >= $iDay)
  300. return true;
  301. return false;
  302. }
  303. protected function getVisitorIpHash()
  304. {
  305. if (!function_exists('getVisitorIP') || !function_exists('bx_get_ip_hash'))
  306. return '0';
  307. return (string)bx_get_ip_hash(getVisitorIP());
  308. }
  309. protected function isNewChatSession($sThreadId)
  310. {
  311. $sThreadId = (string)$sThreadId;
  312. if ($sThreadId === '')
  313. return true;
  314. $aRow = $this->_oDb->getRow("SELECT `ip`, `messages` FROM `sys_agents_chat_history` WHERE `thread_id` = :t", [
  315. 't' => $sThreadId,
  316. ]);
  317. if (!$aRow)
  318. return true;
  319. $iIp = (int)($aRow['ip'] ?? 0);
  320. $sMessages = trim((string)($aRow['messages'] ?? ''));
  321. return $iIp <= 0 || $sMessages === '' || $sMessages === '[]';
  322. }
  323. protected function countChatSessionsByIp($sIp, $sPrefix, $iWindowSec)
  324. {
  325. $sSince = date('Y-m-d H:i:s', time() - (int)$iWindowSec);
  326. return (int)$this->_oDb->getOne("
  327. SELECT COUNT(*) FROM `sys_agents_chat_history`
  328. WHERE `ip` = :ip
  329. AND (`thread_id` = :prefix OR `thread_id` LIKE :prefix_like)
  330. AND `created_at` >= :since
  331. ", [
  332. 'ip' => $sIp,
  333. 'prefix' => $sPrefix,
  334. 'prefix_like' => $sPrefix . ':%',
  335. 'since' => $sSince,
  336. ]);
  337. }
  338. public function extractChatPromptFromRequest($aData)
  339. {
  340. if (!is_array($aData))
  341. return '';
  342. if (!empty($aData['prompt']) && is_string($aData['prompt']))
  343. return trim($aData['prompt']);
  344. if (empty($aData['messages']) || !is_array($aData['messages']))
  345. return '';
  346. for ($i = count($aData['messages']) - 1; $i >= 0; $i--) {
  347. $aMessage = $aData['messages'][$i];
  348. if (!is_array($aMessage))
  349. continue;
  350. if (($aMessage['role'] ?? '') !== 'user')
  351. continue;
  352. if (isset($aMessage['content']) && is_string($aMessage['content']))
  353. return trim($aMessage['content']);
  354. if (!empty($aMessage['parts']) && is_array($aMessage['parts'])) {
  355. $aText = [];
  356. foreach ($aMessage['parts'] as $aPart) {
  357. if (is_array($aPart) && ($aPart['type'] ?? '') === 'text' && isset($aPart['content']))
  358. $aText[] = $aPart['content'];
  359. }
  360. $s = trim(implode("\n", $aText));
  361. if ($s !== '')
  362. return $s;
  363. }
  364. }
  365. return '';
  366. }
  367. /**
  368. * Guests: UNA session id. Members: profile id.
  369. * Guest chats remember agent ids in session so login can adopt those threads.
  370. */
  371. public function resolveChatHistoryParams($iAgentId = 0)
  372. {
  373. $iAgentId = (int)$iAgentId;
  374. $iProfileId = (int)bx_get_logged_profile_id();
  375. $oSession = BxDolSession::getInstance();
  376. $sSessionKey = 'ai_chat_agent_ids';
  377. if ($iProfileId) {
  378. $aIds = $oSession->getValue($sSessionKey);
  379. if (is_array($aIds) && $aIds) {
  380. $sSessionId = (string)$oSession->getId();
  381. if ($iAgentId)
  382. $aIds[] = $iAgentId;
  383. foreach (array_unique(array_map('intval', $aIds)) as $iStoredAgentId) {
  384. if (!$iStoredAgentId || $sSessionId === '')
  385. continue;
  386. $aAgent = BxDolAIQuery::getAgentObject($iStoredAgentId);
  387. if ($aAgent)
  388. $this->_oDb->adoptGuestChatHistory($aAgent, $sSessionId, $iProfileId);
  389. }
  390. $oSession->unsetValue($sSessionKey);
  391. }
  392. return [
  393. 'sender_profile_id' => $iProfileId,
  394. 'chat_history_subindex' => (string)$iProfileId,
  395. ];
  396. }
  397. if ($iAgentId) {
  398. $aIds = $oSession->getValue($sSessionKey);
  399. $aIds = array_map('intval', is_array($aIds) ? $aIds : []);
  400. if (!in_array($iAgentId, $aIds, true)) {
  401. $aIds[] = $iAgentId;
  402. $oSession->setValue($sSessionKey, $aIds);
  403. }
  404. }
  405. return [
  406. 'sender_profile_id' => 0,
  407. 'chat_history_subindex' => (string)$oSession->getId(),
  408. ];
  409. }
  410. /**
  411. * `{trigger}:{agentId}:{contextPid}:{userSubindex}`.
  412. * Context is omitted when 0 so existing site-wide threads still load.
  413. * User suffix (session id or profile id) stays last so adopt can replace it.
  414. */
  415. public static function chatHistoryThreadId($aAgent, $aParams = [])
  416. {
  417. $s = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  418. $iContextPid = (int)($aParams['chat_history_context_pid'] ?? 0);
  419. if ($iContextPid > 0)
  420. $s .= ':' . $iContextPid;
  421. if (isset($aParams['chat_history_subindex']) && $aParams['chat_history_subindex'] !== '')
  422. $s .= ':' . (string)$aParams['chat_history_subindex'];
  423. return $s;
  424. }
  425. public function getChatHistoryRowId($aAgent, $aParams = [])
  426. {
  427. $sThreadId = self::chatHistoryThreadId($aAgent, $aParams);
  428. if ($sThreadId === '' || $sThreadId === ':')
  429. return 0;
  430. return (int)$this->_oDb->getOne("SELECT `id` FROM `sys_agents_chat_history` WHERE `thread_id` = :t", [
  431. 't' => $sThreadId,
  432. ]);
  433. }
  434. public function getCurrentChatHistoryId()
  435. {
  436. if (!is_array($this->_aChatContext))
  437. return 0;
  438. $aAgent = $this->_aChatContext['agent'] ?? null;
  439. $aParams = $this->_aChatContext['params'] ?? [];
  440. if (!is_array($aAgent))
  441. return 0;
  442. return $this->getChatHistoryRowId($aAgent, is_array($aParams) ? $aParams : []);
  443. }
  444. public function emitConversationClosed($sReason, $sSummary = '', $aAgent = null, $aParams = null)
  445. {
  446. if (!is_array($aAgent) && is_array($this->_aChatContext))
  447. $aAgent = $this->_aChatContext['agent'] ?? null;
  448. if (!is_array($aParams) && is_array($this->_aChatContext))
  449. $aParams = $this->_aChatContext['params'] ?? [];
  450. if (!is_array($aAgent) || empty($aAgent['id']))
  451. return false;
  452. $iHistoryId = $this->getChatHistoryRowId($aAgent, is_array($aParams) ? $aParams : []);
  453. if ($iHistoryId <= 0)
  454. return false;
  455. if ($this->_oDb->isFieldExists('sys_agents_chat_history', 'closed_reason')) {
  456. $sClosed = (string)$this->_oDb->getOne(
  457. "SELECT `closed_reason` FROM `sys_agents_chat_history` WHERE `id` = :id",
  458. ['id' => $iHistoryId]
  459. );
  460. if ($sClosed !== '')
  461. return false;
  462. }
  463. bx_alert('system', 'agent_conversation_closed', $iHistoryId, 0, [
  464. 'agent_id' => (int)$aAgent['id'],
  465. 'summary' => (string)$sSummary,
  466. 'reason' => (string)$sReason,
  467. ]);
  468. return true;
  469. }
  470. /**
  471. * @return array{thread_id:string,chat_history_context_pid:int,chat_history_subindex:string}|false
  472. */
  473. public static function parseChatHistoryThreadId($aAgent, $sThreadId)
  474. {
  475. $sThreadId = (string)$sThreadId;
  476. $sBase = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  477. if ($sBase === ':' || $sThreadId === '')
  478. return false;
  479. if ($sThreadId !== $sBase && strpos($sThreadId, $sBase . ':') !== 0)
  480. return false;
  481. $sRest = $sThreadId === $sBase ? '' : substr($sThreadId, strlen($sBase) + 1);
  482. $aParts = $sRest === '' ? [] : explode(':', $sRest);
  483. $iContext = 0;
  484. $sSub = '';
  485. if (count($aParts) >= 2) {
  486. $iContext = (int)$aParts[0];
  487. $sSub = implode(':', array_slice($aParts, 1));
  488. } elseif (count($aParts) === 1) {
  489. $sSub = $aParts[0];
  490. }
  491. return [
  492. 'thread_id' => $sThreadId,
  493. 'chat_history_context_pid' => $iContext,
  494. 'chat_history_subindex' => $sSub,
  495. ];
  496. }
  497. public function isOwnAgentChatThread($aAgent, $sThreadId)
  498. {
  499. $aParsed = self::parseChatHistoryThreadId($aAgent, $sThreadId);
  500. if (!$aParsed)
  501. return false;
  502. $aMine = $this->resolveChatHistoryParams((int)$aAgent['id']);
  503. $sMine = (string)($aMine['chat_history_subindex'] ?? '');
  504. return $sMine !== '' && $aParsed['chat_history_subindex'] === $sMine;
  505. }
  506. public function storedChatJsonToUiMessages($sJson)
  507. {
  508. $aStored = json_decode((string)$sJson, true);
  509. if (!is_array($aStored) || $aStored === [])
  510. return [];
  511. $aResult = [];
  512. $i = 0;
  513. foreach ($aStored as $aMessage) {
  514. if (!is_array($aMessage))
  515. continue;
  516. $sType = (string)($aMessage['type'] ?? '');
  517. if ($sType === 'tool_call' || $sType === 'tool_call_result')
  518. continue;
  519. $sRole = (string)($aMessage['role'] ?? '');
  520. if ($sRole === 'model')
  521. $sRole = 'assistant';
  522. if ($sRole !== 'user' && $sRole !== 'assistant')
  523. continue;
  524. $aParts = [];
  525. $mixedContent = $aMessage['content'] ?? '';
  526. if (is_string($mixedContent) && $mixedContent !== '') {
  527. $aParts[] = ['type' => 'text', 'content' => $mixedContent];
  528. } elseif (is_array($mixedContent)) {
  529. foreach ($mixedContent as $aBlock) {
  530. if (!is_array($aBlock))
  531. continue;
  532. $sBlockType = (string)($aBlock['type'] ?? '');
  533. $sBlockText = (string)($aBlock['content'] ?? '');
  534. if ($sBlockText === '')
  535. continue;
  536. if ($sBlockType === 'text' || $sBlockType === '')
  537. $aParts[] = ['type' => 'text', 'content' => $sBlockText];
  538. }
  539. }
  540. $sId = '';
  541. if (!empty($aMessage['__id']) && is_string($aMessage['__id']))
  542. $sId = $aMessage['__id'];
  543. elseif (!empty($aMessage['metadata']['__id']) && is_string($aMessage['metadata']['__id']))
  544. $sId = $aMessage['metadata']['__id'];
  545. $mixedActions = $aMessage['actions'] ?? ($aMessage['metadata']['actions'] ?? null);
  546. $aUi = $this->finalizeUiChatMessage($sId !== '' ? $sId : ('msg_' . $i), $sRole, $aParts, $mixedActions);
  547. if (!$aUi)
  548. continue;
  549. $aResult[] = $aUi;
  550. $i++;
  551. }
  552. return $aResult;
  553. }
  554. public function getChatHistoryUiMessagesByThread($aAgent, $sThreadId)
  555. {
  556. if (!self::parseChatHistoryThreadId($aAgent, $sThreadId))
  557. return false;
  558. $sJson = $this->_oDb->getOne("SELECT `messages` FROM `sys_agents_chat_history` WHERE `thread_id` = :t", [
  559. 't' => (string)$sThreadId,
  560. ]);
  561. return $this->storedChatJsonToUiMessages($sJson);
  562. }
  563. public function getChatHistoryArtifactsByThread($aAgent, $sThreadId)
  564. {
  565. if (!self::parseChatHistoryThreadId($aAgent, $sThreadId))
  566. return [];
  567. return $this->_oDb->getChatArtifactsByThreadId((string)$sThreadId);
  568. }
  569. public function listAgentChatThreads($aAgent)
  570. {
  571. $aMineParams = $this->resolveChatHistoryParams((int)$aAgent['id']);
  572. $sMySub = (string)($aMineParams['chat_history_subindex'] ?? '');
  573. $sMyDefault = self::chatHistoryThreadId($aAgent, $aMineParams);
  574. $aOut = [];
  575. $bHaveMineDefault = false;
  576. $aRows = $this->_oDb->getAgentChatHistoryRows($aAgent);
  577. if (!is_array($aRows))
  578. $aRows = [];
  579. $aHistoryIds = [];
  580. foreach ($aRows as $aRow)
  581. $aHistoryIds[] = (int)($aRow['id'] ?? 0);
  582. $aArtifactsMap = $this->_oDb->getChatArtifactsForAgent($aAgent);
  583. if (!$aArtifactsMap && $aHistoryIds)
  584. $aArtifactsMap = $this->_oDb->getChatArtifactsByHistoryIds($aHistoryIds);
  585. foreach ($aRows as $aRow) {
  586. $sThreadId = (string)($aRow['thread_id'] ?? '');
  587. $aParsed = self::parseChatHistoryThreadId($aAgent, $sThreadId);
  588. if (!$aParsed)
  589. continue;
  590. $aUi = $this->storedChatJsonToUiMessages($aRow['messages'] ?? '');
  591. if (!$aUi && $sThreadId !== $sMyDefault)
  592. continue;
  593. $bMine = ($sMySub !== '' && $aParsed['chat_history_subindex'] === $sMySub);
  594. if ($sThreadId === $sMyDefault)
  595. $bHaveMineDefault = true;
  596. $iHistoryId = (int)($aRow['id'] ?? 0);
  597. $aArtifacts = ($iHistoryId && !empty($aArtifactsMap[$iHistoryId])) ? $aArtifactsMap[$iHistoryId] : [];
  598. $aOut[] = $this->formatAgentChatThread($aParsed, $aRow, $aUi, $bMine, $aArtifacts);
  599. }
  600. if (!$bHaveMineDefault && $sMyDefault !== '') {
  601. $aParsedMine = self::parseChatHistoryThreadId($aAgent, $sMyDefault);
  602. if ($aParsedMine)
  603. $aOut[] = $this->formatAgentChatThread($aParsedMine, [], [], true);
  604. }
  605. usort($aOut, function ($a, $b) {
  606. return strcmp((string)$b['updated_sort'], (string)$a['updated_sort']);
  607. });
  608. return $aOut;
  609. }
  610. protected function formatAgentChatThread($aParsed, $aRow, $aUi, $bMine, $aArtifacts = [])
  611. {
  612. $iContext = (int)$aParsed['chat_history_context_pid'];
  613. $sSub = (string)$aParsed['chat_history_subindex'];
  614. $sContextName = '';
  615. if ($iContext > 0) {
  616. $oContext = BxDolProfile::getInstance($iContext);
  617. $sContextName = $oContext ? $oContext->getDisplayName() : ('#' . $iContext);
  618. }
  619. $sTitle = _t('_sys_agents_agents_txt_guest_chat');
  620. $oProfile = (ctype_digit($sSub) && (int)$sSub > 0) ? BxDolProfile::getInstance((int)$sSub) : false;
  621. if ($oProfile)
  622. $sTitle = $oProfile->getDisplayName();
  623. if ($sContextName !== '')
  624. $sTitle .= ' · ' . $sContextName;
  625. $sReason = trim((string)($aRow['closed_reason'] ?? ''));
  626. $sStatus = $sReason !== '' ? 'closed' : 'opened';
  627. $sStatusLabel = $sStatus;
  628. if ($sStatus === 'closed' && $sReason !== '')
  629. $sStatusLabel .= ' (' . $sReason . ')';
  630. $aArtifacts = is_array($aArtifacts) ? $aArtifacts : [];
  631. $sCreated = $this->formatAgentChatThreadTime($aRow['created_at'] ?? '');
  632. $sUpdated = $this->formatAgentChatThreadTime($aRow['updated_at'] ?? '');
  633. $sUpdatedRaw = (string)($aRow['updated_at'] ?? '');
  634. $sMeta = 'status: ' . $sStatusLabel;
  635. $sDates = '';
  636. if ($sCreated !== '' && $sUpdated !== '')
  637. $sDates = $sCreated . ' - ' . $sUpdated;
  638. else if ($sCreated !== '')
  639. $sDates = $sCreated;
  640. else if ($sUpdated !== '')
  641. $sDates = $sUpdated;
  642. if ($sDates !== '')
  643. $sMeta .= "\n" . $sDates;
  644. return [
  645. 'thread_id' => $aParsed['thread_id'],
  646. 'writable' => $bMine ? 1 : 0,
  647. 'context_pid' => $iContext,
  648. 'title' => $sTitle,
  649. 'meta' => $sMeta,
  650. 'status' => $sStatus,
  651. 'closed_reason' => $sReason,
  652. 'created_at' => $sCreated,
  653. 'updated_at' => $sUpdated,
  654. 'artifacts' => is_array($aArtifacts) ? $aArtifacts : [],
  655. 'updated_sort' => $sUpdatedRaw,
  656. 'class_mine' => $bMine ? ' bx-agents-popup-chat-thread-mine' : '',
  657. ];
  658. }
  659. protected function formatAgentChatThreadTime($mixed): string
  660. {
  661. $s = trim((string)$mixed);
  662. if ($s === '' || $s === '0')
  663. return '';
  664. $i = ctype_digit($s) ? (int)$s : (int)strtotime($s);
  665. if ($i <= 86400)
  666. return '';
  667. return date('d.m.Y H:i', $i);
  668. }
  669. /**
  670. * Context from chat HTTP (`?context=`). Missing/0 = site-wide. Invalid/denied = false (403).
  671. * Posting in a group is not the same as viewing it; chat requires post by default.
  672. * @return int|false
  673. */
  674. public function resolveChatHistoryContextPid($bRequirePost = true)
  675. {
  676. $mixed = bx_get('context');
  677. if ($mixed === false)
  678. return 0;
  679. $iPid = (int)$mixed;
  680. if ($iPid <= 0)
  681. return 0;
  682. $oProfile = BxDolProfile::getInstance($iPid);
  683. if (!$oProfile || !bx_srv('system', 'is_module_context', [$oProfile->getModule()]))
  684. return false;
  685. if (!$this->isAllowedContextChat($oProfile, $bRequirePost))
  686. return false;
  687. return $iPid;
  688. }
  689. /**
  690. * Context of the current page (AI agent block).
  691. * `bx_get_page_info()` reads the URL; App page JSON merges `i`/`id` into $_GET instead.
  692. * @param bool $bRequirePost also require posting into the context (membership / post ACL)
  693. */
  694. public function resolveChatHistoryContextPidFromPage($bRequirePost = false)
  695. {
  696. $aInfo = bx_get_page_info();
  697. if ($aInfo && !empty($aInfo['context_profile_id']))
  698. return $this->filterContextPid((int)$aInfo['context_profile_id'], $bRequirePost);
  699. $sUri = bx_get('i');
  700. $iContentId = (int)bx_process_input(bx_get('id'), BX_DATA_INT);
  701. if ($sUri && $iContentId > 0) {
  702. $oPage = BxDolPage::getObjectInstanceByURI($sUri);
  703. $sModule = $oPage ? (string)$oPage->getModule() : '';
  704. if ($sModule !== '' && bx_srv('system', 'is_module_context', [$sModule])) {
  705. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModule);
  706. if ($oProfile && ($iPid = $this->filterContextPid((int)$oProfile->id(), $bRequirePost)))
  707. return $iPid;
  708. }
  709. }
  710. $iPid = (int)bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  711. if ($iPid > 0 && ($iPid = $this->filterContextPid($iPid, $bRequirePost)))
  712. return $iPid;
  713. return 0;
  714. }
  715. public function filterContextPid($iPid, $bRequirePost = false)
  716. {
  717. $iPid = (int)$iPid;
  718. if ($iPid <= 0)
  719. return 0;
  720. $oProfile = BxDolProfile::getInstance($iPid);
  721. if (!$oProfile || !bx_srv('system', 'is_module_context', [$oProfile->getModule()]))
  722. return 0;
  723. if (!$this->isAllowedContextChat($oProfile, $bRequirePost))
  724. return 0;
  725. return $iPid;
  726. }
  727. protected function isAllowedContextChat($oProfile, $bRequirePost = false)
  728. {
  729. if ($oProfile->checkAllowedProfileView() !== CHECK_ACTION_RESULT_ALLOWED)
  730. return false;
  731. if ($bRequirePost && $oProfile->checkAllowedPostInProfile() !== CHECK_ACTION_RESULT_ALLOWED)
  732. return false;
  733. return true;
  734. }
  735. /**
  736. * Transcript for TanStack `useChat` hydrate / `initialMessages`.
  737. * Loads the same NeuronAI chat history the agent uses when streaming.
  738. *
  739. * @param bool $bAppendLimitMessage when the session is at max_turns, append limit_message for the UI
  740. */
  741. public function getChatHistoryUiMessages($iAgentId, $aParams = [], $bAppendLimitMessage = true)
  742. {
  743. if (!isset($aParams['chat_history_subindex']))
  744. $aParams = array_merge($this->resolveChatHistoryParams($iAgentId), $aParams);
  745. $aAgent = BxDolAIQuery::getAgentObject((int)$iAgentId);
  746. if (!$aAgent || empty($aAgent['chat_history_context']))
  747. return [];
  748. $sThreadId = self::chatHistoryThreadId($aAgent, $aParams);
  749. $sJson = $this->_oDb->getOne("SELECT `messages` FROM `sys_agents_chat_history` WHERE `thread_id` = :t", [
  750. 't' => $sThreadId,
  751. ]);
  752. $aStored = json_decode((string)$sJson, true);
  753. if (!is_array($aStored) || $aStored === [])
  754. return [];
  755. try {
  756. $o = self::getAgentInstance((int)$iAgentId, $aParams);
  757. } catch (Exception $oException) {
  758. bx_log('sys_agents', "Hydrate exception for agent {$iAgentId}: " . $oException->getMessage(), BX_LOG_ERR);
  759. return [];
  760. }
  761. $aResult = $this->neuronMessagesToUiMessages($o->getChatHistory()->getMessages());
  762. if ($bAppendLimitMessage && $this->isChatTurnLimitReached($aAgent, $this->countChatUserTurns($aResult))) {
  763. $aResult[] = [
  764. 'id' => 'agent_limit_message',
  765. 'role' => 'assistant',
  766. 'parts' => [['type' => 'text', 'content' => $this->getChatLimitMessage($aAgent)]],
  767. ];
  768. }
  769. return $aResult;
  770. }
  771. /**
  772. * @param NeuronAI\Chat\Messages\Message[] $aMessages
  773. * @return array<int, array<string, mixed>>
  774. */
  775. protected function neuronMessagesToUiMessages($aMessages)
  776. {
  777. $aResult = [];
  778. $i = 0;
  779. foreach ($aMessages as $oMessage) {
  780. if (
  781. $oMessage instanceof NeuronAI\Chat\Messages\ToolCallMessage
  782. || $oMessage instanceof NeuronAI\Chat\Messages\ToolResultMessage
  783. ) {
  784. continue;
  785. }
  786. $sRole = $oMessage->getRole();
  787. if ($sRole === 'model')
  788. $sRole = 'assistant';
  789. if ($sRole !== 'user' && $sRole !== 'assistant')
  790. continue;
  791. $aParts = [];
  792. foreach ($oMessage->getContentBlocks() as $oBlock) {
  793. if ($oBlock instanceof NeuronAI\Chat\Messages\ContentBlocks\ReasoningContent) {
  794. if ($oBlock->content !== '')
  795. $aParts[] = ['type' => 'thinking', 'content' => $oBlock->content];
  796. continue;
  797. }
  798. if ($oBlock instanceof NeuronAI\Chat\Messages\ContentBlocks\TextContent && $oBlock->content !== '')
  799. $aParts[] = ['type' => 'text', 'content' => $oBlock->content];
  800. }
  801. $sId = $oMessage->getMetadata('__id');
  802. $aUi = $this->finalizeUiChatMessage(
  803. is_string($sId) && $sId !== '' ? $sId : ('msg_' . $i),
  804. $sRole,
  805. $aParts,
  806. $sRole === 'assistant' ? $oMessage->getMetadata('actions') : null
  807. );
  808. if (!$aUi)
  809. continue;
  810. $aResult[] = $aUi;
  811. $i++;
  812. }
  813. return $aResult;
  814. }
  815. /**
  816. * Split assistant JSON {content, actions} into UI text + actions.
  817. * User messages pass through unchanged.
  818. *
  819. * @param array<int, array<string, mixed>> $aParts
  820. * @return array<string, mixed>|null
  821. */
  822. protected function finalizeUiChatMessage($sId, $sRole, $aParts, $mixedActions = null)
  823. {
  824. $aActions = $sRole === 'assistant' ? $this->sanitizeChatActions($mixedActions) : [];
  825. if ($sRole === 'assistant') {
  826. $sText = '';
  827. foreach ($aParts as $aPart) {
  828. if (($aPart['type'] ?? '') === 'text')
  829. $sText .= (string)($aPart['content'] ?? '');
  830. }
  831. $aParsed = $this->parseAssistantChatPayload($sText);
  832. if ($aParsed) {
  833. $aKept = [];
  834. foreach ($aParts as $aPart) {
  835. if (($aPart['type'] ?? '') === 'text')
  836. continue;
  837. $aKept[] = $aPart;
  838. }
  839. if ($aParsed['content'] !== '')
  840. $aKept[] = ['type' => 'text', 'content' => $aParsed['content']];
  841. $aParts = $aKept;
  842. if (!$aActions)
  843. $aActions = $aParsed['actions'];
  844. }
  845. }
  846. if (!$aParts && !$aActions)
  847. return null;
  848. $aOut = [
  849. 'id' => $sId,
  850. 'role' => $sRole,
  851. 'parts' => $aParts,
  852. ];
  853. if ($sRole === 'assistant')
  854. $aOut['actions'] = $aActions;
  855. return $aOut;
  856. }
  857. /**
  858. * @return array{content: string, actions: array}|null
  859. */
  860. protected function parseAssistantChatPayload($sText)
  861. {
  862. $s = trim((string)$sText);
  863. if ($s === '')
  864. return null;
  865. if (preg_match('/^```(?:json)?\s*(\{.*\})\s*```$/s', $s, $aM))
  866. $s = trim($aM[1]);
  867. if ($s === '' || $s[0] !== '{') {
  868. $iStart = strpos($s, '{');
  869. $iEnd = strrpos($s, '}');
  870. if ($iStart === false || $iEnd === false || $iEnd <= $iStart)
  871. return null;
  872. $s = substr($s, $iStart, $iEnd - $iStart + 1);
  873. }
  874. $a = json_decode($s, true);
  875. if (!is_array($a) || !array_key_exists('content', $a) || !is_string($a['content']))
  876. return null;
  877. if (!array_key_exists('actions', $a))
  878. return null;
  879. return [
  880. 'content' => $a['content'],
  881. 'actions' => $this->sanitizeChatActions($a['actions']),
  882. ];
  883. }
  884. /**
  885. * @return array<int, array<string, mixed>>
  886. */
  887. protected function sanitizeChatActions($mixed)
  888. {
  889. if (!is_array($mixed))
  890. return [];
  891. $aOut = [];
  892. foreach ($mixed as $aItem) {
  893. if (!is_array($aItem))
  894. continue;
  895. $sType = strtolower(trim((string)($aItem['type'] ?? '')));
  896. $sLabel = trim((string)($aItem['label'] ?? ''));
  897. if ($sLabel === '')
  898. continue;
  899. if (function_exists('mb_substr'))
  900. $sLabel = mb_substr($sLabel, 0, 80);
  901. else
  902. $sLabel = substr($sLabel, 0, 80);
  903. if ($sType === 'reply') {
  904. $aOut[] = ['type' => 'reply', 'label' => $sLabel];
  905. } elseif ($sType === 'link') {
  906. $sUrl = trim((string)($aItem['url'] ?? ''));
  907. if (!$this->isAllowedChatActionUrl($sUrl))
  908. continue;
  909. $aOut[] = ['type' => 'link', 'label' => $sLabel, 'url' => $sUrl];
  910. }
  911. if (count($aOut) >= 8)
  912. break;
  913. }
  914. return $aOut;
  915. }
  916. protected function isAllowedChatActionUrl($sUrl)
  917. {
  918. $a = parse_url((string)$sUrl);
  919. if (($a['scheme'] ?? '') !== 'https' || empty($a['host']))
  920. return false;
  921. $sHost = strtolower((string)$a['host']);
  922. $aAllowed = ['hiweave.com', 'www.hiweave.com'];
  923. if (defined('BX_DOL_URL_ROOT')) {
  924. $aSite = parse_url(BX_DOL_URL_ROOT);
  925. if (!empty($aSite['host']))
  926. $aAllowed[] = strtolower((string)$aSite['host']);
  927. }
  928. return in_array($sHost, $aAllowed, true);
  929. }
  930. protected function sseChatEvent($aPayload)
  931. {
  932. return 'data: ' . json_encode($aPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n\n";
  933. }
  934. protected function parseSseChatEvent($sEvent)
  935. {
  936. $s = trim((string)$sEvent);
  937. if (!preg_match('/^data:\s*(.+)$/s', $s, $aM))
  938. return null;
  939. $a = json_decode($aM[1], true);
  940. return is_array($a) ? $a : null;
  941. }
  942. protected function resetChatActionsSseState()
  943. {
  944. return [
  945. 'mode' => 'undecided',
  946. 'buf' => '',
  947. 'messageId' => '',
  948. 'held' => [],
  949. ];
  950. }
  951. protected function emitHeldChatActionsSse(&$aState, $fEmit)
  952. {
  953. foreach ($aState['held'] as $sHeld)
  954. $fEmit($sHeld);
  955. $aState['held'] = [];
  956. }
  957. protected function emitParsedChatActionsSse(&$aState, $fEmit, $aParsed)
  958. {
  959. $sId = $aState['messageId'] !== '' ? $aState['messageId'] : ('msg_' . uniqid());
  960. $fEmit($this->sseChatEvent([
  961. 'type' => 'TEXT_MESSAGE_START',
  962. 'messageId' => $sId,
  963. 'role' => 'assistant',
  964. ]));
  965. if ($aParsed['content'] !== '') {
  966. $fEmit($this->sseChatEvent([
  967. 'type' => 'TEXT_MESSAGE_CONTENT',
  968. 'messageId' => $sId,
  969. 'delta' => $aParsed['content'],
  970. ]));
  971. }
  972. $fEmit($this->sseChatEvent([
  973. 'type' => 'TEXT_MESSAGE_END',
  974. 'messageId' => $sId,
  975. ]));
  976. if (!empty($aParsed['actions'])) {
  977. $fEmit($this->sseChatEvent([
  978. 'type' => 'CUSTOM',
  979. 'name' => 'chat_actions',
  980. 'messageId' => $sId,
  981. 'value' => $aParsed['actions'],
  982. ]));
  983. }
  984. $aState = $this->resetChatActionsSseState();
  985. }
  986. protected function processChatActionsSseEvent($sEvent, &$aState, $fEmit)
  987. {
  988. $aPayload = $this->parseSseChatEvent($sEvent);
  989. $sType = is_array($aPayload) ? (string)($aPayload['type'] ?? '') : '';
  990. if ($sType === 'TEXT_MESSAGE_START') {
  991. if ($aState['held'] || $aState['buf'] !== '')
  992. $this->flushChatActionsSseState($aState, $fEmit);
  993. $aState['messageId'] = (string)($aPayload['messageId'] ?? '');
  994. $aState['held'][] = $sEvent;
  995. return;
  996. }
  997. if ($sType === 'TEXT_MESSAGE_CONTENT') {
  998. $sDelta = (string)($aPayload['delta'] ?? '');
  999. $aState['buf'] .= $sDelta;
  1000. if ($aState['mode'] === 'text') {
  1001. $fEmit($sEvent);
  1002. return;
  1003. }
  1004. if ($aState['mode'] === 'undecided') {
  1005. $mixed = $this->chatActionsStreamLooksLikeJson($aState['buf']);
  1006. if ($mixed === false) {
  1007. $aState['mode'] = 'text';
  1008. $this->emitHeldChatActionsSse($aState, $fEmit);
  1009. $fEmit($sEvent);
  1010. return;
  1011. }
  1012. if ($mixed === true)
  1013. $aState['mode'] = 'json';
  1014. }
  1015. $aState['held'][] = $sEvent;
  1016. return;
  1017. }
  1018. if ($sType === 'TEXT_MESSAGE_END') {
  1019. $aParsed = $this->parseAssistantChatPayload($aState['buf']);
  1020. if ($aParsed && ($aState['mode'] === 'json' || $aState['mode'] === 'undecided')) {
  1021. $this->emitParsedChatActionsSse($aState, $fEmit, $aParsed);
  1022. return;
  1023. }
  1024. $this->emitHeldChatActionsSse($aState, $fEmit);
  1025. $fEmit($sEvent);
  1026. $aState = $this->resetChatActionsSseState();
  1027. return;
  1028. }
  1029. if ($sType === 'RUN_ERROR' || $sType === 'RUN_FINISHED') {
  1030. $this->flushChatActionsSseState($aState, $fEmit);
  1031. $fEmit($sEvent);
  1032. return;
  1033. }
  1034. if ($aState['mode'] === 'json' || $aState['held'])
  1035. $this->flushChatActionsSseState($aState, $fEmit);
  1036. $fEmit($sEvent);
  1037. }
  1038. protected function chatActionsStreamLooksLikeJson($sBuf)
  1039. {
  1040. $s = ltrim((string)$sBuf);
  1041. if ($s === '')
  1042. return null;
  1043. $sFirst = $s[0];
  1044. if ($sFirst === '{' || $sFirst === '`')
  1045. return true;
  1046. return false;
  1047. }
  1048. protected function flushChatActionsSseState(&$aState, $fEmit)
  1049. {
  1050. if ($aState['buf'] === '' && !$aState['held'])
  1051. return;
  1052. $aParsed = $this->parseAssistantChatPayload($aState['buf']);
  1053. if ($aParsed && $aState['mode'] !== 'text') {
  1054. $this->emitParsedChatActionsSse($aState, $fEmit, $aParsed);
  1055. return;
  1056. }
  1057. $this->emitHeldChatActionsSse($aState, $fEmit);
  1058. $aState = $this->resetChatActionsSseState();
  1059. }
  1060. protected function persistAssistantChatActions($oAgent)
  1061. {
  1062. if (!is_object($oAgent) || !method_exists($oAgent, 'getChatHistory'))
  1063. return;
  1064. $oHistory = $oAgent->getChatHistory();
  1065. if (!($oHistory instanceof BxDolAiChatHistory) || !method_exists($oHistory, 'persistMessages'))
  1066. return;
  1067. $aMessages = $oHistory->getMessages();
  1068. for ($i = count($aMessages) - 1; $i >= 0; $i--) {
  1069. $oMessage = $aMessages[$i];
  1070. if (
  1071. $oMessage instanceof NeuronAI\Chat\Messages\ToolCallMessage
  1072. || $oMessage instanceof NeuronAI\Chat\Messages\ToolResultMessage
  1073. ) {
  1074. continue;
  1075. }
  1076. $sRole = $oMessage->getRole();
  1077. if ($sRole === 'model')
  1078. $sRole = 'assistant';
  1079. if ($sRole !== 'assistant')
  1080. return;
  1081. $aParsed = $this->parseAssistantChatPayload((string)$oMessage->getContent());
  1082. if (!$aParsed)
  1083. return;
  1084. $oMessage->setContents($aParsed['content']);
  1085. $oMessage->addMetadata('actions', $aParsed['actions']);
  1086. $oHistory->persistMessages();
  1087. return;
  1088. }
  1089. }
  1090. public function streamAgentChat($iAgentId, $sPrompt, $aParams = [], $sThreadId = null)
  1091. {
  1092. if (!isset($aParams['chat_history_subindex']))
  1093. $aParams = array_merge($this->resolveChatHistoryParams($iAgentId), $aParams);
  1094. // Staging header.inc.php dumps HTML on fatals and warns on null error_get_last().
  1095. // Any HTML after SSE is parsed by TanStack as "unterminated trailing data".
  1096. @ini_set('display_errors', '0');
  1097. @ini_set('zlib.output_compression', '0');
  1098. @ini_set('implicit_flush', '1');
  1099. if (function_exists('apache_setenv'))
  1100. @apache_setenv('no-gzip', '1');
  1101. while (ob_get_level())
  1102. ob_end_clean();
  1103. // UNA BxDolDb::pdoExceptionHandler dumps HTML 503 on uncaught PDOException.
  1104. restore_exception_handler();
  1105. $oAdapter = new NeuronAI\Chat\Messages\Stream\Adapters\AGUIAdapter($sThreadId);
  1106. http_response_code(200);
  1107. foreach ($oAdapter->getHeaders() as $sName => $sValue)
  1108. header($sName . ': ' . $sValue);
  1109. $fEmit = function ($sEvent) {
  1110. echo $sEvent;
  1111. if (ob_get_level())
  1112. ob_flush();
  1113. flush();
  1114. };
  1115. $aAgent = BxDolAIQuery::getAgentObject((int)$iAgentId);
  1116. if ($aAgent)
  1117. $this->_aChatContext = ['agent' => $aAgent, 'params' => $aParams];
  1118. if ($aAgent && $this->isChatSessionRateLimited($aAgent, $aParams)) {
  1119. $this->emitChatErrorSse($oAdapter, $fEmit, $this->getChatSessionRateLimitError());
  1120. error_clear_last();
  1121. @ini_set('display_errors', '0');
  1122. exit;
  1123. }
  1124. $iRequestTurns = (int)($aParams['request_user_turns'] ?? 0);
  1125. if ($aAgent && $this->isChatTurnLimitReached($aAgent, $this->getChatUserTurnCount((int)$iAgentId, $aParams), $iRequestTurns)) {
  1126. $this->emitConversationClosed('limit', '', $aAgent, $aParams);
  1127. $this->emitChatLimitSse($oAdapter, $fEmit, $this->getChatLimitMessage($aAgent));
  1128. error_clear_last();
  1129. @ini_set('display_errors', '0');
  1130. exit;
  1131. }
  1132. $sPrompt = $this->applyChatInputLimit((string)$sPrompt, $aAgent ?: []);
  1133. $bStarted = false;
  1134. $aSseState = $this->resetChatActionsSseState();
  1135. $o = null;
  1136. try {
  1137. $o = self::getAgentInstance((int)$iAgentId, $aParams);
  1138. $oHandler = $o->stream(new NeuronAI\Chat\Messages\UserMessage($sPrompt));
  1139. foreach ($oHandler->events($oAdapter) as $sEvent) {
  1140. $bStarted = true;
  1141. $this->processChatActionsSseEvent($sEvent, $aSseState, $fEmit);
  1142. }
  1143. $this->flushChatActionsSseState($aSseState, $fEmit);
  1144. $this->persistAssistantChatActions($o);
  1145. if ($aAgent && $this->isChatTurnLimitReached($aAgent, $this->getChatUserTurnCount((int)$iAgentId, $aParams)))
  1146. $this->emitConversationClosed('limit', '', $aAgent, $aParams);
  1147. } catch (Throwable $oException) {
  1148. bx_log('sys_agents', "Stream exception for agent {$iAgentId}: " . $oException->getMessage() . " INPUT:" . $sPrompt);
  1149. $this->flushChatActionsSseState($aSseState, $fEmit);
  1150. if (!$bStarted) {
  1151. foreach ($oAdapter->start() as $sEvent)
  1152. $fEmit($sEvent);
  1153. }
  1154. $sMsg = $oException->getMessage();
  1155. $fEmit('data: ' . json_encode(['type' => 'RUN_ERROR', 'message' => $sMsg !== '' ? $sMsg : _t('_sys_agents_exception')]) . "\n\n");
  1156. foreach ($oAdapter->end() as $sEvent)
  1157. $fEmit($sEvent);
  1158. }
  1159. error_clear_last();
  1160. @ini_set('display_errors', '0');
  1161. exit;
  1162. }
  1163. /**
  1164. * SSE assistant reply without calling the model. Used when max_turns is hit.
  1165. */
  1166. protected function emitChatLimitSse($oAdapter, $fEmit, $sMessage)
  1167. {
  1168. foreach ($oAdapter->start() as $sEvent)
  1169. $fEmit($sEvent);
  1170. $oChunk = new NeuronAI\Chat\Messages\Stream\Chunks\TextChunk('msg_limit', (string)$sMessage);
  1171. foreach ($oAdapter->transform($oChunk) as $sEvent)
  1172. $fEmit($sEvent);
  1173. foreach ($oAdapter->end() as $sEvent)
  1174. $fEmit($sEvent);
  1175. }
  1176. /**
  1177. * SSE error without an assistant bubble. Used when a new session is rate-limited.
  1178. */
  1179. protected function emitChatErrorSse($oAdapter, $fEmit, $sMessage)
  1180. {
  1181. foreach ($oAdapter->start() as $sEvent)
  1182. $fEmit($sEvent);
  1183. $sMsg = trim((string)$sMessage);
  1184. $fEmit('data: ' . json_encode([
  1185. 'type' => 'RUN_ERROR',
  1186. 'message' => $sMsg !== '' ? $sMsg : _t('_sys_agents_exception'),
  1187. ]) . "\n\n");
  1188. foreach ($oAdapter->end() as $sEvent)
  1189. $fEmit($sEvent);
  1190. }
  1191. public function sendMessengerMessage ($iSender, $iRecipient, $sMsg)
  1192. {
  1193. $oMessengerModule = BxDolModule::getInstance('bx_messenger');
  1194. $aAutoReplyData = [
  1195. 'message' => $sMsg,
  1196. 'participants' => [$iSender, $iRecipient],
  1197. ];
  1198. $iSaveProfileId = $oMessengerModule->setProfileId($iSender);
  1199. $a = $oMessengerModule->sendMessage($aAutoReplyData, $iRecipient, $iSender);
  1200. $oMessengerModule->setProfileId($iSaveProfileId);
  1201. return $a;
  1202. }
  1203. public function getAgentsByAlertUnitAndAction($sUnit, $sAction)
  1204. {
  1205. $aAgents = [];
  1206. $a = $this->_oDb->getAgentsWithAlert();
  1207. foreach ($a as $r) {
  1208. $aAlert = explode(':', $r['alert']); // TODO: remake to concantenate $sUnit and $sAction and then compare
  1209. if (count($aAlert) == 2 && $aAlert[0] == $sUnit && $aAlert[1] == $sAction)
  1210. $aAgents[] = $r;
  1211. }
  1212. return $aAgents;
  1213. }
  1214. public function getAgentsBy($aParams)
  1215. {
  1216. return $this->_oDb->getAgentsBy($aParams);
  1217. }
  1218. public function getAgentsByProfileId($iProfileId)
  1219. {
  1220. return $this->_oDb->getAgentsByProfileId($iProfileId);
  1221. }
  1222. public function getAgentsByFormObject($sFormObject)
  1223. {
  1224. return $this->_oDb->getAgentsByFormObject($sFormObject);
  1225. }
  1226. public function getAgentsByTriggerType($sTrigger)
  1227. {
  1228. return $this->_oDb->getAgentsByTriggerType($sTrigger);
  1229. }
  1230. public function getAgentById($iId)
  1231. {
  1232. return $this->_oDb->getAgentById($iId);
  1233. }
  1234. public function getAgentByTriggerWebhookKey($sKey)
  1235. {
  1236. return $this->_oDb->getAgentByTriggerWebhookKey($sKey);
  1237. }
  1238. public function evalCode($aAutomator, $aParams = [])
  1239. {
  1240. try {
  1241. $this->_evalCode($aAutomator, $aParams);
  1242. }
  1243. catch (Exception $oException) {
  1244. $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
  1245. }
  1246. catch (Error $oError) {
  1247. $this->log($oError->getFile() . ':' . $oError->getLine() . ' ' . $oError->getMessage());
  1248. }
  1249. }
  1250. public function emulCode($aAutomator, $aParams = [])
  1251. {
  1252. ob_start();
  1253. try {
  1254. $this->_evalCode($aAutomator, $aParams);
  1255. }
  1256. catch (Exception $oException) {
  1257. return $oException->getMessage();
  1258. }
  1259. catch (Error $oError) {
  1260. return $oError->getMessage();
  1261. }
  1262. finally {
  1263. $sOutput = ob_get_clean();
  1264. if(!empty($sOutput))
  1265. return $sOutput;
  1266. }
  1267. }
  1268. public function log($mixedContents, $sSection = '')
  1269. {
  1270. if(!$this->_bWriteLog)
  1271. return;
  1272. if(is_array($mixedContents))
  1273. $mixedContents = var_export($mixedContents, true);
  1274. else if(is_object($mixedContents))
  1275. $mixedContents = json_encode($mixedContents);
  1276. if(empty($sSection))
  1277. $sSection = "Core";
  1278. bx_log('sys_agents', ":\n[" . $sSection . "] " . $mixedContents, BX_LOG_ERR);
  1279. }
  1280. protected function _evalCode($aAutomator, $aParams = [])
  1281. {
  1282. $sCode = '';
  1283. switch($aAutomator['type']) {
  1284. case BX_DOL_AI_AUTOMATOR_EVENT:
  1285. $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
  1286. break;
  1287. case BX_DOL_AI_AUTOMATOR_SCHEDULER:
  1288. $sCode = $aAutomator['code'] . '; onCron();';
  1289. break;
  1290. case BX_DOL_AI_AUTOMATOR_WEBHOOK:
  1291. $sCode = $aAutomator['code'] . '; onHook();';
  1292. break;
  1293. }
  1294. eval($sCode);
  1295. }
  1296. }