| 
 | 1 | +# Browser Use Example  | 
 | 2 | + | 
 | 3 | +This example demonstrates how to use AStack to build a browser automation pipeline that can understand natural language intents, execute web browsing tasks, and return formatted results.  | 
 | 4 | + | 
 | 5 | +## Overview  | 
 | 6 | + | 
 | 7 | +The Browser Use example showcases:  | 
 | 8 | + | 
 | 9 | +1. Natural language task planning using an LLM-powered agent  | 
 | 10 | +2. Browser automation with Playwright  | 
 | 11 | +3. State management for tracking browser context  | 
 | 12 | +4. Result processing and DOM snapshot filtering  | 
 | 13 | +5. Automatic resource cleanup  | 
 | 14 | +6. Pipeline-based execution flow  | 
 | 15 | + | 
 | 16 | +## Pipeline Architecture  | 
 | 17 | + | 
 | 18 | +The pipeline consists of several components working together:  | 
 | 19 | + | 
 | 20 | +```mermaid  | 
 | 21 | +graph TD  | 
 | 22 | + %% Components  | 
 | 23 | + Planner[PlanningAgent]  | 
 | 24 | + Browser[BrowserAgent]  | 
 | 25 | + BrowserState[BrowserStateComponent]  | 
 | 26 | + Formatter[ResultFormatter]  | 
 | 27 | +   | 
 | 28 | + %% Define ports as nodes to show this-connection  | 
 | 29 | + Browser_Result["browser.result"]  | 
 | 30 | + Browser_Stop["browser.stop"]  | 
 | 31 | +   | 
 | 32 | + %% Connections  | 
 | 33 | + Planner -->|plan| Browser  | 
 | 34 | + Browser -->|stateUpdate| BrowserState  | 
 | 35 | + BrowserState -->|broadcast| Browser  | 
 | 36 | + Browser --> Browser_Result  | 
 | 37 | + Browser_Result -->|data| Formatter  | 
 | 38 | + Browser_Result -->|"Self-Connection"| Browser_Stop  | 
 | 39 | + Browser_Stop --> Browser  | 
 | 40 | +   | 
 | 41 | + %% Style  | 
 | 42 | + classDef component fill:#f9f9f9,stroke:#333,stroke-width:1px;  | 
 | 43 | + classDef port fill:#e1f5fe,stroke:#0288d1,stroke-width:1px;  | 
 | 44 | + classDef selfconnect fill:#ffecb3,stroke:#ffa000,stroke-width:1px;  | 
 | 45 | + class Planner,Browser,BrowserState,Formatter component;  | 
 | 46 | + class Browser_Result,Browser_Stop port;  | 
 | 47 | +   | 
 | 48 | + %% Notes  | 
 | 49 | + subgraph Pipeline Flow  | 
 | 50 | + direction TB  | 
 | 51 | + note1[User Intent] -->|Input| Planner  | 
 | 52 | + Formatter -->|Output| note2[Formatted Result]  | 
 | 53 | + end  | 
 | 54 | +   | 
 | 55 | + %% Style for this-connection  | 
 | 56 | + linkStyle 4 stroke:#F44336,stroke-width:2px,stroke-dasharray: 5 5;  | 
 | 57 | +```  | 
 | 58 | + | 
 | 59 | +### Component Responsibilities  | 
 | 60 | + | 
 | 61 | +- **PlanningAgent**: Converts natural language intent into a step-by-step plan for browser automation  | 
 | 62 | +- **BrowserAgent**: Executes browser automation actions using Playwright  | 
 | 63 | +- **BrowserStateComponent**: Manages and broadcasts browser state (DOM, URL, etc.) to other components  | 
 | 64 | +- **ResultFormatter**: Filters DOM snapshot data and passes through relevant results  | 
 | 65 | + | 
 | 66 | +### Data Flow  | 
 | 67 | + | 
 | 68 | +1. User intent is sent to the `PlanningAgent`  | 
 | 69 | +2. `PlanningAgent` creates a plan and sends it to `BrowserAgent`  | 
 | 70 | +3. `BrowserAgent` executes browser actions and sends state updates to `BrowserStateComponent`  | 
 | 71 | +4. `BrowserStateComponent` broadcasts state to interested components  | 
 | 72 | +5. `BrowserAgent` sends result data to `ResultFormatter` and signals itself to close  | 
 | 73 | +6. `ResultFormatter` filters out DOM snapshot data and passes through relevant results  | 
 | 74 | +7. Pipeline returns the formatted result  | 
 | 75 | + | 
 | 76 | +## Usage  | 
 | 77 | + | 
 | 78 | +```bash  | 
 | 79 | +# Install dependencies  | 
 | 80 | +pnpm install  | 
 | 81 | + | 
 | 82 | +# Run the example with a custom query  | 
 | 83 | +DEEPSEEK_API_KEY=your_api_key pnpm start "your query here"  | 
 | 84 | + | 
 | 85 | +# Or use the default query  | 
 | 86 | +DEEPSEEK_API_KEY=your_api_key pnpm start  | 
 | 87 | +```  | 
 | 88 | + | 
 | 89 | +## Key Design Patterns  | 
 | 90 | + | 
 | 91 | +- **"Everything is a Component" Principle**: All functionality is encapsulated in composable components  | 
 | 92 | +- **Zero-Adapter Design**: Components communicate directly through ports without adapters  | 
 | 93 | +- **Automatic Resource Management**: Browser resources are automatically cleaned up after task completion  | 
 | 94 | +- **Declarative Pipeline Configuration**: Components and connections are defined declaratively  | 
 | 95 | + | 
 | 96 | +## Extending the Example  | 
 | 97 | + | 
 | 98 | +You can extend this example by:  | 
 | 99 | + | 
 | 100 | +1. Adding new browser tools (e.g., form filling, file downloads)  | 
 | 101 | +2. Implementing error recovery strategies  | 
 | 102 | +3. Adding authentication support  | 
 | 103 | +4. Creating specialized components for specific websites  | 
 | 104 | +5. Adding visualization for real-time monitoring  | 
0 commit comments