File size: 7,091 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | import { ContentTypes, ToolCallTypes } from 'librechat-data-provider';
import type { Agents, PartMetadata, TMessageContentParts } from 'librechat-data-provider';
import type { ToolCall } from '@langchain/core/messages/tool';
import { filterMalformedContentParts } from './content';
describe('filterMalformedContentParts', () => {
describe('basic filtering', () => {
it('should keep valid tool_call content parts', () => {
const parts: TMessageContentParts[] = [
{
type: ContentTypes.TOOL_CALL,
tool_call: {
id: 'test-id',
name: 'test_function',
type: ToolCallTypes.TOOL_CALL,
args: '{}',
progress: 1,
output: 'result',
},
},
];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(1);
expect(result[0]).toEqual(parts[0]);
});
it('should filter out malformed tool_call content parts without tool_call property', () => {
const parts: TMessageContentParts[] = [
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(0);
});
it('should keep other content types unchanged', () => {
const parts: TMessageContentParts[] = [
{ type: ContentTypes.TEXT, text: 'Hello world' },
{ type: ContentTypes.THINK, think: 'Thinking...' },
];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(2);
expect(result).toEqual(parts);
});
it('should filter out null or undefined parts', () => {
const parts = [
{ type: ContentTypes.TEXT, text: 'Valid' },
null,
undefined,
{ type: ContentTypes.TEXT, text: 'Also valid' },
] as TMessageContentParts[];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(2);
expect(result[0]).toHaveProperty('text', 'Valid');
expect(result[1]).toHaveProperty('text', 'Also valid');
});
it('should return non-array input unchanged', () => {
const notAnArray = { some: 'object' };
const result = filterMalformedContentParts(notAnArray);
expect(result).toBe(notAnArray);
});
});
describe('real-life example with multiple tool calls', () => {
it('should filter out malformed tool_call entries from actual MCP response', () => {
const parts: TMessageContentParts[] = [
{
type: ContentTypes.THINK,
think:
'The user is asking for 10 different time zones, similar to what would be displayed in a stock trading room floor.',
},
{
type: ContentTypes.TEXT,
text: '# Global Market Times\n\nShowing current time in 10 major financial centers:',
tool_call_ids: ['tooluse_Yjfib8PoRXCeCcHRH0JqCw'],
},
{
type: ContentTypes.TOOL_CALL,
tool_call: {
id: 'tooluse_Yjfib8PoRXCeCcHRH0JqCw',
name: 'get_current_time_mcp_time',
args: '{"timezone":"America/New_York"}',
type: ToolCallTypes.TOOL_CALL,
progress: 1,
output: '{"timezone":"America/New_York","datetime":"2025-11-13T13:43:17-05:00"}',
},
},
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
{
type: ContentTypes.TOOL_CALL,
tool_call: {
id: 'tooluse_CPsGv9kXTrewVkcO7BEYIg',
name: 'get_current_time_mcp_time',
args: '{"timezone":"Europe/London"}',
type: ToolCallTypes.TOOL_CALL,
progress: 1,
output: '{"timezone":"Europe/London","datetime":"2025-11-13T18:43:19+00:00"}',
},
},
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
{
type: ContentTypes.TOOL_CALL,
tool_call: {
id: 'tooluse_5jihRbd4TDWCGebwmAUlfQ',
name: 'get_current_time_mcp_time',
args: '{"timezone":"Asia/Tokyo"}',
type: ToolCallTypes.TOOL_CALL,
progress: 1,
output: '{"timezone":"Asia/Tokyo","datetime":"2025-11-14T03:43:21+09:00"}',
},
},
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
{ type: ContentTypes.TOOL_CALL } as TMessageContentParts,
{
type: ContentTypes.TEXT,
text: '## Major Financial Markets Clock:\n\n| Market | Local Time | Day |',
},
];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(6);
expect(result[0].type).toBe(ContentTypes.THINK);
expect(result[1].type).toBe(ContentTypes.TEXT);
expect(result[2].type).toBe(ContentTypes.TOOL_CALL);
expect(result[3].type).toBe(ContentTypes.TOOL_CALL);
expect(result[4].type).toBe(ContentTypes.TOOL_CALL);
expect(result[5].type).toBe(ContentTypes.TEXT);
const toolCalls = result.filter((part) => part.type === ContentTypes.TOOL_CALL);
expect(toolCalls).toHaveLength(3);
toolCalls.forEach((toolCall) => {
if (toolCall.type === ContentTypes.TOOL_CALL) {
expect(toolCall.tool_call).toBeDefined();
expect(toolCall.tool_call).toHaveProperty('id');
expect(toolCall.tool_call).toHaveProperty('name');
}
});
});
it('should handle empty array', () => {
const result = filterMalformedContentParts([]);
expect(result).toEqual([]);
});
it('should handle array with only malformed tool calls', () => {
const parts = [
{ type: ContentTypes.TOOL_CALL },
{ type: ContentTypes.TOOL_CALL },
{ type: ContentTypes.TOOL_CALL },
] as TMessageContentParts[];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(0);
});
});
describe('edge cases', () => {
it('should filter out tool_call with null tool_call property', () => {
const parts = [
{ type: ContentTypes.TOOL_CALL, tool_call: null as unknown as ToolCall },
] as TMessageContentParts[];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(0);
});
it('should filter out tool_call with non-object tool_call property', () => {
const parts = [
{
type: ContentTypes.TOOL_CALL,
tool_call: 'not an object' as unknown as ToolCall & PartMetadata,
},
] as TMessageContentParts[];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(0);
});
it('should keep tool_call with empty object as tool_call', () => {
const parts: TMessageContentParts[] = [
{
type: ContentTypes.TOOL_CALL,
tool_call: {} as unknown as Agents.ToolCall & PartMetadata,
},
];
const result = filterMalformedContentParts(parts);
expect(result).toHaveLength(1);
});
});
});
|