@librechat/agents 3.0.776 → 3.1.1

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 (108) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +2 -5
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +20 -5
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/graphs/MultiAgentGraph.cjs +26 -17
  6. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  7. package/dist/cjs/llm/bedrock/index.cjs +98 -25
  8. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  9. package/dist/cjs/llm/openai/index.cjs +1 -0
  10. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +3 -0
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/core.cjs +1 -1
  14. package/dist/cjs/messages/core.cjs.map +1 -1
  15. package/dist/cjs/stream.cjs +4 -2
  16. package/dist/cjs/stream.cjs.map +1 -1
  17. package/dist/cjs/tools/CodeExecutor.cjs +37 -27
  18. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  19. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +21 -17
  20. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  21. package/dist/cjs/tools/ToolNode.cjs +10 -5
  22. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  23. package/dist/cjs/tools/ToolSearch.cjs +37 -30
  24. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  25. package/dist/cjs/tools/search/schema.cjs +25 -23
  26. package/dist/cjs/tools/search/schema.cjs.map +1 -1
  27. package/dist/cjs/tools/search/tool.cjs +9 -33
  28. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  29. package/dist/cjs/utils/schema.cjs +27 -0
  30. package/dist/cjs/utils/schema.cjs.map +1 -0
  31. package/dist/cjs/utils/title.cjs +28 -14
  32. package/dist/cjs/utils/title.cjs.map +1 -1
  33. package/dist/esm/agents/AgentContext.mjs +2 -5
  34. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  35. package/dist/esm/graphs/Graph.mjs +20 -5
  36. package/dist/esm/graphs/Graph.mjs.map +1 -1
  37. package/dist/esm/graphs/MultiAgentGraph.mjs +26 -17
  38. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  39. package/dist/esm/llm/bedrock/index.mjs +97 -24
  40. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  41. package/dist/esm/llm/openai/index.mjs +1 -0
  42. package/dist/esm/llm/openai/index.mjs.map +1 -1
  43. package/dist/esm/main.mjs +1 -0
  44. package/dist/esm/main.mjs.map +1 -1
  45. package/dist/esm/messages/core.mjs +1 -1
  46. package/dist/esm/messages/core.mjs.map +1 -1
  47. package/dist/esm/stream.mjs +4 -2
  48. package/dist/esm/stream.mjs.map +1 -1
  49. package/dist/esm/tools/CodeExecutor.mjs +37 -27
  50. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  51. package/dist/esm/tools/ProgrammaticToolCalling.mjs +21 -17
  52. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  53. package/dist/esm/tools/ToolNode.mjs +10 -5
  54. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  55. package/dist/esm/tools/ToolSearch.mjs +37 -30
  56. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  57. package/dist/esm/tools/search/schema.mjs +25 -23
  58. package/dist/esm/tools/search/schema.mjs.map +1 -1
  59. package/dist/esm/tools/search/tool.mjs +10 -34
  60. package/dist/esm/tools/search/tool.mjs.map +1 -1
  61. package/dist/esm/utils/schema.mjs +24 -0
  62. package/dist/esm/utils/schema.mjs.map +1 -0
  63. package/dist/esm/utils/title.mjs +28 -14
  64. package/dist/esm/utils/title.mjs.map +1 -1
  65. package/dist/types/llm/bedrock/index.d.ts +86 -7
  66. package/dist/types/llm/bedrock/types.d.ts +27 -0
  67. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  68. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  69. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  70. package/dist/types/tools/CodeExecutor.d.ts +1 -15
  71. package/dist/types/tools/ProgrammaticToolCalling.d.ts +1 -13
  72. package/dist/types/tools/ToolSearch.d.ts +1 -15
  73. package/dist/types/tools/search/schema.d.ts +25 -7
  74. package/dist/types/tools/search/tool.d.ts +1 -52
  75. package/dist/types/tools/search/types.d.ts +5 -23
  76. package/dist/types/types/tools.d.ts +2 -0
  77. package/dist/types/utils/index.d.ts +1 -0
  78. package/dist/types/utils/schema.d.ts +8 -0
  79. package/package.json +5 -2
  80. package/src/agents/AgentContext.ts +5 -11
  81. package/src/graphs/Graph.ts +23 -5
  82. package/src/graphs/MultiAgentGraph.ts +26 -17
  83. package/src/llm/bedrock/index.ts +180 -43
  84. package/src/llm/bedrock/llm.spec.ts +616 -0
  85. package/src/llm/bedrock/types.ts +51 -0
  86. package/src/llm/bedrock/utils/index.ts +18 -0
  87. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  88. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  89. package/src/messages/core.ts +1 -1
  90. package/src/scripts/code_exec_multi_session.ts +241 -0
  91. package/src/scripts/thinking-bedrock.ts +159 -0
  92. package/src/scripts/thinking.ts +39 -18
  93. package/src/scripts/tools.ts +7 -3
  94. package/src/specs/agent-handoffs.test.ts +1 -2
  95. package/src/specs/tool-error.test.ts +7 -2
  96. package/src/stream.ts +4 -2
  97. package/src/test/mockTools.ts +34 -14
  98. package/src/tools/CodeExecutor.ts +48 -31
  99. package/src/tools/ProgrammaticToolCalling.ts +24 -23
  100. package/src/tools/ToolNode.ts +9 -5
  101. package/src/tools/ToolSearch.ts +54 -43
  102. package/src/tools/search/schema.ts +30 -25
  103. package/src/tools/search/tool.ts +23 -16
  104. package/src/tools/search/types.ts +5 -29
  105. package/src/types/tools.ts +2 -0
  106. package/src/utils/index.ts +1 -0
  107. package/src/utils/schema.ts +35 -0
  108. package/src/utils/title.ts +31 -19
