Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
"""Agent management commands for cua-bench CLI."""
from ._help import examples
import re
from pathlib import Path
# ANSI color codes
RESET = "\033[0m"
BOLD = "\033[1m"
CYAN = "\033[36m"
GREEN = "\033[92m"
YELLOW = "\033[33m"
RED = "\033[91m"
GREY = "\033[90m"
def register_parser(subparsers):
"""Register the agent subcommand and its subcommands."""
agent_parser = subparsers.add_parser(
"agent",
help="Scaffold custom agents",
description="Create and manage custom agents for cua-bench.",
**examples(("Scaffold an agent in ./my-agent", "cb agent init my-agent")),
)
agent_subparsers = agent_parser.add_subparsers(
dest="agent_command",
title="commands",
description="Available agent commands",
)
# cb agent init <name>
init_parser = agent_subparsers.add_parser(
"init",
help="Create a new agent from template",
description="Scaffold a new custom agent with working sample code.",
**examples(
("Scaffold an agent in ./my-agent", "cb agent init my-agent"),
(
"Scaffold it into another directory",
"cb agent init my-agent --output-dir ./agents/my-agent",
),
),
)
init_parser.add_argument(
"name",
help="Agent name (e.g., 'my-agent')",
)
init_parser.add_argument(
"--output-dir",
"-o",
help="Output directory (default: ./<name>)",
)
# cb agent build <path> (deprecated)
build_parser = agent_subparsers.add_parser(
"build",
help="Deprecated: cb run executes agents in-process",
description="Build a Docker image for an agent directory containing a Dockerfile.",
**examples(
("Build an agent image (deprecated)", "cb agent build ./my-agent --tag my-agent:dev")
),
)
build_parser.add_argument(
"path",
help="Path to agent directory (containing Dockerfile)",
)
build_parser.add_argument(
"--tag",
"-t",
help="Image tag (default: agent directory name)",
)
def execute(args):
"""Execute agent subcommands."""
if not hasattr(args, "agent_command") or args.agent_command is None:
print("Usage: cb agent <command>")
print("Commands: init, build")
print("Run 'cb agent <command> --help' for more information.")
return 1
if args.agent_command == "init":
return create_agent_scaffold(args.name, args.output_dir)
elif args.agent_command == "build":
return build_agent(args.path, getattr(args, "tag", None))
print(f"Unknown agent command: {args.agent_command}")
return 1
def build_agent(agent_path: str, tag: str | None = None) -> int:
"""Deprecated: ``cb run`` runs agents in-process (no agent images)."""
print(
f"{YELLOW}cb agent build is deprecated and does nothing: cb run executes agents in "
f"this Python environment. Install the agent package (pip install -e {agent_path}) and "
f"run it with --agent-import-path module:Class, or give it an import_path in "
f".cua/agents.yaml.{RESET}"
)
return 0
def create_agent_scaffold(name: str, output_dir: str | None = None) -> int:
"""Create a new agent from template.
Creates a complete agent package with:
- agent.py: Agent implementation (BaseAgent subclass)
- main.py: Container entry point
- Dockerfile: For building agent container
- requirements.txt: Python dependencies
- .dockerignore: Docker build ignores
Args:
name: Agent name (e.g., 'my-agent')
output_dir: Output directory (default: ./<name>)
Returns:
Exit code (0 for success, 1 for error)
"""
# Validate agent name
if not re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", name):
print(f"{RED}Error: Invalid agent name '{name}'{RESET}")
print(
"Agent name must start with a letter and contain only letters, numbers, underscores, and hyphens."
)
return 1
# Determine output directory
if output_dir:
agent_dir = Path(output_dir)
else:
agent_dir = Path.cwd() / name
# Check if directory already exists
if agent_dir.exists():
print(f"{RED}Error: Directory already exists: {agent_dir}{RESET}")
return 1
# Create class name from agent name
# my-agent -> MyAgent, my_agent -> MyAgent
class_name = "".join(word.capitalize() for word in re.split(r"[-_]", name))
# Get templates directory
templates_dir = Path(__file__).parent.parent / "templates" / "agent"
if not templates_dir.exists():
print(f"{RED}Error: Templates directory not found: {templates_dir}{RESET}")
return 1
# Create agent directory
agent_dir.mkdir(parents=True, exist_ok=True)
# Template variables
variables = {
"{{agent_name}}": name,
"{{class_name}}": class_name,
}
# Copy and process templates
template_files = [
("__init__.py.template", "__init__.py"),
("agent.py.template", "agent.py"),
("main.py.template", "main.py"),
("requirements.txt.template", "requirements.txt"),
("Dockerfile.template", "Dockerfile"),
(".dockerignore.template", ".dockerignore"),
]
for template_name, output_name in template_files:
template_path = templates_dir / template_name
output_path = agent_dir / output_name
if not template_path.exists():
print(f"{YELLOW}Warning: Template not found: {template_path}{RESET}")
continue
# Read and process template
content = template_path.read_text()
for placeholder, value in variables.items():
content = content.replace(placeholder, value)
# Write output file
output_path.write_text(content)
print(f"{GREEN}✓ Created agent '{name}' at {agent_dir}{RESET}")
print()
print(f"{BOLD}Next steps:{RESET}")
print(f" 1. {CYAN}cd {agent_dir}{RESET}")
print(f" 2. Edit {BOLD}agent.py{RESET} to implement your agent logic")
print(f" 3. Add dependencies to {BOLD}requirements.txt{RESET}")
print()
print(f"{BOLD}Build and run locally:{RESET}")
print(f" {CYAN}cb agent build ./{name}{RESET}")
print(f" {CYAN}cb run <task> --agent-import-path ./{name}{RESET}")
return 0