mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 19:30:15 +01:00
🔧 fix: Await MCP Instructions and Filter Malformed Tool Calls (#10485)
* fix: Await MCP instructions formatting in AgentClient * fix: don't render or aggregate malformed tool calls * fix: implement filter for malformed tool call content parts and add tests
This commit is contained in:
parent
aff3cd3667
commit
cabc8afeac
10 changed files with 467 additions and 9 deletions
46
packages/api/src/utils/content.ts
Normal file
46
packages/api/src/utils/content.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { ContentTypes } from 'librechat-data-provider';
|
||||
import type { TMessageContentParts } from 'librechat-data-provider';
|
||||
|
||||
/**
|
||||
* Filters out malformed tool call content parts that don't have the required tool_call property.
|
||||
* This handles edge cases where tool_call content parts may be created with only a type property
|
||||
* but missing the actual tool_call data.
|
||||
*
|
||||
* @param contentParts - Array of content parts to filter
|
||||
* @returns Filtered array with malformed tool calls removed
|
||||
*
|
||||
* @example
|
||||
* // Removes malformed tool_call without the tool_call property
|
||||
* const parts = [
|
||||
* { type: 'tool_call', tool_call: { id: '123', name: 'test' } }, // valid - kept
|
||||
* { type: 'tool_call' }, // invalid - filtered out
|
||||
* { type: 'text', text: 'Hello' }, // valid - kept (other types pass through)
|
||||
* ];
|
||||
* const filtered = filterMalformedContentParts(parts);
|
||||
* // Returns all parts except the malformed tool_call
|
||||
*/
|
||||
export function filterMalformedContentParts(
|
||||
contentParts: TMessageContentParts[],
|
||||
): TMessageContentParts[];
|
||||
export function filterMalformedContentParts<T>(contentParts: T): T;
|
||||
export function filterMalformedContentParts<T>(
|
||||
contentParts: T | TMessageContentParts[],
|
||||
): T | TMessageContentParts[] {
|
||||
if (!Array.isArray(contentParts)) {
|
||||
return contentParts;
|
||||
}
|
||||
|
||||
return contentParts.filter((part) => {
|
||||
if (!part || typeof part !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { type } = part;
|
||||
|
||||
if (type === ContentTypes.TOOL_CALL) {
|
||||
return 'tool_call' in part && part.tool_call != null && typeof part.tool_call === 'object';
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue