@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,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
|
+
}
|