@darbotlabs/darbot-browser-mcp 0.1.1 → 1.3.0

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 +1 -1
  2. package/README.md +249 -158
  3. package/cli.js +1 -1
  4. package/config.d.ts +77 -1
  5. package/index.d.ts +1 -1
  6. package/index.js +1 -1
  7. package/lib/ai/context.js +150 -0
  8. package/lib/ai/guardrails.js +382 -0
  9. package/lib/ai/integration.js +397 -0
  10. package/lib/ai/intent.js +237 -0
  11. package/lib/ai/manualPromise.js +111 -0
  12. package/lib/ai/memory.js +273 -0
  13. package/lib/ai/ml-scorer.js +265 -0
  14. package/lib/ai/orchestrator-tools.js +292 -0
  15. package/lib/ai/orchestrator.js +473 -0
  16. package/lib/ai/planner.js +300 -0
  17. package/lib/ai/reporter.js +493 -0
  18. package/lib/ai/workflow.js +407 -0
  19. package/lib/auth/apiKeyAuth.js +46 -0
  20. package/lib/auth/entraAuth.js +110 -0
  21. package/lib/auth/entraJwtVerifier.js +117 -0
  22. package/lib/auth/index.js +210 -0
  23. package/lib/auth/managedIdentityAuth.js +175 -0
  24. package/lib/auth/mcpOAuthProvider.js +186 -0
  25. package/lib/auth/tunnelAuth.js +120 -0
  26. package/lib/browserContextFactory.js +1 -1
  27. package/lib/browserServer.js +1 -1
  28. package/lib/cdpRelay.js +2 -2
  29. package/lib/common.js +68 -0
  30. package/lib/config.js +62 -3
  31. package/lib/connection.js +1 -1
  32. package/lib/context.js +1 -1
  33. package/lib/fileUtils.js +1 -1
  34. package/lib/guardrails.js +382 -0
  35. package/lib/health.js +178 -0
  36. package/lib/httpServer.js +1 -1
  37. package/lib/index.js +1 -1
  38. package/lib/javascript.js +1 -1
  39. package/lib/manualPromise.js +1 -1
  40. package/lib/memory.js +273 -0
  41. package/lib/openapi.js +373 -0
  42. package/lib/orchestrator.js +473 -0
  43. package/lib/package.js +1 -1
  44. package/lib/pageSnapshot.js +17 -2
  45. package/lib/planner.js +302 -0
  46. package/lib/program.js +17 -5
  47. package/lib/reporter.js +493 -0
  48. package/lib/resources/resource.js +1 -1
  49. package/lib/server.js +5 -3
  50. package/lib/tab.js +1 -1
  51. package/lib/tools/ai-native.js +298 -0
  52. package/lib/tools/autonomous.js +147 -0
  53. package/lib/tools/clock.js +183 -0
  54. package/lib/tools/common.js +1 -1
  55. package/lib/tools/console.js +1 -1
  56. package/lib/tools/diagnostics.js +132 -0
  57. package/lib/tools/dialogs.js +1 -1
  58. package/lib/tools/emulation.js +155 -0
  59. package/lib/tools/files.js +1 -1
  60. package/lib/tools/install.js +1 -1
  61. package/lib/tools/keyboard.js +1 -1
  62. package/lib/tools/navigate.js +1 -1
  63. package/lib/tools/network.js +1 -1
  64. package/lib/tools/pageSnapshot.js +58 -0
  65. package/lib/tools/pdf.js +1 -1
  66. package/lib/tools/profiles.js +76 -25
  67. package/lib/tools/screenshot.js +1 -1
  68. package/lib/tools/scroll.js +93 -0
  69. package/lib/tools/snapshot.js +1 -1
  70. package/lib/tools/storage.js +328 -0
  71. package/lib/tools/tab.js +16 -0
  72. package/lib/tools/tabs.js +1 -1
  73. package/lib/tools/testing.js +1 -1
  74. package/lib/tools/tool.js +1 -1
  75. package/lib/tools/utils.js +1 -1
  76. package/lib/tools/vision.js +1 -1
  77. package/lib/tools/wait.js +1 -1
  78. package/lib/tools.js +22 -1
  79. package/lib/transport.js +251 -31
  80. package/package.json +54 -21
