@fractal-solutions/qflow 1.0.82 → 1.0.83
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/tools.js +2 -0
- package/dist/nodes/browser_control.d.ts +13 -0
- package/dist/nodes/browser_control.js +15 -2
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/navigator.d.ts +164 -0
- package/dist/nodes/navigator.js +585 -0
- package/package.json +1 -1
package/dist/agent/tools.js
CHANGED
|
@@ -38,6 +38,7 @@ import { SummarizeNode } from '../nodes/summarize.js';
|
|
|
38
38
|
import { HttpServerNode } from '../nodes/http_server.js';
|
|
39
39
|
import { InteractiveWebviewNode } from '../nodes/interactive_webview.js';
|
|
40
40
|
import { WebviewNode } from '../nodes/webview.js';
|
|
41
|
+
import { NavigatorNode } from '../nodes/navigator.js';
|
|
41
42
|
import qflowConfig from '../../qflow.config.js'; // Import the config file
|
|
42
43
|
import path from 'path';
|
|
43
44
|
import { fileURLToPath } from 'url';
|
|
@@ -97,6 +98,7 @@ const coreToolClasses = [
|
|
|
97
98
|
HttpServerNode,
|
|
98
99
|
InteractiveWebviewNode,
|
|
99
100
|
WebviewNode,
|
|
101
|
+
NavigatorNode,
|
|
100
102
|
];
|
|
101
103
|
let customToolClasses = [];
|
|
102
104
|
// Dynamically import custom nodes from qflow.config.js
|
|
@@ -26,6 +26,19 @@ export class BrowserControlNode extends AsyncNode {
|
|
|
26
26
|
type: string;
|
|
27
27
|
description: string;
|
|
28
28
|
};
|
|
29
|
+
script: {
|
|
30
|
+
type: string;
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
browserType: {
|
|
34
|
+
type: string;
|
|
35
|
+
enum: string[];
|
|
36
|
+
description: string;
|
|
37
|
+
};
|
|
38
|
+
launchOptions: {
|
|
39
|
+
type: string;
|
|
40
|
+
description: string;
|
|
41
|
+
};
|
|
29
42
|
};
|
|
30
43
|
required: string[];
|
|
31
44
|
};
|
|
@@ -4,13 +4,13 @@ export class BrowserControlNode extends AsyncNode {
|
|
|
4
4
|
static getToolDefinition() {
|
|
5
5
|
return {
|
|
6
6
|
name: "browser_control",
|
|
7
|
-
description: "Controls a
|
|
7
|
+
description: "Controls a Playwright browser to navigate pages, interact with elements, evaluate scripts, take screenshots, and close the session.",
|
|
8
8
|
parameters: {
|
|
9
9
|
type: "object",
|
|
10
10
|
properties: {
|
|
11
11
|
action: {
|
|
12
12
|
type: "string",
|
|
13
|
-
enum: ["goto", "click", "type", "screenshot", "close"],
|
|
13
|
+
enum: ["goto", "click", "type", "screenshot", "evaluate", "close"],
|
|
14
14
|
description: "The browser action to perform."
|
|
15
15
|
},
|
|
16
16
|
url: {
|
|
@@ -28,6 +28,19 @@ export class BrowserControlNode extends AsyncNode {
|
|
|
28
28
|
path: {
|
|
29
29
|
type: "string",
|
|
30
30
|
description: "The file path to save a screenshot (for 'screenshot' action)."
|
|
31
|
+
},
|
|
32
|
+
script: {
|
|
33
|
+
type: "string",
|
|
34
|
+
description: "JavaScript expression or function body to evaluate in the page context (for 'evaluate' action)."
|
|
35
|
+
},
|
|
36
|
+
browserType: {
|
|
37
|
+
type: "string",
|
|
38
|
+
enum: ["chromium", "firefox", "webkit"],
|
|
39
|
+
description: "Browser engine to launch when no browser exists in shared state. Defaults to 'chromium'."
|
|
40
|
+
},
|
|
41
|
+
launchOptions: {
|
|
42
|
+
type: "object",
|
|
43
|
+
description: "Playwright launch options used when a new browser is created (e.g. { headless: true })."
|
|
31
44
|
}
|
|
32
45
|
},
|
|
33
46
|
required: ["action"]
|
package/dist/nodes/index.d.ts
CHANGED
package/dist/nodes/index.js
CHANGED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
export class NavigatorNode 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
|
+
baseUrl: {
|
|
14
|
+
type: string;
|
|
15
|
+
description: string;
|
|
16
|
+
};
|
|
17
|
+
autoStartOrchestrator: {
|
|
18
|
+
type: string;
|
|
19
|
+
description: string;
|
|
20
|
+
};
|
|
21
|
+
orchestratorWaitMs: {
|
|
22
|
+
type: string;
|
|
23
|
+
description: string;
|
|
24
|
+
};
|
|
25
|
+
profileId: {
|
|
26
|
+
type: string;
|
|
27
|
+
description: string;
|
|
28
|
+
};
|
|
29
|
+
profileName: {
|
|
30
|
+
type: string;
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
instanceId: {
|
|
34
|
+
type: string;
|
|
35
|
+
description: string;
|
|
36
|
+
};
|
|
37
|
+
mode: {
|
|
38
|
+
type: string;
|
|
39
|
+
enum: string[];
|
|
40
|
+
description: string;
|
|
41
|
+
};
|
|
42
|
+
port: {
|
|
43
|
+
type: string;
|
|
44
|
+
description: string;
|
|
45
|
+
};
|
|
46
|
+
tabId: {
|
|
47
|
+
type: string;
|
|
48
|
+
description: string;
|
|
49
|
+
};
|
|
50
|
+
url: {
|
|
51
|
+
type: string;
|
|
52
|
+
description: string;
|
|
53
|
+
};
|
|
54
|
+
timeout: {
|
|
55
|
+
type: string;
|
|
56
|
+
description: string;
|
|
57
|
+
};
|
|
58
|
+
blockImages: {
|
|
59
|
+
type: string;
|
|
60
|
+
description: string;
|
|
61
|
+
};
|
|
62
|
+
blockAds: {
|
|
63
|
+
type: string;
|
|
64
|
+
description: string;
|
|
65
|
+
};
|
|
66
|
+
snapshotOptions: {
|
|
67
|
+
type: string;
|
|
68
|
+
description: string;
|
|
69
|
+
};
|
|
70
|
+
raw: {
|
|
71
|
+
type: string;
|
|
72
|
+
description: string;
|
|
73
|
+
};
|
|
74
|
+
expression: {
|
|
75
|
+
type: string;
|
|
76
|
+
description: string;
|
|
77
|
+
};
|
|
78
|
+
awaitExpression: {
|
|
79
|
+
type: string;
|
|
80
|
+
description: string;
|
|
81
|
+
};
|
|
82
|
+
interaction: {
|
|
83
|
+
type: string;
|
|
84
|
+
description: string;
|
|
85
|
+
};
|
|
86
|
+
interactions: {
|
|
87
|
+
type: string;
|
|
88
|
+
items: {
|
|
89
|
+
type: string;
|
|
90
|
+
};
|
|
91
|
+
description: string;
|
|
92
|
+
};
|
|
93
|
+
screenshotOptions: {
|
|
94
|
+
type: string;
|
|
95
|
+
description: string;
|
|
96
|
+
};
|
|
97
|
+
pdfOptions: {
|
|
98
|
+
type: string;
|
|
99
|
+
description: string;
|
|
100
|
+
};
|
|
101
|
+
outputPath: {
|
|
102
|
+
type: string;
|
|
103
|
+
description: string;
|
|
104
|
+
};
|
|
105
|
+
includeBase64: {
|
|
106
|
+
type: string;
|
|
107
|
+
description: string;
|
|
108
|
+
};
|
|
109
|
+
owner: {
|
|
110
|
+
type: string;
|
|
111
|
+
description: string;
|
|
112
|
+
};
|
|
113
|
+
ttl: {
|
|
114
|
+
type: string;
|
|
115
|
+
description: string;
|
|
116
|
+
};
|
|
117
|
+
cookies: {
|
|
118
|
+
type: string;
|
|
119
|
+
items: {
|
|
120
|
+
type: string;
|
|
121
|
+
};
|
|
122
|
+
description: string;
|
|
123
|
+
};
|
|
124
|
+
waitTimeoutMs: {
|
|
125
|
+
type: string;
|
|
126
|
+
description: string;
|
|
127
|
+
};
|
|
128
|
+
pollIntervalMs: {
|
|
129
|
+
type: string;
|
|
130
|
+
description: string;
|
|
131
|
+
};
|
|
132
|
+
pinchtabBinaryPath: {
|
|
133
|
+
type: string;
|
|
134
|
+
description: string;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
required: string[];
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
execAsync(prepRes: any, shared: any): Promise<unknown>;
|
|
141
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
142
|
+
requireParam(key: any): void;
|
|
143
|
+
ensureOrchestrator(baseUrl: any): Promise<{
|
|
144
|
+
status: string;
|
|
145
|
+
message: string;
|
|
146
|
+
instances: unknown;
|
|
147
|
+
}>;
|
|
148
|
+
waitInstanceReady(baseUrl: any, instanceId: any): Promise<{
|
|
149
|
+
ready: boolean;
|
|
150
|
+
waitedMs: number;
|
|
151
|
+
instance: unknown;
|
|
152
|
+
}>;
|
|
153
|
+
resolvePinchTabBinaryPath(): Promise<any>;
|
|
154
|
+
startPinchTabProcess(env: any): Promise<void>;
|
|
155
|
+
requestJson(baseUrl: any, route: any, method: string | undefined, body: any): Promise<unknown>;
|
|
156
|
+
requestText(baseUrl: any, route: any, method?: string): Promise<string>;
|
|
157
|
+
requestBinary(baseUrl: any, route: any, options?: {}): Promise<{
|
|
158
|
+
contentType: string | null;
|
|
159
|
+
sizeBytes: number;
|
|
160
|
+
outputPath: any;
|
|
161
|
+
extension: any;
|
|
162
|
+
}>;
|
|
163
|
+
}
|
|
164
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { log } from '../logger.js';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
import { promises as fs } from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import os from 'os';
|
|
7
|
+
function normalizeBaseUrl(baseUrl = 'http://localhost:9867') {
|
|
8
|
+
return baseUrl.replace(/\/+$/, '');
|
|
9
|
+
}
|
|
10
|
+
function buildQuery(params = {}) {
|
|
11
|
+
const searchParams = new URLSearchParams();
|
|
12
|
+
for (const [key, value] of Object.entries(params)) {
|
|
13
|
+
if (value === undefined || value === null)
|
|
14
|
+
continue;
|
|
15
|
+
if (typeof value === 'boolean') {
|
|
16
|
+
if (value)
|
|
17
|
+
searchParams.append(key, 'true');
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
searchParams.append(key, String(value));
|
|
21
|
+
}
|
|
22
|
+
const query = searchParams.toString();
|
|
23
|
+
return query ? `?${query}` : '';
|
|
24
|
+
}
|
|
25
|
+
async function sleep(ms) {
|
|
26
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
27
|
+
}
|
|
28
|
+
async function fileExists(filePath) {
|
|
29
|
+
try {
|
|
30
|
+
await fs.access(filePath);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class NavigatorNode extends AsyncNode {
|
|
38
|
+
static getToolDefinition() {
|
|
39
|
+
return {
|
|
40
|
+
name: "navigator",
|
|
41
|
+
description: "Controls browser automation through a local PinchTab orchestrator (instances, tabs, snapshots, actions, text extraction, locks, media export, and more).",
|
|
42
|
+
parameters: {
|
|
43
|
+
type: "object",
|
|
44
|
+
properties: {
|
|
45
|
+
action: {
|
|
46
|
+
type: "string",
|
|
47
|
+
enum: [
|
|
48
|
+
"ensure_orchestrator",
|
|
49
|
+
"create_profile",
|
|
50
|
+
"list_profiles",
|
|
51
|
+
"get_profile",
|
|
52
|
+
"delete_profile",
|
|
53
|
+
"start_instance",
|
|
54
|
+
"list_instances",
|
|
55
|
+
"get_instance",
|
|
56
|
+
"wait_instance_ready",
|
|
57
|
+
"stop_instance",
|
|
58
|
+
"get_instance_logs",
|
|
59
|
+
"open_tab",
|
|
60
|
+
"list_tabs",
|
|
61
|
+
"get_tab",
|
|
62
|
+
"close_tab",
|
|
63
|
+
"navigate",
|
|
64
|
+
"snapshot",
|
|
65
|
+
"text",
|
|
66
|
+
"evaluate",
|
|
67
|
+
"action",
|
|
68
|
+
"actions",
|
|
69
|
+
"screenshot",
|
|
70
|
+
"pdf",
|
|
71
|
+
"get_cookies",
|
|
72
|
+
"set_cookies",
|
|
73
|
+
"delete_cookies",
|
|
74
|
+
"lock_tab",
|
|
75
|
+
"unlock_tab",
|
|
76
|
+
"tab_lock_status",
|
|
77
|
+
"rotate_fingerprint",
|
|
78
|
+
"fingerprint_status",
|
|
79
|
+
],
|
|
80
|
+
description: "The PinchTab operation to run."
|
|
81
|
+
},
|
|
82
|
+
baseUrl: {
|
|
83
|
+
type: "string",
|
|
84
|
+
description: "PinchTab orchestrator base URL. Defaults to http://localhost:9867."
|
|
85
|
+
},
|
|
86
|
+
autoStartOrchestrator: {
|
|
87
|
+
type: "boolean",
|
|
88
|
+
description: "If true, auto-starts `pinchtab` when the orchestrator is unreachable."
|
|
89
|
+
},
|
|
90
|
+
orchestratorWaitMs: {
|
|
91
|
+
type: "number",
|
|
92
|
+
description: "How long to wait for orchestrator startup/readiness in milliseconds."
|
|
93
|
+
},
|
|
94
|
+
profileId: {
|
|
95
|
+
type: "string",
|
|
96
|
+
description: "Profile ID for profile/instance operations."
|
|
97
|
+
},
|
|
98
|
+
profileName: {
|
|
99
|
+
type: "string",
|
|
100
|
+
description: "Name used when creating a profile."
|
|
101
|
+
},
|
|
102
|
+
instanceId: {
|
|
103
|
+
type: "string",
|
|
104
|
+
description: "Instance ID for instance/tab-scoped operations."
|
|
105
|
+
},
|
|
106
|
+
mode: {
|
|
107
|
+
type: "string",
|
|
108
|
+
enum: ["headless", "headed"],
|
|
109
|
+
description: "Instance browser mode."
|
|
110
|
+
},
|
|
111
|
+
port: {
|
|
112
|
+
type: "number",
|
|
113
|
+
description: "Optional explicit port for a started instance."
|
|
114
|
+
},
|
|
115
|
+
tabId: {
|
|
116
|
+
type: "string",
|
|
117
|
+
description: "Tab ID for tab operations."
|
|
118
|
+
},
|
|
119
|
+
url: {
|
|
120
|
+
type: "string",
|
|
121
|
+
description: "URL for open_tab or navigate."
|
|
122
|
+
},
|
|
123
|
+
timeout: {
|
|
124
|
+
type: "number",
|
|
125
|
+
description: "Navigation timeout in milliseconds."
|
|
126
|
+
},
|
|
127
|
+
blockImages: {
|
|
128
|
+
type: "boolean",
|
|
129
|
+
description: "Whether to block image loading on navigate."
|
|
130
|
+
},
|
|
131
|
+
blockAds: {
|
|
132
|
+
type: "boolean",
|
|
133
|
+
description: "Whether to block ads on navigate."
|
|
134
|
+
},
|
|
135
|
+
snapshotOptions: {
|
|
136
|
+
type: "object",
|
|
137
|
+
description: "Snapshot options. Supported keys: interactive, compact, depth, maxTokens, filter."
|
|
138
|
+
},
|
|
139
|
+
raw: {
|
|
140
|
+
type: "boolean",
|
|
141
|
+
description: "Text extraction mode. true returns raw/full text."
|
|
142
|
+
},
|
|
143
|
+
expression: {
|
|
144
|
+
type: "string",
|
|
145
|
+
description: "JavaScript expression for evaluate."
|
|
146
|
+
},
|
|
147
|
+
awaitExpression: {
|
|
148
|
+
type: "boolean",
|
|
149
|
+
description: "If true, await the evaluated expression."
|
|
150
|
+
},
|
|
151
|
+
interaction: {
|
|
152
|
+
type: "object",
|
|
153
|
+
description: "Single action payload for `action` (e.g., { kind, ref, text, key })."
|
|
154
|
+
},
|
|
155
|
+
interactions: {
|
|
156
|
+
type: "array",
|
|
157
|
+
items: { type: "object" },
|
|
158
|
+
description: "Action list for `actions` as [{ kind, ... }]."
|
|
159
|
+
},
|
|
160
|
+
screenshotOptions: {
|
|
161
|
+
type: "object",
|
|
162
|
+
description: "Screenshot query options. Supported keys: format, quality."
|
|
163
|
+
},
|
|
164
|
+
pdfOptions: {
|
|
165
|
+
type: "object",
|
|
166
|
+
description: "PDF query options. Supported keys: landscape, margins, scale, pages."
|
|
167
|
+
},
|
|
168
|
+
outputPath: {
|
|
169
|
+
type: "string",
|
|
170
|
+
description: "Optional destination file path for screenshot/pdf."
|
|
171
|
+
},
|
|
172
|
+
includeBase64: {
|
|
173
|
+
type: "boolean",
|
|
174
|
+
description: "If true, includes base64 payload for binary screenshot/pdf responses."
|
|
175
|
+
},
|
|
176
|
+
owner: {
|
|
177
|
+
type: "string",
|
|
178
|
+
description: "Owner identifier for lock/unlock operations."
|
|
179
|
+
},
|
|
180
|
+
ttl: {
|
|
181
|
+
type: "number",
|
|
182
|
+
description: "Lock TTL in seconds."
|
|
183
|
+
},
|
|
184
|
+
cookies: {
|
|
185
|
+
type: "array",
|
|
186
|
+
items: { type: "object" },
|
|
187
|
+
description: "Cookies payload for set/delete cookie actions."
|
|
188
|
+
},
|
|
189
|
+
waitTimeoutMs: {
|
|
190
|
+
type: "number",
|
|
191
|
+
description: "wait_instance_ready total timeout in milliseconds."
|
|
192
|
+
},
|
|
193
|
+
pollIntervalMs: {
|
|
194
|
+
type: "number",
|
|
195
|
+
description: "wait_instance_ready polling interval in milliseconds."
|
|
196
|
+
},
|
|
197
|
+
pinchtabBinaryPath: {
|
|
198
|
+
type: "string",
|
|
199
|
+
description: "Optional full path to PinchTab binary. If omitted, NavigatorNode attempts auto-detection."
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
required: ["action"]
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
async execAsync(prepRes, shared) {
|
|
207
|
+
const baseUrl = normalizeBaseUrl(this.params.baseUrl);
|
|
208
|
+
const action = this.params.action;
|
|
209
|
+
const autoStartOrchestrator = Boolean(this.params.autoStartOrchestrator);
|
|
210
|
+
if (!action) {
|
|
211
|
+
throw new Error('NavigatorNode requires an `action` parameter.');
|
|
212
|
+
}
|
|
213
|
+
if (action === 'ensure_orchestrator') {
|
|
214
|
+
return await this.ensureOrchestrator(baseUrl);
|
|
215
|
+
}
|
|
216
|
+
if (autoStartOrchestrator) {
|
|
217
|
+
await this.ensureOrchestrator(baseUrl);
|
|
218
|
+
}
|
|
219
|
+
switch (action) {
|
|
220
|
+
case 'create_profile':
|
|
221
|
+
this.requireParam('profileName');
|
|
222
|
+
return await this.requestJson(baseUrl, '/profiles', 'POST', { name: this.params.profileName });
|
|
223
|
+
case 'list_profiles':
|
|
224
|
+
return await this.requestJson(baseUrl, '/profiles');
|
|
225
|
+
case 'get_profile':
|
|
226
|
+
this.requireParam('profileId');
|
|
227
|
+
return await this.requestJson(baseUrl, `/profiles/${encodeURIComponent(this.params.profileId)}`);
|
|
228
|
+
case 'delete_profile':
|
|
229
|
+
this.requireParam('profileId');
|
|
230
|
+
return await this.requestJson(baseUrl, `/profiles/${encodeURIComponent(this.params.profileId)}`, 'DELETE');
|
|
231
|
+
case 'start_instance':
|
|
232
|
+
return await this.requestJson(baseUrl, '/instances/start', 'POST', {
|
|
233
|
+
profileId: this.params.profileId,
|
|
234
|
+
mode: this.params.mode,
|
|
235
|
+
port: this.params.port
|
|
236
|
+
});
|
|
237
|
+
case 'list_instances':
|
|
238
|
+
return await this.requestJson(baseUrl, '/instances');
|
|
239
|
+
case 'get_instance':
|
|
240
|
+
this.requireParam('instanceId');
|
|
241
|
+
return await this.requestJson(baseUrl, `/instances/${encodeURIComponent(this.params.instanceId)}`);
|
|
242
|
+
case 'wait_instance_ready':
|
|
243
|
+
this.requireParam('instanceId');
|
|
244
|
+
return await this.waitInstanceReady(baseUrl, this.params.instanceId);
|
|
245
|
+
case 'stop_instance':
|
|
246
|
+
this.requireParam('instanceId');
|
|
247
|
+
return await this.requestJson(baseUrl, `/instances/${encodeURIComponent(this.params.instanceId)}/stop`, 'POST');
|
|
248
|
+
case 'get_instance_logs':
|
|
249
|
+
this.requireParam('instanceId');
|
|
250
|
+
return await this.requestText(baseUrl, `/instances/${encodeURIComponent(this.params.instanceId)}/logs`);
|
|
251
|
+
case 'open_tab':
|
|
252
|
+
this.requireParam('instanceId');
|
|
253
|
+
return await this.requestJson(baseUrl, '/tabs/new', 'POST', {
|
|
254
|
+
instanceId: this.params.instanceId,
|
|
255
|
+
url: this.params.url
|
|
256
|
+
});
|
|
257
|
+
case 'list_tabs': {
|
|
258
|
+
const query = buildQuery({ instanceId: this.params.instanceId });
|
|
259
|
+
return await this.requestJson(baseUrl, `/tabs${query}`);
|
|
260
|
+
}
|
|
261
|
+
case 'get_tab':
|
|
262
|
+
this.requireParam('tabId');
|
|
263
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}`);
|
|
264
|
+
case 'close_tab':
|
|
265
|
+
this.requireParam('tabId');
|
|
266
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/close`, 'POST');
|
|
267
|
+
case 'navigate':
|
|
268
|
+
this.requireParam('tabId');
|
|
269
|
+
this.requireParam('url');
|
|
270
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/navigate`, 'POST', {
|
|
271
|
+
url: this.params.url,
|
|
272
|
+
timeout: this.params.timeout,
|
|
273
|
+
blockImages: this.params.blockImages,
|
|
274
|
+
blockAds: this.params.blockAds
|
|
275
|
+
});
|
|
276
|
+
case 'snapshot': {
|
|
277
|
+
this.requireParam('tabId');
|
|
278
|
+
const options = this.params.snapshotOptions || {};
|
|
279
|
+
const query = buildQuery({
|
|
280
|
+
interactive: options.interactive,
|
|
281
|
+
compact: options.compact,
|
|
282
|
+
depth: options.depth,
|
|
283
|
+
maxTokens: options.maxTokens,
|
|
284
|
+
filter: options.filter
|
|
285
|
+
});
|
|
286
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/snapshot${query}`);
|
|
287
|
+
}
|
|
288
|
+
case 'text': {
|
|
289
|
+
this.requireParam('tabId');
|
|
290
|
+
const query = buildQuery({ raw: this.params.raw });
|
|
291
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/text${query}`);
|
|
292
|
+
}
|
|
293
|
+
case 'evaluate':
|
|
294
|
+
this.requireParam('tabId');
|
|
295
|
+
this.requireParam('expression');
|
|
296
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/evaluate`, 'POST', {
|
|
297
|
+
expression: this.params.expression,
|
|
298
|
+
await: this.params.awaitExpression
|
|
299
|
+
});
|
|
300
|
+
case 'action':
|
|
301
|
+
this.requireParam('tabId');
|
|
302
|
+
this.requireParam('interaction');
|
|
303
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/action`, 'POST', this.params.interaction);
|
|
304
|
+
case 'actions':
|
|
305
|
+
this.requireParam('tabId');
|
|
306
|
+
this.requireParam('interactions');
|
|
307
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/actions`, 'POST', {
|
|
308
|
+
actions: this.params.interactions
|
|
309
|
+
});
|
|
310
|
+
case 'screenshot': {
|
|
311
|
+
this.requireParam('tabId');
|
|
312
|
+
const options = this.params.screenshotOptions || {};
|
|
313
|
+
const query = buildQuery({
|
|
314
|
+
format: options.format,
|
|
315
|
+
quality: options.quality
|
|
316
|
+
});
|
|
317
|
+
return await this.requestBinary(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/screenshot${query}`, {
|
|
318
|
+
defaultFileExtension: options.format === 'jpeg' ? 'jpg' : 'png'
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
case 'pdf': {
|
|
322
|
+
this.requireParam('tabId');
|
|
323
|
+
const options = this.params.pdfOptions || {};
|
|
324
|
+
const query = buildQuery({
|
|
325
|
+
landscape: options.landscape,
|
|
326
|
+
margins: options.margins,
|
|
327
|
+
scale: options.scale,
|
|
328
|
+
pages: options.pages
|
|
329
|
+
});
|
|
330
|
+
return await this.requestBinary(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/pdf${query}`, {
|
|
331
|
+
defaultFileExtension: 'pdf'
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
case 'get_cookies':
|
|
335
|
+
this.requireParam('tabId');
|
|
336
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/cookies`);
|
|
337
|
+
case 'set_cookies':
|
|
338
|
+
this.requireParam('tabId');
|
|
339
|
+
this.requireParam('cookies');
|
|
340
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/cookies`, 'POST', {
|
|
341
|
+
action: 'set',
|
|
342
|
+
cookies: this.params.cookies
|
|
343
|
+
});
|
|
344
|
+
case 'delete_cookies':
|
|
345
|
+
this.requireParam('tabId');
|
|
346
|
+
this.requireParam('cookies');
|
|
347
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/cookies`, 'POST', {
|
|
348
|
+
action: 'delete',
|
|
349
|
+
cookies: this.params.cookies
|
|
350
|
+
});
|
|
351
|
+
case 'lock_tab':
|
|
352
|
+
this.requireParam('tabId');
|
|
353
|
+
this.requireParam('owner');
|
|
354
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/lock`, 'POST', {
|
|
355
|
+
owner: this.params.owner,
|
|
356
|
+
ttl: this.params.ttl ?? 60
|
|
357
|
+
});
|
|
358
|
+
case 'unlock_tab':
|
|
359
|
+
this.requireParam('tabId');
|
|
360
|
+
this.requireParam('owner');
|
|
361
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/unlock`, 'POST', {
|
|
362
|
+
owner: this.params.owner
|
|
363
|
+
});
|
|
364
|
+
case 'tab_lock_status':
|
|
365
|
+
this.requireParam('tabId');
|
|
366
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/locks`);
|
|
367
|
+
case 'rotate_fingerprint':
|
|
368
|
+
this.requireParam('tabId');
|
|
369
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/fingerprint/rotate`, 'POST');
|
|
370
|
+
case 'fingerprint_status':
|
|
371
|
+
this.requireParam('tabId');
|
|
372
|
+
return await this.requestJson(baseUrl, `/tabs/${encodeURIComponent(this.params.tabId)}/fingerprint/status`);
|
|
373
|
+
default:
|
|
374
|
+
throw new Error(`Unsupported navigator action: ${action}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async postAsync(shared, prepRes, execRes) {
|
|
378
|
+
shared.navigatorLastResult = execRes;
|
|
379
|
+
if (this.params.action === 'start_instance' && execRes && execRes.id) {
|
|
380
|
+
shared.navigatorInstanceId = execRes.id;
|
|
381
|
+
}
|
|
382
|
+
if (this.params.action === 'create_profile' && execRes && execRes.id) {
|
|
383
|
+
shared.navigatorProfileId = execRes.id;
|
|
384
|
+
}
|
|
385
|
+
if (this.params.action === 'open_tab' && execRes && execRes.id) {
|
|
386
|
+
shared.navigatorTabId = execRes.id;
|
|
387
|
+
}
|
|
388
|
+
return execRes;
|
|
389
|
+
}
|
|
390
|
+
requireParam(key) {
|
|
391
|
+
if (this.params[key] === undefined || this.params[key] === null || this.params[key] === '') {
|
|
392
|
+
throw new Error(`NavigatorNode requires a \`${key}\` parameter for \`${this.params.action}\`.`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async ensureOrchestrator(baseUrl) {
|
|
396
|
+
const startupWindowMs = Number(this.params.orchestratorWaitMs ?? 12000);
|
|
397
|
+
try {
|
|
398
|
+
const instances = await this.requestJson(baseUrl, '/instances');
|
|
399
|
+
return {
|
|
400
|
+
status: 'running',
|
|
401
|
+
message: 'PinchTab orchestrator is reachable.',
|
|
402
|
+
instances
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
log(`[NavigatorNode] PinchTab orchestrator not reachable at ${baseUrl}: ${error.message}`, this.params.logging);
|
|
407
|
+
}
|
|
408
|
+
if (!this.params.autoStartOrchestrator && this.params.action !== 'ensure_orchestrator') {
|
|
409
|
+
throw new Error(`PinchTab orchestrator is not reachable at ${baseUrl}. Set \`autoStartOrchestrator: true\` to attempt auto-start.`);
|
|
410
|
+
}
|
|
411
|
+
if (!this.params.autoStartOrchestrator && this.params.action === 'ensure_orchestrator') {
|
|
412
|
+
throw new Error(`PinchTab orchestrator is not reachable at ${baseUrl}.`);
|
|
413
|
+
}
|
|
414
|
+
const base = new URL(baseUrl);
|
|
415
|
+
const port = base.port ? Number(base.port) : 9867;
|
|
416
|
+
const env = { ...process.env };
|
|
417
|
+
if (Number.isFinite(port) && port > 0) {
|
|
418
|
+
env.BRIDGE_PORT = String(port);
|
|
419
|
+
}
|
|
420
|
+
const binaryPath = await this.resolvePinchTabBinaryPath();
|
|
421
|
+
if (binaryPath) {
|
|
422
|
+
env.PINCHTAB_BINARY_PATH = binaryPath;
|
|
423
|
+
log(`[NavigatorNode] Using PinchTab binary at ${binaryPath}`, this.params.logging);
|
|
424
|
+
}
|
|
425
|
+
log(`[NavigatorNode] Starting PinchTab orchestrator on ${baseUrl}`, this.params.logging);
|
|
426
|
+
await this.startPinchTabProcess(env);
|
|
427
|
+
const startedAt = Date.now();
|
|
428
|
+
let lastError = null;
|
|
429
|
+
while (Date.now() - startedAt < startupWindowMs) {
|
|
430
|
+
try {
|
|
431
|
+
const instances = await this.requestJson(baseUrl, '/instances');
|
|
432
|
+
return {
|
|
433
|
+
status: 'started',
|
|
434
|
+
message: 'PinchTab orchestrator started successfully.',
|
|
435
|
+
instances
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
catch (error) {
|
|
439
|
+
lastError = error;
|
|
440
|
+
await sleep(300);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
throw new Error(`Failed to start PinchTab orchestrator at ${baseUrl} within ${startupWindowMs}ms: ${lastError?.message || 'unknown error'}`);
|
|
444
|
+
}
|
|
445
|
+
async waitInstanceReady(baseUrl, instanceId) {
|
|
446
|
+
const timeoutMs = Number(this.params.waitTimeoutMs ?? 20000);
|
|
447
|
+
const pollIntervalMs = Number(this.params.pollIntervalMs ?? 500);
|
|
448
|
+
const startedAt = Date.now();
|
|
449
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
450
|
+
const statusData = await this.requestJson(baseUrl, `/instances/${encodeURIComponent(instanceId)}`);
|
|
451
|
+
const state = (statusData?.status || '').toLowerCase();
|
|
452
|
+
if (state === 'running' || state === 'chrome_ready') {
|
|
453
|
+
return {
|
|
454
|
+
ready: true,
|
|
455
|
+
waitedMs: Date.now() - startedAt,
|
|
456
|
+
instance: statusData
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
await sleep(pollIntervalMs);
|
|
460
|
+
}
|
|
461
|
+
const latestStatus = await this.requestJson(baseUrl, `/instances/${encodeURIComponent(instanceId)}`);
|
|
462
|
+
return {
|
|
463
|
+
ready: false,
|
|
464
|
+
waitedMs: timeoutMs,
|
|
465
|
+
instance: latestStatus
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
async resolvePinchTabBinaryPath() {
|
|
469
|
+
if (this.params.pinchtabBinaryPath) {
|
|
470
|
+
return this.params.pinchtabBinaryPath;
|
|
471
|
+
}
|
|
472
|
+
const home = os.homedir();
|
|
473
|
+
if (!home)
|
|
474
|
+
return null;
|
|
475
|
+
const directPath = path.join(home, '.pinchtab', 'bin', 'pinchtab-linux-x64');
|
|
476
|
+
if (await fileExists(directPath)) {
|
|
477
|
+
return directPath;
|
|
478
|
+
}
|
|
479
|
+
const baseDir = path.join(home, '.pinchtab', 'bin');
|
|
480
|
+
let entries = [];
|
|
481
|
+
try {
|
|
482
|
+
entries = await fs.readdir(baseDir, { withFileTypes: true });
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
const versionDirs = entries.filter(entry => entry.isDirectory()).map(entry => entry.name);
|
|
488
|
+
for (const versionDir of versionDirs) {
|
|
489
|
+
const x64Path = path.join(baseDir, versionDir, 'pinchtab-linux-x64');
|
|
490
|
+
if (await fileExists(x64Path))
|
|
491
|
+
return x64Path;
|
|
492
|
+
const amd64Path = path.join(baseDir, versionDir, 'pinchtab-linux-amd64');
|
|
493
|
+
if (await fileExists(amd64Path))
|
|
494
|
+
return amd64Path;
|
|
495
|
+
}
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
async startPinchTabProcess(env) {
|
|
499
|
+
await new Promise((resolve, reject) => {
|
|
500
|
+
const proc = spawn('pinchtab', [], {
|
|
501
|
+
detached: true,
|
|
502
|
+
stdio: 'ignore',
|
|
503
|
+
env
|
|
504
|
+
});
|
|
505
|
+
let settled = false;
|
|
506
|
+
let exited = false;
|
|
507
|
+
let exitCode = null;
|
|
508
|
+
let exitSignal = null;
|
|
509
|
+
proc.once('exit', (code, signal) => {
|
|
510
|
+
exited = true;
|
|
511
|
+
exitCode = code;
|
|
512
|
+
exitSignal = signal;
|
|
513
|
+
});
|
|
514
|
+
proc.once('error', (error) => {
|
|
515
|
+
if (settled)
|
|
516
|
+
return;
|
|
517
|
+
settled = true;
|
|
518
|
+
reject(new Error(`Unable to launch 'pinchtab': ${error.message}`));
|
|
519
|
+
});
|
|
520
|
+
proc.once('spawn', () => {
|
|
521
|
+
setTimeout(() => {
|
|
522
|
+
if (settled)
|
|
523
|
+
return;
|
|
524
|
+
if (exited) {
|
|
525
|
+
settled = true;
|
|
526
|
+
reject(new Error(`'pinchtab' exited immediately (code: ${String(exitCode)}, signal: ${String(exitSignal)}).`));
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
settled = true;
|
|
530
|
+
proc.unref();
|
|
531
|
+
resolve();
|
|
532
|
+
}, 350);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
async requestJson(baseUrl, route, method = 'GET', body) {
|
|
537
|
+
const response = await fetch(`${baseUrl}${route}`, {
|
|
538
|
+
method,
|
|
539
|
+
headers: {
|
|
540
|
+
'Content-Type': 'application/json'
|
|
541
|
+
},
|
|
542
|
+
body: body === undefined ? undefined : JSON.stringify(body)
|
|
543
|
+
});
|
|
544
|
+
if (!response.ok) {
|
|
545
|
+
const errorBody = await response.text();
|
|
546
|
+
throw new Error(`HTTP ${response.status} ${response.statusText} (${method} ${route}) ${errorBody}`);
|
|
547
|
+
}
|
|
548
|
+
if (response.status === 204) {
|
|
549
|
+
return { ok: true };
|
|
550
|
+
}
|
|
551
|
+
return await response.json();
|
|
552
|
+
}
|
|
553
|
+
async requestText(baseUrl, route, method = 'GET') {
|
|
554
|
+
const response = await fetch(`${baseUrl}${route}`, { method });
|
|
555
|
+
if (!response.ok) {
|
|
556
|
+
const errorBody = await response.text();
|
|
557
|
+
throw new Error(`HTTP ${response.status} ${response.statusText} (${method} ${route}) ${errorBody}`);
|
|
558
|
+
}
|
|
559
|
+
return await response.text();
|
|
560
|
+
}
|
|
561
|
+
async requestBinary(baseUrl, route, options = {}) {
|
|
562
|
+
const response = await fetch(`${baseUrl}${route}`);
|
|
563
|
+
if (!response.ok) {
|
|
564
|
+
const errorBody = await response.text();
|
|
565
|
+
throw new Error(`HTTP ${response.status} ${response.statusText} (GET ${route}) ${errorBody}`);
|
|
566
|
+
}
|
|
567
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
568
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
569
|
+
const outputPath = this.params.outputPath;
|
|
570
|
+
if (outputPath) {
|
|
571
|
+
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
|
572
|
+
await fs.writeFile(outputPath, buffer);
|
|
573
|
+
}
|
|
574
|
+
const result = {
|
|
575
|
+
contentType: response.headers.get('content-type') || null,
|
|
576
|
+
sizeBytes: buffer.byteLength,
|
|
577
|
+
outputPath: outputPath || null,
|
|
578
|
+
extension: options.defaultFileExtension || null
|
|
579
|
+
};
|
|
580
|
+
if (this.params.includeBase64) {
|
|
581
|
+
result.base64 = buffer.toString('base64');
|
|
582
|
+
}
|
|
583
|
+
return result;
|
|
584
|
+
}
|
|
585
|
+
}
|
package/package.json
CHANGED