File size: 24,743 Bytes
a37e6db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | """
Browser Automation Tool
Provides Chrome DevTools Protocol (CDP) control via Playwright for:
- Web scraping and data extraction
- Form filling and submission
- Multi-step web workflows
- Screenshot capture
- Browser-based testing
- PDF generation
- Network interception
Governance Integration:
- All browser actions require INTERN+ maturity level
- Full audit trail via browser_audit table
- Agent execution tracking for all browser sessions
Refactored to use standardized decorators and service factory.
"""
import asyncio
import base64
from datetime import datetime
from typing import Any, Dict, Optional
import uuid
from playwright.async_api import Browser, BrowserContext, Page, Playwright, async_playwright
from sqlalchemy.orm import Session
from core.agent_context_resolver import AgentContextResolver
from core.feature_flags import FeatureFlags
from core.models import AgentExecution
from core.service_factory import ServiceFactory
from core.structured_logger import get_logger
import os
logger = get_logger(__name__)
# Feature flags
BROWSER_HEADLESS = os.getenv("BROWSER_HEADLESS", "true").lower() == "true"
class BrowserSession:
"""
Managed browser session with context and page tracking.
Each session represents a unique browser instance that can be
reused across multiple operations for maintaining state (cookies,
localStorage, etc.).
"""
def __init__(
self,
session_id: str,
user_id: str,
agent_id: Optional[str] = None,
headless: bool = True,
browser_type: str = "chromium"
):
self.session_id = session_id
self.user_id = user_id
self.agent_id = agent_id
self.headless = headless
self.browser_type = browser_type
self.playwright: Optional[Playwright] = None
self.browser: Optional[Browser] = None
self.context: Optional[BrowserContext] = None
self.page: Optional[Page] = None
self.created_at = datetime.now()
self.last_used = datetime.now()
async def start(self):
"""Start the browser session."""
try:
self.playwright = await async_playwright().start()
# Select browser type
if self.browser_type == "firefox":
self.browser = await self.playwright.firefox.launch(headless=self.headless)
elif self.browser_type == "webkit":
self.browser = await self.playwright.webkit.launch(headless=self.headless)
else: # chromium (default)
self.browser = await self.playwright.chromium.launch(headless=self.headless)
# Create context with realistic viewport
self.context = await self.browser.new_context(
viewport={"width": 1920, "height": 1080},
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
)
# Create default page
self.page = await self.context.new_page()
logger.info(f"Browser session {self.session_id} started ({self.browser_type})")
return True
except Exception as e:
logger.error(f"Failed to start browser session {self.session_id}: {e}")
raise
async def close(self):
"""Close the browser session and cleanup resources."""
try:
if self.page:
await self.page.close()
if self.context:
await self.context.close()
if self.browser:
await self.browser.close()
if self.playwright:
await self.playwright.stop()
logger.info(f"Browser session {self.session_id} closed")
return True
except Exception as e:
logger.error(f"Error closing browser session {self.session_id}: {e}")
return False
class BrowserSessionManager:
"""
Manages active browser sessions with automatic cleanup.
Sessions are stored in memory and automatically cleaned up after
a timeout period of inactivity.
"""
def __init__(self, session_timeout_minutes: int = 30):
self.sessions: Dict[str, BrowserSession] = {}
self.session_timeout_minutes = session_timeout_minutes
def get_session(self, session_id: str) -> Optional[BrowserSession]:
"""Get an existing session by ID."""
return self.sessions.get(session_id)
async def create_session(
self,
user_id: str,
agent_id: Optional[str] = None,
headless: bool = True,
browser_type: str = "chromium"
) -> BrowserSession:
"""Create and start a new browser session."""
session_id = str(uuid.uuid4())
session = BrowserSession(
session_id=session_id,
user_id=user_id,
agent_id=agent_id,
headless=headless,
browser_type=browser_type
)
await session.start()
self.sessions[session_id] = session
return session
async def close_session(self, session_id: str) -> bool:
"""Close and remove a session."""
session = self.sessions.get(session_id)
if session:
await session.close()
del self.sessions[session_id]
return True
return False
async def cleanup_expired_sessions(self):
"""Remove expired sessions based on last used time."""
now = datetime.now()
expired_ids = []
for session_id, session in self.sessions.items():
elapsed = (now - session.last_used).total_seconds() / 60
if elapsed > self.session_timeout_minutes:
expired_ids.append(session_id)
for session_id in expired_ids:
logger.info(f"Cleaning up expired browser session: {session_id}")
await self.close_session(session_id)
return len(expired_ids)
# Global session manager
_session_manager = BrowserSessionManager()
def get_browser_manager() -> BrowserSessionManager:
"""Get the global browser session manager."""
return _session_manager
# ============================================================================
# Browser Tool Functions
# ============================================================================
async def browser_create_session(
user_id: str,
agent_id: Optional[str] = None,
headless: bool = None,
browser_type: str = "chromium",
db: Optional[Session] = None
) -> Dict[str, Any]:
"""
Create a new browser session with governance integration.
Args:
user_id: User ID creating the session
agent_id: Agent ID creating the session (for governance)
headless: Whether to run headless (default from env)
browser_type: Browser type (chromium, firefox, webkit)
db: Database session for governance
Returns:
Dict with session_id and metadata
"""
agent = None
governance_check = None
agent_execution = None
try:
# Governance: Check agent permissions (browser_navigate = INTERN+)
if FeatureFlags.should_enforce_governance('browser') and agent_id and db:
resolver = AgentContextResolver(db)
governance = ServiceFactory.get_governance_service(db)
agent, _ = await resolver.resolve_agent_for_request(
user_id=user_id,
requested_agent_id=agent_id,
action_type="browser_navigate"
)
if agent:
governance_check = governance.can_perform_action(
agent_id=agent.id,
action_type="browser_navigate"
)
if not governance_check["allowed"]:
logger.warning(f"Governance blocked browser session: {governance_check['reason']}")
return {
"success": False,
"error": f"Agent not permitted to use browser: {governance_check['reason']}"
}
# Create execution record
agent_execution = AgentExecution(
agent_id=agent.id,
workspace_id="default",
status="running",
input_summary="Create browser session",
triggered_by="browser_tool"
)
db.add(agent_execution)
db.commit()
db.refresh(agent_execution)
# Create session
if headless is None:
headless = BROWSER_HEADLESS
session = await get_browser_manager().create_session(
user_id=user_id,
agent_id=agent_id if agent else None,
headless=headless,
browser_type=browser_type
)
# Record outcome
if agent and db and FeatureFlags.should_enforce_governance('browser'):
governance = ServiceFactory.get_governance_service(db)
await governance.record_outcome(agent.id, success=True)
if agent_execution:
agent_execution.status = "completed"
agent_execution.output_summary = f"Created browser session {session.session_id}"
agent_execution.completed_at = datetime.now()
db.commit()
logger.info(f"Created browser session {session.session_id} for user {user_id}")
return {
"success": True,
"session_id": session.session_id,
"browser_type": browser_type,
"headless": headless,
"agent_id": agent.id if agent else None,
"created_at": session.created_at.isoformat()
}
except Exception as e:
logger.error(f"Failed to create browser session: {e}")
if agent_execution and db and FeatureFlags.should_enforce_governance('browser'):
try:
governance = ServiceFactory.get_governance_service(db)
await governance.record_outcome(agent.id, success=False)
agent_execution.status = "failed"
agent_execution.error_message = str(e)
agent_execution.completed_at = datetime.now()
db.commit()
except Exception as inner_e:
logger.error(f"Failed to record execution failure: {inner_e}")
return {
"success": False,
"error": str(e)
}
async def browser_navigate(
session_id: str,
url: str,
wait_until: str = "load",
user_id: str = None,
agent_id: Optional[str] = None,
db: Optional[Session] = None
) -> Dict[str, Any]:
"""
Navigate to a URL in an existing browser session.
Args:
session_id: Browser session ID
url: URL to navigate to
wait_until: When to consider navigation succeeded (load, domcontentloaded, networkidle)
user_id: User ID for validation
agent_id: Agent ID for governance
db: Database session for governance
Returns:
Dict with navigation result and page info
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
# Navigate to URL
response = await session.page.goto(url, wait_until=wait_until, timeout=30000)
session.last_used = datetime.now()
# Get page info
title = await session.page.title()
url_final = session.page.url
logger.info(f"Navigated session {session_id} to {url}")
return {
"success": True,
"session_id": session_id,
"url": url_final,
"title": title,
"status": response.status if response else None,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Navigation failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_screenshot(
session_id: str,
path: Optional[str] = None,
full_page: bool = False,
user_id: str = None
) -> Dict[str, Any]:
"""
Take a screenshot of the current page.
Args:
session_id: Browser session ID
path: Optional file path to save screenshot
full_page: Whether to capture full scrolling page
user_id: User ID for validation
Returns:
Dict with screenshot data (base64) or file path
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
# Take screenshot
screenshot_bytes = await session.page.screenshot(
full_page=full_page,
type="png"
)
session.last_used = datetime.now()
# Encode to base64
screenshot_base64 = base64.b64encode(screenshot_bytes).decode("utf-8")
# Save to file if path provided
if path:
import os
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
with open(path, "wb") as f:
f.write(screenshot_bytes)
logger.info(f"Screenshot saved to {path}")
return {
"success": True,
"path": path,
"size_bytes": len(screenshot_bytes)
}
logger.info(f"Screenshot taken for session {session_id}")
return {
"success": True,
"data": screenshot_base64,
"size_bytes": len(screenshot_bytes),
"format": "png"
}
except Exception as e:
logger.error(f"Screenshot failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_fill_form(
session_id: str,
selectors: Dict[str, str],
submit: bool = False,
user_id: str = None
) -> Dict[str, Any]:
"""
Fill form fields using CSS selectors.
Args:
session_id: Browser session ID
selectors: Dict mapping CSS selectors to values
submit: Whether to submit the form after filling
user_id: User ID for validation
Returns:
Dict with fill result
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
filled_count = 0
for selector, value in selectors.items():
try:
# Wait for element
await session.page.wait_for_selector(selector, timeout=5000)
# Check input type and fill accordingly
element = await session.page.query_selector(selector)
tag_name = await element.evaluate("el => el.tagName")
input_type = await element.evaluate("el => el.type || ''")
if tag_name in ["INPUT", "TEXTAREA"]:
await session.page.fill(selector, value)
filled_count += 1
elif tag_name == "SELECT":
await session.page.select_option(selector, value)
filled_count += 1
else:
logger.warning(f"Unsupported element type: {tag_name} for selector {selector}")
except Exception as e:
logger.warning(f"Failed to fill {selector}: {e}")
session.last_used = datetime.now()
result = {
"success": True,
"session_id": session_id,
"fields_filled": filled_count
}
# Submit form if requested
if submit:
# Try to find submit button or form
try:
# Look for button with type="submit"
submit_button = await session.page.query_selector("button[type='submit']")
if submit_button:
await submit_button.click()
result["submitted"] = True
result["submission_method"] = "submit_button"
else:
# Try form submission
await session.page.evaluate("() => document.querySelector('form')?.submit()")
result["submitted"] = True
result["submission_method"] = "form_submit"
except Exception as submit_error:
logger.warning(f"Form submission failed: {submit_error}")
result["submitted"] = False
result["submit_error"] = str(submit_error)
logger.info(f"Filled {filled_count} fields in session {session_id}")
return result
except Exception as e:
logger.error(f"Form fill failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_click(
session_id: str,
selector: str,
wait_for: Optional[str] = None,
user_id: str = None
) -> Dict[str, Any]:
"""
Click an element using CSS selector.
Args:
session_id: Browser session ID
selector: CSS selector for element to click
wait_for: Optional selector to wait for after click
user_id: User ID for validation
Returns:
Dict with click result
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
# Wait for element to be clickable
await session.page.wait_for_selector(selector, state="visible", timeout=5000)
# Click element
await session.page.click(selector)
session.last_used = datetime.now()
# Wait for navigation or element if specified
if wait_for:
try:
await session.page.wait_for_selector(wait_for, timeout=5000)
except Exception as e:
logger.debug(f"Wait for selector '{wait_for}' not found or timeout: {e}")
# Continue anyway - don't fail the entire operation
logger.info(f"Clicked {selector} in session {session_id}")
return {
"success": True,
"session_id": session_id,
"selector": selector
}
except Exception as e:
logger.error(f"Click failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_extract_text(
session_id: str,
selector: Optional[str] = None,
user_id: str = None
) -> Dict[str, Any]:
"""
Extract text content from the page or specific elements.
Args:
session_id: Browser session ID
selector: Optional CSS selector (if None, extracts full page text)
user_id: User ID for validation
Returns:
Dict with extracted text
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
if selector:
# Extract text from specific element(s)
elements = await session.page.query_selector_all(selector)
texts = [await el.inner_text() for el in elements]
result_text = "\n".join(texts)
else:
# Extract full page text
result_text = await session.page.inner_text("body")
session.last_used = datetime.now()
logger.info(f"Extracted {len(result_text)} chars from session {session_id}")
return {
"success": True,
"session_id": session_id,
"text": result_text,
"length": len(result_text)
}
except Exception as e:
logger.error(f"Text extraction failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_execute_script(
session_id: str,
script: str,
user_id: str = None
) -> Dict[str, Any]:
"""
Execute JavaScript in the browser context.
Args:
session_id: Browser session ID
script: JavaScript code to execute
user_id: User ID for validation
Returns:
Dict with execution result
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
# Execute script
result = await session.page.evaluate(script)
session.last_used = datetime.now()
logger.info(f"Executed script in session {session_id}")
return {
"success": True,
"session_id": session_id,
"result": result
}
except Exception as e:
logger.error(f"Script execution failed for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_close_session(
session_id: str,
user_id: str = None
) -> Dict[str, Any]:
"""
Close a browser session.
Args:
session_id: Browser session ID
user_id: User ID for validation
Returns:
Dict with close result
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
success = await get_browser_manager().close_session(session_id)
if success:
logger.info(f"Closed browser session {session_id}")
return {
"success": True,
"session_id": session_id
}
else:
return {
"success": False,
"error": "Failed to close session"
}
except Exception as e:
logger.error(f"Failed to close session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
async def browser_get_page_info(
session_id: str,
user_id: str = None
) -> Dict[str, Any]:
"""
Get information about the current page.
Args:
session_id: Browser session ID
user_id: User ID for validation
Returns:
Dict with page information
"""
session = get_browser_manager().get_session(session_id)
if not session:
return {
"success": False,
"error": f"Browser session {session_id} not found"
}
if user_id and session.user_id != user_id:
return {
"success": False,
"error": "Session belongs to different user"
}
try:
# Get page info
title = await session.page.title()
url = session.page.url
# Get cookies
cookies = await session.context.cookies()
logger.info(f"Retrieved page info for session {session_id}")
return {
"success": True,
"session_id": session_id,
"title": title,
"url": url,
"cookies_count": len(cookies)
}
except Exception as e:
logger.error(f"Failed to get page info for session {session_id}: {e}")
return {
"success": False,
"error": str(e)
}
|