@librechat/agents 3.0.77 → 3.0.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/cjs/graphs/Graph.cjs +19 -5
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/llm/bedrock/index.cjs +135 -23
  4. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  5. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +465 -0
  6. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -0
  7. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +238 -0
  8. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -0
  9. package/dist/cjs/messages/core.cjs +1 -1
  10. package/dist/cjs/messages/core.cjs.map +1 -1
  11. package/dist/cjs/stream.cjs +4 -2
  12. package/dist/cjs/stream.cjs.map +1 -1
  13. package/dist/cjs/tools/ToolNode.cjs +9 -5
  14. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  15. package/dist/esm/graphs/Graph.mjs +19 -5
  16. package/dist/esm/graphs/Graph.mjs.map +1 -1
  17. package/dist/esm/llm/bedrock/index.mjs +134 -22
  18. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  19. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +460 -0
  20. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -0
  21. package/dist/esm/llm/bedrock/utils/message_outputs.mjs +231 -0
  22. package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -0
  23. package/dist/esm/messages/core.mjs +1 -1
  24. package/dist/esm/messages/core.mjs.map +1 -1
  25. package/dist/esm/stream.mjs +4 -2
  26. package/dist/esm/stream.mjs.map +1 -1
  27. package/dist/esm/tools/ToolNode.mjs +9 -5
  28. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  29. package/dist/types/llm/bedrock/index.d.ts +83 -7
  30. package/dist/types/llm/bedrock/types.d.ts +27 -0
  31. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  32. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  33. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  34. package/dist/types/types/tools.d.ts +2 -0
  35. package/package.json +5 -3
  36. package/src/graphs/Graph.ts +23 -5
  37. package/src/llm/bedrock/index.ts +232 -41
  38. package/src/llm/bedrock/llm.spec.ts +616 -0
  39. package/src/llm/bedrock/types.ts +51 -0
  40. package/src/llm/bedrock/utils/index.ts +18 -0
  41. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  42. package/src/llm/bedrock/utils/message_outputs.ts +296 -0
  43. package/src/messages/core.ts +1 -1
  44. package/src/scripts/code_exec_multi_session.ts +241 -0
  45. package/src/scripts/thinking.ts +39 -18
  46. package/src/scripts/tools.ts +7 -3
  47. package/src/stream.ts +4 -2
  48. package/src/tools/ToolNode.ts +9 -5
  49. package/src/types/tools.ts +2 -0
@@ -1,5 +1,9 @@
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+
3
7
  *
4
8
  * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
5
9
  * causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
@@ -12,15 +16,85 @@
12
16
  */
13
17
 
14
18
  import { ChatBedrockConverse } from '@langchain/aws';
15
- import type { ChatBedrockConverseInput } from '@langchain/aws';
16
19
  import { AIMessageChunk } from '@langchain/core/messages';
17
- import type { BaseMessage } from '@langchain/core/messages';
18
- import { ChatGenerationChunk } from '@langchain/core/outputs';
20
+ import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
19
21
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
22
+ import type { ChatBedrockConverseInput } from '@langchain/aws';
23
+ import type { BaseMessage } from '@langchain/core/messages';
24
+ import {
25
+ ConverseCommand,
26
+ ConverseStreamCommand,
27
+ } from '@aws-sdk/client-bedrock-runtime';
28
+ import {
29
+ convertToConverseMessages,
30
+ convertConverseMessageToLangChainMessage,
31
+ handleConverseStreamContentBlockStart,
32
+ handleConverseStreamContentBlockDelta,
33
+ handleConverseStreamMetadata,
34
+ } from './utils';
35
+
36
+ /**
37
+ * Service tier type for Bedrock invocations.
38
+ * Requires AWS SDK >= 3.966.0 to actually work.
39
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
40
+ */
41
+ export type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';
42
+
43
+ /**
44
+ * Extended input interface with additional features:
45
+ * - applicationInferenceProfile: Use an inference profile ARN instead of model ID
46
+ * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)
47
+ */
48
+ export interface CustomChatBedrockConverseInput
49
+ extends ChatBedrockConverseInput {
50
+ /**
51
+ * Application Inference Profile ARN to use for the model.
52
+ * For example, "arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx"
53
+ * When provided, this ARN will be used for the actual inference calls instead of the model ID.
54
+ * Must still provide `model` as normal modelId to benefit from all the metadata.
55
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html
56
+ */
57
+ applicationInferenceProfile?: string;
58
+
59
+ /**
60
+ * Service tier for model invocation.
61
+ * Specifies the processing tier type used for serving the request.
62
+ * Supported values are 'priority', 'default', 'flex', and 'reserved'.
63
+ *
64
+ * - 'priority': Prioritized processing for lower latency
65
+ * - 'default': Standard processing tier
66
+ * - 'flex': Flexible processing tier with lower cost
67
+ * - 'reserved': Reserved capacity for consistent performance
68
+ *
69
+ * If not provided, AWS uses the default tier.
70
+ * Note: Requires AWS SDK >= 3.966.0 to work.
71
+ * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html
72
+ */
73
+ serviceTier?: ServiceTierType;
74
+ }
75
+
76
+ /**
77
+ * Extended call options with serviceTier override support.
78
+ */
79
+ export interface CustomChatBedrockConverseCallOptions {
80
+ serviceTier?: ServiceTierType;
81
+ }
20
82
 
