@loxia-labs/loxia-autopilot-one 1.0.1

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.
Files changed (80) hide show
  1. package/LICENSE +267 -0
  2. package/README.md +509 -0
  3. package/bin/cli.js +117 -0
  4. package/package.json +94 -0
  5. package/scripts/install-scanners.js +236 -0
  6. package/src/analyzers/CSSAnalyzer.js +297 -0
  7. package/src/analyzers/ConfigValidator.js +690 -0
  8. package/src/analyzers/ESLintAnalyzer.js +320 -0
  9. package/src/analyzers/JavaScriptAnalyzer.js +261 -0
  10. package/src/analyzers/PrettierFormatter.js +247 -0
  11. package/src/analyzers/PythonAnalyzer.js +266 -0
  12. package/src/analyzers/SecurityAnalyzer.js +729 -0
  13. package/src/analyzers/TypeScriptAnalyzer.js +247 -0
  14. package/src/analyzers/codeCloneDetector/analyzer.js +344 -0
  15. package/src/analyzers/codeCloneDetector/detector.js +203 -0
  16. package/src/analyzers/codeCloneDetector/index.js +160 -0
  17. package/src/analyzers/codeCloneDetector/parser.js +199 -0
  18. package/src/analyzers/codeCloneDetector/reporter.js +148 -0
  19. package/src/analyzers/codeCloneDetector/scanner.js +59 -0
  20. package/src/core/agentPool.js +1474 -0
  21. package/src/core/agentScheduler.js +2147 -0
  22. package/src/core/contextManager.js +709 -0
  23. package/src/core/messageProcessor.js +732 -0
  24. package/src/core/orchestrator.js +548 -0
  25. package/src/core/stateManager.js +877 -0
  26. package/src/index.js +631 -0
  27. package/src/interfaces/cli.js +549 -0
  28. package/src/interfaces/webServer.js +2162 -0
  29. package/src/modules/fileExplorer/controller.js +280 -0
  30. package/src/modules/fileExplorer/index.js +37 -0
  31. package/src/modules/fileExplorer/middleware.js +92 -0
  32. package/src/modules/fileExplorer/routes.js +125 -0
  33. package/src/modules/fileExplorer/types.js +44 -0
  34. package/src/services/aiService.js +1232 -0
  35. package/src/services/apiKeyManager.js +164 -0
  36. package/src/services/benchmarkService.js +366 -0
  37. package/src/services/budgetService.js +539 -0
  38. package/src/services/contextInjectionService.js +247 -0
  39. package/src/services/conversationCompactionService.js +637 -0
  40. package/src/services/errorHandler.js +810 -0
  41. package/src/services/fileAttachmentService.js +544 -0
  42. package/src/services/modelRouterService.js +366 -0
  43. package/src/services/modelsService.js +322 -0
  44. package/src/services/qualityInspector.js +796 -0
  45. package/src/services/tokenCountingService.js +536 -0
  46. package/src/tools/agentCommunicationTool.js +1344 -0
  47. package/src/tools/agentDelayTool.js +485 -0
  48. package/src/tools/asyncToolManager.js +604 -0
  49. package/src/tools/baseTool.js +800 -0
  50. package/src/tools/browserTool.js +920 -0
  51. package/src/tools/cloneDetectionTool.js +621 -0
  52. package/src/tools/dependencyResolverTool.js +1215 -0
  53. package/src/tools/fileContentReplaceTool.js +875 -0
  54. package/src/tools/fileSystemTool.js +1107 -0
  55. package/src/tools/fileTreeTool.js +853 -0
  56. package/src/tools/imageTool.js +901 -0
  57. package/src/tools/importAnalyzerTool.js +1060 -0
  58. package/src/tools/jobDoneTool.js +248 -0
  59. package/src/tools/seekTool.js +956 -0
  60. package/src/tools/staticAnalysisTool.js +1778 -0
  61. package/src/tools/taskManagerTool.js +2873 -0
  62. package/src/tools/terminalTool.js +2304 -0
  63. package/src/tools/webTool.js +1430 -0
  64. package/src/types/agent.js +519 -0
  65. package/src/types/contextReference.js +972 -0
  66. package/src/types/conversation.js +730 -0
  67. package/src/types/toolCommand.js +747 -0
  68. package/src/utilities/attachmentValidator.js +292 -0
  69. package/src/utilities/configManager.js +582 -0
  70. package/src/utilities/constants.js +722 -0
  71. package/src/utilities/directoryAccessManager.js +535 -0
  72. package/src/utilities/fileProcessor.js +307 -0
  73. package/src/utilities/logger.js +436 -0
  74. package/src/utilities/tagParser.js +1246 -0
  75. package/src/utilities/toolConstants.js +317 -0
  76. package/web-ui/build/index.html +15 -0
  77. package/web-ui/build/logo.png +0 -0
  78. package/web-ui/build/logo2.png +0 -0
  79. package/web-ui/build/static/index-CjkkcnFA.js +344 -0
  80. package/web-ui/build/static/index-Dy2bYbOa.css +1 -0
