UNPKG

@ejazullah/smart-browser-automation

Version:

A smart AI-driven browser automation library and REST API server using MCP (Model Context Protocol) and LangChain for multi-step task execution. Includes both programmatic library usage and HTTP API server for remote automation.

89 lines (73 loc) โ€ข 2.91 kB
// Simple JavaScript example using the smart-browser-automation package import { SmartBrowserAutomation, HuggingFaceConfig, OllamaConfig, OpenAIConfig } from '@ejazullah/smart-browser-automation'; async function simpleExample() { console.log('๐Ÿš€ Starting simple JavaScript example...'); // Create configuration - choose one of these options: // Option 1: HuggingFace const hfConfig = new HuggingFaceConfig('your-huggingface-api-key'); // Option 2: Ollama (local) // const ollamaConfig = new OllamaConfig('http://localhost:11434', 'llama2'); // Option 3: OpenAI // const openaiConfig = new OpenAIConfig('your-openai-api-key', 'gpt-4'); // Create automation instance const automation = new SmartBrowserAutomation({ maxSteps: 5, // Maximum steps to execute temperature: 0.1 // Lower = more focused, higher = more creative }); try { console.log('๐Ÿ“ก Initializing automation...'); // Initialize with your MCP server and Chrome DevTools endpoints await automation.initialize( hfConfig, // LLM configuration 'http://localhost:3000', // MCP server endpoint 'ws://localhost:9222' // Chrome DevTools WebSocket ); console.log('โœ… Automation initialized successfully!'); // Define progress callback (optional) const progressCallback = (update) => { console.log(`๐Ÿ“Š ${update.type}: ${update.message}`); if (update.step) { console.log(` Step: ${update.step}`); } if (update.toolName) { console.log(` Tool: ${update.toolName}`); } }; // Execute a simple task console.log('๐ŸŽฏ Executing task...'); const result = await automation.executeTask( "Navigate to https://google.com and search for 'JavaScript tutorials'", { verbose: true, // Show detailed logs onProgress: progressCallback // Progress updates } ); // Display results console.log('๐ŸŽ‰ Task completed!'); console.log('๐Ÿ“ˆ Results:', { success: result.success, stepsCompleted: result.steps, totalResults: result.results.length, wasCompleted: result.completed }); // Show each step that was executed result.results.forEach((step, index) => { console.log(`\n๐Ÿ“ Step ${index + 1}:`); console.log(` Tool: ${step.tool || 'N/A'}`); console.log(` Type: ${step.type || 'tool-execution'}`); if (step.response) { console.log(` Response: ${step.response.substring(0, 100)}...`); } }); } catch (error) { console.error('โŒ Error occurred:', error.message); console.error('Stack trace:', error.stack); } finally { // Always clean up console.log('๐Ÿงน Cleaning up...'); await automation.close(); console.log('โœ… Cleanup completed'); } } // Run the example simpleExample();