@librechat/agents 2.4.30 → 2.4.33

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 (124) hide show
  1. package/dist/cjs/common/enum.cjs +1 -0
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/events.cjs +3 -3
  4. package/dist/cjs/events.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +2 -1
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/main.cjs +7 -2
  8. package/dist/cjs/main.cjs.map +1 -1
  9. package/dist/cjs/messages/ids.cjs +23 -0
  10. package/dist/cjs/messages/ids.cjs.map +1 -0
  11. package/dist/cjs/splitStream.cjs +2 -1
  12. package/dist/cjs/splitStream.cjs.map +1 -1
  13. package/dist/cjs/stream.cjs +87 -154
  14. package/dist/cjs/stream.cjs.map +1 -1
  15. package/dist/cjs/tools/ToolNode.cjs +14 -3
  16. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  17. package/dist/cjs/tools/handlers.cjs +144 -0
  18. package/dist/cjs/tools/handlers.cjs.map +1 -0
  19. package/dist/cjs/tools/search/content.cjs +140 -0
  20. package/dist/cjs/tools/search/content.cjs.map +1 -0
  21. package/dist/cjs/tools/search/firecrawl.cjs +131 -0
  22. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
  23. package/dist/cjs/tools/search/format.cjs +203 -0
  24. package/dist/cjs/tools/search/format.cjs.map +1 -0
  25. package/dist/cjs/tools/search/highlights.cjs +245 -0
  26. package/dist/cjs/tools/search/highlights.cjs.map +1 -0
  27. package/dist/cjs/tools/search/rerankers.cjs +194 -0
  28. package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
  29. package/dist/cjs/tools/search/schema.cjs +70 -0
  30. package/dist/cjs/tools/search/schema.cjs.map +1 -0
  31. package/dist/cjs/tools/search/search.cjs +491 -0
  32. package/dist/cjs/tools/search/search.cjs.map +1 -0
  33. package/dist/cjs/tools/search/tool.cjs +292 -0
  34. package/dist/cjs/tools/search/tool.cjs.map +1 -0
  35. package/dist/cjs/tools/search/utils.cjs +66 -0
  36. package/dist/cjs/tools/search/utils.cjs.map +1 -0
  37. package/dist/esm/common/enum.mjs +1 -0
  38. package/dist/esm/common/enum.mjs.map +1 -1
  39. package/dist/esm/events.mjs +1 -1
  40. package/dist/esm/events.mjs.map +1 -1
  41. package/dist/esm/graphs/Graph.mjs +2 -1
  42. package/dist/esm/graphs/Graph.mjs.map +1 -1
  43. package/dist/esm/main.mjs +4 -1
  44. package/dist/esm/main.mjs.map +1 -1
  45. package/dist/esm/messages/ids.mjs +21 -0
  46. package/dist/esm/messages/ids.mjs.map +1 -0
  47. package/dist/esm/splitStream.mjs +2 -1
  48. package/dist/esm/splitStream.mjs.map +1 -1
  49. package/dist/esm/stream.mjs +87 -152
  50. package/dist/esm/stream.mjs.map +1 -1
  51. package/dist/esm/tools/ToolNode.mjs +14 -3
  52. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  53. package/dist/esm/tools/handlers.mjs +141 -0
  54. package/dist/esm/tools/handlers.mjs.map +1 -0
  55. package/dist/esm/tools/search/content.mjs +119 -0
  56. package/dist/esm/tools/search/content.mjs.map +1 -0
  57. package/dist/esm/tools/search/firecrawl.mjs +128 -0
  58. package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
  59. package/dist/esm/tools/search/format.mjs +201 -0
  60. package/dist/esm/tools/search/format.mjs.map +1 -0
  61. package/dist/esm/tools/search/highlights.mjs +243 -0
  62. package/dist/esm/tools/search/highlights.mjs.map +1 -0
  63. package/dist/esm/tools/search/rerankers.mjs +188 -0
  64. package/dist/esm/tools/search/rerankers.mjs.map +1 -0
  65. package/dist/esm/tools/search/schema.mjs +61 -0
  66. package/dist/esm/tools/search/schema.mjs.map +1 -0
  67. package/dist/esm/tools/search/search.mjs +488 -0
  68. package/dist/esm/tools/search/search.mjs.map +1 -0
  69. package/dist/esm/tools/search/tool.mjs +290 -0
  70. package/dist/esm/tools/search/tool.mjs.map +1 -0
  71. package/dist/esm/tools/search/utils.mjs +61 -0
  72. package/dist/esm/tools/search/utils.mjs.map +1 -0
  73. package/dist/types/common/enum.d.ts +1 -0
  74. package/dist/types/graphs/Graph.d.ts +1 -1
  75. package/dist/types/index.d.ts +2 -0
  76. package/dist/types/messages/ids.d.ts +3 -0
  77. package/dist/types/messages/index.d.ts +1 -0
  78. package/dist/types/scripts/search.d.ts +1 -0
  79. package/dist/types/stream.d.ts +0 -8
  80. package/dist/types/tools/ToolNode.d.ts +6 -0
  81. package/dist/types/tools/example.d.ts +23 -3
  82. package/dist/types/tools/handlers.d.ts +8 -0
  83. package/dist/types/tools/search/content.d.ts +4 -0
  84. package/dist/types/tools/search/firecrawl.d.ts +38 -0
  85. package/dist/types/tools/search/format.d.ts +5 -0
  86. package/dist/types/tools/search/highlights.d.ts +13 -0
  87. package/dist/types/tools/search/index.d.ts +2 -0
  88. package/dist/types/tools/search/rerankers.d.ts +36 -0
  89. package/dist/types/tools/search/schema.d.ts +16 -0
  90. package/dist/types/tools/search/search.d.ts +9 -0
  91. package/dist/types/tools/search/test.d.ts +1 -0
  92. package/dist/types/tools/search/tool.d.ts +33 -0
  93. package/dist/types/tools/search/types.d.ts +540 -0
  94. package/dist/types/tools/search/utils.d.ts +10 -0
  95. package/package.json +10 -7
  96. package/src/common/enum.ts +1 -0
  97. package/src/events.ts +49 -15
  98. package/src/graphs/Graph.ts +6 -2
  99. package/src/index.ts +2 -0
  100. package/src/messages/ids.ts +26 -0
  101. package/src/messages/index.ts +1 -0
  102. package/src/scripts/search.ts +146 -0
  103. package/src/splitStream.test.ts +132 -71
  104. package/src/splitStream.ts +2 -1
  105. package/src/stream.ts +94 -183
  106. package/src/tools/ToolNode.ts +37 -14
  107. package/src/tools/handlers.ts +167 -0
  108. package/src/tools/search/content.test.ts +173 -0
  109. package/src/tools/search/content.ts +147 -0
  110. package/src/tools/search/firecrawl.ts +158 -0
  111. package/src/tools/search/format.ts +252 -0
  112. package/src/tools/search/highlights.ts +320 -0
  113. package/src/tools/search/index.ts +2 -0
  114. package/src/tools/search/output.md +2775 -0
  115. package/src/tools/search/rerankers.ts +269 -0
  116. package/src/tools/search/schema.ts +63 -0
  117. package/src/tools/search/search.ts +680 -0
  118. package/src/tools/search/test.html +884 -0
  119. package/src/tools/search/test.md +643 -0
  120. package/src/tools/search/test.ts +159 -0
  121. package/src/tools/search/tool.ts +427 -0
  122. package/src/tools/search/types.ts +621 -0
  123. package/src/tools/search/utils.ts +79 -0
  124. package/src/utils/llmConfig.ts +1 -1