@@ -0,0 +1,920 @@
1
+ /**
2
+ * BrowserTool - Handle browser automation and web interaction tasks
3
+ *
4
+ * Purpose:
5
+ * - Automate web browser interactions
6
+ * - Navigate websites and extract data
7
+ * - Fill forms and interact with web elements
8
+ * - Take screenshots and analyze web content
9
+ * - Handle authentication and sessions
10
+ */
11
+
12
+ import { BaseTool } from './baseTool.js';
13
+ import TagParser from '../utilities/tagParser.js';
14
+ import puppeteer from 'puppeteer';
15
+ import fs from 'fs/promises';
16
+ import path from 'path';
17
+
18
+ import {
19
+ TOOL_STATUS,
20
+ SYSTEM_DEFAULTS
21
+ } from '../utilities/constants.js';
22
+
23
+ class BrowserTool extends BaseTool {
24
+ constructor(config = {}, logger = null) {
25
+ super(config, logger);
26
+
27
+ // Tool metadata
28
+ this.requiresProject = false;
29
+ this.isAsync = true; // Browser operations can be slow
30
+ this.timeout = config.timeout || 60000; // 1 minute default
31
+ this.maxConcurrentOperations = config.maxConcurrentOperations || 2;
32
+
33
+ // Browser configuration
34
+ this.browserConfig = {
35
+ headless: config.headless !== false, // Default to headless
36
+ viewport: config.viewport || { width: 1280, height: 720 },
37
+ userAgent: config.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
38
+ timeout: config.pageTimeout || 30000
39
+ };
40
+
41
+ // Active browser instances
42
+ this.browserInstances = new Map();
43
+
44
+ // Security settings
45
+ this.allowedDomains = config.allowedDomains || null; // null = all allowed
46
+ this.blockedDomains = config.blockedDomains || [];
47
+ this.maxScreenshotSize = config.maxScreenshotSize || 5 * 1024 * 1024; // 5MB
48
+
49
+ // Operation history
50
+ this.operationHistory = [];
51
+ }
52
+
53
+ /**
54
+ * Get tool description for LLM consumption
55
+ * @returns {string} Tool description
56
+ */
57
+ getDescription() {
58
+ return `
59
+ Browser Tool: Automate web browser interactions, navigation, and data extraction.
60
+
61
+ IMPORTANT - SYNTAX: Use self-closing tags with angle brackets < >, not square brackets [ ].
62
+
63
+ XML USAGE (Recommended):
64
+ <browser>
65
+ <navigate url="https://example.com" />
66
+ <wait wait-time="2000" />
67
+ <click selector="button.submit" />
68
+ <screenshot output-path="screenshot.png" />
69
+ </browser>
70
+
71
+ JSON USAGE (Alternative):
72
+ \`\`\`json
73
+ {
74
+ "toolId": "browser",
75
+ "actions": [
76
+ { "type": "navigate", "url": "https://example.com" },
77
+ { "type": "wait", "waitTime": 2000 },
78
+ { "type": "click", "selector": "button.submit" },
79
+ { "type": "screenshot", "outputPath": "screenshot.png" }
80
+ ]
81
+ }
82
+ \`\`\`
83
+
84
+ SUPPORTED ACTIONS:
85
+ - navigate: Navigate to a URL (requires: url)
86
+ - click: Click on an element (requires: selector)
87
+ - type: Type text into an input field (requires: selector, text)
88
+ - wait: Wait for element or time (requires: wait-time OR wait-selector)
89
+ - screenshot: Take screenshot of page (requires: output-path)
90
+ - extract: Extract text/data from elements (requires: selector, attribute)
91
+ - scroll: Scroll page or element (requires: direction)
92
+ - close: Close browser instance (no parameters)
93
+
94
+ PARAMETER REFERENCE:
95
+ - url: Target URL for navigation
96
+ - selector: CSS selector for element targeting (e.g., "#id", ".class", "button[type='submit']")
97
+ - text: Text content to type
98
+ - output-path: File path for screenshots (kebab-case in XML, outputPath in JSON)
99
+ - wait-time: Time to wait in milliseconds (100-30000, kebab-case in XML, waitTime in JSON)
100
+ - wait-selector: Element selector to wait for (kebab-case in XML, waitSelector in JSON)
101
+ - wait-type: Wait type - "visible" or "hidden" (default: "visible")
102
+ - attribute: HTML attribute to extract (default: "text")
103
+ - direction: Scroll direction - "up", "down", "left", "right"
104
+ - pixels: Scroll distance in pixels (default: 300)
105
+ - full-page: Take full page screenshot - "true" or "false"
106
+
107
+ EXAMPLES:
108
+
109
+ Example 1 - Research and screenshot:
110
+ <browser>
111
+ <navigate url="https://github.com/trending" />
112
+ <wait wait-time="2000" />
113
+ <screenshot output-path="trending.png" />
114
+ <extract selector="h2.h3" attribute="text" />
115
+ <close />
116
+ </browser>
117
+
118
+ Example 2 - Form automation:
119
+ <browser>
120
+ <navigate url="https://example.com/login" />
121
+ <wait wait-selector="input#username" />
122
+ <type selector="input#username" text="myuser" />
123
+ <type selector="input#password" text="mypass" />
124
+ <click selector="button[type='submit']" />
125
+ <wait wait-selector=".success-message" />
126
+ <close />
127
+ </browser>
128
+
129
+ Example 3 - Data extraction:
130
+ <browser>
131
+ <navigate url="https://example.com/products" />
132
+ <wait wait-time="1000" />
133
+ <extract selector="h1.product-title" attribute="text" />
134
+ <extract selector="span.price" attribute="text" />
135
+ <extract selector="img.product" attribute="src" />
136
+ <close />
137
+ </browser>
138
+
139
+ Example 4 - Scrolling and waiting:
140
+ <browser>
141
+ <navigate url="https://example.com/long-page" />
142
+ <scroll direction="down" pixels="500" />
143
+ <wait wait-time="1000" />
144
+ <scroll direction="down" pixels="500" />
145
+ <screenshot output-path="scrolled.png" full-page="true" />
146
+ <close />
147
+ </browser>
148
+
149
+ SYNTAX RULES:
150
+ ✅ DO: <navigate url="..." /> (self-closing with />)
151
+ ✅ DO: <navigate url="..."> (can omit /> if preferred)
152
+ ❌ DON'T: <navigate url="..."></navigate> (no closing tag needed)
153
+ ❌ DON'T: [navigate url="..." /] (square brackets not supported)
154
+
155
+ ATTRIBUTE NAMING:
156
+ - XML uses kebab-case: wait-time, output-path, wait-selector
157
+ - JSON uses camelCase: waitTime, outputPath, waitSelector
158
+ - Parser auto-converts kebab-case to camelCase internally
159
+
160
+ SECURITY:
161
+ - Restricted to allowed domains (if configured)
162
+ - Blocked domains enforced for security
163
+ - Screenshot size limits enforced (max 5MB)
164
+ - Automatic cleanup of browser instances after 10 minutes
165
+ - Sandboxed browser execution
166
+
167
+ BROWSER SESSION:
168
+ The tool maintains browser instances per agent context.
169
+ Use <close /> action to explicitly cleanup, or instances auto-cleanup after timeout.
170
+
171
+ VIEWPORT: ${this.browserConfig.viewport.width}x${this.browserConfig.viewport.height}
172
+ TIMEOUT: ${this.browserConfig.timeout}ms per operation
173
+ MAX WAIT TIME: 30 seconds (30000ms)
174
+ `;
175
+ }
176
+
177
+ /**
178
+ * Parse parameters from tool command content
179
+ * @param {string} content - Raw tool command content
180
+ * @returns {Object} Parsed parameters
181
+ */
182
+ parseParameters(content) {
183
+ try {
184
+ // Try JSON first (for JSON in code blocks)
185
+ if (content.trim().startsWith('{')) {
186
+ const parsed = JSON.parse(content);
187
+ // Ensure actions is an array
188
+ if (!parsed.actions) {
189
+ parsed.actions = [];
190
+ }
191
+ if (!Array.isArray(parsed.actions)) {
192
+ parsed.actions = [parsed.actions];
193
+ }
194
+ return parsed;
195
+ }
196
+
197
+ const params = {};
198
+ const actions = [];
199
+
200
+ // Extract self-closing tags (e.g., <navigate url="..." />)
201
+ const selfClosingTags = [
202
+ 'navigate', 'click', 'type', 'wait', 'screenshot',
203
+ 'extract', 'scroll', 'close'
204
+ ];
205
+
206
+ for (const tagName of selfClosingTags) {
207
+ // Extract self-closing tags using regex
208
+ const selfClosingPattern = new RegExp(
209
+ `<${tagName}\\s+([^>]*?)\\s*\\/?>`,
210
+ 'gi'
211
+ );
212
+
213
+ let match;
214
+ while ((match = selfClosingPattern.exec(content)) !== null) {
215
+ const attributeString = match[1];
216
+ const attributes = this.parseAttributes(attributeString);
217
+
218
+ const action = {
219
+ type: tagName,
220
+ ...attributes
221
+ };
222
+
223
+ // Normalize common attribute names (kebab-case to camelCase)
224
+ if (action['output-path']) {
225
+ action.outputPath = action['output-path'];
226
+ delete action['output-path'];
227
+ }
228
+ if (action['wait-time']) {
229
+ action.waitTime = parseInt(action['wait-time'], 10);
230
+ delete action['wait-time'];
231
+ }
232
+ if (action['wait-selector']) {
233
+ action.waitSelector = action['wait-selector'];
234
+ delete action['wait-selector'];
235
+ }
236
+ if (action['wait-type']) {
237
+ action.waitType = action['wait-type'];
238
+ delete action['wait-type'];
239
+ }
240
+ if (action['full-page']) {
241
+ action.fullPage = action['full-page'] === 'true';
242
+ delete action['full-page'];
243
+ }
244
+
245
+ actions.push(action);
246
+ }
247
+ }
248
+
249
+ // Ensure actions is always an array (even if empty)
250
+ params.actions = Array.isArray(actions) ? actions : [];
251
+ params.rawContent = content.trim();
252
+
253
+ return params;
254
+
255
+ } catch (error) {
256
+ // If JSON parsing fails, return a structure with empty actions
257
+ // This prevents "actions is not iterable" errors
258
+ this.logger?.error('Failed to parse browser parameters', { error: error.message });
259
+ return {
260
+ actions: [], // Always return an array
261
+ rawContent: content.trim(),
262
+ parseError: error.message
263
+ };
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Parse attributes from attribute string
269
+ * @param {string} attributeString - String containing attributes
270
+ * @returns {Object} Parsed attributes object
271
+ * @private
272
+ */
273
+ parseAttributes(attributeString) {
274
+ const attributes = {};
275
+
276
+ if (!attributeString) return attributes;
277
+
278
+ // Match attribute="value" or attribute='value'
279
+ const attrPattern = /([\w-]+)\s*=\s*["']([^"']*)["']/g;
280
+ let match;
281
+
282
+ while ((match = attrPattern.exec(attributeString)) !== null) {
283
+ const attrName = match[1];
284
+ const attrValue = match[2];
285
+ attributes[attrName] = attrValue;
286
+ }
287
+
288
+ return attributes;
289
+ }
290
+
291
+ /**
292
+ * Get required parameters
293
+ * @returns {Array<string>} Array of required parameter names
294
+ */
295
+ getRequiredParameters() {
296
+ return ['actions'];
297
+ }
298
+
299
+ /**
300
+ * Custom parameter validation
301
+ * @param {Object} params - Parameters to validate
302
+ * @returns {Object} Validation result
303
+ */
304
+ customValidateParameters(params) {
305
+ const errors = [];
306
+
307
+ if (!params.actions || !Array.isArray(params.actions) || params.actions.length === 0) {
308
+ errors.push('At least one action is required');
309
+ } else {
310
+ // Validate each action
311
+ for (const [index, action] of params.actions.entries()) {
312
+ if (!action.type) {
313
+ errors.push(`Action ${index + 1}: type is required`);
314
+ continue;
315
+ }
316
+
317
+ switch (action.type) {
318
+ case 'navigate':
319
+ if (!action.url) {
320
+ errors.push(`Action ${index + 1}: url is required for navigate`);
321
+ } else if (!this.isAllowedUrl(action.url)) {
322
+ errors.push(`Action ${index + 1}: URL not allowed: ${action.url}`);
323
+ }
324
+ break;
325
+
326
+ case 'click':
327
+ case 'extract':
328
+ if (!action.selector) {
329
+ errors.push(`Action ${index + 1}: selector is required for ${action.type}`);
330
+ }
331
+ break;
332
+
333
+ case 'type':
334
+ if (!action.selector) {
335
+ errors.push(`Action ${index + 1}: selector is required for type`);
336
+ }
337
+ if (!action.text && action.text !== '') {
338
+ errors.push(`Action ${index + 1}: text is required for type`);
339
+ }
340
+ break;
341
+
342
+ case 'wait':
343
+ if (!action.waitTime && !action.waitSelector) {
344
+ errors.push(`Action ${index + 1}: either wait-time or wait-selector is required`);
345
+ }
346
+ if (action.waitTime && (action.waitTime < 100 || action.waitTime > 30000)) {
347
+ errors.push(`Action ${index + 1}: wait-time must be between 100 and 30000 milliseconds`);
348
+ }
349
+ break;
350
+
351
+ case 'screenshot':
352
+ if (!action.outputPath) {
353
+ errors.push(`Action ${index + 1}: output-path is required for screenshot`);
354
+ }
355
+ break;
356
+
357
+ case 'scroll':
358
+ if (!action.direction) {
359
+ errors.push(`Action ${index + 1}: direction is required for scroll`);
360
+ } else if (!['up', 'down', 'left', 'right'].includes(action.direction)) {
361
+ errors.push(`Action ${index + 1}: invalid scroll direction: ${action.direction}`);
362
+ }
363
+ break;
364
+
365
+ case 'close':
366
+ // No additional validation needed
367
+ break;
368
+
369
+ default:
370
+ errors.push(`Action ${index + 1}: unknown action type: ${action.type}`);
371
+ }
372
+ }
373
+ }
374
+
375
+ return {
376
+ valid: errors.length === 0,
377
+ errors
378
+ };
379
+ }
380
+
381
+ /**
382
+ * Execute tool with parsed parameters
383
+ * @param {Object} params - Parsed parameters
384
+ * @param {Object} context - Execution context
385
+ * @returns {Promise<Object>} Execution result
386
+ */
387
+ async execute(params, context) {
388
+ const { actions } = params;
389
+ const { agentId, projectDir } = context;
390
+
391
+ const browserKey = `${agentId}-${projectDir || 'default'}`;
392
+ const results = [];
393
+
394
+ for (const action of actions) {
395
+ try {
396
+ let result;
397
+
398
+ switch (action.type) {
399
+ case 'navigate':
400
+ result = await this.navigate(action.url, browserKey);
401
+ break;
402
+
403
+ case 'click':
404
+ result = await this.click(action.selector, browserKey);
405
+ break;
406
+
407
+ case 'type':
408
+ result = await this.type(action.selector, action.text, browserKey);
409
+ break;
410
+
411
+ case 'wait':
412
+ result = await this.wait(action, browserKey);
413
+ break;
414
+
415
+ case 'screenshot':
416
+ result = await this.screenshot(action.outputPath, projectDir, browserKey, action);
417
+ break;
418
+
419
+ case 'extract':
420
+ result = await this.extract(action.selector, action.attribute || 'text', browserKey);
421
+ break;
422
+
423
+ case 'scroll':
424
+ result = await this.scroll(action, browserKey);
425
+ break;
426
+
427
+ case 'close':
428
+ result = await this.closeBrowser(browserKey);
429
+ break;
430
+
431
+ default:
432
+ throw new Error(`Unknown action type: ${action.type}`);
433
+ }
434
+
435
+ results.push(result);
436
+ this.addToHistory(action, result, agentId);
437
+
438
+ } catch (error) {
439
+ const errorResult = {
440
+ success: false,
441
+ action: action.type,
442
+ error: error.message,
443
+ selector: action.selector || action.url
444
+ };
445
+
446
+ results.push(errorResult);
447
+ this.addToHistory(action, errorResult, agentId);
448
+ }
449
+ }
450
+
451
+ return {
452
+ success: true,
453
+ actions: results,
454
+ executedActions: actions.length,
455
+ toolUsed: 'browser'
456
+ };
457
+ }
458
+
459
+ /**
460
+ * Navigate to URL
461
+ * @private
462
+ */
463
+ async navigate(url, browserKey) {
464
+ const browser = await this.getBrowser(browserKey);
465
+ const page = await this.getPage(browserKey);
466
+
467
+ const startTime = Date.now();
468
+
469
+ try {
470
+ await page.goto(url, {
471
+ waitUntil: 'networkidle0',
472
+ timeout: this.browserConfig.timeout
473
+ });
474
+
475
+ const title = await page.title();
476
+ const currentUrl = page.url();
477
+
478
+ return {
479
+ success: true,
480
+ action: 'navigate',
481
+ url: currentUrl,
482
+ originalUrl: url,
483
+ title,
484
+ loadTime: Date.now() - startTime,
485
+ message: `Navigated to ${url}`
486
+ };
487
+
488
+ } catch (error) {
489
+ throw new Error(`Navigation failed: ${error.message}`);
490
+ }
491
+ }
492
+
493
+ /**
494
+ * Click on element
495
+ * @private
496
+ */
497
+ async click(selector, browserKey) {
498
+ const page = await this.getPage(browserKey);
499
+
500
+ try {
501
+ await page.waitForSelector(selector, { timeout: 5000 });
502
+ await page.click(selector);
503
+
504
+ return {
505
+ success: true,
506
+ action: 'click',
507
+ selector,
508
+ message: `Clicked on ${selector}`
509
+ };
510
+
511
+ } catch (error) {
512
+ throw new Error(`Click failed on ${selector}: ${error.message}`);
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Type text into element
518
+ * @private
519
+ */
520
+ async type(selector, text, browserKey) {
521
+ const page = await this.getPage(browserKey);
522
+
523
+ try {
524
+ await page.waitForSelector(selector, { timeout: 5000 });
525
+ await page.focus(selector);
526
+ await page.keyboard.selectAll();
527
+ await page.type(selector, text);
528
+
529
+ return {
530
+ success: true,
531
+ action: 'type',
532
+ selector,
533
+ text,
534
+ textLength: text.length,
535
+ message: `Typed ${text.length} characters into ${selector}`
536
+ };
537
+
538
+ } catch (error) {
539
+ throw new Error(`Type failed on ${selector}: ${error.message}`);
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Wait for condition
545
+ * @private
546
+ */
547
+ async wait(action, browserKey) {
548
+ const page = await this.getPage(browserKey);
549
+
550
+ try {
551
+ if (action.waitTime) {
552
+ await page.waitForTimeout(action.waitTime);
553
+ return {
554
+ success: true,
555
+ action: 'wait',
556
+ waitTime: action.waitTime,
557
+ message: `Waited ${action.waitTime}ms`
558
+ };
559
+ }
560
+
561
+ if (action.waitSelector) {
562
+ const waitType = action.waitType || 'visible';
563
+ const options = { timeout: 10000 };
564
+
565
+ if (waitType === 'hidden') {
566
+ await page.waitForSelector(action.waitSelector, { ...options, hidden: true });
567
+ } else {
568
+ await page.waitForSelector(action.waitSelector, options);
569
+ }
570
+
571
+ return {
572
+ success: true,
573
+ action: 'wait',
574
+ waitSelector: action.waitSelector,
575
+ waitType,
576
+ message: `Waited for ${action.waitSelector} to be ${waitType}`
577
+ };
578
+ }
579
+
580
+ } catch (error) {
581
+ throw new Error(`Wait failed: ${error.message}`);
582
+ }
583
+ }
584
+
585
+ /**
586
+ * Take screenshot
587
+ * @private
588
+ */
589
+ async screenshot(outputPath, projectDir, browserKey, options = {}) {
590
+ const page = await this.getPage(browserKey);
591
+
592
+ try {
593
+ const fullPath = projectDir ? path.resolve(projectDir, outputPath) : path.resolve(outputPath);
594
+
595
+ // Ensure directory exists
596
+ const dir = path.dirname(fullPath);
597
+ await fs.mkdir(dir, { recursive: true });
598
+
599
+ const screenshotOptions = {
600
+ path: fullPath,
601
+ type: 'png'
602
+ };
603
+
604
+ if (options.fullPage) {
605
+ screenshotOptions.fullPage = true;
606
+ }
607
+
608
+ await page.screenshot(screenshotOptions);
609
+
610
+ const stats = await fs.stat(fullPath);
611
+
612
+ if (stats.size > this.maxScreenshotSize) {
613
+ throw new Error(`Screenshot too large: ${stats.size} bytes (max ${this.maxScreenshotSize})`);
614
+ }
615
+
616
+ return {
617
+ success: true,
618
+ action: 'screenshot',
619
+ outputPath: path.relative(projectDir || process.cwd(), fullPath),
620
+ size: stats.size,
621
+ fullPage: !!options.fullPage,
622
+ message: `Screenshot saved: ${outputPath}`
623
+ };
624
+
625
+ } catch (error) {
626
+ throw new Error(`Screenshot failed: ${error.message}`);
627
+ }
628
+ }
629
+
630
+ /**
631
+ * Extract data from elements
632
+ * @private
633
+ */
634
+ async extract(selector, attribute, browserKey) {
635
+ const page = await this.getPage(browserKey);
636
+
637
+ try {
638
+ await page.waitForSelector(selector, { timeout: 5000 });
639
+
640
+ let data;
641
+ if (attribute === 'text') {
642
+ data = await page.$eval(selector, el => el.textContent.trim());
643
+ } else {
644
+ data = await page.$eval(selector, (el, attr) => el.getAttribute(attr), attribute);
645
+ }
646
+
647
+ return {
648
+ success: true,
649
+ action: 'extract',
650
+ selector,
651
+ attribute,
652
+ data,
653
+ message: `Extracted ${attribute} from ${selector}`
654
+ };
655
+
656
+ } catch (error) {
657
+ throw new Error(`Extract failed on ${selector}: ${error.message}`);
658
+ }
659
+ }
660
+
661
+ /**
662
+ * Scroll page or element
663
+ * @private
664
+ */
665
+ async scroll(action, browserKey) {
666
+ const page = await this.getPage(browserKey);
667
+
668
+ try {
669
+ const { direction, pixels = 300, selector } = action;
670
+
671
+ if (selector) {
672
+ // Scroll specific element
673
+ await page.evaluate((sel, dir, px) => {
674
+ const element = document.querySelector(sel);
675
+ if (element) {
676
+ if (dir === 'down') element.scrollTop += px;
677
+ else if (dir === 'up') element.scrollTop -= px;
678
+ else if (dir === 'right') element.scrollLeft += px;
679
+ else if (dir === 'left') element.scrollLeft -= px;
680
+ }
681
+ }, selector, direction, pixels);
682
+ } else {
683
+ // Scroll page
684
+ await page.evaluate((dir, px) => {
685
+ if (dir === 'down') window.scrollBy(0, px);
686
+ else if (dir === 'up') window.scrollBy(0, -px);
687
+ else if (dir === 'right') window.scrollBy(px, 0);
688
+ else if (dir === 'left') window.scrollBy(-px, 0);
689
+ }, direction, pixels);
690
+ }
691
+
692
+ return {
693
+ success: true,
694
+ action: 'scroll',
695
+ direction,
696
+ pixels,
697
+ selector: selector || 'page',
698
+ message: `Scrolled ${direction} by ${pixels}px`
699
+ };
700
+
701
+ } catch (error) {
702
+ throw new Error(`Scroll failed: ${error.message}`);
703
+ }
704
+ }
705
+
706
+ /**
707
+ * Get or create browser instance
708
+ * @private
709
+ */
710
+ async getBrowser(browserKey) {
711
+ if (this.browserInstances.has(browserKey)) {
712
+ const instance = this.browserInstances.get(browserKey);
713
+ if (!instance.browser.isConnected()) {
714
+ this.browserInstances.delete(browserKey);
715
+ } else {
716
+ return instance.browser;
717
+ }
718
+ }
719
+
720
+ const browser = await puppeteer.launch({
721
+ headless: this.browserConfig.headless,
722
+ args: [
723
+ '--no-sandbox',
724
+ '--disable-setuid-sandbox',
725
+ '--disable-dev-shm-usage',
726
+ '--disable-gpu'
727
+ ]
728
+ });
729
+
730
+ const page = await browser.newPage();
731
+ await page.setViewport(this.browserConfig.viewport);
732
+ await page.setUserAgent(this.browserConfig.userAgent);
733
+
734
+ this.browserInstances.set(browserKey, {
735
+ browser,
736
+ page,
737
+ created: Date.now()
738
+ });
739
+
740
+ // Auto-cleanup after 10 minutes of inactivity
741
+ setTimeout(() => {
742
+ this.cleanupBrowser(browserKey);
743
+ }, 10 * 60 * 1000);
744
+
745
+ return browser;
746
+ }
747
+
748
+ /**
749
+ * Get page instance
750
+ * @private
751
+ */
752
+ async getPage(browserKey) {
753
+ await this.getBrowser(browserKey);
754
+ return this.browserInstances.get(browserKey).page;
755
+ }
756
+
757
+ /**
758
+ * Close browser instance
759
+ * @private
760
+ */
761
+ async closeBrowser(browserKey) {
762
+ const instance = this.browserInstances.get(browserKey);
763
+
764
+ if (instance) {
765
+ try {
766
+ await instance.browser.close();
767
+ this.browserInstances.delete(browserKey);
768
+
769
+ return {
770
+ success: true,
771
+ action: 'close',
772
+ browserKey,
773
+ message: 'Browser instance closed'
774
+ };
775
+ } catch (error) {
776
+ throw new Error(`Failed to close browser: ${error.message}`);
777
+ }
778
+ }
779
+
780
+ return {
781
+ success: true,
782
+ action: 'close',
783
+ browserKey,
784
+ message: 'Browser instance not found (already closed)'
785
+ };
786
+ }
787
+
788
+ /**
789
+ * Cleanup browser instance
790
+ * @private
791
+ */
792
+ async cleanupBrowser(browserKey) {
793
+ const instance = this.browserInstances.get(browserKey);
794
+
795
+ if (instance) {
796
+ try {
797
+ await instance.browser.close();
798
+ } catch (error) {
799
+ // Ignore cleanup errors
800
+ } finally {
801
+ this.browserInstances.delete(browserKey);
802
+ }
803
+ }
804
+ }
805
+
806
+ /**
807
+ * Check if URL is allowed
808
+ * @private
809
+ */
810
+ isAllowedUrl(url) {
811
+ try {
812
+ const urlObj = new URL(url);
813
+ const hostname = urlObj.hostname.toLowerCase();
814
+
815
+ // Check blocked domains
816
+ if (this.blockedDomains.some(blocked => hostname.includes(blocked.toLowerCase()))) {
817
+ return false;
818
+ }
819
+
820
+ // Check allowed domains if specified
821
+ if (this.allowedDomains && this.allowedDomains.length > 0) {
822
+ return this.allowedDomains.some(allowed => hostname.includes(allowed.toLowerCase()));
823
+ }
824
+
825
+ return true;
826
+ } catch {
827
+ return false;
828
+ }
829
+ }
830
+
831
+ /**
832
+ * Add operation to history
833
+ * @private
834
+ */
835
+ addToHistory(action, result, agentId) {
836
+ const historyEntry = {
837
+ timestamp: new Date().toISOString(),
838
+ agentId,
839
+ action: action.type,
840
+ selector: action.selector || action.url,
841
+ success: result.success,
842
+ loadTime: result.loadTime || 0
843
+ };
844
+
845
+ this.operationHistory.push(historyEntry);
846
+
847
+ // Keep only last 100 entries
848
+ if (this.operationHistory.length > 100) {
849
+ this.operationHistory = this.operationHistory.slice(-100);
850
+ }
851
+ }
852
+
853
+ /**
854
+ * Get supported actions for this tool
855
+ * @returns {Array<string>} Array of supported action names
856
+ */
857
+ getSupportedActions() {
858
+ return ['navigate', 'click', 'type', 'wait', 'screenshot', 'extract', 'scroll', 'close'];
859
+ }
860
+
861
+ /**
862
+ * Get parameter schema for validation
863
+ * @returns {Object} Parameter schema
864
+ */
865
+ getParameterSchema() {
866
+ return {
867
+ type: 'object',
868
+ properties: {
869
+ actions: {
870
+ type: 'array',
871
+ minItems: 1,
872
+ items: {
873
+ type: 'object',
874
+ properties: {
875
+ type: {
876
+ type: 'string',
877
+ enum: this.getSupportedActions()
878
+ },
879
+ url: { type: 'string' },
880
+ selector: { type: 'string' },
881
+ text: { type: 'string' },
882
+ outputPath: { type: 'string' },
883
+ waitTime: { type: 'integer' },
884
+ waitSelector: { type: 'string' },
885
+ attribute: { type: 'string' },
886
+ direction: { type: 'string' },
887
+ pixels: { type: 'integer' }
888
+ },
889
+ required: ['type']
890
+ }
891
+ }
892
+ },
893
+ required: ['actions']
894
+ };
895
+ }
896
+
897
+ /**
898
+ * Resource cleanup
899
+ * @param {string} operationId - Operation identifier
900
+ */
901
+ async cleanup(operationId) {
902
+ // Close all browser instances
903
+ for (const [browserKey] of this.browserInstances) {
904
+ await this.cleanupBrowser(browserKey);
905
+ }
906
+ }
907
+
908
+ /**
909
+ * Get operation history for debugging
910
+ * @returns {Array} Operation history
911
+ */
912
+ getOperationHistory(agentId = null) {
913
+ if (agentId) {
914
+ return this.operationHistory.filter(entry => entry.agentId === agentId);
915
+ }
916
+ return [...this.operationHistory];
917
+ }
918
+ }
919
+
920
+ export default BrowserTool;