@librechat/agents 2.4.318 → 2.4.319

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/cjs/events.cjs +3 -3
  2. package/dist/cjs/events.cjs.map +1 -1
  3. package/dist/cjs/main.cjs +5 -2
  4. package/dist/cjs/main.cjs.map +1 -1
  5. package/dist/cjs/messages/ids.cjs +23 -0
  6. package/dist/cjs/messages/ids.cjs.map +1 -0
  7. package/dist/cjs/stream.cjs +8 -155
  8. package/dist/cjs/stream.cjs.map +1 -1
  9. package/dist/cjs/tools/handlers.cjs +144 -0
  10. package/dist/cjs/tools/handlers.cjs.map +1 -0
  11. package/dist/cjs/tools/search/tool.cjs +2 -4
  12. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  13. package/dist/esm/events.mjs +1 -1
  14. package/dist/esm/events.mjs.map +1 -1
  15. package/dist/esm/main.mjs +3 -1
  16. package/dist/esm/main.mjs.map +1 -1
  17. package/dist/esm/messages/ids.mjs +21 -0
  18. package/dist/esm/messages/ids.mjs.map +1 -0
  19. package/dist/esm/stream.mjs +7 -152
  20. package/dist/esm/stream.mjs.map +1 -1
  21. package/dist/esm/tools/handlers.mjs +141 -0
  22. package/dist/esm/tools/handlers.mjs.map +1 -0
  23. package/dist/esm/tools/search/tool.mjs +2 -4
  24. package/dist/esm/tools/search/tool.mjs.map +1 -1
  25. package/dist/types/index.d.ts +1 -0
  26. package/dist/types/messages/ids.d.ts +3 -0
  27. package/dist/types/messages/index.d.ts +1 -0
  28. package/dist/types/stream.d.ts +0 -8
  29. package/dist/types/tools/handlers.d.ts +8 -0
  30. package/dist/types/tools/search/types.d.ts +8 -7
  31. package/package.json +1 -1
  32. package/src/events.ts +49 -15
  33. package/src/index.ts +1 -0
  34. package/src/messages/ids.ts +26 -0
  35. package/src/messages/index.ts +1 -0
  36. package/src/stream.ts +4 -186
  37. package/src/tools/handlers.ts +167 -0
  38. package/src/tools/search/tool.ts +2 -4
  39. package/src/tools/search/types.ts +9 -9
@@ -0,0 +1,26 @@
1
+ // src/stream.ts
2
+ import { nanoid } from 'nanoid';
3
+ import type { Graph } from '@/graphs';
4
+ import type * as t from '@/types';
5
+
6
+ export const getMessageId = (
7
+ stepKey: string,
8
+ graph: Graph<t.BaseGraphState>,
9
+ returnExistingId = false
10
+ ): string | undefined => {
11
+ const messageId = graph.messageIdsByStepKey.get(stepKey);
12
+ if (messageId != null && messageId) {
13
+ return returnExistingId ? messageId : undefined;
14
+ }
15
+
16
+ const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);
17
+ if (prelimMessageId != null && prelimMessageId) {
18
+ graph.prelimMessageIdsByStepKey.delete(stepKey);
19
+ graph.messageIdsByStepKey.set(stepKey, prelimMessageId);
20
+ return prelimMessageId;
21
+ }
22
+
23
+ const message_id = `msg_${nanoid()}`;
24
+ graph.messageIdsByStepKey.set(stepKey, message_id);
25
+ return message_id;
26
+ };
@@ -1,3 +1,4 @@
1
1
  export * from './core';
2
+ export * from './ids';
2
3
  export * from './prune';
3
4
  export * from './format';
package/src/stream.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  // src/stream.ts
2
- import { nanoid } from 'nanoid';
3
2
  import type { AIMessageChunk } from '@langchain/core/messages';
4
- import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
3
+ import type { ToolCall } from '@langchain/core/messages/tool';
5
4
  import type { Graph } from '@/graphs';
6
5
  import type * as t from '@/types';
7
6
  import { StepTypes, ContentTypes, GraphEvents, ToolCallTypes } from '@/common';
7
+ import { handleToolCalls, handleToolCallChunks } from '@/tools/handlers';
8
+ import { getMessageId } from '@/messages';
8
9
 
