illuma-agents 1.0.2 → 1.0.4
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/LICENSE +25 -21
- package/dist/cjs/agents/AgentContext.cjs +222 -0
- package/dist/cjs/agents/AgentContext.cjs.map +1 -0
- package/dist/cjs/common/enum.cjs +5 -4
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/events.cjs +7 -5
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +328 -207
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/ollama/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +35 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +3 -1
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +0 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +12 -1
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +123 -0
- package/dist/cjs/messages/cache.cjs.map +1 -0
- package/dist/cjs/messages/content.cjs +53 -0
- package/dist/cjs/messages/content.cjs.map +1 -0
- package/dist/cjs/messages/format.cjs +17 -29
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/run.cjs +119 -74
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +77 -73
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs +45 -0
- package/dist/cjs/tools/Calculator.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +22 -22
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +5 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +20 -20
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +31 -0
- package/dist/cjs/utils/events.cjs.map +1 -0
- package/dist/cjs/utils/handlers.cjs +70 -0
- package/dist/cjs/utils/handlers.cjs.map +1 -0
- package/dist/cjs/utils/tokens.cjs +54 -7
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +220 -0
- package/dist/esm/agents/AgentContext.mjs.map +1 -0
- package/dist/esm/common/enum.mjs +5 -4
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/events.mjs +7 -5
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +330 -209
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/ollama/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +35 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +3 -1
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +0 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +7 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +120 -0
- package/dist/esm/messages/cache.mjs.map +1 -0
- package/dist/esm/messages/content.mjs +51 -0
- package/dist/esm/messages/content.mjs.map +1 -0
- package/dist/esm/messages/format.mjs +18 -29
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/run.mjs +119 -74
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +77 -73
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs +24 -0
- package/dist/esm/tools/Calculator.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +22 -22
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +5 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +20 -20
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +29 -0
- package/dist/esm/utils/events.mjs.map +1 -0
- package/dist/esm/utils/handlers.mjs +68 -0
- package/dist/esm/utils/handlers.mjs.map +1 -0
- package/dist/esm/utils/tokens.mjs +54 -8
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +94 -0
- package/dist/types/common/enum.d.ts +7 -5
- package/dist/types/events.d.ts +3 -3
- package/dist/types/graphs/Graph.d.ts +60 -66
- package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
- package/dist/types/graphs/index.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/llm/openai/index.d.ts +10 -0
- package/dist/types/messages/cache.d.ts +20 -0
- package/dist/types/messages/content.d.ts +7 -0
- package/dist/types/messages/format.d.ts +1 -7
- package/dist/types/messages/index.d.ts +2 -0
- package/dist/types/messages/reducer.d.ts +9 -0
- package/dist/types/run.d.ts +16 -10
- package/dist/types/stream.d.ts +4 -3
- package/dist/types/tools/Calculator.d.ts +8 -0
- package/dist/types/tools/ToolNode.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +9 -7
- package/dist/types/tools/search/tool.d.ts +4 -4
- package/dist/types/types/graph.d.ts +124 -11
- package/dist/types/types/llm.d.ts +13 -9
- package/dist/types/types/messages.d.ts +4 -0
- package/dist/types/types/run.d.ts +46 -8
- package/dist/types/types/stream.d.ts +3 -2
- package/dist/types/utils/events.d.ts +6 -0
- package/dist/types/utils/handlers.d.ts +34 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/tokens.d.ts +24 -0
- package/package.json +162 -145
- package/src/agents/AgentContext.ts +323 -0
- package/src/common/enum.ts +177 -176
- package/src/events.ts +197 -191
- package/src/graphs/Graph.ts +1058 -846
- package/src/graphs/MultiAgentGraph.ts +598 -0
- package/src/graphs/index.ts +2 -1
- package/src/index.ts +25 -24
- package/src/llm/anthropic/index.ts +413 -413
- package/src/llm/google/index.ts +222 -222
- package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
- package/src/llm/ollama/index.ts +92 -92
- package/src/llm/openai/index.ts +894 -853
- package/src/llm/openai/utils/index.ts +920 -918
- package/src/llm/openrouter/index.ts +60 -60
- package/src/llm/providers.ts +55 -57
- package/src/llm/vertexai/index.ts +360 -360
- package/src/messages/cache.test.ts +461 -0
- package/src/messages/cache.ts +151 -0
- package/src/messages/content.test.ts +362 -0
- package/src/messages/content.ts +63 -0
- package/src/messages/format.ts +611 -625
- package/src/messages/formatAgentMessages.test.ts +1144 -917
- package/src/messages/index.ts +6 -4
- package/src/messages/reducer.ts +80 -0
- package/src/run.ts +447 -381
- package/src/scripts/abort.ts +157 -138
- package/src/scripts/ant_web_search.ts +158 -158
- package/src/scripts/cli.ts +172 -167
- package/src/scripts/cli2.ts +133 -125
- package/src/scripts/cli3.ts +184 -178
- package/src/scripts/cli4.ts +191 -184
- package/src/scripts/cli5.ts +191 -184
- package/src/scripts/code_exec.ts +213 -214
- package/src/scripts/code_exec_simple.ts +147 -129
- package/src/scripts/content.ts +138 -120
- package/src/scripts/handoff-test.ts +135 -0
- package/src/scripts/multi-agent-chain.ts +278 -0
- package/src/scripts/multi-agent-conditional.ts +220 -0
- package/src/scripts/multi-agent-document-review-chain.ts +197 -0
- package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
- package/src/scripts/multi-agent-parallel.ts +343 -0
- package/src/scripts/multi-agent-sequence.ts +212 -0
- package/src/scripts/multi-agent-supervisor.ts +364 -0
- package/src/scripts/multi-agent-test.ts +186 -0
- package/src/scripts/search.ts +146 -150
- package/src/scripts/simple.ts +225 -225
- package/src/scripts/stream.ts +140 -122
- package/src/scripts/test-custom-prompt-key.ts +145 -0
- package/src/scripts/test-handoff-input.ts +170 -0
- package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
- package/src/scripts/test-tools-before-handoff.ts +222 -0
- package/src/scripts/tools.ts +153 -155
- package/src/specs/agent-handoffs.test.ts +889 -0
- package/src/specs/anthropic.simple.test.ts +320 -317
- package/src/specs/azure.simple.test.ts +325 -316
- package/src/specs/openai.simple.test.ts +311 -316
- package/src/specs/openrouter.simple.test.ts +107 -0
- package/src/specs/prune.test.ts +758 -763
- package/src/specs/reasoning.test.ts +201 -165
- package/src/specs/thinking-prune.test.ts +769 -703
- package/src/specs/token-memoization.test.ts +39 -0
- package/src/stream.ts +664 -651
- package/src/tools/Calculator.test.ts +278 -0
- package/src/tools/Calculator.ts +25 -0
- package/src/tools/CodeExecutor.ts +220 -220
- package/src/tools/ToolNode.ts +170 -170
- package/src/tools/handlers.ts +341 -336
- package/src/types/graph.ts +372 -185
- package/src/types/llm.ts +141 -140
- package/src/types/messages.ts +4 -0
- package/src/types/run.ts +128 -89
- package/src/types/stream.ts +401 -400
- package/src/utils/events.ts +32 -0
- package/src/utils/handlers.ts +107 -0
- package/src/utils/index.ts +6 -5
- package/src/utils/llmConfig.ts +183 -183
- package/src/utils/tokens.ts +129 -70
- package/dist/types/scripts/abort.d.ts +0 -1
- package/dist/types/scripts/ant_web_search.d.ts +0 -1
- package/dist/types/scripts/args.d.ts +0 -7
- package/dist/types/scripts/caching.d.ts +0 -1
- package/dist/types/scripts/cli.d.ts +0 -1
- package/dist/types/scripts/cli2.d.ts +0 -1
- package/dist/types/scripts/cli3.d.ts +0 -1
- package/dist/types/scripts/cli4.d.ts +0 -1
- package/dist/types/scripts/cli5.d.ts +0 -1
- package/dist/types/scripts/code_exec.d.ts +0 -1
- package/dist/types/scripts/code_exec_files.d.ts +0 -1
- package/dist/types/scripts/code_exec_simple.d.ts +0 -1
- package/dist/types/scripts/content.d.ts +0 -1
- package/dist/types/scripts/empty_input.d.ts +0 -1
- package/dist/types/scripts/image.d.ts +0 -1
- package/dist/types/scripts/memory.d.ts +0 -1
- package/dist/types/scripts/search.d.ts +0 -1
- package/dist/types/scripts/simple.d.ts +0 -1
- package/dist/types/scripts/stream.d.ts +0 -1
- package/dist/types/scripts/thinking.d.ts +0 -1
- package/dist/types/scripts/tools.d.ts +0 -1
- package/dist/types/specs/spec.utils.d.ts +0 -1
- package/dist/types/tools/example.d.ts +0 -78
- package/src/tools/example.ts +0 -129
package/src/scripts/abort.ts
CHANGED
|
@@ -1,138 +1,157 @@
|
|
|
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
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
[GraphEvents.
|
|
25
|
-
[GraphEvents.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
console.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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 } from '@/events';
|
|
9
|
+
|
|
10
|
+
import { getArgs } from '@/scripts/args';
|
|
11
|
+
import { Run } from '@/run';
|
|
12
|
+
import { GraphEvents, Callback } from '@/common';
|
|
13
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
14
|
+
|
|
15
|
+
const conversationHistory: BaseMessage[] = [];
|
|
16
|
+
|
|
17
|
+
async function testStandardStreaming(): Promise<void> {
|
|
18
|
+
const { userName, location, provider, currentDate } = await getArgs();
|
|
19
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
20
|
+
const controller = new AbortController();
|
|
21
|
+
|
|
22
|
+
const customHandlers = {
|
|
23
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
24
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
25
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
26
|
+
handle: (
|
|
27
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
28
|
+
data: t.StreamEventData
|
|
29
|
+
): void => {
|
|
30
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
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
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const llmConfig = getLLMConfig(provider);
|
|
79
|
+
const signal = controller.signal;
|
|
80
|
+
const run = await Run.create<t.IState>({
|
|
81
|
+
runId: 'test-run-id',
|
|
82
|
+
graphConfig: {
|
|
83
|
+
type: 'standard',
|
|
84
|
+
signal,
|
|
85
|
+
llmConfig,
|
|
86
|
+
tools: [],
|
|
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
|
+
customHandlers,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const config = {
|
|
95
|
+
configurable: {
|
|
96
|
+
provider,
|
|
97
|
+
thread_id: 'conversation-num-1',
|
|
98
|
+
},
|
|
99
|
+
signal,
|
|
100
|
+
streamMode: 'values',
|
|
101
|
+
version: 'v2' as const,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
console.log('Test 1: Weather query (content parts test)');
|
|
105
|
+
|
|
106
|
+
const userMessage = `
|
|
107
|
+
Make a search for the weather in ${location} today, which is ${currentDate}.
|
|
108
|
+
Make sure to always refer to me by name, which is ${userName}.
|
|
109
|
+
After giving me a thorough summary, tell me a joke about the weather forecast we went over.
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
113
|
+
|
|
114
|
+
const inputs = {
|
|
115
|
+
messages: conversationHistory,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Set a timeout to abort the operation after 5 seconds
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
controller.abort();
|
|
121
|
+
console.log('Operation aborted');
|
|
122
|
+
console.log('Current content parts:');
|
|
123
|
+
console.dir(contentParts, { depth: null });
|
|
124
|
+
}, 4000);
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
128
|
+
const finalMessages = run.getRunMessages();
|
|
129
|
+
if (finalMessages) {
|
|
130
|
+
conversationHistory.push(...finalMessages);
|
|
131
|
+
console.dir(conversationHistory, { depth: null });
|
|
132
|
+
}
|
|
133
|
+
console.dir(finalContentParts, { depth: null });
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if ((error as Error)?.name === 'AbortError') {
|
|
136
|
+
console.log('Operation was aborted');
|
|
137
|
+
} else {
|
|
138
|
+
console.error('An error occurred:', error);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
console.log('\n\n====================\n\n');
|
|
143
|
+
console.dir(contentParts, { depth: null });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
147
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
148
|
+
console.log('Conversation history:');
|
|
149
|
+
process.exit(1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
testStandardStreaming().catch((err) => {
|
|
153
|
+
console.error(err);
|
|
154
|
+
console.log('Conversation history:');
|
|
155
|
+
console.dir(conversationHistory, { depth: null });
|
|
156
|
+
process.exit(1);
|
|
157
|
+
});
|
|
@@ -1,158 +1,158 @@
|
|
|
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
|
-
|
|
10
|
-
import { getArgs } from '@/scripts/args';
|
|
11
|
-
import { Run } from '@/run';
|
|
12
|
-
import { GraphEvents, Callback, Providers } from '@/common';
|
|
13
|
-
import { getLLMConfig } from '@/utils/llmConfig';
|
|
14
|
-
|
|
15
|
-
const conversationHistory: BaseMessage[] = [];
|
|
16
|
-
let _contentParts: (t.MessageContentComplex | undefined)[] = [];
|
|
17
|
-
async function testStandardStreaming(): Promise<void> {
|
|
18
|
-
const { userName, location, currentDate } = await getArgs();
|
|
19
|
-
const { contentParts, aggregateContent } = createContentAggregator();
|
|
20
|
-
_contentParts = contentParts;
|
|
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: (
|
|
27
|
-
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
28
|
-
data: t.StreamEventData
|
|
29
|
-
): void => {
|
|
30
|
-
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
31
|
-
// console.dir(data, { depth: null });
|
|
32
|
-
aggregateContent({
|
|
33
|
-
event,
|
|
34
|
-
data: data as unknown as { result: t.ToolEndEvent },
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
[GraphEvents.ON_RUN_STEP]: {
|
|
39
|
-
handle: (
|
|
40
|
-
event: GraphEvents.ON_RUN_STEP,
|
|
41
|
-
data: t.StreamEventData
|
|
42
|
-
): void => {
|
|
43
|
-
console.log('====== ON_RUN_STEP ======');
|
|
44
|
-
console.dir(data, { depth: null });
|
|
45
|
-
aggregateContent({ event, data: data as t.RunStep });
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
49
|
-
handle: (
|
|
50
|
-
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
51
|
-
data: t.StreamEventData
|
|
52
|
-
): void => {
|
|
53
|
-
console.log('====== ON_RUN_STEP_DELTA ======');
|
|
54
|
-
console.dir(data, { depth: null });
|
|
55
|
-
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
59
|
-
handle: (
|
|
60
|
-
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
61
|
-
data: t.StreamEventData
|
|
62
|
-
): void => {
|
|
63
|
-
console.log('====== ON_MESSAGE_DELTA ======');
|
|
64
|
-
console.dir(data, { depth: null });
|
|
65
|
-
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
[GraphEvents.TOOL_START]: {
|
|
69
|
-
handle: (
|
|
70
|
-
_event: string,
|
|
71
|
-
data: t.StreamEventData,
|
|
72
|
-
metadata?: Record<string, unknown>
|
|
73
|
-
): void => {
|
|
74
|
-
console.log('====== TOOL_START ======');
|
|
75
|
-
// console.dir(data, { depth: null });
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const llmConfig = getLLMConfig(
|
|
81
|
-
Providers.ANTHROPIC
|
|
82
|
-
) as t.AnthropicClientOptions & t.SharedLLMConfig;
|
|
83
|
-
llmConfig.model = 'claude-
|
|
84
|
-
|
|
85
|
-
const run = await Run.create<t.IState>({
|
|
86
|
-
runId: 'test-run-id',
|
|
87
|
-
graphConfig: {
|
|
88
|
-
type: 'standard',
|
|
89
|
-
llmConfig,
|
|
90
|
-
tools: [
|
|
91
|
-
{
|
|
92
|
-
type: 'web_search_20250305',
|
|
93
|
-
name: 'web_search',
|
|
94
|
-
max_uses: 5,
|
|
95
|
-
},
|
|
96
|
-
],
|
|
97
|
-
instructions: 'You are a friendly AI assistant.',
|
|
98
|
-
// additional_instructions: `Always address the user by their name. The user's name is ${userName} and they are located in ${location}.`,
|
|
99
|
-
},
|
|
100
|
-
returnContent: true,
|
|
101
|
-
customHandlers,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const config = {
|
|
105
|
-
configurable: {
|
|
106
|
-
provider: Providers.ANTHROPIC,
|
|
107
|
-
thread_id: 'conversation-num-1',
|
|
108
|
-
},
|
|
109
|
-
streamMode: 'values',
|
|
110
|
-
version: 'v2' as const,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
console.log('Test 1: Search query (search tool test)');
|
|
114
|
-
|
|
115
|
-
// const userMessage = `
|
|
116
|
-
// Make a search for the weather in ${location} today, which is ${currentDate}.
|
|
117
|
-
// Before making the search, please let me know what you're about to do, then immediately start searching without hesitation.
|
|
118
|
-
// Make sure to always refer to me by name, which is ${userName}.
|
|
119
|
-
// After giving me a thorough summary, tell me a joke about the weather forecast we went over.
|
|
120
|
-
// `;
|
|
121
|
-
// const userMessage = 'Are massage guns good?';
|
|
122
|
-
// const userMessage = 'What is functional programming?';
|
|
123
|
-
const userMessage = "Get me today's trending news.";
|
|
124
|
-
// const userMessage = "search recent italy earthquake volcano activity";
|
|
125
|
-
// const userMessage =
|
|
126
|
-
// "use 'Trump' as the exact search query and tell me what you find.";
|
|
127
|
-
|
|
128
|
-
conversationHistory.push(new HumanMessage(userMessage));
|
|
129
|
-
|
|
130
|
-
const inputs = {
|
|
131
|
-
messages: conversationHistory,
|
|
132
|
-
};
|
|
133
|
-
const finalContentParts = await run.processStream(inputs, config);
|
|
134
|
-
const finalMessages = run.getRunMessages();
|
|
135
|
-
if (finalMessages) {
|
|
136
|
-
conversationHistory.push(...finalMessages);
|
|
137
|
-
console.dir(conversationHistory, { depth: null });
|
|
138
|
-
}
|
|
139
|
-
// console.dir(finalContentParts, { depth: null });
|
|
140
|
-
console.log('\n\n====================\n\n');
|
|
141
|
-
// console.dir(contentParts, { depth: null });
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
process.on('unhandledRejection', (reason, promise) => {
|
|
145
|
-
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
146
|
-
console.log('Content Parts:');
|
|
147
|
-
console.dir(_contentParts, { depth: null });
|
|
148
|
-
process.exit(1);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
testStandardStreaming().catch((err) => {
|
|
152
|
-
console.error(err);
|
|
153
|
-
console.log('Conversation history:');
|
|
154
|
-
console.dir(conversationHistory, { depth: null });
|
|
155
|
-
console.log('Content Parts:');
|
|
156
|
-
console.dir(_contentParts, { depth: null });
|
|
157
|
-
process.exit(1);
|
|
158
|
-
});
|
|
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
|
+
|
|
10
|
+
import { getArgs } from '@/scripts/args';
|
|
11
|
+
import { Run } from '@/run';
|
|
12
|
+
import { GraphEvents, Callback, Providers } from '@/common';
|
|
13
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
14
|
+
|
|
15
|
+
const conversationHistory: BaseMessage[] = [];
|
|
16
|
+
let _contentParts: (t.MessageContentComplex | undefined)[] = [];
|
|
17
|
+
async function testStandardStreaming(): Promise<void> {
|
|
18
|
+
const { userName, location, currentDate } = await getArgs();
|
|
19
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
20
|
+
_contentParts = contentParts;
|
|
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: (
|
|
27
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
28
|
+
data: t.StreamEventData
|
|
29
|
+
): void => {
|
|
30
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
31
|
+
// console.dir(data, { depth: null });
|
|
32
|
+
aggregateContent({
|
|
33
|
+
event,
|
|
34
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
39
|
+
handle: (
|
|
40
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
41
|
+
data: t.StreamEventData
|
|
42
|
+
): void => {
|
|
43
|
+
console.log('====== ON_RUN_STEP ======');
|
|
44
|
+
console.dir(data, { depth: null });
|
|
45
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
49
|
+
handle: (
|
|
50
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
51
|
+
data: t.StreamEventData
|
|
52
|
+
): void => {
|
|
53
|
+
console.log('====== ON_RUN_STEP_DELTA ======');
|
|
54
|
+
console.dir(data, { depth: null });
|
|
55
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
59
|
+
handle: (
|
|
60
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
61
|
+
data: t.StreamEventData
|
|
62
|
+
): void => {
|
|
63
|
+
console.log('====== ON_MESSAGE_DELTA ======');
|
|
64
|
+
console.dir(data, { depth: null });
|
|
65
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
[GraphEvents.TOOL_START]: {
|
|
69
|
+
handle: (
|
|
70
|
+
_event: string,
|
|
71
|
+
data: t.StreamEventData,
|
|
72
|
+
metadata?: Record<string, unknown>
|
|
73
|
+
): void => {
|
|
74
|
+
console.log('====== TOOL_START ======');
|
|
75
|
+
// console.dir(data, { depth: null });
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const llmConfig = getLLMConfig(
|
|
81
|
+
Providers.ANTHROPIC
|
|
82
|
+
) as t.AnthropicClientOptions & t.SharedLLMConfig;
|
|
83
|
+
llmConfig.model = 'claude-haiku-4-5';
|
|
84
|
+
|
|
85
|
+
const run = await Run.create<t.IState>({
|
|
86
|
+
runId: 'test-run-id',
|
|
87
|
+
graphConfig: {
|
|
88
|
+
type: 'standard',
|
|
89
|
+
llmConfig,
|
|
90
|
+
tools: [
|
|
91
|
+
{
|
|
92
|
+
type: 'web_search_20250305',
|
|
93
|
+
name: 'web_search',
|
|
94
|
+
max_uses: 5,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
instructions: 'You are a friendly AI assistant.',
|
|
98
|
+
// additional_instructions: `Always address the user by their name. The user's name is ${userName} and they are located in ${location}.`,
|
|
99
|
+
},
|
|
100
|
+
returnContent: true,
|
|
101
|
+
customHandlers,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const config = {
|
|
105
|
+
configurable: {
|
|
106
|
+
provider: Providers.ANTHROPIC,
|
|
107
|
+
thread_id: 'conversation-num-1',
|
|
108
|
+
},
|
|
109
|
+
streamMode: 'values',
|
|
110
|
+
version: 'v2' as const,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
console.log('Test 1: Search query (search tool test)');
|
|
114
|
+
|
|
115
|
+
// const userMessage = `
|
|
116
|
+
// Make a search for the weather in ${location} today, which is ${currentDate}.
|
|
117
|
+
// Before making the search, please let me know what you're about to do, then immediately start searching without hesitation.
|
|
118
|
+
// Make sure to always refer to me by name, which is ${userName}.
|
|
119
|
+
// After giving me a thorough summary, tell me a joke about the weather forecast we went over.
|
|
120
|
+
// `;
|
|
121
|
+
// const userMessage = 'Are massage guns good?';
|
|
122
|
+
// const userMessage = 'What is functional programming?';
|
|
123
|
+
const userMessage = "Get me today's trending news.";
|
|
124
|
+
// const userMessage = "search recent italy earthquake volcano activity";
|
|
125
|
+
// const userMessage =
|
|
126
|
+
// "use 'Trump' as the exact search query and tell me what you find.";
|
|
127
|
+
|
|
128
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
129
|
+
|
|
130
|
+
const inputs = {
|
|
131
|
+
messages: conversationHistory,
|
|
132
|
+
};
|
|
133
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
134
|
+
const finalMessages = run.getRunMessages();
|
|
135
|
+
if (finalMessages) {
|
|
136
|
+
conversationHistory.push(...finalMessages);
|
|
137
|
+
console.dir(conversationHistory, { depth: null });
|
|
138
|
+
}
|
|
139
|
+
// console.dir(finalContentParts, { depth: null });
|
|
140
|
+
console.log('\n\n====================\n\n');
|
|
141
|
+
// console.dir(contentParts, { depth: null });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
145
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
146
|
+
console.log('Content Parts:');
|
|
147
|
+
console.dir(_contentParts, { depth: null });
|
|
148
|
+
process.exit(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
testStandardStreaming().catch((err) => {
|
|
152
|
+
console.error(err);
|
|
153
|
+
console.log('Conversation history:');
|
|
154
|
+
console.dir(conversationHistory, { depth: null });
|
|
155
|
+
console.log('Content Parts:');
|
|
156
|
+
console.dir(_contentParts, { depth: null });
|
|
157
|
+
process.exit(1);
|
|
158
|
+
});
|