@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,277 @@
|
|
|
1
|
+
// src/scripts/code_exec_ptc.ts
|
|
2
|
+
/**
|
|
3
|
+
* Live LLM test for Programmatic Tool Calling (PTC).
|
|
4
|
+
* Run with: npm run code_exec_ptc
|
|
5
|
+
*
|
|
6
|
+
* Tests PTC with a real LLM in the loop, demonstrating:
|
|
7
|
+
* 1. LLM decides when to use PTC
|
|
8
|
+
* 2. LLM writes Python code that calls tools programmatically
|
|
9
|
+
* 3. ToolNode automatically injects programmatic tools
|
|
10
|
+
* 4. Tools filtered by allowed_callers
|
|
11
|
+
*/
|
|
12
|
+
import { config } from 'dotenv';
|
|
13
|
+
config();
|
|
14
|
+
|
|
15
|
+
import { randomUUID } from 'crypto';
|
|
16
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
17
|
+
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
18
|
+
import type * as t from '@/types';
|
|
19
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
20
|
+
import {
|
|
21
|
+
ToolEndHandler,
|
|
22
|
+
ModelEndHandler,
|
|
23
|
+
createMetadataAggregator,
|
|
24
|
+
} from '@/events';
|
|
25
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
26
|
+
import { getArgs } from '@/scripts/args';
|
|
27
|
+
import { GraphEvents } from '@/common';
|
|
28
|
+
import { Run } from '@/run';
|
|
29
|
+
import { createCodeExecutionTool } from '@/tools/CodeExecutor';
|
|
30
|
+
import { createProgrammaticToolCallingTool } from '@/tools/ProgrammaticToolCalling';
|
|
31
|
+
import {
|
|
32
|
+
createGetTeamMembersTool,
|
|
33
|
+
createGetExpensesTool,
|
|
34
|
+
createGetWeatherTool,
|
|
35
|
+
createProgrammaticToolRegistry,
|
|
36
|
+
} from '@/test/mockTools';
|
|
37
|
+
|
|
38
|
+
const conversationHistory: BaseMessage[] = [];
|
|
39
|
+
|
|
40
|
+
async function testProgrammaticToolCalling(): Promise<void> {
|
|
41
|
+
const { userName, location, provider, currentDate } = await getArgs();
|
|
42
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
43
|
+
|
|
44
|
+
const customHandlers = {
|
|
45
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
46
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
47
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
48
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
49
|
+
handle: (
|
|
50
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
51
|
+
data: t.StreamEventData
|
|
52
|
+
): void => {
|
|
53
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
54
|
+
console.dir(data, { depth: null });
|
|
55
|
+
aggregateContent({
|
|
56
|
+
event,
|
|
57
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
62
|
+
handle: (
|
|
63
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
64
|
+
data: t.StreamEventData
|
|
65
|
+
): void => {
|
|
66
|
+
console.log('====== ON_RUN_STEP ======');
|
|
67
|
+
console.dir(data, { depth: null });
|
|
68
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
72
|
+
handle: (
|
|
73
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
74
|
+
data: t.StreamEventData
|
|
75
|
+
): void => {
|
|
76
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
80
|
+
handle: (
|
|
81
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
82
|
+
data: t.StreamEventData
|
|
83
|
+
): void => {
|
|
84
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
[GraphEvents.TOOL_START]: {
|
|
88
|
+
handle: (
|
|
89
|
+
_event: string,
|
|
90
|
+
data: t.StreamEventData,
|
|
91
|
+
metadata?: Record<string, unknown>
|
|
92
|
+
): void => {
|
|
93
|
+
console.log('====== TOOL_START ======');
|
|
94
|
+
console.dir(data, { depth: null });
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const llmConfig = getLLMConfig(provider);
|
|
100
|
+
|
|
101
|
+
// Create mock tool instances
|
|
102
|
+
const teamTool = createGetTeamMembersTool();
|
|
103
|
+
const expensesTool = createGetExpensesTool();
|
|
104
|
+
const weatherTool = createGetWeatherTool();
|
|
105
|
+
|
|
106
|
+
// Create special tools
|
|
107
|
+
const codeExecTool = createCodeExecutionTool();
|
|
108
|
+
const ptcTool = createProgrammaticToolCallingTool();
|
|
109
|
+
|
|
110
|
+
// Build complete tool list and map
|
|
111
|
+
const allTools = [teamTool, expensesTool, weatherTool, codeExecTool, ptcTool];
|
|
112
|
+
const toolMap = new Map(allTools.map((t) => [t.name, t]));
|
|
113
|
+
|
|
114
|
+
// Create tool registry with allowed_callers configuration
|
|
115
|
+
// Only includes business logic tools (not special tools)
|
|
116
|
+
// Special tools (execute_code, PTC) are always bound directly to LLM
|
|
117
|
+
const toolRegistry = createProgrammaticToolRegistry();
|
|
118
|
+
|
|
119
|
+
console.log('\n' + '='.repeat(70));
|
|
120
|
+
console.log('Tool Configuration Summary:');
|
|
121
|
+
console.log('='.repeat(70));
|
|
122
|
+
console.log('Total tools:', allTools.length);
|
|
123
|
+
console.log(
|
|
124
|
+
'Programmatic-allowed:',
|
|
125
|
+
Array.from(toolRegistry.values())
|
|
126
|
+
.filter((t) => t.allowed_callers?.includes('code_execution'))
|
|
127
|
+
.map((t) => t.name)
|
|
128
|
+
.join(', ')
|
|
129
|
+
);
|
|
130
|
+
console.log(
|
|
131
|
+
'Direct-callable:',
|
|
132
|
+
Array.from(toolRegistry.values())
|
|
133
|
+
.filter((t) => !t.allowed_callers || t.allowed_callers.includes('direct'))
|
|
134
|
+
.map((t) => t.name)
|
|
135
|
+
.join(', ')
|
|
136
|
+
);
|
|
137
|
+
console.log('='.repeat(70) + '\n');
|
|
138
|
+
|
|
139
|
+
const run = await Run.create<t.IState>({
|
|
140
|
+
runId: randomUUID(),
|
|
141
|
+
graphConfig: {
|
|
142
|
+
type: 'standard',
|
|
143
|
+
llmConfig,
|
|
144
|
+
agents: [
|
|
145
|
+
{
|
|
146
|
+
agentId: 'default',
|
|
147
|
+
provider: llmConfig.provider,
|
|
148
|
+
clientOptions: llmConfig,
|
|
149
|
+
tools: allTools,
|
|
150
|
+
toolMap,
|
|
151
|
+
toolRegistry,
|
|
152
|
+
instructions:
|
|
153
|
+
'You are a friendly AI assistant with advanced coding capabilities. ' +
|
|
154
|
+
'You have access to team and expense management tools, but ONLY through programmatic code execution. ' +
|
|
155
|
+
'When you need to analyze expenses or process team data, use the programmatic_code_execution tool ' +
|
|
156
|
+
'to write Python code that calls get_team_members(), get_expenses(), and get_weather() functions. ' +
|
|
157
|
+
'These functions are async - use await. Use asyncio.gather() for parallel execution.',
|
|
158
|
+
additional_instructions: `The user's name is ${userName} and they are located in ${location}. Today is ${currentDate}.`,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
},
|
|
162
|
+
returnContent: true,
|
|
163
|
+
customHandlers,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const config: Partial<RunnableConfig> & {
|
|
167
|
+
version: 'v1' | 'v2';
|
|
168
|
+
run_id?: string;
|
|
169
|
+
streamMode: string;
|
|
170
|
+
} = {
|
|
171
|
+
configurable: {
|
|
172
|
+
provider,
|
|
173
|
+
thread_id: 'ptc-conversation-1',
|
|
174
|
+
},
|
|
175
|
+
streamMode: 'values',
|
|
176
|
+
version: 'v2' as const,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
console.log('Test 1: Team Expense Analysis with PTC');
|
|
180
|
+
console.log('='.repeat(70) + '\n');
|
|
181
|
+
|
|
182
|
+
const userMessage1 = `Hi ${userName}! I need you to analyze our team's expenses. Please:
|
|
183
|
+
|
|
184
|
+
1. Get the list of all team members
|
|
185
|
+
2. For each member, fetch their expense records
|
|
186
|
+
3. Calculate the total expenses per member
|
|
187
|
+
4. Identify anyone who spent more than $500
|
|
188
|
+
5. Show me a summary report
|
|
189
|
+
|
|
190
|
+
IMPORTANT: Use the programmatic_code_execution tool to do this efficiently.
|
|
191
|
+
Don't call each tool separately - write Python code that orchestrates all the calls!`;
|
|
192
|
+
|
|
193
|
+
conversationHistory.push(new HumanMessage(userMessage1));
|
|
194
|
+
|
|
195
|
+
let inputs = {
|
|
196
|
+
messages: conversationHistory,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const finalContentParts1 = await run.processStream(inputs, config);
|
|
200
|
+
const finalMessages1 = run.getRunMessages();
|
|
201
|
+
if (finalMessages1) {
|
|
202
|
+
conversationHistory.push(...finalMessages1);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
console.log('\n\n====================\n\n');
|
|
206
|
+
console.log('Content Parts:');
|
|
207
|
+
console.dir(contentParts, { depth: null });
|
|
208
|
+
|
|
209
|
+
console.log('\n\n' + '='.repeat(70));
|
|
210
|
+
console.log('Test 2: Conditional Logic and Parallel Execution');
|
|
211
|
+
console.log('='.repeat(70) + '\n');
|
|
212
|
+
|
|
213
|
+
const userMessage2 = `Great job! Now let's test some advanced patterns. Please:
|
|
214
|
+
|
|
215
|
+
1. Check the weather in both San Francisco and New York (in parallel!)
|
|
216
|
+
2. Based on which city has better weather (warmer), fetch the team members
|
|
217
|
+
3. For the Engineering team members only, calculate their travel expenses
|
|
218
|
+
4. Show me the results
|
|
219
|
+
|
|
220
|
+
Again, use programmatic_code_execution for maximum efficiency. Use asyncio.gather()
|
|
221
|
+
to check both cities' weather at the same time!`;
|
|
222
|
+
|
|
223
|
+
conversationHistory.push(new HumanMessage(userMessage2));
|
|
224
|
+
|
|
225
|
+
inputs = {
|
|
226
|
+
messages: conversationHistory,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const finalContentParts2 = await run.processStream(inputs, config);
|
|
230
|
+
const finalMessages2 = run.getRunMessages();
|
|
231
|
+
if (finalMessages2) {
|
|
232
|
+
conversationHistory.push(...finalMessages2);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
console.log('\n\n====================\n\n');
|
|
236
|
+
console.log('Final Content Parts:');
|
|
237
|
+
console.dir(finalContentParts2, { depth: null });
|
|
238
|
+
|
|
239
|
+
console.log('\n\n' + '='.repeat(70));
|
|
240
|
+
console.log('Generating conversation title...');
|
|
241
|
+
console.log('='.repeat(70) + '\n');
|
|
242
|
+
|
|
243
|
+
const { handleLLMEnd, collected } = createMetadataAggregator();
|
|
244
|
+
const titleResult = await run.generateTitle({
|
|
245
|
+
provider,
|
|
246
|
+
inputText: userMessage1,
|
|
247
|
+
contentParts,
|
|
248
|
+
chainOptions: {
|
|
249
|
+
callbacks: [
|
|
250
|
+
{
|
|
251
|
+
handleLLMEnd,
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
console.log('Generated Title:', titleResult);
|
|
258
|
+
console.log('Collected metadata:', collected);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
262
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
263
|
+
console.log('Conversation history:');
|
|
264
|
+
console.dir(conversationHistory, { depth: null });
|
|
265
|
+
process.exit(1);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
process.on('uncaughtException', (err) => {
|
|
269
|
+
console.error('Uncaught Exception:', err);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
testProgrammaticToolCalling().catch((err) => {
|
|
273
|
+
console.error(err);
|
|
274
|
+
console.log('Conversation history:');
|
|
275
|
+
console.dir(conversationHistory, { depth: null });
|
|
276
|
+
process.exit(1);
|
|
277
|
+
});
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// src/scripts/programmatic_exec.ts
|
|
2
|
+
/**
|
|
3
|
+
* Test script for Programmatic Tool Calling (PTC).
|
|
4
|
+
* Run with: npm run programmatic_exec
|
|
5
|
+
*
|
|
6
|
+
* Demonstrates:
|
|
7
|
+
* 1. Runtime toolMap injection - the tool map is passed at invocation time
|
|
8
|
+
* 2. Tool classification with allowed_callers (inspired by Anthropic's API)
|
|
9
|
+
* - 'direct': Tool can only be called directly by the LLM (default)
|
|
10
|
+
* - 'code_execution': Tool can only be called from within PTC
|
|
11
|
+
* - Both: Tool can be called either way
|
|
12
|
+
*
|
|
13
|
+
* IMPORTANT: The Python code passed to PTC should NOT define the tool functions.
|
|
14
|
+
* The Code API automatically generates async function stubs from the tool definitions.
|
|
15
|
+
* The code should just CALL the tools as if they're already available:
|
|
16
|
+
* - result = await get_weather(city="SF")
|
|
17
|
+
* - results = await asyncio.gather(tool1(), tool2())
|
|
18
|
+
*/
|
|
19
|
+
import { config } from 'dotenv';
|
|
20
|
+
config();
|
|
21
|
+
|
|
22
|
+
import type { StructuredToolInterface } from '@langchain/core/tools';
|
|
23
|
+
import type { LCTool, ToolMap } from '@/types';
|
|
24
|
+
import { createProgrammaticToolCallingTool } from '@/tools/ProgrammaticToolCalling';
|
|
25
|
+
import {
|
|
26
|
+
createGetTeamMembersTool,
|
|
27
|
+
createGetExpensesTool,
|
|
28
|
+
createGetWeatherTool,
|
|
29
|
+
createCalculatorTool,
|
|
30
|
+
createProgrammaticToolRegistry,
|
|
31
|
+
} from '@/test/mockTools';
|
|
32
|
+
|
|
33
|
+
// ============================================================================
|
|
34
|
+
// Test Runner
|
|
35
|
+
// ============================================================================
|
|
36
|
+
|
|
37
|
+
interface RunTestOptions {
|
|
38
|
+
toolMap: ToolMap;
|
|
39
|
+
tools?: LCTool[];
|
|
40
|
+
session_id?: string;
|
|
41
|
+
timeout?: number;
|
|
42
|
+
showArtifact?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function runTest(
|
|
46
|
+
ptcTool: ReturnType<typeof createProgrammaticToolCallingTool>,
|
|
47
|
+
testName: string,
|
|
48
|
+
code: string,
|
|
49
|
+
options: RunTestOptions
|
|
50
|
+
): Promise<void> {
|
|
51
|
+
console.log(`\n${'='.repeat(70)}`);
|
|
52
|
+
console.log(`TEST: ${testName}`);
|
|
53
|
+
console.log('='.repeat(70));
|
|
54
|
+
console.log('\nCode:');
|
|
55
|
+
console.log('```python');
|
|
56
|
+
console.log(code.trim());
|
|
57
|
+
console.log('```\n');
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const startTime = Date.now();
|
|
61
|
+
|
|
62
|
+
// Manual testing: schema params + extras (LangChain moves extras to config.toolCall)
|
|
63
|
+
const result = await ptcTool.invoke({
|
|
64
|
+
code,
|
|
65
|
+
tools: options.tools,
|
|
66
|
+
session_id: options.session_id,
|
|
67
|
+
timeout: options.timeout,
|
|
68
|
+
toolMap: options.toolMap, // Non-schema field → config.toolCall.toolMap
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const duration = Date.now() - startTime;
|
|
72
|
+
|
|
73
|
+
console.log(`Result (${duration}ms):`);
|
|
74
|
+
if (Array.isArray(result)) {
|
|
75
|
+
console.log(result[0]);
|
|
76
|
+
if (options.showArtifact) {
|
|
77
|
+
console.log('\n--- Artifact ---');
|
|
78
|
+
console.dir(result[1], { depth: null });
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
console.log(result);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error('Error:', error instanceof Error ? error.message : error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ============================================================================
|
|
89
|
+
// Main
|
|
90
|
+
// ============================================================================
|
|
91
|
+
|
|
92
|
+
async function main(): Promise<void> {
|
|
93
|
+
console.log('Programmatic Tool Calling (PTC) - Test Script');
|
|
94
|
+
console.log('==============================================');
|
|
95
|
+
console.log('Demonstrating runtime toolMap injection\n');
|
|
96
|
+
|
|
97
|
+
const apiKey = process.env.LIBRECHAT_CODE_API_KEY;
|
|
98
|
+
if (!apiKey) {
|
|
99
|
+
console.error(
|
|
100
|
+
'Error: LIBRECHAT_CODE_API_KEY environment variable is not set.'
|
|
101
|
+
);
|
|
102
|
+
console.log('Please set it in your .env file or environment.');
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
console.log('Creating mock tools...');
|
|
107
|
+
const mockTools: StructuredToolInterface[] = [
|
|
108
|
+
createGetTeamMembersTool(),
|
|
109
|
+
createGetExpensesTool(),
|
|
110
|
+
createGetWeatherTool(),
|
|
111
|
+
createCalculatorTool(),
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
const toolMap: ToolMap = new Map(mockTools.map((t) => [t.name, t]));
|
|
115
|
+
const toolDefinitions = Array.from(createProgrammaticToolRegistry().values());
|
|
116
|
+
|
|
117
|
+
console.log(
|
|
118
|
+
`ToolMap contains ${toolMap.size} tools: ${Array.from(toolMap.keys()).join(', ')}`
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
console.log('\nCreating PTC tool (without toolMap)...');
|
|
122
|
+
const ptcTool = createProgrammaticToolCallingTool({ apiKey });
|
|
123
|
+
console.log('PTC tool created successfully!');
|
|
124
|
+
console.log(
|
|
125
|
+
'Note: toolMap will be passed at runtime with each invocation.\n'
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const baseOptions = { toolMap, tools: toolDefinitions };
|
|
129
|
+
|
|
130
|
+
// =========================================================================
|
|
131
|
+
// Test 1: Simple async tool call
|
|
132
|
+
// =========================================================================
|
|
133
|
+
await runTest(
|
|
134
|
+
ptcTool,
|
|
135
|
+
'Simple async tool call',
|
|
136
|
+
`
|
|
137
|
+
# Tools are auto-generated as async functions - just await them
|
|
138
|
+
result = await get_weather(city="San Francisco")
|
|
139
|
+
print(f"Weather in SF: {result['temperature']}°F, {result['condition']}")
|
|
140
|
+
`,
|
|
141
|
+
{ ...baseOptions, showArtifact: true }
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// =========================================================================
|
|
145
|
+
// Test 2: Sequential loop with await
|
|
146
|
+
// =========================================================================
|
|
147
|
+
await runTest(
|
|
148
|
+
ptcTool,
|
|
149
|
+
'Sequential loop - Process team expenses',
|
|
150
|
+
`
|
|
151
|
+
# Each tool call uses await
|
|
152
|
+
team = await get_team_members()
|
|
153
|
+
print("Team expense report:")
|
|
154
|
+
print("-" * 30)
|
|
155
|
+
total = 0
|
|
156
|
+
for member in team:
|
|
157
|
+
expenses = await get_expenses(user_id=member['id'])
|
|
158
|
+
member_total = sum(e['amount'] for e in expenses)
|
|
159
|
+
total += member_total
|
|
160
|
+
print(f"{member['name']}: \${member_total:.2f}")
|
|
161
|
+
print("-" * 30)
|
|
162
|
+
print(f"Total: \${total:.2f}")
|
|
163
|
+
`,
|
|
164
|
+
baseOptions
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// =========================================================================
|
|
168
|
+
// Test 3: Parallel execution with asyncio.gather
|
|
169
|
+
// =========================================================================
|
|
170
|
+
await runTest(
|
|
171
|
+
ptcTool,
|
|
172
|
+
'Parallel execution - Weather for multiple cities',
|
|
173
|
+
`
|
|
174
|
+
# Use asyncio.gather for parallel tool calls - single round-trip!
|
|
175
|
+
import asyncio
|
|
176
|
+
|
|
177
|
+
cities = ["San Francisco", "New York", "London"]
|
|
178
|
+
results = await asyncio.gather(*[
|
|
179
|
+
get_weather(city=city)
|
|
180
|
+
for city in cities
|
|
181
|
+
])
|
|
182
|
+
|
|
183
|
+
print("Weather report:")
|
|
184
|
+
for city, weather in zip(cities, results):
|
|
185
|
+
print(f" {city}: {weather['temperature']}°F, {weather['condition']}")
|
|
186
|
+
`,
|
|
187
|
+
baseOptions
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
// =========================================================================
|
|
191
|
+
// Test 4: Chained dependencies
|
|
192
|
+
// =========================================================================
|
|
193
|
+
await runTest(
|
|
194
|
+
ptcTool,
|
|
195
|
+
'Chained dependencies - Get team then process each',
|
|
196
|
+
`
|
|
197
|
+
# Get team first, then fetch expenses for each
|
|
198
|
+
team = await get_team_members()
|
|
199
|
+
engineering = [m for m in team if m['department'] == 'Engineering']
|
|
200
|
+
|
|
201
|
+
print(f"Engineering team ({len(engineering)} members):")
|
|
202
|
+
for member in engineering:
|
|
203
|
+
expenses = await get_expenses(user_id=member['id'])
|
|
204
|
+
equipment = sum(e['amount'] for e in expenses if e['category'] == 'equipment')
|
|
205
|
+
print(f" {member['name']}: \${equipment:.2f} on equipment")
|
|
206
|
+
`,
|
|
207
|
+
baseOptions
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// =========================================================================
|
|
211
|
+
// Test 5: Conditional logic
|
|
212
|
+
// =========================================================================
|
|
213
|
+
await runTest(
|
|
214
|
+
ptcTool,
|
|
215
|
+
'Conditional logic - Find high spenders',
|
|
216
|
+
`
|
|
217
|
+
team = await get_team_members()
|
|
218
|
+
high_spenders = []
|
|
219
|
+
|
|
220
|
+
for member in team:
|
|
221
|
+
expenses = await get_expenses(user_id=member['id'])
|
|
222
|
+
total = sum(e['amount'] for e in expenses)
|
|
223
|
+
if total > 300:
|
|
224
|
+
high_spenders.append((member['name'], total))
|
|
225
|
+
|
|
226
|
+
if high_spenders:
|
|
227
|
+
print("High spenders (over $300):")
|
|
228
|
+
for name, amount in sorted(high_spenders, key=lambda x: x[1], reverse=True):
|
|
229
|
+
print(f" {name}: \${amount:.2f}")
|
|
230
|
+
else:
|
|
231
|
+
print("No high spenders found.")
|
|
232
|
+
`,
|
|
233
|
+
baseOptions
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// =========================================================================
|
|
237
|
+
// Test 6: Mixed parallel and sequential
|
|
238
|
+
// =========================================================================
|
|
239
|
+
await runTest(
|
|
240
|
+
ptcTool,
|
|
241
|
+
'Mixed - Parallel expense fetch after sequential team fetch',
|
|
242
|
+
`
|
|
243
|
+
import asyncio
|
|
244
|
+
|
|
245
|
+
# Step 1: Get team (one tool call)
|
|
246
|
+
team = await get_team_members()
|
|
247
|
+
print(f"Fetched {len(team)} team members")
|
|
248
|
+
|
|
249
|
+
# Step 2: Get all expenses in parallel (single round-trip for all!)
|
|
250
|
+
all_expenses = await asyncio.gather(*[
|
|
251
|
+
get_expenses(user_id=member['id'])
|
|
252
|
+
for member in team
|
|
253
|
+
])
|
|
254
|
+
|
|
255
|
+
# Step 3: Process and output
|
|
256
|
+
print("\\nExpense summary:")
|
|
257
|
+
for member, expenses in zip(team, all_expenses):
|
|
258
|
+
total = sum(e['amount'] for e in expenses)
|
|
259
|
+
print(f" {member['name']}: \${total:.2f} ({len(expenses)} items)")
|
|
260
|
+
`,
|
|
261
|
+
baseOptions
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
// =========================================================================
|
|
265
|
+
// Test 7: Calculator usage
|
|
266
|
+
// =========================================================================
|
|
267
|
+
await runTest(
|
|
268
|
+
ptcTool,
|
|
269
|
+
'Calculator tool usage',
|
|
270
|
+
`
|
|
271
|
+
# All tools are async - use await
|
|
272
|
+
result1 = await calculator(expression="2 + 2 * 3")
|
|
273
|
+
result2 = await calculator(expression="(10 + 5) / 3")
|
|
274
|
+
|
|
275
|
+
print(f"2 + 2 * 3 = {result1['result']}")
|
|
276
|
+
print(f"(10 + 5) / 3 = {result2['result']:.2f}")
|
|
277
|
+
`,
|
|
278
|
+
baseOptions
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
// =========================================================================
|
|
282
|
+
// Test 8: Error handling in code
|
|
283
|
+
// =========================================================================
|
|
284
|
+
await runTest(
|
|
285
|
+
ptcTool,
|
|
286
|
+
'Error handling - Invalid city',
|
|
287
|
+
`
|
|
288
|
+
# Tool errors become Python exceptions - handle with try/except
|
|
289
|
+
cities = ["San Francisco", "InvalidCity", "New York"]
|
|
290
|
+
|
|
291
|
+
for city in cities:
|
|
292
|
+
try:
|
|
293
|
+
weather = await get_weather(city=city)
|
|
294
|
+
print(f"{city}: {weather['temperature']}°F")
|
|
295
|
+
except Exception as e:
|
|
296
|
+
print(f"{city}: Error - {e}")
|
|
297
|
+
`,
|
|
298
|
+
baseOptions
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
// =========================================================================
|
|
302
|
+
// Test 9: Early termination
|
|
303
|
+
// =========================================================================
|
|
304
|
+
await runTest(
|
|
305
|
+
ptcTool,
|
|
306
|
+
'Early termination - Stop when condition met',
|
|
307
|
+
`
|
|
308
|
+
# Stop as soon as we find what we need - no wasted tool calls
|
|
309
|
+
team = await get_team_members()
|
|
310
|
+
|
|
311
|
+
for member in team:
|
|
312
|
+
expenses = await get_expenses(user_id=member['id'])
|
|
313
|
+
if any(e['category'] == 'equipment' for e in expenses):
|
|
314
|
+
print(f"First team member with equipment expense: {member['name']}")
|
|
315
|
+
equipment_total = sum(e['amount'] for e in expenses if e['category'] == 'equipment')
|
|
316
|
+
print(f"Equipment total: \${equipment_total:.2f}")
|
|
317
|
+
break
|
|
318
|
+
else:
|
|
319
|
+
print("No team member has equipment expenses")
|
|
320
|
+
`,
|
|
321
|
+
baseOptions
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
// =========================================================================
|
|
325
|
+
// Test 10: Subset of tools
|
|
326
|
+
// =========================================================================
|
|
327
|
+
await runTest(
|
|
328
|
+
ptcTool,
|
|
329
|
+
'Subset of tools - Only weather',
|
|
330
|
+
`
|
|
331
|
+
# Only the weather tool is available in this execution
|
|
332
|
+
import asyncio
|
|
333
|
+
|
|
334
|
+
sf, nyc = await asyncio.gather(
|
|
335
|
+
get_weather(city="San Francisco"),
|
|
336
|
+
get_weather(city="New York")
|
|
337
|
+
)
|
|
338
|
+
print(f"SF: {sf['temperature']}°F vs NYC: {nyc['temperature']}°F")
|
|
339
|
+
difference = abs(sf['temperature'] - nyc['temperature'])
|
|
340
|
+
print(f"Temperature difference: {difference}°F")
|
|
341
|
+
`,
|
|
342
|
+
{
|
|
343
|
+
...baseOptions,
|
|
344
|
+
tools: [toolDefinitions.find((t) => t.name === 'get_weather')!],
|
|
345
|
+
}
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
// =========================================================================
|
|
349
|
+
// Test 11: Note about ToolNode injection
|
|
350
|
+
// =========================================================================
|
|
351
|
+
console.log(`\n${'='.repeat(70)}`);
|
|
352
|
+
console.log('NOTE: ToolNode Runtime Injection');
|
|
353
|
+
console.log('='.repeat(70));
|
|
354
|
+
console.log(
|
|
355
|
+
'\nWhen PTC is invoked through ToolNode in a real agent:\n' +
|
|
356
|
+
'- ToolNode detects call.name === "programmatic_code_execution"\n' +
|
|
357
|
+
'- ToolNode injects: { ...invokeParams, toolMap, programmaticToolDefs }\n' +
|
|
358
|
+
'- PTC tool extracts these from params (not from config)\n' +
|
|
359
|
+
'- No explicit tools parameter needed in schema\n\n' +
|
|
360
|
+
'This test demonstrates param injection with explicit tools:\n'
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
await runTest(
|
|
364
|
+
ptcTool,
|
|
365
|
+
'Runtime injection with explicit tools',
|
|
366
|
+
`
|
|
367
|
+
# ToolNode would inject toolMap+programmaticToolDefs
|
|
368
|
+
# For this test, we pass tools explicitly (same effect)
|
|
369
|
+
team = await get_team_members()
|
|
370
|
+
print(f"Team size: {len(team)}")
|
|
371
|
+
for member in team:
|
|
372
|
+
print(f"- {member['name']} ({member['department']})")
|
|
373
|
+
`,
|
|
374
|
+
baseOptions
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
console.log('\n' + '='.repeat(70));
|
|
378
|
+
console.log('All tests completed!');
|
|
379
|
+
console.log('='.repeat(70) + '\n');
|
|
380
|
+
console.log('Summary of allowed_callers patterns:');
|
|
381
|
+
console.log(
|
|
382
|
+
'- get_team_members, get_expenses, calculator: code_execution only'
|
|
383
|
+
);
|
|
384
|
+
console.log('- get_weather: both direct and code_execution');
|
|
385
|
+
console.log(
|
|
386
|
+
'\nIn a real agent setup, the LLM would only see tools with allowed_callers'
|
|
387
|
+
);
|
|
388
|
+
console.log(
|
|
389
|
+
'including "direct", while PTC can call any tool with "code_execution".\n'
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
main().catch((err) => {
|
|
394
|
+
console.error('Fatal error:', err);
|
|
395
|
+
process.exit(1);
|
|
396
|
+
});
|