@@ -0,0 +1,563 @@
1
+ /**
2
+ * Utility functions for converting LangChain messages to Bedrock Converse messages.
3
+ * Ported from @langchain/aws common.js
4
+ */
5
+ import {
6
+ type BaseMessage,
7
+ isAIMessage,
8
+ parseBase64DataUrl,
9
+ parseMimeType,
10
+ MessageContentComplex,
11
+ } from '@langchain/core/messages';
12
+ import type {
13
+ BedrockMessage,
14
+ BedrockSystemContentBlock,
15
+ BedrockContentBlock,
16
+ MessageContentReasoningBlock,
17
+ } from '../types';
18
+
19
+ /**
20
+ * Convert a LangChain reasoning block to a Bedrock reasoning block.
21
+ */
22
+ export function langchainReasoningBlockToBedrockReasoningBlock(
23
+ content: MessageContentReasoningBlock
24
+ ): {
25
+ reasoningText?: { text?: string; signature?: string };
26
+ redactedContent?: Uint8Array;
27
+ } {
28
+ if (content.reasoningText != null) {
29
+ return {
30
+ reasoningText: content.reasoningText,
31
+ };
32
+ }
33
+ if (content.redactedContent != null && content.redactedContent !== '') {
34
+ return {
35
+ redactedContent: new Uint8Array(
36
+ Buffer.from(content.redactedContent, 'base64')
37
+ ),
38
+ };
39
+ }
40
+ throw new Error('Invalid reasoning content');
41
+ }
42
+
43
+ /**
44
+ * Concatenate consecutive reasoning blocks in content array.
45
+ */
46
+ export function concatenateLangchainReasoningBlocks(
47
+ content: Array<MessageContentComplex | MessageContentReasoningBlock>
48
+ ): Array<MessageContentComplex | MessageContentReasoningBlock> {
49
+ const result: Array<MessageContentComplex | MessageContentReasoningBlock> =
50
+ [];
51
+
52
+ for (const block of content) {
53
+ if (block.type === 'reasoning_content') {
54
+ const currentReasoning = block as MessageContentReasoningBlock;
55
+ const lastIndex = result.length - 1;
56
+
57
+ // Check if we can merge with the previous block
58
+ if (lastIndex >= 0) {
59
+ const lastBlock = result[lastIndex];
60
+ if (
61
+ lastBlock.type === 'reasoning_content' &&
62
+ (lastBlock as MessageContentReasoningBlock).reasoningText != null &&
63
+ currentReasoning.reasoningText != null
64
+ ) {
65
+ const lastReasoning = lastBlock as MessageContentReasoningBlock;
66
+ // Merge consecutive reasoning text blocks
67
+ const lastText = lastReasoning.reasoningText?.text;
68
+ const currentText = currentReasoning.reasoningText.text;
69
+ if (
70
+ lastText != null &&
71
+ lastText !== '' &&
72
+ currentText != null &&
73
+ currentText !== ''
74
+ ) {
75
+ lastReasoning.reasoningText!.text = lastText + currentText;
76
+ } else if (
77
+ currentReasoning.reasoningText.signature != null &&
78
+ currentReasoning.reasoningText.signature !== ''
79
+ ) {
80
+ lastReasoning.reasoningText!.signature =
81
+ currentReasoning.reasoningText.signature;
82
+ }
83
+ continue;
84
+ }
85
+ }
86
+
87
+ result.push({ ...block } as MessageContentReasoningBlock);
88
+ } else {
89
+ result.push(block);
90
+ }
91
+ }
92
+
93
+ return result;
94
+ }
95
+
96
+ /**
97
+ * Extract image info from a base64 string or URL.
98
+ */
99
+ export function extractImageInfo(base64: string): BedrockContentBlock {
100
+ // Extract the format from the base64 string
101
+ const formatMatch = base64.match(/^data:image\/(\w+);base64,/);
102
+ let format: 'gif' | 'jpeg' | 'png' | 'webp' | undefined;
103
+ if (formatMatch) {
104
+ const extractedFormat = formatMatch[1].toLowerCase();
105
+ if (['gif', 'jpeg', 'png', 'webp'].includes(extractedFormat)) {
106
+ format = extractedFormat as typeof format;
107
+ }
108
+ }
109
+
110
+ // Remove the data URL prefix if present
111
+ const base64Data = base64.replace(/^data:image\/\w+;base64,/, '');
112
+
113
+ // Convert base64 to Uint8Array
114
+ const binaryString = atob(base64Data);
115
+ const bytes = new Uint8Array(binaryString.length);
116
+ for (let i = 0; i < binaryString.length; i += 1) {
117
+ bytes[i] = binaryString.charCodeAt(i);
118
+ }
119
+
120
+ return {
121
+ image: {
122
+ format,
123
+ source: {
124
+ bytes,
125
+ },
126
+ },
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Check if a block has a cache point.
132
+ */
133
+ function isDefaultCachePoint(block: unknown): boolean {
134
+ if (typeof block !== 'object' || block === null) {
135
+ return false;
136
+ }
137
+ if (!('cachePoint' in block)) {
138
+ return false;
139
+ }
140
+ const cachePoint = (block as { cachePoint?: unknown }).cachePoint;
141
+ if (typeof cachePoint !== 'object' || cachePoint === null) {
142
+ return false;
143
+ }
144
+ if (!('type' in cachePoint)) {
145
+ return false;
146
+ }
147
+ return (cachePoint as { type?: string }).type === 'default';
148
+ }
149
+
150
+ /**
151
+ * Convert a LangChain content block to a Bedrock Converse content block.
152
+ */
153
+ function convertLangChainContentBlockToConverseContentBlock({
154
+ block,
155
+ onUnknown = 'throw',
156
+ }: {
157
+ block: string | MessageContentComplex;
158
+ onUnknown?: 'throw' | 'passthrough';
159
+ }): BedrockContentBlock {
160
+ if (typeof block === 'string') {
161
+ return { text: block };
162
+ }
163
+
164
+ if (block.type === 'text') {
165
+ return { text: (block as { text: string }).text };
166
+ }
167
+
168
+ if (block.type === 'image_url') {
169
+ const imageUrl =
170
+ typeof (block as { image_url: string | { url: string } }).image_url ===
171
+ 'string'
172
+ ? (block as { image_url: string }).image_url
173
+ : (block as { image_url: { url: string } }).image_url.url;
174
+ return extractImageInfo(imageUrl);
175
+ }
176
+
177
+ if (block.type === 'image') {
178
+ // Handle standard image block format
179
+ const imageBlock = block as {
180
+ source_type?: string;
181
+ url?: string;
182
+ data?: string;
183
+ mime_type?: string;
184
+ };
185
+ if (
186
+ imageBlock.source_type === 'url' &&
187
+ imageBlock.url != null &&
188
+ imageBlock.url !== ''
189
+ ) {
190
+ const parsedData = parseBase64DataUrl({
191
+ dataUrl: imageBlock.url,
192
+ asTypedArray: true,
193
+ });
194
+ if (parsedData != null) {
195
+ const parsedMimeType = parseMimeType(parsedData.mime_type);
196
+ return {
197
+ image: {
198
+ format: parsedMimeType.subtype as 'gif' | 'jpeg' | 'png' | 'webp',
199
+ source: {
200
+ bytes: parsedData.data as Uint8Array,
201
+ },
202
+ },
203
+ };
204
+ }
205
+ } else if (
206
+ imageBlock.source_type === 'base64' &&
207
+ imageBlock.data != null &&
208
+ imageBlock.data !== ''
209
+ ) {
210
+ let format: 'gif' | 'jpeg' | 'png' | 'webp' | undefined;
211
+ if (imageBlock.mime_type != null && imageBlock.mime_type !== '') {
212
+ const parsedMimeType = parseMimeType(imageBlock.mime_type);
213
+ format = parsedMimeType.subtype as typeof format;
214
+ }
215
+ return {
216
+ image: {
217
+ format,
218
+ source: {
219
+ bytes: Uint8Array.from(atob(imageBlock.data), (c) =>
220
+ c.charCodeAt(0)
221
+ ),
222
+ },
223
+ },
224
+ };
225
+ }
226
+ // If it already has the Bedrock image structure, pass through
227
+ if ((block as { image?: unknown }).image !== undefined) {
228
+ return {
229
+ image: (block as { image: unknown }).image,
230
+ } as BedrockContentBlock;
231
+ }
232
+ }
233
+
234
+ if (
235
+ block.type === 'document' &&
236
+ (block as { document?: unknown }).document !== undefined
237
+ ) {
238
+ return {
239
+ document: (block as { document: unknown }).document,
240
+ } as BedrockContentBlock;
241
+ }
242
+
243
+ if (isDefaultCachePoint(block)) {
244
+ return {
245
+ cachePoint: {
246
+ type: 'default',
247
+ },
248
+ } as BedrockContentBlock;
249
+ }
250
+
251
+ if (onUnknown === 'throw') {
252
+ throw new Error(`Unsupported content block type: ${block.type}`);
253
+ } else {
254
+ return block as unknown as BedrockContentBlock;
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Convert a system message to Bedrock system content blocks.
260
+ */
261
+ function convertSystemMessageToConverseMessage(
262
+ msg: BaseMessage
263
+ ): BedrockSystemContentBlock[] {
264
+ if (typeof msg.content === 'string') {
265
+ return [{ text: msg.content }];
266
+ } else if (Array.isArray(msg.content) && msg.content.length > 0) {
267
+ const contentBlocks: BedrockSystemContentBlock[] = [];
268
+ for (const block of msg.content) {
269
+ if (
270
+ typeof block === 'object' &&
271
+ block.type === 'text' &&
272
+ typeof (block as { text?: string }).text === 'string'
273
+ ) {
274
+ contentBlocks.push({
275
+ text: (block as { text: string }).text,
276
+ });
277
+ } else if (isDefaultCachePoint(block)) {
278
+ contentBlocks.push({
279
+ cachePoint: {
280
+ type: 'default',
281
+ },
282
+ } as BedrockSystemContentBlock);
283
+ } else {
284
+ break;
285
+ }
286
+ }
287
+ if (msg.content.length === contentBlocks.length) {
288
+ return contentBlocks;
289
+ }
290
+ }
291
+ throw new Error(
292
+ 'System message content must be either a string, or an array of text blocks, optionally including a cache point.'
293
+ );
294
+ }
295
+
296
+ /**
297
+ * Convert an AI message to a Bedrock message.
298
+ */
299
+ function convertAIMessageToConverseMessage(msg: BaseMessage): BedrockMessage {
300
+ // Check for v1 format from other providers (PR #9766 fix)
301
+ if (msg.response_metadata.output_version === 'v1') {
302
+ return convertFromV1ToChatBedrockConverseMessage(msg);
303
+ }
304
+
305
+ const assistantMsg: BedrockMessage = {
306
+ role: 'assistant',
307
+ content: [],
308
+ };
309
+
310
+ if (typeof msg.content === 'string' && msg.content !== '') {
311
+ assistantMsg.content?.push({ text: msg.content });
312
+ } else if (Array.isArray(msg.content)) {
313
+ const concatenatedBlocks = concatenateLangchainReasoningBlocks(
314
+ msg.content as Array<MessageContentComplex | MessageContentReasoningBlock>
315
+ );
316
+ const contentBlocks: BedrockContentBlock[] = [];
317
+
318
+ concatenatedBlocks.forEach((block) => {
319
+ if (block.type === 'text' && (block as { text?: string }).text !== '') {
320
+ // Merge whitespace/newlines with previous text blocks to avoid validation errors.
321
+ const text = (block as { text: string }).text;
322
+ const cleanedText = text.replace(/\n/g, '').trim();
323
+ if (cleanedText === '') {
324
+ if (contentBlocks.length > 0) {
325
+ const lastBlock = contentBlocks[contentBlocks.length - 1];
326
+ if ('text' in lastBlock) {
327
+ const mergedTextContent = `${lastBlock.text}${text}`;
328
+ (lastBlock as { text: string }).text = mergedTextContent;
329
+ }
330
+ }
331
+ } else {
332
+ contentBlocks.push({ text });
333
+ }
334
+ } else if (block.type === 'reasoning_content') {
335
+ contentBlocks.push({
336
+ reasoningContent: langchainReasoningBlockToBedrockReasoningBlock(
337
+ block as MessageContentReasoningBlock
338
+ ),
339
+ } as BedrockContentBlock);
340
+ } else if (isDefaultCachePoint(block)) {
341
+ contentBlocks.push({
342
+ cachePoint: {
343
+ type: 'default',
344
+ },
345
+ } as BedrockContentBlock);
346
+ } else {
347
+ const blockValues = Object.fromEntries(
348
+ Object.entries(block).filter(([key]) => key !== 'type')
349
+ );
350
+ throw new Error(
351
+ `Unsupported content block type: ${block.type} with content of ${JSON.stringify(blockValues, null, 2)}`
352
+ );
353
+ }
354
+ });
355
+
356
+ assistantMsg.content = [...(assistantMsg.content ?? []), ...contentBlocks];
357
+ }
358
+
359
+ // Important: this must be placed after any reasoning content blocks
360
+ if (isAIMessage(msg) && msg.tool_calls != null && msg.tool_calls.length > 0) {
361
+ const toolUseBlocks = msg.tool_calls.map((tc) => ({
362
+ toolUse: {
363
+ toolUseId: tc.id,
364
+ name: tc.name,
365
+ input: tc.args as Record<string, unknown>,
366
+ },
367
+ }));
368
+ assistantMsg.content = [
369
+ ...(assistantMsg.content ?? []),
370
+ ...toolUseBlocks,
371
+ ] as BedrockContentBlock[];
372
+ }
373
+
374
+ return assistantMsg;
375
+ }
376
+
377
+ /**
378
+ * Convert a v1 format message from other providers to Bedrock format.
379
+ * This handles messages with standard content blocks like tool_call and reasoning.
380
+ * (Implements PR #9766 fix for output_version v1 detection)
381
+ */
382
+ function convertFromV1ToChatBedrockConverseMessage(
383
+ msg: BaseMessage
384
+ ): BedrockMessage {
385
+ const assistantMsg: BedrockMessage = {
386
+ role: 'assistant',
387
+ content: [],
388
+ };
389
+
390
+ if (Array.isArray(msg.content)) {
391
+ for (const block of msg.content) {
392
+ if (typeof block === 'string') {
393
+ assistantMsg.content?.push({ text: block });
394
+ } else if (block.type === 'text') {
395
+ assistantMsg.content?.push({ text: (block as { text: string }).text });
396
+ } else if (block.type === 'tool_call') {
397
+ const toolCall = block as {
398
+ id: string;
399
+ name: string;
400
+ args: Record<string, unknown>;
401
+ };
402
+ assistantMsg.content?.push({
403
+ toolUse: {
404
+ toolUseId: toolCall.id,
405
+ name: toolCall.name,
406
+ input: toolCall.args as Record<string, unknown>,
407
+ },
408
+ } as BedrockContentBlock);
409
+ } else if (block.type === 'reasoning') {
410
+ const reasoning = block as { reasoning: string };
411
+ assistantMsg.content?.push({
412
+ reasoningContent: {
413
+ reasoningText: { text: reasoning.reasoning },
414
+ },
415
+ } as BedrockContentBlock);
416
+ } else if (block.type === 'reasoning_content') {
417
+ assistantMsg.content?.push({
418
+ reasoningContent: langchainReasoningBlockToBedrockReasoningBlock(
419
+ block as MessageContentReasoningBlock
420
+ ),
421
+ } as BedrockContentBlock);
422
+ }
423
+ }
424
+ } else if (typeof msg.content === 'string' && msg.content !== '') {
425
+ assistantMsg.content?.push({ text: msg.content });
426
+ }
427
+
428
+ // Also handle tool_calls from the message
429
+ if (isAIMessage(msg) && msg.tool_calls != null && msg.tool_calls.length > 0) {
430
+ // Check if tool calls are already in content
431
+ const existingToolUseIds = new Set(
432
+ assistantMsg.content
433
+ ?.filter((c) => 'toolUse' in c)
434
+ .map(
435
+ (c) => (c as { toolUse: { toolUseId: string } }).toolUse.toolUseId
436
+ ) ?? []
437
+ );
438
+
439
+ for (const tc of msg.tool_calls) {
440
+ if (!existingToolUseIds.has(tc.id ?? '')) {
441
+ assistantMsg.content?.push({
442
+ toolUse: {
443
+ toolUseId: tc.id,
444
+ name: tc.name,
445
+ input: tc.args as Record<string, unknown>,
446
+ },
447
+ } as BedrockContentBlock);
448
+ }
449
+ }
450
+ }
451
+
452
+ return assistantMsg;
453
+ }
454
+
455
+ /**
456
+ * Convert a human message to a Bedrock message.
457
+ */
458
+ function convertHumanMessageToConverseMessage(
459
+ msg: BaseMessage
460
+ ): BedrockMessage {
461
+ const userMessage: BedrockMessage = {
462
+ role: 'user',
463
+ content: [],
464
+ };
465
+
466
+ if (typeof msg.content === 'string') {
467
+ userMessage.content = [{ text: msg.content }];
468
+ } else if (Array.isArray(msg.content)) {
469
+ userMessage.content = msg.content.map((block) =>
470
+ convertLangChainContentBlockToConverseContentBlock({ block })
471
+ );
472
+ }
473
+
474
+ return userMessage;
475
+ }
476
+
477
+ /**
478
+ * Convert a tool message to a Bedrock message.
479
+ */
480
+ function convertToolMessageToConverseMessage(msg: BaseMessage): BedrockMessage {
481
+ const toolCallId = (msg as { tool_call_id?: string }).tool_call_id;
482
+
483
+ let content: BedrockContentBlock[];
484
+ if (typeof msg.content === 'string') {
485
+ content = [{ text: msg.content }];
486
+ } else if (Array.isArray(msg.content)) {
487
+ content = msg.content.map((block) =>
488
+ convertLangChainContentBlockToConverseContentBlock({
489
+ block,
490
+ onUnknown: 'passthrough',
491
+ })
492
+ );
493
+ } else {
494
+ content = [{ text: String(msg.content) }];
495
+ }
496
+
497
+ return {
498
+ role: 'user',
499
+ content: [
500
+ {
501
+ toolResult: {
502
+ toolUseId: toolCallId,
503
+ content: content as { text: string }[],
504
+ },
505
+ },
506
+ ],
507
+ };
508
+ }
509
+
510
+ /**
511
+ * Convert LangChain messages to Bedrock Converse messages.
512
+ */
513
+ export function convertToConverseMessages(messages: BaseMessage[]): {
514
+ converseMessages: BedrockMessage[];
515
+ converseSystem: BedrockSystemContentBlock[];
516
+ } {
517
+ const converseSystem = messages
518
+ .filter((msg) => msg._getType() === 'system')
519
+ .flatMap((msg) => convertSystemMessageToConverseMessage(msg));
520
+
521
+ const converseMessages = messages
522
+ .filter((msg) => msg._getType() !== 'system')
523
+ .map((msg) => {
524
+ if (msg._getType() === 'ai') {
525
+ return convertAIMessageToConverseMessage(msg);
526
+ } else if (msg._getType() === 'human' || msg._getType() === 'generic') {
527
+ return convertHumanMessageToConverseMessage(msg);
528
+ } else if (msg._getType() === 'tool') {
529
+ return convertToolMessageToConverseMessage(msg);
530
+ } else {
531
+ throw new Error(`Unsupported message type: ${msg._getType()}`);
532
+ }
533
+ });
534
+
535
+ // Combine consecutive user tool result messages into a single message
536
+ const combinedConverseMessages = converseMessages.reduce<BedrockMessage[]>(
537
+ (acc, curr) => {
538
+ const lastMessage = acc[acc.length - 1];
539
+ if (lastMessage == null) {
540
+ acc.push(curr);
541
+ return acc;
542
+ }
543
+ const lastHasToolResult =
544
+ lastMessage.content?.some((c) => 'toolResult' in c) === true;
545
+ const currHasToolResult =
546
+ curr.content?.some((c) => 'toolResult' in c) === true;
547
+ if (
548
+ lastMessage.role === 'user' &&
549
+ lastHasToolResult &&
550
+ curr.role === 'user' &&
551
+ currHasToolResult
552
+ ) {
553
+ lastMessage.content = lastMessage.content?.concat(curr.content ?? []);
554
+ } else {
555
+ acc.push(curr);
556
+ }
557
+ return acc;
558
+ },
559
+ []
560
+ );
561
+
562
+ return { converseMessages: combinedConverseMessages, converseSystem };
563
+ }