from typing import Dict, Any
from browser_use import Agent as BrowserAgent
from langchain_openai import ChatOpenAI
from .base import Tool

class BrowserUseTool(Tool):
    """Tool for advanced web browsing using browser-use"""
    
    def __init__(self, openai_api_key: str):
        super().__init__(
            name="browser_use",
            description="Advanced web browsing and interaction with websites"
        )
        self.openai_api_key = openai_api_key
        
    async def execute(self, task: str) -> Dict[str, Any]:
        """Execute a web browsing task using browser-use"""
        try:
            # Initialize browser-use agent
            agent = BrowserAgent(
                task=task,
                llm=ChatOpenAI(
                    api_key=self.openai_api_key,
                    model="gpt-4"
                )
            )
            
            # Execute the browsing task
            result = await agent.run()
            
            return {
                "success": True,
                "result": result
            }
            
        except Exception as e:
            return {
                "success": False,
                "error": f"Error during web browsing: {str(e)}"
            }
