@librechat/agents 3.0.81 → 3.0.772
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 +5 -19
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +25 -98
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +28 -2
- 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 +2 -4
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +5 -9
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +5 -19
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +24 -97
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +28 -2
- 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 +2 -4
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +5 -9
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/types/llm/bedrock/index.d.ts +7 -86
- package/dist/types/types/tools.d.ts +0 -2
- package/package.json +3 -6
- package/src/graphs/Graph.ts +5 -23
- package/src/llm/bedrock/index.ts +43 -180
- package/src/messages/cache.test.ts +44 -0
- package/src/messages/cache.ts +34 -2
- package/src/messages/core.ts +1 -1
- package/src/scripts/thinking.ts +18 -39
- package/src/scripts/tools.ts +3 -7
- package/src/stream.ts +2 -4
- package/src/tools/ToolNode.ts +5 -9
- package/src/types/tools.ts +0 -2
- package/dist/types/llm/bedrock/types.d.ts +0 -27
- package/dist/types/llm/bedrock/utils/index.d.ts +0 -5
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +0 -31
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +0 -33
- package/src/llm/bedrock/llm.spec.ts +0 -616
- package/src/llm/bedrock/types.ts +0 -51
- package/src/llm/bedrock/utils/index.ts +0 -18
- package/src/llm/bedrock/utils/message_inputs.ts +0 -563
- package/src/llm/bedrock/utils/message_outputs.ts +0 -310
- package/src/scripts/code_exec_multi_session.ts +0 -241
- package/src/scripts/thinking-bedrock.ts +0 -159
package/src/llm/bedrock/index.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
|
|
3
|
-
* and adds support for latest @langchain/aws features:
|
|
4
|
-
*
|
|
5
|
-
* - Application Inference Profiles (PR #9129)
|
|
6
|
-
* - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
|
|
7
3
|
*
|
|
8
4
|
* Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
|
|
9
5
|
* causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
|
|
@@ -16,74 +12,15 @@
|
|
|
16
12
|
*/
|
|
17
13
|
|
|
18
14
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
19
|
-
import { AIMessageChunk } from '@langchain/core/messages';
|
|
20
|
-
import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
|
|
21
|
-
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
22
15
|
import type { ChatBedrockConverseInput } from '@langchain/aws';
|
|
16
|
+
import { AIMessageChunk } from '@langchain/core/messages';
|
|
23
17
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* Service tier type for Bedrock invocations.
|
|
27
|
-
* Requires AWS SDK >= 3.966.0 to actually work.
|
|
28
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
29
|
-
*/
|
|
30
|
-
export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Extended input interface with additional features:
|
|
34
|
-
* - applicationInferenceProfile: Use an inference profile ARN instead of model ID
|
|
35
|
-
* - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
|
|
36
|
-
*/
|
|
37
|
-
export interface CustomChatBedrockConverseInput
|
|
38
|
-
extends ChatBedrockConverseInput {
|
|
39
|
-
/**
|
|
40
|
-
* Application Inference Profile ARN to use for the model.
|
|
41
|
-
* For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
|
|
42
|
-
* When provided, this ARN will be used for the actual inference calls instead of the model ID.
|
|
43
|
-
* Must still provide `model` as normal modelId to benefit from all the metadata.
|
|
44
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
|
|
45
|
-
*/
|
|
46
|
-
applicationInferenceProfile?: string;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Service tier for model invocation.
|
|
50
|
-
* Specifies the processing tier type used for serving the request.
|
|
51
|
-
* Supported values are 'priority', 'default', 'flex', and 'reserved'.
|
|
52
|
-
*
|
|
53
|
-
* - 'priority': Prioritized processing for lower latency
|
|
54
|
-
* - 'default': Standard processing tier
|
|
55
|
-
* - 'flex': Flexible processing tier with lower cost
|
|
56
|
-
* - 'reserved': Reserved capacity for consistent performance
|
|
57
|
-
*
|
|
58
|
-
* If not provided, AWS uses the default tier.
|
|
59
|
-
* Note: Requires AWS SDK >= 3.966.0 to work.
|
|
60
|
-
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
|
|
61
|
-
*/
|
|
62
|
-
serviceTier?: ServiceTierType;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Extended call options with serviceTier override support.
|
|
67
|
-
*/
|
|
68
|
-
export interface CustomChatBedrockConverseCallOptions {
|
|
69
|
-
serviceTier?: ServiceTierType;
|
|
70
|
-
}
|
|
18
|
+
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
19
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
71
20
|
|
|
72
21
|
export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
73
|
-
|
|
74
|
-
* Application Inference Profile ARN to use instead of model ID.
|
|
75
|
-
*/
|
|
76
|
-
applicationInferenceProfile?: string;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Service tier for model invocation.
|
|
80
|
-
*/
|
|
81
|
-
serviceTier?: ServiceTierType;
|
|
82
|
-
|
|
83
|
-
constructor(fields?: CustomChatBedrockConverseInput) {
|
|
22
|
+
constructor(fields?: ChatBedrockConverseInput) {
|
|
84
23
|
super(fields);
|
|
85
|
-
this.applicationInferenceProfile = fields?.applicationInferenceProfile;
|
|
86
|
-
this.serviceTier = fields?.serviceTier;
|
|
87
24
|
}
|
|
88
25
|
|
|
89
26
|
static lc_name(): string {
|
|
@@ -91,126 +28,52 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
91
28
|
}
|
|
92
29
|
|
|
93
30
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
|
|
97
|
-
protected getModelId(): string {
|
|
98
|
-
return this.applicationInferenceProfile ?? this.model;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Override invocationParams to add serviceTier support.
|
|
103
|
-
*/
|
|
104
|
-
override invocationParams(
|
|
105
|
-
options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions
|
|
106
|
-
): ReturnType<ChatBedrockConverse['invocationParams']> & {
|
|
107
|
-
serviceTier?: { type: ServiceTierType };
|
|
108
|
-
} {
|
|
109
|
-
const baseParams = super.invocationParams(options);
|
|
110
|
-
|
|
111
|
-
/** Service tier from options or fall back to class-level setting */
|
|
112
|
-
const serviceTierType = options?.serviceTier ?? this.serviceTier;
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
...baseParams,
|
|
116
|
-
serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Override _generateNonStreaming to use applicationInferenceProfile as modelId.
|
|
122
|
-
* Uses the same model-swapping pattern as streaming for consistency.
|
|
31
|
+
* Override _streamResponseChunks to strip contentBlockIndex from response_metadata
|
|
32
|
+
* This prevents LangChain's merge conflicts when the same index is used for
|
|
33
|
+
* different content types (text vs tool calls)
|
|
123
34
|
*/
|
|
124
|
-
|
|
35
|
+
async *_streamResponseChunks(
|
|
125
36
|
messages: BaseMessage[],
|
|
126
|
-
options: this['ParsedCallOptions']
|
|
127
|
-
runManager?: CallbackManagerForLLMRun
|
|
128
|
-
): Promise<ChatResult> {
|
|
129
|
-
// Temporarily swap model for applicationInferenceProfile support
|
|
130
|
-
const originalModel = this.model;
|
|
131
|
-
if (
|
|
132
|
-
this.applicationInferenceProfile != null &&
|
|
133
|
-
this.applicationInferenceProfile !== ''
|
|
134
|
-
) {
|
|
135
|
-
this.model = this.applicationInferenceProfile;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
try {
|
|
139
|
-
return await super._generateNonStreaming(messages, options, runManager);
|
|
140
|
-
} finally {
|
|
141
|
-
// Restore original model
|
|
142
|
-
this.model = originalModel;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Override _streamResponseChunks to:
|
|
148
|
-
* 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
|
|
149
|
-
* 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
|
|
150
|
-
*
|
|
151
|
-
* Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
|
|
152
|
-
* internal chunk handling which correctly preserves array content for reasoning blocks.
|
|
153
|
-
*/
|
|
154
|
-
override async *_streamResponseChunks(
|
|
155
|
-
messages: BaseMessage[],
|
|
156
|
-
options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
|
|
37
|
+
options: this['ParsedCallOptions'],
|
|
157
38
|
runManager?: CallbackManagerForLLMRun
|
|
158
39
|
): AsyncGenerator<ChatGenerationChunk> {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
40
|
+
const baseStream = super._streamResponseChunks(
|
|
41
|
+
messages,
|
|
42
|
+
options,
|
|
43
|
+
runManager
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
for await (const chunk of baseStream) {
|
|
47
|
+
// Only process if we have response_metadata
|
|
48
|
+
if (
|
|
49
|
+
chunk.message instanceof AIMessageChunk &&
|
|
50
|
+
(chunk.message as Partial<AIMessageChunk>).response_metadata &&
|
|
51
|
+
typeof chunk.message.response_metadata === 'object'
|
|
52
|
+
) {
|
|
53
|
+
// Check if contentBlockIndex exists anywhere in response_metadata (top level or nested)
|
|
54
|
+
const hasContentBlockIndex = this.hasContentBlockIndex(
|
|
55
|
+
chunk.message.response_metadata
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (hasContentBlockIndex) {
|
|
59
|
+
const cleanedMetadata = this.removeContentBlockIndex(
|
|
60
|
+
chunk.message.response_metadata
|
|
61
|
+
) as Record<string, unknown>;
|
|
62
|
+
|
|
63
|
+
yield new ChatGenerationChunk({
|
|
64
|
+
text: chunk.text,
|
|
65
|
+
message: new AIMessageChunk({
|
|
66
|
+
...chunk.message,
|
|
67
|
+
response_metadata: cleanedMetadata,
|
|
68
|
+
}),
|
|
69
|
+
generationInfo: chunk.generationInfo,
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
179
73
|
}
|
|
180
|
-
} finally {
|
|
181
|
-
// Restore original model
|
|
182
|
-
this.model = originalModel;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
74
|
|
|
186
|
-
|
|
187
|
-
* Clean a chunk by removing contentBlockIndex from response_metadata.
|
|
188
|
-
*/
|
|
189
|
-
private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
|
|
190
|
-
const message = chunk.message;
|
|
191
|
-
if (!(message instanceof AIMessageChunk)) {
|
|
192
|
-
return chunk;
|
|
75
|
+
yield chunk;
|
|
193
76
|
}
|
|
194
|
-
|
|
195
|
-
const metadata = message.response_metadata as Record<string, unknown>;
|
|
196
|
-
const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
|
|
197
|
-
if (!hasContentBlockIndex) {
|
|
198
|
-
return chunk;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
|
|
202
|
-
string,
|
|
203
|
-
unknown
|
|
204
|
-
>;
|
|
205
|
-
|
|
206
|
-
return new ChatGenerationChunk({
|
|
207
|
-
text: chunk.text,
|
|
208
|
-
message: new AIMessageChunk({
|
|
209
|
-
...message,
|
|
210
|
-
response_metadata: cleanedMetadata,
|
|
211
|
-
}),
|
|
212
|
-
generationInfo: chunk.generationInfo,
|
|
213
|
-
});
|
|
214
77
|
}
|
|
215
78
|
|
|
216
79
|
/**
|
|
@@ -1048,6 +1048,50 @@ describe('Immutability - addBedrockCacheControl does not mutate original message
|
|
|
1048
1048
|
expect(anthropicFirstContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
1049
1049
|
expect('cache_control' in anthropicFirstContent[0]).toBe(true);
|
|
1050
1050
|
});
|
|
1051
|
+
|
|
1052
|
+
it('should keep lc_kwargs.content in sync with content for LangChain messages', () => {
|
|
1053
|
+
type LangChainLikeMsg = TestMsg & {
|
|
1054
|
+
lc_kwargs?: { content?: MessageContentComplex[] };
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
const messagesWithLcKwargs: LangChainLikeMsg[] = [
|
|
1058
|
+
{
|
|
1059
|
+
role: 'user',
|
|
1060
|
+
content: [{ type: ContentTypes.TEXT, text: 'User message' }],
|
|
1061
|
+
lc_kwargs: {
|
|
1062
|
+
content: [{ type: ContentTypes.TEXT, text: 'User message' }],
|
|
1063
|
+
},
|
|
1064
|
+
},
|
|
1065
|
+
{
|
|
1066
|
+
role: 'assistant',
|
|
1067
|
+
content: [{ type: ContentTypes.TEXT, text: 'Assistant response' }],
|
|
1068
|
+
lc_kwargs: {
|
|
1069
|
+
content: [{ type: ContentTypes.TEXT, text: 'Assistant response' }],
|
|
1070
|
+
},
|
|
1071
|
+
},
|
|
1072
|
+
];
|
|
1073
|
+
|
|
1074
|
+
const bedrockResult = addBedrockCacheControl(messagesWithLcKwargs);
|
|
1075
|
+
|
|
1076
|
+
const resultFirst = bedrockResult[0] as LangChainLikeMsg;
|
|
1077
|
+
const resultSecond = bedrockResult[1] as LangChainLikeMsg;
|
|
1078
|
+
|
|
1079
|
+
expect(resultFirst.content).toEqual(resultFirst.lc_kwargs?.content);
|
|
1080
|
+
expect(resultSecond.content).toEqual(resultSecond.lc_kwargs?.content);
|
|
1081
|
+
|
|
1082
|
+
const firstContent = resultFirst.content as MessageContentComplex[];
|
|
1083
|
+
const firstLcContent = resultFirst.lc_kwargs
|
|
1084
|
+
?.content as MessageContentComplex[];
|
|
1085
|
+
expect(firstContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
1086
|
+
expect(firstLcContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
1087
|
+
|
|
1088
|
+
const originalFirst = messagesWithLcKwargs[0];
|
|
1089
|
+
const originalContent = originalFirst.content as MessageContentComplex[];
|
|
1090
|
+
const originalLcContent = originalFirst.lc_kwargs
|
|
1091
|
+
?.content as MessageContentComplex[];
|
|
1092
|
+
expect(originalContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
1093
|
+
expect(originalLcContent.some((b) => 'cachePoint' in b)).toBe(false);
|
|
1094
|
+
});
|
|
1051
1095
|
});
|
|
1052
1096
|
|
|
1053
1097
|
describe('Multi-turn cache cleanup', () => {
|
package/src/messages/cache.ts
CHANGED
|
@@ -31,10 +31,34 @@ function cloneMessageWithContent<T extends MessageWithContent>(message: T): T {
|
|
|
31
31
|
if (message.content === undefined) {
|
|
32
32
|
return { ...message };
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
|
|
35
|
+
const clonedContent = deepCloneContent(message.content);
|
|
36
|
+
const cloned = {
|
|
35
37
|
...message,
|
|
36
|
-
content:
|
|
38
|
+
content: clonedContent,
|
|
37
39
|
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* LangChain messages store internal state in lc_kwargs.
|
|
43
|
+
* Clone it but don't sync content yet - that happens after all modifications.
|
|
44
|
+
*/
|
|
45
|
+
const lcKwargs = (message as Record<string, unknown>).lc_kwargs;
|
|
46
|
+
if (lcKwargs != null && typeof lcKwargs === 'object') {
|
|
47
|
+
(cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return cloned;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Syncs lc_kwargs.content with the message's content property.
|
|
55
|
+
* Call this after all modifications to ensure LangChain serialization works correctly.
|
|
56
|
+
*/
|
|
57
|
+
function syncLcKwargsContent<T extends MessageWithContent>(message: T): void {
|
|
58
|
+
const lcKwargs = (message as Record<string, unknown>).lc_kwargs;
|
|
59
|
+
if (lcKwargs != null && typeof lcKwargs === 'object') {
|
|
60
|
+
(lcKwargs as Record<string, unknown>).content = message.content;
|
|
61
|
+
}
|
|
38
62
|
}
|
|
39
63
|
|
|
40
64
|
/**
|
|
@@ -114,6 +138,7 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
114
138
|
}
|
|
115
139
|
|
|
116
140
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
141
|
+
syncLcKwargsContent(message);
|
|
117
142
|
continue;
|
|
118
143
|
}
|
|
119
144
|
|
|
@@ -138,6 +163,8 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
138
163
|
}
|
|
139
164
|
}
|
|
140
165
|
}
|
|
166
|
+
|
|
167
|
+
syncLcKwargsContent(message);
|
|
141
168
|
}
|
|
142
169
|
|
|
143
170
|
return updatedMessages;
|
|
@@ -314,6 +341,7 @@ export function addBedrockCacheControl<
|
|
|
314
341
|
}
|
|
315
342
|
|
|
316
343
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
344
|
+
syncLcKwargsContent(message);
|
|
317
345
|
continue;
|
|
318
346
|
}
|
|
319
347
|
|
|
@@ -323,6 +351,7 @@ export function addBedrockCacheControl<
|
|
|
323
351
|
{ cachePoint: { type: 'default' } },
|
|
324
352
|
] as MessageContentComplex[];
|
|
325
353
|
messagesModified++;
|
|
354
|
+
syncLcKwargsContent(message);
|
|
326
355
|
continue;
|
|
327
356
|
}
|
|
328
357
|
|
|
@@ -338,6 +367,7 @@ export function addBedrockCacheControl<
|
|
|
338
367
|
}
|
|
339
368
|
|
|
340
369
|
if (!hasCacheableContent) {
|
|
370
|
+
syncLcKwargsContent(message);
|
|
341
371
|
continue;
|
|
342
372
|
}
|
|
343
373
|
|
|
@@ -364,6 +394,8 @@ export function addBedrockCacheControl<
|
|
|
364
394
|
}
|
|
365
395
|
messagesModified++;
|
|
366
396
|
}
|
|
397
|
+
|
|
398
|
+
syncLcKwargsContent(message);
|
|
367
399
|
}
|
|
368
400
|
|
|
369
401
|
return updatedMessages;
|
package/src/messages/core.ts
CHANGED
|
@@ -41,7 +41,7 @@ User: ${userMessage[1]}
|
|
|
41
41
|
const _allowedTypes = ['image_url', 'text', 'tool_use', 'tool_result'];
|
|
42
42
|
const allowedTypesByProvider: Record<string, string[]> = {
|
|
43
43
|
default: _allowedTypes,
|
|
44
|
-
[Providers.ANTHROPIC]: [..._allowedTypes, 'thinking'
|
|
44
|
+
[Providers.ANTHROPIC]: [..._allowedTypes, 'thinking'],
|
|
45
45
|
[Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],
|
|
46
46
|
[Providers.OPENAI]: _allowedTypes,
|
|
47
47
|
};
|
package/src/scripts/thinking.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
// src/scripts/test-thinking.ts
|
|
2
2
|
import { config } from 'dotenv';
|
|
3
3
|
config();
|
|
4
|
-
import {
|
|
5
|
-
HumanMessage,
|
|
6
|
-
SystemMessage,
|
|
7
|
-
BaseMessage,
|
|
8
|
-
} from '@langchain/core/messages';
|
|
4
|
+
import { HumanMessage, SystemMessage, BaseMessage } from '@langchain/core/messages';
|
|
9
5
|
import type { UsageMetadata } from '@langchain/core/messages';
|
|
10
6
|
import * as t from '@/types';
|
|
11
7
|
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
@@ -25,23 +21,17 @@ async function testThinking(): Promise<void> {
|
|
|
25
21
|
const instructions = `You are a helpful AI assistant for ${userName}. When answering questions, be thorough in your reasoning.`;
|
|
26
22
|
const { contentParts, aggregateContent } = createContentAggregator();
|
|
27
23
|
_contentParts = contentParts as t.MessageContentComplex[];
|
|
28
|
-
|
|
24
|
+
|
|
29
25
|
// Set up event handlers
|
|
30
26
|
const customHandlers = {
|
|
31
27
|
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
32
28
|
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(collectedUsage),
|
|
33
29
|
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
34
30
|
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
35
|
-
handle: (
|
|
36
|
-
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
37
|
-
data: t.StreamEventData
|
|
38
|
-
): void => {
|
|
31
|
+
handle: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData): void => {
|
|
39
32
|
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
40
|
-
aggregateContent({
|
|
41
|
-
|
|
42
|
-
data: data as unknown as { result: t.ToolEndEvent },
|
|
43
|
-
});
|
|
44
|
-
},
|
|
33
|
+
aggregateContent({ event, data: data as unknown as { result: t.ToolEndEvent } });
|
|
34
|
+
}
|
|
45
35
|
},
|
|
46
36
|
[GraphEvents.ON_RUN_STEP]: {
|
|
47
37
|
handle: (event: GraphEvents.ON_RUN_STEP, data: t.RunStep) => {
|
|
@@ -49,38 +39,29 @@ async function testThinking(): Promise<void> {
|
|
|
49
39
|
},
|
|
50
40
|
},
|
|
51
41
|
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
52
|
-
handle: (
|
|
53
|
-
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
54
|
-
data: t.RunStepDeltaEvent
|
|
55
|
-
) => {
|
|
42
|
+
handle: (event: GraphEvents.ON_RUN_STEP_DELTA, data: t.RunStepDeltaEvent) => {
|
|
56
43
|
aggregateContent({ event, data });
|
|
57
44
|
},
|
|
58
45
|
},
|
|
59
46
|
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
60
|
-
handle: (
|
|
61
|
-
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
62
|
-
data: t.MessageDeltaEvent
|
|
63
|
-
) => {
|
|
47
|
+
handle: (event: GraphEvents.ON_MESSAGE_DELTA, data: t.MessageDeltaEvent) => {
|
|
64
48
|
aggregateContent({ event, data });
|
|
65
49
|
},
|
|
66
50
|
},
|
|
67
51
|
[GraphEvents.ON_REASONING_DELTA]: {
|
|
68
|
-
handle: (
|
|
69
|
-
event: GraphEvents.ON_REASONING_DELTA,
|
|
70
|
-
data: t.ReasoningDeltaEvent
|
|
71
|
-
) => {
|
|
52
|
+
handle: (event: GraphEvents.ON_REASONING_DELTA, data: t.ReasoningDeltaEvent) => {
|
|
72
53
|
aggregateContent({ event, data });
|
|
73
54
|
},
|
|
74
55
|
},
|
|
75
56
|
};
|
|
76
57
|
|
|
77
58
|
const baseLlmConfig: t.LLMConfig = getLLMConfig(Providers.ANTHROPIC);
|
|
78
|
-
|
|
59
|
+
|
|
79
60
|
// Enable thinking with token budget
|
|
80
61
|
const llmConfig = {
|
|
81
62
|
...baseLlmConfig,
|
|
82
63
|
model: 'claude-3-7-sonnet-latest',
|
|
83
|
-
thinking: { type:
|
|
64
|
+
thinking: { type: "enabled", budget_tokens: 2000 }
|
|
84
65
|
};
|
|
85
66
|
|
|
86
67
|
const run = await Run.create<t.IState>({
|
|
@@ -112,7 +93,7 @@ async function testThinking(): Promise<void> {
|
|
|
112
93
|
console.log('Running first query with thinking enabled...');
|
|
113
94
|
const firstInputs = { messages: [...conversationHistory] };
|
|
114
95
|
await run.processStream(firstInputs, config);
|
|
115
|
-
|
|
96
|
+
|
|
116
97
|
// Extract and display thinking blocks
|
|
117
98
|
const finalMessages = run.getRunMessages();
|
|
118
99
|
|
|
@@ -120,32 +101,30 @@ async function testThinking(): Promise<void> {
|
|
|
120
101
|
console.log('\n\nTest 2: Multi-turn conversation with thinking enabled');
|
|
121
102
|
const userMessage2 = `Given your previous analysis, what would be the most significant technical challenges in making this transition?`;
|
|
122
103
|
conversationHistory.push(new HumanMessage(userMessage2));
|
|
123
|
-
|
|
104
|
+
|
|
124
105
|
console.log('Running second query with thinking enabled...');
|
|
125
106
|
const secondInputs = { messages: [...conversationHistory] };
|
|
126
107
|
await run.processStream(secondInputs, config);
|
|
127
|
-
|
|
108
|
+
|
|
128
109
|
// Display thinking blocks for second response
|
|
129
110
|
const finalMessages2 = run.getRunMessages();
|
|
130
111
|
|
|
131
112
|
// Test 3: Redacted thinking mode
|
|
132
113
|
console.log('\n\nTest 3: Redacted thinking mode');
|
|
133
|
-
const magicString =
|
|
134
|
-
'ANTHROPIC_MAGIC_STRING_TRIGGER_REDACTED_THINKING_46C9A13E193C177646C7398A98432ECCCE4C1253D5E2D82641AC0E52CC2876CB';
|
|
114
|
+
const magicString = "ANTHROPIC_MAGIC_STRING_TRIGGER_REDACTED_THINKING_46C9A13E193C177646C7398A98432ECCCE4C1253D5E2D82641AC0E52CC2876CB";
|
|
135
115
|
const userMessage3 = `${magicString}\n\nExplain how quantum computing works in simple terms.`;
|
|
136
|
-
|
|
116
|
+
|
|
137
117
|
// Reset conversation for clean test
|
|
138
118
|
conversationHistory.length = 0;
|
|
139
119
|
conversationHistory.push(new HumanMessage(userMessage3));
|
|
140
|
-
|
|
120
|
+
|
|
141
121
|
console.log('Running query with redacted thinking...');
|
|
142
122
|
const thirdInputs = { messages: [...conversationHistory] };
|
|
143
123
|
await run.processStream(thirdInputs, config);
|
|
144
|
-
|
|
124
|
+
|
|
145
125
|
// Display redacted thinking blocks
|
|
146
126
|
const finalMessages3 = run.getRunMessages();
|
|
147
127
|
console.log('\n\nThinking feature test completed!');
|
|
148
|
-
console.dir(finalMessages3, { depth: null });
|
|
149
128
|
}
|
|
150
129
|
|
|
151
130
|
process.on('unhandledRejection', (reason, promise) => {
|
|
@@ -168,4 +147,4 @@ testThinking().catch((err) => {
|
|
|
168
147
|
console.log('Content parts:');
|
|
169
148
|
console.dir(_contentParts, { depth: null });
|
|
170
149
|
process.exit(1);
|
|
171
|
-
});
|
|
150
|
+
});
|
package/src/scripts/tools.ts
CHANGED
|
@@ -18,13 +18,9 @@ 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
|
-
|
|
24
|
-
(name?: string) => {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
),
|
|
21
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(undefined, (name?: string) => {
|
|
22
|
+
return true;
|
|
23
|
+
}),
|
|
28
24
|
[GraphEvents.CHAT_MODEL_END]: {
|
|
29
25
|
handle: (
|
|
30
26
|
_event: string,
|
package/src/stream.ts
CHANGED
|
@@ -339,8 +339,7 @@ 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)
|
|
343
|
-
c.type === 'redacted_thinking'
|
|
342
|
+
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
344
343
|
)
|
|
345
344
|
) {
|
|
346
345
|
await graph.dispatchReasoningDelta(stepId, {
|
|
@@ -366,8 +365,7 @@ hasToolCallChunks: ${hasToolCallChunks}
|
|
|
366
365
|
Array.isArray(chunk.content) &&
|
|
367
366
|
(chunk.content[0]?.type === ContentTypes.THINKING ||
|
|
368
367
|
chunk.content[0]?.type === ContentTypes.REASONING ||
|
|
369
|
-
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT
|
|
370
|
-
chunk.content[0]?.type === 'redacted_thinking')
|
|
368
|
+
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)
|
|
371
369
|
) {
|
|
372
370
|
reasoning_content = 'valid';
|
|
373
371
|
} 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).
|
|
149
148
|
* Both session_id and _injected_files are injected directly to invokeParams
|
|
150
149
|
* (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,18 +156,14 @@ 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
|
-
/**
|
|
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
|
-
*/
|
|
159
|
+
if (codeSession?.session_id != null && codeSession.files.length > 0) {
|
|
160
|
+
/** Convert tracked files to CodeEnvFile format for the API */
|
|
165
161
|
const fileRefs: t.CodeEnvFile[] = codeSession.files.map((file) => ({
|
|
166
|
-
session_id:
|
|
162
|
+
session_id: codeSession.session_id,
|
|
167
163
|
id: file.id,
|
|
168
164
|
name: file.name,
|
|
169
165
|
}));
|
|
170
|
-
/** Inject
|
|
166
|
+
/** Inject session_id and files directly - bypasses Zod, reaches config.toolCall */
|
|
171
167
|
invokeParams = {
|
|
172
168
|
...invokeParams,
|
|
173
169
|
session_id: codeSession.session_id,
|
package/src/types/tools.ts
CHANGED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type definitions for Bedrock Converse utilities.
|
|
3
|
-
*/
|
|
4
|
-
import type { Message as BedrockMessage, SystemContentBlock as BedrockSystemContentBlock, ContentBlock as BedrockContentBlock, ConverseResponse, ContentBlockDeltaEvent, ConverseStreamMetadataEvent, ContentBlockStartEvent, ReasoningContentBlock, ReasoningContentBlockDelta } from '@aws-sdk/client-bedrock-runtime';
|
|
5
|
-
/**
|
|
6
|
-
* Reasoning content block type for LangChain messages.
|
|
7
|
-
*/
|
|
8
|
-
export interface MessageContentReasoningBlock {
|
|
9
|
-
type: 'reasoning_content';
|
|
10
|
-
reasoningText?: {
|
|
11
|
-
text?: string;
|
|
12
|
-
signature?: string;
|
|
13
|
-
};
|
|
14
|
-
redactedContent?: string;
|
|
15
|
-
}
|
|
16
|
-
export interface MessageContentReasoningBlockReasoningTextPartial {
|
|
17
|
-
type: 'reasoning_content';
|
|
18
|
-
reasoningText: {
|
|
19
|
-
text?: string;
|
|
20
|
-
signature?: string;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export interface MessageContentReasoningBlockRedacted {
|
|
24
|
-
type: 'reasoning_content';
|
|
25
|
-
redactedContent: string;
|
|
26
|
-
}
|
|
27
|
-
export type { BedrockMessage, BedrockSystemContentBlock, BedrockContentBlock, ConverseResponse, ContentBlockDeltaEvent, ConverseStreamMetadataEvent, ContentBlockStartEvent, ReasoningContentBlock, ReasoningContentBlockDelta, };
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bedrock Converse utility exports.
|
|
3
|
-
*/
|
|
4
|
-
export { convertToConverseMessages, extractImageInfo, langchainReasoningBlockToBedrockReasoningBlock, concatenateLangchainReasoningBlocks, } from './message_inputs';
|
|
5
|
-
export { convertConverseMessageToLangChainMessage, handleConverseStreamContentBlockStart, handleConverseStreamContentBlockDelta, handleConverseStreamMetadata, bedrockReasoningBlockToLangchainReasoningBlock, bedrockReasoningDeltaToLangchainPartialReasoningBlock, } from './message_outputs';
|