9
10
  /**
10
11
  * Parses content to extract thinking sections enclosed in <think> tags using string operations
@@ -65,107 +66,6 @@ function getNonEmptyValue(possibleValues: string[]): string | undefined {
65
66
  }
66
67
  return undefined;
67
68
  }
68
-
69
- export const getMessageId = (
70
- stepKey: string,
71
- graph: Graph<t.BaseGraphState>,
72
- returnExistingId = false
73
- ): string | undefined => {
74
- const messageId = graph.messageIdsByStepKey.get(stepKey);
75
- if (messageId != null && messageId) {
76
- return returnExistingId ? messageId : undefined;
77
- }
78
-
79
- const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);
80
- if (prelimMessageId != null && prelimMessageId) {
81
- graph.prelimMessageIdsByStepKey.delete(stepKey);
82
- graph.messageIdsByStepKey.set(stepKey, prelimMessageId);
83
- return prelimMessageId;
84
- }
85
-
86
- const message_id = `msg_${nanoid()}`;
87
- graph.messageIdsByStepKey.set(stepKey, message_id);
88
- return message_id;
89
- };
90
-
91
- export const handleToolCalls = (
92
- toolCalls?: ToolCall[],
93
- metadata?: Record<string, unknown>,
94
- graph?: Graph
95
- ): void => {
96
- if (!graph || !metadata) {
97
- console.warn(`Graph or metadata not found in ${event} event`);
98
- return;
99
- }
100
-
101
- if (!toolCalls) {
102
- return;
103
- }
104
-
105
- if (toolCalls.length === 0) {
106
- return;
107
- }
108
-
109
- const stepKey = graph.getStepKey(metadata);
110
-
111
- for (const tool_call of toolCalls) {
112
- const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
113
- tool_call.id = toolCallId;
114
- if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
115
- continue;
116
- }
117
-
118
- let prevStepId = '';
119
- let prevRunStep: t.RunStep | undefined;
120
- try {
121
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
122
- prevRunStep = graph.getRunStep(prevStepId);
123
- } catch {
124
- // no previous step
125
- }
126
-
127
- const dispatchToolCallIds = (lastMessageStepId: string): void => {
128
- graph.dispatchMessageDelta(lastMessageStepId, {
129
- content: [
130
- {
131
- type: 'text',
132
- text: '',
133
- tool_call_ids: [toolCallId],
134
- },
135
- ],
136
- });
137
- };
138
- /* If the previous step exists and is a message creation */
139
- if (
140
- prevStepId &&
141
- prevRunStep &&
142
- prevRunStep.type === StepTypes.MESSAGE_CREATION
143
- ) {
144
- dispatchToolCallIds(prevStepId);
145
- graph.messageStepHasToolCalls.set(prevStepId, true);
146
- /* If the previous step doesn't exist or is not a message creation */
147
- } else if (
148
- !prevRunStep ||
149
- prevRunStep.type !== StepTypes.MESSAGE_CREATION
150
- ) {
151
- const messageId = getMessageId(stepKey, graph, true) ?? '';
152
- const stepId = graph.dispatchRunStep(stepKey, {
153
- type: StepTypes.MESSAGE_CREATION,
154
- message_creation: {
155
- message_id: messageId,
156
- },
157
- });
158
- dispatchToolCallIds(stepId);
159
- graph.messageStepHasToolCalls.set(prevStepId, true);
160
- }
161
-
162
- graph.dispatchRunStep(stepKey, {
163
- type: StepTypes.TOOL_CALLS,
164
- tool_calls: [tool_call],
165
- });
166
- }
167
- };
168
-
169
69
  export class ChatModelStreamHandler implements t.EventHandler {
170
70
  handle(
171
71
  event: string,
@@ -230,7 +130,7 @@ export class ChatModelStreamHandler implements t.EventHandler {
230
130
  chunk.tool_call_chunks.length &&
231
131
  typeof chunk.tool_call_chunks[0]?.index === 'number'
232
132
  ) {
233
- this.handleToolCallChunks({
133
+ handleToolCallChunks({
234
134
  graph,
235
135
  stepKey,
236
136
  toolCallChunks: chunk.tool_call_chunks,
@@ -358,88 +258,6 @@ hasToolCallChunks: ${hasToolCallChunks}
358
258
  });
359
259
  }
360
260
  }
361
- handleToolCallChunks = ({
362
- graph,
363
- stepKey,
364
- toolCallChunks,
365
- }: {
366
- graph: Graph;
367
- stepKey: string;
368
- toolCallChunks: ToolCallChunk[];
369
- }): void => {
370
- let prevStepId: string;
371
- let prevRunStep: t.RunStep | undefined;
372
- try {
373
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
374
- prevRunStep = graph.getRunStep(prevStepId);
375
- } catch {
376
- /** Edge Case: If no previous step exists, create a new message creation step */
377
- const message_id = getMessageId(stepKey, graph, true) ?? '';
378
- prevStepId = graph.dispatchRunStep(stepKey, {
379
- type: StepTypes.MESSAGE_CREATION,
380
- message_creation: {
381
- message_id,
382
- },
383
- });
384
- prevRunStep = graph.getRunStep(prevStepId);
385
- }
386
-
387
- const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
388
-
389
- /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
390
- const tool_calls: ToolCall[] | undefined =
391
- prevStepId &&
392
- prevRunStep &&
393
- prevRunStep.type === StepTypes.MESSAGE_CREATION
394
- ? []
395
- : undefined;
396
-
397
- /** Edge Case: `id` and `name` fields cannot be empty strings */
398
- for (const toolCallChunk of toolCallChunks) {
399
- if (toolCallChunk.name === '') {
400
- toolCallChunk.name = undefined;
401
- }
402
- if (toolCallChunk.id === '') {
403
- toolCallChunk.id = undefined;
404
- } else if (
405
- tool_calls != null &&
406
- toolCallChunk.id != null &&
407
- toolCallChunk.name != null
408
- ) {
409
- tool_calls.push({
410
- args: {},
411
- id: toolCallChunk.id,
412
- name: toolCallChunk.name,
413
- type: ToolCallTypes.TOOL_CALL,
414
- });
415
- }
416
- }
417
-
418
- let stepId: string = _stepId;
419
- const alreadyDispatched =
420
- prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
421
- graph.messageStepHasToolCalls.has(prevStepId);
422
- if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
423
- graph.dispatchMessageDelta(prevStepId, {
424
- content: [
425
- {
426
- type: ContentTypes.TEXT,
427
- text: '',
428
- tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
429
- },
430
- ],
431
- });
432
- graph.messageStepHasToolCalls.set(prevStepId, true);
433
- stepId = graph.dispatchRunStep(stepKey, {
434
- type: StepTypes.TOOL_CALLS,
435
- tool_calls,
436
- });
437
- }
438
- graph.dispatchRunStepDelta(stepId, {
439
- type: StepTypes.TOOL_CALLS,
440
- tool_calls: toolCallChunks,
441
- });
442
- };
443
261
  handleReasoning(chunk: Partial<AIMessageChunk>, graph: Graph): void {
444
262
  let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey] as
445
263
  | string
@@ -0,0 +1,167 @@
1
+ /* eslint-disable no-console */
2
+ // src/tools/handlers.ts
3
+ import { nanoid } from 'nanoid';
4
+ import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
5
+ import type { Graph } from '@/graphs';
6
+ import type * as t from '@/types';
7
+ import { StepTypes, ContentTypes, ToolCallTypes } from '@/common';
8
+ import { getMessageId } from '@/messages';
9
+
10
+ export function handleToolCallChunks({
11
+ graph,
12
+ stepKey,
13
+ toolCallChunks,
14
+ }: {
15
+ graph: Graph;
16
+ stepKey: string;
17
+ toolCallChunks: ToolCallChunk[];
18
+ }): void {
19
+ let prevStepId: string;
20
+ let prevRunStep: t.RunStep | undefined;
21
+ try {
22
+ prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
23
+ prevRunStep = graph.getRunStep(prevStepId);
24
+ } catch {
25
+ /** Edge Case: If no previous step exists, create a new message creation step */
26
+ const message_id = getMessageId(stepKey, graph, true) ?? '';
27
+ prevStepId = graph.dispatchRunStep(stepKey, {
28
+ type: StepTypes.MESSAGE_CREATION,
29
+ message_creation: {
30
+ message_id,
31
+ },
32
+ });
33
+ prevRunStep = graph.getRunStep(prevStepId);
34
+ }
35
+
36
+ const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
37
+
38
+ /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
39
+ const tool_calls: ToolCall[] | undefined =
40
+ prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION
41
+ ? []
42
+ : undefined;
43
+
44
+ /** Edge Case: `id` and `name` fields cannot be empty strings */
45
+ for (const toolCallChunk of toolCallChunks) {
46
+ if (toolCallChunk.name === '') {
47
+ toolCallChunk.name = undefined;
48
+ }
49
+ if (toolCallChunk.id === '') {
50
+ toolCallChunk.id = undefined;
51
+ } else if (
52
+ tool_calls != null &&
53
+ toolCallChunk.id != null &&
54
+ toolCallChunk.name != null
55
+ ) {
56
+ tool_calls.push({
57
+ args: {},
58
+ id: toolCallChunk.id,
59
+ name: toolCallChunk.name,
60
+ type: ToolCallTypes.TOOL_CALL,
61
+ });
62
+ }
63
+ }
64
+
65
+ let stepId: string = _stepId;
66
+ const alreadyDispatched =
67
+ prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
68
+ graph.messageStepHasToolCalls.has(prevStepId);
69
+ if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
70
+ graph.dispatchMessageDelta(prevStepId, {
71
+ content: [
72
+ {
73
+ type: ContentTypes.TEXT,
74
+ text: '',
75
+ tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
76
+ },
77
+ ],
78
+ });
79
+ graph.messageStepHasToolCalls.set(prevStepId, true);
80
+ stepId = graph.dispatchRunStep(stepKey, {
81
+ type: StepTypes.TOOL_CALLS,
82
+ tool_calls,
83
+ });
84
+ }
85
+ graph.dispatchRunStepDelta(stepId, {
86
+ type: StepTypes.TOOL_CALLS,
87
+ tool_calls: toolCallChunks,
88
+ });
89
+ }
90
+
91
+ export const handleToolCalls = (
92
+ toolCalls?: ToolCall[],
93
+ metadata?: Record<string, unknown>,
94
+ graph?: Graph
95
+ ): void => {
96
+ if (!graph || !metadata) {
97
+ console.warn(`Graph or metadata not found in ${event} event`);
98
+ return;
99
+ }
100
+
101
+ if (!toolCalls) {
102
+ return;
103
+ }
104
+
105
+ if (toolCalls.length === 0) {
106
+ return;
107
+ }
108
+
109
+ const stepKey = graph.getStepKey(metadata);
110
+
111
+ for (const tool_call of toolCalls) {
112
+ const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
113
+ tool_call.id = toolCallId;
114
+ if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
115
+ continue;
116
+ }
117
+
118
+ let prevStepId = '';
119
+ let prevRunStep: t.RunStep | undefined;
120
+ try {
121
+ prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
122
+ prevRunStep = graph.getRunStep(prevStepId);
123
+ } catch {
124
+ // no previous step
125
+ }
126
+
127
+ const dispatchToolCallIds = (lastMessageStepId: string): void => {
128
+ graph.dispatchMessageDelta(lastMessageStepId, {
129
+ content: [
130
+ {
131
+ type: 'text',
132
+ text: '',
133
+ tool_call_ids: [toolCallId],
134
+ },
135
+ ],
136
+ });
137
+ };
138
+ /* If the previous step exists and is a message creation */
139
+ if (
140
+ prevStepId &&
141
+ prevRunStep &&
142
+ prevRunStep.type === StepTypes.MESSAGE_CREATION
143
+ ) {
144
+ dispatchToolCallIds(prevStepId);
145
+ graph.messageStepHasToolCalls.set(prevStepId, true);
146
+ /* If the previous step doesn't exist or is not a message creation */
147
+ } else if (
148
+ !prevRunStep ||
149
+ prevRunStep.type !== StepTypes.MESSAGE_CREATION
150
+ ) {
151
+ const messageId = getMessageId(stepKey, graph, true) ?? '';
152
+ const stepId = graph.dispatchRunStep(stepKey, {
153
+ type: StepTypes.MESSAGE_CREATION,
154
+ message_creation: {
155
+ message_id: messageId,
156
+ },
157
+ });
158
+ dispatchToolCallIds(stepId);
159
+ graph.messageStepHasToolCalls.set(prevStepId, true);
160
+ }
161
+
162
+ graph.dispatchRunStep(stepKey, {
163
+ type: StepTypes.TOOL_CALLS,
164
+ tool_calls: [tool_call],
165
+ });
166
+ }
167
+ };
@@ -170,10 +170,8 @@ export const createSearchTool = (
170
170
  });
