🎯 feat: Render Tool Intent as the Live Tool-Call Label (#14536)

* 🎯 feat: Render Tool Intent as the Live Tool-Call Label

The tool_intents capability injects a model-authored `intent` sentence as
the FIRST key of a tool call's args, and the SDK's coding tools carry it
natively — but no client component ever read it, so cards kept showing
their generic labels ("Running command") while the intent streamed by
unused.

A shared useToolCallIntent hook extracts the intent from streaming args
via parseJsonField's partial-JSON fallback, so the label renders from the
first delta — before any other arg exists — and keeps updating as it
streams. When present, the intent replaces the generic in-progress label
and persists as the settled label (completion is a UI state, not a tense
change, matching the SDK's applyOutcome design). Cancelled, error, and
background states keep their existing precedence.

Wired into BashCall, ExecuteCode, the generic ToolCall (MCP, actions,
plugin tools), ReadFileCall, SkillCall, and FileAuthoringCall. Non-string
`intent` business params are ignored. SubagentCall keeps its verb+name
header design for a follow-up.

* 🎯 fix: Harden Intent Label Extraction per Review

Gate the label on intent being the FIRST args key (the label contract's
first-position rule), so a tool's own business param named intent — e.g. a
CRM's {"q":"acme","intent":"billing_inquiry"} — no longer renders as the
status label. Bound the label to a single 256-char line before it reaches
ProgressText's nowrap layout, mirroring the SDK's outcome-label cap.

Decode the full JSON escape set in parseJsonField's streaming fallback
(\t \r \b \f \/ and \uXXXX with surrogate pairs) so a partial label renders
exactly as its settled JSON.parse form; stream-edge incompletions (dangling
escape, partial \uXX, split surrogate) are held back rather than shown.

Wire web_search into the intent label: Part.tsx now passes toolCall.args
and the WebSearch card prefers the intent for its progress and completed
texts — it carries intent natively but never received args at all.

* 🎯 fix: Round-2 Review — Stable Live Region, Split Low Surrogates, Specialized Cards

Keep the aria-live region on its stable generic value while the intent
streams: an atomic polite region re-announces the whole growing sentence
on every delta otherwise. The settled intent is still announced once via
the finished text.

Hold back a decoded high surrogate while its low-surrogate escape is
still streaming (\ud83d\u, \ud83d\ude0), not only when the high half ends
the value exactly; a complete following escape composes the pair on the
next iteration, and a lone surrogate followed by ordinary text stays
emitted, matching JSON.parse.

Thread args into the specialized cards for explicitly opted-in tools:
RetrievalCall (file_search) and the image-gen cards (image_gen_oai,
image_edit_oai, gemini_image_gen) now resolve the intent for their
progress and settled labels, with the image phase texts as fallback.

* 🎯 fix: Round-3 Review — Live-Region Settled Announcements & Remaining Cards

Announce the settled intent once through the aria-live regions of
RetrievalCall, the image-gen card, and WebSearch, while each region keeps
a stable generic value during streaming (WebSearch was still piping the
growing intent into its atomic region on every delta).

Pass object-valued args through Part.tsx to the image-gen card instead of
coercing them to '' — persisted/completed calls carry object args, so the
first-key intent was invisible on reload.

Guard complete serialized args against non-string intents: parseJsonField's
JSON branch would coerce {"intent":{...}} into "[object Object]"; the hook
now type-checks the parsed field, matching the object-args path.

Wire the subagent card: the SDK-native subagent intent now leads its
header, without overriding error or cancellation framing.

* 🎯 fix: Round-4 Review — Constant-Cost Extraction, Final-Search Settling, Safe Truncation

Replace the hook's JSON.parse-per-delta with a single anchored regex over
a bounded 2 KB head window: the first-position contract lets one match do
the business-param gating and the value capture (complete or streaming),
so per-delta cost stays constant while a large code/content argument
streams behind the label. A non-string first-key intent never matches the
opening quote, keeping the round-3 guard without parsing.

Settle a web search that is the message's final part once submission ends:
`complete` previously required !isLast permanently, so the last-part case
shimmered forever and never announced its settled intent.

Back the truncation cut off a high surrogate so a bounded multilingual
label never ends in a replacement glyph before the ellipsis.

* 🎯 fix: Round-5 Review — Keep Terminal Lone Surrogates in Settled Values

Thread value completeness from the extractor into the escape decoder: a
captured closing quote means the value is settled, so the stream-edge
hold-backs (partial \uXX, high-surrogate deferral) no longer apply and a
value genuinely ending in a lone high surrogate keeps its final code
unit, matching JSON.parse and the object-args rendering. Streaming
callers keep the hold-back behavior unchanged.
This commit is contained in:
Danny Avila 2026-07-30 21:31:51 -04:00 committed by GitHub
parent d6c2dc5d8e
commit ff9d89540c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 407 additions and 35 deletions

View file

@ -223,7 +223,7 @@ const Part = memo(function Part({
initialProgress={toolCall.progress ?? 0.1}
isSubmitting={isSubmitting}
toolName={toolCall.name}
args={typeof toolCall.args === 'string' ? toolCall.args : ''}
args={toolCall.args ?? ''}
output={toolCall.output ?? ''}
attachments={attachments}
hideAttachments={hideAttachments}
@ -318,6 +318,7 @@ const Part = memo(function Part({
} else if (toolCall.name === Tools.web_search) {
return (
<WebSearch
args={toolCall.args}
output={toolCall.output ?? ''}
initialProgress={toolCall.progress ?? 0.1}
isSubmitting={isSubmitting}
@ -331,6 +332,7 @@ const Part = memo(function Part({
<RetrievalCall
initialProgress={toolCall.progress ?? 0.1}
isSubmitting={isSubmitting}
args={toolCall.args}
output={toolCall.output ?? undefined}
attachments={attachments}
onExpand={onToolExpand}

View file

@ -12,6 +12,7 @@ import useToolCallState from './useToolCallState';
import useLazyHighlight from './useLazyHighlight';
import { ERROR_PATTERNS } from './ExecuteCode';
import { AttachmentGroup } from './Attachment';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
@ -75,7 +76,15 @@ export default function BashCall({
timerRef.current = setTimeout(() => setIsCopied(false), 3000);
}, [command]);
/** The model-authored `intent` streams as the FIRST args key, so it is the
* live label from the earliest delta before the command exists and while
* it runs. It persists as the settled label too (completion is a UI state,
* not a tense change); the generic texts are the no-intent fallback. */
const intent = useToolCallIntent(args);
const inProgressText = (() => {
if (intent != null) {
return intent;
}
if (isWritingCommand) {
return localize('com_ui_writing_command');
}
@ -95,7 +104,7 @@ export default function BashCall({
finishedText={
cancelled
? localize('com_ui_cancelled')
: (backgroundFinishedText ?? localize('com_ui_command_finished'))
: (backgroundFinishedText ?? intent ?? localize('com_ui_command_finished'))
}
errorSuffix={
(hasError && !cancelled) || backgroundFailed

View file

@ -9,6 +9,7 @@ import useLazyHighlight from './useLazyHighlight';
import useToolCallState from './useToolCallState';
import CodeWindowHeader from './CodeWindowHeader';
import { AttachmentGroup } from './Attachment';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import Stdout from './Stdout';
import { cn } from '~/utils';
@ -74,6 +75,9 @@ export default function ExecuteCode({
}) {
const localize = useLocalize();
const { lang = 'py', code } = useParseArgs(args) ?? ({} as ParsedArgs);
/** Model-authored live label, streamed as the first args key; persists as
* the settled label (completion is a UI state, not a tense change). */
const intent = useToolCallIntent(args);
const sandboxStarting = useRecoilValue(sandboxStartingByToolCallId(toolCallId ?? ''));
const { showCode, toggleCode, expandStyle, expandRef, progress, cancelled, hasError, hasOutput } =
@ -106,12 +110,13 @@ export default function ExecuteCode({
progress={progress}
onClick={toggleCode}
inProgressText={
sandboxStarting ? localize('com_ui_sandbox_starting') : localize('com_ui_analyzing')
intent ??
(sandboxStarting ? localize('com_ui_sandbox_starting') : localize('com_ui_analyzing'))
}
finishedText={
cancelled
? localize('com_ui_cancelled')
: (backgroundFinishedText ?? localize('com_ui_analyzing_finished'))
: (backgroundFinishedText ?? intent ?? localize('com_ui_analyzing_finished'))
}
errorSuffix={
(hasError && !cancelled) || backgroundFailed

View file

@ -8,6 +8,7 @@ import useLazyHighlight from './useLazyHighlight';
import CodeWindowHeader from './CodeWindowHeader';
import { AttachmentGroup } from './Attachment';
import { langFromPath } from './ReadFileCall';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
@ -128,6 +129,7 @@ export default function FileAuthoringCall({
* `Created`/`Updated`, so key the finished label off it for truthfulness. */
const overwrote = isCreate && output.startsWith('Updated ');
const filePath = useMemo(() => parseJsonField(args, 'file_path'), [args]);
const intent = useToolCallIntent(args);
const authoredContent = useMemo(() => parseJsonField(args, 'content'), [args]);
const editArgsPreview = useMemo(() => buildEditArgsPreview(args), [args]);
const fileName = filePath.split('/').pop() || filePath;
@ -162,11 +164,16 @@ export default function FileAuthoringCall({
<ProgressText
progress={progress}
onClick={toggleCode}
inProgressText={localize(isCreate ? 'com_ui_creating_file' : 'com_ui_editing_file', {
0: fileName,
})}
inProgressText={
intent ??
localize(isCreate ? 'com_ui_creating_file' : 'com_ui_editing_file', {
0: fileName,
})
}
finishedText={
cancelled ? localize('com_ui_cancelled') : localize(finishedKey, { 0: fileName })
cancelled
? localize('com_ui_cancelled')
: (intent ?? localize(finishedKey, { 0: fileName }))
}
errorSuffix={hasError && !cancelled ? localize('com_ui_tool_failed') : undefined}
icon={

View file

@ -1,11 +1,12 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import { AGENT_STYLE_TOOLS } from '.';
import { PixelCard } from '@librechat/client';
import type { TAttachment, TFile, TAttachmentMetadata } from 'librechat-data-provider';
import { ToolIcon, isError } from '~/components/Chat/Messages/Content/ToolOutput';
import Image from '~/components/Chat/Messages/Content/Image';
import { useProgress, useLocalize } from '~/hooks';
import { useToolCallIntent } from '../intent';
import ProgressText from './ProgressText';
import { AGENT_STYLE_TOOLS } from '.';
import { scaleImage } from '~/utils';
function computeCancelled(
@ -42,6 +43,9 @@ export default function OpenAIImageGen({
hideAttachments?: boolean;
}) {
const localize = useLocalize();
/** Model-authored live label (injected when the tool is opted into
* describe_intent); wins over the phase texts. */
const intent = useToolCallIntent(_args);
const isAgentStyle = toolName != null && AGENT_STYLE_TOOLS.has(toolName);
const [agentProgress, setAgentProgress] = useState(initialProgress);
const legacyProgress = useProgress(isAgentStyle ? 1 : initialProgress);
@ -221,12 +225,12 @@ export default function OpenAIImageGen({
if (cancelled) {
return localize('com_ui_cancelled');
}
return localize('com_ui_image_created');
return intent ?? localize('com_ui_image_created');
})()}
</span>
<div className="relative my-1 flex h-5 shrink-0 items-center gap-2">
<ToolIcon type="image_gen" isAnimating={isInProgress} />
<ProgressText progress={progress} error={cancelled} toolName={toolName} />
<ProgressText progress={progress} error={cancelled} toolName={toolName} intent={intent} />
</div>
{isAgentStyle && !hideAttachments && (
<div className="relative mb-2 flex w-full justify-start">

View file

@ -6,10 +6,13 @@ export default function ProgressText({
progress,
error,
toolName = '',
intent,
}: {
progress: number;
error?: boolean;
toolName?: string;
/** Model-authored label; wins over the phase texts (error state excepted). */
intent?: string;
}) {
const localize = useLocalize();
@ -17,6 +20,9 @@ export default function ProgressText({
if (error) {
return localize('com_ui_image_gen_failed');
}
if (intent != null) {
return intent;
}
if (toolName === 'image_edit_oai') {
if (progress >= 1) {

View file

@ -7,6 +7,7 @@ import useLazyHighlight from './useLazyHighlight';
import CodeWindowHeader from './CodeWindowHeader';
import { AttachmentGroup } from './Attachment';
import parseJsonField from './parseJsonField';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
@ -80,6 +81,7 @@ export default function ReadFileCall({
}) {
const localize = useLocalize();
const filePath = useMemo(() => parseJsonField(args, 'file_path'), [args]);
const intent = useToolCallIntent(args);
const fileName = filePath.split('/').pop() || filePath;
const lang = useMemo(() => langFromPath(filePath), [filePath]);
@ -94,9 +96,11 @@ export default function ReadFileCall({
<ProgressText
progress={progress}
onClick={toggleCode}
inProgressText={localize('com_ui_reading_file', { 0: fileName })}
inProgressText={intent ?? localize('com_ui_reading_file', { 0: fileName })}
finishedText={
cancelled ? localize('com_ui_cancelled') : localize('com_ui_read_file', { 0: fileName })
cancelled
? localize('com_ui_cancelled')
: (intent ?? localize('com_ui_read_file', { 0: fileName }))
}
errorSuffix={hasError && !cancelled ? localize('com_ui_tool_failed') : undefined}
icon={

View file

@ -5,6 +5,7 @@ import ProgressText from '~/components/Chat/Messages/Content/ProgressText';
import useToolCallState from './useToolCallState';
import { AttachmentGroup } from './Attachment';
import parseJsonField from './parseJsonField';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import Stdout from './Stdout';
import { cn } from '~/utils';
@ -28,6 +29,7 @@ export default function SkillCall({
}) {
const localize = useLocalize();
const skillName = useMemo(() => parseJsonField(args, 'skillName'), [args]);
const intent = useToolCallIntent(args);
const { showCode, toggleCode, expandStyle, expandRef, progress, cancelled, hasError, hasOutput } =
useToolCallState(initialProgress, isSubmitting, output, !!skillName, onExpand);
@ -38,11 +40,11 @@ export default function SkillCall({
<ProgressText
progress={progress}
onClick={toggleCode}
inProgressText={localize('com_ui_skill_running', { 0: skillName })}
inProgressText={intent ?? localize('com_ui_skill_running', { 0: skillName })}
finishedText={
cancelled
? localize('com_ui_cancelled')
: localize('com_ui_skill_finished', { 0: skillName })
: (intent ?? localize('com_ui_skill_finished', { 0: skillName }))
}
errorSuffix={hasError && !cancelled ? localize('com_ui_tool_failed') : undefined}
icon={

View file

@ -18,6 +18,7 @@ import { subagentProgressByToolCallId } from '~/store';
import { useAgentsMapContext } from '~/Providers';
import { useMCPServerNames } from '~/hooks/MCP';
import { AttachmentGroup } from './Attachment';
import { useToolCallIntent } from './intent';
import { useLocalize } from '~/hooks';
import Reasoning from './Reasoning';
import Text from './Text';
@ -252,9 +253,13 @@ export default function SubagentCall({
/** Base verb-only label ("Running agent" / "Ran agent"). The agent name
* is rendered separately as a muted sub-label so "agent" stays a
* constant visual anchor regardless of name length. */
/** Model-authored live label (subagent carries `intent` natively); wins
* over the generic verb, never over error/cancellation framing. */
const intent = useToolCallIntent(args);
const getHeaderText = () => {
if (hasError) return localize('com_ui_subagent_errored');
if (cancelled) return localize('com_ui_subagent_cancelled');
if (intent != null) return intent;
if (running) return localize('com_ui_subagent_running');
return localize('com_ui_subagent_complete');
};

View file

@ -122,6 +122,85 @@ describe('BashCall status text', () => {
);
});
describe('BashCall intent label', () => {
it('shows a streaming intent before any other arg exists (first key, partial JSON)', () => {
renderBashCall('{"intent":"Checking the countdown ta');
expect(screen.getByTestId('progress-text')).toHaveTextContent('Checking the countdown ta');
expect(screen.queryByText('Writing command')).not.toBeInTheDocument();
});
it('keeps the intent as the in-progress label once the command has streamed', () => {
renderBashCall('{"intent":"Waiting for the task to settle","command":"sleep 8; echo waited"}');
expect(screen.getByTestId('progress-text')).toHaveTextContent('Waiting for the task to settle');
expect(screen.queryByText('Running command')).not.toBeInTheDocument();
expect(screen.getByText(/sleep 8/)).toBeInTheDocument();
});
it('keeps the intent as the settled label (completion is a UI state, not a tense change)', () => {
render(
<RecoilRoot>
<BashCall
initialProgress={1}
isSubmitting={false}
args={'{"intent":"Waiting for the task to settle","command":"sleep 8"}'}
output="waited"
/>
</RecoilRoot>,
);
expect(screen.getByTestId('progress-text')).toHaveTextContent('Waiting for the task to settle');
expect(screen.queryByText('Finished running')).not.toBeInTheDocument();
});
it('falls back to the generic labels when no intent is present', () => {
renderBashCall({ command: 'sleep 10' });
expect(screen.getByTestId('progress-text')).toHaveTextContent('Running command');
});
it('ignores a non-string intent arg (a business param, not the label contract)', () => {
renderBashCall({ intent: { nested: true }, command: 'sleep 10' });
expect(screen.getByTestId('progress-text')).toHaveTextContent('Running command');
});
it('ignores a non-string intent in complete SERIALIZED args (no String() coercion)', () => {
renderBashCall('{"intent":{"topic":"billing"},"command":"sleep 10"}');
expect(screen.getByTestId('progress-text')).toHaveTextContent('Running command');
expect(screen.queryByText(/object Object/)).not.toBeInTheDocument();
});
it('ignores an intent that is not the FIRST args key (label contract is first-position)', () => {
renderBashCall('{"command":"sleep 10","intent":"billing_inquiry"}');
expect(screen.getByTestId('progress-text')).toHaveTextContent('Running command');
expect(screen.queryByText('billing_inquiry')).not.toBeInTheDocument();
});
it('bounds a runaway intent to a single 256-char line', () => {
const runaway = `Checking ${'a very long clause '.repeat(30)}end`;
renderBashCall({ intent: runaway, command: 'sleep 10' });
const text = screen.getByTestId('progress-text').textContent ?? '';
expect(text.length).toBeLessThanOrEqual(256);
expect(text.endsWith('…')).toBe(true);
});
it('never splits a surrogate pair at the truncation boundary', () => {
const straddling = `${'x'.repeat(254)}😀 and more text to exceed the bound`;
renderBashCall({ intent: straddling, command: 'sleep 10' });
const text = screen.getByTestId('progress-text').textContent ?? '';
expect(text.endsWith('x…')).toBe(true);
expect(text).not.toContain('<27>');
});
it('decodes unicode escapes in a streaming intent (no literal \\uXXXX flash)', () => {
renderBashCall('{"intent":"Checking caf\\u00e9 menu da');
expect(screen.getByTestId('progress-text')).toHaveTextContent('Checking café menu da');
});
it('keeps a terminal lone high surrogate once the value is settled (matches JSON.parse)', () => {
renderBashCall('{"intent":"odd \\ud83d","command":"sleep 10"}');
const text = screen.getByTestId('progress-text').textContent ?? '';
expect(text).toBe('odd \ud83d');
});
});
describe('BashCall backgrounded calls', () => {
const HANDLE_OUTPUT = JSON.stringify({
background_task_id: 'task-1',

View file

@ -98,9 +98,47 @@ describe('parseJsonField', () => {
expect(parseJsonField(partial, 'file_path')).toBe('C:\\note');
});
it('preserves unknown escape sequences', () => {
const partial = '{"command":"tab\\there","incomplete":';
expect(parseJsonField(partial, 'command')).toBe('tab\\there');
it('decodes the full JSON escape set (matches the settled JSON.parse rendering)', () => {
const partial = '{"command":"tab\\there\\rreturn\\bback\\fform\\/slash","incomplete":';
expect(parseJsonField(partial, 'command')).toBe('tab\there\rreturn\bback\fform/slash');
});
it('preserves genuinely unknown escape sequences', () => {
const partial = '{"command":"odd\\qescape","incomplete":';
expect(parseJsonField(partial, 'command')).toBe('odd\\qescape');
});
it('decodes \\uXXXX escapes mid-stream, including surrogate pairs', () => {
const partial = '{"intent":"Checking caf\\u00e9 menu \\ud83d\\ude00 da';
expect(parseJsonField(partial, 'intent')).toBe('Checking café menu 😀 da');
});
it('drops an incomplete \\uXX escape at the stream edge instead of showing it literally', () => {
const partial = '{"intent":"Checking caf\\u00e';
expect(parseJsonField(partial, 'intent')).toBe('Checking caf');
});
it('holds back the high half of a split surrogate pair at the stream edge', () => {
const partial = '{"intent":"Searching \\ud83d';
expect(parseJsonField(partial, 'intent')).toBe('Searching ');
});
it.each(['\\', '\\u', '\\uD', '\\uDE0'])(
'holds the high surrogate while the low escape is still streaming: "\\ud83d%s"',
(lowPrefix) => {
const partial = `{"intent":"Searching \\ud83d${lowPrefix}`;
expect(parseJsonField(partial, 'intent')).toBe('Searching ');
},
);
it('emits a lone high surrogate when followed by ordinary text (matches JSON.parse)', () => {
const partial = '{"intent":"odd \\ud83d tail","incomplete":';
expect(parseJsonField(partial, 'intent')).toBe('odd \ud83d tail');
});
it('keeps a malformed \\u (bad hex mid-string) literal rather than corrupting the tail', () => {
const partial = '{"command":"regex \\uZZZZ tail","incomplete":';
expect(parseJsonField(partial, 'command')).toBe('regex \\uZZZZ tail');
});
});

View file

@ -0,0 +1,88 @@
import { useMemo } from 'react';
import { unescapeJsonString } from './parseJsonField';
/**
* Single-line clamp mirroring the SDK's outcome-label bound
* (`MAX_OUTCOME_CHARS` in `@librechat/agents`): the label is one progress
* line in UI chrome rendered with `whitespace-nowrap`, so a verbose or
* malformed model response must not extend across adjacent controls.
*/
const MAX_INTENT_CHARS = 256;
/**
* Head window scanned for the label. The gate + a full 256-char label (with
* escapes) fit comfortably; bounding the scan keeps per-delta work constant
* even when a tool streams megabytes of `code`/`content` after the intent.
*/
const INTENT_SCAN_CHARS = 2048;
/**
* Anchored prefix extractor: the label contract puts `intent` FIRST
* ("ALWAYS write this field FIRST" that ordering is the entire streaming
* mechanism), so a single anchored match both gates out a tool's own
* business parameter that merely shares the name (e.g. a CRM's
* `{"q":"acme","intent":"billing"}`, which serializes wherever the model
* put it) and captures the string value, complete or still streaming
* (tolerating a missing closing quote and a dangling escape). A non-string
* first-key `intent` (`{"intent":{...}}`) never matches the opening quote,
* so schema-invalid calls keep their generic label without any JSON.parse.
*/
const INTENT_PREFIX_REGEX = /^\s*\{\s*"intent"\s*:\s*"((?:[^"\\]|\\.)*)(")?/;
function boundIntentLabel(label: string): string | undefined {
const singleLine = label.replace(/\s+/g, ' ').trim();
if (singleLine === '') {
return undefined;
}
if (singleLine.length <= MAX_INTENT_CHARS) {
return singleLine;
}
let head = singleLine.slice(0, MAX_INTENT_CHARS - 1);
const lastCode = head.charCodeAt(head.length - 1);
/** Never split a surrogate pair at the cut a lone high surrogate
* renders as a replacement glyph before the ellipsis. */
if (lastCode >= 0xd800 && lastCode <= 0xdbff) {
head = head.slice(0, -1);
}
return `${head}`;
}
/**
* The model-authored `intent` label for a tool call: one sentence, injected
* as the FIRST property of the tool's schema (SDK-native or host-injected),
* so it is the first key providers stream in the args which is what lets
* a card show it as the call's live status label from the earliest delta,
* before the rest of the args exist. Returns undefined until any non-empty
* text has streamed, so callers can fall back to their generic label.
*
* Extraction never parses the args buffer: one anchored regex over a
* bounded head window does the gating and the capture, so per-delta cost
* stays constant while a large `code`/`content` argument streams behind
* the label.
*
* The label persists unchanged when the call settles: completion is a UI
* state (the shimmer stopping), not a tense change (see `applyOutcome` in
* `@librechat/agents` for why there is deliberately no rewrite).
*/
export function useToolCallIntent(args?: string | Record<string, unknown>): string | undefined {
return useMemo(() => {
if (args == null) {
return undefined;
}
if (typeof args === 'object') {
if (Object.keys(args)[0] !== 'intent') {
return undefined;
}
const value = args.intent;
return typeof value === 'string' ? boundIntentLabel(value) : undefined;
}
const match = INTENT_PREFIX_REGEX.exec(args.slice(0, INTENT_SCAN_CHARS));
if (!match) {
return undefined;
}
/** A captured closing quote means the value is settled: decode it
* without the stream-edge hold-backs, so a value genuinely ending in
* a lone high surrogate matches its JSON.parse rendering. */
return boundIntentLabel(unescapeJsonString(match[1], match[2] !== '"'));
}, [args]);
}

View file

@ -21,16 +21,70 @@ function fieldRegex(field: string, flags?: string): RegExp {
return new RegExp(`"${escaped}"\\s*:\\s*"((?:[^"\\\\]|\\\\.)*)(?:"|\\\\?$)`, flags);
}
function unescapeJsonString(value: string): string {
return value.replace(/\\(.)/g, (_, c: string) => {
if (c === 'n') {
return '\n';
const SIMPLE_ESCAPES: Record<string, string> = {
n: '\n',
t: '\t',
r: '\r',
b: '\b',
f: '\f',
'"': '"',
'\\': '\\',
'/': '/',
};
/**
* Decodes the full JSON string escape set so a partially streamed value
* renders exactly as its settled `JSON.parse` form will without this,
* `café` displays literally mid-stream and then snaps to `café` once
* the object becomes valid JSON. Stream-boundary incompletions (a dangling
* `\`, a partial `\uXX`, or the high half of a split surrogate pair) are
* dropped rather than shown, since the next delta completes them; unknown
* escapes elsewhere are preserved literally. Callers that KNOW the value is
* settled (its closing quote was present) pass `streaming: false` so a value
* genuinely ending in a lone high surrogate keeps its final code unit,
* matching `JSON.parse`.
*/
export function unescapeJsonString(value: string, streaming = true): string {
let out = '';
for (let i = 0; i < value.length; i++) {
const ch = value[i];
if (ch !== '\\') {
out += ch;
continue;
}
if (c === '"' || c === '\\') {
return c;
const next = value[i + 1];
if (next === undefined) {
break;
}
return `\\${c}`;
});
i++;
if (next !== 'u') {
out += SIMPLE_ESCAPES[next] ?? `\\${next}`;
continue;
}
const hex = value.slice(i + 1, i + 5);
if (!/^[0-9a-fA-F]{4}$/.test(hex)) {
if (streaming && i + 5 > value.length) {
return out;
}
out += '\\u';
continue;
}
i += 4;
const code = parseInt(hex, 16);
if (streaming && code >= 0xd800 && code <= 0xdbff) {
/** Hold back a high surrogate while its low half could still be
* streaming in the remainder being any proper prefix of `\uXXXX`
* (including the empty string). A complete following escape decodes
* on the next iteration and composes the pair; anything else means
* the lone surrogate is real data (matches `JSON.parse`). */
const rest = value.slice(i + 1);
if (/^(?:\\(?:u[0-9a-fA-F]{0,3})?)?$/.test(rest)) {
return out;
}
}
out += String.fromCharCode(code);
}
return out;
}
/** Extracts a string field from tool call args, handling object, JSON string, and partial-JSON fallback. */

View file

@ -8,6 +8,7 @@ import { useLocalize, useProgress, useExpandCollapse } from '~/hooks';
import { ToolIcon, OutputRenderer, isError } from './ToolOutput';
import FilePreviewDialog from './FilePreviewDialog';
import { sortPagesByRelevance, cn } from '~/utils';
import { useToolCallIntent } from './Parts/intent';
import { useGetFiles } from '~/data-provider';
import ProgressText from './ProgressText';
import store from '~/store';
@ -324,18 +325,24 @@ function FileHeader({
export default function RetrievalCall({
initialProgress = 0.1,
isSubmitting,
args,
output,
attachments,
onExpand,
}: {
initialProgress: number;
isSubmitting: boolean;
args?: string | Record<string, unknown>;
output?: string;
attachments?: TAttachment[];
onExpand?: () => void;
}) {
const progress = useProgress(initialProgress);
const localize = useLocalize();
/** Model-authored live label (injected when file_search is opted into
* describe_intent); persists as the settled label. The sr-only live
* region below deliberately keeps its stable generic value. */
const intent = useToolCallIntent(args);
const errorState = typeof output === 'string' && isError(output);
const cancelled = !isSubmitting && initialProgress < 1 && !errorState;
@ -422,15 +429,15 @@ export default function RetrievalCall({
if (cancelled) {
return localize('com_ui_cancelled');
}
return localize('com_ui_retrieved_files');
return intent ?? localize('com_ui_retrieved_files');
})()}
</span>
<div className="relative my-1 flex h-5 shrink-0 items-center gap-2.5">
<ProgressText
progress={progress}
onClick={hasOutput ? handleToggleOutput : undefined}
inProgressText={localize('com_ui_searching_files')}
finishedText={localize('com_ui_retrieved_files')}
inProgressText={intent ?? localize('com_ui_searching_files')}
finishedText={intent ?? localize('com_ui_retrieved_files')}
errorSuffix={errorState && !cancelled ? localize('com_ui_tool_failed') : undefined}
icon={
<ToolIcon type="file_search" isAnimating={progress < 1 && !cancelled && !errorState} />

View file

@ -13,6 +13,7 @@ import type { TAttachment } from 'librechat-data-provider';
import { useLocalize, useProgress, useExpandCollapse } from '~/hooks';
import { ToolIcon, getToolIconType, isError } from './ToolOutput';
import { useMCPIconMap, useMCPServerNames } from '~/hooks/MCP';
import { useToolCallIntent } from './Parts/intent';
import { AttachmentGroup } from './Parts';
import ToolCallInfo from './ToolCallInfo';
import ProgressText from './ProgressText';
@ -187,10 +188,18 @@ export default function ToolCall({
return undefined;
}, [isMCPToolCall, mcpServerName, domain, localize]);
/** Model-authored live label, streamed as the first args key (injected by
* the `tool_intents` capability); persists as the settled label
* completion is a UI state, not a tense change. */
const intent = useToolCallIntent(_args);
const getFinishedText = () => {
if (cancelled) {
return localize('com_ui_cancelled');
}
if (intent != null) {
return intent;
}
if (isMCPToolCall === true) {
return localize('com_assistants_completed_function', { 0: function_name });
}
@ -206,6 +215,10 @@ export default function ToolCall({
return (
<>
{/* The live region gets a STABLE in-progress value: the streaming
intent grows on every delta, and an atomic polite region would
re-announce the whole sentence each time. The settled intent is
announced once via getFinishedText. */}
<span className="sr-only" aria-live="polite" aria-atomic="true">
{(() => {
if (progress < 1 && !showCancelled) {
@ -225,9 +238,10 @@ export default function ToolCall({
progress={progress}
onClick={handleToggleInfo}
inProgressText={
function_name
intent ??
(function_name
? localize('com_assistants_running_var', { 0: function_name })
: localize('com_assistants_running_action')
: localize('com_assistants_running_action'))
}
authText={
!showCancelled && authDomain.length > 0 ? localize('com_ui_requires_auth') : undefined

View file

@ -6,6 +6,7 @@ import type { TAttachment, ValidSource, SearchResultData } from 'librechat-data-
import { FaviconImage, getCleanDomain } from '~/components/Web/SourceHovercard';
import { StackedFavicons } from '~/components/Web/Sources';
import { useLocalize, useExpandCollapse } from '~/hooks';
import { useToolCallIntent } from './Parts/intent';
import { useSearchContext } from '~/Providers';
import cn from '~/utils/cn';
import store from '~/store';
@ -80,18 +81,23 @@ export default function WebSearch({
initialProgress: progress = 0.1,
isSubmitting,
isLast,
args,
output,
attachments,
onExpand,
}: {
isLast?: boolean;
isSubmitting: boolean;
args?: string | Record<string, unknown>;
output?: string | null;
initialProgress: number;
attachments?: TAttachment[];
onExpand?: () => void;
}) {
const localize = useLocalize();
/** Model-authored live label (web_search carries `intent` natively);
* persists as the settled label like the other tool cards. */
const intent = useToolCallIntent(args);
const { searchResults } = useSearchContext();
const error = typeof output === 'string' && output.toLowerCase().includes('error processing');
@ -105,8 +111,12 @@ export default function WebSearch({
const effectiveProgress = hasResults && !isSubmitting ? 1 : progress;
const cancelled = (!isSubmitting && effectiveProgress < 1) || error === true;
const complete = !isLast && effectiveProgress === 1;
const finalizing = isSubmitting && isLast && effectiveProgress === 1;
/** A search that is the message's FINAL part stays "finalizing" only while
* the submission is live afterwards it must settle like any other call,
* or the completed label (and its settled intent announcement) never
* renders and the card shimmers forever. */
const complete = effectiveProgress === 1 && !finalizing && (!isLast || !isSubmitting);
const ownTurn = useMemo((): string => {
if (!attachments) {
@ -156,7 +166,10 @@ export default function WebSearch({
}, [searchResults, complete, finalizing, ownTurn]);
const showSources = streamingSources.length > 0;
const progressText = useMemo(() => {
/** Stable phase text: the live region must not re-announce the growing
* intent on every delta, so it always gets this value while streaming;
* the settled intent is announced once via the completed branch. */
const genericProgressText = useMemo(() => {
let text: ProgressKeys =
ownTurn !== '0' ? 'com_ui_web_searching_again' : 'com_ui_web_searching';
if (showSources) {
@ -167,6 +180,7 @@ export default function WebSearch({
}
return localize(text);
}, [ownTurn, localize, showSources, finalizing]);
const progressText = intent ?? genericProgressText;
const autoExpand = useRecoilValue(store.autoExpandTools);
const sourceCount = allSources.length;
@ -195,7 +209,7 @@ export default function WebSearch({
if (complete) {
const hasSourceData = sourceCount > 0;
const completedText = localize('com_ui_web_searched');
const completedText = intent ?? localize('com_ui_web_searched');
return (
<div className="mb-2">
@ -271,7 +285,7 @@ export default function WebSearch({
return (
<div className="my-1 flex items-center gap-2.5">
<span className="sr-only" aria-live="polite" aria-atomic="true">
{progressText}
{genericProgressText}
</span>
{showSources && <StackedFavicons sources={streamingSources} start={-5} />}
<Globe className="size-4 shrink-0 text-text-secondary" aria-hidden="true" />

View file

@ -119,6 +119,40 @@ describe('ToolCall', () => {
jest.clearAllMocks();
});
describe('intent label', () => {
it('renders a streaming intent as the live label before args are complete', () => {
renderWithRecoil(
<ToolCall
{...mockProps}
args={'{"intent":"Searching for OAuth handling in the callbac'}
output={null}
initialProgress={0.5}
isSubmitting={true}
/>,
);
expect(
screen.getAllByText(/Searching for OAuth handling in the callbac/).length,
).toBeGreaterThan(0);
/** The aria-live region keeps its STABLE generic value while the intent
* streams an atomic polite region would otherwise re-announce the
* whole growing sentence on every delta. */
expect(screen.getByText('Running testFunction')).toBeInTheDocument();
});
it('keeps the intent as the settled label instead of the generic completion text', () => {
renderWithRecoil(
<ToolCall {...mockProps} args={'{"intent":"Looking up the customer record","q":"acme"}'} />,
);
expect(screen.getAllByText('Looking up the customer record').length).toBeGreaterThan(0);
expect(screen.queryByText('Completed testFunction')).not.toBeInTheDocument();
});
it('falls back to the generic labels when args carry no intent', () => {
renderWithRecoil(<ToolCall {...mockProps} />);
expect(screen.getAllByText('Completed testFunction').length).toBeGreaterThan(0);
});
});
describe('attachments prop passing', () => {
it('should pass attachments to ToolCallInfo when provided', () => {
const attachments = [