mirror of
https://github.com/trycua/computer.git
synced 2026-05-14 20:39:03 -05:00
Merge branch 'main' into feat/cua-bench-submodules
This commit is contained in:
@@ -8,6 +8,7 @@ from . import (
|
||||
composed_grounded,
|
||||
gelato,
|
||||
gemini,
|
||||
generic_vlm,
|
||||
glm45v,
|
||||
gta1,
|
||||
holo,
|
||||
@@ -16,7 +17,6 @@ from . import (
|
||||
omniparser,
|
||||
openai,
|
||||
opencua,
|
||||
generic_vlm,
|
||||
uiins,
|
||||
uitars,
|
||||
uitars2,
|
||||
@@ -24,19 +24,19 @@ from . import (
|
||||
|
||||
__all__ = [
|
||||
"anthropic",
|
||||
"openai",
|
||||
"uitars",
|
||||
"omniparser",
|
||||
"gta1",
|
||||
"composed_grounded",
|
||||
"glm45v",
|
||||
"opencua",
|
||||
"internvl",
|
||||
"holo",
|
||||
"moondream3",
|
||||
"gelato",
|
||||
"gemini",
|
||||
"generic_vlm",
|
||||
"glm45v",
|
||||
"gta1",
|
||||
"holo",
|
||||
"internvl",
|
||||
"moondream3",
|
||||
"omniparser",
|
||||
"openai",
|
||||
"opencua",
|
||||
"uiins",
|
||||
"gelato",
|
||||
"uitars",
|
||||
"uitars2",
|
||||
]
|
||||
|
||||
@@ -442,7 +442,7 @@ def get_all_element_descriptions(responses_items: List[Dict[str, Any]]) -> List[
|
||||
|
||||
# Conversion functions between responses_items and completion messages formats
|
||||
def convert_responses_items_to_completion_messages(
|
||||
messages: List[Dict[str, Any]],
|
||||
messages: List[Dict[str, Any]],
|
||||
allow_images_in_tool_results: bool = True,
|
||||
send_multiple_user_images_per_parallel_tool_results: bool = False,
|
||||
) -> List[Dict[str, Any]]:
|
||||
@@ -573,25 +573,33 @@ def convert_responses_items_to_completion_messages(
|
||||
"computer_call_output",
|
||||
]
|
||||
# Send tool message + separate user message with image (OpenAI compatible)
|
||||
completion_messages += [
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"content": "[Execution completed. See screenshot below]",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "image_url", "image_url": {"url": output.get("image_url")}}
|
||||
],
|
||||
},
|
||||
] if send_multiple_user_images_per_parallel_tool_results or (not is_next_message_image_result) else [
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"content": "[Execution completed. See screenshot below]",
|
||||
},
|
||||
]
|
||||
completion_messages += (
|
||||
[
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"content": "[Execution completed. See screenshot below]",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {"url": output.get("image_url")},
|
||||
}
|
||||
],
|
||||
},
|
||||
]
|
||||
if send_multiple_user_images_per_parallel_tool_results
|
||||
or (not is_next_message_image_result)
|
||||
else [
|
||||
{
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"content": "[Execution completed. See screenshot below]",
|
||||
},
|
||||
]
|
||||
)
|
||||
else:
|
||||
# Handle text output as tool response
|
||||
completion_messages.append(
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
"""Tools for agent interactions."""
|
||||
|
||||
from .browser_tool import BrowserTool
|
||||
|
||||
__all__ = ["BrowserTool"]
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
Browser Tool for agent interactions.
|
||||
Allows agents to control a browser programmatically via Playwright.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from computer.interface import GenericComputerInterface
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BrowserTool:
|
||||
"""
|
||||
Browser tool that uses the computer SDK's interface to control a browser.
|
||||
Implements the Fara/Magentic-One agent interface for browser control.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
interface: "GenericComputerInterface",
|
||||
):
|
||||
"""
|
||||
Initialize the BrowserTool.
|
||||
|
||||
Args:
|
||||
interface: A GenericComputerInterface instance that provides playwright_exec
|
||||
"""
|
||||
self.interface = interface
|
||||
self.logger = logger
|
||||
|
||||
async def _execute_command(self, command: str, params: dict) -> dict:
|
||||
"""
|
||||
Execute a browser command via the computer interface.
|
||||
|
||||
Args:
|
||||
command: Command name
|
||||
params: Command parameters
|
||||
|
||||
Returns:
|
||||
Response dictionary
|
||||
"""
|
||||
try:
|
||||
result = await self.interface.playwright_exec(command, params)
|
||||
if not result.get("success"):
|
||||
self.logger.error(
|
||||
f"Browser command '{command}' failed: {result.get('error', 'Unknown error')}"
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error executing browser command '{command}': {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
async def visit_url(self, url: str) -> dict:
|
||||
"""
|
||||
Navigate to a URL.
|
||||
|
||||
Args:
|
||||
url: URL to visit
|
||||
|
||||
Returns:
|
||||
Response dictionary with success status and current URL
|
||||
"""
|
||||
return await self._execute_command("visit_url", {"url": url})
|
||||
|
||||
async def click(self, x: int, y: int) -> dict:
|
||||
"""
|
||||
Click at coordinates.
|
||||
|
||||
Args:
|
||||
x: X coordinate
|
||||
y: Y coordinate
|
||||
|
||||
Returns:
|
||||
Response dictionary with success status
|
||||
"""
|
||||
return await self._execute_command("click", {"x": x, "y": y})
|
||||
|
||||
async def type(self, text: str) -> dict:
|
||||
"""
|
||||
Type text into the focused element.
|
||||
|
||||
Args:
|
||||
text: Text to type
|
||||
|
||||
Returns:
|
||||
Response dictionary with success status
|
||||
"""
|
||||
return await self._execute_command("type", {"text": text})
|
||||
|
||||
async def scroll(self, delta_x: int, delta_y: int) -> dict:
|
||||
"""
|
||||
Scroll the page.
|
||||
|
||||
Args:
|
||||
delta_x: Horizontal scroll delta
|
||||
delta_y: Vertical scroll delta
|
||||
|
||||
Returns:
|
||||
Response dictionary with success status
|
||||
"""
|
||||
return await self._execute_command("scroll", {"delta_x": delta_x, "delta_y": delta_y})
|
||||
|
||||
async def web_search(self, query: str) -> dict:
|
||||
"""
|
||||
Navigate to a Google search for the query.
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
|
||||
Returns:
|
||||
Response dictionary with success status and current URL
|
||||
"""
|
||||
return await self._execute_command("web_search", {"query": query})
|
||||
|
||||
async def screenshot(self) -> bytes:
|
||||
"""
|
||||
Take a screenshot of the current browser page.
|
||||
|
||||
Returns:
|
||||
Screenshot image data as bytes (PNG format)
|
||||
"""
|
||||
import base64
|
||||
|
||||
result = await self._execute_command("screenshot", {})
|
||||
if result.get("success") and result.get("screenshot"):
|
||||
# Decode base64 screenshot to bytes
|
||||
screenshot_b64 = result["screenshot"]
|
||||
screenshot_bytes = base64.b64decode(screenshot_b64)
|
||||
return screenshot_bytes
|
||||
else:
|
||||
error = result.get("error", "Unknown error")
|
||||
raise RuntimeError(f"Failed to take screenshot: {error}")
|
||||
@@ -24,7 +24,7 @@ dependencies = [
|
||||
"certifi>=2024.2.2",
|
||||
"litellm>=1.74.12"
|
||||
]
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
|
||||
[project.optional-dependencies]
|
||||
openai = []
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[bumpversion]
|
||||
current_version = 0.1.30
|
||||
current_version = 0.1.31
|
||||
commit = True
|
||||
tag = True
|
||||
tag_name = computer-server-v{new_version}
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
"""
|
||||
Browser manager using Playwright for programmatic browser control.
|
||||
This allows agents to control a browser that runs visibly on the XFCE desktop.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
try:
|
||||
from playwright.async_api import Browser, BrowserContext, Page, async_playwright
|
||||
except ImportError:
|
||||
async_playwright = None
|
||||
Browser = None
|
||||
BrowserContext = None
|
||||
Page = None
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BrowserManager:
|
||||
"""
|
||||
Manages a Playwright browser instance that runs visibly on the XFCE desktop.
|
||||
Uses persistent context to maintain cookies and sessions.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the BrowserManager."""
|
||||
self.playwright = None
|
||||
self.browser: Optional[Browser] = None
|
||||
self.context: Optional[BrowserContext] = None
|
||||
self.page: Optional[Page] = None
|
||||
self._initialized = False
|
||||
self._initialization_error: Optional[str] = None
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def _ensure_initialized(self):
|
||||
"""Ensure the browser is initialized."""
|
||||
# Check if browser was closed and needs reinitialization
|
||||
if self._initialized:
|
||||
try:
|
||||
# Check if context is still valid by trying to access it
|
||||
if self.context:
|
||||
# Try to get pages - this will raise if context is closed
|
||||
_ = self.context.pages
|
||||
# If we get here, context is still alive
|
||||
return
|
||||
else:
|
||||
# Context was closed, need to reinitialize
|
||||
self._initialized = False
|
||||
logger.warning("Browser context was closed, will reinitialize...")
|
||||
except Exception as e:
|
||||
# Context is dead, need to reinitialize
|
||||
logger.warning(f"Browser context is dead ({e}), will reinitialize...")
|
||||
self._initialized = False
|
||||
self.context = None
|
||||
self.page = None
|
||||
# Clean up playwright if it exists
|
||||
if self.playwright:
|
||||
try:
|
||||
await self.playwright.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self.playwright = None
|
||||
|
||||
async with self._lock:
|
||||
# Double-check after acquiring lock (another thread might have initialized it)
|
||||
if self._initialized:
|
||||
try:
|
||||
if self.context:
|
||||
_ = self.context.pages
|
||||
return
|
||||
except Exception:
|
||||
self._initialized = False
|
||||
self.context = None
|
||||
self.page = None
|
||||
if self.playwright:
|
||||
try:
|
||||
await self.playwright.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self.playwright = None
|
||||
|
||||
if async_playwright is None:
|
||||
raise RuntimeError(
|
||||
"playwright is not installed. Please install it with: pip install playwright && playwright install --with-deps firefox"
|
||||
)
|
||||
|
||||
try:
|
||||
# Get display from environment or default to :1
|
||||
display = os.environ.get("DISPLAY", ":1")
|
||||
logger.info(f"Initializing browser with DISPLAY={display}")
|
||||
|
||||
# Start playwright
|
||||
self.playwright = await async_playwright().start()
|
||||
|
||||
# Launch Firefox with persistent context (keeps cookies/sessions)
|
||||
# headless=False is CRITICAL so the visual agent can see it
|
||||
user_data_dir = os.path.join(os.path.expanduser("~"), ".playwright-firefox")
|
||||
os.makedirs(user_data_dir, exist_ok=True)
|
||||
|
||||
# launch_persistent_context returns a BrowserContext, not a Browser
|
||||
# Note: Removed --kiosk mode so the desktop remains visible
|
||||
self.context = await self.playwright.firefox.launch_persistent_context(
|
||||
user_data_dir=user_data_dir,
|
||||
headless=False, # CRITICAL: visible for visual agent
|
||||
viewport={"width": 1024, "height": 768},
|
||||
# Removed --kiosk to allow desktop visibility
|
||||
)
|
||||
|
||||
# Add init script to make the browser less detectable
|
||||
await self.context.add_init_script(
|
||||
"""const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||
Navigator.prototype,
|
||||
"webdriver"
|
||||
).get;
|
||||
defaultGetter.apply(navigator);
|
||||
defaultGetter.toString();
|
||||
Object.defineProperty(Navigator.prototype, "webdriver", {
|
||||
set: undefined,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: new Proxy(defaultGetter, {
|
||||
apply: (target, thisArg, args) => {
|
||||
Reflect.apply(target, thisArg, args);
|
||||
return false;
|
||||
},
|
||||
}),
|
||||
});
|
||||
const patchedGetter = Object.getOwnPropertyDescriptor(
|
||||
Navigator.prototype,
|
||||
"webdriver"
|
||||
).get;
|
||||
patchedGetter.apply(navigator);
|
||||
patchedGetter.toString();"""
|
||||
)
|
||||
|
||||
# Get the first page or create one
|
||||
pages = self.context.pages
|
||||
if pages:
|
||||
self.page = pages[0]
|
||||
else:
|
||||
self.page = await self.context.new_page()
|
||||
|
||||
self._initialized = True
|
||||
logger.info("Browser initialized successfully")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize browser: {e}")
|
||||
import traceback
|
||||
|
||||
logger.error(traceback.format_exc())
|
||||
# Don't raise - return error in execute_command instead
|
||||
self._initialization_error = str(e)
|
||||
raise
|
||||
|
||||
async def _execute_command_impl(self, cmd: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Internal implementation of command execution."""
|
||||
if cmd == "visit_url":
|
||||
url = params.get("url")
|
||||
if not url:
|
||||
return {"success": False, "error": "url parameter is required"}
|
||||
await self.page.goto(url, wait_until="domcontentloaded", timeout=30000)
|
||||
return {"success": True, "url": self.page.url}
|
||||
|
||||
elif cmd == "click":
|
||||
x = params.get("x")
|
||||
y = params.get("y")
|
||||
if x is None or y is None:
|
||||
return {"success": False, "error": "x and y parameters are required"}
|
||||
await self.page.mouse.click(x, y)
|
||||
return {"success": True}
|
||||
|
||||
elif cmd == "type":
|
||||
text = params.get("text")
|
||||
if text is None:
|
||||
return {"success": False, "error": "text parameter is required"}
|
||||
await self.page.keyboard.type(text)
|
||||
return {"success": True}
|
||||
|
||||
elif cmd == "scroll":
|
||||
delta_x = params.get("delta_x", 0)
|
||||
delta_y = params.get("delta_y", 0)
|
||||
await self.page.mouse.wheel(delta_x, delta_y)
|
||||
return {"success": True}
|
||||
|
||||
elif cmd == "web_search":
|
||||
query = params.get("query")
|
||||
if not query:
|
||||
return {"success": False, "error": "query parameter is required"}
|
||||
# Navigate to Google search
|
||||
search_url = f"https://www.google.com/search?q={query}"
|
||||
await self.page.goto(search_url, wait_until="domcontentloaded", timeout=30000)
|
||||
return {"success": True, "url": self.page.url}
|
||||
|
||||
elif cmd == "screenshot":
|
||||
# Take a screenshot and return as base64
|
||||
import base64
|
||||
|
||||
screenshot_bytes = await self.page.screenshot(type="png")
|
||||
screenshot_b64 = base64.b64encode(screenshot_bytes).decode("utf-8")
|
||||
return {"success": True, "screenshot": screenshot_b64}
|
||||
|
||||
else:
|
||||
return {"success": False, "error": f"Unknown command: {cmd}"}
|
||||
|
||||
async def execute_command(self, cmd: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute a browser command with automatic recovery.
|
||||
|
||||
Args:
|
||||
cmd: Command name (visit_url, click, type, scroll, web_search)
|
||||
params: Command parameters
|
||||
|
||||
Returns:
|
||||
Result dictionary with success status and any data
|
||||
"""
|
||||
max_retries = 2
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
await self._ensure_initialized()
|
||||
except Exception as e:
|
||||
error_msg = getattr(self, "_initialization_error", None) or str(e)
|
||||
logger.error(f"Browser initialization failed: {error_msg}")
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Browser initialization failed: {error_msg}. "
|
||||
f"Make sure Playwright and Firefox are installed, and DISPLAY is set correctly.",
|
||||
}
|
||||
|
||||
# Check if page is still valid and get a new one if needed
|
||||
page_valid = False
|
||||
try:
|
||||
if self.page is not None and not self.page.is_closed():
|
||||
# Try to access page.url to check if it's still valid
|
||||
_ = self.page.url
|
||||
page_valid = True
|
||||
except Exception as e:
|
||||
logger.warning(f"Page is invalid: {e}, will get a new page...")
|
||||
self.page = None
|
||||
|
||||
# Get a valid page if we don't have one
|
||||
if not page_valid or self.page is None:
|
||||
try:
|
||||
if self.context:
|
||||
pages = self.context.pages
|
||||
if pages:
|
||||
# Find first non-closed page
|
||||
for p in pages:
|
||||
try:
|
||||
if not p.is_closed():
|
||||
self.page = p
|
||||
logger.info("Reusing existing open page")
|
||||
page_valid = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# If no valid page found, create a new one
|
||||
if not page_valid:
|
||||
self.page = await self.context.new_page()
|
||||
logger.info("Created new page")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get new page: {e}, browser may be closed")
|
||||
# Browser was closed - force reinitialization
|
||||
self._initialized = False
|
||||
self.context = None
|
||||
self.page = None
|
||||
if self.playwright:
|
||||
try:
|
||||
await self.playwright.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self.playwright = None
|
||||
|
||||
# If this isn't the last attempt, continue to retry
|
||||
if attempt < max_retries - 1:
|
||||
logger.info("Browser was closed, retrying with fresh initialization...")
|
||||
continue
|
||||
else:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Browser was closed and cannot be recovered: {e}",
|
||||
}
|
||||
|
||||
# Try to execute the command
|
||||
try:
|
||||
return await self._execute_command_impl(cmd, params)
|
||||
except Exception as e:
|
||||
error_str = str(e)
|
||||
logger.error(f"Error executing command {cmd}: {e}")
|
||||
|
||||
# Check if this is a "browser/page/context closed" error
|
||||
if any(keyword in error_str.lower() for keyword in ["closed", "target", "context"]):
|
||||
logger.warning(
|
||||
f"Browser/page was closed during command execution (attempt {attempt + 1}/{max_retries})"
|
||||
)
|
||||
|
||||
# Force reinitialization
|
||||
self._initialized = False
|
||||
self.context = None
|
||||
self.page = None
|
||||
if self.playwright:
|
||||
try:
|
||||
await self.playwright.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self.playwright = None
|
||||
|
||||
# If this isn't the last attempt, retry
|
||||
if attempt < max_retries - 1:
|
||||
logger.info("Retrying command after browser reinitialization...")
|
||||
continue
|
||||
else:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Command failed after {max_retries} attempts: {error_str}",
|
||||
}
|
||||
else:
|
||||
# Not a browser closed error, return immediately
|
||||
import traceback
|
||||
|
||||
logger.error(traceback.format_exc())
|
||||
return {"success": False, "error": error_str}
|
||||
|
||||
# Should never reach here, but just in case
|
||||
return {"success": False, "error": "Command failed after all retries"}
|
||||
|
||||
async def close(self):
|
||||
"""Close the browser and cleanup resources."""
|
||||
async with self._lock:
|
||||
try:
|
||||
if self.context:
|
||||
await self.context.close()
|
||||
self.context = None
|
||||
if self.browser:
|
||||
await self.browser.close()
|
||||
self.browser = None
|
||||
|
||||
if self.playwright:
|
||||
await self.playwright.stop()
|
||||
self.playwright = None
|
||||
|
||||
self.page = None
|
||||
self._initialized = False
|
||||
logger.info("Browser closed successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing browser: {e}")
|
||||
|
||||
|
||||
# Global instance
|
||||
_browser_manager: Optional[BrowserManager] = None
|
||||
|
||||
|
||||
def get_browser_manager() -> BrowserManager:
|
||||
"""Get or create the global BrowserManager instance."""
|
||||
global _browser_manager
|
||||
if _browser_manager is None:
|
||||
_browser_manager = BrowserManager()
|
||||
return _browser_manager
|
||||
@@ -55,6 +55,34 @@ from .base import BaseAccessibilityHandler, BaseAutomationHandler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Trigger accessibility permissions prompt on macOS
|
||||
try:
|
||||
# Source - https://stackoverflow.com/a/17134
|
||||
# Posted by Andreas
|
||||
# Retrieved 2025-12-03, License - CC BY-SA 4.0
|
||||
# Attempt to create and post a mouse event to trigger the permissions prompt
|
||||
# This will cause macOS to show "Python would like to control this computer using accessibility features"
|
||||
current_pos = CGEventGetLocation(CGEventCreate(None))
|
||||
p = CGPoint()
|
||||
p.x = current_pos.x
|
||||
p.y = current_pos.y
|
||||
|
||||
me = CGEventCreateMouseEvent(None, kCGEventMouseMoved, p, 0)
|
||||
if me:
|
||||
CGEventPost(kCGHIDEventTap, me)
|
||||
CFRelease(me)
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to trigger accessibility permissions prompt: {e}")
|
||||
|
||||
# Trigger screen recording prompt on macOS
|
||||
try:
|
||||
import pyautogui
|
||||
|
||||
pyautogui.screenshot()
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to trigger screenshot permissions prompt: {e}")
|
||||
|
||||
|
||||
# Constants for accessibility API
|
||||
kAXErrorSuccess = 0
|
||||
kAXRoleAttribute = "AXRole"
|
||||
|
||||
@@ -25,6 +25,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
|
||||
from .handlers.factory import HandlerFactory
|
||||
from .browser import get_browser_manager
|
||||
|
||||
# Authentication session TTL (in seconds). Override via env var CUA_AUTH_TTL_SECONDS. Default: 60s
|
||||
AUTH_SESSION_TTL_SECONDS: int = int(os.environ.get("CUA_AUTH_TTL_SECONDS", "60"))
|
||||
@@ -749,5 +750,71 @@ async def agent_response_endpoint(
|
||||
return JSONResponse(content=payload, headers=headers)
|
||||
|
||||
|
||||
@app.post("/playwright_exec")
|
||||
async def playwright_exec_endpoint(
|
||||
request: Request,
|
||||
container_name: Optional[str] = Header(None, alias="X-Container-Name"),
|
||||
api_key: Optional[str] = Header(None, alias="X-API-Key"),
|
||||
):
|
||||
"""
|
||||
Execute Playwright browser commands.
|
||||
|
||||
Headers:
|
||||
- X-Container-Name: Container name for cloud authentication
|
||||
- X-API-Key: API key for cloud authentication
|
||||
|
||||
Body:
|
||||
{
|
||||
"command": "visit_url|click|type|scroll|web_search",
|
||||
"params": {...}
|
||||
}
|
||||
"""
|
||||
# Parse request body
|
||||
try:
|
||||
body = await request.json()
|
||||
command = body.get("command")
|
||||
params = body.get("params", {})
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid JSON body: {str(e)}")
|
||||
|
||||
if not command:
|
||||
raise HTTPException(status_code=400, detail="Command is required")
|
||||
|
||||
# Check if CONTAINER_NAME is set (indicating cloud provider)
|
||||
server_container_name = os.environ.get("CONTAINER_NAME")
|
||||
|
||||
# If cloud provider, perform authentication
|
||||
if server_container_name:
|
||||
logger.info(
|
||||
f"Cloud provider detected. CONTAINER_NAME: {server_container_name}. Performing authentication..."
|
||||
)
|
||||
|
||||
# Validate required headers
|
||||
if not container_name:
|
||||
raise HTTPException(status_code=401, detail="Container name required")
|
||||
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=401, detail="API key required")
|
||||
|
||||
# Validate with AuthenticationManager
|
||||
is_authenticated = await auth_manager.auth(container_name, api_key)
|
||||
if not is_authenticated:
|
||||
raise HTTPException(status_code=401, detail="Authentication failed")
|
||||
|
||||
# Get browser manager and execute command
|
||||
try:
|
||||
browser_manager = get_browser_manager()
|
||||
result = await browser_manager.execute_command(command, params)
|
||||
|
||||
if result.get("success"):
|
||||
return JSONResponse(content=result)
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result.get("error", "Command failed"))
|
||||
except Exception as e:
|
||||
logger.error(f"Error executing playwright command: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "pdm.backend"
|
||||
|
||||
[project]
|
||||
name = "cua-computer-server"
|
||||
version = "0.1.30"
|
||||
version = "0.1.31"
|
||||
|
||||
description = "Server component for the Computer-Use Interface (CUI) framework powering Cua"
|
||||
authors = [
|
||||
@@ -12,7 +12,7 @@ authors = [
|
||||
]
|
||||
readme = "README.md"
|
||||
license = { text = "MIT" }
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
dependencies = [
|
||||
"fastapi>=0.111.0",
|
||||
"uvicorn[standard]>=0.27.0",
|
||||
@@ -24,6 +24,7 @@ dependencies = [
|
||||
"pyperclip>=1.9.0",
|
||||
"websockets>=12.0",
|
||||
"pywinctl>=0.4.1",
|
||||
"playwright>=1.40.0",
|
||||
# OS-specific runtime deps
|
||||
"pyobjc-framework-Cocoa>=10.1; sys_platform == 'darwin'",
|
||||
"pyobjc-framework-Quartz>=10.1; sys_platform == 'darwin'",
|
||||
|
||||
@@ -969,6 +969,35 @@ class Computer:
|
||||
"""
|
||||
return await self.interface.to_screenshot_coordinates(x, y)
|
||||
|
||||
async def playwright_exec(self, command: str, params: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute a Playwright browser command.
|
||||
|
||||
Args:
|
||||
command: The browser command to execute (visit_url, click, type, scroll, web_search)
|
||||
params: Command parameters
|
||||
|
||||
Returns:
|
||||
Dict containing the command result
|
||||
|
||||
Examples:
|
||||
# Navigate to a URL
|
||||
await computer.playwright_exec("visit_url", {"url": "https://example.com"})
|
||||
|
||||
# Click at coordinates
|
||||
await computer.playwright_exec("click", {"x": 100, "y": 200})
|
||||
|
||||
# Type text
|
||||
await computer.playwright_exec("type", {"text": "Hello, world!"})
|
||||
|
||||
# Scroll
|
||||
await computer.playwright_exec("scroll", {"delta_x": 0, "delta_y": -100})
|
||||
|
||||
# Web search
|
||||
await computer.playwright_exec("web_search", {"query": "computer use agent"})
|
||||
"""
|
||||
return await self.interface.playwright_exec(command, params)
|
||||
|
||||
# Add virtual environment management functions to computer interface
|
||||
async def venv_install(self, venv_name: str, requirements: list[str]):
|
||||
"""Install packages in a virtual environment.
|
||||
|
||||
@@ -667,6 +667,56 @@ class GenericComputerInterface(BaseComputerInterface):
|
||||
|
||||
return screenshot_x, screenshot_y
|
||||
|
||||
# Playwright browser control
|
||||
async def playwright_exec(self, command: str, params: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute a Playwright browser command.
|
||||
|
||||
Args:
|
||||
command: The browser command to execute (visit_url, click, type, scroll, web_search)
|
||||
params: Command parameters
|
||||
|
||||
Returns:
|
||||
Dict containing the command result
|
||||
|
||||
Examples:
|
||||
# Navigate to a URL
|
||||
await interface.playwright_exec("visit_url", {"url": "https://example.com"})
|
||||
|
||||
# Click at coordinates
|
||||
await interface.playwright_exec("click", {"x": 100, "y": 200})
|
||||
|
||||
# Type text
|
||||
await interface.playwright_exec("type", {"text": "Hello, world!"})
|
||||
|
||||
# Scroll
|
||||
await interface.playwright_exec("scroll", {"delta_x": 0, "delta_y": -100})
|
||||
|
||||
# Web search
|
||||
await interface.playwright_exec("web_search", {"query": "computer use agent"})
|
||||
"""
|
||||
protocol = "https" if self.api_key else "http"
|
||||
port = "8443" if self.api_key else "8000"
|
||||
url = f"{protocol}://{self.ip_address}:{port}/playwright_exec"
|
||||
|
||||
payload = {"command": command, "params": params or {}}
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if self.api_key:
|
||||
headers["X-API-Key"] = self.api_key
|
||||
if self.vm_name:
|
||||
headers["X-Container-Name"] = self.vm_name
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, json=payload, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
return await response.json()
|
||||
else:
|
||||
error_text = await response.text()
|
||||
return {"success": False, "error": error_text}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
# Websocket Methods
|
||||
async def _keep_alive(self):
|
||||
"""Keep the WebSocket connection alive with automatic reconnection."""
|
||||
|
||||
@@ -45,7 +45,9 @@ class CloudProvider(BaseVMProvider):
|
||||
# Fall back to environment variable if api_key not provided
|
||||
if api_key is None:
|
||||
api_key = os.getenv("CUA_API_KEY")
|
||||
assert api_key, "api_key required for CloudProvider (provide via parameter or CUA_API_KEY environment variable)"
|
||||
assert (
|
||||
api_key
|
||||
), "api_key required for CloudProvider (provide via parameter or CUA_API_KEY environment variable)"
|
||||
self.api_key = api_key
|
||||
self.verbose = verbose
|
||||
self.api_base = (api_base or DEFAULT_API_BASE).rstrip("/")
|
||||
|
||||
@@ -19,7 +19,7 @@ dependencies = [
|
||||
"pydantic>=2.11.1",
|
||||
"mslex>=1.3.0",
|
||||
]
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
|
||||
[project.optional-dependencies]
|
||||
lume = [
|
||||
|
||||
@@ -15,7 +15,7 @@ dependencies = [
|
||||
"httpx>=0.24.0",
|
||||
"posthog>=3.20.0"
|
||||
]
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
|
||||
[tool.pdm]
|
||||
distribution = true
|
||||
|
||||
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
|
||||
name = "cua-mcp-server"
|
||||
description = "MCP Server for Computer-Use Agent (CUA)"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
version = "0.1.15"
|
||||
authors = [
|
||||
{name = "TryCua", email = "gh@trycua.com"}
|
||||
|
||||
@@ -24,7 +24,7 @@ dependencies = [
|
||||
"typing-extensions>=4.9.0",
|
||||
"pydantic>=2.6.3"
|
||||
]
|
||||
requires-python = ">=3.12"
|
||||
requires-python = ">=3.12,<3.14"
|
||||
readme = "README.md"
|
||||
license = {text = "AGPL-3.0-or-later"}
|
||||
keywords = ["computer-vision", "ocr", "ui-analysis", "icon-detection"]
|
||||
|
||||
Reference in New Issue
Block a user