ft-scout 9.5.4 → 9.5.6
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 +1 -1
- package/bin/src/commands/agent.d.ts +5 -0
- package/bin/src/commands/agent.js +45 -3
- package/bin/src/commands/support.d.ts +9 -0
- package/bin/src/commands/support.js +227 -0
- package/bin/src/engine/agent/computerUseLoop.d.ts +107 -0
- package/bin/src/engine/agent/computerUseLoop.js +413 -0
- package/bin/src/engine/agent/index.d.ts +2 -0
- package/bin/src/engine/agent/index.js +2 -0
- package/bin/src/engine/agent/recovery/index.d.ts +4 -0
- package/bin/src/engine/agent/recovery/index.js +4 -0
- package/bin/src/engine/agent/recovery/loopDetector.d.ts +26 -0
- package/bin/src/engine/agent/recovery/loopDetector.js +126 -0
- package/bin/src/engine/agent/recovery/recoveryManager.d.ts +43 -0
- package/bin/src/engine/agent/recovery/recoveryManager.js +271 -0
- package/bin/src/engine/agent/recovery/types.d.ts +48 -0
- package/bin/src/engine/agent/recovery/types.js +4 -0
- package/bin/src/engine/agent/verification/index.d.ts +3 -0
- package/bin/src/engine/agent/verification/index.js +3 -0
- package/bin/src/engine/agent/verification/stateChangeVerifier.d.ts +22 -0
- package/bin/src/engine/agent/verification/stateChangeVerifier.js +282 -0
- package/bin/src/engine/agent/verification/types.d.ts +28 -0
- package/bin/src/engine/agent/verification/types.js +4 -0
- package/bin/src/engine/agentEngine.d.ts +2 -1
- package/bin/src/engine/agentEngine.js +98 -7
- package/bin/src/engine/agentState.d.ts +41 -1
- package/bin/src/engine/agentState.js +45 -0
- package/bin/src/engine/browser/actions/actionEngine.d.ts +41 -0
- package/bin/src/engine/browser/actions/actionEngine.js +707 -0
- package/bin/src/engine/browser/actions/index.d.ts +4 -0
- package/bin/src/engine/browser/actions/index.js +4 -0
- package/bin/src/engine/browser/actions/reliability.d.ts +41 -0
- package/bin/src/engine/browser/actions/reliability.js +170 -0
- package/bin/src/engine/browser/actions/types.d.ts +50 -0
- package/bin/src/engine/browser/actions/types.js +2 -0
- package/bin/src/engine/browser/browserController.d.ts +18 -0
- package/bin/src/engine/browser/browserController.js +44 -12
- package/bin/src/engine/browser/index.d.ts +2 -0
- package/bin/src/engine/browser/index.js +2 -0
- package/bin/src/engine/browser/observer/accessibilityTree.d.ts +14 -0
- package/bin/src/engine/browser/observer/accessibilityTree.js +214 -0
- package/bin/src/engine/browser/observer/browserObserver.d.ts +29 -0
- package/bin/src/engine/browser/observer/browserObserver.js +204 -0
- package/bin/src/engine/browser/observer/domExtractor.d.ts +30 -0
- package/bin/src/engine/browser/observer/domExtractor.js +407 -0
- package/bin/src/engine/browser/observer/index.d.ts +5 -0
- package/bin/src/engine/browser/observer/index.js +5 -0
- package/bin/src/engine/browser/observer/types.d.ts +163 -0
- package/bin/src/engine/browser/observer/types.js +8 -0
- package/bin/src/engine/computer/computerController.d.ts +49 -0
- package/bin/src/engine/computer/computerController.js +249 -104
- package/bin/src/engine/computer/mockComputer.d.ts +6 -1
- package/bin/src/engine/computer/mockComputer.js +58 -0
- package/bin/src/engine/computer/scoutDriver.cs +84 -2
- package/bin/src/engine/llm.js +16 -0
- package/bin/src/engine/models/harpProvider.d.ts +32 -0
- package/bin/src/engine/models/harpProvider.js +124 -0
- package/bin/src/engine/models/index.d.ts +6 -0
- package/bin/src/engine/models/index.js +6 -0
- package/bin/src/engine/models/localModelProvider.d.ts +11 -0
- package/bin/src/engine/models/localModelProvider.js +48 -0
- package/bin/src/engine/models/openaiProvider.d.ts +11 -0
- package/bin/src/engine/models/openaiProvider.js +50 -0
- package/bin/src/engine/models/reasoningOrchestrator.d.ts +17 -0
- package/bin/src/engine/models/reasoningOrchestrator.js +75 -0
- package/bin/src/engine/models/types.d.ts +63 -0
- package/bin/src/engine/models/types.js +2 -0
- package/bin/src/engine/planner.js +64 -34
- package/bin/src/engine/production/index.d.ts +3 -0
- package/bin/src/engine/production/index.js +3 -0
- package/bin/src/engine/production/taskReplay.d.ts +37 -0
- package/bin/src/engine/production/taskReplay.js +81 -0
- package/bin/src/engine/production/telemetry.d.ts +43 -0
- package/bin/src/engine/production/telemetry.js +68 -0
- package/bin/src/engine/runtime/agentRuntime.d.ts +1 -1
- package/bin/src/engine/runtime/agentRuntime.js +99 -0
- package/bin/src/engine/safety/index.d.ts +3 -0
- package/bin/src/engine/safety/index.js +3 -0
- package/bin/src/engine/safety/policyEngine.d.ts +40 -0
- package/bin/src/engine/safety/policyEngine.js +282 -0
- package/bin/src/engine/safety/types.d.ts +24 -0
- package/bin/src/engine/safety/types.js +2 -0
- package/bin/src/engine/safety.d.ts +1 -0
- package/bin/src/engine/safety.js +1 -0
- package/bin/src/engine/subagents/subagentManager.js +2 -2
- package/bin/src/engine/toolRegistry.d.ts +4 -0
- package/bin/src/engine/toolRegistry.js +9 -0
- package/bin/src/engine/tools/domainHandlers.d.ts +61 -0
- package/bin/src/engine/tools/domainHandlers.js +458 -0
- package/bin/src/engine/tools/index.d.ts +4 -0
- package/bin/src/engine/tools/index.js +4 -0
- package/bin/src/engine/tools/types.d.ts +56 -0
- package/bin/src/engine/tools/types.js +2 -0
- package/bin/src/engine/tools/unifiedRouter.d.ts +29 -0
- package/bin/src/engine/tools/unifiedRouter.js +426 -0
- package/bin/src/index.js +26 -6
- package/bin/src/mcp/client.js +1 -1
- package/bin/src/server/server.js +136 -0
- package/bin/src/utils/branding.js +1 -1
- package/bin/src/utils/qrBridge.js +1 -1
- package/bin/src/utils/session.d.ts +5 -1
- package/bin/src/utils/session.js +36 -3
- package/bin/src/utils/support.d.ts +63 -0
- package/bin/src/utils/support.js +182 -0
- package/package.json +2 -2
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import {} from './types.js';
|
|
4
|
+
import { BrowserObserver } from '../observer/browserObserver.js';
|
|
5
|
+
import {} from '../observer/types.js';
|
|
6
|
+
import { resolveTargetElement, recoverStaleElement, detectNavigationAndPopups, withRetry, waitForCondition, normalizeActionTarget, DEFAULT_RETRY_POLICY, } from './reliability.js';
|
|
7
|
+
export class BrowserActionEngine {
|
|
8
|
+
tabs = [];
|
|
9
|
+
activeTabId = '';
|
|
10
|
+
observer;
|
|
11
|
+
workspaceDir;
|
|
12
|
+
retryPolicy;
|
|
13
|
+
constructor(workspaceDir = process.cwd(), retryPolicy = {}) {
|
|
14
|
+
this.workspaceDir = workspaceDir;
|
|
15
|
+
this.observer = new BrowserObserver();
|
|
16
|
+
this.retryPolicy = { ...DEFAULT_RETRY_POLICY, ...retryPolicy };
|
|
17
|
+
const initialTab = {
|
|
18
|
+
id: 'tab-1',
|
|
19
|
+
url: 'about:blank',
|
|
20
|
+
title: 'New Tab',
|
|
21
|
+
html: '<html><head><title>New Tab</title></head><body></body></html>',
|
|
22
|
+
history: { past: [], future: [] },
|
|
23
|
+
};
|
|
24
|
+
this.tabs.push(initialTab);
|
|
25
|
+
this.activeTabId = initialTab.id;
|
|
26
|
+
}
|
|
27
|
+
getObserver() {
|
|
28
|
+
return this.observer;
|
|
29
|
+
}
|
|
30
|
+
getActiveTab() {
|
|
31
|
+
return this.tabs.find(t => t.id === this.activeTabId) || this.tabs[0] || null;
|
|
32
|
+
}
|
|
33
|
+
getTabs() {
|
|
34
|
+
return [...this.tabs];
|
|
35
|
+
}
|
|
36
|
+
async captureCurrentState() {
|
|
37
|
+
const tab = this.getActiveTab();
|
|
38
|
+
return this.observer.observe({
|
|
39
|
+
html: tab?.html || '',
|
|
40
|
+
url: tab?.url || 'about:blank',
|
|
41
|
+
title: tab?.title || '',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async navigate(url, htmlContent) {
|
|
45
|
+
const startTime = Date.now();
|
|
46
|
+
const beforeState = await this.captureCurrentState();
|
|
47
|
+
let cleanUrl = url.trim();
|
|
48
|
+
if (!cleanUrl.startsWith('http://') && !cleanUrl.startsWith('https://') && !cleanUrl.startsWith('about:')) {
|
|
49
|
+
cleanUrl = `https://${cleanUrl}`;
|
|
50
|
+
}
|
|
51
|
+
const tab = this.getActiveTab();
|
|
52
|
+
if (!tab) {
|
|
53
|
+
return {
|
|
54
|
+
success: false,
|
|
55
|
+
action: 'navigate',
|
|
56
|
+
target: cleanUrl,
|
|
57
|
+
beforeState,
|
|
58
|
+
error: 'No active tab available.',
|
|
59
|
+
durationMs: Date.now() - startTime,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (tab.url !== 'about:blank' && tab.url !== cleanUrl) {
|
|
63
|
+
tab.history.past.push(tab.url);
|
|
64
|
+
tab.history.future = [];
|
|
65
|
+
}
|
|
66
|
+
tab.url = cleanUrl;
|
|
67
|
+
// If direct HTML content provided (e.g. for deterministic testing or mock simulation)
|
|
68
|
+
if (htmlContent) {
|
|
69
|
+
tab.html = htmlContent;
|
|
70
|
+
const titleMatch = htmlContent.match(/<title\b[^>]*>([\s\S]*?)<\/title>/i);
|
|
71
|
+
tab.title = titleMatch && titleMatch[1] ? titleMatch[1].trim() : cleanUrl;
|
|
72
|
+
}
|
|
73
|
+
else if (cleanUrl.startsWith('http')) {
|
|
74
|
+
try {
|
|
75
|
+
const res = await fetch(cleanUrl, {
|
|
76
|
+
headers: { 'User-Agent': 'Scout-ActionEngine/9.5.6' },
|
|
77
|
+
signal: AbortSignal.timeout(15000),
|
|
78
|
+
});
|
|
79
|
+
if (res.ok) {
|
|
80
|
+
tab.html = await res.text();
|
|
81
|
+
const titleMatch = tab.html.match(/<title\b[^>]*>([\s\S]*?)<\/title>/i);
|
|
82
|
+
tab.title = titleMatch && titleMatch[1] ? titleMatch[1].trim() : cleanUrl;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
tab.html = `<html><head><title>Error ${res.status}</title></head><body><h1>${res.status} ${res.statusText}</h1></body></html>`;
|
|
86
|
+
tab.title = `Error ${res.status}`;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
tab.html = `<html><head><title>Load Error</title></head><body><p>${err.message}</p></body></html>`;
|
|
91
|
+
tab.title = 'Load Error';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const afterState = await this.captureCurrentState();
|
|
95
|
+
const { navigationDetected } = detectNavigationAndPopups(beforeState, afterState);
|
|
96
|
+
return {
|
|
97
|
+
success: true,
|
|
98
|
+
action: 'navigate',
|
|
99
|
+
target: cleanUrl,
|
|
100
|
+
beforeState,
|
|
101
|
+
afterState,
|
|
102
|
+
durationMs: Date.now() - startTime,
|
|
103
|
+
metadata: {
|
|
104
|
+
statusCode: 200,
|
|
105
|
+
navigationDetected,
|
|
106
|
+
title: tab.title,
|
|
107
|
+
url: tab.url,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async click(target) {
|
|
112
|
+
const startTime = Date.now();
|
|
113
|
+
const beforeState = await this.captureCurrentState();
|
|
114
|
+
const tab = this.getActiveTab();
|
|
115
|
+
if (!tab) {
|
|
116
|
+
return {
|
|
117
|
+
success: false,
|
|
118
|
+
action: 'click',
|
|
119
|
+
target,
|
|
120
|
+
beforeState,
|
|
121
|
+
error: 'No active tab.',
|
|
122
|
+
durationMs: Date.now() - startTime,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// Re-resolve element using prioritized strategy
|
|
126
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
127
|
+
let targetEl = resolved.element;
|
|
128
|
+
if (!targetEl) {
|
|
129
|
+
// Attempt stale-element recovery with fresh observation
|
|
130
|
+
const freshState = await this.captureCurrentState();
|
|
131
|
+
targetEl = recoverStaleElement(target, null, freshState.elements);
|
|
132
|
+
}
|
|
133
|
+
if (!targetEl) {
|
|
134
|
+
return {
|
|
135
|
+
success: false,
|
|
136
|
+
action: 'click',
|
|
137
|
+
target,
|
|
138
|
+
beforeState,
|
|
139
|
+
error: `Element not found matching target: "${JSON.stringify(target)}".`,
|
|
140
|
+
durationMs: Date.now() - startTime,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
let mutatedHtml = tab.html;
|
|
144
|
+
// 1. Link navigation
|
|
145
|
+
if (targetEl.attributes['href']) {
|
|
146
|
+
let nextUrl = targetEl.attributes['href'];
|
|
147
|
+
try {
|
|
148
|
+
nextUrl = new URL(nextUrl, tab.url).toString();
|
|
149
|
+
}
|
|
150
|
+
catch { }
|
|
151
|
+
// If it's an internal hash or anchor
|
|
152
|
+
if (nextUrl.startsWith('#')) {
|
|
153
|
+
tab.url = `${tab.url.split('#')[0]}${nextUrl}`;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return await this.navigate(nextUrl);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// 2. Button / Checkbox / Radio toggle
|
|
160
|
+
if (targetEl.role === 'checkbox') {
|
|
161
|
+
const wasChecked = targetEl.state.checked;
|
|
162
|
+
const newChecked = !wasChecked;
|
|
163
|
+
if (targetEl.attributes['id']) {
|
|
164
|
+
const id = targetEl.attributes['id'];
|
|
165
|
+
if (newChecked) {
|
|
166
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`(<input\\b[^>]*id=["']${id}["'][^>]*)>`, 'i'), '$1 checked>');
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`(<input\\b[^>]*id=["']${id}["'][^>]*)checked([^>]*)>`, 'i'), '$1$2>');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// 3. Dropdown aria-expanded toggle
|
|
174
|
+
if (targetEl.attributes['aria-expanded'] !== undefined) {
|
|
175
|
+
const isExp = targetEl.attributes['aria-expanded'] === 'true';
|
|
176
|
+
const nextExp = !isExp;
|
|
177
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`aria-expanded=["']${isExp}["']`, 'gi'), `aria-expanded="${nextExp}"`);
|
|
178
|
+
}
|
|
179
|
+
// 4. Modal / Dialog open trigger
|
|
180
|
+
if (targetEl.attributes['data-toggle'] === 'modal' || targetEl.attributes['aria-haspopup'] === 'dialog' || targetEl.text.toLowerCase().includes('open modal')) {
|
|
181
|
+
mutatedHtml = mutatedHtml.replace(/<dialog\b(?![^>]*open)/i, '<dialog open ');
|
|
182
|
+
mutatedHtml = mutatedHtml.replace(/class=["']([^"']*\bmodal\b(?![^"']*\bshow\b)[^"']*)["']/i, 'class="$1 show"');
|
|
183
|
+
}
|
|
184
|
+
// 5. Modal close / dismiss button
|
|
185
|
+
if (targetEl.attributes['aria-label'] === 'close' || targetEl.attributes['data-dismiss'] === 'modal' || targetEl.text.toLowerCase() === 'close' || targetEl.text === 'X') {
|
|
186
|
+
mutatedHtml = mutatedHtml.replace(/<dialog\b([^>]*)open/gi, '<dialog$1');
|
|
187
|
+
mutatedHtml = mutatedHtml.replace(/class=["']([^"']*\bmodal\b[^"']*)\b(?:show|active|open)\b([^"']*)["']/gi, 'class="$1$2"');
|
|
188
|
+
}
|
|
189
|
+
// 6. Form submission button
|
|
190
|
+
const isSubmit = targetEl.attributes['type'] === 'submit' ||
|
|
191
|
+
(targetEl.role === 'button' && /submit|register|login|signup|save|send/i.test(targetEl.text || ''));
|
|
192
|
+
if (isSubmit && beforeState.forms && beforeState.forms.length > 0) {
|
|
193
|
+
const form = beforeState.forms.find(f => (f.submitButton && (f.submitButton.id === targetEl.id || f.submitButton.text === targetEl.text)) ||
|
|
194
|
+
(targetEl.attributes['form'] && f.id === targetEl.attributes['form'])) || beforeState.forms[0];
|
|
195
|
+
if (form && form.action) {
|
|
196
|
+
let actionUrl = form.action;
|
|
197
|
+
try {
|
|
198
|
+
actionUrl = new URL(actionUrl, tab.url).toString();
|
|
199
|
+
}
|
|
200
|
+
catch { }
|
|
201
|
+
return await this.navigate(actionUrl);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
tab.html = mutatedHtml;
|
|
205
|
+
const afterState = await this.captureCurrentState();
|
|
206
|
+
const { navigationDetected, popupDetected, newUrl, popupTitle } = detectNavigationAndPopups(beforeState, afterState);
|
|
207
|
+
return {
|
|
208
|
+
success: true,
|
|
209
|
+
action: 'click',
|
|
210
|
+
target,
|
|
211
|
+
beforeState,
|
|
212
|
+
afterState,
|
|
213
|
+
durationMs: Date.now() - startTime,
|
|
214
|
+
metadata: {
|
|
215
|
+
elementId: targetEl.id,
|
|
216
|
+
role: targetEl.role,
|
|
217
|
+
text: targetEl.text,
|
|
218
|
+
strategy: resolved.strategy,
|
|
219
|
+
navigationDetected,
|
|
220
|
+
popupDetected,
|
|
221
|
+
newUrl,
|
|
222
|
+
popupTitle,
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async type(target, text, options) {
|
|
227
|
+
const startTime = Date.now();
|
|
228
|
+
const beforeState = await this.captureCurrentState();
|
|
229
|
+
const tab = this.getActiveTab();
|
|
230
|
+
if (!tab) {
|
|
231
|
+
return {
|
|
232
|
+
success: false,
|
|
233
|
+
action: 'type',
|
|
234
|
+
target,
|
|
235
|
+
beforeState,
|
|
236
|
+
error: 'No active tab.',
|
|
237
|
+
durationMs: Date.now() - startTime,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
241
|
+
let targetEl = resolved.element;
|
|
242
|
+
if (!targetEl) {
|
|
243
|
+
const freshState = await this.captureCurrentState();
|
|
244
|
+
targetEl = recoverStaleElement(target, null, freshState.elements);
|
|
245
|
+
}
|
|
246
|
+
if (!targetEl) {
|
|
247
|
+
return {
|
|
248
|
+
success: false,
|
|
249
|
+
action: 'type',
|
|
250
|
+
target,
|
|
251
|
+
beforeState,
|
|
252
|
+
error: `Input element not found matching "${JSON.stringify(target)}".`,
|
|
253
|
+
durationMs: Date.now() - startTime,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (targetEl.state.disabled || targetEl.state.readOnly) {
|
|
257
|
+
return {
|
|
258
|
+
success: false,
|
|
259
|
+
action: 'type',
|
|
260
|
+
target,
|
|
261
|
+
beforeState,
|
|
262
|
+
error: `Target input ${targetEl.id} is disabled or readonly.`,
|
|
263
|
+
durationMs: Date.now() - startTime,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const currentValue = targetEl.attributes['value'] || '';
|
|
267
|
+
const newValue = options?.clear ? text : `${currentValue}${text}`;
|
|
268
|
+
// Mutate DOM in memory
|
|
269
|
+
let mutatedHtml = tab.html;
|
|
270
|
+
if (targetEl.attributes['id']) {
|
|
271
|
+
const id = targetEl.attributes['id'];
|
|
272
|
+
if (/value=["'][^"']*["']/i.test(mutatedHtml)) {
|
|
273
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`(<(?:input|textarea)\\b[^>]*id=["']${id}["'][^>]*)value=["'][^"']*["']`, 'i'), `$1value="${newValue}"`);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`(<(?:input|textarea)\\b[^>]*id=["']${id}["'][^>]*)>`, 'i'), `$1 value="${newValue}">`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else if (targetEl.attributes['name']) {
|
|
280
|
+
const name = targetEl.attributes['name'];
|
|
281
|
+
mutatedHtml = mutatedHtml.replace(new RegExp(`(<(?:input|textarea)\\b[^>]*name=["']${name}["'][^>]*)>`, 'i'), `$1 value="${newValue}">`);
|
|
282
|
+
}
|
|
283
|
+
tab.html = mutatedHtml;
|
|
284
|
+
const afterState = await this.captureCurrentState();
|
|
285
|
+
return {
|
|
286
|
+
success: true,
|
|
287
|
+
action: 'type',
|
|
288
|
+
target,
|
|
289
|
+
beforeState,
|
|
290
|
+
afterState,
|
|
291
|
+
durationMs: Date.now() - startTime,
|
|
292
|
+
metadata: {
|
|
293
|
+
typedText: text,
|
|
294
|
+
finalValue: newValue,
|
|
295
|
+
elementId: targetEl.id,
|
|
296
|
+
strategy: resolved.strategy,
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
async select(target, value) {
|
|
301
|
+
const startTime = Date.now();
|
|
302
|
+
const beforeState = await this.captureCurrentState();
|
|
303
|
+
const tab = this.getActiveTab();
|
|
304
|
+
if (!tab) {
|
|
305
|
+
return {
|
|
306
|
+
success: false,
|
|
307
|
+
action: 'select',
|
|
308
|
+
target,
|
|
309
|
+
beforeState,
|
|
310
|
+
error: 'No active tab.',
|
|
311
|
+
durationMs: Date.now() - startTime,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
315
|
+
const targetEl = resolved.element;
|
|
316
|
+
if (!targetEl) {
|
|
317
|
+
return {
|
|
318
|
+
success: false,
|
|
319
|
+
action: 'select',
|
|
320
|
+
target,
|
|
321
|
+
beforeState,
|
|
322
|
+
error: `Select dropdown element not found matching "${JSON.stringify(target)}".`,
|
|
323
|
+
durationMs: Date.now() - startTime,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
let mutatedHtml = tab.html;
|
|
327
|
+
// Strip previous selected attributes in that select block
|
|
328
|
+
const selectId = targetEl.attributes['id'];
|
|
329
|
+
if (selectId) {
|
|
330
|
+
const selectRegex = new RegExp(`(<select\\b[^>]*id=["']${selectId}["'][\\s\\S]*?<\\/select>)`, 'i');
|
|
331
|
+
const match = mutatedHtml.match(selectRegex);
|
|
332
|
+
if (match) {
|
|
333
|
+
let selectBlock = match[1];
|
|
334
|
+
selectBlock = selectBlock.replace(/\sselected\b/gi, '');
|
|
335
|
+
// Add selected attribute to option with matching value or text
|
|
336
|
+
selectBlock = selectBlock.replace(new RegExp(`(<option\\b[^>]*(?:value=["']${value}["']|>${value}<)[^>]*)>`, 'i'), '$1 selected>');
|
|
337
|
+
mutatedHtml = mutatedHtml.replace(selectRegex, selectBlock);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
tab.html = mutatedHtml;
|
|
341
|
+
const afterState = await this.captureCurrentState();
|
|
342
|
+
return {
|
|
343
|
+
success: true,
|
|
344
|
+
action: 'select',
|
|
345
|
+
target,
|
|
346
|
+
beforeState,
|
|
347
|
+
afterState,
|
|
348
|
+
durationMs: Date.now() - startTime,
|
|
349
|
+
metadata: {
|
|
350
|
+
selectedValue: value,
|
|
351
|
+
elementId: targetEl.id,
|
|
352
|
+
},
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
async scroll(direction, amount = 500) {
|
|
356
|
+
const startTime = Date.now();
|
|
357
|
+
const beforeState = await this.captureCurrentState();
|
|
358
|
+
const viewport = { ...beforeState.viewport };
|
|
359
|
+
if (direction === 'down')
|
|
360
|
+
viewport.scrollY += amount;
|
|
361
|
+
else if (direction === 'up')
|
|
362
|
+
viewport.scrollY = Math.max(0, viewport.scrollY - amount);
|
|
363
|
+
else if (direction === 'right')
|
|
364
|
+
viewport.scrollX += amount;
|
|
365
|
+
else if (direction === 'left')
|
|
366
|
+
viewport.scrollX = Math.max(0, viewport.scrollX - amount);
|
|
367
|
+
const afterState = await this.observer.observe({ html: this.getActiveTab()?.html || '', url: this.getActiveTab()?.url || '', title: this.getActiveTab()?.title || '' }, { viewport });
|
|
368
|
+
return {
|
|
369
|
+
success: true,
|
|
370
|
+
action: 'scroll',
|
|
371
|
+
target: direction,
|
|
372
|
+
beforeState,
|
|
373
|
+
afterState,
|
|
374
|
+
durationMs: Date.now() - startTime,
|
|
375
|
+
metadata: {
|
|
376
|
+
direction,
|
|
377
|
+
amount,
|
|
378
|
+
newScrollY: viewport.scrollY,
|
|
379
|
+
newScrollX: viewport.scrollX,
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
async hover(target) {
|
|
384
|
+
const startTime = Date.now();
|
|
385
|
+
const beforeState = await this.captureCurrentState();
|
|
386
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
387
|
+
if (!resolved.element) {
|
|
388
|
+
return {
|
|
389
|
+
success: false,
|
|
390
|
+
action: 'hover',
|
|
391
|
+
target,
|
|
392
|
+
beforeState,
|
|
393
|
+
error: `Element not found to hover: "${JSON.stringify(target)}"`,
|
|
394
|
+
durationMs: Date.now() - startTime,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
success: true,
|
|
399
|
+
action: 'hover',
|
|
400
|
+
target,
|
|
401
|
+
beforeState,
|
|
402
|
+
afterState: beforeState,
|
|
403
|
+
durationMs: Date.now() - startTime,
|
|
404
|
+
metadata: {
|
|
405
|
+
elementId: resolved.element.id,
|
|
406
|
+
hovered: true,
|
|
407
|
+
bounds: resolved.element.bounds,
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
async press_key(key) {
|
|
412
|
+
const startTime = Date.now();
|
|
413
|
+
const beforeState = await this.captureCurrentState();
|
|
414
|
+
const tab = this.getActiveTab();
|
|
415
|
+
if (!tab) {
|
|
416
|
+
return {
|
|
417
|
+
success: false,
|
|
418
|
+
action: 'press_key',
|
|
419
|
+
target: key,
|
|
420
|
+
beforeState,
|
|
421
|
+
error: 'No active tab.',
|
|
422
|
+
durationMs: Date.now() - startTime,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
let mutatedHtml = tab.html;
|
|
426
|
+
const normKey = key.trim().toLowerCase();
|
|
427
|
+
// Escape dismisses active modals
|
|
428
|
+
if (normKey === 'escape' || normKey === 'esc') {
|
|
429
|
+
mutatedHtml = mutatedHtml.replace(/<dialog\b([^>]*)open/gi, '<dialog$1');
|
|
430
|
+
mutatedHtml = mutatedHtml.replace(/class=["']([^"']*\bmodal\b[^"']*)\bshow\b([^"']*)["']/gi, 'class="$1$2"');
|
|
431
|
+
}
|
|
432
|
+
// Enter on form submits
|
|
433
|
+
if (normKey === 'enter' && beforeState.forms && beforeState.forms.length > 0) {
|
|
434
|
+
const firstForm = beforeState.forms[0];
|
|
435
|
+
if (firstForm?.submitButton) {
|
|
436
|
+
return await this.click(firstForm.submitButton.id);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
tab.html = mutatedHtml;
|
|
440
|
+
const afterState = await this.captureCurrentState();
|
|
441
|
+
return {
|
|
442
|
+
success: true,
|
|
443
|
+
action: 'press_key',
|
|
444
|
+
target: key,
|
|
445
|
+
beforeState,
|
|
446
|
+
afterState,
|
|
447
|
+
durationMs: Date.now() - startTime,
|
|
448
|
+
metadata: { key },
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
async open_tab(url = 'about:blank', htmlContent) {
|
|
452
|
+
const startTime = Date.now();
|
|
453
|
+
const beforeState = await this.captureCurrentState();
|
|
454
|
+
const newTabId = `tab-${this.tabs.length + 1}`;
|
|
455
|
+
let tabTitle = 'New Tab';
|
|
456
|
+
if (htmlContent) {
|
|
457
|
+
const tm = htmlContent.match(/<title\b[^>]*>([\s\S]*?)<\/title>/i);
|
|
458
|
+
if (tm && tm[1])
|
|
459
|
+
tabTitle = tm[1].trim();
|
|
460
|
+
}
|
|
461
|
+
const newTab = {
|
|
462
|
+
id: newTabId,
|
|
463
|
+
url,
|
|
464
|
+
title: tabTitle,
|
|
465
|
+
html: htmlContent || '<html><head><title>New Tab</title></head><body></body></html>',
|
|
466
|
+
history: { past: [], future: [] },
|
|
467
|
+
};
|
|
468
|
+
this.tabs.push(newTab);
|
|
469
|
+
this.activeTabId = newTabId;
|
|
470
|
+
const afterState = await this.captureCurrentState();
|
|
471
|
+
return {
|
|
472
|
+
success: true,
|
|
473
|
+
action: 'open_tab',
|
|
474
|
+
target: url,
|
|
475
|
+
beforeState,
|
|
476
|
+
afterState,
|
|
477
|
+
durationMs: Date.now() - startTime,
|
|
478
|
+
metadata: {
|
|
479
|
+
tabId: newTabId,
|
|
480
|
+
totalTabs: this.tabs.length,
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
async close_tab(tabId) {
|
|
485
|
+
const startTime = Date.now();
|
|
486
|
+
const beforeState = await this.captureCurrentState();
|
|
487
|
+
const targetId = tabId || this.activeTabId;
|
|
488
|
+
if (this.tabs.length <= 1) {
|
|
489
|
+
return {
|
|
490
|
+
success: false,
|
|
491
|
+
action: 'close_tab',
|
|
492
|
+
target: targetId,
|
|
493
|
+
beforeState,
|
|
494
|
+
error: 'Cannot close the last remaining browser tab.',
|
|
495
|
+
durationMs: Date.now() - startTime,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
const idx = this.tabs.findIndex(t => t.id === targetId);
|
|
499
|
+
if (idx === -1) {
|
|
500
|
+
return {
|
|
501
|
+
success: false,
|
|
502
|
+
action: 'close_tab',
|
|
503
|
+
target: targetId,
|
|
504
|
+
beforeState,
|
|
505
|
+
error: `Tab ID "${targetId}" not found.`,
|
|
506
|
+
durationMs: Date.now() - startTime,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
this.tabs.splice(idx, 1);
|
|
510
|
+
if (this.activeTabId === targetId) {
|
|
511
|
+
this.activeTabId = this.tabs[0].id;
|
|
512
|
+
}
|
|
513
|
+
const afterState = await this.captureCurrentState();
|
|
514
|
+
return {
|
|
515
|
+
success: true,
|
|
516
|
+
action: 'close_tab',
|
|
517
|
+
target: targetId,
|
|
518
|
+
beforeState,
|
|
519
|
+
afterState,
|
|
520
|
+
durationMs: Date.now() - startTime,
|
|
521
|
+
metadata: {
|
|
522
|
+
closedTabId: targetId,
|
|
523
|
+
activeTabId: this.activeTabId,
|
|
524
|
+
remainingTabs: this.tabs.length,
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
async switch_tab(tabId) {
|
|
529
|
+
const startTime = Date.now();
|
|
530
|
+
const beforeState = await this.captureCurrentState();
|
|
531
|
+
const target = this.tabs.find(t => t.id === tabId);
|
|
532
|
+
if (!target) {
|
|
533
|
+
return {
|
|
534
|
+
success: false,
|
|
535
|
+
action: 'switch_tab',
|
|
536
|
+
target: tabId,
|
|
537
|
+
beforeState,
|
|
538
|
+
error: `Tab "${tabId}" does not exist.`,
|
|
539
|
+
durationMs: Date.now() - startTime,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
this.activeTabId = target.id;
|
|
543
|
+
const afterState = await this.captureCurrentState();
|
|
544
|
+
return {
|
|
545
|
+
success: true,
|
|
546
|
+
action: 'switch_tab',
|
|
547
|
+
target: tabId,
|
|
548
|
+
beforeState,
|
|
549
|
+
afterState,
|
|
550
|
+
durationMs: Date.now() - startTime,
|
|
551
|
+
metadata: {
|
|
552
|
+
activeTabId: this.activeTabId,
|
|
553
|
+
url: target.url,
|
|
554
|
+
title: target.title,
|
|
555
|
+
},
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
async wait(condition, param) {
|
|
559
|
+
const startTime = Date.now();
|
|
560
|
+
const beforeState = await this.captureCurrentState();
|
|
561
|
+
const met = await waitForCondition(condition, param, async () => {
|
|
562
|
+
const s = await this.captureCurrentState();
|
|
563
|
+
if (condition === 'networkidle' || condition === 'load') {
|
|
564
|
+
return !s.loadingState.isBusy;
|
|
565
|
+
}
|
|
566
|
+
if (condition === 'element' && typeof param === 'string') {
|
|
567
|
+
return resolveTargetElement(param, s.elements).element !== null;
|
|
568
|
+
}
|
|
569
|
+
if (condition === 'element_hidden' && typeof param === 'string') {
|
|
570
|
+
const el = resolveTargetElement(param, s.elements).element;
|
|
571
|
+
return el === null || !el.state.visible;
|
|
572
|
+
}
|
|
573
|
+
if (condition === 'url_change') {
|
|
574
|
+
return s.url !== beforeState.url;
|
|
575
|
+
}
|
|
576
|
+
return true;
|
|
577
|
+
}, param?.timeoutMs || 5000);
|
|
578
|
+
const afterState = await this.captureCurrentState();
|
|
579
|
+
return {
|
|
580
|
+
success: met,
|
|
581
|
+
action: 'wait',
|
|
582
|
+
target: String(condition),
|
|
583
|
+
beforeState,
|
|
584
|
+
afterState,
|
|
585
|
+
durationMs: Date.now() - startTime,
|
|
586
|
+
metadata: { condition, conditionMet: met },
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
async upload(target, localFilePath) {
|
|
590
|
+
const startTime = Date.now();
|
|
591
|
+
const beforeState = await this.captureCurrentState();
|
|
592
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
593
|
+
if (!resolved.element) {
|
|
594
|
+
return {
|
|
595
|
+
success: false,
|
|
596
|
+
action: 'upload',
|
|
597
|
+
target,
|
|
598
|
+
beforeState,
|
|
599
|
+
error: `File upload element not found matching "${JSON.stringify(target)}".`,
|
|
600
|
+
durationMs: Date.now() - startTime,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
const fullPath = path.isAbsolute(localFilePath)
|
|
604
|
+
? localFilePath
|
|
605
|
+
: path.resolve(this.workspaceDir, localFilePath);
|
|
606
|
+
if (!fs.existsSync(fullPath)) {
|
|
607
|
+
return {
|
|
608
|
+
success: false,
|
|
609
|
+
action: 'upload',
|
|
610
|
+
target,
|
|
611
|
+
beforeState,
|
|
612
|
+
error: `Local file to upload not found at: "${fullPath}".`,
|
|
613
|
+
durationMs: Date.now() - startTime,
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
const fileName = path.basename(fullPath);
|
|
617
|
+
return {
|
|
618
|
+
success: true,
|
|
619
|
+
action: 'upload',
|
|
620
|
+
target,
|
|
621
|
+
beforeState,
|
|
622
|
+
afterState: beforeState,
|
|
623
|
+
durationMs: Date.now() - startTime,
|
|
624
|
+
metadata: {
|
|
625
|
+
elementId: resolved.element.id,
|
|
626
|
+
uploadedFile: fileName,
|
|
627
|
+
filePath: fullPath,
|
|
628
|
+
fileSizeBytes: fs.statSync(fullPath).size,
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
async download(target, destPath) {
|
|
633
|
+
const startTime = Date.now();
|
|
634
|
+
const beforeState = await this.captureCurrentState();
|
|
635
|
+
let downloadUrl = typeof target === 'string' ? target : (target.selectorOrRef || '');
|
|
636
|
+
if (!downloadUrl.startsWith('http://') && !downloadUrl.startsWith('https://')) {
|
|
637
|
+
const resolved = resolveTargetElement(target, beforeState.elements);
|
|
638
|
+
if (resolved.element?.attributes['href']) {
|
|
639
|
+
downloadUrl = resolved.element.attributes['href'];
|
|
640
|
+
try {
|
|
641
|
+
downloadUrl = new URL(downloadUrl, this.getActiveTab()?.url).toString();
|
|
642
|
+
}
|
|
643
|
+
catch { }
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if (!downloadUrl.startsWith('http://') && !downloadUrl.startsWith('https://')) {
|
|
647
|
+
return {
|
|
648
|
+
success: false,
|
|
649
|
+
action: 'download',
|
|
650
|
+
target,
|
|
651
|
+
beforeState,
|
|
652
|
+
error: `Cannot download: "${JSON.stringify(target)}" is not a valid download link or URL.`,
|
|
653
|
+
durationMs: Date.now() - startTime,
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
const fileName = path.basename(new URL(downloadUrl).pathname) || `download_${Date.now()}.dat`;
|
|
657
|
+
const finalPath = destPath
|
|
658
|
+
? (path.isAbsolute(destPath) ? destPath : path.resolve(this.workspaceDir, destPath))
|
|
659
|
+
: path.resolve(this.workspaceDir, 'downloads', fileName);
|
|
660
|
+
try {
|
|
661
|
+
fs.mkdirSync(path.dirname(finalPath), { recursive: true });
|
|
662
|
+
// For in-memory / simulated downloads or HTTP fetch
|
|
663
|
+
if (downloadUrl.startsWith('http')) {
|
|
664
|
+
try {
|
|
665
|
+
const res = await fetch(downloadUrl);
|
|
666
|
+
if (res.ok) {
|
|
667
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
668
|
+
fs.writeFileSync(finalPath, buffer);
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
fs.writeFileSync(finalPath, `Simulated download content for ${downloadUrl}`, 'utf-8');
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
catch {
|
|
675
|
+
fs.writeFileSync(finalPath, `Simulated download content for ${downloadUrl}`, 'utf-8');
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
fs.writeFileSync(finalPath, `Downloaded data from ${downloadUrl}`, 'utf-8');
|
|
680
|
+
}
|
|
681
|
+
return {
|
|
682
|
+
success: true,
|
|
683
|
+
action: 'download',
|
|
684
|
+
target,
|
|
685
|
+
beforeState,
|
|
686
|
+
afterState: beforeState,
|
|
687
|
+
durationMs: Date.now() - startTime,
|
|
688
|
+
metadata: {
|
|
689
|
+
downloadUrl,
|
|
690
|
+
destinationPath: finalPath,
|
|
691
|
+
filename: fileName,
|
|
692
|
+
},
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
catch (err) {
|
|
696
|
+
return {
|
|
697
|
+
success: false,
|
|
698
|
+
action: 'download',
|
|
699
|
+
target,
|
|
700
|
+
beforeState,
|
|
701
|
+
error: `Download error: ${err.message}`,
|
|
702
|
+
durationMs: Date.now() - startTime,
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
//# sourceMappingURL=actionEngine.js.map
|