Skip to content

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

🎈 fix: Fill Reasoning Previews Before Throttling (#16178)

lia-by-librechat[bot]1746a4ee9a87
diff --git a/client/src/components/Chat/Messages/Content/ActivityPhaseGroup.tsx b/client/src/components/Chat/Messages/Content/ActivityPhaseGroup.tsx
index 341d6c151f..4165ccc560 100644
--- a/client/src/components/Chat/Messages/Content/ActivityPhaseGroup.tsx
+++ b/client/src/components/Chat/Messages/Content/ActivityPhaseGroup.tsx
@@ -1,10 +1,19 @@
-import { memo, useId, useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import {
+ memo,
+ useId,
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+ useLayoutEffect,
+} from 'react';
import { useAtomValue } from 'jotai';
import { Button } from '@librechat/client';
import { ContentTypes } from 'librechat-data-provider';
import { Check, Lightbulb, ChevronDown, TriangleAlert } from 'lucide-react';
import type { TAttachment, TMessageContentParts } from 'librechat-data-provider';
-import type { CSSProperties, ReactNode } from 'react';
+import type { CSSProperties, ReactNode, RefObject } from 'react';
import {
useLocalize,
useExpandCollapse,
@@ -100,8 +109,12 @@ function PhaseGlyph({ failed }: { failed: boolean }) {
* from adding descender space, which made a live row 2px taller than the
* settled row it becomes.
*/
-function LiveLine({ text }: { text: string }) {
- return <span className="shimmer max-w-full truncate align-top">{text}</span>;
+function LiveLine({ text, previewRef }: { text: string; previewRef?: RefObject<HTMLSpanElement> }) {
+ return (
+ <span ref={previewRef} className="shimmer max-w-full truncate align-top">
+ {text}
+ </span>
+ );
}
const PhaseLabel = memo(function PhaseLabel({
@@ -111,6 +124,7 @@ const PhaseLabel = memo(function PhaseLabel({
source,
live = false,
lineId,
+ previewRef,
}: {
text: string;
animate: boolean;
@@ -122,6 +136,7 @@ const PhaseLabel = memo(function PhaseLabel({
live?: boolean;
/** Id for the current line, so a live disclosure can be named by it alone. */
lineId?: string;
+ previewRef?: RefObject<HTMLSpanElement>;
}) {
const [lines, setLines] = useState<{
current: string;
@@ -187,7 +202,7 @@ const PhaseLabel = memo(function PhaseLabel({
failed && 'text-text-warning',
)}
>
- {live ? <LiveLine text={lines.current} /> : lines.current}
+ {live ? <LiveLine text={lines.current} previewRef={previewRef} /> : lines.current}
</span>
</span>
);
@@ -242,8 +257,8 @@ function SpanGlyph({
*
* Its own component so only a live card pays for it — the localization and MCP
* lookups, and the throttle. `liveParts` is rebuilt on every streamed delta;
- * the throttle is what keeps that from reaching the DOM more than twice a
- * second.
+ * full reasoning lines and tool activity repaint at most twice a second.
+ * Short reasoning sentences stream freely until they fill the available row.
*/
function LivePhaseHeader({
parts,
@@ -280,7 +295,32 @@ function LivePhaseHeader({
: activity.text;
const { source } = activity;
const line = useMemo(() => ({ text, source }), [text, source]);
- const painted = useThrottledValue(line, LIVE_ACTIVITY_THROTTLE_MS);
+ const previewRef = useRef<HTMLSpanElement>(null);
+ const [isPreviewFull, setIsPreviewFull] = useState(false);
+ const painted = useThrottledValue(
+ line,
+ source.startsWith('think:') && !isPreviewFull ? 0 : LIVE_ACTIVITY_THROTTLE_MS,
+ );
+ const measurePreview = useCallback(() => {
+ const preview = previewRef.current;
+ const width = preview?.parentElement?.clientWidth ?? 0;
+ setIsPreviewFull(width > 0 && (preview?.scrollWidth ?? 0) >= width);
+ }, []);
+
+ /** Measure the painted sentence, not the accumulated reasoning or the next
+ * queued line: a newly displayed short sentence must fill before it waits. */
+ useLayoutEffect(measurePreview, [measurePreview, painted.text, painted.source]);
+ useLayoutEffect(() => {
+ const preview = previewRef.current;
+ const row = preview?.parentElement;
+ if (!preview || !row || typeof ResizeObserver === 'undefined') {
+ return;
+ }
+ const observer = new ResizeObserver(measurePreview);
+ observer.observe(row);
+ observer.observe(preview);
+ return () => observer.disconnect();
+ }, [measurePreview, painted.source]);
const iconKey = activity.iconNames.join('|');
const iconNames = useMemo(() => (iconKey ? iconKey.split('|') : []), [iconKey]);
const sourceDomains = useMemo(() => getSourceDomains(attachments, SPAN_SITES), [attachments]);
@@ -354,6 +394,7 @@ function LivePhaseHeader({
animate={animate}
live
lineId={lineId}
+ previewRef={previewRef}
/>
{detail && (
<span
diff --git a/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx
index 2f2dcce2ec..c2056e5523 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx
+++ b/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx
@@ -642,6 +642,81 @@ describe('live activity hardening transitions', () => {
</QueryClientProvider>
);
+ it.each([160, 640])('streams each short sentence freely until it fills a %ipx row', (width) => {
+ jest.useFakeTimers();
+ jest.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(width);
+ jest.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function (
+ this: HTMLElement,
+ ) {
+ return (this.textContent?.length ?? 0) * 8;
+ });
+ const content = (think: string): TMessageContentParts[] => [
+ { type: ContentTypes.THINK, think },
+ ];
+ const view = render(frame(content('Let me')));
+ const header = screen.getByRole('button');
+ view.rerender(frame(content('Let me check')));
+ expect(header).toHaveAccessibleName('Let me check');
+
+ const full = 'Checking the available evidence '.repeat(Math.ceil(width / 240)).trim();
+ view.rerender(frame(content(full)));
+ expect(header).toHaveAccessibleName(full);
+ view.rerender(frame(content(full + ' carefully')));
+ expect(header).toHaveAccessibleName(full);
+ act(() => jest.advanceTimersByTime(500));
+ expect(header).toHaveAccessibleName(full + ' carefully');
+
+ view.rerender(frame(content(full + ' carefully. Now I')));
+ act(() => jest.advanceTimersByTime(500));
+ expect(header).toHaveAccessibleName('Now I');
+ view.rerender(frame(content(full + ' carefully. Now I can check')));
+ expect(header).toHaveAccessibleName('Now I can check');
+ expect(screen.getByTestId('activity-phase-announcer')).toBeEmptyDOMElement();
+ jest.restoreAllMocks();
+ });
+
+ it('releases a queued reasoning preview when resizing gives the line more room', () => {
+ jest.useFakeTimers();
+ let width = 160;
+ const callbacks = new Set<ResizeObserverCallback>();
+ const disconnect = jest.fn();
+ jest.spyOn(global, 'ResizeObserver').mockImplementation((callback) => {
+ callbacks.add(callback);
+ return {
+ observe: jest.fn(),
+ unobserve: jest.fn(),
+ disconnect: () => {
+ callbacks.delete(callback);
+ disconnect();
+ },
+ };
+ });
+ jest.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => width);
+ jest.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function (
+ this: HTMLElement,
+ ) {
+ return (this.textContent?.length ?? 0) * 8;
+ });
+ const content = (think: string): TMessageContentParts[] => [
+ { type: ContentTypes.THINK, think },
+ ];
+ const initial = 'Checking the available evidence';
+ const view = render(frame(content(initial)));
+ view.rerender(frame(content(initial + ' carefully')));
+ expect(screen.getByRole('button')).toHaveAccessibleName(initial);
+
+ width = 640;
+ act(() => {
+ for (const callback of callbacks) {
+ callback([], { observe: () => {}, unobserve: () => {}, disconnect: () => {} });
+ }
+ });
+ expect(screen.getByRole('button')).toHaveAccessibleName(initial + ' carefully');
+ view.unmount();
+ expect(disconnect).toHaveBeenCalled();
+ jest.restoreAllMocks();
+ });
+
function SandboxEvent() {
const setStarting = useSetAtom(sandboxStartingByToolCallId('sandbox-call'));
return <button onClick={() => setStarting(true)}>{'Start sandbox'}</button>;
diff --git a/client/src/hooks/Messages/__tests__/useThrottledValue.spec.tsx b/client/src/hooks/Messages/__tests__/useThrottledValue.spec.tsx
index 1ffd885d16..f4f5bdf845 100644
--- a/client/src/hooks/Messages/__tests__/useThrottledValue.spec.tsx
+++ b/client/src/hooks/Messages/__tests__/useThrottledValue.spec.tsx
@@ -24,6 +24,29 @@ describe('useThrottledValue', () => {
expect(result.current).toBe('latest');
});
+ it('cancels a queued frame when switching to pass-through and starts a fresh window', () => {
+ jest.setSystemTime(1000);
+ const { result, rerender, unmount } = renderHook(
+ ({ value, interval }) => useThrottledValue(value, interval),
+ { initialProps: { value: 'first', interval: 500 } },
+ );
+ rerender({ value: 'queued', interval: 500 });
+ act(() => jest.advanceTimersByTime(100));
+ rerender({ value: 'short', interval: 0 });
+ expect(result.current).toBe('short');
+ expect(jest.getTimerCount()).toBe(0);
+
+ rerender({ value: 'fills the row', interval: 0 });
+ rerender({ value: 'fills the row', interval: 500 });
+ rerender({ value: 'next full line', interval: 500 });
+ act(() => jest.advanceTimersByTime(400));
+ expect(result.current).toBe('fills the row');
+ act(() => jest.advanceTimersByTime(100));
+ expect(result.current).toBe('next full line');
+ unmount();
+ expect(jest.getTimerCount()).toBe(0);
+ });
+
it('paints the leading change and the latest trailing value, then releases its timer', () => {
jest.setSystemTime(1000);
const { result, rerender, unmount } = renderHook(({ value }) => useThrottledValue(value, 500), {
diff --git a/client/src/hooks/Messages/useThrottledValue.ts b/client/src/hooks/Messages/useThrottledValue.ts
index 330aa87ef1..6e3e78e90a 100644
--- a/client/src/hooks/Messages/useThrottledValue.ts
+++ b/client/src/hooks/Messages/useThrottledValue.ts
@@ -16,6 +16,10 @@ export default function useThrottledValue<T>(value: T, intervalMs: number): T {
useEffect(() => {
const elapsed = Date.now() - paintedAtRef.current;
if (elapsed >= intervalMs) {
+ if (timerRef.current != null) {
+ clearTimeout(timerRef.current);
+ timerRef.current = null;
+ }
paintedAtRef.current = Date.now();
setThrottled(value);
return;