n8n-nodes-smart-browser-automation 1.6.17 → 1.6.19
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.
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type INodeType, type INodeTypeDescription, type ISupplyDataFunctions, type SupplyData } from 'n8n-workflow';
|
|
2
2
|
export declare class SmartBrowserAutomationTools implements INodeType {
|
|
3
3
|
description: INodeTypeDescription;
|
|
4
|
-
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
|
|
5
4
|
supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData>;
|
|
6
5
|
}
|
|
@@ -55,6 +55,26 @@ async function importToolkitBase() {
|
|
|
55
55
|
// n8n uses Toolkit from @langchain/classic/agents internally.
|
|
56
56
|
return await Promise.resolve().then(() => __importStar(require('@langchain/classic/agents')));
|
|
57
57
|
}
|
|
58
|
+
function formatMcpToolResult(result) {
|
|
59
|
+
if (result === null || result === undefined)
|
|
60
|
+
return '';
|
|
61
|
+
if (typeof result === 'string')
|
|
62
|
+
return result;
|
|
63
|
+
const content = result?.content;
|
|
64
|
+
if (Array.isArray(content)) {
|
|
65
|
+
const texts = content
|
|
66
|
+
.map((c) => (c?.type === 'text' ? String(c?.text ?? '') : ''))
|
|
67
|
+
.filter((t) => t.length > 0);
|
|
68
|
+
if (texts.length)
|
|
69
|
+
return texts.join('\n');
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
return JSON.stringify(result);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return String(result);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
58
78
|
class SmartBrowserAutomationTools {
|
|
59
79
|
description = {
|
|
60
80
|
displayName: 'Smart Browser Automation Tools',
|
|
@@ -66,8 +86,8 @@ class SmartBrowserAutomationTools {
|
|
|
66
86
|
name: 'Browser Automation Tools',
|
|
67
87
|
},
|
|
68
88
|
inputs: [],
|
|
69
|
-
outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool
|
|
70
|
-
outputNames: ['Tools'
|
|
89
|
+
outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool],
|
|
90
|
+
outputNames: ['Tools'],
|
|
71
91
|
icon: 'file:smartBrowserAutomation.svg',
|
|
72
92
|
credentials: [
|
|
73
93
|
{
|
|
@@ -86,44 +106,6 @@ class SmartBrowserAutomationTools {
|
|
|
86
106
|
},
|
|
87
107
|
],
|
|
88
108
|
};
|
|
89
|
-
async execute() {
|
|
90
|
-
const node = this.getNode();
|
|
91
|
-
const sessionManager = BrowserSessionManager_1.default.getInstance();
|
|
92
|
-
const itemIndex = 0;
|
|
93
|
-
try {
|
|
94
|
-
const credentials = await this.getCredentials('smartBrowserAutomationApi');
|
|
95
|
-
const cdpOverride = this.getNodeParameter('cdpOverride', itemIndex, '');
|
|
96
|
-
const useCDP = credentials.browserMode === 'cdp';
|
|
97
|
-
const cdpUrl = (cdpOverride || credentials.cdpEndpoint || '').trim();
|
|
98
|
-
await sessionManager.initialize(credentials.mcpEndpoint, useCDP, useCDP ? cdpUrl : undefined);
|
|
99
|
-
const mcpTools = await sessionManager.listTools();
|
|
100
|
-
const debugJson = {
|
|
101
|
-
mcpEndpoint: credentials.mcpEndpoint,
|
|
102
|
-
browserMode: credentials.browserMode,
|
|
103
|
-
useCDP,
|
|
104
|
-
cdpEndpoint: useCDP ? cdpUrl : undefined,
|
|
105
|
-
toolCount: mcpTools.length,
|
|
106
|
-
tools: mcpTools.map((t) => ({
|
|
107
|
-
name: t.name,
|
|
108
|
-
description: t.description ?? '',
|
|
109
|
-
inputSchema: t.inputSchema ?? undefined,
|
|
110
|
-
})),
|
|
111
|
-
};
|
|
112
|
-
return [[], [{ json: debugJson }]];
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
const err = error;
|
|
116
|
-
const debugJson = {
|
|
117
|
-
error: true,
|
|
118
|
-
message: err?.message ? String(err.message) : String(err),
|
|
119
|
-
code: err?.code ? String(err.code) : undefined,
|
|
120
|
-
};
|
|
121
|
-
throw new n8n_workflow_1.NodeOperationError(node, debugJson.message, { itemIndex, description: JSON.stringify(debugJson) });
|
|
122
|
-
}
|
|
123
|
-
finally {
|
|
124
|
-
await sessionManager.close();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
109
|
async supplyData(itemIndex) {
|
|
128
110
|
const node = this.getNode();
|
|
129
111
|
const credentials = await this.getCredentials('smartBrowserAutomationApi');
|
|
@@ -151,14 +133,37 @@ class SmartBrowserAutomationTools {
|
|
|
151
133
|
}
|
|
152
134
|
const { DynamicStructuredTool } = await importDynamicStructuredTool();
|
|
153
135
|
const { Toolkit } = await importToolkitBase();
|
|
136
|
+
const ctx = this;
|
|
154
137
|
const tools = await Promise.all(mcpTools.map(async (tool) => {
|
|
155
138
|
const schema = await (0, jsonSchemaToZod_1.jsonSchemaToZod)(tool.inputSchema);
|
|
139
|
+
const toolName = tool.name;
|
|
140
|
+
const toolDescription = (tool.description ?? '');
|
|
156
141
|
return new DynamicStructuredTool({
|
|
157
|
-
name:
|
|
158
|
-
description:
|
|
142
|
+
name: toolName,
|
|
143
|
+
description: toolDescription,
|
|
159
144
|
schema,
|
|
160
145
|
func: async (args) => {
|
|
161
|
-
|
|
146
|
+
// Make tool calls visible in n8n execution UI
|
|
147
|
+
const input = {
|
|
148
|
+
tool: toolName,
|
|
149
|
+
arguments: args,
|
|
150
|
+
};
|
|
151
|
+
let runIndex = 0;
|
|
152
|
+
try {
|
|
153
|
+
runIndex = ctx.getNextRunIndex?.() ?? 0;
|
|
154
|
+
}
|
|
155
|
+
catch { }
|
|
156
|
+
const { index } = ctx.addInputData(n8n_workflow_1.NodeConnectionTypes.AiTool, [[{ json: input }]], runIndex);
|
|
157
|
+
try {
|
|
158
|
+
const result = await sessionManager.callTool(toolName, args);
|
|
159
|
+
const output = { tool: toolName, result };
|
|
160
|
+
ctx.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, index, [[{ json: output }]]);
|
|
161
|
+
return formatMcpToolResult(result);
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
ctx.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, index, e);
|
|
165
|
+
throw e;
|
|
166
|
+
}
|
|
162
167
|
},
|
|
163
168
|
metadata: { isFromToolkit: true },
|
|
164
169
|
});
|
|
@@ -175,7 +180,10 @@ class SmartBrowserAutomationTools {
|
|
|
175
180
|
}
|
|
176
181
|
return {
|
|
177
182
|
response: new SmartBrowserAutomationToolkit(tools),
|
|
178
|
-
closeFunction: async () =>
|
|
183
|
+
closeFunction: async () => {
|
|
184
|
+
// Called by n8n when Agent execution finishes
|
|
185
|
+
await sessionManager.close();
|
|
186
|
+
},
|
|
179
187
|
};
|
|
180
188
|
}
|
|
181
189
|
}
|