package/src/events.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  /* eslint-disable no-console */
2
2
  // src/events.ts
3
- import type { UsageMetadata, BaseMessageFields } from '@langchain/core/messages';
3
+ import type {
4
+ UsageMetadata,
5
+ BaseMessageFields,
6
+ } from '@langchain/core/messages';
4
7
  import type { Graph } from '@/graphs';
5
8
  import type * as t from '@/types';
6
- import { handleToolCalls } from '@/stream';
9
+ import { handleToolCalls } from '@/tools/handlers';
7
10
  import { Providers } from '@/common';
8
11
 
9
12
  export class HandlerRegistry {
@@ -27,7 +30,12 @@ export class ModelEndHandler implements t.EventHandler {
27
30
  this.collectedUsage = collectedUsage;
28
31
  }
29
32
 
30
- handle(event: string, data: t.ModelEndData, metadata?: Record<string, unknown>, graph?: Graph): void {
33
+ handle(
34
+ event: string,
35
+ data: t.ModelEndData,
36
+ metadata?: Record<string, unknown>,
37
+ graph?: Graph
38
+ ): void {
31
39
  if (!graph || !metadata) {
32
40
  console.warn(`Graph or metadata not found in ${event} event`);
33
41
  return;
@@ -43,9 +51,12 @@ export class ModelEndHandler implements t.EventHandler {
43
51
  }
44
52
 
45
53
  console.log(`====== ${event.toUpperCase()} ======`);
46
- console.dir({
47
- usage,
48
- }, { depth: null });
54
+ console.dir(
55
+ {
56
+ usage,
57
+ },
58
+ { depth: null }
59
+ );
49
60
 
50
61
  if (metadata.provider !== Providers.GOOGLE) {
51
62
  return;
@@ -60,7 +71,12 @@ export class ToolEndHandler implements t.EventHandler {
60
71
  constructor(callback?: t.ToolEndCallback) {
61
72
  this.callback = callback;
62
73
  }
63
- handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>, graph?: Graph): void {
74
+ handle(
75
+ event: string,
76
+ data: t.StreamEventData | undefined,
77
+ metadata?: Record<string, unknown>,
78
+ graph?: Graph
79
+ ): void {
64
80
  if (!graph || !metadata) {
65
81
  console.warn(`Graph or metadata not found in ${event} event`);
66
82
  return;
@@ -74,14 +90,17 @@ export class ToolEndHandler implements t.EventHandler {
74
90
 
75
91
  this.callback?.(toolEndData, metadata);
76
92
 
77
- graph.handleToolCallCompleted({ input: toolEndData.input, output: toolEndData.output }, metadata);
93
+ graph.handleToolCallCompleted(
94
+ { input: toolEndData.input, output: toolEndData.output },
95
+ metadata
96
+ );
78
97
  }
79
98
  }
80
99
 
81
100
  export class TestLLMStreamHandler implements t.EventHandler {
82
101
  handle(event: string, data: t.StreamEventData | undefined): void {
83
102
  const chunk = data?.chunk;
84
- const isMessageChunk = !!(chunk && 'message' in chunk);
103
+ const isMessageChunk = !!(chunk && 'message' in chunk);
85
104
  const msg = isMessageChunk ? chunk.message : undefined;
86
105
  if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {
87
106
  console.log(msg.tool_call_chunks);
@@ -116,11 +135,17 @@ export class TestChatStreamHandler implements t.EventHandler {
116
135
  }
117
136
 
118
137
  export class LLMStreamHandler implements t.EventHandler {
119
- handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>): void {
138
+ handle(
139
+ event: string,
140
+ data: t.StreamEventData | undefined,
141
+ metadata?: Record<string, unknown>
142
+ ): void {
120
143
  const chunk = data?.chunk;
121
- const isMessageChunk = !!(chunk && 'message' in chunk);
144
+ const isMessageChunk = !!(chunk && 'message' in chunk);
122
145
  const msg = isMessageChunk && chunk.message;
123
- if (metadata) { console.log(metadata); }
146
+ if (metadata) {
147
+ console.log(metadata);
148
+ }
124
149
  if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {
125
150
  console.log(msg.tool_call_chunks);
126
151
  } else if (msg && msg.content) {
@@ -133,12 +158,21 @@ export class LLMStreamHandler implements t.EventHandler {
133
158
  }
134
159
  }
135
160
 
136
- export const createMetadataAggregator = (_collected?: Record<string, NonNullable<BaseMessageFields['response_metadata']>>[]): t.MetadataAggregatorResult => {
161
+ export const createMetadataAggregator = (
162
+ _collected?: Record<
163
+ string,
164
+ NonNullable<BaseMessageFields['response_metadata']>
165
+ >[]
166
+ ): t.MetadataAggregatorResult => {
137
167
  const collected = _collected || [];
138
168
 
139
169
  const handleLLMEnd: t.HandleLLMEnd = (output) => {
140
170
  const { generations } = output;
141
- const lastMessageOutput = (generations[generations.length - 1] as (t.StreamGeneration | undefined)[] | undefined)?.[0];
171
+ const lastMessageOutput = (
172
+ generations[generations.length - 1] as
173
+ | (t.StreamGeneration | undefined)[]
174
+ | undefined
175
+ )?.[0];
142
176
  if (!lastMessageOutput) {
143
177
  return;
144
178
  }
@@ -149,4 +183,4 @@ export const createMetadataAggregator = (_collected?: Record<string, NonNullable
149
183
  };
150
184
 
151
185
  return { handleLLMEnd, collected };
152
- };
186
+ };
@@ -105,7 +105,8 @@ export abstract class Graph<
105
105
  lastToken?: string;
106
106
  tokenTypeSwitch?: 'reasoning' | 'content';
107
107
  reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';
108
- currentTokenType: ContentTypes.TEXT | ContentTypes.THINK = ContentTypes.TEXT;
108
+ currentTokenType: ContentTypes.TEXT | ContentTypes.THINK | 'think_and_text' =
109
+ ContentTypes.TEXT;
109
110
  messageStepHasToolCalls: Map<string, boolean> = new Map();
110
111
  messageIdsByStepKey: Map<string, string> = new Map();
111
112
  prelimMessageIdsByStepKey: Map<string, string> = new Map();
@@ -303,7 +304,10 @@ export class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
303
304
  metadata.langgraph_step as number,
304
305
  metadata.checkpoint_ns as string,
305
306
  ];
306
- if (this.currentTokenType === ContentTypes.THINK) {
307
+ if (
308
+ this.currentTokenType === ContentTypes.THINK ||
309
+ this.currentTokenType === 'think_and_text'
310
+ ) {
307
311
  keyList.push('reasoning');
308
312
  }
309
313
 
package/src/index.ts CHANGED
@@ -10,6 +10,8 @@ export * from './graphs';
10
10
 
11
11
  /* Tools */
12
12
  export * from './tools/CodeExecutor';
13
+ export * from './tools/handlers';
14
+ export * from './tools/search';
13
15
 
14
16
  /* Misc. */
15
17
  export * from './common';
@@ -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';
@@ -0,0 +1,146 @@
1
+ /* eslint-disable no-console */
2
+ // src/scripts/cli.ts
3
+ import { config } from 'dotenv';
4
+ config();
5
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
6
+ import type * as t from '@/types';
7
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
+ import { ToolEndHandler, ModelEndHandler } from '@/events';
9
+ import { createSearchTool } from '@/tools/search';
10
+
11
+ import { getArgs } from '@/scripts/args';
12
+ import { Run } from '@/run';
13
+ import { GraphEvents, Callback } from '@/common';
14
+ import { getLLMConfig } from '@/utils/llmConfig';
15
+
16
+ const conversationHistory: BaseMessage[] = [];
17
+ async function testStandardStreaming(): Promise<void> {
18
+ const { userName, location, provider, currentDate } = await getArgs();
19
+ const { contentParts, aggregateContent } = createContentAggregator();
20
+ const customHandlers = {
21
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
22
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
23
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
24
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
25
+ handle: (
26
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
27
+ data: t.StreamEventData
28
+ ): void => {
29
+ console.log('====== ON_RUN_STEP_COMPLETED ======');
30
+ // console.dir(data, { depth: null });
31
+ aggregateContent({
32
+ event,
33
+ data: data as unknown as { result: t.ToolEndEvent },
34
+ });
35
+ },
36
+ },
37
+ [GraphEvents.ON_RUN_STEP]: {
38
+ handle: (
39
+ event: GraphEvents.ON_RUN_STEP,
40
+ data: t.StreamEventData
41
+ ): void => {
42
+ console.log('====== ON_RUN_STEP ======');
43
+ console.dir(data, { depth: null });
44
+ aggregateContent({ event, data: data as t.RunStep });
45
+ },
46
+ },
47
+ [GraphEvents.ON_RUN_STEP_DELTA]: {
48
+ handle: (
49
+ event: GraphEvents.ON_RUN_STEP_DELTA,
50
+ data: t.StreamEventData
51
+ ): void => {
52
+ console.log('====== ON_RUN_STEP_DELTA ======');
53
+ console.dir(data, { depth: null });
54
+ aggregateContent({ event, data: data as t.RunStepDeltaEvent });
55
+ },
56
+ },
57
+ [GraphEvents.ON_MESSAGE_DELTA]: {
58
+ handle: (
59
+ event: GraphEvents.ON_MESSAGE_DELTA,
60
+ data: t.StreamEventData
61
+ ): void => {
62
+ console.log('====== ON_MESSAGE_DELTA ======');
63
+ console.dir(data, { depth: null });
64
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
65
+ },
66
+ },
67
+ [GraphEvents.TOOL_START]: {
68
+ handle: (
69
+ _event: string,
70
+ data: t.StreamEventData,
71
+ metadata?: Record<string, unknown>
72
+ ): void => {
73
+ console.log('====== TOOL_START ======');
74
+ // console.dir(data, { depth: null });
75
+ },
76
+ },
77
+ };
78
+
79
+ const llmConfig = getLLMConfig(provider);
80
+
81
+ const run = await Run.create<t.IState>({
82
+ runId: 'test-run-id',
83
+ graphConfig: {
84
+ type: 'standard',
85
+ llmConfig,
86
+ tools: [createSearchTool()],
87
+ instructions:
88
+ 'You are a friendly AI assistant. Always address the user by their name.',
89
+ // additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
90
+ },
91
+ returnContent: true,
92
+ customHandlers,
93
+ });
94
+
95
+ const config = {
96
+ configurable: {
97
+ provider,
98
+ thread_id: 'conversation-num-1',
99
+ },
100
+ streamMode: 'values',
101
+ version: 'v2' as const,
102
+ };
103
+
104
+ console.log('Test 1: Search query (search tool test)');
105
+
106
+ // const userMessage = `
107
+ // Make a search for the weather in ${location} today, which is ${currentDate}.
108
+ // Before making the search, please let me know what you're about to do, then immediately start searching without hesitation.
109
+ // Make sure to always refer to me by name, which is ${userName}.
110
+ // After giving me a thorough summary, tell me a joke about the weather forecast we went over.
111
+ // `;
112
+ // const userMessage = 'Are massage guns good?';
113
+ // const userMessage = 'What is functional programming?';
114
+ const userMessage = "Get me today's trending news.";
115
+ // const userMessage = "search recent italy earthquake volcano activity";
116
+ // const userMessage =
117
+ // "use 'Trump' as the exact search query and tell me what you find.";
118
+
119
+ conversationHistory.push(new HumanMessage(userMessage));
120
+
121
+ const inputs = {
122
+ messages: conversationHistory,
123
+ };
124
+ const finalContentParts = await run.processStream(inputs, config);
125
+ const finalMessages = run.getRunMessages();
126
+ if (finalMessages) {
127
+ conversationHistory.push(...finalMessages);
128
+ console.dir(conversationHistory, { depth: null });
129
+ }
130
+ // console.dir(finalContentParts, { depth: null });
131
+ console.log('\n\n====================\n\n');
132
+ // console.dir(contentParts, { depth: null });
133
+ }
134
+
135
+ process.on('unhandledRejection', (reason, promise) => {
136
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
137
+ console.log('Conversation history:');
138
+ process.exit(1);
139
+ });
140
+
141
+ testStandardStreaming().catch((err) => {
142
+ console.error(err);
143
+ console.log('Conversation history:');
144
+ console.dir(conversationHistory, { depth: null });
145
+ process.exit(1);
146
+ });