@librechat/agents 3.2.61 → 3.2.62
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/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +32 -0
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +6 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +12 -0
- package/dist/cjs/hooks/index.cjs.map +1 -0
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +62 -1
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +266 -43
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +274 -7
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/streamMetadata.cjs +69 -0
- package/dist/cjs/llm/openai/streamMetadata.cjs.map +1 -0
- package/dist/cjs/llm/openrouter/index.cjs +5 -6
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +6 -1
- package/dist/cjs/messages/format.cjs +61 -0
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +31 -19
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/stream.cjs +13 -3
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +75 -10
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +111 -0
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +32 -0
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +6 -0
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +12 -1
- package/dist/esm/hooks/index.mjs.map +1 -0
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +62 -1
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +266 -43
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +274 -7
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/streamMetadata.mjs +69 -0
- package/dist/esm/llm/openai/streamMetadata.mjs.map +1 -0
- package/dist/esm/llm/openrouter/index.mjs +5 -6
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -3
- package/dist/esm/messages/format.mjs +61 -0
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +31 -19
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/stream.mjs +13 -3
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +75 -10
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +108 -1
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +3 -1
- package/dist/types/hooks/HookRegistry.d.ts +10 -0
- package/dist/types/hooks/index.d.ts +7 -0
- package/dist/types/hooks/types.d.ts +18 -1
- package/dist/types/llm/bedrock/index.d.ts +5 -0
- package/dist/types/llm/openai/index.d.ts +19 -0
- package/dist/types/llm/openai/streamMetadata.d.ts +16 -0
- package/dist/types/messages/prune.d.ts +4 -3
- package/dist/types/tools/ToolNode.d.ts +1 -0
- package/dist/types/types/llm.d.ts +12 -2
- package/dist/types/types/tools.d.ts +1 -1
- package/dist/types/utils/tokens.d.ts +30 -0
- package/package.json +6 -6
- package/src/__tests__/stream.eagerEventExecution.test.ts +100 -2
- package/src/common/enum.ts +2 -0
- package/src/hooks/HookRegistry.ts +45 -0
- package/src/hooks/__tests__/HookRegistry.test.ts +48 -0
- package/src/hooks/__tests__/executeHooks.test.ts +85 -2
- package/src/hooks/executeHooks.ts +15 -0
- package/src/hooks/index.ts +7 -0
- package/src/hooks/types.ts +18 -1
- package/src/langfuse.ts +134 -1
- package/src/llm/bedrock/index.ts +434 -83
- package/src/llm/bedrock/streamSealDispatch.test.ts +97 -0
- package/src/llm/custom-chat-models.smoke.test.ts +7 -0
- package/src/llm/openai/index.ts +604 -6
- package/src/llm/openai/managedRequests.test.ts +182 -0
- package/src/llm/openai/streamMetadata.spec.ts +86 -0
- package/src/llm/openai/streamMetadata.ts +95 -0
- package/src/llm/openai/streamMetadataDedup.spec.ts +166 -0
- package/src/llm/openrouter/index.ts +9 -5
- package/src/messages/format.ts +96 -3
- package/src/messages/formatAgentMessages.steer.test.ts +326 -0
- package/src/messages/labelContentByAgent.test.ts +75 -0
- package/src/messages/prune.ts +56 -30
- package/src/specs/anthropic.simple.test.ts +4 -2
- package/src/specs/cache.simple.test.ts +17 -4
- package/src/specs/langfuse-callbacks.test.ts +61 -0
- package/src/specs/openai.simple.test.ts +4 -2
- package/src/specs/spec.utils.ts +12 -0
- package/src/specs/summarization.test.ts +9 -13
- package/src/specs/token-accounting-pipeline.test.ts +130 -4
- package/src/specs/tokens.test.ts +214 -0
- package/src/stream.ts +22 -3
- package/src/tools/ToolNode.ts +107 -12
- package/src/tools/__tests__/ToolNode.eagerEventExecution.test.ts +554 -0
- package/src/tools/__tests__/ToolNode.onResultCompletion.test.ts +49 -2
- package/src/tools/__tests__/hitl.test.ts +112 -0
- package/src/tools/subagent/SubagentExecutor.ts +2 -0
- package/src/types/llm.ts +12 -2
- package/src/types/tools.ts +1 -1
- package/src/utils/tokens.ts +181 -3
|
@@ -31,7 +31,59 @@ import { ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
|
|
|
31
31
|
* are promoted from strings to array form with `index` so they merge correctly once
|
|
32
32
|
* the accumulated content is already an array.
|
|
33
33
|
*/
|
|
34
|
+
const MAX_STREAM_QUEUE_CHUNKS = 256;
|
|
35
|
+
const MAX_STREAM_QUEUE_TEXT_CHARS = 8192;
|
|
36
|
+
const STREAM_CHUNK_MIN_SIZE = 4;
|
|
37
|
+
const STREAM_BOUNDARIES = new Set([
|
|
38
|
+
" ",
|
|
39
|
+
".",
|
|
40
|
+
",",
|
|
41
|
+
"!",
|
|
42
|
+
"?",
|
|
43
|
+
";",
|
|
44
|
+
":"
|
|
45
|
+
]);
|
|
46
|
+
function findStreamChunkBoundary(text, minSize) {
|
|
47
|
+
if (minSize >= text.length) return text.length;
|
|
48
|
+
for (let position = minSize; position < text.length; position++) if (STREAM_BOUNDARIES.has(text[position])) return position + 1;
|
|
49
|
+
return text.length;
|
|
50
|
+
}
|
|
51
|
+
function splitStreamToken(text) {
|
|
52
|
+
const chunks = [];
|
|
53
|
+
let currentIndex = 0;
|
|
54
|
+
while (currentIndex < text.length) {
|
|
55
|
+
const chunkSize = findStreamChunkBoundary(text.slice(currentIndex), STREAM_CHUNK_MIN_SIZE);
|
|
56
|
+
chunks.push(text.slice(currentIndex, currentIndex + chunkSize));
|
|
57
|
+
currentIndex += chunkSize;
|
|
58
|
+
}
|
|
59
|
+
return chunks;
|
|
60
|
+
}
|
|
61
|
+
function getCadencedStreamDelay({ targetDelay, lastVisibleContentAt, now }) {
|
|
62
|
+
if (targetDelay <= 0 || lastVisibleContentAt == null) return 0;
|
|
63
|
+
return Math.max(0, targetDelay - (now - lastVisibleContentAt));
|
|
64
|
+
}
|
|
65
|
+
async function waitForStreamDelay(delay, signal) {
|
|
66
|
+
if (delay <= 0 || isSignalAborted(signal)) return;
|
|
67
|
+
await new Promise((resolve) => {
|
|
68
|
+
const timeoutRef = {};
|
|
69
|
+
const onAbort = () => {
|
|
70
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
71
|
+
signal?.removeEventListener("abort", onAbort);
|
|
72
|
+
resolve();
|
|
73
|
+
};
|
|
74
|
+
timeoutRef.current = setTimeout(() => {
|
|
75
|
+
signal?.removeEventListener("abort", onAbort);
|
|
76
|
+
resolve();
|
|
77
|
+
}, delay);
|
|
78
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
79
|
+
if (isSignalAborted(signal)) onAbort();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function isSignalAborted(signal) {
|
|
83
|
+
return signal?.aborted === true;
|
|
84
|
+
}
|
|
34
85
|
var CustomChatBedrockConverse = class extends ChatBedrockConverse {
|
|
86
|
+
_lc_stream_delay;
|
|
35
87
|
/**
|
|
36
88
|
* Whether to insert Bedrock prompt cache checkpoints when available.
|
|
37
89
|
*/
|
|
@@ -59,6 +111,7 @@ var CustomChatBedrockConverse = class extends ChatBedrockConverse {
|
|
|
59
111
|
super(fields);
|
|
60
112
|
this.promptCache = fields?.promptCache;
|
|
61
113
|
this.promptCacheTtl = fields?.promptCacheTtl;
|
|
114
|
+
this._lc_stream_delay = Math.max(0, fields?._lc_stream_delay ?? 0);
|
|
62
115
|
this.applicationInferenceProfile = fields?.applicationInferenceProfile;
|
|
63
116
|
this.serviceTier = fields?.serviceTier;
|
|
64
117
|
this.cacheModelId = fields?.model ?? this.model;
|
|
@@ -126,52 +179,222 @@ var CustomChatBedrockConverse = class extends ChatBedrockConverse {
|
|
|
126
179
|
system: converseSystem,
|
|
127
180
|
...params
|
|
128
181
|
});
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
182
|
+
const streamAbortController = new AbortController();
|
|
183
|
+
const abortStream = () => streamAbortController.abort();
|
|
184
|
+
options.signal?.addEventListener("abort", abortStream, { once: true });
|
|
185
|
+
if (isSignalAborted(options.signal)) abortStream();
|
|
186
|
+
try {
|
|
187
|
+
const stream = (await this.client.send(command, { abortSignal: streamAbortController.signal })).stream;
|
|
188
|
+
if (!stream) return;
|
|
189
|
+
const seenBlockIndices = /* @__PURE__ */ new Set();
|
|
190
|
+
const toolUseBlockIndices = /* @__PURE__ */ new Set();
|
|
191
|
+
const queuedChunks = [];
|
|
192
|
+
const producerState = { done: false };
|
|
193
|
+
let queuedChunkIndex = 0;
|
|
194
|
+
let bufferedTextLength = 0;
|
|
195
|
+
let consumerClosed = false;
|
|
196
|
+
let notifyConsumer;
|
|
197
|
+
let notifyProducer;
|
|
198
|
+
let hasEmittedVisibleContent = false;
|
|
199
|
+
let lastVisibleContentAt;
|
|
200
|
+
/**
|
|
201
|
+
* Guardrails can reject an already-streamed toolUse block at
|
|
202
|
+
* `messageStop` (`guardrail_intervened`), after `contentBlockStop` has
|
|
203
|
+
* passed. Only emit eager-execution seals when no guardrails are
|
|
204
|
+
* configured, so a later intervention can't race an eagerly started tool.
|
|
205
|
+
*/
|
|
206
|
+
const sealToolUseOnStop = options.guardrailConfig == null && this.guardrailConfig == null;
|
|
207
|
+
const notifyConsumerForChunk = () => {
|
|
208
|
+
notifyConsumer?.();
|
|
209
|
+
notifyConsumer = void 0;
|
|
210
|
+
};
|
|
211
|
+
const notifyProducerForSpace = () => {
|
|
212
|
+
notifyProducer?.();
|
|
213
|
+
notifyProducer = void 0;
|
|
214
|
+
};
|
|
215
|
+
const hasQueuedChunks = () => queuedChunkIndex < queuedChunks.length;
|
|
216
|
+
const getQueuedChunkCount = () => queuedChunks.length - queuedChunkIndex;
|
|
217
|
+
const isQueueAtCapacity = () => getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS || bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;
|
|
218
|
+
const waitForNextChunk = async () => {
|
|
219
|
+
if (hasQueuedChunks() || producerState.done || producerState.error != null) return;
|
|
220
|
+
await new Promise((resolve) => {
|
|
221
|
+
notifyConsumer = resolve;
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
const waitForQueueSpace = async () => {
|
|
225
|
+
while (isQueueAtCapacity() && !consumerClosed && !isSignalAborted(options.signal)) await new Promise((resolve) => {
|
|
226
|
+
const signal = options.signal;
|
|
227
|
+
const onAbort = () => {
|
|
228
|
+
signal?.removeEventListener("abort", onAbort);
|
|
229
|
+
resolve();
|
|
230
|
+
};
|
|
231
|
+
const onSpace = () => {
|
|
232
|
+
signal?.removeEventListener("abort", onAbort);
|
|
233
|
+
resolve();
|
|
234
|
+
};
|
|
235
|
+
notifyProducer = onSpace;
|
|
236
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
237
|
+
if (isSignalAborted(signal)) onAbort();
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
const dequeue = () => {
|
|
241
|
+
if (!hasQueuedChunks()) return;
|
|
242
|
+
const queuedChunk = queuedChunks[queuedChunkIndex];
|
|
243
|
+
queuedChunkIndex++;
|
|
244
|
+
if (queuedChunkIndex > 128 && queuedChunkIndex * 2 >= queuedChunks.length) {
|
|
245
|
+
queuedChunks.splice(0, queuedChunkIndex);
|
|
246
|
+
queuedChunkIndex = 0;
|
|
147
247
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
248
|
+
return queuedChunk;
|
|
249
|
+
};
|
|
250
|
+
const enqueue = async (queuedChunk) => {
|
|
251
|
+
await waitForQueueSpace();
|
|
252
|
+
if (consumerClosed || isSignalAborted(options.signal)) {
|
|
253
|
+
abortStream();
|
|
254
|
+
throw new Error("AbortError: User aborted the request.");
|
|
255
|
+
}
|
|
256
|
+
queuedChunks.push(queuedChunk);
|
|
257
|
+
if (queuedChunk.smooth) bufferedTextLength += queuedChunk.textLength;
|
|
258
|
+
notifyConsumerForChunk();
|
|
259
|
+
};
|
|
260
|
+
const enqueueChunk = async ({ chunk, callbackChunk, callbackToken = "", smooth = false, textLength = 0 }) => {
|
|
261
|
+
await enqueue({
|
|
262
|
+
chunk,
|
|
263
|
+
callbackChunk,
|
|
264
|
+
callbackToken,
|
|
265
|
+
smooth,
|
|
266
|
+
textLength: smooth ? textLength : 0
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
const enqueueDelta = async (contentBlockDelta) => {
|
|
270
|
+
const delta = contentBlockDelta.delta;
|
|
271
|
+
if (delta == null) throw new Error("No delta found in content block.");
|
|
272
|
+
const idx = contentBlockDelta.contentBlockIndex;
|
|
273
|
+
if (idx != null) seenBlockIndices.add(idx);
|
|
274
|
+
const text = delta.text;
|
|
275
|
+
const reasoningContent = delta.reasoningContent;
|
|
276
|
+
const reasoningText = reasoningContent?.text;
|
|
277
|
+
const visibleText = typeof text === "string" ? text : typeof reasoningText === "string" ? reasoningText : "";
|
|
278
|
+
const smooth = this._lc_stream_delay > 0 && visibleText !== "";
|
|
279
|
+
const tokenChunks = smooth ? splitStreamToken(visibleText) : [visibleText];
|
|
280
|
+
for (const token of tokenChunks) {
|
|
281
|
+
let splitDelta = contentBlockDelta;
|
|
282
|
+
if (typeof text === "string") splitDelta = {
|
|
283
|
+
...contentBlockDelta,
|
|
284
|
+
delta: { text: token }
|
|
285
|
+
};
|
|
286
|
+
else if (typeof reasoningText === "string" && reasoningContent != null) splitDelta = {
|
|
287
|
+
...contentBlockDelta,
|
|
288
|
+
delta: { reasoningContent: {
|
|
289
|
+
...reasoningContent,
|
|
290
|
+
text: token
|
|
291
|
+
} }
|
|
292
|
+
};
|
|
293
|
+
const deltaChunk = handleConverseStreamContentBlockDelta(splitDelta);
|
|
294
|
+
await enqueueChunk({
|
|
295
|
+
chunk: this.enrichChunk(deltaChunk, seenBlockIndices),
|
|
296
|
+
callbackChunk: deltaChunk,
|
|
297
|
+
callbackToken: deltaChunk.text,
|
|
298
|
+
smooth,
|
|
299
|
+
textLength: token.length
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
const producer = (async () => {
|
|
304
|
+
try {
|
|
305
|
+
for await (const event of stream) {
|
|
306
|
+
if (isSignalAborted(options.signal)) {
|
|
307
|
+
abortStream();
|
|
308
|
+
throw new Error("AbortError: User aborted the request.");
|
|
309
|
+
}
|
|
310
|
+
if (event.contentBlockStart != null) {
|
|
311
|
+
const startChunk = handleConverseStreamContentBlockStart(event.contentBlockStart);
|
|
312
|
+
if (startChunk != null) {
|
|
313
|
+
const idx = event.contentBlockStart.contentBlockIndex;
|
|
314
|
+
if (idx != null) {
|
|
315
|
+
seenBlockIndices.add(idx);
|
|
316
|
+
if (event.contentBlockStart.start?.toolUse != null) toolUseBlockIndices.add(idx);
|
|
317
|
+
}
|
|
318
|
+
await enqueueChunk({
|
|
319
|
+
chunk: this.enrichChunk(startChunk, seenBlockIndices),
|
|
320
|
+
callbackChunk: startChunk,
|
|
321
|
+
callbackToken: startChunk.text
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
} else if (event.contentBlockDelta != null) await enqueueDelta(event.contentBlockDelta);
|
|
325
|
+
else if (event.metadata != null) await enqueueChunk({ chunk: handleConverseStreamMetadata(event.metadata, { streamUsage }) });
|
|
326
|
+
else if (event.contentBlockStop != null) {
|
|
327
|
+
const stopIdx = event.contentBlockStop.contentBlockIndex;
|
|
328
|
+
if (stopIdx != null) {
|
|
329
|
+
seenBlockIndices.add(stopIdx);
|
|
330
|
+
if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {
|
|
331
|
+
const sealChunk = createConverseToolUseStopChunk(stopIdx);
|
|
332
|
+
await enqueueChunk({
|
|
333
|
+
chunk: sealChunk,
|
|
334
|
+
callbackChunk: sealChunk,
|
|
335
|
+
callbackToken: sealChunk.text
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
} else await enqueueChunk({ chunk: new ChatGenerationChunk({
|
|
340
|
+
text: "",
|
|
341
|
+
message: new AIMessageChunk({
|
|
342
|
+
content: "",
|
|
343
|
+
response_metadata: { ...event }
|
|
344
|
+
})
|
|
345
|
+
}) });
|
|
346
|
+
}
|
|
347
|
+
} catch (error) {
|
|
348
|
+
producerState.error = error;
|
|
349
|
+
} finally {
|
|
350
|
+
producerState.done = true;
|
|
351
|
+
notifyConsumerForChunk();
|
|
166
352
|
}
|
|
353
|
+
})();
|
|
354
|
+
try {
|
|
355
|
+
let keepStreaming = true;
|
|
356
|
+
while (keepStreaming) {
|
|
357
|
+
if (isSignalAborted(options.signal)) {
|
|
358
|
+
abortStream();
|
|
359
|
+
throw new Error("AbortError: User aborted the request.");
|
|
360
|
+
}
|
|
361
|
+
await waitForNextChunk();
|
|
362
|
+
const queuedChunk = dequeue();
|
|
363
|
+
if (!queuedChunk) {
|
|
364
|
+
if (producerState.error != null) throw producerState.error;
|
|
365
|
+
if (producerState.done) keepStreaming = false;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if (queuedChunk.smooth) {
|
|
369
|
+
bufferedTextLength = Math.max(0, bufferedTextLength - queuedChunk.textLength);
|
|
370
|
+
notifyProducerForSpace();
|
|
371
|
+
await waitForStreamDelay(getCadencedStreamDelay({
|
|
372
|
+
targetDelay: hasEmittedVisibleContent ? this._lc_stream_delay : 0,
|
|
373
|
+
lastVisibleContentAt,
|
|
374
|
+
now: Date.now()
|
|
375
|
+
}), options.signal);
|
|
376
|
+
if (isSignalAborted(options.signal)) {
|
|
377
|
+
abortStream();
|
|
378
|
+
throw new Error("AbortError: User aborted the request.");
|
|
379
|
+
}
|
|
380
|
+
hasEmittedVisibleContent = true;
|
|
381
|
+
lastVisibleContentAt = Date.now();
|
|
382
|
+
} else notifyProducerForSpace();
|
|
383
|
+
yield queuedChunk.chunk;
|
|
384
|
+
if (queuedChunk.callbackChunk != null) await runManager?.handleLLMNewToken(queuedChunk.callbackToken, void 0, void 0, void 0, void 0, { chunk: queuedChunk.callbackChunk });
|
|
385
|
+
}
|
|
386
|
+
} finally {
|
|
387
|
+
consumerClosed = true;
|
|
388
|
+
if (!producerState.done) {
|
|
389
|
+
abortStream();
|
|
390
|
+
notifyProducerForSpace();
|
|
391
|
+
}
|
|
392
|
+
await producer;
|
|
167
393
|
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
response_metadata: { ...event }
|
|
173
|
-
})
|
|
174
|
-
});
|
|
394
|
+
} finally {
|
|
395
|
+
options.signal?.removeEventListener("abort", abortStream);
|
|
396
|
+
if (!streamAbortController.signal.aborted) streamAbortController.abort();
|
|
397
|
+
}
|
|
175
398
|
}
|
|
176
399
|
/**
|
|
177
400
|
* Inject `index` on content blocks for proper merge behaviour, then strip
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport {\n resolveBedrockPromptCacheTtl,\n supportsBedrockToolCache,\n type PromptCacheTtl,\n} from '@/messages/cache';\nimport { applyCachePointsToConversePayload } from './cachePoints';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * Bedrock models that don't support the 1-hour TTL downgrade to 5m\n * server-side (verified on Sonnet/Opus 4.6), so the default is safe to leave\n * on; use `'5m'` for any model that rejects it.\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n /**\n * The configured model id, captured at construction so it survives the\n * temporary `this.model` swap to an application-inference-profile ARN during\n * generation. Used to gate the Bedrock tool cache point to Claude models\n * (see {@link supportsBedrockToolCache}).\n */\n private readonly cacheModelId: string;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.promptCacheTtl = fields?.promptCacheTtl;\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n // `super(fields)` initializes `this.model` to LangChain's default Claude\n // model when `fields.model` is omitted, so fall back to it rather than ''\n // (which would treat the default Claude model as tool-cache-unsupported).\n this.cacheModelId = fields?.model ?? this.model;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true && supportsBedrockToolCache(this.cacheModelId)\n ? insertBedrockToolCachePoint(\n baseParams.toolConfig,\n true,\n resolveBedrockPromptCacheTtl(this.promptCacheTtl, this.cacheModelId)\n )\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n applyCachePointsToConversePayload({\n cacheControl: options.cache_control,\n system: converseSystem,\n messages: converseMessages,\n params,\n modelId,\n });\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (!response.stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n yield this.enrichChunk(startChunk, seenBlockIndices);\n\n // Registered stream handlers receive chunks through callback\n // events, not the yielded generator — dispatch the start chunk so\n // they see the tool call's id/name (eager chunk state needs both).\n await runManager?.handleLLMNewToken(\n startChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: startChunk }\n );\n }\n } else if (event.contentBlockDelta != null) {\n const deltaChunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n );\n\n const idx = event.contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n yield this.enrichChunk(deltaChunk, seenBlockIndices);\n\n await runManager?.handleLLMNewToken(\n deltaChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: deltaChunk }\n );\n } else if (event.metadata != null) {\n yield handleConverseStreamMetadata(event.metadata, { streamUsage });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n // Converse guarantees the block's input is complete at stop, so\n // emit an explicit seal chunk for eager tool execution — through\n // the callback path too, for registered stream handlers.\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n yield sealChunk;\n await runManager?.handleLLMNewToken(\n sealChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: sealChunk }\n );\n }\n }\n } else {\n yield new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n });\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,UACoB;EACpB,IACE,uBAAuB,YACvB,OAAO,SAAS,sBAAsB,UAEtC,OAAO,SAAS;CAGpB;CAEA,wBAAgC,KAAuB;EACrD,IAAI,QAAQ,QAAQ,QAAQ,KAAA,GAC1B,OAAO;EAGT,IAAI,MAAM,QAAQ,GAAG,GACnB,OAAO,IAAI,KAAK,SAAS,KAAK,wBAAwB,IAAI,CAAC;EAG7D,IAAI,OAAO,QAAQ,UAAU;GAC3B,MAAM,UAAmC,CAAC;GAC1C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAC3C,IAAI,QAAQ,qBACV,QAAQ,OAAO,KAAK,wBAAwB,KAAK;GAGrD,OAAO;EACT;EAEA,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport type { ContentBlockDeltaEvent } from './types';\nimport {\n resolveBedrockPromptCacheTtl,\n supportsBedrockToolCache,\n type PromptCacheTtl,\n} from '@/messages/cache';\nimport { applyCachePointsToConversePayload } from './cachePoints';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\nconst MAX_STREAM_QUEUE_CHUNKS = 256;\nconst MAX_STREAM_QUEUE_TEXT_CHARS = 8192;\nconst STREAM_CHUNK_MIN_SIZE = 4;\nconst STREAM_BOUNDARIES = new Set([' ', '.', ',', '!', '?', ';', ':']);\n\ntype QueuedGenerationChunk = {\n chunk: ChatGenerationChunk;\n callbackChunk?: ChatGenerationChunk;\n callbackToken: string;\n smooth: boolean;\n textLength: number;\n};\n\nfunction findStreamChunkBoundary(text: string, minSize: number): number {\n if (minSize >= text.length) {\n return text.length;\n }\n\n for (let position = minSize; position < text.length; position++) {\n if (STREAM_BOUNDARIES.has(text[position])) {\n return position + 1;\n }\n }\n\n return text.length;\n}\n\nfunction splitStreamToken(text: string): string[] {\n const chunks: string[] = [];\n let currentIndex = 0;\n\n while (currentIndex < text.length) {\n const remainingText = text.slice(currentIndex);\n const chunkSize = findStreamChunkBoundary(\n remainingText,\n STREAM_CHUNK_MIN_SIZE\n );\n chunks.push(text.slice(currentIndex, currentIndex + chunkSize));\n currentIndex += chunkSize;\n }\n\n return chunks;\n}\n\nfunction getCadencedStreamDelay({\n targetDelay,\n lastVisibleContentAt,\n now,\n}: {\n targetDelay: number;\n lastVisibleContentAt?: number;\n now: number;\n}): number {\n if (targetDelay <= 0 || lastVisibleContentAt == null) {\n return 0;\n }\n return Math.max(0, targetDelay - (now - lastVisibleContentAt));\n}\n\nasync function waitForStreamDelay(\n delay: number,\n signal?: AbortSignal\n): Promise<void> {\n if (delay <= 0 || isSignalAborted(signal)) {\n return;\n }\n await new Promise<void>((resolve) => {\n const timeoutRef: { current?: ReturnType<typeof setTimeout> } = {};\n const onAbort = (): void => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n timeoutRef.current = setTimeout(() => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n }, delay);\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n}\n\nfunction isSignalAborted(signal?: AbortSignal): boolean {\n return signal?.aborted === true;\n}\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * Bedrock models that don't support the 1-hour TTL downgrade to 5m\n * server-side (verified on Sonnet/Opus 4.6), so the default is safe to leave\n * on; use `'5m'` for any model that rejects it.\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Minimum delay in milliseconds between visible streamed content deltas.\n */\n _lc_stream_delay?: number;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n _lc_stream_delay: number;\n\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n /**\n * The configured model id, captured at construction so it survives the\n * temporary `this.model` swap to an application-inference-profile ARN during\n * generation. Used to gate the Bedrock tool cache point to Claude models\n * (see {@link supportsBedrockToolCache}).\n */\n private readonly cacheModelId: string;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.promptCacheTtl = fields?.promptCacheTtl;\n this._lc_stream_delay = Math.max(0, fields?._lc_stream_delay ?? 0);\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n // `super(fields)` initializes `this.model` to LangChain's default Claude\n // model when `fields.model` is omitted, so fall back to it rather than ''\n // (which would treat the default Claude model as tool-cache-unsupported).\n this.cacheModelId = fields?.model ?? this.model;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true && supportsBedrockToolCache(this.cacheModelId)\n ? insertBedrockToolCachePoint(\n baseParams.toolConfig,\n true,\n resolveBedrockPromptCacheTtl(this.promptCacheTtl, this.cacheModelId)\n )\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n applyCachePointsToConversePayload({\n cacheControl: options.cache_control,\n system: converseSystem,\n messages: converseMessages,\n params,\n modelId,\n });\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const streamAbortController = new AbortController();\n const abortStream = (): void => streamAbortController.abort();\n options.signal?.addEventListener('abort', abortStream, { once: true });\n if (isSignalAborted(options.signal)) {\n abortStream();\n }\n\n try {\n const response = await this.client.send(command, {\n abortSignal: streamAbortController.signal,\n });\n\n const stream = response.stream;\n if (!stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n const queuedChunks: QueuedGenerationChunk[] = [];\n const producerState: { done: boolean; error?: unknown } = { done: false };\n let queuedChunkIndex = 0;\n let bufferedTextLength = 0;\n let consumerClosed = false;\n let notifyConsumer: (() => void) | undefined;\n let notifyProducer: (() => void) | undefined;\n let hasEmittedVisibleContent = false;\n let lastVisibleContentAt: number | undefined;\n\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n const notifyConsumerForChunk = (): void => {\n notifyConsumer?.();\n notifyConsumer = undefined;\n };\n\n const notifyProducerForSpace = (): void => {\n notifyProducer?.();\n notifyProducer = undefined;\n };\n\n const hasQueuedChunks = (): boolean =>\n queuedChunkIndex < queuedChunks.length;\n\n const getQueuedChunkCount = (): number =>\n queuedChunks.length - queuedChunkIndex;\n\n const isQueueAtCapacity = (): boolean =>\n getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS ||\n bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;\n\n const waitForNextChunk = async (): Promise<void> => {\n if (\n hasQueuedChunks() ||\n producerState.done ||\n producerState.error != null\n ) {\n return;\n }\n await new Promise<void>((resolve) => {\n notifyConsumer = resolve;\n });\n };\n\n const waitForQueueSpace = async (): Promise<void> => {\n while (\n isQueueAtCapacity() &&\n !consumerClosed &&\n !isSignalAborted(options.signal)\n ) {\n await new Promise<void>((resolve) => {\n const signal = options.signal;\n const onAbort = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n const onSpace = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n notifyProducer = onSpace;\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n }\n };\n\n const dequeue = (): QueuedGenerationChunk | undefined => {\n if (!hasQueuedChunks()) {\n return undefined;\n }\n const queuedChunk = queuedChunks[queuedChunkIndex];\n queuedChunkIndex++;\n if (\n queuedChunkIndex > 128 &&\n queuedChunkIndex * 2 >= queuedChunks.length\n ) {\n queuedChunks.splice(0, queuedChunkIndex);\n queuedChunkIndex = 0;\n }\n return queuedChunk;\n };\n\n const enqueue = async (\n queuedChunk: QueuedGenerationChunk\n ): Promise<void> => {\n await waitForQueueSpace();\n if (consumerClosed || isSignalAborted(options.signal)) {\n abortStream();\n throw new Error('AbortError: User aborted the request.');\n }\n queuedChunks.push(queuedChunk);\n if (queuedChunk.smooth) {\n bufferedTextLength += queuedChunk.textLength;\n }\n notifyConsumerForChunk();\n };\n\n const enqueueChunk = async ({\n chunk,\n callbackChunk,\n callbackToken = '',\n smooth = false,\n textLength = 0,\n }: {\n chunk: ChatGenerationChunk;\n callbackChunk?: ChatGenerationChunk;\n callbackToken?: string;\n smooth?: boolean;\n textLength?: number;\n }): Promise<void> => {\n await enqueue({\n chunk,\n callbackChunk,\n callbackToken,\n smooth,\n textLength: smooth ? textLength : 0,\n });\n };\n\n const enqueueDelta = async (\n contentBlockDelta: ContentBlockDeltaEvent\n ): Promise<void> => {\n const delta = contentBlockDelta.delta;\n if (delta == null) {\n throw new Error('No delta found in content block.');\n }\n\n const idx = contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n const text = delta.text;\n const reasoningContent = delta.reasoningContent;\n const reasoningText = reasoningContent?.text;\n const visibleText =\n typeof text === 'string'\n ? text\n : typeof reasoningText === 'string'\n ? reasoningText\n : '';\n const smooth = this._lc_stream_delay > 0 && visibleText !== '';\n const tokenChunks = smooth\n ? splitStreamToken(visibleText)\n : [visibleText];\n\n for (const token of tokenChunks) {\n let splitDelta = contentBlockDelta;\n if (typeof text === 'string') {\n splitDelta = {\n ...contentBlockDelta,\n delta: { text: token },\n };\n } else if (\n typeof reasoningText === 'string' &&\n reasoningContent != null\n ) {\n splitDelta = {\n ...contentBlockDelta,\n delta: {\n reasoningContent: {\n ...reasoningContent,\n text: token,\n },\n },\n };\n }\n\n const deltaChunk = handleConverseStreamContentBlockDelta(splitDelta);\n await enqueueChunk({\n chunk: this.enrichChunk(deltaChunk, seenBlockIndices),\n callbackChunk: deltaChunk,\n callbackToken: deltaChunk.text,\n smooth,\n textLength: token.length,\n });\n }\n };\n\n const producer = (async (): Promise<void> => {\n try {\n for await (const event of stream) {\n if (isSignalAborted(options.signal)) {\n abortStream();\n throw new Error('AbortError: User aborted the request.');\n }\n\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n await enqueueChunk({\n chunk: this.enrichChunk(startChunk, seenBlockIndices),\n callbackChunk: startChunk,\n callbackToken: startChunk.text,\n });\n }\n } else if (event.contentBlockDelta != null) {\n await enqueueDelta(event.contentBlockDelta);\n } else if (event.metadata != null) {\n await enqueueChunk({\n chunk: handleConverseStreamMetadata(event.metadata, {\n streamUsage,\n }),\n });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n await enqueueChunk({\n chunk: sealChunk,\n callbackChunk: sealChunk,\n callbackToken: sealChunk.text,\n });\n }\n }\n } else {\n await enqueueChunk({\n chunk: new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n }),\n });\n }\n }\n } catch (error) {\n producerState.error = error;\n } finally {\n producerState.done = true;\n notifyConsumerForChunk();\n }\n })();\n\n try {\n let keepStreaming = true;\n while (keepStreaming) {\n if (isSignalAborted(options.signal)) {\n abortStream();\n throw new Error('AbortError: User aborted the request.');\n }\n\n await waitForNextChunk();\n const queuedChunk = dequeue();\n\n if (!queuedChunk) {\n if (producerState.error != null) {\n throw producerState.error;\n }\n if (producerState.done) {\n keepStreaming = false;\n }\n continue;\n }\n\n if (queuedChunk.smooth) {\n bufferedTextLength = Math.max(\n 0,\n bufferedTextLength - queuedChunk.textLength\n );\n notifyProducerForSpace();\n await waitForStreamDelay(\n getCadencedStreamDelay({\n targetDelay: hasEmittedVisibleContent\n ? this._lc_stream_delay\n : 0,\n lastVisibleContentAt,\n now: Date.now(),\n }),\n options.signal\n );\n if (isSignalAborted(options.signal)) {\n abortStream();\n throw new Error('AbortError: User aborted the request.');\n }\n hasEmittedVisibleContent = true;\n lastVisibleContentAt = Date.now();\n } else {\n notifyProducerForSpace();\n }\n\n yield queuedChunk.chunk;\n\n if (queuedChunk.callbackChunk != null) {\n await runManager?.handleLLMNewToken(\n queuedChunk.callbackToken,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: queuedChunk.callbackChunk }\n );\n }\n }\n } finally {\n consumerClosed = true;\n if (!producerState.done) {\n abortStream();\n notifyProducerForSpace();\n }\n await producer;\n }\n } finally {\n options.signal?.removeEventListener('abort', abortStream);\n if (!streamAbortController.signal.aborted) {\n streamAbortController.abort();\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,MAAM,0BAA0B;AAChC,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAC9B,MAAM,oBAAoB,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AAAG,CAAC;AAUrE,SAAS,wBAAwB,MAAc,SAAyB;CACtE,IAAI,WAAW,KAAK,QAClB,OAAO,KAAK;CAGd,KAAK,IAAI,WAAW,SAAS,WAAW,KAAK,QAAQ,YACnD,IAAI,kBAAkB,IAAI,KAAK,SAAS,GACtC,OAAO,WAAW;CAItB,OAAO,KAAK;AACd;AAEA,SAAS,iBAAiB,MAAwB;CAChD,MAAM,SAAmB,CAAC;CAC1B,IAAI,eAAe;CAEnB,OAAO,eAAe,KAAK,QAAQ;EAEjC,MAAM,YAAY,wBADI,KAAK,MAAM,YAEn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