illuma-agents 1.0.9 → 1.0.11

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.
Files changed (146) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/agents/AgentContext.cjs +228 -27
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  4. package/dist/cjs/common/enum.cjs +2 -0
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/events.cjs +3 -0
  7. package/dist/cjs/events.cjs.map +1 -1
  8. package/dist/cjs/graphs/Graph.cjs +29 -19
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  10. package/dist/cjs/instrumentation.cjs +1 -1
  11. package/dist/cjs/instrumentation.cjs.map +1 -1
  12. package/dist/cjs/llm/anthropic/index.cjs +1 -1
  13. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  14. package/dist/cjs/llm/bedrock/index.cjs +122 -7
  15. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  16. package/dist/cjs/llm/google/index.cjs +1 -1
  17. package/dist/cjs/llm/google/index.cjs.map +1 -1
  18. package/dist/cjs/llm/openai/index.cjs +108 -6
  19. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  20. package/dist/cjs/llm/openai/utils/index.cjs +87 -1
  21. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  22. package/dist/cjs/llm/openrouter/index.cjs +176 -2
  23. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  24. package/dist/cjs/main.cjs +18 -0
  25. package/dist/cjs/main.cjs.map +1 -1
  26. package/dist/cjs/messages/cache.cjs +149 -54
  27. package/dist/cjs/messages/cache.cjs.map +1 -1
  28. package/dist/cjs/messages/tools.cjs +85 -0
  29. package/dist/cjs/messages/tools.cjs.map +1 -0
  30. package/dist/cjs/stream.cjs +20 -0
  31. package/dist/cjs/stream.cjs.map +1 -1
  32. package/dist/cjs/tools/CodeExecutor.cjs +4 -0
  33. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  34. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +438 -0
  35. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
  36. package/dist/cjs/tools/ToolNode.cjs +54 -6
  37. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  38. package/dist/cjs/tools/ToolSearchRegex.cjs +455 -0
  39. package/dist/cjs/tools/ToolSearchRegex.cjs.map +1 -0
  40. package/dist/cjs/tools/search/tool.cjs +21 -1
  41. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  42. package/dist/cjs/utils/run.cjs +5 -1
  43. package/dist/cjs/utils/run.cjs.map +1 -1
  44. package/dist/esm/agents/AgentContext.mjs +228 -27
  45. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  46. package/dist/esm/common/enum.mjs +2 -0
  47. package/dist/esm/common/enum.mjs.map +1 -1
  48. package/dist/esm/events.mjs +4 -1
  49. package/dist/esm/events.mjs.map +1 -1
  50. package/dist/esm/graphs/Graph.mjs +29 -19
  51. package/dist/esm/graphs/Graph.mjs.map +1 -1
  52. package/dist/esm/instrumentation.mjs +1 -1
  53. package/dist/esm/instrumentation.mjs.map +1 -1
  54. package/dist/esm/llm/anthropic/index.mjs +1 -1
  55. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  56. package/dist/esm/llm/bedrock/index.mjs +122 -7
  57. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  58. package/dist/esm/llm/google/index.mjs +1 -1
  59. package/dist/esm/llm/google/index.mjs.map +1 -1
  60. package/dist/esm/llm/openai/index.mjs +109 -7
  61. package/dist/esm/llm/openai/index.mjs.map +1 -1
  62. package/dist/esm/llm/openai/utils/index.mjs +88 -2
  63. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  64. package/dist/esm/llm/openrouter/index.mjs +176 -2
  65. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  66. package/dist/esm/main.mjs +3 -0
  67. package/dist/esm/main.mjs.map +1 -1
  68. package/dist/esm/messages/cache.mjs +149 -54
  69. package/dist/esm/messages/cache.mjs.map +1 -1
  70. package/dist/esm/messages/tools.mjs +82 -0
  71. package/dist/esm/messages/tools.mjs.map +1 -0
  72. package/dist/esm/stream.mjs +20 -0
  73. package/dist/esm/stream.mjs.map +1 -1
  74. package/dist/esm/tools/CodeExecutor.mjs +4 -0
  75. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  76. package/dist/esm/tools/ProgrammaticToolCalling.mjs +430 -0
  77. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
  78. package/dist/esm/tools/ToolNode.mjs +54 -6
  79. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  80. package/dist/esm/tools/ToolSearchRegex.mjs +448 -0
  81. package/dist/esm/tools/ToolSearchRegex.mjs.map +1 -0
  82. package/dist/esm/tools/search/tool.mjs +21 -1
  83. package/dist/esm/tools/search/tool.mjs.map +1 -1
  84. package/dist/esm/utils/run.mjs +5 -1
  85. package/dist/esm/utils/run.mjs.map +1 -1
  86. package/dist/types/agents/AgentContext.d.ts +65 -5
  87. package/dist/types/common/enum.d.ts +2 -0
  88. package/dist/types/graphs/Graph.d.ts +3 -2
  89. package/dist/types/index.d.ts +2 -0
  90. package/dist/types/llm/anthropic/index.d.ts +1 -1
  91. package/dist/types/llm/bedrock/index.d.ts +31 -4
  92. package/dist/types/llm/google/index.d.ts +1 -1
  93. package/dist/types/llm/openai/index.d.ts +4 -3
  94. package/dist/types/llm/openai/utils/index.d.ts +10 -1
  95. package/dist/types/llm/openrouter/index.d.ts +5 -2
  96. package/dist/types/messages/cache.d.ts +23 -8
  97. package/dist/types/messages/index.d.ts +1 -0
  98. package/dist/types/messages/tools.d.ts +17 -0
  99. package/dist/types/test/mockTools.d.ts +28 -0
  100. package/dist/types/tools/ProgrammaticToolCalling.d.ts +91 -0
  101. package/dist/types/tools/ToolNode.d.ts +10 -2
  102. package/dist/types/tools/ToolSearchRegex.d.ts +80 -0
  103. package/dist/types/types/graph.d.ts +7 -1
  104. package/dist/types/types/tools.d.ts +138 -0
  105. package/package.json +8 -3
  106. package/src/agents/AgentContext.ts +267 -27
  107. package/src/agents/__tests__/AgentContext.test.ts +805 -0
  108. package/src/common/enum.ts +2 -0
  109. package/src/events.ts +5 -1
  110. package/src/graphs/Graph.ts +35 -20
  111. package/src/index.ts +2 -0
  112. package/src/instrumentation.ts +1 -1
  113. package/src/llm/anthropic/index.ts +2 -2
  114. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +473 -0
  115. package/src/llm/bedrock/index.ts +150 -13
  116. package/src/llm/google/index.ts +2 -2
  117. package/src/llm/google/llm.spec.ts +3 -1
  118. package/src/llm/openai/index.ts +135 -9
  119. package/src/llm/openai/utils/index.ts +116 -1
  120. package/src/llm/openrouter/index.ts +224 -3
  121. package/src/messages/__tests__/tools.test.ts +473 -0
  122. package/src/messages/cache.ts +163 -61
  123. package/src/messages/index.ts +1 -0
  124. package/src/messages/tools.ts +99 -0
  125. package/src/scripts/code_exec_ptc.ts +334 -0
  126. package/src/scripts/programmatic_exec.ts +396 -0
  127. package/src/scripts/programmatic_exec_agent.ts +231 -0
  128. package/src/scripts/tool_search_regex.ts +162 -0
  129. package/src/specs/thinking-prune.test.ts +52 -118
  130. package/src/stream.ts +26 -0
  131. package/src/test/mockTools.ts +366 -0
  132. package/src/tools/CodeExecutor.ts +4 -0
  133. package/src/tools/ProgrammaticToolCalling.ts +558 -0
  134. package/src/tools/ToolNode.ts +60 -7
  135. package/src/tools/ToolSearchRegex.ts +535 -0
  136. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +318 -0
  137. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +853 -0
  138. package/src/tools/__tests__/ToolSearchRegex.integration.test.ts +161 -0
  139. package/src/tools/__tests__/ToolSearchRegex.test.ts +232 -0
  140. package/src/tools/search/jina-reranker.test.ts +16 -16
  141. package/src/tools/search/tool.ts +23 -1
  142. package/src/types/graph.ts +7 -1
  143. package/src/types/tools.ts +166 -0
  144. package/src/utils/llmConfig.ts +8 -2
  145. package/src/utils/run.ts +5 -1
  146. package/src/tools/search/direct-url.test.ts +0 -530
