The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Stop pacing Metabot text while the browser tab is hidden (#84859)
Paolo Rascunàb4335c2a73db
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.tsindex 0b0beeb6721..ce310b36df6 100644--- a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts+++ b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts@@ -45,6 +45,10 @@ const getMockErrorStream = () =>createMockSSEStream([{ type: "error", errorText: "boom" }]);describe("processChatResponse", () => {+ afterEach(() => {+ jest.restoreAllMocks();+ });+it("should be able to process a valid stream", async () => {const mockStream = getMockSuccessStream();const result = await processChatResponse(mockStream, expectNoStreamedError);@@ -248,6 +252,39 @@ describe("processChatResponse", () => {]);});+ it("does not pace deltas while the document is hidden", async () => {+ jest.spyOn(document, "hidden", "get").mockReturnValue(true);+ const setTimeoutSpy = jest.spyOn(global, "setTimeout");+ const config = getMockedCallbacks();++ await processChatResponse(+ createMockSSEStream([+ { type: "reasoning-start", id: "r1" },+ {+ type: "reasoning-delta",+ id: "r1",+ delta: "let me check the orders table ",+ },+ { type: "reasoning-end", id: "r1" },+ { type: "tool-input-start", toolCallId: "c1", toolName: "search" },+ { type: "text-delta", id: "t1", delta: "Here is the answer." },+ ]),+ config,+ );++ expect(config.onReasoningDelta).toHaveBeenCalledTimes(1);+ expect(config.onReasoningDelta).toHaveBeenCalledWith({+ type: "reasoning-delta",+ id: "r1",+ delta: "let me check the orders table ",+ });+ expect(config.onTextPart.mock.calls).toEqual([["Here is the answer."]]);+ const pacingWaits = setTimeoutSpy.mock.calls.filter(+ ([, delayMs]) => delayMs === 10,+ );+ expect(pacingWaits).toEqual([]);+ });+it("should resolve with partial response for aborted requests", async () => {const partialEvents: SSEEvent[] = [{ type: "text-delta", id: "t1", delta: "Partial response" },diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.tsindex 32d4adcf156..a5270162479 100644--- a/frontend/src/metabase/api/ai-streaming/smooth-stream.ts+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.ts@@ -12,6 +12,13 @@ const DEFAULT_SMOOTHING_DELAY_MS = 10;const wait = (ms: number) =>new Promise<void>((resolve) => setTimeout(resolve, ms));+// Browsers slow timers in hidden tabs: a wait can take one second or more.+// https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#timeouts_in_inactive_tabs+// One wait per word then delays the stream by minutes, so a hidden tab does not pace the text.+// Outside a browser, `document` does not exist, and the text is paced.+const isDocumentHidden = () =>+ typeof document !== "undefined" && document.hidden;+const takeWord = (buffer: string) => {const match = WORD_PATTERN.exec(buffer);return match ? buffer.slice(0, match.index + match[0].length) : null;@@ -26,7 +33,8 @@ export type SmoothTextOptions = { delayInMs?: number };// Re-emits text/reasoning deltas one word at a time on a fixed cadence, so they// render steadily however unevenly the server chunks them. Awaiting between words-// backpressures the stream, which is what paces it.+// backpressures the stream, which is what paces it. While the document is hidden,+// it emits the whole buffer at once and starts no new wait.export async function* smoothStreamEvents(events: AsyncIterable<SSEEvent>,{ delayInMs = DEFAULT_SMOOTHING_DELAY_MS }: SmoothTextOptions = {},@@ -58,12 +66,19 @@ export async function* smoothStreamEvents(pending = event;let word = takeWord(buffer);- while (word !== null) {+ while (word !== null && !isDocumentHidden()) {yield { ...pending, delta: word };buffer = buffer.slice(word.length);+ if (isDocumentHidden()) {+ break;+ }await wait(delayInMs);word = takeWord(buffer);}++ if (isDocumentHidden()) {+ yield* flush();+ }}yield* flush();diff --git a/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.tsindex aaec038f964..d27aeb7ef9f 100644--- a/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts+++ b/frontend/src/metabase/api/ai-streaming/smooth-stream.unit.spec.ts@@ -86,3 +86,104 @@ describe("smoothStreamEvents", () => {expect(textDeltas(out)).toEqual(["answer "]);});});++describe("smoothStreamEvents (hidden document)", () => {+ const realSetTimeout = global.setTimeout;+ let isHidden = false;++ beforeEach(() => {+ isHidden = false;+ jest.spyOn(document, "hidden", "get").mockImplementation(() => isHidden);+ jest.spyOn(global, "setTimeout");+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it("emits the whole buffer as one delta, with no wait", async () => {+ isHidden = true;++ const out = await smooth([+ { type: "text-delta", id: "t1", delta: "alpha beta gam" },+ { type: "text-end", id: "t1" },+ ]);++ expect(textDeltas(out)).toEqual(["alpha beta gam"]);+ expect(setTimeout).not.toHaveBeenCalled();+ });++ it("flushes the rest of the buffer when the document hides mid-delta", async () => {+ const out: SSEEvent[] = [];+ const events = smoothStreamEvents(+ asAsyncIterable([+ { type: "text-delta", id: "t1", delta: "one two three four " },+ ]),+ { delayInMs: 0 },+ );++ for await (const event of events) {+ out.push(event);+ isHidden = true;+ }++ expect(textDeltas(out)).toEqual(["one ", "two three four "]);+ expect(setTimeout).not.toHaveBeenCalled();+ });++ it("flushes the rest of the buffer when the document hides during a wait", async () => {+ jest.mocked(setTimeout).mockImplementationOnce((callback, ms) => {+ isHidden = true;+ return realSetTimeout(callback, ms);+ });++ const out = await smooth([+ { type: "text-delta", id: "t1", delta: "one two three four " },+ ]);++ expect(textDeltas(out)).toEqual(["one ", "two three four "]);+ expect(setTimeout).toHaveBeenCalledTimes(1);+ });++ it("resumes pacing when the document is visible again", async () => {+ async function* hideThenShow(): AsyncGenerator<SSEEvent> {+ isHidden = true;+ yield { type: "text-delta", id: "t1", delta: "a b c " };+ isHidden = false;+ yield { type: "text-delta", id: "t1", delta: "d e " };+ }++ const out: SSEEvent[] = [];+ for await (const event of smoothStreamEvents(hideThenShow(), {+ delayInMs: 0,+ })) {+ out.push(event);+ }++ expect(textDeltas(out)).toEqual(["a b c ", "d ", "e "]);+ });++ it("keeps event order and passes non-text events through while hidden", async () => {+ isHidden = true;+ const events: SSEEvent[] = [+ { type: "reasoning-start", id: "r1" },+ { type: "reasoning-delta", id: "r1", delta: "let me " },+ { type: "reasoning-delta", id: "r1", delta: "check the table " },+ { type: "reasoning-end", id: "r1" },+ { type: "tool-input-start", toolCallId: "c1", toolName: "search" },+ {+ type: "tool-input-available",+ toolCallId: "c1",+ toolName: "search",+ input: {},+ },+ { type: "text-delta", id: "t1", delta: "Here is the answer." },+ { type: "data-state", data: {} },+ ];++ const out = await smooth(events);++ expect(out).toEqual(events);+ expect(setTimeout).not.toHaveBeenCalled();+ });+});