21
83
  export class CustomChatBedrockConverse extends ChatBedrockConverse {
22
- constructor(fields?: ChatBedrockConverseInput) {
84
+ /**
85
+ * Application Inference Profile ARN to use instead of model ID.
86
+ */
87
+ applicationInferenceProfile?: string;
88
+
89
+ /**
90
+ * Service tier for model invocation.
91
+ */
92
+ serviceTier?: ServiceTierType;
93
+
94
+ constructor(fields?: CustomChatBedrockConverseInput) {
23
95
  super(fields);
96
+ this.applicationInferenceProfile = fields?.applicationInferenceProfile;
97
+ this.serviceTier = fields?.serviceTier;
24
98
  }
25
99
 
26
100
  static lc_name(): string {
@@ -28,52 +102,169 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
28
102
  }
29
103
 
30
104
  /**
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)
105
+ * Get the model ID to use for API calls.
106
+ * Returns applicationInferenceProfile if set, otherwise returns this.model.
107
+ */
108
+ protected getModelId(): string {
109
+ return this.applicationInferenceProfile ?? this.model;
110
+ }
111
+
112
+ /**
113
+ * Override invocationParams to add serviceTier support.
34
114
  */
35
- async *_streamResponseChunks(
115
+ override invocationParams(
116
+ options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions
117
+ ): ReturnType<ChatBedrockConverse['invocationParams']> & {
118
+ serviceTier?: { type: ServiceTierType };
119
+ } {
120
+ const baseParams = super.invocationParams(options);
121
+
122
+ // Get serviceTier from options or fall back to class-level setting
123
+ const serviceTierType = options?.serviceTier ?? this.serviceTier;
124
+
125
+ return {
126
+ ...baseParams,
127
+ serviceTier: serviceTierType ? { type: serviceTierType } : undefined,
128
+ };
129
+ }
130
+
131
+ /**
132
+ * Override _generateNonStreaming to use applicationInferenceProfile as modelId.
133
+ */
134
+ override async _generateNonStreaming(
36
135
  messages: BaseMessage[],
37
- options: this['ParsedCallOptions'],
136
+ options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
137
+ _runManager?: CallbackManagerForLLMRun
138
+ ): Promise<ChatResult> {
139
+ const { converseMessages, converseSystem } =
140
+ convertToConverseMessages(messages);
141
+ const params = this.invocationParams(options);
142
+
143
+ const command = new ConverseCommand({
144
+ modelId: this.getModelId(),
145
+ messages: converseMessages,
146
+ system: converseSystem,
147
+ requestMetadata: options.requestMetadata,
148
+ ...params,
149
+ });
150
+
151
+ const response = await this.client.send(command, {
152
+ abortSignal: options.signal,
153
+ });
154
+
155
+ const { output, ...responseMetadata } = response;
156
+ if (!output?.message) {
157
+ throw new Error('No message found in Bedrock response.');
158
+ }
159
+
160
+ const message = convertConverseMessageToLangChainMessage(
161
+ output.message,
162
+ responseMetadata
163
+ );
164
+
165
+ return {
166
+ generations: [
167
+ {
168
+ text: typeof message.content === 'string' ? message.content : '',
169
+ message,
170
+ },
171
+ ],
172
+ };
173
+ }
174
+
175
+ /**
176
+ * Override _streamResponseChunks to:
177
+ * 1. Use applicationInferenceProfile as modelId
178
+ * 2. Include serviceTier in request
179
+ * 3. Strip contentBlockIndex from response_metadata to prevent merge conflicts
180
+ */
181
+ override async *_streamResponseChunks(
182
+ messages: BaseMessage[],
183
+ options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
38
184
  runManager?: CallbackManagerForLLMRun
39
185
  ): AsyncGenerator<ChatGenerationChunk> {
40
- const baseStream = super._streamResponseChunks(
41
- messages,
42
- options,
43
- runManager
44
- );
186
+ const { converseMessages, converseSystem } =
187
+ convertToConverseMessages(messages);
188
+ const params = this.invocationParams(options);
189
+
190
+ let { streamUsage } = this;
191
+ if (options.streamUsage !== undefined) {
192
+ streamUsage = options.streamUsage;
193
+ }
194
+
195
+ const command = new ConverseStreamCommand({
196
+ modelId: this.getModelId(),
197
+ messages: converseMessages,
198
+ system: converseSystem,
199
+ requestMetadata: options.requestMetadata,
200
+ ...params,
201
+ });
45
202
 
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;
203
+ const response = await this.client.send(command, {
204
+ abortSignal: options.signal,
205
+ });
206
+
207
+ if (response.stream) {
208
+ for await (const event of response.stream) {
209
+ if (event.contentBlockStart != null) {
210
+ const chunk = handleConverseStreamContentBlockStart(
211
+ event.contentBlockStart
212
+ ) as ChatGenerationChunk | undefined;
213
+ if (chunk !== undefined) {
214
+ const cleanedChunk = this.cleanChunk(chunk);
215
+ yield cleanedChunk;
216
+ await runManager?.handleLLMNewToken(cleanedChunk.text || '');
217
+ }
218
+ } else if (event.contentBlockDelta != null) {
219
+ const chunk = handleConverseStreamContentBlockDelta(
220
+ event.contentBlockDelta
221
+ ) as ChatGenerationChunk | undefined;
222
+ if (chunk !== undefined) {
223
+ const cleanedChunk = this.cleanChunk(chunk);
224
+ yield cleanedChunk;
225
+ await runManager?.handleLLMNewToken(cleanedChunk.text || '');
226
+ }
227
+ } else if (event.metadata != null) {
228
+ const chunk = handleConverseStreamMetadata(event.metadata, {
229
+ streamUsage,
230
+ }) as ChatGenerationChunk | undefined;
231
+ if (chunk !== undefined) {
232
+ const cleanedChunk = this.cleanChunk(chunk);
233
+ yield cleanedChunk;
234
+ }
72
235
  }
73
236
  }
237
+ }
238
+ }
74
239
 
75
- yield chunk;
240
+ /**
241
+ * Clean a chunk by removing contentBlockIndex from response_metadata.
242
+ */
243
+ private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
244
+ const message = chunk.message;
245
+ if (!(message instanceof AIMessageChunk)) {
246
+ return chunk;
76
247
  }
248
+
249
+ const metadata = message.response_metadata as Record<string, unknown>;
250
+ const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
251
+ if (!hasContentBlockIndex) {
252
+ return chunk;
253
+ }
254
+
255
+ const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
256
+ string,
257
+ unknown
258
+ >;
259
+
260
+ return new ChatGenerationChunk({
261
+ text: chunk.text,
262
+ message: new AIMessageChunk({
263
+ ...message,
264
+ response_metadata: cleanedMetadata,
265
+ }),
266
+ generationInfo: chunk.generationInfo,
267
+ });
77
268
  }
78
269
 
79
270
  /**