illuma-agents 1.0.37 → 1.0.39
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 +112 -14
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +5 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +148 -8
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +277 -11
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +128 -61
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +22 -7
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +140 -46
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +1 -1
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +2 -2
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/schemas/validate.cjs +173 -0
- package/dist/cjs/schemas/validate.cjs.map +1 -0
- package/dist/cjs/stream.cjs +4 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +22 -21
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +14 -11
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +101 -2
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +862 -0
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +112 -14
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +5 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +149 -9
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +278 -12
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +127 -60
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +140 -46
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +1 -1
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +2 -2
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/schemas/validate.mjs +167 -0
- package/dist/esm/schemas/validate.mjs.map +1 -0
- package/dist/esm/stream.mjs +4 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/BrowserTools.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +22 -21
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +14 -11
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +102 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +827 -0
- package/dist/esm/tools/ToolSearch.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +51 -1
- package/dist/types/common/enum.d.ts +6 -2
- package/dist/types/graphs/Graph.d.ts +12 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +16 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/llm/bedrock/index.d.ts +89 -11
- package/dist/types/llm/bedrock/types.d.ts +27 -0
- package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
- package/dist/types/messages/cache.d.ts +4 -1
- package/dist/types/schemas/index.d.ts +1 -0
- package/dist/types/schemas/validate.d.ts +36 -0
- package/dist/types/tools/CodeExecutor.d.ts +0 -3
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +0 -3
- package/dist/types/tools/ToolNode.d.ts +3 -1
- package/dist/types/tools/ToolSearch.d.ts +148 -0
- package/dist/types/types/graph.d.ts +71 -0
- package/dist/types/types/llm.d.ts +3 -1
- package/dist/types/types/tools.d.ts +42 -2
- package/package.json +13 -6
- package/src/agents/AgentContext.test.ts +312 -0
- package/src/agents/AgentContext.ts +144 -16
- package/src/common/enum.ts +5 -1
- package/src/graphs/Graph.ts +214 -13
- package/src/graphs/MultiAgentGraph.ts +350 -13
- package/src/index.ts +4 -1
- package/src/llm/bedrock/index.ts +221 -99
- package/src/llm/bedrock/llm.spec.ts +616 -0
- package/src/llm/bedrock/types.ts +51 -0
- package/src/llm/bedrock/utils/index.ts +18 -0
- package/src/llm/bedrock/utils/message_inputs.ts +563 -0
- package/src/llm/bedrock/utils/message_outputs.ts +310 -0
- package/src/messages/__tests__/tools.test.ts +21 -21
- package/src/messages/cache.test.ts +304 -0
- package/src/messages/cache.ts +183 -53
- package/src/messages/core.ts +1 -1
- package/src/messages/tools.ts +2 -2
- package/src/schemas/index.ts +2 -0
- package/src/schemas/validate.test.ts +358 -0
- package/src/schemas/validate.ts +238 -0
- package/src/scripts/caching.ts +27 -19
- package/src/scripts/code_exec_files.ts +58 -15
- package/src/scripts/code_exec_multi_session.ts +241 -0
- package/src/scripts/code_exec_session.ts +282 -0
- package/src/scripts/multi-agent-conditional.ts +1 -0
- package/src/scripts/multi-agent-supervisor.ts +1 -0
- package/src/scripts/programmatic_exec_agent.ts +4 -4
- package/src/scripts/test-handoff-preamble.ts +277 -0
- package/src/scripts/test-parallel-handoffs.ts +291 -0
- package/src/scripts/test-tools-before-handoff.ts +8 -4
- package/src/scripts/test_code_api.ts +361 -0
- package/src/scripts/thinking-bedrock.ts +159 -0
- package/src/scripts/thinking.ts +39 -18
- package/src/scripts/{tool_search_regex.ts → tool_search.ts} +5 -5
- package/src/scripts/tools.ts +7 -3
- package/src/specs/cache.simple.test.ts +396 -0
- package/src/stream.ts +4 -2
- package/src/tools/BrowserTools.ts +39 -17
- package/src/tools/CodeExecutor.ts +26 -23
- package/src/tools/ProgrammaticToolCalling.ts +18 -14
- package/src/tools/ToolNode.ts +114 -1
- package/src/tools/ToolSearch.ts +1041 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -2
- package/src/tools/__tests__/{ToolSearchRegex.integration.test.ts → ToolSearch.integration.test.ts} +6 -6
- package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
- package/src/types/graph.test.ts +183 -0
- package/src/types/graph.ts +73 -0
- package/src/types/llm.ts +3 -1
- package/src/types/tools.ts +51 -2
- package/dist/cjs/tools/ToolSearchRegex.cjs +0 -455
- package/dist/cjs/tools/ToolSearchRegex.cjs.map +0 -1
- package/dist/esm/tools/ToolSearchRegex.mjs +0 -448
- package/dist/esm/tools/ToolSearchRegex.mjs.map +0 -1
- package/dist/types/tools/ToolSearchRegex.d.ts +0 -80
- package/src/tools/ToolSearchRegex.ts +0 -535
- package/src/tools/__tests__/ToolSearchRegex.test.ts +0 -232
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config();
|
|
3
|
+
|
|
4
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
5
|
+
import type * as t from '@/types';
|
|
6
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
7
|
+
import { ToolEndHandler } from '@/events';
|
|
8
|
+
import { Providers, GraphEvents } from '@/common';
|
|
9
|
+
import { sleep } from '@/utils/run';
|
|
10
|
+
import { Run } from '@/run';
|
|
11
|
+
|
|
12
|
+
const conversationHistory: BaseMessage[] = [];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Test parallel handoffs - where an LLM calls multiple transfer tools simultaneously
|
|
16
|
+
*
|
|
17
|
+
* Graph structure:
|
|
18
|
+
* coordinator -> [researcher, writer] (via parallel handoff tools)
|
|
19
|
+
*
|
|
20
|
+
* The coordinator agent has two transfer tools:
|
|
21
|
+
* - transfer_to_researcher
|
|
22
|
+
* - transfer_to_writer
|
|
23
|
+
*
|
|
24
|
+
* When given a task that needs both, it should call both tools in parallel.
|
|
25
|
+
*/
|
|
26
|
+
async function testParallelHandoffs() {
|
|
27
|
+
console.log(
|
|
28
|
+
'Testing Parallel Handoffs (LLM calling multiple transfers)...\n'
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
32
|
+
|
|
33
|
+
const agents: t.AgentInputs[] = [
|
|
34
|
+
{
|
|
35
|
+
agentId: 'coordinator',
|
|
36
|
+
provider: Providers.OPENAI,
|
|
37
|
+
clientOptions: {
|
|
38
|
+
modelName: 'gpt-4o-mini',
|
|
39
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
40
|
+
},
|
|
41
|
+
instructions: `You are a COORDINATOR agent. Your job is to delegate tasks to specialized agents.
|
|
42
|
+
|
|
43
|
+
You have access to two transfer tools:
|
|
44
|
+
- transfer_to_researcher: For research and fact-finding tasks
|
|
45
|
+
- transfer_to_writer: For content creation and writing tasks
|
|
46
|
+
|
|
47
|
+
IMPORTANT: When a task requires BOTH research AND writing, you MUST call BOTH transfer tools SIMULTANEOUSLY in the same response. Do not call them sequentially.
|
|
48
|
+
|
|
49
|
+
For example, if asked to "research and write about X", call both transfers at once to enable parallel work.
|
|
50
|
+
|
|
51
|
+
When delegating, provide clear instructions to each agent about what they should do.`,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
agentId: 'researcher',
|
|
55
|
+
provider: Providers.ANTHROPIC,
|
|
56
|
+
clientOptions: {
|
|
57
|
+
modelName: 'claude-haiku-4-5',
|
|
58
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
59
|
+
},
|
|
60
|
+
instructions: `You are a RESEARCHER. When you receive a task:
|
|
61
|
+
1. Provide concise research findings (100-150 words)
|
|
62
|
+
2. Start your response with "📚 RESEARCH FINDINGS:"`,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
agentId: 'writer',
|
|
66
|
+
provider: Providers.ANTHROPIC,
|
|
67
|
+
clientOptions: {
|
|
68
|
+
modelName: 'claude-haiku-4-5',
|
|
69
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
70
|
+
},
|
|
71
|
+
instructions: `You are a WRITER. When you receive a task:
|
|
72
|
+
1. Provide creative content (100-150 words)
|
|
73
|
+
2. Start your response with "✍️ WRITTEN CONTENT:"`,
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Create handoff edges from coordinator to both researcher and writer.
|
|
79
|
+
* These are separate edges so the LLM sees both transfer tools.
|
|
80
|
+
*/
|
|
81
|
+
const edges: t.GraphEdge[] = [
|
|
82
|
+
{
|
|
83
|
+
from: 'coordinator',
|
|
84
|
+
to: 'researcher',
|
|
85
|
+
edgeType: 'handoff',
|
|
86
|
+
description: 'Transfer to researcher for research and fact-finding tasks',
|
|
87
|
+
prompt: 'Research task instructions',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
from: 'coordinator',
|
|
91
|
+
to: 'writer',
|
|
92
|
+
edgeType: 'handoff',
|
|
93
|
+
description: 'Transfer to writer for content creation and writing tasks',
|
|
94
|
+
prompt: 'Writing task instructions',
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
/** Track which agents are active and their timing */
|
|
99
|
+
const activeAgents = new Set<string>();
|
|
100
|
+
const agentTimings: Record<string, { start?: number; end?: number }> = {};
|
|
101
|
+
const startTime = Date.now();
|
|
102
|
+
|
|
103
|
+
const customHandlers = {
|
|
104
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
105
|
+
[GraphEvents.CHAT_MODEL_END]: {
|
|
106
|
+
handle: (
|
|
107
|
+
_event: string,
|
|
108
|
+
_data: t.StreamEventData,
|
|
109
|
+
metadata?: Record<string, unknown>
|
|
110
|
+
): void => {
|
|
111
|
+
const nodeName = metadata?.langgraph_node as string;
|
|
112
|
+
if (nodeName) {
|
|
113
|
+
const elapsed = Date.now() - startTime;
|
|
114
|
+
agentTimings[nodeName] = agentTimings[nodeName] || {};
|
|
115
|
+
agentTimings[nodeName].end = elapsed;
|
|
116
|
+
activeAgents.delete(nodeName);
|
|
117
|
+
console.log(`\n⏱️ [${nodeName}] COMPLETED at ${elapsed}ms`);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
[GraphEvents.CHAT_MODEL_START]: {
|
|
122
|
+
handle: (
|
|
123
|
+
_event: string,
|
|
124
|
+
_data: t.StreamEventData,
|
|
125
|
+
metadata?: Record<string, unknown>
|
|
126
|
+
): void => {
|
|
127
|
+
const nodeName = metadata?.langgraph_node as string;
|
|
128
|
+
if (nodeName) {
|
|
129
|
+
const elapsed = Date.now() - startTime;
|
|
130
|
+
/** Store first start time for parallel overlap calculation */
|
|
131
|
+
if (!agentTimings[nodeName]?.start) {
|
|
132
|
+
agentTimings[nodeName] = agentTimings[nodeName] || {};
|
|
133
|
+
agentTimings[nodeName].start = elapsed;
|
|
134
|
+
}
|
|
135
|
+
activeAgents.add(nodeName);
|
|
136
|
+
console.log(`\n⏱️ [${nodeName}] STARTED at ${elapsed}ms`);
|
|
137
|
+
console.log(
|
|
138
|
+
` Active agents: ${Array.from(activeAgents).join(', ')}`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
144
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
145
|
+
handle: (
|
|
146
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
147
|
+
data: t.StreamEventData
|
|
148
|
+
): void => {
|
|
149
|
+
aggregateContent({
|
|
150
|
+
event,
|
|
151
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
156
|
+
handle: (
|
|
157
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
158
|
+
data: t.StreamEventData
|
|
159
|
+
): void => {
|
|
160
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
164
|
+
handle: (
|
|
165
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
166
|
+
data: t.StreamEventData
|
|
167
|
+
): void => {
|
|
168
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
172
|
+
handle: (
|
|
173
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
174
|
+
data: t.StreamEventData
|
|
175
|
+
): void => {
|
|
176
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const runConfig: t.RunConfig = {
|
|
182
|
+
runId: `parallel-handoffs-${Date.now()}`,
|
|
183
|
+
graphConfig: {
|
|
184
|
+
type: 'multi-agent',
|
|
185
|
+
agents,
|
|
186
|
+
edges,
|
|
187
|
+
},
|
|
188
|
+
customHandlers,
|
|
189
|
+
returnContent: true,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
const run = await Run.create(runConfig);
|
|
194
|
+
|
|
195
|
+
/** Prompt designed to trigger parallel handoffs without confusing language */
|
|
196
|
+
const userMessage = `Help me with two topics:
|
|
197
|
+
1. The history of the internet
|
|
198
|
+
2. A short poem about technology
|
|
199
|
+
|
|
200
|
+
I need information on both topics.`;
|
|
201
|
+
|
|
202
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
203
|
+
|
|
204
|
+
console.log('User message:', userMessage);
|
|
205
|
+
console.log(
|
|
206
|
+
'\nInvoking multi-agent graph with parallel handoff request...\n'
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
const config = {
|
|
210
|
+
configurable: {
|
|
211
|
+
thread_id: 'parallel-handoffs-test-1',
|
|
212
|
+
},
|
|
213
|
+
streamMode: 'values',
|
|
214
|
+
version: 'v2' as const,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const inputs = {
|
|
218
|
+
messages: conversationHistory,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
await run.processStream(inputs, config);
|
|
222
|
+
const finalMessages = run.getRunMessages();
|
|
223
|
+
|
|
224
|
+
if (finalMessages) {
|
|
225
|
+
conversationHistory.push(...finalMessages);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Analyze parallel execution */
|
|
229
|
+
console.log('\n\n========== TIMING SUMMARY ==========');
|
|
230
|
+
console.log('Available timing keys:', Object.keys(agentTimings));
|
|
231
|
+
for (const [agent, timing] of Object.entries(agentTimings)) {
|
|
232
|
+
const duration =
|
|
233
|
+
timing.end && timing.start ? timing.end - timing.start : 'N/A';
|
|
234
|
+
console.log(
|
|
235
|
+
`${agent}: started=${timing.start}ms, ended=${timing.end}ms, duration=${duration}ms`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Check if researcher and writer ran in parallel (handle key variations) */
|
|
240
|
+
const researcherKey = Object.keys(agentTimings).find((k) =>
|
|
241
|
+
k.includes('researcher')
|
|
242
|
+
);
|
|
243
|
+
const writerKey = Object.keys(agentTimings).find((k) =>
|
|
244
|
+
k.includes('writer')
|
|
245
|
+
);
|
|
246
|
+
const researcherTiming = researcherKey
|
|
247
|
+
? agentTimings[researcherKey]
|
|
248
|
+
: undefined;
|
|
249
|
+
const writerTiming = writerKey ? agentTimings[writerKey] : undefined;
|
|
250
|
+
|
|
251
|
+
if (researcherTiming && writerTiming) {
|
|
252
|
+
const bothStarted = researcherTiming.start && writerTiming.start;
|
|
253
|
+
const bothEnded = researcherTiming.end && writerTiming.end;
|
|
254
|
+
|
|
255
|
+
if (bothStarted && bothEnded) {
|
|
256
|
+
const overlap =
|
|
257
|
+
Math.min(researcherTiming.end!, writerTiming.end!) -
|
|
258
|
+
Math.max(researcherTiming.start!, writerTiming.start!);
|
|
259
|
+
|
|
260
|
+
if (overlap > 0) {
|
|
261
|
+
console.log(
|
|
262
|
+
`\n✅ PARALLEL HANDOFFS SUCCESSFUL: ${overlap}ms overlap between researcher and writer`
|
|
263
|
+
);
|
|
264
|
+
} else {
|
|
265
|
+
console.log(
|
|
266
|
+
`\n⚠️ SEQUENTIAL EXECUTION: researcher and writer did not overlap`
|
|
267
|
+
);
|
|
268
|
+
console.log(
|
|
269
|
+
` This may indicate the LLM called transfers sequentially, not in parallel`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
console.log(
|
|
275
|
+
'\n⚠️ Not all agents were invoked. Check if handoffs occurred.'
|
|
276
|
+
);
|
|
277
|
+
console.log(' researcher timing:', researcherTiming);
|
|
278
|
+
console.log(' writer timing:', writerTiming);
|
|
279
|
+
}
|
|
280
|
+
console.log('====================================\n');
|
|
281
|
+
|
|
282
|
+
console.log('Final content parts:', contentParts.length, 'parts');
|
|
283
|
+
console.dir(contentParts, { depth: null });
|
|
284
|
+
await sleep(3000);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
console.error('Error in parallel handoffs test:', error);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
testParallelHandoffs();
|
|
@@ -28,10 +28,14 @@ async function testToolsBeforeHandoff() {
|
|
|
28
28
|
|
|
29
29
|
// Create custom handlers
|
|
30
30
|
const customHandlers = {
|
|
31
|
-
[GraphEvents.TOOL_END]: new ToolEndHandler(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(
|
|
32
|
+
undefined,
|
|
33
|
+
undefined,
|
|
34
|
+
(name?: string) => {
|
|
35
|
+
console.log(`\n✅ Tool completed: ${name}`);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
),
|
|
35
39
|
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
36
40
|
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
37
41
|
[GraphEvents.ON_RUN_STEP]: {
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// src/scripts/test_code_api.ts
|
|
2
|
+
/**
|
|
3
|
+
* Direct test of the Code API to verify session file persistence.
|
|
4
|
+
* This bypasses the LLM and tests the API directly.
|
|
5
|
+
*
|
|
6
|
+
* Run with: npx ts-node -r dotenv/config src/scripts/test_code_api.ts
|
|
7
|
+
*/
|
|
8
|
+
import { config } from 'dotenv';
|
|
9
|
+
config();
|
|
10
|
+
|
|
11
|
+
import fetch, { RequestInit } from 'node-fetch';
|
|
12
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
13
|
+
|
|
14
|
+
const API_KEY = process.env.LIBRECHAT_CODE_API_KEY ?? '';
|
|
15
|
+
const BASE_URL =
|
|
16
|
+
process.env.LIBRECHAT_CODE_BASEURL ?? 'https://api.librechat.ai/v1';
|
|
17
|
+
const PROXY = process.env.PROXY;
|
|
18
|
+
|
|
19
|
+
if (!API_KEY) {
|
|
20
|
+
console.error('LIBRECHAT_CODE_API_KEY not set');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface FileRef {
|
|
25
|
+
id: string;
|
|
26
|
+
name: string;
|
|
27
|
+
session_id?: string;
|
|
28
|
+
/** Lineage tracking - present if file was modified from previous session */
|
|
29
|
+
modified_from?: {
|
|
30
|
+
id: string;
|
|
31
|
+
session_id: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ExecResult {
|
|
36
|
+
session_id: string;
|
|
37
|
+
stdout: string;
|
|
38
|
+
stderr: string;
|
|
39
|
+
files?: FileRef[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface FileInfo {
|
|
43
|
+
name: string;
|
|
44
|
+
metadata: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function makeRequest(
|
|
48
|
+
endpoint: string,
|
|
49
|
+
body?: Record<string, unknown>
|
|
50
|
+
): Promise<unknown> {
|
|
51
|
+
const fetchOptions: RequestInit = {
|
|
52
|
+
method: body ? 'POST' : 'GET',
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
'User-Agent': 'LibreChat/1.0',
|
|
56
|
+
'X-API-Key': API_KEY,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (body) {
|
|
61
|
+
fetchOptions.body = JSON.stringify(body);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (PROXY) {
|
|
65
|
+
fetchOptions.agent = new HttpsProxyAgent(PROXY);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
console.log(`\n>>> ${body ? 'POST' : 'GET'} ${endpoint}`);
|
|
69
|
+
if (body) {
|
|
70
|
+
console.log('Body:', JSON.stringify(body, null, 2));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const response = await fetch(endpoint, fetchOptions);
|
|
74
|
+
const result = await response.json();
|
|
75
|
+
|
|
76
|
+
console.log(`<<< Response (${response.status}):`);
|
|
77
|
+
console.log(JSON.stringify(result, null, 2));
|
|
78
|
+
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
throw new Error(`HTTP ${response.status}: ${JSON.stringify(result)}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function testCodeAPI(): Promise<void> {
|
|
87
|
+
console.log('='.repeat(60));
|
|
88
|
+
console.log('TEST 1: Create a file');
|
|
89
|
+
console.log('='.repeat(60));
|
|
90
|
+
|
|
91
|
+
const createCode = `
|
|
92
|
+
import json
|
|
93
|
+
|
|
94
|
+
config = {
|
|
95
|
+
"app_name": "TestApp",
|
|
96
|
+
"version": "1.0.0",
|
|
97
|
+
"debug": True
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
with open("/mnt/data/test_config.json", "w") as f:
|
|
101
|
+
json.dump(config, f, indent=2)
|
|
102
|
+
|
|
103
|
+
with open("/mnt/data/test_config.json", "r") as f:
|
|
104
|
+
print(f.read())
|
|
105
|
+
`;
|
|
106
|
+
|
|
107
|
+
const result1 = (await makeRequest(`${BASE_URL}/exec`, {
|
|
108
|
+
lang: 'py',
|
|
109
|
+
code: createCode,
|
|
110
|
+
})) as ExecResult;
|
|
111
|
+
|
|
112
|
+
const sessionId = result1.session_id;
|
|
113
|
+
const files = result1.files ?? [];
|
|
114
|
+
|
|
115
|
+
console.log('\n--- Result Summary ---');
|
|
116
|
+
console.log('session_id:', sessionId);
|
|
117
|
+
console.log('files:', files);
|
|
118
|
+
console.log('stdout:', result1.stdout);
|
|
119
|
+
console.log('stderr:', result1.stderr);
|
|
120
|
+
|
|
121
|
+
if (!sessionId || files.length === 0) {
|
|
122
|
+
console.error('\n❌ No session_id or files returned!');
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Check if files now include session_id (new API feature)
|
|
127
|
+
const hasSessionIdInFiles = files.some((f) => f.session_id != null);
|
|
128
|
+
console.log('\n✅ Files include session_id:', hasSessionIdInFiles);
|
|
129
|
+
|
|
130
|
+
console.log('\n' + '='.repeat(60));
|
|
131
|
+
console.log(
|
|
132
|
+
'TEST 2: Fetch files IMMEDIATELY (no delay - testing race condition fix)'
|
|
133
|
+
);
|
|
134
|
+
console.log('='.repeat(60));
|
|
135
|
+
|
|
136
|
+
const filesResult = (await makeRequest(
|
|
137
|
+
`${BASE_URL}/files/${sessionId}?detail=full`
|
|
138
|
+
)) as FileInfo[];
|
|
139
|
+
|
|
140
|
+
console.log('\n--- Files in session (detail=full) ---');
|
|
141
|
+
for (const file of filesResult) {
|
|
142
|
+
console.log('File:', file.name);
|
|
143
|
+
console.log(' metadata:', file.metadata);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (filesResult.length === 0) {
|
|
147
|
+
console.log(
|
|
148
|
+
'\n⚠️ Files endpoint returned empty - race condition may still exist'
|
|
149
|
+
);
|
|
150
|
+
} else {
|
|
151
|
+
console.log('\n✅ Files available immediately!');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Test new normalized detail level
|
|
155
|
+
console.log('\n' + '='.repeat(60));
|
|
156
|
+
console.log('TEST 2b: Fetch files with detail=normalized');
|
|
157
|
+
console.log('='.repeat(60));
|
|
158
|
+
|
|
159
|
+
const normalizedResult = (await makeRequest(
|
|
160
|
+
`${BASE_URL}/files/${sessionId}?detail=normalized`
|
|
161
|
+
)) as FileRef[];
|
|
162
|
+
|
|
163
|
+
console.log('\n--- Files in session (detail=normalized) ---');
|
|
164
|
+
console.log(JSON.stringify(normalizedResult, null, 2));
|
|
165
|
+
|
|
166
|
+
console.log('\n' + '='.repeat(60));
|
|
167
|
+
console.log(
|
|
168
|
+
'TEST 3: Read file IMMEDIATELY using files from original response'
|
|
169
|
+
);
|
|
170
|
+
console.log('='.repeat(60));
|
|
171
|
+
|
|
172
|
+
// Use files directly - if API returns session_id, use that; otherwise add it
|
|
173
|
+
const fileReferences: FileRef[] = files.map((file) => ({
|
|
174
|
+
session_id: file.session_id ?? sessionId,
|
|
175
|
+
id: file.id,
|
|
176
|
+
name: file.name,
|
|
177
|
+
}));
|
|
178
|
+
|
|
179
|
+
console.log(
|
|
180
|
+
'\nFile references we will send:',
|
|
181
|
+
JSON.stringify(fileReferences, null, 2)
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const readCode = `
|
|
185
|
+
import json
|
|
186
|
+
|
|
187
|
+
with open("/mnt/data/test_config.json", "r") as f:
|
|
188
|
+
config = json.load(f)
|
|
189
|
+
print("Read config:")
|
|
190
|
+
print(json.dumps(config, indent=2))
|
|
191
|
+
print("Version:", config.get("version"))
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
const result2 = (await makeRequest(`${BASE_URL}/exec`, {
|
|
195
|
+
lang: 'py',
|
|
196
|
+
code: readCode,
|
|
197
|
+
files: fileReferences,
|
|
198
|
+
})) as ExecResult;
|
|
199
|
+
|
|
200
|
+
console.log('\n--- Result Summary ---');
|
|
201
|
+
console.log('stdout:', result2.stdout);
|
|
202
|
+
console.log('stderr:', result2.stderr);
|
|
203
|
+
|
|
204
|
+
if (result2.stderr && result2.stderr.includes('FileNotFoundError')) {
|
|
205
|
+
console.log(
|
|
206
|
+
'\n❌ File not found! The file reference format might be wrong.'
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// Try alternative format - just session_id
|
|
210
|
+
console.log('\n' + '='.repeat(60));
|
|
211
|
+
console.log('TEST 4: Try with just session_id in request');
|
|
212
|
+
console.log('='.repeat(60));
|
|
213
|
+
|
|
214
|
+
const result3 = (await makeRequest(`${BASE_URL}/exec`, {
|
|
215
|
+
lang: 'py',
|
|
216
|
+
code: readCode,
|
|
217
|
+
session_id: sessionId,
|
|
218
|
+
})) as ExecResult;
|
|
219
|
+
|
|
220
|
+
console.log('\n--- Result Summary ---');
|
|
221
|
+
console.log('stdout:', result3.stdout);
|
|
222
|
+
console.log('stderr:', result3.stderr);
|
|
223
|
+
} else {
|
|
224
|
+
console.log('\n✅ File read successfully!');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// ============================================================
|
|
228
|
+
// TEST 4: MODIFY the file (same filename) - tests editable files
|
|
229
|
+
// ============================================================
|
|
230
|
+
console.log('\n' + '='.repeat(60));
|
|
231
|
+
console.log('TEST 4: MODIFY file in-place (testing editable files feature)');
|
|
232
|
+
console.log('='.repeat(60));
|
|
233
|
+
|
|
234
|
+
const modifyCode = `
|
|
235
|
+
import json
|
|
236
|
+
|
|
237
|
+
# Read the existing file
|
|
238
|
+
with open("/mnt/data/test_config.json", "r") as f:
|
|
239
|
+
config = json.load(f)
|
|
240
|
+
|
|
241
|
+
print("Original config:")
|
|
242
|
+
print(json.dumps(config, indent=2))
|
|
243
|
+
|
|
244
|
+
# Modify the config
|
|
245
|
+
config["version"] = "2.0.0"
|
|
246
|
+
config["modified"] = True
|
|
247
|
+
|
|
248
|
+
# Write BACK to the SAME filename (should work now!)
|
|
249
|
+
with open("/mnt/data/test_config.json", "w") as f:
|
|
250
|
+
json.dump(config, f, indent=2)
|
|
251
|
+
|
|
252
|
+
# Verify the write
|
|
253
|
+
with open("/mnt/data/test_config.json", "r") as f:
|
|
254
|
+
updated = json.load(f)
|
|
255
|
+
|
|
256
|
+
print("\\nUpdated config:")
|
|
257
|
+
print(json.dumps(updated, indent=2))
|
|
258
|
+
`;
|
|
259
|
+
|
|
260
|
+
const result3 = (await makeRequest(`${BASE_URL}/exec`, {
|
|
261
|
+
lang: 'py',
|
|
262
|
+
code: modifyCode,
|
|
263
|
+
files: fileReferences,
|
|
264
|
+
})) as ExecResult;
|
|
265
|
+
|
|
266
|
+
console.log('\n--- Result Summary ---');
|
|
267
|
+
console.log('stdout:', result3.stdout);
|
|
268
|
+
console.log('stderr:', result3.stderr);
|
|
269
|
+
console.log('files:', JSON.stringify(result3.files, null, 2));
|
|
270
|
+
|
|
271
|
+
if (result3.stderr && result3.stderr.includes('Permission denied')) {
|
|
272
|
+
console.log('\n❌ Permission denied - files are still read-only!');
|
|
273
|
+
} else if (result3.stderr && result3.stderr.includes('Error')) {
|
|
274
|
+
console.log('\n❌ Error modifying file:', result3.stderr);
|
|
275
|
+
} else {
|
|
276
|
+
console.log('\n✅ File modified successfully!');
|
|
277
|
+
|
|
278
|
+
// Check for modified_from lineage
|
|
279
|
+
const modifiedFile = result3.files?.find(
|
|
280
|
+
(f) => f.name === 'test_config.json'
|
|
281
|
+
);
|
|
282
|
+
if (modifiedFile) {
|
|
283
|
+
console.log('\n--- Modified File Details ---');
|
|
284
|
+
console.log(' id:', modifiedFile.id);
|
|
285
|
+
console.log(' name:', modifiedFile.name);
|
|
286
|
+
console.log(' session_id:', modifiedFile.session_id);
|
|
287
|
+
if (modifiedFile.modified_from) {
|
|
288
|
+
console.log(
|
|
289
|
+
' modified_from:',
|
|
290
|
+
JSON.stringify(modifiedFile.modified_from)
|
|
291
|
+
);
|
|
292
|
+
console.log(
|
|
293
|
+
'\n✅ Lineage tracking working! File shows it was modified from previous session.'
|
|
294
|
+
);
|
|
295
|
+
} else {
|
|
296
|
+
console.log(
|
|
297
|
+
'\n⚠️ No modified_from field - lineage tracking not present'
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
console.log('\n⚠️ Modified file not found in response files array');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ============================================================
|
|
306
|
+
// TEST 5: Verify modification persists in next execution
|
|
307
|
+
// ============================================================
|
|
308
|
+
console.log('\n' + '='.repeat(60));
|
|
309
|
+
console.log(
|
|
310
|
+
'TEST 5: Verify modified file can be read in subsequent execution'
|
|
311
|
+
);
|
|
312
|
+
console.log('='.repeat(60));
|
|
313
|
+
|
|
314
|
+
// Use the new file references from the modify response
|
|
315
|
+
const newFileRefs: FileRef[] = (result3.files ?? []).map((file) => ({
|
|
316
|
+
session_id: file.session_id ?? result3.session_id,
|
|
317
|
+
id: file.id,
|
|
318
|
+
name: file.name,
|
|
319
|
+
}));
|
|
320
|
+
|
|
321
|
+
if (newFileRefs.length === 0) {
|
|
322
|
+
console.log(
|
|
323
|
+
'\n⚠️ No files returned from modification, skipping verification'
|
|
324
|
+
);
|
|
325
|
+
} else {
|
|
326
|
+
console.log(
|
|
327
|
+
'\nUsing new file references:',
|
|
328
|
+
JSON.stringify(newFileRefs, null, 2)
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
const verifyCode = `
|
|
332
|
+
import json
|
|
333
|
+
|
|
334
|
+
with open("/mnt/data/test_config.json", "r") as f:
|
|
335
|
+
config = json.load(f)
|
|
336
|
+
|
|
337
|
+
print("Verified config:")
|
|
338
|
+
print(json.dumps(config, indent=2))
|
|
339
|
+
|
|
340
|
+
if config.get("version") == "2.0.0" and config.get("modified") == True:
|
|
341
|
+
print("\\n✅ Modification persisted correctly!")
|
|
342
|
+
else:
|
|
343
|
+
print("\\n❌ Modification did NOT persist!")
|
|
344
|
+
`;
|
|
345
|
+
|
|
346
|
+
const result4 = (await makeRequest(`${BASE_URL}/exec`, {
|
|
347
|
+
lang: 'py',
|
|
348
|
+
code: verifyCode,
|
|
349
|
+
files: newFileRefs,
|
|
350
|
+
})) as ExecResult;
|
|
351
|
+
|
|
352
|
+
console.log('\n--- Result Summary ---');
|
|
353
|
+
console.log('stdout:', result4.stdout);
|
|
354
|
+
console.log('stderr:', result4.stderr);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
testCodeAPI().catch((err) => {
|
|
359
|
+
console.error('Error:', err);
|
|
360
|
+
process.exit(1);
|
|
361
|
+
});
|