@@ -0,0 +1,334 @@
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
+ // createProgrammaticToolRegistry,
22
+ createGetTeamMembersTool,
23
+ createGetExpensesTool,
24
+ createGetWeatherTool,
25
+ } from '@/test/mockTools';
26
+ import {
27
+ createMetadataAggregator,
28
+ ModelEndHandler,
29
+ ToolEndHandler,
30
+ } from '@/events';
31
+ import { createProgrammaticToolCallingTool } from '@/tools/ProgrammaticToolCalling';
32
+ import { createCodeExecutionTool } from '@/tools/CodeExecutor';
33
+ import { getLLMConfig } from '@/utils/llmConfig';
34
+ import { getArgs } from '@/scripts/args';
35
+ import { GraphEvents } from '@/common';
36
+ import { Run } from '@/run';
37
+
38
+ const conversationHistory: BaseMessage[] = [];
39
+
40
+ /**
41
+ * Creates a tool registry where ALL business tools are code_execution ONLY.
42
+ * This forces the LLM to use PTC - it cannot call these tools directly.
43
+ */
44
+ function createPTCOnlyToolRegistry(): t.LCToolRegistry {
45
+ const toolDefs: t.LCTool[] = [
46
+ {
47
+ name: 'get_team_members',
48
+ description:
49
+ 'Get list of team members. Returns array of objects with id, name, and department fields.',
50
+ parameters: {
51
+ type: 'object',
52
+ properties: {},
53
+ required: [],
54
+ },
55
+ allowed_callers: ['code_execution'], // PTC ONLY - not direct
56
+ },
57
+ {
58
+ name: 'get_expenses',
59
+ description:
60
+ 'Get expense records for a user. Returns array of objects with amount and category fields.',
61
+ parameters: {
62
+ type: 'object',
63
+ properties: {
64
+ user_id: {
65
+ type: 'string',
66
+ description: 'The user ID to fetch expenses for',
67
+ },
68
+ },
69
+ required: ['user_id'],
70
+ },
71
+ allowed_callers: ['code_execution'], // PTC ONLY - not direct
72
+ },
73
+ {
74
+ name: 'get_weather',
75
+ description:
76
+ 'Get current weather for a city. Returns object with temperature (number) and condition (string) fields.',
77
+ parameters: {
78
+ type: 'object',
79
+ properties: {
80
+ city: {
81
+ type: 'string',
82
+ description: 'City name',
83
+ },
84
+ },
85
+ required: ['city'],
86
+ },
87
+ allowed_callers: ['code_execution'], // PTC ONLY - not direct (changed from ['direct', 'code_execution'])
88
+ },
89
+ ];
90
+
91
+ return new Map(toolDefs.map((def) => [def.name, def]));
92
+ }
93
+
94
+ async function testProgrammaticToolCalling(): Promise<void> {
95
+ const { userName, location, provider, currentDate } = await getArgs();
96
+ const { contentParts, aggregateContent } = createContentAggregator();
97
+
98
+ const customHandlers = {
99
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
100
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
101
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
102
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
103
+ handle: (
104
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
105
+ data: t.StreamEventData
106
+ ): void => {
107
+ console.log('====== ON_RUN_STEP_COMPLETED ======');
108
+ console.dir(data, { depth: null });
109
+ aggregateContent({
110
+ event,
111
+ data: data as unknown as { result: t.ToolEndEvent },
112
+ });
113
+ },
114
+ },
115
+ [GraphEvents.ON_RUN_STEP]: {
116
+ handle: (
117
+ event: GraphEvents.ON_RUN_STEP,
118
+ data: t.StreamEventData
119
+ ): void => {
120
+ console.log('====== ON_RUN_STEP ======');
121
+ console.dir(data, { depth: null });
122
+ aggregateContent({ event, data: data as t.RunStep });
123
+ },
124
+ },
125
+ [GraphEvents.ON_RUN_STEP_DELTA]: {
126
+ handle: (
127
+ event: GraphEvents.ON_RUN_STEP_DELTA,
128
+ data: t.StreamEventData
129
+ ): void => {
130
+ aggregateContent({ event, data: data as t.RunStepDeltaEvent });
131
+ },
132
+ },
133
+ [GraphEvents.ON_MESSAGE_DELTA]: {
134
+ handle: (
135
+ event: GraphEvents.ON_MESSAGE_DELTA,
136
+ data: t.StreamEventData
137
+ ): void => {
138
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
139
+ },
140
+ },
141
+ [GraphEvents.TOOL_START]: {
142
+ handle: (
143
+ _event: string,
144
+ data: t.StreamEventData,
145
+ metadata?: Record<string, unknown>
146
+ ): void => {
147
+ console.log('====== TOOL_START ======');
148
+ console.dir(data, { depth: null });
149
+ },
150
+ },
151
+ };
152
+
153
+ const llmConfig = getLLMConfig(provider);
154
+
155
+ // Create mock tool instances
156
+ const teamTool = createGetTeamMembersTool();
157
+ const expensesTool = createGetExpensesTool();
158
+ const weatherTool = createGetWeatherTool();
159
+
160
+ // Create special tools
161
+ const codeExecTool = createCodeExecutionTool();
162
+ const ptcTool = createProgrammaticToolCallingTool();
163
+
164
+ // Build complete tool list and map
165
+ const allTools = [teamTool, expensesTool, weatherTool, codeExecTool, ptcTool];
166
+ const toolMap = new Map(allTools.map((t) => [t.name, t]));
167
+
168
+ // Create tool registry where ALL business tools are PTC-only
169
+ // This means the LLM CANNOT call get_team_members, get_expenses, get_weather directly
170
+ // It MUST use run_tools_with_code to invoke them
171
+ const toolRegistry = createPTCOnlyToolRegistry();
172
+
173
+ console.log('\n' + '='.repeat(70));
174
+ console.log('Tool Configuration Summary:');
175
+ console.log('='.repeat(70));
176
+ console.log('Total tools:', allTools.length);
177
+ console.log(
178
+ 'Programmatic-allowed:',
179
+ Array.from(toolRegistry.values())
180
+ .filter((t) => t.allowed_callers?.includes('code_execution'))
181
+ .map((t) => t.name)
182
+ .join(', ')
183
+ );
184
+ console.log(
185
+ 'Direct-callable:',
186
+ Array.from(toolRegistry.values())
187
+ .filter((t) => !t.allowed_callers || t.allowed_callers.includes('direct'))
188
+ .map((t) => t.name)
189
+ .join(', ')
190
+ );
191
+ console.log('='.repeat(70) + '\n');
192
+
193
+ const run = await Run.create<t.IState>({
194
+ runId: randomUUID(),
195
+ graphConfig: {
196
+ type: 'standard',
197
+ llmConfig,
198
+ agents: [
199
+ {
200
+ agentId: 'default',
201
+ provider: llmConfig.provider,
202
+ clientOptions: llmConfig,
203
+ tools: allTools,
204
+ toolMap,
205
+ toolRegistry,
206
+ instructions:
207
+ 'You are a friendly AI assistant with advanced coding capabilities.\n\n' +
208
+ 'IMPORTANT: The tools get_team_members(), get_expenses(), and get_weather() are NOT available ' +
209
+ 'for direct function calling. You MUST use the run_tools_with_code tool to invoke them.\n\n' +
210
+ 'When you need to use these tools, write Python code using run_tools_with_code that calls:\n' +
211
+ '- await get_team_members() - returns list of team members\n' +
212
+ '- await get_expenses(user_id="...") - returns expenses for a user\n' +
213
+ '- await get_weather(city="...") - returns weather data\n\n' +
214
+ 'Use asyncio.gather() for parallel execution when calling multiple tools.',
215
+ additional_instructions: `The user's name is ${userName} and they are located in ${location}. Today is ${currentDate}.`,
216
+ },
217
+ ],
218
+ },
219
+ returnContent: true,
220
+ customHandlers,
221
+ });
222
+
223
+ const config: Partial<RunnableConfig> & {
224
+ version: 'v1' | 'v2';
225
+ run_id?: string;
226
+ streamMode: string;
227
+ } = {
228
+ configurable: {
229
+ provider,
230
+ thread_id: 'ptc-conversation-1',
231
+ },
232
+ streamMode: 'values',
233
+ version: 'v2' as const,
234
+ };
235
+
236
+ console.log('Test 1: Team Expense Analysis with PTC');
237
+ console.log('='.repeat(70) + '\n');
238
+
239
+ const userMessage1 = `Hi ${userName}! I need you to analyze our team's expenses. Please:
240
+
241
+ 1. Get the list of all team members
242
+ 2. For each member, fetch their expense records
243
+ 3. Calculate the total expenses per member
244
+ 4. Identify anyone who spent more than $500
245
+ 5. Show me a summary report
246
+
247
+ IMPORTANT: Use the run_tools_with_code tool to do this efficiently.
248
+ Don't call each tool separately - write Python code that orchestrates all the calls!`;
249
+
250
+ conversationHistory.push(new HumanMessage(userMessage1));
251
+
252
+ let inputs = {
253
+ messages: conversationHistory,
254
+ };
255
+
256
+ const finalContentParts1 = await run.processStream(inputs, config);
257
+ const finalMessages1 = run.getRunMessages();
258
+ if (finalMessages1) {
259
+ conversationHistory.push(...finalMessages1);
260
+ }
261
+
262
+ console.log('\n\n====================\n\n');
263
+ console.log('Content Parts:');
264
+ console.dir(contentParts, { depth: null });
265
+
266
+ console.log('\n\n' + '='.repeat(70));
267
+ console.log('Test 2: Conditional Logic and Parallel Execution');
268
+ console.log('='.repeat(70) + '\n');
269
+
270
+ const userMessage2 = `Great job! Now let's test some advanced patterns. Please:
271
+
272
+ 1. Check the weather in both San Francisco and New York (in parallel!)
273
+ 2. Based on which city has better weather (warmer), fetch the team members
274
+ 3. For the Engineering team members only, calculate their travel expenses
275
+ 4. Show me the results
276
+
277
+ Again, use run_tools_with_code for maximum efficiency. Use asyncio.gather()
278
+ to check both cities' weather at the same time!`;
279
+
280
+ conversationHistory.push(new HumanMessage(userMessage2));
281
+
282
+ inputs = {
283
+ messages: conversationHistory,
284
+ };
285
+
286
+ const finalContentParts2 = await run.processStream(inputs, config);
287
+ const finalMessages2 = run.getRunMessages();
288
+ if (finalMessages2) {
289
+ conversationHistory.push(...finalMessages2);
290
+ }
291
+
292
+ console.log('\n\n====================\n\n');
293
+ console.log('Final Content Parts:');
294
+ console.dir(finalContentParts2, { depth: null });
295
+
296
+ console.log('\n\n' + '='.repeat(70));
297
+ console.log('Generating conversation title...');
298
+ console.log('='.repeat(70) + '\n');
299
+
300
+ const { handleLLMEnd, collected } = createMetadataAggregator();
301
+ const titleResult = await run.generateTitle({
302
+ provider,
303
+ inputText: userMessage1,
304
+ contentParts,
305
+ chainOptions: {
306
+ callbacks: [
307
+ {
308
+ handleLLMEnd,
309
+ },
310
+ ],
311
+ },
312
+ });
313
+
314
+ console.log('Generated Title:', titleResult);
315
+ console.log('Collected metadata:', collected);
316
+ }
317
+
318
+ process.on('unhandledRejection', (reason, promise) => {
319
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
320
+ console.log('Conversation history:');
321
+ console.dir(conversationHistory, { depth: null });
322
+ process.exit(1);
323
+ });
324
+
325
+ process.on('uncaughtException', (err) => {
326
+ console.error('Uncaught Exception:', err);
327
+ });
328
+
329
+ testProgrammaticToolCalling().catch((err) => {
330
+ console.error(err);
331
+ console.log('Conversation history:');
332
+ console.dir(conversationHistory, { depth: null });
333
+ process.exit(1);
334
+ });