@librechat/agents 1.4.6 → 1.4.8

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.
@@ -0,0 +1,115 @@
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 { TavilySearchResults } from '@langchain/community/tools/tavily_search';
7
+ import type * as t from '@/types';
8
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
9
+ import { ToolEndHandler } from '@/events';
10
+
11
+
12
+ import { getArgs } from '@/scripts/args';
13
+ import { Run } from '@/run';
14
+ import { GraphEvents, Callback } from '@/common';
15
+ import { getLLMConfig } from '@/utils/llmConfig';
16
+
17
+ const conversationHistory: BaseMessage[] = [];
18
+ async function testStandardStreaming(): Promise<void> {
19
+ const { userName, location, provider, currentDate } = await getArgs();
20
+ const { contentParts, aggregateContent } = createContentAggregator();
21
+ const customHandlers = {
22
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
23
+ // [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
24
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
25
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
26
+ handle: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData): void => {
27
+ console.log('====== ON_RUN_STEP_COMPLETED ======');
28
+ // console.dir(data, { depth: null });
29
+ aggregateContent({ event, data: data as unknown as { result: t.ToolEndEvent } });
30
+ }
31
+ },
32
+ [GraphEvents.ON_RUN_STEP]: {
33
+ handle: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData): void => {
34
+ console.log('====== ON_RUN_STEP ======');
35
+ console.dir(data, { depth: null });
36
+ aggregateContent({ event, data: data as t.RunStep });
37
+ }
38
+ },
39
+ [GraphEvents.ON_RUN_STEP_DELTA]: {
40
+ handle: (event: GraphEvents.ON_RUN_STEP_DELTA, data: t.StreamEventData): void => {
41
+ console.log('====== ON_RUN_STEP_DELTA ======');
42
+ console.dir(data, { depth: null });
43
+ aggregateContent({ event, data: data as t.RunStepDeltaEvent });
44
+ }
45
+ },
46
+ [GraphEvents.ON_MESSAGE_DELTA]: {
47
+ handle: (event: GraphEvents.ON_MESSAGE_DELTA, data: t.StreamEventData): void => {
48
+ console.log('====== ON_MESSAGE_DELTA ======');
49
+ console.dir(data, { depth: null });
50
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
51
+ }
52
+ },
53
+ [GraphEvents.TOOL_START]: {
54
+ handle: (_event: string, data: t.StreamEventData, metadata?: Record<string, unknown>): void => {
55
+ console.log('====== TOOL_START ======');
56
+ // console.dir(data, { depth: null });
57
+ }
58
+ },
59
+ };
60
+
61
+ const llmConfig = getLLMConfig(provider);
62
+
63
+ const run = await Run.create<t.IState>({
64
+ graphConfig: {
65
+ type: 'standard',
66
+ llmConfig,
67
+ tools: [new TavilySearchResults()],
68
+ instructions: 'You are a friendly AI assistant. Always address the user by their name.',
69
+ additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
70
+ },
71
+ returnContent: true,
72
+ customHandlers,
73
+ });
74
+
75
+ const config = {
76
+ configurable: {
77
+ provider,
78
+ thread_id: 'conversation-num-1',
79
+ },
80
+ streamMode: 'values',
81
+ version: 'v2' as const,
82
+ };
83
+
84
+ console.log('Test 1: Simple message test');
85
+
86
+ const userMessage = `hi`;
87
+
88
+ conversationHistory.push(new HumanMessage(userMessage));
89
+
90
+ const inputs = {
91
+ messages: conversationHistory,
92
+ };
93
+ const finalContentParts = await run.processStream(inputs, config);
94
+ const finalMessages = run.getRunMessages();
95
+ if (finalMessages) {
96
+ conversationHistory.push(...finalMessages);
97
+ console.dir(conversationHistory, { depth: null });
98
+ }
99
+ // console.dir(finalContentParts, { depth: null });
100
+ console.log('\n\n====================\n\n');
101
+ // console.dir(contentParts, { depth: null });
102
+ }
103
+
104
+ process.on('unhandledRejection', (reason, promise) => {
105
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
106
+ console.log('Conversation history:');
107
+ process.exit(1);
108
+ });
109
+
110
+ testStandardStreaming().catch((err) => {
111
+ console.error(err);
112
+ console.log('Conversation history:');
113
+ console.dir(conversationHistory, { depth: null });
114
+ process.exit(1);
115
+ });
package/src/stream.ts CHANGED
@@ -221,7 +221,7 @@ export function createContentAggregator(): ContentAggregatorResult {
221
221
  contentParts[index] = { type: partType };
222
222
  }
223
223
 
224
- if (contentPart.type !== contentParts[index]?.type) {
224
+ if (!partType.startsWith(contentParts[index]?.type ?? '')) {
225
225
  console.warn('Content type mismatch');
226
226
  return;
227
227
  }
package/src/types/run.ts CHANGED
@@ -19,6 +19,7 @@ export type StandardGraphConfig = {
19
19
  tools?: t.GenericTool[];
20
20
  toolMap?: t.ToolMap;
21
21
  additional_instructions?: string;
22
+ streamBuffer?: number;
22
23
  clientOptions?: Record<string, unknown>;
23
24
  };
24
25
 
@@ -53,5 +54,7 @@ export type RunConfig = {
53
54
  graphConfig: StandardGraphConfig | CollaborativeGraphConfig | TaskManagerGraphConfig;
54
55
  customHandlers?: Record<string, g.EventHandler>;
55
56
  returnContent?: boolean;
57
+ /** The rate at which to process stream events */
58
+ streamRate?: number;
56
59
  runId?: string;
57
60
  };
package/src/utils/run.ts CHANGED
@@ -7,6 +7,16 @@ import {
7
7
  } from '@langchain/core/runnables';
8
8
  import { AsyncLocalStorageProviderSingleton } from '@langchain/core/singletons';
9
9
 
10
+ /**
11
+ * Delays the execution for a specified number of milliseconds.
12
+ *
13
+ * @param {number} ms - The number of milliseconds to delay.
14
+ * @return {Promise<void>} A promise that resolves after the specified delay.
15
+ */
16
+ export function sleep(ms: number): Promise<void> {
17
+ return new Promise((resolve) => setTimeout(resolve, ms));
18
+ }
19
+
10
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
21
  export interface RunnableCallableArgs extends Partial<any> {
12
22
  name?: string;