Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🫖 fix: Debounce Streaming Tool Code Highlights (#16695)
lia-by-librechat[bot]6ea0b0eead14
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.spec.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.spec.tsxindex 443c6fa3a5..56163199b3 100644--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.spec.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.spec.tsx@@ -1,5 +1,5 @@-import { act, renderHook, waitFor } from '@testing-library/react';-import useLazyHighlight from '../useLazyHighlight';+import { act, render, renderHook, waitFor } from '@testing-library/react';+import useLazyHighlight, { HIGHLIGHT_THROTTLE_MS } from '../useLazyHighlight';/** Real grammars, behind spies, so the count of tokenizations is observable. */const mockHighlightCalls: string[] = [];@@ -110,6 +110,47 @@ describe('useLazyHighlight', () => {expect(result.current).toBeNull();});+ it.each([+ ['bash', 'echo "hello"\n'],+ ['python', 'print("hello")\n'],+ ['javascript', 'const greeting = "hello";\n'],+ ])('keeps %s streaming text current and highlights only after it settles', async (lang, line) => {+ jest.useFakeTimers();+ try {+ function Code({ code }: { code: string }) {+ return <code>{useLazyHighlight(code, lang)}</code>;+ }+ let code = line;+ const { container, rerender } = render(<Code code={code} />);+ await settle();+ expect(container.querySelector('span')).not.toBeNull();+ mockHighlightCalls.length = 0;++ for (let i = 0; i < 20; i++) {+ act(() => jest.advanceTimersByTime(50));+ code += line;+ rerender(<Code code={code} />);+ expect(container.textContent).toBe(code);+ expect(container.querySelector('span')).toBeNull();+ expect(mockHighlightCalls).toEqual([]);+ }++ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS - 1));+ expect(container.querySelector('span')).toBeNull();+ act(() => jest.advanceTimersByTime(1));+ expect(container.textContent).toBe(code);+ expect(container.querySelector('span')).not.toBeNull();+ expect(mockHighlightCalls).toEqual([lang]);++ rerender(<Code code={code} />);+ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS * 2));+ expect(container.querySelector('span')).not.toBeNull();+ expect(mockHighlightCalls).toEqual([lang]);+ } finally {+ jest.useRealTimers();+ }+ });+it('highlights again when the code changes', async () => {const { result, rerender } = renderHighlight(CODE);await waitFor(() => expect(result.current).not.toBeNull());diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.throttle.spec.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.throttle.spec.tsxindex eec9e65b2b..431715e56c 100644--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.throttle.spec.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.throttle.spec.tsx@@ -21,7 +21,7 @@ jest.mock('lowlight', () => ({const flush = () => act(async () => Promise.resolve());-describe('useLazyHighlight throttling', () => {+describe('useLazyHighlight debouncing', () => {beforeEach(() => {jest.useFakeTimers();mockHighlight.mockClear();@@ -55,26 +55,31 @@ describe('useLazyHighlight throttling', () => {expect(result.current).toEqual(['a']);});- it('throttles re-highlights while streaming and highlights the settled value once', async () => {+ it('debounces re-highlights while streaming and highlights the settled value once', async () => {const { result, rerender } = renderHook(({ code }) => useLazyHighlight(code, 'js'), {initialProps: { code: 'a' },});await flush();mockHighlight.mockClear();- const chunks = ['ab', 'abc', 'abcd', 'abcde'];- for (const code of chunks) {+ let code = 'a';+ for (let i = 0; i < 20; i++) {+ act(() => jest.advanceTimersByTime(50));+ code += 'b';rerender({ code });- act(() => jest.advanceTimersByTime(20));+ expect(result.current).toEqual([code]);+ expect(mockHighlight).not.toHaveBeenCalled();}- expect(mockHighlight).not.toHaveBeenCalled();- expect(result.current).toEqual(['abcde']);- act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS));+ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS - 1));+ expect(mockHighlight).not.toHaveBeenCalled();+ act(() => jest.advanceTimersByTime(1));await flush();expect(mockHighlight).toHaveBeenCalledTimes(1);- expect(mockHighlight).toHaveBeenLastCalledWith('js', 'abcde');- expect(result.current).toEqual(['abcde']);+ expect(mockHighlight).toHaveBeenLastCalledWith('js', code);+ expect(result.current).toEqual([code]);+ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS * 2));+ expect(mockHighlight).toHaveBeenCalledTimes(1);});it('keeps completed highlights when the configured throttle changes', async () => {@@ -121,7 +126,7 @@ describe('useLazyHighlight throttling', () => {throttleMs = HIGHLIGHT_THROTTLE_MS * 2;rerender({ code: 'ab' });- act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS * 2 - 101));+ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS * 2 - 1));await flush();expect(mockHighlight).not.toHaveBeenCalled();@@ -150,7 +155,7 @@ describe('useLazyHighlight throttling', () => {await flush();expect(result.current).toEqual(['ab']);});- it('keeps the throttle window stable when the wall clock moves backwards', async () => {+ it('keeps the debounce window stable when the wall clock moves backwards', async () => {const { result, rerender } = renderHook(({ code }) => useLazyHighlight(code, 'js'), {initialProps: { code: 'a' },});@@ -165,6 +170,40 @@ describe('useLazyHighlight throttling', () => {await flush();expect(result.current).toEqual(['ab']);});+ it('debounces a replacement even after a long pause', async () => {+ const { rerender } = renderHook(({ code }) => useLazyHighlight(code, 'js'), {+ initialProps: { code: 'a' },+ });+ await flush();+ mockHighlight.mockClear();++ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS * 2));+ rerender({ code: 'ab' });+ expect(mockHighlight).not.toHaveBeenCalled();+ act(() => jest.advanceTimersByTime(HIGHLIGHT_THROTTLE_MS - 1));+ expect(mockHighlight).not.toHaveBeenCalled();+ act(() => jest.advanceTimersByTime(1));+ expect(mockHighlight).toHaveBeenCalledTimes(1);+ expect(mockHighlight).toHaveBeenLastCalledWith('js', 'ab');+ });++ it('highlights every update immediately when the delay is zero', async () => {+ const { rerender } = renderHook(({ code }) => useLazyHighlight(code, 'js'), {+ initialProps: { code: 'a' },+ wrapper: ({ children }) => (+ <CodeHighlightThrottleContext.Provider value={0}>+ {children}+ </CodeHighlightThrottleContext.Provider>+ ),+ });+ await flush();+ mockHighlight.mockClear();+ rerender({ code: 'ab' });+ expect(mockHighlight).toHaveBeenCalledTimes(1);+ expect(mockHighlight).toHaveBeenLastCalledWith('js', 'ab');+ expect(jest.getTimerCount()).toBe(0);+ });+it('does not parse an initialized value twice on mount', async () => {const { result } = renderHook(() => useLazyHighlight('initialized', 'js'));await flush();diff --git a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.tsindex b0fea391e8..718cf7f866 100644--- a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts+++ b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts@@ -1,6 +1,6 @@import React, { useState, useEffect, useRef } from 'react';-/** Minimum gap between highlights while the input keeps changing (streaming). */+/** Quiet period before re-highlighting streaming code. */export const HIGHLIGHT_THROTTLE_MS = 300;export const CodeHighlightThrottleContext = React.createContext(HIGHLIGHT_THROTTLE_MS);@@ -80,16 +80,14 @@ type HighlightState = { key: string; nodes: React.ReactNode[] | null };const highlightKey = (code: string | undefined, lang: string): string =>code ? `${lang}\0${code}` : '';-const now = (): number => (typeof performance === 'undefined' ? Date.now() : performance.now());-/*** Tokens for a block of code, once the grammars have loaded.** Highlighting runs when the input changes, and once per input: the key the tokens were produced* from is tracked, so a mount that could highlight immediately is not repeated by the effect that- * follows it. While the input keeps changing, as it does for a streaming tool call, highlights are- * throttled to one per `CodeHighlightThrottleContext` interval and the caller is handed its raw- * text in between, so a long code block stays readable without tokenizing every delta. Grammars+ * follows it. While the input keeps changing, highlights wait for a quiet+ * `CodeHighlightThrottleContext` interval. The caller receives current raw text until then,+ * avoiding repeated switches between raw and highlighted code during streaming. Grammars* load on first use, so a caller renders its own raw text until this returns; passing `undefined`* while a pane is closed keeps a collapsed card from tokenizing output nobody is reading.*/@@ -108,7 +106,7 @@ export default function useLazyHighlight(/** The input the tokens held were produced from, or that a pending run will produce. */const scheduledKey = useRef(hasInitialHighlight ? currentKey : '');const prevThrottleMs = useRef<number | null>(hasInitialHighlight ? throttleMs : null);- const lastRunAt = useRef<number | null>(hasInitialHighlight ? now() : null);+ const hasRun = useRef(hasInitialHighlight);const generation = useRef(0);const timer = useRef<ReturnType<typeof setTimeout> | null>(null);@@ -137,7 +135,7 @@ export default function useLazyHighlight(}if (!code) {- lastRunAt.current = null;+ hasRun.current = false;return;}@@ -148,7 +146,7 @@ export default function useLazyHighlight(const run = () => {timer.current = null;- lastRunAt.current = now();+ hasRun.current = true;const gen = ++generation.current;if (lowlightModule) {@@ -169,9 +167,7 @@ export default function useLazyHighlight(});};- /** A clock that jumped backwards would otherwise hold the next highlight for a full window. */- const elapsed = lastRunAt.current === null ? throttleMs : now() - lastRunAt.current;- const wait = throttleMs - elapsed;+ const wait = hasRun.current ? throttleMs : 0;if (wait <= 0) {run();} else {