setup.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. Browser-Use 设置工具
  3. 检查并安装 browser-use 的依赖(CLI 和 Chromium)
  4. """
  5. import subprocess
  6. from pathlib import Path
  7. from typing import Optional
  8. from agent.tools import tool, ToolResult
  9. def _check_browser_use_cli() -> bool:
  10. """检查 browser-use CLI 是否已安装"""
  11. try:
  12. result = subprocess.run(
  13. ["browser-use", "--help"],
  14. capture_output=True,
  15. timeout=5
  16. )
  17. return result.returncode == 0
  18. except (FileNotFoundError, subprocess.TimeoutExpired):
  19. return False
  20. def _check_chromium_installed() -> bool:
  21. """检查 Chromium 是否已安装(Playwright 缓存)"""
  22. # macOS
  23. playwright_cache = Path.home() / "Library" / "Caches" / "ms-playwright"
  24. if not playwright_cache.exists():
  25. # Linux
  26. playwright_cache = Path.home() / ".cache" / "ms-playwright"
  27. if playwright_cache.exists():
  28. chromium_dirs = list(playwright_cache.glob("chromium-*"))
  29. return len(chromium_dirs) > 0
  30. return False
  31. @tool(description="检查 browser-use 依赖并提供安装指导")
  32. async def check_browser_use(
  33. uid: str = ""
  34. ) -> ToolResult:
  35. """
  36. 检查 browser-use 的依赖是否已安装
  37. 检查项:
  38. 1. browser-use CLI 是否可用
  39. 2. Chromium 浏览器是否已安装
  40. Returns:
  41. ToolResult: 检查结果和安装指导
  42. """
  43. cli_installed = _check_browser_use_cli()
  44. chromium_installed = _check_chromium_installed()
  45. output = "# Browser-Use Dependency Check\n\n"
  46. # CLI 状态
  47. output += "## 1. Browser-Use CLI\n\n"
  48. if cli_installed:
  49. output += "✅ **Installed** - `browser-use` command is available\n\n"
  50. else:
  51. output += "❌ **Not Installed**\n\n"
  52. output += "Install with:\n```bash\npip install browser-use\n# or\nuv add browser-use && uv sync\n```\n\n"
  53. # Chromium 状态
  54. output += "## 2. Chromium Browser\n\n"
  55. if chromium_installed:
  56. output += "✅ **Installed** - Chromium is available in Playwright cache\n\n"
  57. else:
  58. output += "❌ **Not Installed**\n\n"
  59. output += "Install with:\n```bash\nuvx browser-use install\n```\n\n"
  60. output += "Installation location:\n"
  61. output += "- macOS: `~/Library/Caches/ms-playwright/`\n"
  62. output += "- Linux: `~/.cache/ms-playwright/`\n\n"
  63. # 总结
  64. if cli_installed and chromium_installed:
  65. output += "## ✅ Ready to Use\n\n"
  66. output += "All dependencies are installed. You can now use browser-use commands.\n\n"
  67. output += "Test with: `browser-use --help`\n"
  68. status = "ready"
  69. else:
  70. output += "## ⚠️ Setup Required\n\n"
  71. output += "Please complete the installation steps above before using browser-use.\n"
  72. status = "incomplete"
  73. return ToolResult(
  74. title="Browser-Use Dependency Check",
  75. output=output,
  76. metadata={
  77. "cli_installed": cli_installed,
  78. "chromium_installed": chromium_installed,
  79. "status": status
  80. }
  81. )
  82. @tool(description="安装 browser-use 的 Chromium 浏览器")
  83. async def install_browser_use_chromium(
  84. uid: str = ""
  85. ) -> ToolResult:
  86. """
  87. 安装 Chromium 浏览器(通过 uvx browser-use install)
  88. 注意:这会下载约 200-300MB 的 Chromium 浏览器
  89. Returns:
  90. ToolResult: 安装结果
  91. """
  92. # 先检查 CLI 是否可用
  93. if not _check_browser_use_cli():
  94. return ToolResult(
  95. title="Browser-Use CLI Not Found",
  96. output="Please install browser-use first:\n```bash\npip install browser-use\n```",
  97. error="browser-use CLI not found"
  98. )
  99. # 检查是否已安装
  100. if _check_chromium_installed():
  101. return ToolResult(
  102. title="Chromium Already Installed",
  103. output="Chromium is already installed in the Playwright cache.\n\n"
  104. "Location: `~/Library/Caches/ms-playwright/` (macOS) or `~/.cache/ms-playwright/` (Linux)"
  105. )
  106. # 执行安装
  107. output = "Installing Chromium browser...\n\n"
  108. output += "This may take a few minutes (downloading ~200-300MB).\n\n"
  109. try:
  110. result = subprocess.run(
  111. ["uvx", "browser-use", "install"],
  112. capture_output=True,
  113. text=True,
  114. timeout=300 # 5 分钟超时
  115. )
  116. if result.returncode == 0:
  117. output += "✅ Installation successful!\n\n"
  118. output += result.stdout
  119. return ToolResult(
  120. title="Chromium Installed",
  121. output=output,
  122. metadata={"installed": True}
  123. )
  124. else:
  125. output += f"❌ Installation failed\n\n"
  126. output += f"Error: {result.stderr}\n"
  127. return ToolResult(
  128. title="Installation Failed",
  129. output=output,
  130. error=result.stderr
  131. )
  132. except subprocess.TimeoutExpired:
  133. return ToolResult(
  134. title="Installation Timeout",
  135. output="Installation timed out after 5 minutes.\n"
  136. "Please try running manually:\n```bash\nuvx browser-use install\n```",
  137. error="Timeout"
  138. )
  139. except Exception as e:
  140. return ToolResult(
  141. title="Installation Error",
  142. output=f"Unexpected error: {str(e)}\n\n"
  143. "Please try running manually:\n```bash\nuvx browser-use install\n```",
  144. error=str(e)
  145. )