@librechat/agents 3.0.42 → 3.0.43
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.
- package/dist/cjs/agents/AgentContext.cjs +134 -70
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +1 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +7 -13
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/main.cjs +5 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +85 -0
- package/dist/cjs/messages/tools.cjs.map +1 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +55 -32
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +30 -13
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +134 -70
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +1 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +8 -14
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +82 -0
- package/dist/esm/messages/tools.mjs.map +1 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +54 -33
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +30 -13
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +37 -17
- package/dist/types/common/enum.d.ts +1 -1
- package/dist/types/messages/index.d.ts +1 -0
- package/dist/types/messages/tools.d.ts +17 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +15 -23
- package/dist/types/tools/ToolNode.d.ts +9 -7
- package/dist/types/types/tools.d.ts +5 -5
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +157 -85
- package/src/agents/__tests__/AgentContext.test.ts +805 -0
- package/src/common/enum.ts +1 -1
- package/src/graphs/Graph.ts +9 -21
- package/src/messages/__tests__/tools.test.ts +473 -0
- package/src/messages/index.ts +1 -0
- package/src/messages/tools.ts +99 -0
- package/src/scripts/code_exec_ptc.ts +78 -21
- package/src/scripts/programmatic_exec.ts +3 -3
- package/src/scripts/programmatic_exec_agent.ts +4 -4
- package/src/tools/ProgrammaticToolCalling.ts +71 -39
- package/src/tools/ToolNode.ts +33 -14
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +9 -9
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +180 -5
- package/src/types/tools.ts +3 -5
|
@@ -6,16 +6,18 @@
|
|
|
6
6
|
import { describe, it, expect, beforeEach } from '@jest/globals';
|
|
7
7
|
import type * as t from '@/types';
|
|
8
8
|
import {
|
|
9
|
-
executeTools,
|
|
10
|
-
formatCompletedResponse,
|
|
11
9
|
createProgrammaticToolCallingTool,
|
|
10
|
+
formatCompletedResponse,
|
|
11
|
+
extractUsedToolNames,
|
|
12
|
+
filterToolsByUsage,
|
|
13
|
+
executeTools,
|
|
12
14
|
} from '../ProgrammaticToolCalling';
|
|
13
15
|
import {
|
|
16
|
+
createProgrammaticToolRegistry,
|
|
14
17
|
createGetTeamMembersTool,
|
|
15
18
|
createGetExpensesTool,
|
|
16
19
|
createGetWeatherTool,
|
|
17
20
|
createCalculatorTool,
|
|
18
|
-
createProgrammaticToolRegistry,
|
|
19
21
|
} from '@/test/mockTools';
|
|
20
22
|
|
|
21
23
|
describe('ProgrammaticToolCalling', () => {
|
|
@@ -183,6 +185,179 @@ describe('ProgrammaticToolCalling', () => {
|
|
|
183
185
|
});
|
|
184
186
|
});
|
|
185
187
|
|
|
188
|
+
describe('extractUsedToolNames', () => {
|
|
189
|
+
const availableTools = new Set([
|
|
190
|
+
'get_weather',
|
|
191
|
+
'get_team_members',
|
|
192
|
+
'get_expenses',
|
|
193
|
+
'calculator',
|
|
194
|
+
'search_docs',
|
|
195
|
+
]);
|
|
196
|
+
|
|
197
|
+
it('extracts single tool name from simple code', () => {
|
|
198
|
+
const code = `result = await get_weather(city="SF")
|
|
199
|
+
print(result)`;
|
|
200
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
201
|
+
|
|
202
|
+
expect(used.size).toBe(1);
|
|
203
|
+
expect(used.has('get_weather')).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('extracts multiple tool names from code', () => {
|
|
207
|
+
const code = `team = await get_team_members()
|
|
208
|
+
for member in team:
|
|
209
|
+
expenses = await get_expenses(user_id=member['id'])
|
|
210
|
+
print(f"{member['name']}: {sum(e['amount'] for e in expenses)}")`;
|
|
211
|
+
|
|
212
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
213
|
+
|
|
214
|
+
expect(used.size).toBe(2);
|
|
215
|
+
expect(used.has('get_team_members')).toBe(true);
|
|
216
|
+
expect(used.has('get_expenses')).toBe(true);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('extracts tools from asyncio.gather calls', () => {
|
|
220
|
+
const code = `results = await asyncio.gather(
|
|
221
|
+
get_weather(city="SF"),
|
|
222
|
+
get_weather(city="NYC"),
|
|
223
|
+
get_expenses(user_id="u1")
|
|
224
|
+
)`;
|
|
225
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
226
|
+
|
|
227
|
+
expect(used.size).toBe(2);
|
|
228
|
+
expect(used.has('get_weather')).toBe(true);
|
|
229
|
+
expect(used.has('get_expenses')).toBe(true);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('does not match partial tool names', () => {
|
|
233
|
+
const code = `# Using get_weather_data instead
|
|
234
|
+
result = await get_weather_data(city="SF")`;
|
|
235
|
+
|
|
236
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
237
|
+
expect(used.has('get_weather')).toBe(false);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('matches tool names in different contexts', () => {
|
|
241
|
+
const code = `# direct call
|
|
242
|
+
x = await calculator(expression="1+1")
|
|
243
|
+
# in list comprehension
|
|
244
|
+
results = [await get_weather(city=c) for c in cities]
|
|
245
|
+
# conditional
|
|
246
|
+
if condition:
|
|
247
|
+
await get_team_members()`;
|
|
248
|
+
|
|
249
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
250
|
+
|
|
251
|
+
expect(used.size).toBe(3);
|
|
252
|
+
expect(used.has('calculator')).toBe(true);
|
|
253
|
+
expect(used.has('get_weather')).toBe(true);
|
|
254
|
+
expect(used.has('get_team_members')).toBe(true);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('returns empty set when no tools are used', () => {
|
|
258
|
+
const code = `print("Hello, World!")
|
|
259
|
+
x = 1 + 2`;
|
|
260
|
+
|
|
261
|
+
const used = extractUsedToolNames(code, availableTools);
|
|
262
|
+
expect(used.size).toBe(0);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('handles tool names with special regex characters', () => {
|
|
266
|
+
const specialTools = new Set(['get_data.v2', 'calc+plus']);
|
|
267
|
+
const code = `await get_data.v2()
|
|
268
|
+
await calc+plus()`;
|
|
269
|
+
|
|
270
|
+
const used = extractUsedToolNames(code, specialTools);
|
|
271
|
+
|
|
272
|
+
expect(used.has('get_data.v2')).toBe(true);
|
|
273
|
+
expect(used.has('calc+plus')).toBe(true);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe('filterToolsByUsage', () => {
|
|
278
|
+
const allToolDefs: t.LCTool[] = [
|
|
279
|
+
{
|
|
280
|
+
name: 'get_weather',
|
|
281
|
+
description: 'Get weather for a city',
|
|
282
|
+
parameters: {
|
|
283
|
+
type: 'object',
|
|
284
|
+
properties: { city: { type: 'string' } },
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
name: 'get_team_members',
|
|
289
|
+
description: 'Get team members',
|
|
290
|
+
parameters: { type: 'object', properties: {} },
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
name: 'get_expenses',
|
|
294
|
+
description: 'Get expenses for a user',
|
|
295
|
+
parameters: {
|
|
296
|
+
type: 'object',
|
|
297
|
+
properties: { user_id: { type: 'string' } },
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
name: 'calculator',
|
|
302
|
+
description: 'Evaluate an expression',
|
|
303
|
+
parameters: {
|
|
304
|
+
type: 'object',
|
|
305
|
+
properties: { expression: { type: 'string' } },
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
];
|
|
309
|
+
|
|
310
|
+
it('filters to only used tools', () => {
|
|
311
|
+
const code = `result = await get_weather(city="SF")
|
|
312
|
+
print(result)`;
|
|
313
|
+
|
|
314
|
+
const filtered = filterToolsByUsage(allToolDefs, code);
|
|
315
|
+
|
|
316
|
+
expect(filtered).toHaveLength(1);
|
|
317
|
+
expect(filtered[0].name).toBe('get_weather');
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it('filters to multiple used tools', () => {
|
|
321
|
+
const code = `team = await get_team_members()
|
|
322
|
+
for member in team:
|
|
323
|
+
expenses = await get_expenses(user_id=member['id'])`;
|
|
324
|
+
|
|
325
|
+
const filtered = filterToolsByUsage(allToolDefs, code);
|
|
326
|
+
|
|
327
|
+
expect(filtered).toHaveLength(2);
|
|
328
|
+
expect(filtered.map((t) => t.name).sort()).toEqual([
|
|
329
|
+
'get_expenses',
|
|
330
|
+
'get_team_members',
|
|
331
|
+
]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('returns all tools when no tools are detected', () => {
|
|
335
|
+
const code = 'print("Hello, World!")';
|
|
336
|
+
|
|
337
|
+
const filtered = filterToolsByUsage(allToolDefs, code);
|
|
338
|
+
|
|
339
|
+
expect(filtered).toHaveLength(4);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('preserves tool definition structure', () => {
|
|
343
|
+
const code = 'await calculator(expression="2+2")';
|
|
344
|
+
|
|
345
|
+
const filtered = filterToolsByUsage(allToolDefs, code);
|
|
346
|
+
|
|
347
|
+
expect(filtered).toHaveLength(1);
|
|
348
|
+
expect(filtered[0]).toEqual(allToolDefs[3]);
|
|
349
|
+
expect(filtered[0].parameters).toBeDefined();
|
|
350
|
+
expect(filtered[0].description).toBe('Evaluate an expression');
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('handles empty tool definitions', () => {
|
|
354
|
+
const code = 'await get_weather(city="SF")';
|
|
355
|
+
const filtered = filterToolsByUsage([], code);
|
|
356
|
+
|
|
357
|
+
expect(filtered).toHaveLength(0);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
186
361
|
describe('formatCompletedResponse', () => {
|
|
187
362
|
it('formats response with stdout', () => {
|
|
188
363
|
const response: t.ProgrammaticExecutionResponse = {
|
|
@@ -332,7 +507,7 @@ describe('ProgrammaticToolCalling', () => {
|
|
|
332
507
|
name: 'programmatic_code_execution',
|
|
333
508
|
args,
|
|
334
509
|
toolMap,
|
|
335
|
-
// No
|
|
510
|
+
// No `toolDefs`
|
|
336
511
|
};
|
|
337
512
|
|
|
338
513
|
await expect(ptcTool.invoke(args, { toolCall })).rejects.toThrow(
|
|
@@ -340,7 +515,7 @@ describe('ProgrammaticToolCalling', () => {
|
|
|
340
515
|
);
|
|
341
516
|
});
|
|
342
517
|
|
|
343
|
-
it('uses
|
|
518
|
+
it('uses toolDefs from config when tools not provided', async () => {
|
|
344
519
|
// Skip this test - requires mocking fetch which has complex typing
|
|
345
520
|
// This functionality is tested in the live script tests instead
|
|
346
521
|
});
|
package/src/types/tools.ts
CHANGED
|
@@ -35,11 +35,7 @@ export type ToolNodeOptions = {
|
|
|
35
35
|
data: ToolErrorData,
|
|
36
36
|
metadata?: Record<string, unknown>
|
|
37
37
|
) => Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
programmaticToolMap?: ToolMap;
|
|
40
|
-
/** Tool definitions for programmatic code execution (sent to Code API for stub generation) */
|
|
41
|
-
programmaticToolDefs?: LCTool[];
|
|
42
|
-
/** Tool registry for tool search (deferred tool definitions) */
|
|
38
|
+
/** Tool registry for lazy computation of programmatic tools and tool search */
|
|
43
39
|
toolRegistry?: LCToolRegistry;
|
|
44
40
|
};
|
|
45
41
|
|
|
@@ -128,6 +124,8 @@ export type LCTool = {
|
|
|
128
124
|
/** Map of tool names to tool definitions */
|
|
129
125
|
export type LCToolRegistry = Map<string, LCTool>;
|
|
130
126
|
|
|
127
|
+
export type ProgrammaticCache = { toolMap: ToolMap; toolDefs: LCTool[] };
|
|
128
|
+
|
|
131
129
|
/** Parameters for creating a Tool Search Regex tool */
|
|
132
130
|
export type ToolSearchRegexParams = {
|
|
133
131
|
apiKey?: string;
|