Spaces:
Running
Running
Download src/browser_automation_agent/agent/base.py from sitcod3/dracull-agent: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/sitcod3/dracull-agent/resolve/main/src/browser_automation_agent/agent/base.py
- Command line
-
hf download hf://spaces/sitcod3/dracull-agent/src/browser_automation_agent/agent/base.py
-
curl -L -o base.py https://huggingface.co/spaces/sitcod3/dracull-agent/resolve/main/src/browser_automation_agent/agent/base.py
3.25 kB
| """Base agent classes for PydanticAI integration.""" | |
| from collections.abc import AsyncIterator | |
| from typing import Any, Generic, TypeVar | |
| from ..llm.provider import llm_provider | |
| T = TypeVar("T") | |
| class BaseAgent(Generic[T]): | |
| """Base class for PydanticAI agents.""" | |
| def __init__( | |
| self, | |
| system_prompt: str, | |
| output_type: type[T] | None = None, | |
| tools: list[Any] | None = None, | |
| model: str | None = None, | |
| ) -> None: | |
| self.agent = llm_provider.create_agent( | |
| system_prompt=system_prompt, | |
| output_type=output_type, | |
| tools=tools, | |
| ) | |
| if model: | |
| self.agent.model = model | |
| async def run(self, prompt: str, **kwargs: Any) -> Any: | |
| """Run the agent with a prompt.""" | |
| return await self.agent.run(prompt, **kwargs) | |
| async def run_stream(self, prompt: str, **kwargs: Any) -> AsyncIterator[Any]: | |
| """Run the agent with streaming.""" | |
| async with self.agent.run_stream(prompt, **kwargs) as result: | |
| async for chunk in result.stream(): | |
| yield chunk | |
| class NavigationAgent(BaseAgent[str]): | |
| """Agent for planning navigation steps.""" | |
| def __init__(self) -> None: | |
| super().__init__( | |
| system_prompt="""You are a web navigation expert. Given a goal and current page state, | |
| determine the best navigation action to take. Consider: | |
| 1. Direct URL navigation if the target is known | |
| 2. Search and click if the target needs to be found | |
| 3. Form submission for login/search | |
| 4. Pagination handling | |
| Return a single action with reasoning.""", | |
| output_type=str, | |
| ) | |
| class ExtractionAgent(BaseAgent[dict[str, Any]]): | |
| """Agent for extracting structured data from pages.""" | |
| def __init__(self) -> None: | |
| super().__init__( | |
| system_prompt="""You are a data extraction expert. Given HTML content or page state, | |
| extract the requested information in a structured format. Handle: | |
| 1. Tables and lists | |
| 2. Form data | |
| 3. Product information | |
| 4. Article content | |
| 5. Dynamic content | |
| Return JSON with the extracted data.""", | |
| output_type=dict[str, Any], | |
| ) | |
| class DecisionAgent(BaseAgent[bool]): | |
| """Agent for making decisions during automation.""" | |
| def __init__(self) -> None: | |
| super().__init__( | |
| system_prompt="""You are a decision-making agent for browser automation. | |
| Given the current state and options, make a boolean decision. | |
| Consider success criteria, error states, and alternative paths. | |
| Return true/false with brief reasoning.""", | |
| output_type=bool, | |
| ) | |
| class RecoveryAgent(BaseAgent[str]): | |
| """Agent for error recovery strategies.""" | |
| def __init__(self) -> None: | |
| super().__init__( | |
| system_prompt="""You are an error recovery agent. Given an error and page state, | |
| suggest a recovery action: | |
| 1. Retry with different selector | |
| 2. Wait and retry | |
| 3. Navigate back and try alternative path | |
| 4. Refresh page | |
| 5. Switch to different approach | |
| 6. Escalate to human (HITL) | |
| Return the recommended action as a string.""", | |
| output_type=str, | |
| ) |