@librechat/agents 3.1.64 → 3.1.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -144,7 +144,9 @@ export function _convertLangChainToolCallToAnthropic(
144
144
  throw new Error('Anthropic requires all tool calls to have an "id".');
145
145
  }
146
146
  return {
147
- type: 'tool_use',
147
+ type: toolCall.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
148
+ ? 'server_tool_use'
149
+ : 'tool_use',
148
150
  id: toolCall.id,
149
151
  name: toolCall.name,
150
152
  input: toolCall.args,
@@ -364,87 +366,90 @@ function _formatContent(message: BaseMessage) {
364
366
  } else {
365
367
  const contentBlocks = content.map((contentPart) => {
366
368
  /**
367
- * Handle malformed blocks that have server tool fields mixed with text type.
368
- * These can occur when server_tool_use blocks get mislabeled during aggregation.
369
- * Correct their type ONLY if we can confirm it's a server tool by checking the ID prefix.
370
- * Anthropic needs both server_tool_use and web_search_tool_result blocks for citations to work.
369
+ * Normalize server_tool_use blocks into a clean shape the API accepts.
370
+ * These blocks may arrive with the correct type (server_tool_use) or mislabeled
371
+ * as text/tool_use after chunk concatenation or state serialization.
372
+ * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild
373
+ * a clean block with only the properties the API expects.
371
374
  */
372
375
  if (
373
376
  'id' in contentPart &&
374
- 'name' in contentPart &&
375
- 'input' in contentPart &&
376
- contentPart.type === 'text'
377
+ typeof (contentPart as Record<string, unknown>).id === 'string' &&
378
+ ((contentPart as Record<string, unknown>).id as string).startsWith(
379
+ Constants.ANTHROPIC_SERVER_TOOL_PREFIX
380
+ ) &&
381
+ 'name' in contentPart
377
382
  ) {
378
383
  const rawPart = contentPart as Record<string, unknown>;
379
- const id = rawPart.id as string;
380
-
381
- if (id && id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)) {
382
- let input = rawPart.input;
383
-
384
- // Ensure input is an object
385
- if (typeof input === 'string') {
386
- try {
387
- input = JSON.parse(input);
388
- } catch {
389
- input = {};
390
- }
384
+ let input = rawPart.input;
385
+ if (typeof input === 'string') {
386
+ try {
387
+ input = JSON.parse(input);
388
+ } catch {
389
+ input = {};
391
390
  }
391
+ }
392
+ const corrected: AnthropicServerToolUseBlockParam = {
393
+ type: 'server_tool_use',
394
+ id: rawPart.id as string,
395
+ name: (rawPart.name ?? 'web_search') as 'web_search',
396
+ input: (input ?? {}) as Record<string, unknown>,
397
+ };
398
+ return corrected;
399
+ }
392
400
 
393
- const corrected: AnthropicServerToolUseBlockParam = {
394
- type: 'server_tool_use',
395
- id,
396
- name: 'web_search',
397
- input: input as Record<string, unknown>,
401
+ /**
402
+ * Normalize web_search_tool_result blocks into a clean shape.
403
+ * Same rationale as above — the block may carry extra properties from
404
+ * streaming (input, index, etc.) that the API rejects. Rebuild cleanly.
405
+ */
406
+ if (
407
+ 'tool_use_id' in contentPart &&
408
+ typeof (contentPart as Record<string, unknown>).tool_use_id ===
409
+ 'string' &&
410
+ (
411
+ (contentPart as Record<string, unknown>).tool_use_id as string
412
+ ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&
413
+ 'content' in contentPart
414
+ ) {
415
+ const rawPart = contentPart as Record<string, unknown>;
416
+ const content = rawPart.content;
417
+ const isValidContent =
418
+ Array.isArray(content) ||
419
+ (content != null &&
420
+ typeof content === 'object' &&
421
+ 'type' in content &&
422
+ (content as Record<string, unknown>).type ===
423
+ 'web_search_tool_result_error');
424
+
425
+ if (isValidContent) {
426
+ const corrected: AnthropicWebSearchToolResultBlockParam = {
427
+ type: 'web_search_tool_result',
428
+ tool_use_id: rawPart.tool_use_id as string,
429
+ content:
430
+ content as AnthropicWebSearchToolResultBlockParam['content'],
398
431
  };
399
-
400
432
  return corrected;
401
433
  }
402
-
403
- // If it's not a server tool, skip it (return null to filter it out)
404
434
  return null;
405
435
  }
406
436
 
407
437
  /**
408
- * Handle malformed web_search_tool_result blocks marked as text.
409
- * These have tool_use_id and nested content - fix their type instead of filtering.
410
- * Only correct if we can confirm it's a web search result by checking the tool_use_id prefix.
411
- *
412
- * Handles both success results (array content) and error results (object with error_code).
438
+ * Skip non-server malformed blocks that have tool fields mixed with text type.
413
439
  */
440
+ if (
441
+ 'id' in contentPart &&
442
+ 'name' in contentPart &&
443
+ 'input' in contentPart &&
444
+ contentPart.type === 'text'
445
+ ) {
446
+ return null;
447
+ }
414
448
  if (
415
449
  'tool_use_id' in contentPart &&
416
450
  'content' in contentPart &&
417
451
  contentPart.type === 'text'
418
452
  ) {
419
- const rawPart = contentPart as Record<string, unknown>;
420
- const toolUseId = rawPart.tool_use_id as string;
421
- const content = rawPart.content;
422
-
423
- if (
424
- toolUseId &&
425
- toolUseId.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
426
- ) {
427
- // Verify content is either an array (success) or error object
428
- const isValidContent =
429
- Array.isArray(content) ||
430
- (content != null &&
431
- typeof content === 'object' &&
432
- 'type' in content &&
433
- (content as Record<string, unknown>).type ===
434
- 'web_search_tool_result_error');
435
-
436
- if (isValidContent) {
437
- const corrected: AnthropicWebSearchToolResultBlockParam = {
438
- type: 'web_search_tool_result',
439
- tool_use_id: toolUseId,
440
- content:
441
- content as AnthropicWebSearchToolResultBlockParam['content'],
442
- };
443
- return corrected;
444
- }
445
- }
446
-
447
- // If it's not a recognized server tool result format, skip it (return null to filter it out)
448
453
  return null;
449
454
  }
450
455
 
@@ -540,6 +545,15 @@ function _formatContent(message: BaseMessage) {
540
545
  contentPartCopy.type = 'tool_use';
541
546
  }
542
547
 
548
+ if (
549
+ contentPartCopy.type === 'tool_use' &&
550
+ 'id' in contentPartCopy &&
551
+ typeof contentPartCopy.id === 'string' &&
552
+ contentPartCopy.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
553
+ ) {
554
+ contentPartCopy.type = 'server_tool_use';
555
+ }
556
+
543
557
  if ('input' in contentPartCopy) {
544
558
  // Anthropic tool use inputs should be valid objects, when applicable.
545
559
  if (typeof contentPartCopy.input === 'string') {
@@ -594,7 +608,11 @@ function _formatContent(message: BaseMessage) {
594
608
  throw new Error('Unsupported message content format');
595
609
  }
596
610
  });
597
- return contentBlocks.filter((block) => block !== null);
611
+ return contentBlocks.filter(
612
+ (block) =>
613
+ block !== null &&
614
+ !(block.type === 'text' && 'text' in block && block.text === '')
615
+ );
598
616
  }
599
617
  }
600
618
 
@@ -631,19 +649,26 @@ export function _convertMessagesToAnthropicPayload(
631
649
  }
632
650
  if (isAIMessage(message) && !!message.tool_calls?.length) {
633
651
  if (typeof message.content === 'string') {
652
+ const clientToolCalls = message.tool_calls.filter(
653
+ (tc) =>
654
+ !(
655
+ tc.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ?? false
656
+ )
657
+ );
634
658
  if (message.content === '') {
635
659
  return {
636
660
  role,
637
- content: message.tool_calls.map(
638
- _convertLangChainToolCallToAnthropic
639
- ),
661
+ content:
662
+ clientToolCalls.length > 0
663
+ ? clientToolCalls.map(_convertLangChainToolCallToAnthropic)
664
+ : [{ type: 'text' as const, text: ' ' }],
640
665
  };
641
666
  } else {
642
667
  return {
643
668
  role,
644
669
  content: [
645
- { type: 'text', text: message.content },
646
- ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
670
+ { type: 'text' as const, text: message.content },
671
+ ...clientToolCalls.map(_convertLangChainToolCallToAnthropic),
647
672
  ],
648
673
  };
649
674
  }
@@ -0,0 +1,349 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages';
3
+ import type { BaseMessage } from '@langchain/core/messages';
4
+ import { _convertMessagesToAnthropicPayload } from './message_inputs';
5
+
6
+ describe('_convertMessagesToAnthropicPayload — server tool use (web search) multi-turn', () => {
7
+ it('corrects tool_use blocks with srvtoolu_ IDs to server_tool_use', () => {
8
+ const messageHistory: BaseMessage[] = [
9
+ new HumanMessage('search for X and Y'),
10
+ new AIMessage({
11
+ content: [
12
+ { type: 'text', text: 'I will search for that.' },
13
+ {
14
+ type: 'tool_use',
15
+ id: 'srvtoolu_1',
16
+ name: 'web_search',
17
+ input: { query: 'X' },
18
+ },
19
+ {
20
+ type: 'web_search_tool_result',
21
+ tool_use_id: 'srvtoolu_1',
22
+ content: [
23
+ {
24
+ type: 'web_search_result',
25
+ url: 'https://example.com',
26
+ title: 'Result',
27
+ encrypted_content: 'abc',
28
+ page_age: '1d',
29
+ },
30
+ ],
31
+ },
32
+ {
33
+ type: 'tool_use',
34
+ id: 'srvtoolu_2',
35
+ name: 'web_search',
36
+ input: { query: 'Y' },
37
+ },
38
+ {
39
+ type: 'web_search_tool_result',
40
+ tool_use_id: 'srvtoolu_2',
41
+ content: [
42
+ {
43
+ type: 'web_search_result',
44
+ url: 'https://example2.com',
45
+ title: 'Result 2',
46
+ encrypted_content: 'def',
47
+ page_age: '2d',
48
+ },
49
+ ],
50
+ },
51
+ { type: 'text', text: 'Here are the results.' },
52
+ ],
53
+ tool_calls: [
54
+ {
55
+ id: 'srvtoolu_1',
56
+ name: 'web_search',
57
+ args: { query: 'X' },
58
+ type: 'tool_call',
59
+ },
60
+ {
61
+ id: 'srvtoolu_2',
62
+ name: 'web_search',
63
+ args: { query: 'Y' },
64
+ type: 'tool_call',
65
+ },
66
+ ],
67
+ }),
68
+ new HumanMessage('follow up question'),
69
+ ];
70
+
71
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
72
+ expect(messages).toHaveLength(3);
73
+
74
+ const assistantContent = messages[1].content as any[];
75
+ const serverToolBlocks = assistantContent.filter(
76
+ (b: any) => b.type === 'server_tool_use'
77
+ );
78
+ const searchResultBlocks = assistantContent.filter(
79
+ (b: any) => b.type === 'web_search_tool_result'
80
+ );
81
+ const regularToolUseBlocks = assistantContent.filter(
82
+ (b: any) => b.type === 'tool_use' && b.id?.startsWith('srvtoolu_')
83
+ );
84
+
85
+ expect(serverToolBlocks).toHaveLength(2);
86
+ expect(searchResultBlocks).toHaveLength(2);
87
+ expect(regularToolUseBlocks).toHaveLength(0);
88
+
89
+ // Verify blocks are clean (no extra streaming properties)
90
+ for (const b of serverToolBlocks) {
91
+ expect(Object.keys(b).sort()).toEqual(
92
+ ['id', 'input', 'name', 'type'].sort()
93
+ );
94
+ }
95
+ for (const b of searchResultBlocks) {
96
+ expect(Object.keys(b).sort()).toEqual(
97
+ ['content', 'tool_use_id', 'type'].sort()
98
+ );
99
+ }
100
+ });
101
+
102
+ it('corrects text-typed server tool blocks back to correct types', () => {
103
+ const messageHistory: BaseMessage[] = [
104
+ new HumanMessage('search for X'),
105
+ new AIMessage({
106
+ content: [
107
+ {
108
+ type: 'text',
109
+ id: 'srvtoolu_1',
110
+ name: 'web_search',
111
+ input: '{"query":"X"}',
112
+ },
113
+ {
114
+ type: 'text',
115
+ tool_use_id: 'srvtoolu_1',
116
+ content: [
117
+ {
118
+ type: 'web_search_result',
119
+ url: 'https://example.com',
120
+ title: 'Result',
121
+ encrypted_content: 'abc',
122
+ page_age: '1d',
123
+ },
124
+ ],
125
+ },
126
+ { type: 'text', text: 'Found results.' },
127
+ ],
128
+ tool_calls: [
129
+ {
130
+ id: 'srvtoolu_1',
131
+ name: 'web_search',
132
+ args: { query: 'X' },
133
+ type: 'tool_call',
134
+ },
135
+ ],
136
+ }),
137
+ new HumanMessage('follow up'),
138
+ ];
139
+
140
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
141
+ const assistantContent = messages[1].content as any[];
142
+
143
+ expect(assistantContent[0]).toEqual({
144
+ type: 'server_tool_use',
145
+ id: 'srvtoolu_1',
146
+ name: 'web_search',
147
+ input: { query: 'X' },
148
+ });
149
+ expect(assistantContent[1].type).toBe('web_search_tool_result');
150
+ expect(assistantContent[1].tool_use_id).toBe('srvtoolu_1');
151
+ expect(assistantContent[2]).toEqual({
152
+ type: 'text',
153
+ text: 'Found results.',
154
+ });
155
+ });
156
+
157
+ it('filters server tool calls when content is a string', () => {
158
+ const messageHistory: BaseMessage[] = [
159
+ new HumanMessage('search for X'),
160
+ new AIMessage({
161
+ content: 'I searched and found results.',
162
+ tool_calls: [
163
+ {
164
+ id: 'srvtoolu_1',
165
+ name: 'web_search',
166
+ args: { query: 'X' },
167
+ type: 'tool_call',
168
+ },
169
+ {
170
+ id: 'toolu_regular',
171
+ name: 'calculator',
172
+ args: { expr: '2+2' },
173
+ type: 'tool_call',
174
+ },
175
+ ],
176
+ }),
177
+ new ToolMessage({
178
+ content: '4',
179
+ tool_call_id: 'toolu_regular',
180
+ }),
181
+ new HumanMessage('follow up'),
182
+ ];
183
+
184
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
185
+ const assistantContent = messages[1].content as any[];
186
+
187
+ const toolUseBlocks = assistantContent.filter(
188
+ (b: any) => b.type === 'tool_use'
189
+ );
190
+ expect(toolUseBlocks).toHaveLength(1);
191
+ expect(toolUseBlocks[0].id).toBe('toolu_regular');
192
+
193
+ const serverToolBlocks = assistantContent.filter((b: any) =>
194
+ b.id?.startsWith('srvtoolu_')
195
+ );
196
+ expect(serverToolBlocks).toHaveLength(0);
197
+ });
198
+
199
+ it('handles empty string content with only server tool calls', () => {
200
+ const messageHistory: BaseMessage[] = [
201
+ new HumanMessage('search for X'),
202
+ new AIMessage({
203
+ content: '',
204
+ tool_calls: [
205
+ {
206
+ id: 'srvtoolu_1',
207
+ name: 'web_search',
208
+ args: { query: 'X' },
209
+ type: 'tool_call',
210
+ },
211
+ ],
212
+ }),
213
+ new HumanMessage('follow up'),
214
+ ];
215
+
216
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
217
+ const assistantContent = messages[1].content as any[];
218
+ expect(assistantContent).toHaveLength(1);
219
+ expect(assistantContent[0].type).toBe('text');
220
+ expect(assistantContent[0].text).toBe(' ');
221
+ });
222
+
223
+ it('preserves regular tool_use blocks alongside corrected server tool blocks', () => {
224
+ const messageHistory: BaseMessage[] = [
225
+ new HumanMessage('search for X and calculate 2+2'),
226
+ new AIMessage({
227
+ content: [
228
+ { type: 'text', text: 'Let me help.' },
229
+ {
230
+ type: 'tool_use',
231
+ id: 'srvtoolu_1',
232
+ name: 'web_search',
233
+ input: { query: 'X' },
234
+ },
235
+ {
236
+ type: 'web_search_tool_result',
237
+ tool_use_id: 'srvtoolu_1',
238
+ content: [
239
+ {
240
+ type: 'web_search_result',
241
+ url: 'https://example.com',
242
+ title: 'Result',
243
+ encrypted_content: 'abc',
244
+ page_age: '1d',
245
+ },
246
+ ],
247
+ },
248
+ {
249
+ type: 'tool_use',
250
+ id: 'toolu_calc',
251
+ name: 'calculator',
252
+ input: { expr: '2+2' },
253
+ },
254
+ ],
255
+ tool_calls: [
256
+ {
257
+ id: 'srvtoolu_1',
258
+ name: 'web_search',
259
+ args: { query: 'X' },
260
+ type: 'tool_call',
261
+ },
262
+ {
263
+ id: 'toolu_calc',
264
+ name: 'calculator',
265
+ args: { expr: '2+2' },
266
+ type: 'tool_call',
267
+ },
268
+ ],
269
+ }),
270
+ new ToolMessage({
271
+ content: '4',
272
+ tool_call_id: 'toolu_calc',
273
+ }),
274
+ new HumanMessage('follow up'),
275
+ ];
276
+
277
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
278
+ const assistantContent = messages[1].content as any[];
279
+
280
+ const serverToolUse = assistantContent.filter(
281
+ (b: any) => b.type === 'server_tool_use'
282
+ );
283
+ const webSearchResult = assistantContent.filter(
284
+ (b: any) => b.type === 'web_search_tool_result'
285
+ );
286
+ const regularToolUse = assistantContent.filter(
287
+ (b: any) => b.type === 'tool_use' && !b.id?.startsWith('srvtoolu_')
288
+ );
289
+
290
+ expect(serverToolUse).toHaveLength(1);
291
+ expect(serverToolUse[0].id).toBe('srvtoolu_1');
292
+ expect(webSearchResult).toHaveLength(1);
293
+ expect(regularToolUse).toHaveLength(1);
294
+ expect(regularToolUse[0].id).toBe('toolu_calc');
295
+ });
296
+
297
+ it('filters out empty text blocks from array content', () => {
298
+ const messageHistory: BaseMessage[] = [
299
+ new HumanMessage('search for X'),
300
+ new AIMessage({
301
+ content: [
302
+ { type: 'text', text: '' },
303
+ {
304
+ type: 'server_tool_use',
305
+ id: 'srvtoolu_1',
306
+ name: 'web_search',
307
+ input: { query: 'X' },
308
+ },
309
+ {
310
+ type: 'web_search_tool_result',
311
+ tool_use_id: 'srvtoolu_1',
312
+ content: [
313
+ {
314
+ type: 'web_search_result',
315
+ url: 'https://example.com',
316
+ title: 'Result',
317
+ encrypted_content: 'abc',
318
+ page_age: '1d',
319
+ },
320
+ ],
321
+ },
322
+ { type: 'text', text: '' },
323
+ { type: 'text', text: 'Here are the results.' },
324
+ ],
325
+ tool_calls: [
326
+ {
327
+ id: 'srvtoolu_1',
328
+ name: 'web_search',
329
+ args: { query: 'X' },
330
+ type: 'tool_call',
331
+ },
332
+ ],
333
+ }),
334
+ new HumanMessage('follow up'),
335
+ ];
336
+
337
+ const { messages } = _convertMessagesToAnthropicPayload(messageHistory);
338
+ const assistantContent = messages[1].content as any[];
339
+
340
+ const emptyTextBlocks = assistantContent.filter(
341
+ (b: any) => b.type === 'text' && b.text === ''
342
+ );
343
+ expect(emptyTextBlocks).toHaveLength(0);
344
+
345
+ const textBlocks = assistantContent.filter((b: any) => b.type === 'text');
346
+ expect(textBlocks).toHaveLength(1);
347
+ expect(textBlocks[0].text).toBe('Here are the results.');
348
+ });
349
+ });
@@ -41,7 +41,14 @@ User: ${userMessage[1]}
41
41
  const _allowedTypes = ['image_url', 'text', 'tool_use', 'tool_result'];
42
42
  const allowedTypesByProvider: Record<string, string[]> = {
43
43
  default: _allowedTypes,
44
- [Providers.ANTHROPIC]: [..._allowedTypes, 'thinking', 'redacted_thinking'],
44
+ [Providers.ANTHROPIC]: [
45
+ ..._allowedTypes,
46
+ 'thinking',
47
+ 'redacted_thinking',
48
+ 'server_tool_use',
49
+ 'web_search_tool_result',
50
+ 'web_search_result',
51
+ ],
45
52
  [Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],
46
53
  [Providers.OPENAI]: _allowedTypes,
47
54
  };