@librechat/agents 3.1.86 → 3.1.88
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/README.md +69 -0
- package/dist/cjs/events.cjs +23 -0
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +133 -18
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +251 -53
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/init.cjs +1 -5
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +113 -24
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +3 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +18 -5
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/openai/index.cjs +253 -0
- package/dist/cjs/openai/index.cjs.map +1 -0
- package/dist/cjs/responses/index.cjs +448 -0
- package/dist/cjs/responses/index.cjs.map +1 -0
- package/dist/cjs/run.cjs +108 -7
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +1057 -0
- package/dist/cjs/session/AgentSession.cjs.map +1 -0
- package/dist/cjs/session/JsonlSessionStore.cjs +425 -0
- package/dist/cjs/session/JsonlSessionStore.cjs.map +1 -0
- package/dist/cjs/session/handlers.cjs +221 -0
- package/dist/cjs/session/handlers.cjs.map +1 -0
- package/dist/cjs/session/ids.cjs +22 -0
- package/dist/cjs/session/ids.cjs.map +1 -0
- package/dist/cjs/session/messageSerialization.cjs +179 -0
- package/dist/cjs/session/messageSerialization.cjs.map +1 -0
- package/dist/cjs/stream.cjs +475 -11
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +1 -1
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +177 -59
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/eagerEventExecution.cjs +113 -0
- package/dist/cjs/tools/eagerEventExecution.cjs.map +1 -0
- package/dist/cjs/tools/handlers.cjs +1 -1
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/streamedToolCallSeals.cjs +42 -0
- package/dist/cjs/tools/streamedToolCallSeals.cjs.map +1 -0
- package/dist/esm/events.mjs +23 -1
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +133 -18
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +251 -53
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/init.mjs +1 -5
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +113 -25
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +4 -2
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/main.mjs +5 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/openai/index.mjs +246 -0
- package/dist/esm/openai/index.mjs.map +1 -0
- package/dist/esm/responses/index.mjs +440 -0
- package/dist/esm/responses/index.mjs.map +1 -0
- package/dist/esm/run.mjs +108 -7
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +1054 -0
- package/dist/esm/session/AgentSession.mjs.map +1 -0
- package/dist/esm/session/JsonlSessionStore.mjs +422 -0
- package/dist/esm/session/JsonlSessionStore.mjs.map +1 -0
- package/dist/esm/session/handlers.mjs +219 -0
- package/dist/esm/session/handlers.mjs.map +1 -0
- package/dist/esm/session/ids.mjs +17 -0
- package/dist/esm/session/ids.mjs.map +1 -0
- package/dist/esm/session/messageSerialization.mjs +173 -0
- package/dist/esm/session/messageSerialization.mjs.map +1 -0
- package/dist/esm/stream.mjs +476 -12
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +1 -1
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +177 -59
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/eagerEventExecution.mjs +107 -0
- package/dist/esm/tools/eagerEventExecution.mjs.map +1 -0
- package/dist/esm/tools/handlers.mjs +1 -1
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/streamedToolCallSeals.mjs +36 -0
- package/dist/esm/tools/streamedToolCallSeals.mjs.map +1 -0
- package/dist/types/events.d.ts +1 -0
- package/dist/types/graphs/Graph.d.ts +24 -9
- package/dist/types/index.d.ts +1 -0
- package/dist/types/llm/openai/index.d.ts +1 -0
- package/dist/types/openai/index.d.ts +75 -0
- package/dist/types/responses/index.d.ts +97 -0
- package/dist/types/run.d.ts +2 -0
- package/dist/types/session/AgentSession.d.ts +32 -0
- package/dist/types/session/JsonlSessionStore.d.ts +67 -0
- package/dist/types/session/handlers.d.ts +8 -0
- package/dist/types/session/ids.d.ts +4 -0
- package/dist/types/session/index.d.ts +5 -0
- package/dist/types/session/messageSerialization.d.ts +7 -0
- package/dist/types/session/types.d.ts +191 -0
- package/dist/types/tools/ToolNode.d.ts +12 -1
- package/dist/types/tools/eagerEventExecution.d.ts +23 -0
- package/dist/types/tools/streamedToolCallSeals.d.ts +13 -0
- package/dist/types/types/hitl.d.ts +4 -0
- package/dist/types/types/run.d.ts +11 -1
- package/dist/types/types/tools.d.ts +36 -0
- package/package.json +19 -2
- package/src/__tests__/stream.eagerEventExecution.test.ts +2571 -0
- package/src/events.ts +29 -0
- package/src/graphs/Graph.ts +224 -50
- package/src/graphs/MultiAgentGraph.ts +1 -1
- package/src/graphs/__tests__/composition.smoke.test.ts +30 -0
- package/src/index.ts +3 -0
- package/src/llm/anthropic/index.ts +356 -84
- package/src/llm/anthropic/llm.spec.ts +64 -0
- package/src/llm/custom-chat-models.smoke.test.ts +175 -4
- package/src/llm/openai/contentBlocks.test.ts +35 -0
- package/src/llm/openai/deepseek.test.ts +201 -2
- package/src/llm/openai/index.ts +171 -26
- package/src/llm/openai/utils/index.ts +22 -0
- package/src/llm/openrouter/index.ts +4 -2
- package/src/openai/__tests__/openai.test.ts +337 -0
- package/src/openai/index.ts +404 -0
- package/src/responses/__tests__/responses.test.ts +652 -0
- package/src/responses/index.ts +677 -0
- package/src/run.ts +158 -8
- package/src/scripts/compare_pi_vs_ours.ts +592 -173
- package/src/scripts/session_live.ts +548 -0
- package/src/session/AgentSession.ts +1432 -0
- package/src/session/JsonlSessionStore.ts +572 -0
- package/src/session/__tests__/JsonlSessionStore.test.ts +1410 -0
- package/src/session/__tests__/handlers.test.ts +161 -0
- package/src/session/handlers.ts +272 -0
- package/src/session/ids.ts +17 -0
- package/src/session/index.ts +44 -0
- package/src/session/messageSerialization.ts +207 -0
- package/src/session/types.ts +275 -0
- package/src/specs/custom-event-await.test.ts +89 -0
- package/src/specs/summarization.test.ts +1 -1
- package/src/stream.ts +756 -48
- package/src/summarization/node.ts +1 -1
- package/src/tools/ToolNode.ts +299 -126
- package/src/tools/__tests__/ToolNode.eagerEventExecution.test.ts +373 -0
- package/src/tools/__tests__/handlers.test.ts +2 -1
- package/src/tools/__tests__/hitl.test.ts +206 -110
- package/src/tools/eagerEventExecution.ts +153 -0
- package/src/tools/handlers.ts +8 -4
- package/src/tools/streamedToolCallSeals.ts +57 -0
- package/src/types/hitl.ts +4 -0
- package/src/types/run.ts +11 -0
- package/src/types/tools.ts +36 -0
- package/dist/cjs/llm/text.cjs +0 -69
- package/dist/cjs/llm/text.cjs.map +0 -1
- package/dist/esm/llm/text.mjs +0 -67
- package/dist/esm/llm/text.mjs.map +0 -1
|
@@ -6,17 +6,11 @@ import { isLangChainTool, convertToOpenAITool } from '@langchain/core/utils/func
|
|
|
6
6
|
import { ChatDeepSeek as ChatDeepSeek$1 } from '@langchain/deepseek';
|
|
7
7
|
import { getEndpoint, AzureChatOpenAI as AzureChatOpenAI$1, ChatOpenAI as ChatOpenAI$1, OpenAIClient, AzureChatOpenAICompletions, AzureChatOpenAIResponses, ChatOpenAIResponses, ChatOpenAICompletions, getHeadersWithUserAgent } from '@langchain/openai';
|
|
8
8
|
import { _convertMessagesToOpenAIParams, isReasoningModel } from './utils/index.mjs';
|
|
9
|
-
import '../../common/enum.mjs';
|
|
10
|
-
import 'nanoid';
|
|
11
|
-
import '../../messages/core.mjs';
|
|
12
|
-
import '@langchain/core/callbacks/dispatch';
|
|
13
|
-
import 'uuid';
|
|
14
|
-
import { sleep } from '../../utils/run.mjs';
|
|
15
|
-
import 'ai-tokenizer';
|
|
16
|
-
import 'zod-to-json-schema';
|
|
17
9
|
|
|
18
10
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
19
11
|
const iife = (fn) => fn();
|
|
12
|
+
const STREAM_CHUNK_MIN_SIZE = 4;
|
|
13
|
+
const STREAM_BOUNDARIES = new Set([' ', '.', ',', '!', '?', ';', ':']);
|
|
20
14
|
function isHeaders(headers) {
|
|
21
15
|
return (typeof Headers !== 'undefined' &&
|
|
22
16
|
headers !== null &&
|
|
@@ -237,14 +231,113 @@ function getCustomOpenAIClientOptions(owner, options) {
|
|
|
237
231
|
};
|
|
238
232
|
return requestOptions;
|
|
239
233
|
}
|
|
240
|
-
|
|
234
|
+
function findStreamChunkBoundary(text, minSize) {
|
|
235
|
+
if (minSize >= text.length) {
|
|
236
|
+
return text.length;
|
|
237
|
+
}
|
|
238
|
+
for (let position = minSize; position < text.length; position++) {
|
|
239
|
+
if (STREAM_BOUNDARIES.has(text[position])) {
|
|
240
|
+
return position + 1;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return text.length;
|
|
244
|
+
}
|
|
245
|
+
function splitStreamToken(text) {
|
|
246
|
+
const chunks = [];
|
|
247
|
+
let currentIndex = 0;
|
|
248
|
+
while (currentIndex < text.length) {
|
|
249
|
+
const remainingText = text.slice(currentIndex);
|
|
250
|
+
const chunkSize = findStreamChunkBoundary(remainingText, STREAM_CHUNK_MIN_SIZE);
|
|
251
|
+
chunks.push(text.slice(currentIndex, currentIndex + chunkSize));
|
|
252
|
+
currentIndex += chunkSize;
|
|
253
|
+
}
|
|
254
|
+
return chunks;
|
|
255
|
+
}
|
|
256
|
+
function splitTextGenerationChunk(chunk) {
|
|
257
|
+
const { message } = chunk;
|
|
258
|
+
if (!chunk.text ||
|
|
259
|
+
!(message instanceof AIMessageChunk) ||
|
|
260
|
+
typeof message.content !== 'string' ||
|
|
261
|
+
message.content !== chunk.text ||
|
|
262
|
+
chunk.generationInfo?.logprobs != null ||
|
|
263
|
+
chunk.generationInfo?.finish_reason != null) {
|
|
264
|
+
return [chunk];
|
|
265
|
+
}
|
|
266
|
+
const tokenChunks = splitStreamToken(chunk.text);
|
|
267
|
+
if (tokenChunks.length <= 1) {
|
|
268
|
+
return [chunk];
|
|
269
|
+
}
|
|
270
|
+
let emittedUsage = false;
|
|
271
|
+
return tokenChunks.map((token) => {
|
|
272
|
+
const usageMetadata = emittedUsage && message.usage_metadata != null
|
|
273
|
+
? undefined
|
|
274
|
+
: message.usage_metadata;
|
|
275
|
+
if (message.usage_metadata != null && !emittedUsage) {
|
|
276
|
+
emittedUsage = true;
|
|
277
|
+
}
|
|
278
|
+
return new ChatGenerationChunk({
|
|
279
|
+
text: token,
|
|
280
|
+
generationInfo: chunk.generationInfo,
|
|
281
|
+
message: new AIMessageChunk(Object.assign({}, message, {
|
|
282
|
+
content: token,
|
|
283
|
+
usage_metadata: usageMetadata,
|
|
284
|
+
})),
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async function emitStreamChunkCallback(chunk, runManager) {
|
|
289
|
+
await runManager?.handleLLMNewToken(chunk.text, getStreamChunkTokenIndices(chunk), undefined, undefined, undefined, { chunk });
|
|
290
|
+
}
|
|
291
|
+
function getStreamChunkTokenIndices(chunk) {
|
|
292
|
+
const prompt = chunk.generationInfo?.prompt;
|
|
293
|
+
const completion = chunk.generationInfo?.completion;
|
|
294
|
+
if (typeof prompt === 'number' && typeof completion === 'number') {
|
|
295
|
+
return { prompt, completion };
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
async function* delayStreamChunks(chunks, delay, signal, runManager) {
|
|
300
|
+
let lastYieldedAt;
|
|
241
301
|
for await (const chunk of chunks) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
302
|
+
const outputChunks = delay != null && delay > 0 ? splitTextGenerationChunk(chunk) : [chunk];
|
|
303
|
+
for (const outputChunk of outputChunks) {
|
|
304
|
+
signal?.throwIfAborted();
|
|
305
|
+
if (delay != null && delay > 0 && lastYieldedAt != null) {
|
|
306
|
+
const timeSinceLastYield = Date.now() - lastYieldedAt;
|
|
307
|
+
const timeToWait = Math.max(0, delay - timeSinceLastYield);
|
|
308
|
+
if (timeToWait > 0) {
|
|
309
|
+
await sleepWithAbort(timeToWait, signal);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
signal?.throwIfAborted();
|
|
313
|
+
lastYieldedAt = Date.now();
|
|
314
|
+
await emitStreamChunkCallback(outputChunk, runManager);
|
|
315
|
+
signal?.throwIfAborted();
|
|
316
|
+
yield outputChunk;
|
|
245
317
|
}
|
|
246
318
|
}
|
|
247
319
|
}
|
|
320
|
+
async function sleepWithAbort(delay, signal) {
|
|
321
|
+
if (delay <= 0) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
signal?.throwIfAborted();
|
|
325
|
+
await new Promise((resolve, reject) => {
|
|
326
|
+
const timeout = setTimeout(() => {
|
|
327
|
+
signal?.removeEventListener('abort', onAbort);
|
|
328
|
+
resolve();
|
|
329
|
+
}, delay);
|
|
330
|
+
const onAbort = () => {
|
|
331
|
+
clearTimeout(timeout);
|
|
332
|
+
signal?.removeEventListener('abort', onAbort);
|
|
333
|
+
reject(signal?.reason ?? new Error('AbortError: User aborted request.'));
|
|
334
|
+
};
|
|
335
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
336
|
+
if (signal?.aborted === true) {
|
|
337
|
+
onAbort();
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
248
341
|
function createAbortHandler(controller) {
|
|
249
342
|
return function () {
|
|
250
343
|
controller.abort();
|
|
@@ -279,7 +372,7 @@ class CustomOpenAIClient extends OpenAIClient {
|
|
|
279
372
|
this.abortHandler = handler;
|
|
280
373
|
if (signal)
|
|
281
374
|
signal.addEventListener('abort', handler, { once: true });
|
|
282
|
-
const timeout = setTimeout(
|
|
375
|
+
const timeout = setTimeout(handler, ms);
|
|
283
376
|
const fetchOptions = {
|
|
284
377
|
signal: controller.signal,
|
|
285
378
|
...options,
|
|
@@ -306,7 +399,7 @@ class CustomAzureOpenAIClient extends AzureOpenAI {
|
|
|
306
399
|
this.abortHandler = handler;
|
|
307
400
|
if (signal)
|
|
308
401
|
signal.addEventListener('abort', handler, { once: true });
|
|
309
|
-
const timeout = setTimeout(
|
|
402
|
+
const timeout = setTimeout(handler, ms);
|
|
310
403
|
const fetchOptions = {
|
|
311
404
|
signal: controller.signal,
|
|
312
405
|
...options,
|
|
@@ -773,7 +866,7 @@ class ChatOpenAI extends ChatOpenAI$1 {
|
|
|
773
866
|
return this.getReasoningParams(options);
|
|
774
867
|
}
|
|
775
868
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
776
|
-
yield* delayStreamChunks(super._streamResponseChunks(messages, options,
|
|
869
|
+
yield* delayStreamChunks(super._streamResponseChunks(messages, options, undefined), this._lc_stream_delay, options.signal, runManager);
|
|
777
870
|
}
|
|
778
871
|
}
|
|
779
872
|
class AzureChatOpenAI extends AzureChatOpenAI$1 {
|
|
@@ -853,7 +946,7 @@ class AzureChatOpenAI extends AzureChatOpenAI$1 {
|
|
|
853
946
|
return requestOptions;
|
|
854
947
|
}
|
|
855
948
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
856
|
-
yield* delayStreamChunks(super._streamResponseChunks(messages, options,
|
|
949
|
+
yield* delayStreamChunks(super._streamResponseChunks(messages, options, undefined), this._lc_stream_delay, options.signal, runManager);
|
|
857
950
|
}
|
|
858
951
|
}
|
|
859
952
|
class ChatDeepSeek extends ChatDeepSeek$1 {
|
|
@@ -940,7 +1033,7 @@ class ChatDeepSeek extends ChatDeepSeek$1 {
|
|
|
940
1033
|
return requestOptions;
|
|
941
1034
|
}
|
|
942
1035
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
943
|
-
yield* delayStreamChunks(this._streamResponseChunksWithReasoning(messages, options,
|
|
1036
|
+
yield* delayStreamChunks(this._streamResponseChunksWithReasoning(messages, options, undefined), this._lc_stream_delay, options.signal, runManager);
|
|
944
1037
|
}
|
|
945
1038
|
/** Parses raw `<think>` fallback tags across chunks and emits sanitized DeepSeek stream chunks. */
|
|
946
1039
|
async *_streamResponseChunksWithReasoning(messages, options, runManager) {
|
|
@@ -1154,12 +1247,7 @@ class ChatDeepSeek extends ChatDeepSeek$1 {
|
|
|
1154
1247
|
await runManager?.handleLLMNewToken(chunk.text, this._getDeepSeekTokenIndices(chunk), undefined, undefined, undefined, { chunk });
|
|
1155
1248
|
}
|
|
1156
1249
|
_getDeepSeekTokenIndices(chunk) {
|
|
1157
|
-
|
|
1158
|
-
const completion = chunk.generationInfo?.completion;
|
|
1159
|
-
if (typeof prompt === 'number' && typeof completion === 'number') {
|
|
1160
|
-
return { prompt, completion };
|
|
1161
|
-
}
|
|
1162
|
-
return undefined;
|
|
1250
|
+
return getStreamChunkTokenIndices(chunk);
|
|
1163
1251
|
}
|
|
1164
1252
|
_getDeepSeekPartialTagSplitIndex(text, tag) {
|
|
1165
1253
|
for (let i = tag.length - 1; i >= 1; i--) {
|
|
@@ -1227,9 +1315,9 @@ class ChatXAI extends ChatXAI$1 {
|
|
|
1227
1315
|
return requestOptions;
|
|
1228
1316
|
}
|
|
1229
1317
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
1230
|
-
yield* delayStreamChunks(super._streamResponseChunks(messages, options,
|
|
1318
|
+
yield* delayStreamChunks(super._streamResponseChunks(messages, options, undefined), this._lc_stream_delay, options.signal, runManager);
|
|
1231
1319
|
}
|
|
1232
1320
|
}
|
|
1233
1321
|
|
|
1234
|
-
export { AzureChatOpenAI, ChatDeepSeek, ChatMoonshot, ChatOpenAI, ChatXAI, CustomAzureOpenAIClient, CustomOpenAIClient, _convertToOpenAITool, isHeaders, normalizeHeaders };
|
|
1322
|
+
export { AzureChatOpenAI, ChatDeepSeek, ChatMoonshot, ChatOpenAI, ChatXAI, CustomAzureOpenAIClient, CustomOpenAIClient, _convertToOpenAITool, emitStreamChunkCallback, isHeaders, normalizeHeaders };
|
|
1235
1323
|
//# sourceMappingURL=index.mjs.map
|