@librechat/agents 3.0.776 → 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/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/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 +5 -2
- 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/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/stream.ts +4 -2
- package/src/tools/ToolNode.ts +9 -5
- package/src/types/tools.ts +2 -0
|
@@ -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,
|
package/src/stream.ts
CHANGED
|
@@ -339,7 +339,8 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
339
339
|
(c) =>
|
|
340
340
|
(c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
|
|
341
341
|
(c.type?.startsWith(ContentTypes.REASONING) ?? false) ||
|
|
342
|
-
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
342
|
+
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false) ||
|
|
343
|
+
c.type === 'redacted_thinking'
|
|
343
344
|
)
|
|
344
345
|
) {
|
|
345
346
|
await graph.dispatchReasoningDelta(stepId, {
|
|
@@ -365,7 +366,8 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
365
366
|
Array.isArray(chunk.content) &&
|
|
366
367
|
(chunk.content[0]?.type === ContentTypes.THINKING ||
|
|
367
368
|
chunk.content[0]?.type === ContentTypes.REASONING ||
|
|
368
|
-
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT
|
|
369
|
+
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT ||
|
|
370
|
+
chunk.content[0]?.type === 'redacted_thinking')
|
|
369
371
|
) {
|
|
370
372
|
reasoning_content = 'valid';
|
|
371
373
|
} else if (
|
package/src/tools/ToolNode.ts
CHANGED
|
@@ -145,9 +145,9 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
147
|
* Inject session context for code execution tools when available.
|
|
148
|
+
* Each file uses its own session_id (supporting multi-session file tracking).
|
|
148
149
|
* Both session_id and _injected_files are injected directly to invokeParams
|
|
149
150
|
* (not inside args) so they bypass Zod schema validation and reach config.toolCall.
|
|
150
|
-
* This avoids /files endpoint race conditions.
|
|
151
151
|
*/
|
|
152
152
|
if (
|
|
153
153
|
call.name === Constants.EXECUTE_CODE ||
|
|
@@ -156,14 +156,18 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
156
156
|
const codeSession = this.sessions?.get(Constants.EXECUTE_CODE) as
|
|
157
157
|
| t.CodeSessionContext
|
|
158
158
|
| undefined;
|
|
159
|
-
if (codeSession?.
|
|
160
|
-
/**
|
|
159
|
+
if (codeSession?.files != null && codeSession.files.length > 0) {
|
|
160
|
+
/**
|
|
161
|
+
* Convert tracked files to CodeEnvFile format for the API.
|
|
162
|
+
* Each file uses its own session_id (set when file was created).
|
|
163
|
+
* This supports files from multiple parallel/sequential executions.
|
|
164
|
+
*/
|
|
161
165
|
const fileRefs: t.CodeEnvFile[] = codeSession.files.map((file) => ({
|
|
162
|
-
session_id: codeSession.session_id,
|
|
166
|
+
session_id: file.session_id ?? codeSession.session_id,
|
|
163
167
|
id: file.id,
|
|
164
168
|
name: file.name,
|
|
165
169
|
}));
|
|
166
|
-
/** Inject session_id and files
|
|
170
|
+
/** Inject latest session_id and files - bypasses Zod, reaches config.toolCall */
|
|
167
171
|
invokeParams = {
|
|
168
172
|
...invokeParams,
|
|
169
173
|
session_id: codeSession.session_id,
|