| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- """
- Browser-Use 设置工具
- 检查并安装 browser-use 的依赖(CLI 和 Chromium)
- """
- import subprocess
- from pathlib import Path
- from typing import Optional
- from agent.tools import tool, ToolResult
- def _check_browser_use_cli() -> bool:
- """检查 browser-use CLI 是否已安装"""
- try:
- result = subprocess.run(
- ["browser-use", "--help"],
- capture_output=True,
- timeout=5
- )
- return result.returncode == 0
- except (FileNotFoundError, subprocess.TimeoutExpired):
- return False
- def _check_chromium_installed() -> bool:
- """检查 Chromium 是否已安装(Playwright 缓存)"""
- # macOS
- playwright_cache = Path.home() / "Library" / "Caches" / "ms-playwright"
- if not playwright_cache.exists():
- # Linux
- playwright_cache = Path.home() / ".cache" / "ms-playwright"
- if playwright_cache.exists():
- chromium_dirs = list(playwright_cache.glob("chromium-*"))
- return len(chromium_dirs) > 0
- return False
- @tool(description="检查 browser-use 依赖并提供安装指导")
- async def check_browser_use(
- uid: str = ""
- ) -> ToolResult:
- """
- 检查 browser-use 的依赖是否已安装
- 检查项:
- 1. browser-use CLI 是否可用
- 2. Chromium 浏览器是否已安装
- Returns:
- ToolResult: 检查结果和安装指导
- """
- cli_installed = _check_browser_use_cli()
- chromium_installed = _check_chromium_installed()
- output = "# Browser-Use Dependency Check\n\n"
- # CLI 状态
- output += "## 1. Browser-Use CLI\n\n"
- if cli_installed:
- output += "✅ **Installed** - `browser-use` command is available\n\n"
- else:
- output += "❌ **Not Installed**\n\n"
- output += "Install with:\n```bash\npip install browser-use\n# or\nuv add browser-use && uv sync\n```\n\n"
- # Chromium 状态
- output += "## 2. Chromium Browser\n\n"
- if chromium_installed:
- output += "✅ **Installed** - Chromium is available in Playwright cache\n\n"
- else:
- output += "❌ **Not Installed**\n\n"
- output += "Install with:\n```bash\nuvx browser-use install\n```\n\n"
- output += "Installation location:\n"
- output += "- macOS: `~/Library/Caches/ms-playwright/`\n"
- output += "- Linux: `~/.cache/ms-playwright/`\n\n"
- # 总结
- if cli_installed and chromium_installed:
- output += "## ✅ Ready to Use\n\n"
- output += "All dependencies are installed. You can now use browser-use commands.\n\n"
- output += "Test with: `browser-use --help`\n"
- status = "ready"
- else:
- output += "## ⚠️ Setup Required\n\n"
- output += "Please complete the installation steps above before using browser-use.\n"
- status = "incomplete"
- return ToolResult(
- title="Browser-Use Dependency Check",
- output=output,
- metadata={
- "cli_installed": cli_installed,
- "chromium_installed": chromium_installed,
- "status": status
- }
- )
- @tool(description="安装 browser-use 的 Chromium 浏览器")
- async def install_browser_use_chromium(
- uid: str = ""
- ) -> ToolResult:
- """
- 安装 Chromium 浏览器(通过 uvx browser-use install)
- 注意:这会下载约 200-300MB 的 Chromium 浏览器
- Returns:
- ToolResult: 安装结果
- """
- # 先检查 CLI 是否可用
- if not _check_browser_use_cli():
- return ToolResult(
- title="Browser-Use CLI Not Found",
- output="Please install browser-use first:\n```bash\npip install browser-use\n```",
- error="browser-use CLI not found"
- )
- # 检查是否已安装
- if _check_chromium_installed():
- return ToolResult(
- title="Chromium Already Installed",
- output="Chromium is already installed in the Playwright cache.\n\n"
- "Location: `~/Library/Caches/ms-playwright/` (macOS) or `~/.cache/ms-playwright/` (Linux)"
- )
- # 执行安装
- output = "Installing Chromium browser...\n\n"
- output += "This may take a few minutes (downloading ~200-300MB).\n\n"
- try:
- result = subprocess.run(
- ["uvx", "browser-use", "install"],
- capture_output=True,
- text=True,
- timeout=300 # 5 分钟超时
- )
- if result.returncode == 0:
- output += "✅ Installation successful!\n\n"
- output += result.stdout
- return ToolResult(
- title="Chromium Installed",
- output=output,
- metadata={"installed": True}
- )
- else:
- output += f"❌ Installation failed\n\n"
- output += f"Error: {result.stderr}\n"
- return ToolResult(
- title="Installation Failed",
- output=output,
- error=result.stderr
- )
- except subprocess.TimeoutExpired:
- return ToolResult(
- title="Installation Timeout",
- output="Installation timed out after 5 minutes.\n"
- "Please try running manually:\n```bash\nuvx browser-use install\n```",
- error="Timeout"
- )
- except Exception as e:
- return ToolResult(
- title="Installation Error",
- output=f"Unexpected error: {str(e)}\n\n"
- "Please try running manually:\n```bash\nuvx browser-use install\n```",
- error=str(e)
- )
|