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
🖍️ perf: Highlight Tool Code Once, and Only When Its Pane Is Open (#16143)
Danny Avila692e7f6dc42d
diff --git a/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx b/client/src/components/Chat/Messages/Content/Parts/BashCall.tsxindex dd1c59fb80..47312563d0 100644--- a/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx@@ -90,7 +90,7 @@ export default function BashCall({extraCancelled: cancelledInBackground,});- const highlighted = useLazyHighlight(command || undefined, 'bash');+ const highlighted = useLazyHighlight(showCode ? command || undefined : undefined, 'bash');const { ref: commandPaneRef, onScroll: onCommandPaneScroll } = useFollowScroll<HTMLDivElement>(highlighted ?? command,phase === 'running',diff --git a/client/src/components/Chat/Messages/Content/Parts/ExecuteCode.tsx b/client/src/components/Chat/Messages/Content/Parts/ExecuteCode.tsxindex 35678fb929..52090262c3 100644--- a/client/src/components/Chat/Messages/Content/Parts/ExecuteCode.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/ExecuteCode.tsx@@ -128,7 +128,7 @@ export default function ExecuteCode({extraCancelled: cancelledInBackground,});- const highlighted = useLazyHighlight(code, lang);+ const highlighted = useLazyHighlight(showCode ? code : undefined, lang);const { ref: codePaneRef, onScroll: onCodePaneScroll } = useFollowScroll<HTMLPreElement>(highlighted ?? code ?? '',phase === 'running',@@ -187,7 +187,9 @@ export default function ExecuteCode({onScroll={onCodePaneScroll}className="max-h-[300px] overflow-auto bg-surface-chat p-4 font-mono text-xs dark:bg-surface-primary-alt">- <code className={`hljs language-${lang} !whitespace-pre`}>{highlighted}</code>+ <code className={`hljs language-${lang} !whitespace-pre`}>+ {highlighted ?? code}+ </code></pre>)}<PtcToolTracediff --git a/client/src/components/Chat/Messages/Content/Parts/FileAuthoringCall.tsx b/client/src/components/Chat/Messages/Content/Parts/FileAuthoringCall.tsxindex 0fb96450b9..41fb00aeba 100644--- a/client/src/components/Chat/Messages/Content/Parts/FileAuthoringCall.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/FileAuthoringCall.tsx@@ -178,7 +178,10 @@ export default function FileAuthoringCall({runStepStatus,});- const highlighted = useLazyHighlight(!parsedDiff && preview ? preview : undefined, previewLang);+ const highlighted = useLazyHighlight(+ showCode && !parsedDiff && preview ? preview : undefined,+ previewLang,+ );const { ref: previewPaneRef, onScroll: onPreviewPaneScroll } = useFollowScroll<HTMLPreElement>(highlighted ?? preview,phase === 'running',diff --git a/client/src/components/Chat/Messages/Content/Parts/ReadFileCall.tsx b/client/src/components/Chat/Messages/Content/Parts/ReadFileCall.tsxindex 27a114b030..a46a527360 100644--- a/client/src/components/Chat/Messages/Content/Parts/ReadFileCall.tsx+++ b/client/src/components/Chat/Messages/Content/Parts/ReadFileCall.tsx@@ -103,7 +103,7 @@ export default function ReadFileCall({runStepStatus,});- const highlighted = useLazyHighlight(hasOutput ? output : undefined, lang);+ const highlighted = useLazyHighlight(showCode && hasOutput ? output : undefined, lang);return (<>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.tsxnew file mode 100644index 0000000000..443c6fa3a5--- /dev/null+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/useLazyHighlight.spec.tsx@@ -0,0 +1,122 @@+import { act, renderHook, waitFor } from '@testing-library/react';+import useLazyHighlight from '../useLazyHighlight';++/** Real grammars, behind spies, so the count of tokenizations is observable. */+const mockHighlightCalls: string[] = [];++jest.mock('lowlight', () => {+ const actual = jest.requireActual('lowlight');+ return {+ ...actual,+ lowlight: {+ registered: (lang: string) => actual.lowlight.registered(lang),+ highlight: (lang: string, code: string) => {+ mockHighlightCalls.push(lang);+ return actual.lowlight.highlight(lang, code);+ },+ highlightAuto: (code: string) => {+ mockHighlightCalls.push('auto');+ return actual.lowlight.highlightAuto(code);+ },+ },+ };+});++const CODE = "const greeting = 'hello';\nconsole.log(greeting);\n";+const OTHER_CODE = 'print("hello")\n';++/** Renders the hook the way a tool card does, with the pane's open state deciding the input. */+function renderHighlight(code: string | undefined, lang = 'javascript') {+ return renderHook(+ ({ code: current, lang: currentLang }: { code: string | undefined; lang: string }) =>+ useLazyHighlight(current, currentLang),+ { initialProps: { code, lang } },+ );+}++/** Lets the grammars finish loading and any queued effect settle. */+async function settle() {+ await act(async () => {+ await Promise.resolve();+ });+}++/** Loads the grammars once, as the first opened card in a session does. */+async function warmGrammars() {+ const warm = renderHighlight(CODE);+ await waitFor(() => expect(warm.result.current).not.toBeNull());+ warm.unmount();+ mockHighlightCalls.length = 0;+}++describe('useLazyHighlight', () => {+ beforeEach(() => {+ mockHighlightCalls.length = 0;+ });++ it('holds nothing, and tokenizes nothing, while its caller passes no code', async () => {+ const { result } = renderHighlight(undefined);++ expect(result.current).toBeNull();+ await settle();+ expect(result.current).toBeNull();+ expect(mockHighlightCalls).toEqual([]);+ });++ it('highlights once the grammars have loaded', async () => {+ const { result } = renderHighlight(CODE);++ await waitFor(() => expect(result.current).not.toBeNull());+ expect(result.current?.length).toBeGreaterThan(0);+ });++ it('tokenizes one input once, not once per render', async () => {+ /* The grammars stay loaded for the rest of the session, so a card opened later can highlight+ * during its first render. Repeating that in the effect that follows costs a second+ * tokenization and a second commit for every card the reader opens. */+ await warmGrammars();++ const { result, rerender } = renderHighlight(CODE);+ expect(result.current).not.toBeNull();++ await settle();+ rerender({ code: CODE, lang: 'javascript' });+ await settle();++ expect(mockHighlightCalls).toHaveLength(1);+ });++ it('tokenizes nothing for a card whose pane is closed', async () => {+ await warmGrammars();++ const { result, rerender } = renderHighlight(undefined);+ rerender({ code: undefined, lang: 'javascript' });+ await settle();++ expect(result.current).toBeNull();+ expect(mockHighlightCalls).toEqual([]);+ });++ it('highlights when the pane opens, and drops what it holds when it closes', async () => {+ await warmGrammars();+ const { result, rerender } = renderHighlight(undefined);++ rerender({ code: CODE, lang: 'javascript' });+ await waitFor(() => expect(result.current).not.toBeNull());+ expect(mockHighlightCalls).toHaveLength(1);++ rerender({ code: undefined, lang: 'javascript' });+ await settle();+ expect(result.current).toBeNull();+ });++ it('highlights again when the code changes', async () => {+ const { result, rerender } = renderHighlight(CODE);+ await waitFor(() => expect(result.current).not.toBeNull());+ const first = result.current;++ rerender({ code: OTHER_CODE, lang: 'python' });+ await waitFor(() => expect(result.current).not.toBe(first));+ expect(result.current?.length).toBeGreaterThan(0);+ });+});diff --git a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.tsindex bd0883a033..1a720e4512 100644--- a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts+++ b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts@@ -1,4 +1,4 @@-import React, { useState, useEffect, useRef } from 'react';+import React, { useState, useEffect } from 'react';interface HastText {type: 'text';@@ -59,32 +59,48 @@ function highlightCode(mod: LowlightModule, code: string, lang: string): React.R}}+/** The tokens held, with the input they were produced from. Nothing to show is the empty key,+ * so a hook mounted without code still recognizes the first code it receives as new. */+type HighlightState = { key: string; nodes: React.ReactNode[] | null };++const NOTHING_HIGHLIGHTED: HighlightState = { key: '', nodes: null };++const highlightKey = (code: string | undefined, lang: string): string =>+ code ? `${lang}\0${code}` : '';++/**+ * Tokens for a block of code, once the grammars have loaded.+ *+ * Highlighting runs when the input changes, and once per input: the tokens carry the key they+ * were produced from, so a mount that could highlight immediately is not repeated by the effect+ * that follows it. 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.+ */export default function useLazyHighlight(code: string | undefined,lang: string,): React.ReactNode[] | null {- const [highlighted, setHighlighted] = useState<React.ReactNode[] | null>(() => {- if (!code || !lowlightModule) {- return null;- }- return highlightCode(lowlightModule, code, lang);- });- const prevKey = useRef('');+ const [state, setState] = useState<HighlightState>(() =>+ code && lowlightModule+ ? { key: highlightKey(code, lang), nodes: highlightCode(lowlightModule, code, lang) }+ : NOTHING_HIGHLIGHTED,+ );+ const key = highlightKey(code, lang);+ const currentKey = state.key;useEffect(() => {- const key = `${lang}\0${code ?? ''}`;- if (key === prevKey.current) {+ if (key === currentKey) {return;}- prevKey.current = key;if (!code) {- setHighlighted(null);+ setState(NOTHING_HIGHLIGHTED);return;}if (lowlightModule) {- setHighlighted(highlightCode(lowlightModule, code, lang));+ setState({ key, nodes: highlightCode(lowlightModule, code, lang) });return;}@@ -92,18 +108,18 @@ export default function useLazyHighlight(loadLowlight().then((mod) => {if (!cancelled) {- setHighlighted(highlightCode(mod, code, lang));+ setState({ key, nodes: highlightCode(mod, code, lang) });}}).catch(() => {if (!cancelled) {- setHighlighted([code]);+ setState({ key, nodes: [code] });}});return () => {cancelled = true;};- }, [code, lang]);+ }, [key, currentKey, code, lang]);- return highlighted;+ return state.nodes;}