@fractal-solutions/qflow 1.0.60 → 1.0.62
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/README.md +75 -74
- package/dist/qflow.config.d.ts +5 -0
- package/dist/qflow.config.js +16 -0
- package/dist/src/agent/agent.d.ts +23 -0
- package/dist/src/agent/agent.js +365 -0
- package/dist/src/agent/base_agent.d.ts +21 -0
- package/dist/src/agent/base_agent.js +206 -0
- package/dist/src/agent/logger.d.ts +9 -0
- package/dist/src/agent/logger.js +45 -0
- package/dist/src/agent/lpavrl_agent.d.ts +5 -0
- package/dist/src/agent/lpavrl_agent.js +178 -0
- package/dist/src/agent/ooda_agent.d.ts +5 -0
- package/dist/src/agent/ooda_agent.js +167 -0
- package/dist/src/agent/pema_agent.d.ts +5 -0
- package/dist/src/agent/pema_agent.js +171 -0
- package/dist/src/agent/raor_agent.d.ts +5 -0
- package/dist/src/agent/raor_agent.js +164 -0
- package/dist/src/agent/tools.d.ts +1 -0
- package/dist/src/agent/tools.js +148 -0
- package/dist/src/logger.d.ts +9 -0
- package/dist/src/logger.js +107 -0
- package/dist/src/nodes/agent_llm.d.ts +31 -0
- package/dist/src/nodes/agent_llm.js +302 -0
- package/dist/src/nodes/browser_control.d.ts +35 -0
- package/dist/src/nodes/browser_control.js +106 -0
- package/dist/src/nodes/code_interpreter.d.ts +38 -0
- package/dist/src/nodes/code_interpreter.js +150 -0
- package/dist/src/nodes/data_extractor.d.ts +40 -0
- package/dist/src/nodes/data_extractor.js +118 -0
- package/dist/src/nodes/data_validation.d.ts +36 -0
- package/dist/src/nodes/data_validation.js +77 -0
- package/dist/src/nodes/database.d.ts +50 -0
- package/dist/src/nodes/database.js +135 -0
- package/dist/src/nodes/display_image.d.ts +18 -0
- package/dist/src/nodes/display_image.js +63 -0
- package/dist/src/nodes/embedding.d.ts +29 -0
- package/dist/src/nodes/embedding.js +69 -0
- package/dist/src/nodes/filesystem.d.ts +105 -0
- package/dist/src/nodes/filesystem.js +160 -0
- package/dist/src/nodes/gis.d.ts +62 -0
- package/dist/src/nodes/gis.js +133 -0
- package/dist/src/nodes/git.d.ts +46 -0
- package/dist/src/nodes/git.js +99 -0
- package/dist/src/nodes/github.d.ts +83 -0
- package/dist/src/nodes/github.js +135 -0
- package/dist/src/nodes/hackernews.d.ts +47 -0
- package/dist/src/nodes/hackernews.js +79 -0
- package/dist/src/nodes/hardware_interaction.d.ts +39 -0
- package/dist/src/nodes/hardware_interaction.js +131 -0
- package/dist/src/nodes/http.d.ts +77 -0
- package/dist/src/nodes/http.js +120 -0
- package/dist/src/nodes/http_server.d.ts +54 -0
- package/dist/src/nodes/http_server.js +223 -0
- package/dist/src/nodes/image_gallery.d.ts +37 -0
- package/dist/src/nodes/image_gallery.js +129 -0
- package/dist/src/nodes/index.d.ts +48 -0
- package/dist/src/nodes/index.js +48 -0
- package/dist/src/nodes/interactive_input.d.ts +26 -0
- package/dist/src/nodes/interactive_input.js +95 -0
- package/dist/src/nodes/interactive_webview.d.ts +78 -0
- package/dist/src/nodes/interactive_webview.js +369 -0
- package/dist/src/nodes/iterator.d.ts +23 -0
- package/dist/src/nodes/iterator.js +43 -0
- package/dist/src/nodes/llm.d.ts +160 -0
- package/dist/src/nodes/llm.js +367 -0
- package/dist/src/nodes/memory.d.ts +43 -0
- package/dist/src/nodes/memory.js +90 -0
- package/dist/src/nodes/multimedia_processing.d.ts +57 -0
- package/dist/src/nodes/multimedia_processing.js +129 -0
- package/dist/src/nodes/openrouter_llm.d.ts +38 -0
- package/dist/src/nodes/openrouter_llm.js +156 -0
- package/dist/src/nodes/pdf_processor.d.ts +43 -0
- package/dist/src/nodes/pdf_processor.js +112 -0
- package/dist/src/nodes/remote_execution.d.ts +51 -0
- package/dist/src/nodes/remote_execution.js +132 -0
- package/dist/src/nodes/s3.d.ts +73 -0
- package/dist/src/nodes/s3.js +107 -0
- package/dist/src/nodes/scheduler.d.ts +51 -0
- package/dist/src/nodes/scheduler.js +131 -0
- package/dist/src/nodes/search.d.ts +61 -0
- package/dist/src/nodes/search.js +118 -0
- package/dist/src/nodes/semantic_memory.d.ts +64 -0
- package/dist/src/nodes/semantic_memory.js +160 -0
- package/dist/src/nodes/shell.d.ts +25 -0
- package/dist/src/nodes/shell.js +45 -0
- package/dist/src/nodes/speech_synthesis.d.ts +72 -0
- package/dist/src/nodes/speech_synthesis.js +171 -0
- package/dist/src/nodes/spreadsheet.d.ts +61 -0
- package/dist/src/nodes/spreadsheet.js +294 -0
- package/dist/src/nodes/stripe.d.ts +67 -0
- package/dist/src/nodes/stripe.js +117 -0
- package/dist/src/nodes/subflow.d.ts +23 -0
- package/dist/src/nodes/subflow.js +36 -0
- package/dist/src/nodes/summarize.d.ts +26 -0
- package/dist/src/nodes/summarize.js +51 -0
- package/dist/src/nodes/system_notification.d.ts +26 -0
- package/dist/src/nodes/system_notification.js +74 -0
- package/dist/src/nodes/transform.d.ts +23 -0
- package/dist/src/nodes/transform.js +52 -0
- package/dist/src/nodes/user.d.ts +25 -0
- package/dist/src/nodes/user.js +48 -0
- package/dist/src/nodes/webhook.d.ts +49 -0
- package/dist/src/nodes/webhook.js +161 -0
- package/dist/src/nodes/webscraper.d.ts +25 -0
- package/dist/src/nodes/webscraper.js +49 -0
- package/dist/src/nodes/websockets.d.ts +27 -0
- package/dist/src/nodes/websockets.js +98 -0
- package/dist/src/nodes/webview.d.ts +38 -0
- package/dist/src/nodes/webview.js +55 -0
- package/dist/src/qflow.d.ts +69 -0
- package/dist/src/qflow.js +320 -0
- package/dist/src/types.d.ts +6 -0
- package/dist/src/types.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { DeepSeekLLMNode, OpenAILLMNode, GeminiLLMNode, OllamaLLMNode, HuggingFaceLLMNode } from './llm.js';
|
|
2
|
+
import { log } from '../logger.js';
|
|
3
|
+
/**
|
|
4
|
+
* A specialized DeepSeekLLMNode for use within the AgentNode.
|
|
5
|
+
* It expects the full prompt (conversation history) to be provided directly via setParams.
|
|
6
|
+
*/
|
|
7
|
+
export class AgentDeepSeekLLMNode extends DeepSeekLLMNode {
|
|
8
|
+
// Override execAsync to handle the conversation history format
|
|
9
|
+
async execAsync() {
|
|
10
|
+
const { prompt, apiKey, keyword } = this.params; // prompt here is the stringified conversation history
|
|
11
|
+
if (!prompt) {
|
|
12
|
+
throw new Error("AgentDeepSeekLLMNode: Prompt (conversation history) is missing from params.");
|
|
13
|
+
}
|
|
14
|
+
if (!apiKey) {
|
|
15
|
+
throw new Error("DeepSeek API Key is not configured for AgentDeepSeekLLMNode.");
|
|
16
|
+
}
|
|
17
|
+
let messages;
|
|
18
|
+
try {
|
|
19
|
+
messages = JSON.parse(prompt);
|
|
20
|
+
// Ensure messages array contains objects with role and content
|
|
21
|
+
if (!Array.isArray(messages) || !messages.every(msg => msg.role && msg.content !== undefined)) {
|
|
22
|
+
throw new Error("Parsed prompt is not a valid messages array.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
throw new Error(`AgentDeepSeekLLMNode: Invalid prompt format. Expected stringified JSON array of messages. Error: ${e.message}`);
|
|
27
|
+
}
|
|
28
|
+
log(`[DeepSeek] Sending prompt for "${keyword || 'agent_reasoning'}"...`, this.params.logging);
|
|
29
|
+
const response = await fetch('https://api.deepseek.com/chat/completions', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
'Authorization': `Bearer ${apiKey}`
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
model: 'deepseek-chat',
|
|
37
|
+
messages: messages, // Use the parsed messages array directly
|
|
38
|
+
max_tokens: 2048,
|
|
39
|
+
temperature: 0.7,
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const errorData = await response.json();
|
|
44
|
+
throw new Error(`DeepSeek API error: ${response.status} - ${errorData.error.message}`);
|
|
45
|
+
}
|
|
46
|
+
const data = await response.json();
|
|
47
|
+
//console.log("DeepSeek Raw Data:", JSON.stringify(data, null, 2)); // Added logging
|
|
48
|
+
if (!data.choices || data.choices.length === 0 || !data.choices[0].message || typeof data.choices[0].message.content !== 'string') {
|
|
49
|
+
throw new Error('Invalid response structure from DeepSeek API or missing content.');
|
|
50
|
+
}
|
|
51
|
+
const llmResponse = data.choices[0].message.content.trim();
|
|
52
|
+
log(`[DeepSeek] Received response for "${keyword || 'agent_reasoning'}".`, this.params.logging);
|
|
53
|
+
return llmResponse; // Return the actual content
|
|
54
|
+
}
|
|
55
|
+
async postAsync(shared, prepRes, execRes) {
|
|
56
|
+
shared.llmResponse = execRes;
|
|
57
|
+
return execRes; // Return the actual LLM response
|
|
58
|
+
}
|
|
59
|
+
// preparePrompt is no longer needed as execAsync handles the prompt directly
|
|
60
|
+
preparePrompt(shared) {
|
|
61
|
+
// This method is intentionally left empty or can throw an error if called,
|
|
62
|
+
// as the prompt is handled directly in execAsync for AgentNode integration.
|
|
63
|
+
throw new Error("preparePrompt should not be called on AgentDeepSeekLLMNode. Prompt is handled in execAsync.");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A specialized OpenAILLMNode for use within the AgentNode.
|
|
68
|
+
* It expects the full prompt (conversation history) to be provided directly via setParams.
|
|
69
|
+
*/
|
|
70
|
+
export class AgentOpenAILLMNode extends OpenAILLMNode {
|
|
71
|
+
// Override execAsync to handle the conversation history format
|
|
72
|
+
async execAsync() {
|
|
73
|
+
const { prompt, apiKey, keyword } = this.params; // prompt here is the stringified conversation history
|
|
74
|
+
if (!prompt) {
|
|
75
|
+
throw new Error("AgentOpenAILLMNode: Prompt (conversation history) is missing from params.");
|
|
76
|
+
}
|
|
77
|
+
if (!apiKey) {
|
|
78
|
+
throw new Error("OpenAI API Key is not configured for AgentOpenAILLMNode.");
|
|
79
|
+
}
|
|
80
|
+
let messages;
|
|
81
|
+
try {
|
|
82
|
+
messages = JSON.parse(prompt);
|
|
83
|
+
// Ensure messages array contains objects with role and content
|
|
84
|
+
if (!Array.isArray(messages) || !messages.every(msg => msg.role && msg.content !== undefined)) {
|
|
85
|
+
throw new Error("Parsed prompt is not a valid messages array.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
throw new Error(`AgentOpenAILLMNode: Invalid prompt format. Expected stringified JSON array of messages. Error: ${e.message}`);
|
|
90
|
+
}
|
|
91
|
+
log(`[OpenAI] Sending prompt for "${keyword || 'agent_reasoning'}"...`, this.params.logging);
|
|
92
|
+
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
headers: {
|
|
95
|
+
'Content-Type': 'application/json',
|
|
96
|
+
'Authorization': `Bearer ${apiKey}`
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify({
|
|
99
|
+
model: 'gpt-3.5-turbo', // Or gpt-4, etc.
|
|
100
|
+
messages: messages, // Use the parsed messages array directly
|
|
101
|
+
max_tokens: 1500, // Increased max_tokens for agent responses
|
|
102
|
+
temperature: 0.7,
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
if (!response.ok) {
|
|
106
|
+
const errorData = await response.json();
|
|
107
|
+
throw new Error(`OpenAI API error: ${response.status} - ${errorData.error.message}`);
|
|
108
|
+
}
|
|
109
|
+
const data = await response.json();
|
|
110
|
+
if (!data.choices || data.choices.length === 0 || !data.choices[0].message || typeof data.choices[0].message.content !== 'string') {
|
|
111
|
+
throw new Error('Invalid response structure from OpenAI API or missing content.');
|
|
112
|
+
}
|
|
113
|
+
const llmResponse = data.choices[0].message.content.trim();
|
|
114
|
+
log(`[OpenAI] Received response for "${keyword || 'agent_reasoning'}".`, this.params.logging);
|
|
115
|
+
return llmResponse; // Return the actual content
|
|
116
|
+
}
|
|
117
|
+
async postAsync(shared, prepRes, execRes) {
|
|
118
|
+
shared.llmResponse = execRes;
|
|
119
|
+
return execRes; // Return the actual LLM response
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export class AgentGeminiLLMNode extends GeminiLLMNode {
|
|
123
|
+
async execAsync() {
|
|
124
|
+
const messages = JSON.parse(this.params.prompt);
|
|
125
|
+
const { apiKey } = this.params;
|
|
126
|
+
if (!messages || messages.length === 0) {
|
|
127
|
+
throw new Error('Messages are required for AgentGeminiLLMNode.');
|
|
128
|
+
}
|
|
129
|
+
if (!apiKey) {
|
|
130
|
+
throw new Error('Google Gemini API Key is required.');
|
|
131
|
+
}
|
|
132
|
+
log(`[AgentGemini] Sending prompt to Gemini...`, this.params.logging);
|
|
133
|
+
try {
|
|
134
|
+
const contents = messages.map(msg => ({
|
|
135
|
+
role: msg.role === 'user' ? 'user' : 'model',
|
|
136
|
+
parts: [{ text: msg.content }]
|
|
137
|
+
}));
|
|
138
|
+
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${apiKey}`, {
|
|
139
|
+
method: 'POST',
|
|
140
|
+
headers: {
|
|
141
|
+
'Content-Type': 'application/json'
|
|
142
|
+
},
|
|
143
|
+
body: JSON.stringify({
|
|
144
|
+
contents: contents,
|
|
145
|
+
safetySettings: [
|
|
146
|
+
{
|
|
147
|
+
"category": "HARM_CATEGORY_HARASSMENT",
|
|
148
|
+
"threshold": "BLOCK_NONE"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
|
152
|
+
"threshold": "BLOCK_NONE"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
156
|
+
"threshold": "BLOCK_NONE"
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
160
|
+
"threshold": "BLOCK_NONE"
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
})
|
|
164
|
+
});
|
|
165
|
+
if (!response.ok) {
|
|
166
|
+
const errorData = await response.json();
|
|
167
|
+
throw new Error(`Gemini API error: ${response.status} - ${errorData.error.message}`);
|
|
168
|
+
}
|
|
169
|
+
const data = await response.json();
|
|
170
|
+
const llmResponse = data.candidates[0].content.parts[0].text.trim();
|
|
171
|
+
log(`[AgentGemini] Received response from Gemini.`, this.params.logging);
|
|
172
|
+
return llmResponse;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
log('AgentGeminiLLMNode: Error during API call:', this.params.logging, { type: 'error' });
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async postAsync(shared, prepRes, execRes) {
|
|
180
|
+
shared.llmResponse = execRes; // Store the full LLM response object
|
|
181
|
+
return execRes; // Return the full LLM response object
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
export class AgentOllamaLLMNode extends OllamaLLMNode {
|
|
185
|
+
preparePrompt(shared) {
|
|
186
|
+
const messages = JSON.parse(this.params.prompt);
|
|
187
|
+
// Ollama's /api/generate endpoint expects a single prompt string,
|
|
188
|
+
// so we'll concatenate the conversation history into a single prompt.
|
|
189
|
+
// A more sophisticated approach might use a chat-specific endpoint if available.
|
|
190
|
+
this.params.prompt = messages.map(msg => `${msg.role}: ${msg.content}`).join('\n') + '\nassistant:';
|
|
191
|
+
}
|
|
192
|
+
async execAsync(prepRes, shared) {
|
|
193
|
+
this.preparePrompt(shared);
|
|
194
|
+
const { prompt, model = 'llama2', baseUrl = 'http://localhost:11434' } = this.params;
|
|
195
|
+
if (!prompt) {
|
|
196
|
+
throw new Error('Prompt is required for AgentOllamaLLMNode.');
|
|
197
|
+
}
|
|
198
|
+
log(`[AgentOllama] Sending prompt to ${model} at ${baseUrl}...`, this.params.logging);
|
|
199
|
+
try {
|
|
200
|
+
const response = await fetch(`${baseUrl}/api/generate`, {
|
|
201
|
+
method: 'POST',
|
|
202
|
+
headers: {
|
|
203
|
+
'Content-Type': 'application/json',
|
|
204
|
+
},
|
|
205
|
+
body: JSON.stringify({
|
|
206
|
+
model: model,
|
|
207
|
+
prompt: prompt,
|
|
208
|
+
stream: false,
|
|
209
|
+
}),
|
|
210
|
+
});
|
|
211
|
+
if (!response.ok) {
|
|
212
|
+
const errorData = await response.json();
|
|
213
|
+
throw new Error(`Ollama API error: ${response.status} - ${errorData.error}`);
|
|
214
|
+
}
|
|
215
|
+
const data = await response.json();
|
|
216
|
+
const llmResponse = data.response.trim();
|
|
217
|
+
log(`[AgentOllama] Received response from ${model}.`, this.params.logging);
|
|
218
|
+
return llmResponse;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
log('AgentOllamaLLMNode: Error during API call:', this.params.logging, { type: 'error' });
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async postAsync(shared, prepRes, execRes) {
|
|
226
|
+
shared.llmResponse = execRes; // Store the full LLM response object
|
|
227
|
+
return execRes; // Return the full LLM response object
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export class AgentHuggingFaceLLMNode extends HuggingFaceLLMNode {
|
|
231
|
+
preparePrompt(shared) {
|
|
232
|
+
const messages = JSON.parse(this.params.prompt);
|
|
233
|
+
// Hugging Face router (OpenAI-compatible) expects a messages array.
|
|
234
|
+
// We'll pass the parsed messages directly.
|
|
235
|
+
this.params.messages = messages;
|
|
236
|
+
}
|
|
237
|
+
async execAsync(prepRes, shared) {
|
|
238
|
+
this.preparePrompt(shared); // Prepare the prompt from shared state
|
|
239
|
+
const { messages, model, hfToken, temperature, max_new_tokens, baseUrl } = this.params;
|
|
240
|
+
if (!messages || messages.length === 0) {
|
|
241
|
+
throw new Error('AgentHuggingFaceLLMNode requires `messages`.');
|
|
242
|
+
}
|
|
243
|
+
if (!model) {
|
|
244
|
+
throw new Error('AgentHuggingFaceLLMNode requires a `model`.');
|
|
245
|
+
}
|
|
246
|
+
if (!hfToken) {
|
|
247
|
+
throw new Error('AgentHuggingFaceLLMNode requires a `hfToken`.');
|
|
248
|
+
}
|
|
249
|
+
const url = `${baseUrl || 'https://router.huggingface.co/v1'}/chat/completions`;
|
|
250
|
+
const headers = {
|
|
251
|
+
'Content-Type': 'application/json',
|
|
252
|
+
'Authorization': `Bearer ${hfToken}`,
|
|
253
|
+
};
|
|
254
|
+
const body = JSON.stringify({
|
|
255
|
+
model: model,
|
|
256
|
+
messages: messages,
|
|
257
|
+
temperature: temperature || 0.7,
|
|
258
|
+
max_tokens: max_new_tokens || 500,
|
|
259
|
+
});
|
|
260
|
+
log(`[AgentHuggingFace] Sending prompt to ${model} at ${url}...`, this.params.logging);
|
|
261
|
+
try {
|
|
262
|
+
const response = await fetch(url, {
|
|
263
|
+
method: 'POST',
|
|
264
|
+
headers: headers,
|
|
265
|
+
body: body,
|
|
266
|
+
});
|
|
267
|
+
if (!response.ok) {
|
|
268
|
+
let errorData;
|
|
269
|
+
try {
|
|
270
|
+
errorData = await response.json();
|
|
271
|
+
}
|
|
272
|
+
catch (e) {
|
|
273
|
+
errorData = await response.text(); // Get raw text if not JSON
|
|
274
|
+
}
|
|
275
|
+
throw new Error(`Hugging Face API error: ${response.status} ${response.statusText} - ${JSON.stringify(errorData)}`);
|
|
276
|
+
}
|
|
277
|
+
const data = await response.json();
|
|
278
|
+
//console.log("HuggingFace Agent Raw Data:", JSON.stringify(data, null, 2)); // Added logging
|
|
279
|
+
let llmResponse = '';
|
|
280
|
+
if (data && data.choices && data.choices.length > 0 && data.choices[0].message && typeof data.choices[0].message.content === 'string') {
|
|
281
|
+
llmResponse = data.choices[0].message.content.trim();
|
|
282
|
+
}
|
|
283
|
+
else if (data && Array.isArray(data) && data[0] && typeof data[0].generated_text === 'string') {
|
|
284
|
+
// Fallback for models that return directly generated_text (e.g., older HF inference API)
|
|
285
|
+
llmResponse = data[0].generated_text.trim();
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
throw new Error('Invalid or unexpected response structure from Hugging Face API.');
|
|
289
|
+
}
|
|
290
|
+
log(`[AgentHuggingFace] Received response from ${model}.`, this.params.logging);
|
|
291
|
+
return data; // Return the full data object for AgentNode to parse
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
log('AgentHuggingFaceLLMNode: Error during API call:', this.params.logging, { type: 'error' });
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
async postAsync(shared, prepRes, execRes) {
|
|
299
|
+
shared.llmResponse = execRes; // Store the full LLM response object
|
|
300
|
+
return execRes; // Return the full LLM response object
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class BrowserControlNode extends AsyncNode {
|
|
2
|
+
static getToolDefinition(): {
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
parameters: {
|
|
6
|
+
type: string;
|
|
7
|
+
properties: {
|
|
8
|
+
action: {
|
|
9
|
+
type: string;
|
|
10
|
+
enum: string[];
|
|
11
|
+
description: string;
|
|
12
|
+
};
|
|
13
|
+
url: {
|
|
14
|
+
type: string;
|
|
15
|
+
description: string;
|
|
16
|
+
};
|
|
17
|
+
selector: {
|
|
18
|
+
type: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
text: {
|
|
22
|
+
type: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
path: {
|
|
26
|
+
type: string;
|
|
27
|
+
description: string;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
required: string[];
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
execAsync(prepRes: any, shared: any): Promise<{}>;
|
|
34
|
+
}
|
|
35
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { chromium, firefox, webkit } from 'playwright';
|
|
3
|
+
export class BrowserControlNode extends AsyncNode {
|
|
4
|
+
static getToolDefinition() {
|
|
5
|
+
return {
|
|
6
|
+
name: "browser_control",
|
|
7
|
+
description: "Controls a web browser to navigate pages, interact with elements, and take screenshots.",
|
|
8
|
+
parameters: {
|
|
9
|
+
type: "object",
|
|
10
|
+
properties: {
|
|
11
|
+
action: {
|
|
12
|
+
type: "string",
|
|
13
|
+
enum: ["goto", "click", "type", "screenshot", "close"],
|
|
14
|
+
description: "The browser action to perform."
|
|
15
|
+
},
|
|
16
|
+
url: {
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "The URL to navigate to (for 'goto' action)."
|
|
19
|
+
},
|
|
20
|
+
selector: {
|
|
21
|
+
type: "string",
|
|
22
|
+
description: "A CSS selector to target an element (for 'click' and 'type' actions)."
|
|
23
|
+
},
|
|
24
|
+
text: {
|
|
25
|
+
type: "string",
|
|
26
|
+
description: "The text to type into an input field (for 'type' action)."
|
|
27
|
+
},
|
|
28
|
+
path: {
|
|
29
|
+
type: "string",
|
|
30
|
+
description: "The file path to save a screenshot (for 'screenshot' action)."
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
required: ["action"]
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async execAsync(prepRes, shared) {
|
|
38
|
+
const { action, url, selector, text, path, script, browserType = 'chromium', launchOptions = { headless: true }, } = this.params;
|
|
39
|
+
let browser = shared.browser;
|
|
40
|
+
let page = shared.page;
|
|
41
|
+
try {
|
|
42
|
+
if (!browser || !browser.isConnected()) {
|
|
43
|
+
const browserLauncher = { chromium, firefox, webkit }[browserType];
|
|
44
|
+
if (!browserLauncher) {
|
|
45
|
+
throw new Error(`Unsupported browser type: ${browserType}`);
|
|
46
|
+
}
|
|
47
|
+
browser = await browserLauncher.launch(launchOptions);
|
|
48
|
+
const context = await browser.newContext();
|
|
49
|
+
page = await context.newPage();
|
|
50
|
+
shared.browser = browser;
|
|
51
|
+
shared.page = page;
|
|
52
|
+
}
|
|
53
|
+
let result = {};
|
|
54
|
+
switch (action) {
|
|
55
|
+
case 'goto':
|
|
56
|
+
if (!url)
|
|
57
|
+
throw new Error('`url` parameter is required for `goto` action.');
|
|
58
|
+
await page.goto(url);
|
|
59
|
+
result = { message: `Navigated to ${url}` };
|
|
60
|
+
break;
|
|
61
|
+
case 'click':
|
|
62
|
+
if (!selector)
|
|
63
|
+
throw new Error('`selector` parameter is required for `click` action.');
|
|
64
|
+
await page.click(selector);
|
|
65
|
+
result = { message: `Clicked on ${selector}` };
|
|
66
|
+
break;
|
|
67
|
+
case 'type':
|
|
68
|
+
if (!selector || text === undefined)
|
|
69
|
+
throw new Error('`selector` and `text` parameters are required for `type` action.');
|
|
70
|
+
await page.fill(selector, text);
|
|
71
|
+
result = { message: `Typed into ${selector}` };
|
|
72
|
+
break;
|
|
73
|
+
case 'screenshot':
|
|
74
|
+
if (!path)
|
|
75
|
+
throw new Error('`path` parameter is required for `screenshot` action.');
|
|
76
|
+
await page.screenshot({ path });
|
|
77
|
+
result = { message: `Screenshot saved to ${path}` };
|
|
78
|
+
break;
|
|
79
|
+
case 'evaluate':
|
|
80
|
+
if (!script)
|
|
81
|
+
throw new Error('`script` parameter is required for `evaluate` action.');
|
|
82
|
+
result = await page.evaluate(script);
|
|
83
|
+
break;
|
|
84
|
+
case 'close':
|
|
85
|
+
if (browser && browser.isConnected()) {
|
|
86
|
+
await browser.close();
|
|
87
|
+
delete shared.browser;
|
|
88
|
+
delete shared.page;
|
|
89
|
+
}
|
|
90
|
+
result = { message: 'Browser closed' };
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(`Unsupported browser action: ${action}`);
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (browser && browser.isConnected() && action !== 'close') {
|
|
99
|
+
await browser.close();
|
|
100
|
+
delete shared.browser;
|
|
101
|
+
delete shared.page;
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`BrowserControlNode failed during '${action}' action: ${error.message}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export class CodeInterpreterNode extends AsyncNode {
|
|
2
|
+
static getToolDefinition(): {
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
parameters: {
|
|
6
|
+
type: string;
|
|
7
|
+
properties: {
|
|
8
|
+
code: {
|
|
9
|
+
type: string;
|
|
10
|
+
description: string;
|
|
11
|
+
};
|
|
12
|
+
timeout: {
|
|
13
|
+
type: string;
|
|
14
|
+
description: string;
|
|
15
|
+
};
|
|
16
|
+
args: {
|
|
17
|
+
type: string;
|
|
18
|
+
items: {
|
|
19
|
+
type: string;
|
|
20
|
+
};
|
|
21
|
+
description: string;
|
|
22
|
+
};
|
|
23
|
+
requireConfirmation: {
|
|
24
|
+
type: string;
|
|
25
|
+
description: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
required: string[];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
execAsync(): Promise<{
|
|
32
|
+
stdout: string;
|
|
33
|
+
stderr: string;
|
|
34
|
+
exitCode: number;
|
|
35
|
+
}>;
|
|
36
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
37
|
+
}
|
|
38
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { AsyncNode, AsyncFlow } from '../qflow.js';
|
|
2
|
+
import { UserInputNode } from './user.js';
|
|
3
|
+
import { promises as fs } from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import { log } from '../logger.js';
|
|
8
|
+
let nodeChildProcess = null;
|
|
9
|
+
if (!process.versions.bun) {
|
|
10
|
+
nodeChildProcess = require('child_process');
|
|
11
|
+
}
|
|
12
|
+
function syntaxHighlight(code) {
|
|
13
|
+
return code
|
|
14
|
+
.replace(/\b(def|class|import|from|return|if|else|elif|for|while|with|try|except|finally|raise|assert|yield|lambda|pass|continue|break)\b/g, chalk.blue.bold('$1'))
|
|
15
|
+
.replace(/("""[\s\S]*?""")/g, chalk.hex('#6272a4').italic('$1'))
|
|
16
|
+
.replace(/('''[\s\S]*?''')/g, chalk.hex('#6272a4').italic('$1'))
|
|
17
|
+
.replace(/(".*?")/g, chalk.hex('#f1fa8c')('$1'))
|
|
18
|
+
.replace(/('.*?')/g, chalk.hex('#f1fa8c')('$1'))
|
|
19
|
+
.replace(/#.*/g, chalk.hex('#6272a4').italic('$&'))
|
|
20
|
+
.replace(/\b(True|False|None)\b/g, chalk.hex('#bd93f9').bold('$1'))
|
|
21
|
+
.replace(/\b(self|cls)\b/g, chalk.hex('#ffb86c').italic('$1'));
|
|
22
|
+
}
|
|
23
|
+
export class CodeInterpreterNode extends AsyncNode {
|
|
24
|
+
static getToolDefinition() {
|
|
25
|
+
return {
|
|
26
|
+
name: "code_interpreter",
|
|
27
|
+
description: "Executes Python code (requires user confirmation).",
|
|
28
|
+
parameters: {
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: {
|
|
31
|
+
code: {
|
|
32
|
+
type: "string",
|
|
33
|
+
description: "The Python code snippet to execute."
|
|
34
|
+
},
|
|
35
|
+
timeout: {
|
|
36
|
+
type: "number",
|
|
37
|
+
description: "Optional. Maximum execution time in milliseconds. Defaults to 30000 (30 seconds)."
|
|
38
|
+
},
|
|
39
|
+
args: {
|
|
40
|
+
type: "array",
|
|
41
|
+
items: { type: "string" },
|
|
42
|
+
description: "Optional. Command-line arguments to pass to the script."
|
|
43
|
+
},
|
|
44
|
+
requireConfirmation: {
|
|
45
|
+
type: "boolean",
|
|
46
|
+
description: "Optional. If true, the user will be prompted for confirmation before executing the code. Defaults to true."
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
required: ["code"]
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async execAsync() {
|
|
54
|
+
const { code, timeout = 30000, args = [], requireConfirmation = true, interpreterPath } = this.params;
|
|
55
|
+
if (!code) {
|
|
56
|
+
throw new Error('CodeInterpreterNode requires a `code` parameter.');
|
|
57
|
+
}
|
|
58
|
+
const tempDir = os.tmpdir();
|
|
59
|
+
const tempFileName = `qflow_python_code_${Date.now()}_${Math.random().toString(36).substring(2, 8)}.py`;
|
|
60
|
+
const tempFilePath = path.join(tempDir, tempFileName);
|
|
61
|
+
const interpreterCommand = interpreterPath || 'python';
|
|
62
|
+
log(`Executing code with interpreter: ${interpreterCommand}`, this.params.logging);
|
|
63
|
+
if (!requireConfirmation)
|
|
64
|
+
log(`Code to execute:\n${code}`, this.params.logging);
|
|
65
|
+
if (requireConfirmation) {
|
|
66
|
+
const confirmNode = new UserInputNode();
|
|
67
|
+
confirmNode.setParams({
|
|
68
|
+
prompt: `The agent wants to execute the following Python code:\n${syntaxHighlight(code)}\nDo you approve? (yes/no): `
|
|
69
|
+
});
|
|
70
|
+
const confirmFlow = new AsyncFlow(confirmNode);
|
|
71
|
+
const confirmation = await confirmFlow.runAsync({});
|
|
72
|
+
if (confirmation.toLowerCase() !== 'yes') {
|
|
73
|
+
throw new Error('Code execution denied by user.');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
await fs.writeFile(tempFilePath, code, 'utf-8');
|
|
78
|
+
let stdout = '';
|
|
79
|
+
let stderr = '';
|
|
80
|
+
let exitCode = 1;
|
|
81
|
+
const abortController = new AbortController();
|
|
82
|
+
const timeoutId = setTimeout(() => abortController.abort(), timeout);
|
|
83
|
+
try {
|
|
84
|
+
if (process.versions.bun) {
|
|
85
|
+
const childProcess = Bun.spawn([interpreterCommand, tempFilePath, ...args], {
|
|
86
|
+
stdin: 'inherit',
|
|
87
|
+
stdout: 'pipe',
|
|
88
|
+
stderr: 'pipe',
|
|
89
|
+
signal: abortController.signal,
|
|
90
|
+
});
|
|
91
|
+
stdout = await new Response(childProcess.stdout).text();
|
|
92
|
+
stderr = await new Response(childProcess.stderr).text();
|
|
93
|
+
const bunExitResult = await childProcess.exited;
|
|
94
|
+
if (abortController.signal.aborted) {
|
|
95
|
+
stderr += `\nExecution timed out after ${timeout}ms.`;
|
|
96
|
+
exitCode = 1;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
exitCode = Number(bunExitResult !== null ? bunExitResult : 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (nodeChildProcess) {
|
|
103
|
+
const childProcess = nodeChildProcess.spawn(interpreterCommand, [tempFilePath, ...args], {
|
|
104
|
+
stdio: ['inherit', 'pipe', 'pipe'],
|
|
105
|
+
signal: abortController.signal,
|
|
106
|
+
});
|
|
107
|
+
const stdoutChunks = [];
|
|
108
|
+
const stderrChunks = [];
|
|
109
|
+
childProcess.stdout.on('data', (data) => stdoutChunks.push(data));
|
|
110
|
+
childProcess.stderr.on('data', (data) => stderrChunks.push(data));
|
|
111
|
+
const { code: exitCodeFromClose, signal: signalFromClose } = await new Promise((resolve, reject) => {
|
|
112
|
+
childProcess.on('close', (code, signal) => resolve({ code, signal }));
|
|
113
|
+
childProcess.on('error', (err) => reject(err));
|
|
114
|
+
});
|
|
115
|
+
stdout = Buffer.concat(stdoutChunks).toString('utf-8');
|
|
116
|
+
stderr = Buffer.concat(stderrChunks).toString('utf-8');
|
|
117
|
+
exitCode = exitCodeFromClose !== null ? exitCodeFromClose : (signalFromClose ? 1 : 1);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new Error('Unsupported runtime environment.');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error.name === "AbortError" || error.code === "ABORT_ERR") {
|
|
125
|
+
stderr += `\nExecution timed out after ${timeout}ms.`;
|
|
126
|
+
exitCode = 1;
|
|
127
|
+
}
|
|
128
|
+
else if (error.code === 'ENOENT') {
|
|
129
|
+
stderr += `\nError: Command '${interpreterCommand}' not found. Please ensure it is installed and in your system's PATH.`;
|
|
130
|
+
exitCode = 1;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
stderr += `\nError during execution: ${error.message}`;
|
|
134
|
+
exitCode = 1;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
clearTimeout(timeoutId);
|
|
139
|
+
}
|
|
140
|
+
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await fs.unlink(tempFilePath).catch(e => log(`Failed to delete temp file ${tempFilePath}:`, this.params.logging, { type: 'error' }));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async postAsync(shared, prepRes, execRes) {
|
|
147
|
+
shared.codeInterpreterResult = execRes;
|
|
148
|
+
return 'default';
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class DataExtractorNode extends AsyncNode {
|
|
2
|
+
static getToolDefinition(): {
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
parameters: {
|
|
6
|
+
type: string;
|
|
7
|
+
properties: {
|
|
8
|
+
input: {
|
|
9
|
+
type: string;
|
|
10
|
+
description: string;
|
|
11
|
+
};
|
|
12
|
+
type: {
|
|
13
|
+
type: string;
|
|
14
|
+
enum: string[];
|
|
15
|
+
description: string;
|
|
16
|
+
};
|
|
17
|
+
selector: {
|
|
18
|
+
type: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
jsonPath: {
|
|
22
|
+
type: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
regex: {
|
|
26
|
+
type: string;
|
|
27
|
+
description: string;
|
|
28
|
+
};
|
|
29
|
+
group: {
|
|
30
|
+
type: string;
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
required: string[];
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
execAsync(): Promise<any>;
|
|
38
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
39
|
+
}
|
|
40
|
+
import { AsyncNode } from '../qflow.js';
|