171
171
  const turn = runnableConfig.toolCall?.turn ?? 0;
172
172
  const { output, references } = formatResultsForLLM(turn, searchResult);
173
- return [
174
- output,
175
- { [Constants.WEB_SEARCH]: { turn, ...searchResult, references } },
176
- ];
173
+ const data: t.SearchResultData = { turn, ...searchResult, references };
174
+ return [output, { [Constants.WEB_SEARCH]: data }];
177
175
  },
178
176
  {
179
177
  name: Constants.WEB_SEARCH,
@@ -27,6 +27,7 @@ export type ResultReference = {
27
27
  attribution?: string;
28
28
  };
29
29
  export interface SearchResultData {
30
+ turn?: number;
30
31
  organic?: ProcessedOrganic[];
31
32
  topStories?: ProcessedTopStory[];
32
33
  images?: ImageResult[];
@@ -98,15 +99,6 @@ export interface ScraperExtractionResult {
98
99
  no_extraction: ScraperContentResult;
99
100
  }
100
101
 
101
- // Define type for SearXNG result
102
- export interface SearXNGResult {
103
- title?: string;
104
- url?: string;
105
- content?: string;
106
- publishedDate?: string;
107
- img_src?: string;
108
- }
109
-
110
102
  export interface JinaRerankerResult {
111
103
  index: number;
112
104
  relevance_score: number;
@@ -555,3 +547,11 @@ export interface SearxNGSearchPayload {
555
547
  */
556
548
  disabled_engines?: string;
557
549
  }
550
+
551
+ export interface SearXNGResult {
552
+ title?: string;
553
+ url?: string;
554
+ content?: string;
555
+ publishedDate?: string;
556
+ img_src?: string;
557
+ }