@librechat/agents 3.0.35 → 3.0.40
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 +71 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/events.cjs +3 -0
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +7 -2
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/instrumentation.cjs +1 -1
- package/dist/cjs/instrumentation.cjs.map +1 -1
- package/dist/cjs/main.cjs +12 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +329 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +34 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearchRegex.cjs +455 -0
- package/dist/cjs/tools/ToolSearchRegex.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +71 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/events.mjs +4 -1
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +7 -2
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/instrumentation.mjs +1 -1
- package/dist/esm/instrumentation.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +324 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +34 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearchRegex.mjs +448 -0
- package/dist/esm/tools/ToolSearchRegex.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +25 -1
- package/dist/types/common/enum.d.ts +2 -0
- package/dist/types/graphs/Graph.d.ts +2 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/test/mockTools.d.ts +28 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +86 -0
- package/dist/types/tools/ToolNode.d.ts +7 -1
- package/dist/types/tools/ToolSearchRegex.d.ts +80 -0
- package/dist/types/types/graph.d.ts +7 -1
- package/dist/types/types/tools.d.ts +136 -0
- package/package.json +5 -1
- package/src/agents/AgentContext.ts +86 -0
- package/src/common/enum.ts +2 -0
- package/src/events.ts +5 -1
- package/src/graphs/Graph.ts +8 -1
- package/src/index.ts +2 -0
- package/src/instrumentation.ts +1 -1
- package/src/llm/google/llm.spec.ts +3 -1
- package/src/scripts/code_exec_ptc.ts +277 -0
- package/src/scripts/programmatic_exec.ts +396 -0
- package/src/scripts/programmatic_exec_agent.ts +231 -0
- package/src/scripts/tool_search_regex.ts +162 -0
- package/src/test/mockTools.ts +366 -0
- package/src/tools/ProgrammaticToolCalling.ts +423 -0
- package/src/tools/ToolNode.ts +38 -4
- package/src/tools/ToolSearchRegex.ts +535 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +318 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +613 -0
- package/src/tools/__tests__/ToolSearchRegex.integration.test.ts +161 -0
- package/src/tools/__tests__/ToolSearchRegex.test.ts +232 -0
- package/src/types/graph.ts +7 -1
- package/src/types/tools.ts +166 -0
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
// src/tools/__tests__/ProgrammaticToolCalling.test.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unit tests for Programmatic Tool Calling.
|
|
4
|
+
* Tests manual invocation with mock tools and Code API responses.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, beforeEach } from '@jest/globals';
|
|
7
|
+
import type * as t from '@/types';
|
|
8
|
+
import {
|
|
9
|
+
executeTools,
|
|
10
|
+
formatCompletedResponse,
|
|
11
|
+
createProgrammaticToolCallingTool,
|
|
12
|
+
} from '../ProgrammaticToolCalling';
|
|
13
|
+
import {
|
|
14
|
+
createGetTeamMembersTool,
|
|
15
|
+
createGetExpensesTool,
|
|
16
|
+
createGetWeatherTool,
|
|
17
|
+
createCalculatorTool,
|
|
18
|
+
createProgrammaticToolRegistry,
|
|
19
|
+
} from '@/test/mockTools';
|
|
20
|
+
|
|
21
|
+
describe('ProgrammaticToolCalling', () => {
|
|
22
|
+
describe('executeTools', () => {
|
|
23
|
+
let toolMap: t.ToolMap;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
const tools = [
|
|
27
|
+
createGetTeamMembersTool(),
|
|
28
|
+
createGetExpensesTool(),
|
|
29
|
+
createGetWeatherTool(),
|
|
30
|
+
createCalculatorTool(),
|
|
31
|
+
];
|
|
32
|
+
toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('executes a single tool successfully', async () => {
|
|
36
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
37
|
+
{
|
|
38
|
+
id: 'call_001',
|
|
39
|
+
name: 'get_weather',
|
|
40
|
+
input: { city: 'San Francisco' },
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
45
|
+
|
|
46
|
+
expect(results).toHaveLength(1);
|
|
47
|
+
expect(results[0].call_id).toBe('call_001');
|
|
48
|
+
expect(results[0].is_error).toBe(false);
|
|
49
|
+
expect(results[0].result).toEqual({
|
|
50
|
+
temperature: 65,
|
|
51
|
+
condition: 'Foggy',
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('executes multiple tools in parallel', async () => {
|
|
56
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
57
|
+
{
|
|
58
|
+
id: 'call_001',
|
|
59
|
+
name: 'get_weather',
|
|
60
|
+
input: { city: 'San Francisco' },
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 'call_002',
|
|
64
|
+
name: 'get_weather',
|
|
65
|
+
input: { city: 'New York' },
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 'call_003',
|
|
69
|
+
name: 'get_weather',
|
|
70
|
+
input: { city: 'London' },
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const startTime = Date.now();
|
|
75
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
76
|
+
const duration = Date.now() - startTime;
|
|
77
|
+
|
|
78
|
+
// Should execute in parallel (< 150ms total, not 120ms sequential)
|
|
79
|
+
expect(duration).toBeLessThan(150);
|
|
80
|
+
|
|
81
|
+
expect(results).toHaveLength(3);
|
|
82
|
+
expect(results[0].is_error).toBe(false);
|
|
83
|
+
expect(results[1].is_error).toBe(false);
|
|
84
|
+
expect(results[2].is_error).toBe(false);
|
|
85
|
+
|
|
86
|
+
expect(results[0].result.temperature).toBe(65);
|
|
87
|
+
expect(results[1].result.temperature).toBe(75);
|
|
88
|
+
expect(results[2].result.temperature).toBe(55);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('handles tool not found error', async () => {
|
|
92
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
93
|
+
{
|
|
94
|
+
id: 'call_001',
|
|
95
|
+
name: 'nonexistent_tool',
|
|
96
|
+
input: {},
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
101
|
+
|
|
102
|
+
expect(results).toHaveLength(1);
|
|
103
|
+
expect(results[0].call_id).toBe('call_001');
|
|
104
|
+
expect(results[0].is_error).toBe(true);
|
|
105
|
+
expect(results[0].error_message).toContain('nonexistent_tool');
|
|
106
|
+
expect(results[0].error_message).toContain('Available tools:');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('handles tool execution error', async () => {
|
|
110
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
111
|
+
{
|
|
112
|
+
id: 'call_001',
|
|
113
|
+
name: 'get_weather',
|
|
114
|
+
input: { city: 'InvalidCity' },
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
119
|
+
|
|
120
|
+
expect(results).toHaveLength(1);
|
|
121
|
+
expect(results[0].call_id).toBe('call_001');
|
|
122
|
+
expect(results[0].is_error).toBe(true);
|
|
123
|
+
expect(results[0].error_message).toContain('Weather data not available');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('handles mix of successful and failed tool calls', async () => {
|
|
127
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
128
|
+
{
|
|
129
|
+
id: 'call_001',
|
|
130
|
+
name: 'get_weather',
|
|
131
|
+
input: { city: 'San Francisco' },
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
id: 'call_002',
|
|
135
|
+
name: 'get_weather',
|
|
136
|
+
input: { city: 'InvalidCity' },
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
id: 'call_003',
|
|
140
|
+
name: 'get_weather',
|
|
141
|
+
input: { city: 'New York' },
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
146
|
+
|
|
147
|
+
expect(results).toHaveLength(3);
|
|
148
|
+
expect(results[0].is_error).toBe(false);
|
|
149
|
+
expect(results[1].is_error).toBe(true);
|
|
150
|
+
expect(results[2].is_error).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('executes tools with different parameters', async () => {
|
|
154
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
155
|
+
{
|
|
156
|
+
id: 'call_001',
|
|
157
|
+
name: 'get_team_members',
|
|
158
|
+
input: {},
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 'call_002',
|
|
162
|
+
name: 'get_expenses',
|
|
163
|
+
input: { user_id: 'u1' },
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: 'call_003',
|
|
167
|
+
name: 'calculator',
|
|
168
|
+
input: { expression: '2 + 2 * 3' },
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
173
|
+
|
|
174
|
+
expect(results).toHaveLength(3);
|
|
175
|
+
expect(results[0].is_error).toBe(false);
|
|
176
|
+
expect(results[1].is_error).toBe(false);
|
|
177
|
+
expect(results[2].is_error).toBe(false);
|
|
178
|
+
|
|
179
|
+
expect(Array.isArray(results[0].result)).toBe(true);
|
|
180
|
+
expect(results[0].result).toHaveLength(3);
|
|
181
|
+
expect(Array.isArray(results[1].result)).toBe(true);
|
|
182
|
+
expect(results[2].result.result).toBe(8);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('formatCompletedResponse', () => {
|
|
187
|
+
it('formats response with stdout', () => {
|
|
188
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
189
|
+
status: 'completed',
|
|
190
|
+
stdout: 'Hello, World!\n',
|
|
191
|
+
stderr: '',
|
|
192
|
+
files: [],
|
|
193
|
+
session_id: 'sess_abc123',
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const [output, artifact] = formatCompletedResponse(response);
|
|
197
|
+
|
|
198
|
+
expect(output).toContain('stdout:\nHello, World!');
|
|
199
|
+
expect(artifact.session_id).toBe('sess_abc123');
|
|
200
|
+
expect(artifact.files).toEqual([]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('shows empty output message when no stdout', () => {
|
|
204
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
205
|
+
status: 'completed',
|
|
206
|
+
stdout: '',
|
|
207
|
+
stderr: '',
|
|
208
|
+
files: [],
|
|
209
|
+
session_id: 'sess_abc123',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const [output] = formatCompletedResponse(response);
|
|
213
|
+
|
|
214
|
+
expect(output).toContain(
|
|
215
|
+
'stdout: Empty. Ensure you\'re writing output explicitly'
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('includes stderr when present', () => {
|
|
220
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
221
|
+
status: 'completed',
|
|
222
|
+
stdout: 'Output\n',
|
|
223
|
+
stderr: 'Warning: deprecated function\n',
|
|
224
|
+
files: [],
|
|
225
|
+
session_id: 'sess_abc123',
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const [output] = formatCompletedResponse(response);
|
|
229
|
+
|
|
230
|
+
expect(output).toContain('stdout:\nOutput');
|
|
231
|
+
expect(output).toContain('stderr:\nWarning: deprecated function');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('formats file information correctly', () => {
|
|
235
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
236
|
+
status: 'completed',
|
|
237
|
+
stdout: 'Generated report\n',
|
|
238
|
+
stderr: '',
|
|
239
|
+
files: [
|
|
240
|
+
{ id: '1', name: 'report.pdf' },
|
|
241
|
+
{ id: '2', name: 'data.csv' },
|
|
242
|
+
],
|
|
243
|
+
session_id: 'sess_abc123',
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const [output, artifact] = formatCompletedResponse(response);
|
|
247
|
+
|
|
248
|
+
expect(output).toContain('Generated files:');
|
|
249
|
+
expect(output).toContain('report.pdf');
|
|
250
|
+
expect(output).toContain('data.csv');
|
|
251
|
+
expect(output).toContain('session_id: sess_abc123');
|
|
252
|
+
expect(artifact.files).toHaveLength(2);
|
|
253
|
+
expect(artifact.files).toEqual(response.files);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('handles image files with special message', () => {
|
|
257
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
258
|
+
status: 'completed',
|
|
259
|
+
stdout: '',
|
|
260
|
+
stderr: '',
|
|
261
|
+
files: [
|
|
262
|
+
{ id: '1', name: 'chart.png' },
|
|
263
|
+
{ id: '2', name: 'photo.jpg' },
|
|
264
|
+
],
|
|
265
|
+
session_id: 'sess_abc123',
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const [output] = formatCompletedResponse(response);
|
|
269
|
+
|
|
270
|
+
expect(output).toContain('chart.png');
|
|
271
|
+
expect(output).toContain('Image is already displayed to the user');
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe('createProgrammaticToolCallingTool - Manual Invocation', () => {
|
|
276
|
+
let ptcTool: ReturnType<typeof createProgrammaticToolCallingTool>;
|
|
277
|
+
let toolMap: t.ToolMap;
|
|
278
|
+
let toolDefinitions: t.LCTool[];
|
|
279
|
+
|
|
280
|
+
beforeEach(() => {
|
|
281
|
+
const tools = [
|
|
282
|
+
createGetTeamMembersTool(),
|
|
283
|
+
createGetExpensesTool(),
|
|
284
|
+
createGetWeatherTool(),
|
|
285
|
+
];
|
|
286
|
+
toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
287
|
+
toolDefinitions = Array.from(
|
|
288
|
+
createProgrammaticToolRegistry().values()
|
|
289
|
+
).filter((t) =>
|
|
290
|
+
['get_team_members', 'get_expenses', 'get_weather'].includes(t.name)
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
ptcTool = createProgrammaticToolCallingTool({
|
|
294
|
+
apiKey: 'test-key',
|
|
295
|
+
baseUrl: 'http://mock-api',
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('throws error when no toolMap provided', async () => {
|
|
300
|
+
await expect(
|
|
301
|
+
ptcTool.invoke({
|
|
302
|
+
code: 'result = await get_weather(city="SF")\nprint(result)',
|
|
303
|
+
tools: toolDefinitions,
|
|
304
|
+
toolMap,
|
|
305
|
+
})
|
|
306
|
+
).rejects.toThrow('No toolMap provided');
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('throws error when toolMap is empty', async () => {
|
|
310
|
+
const args = {
|
|
311
|
+
code: 'result = await get_weather(city="SF")\nprint(result)',
|
|
312
|
+
tools: toolDefinitions,
|
|
313
|
+
toolMap: new Map(),
|
|
314
|
+
};
|
|
315
|
+
const toolCall = {
|
|
316
|
+
name: 'programmatic_tool_calling',
|
|
317
|
+
args,
|
|
318
|
+
};
|
|
319
|
+
await expect(
|
|
320
|
+
ptcTool.invoke(args, {
|
|
321
|
+
toolCall,
|
|
322
|
+
})
|
|
323
|
+
).rejects.toThrow('No toolMap provided');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('throws error when no tool definitions provided', async () => {
|
|
327
|
+
const args = {
|
|
328
|
+
code: 'result = await get_weather(city="SF")\nprint(result)',
|
|
329
|
+
// No tools
|
|
330
|
+
};
|
|
331
|
+
const toolCall = {
|
|
332
|
+
name: 'programmatic_code_execution',
|
|
333
|
+
args,
|
|
334
|
+
toolMap,
|
|
335
|
+
// No programmaticToolDefs
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
await expect(ptcTool.invoke(args, { toolCall })).rejects.toThrow(
|
|
339
|
+
'No tool definitions provided'
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it('uses programmaticToolDefs from config when tools not provided', async () => {
|
|
344
|
+
// Skip this test - requires mocking fetch which has complex typing
|
|
345
|
+
// This functionality is tested in the live script tests instead
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe('Tool Classification', () => {
|
|
350
|
+
it('filters tools by allowed_callers', () => {
|
|
351
|
+
const registry = createProgrammaticToolRegistry();
|
|
352
|
+
|
|
353
|
+
const codeExecutionTools = Array.from(registry.values()).filter((t) =>
|
|
354
|
+
(t.allowed_callers ?? ['direct']).includes('code_execution')
|
|
355
|
+
);
|
|
356
|
+
// get_team_members, get_expenses, calculator: code_execution only
|
|
357
|
+
const codeOnlyTools = codeExecutionTools.filter(
|
|
358
|
+
(t) => !(t.allowed_callers?.includes('direct') === true)
|
|
359
|
+
);
|
|
360
|
+
expect(codeOnlyTools.length).toBeGreaterThanOrEqual(3);
|
|
361
|
+
|
|
362
|
+
// get_weather: both direct and code_execution
|
|
363
|
+
const bothTools = Array.from(registry.values()).filter(
|
|
364
|
+
(t) =>
|
|
365
|
+
t.allowed_callers?.includes('direct') === true &&
|
|
366
|
+
t.allowed_callers.includes('code_execution')
|
|
367
|
+
);
|
|
368
|
+
expect(bothTools.length).toBeGreaterThanOrEqual(1);
|
|
369
|
+
expect(bothTools.some((t) => t.name === 'get_weather')).toBe(true);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe('Error Handling', () => {
|
|
374
|
+
let toolMap: t.ToolMap;
|
|
375
|
+
|
|
376
|
+
beforeEach(() => {
|
|
377
|
+
const tools = [createGetWeatherTool()];
|
|
378
|
+
toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('returns error for invalid city without throwing', async () => {
|
|
382
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
383
|
+
{
|
|
384
|
+
id: 'call_001',
|
|
385
|
+
name: 'get_weather',
|
|
386
|
+
input: { city: 'InvalidCity' },
|
|
387
|
+
},
|
|
388
|
+
];
|
|
389
|
+
|
|
390
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
391
|
+
|
|
392
|
+
expect(results).toHaveLength(1);
|
|
393
|
+
expect(results[0].is_error).toBe(true);
|
|
394
|
+
expect(results[0].result).toBeNull();
|
|
395
|
+
expect(results[0].error_message).toContain('Weather data not available');
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('continues execution when one tool fails', async () => {
|
|
399
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
400
|
+
{
|
|
401
|
+
id: 'call_001',
|
|
402
|
+
name: 'get_weather',
|
|
403
|
+
input: { city: 'San Francisco' },
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
id: 'call_002',
|
|
407
|
+
name: 'get_weather',
|
|
408
|
+
input: { city: 'InvalidCity' },
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
id: 'call_003',
|
|
412
|
+
name: 'get_weather',
|
|
413
|
+
input: { city: 'London' },
|
|
414
|
+
},
|
|
415
|
+
];
|
|
416
|
+
|
|
417
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
418
|
+
|
|
419
|
+
expect(results).toHaveLength(3);
|
|
420
|
+
expect(results[0].is_error).toBe(false);
|
|
421
|
+
expect(results[1].is_error).toBe(true);
|
|
422
|
+
expect(results[2].is_error).toBe(false);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe('Parallel Execution Performance', () => {
|
|
427
|
+
let toolMap: t.ToolMap;
|
|
428
|
+
|
|
429
|
+
beforeEach(() => {
|
|
430
|
+
const tools = [createGetExpensesTool()];
|
|
431
|
+
toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it('executes tools in parallel, not sequentially', async () => {
|
|
435
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
436
|
+
{ id: 'call_001', name: 'get_expenses', input: { user_id: 'u1' } },
|
|
437
|
+
{ id: 'call_002', name: 'get_expenses', input: { user_id: 'u2' } },
|
|
438
|
+
{ id: 'call_003', name: 'get_expenses', input: { user_id: 'u3' } },
|
|
439
|
+
];
|
|
440
|
+
|
|
441
|
+
const startTime = Date.now();
|
|
442
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
443
|
+
const duration = Date.now() - startTime;
|
|
444
|
+
|
|
445
|
+
// Each tool has 30ms delay
|
|
446
|
+
// Sequential would be ~90ms, parallel should be ~30-50ms
|
|
447
|
+
expect(duration).toBeLessThan(80);
|
|
448
|
+
expect(results).toHaveLength(3);
|
|
449
|
+
expect(results.every((r) => r.is_error === false)).toBe(true);
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
describe('Response Formatting', () => {
|
|
454
|
+
it('formats stdout-only response', () => {
|
|
455
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
456
|
+
status: 'completed',
|
|
457
|
+
stdout: 'Team size: 3\n- Alice\n- Bob\n- Charlie\n',
|
|
458
|
+
stderr: '',
|
|
459
|
+
files: [],
|
|
460
|
+
session_id: 'sess_xyz',
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
const [output, artifact] = formatCompletedResponse(response);
|
|
464
|
+
|
|
465
|
+
expect(output).toBe('stdout:\nTeam size: 3\n- Alice\n- Bob\n- Charlie');
|
|
466
|
+
expect(artifact).toEqual({
|
|
467
|
+
session_id: 'sess_xyz',
|
|
468
|
+
files: [],
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it('formats response with files', () => {
|
|
473
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
474
|
+
status: 'completed',
|
|
475
|
+
stdout: 'Report generated\n',
|
|
476
|
+
stderr: '',
|
|
477
|
+
files: [
|
|
478
|
+
{ id: '1', name: 'report.csv' },
|
|
479
|
+
{ id: '2', name: 'chart.png' },
|
|
480
|
+
],
|
|
481
|
+
session_id: 'sess_xyz',
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const [output, artifact] = formatCompletedResponse(response);
|
|
485
|
+
|
|
486
|
+
expect(output).toContain('Generated files:');
|
|
487
|
+
expect(output).toContain('report.csv');
|
|
488
|
+
expect(output).toContain('chart.png');
|
|
489
|
+
expect(output).toContain('session_id: sess_xyz');
|
|
490
|
+
expect(output).toContain('File is already downloaded');
|
|
491
|
+
expect(output).toContain('Image is already displayed');
|
|
492
|
+
expect(artifact.files).toHaveLength(2);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it('handles multiple files with correct separators', () => {
|
|
496
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
497
|
+
status: 'completed',
|
|
498
|
+
stdout: 'Done\n',
|
|
499
|
+
stderr: '',
|
|
500
|
+
files: [
|
|
501
|
+
{ id: '1', name: 'file1.txt' },
|
|
502
|
+
{ id: '2', name: 'file2.txt' },
|
|
503
|
+
],
|
|
504
|
+
session_id: 'sess_xyz',
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const [output] = formatCompletedResponse(response);
|
|
508
|
+
|
|
509
|
+
// 2 files format: "- /mnt/data/file1.txt | ..., - /mnt/data/file2.txt | ..."
|
|
510
|
+
expect(output).toContain('file1.txt');
|
|
511
|
+
expect(output).toContain('file2.txt');
|
|
512
|
+
expect(output).toContain('- /mnt/data/file1.txt');
|
|
513
|
+
expect(output).toContain('- /mnt/data/file2.txt');
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it('handles many files with newline separators', () => {
|
|
517
|
+
const response: t.ProgrammaticExecutionResponse = {
|
|
518
|
+
status: 'completed',
|
|
519
|
+
stdout: 'Done\n',
|
|
520
|
+
stderr: '',
|
|
521
|
+
files: [
|
|
522
|
+
{ id: '1', name: 'file1.txt' },
|
|
523
|
+
{ id: '2', name: 'file2.txt' },
|
|
524
|
+
{ id: '3', name: 'file3.txt' },
|
|
525
|
+
{ id: '4', name: 'file4.txt' },
|
|
526
|
+
],
|
|
527
|
+
session_id: 'sess_xyz',
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const [output] = formatCompletedResponse(response);
|
|
531
|
+
|
|
532
|
+
// More than 3 files should use newline separators
|
|
533
|
+
expect(output).toContain('file1.txt');
|
|
534
|
+
expect(output).toContain('file4.txt');
|
|
535
|
+
expect(output.match(/,\n/g)?.length).toBeGreaterThanOrEqual(2);
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
describe('Tool Data Extraction', () => {
|
|
540
|
+
let toolMap: t.ToolMap;
|
|
541
|
+
|
|
542
|
+
beforeEach(() => {
|
|
543
|
+
const tools = [
|
|
544
|
+
createGetTeamMembersTool(),
|
|
545
|
+
createGetExpensesTool(),
|
|
546
|
+
createCalculatorTool(),
|
|
547
|
+
];
|
|
548
|
+
toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it('extracts correct data from team members tool', async () => {
|
|
552
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
553
|
+
{ id: 'call_001', name: 'get_team_members', input: {} },
|
|
554
|
+
];
|
|
555
|
+
|
|
556
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
557
|
+
|
|
558
|
+
expect(results[0].result).toEqual([
|
|
559
|
+
{ id: 'u1', name: 'Alice', department: 'Engineering' },
|
|
560
|
+
{ id: 'u2', name: 'Bob', department: 'Marketing' },
|
|
561
|
+
{ id: 'u3', name: 'Charlie', department: 'Engineering' },
|
|
562
|
+
]);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it('extracts correct data from expenses tool', async () => {
|
|
566
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
567
|
+
{ id: 'call_001', name: 'get_expenses', input: { user_id: 'u1' } },
|
|
568
|
+
];
|
|
569
|
+
|
|
570
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
571
|
+
|
|
572
|
+
expect(results[0].result).toEqual([
|
|
573
|
+
{ amount: 150.0, category: 'travel' },
|
|
574
|
+
{ amount: 75.5, category: 'meals' },
|
|
575
|
+
]);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
it('handles empty expense data', async () => {
|
|
579
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
580
|
+
{
|
|
581
|
+
id: 'call_001',
|
|
582
|
+
name: 'get_expenses',
|
|
583
|
+
input: { user_id: 'nonexistent' },
|
|
584
|
+
},
|
|
585
|
+
];
|
|
586
|
+
|
|
587
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
588
|
+
|
|
589
|
+
expect(results[0].is_error).toBe(false);
|
|
590
|
+
expect(results[0].result).toEqual([]);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it('calculates correct result', async () => {
|
|
594
|
+
const toolCalls: t.PTCToolCall[] = [
|
|
595
|
+
{
|
|
596
|
+
id: 'call_001',
|
|
597
|
+
name: 'calculator',
|
|
598
|
+
input: { expression: '2 + 2 * 3' },
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
id: 'call_002',
|
|
602
|
+
name: 'calculator',
|
|
603
|
+
input: { expression: '(10 + 5) / 3' },
|
|
604
|
+
},
|
|
605
|
+
];
|
|
606
|
+
|
|
607
|
+
const results = await executeTools(toolCalls, toolMap);
|
|
608
|
+
|
|
609
|
+
expect(results[0].result.result).toBe(8);
|
|
610
|
+
expect(results[1].result.result).toBe(5);
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
});
|