BxDolAiTriggerChat.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. class BxDolAiTriggerChat extends BxDolAiTrigger
  10. {
  11. protected $_sType = 'manual';
  12. protected function appliesChatLimits()
  13. {
  14. return true;
  15. }
  16. /**
  17. * HTTP/SSE only. GET hydrates transcript; POST streams (or falls back to call()).
  18. */
  19. public function handle($mixed = null)
  20. {
  21. $iAgentId = (int)$mixed;
  22. $sMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
  23. if ('GET' === $sMethod)
  24. return $this->hydrate($iAgentId);
  25. if ('POST' !== $sMethod)
  26. return echoJson(['code' => 405, 'msg' => _t('_error occured')]);
  27. $aAgent = $this->loadAgent($iAgentId);
  28. if (!$aAgent)
  29. return echoJson(['code' => $iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found')]);
  30. $oAi = $this->getAi();
  31. if (!$oAi)
  32. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception')]);
  33. if (!$oAi->canChatDirectly($aAgent))
  34. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  35. $sJson = file_get_contents('php://input');
  36. $aData = json_decode($sJson, true);
  37. if (json_last_error() !== JSON_ERROR_NONE)
  38. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  39. $aTurn = $this->extractUserTurnFromRequest($aData);
  40. $sPrompt = BxDolAiChatLimits::getInstance()->applyChatInputLimit($aTurn['text'], $aAgent);
  41. $oMessage = BxDolAiChatImages::getInstance()->makeUserMessage($sPrompt, $aTurn['images']);
  42. if (!$oMessage)
  43. return echoJson(['code' => 400, 'msg' => _t('_sys_agents_json_field_err')]);
  44. $aParams = $this->historyParams($aAgent);
  45. if (!$aParams)
  46. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized')]);
  47. $aParams['request_user_turns'] = BxDolAiChatLimits::getInstance()->countChatUserTurns($aData['messages'] ?? []);
  48. $sThreadId = !empty($aData['threadId']) ? $aData['threadId'] : null;
  49. if (class_exists('NeuronAI\Chat\Messages\Stream\Adapters\AGUIAdapter')) {
  50. $this->stream($aAgent['id'], $oMessage, $aParams, $sThreadId);
  51. return;
  52. }
  53. try {
  54. $sReply = $this->call($aAgent, $sPrompt !== '' ? $sPrompt : ' ');
  55. } catch (Throwable $o) {
  56. return echoJson(['code' => 500, 'msg' => $o->getMessage(), 'messages' => []]);
  57. }
  58. return echoJson(['code' => 200, 'msg' => $sReply, 'text' => $sReply]);
  59. }
  60. /**
  61. * Composer image upload (`system/ai_chat_upload`): one `$_FILES['file']` into the chat images storage.
  62. *
  63. * @return array{file_id:int,url:string,mime:string}|array{error:string,code?:int}
  64. */
  65. public function upload($iAgentId)
  66. {
  67. $aAgent = $this->loadAgent($iAgentId);
  68. if (!$aAgent)
  69. return ['error' => _t('_sys_agents_agent_not_found'), 'code' => (int)$iAgentId ? 404 : 400];
  70. $oAi = $this->getAi();
  71. if (!$oAi || !$oAi->canChatDirectly($aAgent))
  72. return ['error' => _t('_sys_agents_unauthorized'), 'code' => 403];
  73. $aParams = $this->historyParams($aAgent);
  74. if (!$aParams)
  75. return ['error' => _t('_sys_agents_unauthorized'), 'code' => 403];
  76. $aFile = $_FILES['file'] ?? null;
  77. if (!is_array($aFile))
  78. return ['error' => 'No file'];
  79. $aStored = BxDolAiChatImages::getInstance()->storeUpload($aFile, (int)bx_get_logged_profile_id());
  80. if (!empty($aStored['error']))
  81. return ['error' => $aStored['error']];
  82. return $aStored;
  83. }
  84. /**
  85. * The caller's own conversations with an agent (`system/get_ai_chat_threads`).
  86. *
  87. * @return array{threads:array}|array{error:string,code:int}
  88. */
  89. public function listThreads($iAgentId)
  90. {
  91. $aAgent = $this->loadAgent($iAgentId);
  92. if (!$aAgent)
  93. return ['error' => _t('_sys_agents_agent_not_found'), 'code' => (int)$iAgentId ? 404 : 400];
  94. $oAi = $this->getAi();
  95. if (!$oAi || !$oAi->canChatDirectly($aAgent))
  96. return ['error' => _t('_sys_agents_unauthorized'), 'code' => 403];
  97. return ['threads' => BxDolAiChat::getInstance()->listOwnAgentChatThreads($aAgent)];
  98. }
  99. /**
  100. * One of the caller's earlier conversations, read-only (`system/get_ai_chat_thread`):
  101. * the transcript in the same UI message shape hydrate returns. Does not open or
  102. * close anything — which thread is current is left as it was.
  103. *
  104. * @return array{messages:array,status:string,closed_reason:string,artifacts:array}|array{error:string,code:int}
  105. */
  106. public function getThread($iAgentId, $sThreadId = '')
  107. {
  108. $aAgent = $this->loadAgent($iAgentId);
  109. if (!$aAgent)
  110. return ['error' => _t('_sys_agents_agent_not_found'), 'code' => (int)$iAgentId ? 404 : 400];
  111. $oAi = $this->getAi();
  112. if (!$oAi || !$oAi->canChatDirectly($aAgent))
  113. return ['error' => _t('_sys_agents_unauthorized'), 'code' => 403];
  114. $oChat = BxDolAiChat::getInstance();
  115. $sThreadId = trim((string)$sThreadId);
  116. if ($sThreadId === '' || !$oChat->isOwnAgentChatThread($aAgent, $sThreadId))
  117. return ['error' => _t('_sys_agents_unauthorized'), 'code' => 403];
  118. $aMessages = $oChat->getChatHistoryUiMessagesByThread($aAgent, $sThreadId);
  119. if ($aMessages === false)
  120. return ['error' => _t('_sys_agents_agent_not_found'), 'code' => 404];
  121. $sReason = trim((string)$oChat->getChatHistoryClosedReasonByThread($aAgent, $sThreadId));
  122. return [
  123. 'messages' => is_array($aMessages) ? $aMessages : [],
  124. 'status' => $sReason !== '' ? 'closed' : 'opened',
  125. 'closed_reason' => $sReason,
  126. 'artifacts' => $oChat->getChatHistoryArtifactsByThread($aAgent, $sThreadId),
  127. ];
  128. }
  129. public function extractPromptFromRequest($aData)
  130. {
  131. $aTurn = $this->extractUserTurnFromRequest($aData);
  132. return $aTurn['text'];
  133. }
  134. /**
  135. * Last user turn: text + image URLs from AG-UI `content` / `parts`.
  136. *
  137. * @return array{text:string,images:array<int,array{url:string,mime:string}>}
  138. */
  139. public function extractUserTurnFromRequest($aData)
  140. {
  141. $aEmpty = ['text' => '', 'images' => []];
  142. if (!is_array($aData))
  143. return $aEmpty;
  144. $sPrompt = (!empty($aData['prompt']) && is_string($aData['prompt'])) ? trim($aData['prompt']) : '';
  145. if (empty($aData['messages']) || !is_array($aData['messages']))
  146. return $sPrompt !== '' ? ['text' => $sPrompt, 'images' => []] : $aEmpty;
  147. $oImages = BxDolAiChatImages::getInstance();
  148. for ($i = count($aData['messages']) - 1; $i >= 0; $i--) {
  149. $aMessage = $aData['messages'][$i];
  150. if (!is_array($aMessage) || ($aMessage['role'] ?? '') !== 'user')
  151. continue;
  152. $aParts = [];
  153. if (!empty($aMessage['parts']) && is_array($aMessage['parts']))
  154. $aParts = array_merge($aParts, $aMessage['parts']);
  155. if (isset($aMessage['content']) && is_array($aMessage['content']))
  156. $aParts = array_merge($aParts, $aMessage['content']);
  157. $aText = [];
  158. $aImages = [];
  159. foreach ($aParts as $aPart) {
  160. if (!is_array($aPart))
  161. continue;
  162. $sType = strtolower((string)($aPart['type'] ?? ''));
  163. if ($sType === 'text') {
  164. $s = trim((string)($aPart['content'] ?? $aPart['text'] ?? ''));
  165. if ($s !== '')
  166. $aText[] = $s;
  167. continue;
  168. }
  169. $aImage = $oImages->parsePart($aPart);
  170. if ($aImage)
  171. $aImages[] = $aImage;
  172. }
  173. if (!$aParts && isset($aMessage['content']) && is_string($aMessage['content']))
  174. $aText[] = trim($aMessage['content']);
  175. $sText = trim(implode("\n", $aText));
  176. if ($sText === '' && $sPrompt !== '')
  177. $sText = $sPrompt;
  178. return [
  179. 'text' => $sText,
  180. 'images' => array_slice($aImages, 0, BxDolAiChatImages::MAX_PER_TURN),
  181. ];
  182. }
  183. return $aEmpty;
  184. }
  185. /**
  186. * @param string|NeuronAI\Chat\Messages\UserMessage $mixedPrompt plain text, or a ready user message (text + images)
  187. */
  188. public function stream($iAgentId, $mixedPrompt, $aParams = [], $sThreadId = null)
  189. {
  190. $oChat = BxDolAiChat::getInstance();
  191. $oLimits = BxDolAiChatLimits::getInstance();
  192. if (!isset($aParams['chat_history_subindex']))
  193. $aParams = array_merge($oChat->resolveChatHistoryParams($iAgentId), $aParams);
  194. // Staging header.inc.php dumps HTML on fatals and warns on null error_get_last().
  195. // Any HTML after SSE is parsed by TanStack as "unterminated trailing data".
  196. @ini_set('display_errors', '0');
  197. @ini_set('zlib.output_compression', '0');
  198. @ini_set('implicit_flush', '1');
  199. if (function_exists('apache_setenv'))
  200. @apache_setenv('no-gzip', '1');
  201. while (ob_get_level())
  202. ob_end_clean();
  203. // UNA BxDolDb::pdoExceptionHandler dumps HTML 503 on uncaught PDOException.
  204. restore_exception_handler();
  205. $oAdapter = new NeuronAI\Chat\Messages\Stream\Adapters\AGUIAdapter($sThreadId);
  206. http_response_code(200);
  207. foreach ($oAdapter->getHeaders() as $sName => $sValue)
  208. header($sName . ': ' . $sValue);
  209. $fEmit = function ($sEvent) {
  210. echo $sEvent;
  211. if (ob_get_level())
  212. ob_flush();
  213. flush();
  214. };
  215. $aAgent = BxDolAiQuery::getAgentObject((int)$iAgentId);
  216. if ($aAgent)
  217. $oChat->setChatContext($aAgent, $aParams);
  218. if ($aAgent && $oLimits->isChatSessionRateLimited($aAgent, $aParams)) {
  219. $this->emitErrorSse($oAdapter, $fEmit, $oLimits->getChatSessionRateLimitError());
  220. error_clear_last();
  221. @ini_set('display_errors', '0');
  222. exit;
  223. }
  224. if ($aAgent)
  225. $oChat->openPartitionedChatThread($aAgent, $aParams);
  226. $iRequestTurns = (int)($aParams['request_user_turns'] ?? 0);
  227. if ($aAgent && $oLimits->isChatTurnLimitReached($aAgent, $oLimits->getChatUserTurnCount((int)$iAgentId, $aParams), $iRequestTurns)) {
  228. $oChat->emitConversationClosed('limit', '', $aAgent, $aParams);
  229. $this->emitLimitSse($oAdapter, $fEmit, $oLimits->getChatLimitMessage($aAgent));
  230. error_clear_last();
  231. @ini_set('display_errors', '0');
  232. exit;
  233. }
  234. $oUserMessage = $mixedPrompt instanceof NeuronAI\Chat\Messages\UserMessage
  235. ? $mixedPrompt
  236. : BxDolAiChatImages::getInstance()->makeUserMessage($oLimits->applyChatInputLimit((string)$mixedPrompt, $aAgent ?: []), []);
  237. $sPrompt = $oUserMessage ? (string)$oUserMessage->getContent() : '';
  238. $oChat->resetPendingChatActions();
  239. $bStarted = false;
  240. $aSseState = $this->resetActionsSseState();
  241. $o = null;
  242. try {
  243. if (!$oUserMessage)
  244. throw new Exception('Empty chat message');
  245. $o = BxDolAi::getAgentInstance((int)$iAgentId, $aParams);
  246. $oHandler = $o->stream($oUserMessage);
  247. foreach ($oHandler->events($oAdapter) as $sChunk) {
  248. $bStarted = true;
  249. foreach ($this->splitSseEvents($sChunk) as $sEvent) {
  250. $aPayload = $this->parseSseEvent($sEvent);
  251. $sType = is_array($aPayload) ? (string)($aPayload['type'] ?? '') : '';
  252. // Buttons queued by tools must reach the client before RUN_FINISHED.
  253. if ($sType === 'RUN_FINISHED')
  254. $this->finishActionsSse($o, $aSseState, $fEmit);
  255. $this->processActionsSseEvent($sEvent, $aSseState, $fEmit);
  256. }
  257. }
  258. $this->flushActionsSseState($aSseState, $fEmit);
  259. if (empty($aSseState['customEmitted']))
  260. $this->finishActionsSse($o, $aSseState, $fEmit);
  261. if ($aAgent && $oLimits->isChatTurnLimitReached($aAgent, $oLimits->getChatUserTurnCount((int)$iAgentId, $aParams)))
  262. $oChat->emitConversationClosed('limit', '', $aAgent, $aParams);
  263. } catch (Throwable $oException) {
  264. bx_log('sys_agents', "Stream exception for agent {$iAgentId}: " . $oException->getMessage() . " INPUT:" . $sPrompt);
  265. $this->flushActionsSseState($aSseState, $fEmit);
  266. if (!$bStarted) {
  267. foreach ($oAdapter->start() as $sEvent)
  268. $fEmit($sEvent);
  269. }
  270. $sMsg = isAdmin() ? self::visibleError($oException) : $oException->getMessage();
  271. $fEmit('data: ' . json_encode(['type' => 'RUN_ERROR', 'message' => $sMsg !== '' ? $sMsg : _t('_sys_agents_exception')]) . "\n\n");
  272. foreach ($oAdapter->end() as $sEvent)
  273. $fEmit($sEvent);
  274. }
  275. error_clear_last();
  276. @ini_set('display_errors', '0');
  277. exit;
  278. }
  279. protected function hydrate($iAgentId)
  280. {
  281. if (false !== bx_get('offset'))
  282. return echoJson(['code' => 405, 'msg' => _t('_error occured'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  283. $aAgent = $this->loadAgent($iAgentId);
  284. if (!$aAgent)
  285. return echoJson(['code' => (int)$iAgentId ? 404 : 400, 'msg' => _t('_sys_agents_agent_not_found'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  286. $oAi = $this->getAi();
  287. if (!$oAi)
  288. return echoJson(['code' => 503, 'msg' => _t('_sys_agents_exception'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  289. if (!$oAi->canChatDirectly($aAgent))
  290. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  291. $aParams = $this->historyParams($aAgent);
  292. if (!$aParams)
  293. return echoJson(['code' => 403, 'msg' => _t('_sys_agents_unauthorized'), 'messages' => [], 'activeRun' => null, 'interrupts' => null]);
  294. $oChat = BxDolAiChat::getInstance();
  295. $oChat->openPartitionedChatThread($aAgent, $aParams);
  296. try {
  297. $aMessages = $oChat->getChatHistoryUiMessages($aAgent['id'], $aParams);
  298. } catch (Throwable $o) {
  299. $aMessages = [];
  300. }
  301. if (!is_array($aMessages))
  302. $aMessages = [];
  303. return echoJson([
  304. 'messages' => $aMessages,
  305. 'activeRun' => null,
  306. 'interrupts' => null,
  307. ]);
  308. }
  309. /**
  310. * Member/guest subindex + optional `?context=` (group/org profile id).
  311. * Invalid context is 403 — never fall back to the site-wide thread.
  312. * Without `?chat=`, follow the owner's current open thread (see BxDolAiChat::resolveCurrentChatThread).
  313. */
  314. protected function historyParams($aAgent)
  315. {
  316. $oChat = BxDolAiChat::getInstance();
  317. $mixedContext = $oChat->resolveChatHistoryContextPid();
  318. if ($mixedContext === false)
  319. return false;
  320. $aParams = $oChat->resolveChatHistoryParams((int)$aAgent['id']);
  321. $aParams['chat_history_context_pid'] = (int)$mixedContext;
  322. return $oChat->resolveCurrentChatThread($aAgent, $aParams);
  323. }
  324. protected function loadAgent($iAgentId)
  325. {
  326. $iAgentId = (int)$iAgentId;
  327. if (!$iAgentId)
  328. return false;
  329. $aAgent = BxDolAiQuery::getAgentObject($iAgentId);
  330. if (!$aAgent || empty($aAgent['active']))
  331. return false;
  332. return $aAgent;
  333. }
  334. protected function sseEvent($aPayload)
  335. {
  336. return 'data: ' . json_encode($aPayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n\n";
  337. }
  338. /**
  339. * One adapter chunk may carry several SSE frames (`data: …\n\n` ×N). Split so each is processed on its own.
  340. *
  341. * @return string[]
  342. */
  343. protected function splitSseEvents($sChunk)
  344. {
  345. $s = trim(str_replace(["\r\n", "\r"], "\n", (string)$sChunk));
  346. if ($s === '')
  347. return [$sChunk];
  348. $aOut = [];
  349. foreach (preg_split('/\n\s*\n/', $s) as $sFrame) {
  350. $sFrame = trim($sFrame);
  351. if ($sFrame !== '')
  352. $aOut[] = $sFrame . "\n\n";
  353. }
  354. return $aOut ?: [$sChunk];
  355. }
  356. protected function parseSseEvent($sEvent)
  357. {
  358. $s = str_replace(["\r\n", "\r"], "\n", (string)$sEvent);
  359. $aLast = null;
  360. foreach (preg_split('/\n\s*\n/', trim($s)) as $sFrame) {
  361. $aData = [];
  362. foreach (explode("\n", $sFrame) as $sLine) {
  363. if (stripos($sLine, 'data:') === 0)
  364. $aData[] = ltrim(substr($sLine, 5));
  365. }
  366. if (!$aData)
  367. continue;
  368. $a = json_decode(implode("\n", $aData), true);
  369. if (is_array($a))
  370. $aLast = $a;
  371. }
  372. return $aLast;
  373. }
  374. /**
  375. * `lastMessageId` / `lastText` remember the assistant message just finished (so
  376. * buttons queued by a tool can be attached to it); `customEmitted` — the
  377. * `chat_actions` event went out for this turn already.
  378. */
  379. protected function resetActionsSseState($aKeep = [])
  380. {
  381. return array_merge([
  382. 'mode' => 'undecided',
  383. 'buf' => '',
  384. 'messageId' => '',
  385. 'held' => [],
  386. 'lastMessageId' => '',
  387. 'lastText' => '',
  388. 'customEmitted' => false,
  389. ], $aKeep);
  390. }
  391. protected function rememberActionsSseTurn(&$aState, $sMessageId = '', $sText = '', $bCustom = null)
  392. {
  393. $aState = $this->resetActionsSseState([
  394. 'lastMessageId' => $sMessageId !== '' ? $sMessageId : (string)($aState['lastMessageId'] ?? ''),
  395. 'lastText' => $sText !== '' ? $sText : (string)($aState['lastText'] ?? ''),
  396. 'customEmitted' => $bCustom === null ? !empty($aState['customEmitted']) : (bool)$bCustom,
  397. ]);
  398. }
  399. protected function emitHeldActionsSse(&$aState, $fEmit)
  400. {
  401. foreach ($aState['held'] as $sHeld)
  402. $fEmit($sHeld);
  403. $aState['held'] = [];
  404. }
  405. protected function emitParsedActionsSse(&$aState, $fEmit, $aParsed)
  406. {
  407. $sId = $aState['messageId'] !== '' ? $aState['messageId'] : ('msg_' . uniqid());
  408. $fEmit($this->sseEvent([
  409. 'type' => 'TEXT_MESSAGE_START',
  410. 'messageId' => $sId,
  411. 'role' => 'assistant',
  412. ]));
  413. if ($aParsed['content'] !== '') {
  414. $fEmit($this->sseEvent([
  415. 'type' => 'TEXT_MESSAGE_CONTENT',
  416. 'messageId' => $sId,
  417. 'delta' => $aParsed['content'],
  418. ]));
  419. }
  420. $fEmit($this->sseEvent([
  421. 'type' => 'TEXT_MESSAGE_END',
  422. 'messageId' => $sId,
  423. ]));
  424. $bCustom = false;
  425. if (!empty($aParsed['actions']))
  426. $bCustom = $this->emitActionsCustomSse($sId, $aParsed['actions'], $fEmit);
  427. $aState = $this->resetActionsSseState([
  428. 'lastMessageId' => $sId,
  429. 'lastText' => (string)($aParsed['content'] ?? ''),
  430. 'customEmitted' => !empty($aState['customEmitted']) || $bCustom,
  431. ]);
  432. if (empty($aState['customEmitted']))
  433. $this->emitPendingActionsSse($aState, $fEmit, $aParsed['actions'] ?? []);
  434. }
  435. /**
  436. * `chat_actions` custom event: `messageId` + `value` (the buttons) — what the App chat widget reads.
  437. */
  438. protected function emitActionsCustomSse($sMessageId, $aActions, $fEmit)
  439. {
  440. $aActions = BxDolAiChatUi::getInstance()->sanitizeChatActions($aActions);
  441. if (!$aActions)
  442. return false;
  443. $fEmit($this->sseEvent([
  444. 'type' => 'CUSTOM',
  445. 'name' => 'chat_actions',
  446. 'messageId' => (string)$sMessageId,
  447. 'value' => $aActions,
  448. ]));
  449. return true;
  450. }
  451. /**
  452. * Buttons queued by the `chat_buttons` tool (plus `$aExtra`), once per turn,
  453. * pinned to the last assistant message.
  454. */
  455. protected function emitPendingActionsSse(&$aState, $fEmit, $aExtra = [])
  456. {
  457. if (!empty($aState['customEmitted']))
  458. return false;
  459. $oChat = BxDolAiChat::getInstance();
  460. $aActions = BxDolAiChatUi::getInstance()->mergeChatActions($oChat->getPendingChatActions(), $aExtra);
  461. if (!$aActions)
  462. return false;
  463. $sMid = (string)($aState['lastMessageId'] ?? '');
  464. if ($sMid === '')
  465. $sMid = (string)($aState['messageId'] ?? '');
  466. if (!$this->emitActionsCustomSse($sMid, $aActions, $fEmit))
  467. return false;
  468. $aState['customEmitted'] = true;
  469. return true;
  470. }
  471. /**
  472. * End of the model turn: flush held text, pin actions to the stored assistant
  473. * message, emit whatever buttons are still pending.
  474. */
  475. protected function finishActionsSse($oAgent, &$aState, $fEmit)
  476. {
  477. $this->flushActionsSseState($aState, $fEmit);
  478. $aDone = $this->persistAssistantActions($oAgent, (string)($aState['lastText'] ?? ''));
  479. if (($aState['lastMessageId'] ?? '') === '' && !empty($aDone['messageId']))
  480. $aState['lastMessageId'] = (string)$aDone['messageId'];
  481. $this->emitPendingActionsSse($aState, $fEmit, $aDone['actions'] ?? []);
  482. }
  483. protected function processActionsSseEvent($sEvent, &$aState, $fEmit)
  484. {
  485. $oUi = BxDolAiChatUi::getInstance();
  486. $aPayload = $this->parseSseEvent($sEvent);
  487. $sType = is_array($aPayload) ? (string)($aPayload['type'] ?? '') : '';
  488. if ($sType === 'TEXT_MESSAGE_START') {
  489. if ($aState['held'] || $aState['buf'] !== '')
  490. $this->flushActionsSseState($aState, $fEmit);
  491. $aState['messageId'] = (string)($aPayload['messageId'] ?? '');
  492. $aState['held'][] = $sEvent;
  493. return;
  494. }
  495. if ($sType === 'TEXT_MESSAGE_CONTENT') {
  496. $sDelta = (string)($aPayload['delta'] ?? '');
  497. $aState['buf'] .= $sDelta;
  498. BxDolAiChat::getInstance()->markTurnText($sDelta);
  499. if ($aState['mode'] === 'text') {
  500. $fEmit($sEvent);
  501. return;
  502. }
  503. if ($aState['mode'] === 'undecided') {
  504. $mixed = $this->actionsStreamLooksLikeJson($aState['buf']);
  505. if ($mixed === false) {
  506. $aState['mode'] = 'text';
  507. $this->emitHeldActionsSse($aState, $fEmit);
  508. $fEmit($sEvent);
  509. return;
  510. }
  511. if ($mixed === true)
  512. $aState['mode'] = 'json';
  513. }
  514. $aState['held'][] = $sEvent;
  515. return;
  516. }
  517. if ($sType === 'TEXT_MESSAGE_END') {
  518. $aParsed = $oUi->parseAssistantChatPayload($aState['buf']);
  519. if ($aParsed && ($aState['mode'] === 'json' || $aState['mode'] === 'undecided')) {
  520. $this->emitParsedActionsSse($aState, $fEmit, $aParsed);
  521. return;
  522. }
  523. $sId = (string)($aState['messageId'] ?? '');
  524. $sText = (string)($aState['buf'] ?? '');
  525. $this->emitHeldActionsSse($aState, $fEmit);
  526. $fEmit($sEvent);
  527. $this->rememberActionsSseTurn($aState, $sId, $sText);
  528. $this->emitPendingActionsSse($aState, $fEmit);
  529. return;
  530. }
  531. if ($sType === 'RUN_ERROR' || $sType === 'RUN_FINISHED') {
  532. $this->flushActionsSseState($aState, $fEmit);
  533. $fEmit($sEvent);
  534. return;
  535. }
  536. // Tool call frames are internal: not forwarded, but they end the text so far.
  537. if ($sType === 'TOOL_CALL_START' || $sType === 'TOOL_CALL_ARGS' || $sType === 'TOOL_CALL_END' || $sType === 'TOOL_CALL_RESULT') {
  538. if ($aState['mode'] === 'json' || $aState['held'] || $aState['buf'] !== '')
  539. $this->flushActionsSseState($aState, $fEmit);
  540. return;
  541. }
  542. if ($aState['mode'] === 'json' || $aState['held'])
  543. $this->flushActionsSseState($aState, $fEmit);
  544. $fEmit($sEvent);
  545. }
  546. protected function actionsStreamLooksLikeJson($sBuf)
  547. {
  548. $s = ltrim((string)$sBuf);
  549. if ($s === '')
  550. return null;
  551. $sFirst = $s[0];
  552. if ($sFirst === '{' || $sFirst === '`')
  553. return true;
  554. return false;
  555. }
  556. protected function flushActionsSseState(&$aState, $fEmit)
  557. {
  558. if ($aState['buf'] === '' && !$aState['held'])
  559. return;
  560. $aParsed = BxDolAiChatUi::getInstance()->parseAssistantChatPayload($aState['buf']);
  561. if ($aParsed && $aState['mode'] !== 'text') {
  562. $this->emitParsedActionsSse($aState, $fEmit, $aParsed);
  563. return;
  564. }
  565. $sId = (string)($aState['messageId'] ?? '');
  566. $sText = (string)($aState['buf'] ?? '');
  567. $this->emitHeldActionsSse($aState, $fEmit);
  568. $this->rememberActionsSseTurn($aState, $sId, $sText);
  569. }
  570. /**
  571. * Pin this turn's actions (JSON payload and/or `chat_buttons` tool) to the last
  572. * assistant message in history, so hydrate shows them again.
  573. *
  574. * @return array{messageId: string, actions: array}
  575. */
  576. protected function persistAssistantActions($oAgent, $sThisTurnText = '')
  577. {
  578. $oUi = BxDolAiChatUi::getInstance();
  579. $aEmpty = ['messageId' => '', 'actions' => []];
  580. $aPending = BxDolAiChat::getInstance()->takePendingChatActions();
  581. if (!is_object($oAgent) || !method_exists($oAgent, 'getChatHistory'))
  582. return ['messageId' => '', 'actions' => $aPending];
  583. $oHistory = $oAgent->getChatHistory();
  584. if (!is_object($oHistory) || !method_exists($oHistory, 'getMessages'))
  585. return ['messageId' => '', 'actions' => $aPending];
  586. $aMessages = $oHistory->getMessages();
  587. for ($i = count($aMessages) - 1; $i >= 0; $i--) {
  588. $oMessage = $aMessages[$i];
  589. if ($oMessage instanceof NeuronAI\Chat\Messages\ToolResultMessage)
  590. continue;
  591. // When the model obeys "do not write another message after chat_buttons"
  592. // the turn ends on the ToolCallMessage: pin the buttons to it, or they
  593. // would be lost from history.
  594. $sRole = $oMessage->getRole();
  595. if ($sRole === 'model' || $oMessage instanceof NeuronAI\Chat\Messages\ToolCallMessage)
  596. $sRole = 'assistant';
  597. if ($sRole !== 'assistant')
  598. return ['messageId' => '', 'actions' => $aPending];
  599. $sStored = (string)$oMessage->getContent();
  600. $aParsed = $oUi->parseAssistantChatPayload($sStored);
  601. if (!$aParsed && $sThisTurnText !== '')
  602. $aParsed = $oUi->parseAssistantChatPayload($sThisTurnText);
  603. $aActions = $oUi->mergeChatActions($aPending, is_array($aParsed) ? ($aParsed['actions'] ?? []) : []);
  604. if (!$aActions)
  605. return $aEmpty;
  606. if ($aParsed && method_exists($oMessage, 'setContents'))
  607. $oMessage->setContents($aParsed['content']);
  608. if (method_exists($oMessage, 'addMetadata'))
  609. $oMessage->addMetadata('actions', $aActions);
  610. if (method_exists($oHistory, 'persistMessages'))
  611. $oHistory->persistMessages();
  612. $sId = method_exists($oMessage, 'getMetadata') ? $oMessage->getMetadata('__id') : '';
  613. return [
  614. 'messageId' => is_string($sId) ? $sId : '',
  615. 'actions' => $aActions,
  616. ];
  617. }
  618. return ['messageId' => '', 'actions' => $aPending];
  619. }
  620. /**
  621. * SSE assistant reply without calling the model. Used when max_turns is hit.
  622. */
  623. protected function emitLimitSse($oAdapter, $fEmit, $sMessage)
  624. {
  625. foreach ($oAdapter->start() as $sEvent)
  626. $fEmit($sEvent);
  627. $oChunk = new NeuronAI\Chat\Messages\Stream\Chunks\TextChunk('msg_limit', (string)$sMessage);
  628. foreach ($oAdapter->transform($oChunk) as $sEvent)
  629. $fEmit($sEvent);
  630. foreach ($oAdapter->end() as $sEvent)
  631. $fEmit($sEvent);
  632. }
  633. /**
  634. * SSE error without an assistant bubble. Used when a new session is rate-limited.
  635. */
  636. protected function emitErrorSse($oAdapter, $fEmit, $sMessage)
  637. {
  638. foreach ($oAdapter->start() as $sEvent)
  639. $fEmit($sEvent);
  640. $sMsg = trim((string)$sMessage);
  641. $fEmit('data: ' . json_encode([
  642. 'type' => 'RUN_ERROR',
  643. 'message' => $sMsg !== '' ? $sMsg : _t('_sys_agents_exception'),
  644. ]) . "\n\n");
  645. foreach ($oAdapter->end() as $sEvent)
  646. $fEmit($sEvent);
  647. }
  648. }
  649. /** @} */