@@ -0,0 +1,493 @@
1
+ /**
2
+ * Copyright (c) DarbotLabs.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import fs from 'node:fs';
17
+ import path from 'node:path';
18
+ import debug from 'debug';
19
+ const log = debug('darbot:reporter');
20
+ /**
21
+ * Report generator for autonomous crawling sessions
22
+ */
23
+ export class CrawlReporter {
24
+ config;
25
+ report;
26
+ errors = [];
27
+ constructor(sessionId, startUrl, goal, config = {}) {
28
+ this.config = {
29
+ outputDir: path.join(process.cwd(), '.darbot', 'reports'),
30
+ generateHTML: true,
31
+ generateJSON: true,
32
+ includeScreenshots: true,
33
+ ...config
34
+ };
35
+ this.report = {
36
+ sessionId,
37
+ startTime: Date.now(),
38
+ endTime: 0,
39
+ startUrl,
40
+ goal,
41
+ stats: {
42
+ pagesVisited: 0,
43
+ totalLinks: 0,
44
+ maxDepth: 0,
45
+ duration: 0,
46
+ screenshots: 0,
47
+ errors: 0
48
+ },
49
+ states: [],
50
+ errors: [],
51
+ graph: {
52
+ nodes: [],
53
+ edges: []
54
+ }
55
+ };
56
+ this.ensureOutputDirectory();
57
+ }
58
+ /**
59
+ * Add a crawled state to the report
60
+ */
61
+ addState(state) {
62
+ this.report.states.push(state);
63
+ this.updateStats();
64
+ this.updateGraph(state);
65
+ }
66
+ /**
67
+ * Add an error to the report
68
+ */
69
+ addError(url, error) {
70
+ const errorEntry = { url, error, timestamp: Date.now() };
71
+ this.errors.push(errorEntry);
72
+ this.report.errors.push(errorEntry);
73
+ this.report.stats.errors++;
74
+ }
75
+ /**
76
+ * Finalize and generate the report
77
+ */
78
+ async generateReport() {
79
+ this.report.endTime = Date.now();
80
+ this.report.stats.duration = this.report.endTime - this.report.startTime;
81
+ const reportDir = path.join(this.config.outputDir, this.report.sessionId);
82
+ if (!fs.existsSync(reportDir))
83
+ fs.mkdirSync(reportDir, { recursive: true });
84
+ let reportPath = '';
85
+ // Generate JSON report
86
+ if (this.config.generateJSON) {
87
+ const jsonPath = path.join(reportDir, 'report.json');
88
+ await fs.promises.writeFile(jsonPath, JSON.stringify(this.report, null, 2));
89
+ log('Generated JSON report:', jsonPath);
90
+ }
91
+ // Generate HTML report
92
+ if (this.config.generateHTML) {
93
+ reportPath = path.join(reportDir, 'report.html');
94
+ const htmlContent = this.generateHTML();
95
+ await fs.promises.writeFile(reportPath, htmlContent);
96
+ log('Generated HTML report:', reportPath);
97
+ }
98
+ // Copy screenshots to report directory if enabled
99
+ if (this.config.includeScreenshots)
100
+ await this.copyScreenshots(reportDir);
101
+ return reportPath;
102
+ }
103
+ /**
104
+ * Update statistics based on current states
105
+ */
106
+ updateStats() {
107
+ const stats = this.report.stats;
108
+ stats.pagesVisited = this.report.states.filter(s => s.visited).length;
109
+ stats.totalLinks = this.report.states.reduce((sum, state) => sum + state.links.length, 0);
110
+ stats.maxDepth = Math.max(...this.report.states.map(s => this.getDepthFromUrl(s.url)));
111
+ stats.screenshots = this.report.states.filter(s => s.screenshot).length;
112
+ }
113
+ /**
114
+ * Update the graph representation
115
+ */
116
+ updateGraph(state) {
117
+ const { nodes, edges } = this.report.graph;
118
+ // Add node if not exists
119
+ if (!nodes.find(n => n.url === state.url)) {
120
+ nodes.push({
121
+ id: state.stateHash,
122
+ label: state.title || state.url,
123
+ url: state.url,
124
+ depth: this.getDepthFromUrl(state.url),
125
+ visited: state.visited
126
+ });
127
+ }
128
+ // Add edges for links
129
+ state.links.forEach(link => {
130
+ const linkHash = this.hashUrl(link);
131
+ if (!edges.find(e => e.from === state.stateHash && e.to === linkHash)) {
132
+ edges.push({
133
+ from: state.stateHash,
134
+ to: linkHash
135
+ });
136
+ }
137
+ });
138
+ }
139
+ /**
140
+ * Generate HTML report content
141
+ */
142
+ generateHTML() {
143
+ const template = this.config.templatePath ?
144
+ this.loadTemplate() :
145
+ this.getDefaultTemplate();
146
+ return template
147
+ .replace('{{SESSION_ID}}', this.report.sessionId)
148
+ .replace('{{START_URL}}', this.report.startUrl)
149
+ .replace('{{GOAL}}', this.report.goal || 'Autonomous exploration')
150
+ .replace('{{START_TIME}}', new Date(this.report.startTime).toISOString())
151
+ .replace('{{END_TIME}}', new Date(this.report.endTime).toISOString())
152
+ .replace('{{DURATION}}', this.formatDuration(this.report.stats.duration))
153
+ .replace('{{PAGES_VISITED}}', this.report.stats.pagesVisited.toString())
154
+ .replace('{{TOTAL_LINKS}}', this.report.stats.totalLinks.toString())
155
+ .replace('{{MAX_DEPTH}}', this.report.stats.maxDepth.toString())
156
+ .replace('{{SCREENSHOTS}}', this.report.stats.screenshots.toString())
157
+ .replace('{{ERRORS}}', this.report.stats.errors.toString())
158
+ .replace('{{STATES_TABLE}}', this.generateStatesTable())
159
+ .replace('{{ERRORS_TABLE}}', this.generateErrorsTable())
160
+ .replace('{{GRAPH_DATA}}', JSON.stringify(this.report.graph))
161
+ .replace('{{SCREENSHOT_GALLERY}}', this.generateScreenshotGallery());
162
+ }
163
+ /**
164
+ * Load custom template
165
+ */
166
+ loadTemplate() {
167
+ try {
168
+ return fs.readFileSync(this.config.templatePath, 'utf-8');
169
+ }
170
+ catch (error) {
171
+ log('Failed to load template, using default:', error);
172
+ return this.getDefaultTemplate();
173
+ }
174
+ }
175
+ /**
176
+ * Get default HTML template
177
+ */
178
+ getDefaultTemplate() {
179
+ return `<!DOCTYPE html>
180
+ <html lang="en">
181
+ <head>
182
+ <meta charset="UTF-8">
183
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
184
+ <title>Darbot Crawl Report - {{SESSION_ID}}</title>
185
+ <style>
186
+ body {
187
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
188
+ line-height: 1.6;
189
+ color: #333;
190
+ max-width: 1200px;
191
+ margin: 0 auto;
192
+ padding: 20px;
193
+ background: #f5f5f5;
194
+ }
195
+ .header {
196
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
197
+ color: white;
198
+ padding: 30px;
199
+ border-radius: 10px;
200
+ margin-bottom: 30px;
201
+ }
202
+ .stats-grid {
203
+ display: grid;
204
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
205
+ gap: 20px;
206
+ margin-bottom: 30px;
207
+ }
208
+ .stat-card {
209
+ background: white;
210
+ padding: 20px;
211
+ border-radius: 8px;
212
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
213
+ text-align: center;
214
+ }
215
+ .stat-number {
216
+ font-size: 2em;
217
+ font-weight: bold;
218
+ color: #667eea;
219
+ }
220
+ .section {
221
+ background: white;
222
+ margin: 20px 0;
223
+ padding: 25px;
224
+ border-radius: 8px;
225
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
226
+ }
227
+ .section h2 {
228
+ color: #333;
229
+ border-bottom: 2px solid #667eea;
230
+ padding-bottom: 10px;
231
+ }
232
+ table {
233
+ width: 100%;
234
+ border-collapse: collapse;
235
+ margin-top: 15px;
236
+ }
237
+ th, td {
238
+ padding: 12px;
239
+ text-align: left;
240
+ border-bottom: 1px solid #ddd;
241
+ }
242
+ th {
243
+ background-color: #f8f9fa;
244
+ font-weight: 600;
245
+ }
246
+ tr:hover {
247
+ background-color: #f5f5f5;
248
+ }
249
+ .url {
250
+ color: #667eea;
251
+ text-decoration: none;
252
+ word-break: break-all;
253
+ }
254
+ .url:hover {
255
+ text-decoration: underline;
256
+ }
257
+ .error {
258
+ color: #e74c3c;
259
+ }
260
+ .screenshot-gallery {
261
+ display: grid;
262
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
263
+ gap: 20px;
264
+ margin-top: 20px;
265
+ }
266
+ .screenshot-item {
267
+ border: 1px solid #ddd;
268
+ border-radius: 8px;
269
+ overflow: hidden;
270
+ }
271
+ .screenshot-item img {
272
+ width: 100%;
273
+ height: 200px;
274
+ object-fit: cover;
275
+ }
276
+ .screenshot-info {
277
+ padding: 15px;
278
+ background: #f8f9fa;
279
+ }
280
+ .graph-container {
281
+ height: 400px;
282
+ border: 1px solid #ddd;
283
+ border-radius: 8px;
284
+ background: white;
285
+ display: flex;
286
+ align-items: center;
287
+ justify-content: center;
288
+ color: #666;
289
+ }
290
+ </style>
291
+ </head>
292
+ <body>
293
+ <div class="header">
294
+ <h1>Darbot Crawl Report</h1>
295
+ <p><strong>Session:</strong> {{SESSION_ID}}</p>
296
+ <p><strong>Start URL:</strong> <a href="{{START_URL}}" class="url" style="color: white;">{{START_URL}}</a></p>
297
+ <p><strong>Goal:</strong> {{GOAL}}</p>
298
+ <p><strong>Duration:</strong> {{DURATION}} ({{START_TIME}} - {{END_TIME}})</p>
299
+ </div>
300
+
301
+ <div class="stats-grid">
302
+ <div class="stat-card">
303
+ <div class="stat-number">{{PAGES_VISITED}}</div>
304
+ <div>Pages Visited</div>
305
+ </div>
306
+ <div class="stat-card">
307
+ <div class="stat-number">{{TOTAL_LINKS}}</div>
308
+ <div>Total Links</div>
309
+ </div>
310
+ <div class="stat-card">
311
+ <div class="stat-number">{{MAX_DEPTH}}</div>
312
+ <div>Max Depth</div>
313
+ </div>
314
+ <div class="stat-card">
315
+ <div class="stat-number">{{SCREENSHOTS}}</div>
316
+ <div>Screenshots</div>
317
+ </div>
318
+ <div class="stat-card">
319
+ <div class="stat-number">{{ERRORS}}</div>
320
+ <div>Errors</div>
321
+ </div>
322
+ </div>
323
+
324
+ <div class="section">
325
+ <h2>Crawled Pages</h2>
326
+ {{STATES_TABLE}}
327
+ </div>
328
+
329
+ <div class="section">
330
+ <h2>[FAILED] Errors</h2>
331
+ {{ERRORS_TABLE}}
332
+ </div>
333
+
334
+ <div class="section">
335
+ <h2>🕸️ Site Graph</h2>
336
+ <div class="graph-container">
337
+ Graph visualization would be rendered here with a library like D3.js or vis.js
338
+ <br>
339
+ Graph data: {{GRAPH_DATA}}
340
+ </div>
341
+ </div>
342
+
343
+ <div class="section">
344
+ <h2>Screenshot Gallery</h2>
345
+ {{SCREENSHOT_GALLERY}}
346
+ </div>
347
+ </body>
348
+ </html>`;
349
+ }
350
+ /**
351
+ * Generate states table HTML
352
+ */
353
+ generateStatesTable() {
354
+ if (this.report.states.length === 0)
355
+ return '<p>No pages visited.</p>';
356
+ let html = `<table>
357
+ <thead>
358
+ <tr>
359
+ <th>URL</th>
360
+ <th>Title</th>
361
+ <th>Timestamp</th>
362
+ <th>Links</th>
363
+ <th>Screenshot</th>
364
+ </tr>
365
+ </thead>
366
+ <tbody>`;
367
+ this.report.states.forEach(state => {
368
+ html += `
369
+ <tr>
370
+ <td><a href="${state.url}" class="url" target="_blank">${state.url}</a></td>
371
+ <td>${state.title || 'Untitled'}</td>
372
+ <td>${new Date(state.timestamp).toLocaleString()}</td>
373
+ <td>${state.links.length}</td>
374
+ <td>${state.screenshot ? '[PENDING VALIDATION]' : '[FAILED]'}</td>
375
+ </tr>`;
376
+ });
377
+ html += '</tbody></table>';
378
+ return html;
379
+ }
380
+ /**
381
+ * Generate errors table HTML
382
+ */
383
+ generateErrorsTable() {
384
+ if (this.report.errors.length === 0)
385
+ return '<p>No errors encountered during crawling.</p>';
386
+ let html = `<table>
387
+ <thead>
388
+ <tr>
389
+ <th>URL</th>
390
+ <th>Error</th>
391
+ <th>Timestamp</th>
392
+ </tr>
393
+ </thead>
394
+ <tbody>`;
395
+ this.report.errors.forEach(error => {
396
+ html += `
397
+ <tr>
398
+ <td><a href="${error.url}" class="url" target="_blank">${error.url}</a></td>
399
+ <td class="error">${error.error}</td>
400
+ <td>${new Date(error.timestamp).toLocaleString()}</td>
401
+ </tr>`;
402
+ });
403
+ html += '</tbody></table>';
404
+ return html;
405
+ }
406
+ /**
407
+ * Generate screenshot gallery HTML
408
+ */
409
+ generateScreenshotGallery() {
410
+ const statesWithScreenshots = this.report.states.filter(s => s.screenshot);
411
+ if (statesWithScreenshots.length === 0)
412
+ return '<p>No screenshots available.</p>';
413
+ let html = '<div class="screenshot-gallery">';
414
+ statesWithScreenshots.forEach(state => {
415
+ const screenshotName = path.basename(state.screenshot);
416
+ html += `
417
+ <div class="screenshot-item">
418
+ <img src="screenshots/${screenshotName}" alt="Screenshot of ${state.title || state.url}">
419
+ <div class="screenshot-info">
420
+ <strong>${state.title || 'Untitled'}</strong><br>
421
+ <a href="${state.url}" class="url" target="_blank">${state.url}</a><br>
422
+ <small>${new Date(state.timestamp).toLocaleString()}</small>
423
+ </div>
424
+ </div>`;
425
+ });
426
+ html += '</div>';
427
+ return html;
428
+ }
429
+ /**
430
+ * Copy screenshots to report directory
431
+ */
432
+ async copyScreenshots(reportDir) {
433
+ const screenshotsDir = path.join(reportDir, 'screenshots');
434
+ if (!fs.existsSync(screenshotsDir))
435
+ fs.mkdirSync(screenshotsDir, { recursive: true });
436
+ for (const state of this.report.states) {
437
+ if (state.screenshot && fs.existsSync(state.screenshot)) {
438
+ const fileName = path.basename(state.screenshot);
439
+ const destPath = path.join(screenshotsDir, fileName);
440
+ try {
441
+ await fs.promises.copyFile(state.screenshot, destPath);
442
+ }
443
+ catch (error) {
444
+ log('Failed to copy screenshot:', error);
445
+ }
446
+ }
447
+ }
448
+ }
449
+ /**
450
+ * Ensure output directory exists
451
+ */
452
+ ensureOutputDirectory() {
453
+ if (!fs.existsSync(this.config.outputDir))
454
+ fs.mkdirSync(this.config.outputDir, { recursive: true });
455
+ }
456
+ /**
457
+ * Format duration in human-readable format
458
+ */
459
+ formatDuration(ms) {
460
+ const seconds = Math.floor(ms / 1000);
461
+ const minutes = Math.floor(seconds / 60);
462
+ const hours = Math.floor(minutes / 60);
463
+ if (hours > 0)
464
+ return `${hours}h ${minutes % 60}m ${seconds % 60}s`;
465
+ else if (minutes > 0)
466
+ return `${minutes}m ${seconds % 60}s`;
467
+ else
468
+ return `${seconds}s`;
469
+ }
470
+ /**
471
+ * Get depth from URL (simple heuristic)
472
+ */
473
+ getDepthFromUrl(url) {
474
+ try {
475
+ return new URL(url).pathname.split('/').filter(segment => segment.length > 0).length;
476
+ }
477
+ catch {
478
+ return 0;
479
+ }
480
+ }
481
+ /**
482
+ * Simple hash function for URLs
483
+ */
484
+ hashUrl(url) {
485
+ let hash = 0;
486
+ for (let i = 0; i < url.length; i++) {
487
+ const char = url.charCodeAt(i);
488
+ hash = ((hash << 5) - hash) + char;
489
+ hash = hash & hash;
490
+ }
491
+ return Math.abs(hash).toString(16);
492
+ }
493
+ }