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/utils/tokens.ts
CHANGED
|
@@ -1,70 +1,129 @@
|
|
|
1
|
-
import { Tiktoken } from 'js-tiktoken/lite';
|
|
2
|
-
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
|
-
import { ContentTypes } from '@/common/enum';
|
|
4
|
-
|
|
5
|
-
export function getTokenCountForMessage(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
item
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
1
|
+
import { Tiktoken } from 'js-tiktoken/lite';
|
|
2
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
|
+
import { ContentTypes } from '@/common/enum';
|
|
4
|
+
|
|
5
|
+
export function getTokenCountForMessage(
|
|
6
|
+
message: BaseMessage,
|
|
7
|
+
getTokenCount: (text: string) => number
|
|
8
|
+
): number {
|
|
9
|
+
const tokensPerMessage = 3;
|
|
10
|
+
|
|
11
|
+
const processValue = (value: unknown): void => {
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
for (const item of value) {
|
|
14
|
+
if (
|
|
15
|
+
!item ||
|
|
16
|
+
!item.type ||
|
|
17
|
+
item.type === ContentTypes.ERROR ||
|
|
18
|
+
item.type === ContentTypes.IMAGE_URL
|
|
19
|
+
) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (item.type === ContentTypes.TOOL_CALL && item.tool_call != null) {
|
|
24
|
+
const toolName = item.tool_call?.name || '';
|
|
25
|
+
if (toolName != null && toolName && typeof toolName === 'string') {
|
|
26
|
+
numTokens += getTokenCount(toolName);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const args = item.tool_call?.args || '';
|
|
30
|
+
if (args != null && args && typeof args === 'string') {
|
|
31
|
+
numTokens += getTokenCount(args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const output = item.tool_call?.output || '';
|
|
35
|
+
if (output != null && output && typeof output === 'string') {
|
|
36
|
+
numTokens += getTokenCount(output);
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const nestedValue = item[item.type];
|
|
42
|
+
|
|
43
|
+
if (!nestedValue) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
processValue(nestedValue);
|
|
48
|
+
}
|
|
49
|
+
} else if (typeof value === 'string') {
|
|
50
|
+
numTokens += getTokenCount(value);
|
|
51
|
+
} else if (typeof value === 'number') {
|
|
52
|
+
numTokens += getTokenCount(value.toString());
|
|
53
|
+
} else if (typeof value === 'boolean') {
|
|
54
|
+
numTokens += getTokenCount(value.toString());
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let numTokens = tokensPerMessage;
|
|
59
|
+
processValue(message.content);
|
|
60
|
+
return numTokens;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let encoderPromise: Promise<Tiktoken> | undefined;
|
|
64
|
+
let tokenCounterPromise: Promise<(message: BaseMessage) => number> | undefined;
|
|
65
|
+
|
|
66
|
+
async function getSharedEncoder(): Promise<Tiktoken> {
|
|
67
|
+
if (encoderPromise) {
|
|
68
|
+
return encoderPromise;
|
|
69
|
+
}
|
|
70
|
+
encoderPromise = (async (): Promise<Tiktoken> => {
|
|
71
|
+
const res = await fetch('https://tiktoken.pages.dev/js/o200k_base.json');
|
|
72
|
+
const o200k_base = await res.json();
|
|
73
|
+
return new Tiktoken(o200k_base);
|
|
74
|
+
})();
|
|
75
|
+
return encoderPromise;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a singleton token counter function that reuses the same encoder instance.
|
|
80
|
+
* This avoids creating multiple function closures and prevents potential memory issues.
|
|
81
|
+
*/
|
|
82
|
+
export const createTokenCounter = async (): Promise<
|
|
83
|
+
(message: BaseMessage) => number
|
|
84
|
+
> => {
|
|
85
|
+
if (tokenCounterPromise) {
|
|
86
|
+
return tokenCounterPromise;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
tokenCounterPromise = (async (): Promise<
|
|
90
|
+
(message: BaseMessage) => number
|
|
91
|
+
> => {
|
|
92
|
+
const enc = await getSharedEncoder();
|
|
93
|
+
const countTokens = (text: string): number => enc.encode(text).length;
|
|
94
|
+
return (message: BaseMessage): number =>
|
|
95
|
+
getTokenCountForMessage(message, countTokens);
|
|
96
|
+
})();
|
|
97
|
+
|
|
98
|
+
return tokenCounterPromise;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Utility to manage the token encoder lifecycle explicitly.
|
|
103
|
+
* Useful for applications that need fine-grained control over resource management.
|
|
104
|
+
*/
|
|
105
|
+
export const TokenEncoderManager = {
|
|
106
|
+
/**
|
|
107
|
+
* Pre-initializes the encoder. This can be called during app startup
|
|
108
|
+
* to avoid lazy loading delays later.
|
|
109
|
+
*/
|
|
110
|
+
async initialize(): Promise<void> {
|
|
111
|
+
await getSharedEncoder();
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Clears the cached encoder and token counter.
|
|
116
|
+
* Useful for testing or when you need to force a fresh reload.
|
|
117
|
+
*/
|
|
118
|
+
reset(): void {
|
|
119
|
+
encoderPromise = undefined;
|
|
120
|
+
tokenCounterPromise = undefined;
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Checks if the encoder has been initialized.
|
|
125
|
+
*/
|
|
126
|
+
isInitialized(): boolean {
|
|
127
|
+
return encoderPromise !== undefined;
|
|
128
|
+
},
|
|
129
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function capitalizeFirstLetter(string: string): string;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
|
|
2
|
-
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
export declare const fetchRandomImageTool: DynamicStructuredTool<z.ZodObject<{
|
|
5
|
-
input: z.ZodOptional<z.ZodString>;
|
|
6
|
-
}, "strip", z.ZodTypeAny, {
|
|
7
|
-
input?: string | undefined;
|
|
8
|
-
}, {
|
|
9
|
-
input?: string | undefined;
|
|
10
|
-
}>, {
|
|
11
|
-
input?: string | undefined;
|
|
12
|
-
}, {
|
|
13
|
-
input?: string | undefined;
|
|
14
|
-
}, (string | undefined)[] | ({
|
|
15
|
-
type: string;
|
|
16
|
-
text: string;
|
|
17
|
-
}[] | {
|
|
18
|
-
content: {
|
|
19
|
-
type: string;
|
|
20
|
-
image_url: {
|
|
21
|
-
url: string;
|
|
22
|
-
};
|
|
23
|
-
}[];
|
|
24
|
-
})[]>;
|
|
25
|
-
export declare const fetchRandomImageURL: DynamicStructuredTool<z.ZodObject<{
|
|
26
|
-
input: z.ZodOptional<z.ZodString>;
|
|
27
|
-
}, "strip", z.ZodTypeAny, {
|
|
28
|
-
input?: string | undefined;
|
|
29
|
-
}, {
|
|
30
|
-
input?: string | undefined;
|
|
31
|
-
}>, {
|
|
32
|
-
input?: string | undefined;
|
|
33
|
-
}, {
|
|
34
|
-
input?: string | undefined;
|
|
35
|
-
}, (string | undefined)[] | ({
|
|
36
|
-
type: string;
|
|
37
|
-
text: string;
|
|
38
|
-
}[] | {
|
|
39
|
-
content: {
|
|
40
|
-
type: string;
|
|
41
|
-
image_url: {
|
|
42
|
-
url: string;
|
|
43
|
-
};
|
|
44
|
-
}[];
|
|
45
|
-
})[]>;
|
|
46
|
-
export declare const chartTool: DynamicStructuredTool<z.ZodObject<{
|
|
47
|
-
data: z.ZodArray<z.ZodObject<{
|
|
48
|
-
label: z.ZodString;
|
|
49
|
-
value: z.ZodNumber;
|
|
50
|
-
}, "strip", z.ZodTypeAny, {
|
|
51
|
-
value: number;
|
|
52
|
-
label: string;
|
|
53
|
-
}, {
|
|
54
|
-
value: number;
|
|
55
|
-
label: string;
|
|
56
|
-
}>, "many">;
|
|
57
|
-
}, "strip", z.ZodTypeAny, {
|
|
58
|
-
data: {
|
|
59
|
-
value: number;
|
|
60
|
-
label: string;
|
|
61
|
-
}[];
|
|
62
|
-
}, {
|
|
63
|
-
data: {
|
|
64
|
-
value: number;
|
|
65
|
-
label: string;
|
|
66
|
-
}[];
|
|
67
|
-
}>, {
|
|
68
|
-
data: {
|
|
69
|
-
value: number;
|
|
70
|
-
label: string;
|
|
71
|
-
}[];
|
|
72
|
-
}, {
|
|
73
|
-
data: {
|
|
74
|
-
value: number;
|
|
75
|
-
label: string;
|
|
76
|
-
}[];
|
|
77
|
-
}, string>;
|
|
78
|
-
export declare const tavilyTool: TavilySearchResults;
|
package/src/tools/example.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
|
|
2
|
-
import { DynamicStructuredTool , tool } from '@langchain/core/tools';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
import { config } from 'dotenv';
|
|
5
|
-
|
|
6
|
-
config();
|
|
7
|
-
|
|
8
|
-
import fetch from 'node-fetch';
|
|
9
|
-
import { Constants } from '@/common';
|
|
10
|
-
|
|
11
|
-
const fetchImageSchema = z.object({ input: z.string().optional() });
|
|
12
|
-
|
|
13
|
-
export const fetchRandomImageTool = tool(
|
|
14
|
-
async () => {
|
|
15
|
-
try {
|
|
16
|
-
// Lorem Picsum provides random images at any size
|
|
17
|
-
const imageUrl = 'https://picsum.photos/200/300';
|
|
18
|
-
|
|
19
|
-
const imageResponse = await fetch(imageUrl);
|
|
20
|
-
// eslint-disable-next-line no-console
|
|
21
|
-
console.log(imageResponse);
|
|
22
|
-
const arrayBuffer = await imageResponse.arrayBuffer();
|
|
23
|
-
const base64 = Buffer.from(arrayBuffer).toString('base64');
|
|
24
|
-
const content = [{
|
|
25
|
-
type: 'image_url',
|
|
26
|
-
image_url: {
|
|
27
|
-
url: `data:image/jpeg;base64,${base64}`,
|
|
28
|
-
},
|
|
29
|
-
}];
|
|
30
|
-
|
|
31
|
-
const response = [
|
|
32
|
-
{
|
|
33
|
-
type: 'text',
|
|
34
|
-
text: 'Random image from Lorem Picsum, taken at 800x600',
|
|
35
|
-
},
|
|
36
|
-
];
|
|
37
|
-
return [response, { content }];
|
|
38
|
-
} catch (error) {
|
|
39
|
-
return [`Error fetching image: ${(error as Error).message}`, undefined];
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
name: 'fetchRandomImage',
|
|
44
|
-
description: 'Fetches a random image from Lorem Picsum',
|
|
45
|
-
schema: fetchImageSchema,
|
|
46
|
-
responseFormat: Constants.CONTENT_AND_ARTIFACT,
|
|
47
|
-
}
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
export const fetchRandomImageURL = tool(
|
|
51
|
-
async () => {
|
|
52
|
-
try {
|
|
53
|
-
const imageUrl = 'https://picsum.photos/200/300';
|
|
54
|
-
|
|
55
|
-
const imageResponse = await fetch(imageUrl);
|
|
56
|
-
const content = [{
|
|
57
|
-
type: 'image_url',
|
|
58
|
-
image_url: {
|
|
59
|
-
url: imageResponse.url,
|
|
60
|
-
},
|
|
61
|
-
}];
|
|
62
|
-
|
|
63
|
-
// eslint-disable-next-line no-console
|
|
64
|
-
console.log('URL RESPONSE', imageResponse.url);
|
|
65
|
-
|
|
66
|
-
const response = [
|
|
67
|
-
{
|
|
68
|
-
type: 'text',
|
|
69
|
-
text: 'Random image from Lorem Picsum, taken at 800x600',
|
|
70
|
-
},
|
|
71
|
-
];
|
|
72
|
-
return [response, { content }];
|
|
73
|
-
} catch (error) {
|
|
74
|
-
return [`Error fetching image: ${(error as Error).message}`, undefined];
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
name: 'fetchRandomImage',
|
|
79
|
-
description: 'Fetches a random image from Lorem Picsum',
|
|
80
|
-
schema: fetchImageSchema,
|
|
81
|
-
responseFormat: Constants.CONTENT_AND_ARTIFACT,
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
export const chartTool = new DynamicStructuredTool({
|
|
86
|
-
name: 'generate_bar_chart',
|
|
87
|
-
description:
|
|
88
|
-
'Generates a text-based bar chart from an array of data points and returns it as a string.',
|
|
89
|
-
schema: z.object({
|
|
90
|
-
data: z.array(
|
|
91
|
-
z.object({
|
|
92
|
-
label: z.string(),
|
|
93
|
-
value: z.number(),
|
|
94
|
-
})
|
|
95
|
-
),
|
|
96
|
-
}),
|
|
97
|
-
func: async ({ data }): Promise<string> => {
|
|
98
|
-
const maxValue = Math.max(...data.map(d => d.value));
|
|
99
|
-
const chartHeight = 20;
|
|
100
|
-
const chartWidth = 50;
|
|
101
|
-
|
|
102
|
-
let chart = '';
|
|
103
|
-
|
|
104
|
-
// Generate Y-axis labels and bars
|
|
105
|
-
for (let i = chartHeight; i >= 0; i--) {
|
|
106
|
-
const row = data.map(d => {
|
|
107
|
-
const barHeight = Math.round((d.value / maxValue) * chartHeight);
|
|
108
|
-
return barHeight >= i ? '█' : ' ';
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const yLabel = i === chartHeight ? maxValue.toString().padStart(4) :
|
|
112
|
-
i === 0 ? '0'.padStart(4) :
|
|
113
|
-
i % 5 === 0 ? Math.round((i / chartHeight) * maxValue).toString().padStart(4) : ' ';
|
|
114
|
-
|
|
115
|
-
chart += `${yLabel} │${row.join(' ')} \n`;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Generate X-axis
|
|
119
|
-
chart += ' ├' + '─'.repeat(chartWidth) + '\n';
|
|
120
|
-
|
|
121
|
-
// Generate X-axis labels
|
|
122
|
-
const xLabels = data.map(d => d.label.padEnd(5).substring(0, 5)).join(' ');
|
|
123
|
-
chart += ` ${xLabels}`;
|
|
124
|
-
|
|
125
|
-
return chart;
|
|
126
|
-
},
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
export const tavilyTool = new TavilySearchResults();
|