🥅 fix: Keep Tool Error Prefix Parsing In Bounds (#15857)

This commit is contained in:
Danny Avila 2026-09-12 07:33:16 -04:00 • committed by GitHub
parent 1d45f5bd40
commit 7b71c3fc35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 56 additions and 7 deletions

View file

@ -1,6 +1,7 @@
import { useState, useMemo, useCallback } from 'react';
import copy from 'copy-to-clipboard';
import { Button } from '@librechat/client';
import { hasToolCallErrorPrefix, stripToolCallErrorPrefix } from 'librechat-data-provider';
import CopyButton from '~/components/Messages/Content/CopyButton';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
@ -10,11 +11,10 @@ interface ContentBlock {
text?: string;
}
const ERROR_PREFIX = /^Error:\s*(\[.*?\]\s*)*tool call failed:\s*/i;
const ERROR_INNER = /^Error\s+\w+ing to endpoint\s*\(HTTP \d+\):\s*/i;
function cleanError(text: string): string {
let cleaned = text.replace(ERROR_PREFIX, '').trim();
let cleaned = stripToolCallErrorPrefix(text).trim();
cleaned = cleaned.replace(ERROR_INNER, '').trim();
if (cleaned.endsWith('Please fix your mistakes.')) {
cleaned = cleaned.slice(0, -'Please fix your mistakes.'.length).trim();
@ -23,7 +23,7 @@ function cleanError(text: string): string {
}
export function isError(text: string): boolean {
return ERROR_PREFIX.test(text) || text.startsWith('Error processing tool');
return hasToolCallErrorPrefix(text) || text.startsWith('Error processing tool');
}
function isStructuredText(text: string): boolean {

View file

@ -1,6 +1,6 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import OutputRenderer from '../OutputRenderer';
import OutputRenderer, { isError } from '../OutputRenderer';
jest.mock('copy-to-clipboard', () => jest.fn());
@ -21,4 +21,8 @@ describe('OutputRenderer', () => {
expect(copyPositioner).toHaveClass('absolute', 'right-0', 'top-1/2', '-translate-y-1/2');
expect(copyPositioner?.parentElement).toHaveClass('relative', 'pr-10');
});
it('does not treat text between bracketed prefixes as a tool-call error', () => {
expect(isError('Error: [agent] unexpected [search] tool call failed: unavailable')).toBe(false);
});
});

View file

@ -2,7 +2,11 @@ import yaml from 'js-yaml';
import { Types } from 'mongoose';
import { GraphEvents, Constants, ToolEndHandler } from '@librechat/agents';
import { logger, normalizeSkillFrontmatterKeys } from '@librechat/data-schemas';
import { hasActivePiiFields, hasActivePiiPatterns } from 'librechat-data-provider';
import {
hasActivePiiFields,
hasActivePiiPatterns,
hasToolCallErrorPrefix,
} from 'librechat-data-provider';
import type {
LCTool,
FileRefs,
@ -5124,7 +5128,7 @@ function getFileAuthoringQueueKey(
* failed background run renders as clean stdout.
*/
function toBackgroundToolFailure(toolName: string, message: string): string {
if (/^Error:\s*(\[.*?\]\s*)*tool call failed:/i.test(message)) {
if (hasToolCallErrorPrefix(message)) {
return message;
}
return `Error: [${toolName}] tool call failed: ${message}`;

View file

@ -8,6 +8,7 @@ import {
ApprovalEvents,
SteerEvents,
parseTextParts,
hasToolCallErrorPrefix,
reconcileContextUsageFromEvent,
} from 'librechat-data-provider';
import type {
@ -126,7 +127,7 @@ function completedToolExecutionStatus(call: Agents.ToolCall): ToolExecutionStatu
}
const output = call.output;
return typeof output === 'string' &&
(/^Error:\s*(\[.*?\]\s*)*tool call failed:/i.test(output) ||
(hasToolCallErrorPrefix(output) ||
/^Error processing tool(?::|$)/i.test(output) ||
/^Error:[\s\S]*\n Please fix your mistakes\.$/i.test(output))
? 'error'

View file

@ -0,0 +1,30 @@
import { hasToolCallErrorPrefix, stripToolCallErrorPrefix } from './errors';
describe('tool call errors', () => {
test.each([
'Error: tool call failed: unavailable',
'Error: [search] tool call failed: unavailable',
'Error: [agent] [search] tool call failed: unavailable',
])('recognizes %s', (message) => {
expect(hasToolCallErrorPrefix(message)).toBe(true);
});
test('does not scan across bracket boundaries', () => {
const ambiguous = 'Error: [agent] unexpected [search] tool call failed: unavailable';
expect(hasToolCallErrorPrefix(ambiguous)).toBe(false);
expect(stripToolCallErrorPrefix(ambiguous)).toBe(ambiguous);
});
test('rejects repeated bracket segments without the required suffix', () => {
const incomplete = `Error: ${'[tool] '.repeat(20)}unavailable`;
expect(hasToolCallErrorPrefix(incomplete)).toBe(false);
});
test('strips the complete prefix', () => {
expect(stripToolCallErrorPrefix('Error: [agent] [search] tool call failed: unavailable')).toBe(
'unavailable',
);
});
});

View file

@ -0,0 +1,9 @@
const TOOL_CALL_ERROR_PREFIX = /^Error:\s*(?:\[[^\]]*\]\s*)*tool call failed:\s*/i;
export function hasToolCallErrorPrefix(text: string): boolean {
return TOOL_CALL_ERROR_PREFIX.test(text);
}
export function stripToolCallErrorPrefix(text: string): string {
return text.replace(TOOL_CALL_ERROR_PREFIX, '');
}

View file

@ -9,6 +9,7 @@ export * from './file-config';
export * from './resolve-llm-delivery-path';
/* messages */
export * from './messages';
export * from './errors';
/* run steps */
export * from './runSteps';
/* artifacts */