π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix: pass streamed report text as gateway completion notification body (#16913)
Arvin Xubd28375d2a2d
diff --git a/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.test.ts b/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.test.tsindex 4501d16f291..cb9423930cb 100644--- a/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.test.ts+++ b/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.test.ts@@ -263,4 +263,140 @@ describe('createGatewayEventHandler', () => {visibleLoadingDone: true,});});++ // ββββββββββββββββββββββββββββββββββββββββββββββββββββ+ // Gateway completion notification body+ // ββββββββββββββββββββββββββββββββββββββββββββββββββββ++ const createLifecycle = () => ({+ afterRunComplete: vi.fn().mockResolvedValue(undefined),+ completeRun: vi.fn().mockResolvedValue({ requeued: false }),+ });++ it('passes the streamed report text as the gateway completion notification body', async () => {+ vi.spyOn(messageService, 'getMessages').mockResolvedValue([] as unknown as UIChatMessage[]);+ const lifecycle = createLifecycle();+ const store = createStore();+ const handler = createGatewayEventHandler(() => store, {+ assistantMessageId: 'answer-msg',+ context,+ operationId: 'op-1',+ runLifecycle: lifecycle as any,+ runtimeType: 'gateway',+ });++ // Stream the report text so `accumulatedContent` (the optimistic in-memory+ // source) holds it, then end the run with no terminal uiMessages snapshot.+ handler(makeEvent('stream_chunk', { chunkType: 'text', content: 'The final report.' }));+ handler(makeEvent('agent_runtime_end', { reason: 'completed' }));+ await flush();++ expect(lifecycle.completeRun).toHaveBeenCalledWith(+ expect.objectContaining({ status: 'completed' }),+ );+ expect(lifecycle.afterRunComplete).toHaveBeenCalledWith(+ expect.objectContaining({+ notification: { content: 'The final report.' },+ status: 'completed',+ }),+ );+ });++ it('prefers the terminal snapshot final assistant text over the streamed accumulator', async () => {+ vi.spyOn(messageService, 'getMessages').mockResolvedValue([] as unknown as UIChatMessage[]);+ const lifecycle = createLifecycle();+ const store = createStore();+ const handler = createGatewayEventHandler(() => store, {+ assistantMessageId: 'answer-msg',+ context,+ operationId: 'op-1',+ runLifecycle: lifecycle as any,+ runtimeType: 'gateway',+ });++ // The stream dropped the tail; the server-finalized snapshot has the whole+ // report, and the chat is reconciled to it β so must the notification be.+ handler(makeEvent('stream_chunk', { chunkType: 'text', content: 'The final rep' }));+ handler(+ makeEvent('agent_runtime_end', {+ reason: 'completed',+ uiMessages: [+ { content: 'run it', id: 'user-msg', role: 'user' },+ { content: 'The final report.', id: 'answer-msg', role: 'assistant' },+ ] as unknown as UIChatMessage[],+ }),+ );+ await flush();++ expect(lifecycle.afterRunComplete).toHaveBeenCalledWith(+ expect.objectContaining({ notification: { content: 'The final report.' } }),+ );+ });++ it('falls back to the streamed accumulator when the terminal snapshot carries no assistant text', async () => {+ vi.spyOn(messageService, 'getMessages').mockResolvedValue([] as unknown as UIChatMessage[]);+ const lifecycle = createLifecycle();+ const store = createStore();+ const handler = createGatewayEventHandler(() => store, {+ assistantMessageId: 'answer-msg',+ context,+ operationId: 'op-1',+ runLifecycle: lifecycle as any,+ runtimeType: 'gateway',+ });++ handler(makeEvent('stream_chunk', { chunkType: 'text', content: 'The final report.' }));+ handler(+ makeEvent('agent_runtime_end', {+ reason: 'completed',+ uiMessages: [+ { content: '', id: 'answer-msg', role: 'assistant' },+ ] as unknown as UIChatMessage[],+ }),+ );+ await flush();++ expect(lifecycle.afterRunComplete).toHaveBeenCalledWith(+ expect.objectContaining({ notification: { content: 'The final report.' } }),+ );+ });++ it('passes empty notification content when nothing streamed so afterRunComplete can fall back to the store', async () => {+ vi.spyOn(messageService, 'getMessages').mockResolvedValue([] as unknown as UIChatMessage[]);+ const lifecycle = createLifecycle();+ const store = createStore();+ const handler = createGatewayEventHandler(() => store, {+ assistantMessageId: 'answer-msg',+ context,+ operationId: 'op-1',+ runLifecycle: lifecycle as any,+ runtimeType: 'gateway',+ });++ handler(makeEvent('agent_runtime_end', { reason: 'completed' }));+ await flush();++ expect(lifecycle.afterRunComplete).toHaveBeenCalledWith(+ expect.objectContaining({ notification: { content: '' } }),+ );+ });++ it('does not fire afterRunComplete on a cancelled (non-completed) gateway run', async () => {+ vi.spyOn(messageService, 'getMessages').mockResolvedValue([] as unknown as UIChatMessage[]);+ const lifecycle = createLifecycle();+ const store = createStore();+ const handler = createGatewayEventHandler(() => store, {+ assistantMessageId: 'answer-msg',+ context,+ operationId: 'op-1',+ runLifecycle: lifecycle as any,+ runtimeType: 'gateway',+ });++ handler(makeEvent('stream_chunk', { chunkType: 'text', content: 'partial' }));+ handler(makeEvent('agent_runtime_end', { reason: 'interrupted' }));+ await flush();++ expect(lifecycle.afterRunComplete).not.toHaveBeenCalled();+ });});diff --git a/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.ts b/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.tsindex 84cc922c663..cb8acb9c1d8 100644--- a/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.ts+++ b/src/store/chat/slices/agentRun/actions/transports/gateway/gatewayEventHandler.ts@@ -826,6 +826,10 @@ export const createGatewayEventHandler = (get().internal_toggleToolCallingStreaming(currentAssistantMessageId, undefined);endReasoningIfNeeded();+ // The terminal snapshot, when the server pushed one β the reconciled+ // Source of Truth for this run's final assistant text.+ let terminalMessages: UIChatMessage[] | undefined;+// Reconcile messages FIRST so the terminal run lifecycle's notification// (afterRunComplete) can read the final assistant content from the store.//@@ -834,6 +838,7 @@ export const createGatewayEventHandler = (// to a DB refetch only if the snapshot is absent (older server// builds, or push-event delivery edge cases).if (Array.isArray(data?.uiMessages)) {+ terminalMessages = data.uiMessages;get().replaceMessages(data.uiMessages, {action: 'gateway/agent_runtime_end',context,@@ -879,7 +884,28 @@ export const createGatewayEventHandler = (status,});if (!requeued && status === 'completed') {- await runLifecycle.afterRunComplete({ ...lifecycleEventBase, status });+ // Notification body, resolved most-authoritative first:+ //+ // 1. the terminal snapshot's final assistant text β server-+ // finalized, so it wins over the optimistic stream even when+ // the two disagree (dropped chunks, server-side rewrites);+ // 2. `accumulatedContent`, the in-memory stream (a closure+ // untouched by `replaceMessages`), for the no-snapshot path+ // where `fetchAndReplaceMessages` races the executor's DB+ // write and would otherwise leave the body empty. Its stale+ // predecessor is NOT read back from that refetch: a not-yet-+ // written assistant row would surface the PRIOR turn's reply;+ // 3. nothing (`''`), letting `afterRunComplete` fall back to its+ // store read and then to the generic "generation finished".+ const finalAssistantContent = terminalMessages?.findLast(+ (message) => message.role === 'assistant',+ )?.content;++ await runLifecycle.afterRunComplete({+ ...lifecycleEventBase,+ notification: { content: finalAssistantContent || accumulatedContent },+ status,+ });}} else {// hetero reuses this handler only for message reconciliation; its