@fractal-solutions/qflow 1.0.19 → 1.0.20
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/dist/agent/agent.d.ts +14 -0
- package/dist/agent/agent.js +133 -0
- package/dist/agent/tools.d.ts +923 -0
- package/dist/agent/tools.js +427 -0
- package/dist/nodes/agent_llm.d.ts +23 -0
- package/dist/nodes/agent_llm.js +220 -0
- package/dist/nodes/code_interpreter.d.ts +9 -0
- package/dist/nodes/code_interpreter.js +115 -0
- package/dist/nodes/data_extractor.d.ts +5 -0
- package/dist/nodes/data_extractor.js +80 -0
- package/dist/nodes/embedding.d.ts +7 -0
- package/dist/nodes/embedding.js +44 -0
- package/dist/nodes/filesystem.d.ts +41 -0
- package/dist/nodes/filesystem.js +87 -0
- package/dist/nodes/http.d.ts +26 -0
- package/dist/nodes/http.js +66 -0
- package/dist/nodes/index.d.ts +15 -0
- package/dist/nodes/index.js +15 -0
- package/dist/nodes/llm.d.ts +6 -2
- package/dist/nodes/llm.js +47 -5
- package/dist/nodes/memory.d.ts +12 -0
- package/dist/nodes/memory.js +48 -0
- package/dist/nodes/search.d.ts +25 -0
- package/dist/nodes/search.js +77 -0
- package/dist/nodes/semantic_memory.d.ts +17 -0
- package/dist/nodes/semantic_memory.js +110 -0
- package/dist/nodes/shell.d.ts +11 -0
- package/dist/nodes/shell.js +29 -0
- package/dist/nodes/summarize.d.ts +12 -0
- package/dist/nodes/summarize.js +30 -0
- package/dist/nodes/transform.d.ts +5 -0
- package/dist/nodes/transform.js +31 -0
- package/dist/nodes/user.d.ts +11 -0
- package/dist/nodes/user.js +30 -0
- package/dist/nodes/webscraper.d.ts +1 -1
- package/dist/nodes/webscraper.js +7 -3
- package/dist/qflow.js +4 -12
- package/package.json +1 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export class MemoryNode extends AsyncNode {
|
|
5
|
+
async execAsync() { const { action, content, query, id, memoryPath = './agent_memories' } = this.params; if (!action) {
|
|
6
|
+
throw new Error('MemoryNode requires an `action` (store or retrieve).');
|
|
7
|
+
} const fullMemoryPath = path.resolve(memoryPath); switch (action) {
|
|
8
|
+
case 'store':
|
|
9
|
+
if (!content) {
|
|
10
|
+
throw new Error('Store action requires `content`.');
|
|
11
|
+
}
|
|
12
|
+
const memoryId = id || `mem_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
13
|
+
const filePath = path.join(fullMemoryPath, `${memoryId}.txt`);
|
|
14
|
+
await fs.mkdir(fullMemoryPath, { recursive: true });
|
|
15
|
+
await fs.writeFile(filePath, content, 'utf-8');
|
|
16
|
+
console.log(`[MemoryNode] Stored memory: ${memoryId}`);
|
|
17
|
+
return { status: 'stored', id: memoryId, filePath };
|
|
18
|
+
case 'retrieve':
|
|
19
|
+
if (!query) {
|
|
20
|
+
throw new Error('Retrieve action requires a `query`.');
|
|
21
|
+
}
|
|
22
|
+
console.log(`[MemoryNode] Retrieving memories for query: "${query}"...`);
|
|
23
|
+
const retrievedMemories = [];
|
|
24
|
+
try {
|
|
25
|
+
const files = await fs.readdir(fullMemoryPath);
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
if (file.endsWith('.txt')) {
|
|
28
|
+
const filePath = path.join(fullMemoryPath, file);
|
|
29
|
+
const fileContent = await fs.readFile(filePath, 'utf-8');
|
|
30
|
+
if (fileContent.toLowerCase().includes(query.toLowerCase())) {
|
|
31
|
+
retrievedMemories.push({ id: file.replace('.txt', ''), content: fileContent });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code === 'ENOENT') {
|
|
38
|
+
console.log(`[MemoryNode] Memory path ${fullMemoryPath} does not exist. No memories to retrieve.`);
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
console.log(`[MemoryNode] Retrieved ${retrievedMemories.length} memories.`);
|
|
44
|
+
return retrievedMemories;
|
|
45
|
+
default: throw new Error(`Unsupported action: ${action}`);
|
|
46
|
+
} }
|
|
47
|
+
async postAsync(shared, prepRes, execRes) { shared.memoryResult = execRes; return 'default'; }
|
|
48
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a web search using DuckDuckGo's HTML-only interface.
|
|
3
|
+
* This node does not require an API key and is designed for reliability without heavy dependencies.
|
|
4
|
+
* @param {object} params - The parameters for the node.
|
|
5
|
+
* @param {string} params.query - The search query.
|
|
6
|
+
* @returns {Promise<object[]>} A promise that resolves to an array of search result objects.
|
|
7
|
+
*/
|
|
8
|
+
export class DuckDuckGoSearchNode extends AsyncNode {
|
|
9
|
+
execAsync(): Promise<any[]>;
|
|
10
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Performs a web search using the Google Custom Search JSON API.
|
|
14
|
+
* This node requires a Google API Key and a Custom Search Engine ID.
|
|
15
|
+
* @param {object} params - The parameters for the node.
|
|
16
|
+
* @param {string} params.query - The search query.
|
|
17
|
+
* @param {string} params.apiKey - Your Google API Key.
|
|
18
|
+
* @param {string} params.cseId - Your Custom Search Engine ID.
|
|
19
|
+
* @returns {Promise<object[]>} A promise that resolves to an array of search result objects.
|
|
20
|
+
*/
|
|
21
|
+
export class GoogleSearchNode extends AsyncNode {
|
|
22
|
+
execAsync(): Promise<any>;
|
|
23
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
24
|
+
}
|
|
25
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import * as cheerio from 'cheerio';
|
|
3
|
+
/**
|
|
4
|
+
* Performs a web search using DuckDuckGo's HTML-only interface.
|
|
5
|
+
* This node does not require an API key and is designed for reliability without heavy dependencies.
|
|
6
|
+
* @param {object} params - The parameters for the node.
|
|
7
|
+
* @param {string} params.query - The search query.
|
|
8
|
+
* @returns {Promise<object[]>} A promise that resolves to an array of search result objects.
|
|
9
|
+
*/
|
|
10
|
+
export class DuckDuckGoSearchNode extends AsyncNode {
|
|
11
|
+
async execAsync() {
|
|
12
|
+
const { query } = this.params;
|
|
13
|
+
if (!query) {
|
|
14
|
+
throw new Error('DuckDuckGoSearchNode requires a `query` parameter.');
|
|
15
|
+
}
|
|
16
|
+
const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
|
|
17
|
+
console.log(`[DuckDuckGo] Searching for: "${query}"`);
|
|
18
|
+
const response = await fetch(url);
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
throw new Error(`DuckDuckGo HTML search failed with status ${response.status}`);
|
|
21
|
+
}
|
|
22
|
+
const html = await response.text();
|
|
23
|
+
const $ = cheerio.load(html);
|
|
24
|
+
const results = [];
|
|
25
|
+
$('.result__body').each((i, el) => {
|
|
26
|
+
const title = $(el).find('.result__title a').text().trim();
|
|
27
|
+
const link = $(el).find('.result__url').attr('href');
|
|
28
|
+
const snippet = $(el).find('.result__snippet').text().trim();
|
|
29
|
+
if (title && link) {
|
|
30
|
+
results.push({
|
|
31
|
+
title,
|
|
32
|
+
link,
|
|
33
|
+
snippet,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return results;
|
|
38
|
+
}
|
|
39
|
+
async postAsync(shared, prepRes, execRes) {
|
|
40
|
+
shared.searchResults = execRes;
|
|
41
|
+
return 'default';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Performs a web search using the Google Custom Search JSON API.
|
|
46
|
+
* This node requires a Google API Key and a Custom Search Engine ID.
|
|
47
|
+
* @param {object} params - The parameters for the node.
|
|
48
|
+
* @param {string} params.query - The search query.
|
|
49
|
+
* @param {string} params.apiKey - Your Google API Key.
|
|
50
|
+
* @param {string} params.cseId - Your Custom Search Engine ID.
|
|
51
|
+
* @returns {Promise<object[]>} A promise that resolves to an array of search result objects.
|
|
52
|
+
*/
|
|
53
|
+
export class GoogleSearchNode extends AsyncNode {
|
|
54
|
+
async execAsync() {
|
|
55
|
+
const { query, apiKey, cseId } = this.params;
|
|
56
|
+
if (!query || !apiKey || !cseId) {
|
|
57
|
+
throw new Error('GoogleSearchNode requires `query`, `apiKey`, and `cseId` parameters.');
|
|
58
|
+
}
|
|
59
|
+
const url = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${cseId}&q=${encodeURIComponent(query)}`;
|
|
60
|
+
console.log(`[Google Search] Searching for: "${query}"`);
|
|
61
|
+
const response = await fetch(url);
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
const errorData = await response.json();
|
|
64
|
+
throw new Error(`Google Search API error: ${response.status} - ${errorData.error.message}`);
|
|
65
|
+
}
|
|
66
|
+
const data = await response.json();
|
|
67
|
+
return (data.items || []).map(item => ({
|
|
68
|
+
title: item.title,
|
|
69
|
+
link: item.link,
|
|
70
|
+
snippet: item.snippet,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
async postAsync(shared, prepRes, execRes) {
|
|
74
|
+
shared.searchResults = execRes;
|
|
75
|
+
return 'default';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class SemanticMemoryNode extends AsyncNode {
|
|
2
|
+
constructor();
|
|
3
|
+
memoryIndex: {};
|
|
4
|
+
indexFilePath: string;
|
|
5
|
+
memoriesDir: string;
|
|
6
|
+
execAsync(): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
content: any;
|
|
9
|
+
metadata: any;
|
|
10
|
+
similarity: number;
|
|
11
|
+
}[] | {
|
|
12
|
+
status: string;
|
|
13
|
+
id: any;
|
|
14
|
+
}>;
|
|
15
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
16
|
+
}
|
|
17
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AsyncNode, AsyncFlow } from '../qflow.js';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { EmbeddingNode } from './embedding.js';
|
|
5
|
+
// Utility function for cosine similarity
|
|
6
|
+
function cosineSimilarity(vecA, vecB) {
|
|
7
|
+
let dotProduct = 0;
|
|
8
|
+
let magnitudeA = 0;
|
|
9
|
+
let magnitudeB = 0;
|
|
10
|
+
for (let i = 0; i < vecA.length; i++) {
|
|
11
|
+
dotProduct += vecA[i] * vecB[i];
|
|
12
|
+
magnitudeA += vecA[i] * vecA[i];
|
|
13
|
+
magnitudeB += vecB[i] * vecB[i];
|
|
14
|
+
}
|
|
15
|
+
magnitudeA = Math.sqrt(magnitudeA);
|
|
16
|
+
magnitudeB = Math.sqrt(magnitudeB);
|
|
17
|
+
if (magnitudeA === 0 || magnitudeB === 0)
|
|
18
|
+
return 0; // Avoid division by zero
|
|
19
|
+
return dotProduct / (magnitudeA * magnitudeB);
|
|
20
|
+
}
|
|
21
|
+
export class SemanticMemoryNode extends AsyncNode {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
this.memoryIndex = {}; // In-memory index: { id: { content: string, embedding: number[], metadata: object } }
|
|
25
|
+
this.indexFilePath = '';
|
|
26
|
+
this.memoriesDir = '';
|
|
27
|
+
}
|
|
28
|
+
async execAsync() {
|
|
29
|
+
const { action, content, query, id, metadata, memoryPath = './semantic_memories', embeddingModel, embeddingBaseUrl } = this.params;
|
|
30
|
+
this.memoriesDir = path.resolve(memoryPath);
|
|
31
|
+
this.indexFilePath = path.join(this.memoriesDir, 'index.json');
|
|
32
|
+
// Ensure memories directory exists
|
|
33
|
+
await fs.mkdir(this.memoriesDir, { recursive: true }).catch(() => { });
|
|
34
|
+
// Load existing index
|
|
35
|
+
try {
|
|
36
|
+
const indexContent = await fs.readFile(this.indexFilePath, 'utf-8');
|
|
37
|
+
this.memoryIndex = JSON.parse(indexContent);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e.code === 'ENOENT') {
|
|
41
|
+
this.memoryIndex = {}; // File doesn't exist, start with empty index
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw new Error(`Failed to load memory index: ${e.message}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
switch (action) {
|
|
48
|
+
case 'store':
|
|
49
|
+
if (!content) {
|
|
50
|
+
throw new Error('Store action requires `content`.');
|
|
51
|
+
}
|
|
52
|
+
const memoryId = id || `sem_mem_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
53
|
+
// Generate embedding for the content
|
|
54
|
+
const embedNodeStore = new EmbeddingNode();
|
|
55
|
+
embedNodeStore.setParams({
|
|
56
|
+
text: content,
|
|
57
|
+
model: embeddingModel,
|
|
58
|
+
baseUrl: embeddingBaseUrl
|
|
59
|
+
});
|
|
60
|
+
const embedFlowStore = new AsyncFlow(embedNodeStore);
|
|
61
|
+
const embedResultStore = await embedFlowStore.runAsync({});
|
|
62
|
+
const embedding = embedResultStore.embedding;
|
|
63
|
+
this.memoryIndex[memoryId] = {
|
|
64
|
+
content: content,
|
|
65
|
+
embedding: embedding,
|
|
66
|
+
metadata: metadata || {},
|
|
67
|
+
};
|
|
68
|
+
// Save updated index
|
|
69
|
+
await fs.writeFile(this.indexFilePath, JSON.stringify(this.memoryIndex, null, 2), 'utf-8');
|
|
70
|
+
console.log(`[SemanticMemory] Stored memory: ${memoryId}`);
|
|
71
|
+
return { status: 'stored', id: memoryId };
|
|
72
|
+
case 'retrieve':
|
|
73
|
+
if (!query) {
|
|
74
|
+
throw new Error('Retrieve action requires a `query`.');
|
|
75
|
+
}
|
|
76
|
+
console.log(`[SemanticMemory] Retrieving memories for query: "${query}"...`);
|
|
77
|
+
if (Object.keys(this.memoryIndex).length === 0) {
|
|
78
|
+
console.log('[SemanticMemory] No memories stored. Returning empty.');
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
// Generate embedding for the query
|
|
82
|
+
const embedNodeRetrieve = new EmbeddingNode();
|
|
83
|
+
embedNodeRetrieve.setParams({
|
|
84
|
+
text: query,
|
|
85
|
+
model: embeddingModel,
|
|
86
|
+
baseUrl: embeddingBaseUrl
|
|
87
|
+
});
|
|
88
|
+
const embedFlowRetrieve = new AsyncFlow(embedNodeRetrieve);
|
|
89
|
+
const embedResultRetrieve = await embedFlowRetrieve.runAsync({});
|
|
90
|
+
const queryEmbedding = embedResultRetrieve.embedding;
|
|
91
|
+
const similarities = [];
|
|
92
|
+
for (const memId in this.memoryIndex) {
|
|
93
|
+
const memory = this.memoryIndex[memId];
|
|
94
|
+
const similarity = cosineSimilarity(queryEmbedding, memory.embedding);
|
|
95
|
+
similarities.push({ id: memId, content: memory.content, metadata: memory.metadata, similarity: similarity });
|
|
96
|
+
}
|
|
97
|
+
// Sort by similarity (descending) and return top results
|
|
98
|
+
similarities.sort((a, b) => b.similarity - a.similarity);
|
|
99
|
+
const topResults = similarities.slice(0, this.params.topK || 5);
|
|
100
|
+
console.log(`[SemanticMemory] Retrieved ${topResults.length} semantic memories.`);
|
|
101
|
+
return topResults;
|
|
102
|
+
default:
|
|
103
|
+
throw new Error(`Unsupported action: ${action}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async postAsync(shared, prepRes, execRes) {
|
|
107
|
+
shared.semanticMemoryResult = execRes;
|
|
108
|
+
return 'default';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executes a shell command as if in a terminal.
|
|
3
|
+
* @param {object} params - The parameters for the node.
|
|
4
|
+
* @param {string} params.command - The command string to execute (e.g., "ls -l" or "echo 'hello' > file.txt").
|
|
5
|
+
* @returns {Promise<object>} A promise that resolves to an object containing the stdout and stderr of the command.
|
|
6
|
+
*/
|
|
7
|
+
export class ShellCommandNode extends AsyncNode {
|
|
8
|
+
execAsync(): Promise<any>;
|
|
9
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
10
|
+
}
|
|
11
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { exec } from 'child_process';
|
|
3
|
+
/**
|
|
4
|
+
* Executes a shell command as if in a terminal.
|
|
5
|
+
* @param {object} params - The parameters for the node.
|
|
6
|
+
* @param {string} params.command - The command string to execute (e.g., "ls -l" or "echo 'hello' > file.txt").
|
|
7
|
+
* @returns {Promise<object>} A promise that resolves to an object containing the stdout and stderr of the command.
|
|
8
|
+
*/
|
|
9
|
+
export class ShellCommandNode extends AsyncNode {
|
|
10
|
+
async execAsync() {
|
|
11
|
+
const { command } = this.params;
|
|
12
|
+
if (!command) {
|
|
13
|
+
throw new Error('Missing required parameter: command');
|
|
14
|
+
}
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
exec(command, (error, stdout, stderr) => {
|
|
17
|
+
if (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
resolve({ stdout, stderr });
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async postAsync(shared, prepRes, execRes) {
|
|
26
|
+
shared.shellResult = execRes;
|
|
27
|
+
return 'default';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Summarizes a given text using an LLM.
|
|
3
|
+
* @param {object} params - The parameters for the node.
|
|
4
|
+
* @param {string} params.text - The text content to summarize.
|
|
5
|
+
* @param {DeepSeekLLMNode} params.llmNode - An instance of an LLM node to perform the summarization.
|
|
6
|
+
* @returns {Promise<string>} A promise that resolves to the summarized text.
|
|
7
|
+
*/
|
|
8
|
+
export class SummarizeNode extends AsyncNode {
|
|
9
|
+
execAsync(): Promise<any>;
|
|
10
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { DeepSeekLLMNode } from './llm.js'; // Assuming DeepSeekLLMNode for summarization
|
|
3
|
+
/**
|
|
4
|
+
* Summarizes a given text using an LLM.
|
|
5
|
+
* @param {object} params - The parameters for the node.
|
|
6
|
+
* @param {string} params.text - The text content to summarize.
|
|
7
|
+
* @param {DeepSeekLLMNode} params.llmNode - An instance of an LLM node to perform the summarization.
|
|
8
|
+
* @returns {Promise<string>} A promise that resolves to the summarized text.
|
|
9
|
+
*/
|
|
10
|
+
export class SummarizeNode extends AsyncNode {
|
|
11
|
+
async execAsync() {
|
|
12
|
+
const { text, llmNode } = this.params;
|
|
13
|
+
if (!text) {
|
|
14
|
+
throw new Error('SummarizeNode requires a `text` parameter.');
|
|
15
|
+
}
|
|
16
|
+
if (!llmNode) {
|
|
17
|
+
throw new Error('SummarizeNode requires an `llmNode` instance for summarization.');
|
|
18
|
+
}
|
|
19
|
+
console.log(`[Summarize] Summarizing text (length: ${text.length})...`);
|
|
20
|
+
const prompt = `Please summarize the following text concisely, focusing on key information relevant to an agent's task. Keep the summary under 500 words:\n\n${text}`;
|
|
21
|
+
llmNode.setParams({ prompt: prompt, keyword: 'summarization' });
|
|
22
|
+
const summary = await llmNode.runAsync({});
|
|
23
|
+
console.log(`[Summarize] Summary generated (length: ${summary.length}).`);
|
|
24
|
+
return summary;
|
|
25
|
+
}
|
|
26
|
+
async postAsync(shared, prepRes, execRes) {
|
|
27
|
+
shared.summary = execRes;
|
|
28
|
+
return 'default';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
export class TransformNode extends AsyncNode {
|
|
3
|
+
async execAsync() {
|
|
4
|
+
const { input, transformFunction } = this.params;
|
|
5
|
+
if (transformFunction === undefined) {
|
|
6
|
+
throw new Error('TransformNode requires a `transformFunction`.');
|
|
7
|
+
}
|
|
8
|
+
let func;
|
|
9
|
+
try {
|
|
10
|
+
// Create a function from the string. This allows dynamic transformations.
|
|
11
|
+
// Using new Function() is powerful but requires careful handling of scope and security.
|
|
12
|
+
// For this context, we assume the user provides safe functions.
|
|
13
|
+
func = new Function('data', `return (${transformFunction})(data);`);
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
throw new Error(`Invalid transformFunction provided: ${e.message}`);
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const result = func(input);
|
|
20
|
+
console.log(`[TransformNode] Transformation complete. Result: ${JSON.stringify(result).substring(0, 100)}...`);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
throw new Error(`Error during transformation execution: ${e.message}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async postAsync(shared, prepRes, execRes) {
|
|
28
|
+
shared.transformedData = execRes;
|
|
29
|
+
return 'default';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompts the user for input and waits for their response.
|
|
3
|
+
* @param {object} params - The parameters for the node.
|
|
4
|
+
* @param {string} params.prompt - The message to display to the user.
|
|
5
|
+
* @returns {Promise<string>} A promise that resolves to the user's input.
|
|
6
|
+
*/
|
|
7
|
+
export class UserInputNode extends AsyncNode {
|
|
8
|
+
execAsync(): Promise<any>;
|
|
9
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
10
|
+
}
|
|
11
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import readline from 'readline';
|
|
3
|
+
/**
|
|
4
|
+
* Prompts the user for input and waits for their response.
|
|
5
|
+
* @param {object} params - The parameters for the node.
|
|
6
|
+
* @param {string} params.prompt - The message to display to the user.
|
|
7
|
+
* @returns {Promise<string>} A promise that resolves to the user's input.
|
|
8
|
+
*/
|
|
9
|
+
export class UserInputNode extends AsyncNode {
|
|
10
|
+
async execAsync() {
|
|
11
|
+
const { prompt } = this.params;
|
|
12
|
+
if (!prompt) {
|
|
13
|
+
throw new Error('Missing required parameter: prompt');
|
|
14
|
+
}
|
|
15
|
+
const rl = readline.createInterface({
|
|
16
|
+
input: process.stdin,
|
|
17
|
+
output: process.stdout
|
|
18
|
+
});
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
rl.question(prompt, (answer) => {
|
|
21
|
+
rl.close();
|
|
22
|
+
resolve(answer);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async postAsync(shared, prepRes, execRes) {
|
|
27
|
+
shared.userInput = execRes;
|
|
28
|
+
return 'default';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -6,6 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export class ScrapeURLNode extends AsyncNode {
|
|
8
8
|
execAsync(): Promise<string>;
|
|
9
|
-
postAsync(shared: any, prepRes: any, execRes: any): Promise<
|
|
9
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
10
10
|
}
|
|
11
11
|
import { AsyncNode } from '../qflow.js';
|
package/dist/nodes/webscraper.js
CHANGED
|
@@ -21,8 +21,12 @@ export class ScrapeURLNode extends AsyncNode {
|
|
|
21
21
|
return html;
|
|
22
22
|
}
|
|
23
23
|
async postAsync(shared, prepRes, execRes) {
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
// Truncate the scraped HTML to avoid exceeding LLM context windows
|
|
25
|
+
const MAX_HTML_LENGTH = 4000;
|
|
26
|
+
shared.webScrapedContent = execRes.substring(0, MAX_HTML_LENGTH);
|
|
27
|
+
if (execRes.length > MAX_HTML_LENGTH) {
|
|
28
|
+
console.warn(`[WebScraper] Truncated HTML content from ${execRes.length} to ${MAX_HTML_LENGTH} characters.`);
|
|
29
|
+
}
|
|
30
|
+
return shared.webScrapedContent;
|
|
27
31
|
}
|
|
28
32
|
}
|
package/dist/qflow.js
CHANGED
|
@@ -4,7 +4,7 @@ class BaseNode {
|
|
|
4
4
|
this.successors = {};
|
|
5
5
|
}
|
|
6
6
|
setParams(params) {
|
|
7
|
-
this.params = params;
|
|
7
|
+
this.params = { ...this.params, ...params };
|
|
8
8
|
}
|
|
9
9
|
next(node, action = "default") {
|
|
10
10
|
if (this.successors[action]) {
|
|
@@ -179,19 +179,11 @@ class AsyncFlow extends Flow {
|
|
|
179
179
|
let lastAction = null;
|
|
180
180
|
let lastExecRes = null;
|
|
181
181
|
let p = { ...this.params, ...params };
|
|
182
|
-
let currentPrepRes = undefined; // This will hold the prepRes for the current node
|
|
183
182
|
while (curr) {
|
|
184
183
|
curr.setParams(p);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
currentPrepRes = lastExecRes;
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
currentPrepRes = curr instanceof AsyncNode ? await curr.prepAsync(shared) : curr.prep(shared);
|
|
192
|
-
}
|
|
193
|
-
const execRes = curr instanceof AsyncNode ? await curr._exec(currentPrepRes, shared) : curr._exec(currentPrepRes, shared);
|
|
194
|
-
lastAction = curr instanceof AsyncNode ? await curr.postAsync(shared, currentPrepRes, execRes) : curr.post(shared, currentPrepRes, execRes);
|
|
184
|
+
const prepRes = curr instanceof AsyncNode ? await curr.prepAsync(shared) : curr.prep(shared);
|
|
185
|
+
const execRes = curr instanceof AsyncNode ? await curr._exec(prepRes, shared) : curr._exec(prepRes, shared);
|
|
186
|
+
lastAction = curr instanceof AsyncNode ? await curr.postAsync(shared, prepRes, execRes) : curr.post(shared, prepRes, execRes);
|
|
195
187
|
lastExecRes = execRes; // Store execRes for the next iteration
|
|
196
188
|
curr = this.getNextNode(curr, lastAction);
|
|
197
189
|
}
|
package/package.json
CHANGED