@librechat/agents 3.0.775 → 3.1.0
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/graphs/Graph.cjs +19 -5
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +98 -25
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +27 -77
- 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/stream.cjs +4 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +9 -5
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +19 -5
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +97 -24
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +27 -77
- 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/stream.mjs +4 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +9 -5
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/types/llm/bedrock/index.d.ts +86 -7
- 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/types/tools.d.ts +2 -0
- package/package.json +7 -4
- package/src/graphs/Graph.ts +23 -5
- package/src/llm/bedrock/index.ts +180 -43
- 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/cache.test.ts +6 -12
- package/src/messages/cache.ts +48 -107
- package/src/messages/core.ts +1 -1
- package/src/scripts/code_exec_multi_session.ts +241 -0
- package/src/scripts/thinking-bedrock.ts +159 -0
- package/src/scripts/thinking.ts +39 -18
- 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/ToolNode.ts +9 -5
- package/src/types/tools.ts +2 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// src/scripts/code_exec_multi_session.ts
|
|
2
|
+
/**
|
|
3
|
+
* Tests multi-session file tracking for code execution.
|
|
4
|
+
* Verifies that:
|
|
5
|
+
* 1. Files from multiple executions are accumulated
|
|
6
|
+
* 2. Each file tracks its source session_id
|
|
7
|
+
* 3. Edited/recreated files replace older versions (latest preferred)
|
|
8
|
+
*
|
|
9
|
+
* Run with: npm run code_exec_multi_session
|
|
10
|
+
*/
|
|
11
|
+
import { config } from 'dotenv';
|
|
12
|
+
config();
|
|
13
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
14
|
+
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
15
|
+
import type * as t from '@/types';
|
|
16
|
+
import { ChatModelStreamHandler } from '@/stream';
|
|
17
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
18
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
19
|
+
import { getArgs } from '@/scripts/args';
|
|
20
|
+
import { Constants, GraphEvents } from '@/common';
|
|
21
|
+
import { Run } from '@/run';
|
|
22
|
+
import { createCodeExecutionTool } from '@/tools/CodeExecutor';
|
|
23
|
+
|
|
24
|
+
const conversationHistory: BaseMessage[] = [];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Prints session context from the graph
|
|
28
|
+
*/
|
|
29
|
+
function printSessionContext(run: Run<t.IState>, label: string): void {
|
|
30
|
+
const graph = run.Graph;
|
|
31
|
+
if (!graph) {
|
|
32
|
+
console.log(`\n[${label}] No graph available`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const session = graph.sessions.get(Constants.EXECUTE_CODE) as
|
|
37
|
+
| t.CodeSessionContext
|
|
38
|
+
| undefined;
|
|
39
|
+
|
|
40
|
+
console.log(`\n========== ${label} ==========`);
|
|
41
|
+
if (!session) {
|
|
42
|
+
console.log(' No session context stored yet');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log(` Latest session_id: ${session.session_id}`);
|
|
47
|
+
console.log(` Files tracked: ${session.files.length}`);
|
|
48
|
+
for (const file of session.files) {
|
|
49
|
+
console.log(` - ${file.name} (session: ${file.session_id})`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function testMultiSessionFiles(): Promise<void> {
|
|
54
|
+
const { provider } = await getArgs();
|
|
55
|
+
|
|
56
|
+
const customHandlers = {
|
|
57
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
58
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
59
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const llmConfig = getLLMConfig(provider);
|
|
63
|
+
|
|
64
|
+
const run = await Run.create<t.IState>({
|
|
65
|
+
runId: 'multi-session-test',
|
|
66
|
+
graphConfig: {
|
|
67
|
+
type: 'standard',
|
|
68
|
+
llmConfig,
|
|
69
|
+
tools: [createCodeExecutionTool()],
|
|
70
|
+
instructions: `You are a coding assistant. Execute code exactly as requested.
|
|
71
|
+
When asked to create files, use Python and save to /mnt/data/.
|
|
72
|
+
When reading files, print their contents.
|
|
73
|
+
Be concise in responses.`,
|
|
74
|
+
},
|
|
75
|
+
returnContent: true,
|
|
76
|
+
customHandlers,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const streamConfig: Partial<RunnableConfig> & {
|
|
80
|
+
version: 'v1' | 'v2';
|
|
81
|
+
streamMode: string;
|
|
82
|
+
} = {
|
|
83
|
+
configurable: {
|
|
84
|
+
provider,
|
|
85
|
+
thread_id: 'multi-session-test',
|
|
86
|
+
},
|
|
87
|
+
streamMode: 'values',
|
|
88
|
+
version: 'v2' as const,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// ========== TEST 1: Create first file ==========
|
|
92
|
+
console.log('\n\n' + '='.repeat(60));
|
|
93
|
+
console.log('TEST 1: Create first file (file_a.txt)');
|
|
94
|
+
console.log('='.repeat(60));
|
|
95
|
+
|
|
96
|
+
conversationHistory.push(
|
|
97
|
+
new HumanMessage(`
|
|
98
|
+
Create a file called "file_a.txt" with the content:
|
|
99
|
+
"This is file A, version 1"
|
|
100
|
+
Print confirmation when done.
|
|
101
|
+
`)
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
await run.processStream({ messages: conversationHistory }, streamConfig);
|
|
105
|
+
const messages1 = run.getRunMessages();
|
|
106
|
+
if (messages1) conversationHistory.push(...messages1);
|
|
107
|
+
|
|
108
|
+
printSessionContext(run, 'After Test 1');
|
|
109
|
+
|
|
110
|
+
// ========== TEST 2: Create second file (different session) ==========
|
|
111
|
+
console.log('\n\n' + '='.repeat(60));
|
|
112
|
+
console.log('TEST 2: Create second file (file_b.txt)');
|
|
113
|
+
console.log('Expecting: Both file_a.txt and file_b.txt tracked');
|
|
114
|
+
console.log('='.repeat(60));
|
|
115
|
+
|
|
116
|
+
conversationHistory.push(
|
|
117
|
+
new HumanMessage(`
|
|
118
|
+
Create a NEW file called "file_b.txt" with the content:
|
|
119
|
+
"This is file B"
|
|
120
|
+
Print confirmation when done.
|
|
121
|
+
`)
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
await run.processStream({ messages: conversationHistory }, streamConfig);
|
|
125
|
+
const messages2 = run.getRunMessages();
|
|
126
|
+
if (messages2) conversationHistory.push(...messages2);
|
|
127
|
+
|
|
128
|
+
printSessionContext(run, 'After Test 2');
|
|
129
|
+
|
|
130
|
+
// ========== TEST 3: Read BOTH files (verifies accumulation) ==========
|
|
131
|
+
console.log('\n\n' + '='.repeat(60));
|
|
132
|
+
console.log('TEST 3: Read BOTH files from previous executions');
|
|
133
|
+
console.log('This verifies multi-session file accumulation works');
|
|
134
|
+
console.log('='.repeat(60));
|
|
135
|
+
|
|
136
|
+
conversationHistory.push(
|
|
137
|
+
new HumanMessage(`
|
|
138
|
+
Read and print the contents of BOTH files:
|
|
139
|
+
1. file_a.txt
|
|
140
|
+
2. file_b.txt
|
|
141
|
+
|
|
142
|
+
Show me what's in each file.
|
|
143
|
+
`)
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
await run.processStream({ messages: conversationHistory }, streamConfig);
|
|
147
|
+
const messages3 = run.getRunMessages();
|
|
148
|
+
if (messages3) conversationHistory.push(...messages3);
|
|
149
|
+
|
|
150
|
+
printSessionContext(run, 'After Test 3');
|
|
151
|
+
|
|
152
|
+
// ========== TEST 4: Edit file_a.txt (verifies latest-wins) ==========
|
|
153
|
+
console.log('\n\n' + '='.repeat(60));
|
|
154
|
+
console.log('TEST 4: Edit file_a.txt (create new version)');
|
|
155
|
+
console.log('Expecting: Old file_a.txt replaced with new version');
|
|
156
|
+
console.log('='.repeat(60));
|
|
157
|
+
|
|
158
|
+
conversationHistory.push(
|
|
159
|
+
new HumanMessage(`
|
|
160
|
+
Create an UPDATED version of "file_a.txt" with the content:
|
|
161
|
+
"This is file A, version 2 - UPDATED"
|
|
162
|
+
Print confirmation when done.
|
|
163
|
+
`)
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
await run.processStream({ messages: conversationHistory }, streamConfig);
|
|
167
|
+
const messages4 = run.getRunMessages();
|
|
168
|
+
if (messages4) conversationHistory.push(...messages4);
|
|
169
|
+
|
|
170
|
+
printSessionContext(run, 'After Test 4');
|
|
171
|
+
|
|
172
|
+
// ========== TEST 5: Read file_a.txt (verifies latest version) ==========
|
|
173
|
+
console.log('\n\n' + '='.repeat(60));
|
|
174
|
+
console.log('TEST 5: Read file_a.txt to verify it has the UPDATED content');
|
|
175
|
+
console.log('Expected: "version 2 - UPDATED" NOT "version 1"');
|
|
176
|
+
console.log('='.repeat(60));
|
|
177
|
+
|
|
178
|
+
conversationHistory.push(
|
|
179
|
+
new HumanMessage(`
|
|
180
|
+
Read and print the contents of file_a.txt.
|
|
181
|
+
Tell me what version it shows.
|
|
182
|
+
`)
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
await run.processStream({ messages: conversationHistory }, streamConfig);
|
|
186
|
+
const messages5 = run.getRunMessages();
|
|
187
|
+
if (messages5) conversationHistory.push(...messages5);
|
|
188
|
+
|
|
189
|
+
printSessionContext(run, 'Final Session State');
|
|
190
|
+
|
|
191
|
+
// ========== SUMMARY ==========
|
|
192
|
+
console.log('\n\n' + '='.repeat(60));
|
|
193
|
+
console.log('TEST SUMMARY');
|
|
194
|
+
console.log('='.repeat(60));
|
|
195
|
+
|
|
196
|
+
const finalSession = run.Graph?.sessions.get(Constants.EXECUTE_CODE) as
|
|
197
|
+
| t.CodeSessionContext
|
|
198
|
+
| undefined;
|
|
199
|
+
|
|
200
|
+
if (finalSession) {
|
|
201
|
+
const uniqueSessionIds = new Set(
|
|
202
|
+
finalSession.files.map((f) => f.session_id)
|
|
203
|
+
);
|
|
204
|
+
console.log(`\nTotal files tracked: ${finalSession.files.length}`);
|
|
205
|
+
console.log(`Unique session_ids: ${uniqueSessionIds.size}`);
|
|
206
|
+
console.log('\nFiles:');
|
|
207
|
+
for (const file of finalSession.files) {
|
|
208
|
+
console.log(
|
|
209
|
+
` - ${file.name} (session: ${file.session_id?.slice(0, 20)}...)`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Verify expectations
|
|
214
|
+
const fileACount = finalSession.files.filter(
|
|
215
|
+
(f) => f.name === 'file_a.txt'
|
|
216
|
+
).length;
|
|
217
|
+
const fileBCount = finalSession.files.filter(
|
|
218
|
+
(f) => f.name === 'file_b.txt'
|
|
219
|
+
).length;
|
|
220
|
+
|
|
221
|
+
console.log('\n✓ Checks:');
|
|
222
|
+
console.log(` file_a.txt count: ${fileACount} (expected: 1, latest wins)`);
|
|
223
|
+
console.log(` file_b.txt count: ${fileBCount} (expected: 1)`);
|
|
224
|
+
|
|
225
|
+
if (fileACount === 1 && fileBCount === 1) {
|
|
226
|
+
console.log('\n✅ All tests passed! Multi-session tracking works.');
|
|
227
|
+
} else {
|
|
228
|
+
console.log('\n❌ Test failed - unexpected file counts');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
234
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
235
|
+
process.exit(1);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
testMultiSessionFiles().catch((err) => {
|
|
239
|
+
console.error('Test failed:', err);
|
|
240
|
+
process.exit(1);
|
|
241
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// src/scripts/thinking-bedrock.ts
|
|
2
|
+
import { config } from 'dotenv';
|
|
3
|
+
config();
|
|
4
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
5
|
+
import type { UsageMetadata } from '@langchain/core/messages';
|
|
6
|
+
import * as t from '@/types';
|
|
7
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
8
|
+
import { createCodeExecutionTool } from '@/tools/CodeExecutor';
|
|
9
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
10
|
+
import { GraphEvents, Providers } from '@/common';
|
|
11
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
12
|
+
import { getArgs } from '@/scripts/args';
|
|
13
|
+
import { Run } from '@/run';
|
|
14
|
+
|
|
15
|
+
const conversationHistory: BaseMessage[] = [];
|
|
16
|
+
let _contentParts: t.MessageContentComplex[] = [];
|
|
17
|
+
const collectedUsage: UsageMetadata[] = [];
|
|
18
|
+
|
|
19
|
+
async function testBedrockThinking(): Promise<void> {
|
|
20
|
+
const { userName } = await getArgs();
|
|
21
|
+
const instructions = `You are a helpful AI assistant for ${userName}. When answering questions, be thorough in your reasoning.`;
|
|
22
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
23
|
+
_contentParts = contentParts as t.MessageContentComplex[];
|
|
24
|
+
|
|
25
|
+
// Set up event handlers
|
|
26
|
+
const customHandlers = {
|
|
27
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
28
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(collectedUsage),
|
|
29
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
30
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
31
|
+
handle: (
|
|
32
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
33
|
+
data: t.StreamEventData
|
|
34
|
+
): void => {
|
|
35
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
36
|
+
aggregateContent({
|
|
37
|
+
event,
|
|
38
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
43
|
+
handle: (event: GraphEvents.ON_RUN_STEP, data: t.RunStep) => {
|
|
44
|
+
aggregateContent({ event, data });
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
48
|
+
handle: (
|
|
49
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
50
|
+
data: t.RunStepDeltaEvent
|
|
51
|
+
) => {
|
|
52
|
+
aggregateContent({ event, data });
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
56
|
+
handle: (
|
|
57
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
58
|
+
data: t.MessageDeltaEvent
|
|
59
|
+
) => {
|
|
60
|
+
aggregateContent({ event, data });
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
[GraphEvents.ON_REASONING_DELTA]: {
|
|
64
|
+
handle: (
|
|
65
|
+
event: GraphEvents.ON_REASONING_DELTA,
|
|
66
|
+
data: t.ReasoningDeltaEvent
|
|
67
|
+
) => {
|
|
68
|
+
aggregateContent({ event, data });
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const baseLlmConfig = getLLMConfig(Providers.BEDROCK);
|
|
74
|
+
|
|
75
|
+
// Enable thinking with token budget for Bedrock
|
|
76
|
+
const llmConfig = {
|
|
77
|
+
...baseLlmConfig,
|
|
78
|
+
model: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0',
|
|
79
|
+
maxTokens: 5000,
|
|
80
|
+
additionalModelRequestFields: {
|
|
81
|
+
thinking: { type: 'enabled', budget_tokens: 2000 },
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const run = await Run.create<t.IState>({
|
|
86
|
+
runId: 'test-bedrock-thinking-id',
|
|
87
|
+
graphConfig: {
|
|
88
|
+
instructions,
|
|
89
|
+
type: 'standard',
|
|
90
|
+
tools: [createCodeExecutionTool()],
|
|
91
|
+
llmConfig,
|
|
92
|
+
},
|
|
93
|
+
returnContent: true,
|
|
94
|
+
customHandlers: customHandlers as t.RunConfig['customHandlers'],
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const config = {
|
|
98
|
+
configurable: {
|
|
99
|
+
thread_id: 'bedrock-thinking-test-thread',
|
|
100
|
+
},
|
|
101
|
+
streamMode: 'values',
|
|
102
|
+
version: 'v2' as const,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Test 1: Regular thinking mode
|
|
106
|
+
console.log('\n\nTest 1: Bedrock Regular thinking mode');
|
|
107
|
+
const userMessage1 = `Please print 'hello world' in python`;
|
|
108
|
+
conversationHistory.push(new HumanMessage(userMessage1));
|
|
109
|
+
|
|
110
|
+
console.log('Running first query with Bedrock thinking enabled...');
|
|
111
|
+
const firstInputs = { messages: [...conversationHistory] };
|
|
112
|
+
await run.processStream(firstInputs, config);
|
|
113
|
+
|
|
114
|
+
// Extract and display thinking blocks
|
|
115
|
+
const finalMessages = run.getRunMessages();
|
|
116
|
+
console.log('\n\nFinal messages after Test 1:');
|
|
117
|
+
console.dir(finalMessages, { depth: null });
|
|
118
|
+
|
|
119
|
+
// Test 2: Try multi-turn conversation
|
|
120
|
+
console.log(
|
|
121
|
+
'\n\nTest 2: Multi-turn conversation with Bedrock thinking enabled'
|
|
122
|
+
);
|
|
123
|
+
const userMessage2 = `Given your previous analysis, what would be the most significant technical challenges in making this transition?`;
|
|
124
|
+
conversationHistory.push(new HumanMessage(userMessage2));
|
|
125
|
+
|
|
126
|
+
console.log('Running second query with Bedrock thinking enabled...');
|
|
127
|
+
const secondInputs = { messages: [...conversationHistory] };
|
|
128
|
+
await run.processStream(secondInputs, config);
|
|
129
|
+
|
|
130
|
+
// Display thinking blocks for second response
|
|
131
|
+
const finalMessages2 = run.getRunMessages();
|
|
132
|
+
console.log('\n\nBedrock thinking feature test completed!');
|
|
133
|
+
console.dir(finalMessages2, { depth: null });
|
|
134
|
+
|
|
135
|
+
console.log('\n\nContent parts:');
|
|
136
|
+
console.dir(_contentParts, { depth: null });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
140
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
141
|
+
console.log('Conversation history:');
|
|
142
|
+
console.dir(conversationHistory, { depth: null });
|
|
143
|
+
console.log('Content parts:');
|
|
144
|
+
console.dir(_contentParts, { depth: null });
|
|
145
|
+
process.exit(1);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
process.on('uncaughtException', (err) => {
|
|
149
|
+
console.error('Uncaught Exception:', err);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
testBedrockThinking().catch((err) => {
|
|
153
|
+
console.error(err);
|
|
154
|
+
console.log('Conversation history:');
|
|
155
|
+
console.dir(conversationHistory, { depth: null });
|
|
156
|
+
console.log('Content parts:');
|
|
157
|
+
console.dir(_contentParts, { depth: null });
|
|
158
|
+
process.exit(1);
|
|
159
|
+
});
|
package/src/scripts/thinking.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// src/scripts/test-thinking.ts
|
|
2
2
|
import { config } from 'dotenv';
|
|
3
3
|
config();
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HumanMessage,
|
|
6
|
+
SystemMessage,
|
|
7
|
+
BaseMessage,
|
|
8
|
+
} from '@langchain/core/messages';
|
|
5
9
|
import type { UsageMetadata } from '@langchain/core/messages';
|
|
6
10
|
import * as t from '@/types';
|
|
7
11
|
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
@@ -21,17 +25,23 @@ async function testThinking(): Promise<void> {
|
|
|
21
25
|
const instructions = `You are a helpful AI assistant for ${userName}. When answering questions, be thorough in your reasoning.`;
|
|
22
26
|
const { contentParts, aggregateContent } = createContentAggregator();
|
|
23
27
|
_contentParts = contentParts as t.MessageContentComplex[];
|
|
24
|
-
|
|
28
|
+
|
|
25
29
|
// Set up event handlers
|
|
26
30
|
const customHandlers = {
|
|
27
31
|
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
28
32
|
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(collectedUsage),
|
|
29
33
|
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
30
34
|
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
31
|
-
handle: (
|
|
35
|
+
handle: (
|
|
36
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
37
|
+
data: t.StreamEventData
|
|
38
|
+
): void => {
|
|
32
39
|
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
33
|
-
aggregateContent({
|
|
34
|
-
|
|
40
|
+
aggregateContent({
|
|
41
|
+
event,
|
|
42
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
43
|
+
});
|
|
44
|
+
},
|
|
35
45
|
},
|
|
36
46
|
[GraphEvents.ON_RUN_STEP]: {
|
|
37
47
|
handle: (event: GraphEvents.ON_RUN_STEP, data: t.RunStep) => {
|
|
@@ -39,29 +49,38 @@ async function testThinking(): Promise<void> {
|
|
|
39
49
|
},
|
|
40
50
|
},
|
|
41
51
|
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
42
|
-
handle: (
|
|
52
|
+
handle: (
|
|
53
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
54
|
+
data: t.RunStepDeltaEvent
|
|
55
|
+
) => {
|
|
43
56
|
aggregateContent({ event, data });
|
|
44
57
|
},
|
|
45
58
|
},
|
|
46
59
|
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
47
|
-
handle: (
|
|
60
|
+
handle: (
|
|
61
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
62
|
+
data: t.MessageDeltaEvent
|
|
63
|
+
) => {
|
|
48
64
|
aggregateContent({ event, data });
|
|
49
65
|
},
|
|
50
66
|
},
|
|
51
67
|
[GraphEvents.ON_REASONING_DELTA]: {
|
|
52
|
-
handle: (
|
|
68
|
+
handle: (
|
|
69
|
+
event: GraphEvents.ON_REASONING_DELTA,
|
|
70
|
+
data: t.ReasoningDeltaEvent
|
|
71
|
+
) => {
|
|
53
72
|
aggregateContent({ event, data });
|
|
54
73
|
},
|
|
55
74
|
},
|
|
56
75
|
};
|
|
57
76
|
|
|
58
77
|
const baseLlmConfig: t.LLMConfig = getLLMConfig(Providers.ANTHROPIC);
|
|
59
|
-
|
|
78
|
+
|
|
60
79
|
// Enable thinking with token budget
|
|
61
80
|
const llmConfig = {
|
|
62
81
|
...baseLlmConfig,
|
|
63
82
|
model: 'claude-3-7-sonnet-latest',
|
|
64
|
-
thinking: { type:
|
|
83
|
+
thinking: { type: 'enabled', budget_tokens: 2000 },
|
|
65
84
|
};
|
|
66
85
|
|
|
67
86
|
const run = await Run.create<t.IState>({
|
|
@@ -93,7 +112,7 @@ async function testThinking(): Promise<void> {
|
|
|
93
112
|
console.log('Running first query with thinking enabled...');
|
|
94
113
|
const firstInputs = { messages: [...conversationHistory] };
|
|
95
114
|
await run.processStream(firstInputs, config);
|
|
96
|
-
|
|
115
|
+
|
|
97
116
|
// Extract and display thinking blocks
|
|
98
117
|
const finalMessages = run.getRunMessages();
|
|
99
118
|
|
|
@@ -101,30 +120,32 @@ async function testThinking(): Promise<void> {
|
|
|
101
120
|
console.log('\n\nTest 2: Multi-turn conversation with thinking enabled');
|
|
102
121
|
const userMessage2 = `Given your previous analysis, what would be the most significant technical challenges in making this transition?`;
|
|
103
122
|
conversationHistory.push(new HumanMessage(userMessage2));
|
|
104
|
-
|
|
123
|
+
|
|
105
124
|
console.log('Running second query with thinking enabled...');
|
|
106
125
|
const secondInputs = { messages: [...conversationHistory] };
|
|
107
126
|
await run.processStream(secondInputs, config);
|
|
108
|
-
|
|
127
|
+
|
|
109
128
|
// Display thinking blocks for second response
|
|
110
129
|
const finalMessages2 = run.getRunMessages();
|
|
111
130
|
|
|
112
131
|
// Test 3: Redacted thinking mode
|
|
113
132
|
console.log('\n\nTest 3: Redacted thinking mode');
|
|
114
|
-
const magicString =
|
|
133
|
+
const magicString =
|
|
134
|
+
'ANTHROPIC_MAGIC_STRING_TRIGGER_REDACTED_THINKING_46C9A13E193C177646C7398A98432ECCCE4C1253D5E2D82641AC0E52CC2876CB';
|
|
115
135
|
const userMessage3 = `${magicString}\n\nExplain how quantum computing works in simple terms.`;
|
|
116
|
-
|
|
136
|
+
|
|
117
137
|
// Reset conversation for clean test
|
|
118
138
|
conversationHistory.length = 0;
|
|
119
139
|
conversationHistory.push(new HumanMessage(userMessage3));
|
|
120
|
-
|
|
140
|
+
|
|
121
141
|
console.log('Running query with redacted thinking...');
|
|
122
142
|
const thirdInputs = { messages: [...conversationHistory] };
|
|
123
143
|
await run.processStream(thirdInputs, config);
|
|
124
|
-
|
|
144
|
+
|
|
125
145
|
// Display redacted thinking blocks
|
|
126
146
|
const finalMessages3 = run.getRunMessages();
|
|
127
147
|
console.log('\n\nThinking feature test completed!');
|
|
148
|
+
console.dir(finalMessages3, { depth: null });
|
|
128
149
|
}
|
|
129
150
|
|
|
130
151
|
process.on('unhandledRejection', (reason, promise) => {
|
|
@@ -147,4 +168,4 @@ testThinking().catch((err) => {
|
|
|
147
168
|
console.log('Content parts:');
|
|
148
169
|
console.dir(_contentParts, { depth: null });
|
|
149
170
|
process.exit(1);
|
|
150
|
-
});
|
|
171
|
+
});
|
package/src/scripts/tools.ts
CHANGED
|
@@ -18,9 +18,13 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
18
18
|
const { userName, location, provider, currentDate } = await getArgs();
|
|
19
19
|
const { contentParts, aggregateContent } = createContentAggregator();
|
|
20
20
|
const customHandlers = {
|
|
21
|
-
[GraphEvents.TOOL_END]: new ToolEndHandler(
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(
|
|
22
|
+
undefined,
|
|
23
|
+
undefined,
|
|
24
|
+
(name?: string) => {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
),
|
|
24
28
|
[GraphEvents.CHAT_MODEL_END]: {
|
|
25
29
|
handle: (
|
|
26
30
|
_event: string,
|