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: Skip Reasoning Labels for Encrypted Reasoning (#17100)

Danny Avila50324a0baa59
diff --git a/client/src/components/Chat/Messages/Content/Part.tsx b/client/src/components/Chat/Messages/Content/Part.tsx
index cc0d0a47b4..10536892e7 100644
--- a/client/src/components/Chat/Messages/Content/Part.tsx
+++ b/client/src/components/Chat/Messages/Content/Part.tsx
@@ -6,6 +6,7 @@ import {
ContentTypes,
ToolCallTypes,
imageGenTools,
+ isOpaqueReasoning,
isImageVisionTool,
hasToolCallPreview,
getToolCallPreviewRevision,
@@ -646,6 +647,9 @@ const renderers: PartRenderers = {
if (reasoning.trim() === '' && part.reasoning_unavailable === true) {
return <ReasoningMarker label={part.reasoning_label} />;
}
+ if (isOpaqueReasoning(reasoning)) {
+ return <ReasoningMarker encrypted />;
+ }
return (
<Reasoning
partKeyIndex={getPartKeyIndex(part, partIndex ?? 0)}
diff --git a/client/src/components/Chat/Messages/Content/Parts/Reasoning.tsx b/client/src/components/Chat/Messages/Content/Parts/Reasoning.tsx
index abfa7dc2bb..c81dbc89b3 100644
--- a/client/src/components/Chat/Messages/Content/Parts/Reasoning.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/Reasoning.tsx
@@ -2,8 +2,8 @@ import { memo, useMemo, useState, useEffect, useCallback, useRef, useId } from '
import { useAtom } from 'jotai';
import copy from 'copy-to-clipboard';
import { Lightbulb, ChevronDown } from 'lucide-react';
-import { ContentTypes } from 'librechat-data-provider';
import { Button, disclosureChevronVariants } from '@librechat/client';
+import { ContentTypes, readableReasoning } from 'librechat-data-provider';
import type { MouseEvent, FocusEvent } from 'react';
import {
ThinkingContent,
@@ -26,6 +26,12 @@ const stripThinkTags = (reasoning: string): string =>
.replace(/\s*<\/think>$/, '')
.trim();
+/** The reasoning to show and copy: wrapper tags and encoded provider payloads
+ * are removed, and while it streams, a trailing run that could still grow
+ * into a payload is held back until it is classified. */
+const displayReasoning = (reasoning: string, streaming: boolean): string =>
+ stripThinkTags(readableReasoning(reasoning, streaming));
+
const PEEK_SENTENCES = 4;
/** Tail of streaming reasoning, specifically the last few sentences, for the collapsed
@@ -140,13 +146,23 @@ type ReasoningProps = {
* For legacy text-based messages, see Thinking.tsx component.
*/
/** Reasoning that happened but whose text this view cannot show — detached
- * subagent projections keep only a marker. Renders the shared reasoning
- * header row without a disclosure. */
-export const ReasoningMarker = memo(({ label }: { label?: string }) => {
- const localize = useLocalize();
- const display = label?.trim() || localize('com_ui_thoughts');
- return <ThinkingLabel label={display} title={localize('com_ui_thoughts_unavailable')} />;
-});
+ * subagent projections keep only a marker, and providers can return reasoning
+ * encrypted. Renders the shared reasoning header row without a disclosure. */
+export const ReasoningMarker = memo(
+ ({ label, encrypted = false }: { label?: string; encrypted?: boolean }) => {
+ const localize = useLocalize();
+ if (encrypted) {
+ return (
+ <ThinkingLabel
+ label={localize('com_ui_reasoning_encrypted')}
+ title={localize('com_ui_reasoning_encrypted_description')}
+ />
+ );
+ }
+ const display = label?.trim() || localize('com_ui_thoughts');
+ return <ThinkingLabel label={display} title={localize('com_ui_thoughts_unavailable')} />;
+ },
+);
ReasoningMarker.displayName = 'ReasoningMarker';
@@ -166,9 +182,13 @@ const Reasoning = memo((props: ReasoningProps) => {
const { style: expandStyle, ref: expandRef } = useExpandCollapse(isExpanded);
const { shouldRenderBody, mountBody, handleTransitionEnd } = useLazyCollapseBody(isExpanded);
const { isSubmitting, isLatestMessage, nextType } = useMessage();
+ const effectiveIsSubmitting = isLatestMessage ? isSubmitting : false;
+ const streaming = effectiveIsSubmitting === true && isLast;
- // Strip <think> tags from the reasoning content (modern format)
- const reasoningText = useMemo(() => stripThinkTags(reasoning), [reasoning]);
+ const reasoningText = useMemo(
+ () => displayReasoning(reasoning, streaming),
+ [reasoning, streaming],
+ );
const handleClick = useCallback(
(e: MouseEvent<HTMLButtonElement>) => {
@@ -201,8 +221,6 @@ const Reasoning = memo((props: ReasoningProps) => {
}
}, []);
- const effectiveIsSubmitting = isLatestMessage ? isSubmitting : false;
-
const label = useMemo(() => {
const generated = reasoningLabel?.trim();
if (generated) {
@@ -332,7 +350,10 @@ export const ReasoningCompact = memo(
* more expensive on top of the visible peek. */
const { shouldRenderBody, mountBody, handleTransitionEnd } = useLazyCollapseBody(isExpanded);
- const reasoningText = useMemo(() => stripThinkTags(reasoning), [reasoning]);
+ const reasoningText = useMemo(
+ () => displayReasoning(reasoning, isStreaming),
+ [reasoning, isStreaming],
+ );
const handleToggle = useCallback(
(e: MouseEvent<HTMLButtonElement>) => {
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningCompact.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningCompact.test.tsx
index ee6228759b..bb9f66d41b 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningCompact.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningCompact.test.tsx
@@ -116,4 +116,40 @@ describe('ReasoningCompact', () => {
expect(screen.getByText('A long stream of reasoning')).toBeInTheDocument();
});
+
+ it('shows only the readable text of reasoning mixed with an encrypted payload', () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ render(
+ <ReasoningCompact
+ reasoning={`Checking the manifest.\n${encoded}\nRolling back.`}
+ label="Thoughts"
+ showThinking
+ />,
+ );
+
+ const body = screen.getByRole('group', { hidden: true });
+ expect(body).toHaveTextContent('Checking the manifest.');
+ expect(body).toHaveTextContent('Rolling back.');
+ expect(body.textContent).not.toContain(encoded.slice(0, 64));
+ });
+
+ it('holds back a streaming run until it is classified as a word or a payload', () => {
+ const prefix = Buffer.from(Array.from({ length: 75 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ render(
+ <ReasoningCompact
+ reasoning={`Checking the manifest. ${prefix}`}
+ label="Thinking"
+ showThinking
+ isStreaming
+ />,
+ );
+
+ const body = screen.getByRole('group', { hidden: true });
+ expect(body).toHaveTextContent('Checking the manifest.');
+ expect(body.textContent).not.toContain(prefix.slice(0, 32));
+ });
});
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningMarker.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningMarker.test.tsx
new file mode 100644
index 0000000000..bc1009c001
--- /dev/null
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/ReasoningMarker.test.tsx
@@ -0,0 +1,33 @@
+import React from 'react';
+import { render, screen } from '@testing-library/react';
+import { ReasoningMarker } from '../Reasoning';
+
+jest.mock('~/hooks', () => ({
+ useLocalize: () => (key: string) => key,
+}));
+
+jest.mock('../Thinking', () => ({
+ ThinkingLabel: ({ label, title }: { label: string; title?: string }) => (
+ <div data-testid="thinking-label" title={title}>
+ {label}
+ </div>
+ ),
+}));
+
+describe('ReasoningMarker', () => {
+ it('shows the generated label for reasoning this view cannot show', () => {
+ render(<ReasoningMarker label="Planning the answer" />);
+
+ const marker = screen.getByTestId('thinking-label');
+ expect(marker).toHaveTextContent('Planning the answer');
+ expect(marker).toHaveAttribute('title', 'com_ui_thoughts_unavailable');
+ });
+
+ it('shows a neutral encrypted state and ignores any label', () => {
+ render(<ReasoningMarker encrypted label="Inspecting an encoded data snapshot" />);
+
+ const marker = screen.getByTestId('thinking-label');
+ expect(marker).toHaveTextContent('com_ui_reasoning_encrypted');
+ expect(marker).toHaveAttribute('title', 'com_ui_reasoning_encrypted_description');
+ });
+});
diff --git a/client/src/components/Chat/Messages/Content/ToolCallGroup.tsx b/client/src/components/Chat/Messages/Content/ToolCallGroup.tsx
index e36555756e..7225092375 100644
--- a/client/src/components/Chat/Messages/Content/ToolCallGroup.tsx
+++ b/client/src/components/Chat/Messages/Content/ToolCallGroup.tsx
@@ -1,5 +1,5 @@
import { useState, useRef, useMemo, useEffect, useContext, useCallback } from 'react';
-import { Tools, Constants, ContentTypes } from 'librechat-data-provider';
+import { Tools, Constants, ContentTypes, isOpaqueReasoning } from 'librechat-data-provider';
import { ChevronDown, ListChecks, MessageCircleQuestion, TriangleAlert, Users } from 'lucide-react';
import type { TAttachment, TMessageContentParts } from 'librechat-data-provider';
import type { PartWithIndex } from './ParallelContent';
@@ -739,12 +739,16 @@ export default function ToolCallGroup({
const reasoning =
typeof think === 'string' ? think : (think?.value ?? '');
/** A detached-subagent projection carries an empty THINK
- * part flagged `reasoning_unavailable`, which `Part`
- * renders as a `ReasoningMarker`. `ReasoningCompact` has
- * no text to show and returns null, so the marker has to
+ * part flagged `reasoning_unavailable`, and encrypted
+ * reasoning has no readable text; `Part` renders both as a
+ * `ReasoningMarker`. `ReasoningCompact` would show the
+ * encoded payload or return null, so the marker has to
* keep going through the standalone path or it vanishes
* the moment its call joins a group. */
- if (reasoning.trim() === '' && part.reasoning_unavailable === true) {
+ if (
+ (reasoning.trim() === '' && part.reasoning_unavailable === true) ||
+ isOpaqueReasoning(reasoning)
+ ) {
return renderPart(
part,
idx,
diff --git a/client/src/components/Chat/Messages/Content/__tests__/EditContentParts.spec.tsx b/client/src/components/Chat/Messages/Content/__tests__/EditContentParts.spec.tsx
index 9d7b9970ea..6aaba72901 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/EditContentParts.spec.tsx
+++ b/client/src/components/Chat/Messages/Content/__tests__/EditContentParts.spec.tsx
@@ -240,6 +240,34 @@ describe('EditContentParts', () => {
]);
});
+ it('keeps reasoning with an encrypted payload out of the editor', () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ const mixedContent = [
+ { type: ContentTypes.THINK, think: `Checking the manifest.\n${encoded}` },
+ { type: ContentTypes.TEXT, text: 'Original response' },
+ ] as TMessageContentParts[];
+ message.content = mixedContent;
+ const renderReadOnlyPart = jest.fn(() => null);
+
+ render(
+ <EditContentParts
+ content={mixedContent}
+ messageId={message.messageId}
+ isSubmitting={false}
+ enterEdit={jest.fn()}
+ siblingIdx={0}
+ setSiblingIdx={jest.fn()}
+ renderReadOnlyPart={renderReadOnlyPart}
+ />,
+ );
+
+ const editors = screen.getAllByRole('textbox') as HTMLTextAreaElement[];
+ expect(editors.map((editor) => editor.value)).toEqual(['Original response']);
+ expect(renderReadOnlyPart).toHaveBeenCalledWith(mixedContent[0], 0, false);
+ });
+
it('reruns an assistant response with the edited content value', () => {
const enterEdit = jest.fn();
render(
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 65ba73a79f..28cc3a7ea1 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx
+++ b/client/src/components/Chat/Messages/Content/__tests__/LiveParity.test.tsx
@@ -1237,7 +1237,9 @@ describe('live activity hardening transitions', () => {
expect(screen.getByText('Ran agent').closest('[data-testid="activity-phase-card"]')).toBeNull();
});
- it.each(['x'.repeat(1199), 'Earlier sentence. ' + 'x'.repeat(1199)])(
+ /** A non-ASCII run: an unbroken ASCII alphanumeric run this long is
+ * classified as an encoded provider payload and never previewed. */
+ it.each(['é'.repeat(1199), 'Earlier sentence. ' + 'é'.repeat(1199)])(
'does not repeatedly announce one thought as its bounded preview window moves',
(initial) => {
jest.useFakeTimers();
@@ -1255,7 +1257,7 @@ describe('live activity hardening transitions', () => {
expect(screen.getByTestId('activity-phase-announcer')).toBeEmptyDOMElement();
/** The finished sentence is the long run itself, clamped to one line;
* the sentence after it is still being written. */
- expect(screen.getByRole('button')).toHaveAccessibleName(`${'x'.repeat(255)}…`);
+ expect(screen.getByRole('button')).toHaveAccessibleName(`${'é'.repeat(255)}…`);
},
);
@@ -1706,3 +1708,63 @@ it.each(['lookup', 'image_gen_oai'])(
}
},
);
+
+describe('live activity over encrypted reasoning', () => {
+ /** Bedrock `redactedContent`, base64-encoded into THINK text by the SDK. */
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ const think = (text: string, label?: string): TMessageContentParts =>
+ ({
+ type: ContentTypes.THINK,
+ think: text,
+ ...(label != null && { reasoning_label: label }),
+ }) as TMessageContentParts;
+
+ it('names an encrypted thought generically instead of previewing the payload', () => {
+ const activity = getLiveActivity(
+ [think(encoded, 'Inspecting an encoded data snapshot')],
+ (key) => key,
+ [],
+ );
+ expect(activity.text).toBe('com_ui_thinking');
+ });
+
+ it('never titles a span with a label generated from an encrypted thought', () => {
+ const activity = getLiveActivity(
+ [think(encoded, 'Inspecting an encoded data snapshot')],
+ (key) => key,
+ [],
+ undefined,
+ true,
+ );
+ expect(activity.text).toBe('com_ui_running');
+ });
+
+ it('previews the readable text of reasoning mixed with an encrypted payload', () => {
+ const activity = getLiveActivity(
+ [think(`Comparing both lockfiles. ${encoded}`)],
+ (key) => key,
+ [],
+ );
+ expect(activity.text).toBe('Comparing both lockfiles.');
+ });
+
+ it('never previews the undecided start of a streaming payload', () => {
+ const activity = getLiveActivity(
+ [think(`Comparing both lockfiles. ${encoded.slice(0, 100)}`)],
+ (key) => key,
+ [],
+ );
+ expect(activity.text).toBe('Comparing both lockfiles.');
+ });
+
+ it('still previews readable reasoning', () => {
+ const activity = getLiveActivity(
+ [think('Comparing both lockfiles. The resolver pins an older range.')],
+ (key) => key,
+ [],
+ );
+ expect(activity.text).toBe('The resolver pins an older range.');
+ });
+});
diff --git a/client/src/components/Chat/Messages/Content/__tests__/Part.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/Part.test.tsx
index c4488db080..615e6f16ed 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/Part.test.tsx
+++ b/client/src/components/Chat/Messages/Content/__tests__/Part.test.tsx
@@ -10,8 +10,10 @@ jest.mock('../Parts', () => ({
AgentUpdate: () => <div data-testid="agent-update" />,
EmptyText: () => <div data-testid="empty-text" />,
Reasoning: () => <div data-testid="reasoning" />,
- ReasoningMarker: ({ label }: { label?: string }) => (
- <div data-testid="reasoning-marker">{label}</div>
+ ReasoningMarker: ({ label, encrypted }: { label?: string; encrypted?: boolean }) => (
+ <div data-testid="reasoning-marker" data-encrypted={String(encrypted === true)}>
+ {label}
+ </div>
),
Summary: () => <div data-testid="summary" />,
Text: ({ text }: { text?: string }) => <div data-testid="text">{text}</div>,
@@ -280,4 +282,34 @@ describe('Part tool renderer selection', () => {
expect(screen.getByTestId('reasoning')).toBeInTheDocument();
expect(screen.queryByTestId('reasoning-marker')).not.toBeInTheDocument();
});
+
+ it('renders encrypted reasoning as an encrypted marker without its stale label', () => {
+ /** Bedrock `redactedContent`, base64-encoded into THINK text by the SDK. */
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ renderPart({
+ type: ContentTypes.THINK,
+ think: encoded,
+ reasoning_label: 'Inspecting an encoded data snapshot',
+ } as TMessageContentParts);
+
+ const marker = screen.getByTestId('reasoning-marker');
+ expect(marker).toHaveAttribute('data-encrypted', 'true');
+ expect(marker).not.toHaveTextContent('Inspecting an encoded data snapshot');
+ expect(screen.queryByTestId('reasoning')).not.toBeInTheDocument();
+ });
+
+ it('renders persisted encrypted reasoning inside think tags as an encrypted marker', () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ renderPart({
+ type: ContentTypes.THINK,
+ think: `<think>${encoded}</think>`,
+ } as TMessageContentParts);
+
+ expect(screen.getByTestId('reasoning-marker')).toHaveAttribute('data-encrypted', 'true');
+ expect(screen.queryByTestId('reasoning')).not.toBeInTheDocument();
+ });
});
diff --git a/client/src/components/Chat/Messages/Content/__tests__/ToolCallGroup.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/ToolCallGroup.test.tsx
index 187d394bd7..7d80c57167 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/ToolCallGroup.test.tsx
+++ b/client/src/components/Chat/Messages/Content/__tests__/ToolCallGroup.test.tsx
@@ -601,6 +601,28 @@ describe('ToolCallGroup image hoisting', () => {
expect(screen.queryByTestId('compact-reasoning')).not.toBeInTheDocument();
});
+ it('delegates an encrypted-reasoning part to the standalone renderer', () => {
+ const encryptedPart = {
+ type: ContentTypes.THINK,
+ [ContentTypes.THINK]: Buffer.from(
+ Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256),
+ ).toString('base64'),
+ } as TMessageContentParts;
+
+ renderGroup({
+ ...baseProps,
+ parts: [
+ { part: makePart('t1'), idx: 0 },
+ { part: encryptedPart, idx: 1 },
+ ],
+ });
+
+ fireEvent.click(screen.getByRole('button', { name: /^Fetch_image/ }));
+
+ expect(screen.getByTestId('inner-1')).toBeInTheDocument();
+ expect(screen.queryByTestId('compact-reasoning')).not.toBeInTheDocument();
+ });
+
it('unmounts tool bodies after a collapsed group finishes transitioning', () => {
renderGroup(baseProps);
diff --git a/client/src/components/Chat/Messages/Content/__tests__/editableParts.spec.ts b/client/src/components/Chat/Messages/Content/__tests__/editableParts.spec.ts
index ee9686e4b0..72c522e64d 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/editableParts.spec.ts
+++ b/client/src/components/Chat/Messages/Content/__tests__/editableParts.spec.ts
@@ -1,6 +1,6 @@
import { ContentTypes } from 'librechat-data-provider';
-import type { TMessage } from 'librechat-data-provider';
-import { hasEditablePart } from '../editableParts';
+import type { TMessage, TMessageContentParts } from 'librechat-data-provider';
+import { hasEditablePart, isEditablePart } from '../editableParts';
const artifactText = ':::artifact{identifier="demo" type="text/html" title="Demo"}\n<div />\n:::';
@@ -52,3 +52,44 @@ describe('hasEditablePart', () => {
expect(hasEditablePart({ content } as TMessage)).toBe(false);
});
});
+
+describe('encrypted reasoning in the editor', () => {
+ /** Bedrock `redactedContent`, base64-encoded into THINK text by the SDK. */
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+
+ it('offers no field for reasoning that is only an encrypted payload', () => {
+ expect(
+ isEditablePart({ type: ContentTypes.THINK, think: encoded } as TMessageContentParts),
+ ).toBe(false);
+ expect(
+ hasEditablePart({
+ content: [{ type: ContentTypes.THINK, think: `<think>${encoded}</think>` }],
+ } as TMessage),
+ ).toBe(false);
+ });
+
+ it('keeps mixed reasoning read-only so an edit cannot drop its payload', () => {
+ const part = {
+ type: ContentTypes.THINK,
+ think: `Checking the manifest. ${encoded}`,
+ } as TMessageContentParts;
+ expect(isEditablePart(part)).toBe(false);
+ });
+
+ it('still edits readable reasoning and any text part', () => {
+ expect(
+ isEditablePart({
+ type: ContentTypes.THINK,
+ think: 'Checking the manifest.',
+ } as TMessageContentParts),
+ ).toBe(true);
+ expect(
+ isEditablePart({
+ type: ContentTypes.TEXT,
+ text: `Token: ${encoded}`,
+ } as TMessageContentParts),
+ ).toBe(true);
+ });
+});
diff --git a/client/src/components/Chat/Messages/Content/editableParts.ts b/client/src/components/Chat/Messages/Content/editableParts.ts
index 6e54913ba1..7a1e126215 100644
--- a/client/src/components/Chat/Messages/Content/editableParts.ts
+++ b/client/src/components/Chat/Messages/Content/editableParts.ts
@@ -1,4 +1,4 @@
-import { ContentTypes } from 'librechat-data-provider';
+import { ContentTypes, stripOpaqueReasoning } from 'librechat-data-provider';
import type { TMessage, TMessageContentParts, TextData } from 'librechat-data-provider';
import { splitMarkdownIntoBlocks } from './splitMarkdown';
@@ -50,7 +50,16 @@ export const isEditablePart = (part?: TMessageContentParts | null): boolean => {
return false;
}
const text = getPartText(part);
- return text != null && !containsArtifact(text);
+ if (text == null) {
+ return false;
+ }
+ /** The edit endpoint replaces the whole value, and an encoded provider
+ * payload is neither readable nor positionable in an edited draft, so
+ * reasoning that carries one stays read-only rather than losing it. */
+ if (part.type === ContentTypes.THINK && stripOpaqueReasoning(text) !== text) {
+ return false;
+ }
+ return !containsArtifact(text);
};
/**
diff --git a/client/src/components/Chat/Messages/Content/live.ts b/client/src/components/Chat/Messages/Content/live.ts
index 0f43fe5797..df6e7bf8eb 100644
--- a/client/src/components/Chat/Messages/Content/live.ts
+++ b/client/src/components/Chat/Messages/Content/live.ts
@@ -1,4 +1,10 @@
-import { Constants, ContentTypes, stripToolCallErrorPrefix } from 'librechat-data-provider';
+import {
+ Constants,
+ ContentTypes,
+ isOpaqueReasoning,
+ readableReasoning,
+ stripToolCallErrorPrefix,
+} from 'librechat-data-provider';
import type {
Agents,
TAttachment,
@@ -308,8 +314,13 @@ function newestLine(
* Unless the fold is open: the text is on screen then, and a header
* repeating a line of it under the reader's eyes is noise, so the
* header keeps to the thought's label. */
- const reasoning = typeof part.think === 'string' ? part.think : (part.think?.value ?? '');
- const label = part.reasoning_label?.trim();
+ const think = typeof part.think === 'string' ? part.think : (part.think?.value ?? '');
+ /** Encrypted reasoning has no sentence to preview, and a label generated
+ * from it before labels skipped encoded payloads describes the payload. */
+ const opaque = isOpaqueReasoning(think);
+ /** The live header only exists while the run streams. */
+ const reasoning = readableReasoning(think, true);
+ const label = opaque ? undefined : part.reasoning_label?.trim();
if (preferLabels) {
/** Only a generated label will do: the generic thinking line is what
* the grouped thought row itself says. */
@@ -322,7 +333,7 @@ function newestLine(
if (sentence != null) {
return { text: sentence, source: `think:${position}`, comboCount: 1 };
}
- if (label || reasoning.trim()) {
+ if (label || think.trim()) {
return {
text: label || localize('com_ui_thinking'),
source: `think:${position}`,
diff --git a/client/src/hooks/Conversations/__tests__/format.test.ts b/client/src/hooks/Conversations/__tests__/format.test.ts
index 0f66fd034d..134d69ccec 100644
--- a/client/src/hooks/Conversations/__tests__/format.test.ts
+++ b/client/src/hooks/Conversations/__tests__/format.test.ts
@@ -180,6 +180,26 @@ describe('formatMessageContent', () => {
]);
});
+ it('exports only the readable part of reasoning with encrypted payloads', () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ expect(
+ formatMessageContent({
+ sender: 'Assistant',
+ content: { type: ContentTypes.THINK, think: `<think>${encoded}</think>` },
+ localize,
+ }),
+ ).toEqual([]);
+ expect(
+ formatMessageContent({
+ sender: 'Assistant',
+ content: { type: ContentTypes.THINK, think: `Check assumptions\n${encoded}` },
+ localize,
+ }),
+ ).toEqual(['Assistant', 'Thinking:\nCheck assumptions']);
+ });
+
it('concatenates summary chunks without inserted paragraphs', () => {
expect(
formatMessageContent({
diff --git a/client/src/hooks/Conversations/format.ts b/client/src/hooks/Conversations/format.ts
index 0603950c2f..f4069b1f82 100644
--- a/client/src/hooks/Conversations/format.ts
+++ b/client/src/hooks/Conversations/format.ts
@@ -1,4 +1,4 @@
-import { ContentTypes, ToolCallTypes } from 'librechat-data-provider';
+import { ContentTypes, ToolCallTypes, stripOpaqueReasoning } from 'librechat-data-provider';
import type { TMessage, TMessageContentParts } from 'librechat-data-provider';
import type { LocalizeFunction } from '~/common';
@@ -136,7 +136,8 @@ export function formatMessageContent({
}
if (content.type === ContentTypes.THINK) {
- const reasoning = stripThinkTags(getTextValue(content.think));
+ /** Encrypted provider reasoning is not something the reader can use. */
+ const reasoning = stripThinkTags(stripOpaqueReasoning(getTextValue(content.think)));
if (reasoning.trim().length === 0) {
return [];
}
diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json
index acf2621e2f..0826cd7331 100644
--- a/client/src/locales/en/translation.json
+++ b/client/src/locales/en/translation.json
@@ -1336,6 +1336,8 @@
"com_ui_authentication_type": "Authentication Type",
"com_ui_auto": "Auto",
"com_ui_reasoning_for_next_message": "Reasoning for next message",
+ "com_ui_reasoning_encrypted": "Reasoning (encrypted)",
+ "com_ui_reasoning_encrypted_description": "The model returned its reasoning encrypted, so it cannot be shown",
"com_ui_available_options": "Available options",
"com_ui_avatar": "Avatar",
"com_ui_avatar_drop_image": "Drop an image here to use as your profile picture",
diff --git a/packages/api/src/agents/activityLabels/__tests__/wiring.spec.ts b/packages/api/src/agents/activityLabels/__tests__/wiring.spec.ts
index 0e2aaba489..f5232204a2 100644
--- a/packages/api/src/agents/activityLabels/__tests__/wiring.spec.ts
+++ b/packages/api/src/agents/activityLabels/__tests__/wiring.spec.ts
@@ -123,6 +123,22 @@ describe('captureActivityBlockContext', () => {
const context = captureActivityBlockContext(parts, 'agent-a');
expect(context.thinkingExcerpts).toEqual(['Agent A reasoning']);
});
+
+ it('keeps encoded provider reasoning out of the label evidence', () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ const parts: LooseContentPart[] = [
+ { type: 'think', think: 'Comparing both lockfiles before the upgrade.' },
+ { type: 'think', think: encoded },
+ { type: 'think', think: `Pinning the resolver.\n${encoded}` },
+ ];
+ const context = captureActivityBlockContext(parts);
+ expect(context.thinkingExcerpts).toEqual([
+ 'Comparing both lockfiles before the upgrade.',
+ 'Pinning the resolver.',
+ ]);
+ });
});
describe('stripActivityLabelParts', () => {
diff --git a/packages/api/src/agents/activityLabels/wiring.ts b/packages/api/src/agents/activityLabels/wiring.ts
index 35c0905900..335f8217ab 100644
--- a/packages/api/src/agents/activityLabels/wiring.ts
+++ b/packages/api/src/agents/activityLabels/wiring.ts
@@ -1,4 +1,4 @@
-import { ContentTypes } from 'librechat-data-provider';
+import { ContentTypes, stripOpaqueReasoning } from 'librechat-data-provider';
import type { HookCallback } from '@librechat/agents';
import type {
ActivityLabelBlockContext,
@@ -83,7 +83,7 @@ export function captureActivityBlockContext(
thinkingExcerpts.length < MAX_EXCERPTS &&
(executingAgentId == null || part.agentId == null || part.agentId === executingAgentId)
) {
- const think = textValue(part.think).trim();
+ const think = stripOpaqueReasoning(textValue(part.think)).trim();
if (think.length > 0) {
thinkingExcerpts.unshift(think.slice(0, EXCERPT_CHARS));
}
diff --git a/packages/api/src/agents/activityPhases/runtime.spec.ts b/packages/api/src/agents/activityPhases/runtime.spec.ts
index 44a451c606..1eb4b1735f 100644
--- a/packages/api/src/agents/activityPhases/runtime.spec.ts
+++ b/packages/api/src/agents/activityPhases/runtime.spec.ts
@@ -492,6 +492,80 @@ describe('createActivityPhaseWiring', () => {
expect(payloads[1]).toMatchObject({ assistantContext: ['Context for the later work.'] });
});
+ it('sends only readable reasoning excerpts to the phase summarizer', async () => {
+ const encoded = Buffer.from(Array.from({ length: 450 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ const parts: LooseContentPart[] = [
+ { type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-1' } },
+ { type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-2' } },
+ { type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-3' } },
+ { type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-4' } },
+ ];
+ const payloads: GenerateActivityPhasePayload[] = [];
+ const wiring = createActivityPhaseWiring({
+ initialSnapshot: {
+ version: 3,
+ generated: 0,
+ activityCount: 4,
+ failedActivityCount: 0,
+ partialActivityCount: 0,
+ agentIds: [],
+ activities: [
+ {
+ startIndex: 0,
+ status: 'success',
+ toolCallIds: ['tool-1'],
+ thinkingExcerpts: ['Checking the deploy manifest.'],
+ },
+ {
+ startIndex: 1,
+ status: 'success',
+ toolCallIds: ['tool-2'],
+ thinkingExcerpts: [encoded],
+ },
+ {
+ startIndex: 2,
+ status: 'success',
+ toolCallIds: ['tool-3'],
+ thinkingExcerpts: [`Rolling back.\n${encoded}`],
+ },
+ {
+ startIndex: 3,
+ status: 'success',
+ toolCallIds: ['tool-4'],
+ /** A window cut into a payload: only the streamed classification is evidence. */
+ thinkingExcerpts: [`${encoded.slice(-9)} Verified the rollback.`],
+ readableExcerpts: ['Verified the rollback.'],
+ },
+ ],
+ assistantContext: [],
+ pendingReasoning: [],
+ },
+ getContentParts: () => parts,
+ bumpIndexOffset: jest.fn(),
+ emitLabelEvent: jest.fn(async () => undefined),
+ trackPendingFill: jest.fn(),
+ generatePhase: jest.fn(async (payload: GenerateActivityPhasePayload) => {
+ payloads.push(payload);
+ return { label: 'Rolled back the deploy' };
+ }),
+ });
+
+ wiring.complete();
+ await flushDetached();
+
+ expect(payloads).toHaveLength(1);
+ expect(payloads[0].activities.map((activity) => activity.thinkingExcerpts)).toEqual([
+ ['Checking the deploy manifest.'],
+ undefined,
+ ['Rolling back.'],
+ ['Verified the rollback.'],
+ ]);
+ expect(payloads[0].activities[1]).not.toHaveProperty('thinkingExcerpts');
+ expect(payloads[0].activities[3]).not.toHaveProperty('readableExcerpts');
+ });
+
it('keeps a substantial boundary hard when too little preceding work earns a phase', async () => {
const parts: LooseContentPart[] = [
{ type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-1' } },
@@ -838,6 +912,154 @@ describe('createActivityPhaseWiring', () => {
expect(parts).toHaveLength(1);
});
+ it('replays a legacy pending-reasoning snapshot through the stream filter', async () => {
+ const encoded = Buffer.from(
+ Array.from({ length: 1_050 }, (_, i) => (i * 37 + 3) % 256),
+ ).toString('base64');
+ const parts: LooseContentPart[] = [];
+ const wiring = createActivityPhaseWiring({
+ initialSnapshot: {
+ version: 3,
+ generated: 0,
+ activityCount: 0,
+ failedActivityCount: 0,
+ partialActivityCount: 0,
+ agentIds: [],
+ activities: [],
+ assistantContext: [],
+ /** Written before snapshots carried a readable window or cursor. */
+ pendingReasoning: [{ key: 'root', text: `Checking. ${encoded.slice(0, 100)}` }],
+ },
+ getContentParts: () => parts,
+ bumpIndexOffset: jest.fn(),
+ emitLabelEvent: jest.fn(async () => undefined),
+ trackPendingFill: jest.fn(),
+ generatePhase: jest.fn(async () => ({ label: 'unused' })),
+ });
+ const handlers = wiring.handlers({
+ [GraphEvents.ON_RUN_STEP]: { handle: jest.fn() },
+ [GraphEvents.ON_REASONING_DELTA]: { handle: jest.fn() },
+ });
+ handlers?.[GraphEvents.ON_RUN_STEP]?.handle(
+ GraphEvents.ON_RUN_STEP,
+ {
+ id: 'reasoning-step',
+ stepDetails: {
+ type: StepTypes.MESSAGE_CREATION,
+ message_creation: { message_id: 'm', content_type: 'think' },
+ },
+ } as never,
+ undefined,
+ undefined,
+ );
+ for (const think of [encoded.slice(100), ' Rolling back.']) {
+ handlers?.[GraphEvents.ON_REASONING_DELTA]?.handle(
+ GraphEvents.ON_REASONING_DELTA,
+ { id: 'reasoning-step', delta: { content: { type: ContentTypes.THINK, think } } } as never,
+ undefined,
+ undefined,
+ );
+ }
+ parts.push({ type: ContentTypes.THINK, think: `Checking. ${encoded} Rolling back.` });
+ parts.push({ type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-1' } });
+ await wiring.hook(batch('tool-1'), new AbortController().signal);
+
+ const [activity] = wiring.snapshot().activities;
+ expect(activity.readableExcerpts).toEqual(['Checking. Rolling back.']);
+ });
+
+ it('drops the cut leading run of a full legacy pending-reasoning window', async () => {
+ const encoded = Buffer.from(
+ Array.from({ length: 1_050 }, (_, i) => (i * 37 + 3) % 256),
+ ).toString('base64');
+ const readable = 'Checking the manifest before rolling back the deploy. '
+ .repeat(11)
+ .slice(0, 549);
+ const legacyWindow = `${encoded.slice(-50)} ${readable}`;
+ expect(legacyWindow).toHaveLength(600);
+ const parts: LooseContentPart[] = [];
+ const wiring = createActivityPhaseWiring({
+ initialSnapshot: {
+ version: 3,
+ generated: 0,
+ activityCount: 0,
+ failedActivityCount: 0,
+ partialActivityCount: 0,
+ agentIds: [],
+ activities: [],
+ assistantContext: [],
+ pendingReasoning: [{ key: 'root', text: legacyWindow }],
+ },
+ getContentParts: () => parts,
+ bumpIndexOffset: jest.fn(),
+ emitLabelEvent: jest.fn(async () => undefined),
+ trackPendingFill: jest.fn(),
+ generatePhase: jest.fn(async () => ({ label: 'unused' })),
+ });
+ parts.push({ type: ContentTypes.THINK, think: `${encoded} ${readable}` });
+ parts.push({ type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-1' } });
+ await wiring.hook(batch('tool-1'), new AbortController().signal);
+
+ const [activity] = wiring.snapshot().activities;
+ expect(activity.readableExcerpts).toEqual([readable.trim()]);
+ });
+
+ it('classifies streamed reasoning before bounding its prompt evidence', async () => {
+ const encoded = Buffer.from(
+ Array.from({ length: 1_050 }, (_, i) => (i * 37 + 3) % 256),
+ ).toString('base64');
+ const readable = 'Checking the manifest before rolling back the deploy. '
+ .repeat(11)
+ .slice(0, 590);
+ const parts: LooseContentPart[] = [];
+ const wiring = createActivityPhaseWiring({
+ getContentParts: () => parts,
+ bumpIndexOffset: jest.fn(),
+ emitLabelEvent: jest.fn(async () => undefined),
+ trackPendingFill: jest.fn(),
+ generatePhase: jest.fn(async () => ({ label: 'unused' })),
+ });
+ const handlers = wiring.handlers({
+ [GraphEvents.ON_RUN_STEP]: { handle: jest.fn() },
+ [GraphEvents.ON_REASONING_DELTA]: { handle: jest.fn() },
+ });
+ handlers?.[GraphEvents.ON_RUN_STEP]?.handle(
+ GraphEvents.ON_RUN_STEP,
+ {
+ id: 'reasoning-step',
+ stepDetails: {
+ type: StepTypes.MESSAGE_CREATION,
+ message_creation: { message_id: 'm', content_type: 'think' },
+ },
+ } as never,
+ undefined,
+ undefined,
+ );
+ /** A payload-only stream longer than the 600-char window, split so its
+ * final 50-char fragment shares a delta with the readable reasoning. */
+ const deltas = [
+ ...Array.from({ length: 13 }, (_, i) => encoded.slice(i * 100, (i + 1) * 100)),
+ `${encoded.slice(1_300)}\n${readable}`,
+ ];
+ for (const think of deltas) {
+ handlers?.[GraphEvents.ON_REASONING_DELTA]?.handle(
+ GraphEvents.ON_REASONING_DELTA,
+ { id: 'reasoning-step', delta: { content: { type: ContentTypes.THINK, think } } } as never,
+ undefined,
+ undefined,
+ );
+ }
+ parts.push({ type: ContentTypes.THINK, think: deltas.join('') });
+ parts.push({ type: ContentTypes.TOOL_CALL, tool_call: { id: 'tool-1' } });
+ await wiring.hook(batch('tool-1'), new AbortController().signal);
+
+ const [activity] = wiring.snapshot().activities;
+ expect(activity.readableExcerpts).toEqual([readable.trim()]);
+ /** The raw anchor window still starts inside the payload: exactly the
+ * fragment that must never be prompt evidence. */
+ expect(activity.thinkingExcerpts?.[0]?.startsWith(`${encoded.slice(-9)}\n`)).toBe(true);
+ });
+
it('keeps reasoning attached to a tool batch across commentary', async () => {
const parts: LooseContentPart[] = [];
const generatePhase = jest.fn(async () => ({ label: 'unused' }));
diff --git a/packages/api/src/agents/activityPhases/runtime.ts b/packages/api/src/agents/activityPhases/runtime.ts
index 1d0228f828..bbe0232276 100644
--- a/packages/api/src/agents/activityPhases/runtime.ts
+++ b/packages/api/src/agents/activityPhases/runtime.ts
@@ -1,6 +1,15 @@
import { GraphEvents } from '@librechat/agents';
-import { ContentTypes, StepTypes } from 'librechat-data-provider';
+import {
+ StepTypes,
+ ContentTypes,
+ stripOpaqueReasoning,
+ flushOpaqueReasoning,
+ advanceOpaqueReasoning,
+ trimTruncatedReasoning,
+ OPAQUE_REASONING_START,
+} from 'librechat-data-provider';
import type { EventHandler, HookCallback, HookInputByEvent } from '@librechat/agents';
+import type { OpaqueReasoningCursor } from 'librechat-data-provider';
import type { LooseContentPart } from '~/agents/activityLabels/wiring';
import { stringifyActivityEvidence } from '~/agents/activityLabels/runtime';
@@ -26,6 +35,11 @@ export interface ActivityPhaseEntry {
export type TrackedActivity = ActivityPhaseEntry & {
startIndex: number;
+ /** Prompt evidence: the reasoning behind `thinkingExcerpts`, classified while
+ * it streamed so encoded payloads never reach it. `thinkingExcerpts` keep the
+ * raw text because anchoring matches it against THINK parts. Absent on
+ * activities from older snapshots, whose excerpts are stripped instead. */
+ readableExcerpts?: string[];
/** A prior boundary can retain only the materialized tail of a straddling batch. */
partitionStartIndex?: number;
/** Anchor-only activities keep position and status but carry no prompt evidence. */
@@ -74,6 +88,8 @@ export interface ActivityPhaseSnapshot {
pendingReasoning: Array<{
key: string;
text: string;
+ readable?: string;
+ cursor?: OpaqueReasoningCursor;
agentId?: string;
startIndex?: number;
}>;
@@ -196,6 +212,22 @@ function deltaText(data: unknown, key: 'text' | 'think'): string {
return parts.map((part) => textValue((part as DeltaPart | null)?.[key])).join('');
}
+/** Prompt evidence only: anchors keep the raw excerpt so positions still resolve. */
+function withReadableReasoning(
+ activity: ActivityPhaseEntry & Pick<TrackedActivity, 'readableExcerpts'>,
+): ActivityPhaseEntry {
+ const { thinkingExcerpts, readableExcerpts, ...rest } = activity;
+ if (thinkingExcerpts == null && readableExcerpts == null) {
+ return rest;
+ }
+ const readable = (
+ readableExcerpts ?? (thinkingExcerpts ?? []).map((excerpt) => stripOpaqueReasoning(excerpt))
+ )
+ .map((excerpt) => excerpt.trim())
+ .filter((excerpt) => excerpt.length > 0);
+ return readable.length > 0 ? { ...rest, thinkingExcerpts: readable } : rest;
+}
+
function buildSignal(signal?: AbortSignal): AbortSignal {
const timeout = AbortSignal.timeout(PHASE_TIMEOUT_MS);
return signal != null && typeof AbortSignal.any === 'function'
@@ -428,7 +460,22 @@ export function createAssistantPhaseStampingHandlers(
return wrapped;
}
-type PendingReasoning = { text: string; agentId?: string; startIndex?: number };
+type PendingReasoning = {
+ /** Raw rolling window, matched against THINK parts to anchor the activity. */
+ text: string;
+ /** Rolling window of the same reasoning with encoded payloads removed. */
+ readable: string;
+ cursor: OpaqueReasoningCursor;
+ agentId?: string;
+ startIndex?: number;
+};
+
+/** Readable evidence for a finished reasoning lane; its undecided tail settles here. */
+function readableExcerpt(reasoning: PendingReasoning): string {
+ return `${reasoning.readable}${flushOpaqueReasoning(reasoning.cursor)}`
+ .trim()
+ .slice(0, MAX_EXCERPT_CHARS);
+}
/** One side of a boundary split. Every piece of run state that a phase can
* own appears here, so adding tracked state without partitioning it is a
@@ -565,6 +612,7 @@ function boundedAnchor(activity: TrackedActivity): TrackedActivity {
* child-label slot are deliberately not carried. */
label: undefined,
entries: undefined,
+ readableExcerpts: undefined,
childLabelIndex: undefined,
};
return fields;
@@ -605,6 +653,7 @@ function mergeAnchors(earlier: TrackedActivity, later: TrackedActivity): Tracked
mergedAgentIds: foldedAgentIds(earlier, later),
label: undefined,
entries: undefined,
+ readableExcerpts: undefined,
childLabelIndex: undefined,
};
return fields;
@@ -844,35 +893,55 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
let assistantContext: AssistantContextEntry[] = (initialSnapshot?.assistantContext ?? [])
.slice(-MAX_CONTEXT_ITEMS)
.map((entry) => (typeof entry === 'string' ? { text: entry } : { ...entry }));
- const pendingReasoning = new Map<string, { text: string; agentId?: string; startIndex?: number }>(
- (initialSnapshot?.pendingReasoning ?? []).map(({ key, text, agentId, startIndex }) => {
- const boundedText = text.slice(-MAX_EXCERPT_CHARS);
- const needle = boundedText.trim().slice(0, REASONING_ANCHOR_CHARS);
- const rebasedStartIndex = needle ? findReasoningStart(content, boundedText, startIndex) : -1;
- /** The anchor is a prefix, so two lanes can share it. Binding to the
- * first match would replay a still-pending lane on the earlier side of
- * a boundary and delete it; an ambiguous anchor is no anchor. */
- const matchingReasoningParts =
- needle.length > 0
- ? definedPartIndices(content).filter(
- (index) =>
- content[index]?.type === ContentTypes.THINK &&
- textValue(content[index]?.think).includes(needle),
- ).length
- : 0;
- const hasMaterializedReasoning =
- matchingReasoningParts === 1 &&
- content[rebasedStartIndex]?.type === ContentTypes.THINK &&
- textValue(content[rebasedStartIndex]?.think).includes(needle);
- return [
- key,
- {
- text: boundedText,
- ...(agentId != null && { agentId }),
- ...(hasMaterializedReasoning && { startIndex: rebasedStartIndex }),
- },
- ] as const;
- }),
+ const pendingReasoning = new Map<string, PendingReasoning>(
+ (initialSnapshot?.pendingReasoning ?? []).map(
+ ({ key, text, readable, cursor, agentId, startIndex }) => {
+ const boundedText = text.slice(-MAX_EXCERPT_CHARS);
+ const needle = boundedText.trim().slice(0, REASONING_ANCHOR_CHARS);
+ const rebasedStartIndex = needle
+ ? findReasoningStart(content, boundedText, startIndex)
+ : -1;
+ /** The anchor is a prefix, so two lanes can share it. Binding to the
+ * first match would replay a still-pending lane on the earlier side of
+ * a boundary and delete it; an ambiguous anchor is no anchor. */
+ const matchingReasoningParts =
+ needle.length > 0
+ ? definedPartIndices(content).filter(
+ (index) =>
+ content[index]?.type === ContentTypes.THINK &&
+ textValue(content[index]?.think).includes(needle),
+ ).length
+ : 0;
+ const hasMaterializedReasoning =
+ matchingReasoningParts === 1 &&
+ content[rebasedStartIndex]?.type === ContentTypes.THINK &&
+ textValue(content[rebasedStartIndex]?.think).includes(needle);
+ /** Older snapshots kept only the raw window. Replaying it through the
+ * stream filter keeps a payload prefix that was still undecided at the
+ * snapshot in the cursor, where the continuation can classify it; a
+ * full window may have been cut inside a payload, so its leading run
+ * is dropped. */
+ const classified =
+ readable != null && cursor != null
+ ? { readable, cursor }
+ : advanceOpaqueReasoning(
+ OPAQUE_REASONING_START,
+ text.length > MAX_EXCERPT_CHARS || boundedText.length === MAX_EXCERPT_CHARS
+ ? trimTruncatedReasoning(boundedText)
+ : boundedText,
+ );
+ return [
+ key,
+ {
+ text: boundedText,
+ readable: classified.readable,
+ cursor: classified.cursor,
+ ...(agentId != null && { agentId }),
+ ...(hasMaterializedReasoning && { startIndex: rebasedStartIndex }),
+ },
+ ] as const;
+ },
+ ),
);
const reasoningStepKeys = new Map<string, string>();
const stepKinds = new Map<
@@ -1012,15 +1081,15 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
pendingReasoning: [...pendingReasoning].map(([key, reasoning]) => ({
key,
text: reasoning.text.slice(-MAX_EXCERPT_CHARS),
+ readable: reasoning.readable,
+ cursor: reasoning.cursor,
...(reasoning.agentId != null && { agentId: reasoning.agentId }),
...(reasoning.startIndex != null && { startIndex: reasoning.startIndex }),
})),
});
const addPendingReasoning = (onlyKey?: string) => {
- let selected = [...pendingReasoning.entries()] as Array<
- readonly [string, { text: string; agentId?: string; startIndex?: number }]
- >;
+ let selected = [...pendingReasoning.entries()] as Array<readonly [string, PendingReasoning]>;
if (onlyKey != null) {
const reasoning = pendingReasoning.get(onlyKey);
selected = reasoning == null ? [] : [[onlyKey, reasoning] as const];
@@ -1029,8 +1098,10 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
const text = reasoning.text.trim();
if (text) {
const parts = deps.getContentParts();
+ const readable = readableExcerpt(reasoning);
trackActivity({
thinkingExcerpts: [text.slice(0, MAX_EXCERPT_CHARS)],
+ readableExcerpts: readable ? [readable] : [],
...(reasoning.agentId != null && { agentId: reasoning.agentId }),
status: 'success',
startIndex: findReasoningStart(parts, text, reasoning.startIndex),
@@ -1077,11 +1148,13 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
.find(matchesToolIds);
}
if (!matchesToolIds(child)) {
- return activity;
+ return withReadableReasoning(activity);
}
const label =
child?.pending !== true ? textValue(child?.[ContentTypes.ACTIVITY_LABEL]).trim() : '';
- return label ? { ...activity, label, entries: undefined } : activity;
+ return withReadableReasoning(
+ label ? { ...activity, label, entries: undefined } : activity,
+ );
},
);
};
@@ -1358,7 +1431,9 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
* material agent transition. `input.agentId` above still excludes nested
* subagent internals. */
const reasoningKey = input.executingAgentId ?? 'root';
- const reasoning = pendingReasoning.get(reasoningKey)?.text.trim();
+ const pendingLane = pendingReasoning.get(reasoningKey);
+ const reasoning = pendingLane?.text.trim();
+ const readableReasoning = pendingLane != null ? readableExcerpt(pendingLane) : '';
let trackedEntries = input.entries;
let ids = new Set(trackedEntries.map((entry) => entry.toolUseId));
const parts = deps.getContentParts();
@@ -1443,7 +1518,12 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
const awaitingMaterialization = batchStartIndex == null;
trackActivity({
entries,
- ...(reasoning ? { thinkingExcerpts: [reasoning.slice(0, MAX_EXCERPT_CHARS)] } : {}),
+ ...(reasoning
+ ? {
+ thinkingExcerpts: [reasoning.slice(0, MAX_EXCERPT_CHARS)],
+ readableExcerpts: readableReasoning ? [readableReasoning] : [],
+ }
+ : {}),
...(input.executingAgentId != null && { agentId: input.executingAgentId }),
status: activityStatus,
startIndex: trackedStartIndex,
@@ -1492,6 +1572,8 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
const startIndex = deps.getStepIndex?.(step.id);
pendingReasoning.set(reasoningKey, {
text: '',
+ readable: '',
+ cursor: OPAQUE_REASONING_START,
...(step.agentId != null && { agentId: step.agentId }),
...(startIndex != null && { startIndex }),
});
@@ -1584,9 +1666,13 @@ export function createActivityPhaseWiring(deps: ActivityPhaseHostDeps): Activity
const reasoningKey = id ? reasoningStepKeys.get(id) : undefined;
const reasoning = reasoningKey ? pendingReasoning.get(reasoningKey) : undefined;
if (reasoning != null) {
- reasoning.text = `${reasoning.text}${deltaText(data, 'think')}`.slice(
- -MAX_EXCERPT_CHARS,
- );
+ const think = deltaText(data, 'think');
+ reasoning.text = `${reasoning.text}${think}`.slice(-MAX_EXCERPT_CHARS);
+ /** Classified before bounding: a window can cut a payload into a
+ * fragment too short to recognize, so evidence never comes from it. */
+ const next = advanceOpaqueReasoning(reasoning.cursor, think);
+ reasoning.cursor = next.cursor;
+ reasoning.readable = `${reasoning.readable}${next.readable}`.slice(-MAX_EXCERPT_CHARS);
}
return reasoningHandler.handle(event, data, metadata, graph);
},
diff --git a/packages/api/src/agents/reasoningLabels/host.spec.ts b/packages/api/src/agents/reasoningLabels/host.spec.ts
index 7ca2808e64..cc06643f78 100644
--- a/packages/api/src/agents/reasoningLabels/host.spec.ts
+++ b/packages/api/src/agents/reasoningLabels/host.spec.ts
@@ -1,4 +1,5 @@
-import { ContentTypes } from 'librechat-data-provider';
+import { GraphEvents } from '@librechat/agents';
+import { ContentTypes, StepTypes, ReasoningLabelEvents } from 'librechat-data-provider';
import {
createReasoningLabelHostWiring,
getLabelUsageSequenceSeed,
@@ -66,4 +67,48 @@ describe('reasoning label host', () => {
expect(markResumable).toHaveBeenCalledTimes(2);
expect(onMarkFailure).not.toHaveBeenCalled();
});
+
+ it('emits a same-step reset for a resumed step titled from an encoded payload', async () => {
+ const encoded = Buffer.from(Array.from({ length: 900 }, (_, i) => (i * 37 + 3) % 256)).toString(
+ 'base64',
+ );
+ const parts = [
+ {
+ type: ContentTypes.THINK,
+ think: encoded,
+ reasoning_label: 'Inspecting an encoded data snapshot',
+ reasoning_label_step_id: 'reasoning-1',
+ reasoning_label_revision: 1,
+ reasoning_label_attempts: 1,
+ },
+ ];
+ const emitEvent = jest.fn(async () => undefined);
+ const pending: Promise<void>[] = [];
+ const host = createHost({
+ seedFromContent: true,
+ getContentParts: () => parts,
+ getStepIndex: (stepId) => (stepId === 'reasoning-1' ? 0 : undefined),
+ emitEvent,
+ trackPendingFill: (task) => pending.push(task),
+ });
+ const handlers = host.wiring.handlers({
+ [GraphEvents.ON_RUN_STEP]: { handle: () => undefined },
+ });
+ await handlers?.[GraphEvents.ON_RUN_STEP]?.handle(GraphEvents.ON_RUN_STEP, {
+ id: 'reasoning-1',
+ stepDetails: {
+ type: StepTypes.MESSAGE_CREATION,
+ message_creation: { content_type: ContentTypes.THINK },
+ },
+ } as never);
+ await Promise.all(pending);
+
+ expect(emitEvent).toHaveBeenCalledWith(ReasoningLabelEvents.ON_REASONING_LABEL, {
+ index: 0,
+ stepId: 'reasoning-1',
+ previousStepId: 'reasoning-1',
+ attempts: 1,
+ reset: true,
+ });
+ });
});
diff --git a/packages/api/src/agents/reasoningLabels/host.ts b/packages/api/src/agents/reasoningLabels/host.ts
index f4e49875a4..cf4f8a5404 100644
--- a/packages/api/src/agents/reasoningLabels/host.ts
+++ b/packages/api/src/agents/reasoningLabels/host.ts
@@ -5,6 +5,7 @@ import type {
GeneratedReasoningLabel,
GenerateReasoningLabelPayload,
ReasoningLabelAttemptEvent,
+ ReasoningLabelResetEvent,
ReasoningLabelEvent,
ReasoningLabelHostDeps,
ReasoningLabelStatus,
@@ -89,7 +90,10 @@ export interface ReasoningLabelHostScope {
detach?: () => void;
}
-type ReasoningLabelDurableEvent = ReasoningLabelAttemptEvent | ReasoningLabelEvent;
+type ReasoningLabelDurableEvent =
+ | ReasoningLabelAttemptEvent
+ | ReasoningLabelEvent
+ | (ReasoningLabelResetEvent & { reset: true });
export interface CreateReasoningLabelHostWiringOptions {
config: ResolvedReasoningLabelConfig;
@@ -197,6 +201,8 @@ export function createReasoningLabelHostWiring({
getStepIndex,
emitAttemptEvent: (event) => emitEvent(ReasoningLabelEvents.ON_REASONING_LABEL_ATTEMPT, event),
emitLabelEvent: (event) => emitEvent(ReasoningLabelEvents.ON_REASONING_LABEL, event),
+ emitResetEvent: (event) =>
+ emitEvent(ReasoningLabelEvents.ON_REASONING_LABEL, { ...event, reset: true }),
trackPendingFill,
generateLabel,
});
diff --git a/packages/api/src/agents/reasoningLabels/index.ts b/packages/api/src/agents/reasoningLabels/index.ts
index 2a5d8fae4b..9b48925d26 100644
--- a/packages/api/src/agents/reasoningLabels/index.ts
+++ b/packages/api/src/agents/reasoningLabels/index.ts
@@ -10,6 +10,7 @@ export type {
ReasoningLabelAttemptEvent,
ReasoningLabelEvent,
ReasoningLabelHostDeps,
+ ReasoningLabelResetEvent,
ReasoningLabelStatus,
ReasoningLabelWiring,
} from './runtime';
diff --git a/packages/api/src/agents/reasoningLabels/runtime.spec.ts b/packages/api/src/agents/reasoningLabels/runtime.spec.ts
index 20aea85c12..ad667b8306 100644
--- a/packages/api/src/agents/reasoningLabels/runtime.spec.ts
+++ b/packages/api/src/agents/reasoningLabels/runtime.spec.ts
@@ -4,11 +4,14 @@ import type { EventHandler } from '@librechat/agents';
import type {
GeneratedReasoningLabel,
ReasoningLabelAttemptEvent,
+ ReasoningLabelResetEvent,
ReasoningLabelEvent,
} from './runtime';
import type { LooseContentPart } from '~/agents/activityLabels/wiring';
import { createReasoningLabelWiring, synthesizeReasoningLabelGapEvents } from './runtime';
+/** Test deltas end on a separator: the payload filter holds an unfinished
+ * word until the next non-payload character settles it. */
function reasoningDelta(id: string, think: string) {
return { id, delta: { content: { type: ContentTypes.THINK, think } } };
}
@@ -30,6 +33,7 @@ function createHarness(
preservePartOnStart?: boolean;
emitAttemptEvent?: (event: ReasoningLabelAttemptEvent) => Promise<void>;
emitLabelEvent?: (event: ReasoningLabelEvent) => Promise<void>;
+ emitResetEvent?: (event: ReasoningLabelResetEvent) => Promise<void>;
} = {},
) {
const { initialParts, preservePartOnStart, emitAttemptEvent, emitLabelEvent, ...wiringConfig } =
@@ -127,15 +131,15 @@ describe('reasoning labels', () => {
const generate = jest.fn(async () => ({ label: 'Tracing resume ownership' }));
const harness = createHarness(generate, { minChars: 10, updateChars: 8, updateIntervalMs: 0 });
await harness.start();
- await harness.append('123456789');
+ await harness.append('12345678 ');
expect(generate).not.toHaveBeenCalled();
- await harness.append('0');
+ await harness.append('.');
await harness.settle();
expect(generate).toHaveBeenCalledTimes(1);
expect(generate).toHaveBeenCalledWith(
- expect.objectContaining({ revision: 1, status: 'streaming', visibleReasoning: '1234567890' }),
+ expect.objectContaining({ revision: 1, status: 'streaming', visibleReasoning: '12345678 .' }),
);
expect(harness.parts).toHaveLength(1);
expect(harness.parts[0]).toMatchObject({
@@ -168,16 +172,16 @@ describe('reasoning labels', () => {
await harness.settle();
expect(generate).not.toHaveBeenCalled();
- await harness.append('1234', 'reasoning-new');
+ await harness.append('123 ', 'reasoning-new');
await harness.settle();
expect(generate).not.toHaveBeenCalled();
- await harness.append('5', 'reasoning-new');
+ await harness.append('.', 'reasoning-new');
await harness.settle();
expect(generate).toHaveBeenCalledTimes(1);
expect(generate).toHaveBeenCalledWith(
- expect.objectContaining({ visibleReasoning: '12345', revision: 2 }),
+ expect.objectContaining({ visibleReasoning: '123 .', revision: 2 }),
);
});
@@ -195,9 +199,9 @@ describe('reasoning labels', () => {
updateIntervalMs: 3_000,
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
- await harness.append('6789');
+ await harness.append('678 ');
expect(generate).toHaveBeenCalledTimes(1);
await jest.advanceTimersByTimeAsync(3_000);
@@ -224,9 +228,9 @@ describe('reasoning labels', () => {
updateIntervalMs: 3_000,
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
- await harness.append('6789');
+ await harness.append('678 ');
expect(generate).toHaveBeenCalledTimes(1);
await jest.advanceTimersByTimeAsync(3_000);
@@ -245,7 +249,7 @@ describe('reasoning labels', () => {
updateIntervalMs: 0,
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
expect(collectUsage).toHaveBeenCalledTimes(1);
@@ -264,7 +268,7 @@ describe('reasoning labels', () => {
},
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
expect(collectUsage).toHaveBeenCalledTimes(1);
@@ -302,7 +306,7 @@ describe('reasoning labels', () => {
},
);
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await usageStarted;
await emitStarted;
await Promise.resolve();
@@ -354,9 +358,9 @@ describe('reasoning labels', () => {
},
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await streamingEmit;
- await harness.append('x'.repeat(120));
+ await harness.append(' word'.repeat(24));
await harness.close();
await terminalEmit;
await new Promise((resolve) => setImmediate(resolve));
@@ -377,13 +381,13 @@ describe('reasoning labels', () => {
const generate = jest.fn(async () => ({ label: 'Tracing the streaming path' }));
const harness = createHarness(generate, { minChars: 5, updateChars: 100, updateIntervalMs: 0 });
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
- await harness.append(' later');
+ await harness.append(' later.');
expect(harness.parts[0]).toMatchObject({
- think: '12345 later',
+ think: '1234 later.',
reasoning_label: 'Tracing the streaming path',
reasoning_label_step_id: 'reasoning-1',
reasoning_label_revision: 1,
@@ -410,15 +414,15 @@ describe('reasoning labels', () => {
},
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await emitStarted;
- await harness.append(' later');
+ await harness.append(' later.');
releaseEmit?.();
await harness.settle();
expect(harness.parts[0]).toMatchObject({
- think: '12345 later',
+ think: '1234 later.',
reasoning_label: 'Tracing the concurrent stream',
reasoning_label_revision: 1,
});
@@ -443,7 +447,7 @@ describe('reasoning labels', () => {
},
});
await harness.start('reasoning-1', 0);
- await harness.append('12345', 'reasoning-1');
+ await harness.append('1234 ', 'reasoning-1');
await emitStarted;
await harness.start('reasoning-2', 0);
@@ -477,7 +481,7 @@ describe('reasoning labels', () => {
},
});
await harness.start('reasoning-1', 0);
- await harness.append('12345', 'reasoning-1');
+ await harness.append('1234 ', 'reasoning-1');
await emitStarted;
await harness.start('reasoning-2', 0);
@@ -504,8 +508,8 @@ describe('reasoning labels', () => {
.mockResolvedValueOnce({ label: 'Resolved the final direction' });
const harness = createHarness(generate, { minChars: 5, updateChars: 4, updateIntervalMs: 0 });
await harness.start();
- await harness.append('12345');
- await harness.append('6789');
+ await harness.append('1234 ');
+ await harness.append('678 ');
await harness.close();
expect(generate).toHaveBeenCalledTimes(1);
@@ -517,7 +521,7 @@ describe('reasoning labels', () => {
revision: 2,
status: 'complete',
previousLabel: 'Investigating the direction',
- visibleReasoning: '123456789',
+ visibleReasoning: '1234 678',
});
expect(harness.events[harness.events.length - 1]).toMatchObject({
revision: 2,
@@ -530,9 +534,9 @@ describe('reasoning labels', () => {
const generate = jest.fn(async () => ({ label: 'Inspecting the stream' }));
const harness = createHarness(generate, { minChars: 5, updateChars: 400, updateIntervalMs: 0 });
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
- await harness.append('small tail');
+ await harness.append('small tail.');
await harness.close();
await harness.settle();
@@ -554,9 +558,9 @@ describe('reasoning labels', () => {
updateIntervalMs: 3_000,
});
await harness.start();
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.settle();
- await harness.append('x'.repeat(120));
+ await harness.append(' word'.repeat(24));
await harness.close();
await harness.settle();
@@ -577,7 +581,7 @@ describe('reasoning labels', () => {
last_agent_id: 'final-agent',
langgraph_node: 'intermediate-agent',
});
- await harness.append('12345');
+ await harness.append('1234 ');
await harness.close();
await harness.settle();
@@ -594,9 +598,9 @@ describe('reasoning labels', () => {
maxPerRun: 2,
});
await firstSegment.start('reasoning-1', 0);
- await firstSegment.append('12345');
+ await firstSegment.append('1234 ');
await firstSegment.settle();
- await firstSegment.append('6789');
+ await firstSegment.append('678 ');
await firstSegment.settle();
expect(generate).toHaveBeenCalledTimes(2);
@@ -615,7 +619,7 @@ describe('reasoning labels', () => {
initialParts: firstSegment.parts,
});
await resumed.start('reasoning-2', 1);
- await resumed.append('abcde', 'reasoning-2');
+ await resumed.append('abcd ', 'reasoning-2');
await resumed.close('reasoning-2');
await resumed.settle();
@@ -641,7 +645,7 @@ describe('reasoning labels', () => {
});
await resumed.start('reasoning-after-resume', initialParts.length);
- await resumed.append('abcde', 'reasoning-after-resume');
+ await resumed.append('abcd ', 'reasoning-after-resume');
await resumed.settle();
expect(generate).toHaveBeenCalledTimes(1);
@@ -667,7 +671,7 @@ describe('reasoning labels', () => {
],
});
await harness.start('reasoning-new', 1);
- await harness.append('abcde', 'reasoning-new');
+ await harness.append('abcd ', 'reasoning-new');
await harness.settle();
expect(generate).toHaveBeenCalledTimes(1);
@@ -683,9 +687,9 @@ describe('reasoning labels', () => {
maxPerRun: 2,
});
await firstSegment.start('reasoning-1', 0);
- await firstSegment.append('12345', 'reasoning-1');
+ await firstSegment.append('1234 ', 'reasoning-1');
await firstSegment.settle();
- await firstSegment.append('6789', 'reasoning-1');
+ await firstSegment.append('678 ', 'reasoning-1');
await firstSegment.settle();
await firstSegment.start('reasoning-2', 0);
@@ -705,7 +709,7 @@ describe('reasoning labels', () => {
initialParts: firstSegment.parts.map((part) => ({ ...part })),
});
await resumed.start('reasoning-3', 1);
- await resumed.append('vwxyz', 'reasoning-3');
+ await resumed.append('vwxy ', 'reasoning-3');
await resumed.close('reasoning-3');
await resumed.settle();
@@ -732,12 +736,12 @@ describe('reasoning labels', () => {
await harness.start('reasoning-1', 0);
expect(generate).not.toHaveBeenCalled();
- await harness.append('89', 'reasoning-1');
+ await harness.append('8 ', 'reasoning-1');
await harness.settle();
expect(generate).toHaveBeenCalledTimes(1);
expect(generate).toHaveBeenCalledWith(
- expect.objectContaining({ visibleReasoning: '123456789', revision: 2 }),
+ expect.objectContaining({ visibleReasoning: '12345678', revision: 2 }),
);
});
@@ -873,3 +877,267 @@ describe('reasoning labels', () => {
]);
});
});
+
+/** Bedrock's Converse stream returns OpenAI (and redacted Anthropic) reasoning
+ * as `reasoningContent.redactedContent` bytes; the SDK base64-encodes them and
+ * surfaces the result as THINK text. */
+const encodedReasoning = (bytes = 1_050, seed = 7): string =>
+ Buffer.from(Array.from({ length: bytes }, (_, i) => (i * 37 + seed) % 256)).toString('base64');
+
+describe('reasoning labels for encoded reasoning', () => {
+ const config = { minChars: 10, updateChars: 8, updateIntervalMs: 0 };
+
+ it('never labels a step whose reasoning is an encoded payload', async () => {
+ const generate = jest.fn(async () => ({ label: 'Inspecting an encoded data snapshot' }));
+ const harness = createHarness(generate, config);
+ await harness.start();
+ await harness.append(encodedReasoning());
+ await harness.append(encodedReasoning(300, 11));
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).not.toHaveBeenCalled();
+ expect(harness.attemptEvents).toEqual([]);
+ expect(harness.events).toEqual([]);
+ expect(harness.parts[0]?.reasoning_label).toBeUndefined();
+ });
+
+ it('skips an encoded payload that arrives split across small deltas', async () => {
+ const generate = jest.fn(async () => ({ label: 'Inspecting an encoded data snapshot' }));
+ const harness = createHarness(generate, { updateChars: 400, updateIntervalMs: 0 });
+ const payload = encodedReasoning();
+ await harness.start();
+ for (let offset = 0; offset < payload.length; offset += 100) {
+ await harness.append(payload.slice(offset, offset + 100));
+ }
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).not.toHaveBeenCalled();
+ expect(harness.attemptEvents).toEqual([]);
+ });
+
+ it('never spends label revisions on a payload split across small deltas', async () => {
+ const generate = jest.fn(async () => ({ label: 'Checking the retry policy' }));
+ const harness = createHarness(generate, { minChars: 10, updateChars: 50, updateIntervalMs: 0 });
+ const payload = encodedReasoning();
+ await harness.start();
+ await harness.append('Checking the retry policy against the queue backoff. ');
+ await harness.settle();
+ expect(generate).toHaveBeenCalledTimes(1);
+
+ for (let offset = 0; offset < payload.length; offset += 100) {
+ await harness.append(payload.slice(offset, offset + 100));
+ await harness.settle();
+ }
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalledTimes(1);
+ expect(harness.parts[0]).toMatchObject({
+ reasoning_label: 'Checking the retry policy',
+ reasoning_label_status: 'complete',
+ });
+ });
+
+ it('never labels the undecided start of a payload under low thresholds', async () => {
+ const generate = jest.fn(async () => ({ label: 'Inspecting an encoded data snapshot' }));
+ const harness = createHarness(generate, { minChars: 5, updateChars: 4, updateIntervalMs: 0 });
+ const payload = encodedReasoning();
+ await harness.start();
+ for (let offset = 0; offset < payload.length; offset += 10) {
+ await harness.append(payload.slice(offset, offset + 10));
+ await harness.settle();
+ }
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).not.toHaveBeenCalled();
+ expect(harness.attemptEvents).toEqual([]);
+ });
+
+ it('settles a final unpunctuated word when the step closes', async () => {
+ const generate = jest.fn(async () => ({ label: 'Checking the result' }));
+ const harness = createHarness(generate, { minChars: 5, updateChars: 4, updateIntervalMs: 0 });
+ await harness.start();
+ await harness.append('Checking');
+ await harness.settle();
+ expect(generate).not.toHaveBeenCalled();
+
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalledWith(
+ expect.objectContaining({ status: 'complete', visibleReasoning: 'Checking' }),
+ );
+ });
+
+ it('labels mixed reasoning from its readable text only', async () => {
+ const generate = jest.fn(async () => ({ label: 'Comparing retry backoff strategies' }));
+ const harness = createHarness(generate, { ...config, updateChars: 1_000 });
+ const payload = encodedReasoning();
+ await harness.start();
+ await harness.append('Comparing the retry policy against the queue backoff. ');
+ await harness.settle();
+ await harness.append(payload);
+ await harness.append(' Exponential backoff fits the burst pattern, so the queue keeps it. ');
+ await harness.append(
+ 'Jitter spreads the retries across the window to avoid a thundering herd.',
+ );
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalledTimes(2);
+ for (const [payloadArg] of generate.mock.calls as unknown as Array<
+ [{ visibleReasoning: string }]
+ >) {
+ expect(payloadArg.visibleReasoning).not.toContain(payload.slice(0, 64));
+ }
+ expect(generate).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ status: 'complete',
+ visibleReasoning: expect.stringContaining('backoff. Exponential backoff fits'),
+ }),
+ );
+ expect(harness.parts[0]?.reasoning_label).toBe('Comparing retry backoff strategies');
+ });
+
+ it('still labels readable reasoning of the same length', async () => {
+ const generate = jest.fn(async () => ({ label: 'Planning the cache migration' }));
+ const harness = createHarness(generate, config);
+ const readable =
+ 'Planning the cache migration: move session keys first, then rebuild the index. ';
+ await harness.start();
+ await harness.append(readable.repeat(18));
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalledWith(
+ expect.objectContaining({ visibleReasoning: expect.stringContaining('Planning the cache') }),
+ );
+ expect(harness.parts[0]?.reasoning_label).toBe('Planning the cache migration');
+ });
+
+ it('ignores a persisted encoded payload when a resumed step continues', async () => {
+ const generate = jest.fn(async () => ({ label: 'Checking the resumed tool result' }));
+ const harness = createHarness(generate, {
+ ...config,
+ preservePartOnStart: true,
+ initialParts: [
+ {
+ type: ContentTypes.THINK,
+ think: encodedReasoning(),
+ reasoning_label_step_id: 'reasoning-1',
+ reasoning_label_attempts: 0,
+ },
+ ],
+ });
+ await harness.start();
+ await harness.settle();
+ expect(generate).not.toHaveBeenCalled();
+
+ await harness.append(' Checking the tool result.');
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalledTimes(1);
+ expect(generate).toHaveBeenCalledWith(
+ expect.objectContaining({ visibleReasoning: 'Checking the tool result.' }),
+ );
+ });
+ it('drops a payload prefix that was still undecided when a resumed step continues', async () => {
+ const generate = jest.fn(async () => ({ label: 'Rolling back the deploy' }));
+ const payload = encodedReasoning();
+ const harness = createHarness(generate, {
+ minChars: 30,
+ updateChars: 8,
+ updateIntervalMs: 0,
+ preservePartOnStart: true,
+ initialParts: [
+ {
+ type: ContentTypes.THINK,
+ think: `Checking. ${payload.slice(0, 100)}`,
+ reasoning_label_step_id: 'reasoning-1',
+ reasoning_label_attempts: 0,
+ },
+ ],
+ });
+ await harness.start();
+ await harness.append(payload.slice(100));
+ await harness.append(' Rolling back the deploy now.');
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).toHaveBeenCalled();
+ for (const [payloadArg] of generate.mock.calls as unknown as Array<
+ [{ visibleReasoning: string }]
+ >) {
+ expect(payloadArg.visibleReasoning).not.toContain(payload.slice(0, 32));
+ expect(payloadArg.visibleReasoning).toContain('Rolling back the deploy now.');
+ }
+ });
+ it('does not revive a payload-derived title when a resumed step continues briefly', async () => {
+ const generate = jest.fn(async () => ({ label: 'unused' }));
+ const emitResetEvent = jest.fn(async () => undefined);
+ const harness = createHarness(generate, {
+ updateIntervalMs: 0,
+ emitResetEvent,
+ preservePartOnStart: true,
+ initialParts: [
+ {
+ type: ContentTypes.THINK,
+ think: encodedReasoning(),
+ reasoning_label: 'Inspecting an encoded data snapshot',
+ reasoning_label_step_id: 'reasoning-1',
+ reasoning_label_revision: 1,
+ reasoning_label_status: 'complete',
+ reasoning_label_attempts: 1,
+ },
+ ],
+ });
+ await harness.start();
+ await harness.append(' Checking the tool result.');
+ await harness.close();
+ await harness.settle();
+
+ expect(generate).not.toHaveBeenCalled();
+ expect(harness.parts[0]?.reasoning_label).toBeUndefined();
+ expect(harness.events).toEqual([]);
+ /** The resumed client still holds the snapshot title for this same step. */
+ expect(emitResetEvent).toHaveBeenCalledWith({
+ index: 0,
+ stepId: 'reasoning-1',
+ previousStepId: 'reasoning-1',
+ attempts: 1,
+ });
+ });
+
+ it('keeps the title of a resumed step whose reasoning is a short readable token', async () => {
+ const generate = jest.fn(async () => ({ label: 'unused' }));
+ const emitResetEvent = jest.fn(async () => undefined);
+ const harness = createHarness(generate, {
+ minChars: 5,
+ updateIntervalMs: 0,
+ emitResetEvent,
+ preservePartOnStart: true,
+ initialParts: [
+ {
+ type: ContentTypes.THINK,
+ think: 'Investigating',
+ reasoning_label: 'Investigating the failure',
+ reasoning_label_step_id: 'reasoning-1',
+ reasoning_label_revision: 1,
+ reasoning_label_status: 'streaming',
+ reasoning_label_attempts: 1,
+ reasoning_label_submitted_chars: 13,
+ },
+ ],
+ });
+ await harness.start();
+ await harness.close();
+ await harness.settle();
+
+ expect(emitResetEvent).not.toHaveBeenCalled();
+ expect(harness.parts[0]?.reasoning_label).toBe('Investigating the failure');
+ });
+});
diff --git a/packages/api/src/agents/reasoningLabels/runtime.ts b/packages/api/src/agents/reasoningLabels/runtime.ts
index 8bb8c9e731..7c2b2535d7 100644
--- a/packages/api/src/agents/reasoningLabels/runtime.ts
+++ b/packages/api/src/agents/reasoningLabels/runtime.ts
@@ -1,5 +1,13 @@
import { GraphEvents } from '@librechat/agents';
-import { ContentTypes, StepTypes } from 'librechat-data-provider';
+import {
+ StepTypes,
+ ContentTypes,
+ flushOpaqueReasoning,
+ isOpaqueReasoning,
+ advanceOpaqueReasoning,
+ OPAQUE_REASONING_START,
+} from 'librechat-data-provider';
+import type { OpaqueReasoningCursor } from 'librechat-data-provider';
import type { EventHandler } from '@librechat/agents';
import type { LooseContentPart } from '~/agents/activityLabels/wiring';
@@ -39,6 +47,14 @@ export interface ReasoningLabelAttemptEvent {
submittedChars: number;
}
+/** Clears a title the client already holds for a step that keeps its identity. */
+export interface ReasoningLabelResetEvent {
+ index: number;
+ stepId: string;
+ previousStepId: string;
+ attempts: number;
+}
+
export interface ReasoningLabelHostDeps {
minChars?: number;
updateChars?: number;
@@ -53,6 +69,7 @@ export interface ReasoningLabelHostDeps {
getStepIndex: (stepId: string) => number | undefined;
emitAttemptEvent: (event: ReasoningLabelAttemptEvent) => Promise<unknown>;
emitLabelEvent: (event: ReasoningLabelEvent) => Promise<unknown>;
+ emitResetEvent?: (event: ReasoningLabelResetEvent) => Promise<unknown>;
trackPendingFill: (fillDone: Promise<void>) => void;
isClosed?: () => boolean;
generateLabel: (payload: GenerateReasoningLabelPayload) => Promise<GeneratedReasoningLabel>;
@@ -76,8 +93,13 @@ interface ReasoningStepState {
stepId: string;
agentId?: string;
index?: number;
+ /** Readable reasoning only; encoded provider payloads never enter it. */
text: string;
+ /** Settled readable characters only, so encoded payloads never advance the gates. */
totalChars: number;
+ /** Trailing run not yet classified as a word or a payload; it joins `text`
+ * once a later character settles it. */
+ cursor: OpaqueReasoningCursor;
label?: string;
labelStatus?: ReasoningLabelStatus;
revision: number;
@@ -101,6 +123,7 @@ const MAX_TRACKED_REASONING_CHARS = 8_000;
* completion rewrite; a smaller tail only upgrades the current status. */
const FINAL_UPDATE_CHAR_LIMIT = 120;
const OUTPUT_CHAR_LIMIT = 120;
+const THINK_TAGS = /<\/?think>/g;
const REQUEST_TIMEOUT_MS = 12_000;
function textValue(value: unknown): string {
@@ -390,9 +413,16 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
}
clearTimer(state);
+ const visibleReasoning = state.text.trim();
+ if (!visibleReasoning) {
+ if (state.pendingFinal) {
+ state.pendingFinal = false;
+ markComplete(state);
+ }
+ return;
+ }
const status: ReasoningLabelStatus = state.pendingFinal ? 'complete' : 'streaming';
state.pendingFinal = false;
- const visibleReasoning = state.text.trim();
state.submittedChars = state.totalChars;
state.lastSubmittedAt = now();
generated += 1;
@@ -494,6 +524,14 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
}
state.closed = true;
clearTimer(state);
+ /** The stream is over, so a short undecided tail was a final word; a
+ * longer one is the unfinished start of a payload. */
+ const word = flushOpaqueReasoning(state.cursor);
+ if (word) {
+ state.text = appendBoundedReasoning(state.text, word);
+ state.totalChars += word.length;
+ }
+ state.cursor = OPAQUE_REASONING_START;
schedule(state, true);
};
@@ -531,11 +569,28 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
const part = index != null ? deps.getContentParts()[index] : undefined;
const ownsExistingStep =
part?.type === ContentTypes.THINK && part.reasoning_label_step_id === step.id;
+ /** Replayed through the stream filter so a payload that was still
+ * undecided at the snapshot keeps its cursor and is dropped once the
+ * continuation classifies it, instead of settling as readable text. */
+ const resumed = ownsExistingStep
+ ? advanceOpaqueReasoning(
+ OPAQUE_REASONING_START,
+ textValue(part?.think).replace(THINK_TAGS, ''),
+ )
+ : { readable: '', cursor: OPAQUE_REASONING_START };
+ const existingText = resumed.readable;
+ /** A title on a step whose reasoning is entirely an encoded payload can
+ * only describe that payload (labeled before payloads were skipped), so it
+ * is not carried forward where a short readable continuation would
+ * revive it. */
+ const staleLabel =
+ ownsExistingStep &&
+ typeof part.reasoning_label === 'string' &&
+ isOpaqueReasoning(textValue(part.think));
const existingLabel =
- ownsExistingStep && typeof part.reasoning_label === 'string'
+ ownsExistingStep && !staleLabel && typeof part.reasoning_label === 'string'
? part.reasoning_label
: undefined;
- const existingText = ownsExistingStep ? textValue(part?.think) : '';
const persistedSubmittedChars =
ownsExistingStep && typeof part.reasoning_label_submitted_chars === 'number'
? Math.min(part.reasoning_label_submitted_chars, existingText.trim().length)
@@ -554,6 +609,7 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
...(index != null && { index }),
text: appendBoundedReasoning('', existingText),
totalChars: existingText.trim().length,
+ cursor: resumed.cursor,
...(existingLabel != null && { label: existingLabel }),
...(existingLabel != null && {
labelStatus: part?.reasoning_label_status === 'complete' ? 'complete' : 'streaming',
@@ -572,19 +628,37 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
delete part.reasoning_label_revision;
delete part.reasoning_label_status;
delete part.reasoning_label_submitted_chars;
+ } else if (staleLabel) {
+ delete part.reasoning_label;
+ delete part.reasoning_label_status;
}
Object.assign(part, {
reasoning_label_step_id: step.id,
reasoning_label_attempts: generated,
});
}
+ if (staleLabel && index != null && deps.emitResetEvent != null) {
+ /** The resumed client still renders the snapshot's title; the step keeps
+ * its identity, so only an explicit reset clears it. */
+ const resetTask = Promise.resolve(
+ deps.emitResetEvent({
+ index,
+ stepId: step.id,
+ previousStepId: step.id,
+ attempts: generated,
+ }),
+ )
+ .then(() => undefined)
+ .catch(() => undefined);
+ deps.trackPendingFill(resetTask);
+ }
schedule(state);
};
const appendDelta = (data: unknown): void => {
const event = data as { id?: string };
- const text = deltaText(data);
- if (!event.id || !text) {
+ const rawText = deltaText(data);
+ if (!event.id || !rawText) {
return;
}
const state = steps.get(event.id);
@@ -608,6 +682,11 @@ export function createReasoningLabelWiring(deps: ReasoningLabelHostDeps): Reason
: {}),
});
}
+ const { readable: text, cursor } = advanceOpaqueReasoning(state.cursor, rawText);
+ state.cursor = cursor;
+ if (!text) {
+ return;
+ }
state.text = appendBoundedReasoning(state.text, text);
state.totalChars += text.length;
schedule(state);
diff --git a/packages/data-provider/src/index.ts b/packages/data-provider/src/index.ts
index 3238b8ddab..1c9dc09f08 100644
--- a/packages/data-provider/src/index.ts
+++ b/packages/data-provider/src/index.ts
@@ -17,6 +17,7 @@ export * from './errors';
export * from './runSteps';
/* ui parts */
export * from './parts';
+export * from './reasoning';
export * from './toolTiming';
/* artifacts */
export * from './artifacts';
diff --git a/packages/data-provider/src/reasoning.spec.ts b/packages/data-provider/src/reasoning.spec.ts
new file mode 100644
index 0000000000..1c28407be3
--- /dev/null
+++ b/packages/data-provider/src/reasoning.spec.ts
@@ -0,0 +1,167 @@
+import type { OpaqueReasoningCursor } from './reasoning';
+import {
+ isOpaqueReasoning,
+ stripOpaqueReasoning,
+ readableReasoning,
+ flushOpaqueReasoning,
+ trimTruncatedReasoning,
+ advanceOpaqueReasoning,
+ OPAQUE_REASONING_START,
+} from './reasoning';
+
+const bytes = (length: number, seed: number): Buffer =>
+ Buffer.from(Array.from({ length }, (_, i) => (i * 37 + seed) % 256));
+
+/** Bedrock Converse `reasoningContent.redactedContent`, base64-encoded by the SDK
+ * (OpenAI models on Bedrock, and Claude's redacted thinking on Bedrock). */
+const bedrockRedacted = bytes(1_050, 3).toString('base64');
+/** Anthropic `redacted_thinking.data`. */
+const anthropicRedacted = `EmwKAhgBEgy${bytes(600, 5).toString('base64')}`;
+/** OpenAI Responses `reasoning.encrypted_content` (base64url Fernet token). */
+const openAIEncrypted = `gAAAAABo${bytes(900, 9).toString('base64url')}`;
+/** Gemini `thoughtSignature`. */
+const geminiSignature = `CiQB${bytes(300, 13).toString('base64')}`;
+
+const readable = [
+ 'The user wants the deploy rolled back. First I will check which revision is live, then compare it with the last healthy one.',
+ 'Plan:\n1. read `config/app.ts`\n2. patch `getUser()`\n3. run `npm test -- --watch=false`',
+ 'The docs live at https://www.librechat.ai/docs/configuration/librechat_yaml/object_structure/agents#reasoning-labels and say so.',
+ 'Short',
+ '42',
+];
+
+describe('isOpaqueReasoning', () => {
+ it.each([
+ ['Bedrock redactedContent', bedrockRedacted],
+ ['Anthropic redacted_thinking', anthropicRedacted],
+ ['OpenAI encrypted_content', openAIEncrypted],
+ ['Gemini thought signature', geminiSignature],
+ ['concatenated Bedrock deltas', `${bedrockRedacted}${bytes(400, 21).toString('base64')}`],
+ ['payload with surrounding whitespace', `\n ${bedrockRedacted}\n`],
+ ])('classifies %s as opaque', (_name, text) => {
+ expect(isOpaqueReasoning(text)).toBe(true);
+ });
+
+ it.each(readable.map((text) => [text.slice(0, 40), text]))(
+ 'keeps readable text: %s',
+ (_n, text) => {
+ expect(isOpaqueReasoning(text)).toBe(false);
+ },
+ );
+
+ it('classifies a payload inside the persisted think wrapper as opaque', () => {
+ expect(isOpaqueReasoning(`<think>${bedrockRedacted}</think>`)).toBe(true);
+ expect(isOpaqueReasoning(`<think>\n${anthropicRedacted}\n</think>`)).toBe(true);
+ expect(isOpaqueReasoning('<think>Checking the manifest.</think>')).toBe(false);
+ expect(isOpaqueReasoning('<think></think>')).toBe(false);
+ });
+
+ it('does not treat empty or whitespace-only reasoning as opaque', () => {
+ expect(isOpaqueReasoning('')).toBe(false);
+ expect(isOpaqueReasoning(' \n\t')).toBe(false);
+ });
+
+ it('treats readable reasoning that quotes an encoded value as readable', () => {
+ expect(isOpaqueReasoning(`The header carries ${openAIEncrypted}, so the token is set.`)).toBe(
+ false,
+ );
+ });
+});
+
+describe('stripOpaqueReasoning', () => {
+ it('removes encoded payloads and keeps the readable prose around them', () => {
+ const text = `Checking the retry policy.\n${bedrockRedacted}\nBackoff fits the burst pattern.`;
+ expect(stripOpaqueReasoning(text)).toBe(
+ 'Checking the retry policy.\n\nBackoff fits the burst pattern.',
+ );
+ });
+
+ it('leaves readable reasoning unchanged', () => {
+ for (const text of readable) {
+ expect(stripOpaqueReasoning(text)).toBe(text);
+ }
+ });
+
+ it('returns an empty string for a payload-only text', () => {
+ expect(stripOpaqueReasoning(anthropicRedacted)).toBe('');
+ expect(stripOpaqueReasoning(`<think>${anthropicRedacted}</think>`)).toBe('');
+ });
+});
+
+describe('advanceOpaqueReasoning', () => {
+ const stream = (deltas: string[]): { readable: string; cursor: OpaqueReasoningCursor } =>
+ deltas.reduce(
+ (acc, delta) => {
+ const next = advanceOpaqueReasoning(acc.cursor, delta);
+ return { readable: acc.readable + next.readable, cursor: next.cursor };
+ },
+ { readable: '', cursor: OPAQUE_REASONING_START },
+ );
+ const chunks = (text: string, size: number): string[] =>
+ Array.from({ length: Math.ceil(text.length / size) }, (_, i) =>
+ text.slice(i * size, (i + 1) * size),
+ );
+
+ it('passes readable token deltas through, holding only the unfinished word', () => {
+ const result = stream(['Let', ' me', ' check', ' the', ' manifest.', ' Then', ' roll']);
+ expect(result.readable).toBe('Let me check the manifest. Then ');
+ expect(result.cursor).toEqual({ tail: 'roll', opaque: false });
+ });
+
+ it('drops a payload delivered as one delta', () => {
+ expect(stream([bedrockRedacted]).readable).toBe('');
+ });
+
+ it('drops a payload split into deltas shorter than the threshold', () => {
+ const result = stream(chunks(bedrockRedacted, 50));
+ expect(result.readable).toBe('');
+ expect(result.cursor.opaque).toBe(true);
+ });
+
+ it('keeps readable reasoning on both sides of a split payload', () => {
+ const result = stream([
+ 'Checking the retry policy. ',
+ ...chunks(bedrockRedacted, 100),
+ ' Backoff fits the burst pattern.',
+ ]);
+ expect(result.readable).toBe('Checking the retry policy. Backoff fits the burst pattern.');
+ expect(result.cursor).toEqual({ tail: '', opaque: false });
+ });
+
+ it('never releases the end of a payload that shares a delta with readable text', () => {
+ const payload = chunks(bedrockRedacted, 100);
+ const last = payload.pop() ?? '';
+ const result = stream(['Checking. ', ...payload, `${last}. Rolling back.`]);
+ expect(result.readable).toBe('Checking. . Rolling back.');
+ });
+});
+
+describe('readableReasoning', () => {
+ it('strips payloads from completed reasoning and keeps a long final token', () => {
+ const token = 'a'.repeat(60);
+ expect(readableReasoning(`Done.\n${bedrockRedacted}`)).toBe('Done.\n');
+ expect(readableReasoning(`Hash ${token}`)).toBe(`Hash ${token}`);
+ });
+
+ it('withholds a trailing run that could still become a payload while streaming', () => {
+ const prefix = bedrockRedacted.slice(0, 100);
+ expect(readableReasoning(`Checking. ${prefix}`, true)).toBe('Checking. ');
+ expect(readableReasoning(prefix, true)).toBe('');
+ expect(readableReasoning('Checking the manif', true)).toBe('Checking the manif');
+ });
+
+ it('flushes only a word-sized tail', () => {
+ expect(flushOpaqueReasoning({ tail: 'manifest', opaque: false })).toBe('manifest');
+ expect(flushOpaqueReasoning({ tail: 'a'.repeat(40), opaque: false })).toBe('');
+ expect(flushOpaqueReasoning({ tail: '', opaque: true })).toBe('');
+ });
+});
+
+describe('trimTruncatedReasoning', () => {
+ it('drops a leading fragment and keeps the rest', () => {
+ expect(trimTruncatedReasoning(`${bedrockRedacted.slice(-40)} Rolling back.`)).toBe(
+ ' Rolling back.',
+ );
+ expect(trimTruncatedReasoning(' Rolling back.')).toBe(' Rolling back.');
+ });
+});
diff --git a/packages/data-provider/src/reasoning.ts b/packages/data-provider/src/reasoning.ts
new file mode 100644
index 0000000000..c06d1495a5
--- /dev/null
+++ b/packages/data-provider/src/reasoning.ts
@@ -0,0 +1,123 @@
+/**
+ * Shortest unbroken base64/base64url run treated as an encoded provider
+ * payload instead of reasoning prose. Natural-language reasoning breaks far
+ * sooner on whitespace or punctuation, while encrypted reasoning (Bedrock
+ * `redactedContent`, Anthropic `redacted_thinking.data`, OpenAI
+ * `encrypted_content`, signatures) is one long run of these characters.
+ */
+const OPAQUE_REASONING_MIN_RUN = 128;
+/** Longest undecided tail released as a final word once its stream has ended. */
+const OPAQUE_REASONING_MAX_WORD = 32;
+const OPAQUE_REASONING_RUN = new RegExp(`[A-Za-z0-9+/=_-]{${OPAQUE_REASONING_MIN_RUN},}`, 'g');
+/** Persisted reasoning is stored inside `<think>` tags; they are not readable content. */
+const THINK_TAGS = /<\/?think>/g;
+
+function isPayloadChar(code: number): boolean {
+ return (
+ (code >= 48 && code <= 57) ||
+ (code >= 65 && code <= 90) ||
+ (code >= 97 && code <= 122) ||
+ code === 43 ||
+ code === 47 ||
+ code === 61 ||
+ code === 95 ||
+ code === 45
+ );
+}
+
+/**
+ * Removes encoded/encrypted provider payloads from reasoning text, keeping only
+ * readable prose. Returns an empty string when nothing but the persisted
+ * `<think>` wrapper would remain.
+ */
+export function stripOpaqueReasoning(text: string): string {
+ const stripped = text.replace(OPAQUE_REASONING_RUN, '');
+ if (stripped.length === text.length) {
+ return text;
+ }
+ return stripped.replace(THINK_TAGS, '').trim().length === 0 ? '' : stripped;
+}
+
+/** True when reasoning text has content but none of it is human-readable. */
+export function isOpaqueReasoning(text: string): boolean {
+ return text.replace(THINK_TAGS, '').trim().length > 0 && stripOpaqueReasoning(text) === '';
+}
+
+/** Streaming position of {@link advanceOpaqueReasoning}: an undecided trailing run, or an open payload. */
+export interface OpaqueReasoningCursor {
+ /** Trailing payload-alphabet characters too short to classify yet. */
+ readonly tail: string;
+ /** True while the stream is inside a run already long enough to be a payload. */
+ readonly opaque: boolean;
+}
+
+export const OPAQUE_REASONING_START: OpaqueReasoningCursor = { tail: '', opaque: false };
+
+function payloadRunEnd(text: string, from: number): number {
+ let end = from;
+ while (end < text.length && isPayloadChar(text.charCodeAt(end))) {
+ end += 1;
+ }
+ return end;
+}
+
+/**
+ * Feeds one reasoning delta through the payload filter. A payload can arrive
+ * split across deltas shorter than the classification threshold, so a trailing
+ * run is held in the cursor until a non-payload character settles it; readable
+ * words are held only until the next space or punctuation. Once a run is known
+ * to be a payload, it continues through the next non-payload character, even
+ * across deltas, so no fragment of it is ever released as readable text.
+ * Returns the settled readable text, which callers can count and accumulate
+ * directly.
+ */
+export function advanceOpaqueReasoning(
+ cursor: OpaqueReasoningCursor,
+ delta: string,
+): { readable: string; cursor: OpaqueReasoningCursor } {
+ const start = cursor.opaque ? payloadRunEnd(delta, 0) : 0;
+ if (start === delta.length && cursor.opaque) {
+ return { readable: '', cursor };
+ }
+ const combined = `${cursor.opaque ? '' : cursor.tail}${delta.slice(start)}`;
+ let end = combined.length;
+ while (end > 0 && isPayloadChar(combined.charCodeAt(end - 1))) {
+ end -= 1;
+ }
+ const trailing = combined.slice(end);
+ const readable = combined.slice(0, end).replace(OPAQUE_REASONING_RUN, '');
+ if (trailing.length >= OPAQUE_REASONING_MIN_RUN) {
+ return { readable, cursor: { tail: '', opaque: true } };
+ }
+ return { readable, cursor: { tail: trailing, opaque: false } };
+}
+
+/**
+ * The undecided tail of a stream that has ended (or of text still streaming,
+ * for display): a word-sized tail is a final word, while a longer one is more
+ * likely the unclassified start of a payload and is withheld.
+ */
+export function flushOpaqueReasoning(cursor: OpaqueReasoningCursor): string {
+ return !cursor.opaque && cursor.tail.length <= OPAQUE_REASONING_MAX_WORD ? cursor.tail : '';
+}
+
+/**
+ * Reasoning text safe to show: encoded payloads removed and, while the text is
+ * still streaming, a trailing run that could still grow into a payload held
+ * back until it is classified.
+ */
+export function readableReasoning(text: string, streaming = false): string {
+ if (!streaming) {
+ return stripOpaqueReasoning(text);
+ }
+ const { readable, cursor } = advanceOpaqueReasoning(OPAQUE_REASONING_START, text);
+ return `${readable}${flushOpaqueReasoning(cursor)}`;
+}
+
+/**
+ * A bounded window that may have been cut mid-run starts with a fragment too
+ * short to classify, whether of a word or of a payload; drop it.
+ */
+export function trimTruncatedReasoning(text: string): string {
+ return text.slice(payloadRunEnd(text, 0));
+}