🤯 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: onFinish never called when browser tab is backgrounded during SSE streaming (#14461)
Hardya9d21105656e
diff --git a/packages/fetch-sse/src/__tests__/fetchSSE.test.ts b/packages/fetch-sse/src/__tests__/fetchSSE.test.tsindex 7b19c6318d9..963e3434b74 100644--- a/packages/fetch-sse/src/__tests__/fetchSSE.test.ts+++ b/packages/fetch-sse/src/__tests__/fetchSSE.test.ts@@ -140,25 +140,10 @@ describe('fetchSSE', () => {responseAnimation: 'smooth',});- const expectedMessages = [- { text: 'H', type: 'text' },- { text: 'e', type: 'text' },- { text: 'l', type: 'text' },- { text: 'l', type: 'text' },- { text: 'o', type: 'text' },- { text: ' ', type: 'text' },- { text: 'W', type: 'text' },- { text: 'o', type: 'text' },- { text: 'r', type: 'text' },- { text: 'l', type: 'text' },- { text: 'd', type: 'text' },- ];-- expectedMessages.forEach((message, index) => {- expect(mockOnMessageHandle).toHaveBeenNthCalledWith(index + 1, message);- });-- // more assertions for each character...+ const allCalls = mockOnMessageHandle.mock.calls.map((call) => call[0]);+ const allTexts = allCalls.filter((c) => c.type === 'text').map((c) => c.text);+ expect(allTexts.join('')).toBe('Hello World');+expect(mockOnFinish).toHaveBeenCalledWith('Hello World', {observationId: null,toolCalls: undefined,@@ -481,23 +466,9 @@ describe('fetchSSE', () => {responseAnimation: 'smooth',});- const expectedMessages = [- { text: 'H', type: 'text' },- { text: 'e', type: 'text' },- { text: 'l', type: 'text' },- { text: 'l', type: 'text' },- { text: 'o', type: 'text' },- { text: ' ', type: 'text' },- { text: 'W', type: 'text' },- { text: 'o', type: 'text' },- { text: 'r', type: 'text' },- { text: 'l', type: 'text' },- { text: 'd', type: 'text' },- ];-- expectedMessages.forEach((message, index) => {- expect(mockOnMessageHandle).toHaveBeenNthCalledWith(index + 1, message);- });+ const allCalls = mockOnMessageHandle.mock.calls.map((call) => call[0]);+ const allTexts = allCalls.filter((c) => c.type === 'text').map((c) => c.text);+ expect(allTexts.join('')).toBe('Hello World');expect(mockOnFinish).toHaveBeenCalledWith('Hello World', {type: 'done',diff --git a/packages/fetch-sse/src/fetchSSE.ts b/packages/fetch-sse/src/fetchSSE.tsindex e3445fe1387..b144845e676 100644--- a/packages/fetch-sse/src/fetchSSE.ts+++ b/packages/fetch-sse/src/fetchSSE.ts@@ -218,7 +218,15 @@ const createSmoothMessage = (params: {outputQueue.push(...text.split(''));};+ const flushQueue = () => {+ if (outputQueue.length === 0) return;+ const remaining = outputQueue.splice(0).join('');+ buffer += remaining;+ params.onTextUpdate(remaining, buffer);+ };+return {+ flushQueue,isAnimationActive,isTokenRemain: () => outputQueue.length > 0,pushToQueue,@@ -510,6 +518,7 @@ export const fetchSSE = async (url: string, options: RequestInit & FetchSSEOptio// so like abort, we don't need to call onFinishif (response) {textController.stopAnimation();+ thinkingController.stopAnimation();// Ensure all buffered data is processedif (bufferTimer) {@@ -532,9 +541,8 @@ export const fetchSSE = async (url: string, options: RequestInit & FetchSSEOptioconst traceId = response.headers.get(LOBE_CHAT_TRACE_ID);const observationId = response.headers.get(LOBE_CHAT_OBSERVATION_ID);- if (textController.isTokenRemain()) {- await textController.startAnimation(smoothingSpeed);- }+ textController.flushQueue();+ thinkingController.flushQueue();await options?.onFinish?.(output, {grounding,