Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

3.4 KB
Raw Blame
"""{{agent_name}} - Container entry point."""

import asyncio
import os
import sys
from pathlib import Path


async def main():
    """Run the agent in container mode.

    This entry point is called when the agent runs as a Docker container.
    It receives configuration via environment variables set by TaskRunner:
    - CUA_ENV_API_URL: API endpoint for the environment container
    - CUA_ENV_VNC_URL: VNC endpoint for debugging
    - CUA_TASK_PATH: Path to mounted task directory
    - CUA_TASK_INDEX: Task index to run
    - CUA_MODEL: Model to use (optional)
    - CUA_MAX_STEPS: Maximum steps (optional)
    """
    from cua_bench.sessions import RemoteDesktopSession

    # Get config from environment
    api_url = os.environ.get("CUA_ENV_API_URL")
    task_path = os.environ.get("CUA_TASK_PATH", "/app/env")
    task_index = int(os.environ.get("CUA_TASK_INDEX", "0"))
    model = os.environ.get("CUA_MODEL")
    max_steps = int(os.environ.get("CUA_MAX_STEPS", "100"))

    if not api_url:
        print("Error: CUA_ENV_API_URL environment variable not set")
        print("This entry point is designed to run inside a Docker container")
        print("managed by cua-bench TaskRunner.")
        sys.exit(1)

    print(f"{{agent_name}} starting...")
    print(f"  Environment API: {api_url}")
    print(f"  Task path: {task_path}")
    print(f"  Task index: {task_index}")
    print(f"  Model: {model or 'default'}")
    print(f"  Max steps: {max_steps}")

    # Load task config
    try:
        from cua_bench import make
        env = make(task_path)
        tasks = env.tasks_config_fn() if env.tasks_config_fn else []
        task = tasks[task_index] if task_index < len(tasks) else None

        if not task:
            print(f"Error: Task index {task_index} not found (available: {len(tasks)})")
            sys.exit(1)

        task_description = task.description
        print(f"  Task: {task_description[:100]}...")
    except Exception as e:
        print(f"Error loading task: {e}")
        sys.exit(1)

    # Create remote session to environment container
    print("Connecting to environment...")
    session = RemoteDesktopSession(api_url=api_url)

    try:
        await session.connect()
        print("Connected to environment")

        # Import and run agent
        from .agent import {{class_name}}

        agent_kwargs = {"max_steps": max_steps}
        if model:
            agent_kwargs["model"] = model

        agent = {{class_name}}(**agent_kwargs)
        print(f"Running agent: {agent.name()}")

        # Create logging directory if output is mounted
        logging_dir = None
        output_mount = Path("/tmp/td_output")
        if output_mount.exists():
            logging_dir = output_mount
            print(f"  Logging to: {logging_dir}")

        # Run agent
        result = await agent.perform_task(
            task_description=task_description,
            session=session,
            logging_dir=logging_dir,
        )

        print(f"Agent completed: {result}")
        print(f"  Input tokens: {result.total_input_tokens}")
        print(f"  Output tokens: {result.total_output_tokens}")
        print(f"  Failure mode: {result.failure_mode}")

    except Exception as e:
        print(f"Error running agent: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

    finally:
        await session.close()


if __name__ == "__main__":
    asyncio.run(main())