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.1 KB
Raw Blame
"""{{agent_name}} - Custom agent implementation."""

import cua_bench as cb
from cua_bench.agents import BaseAgent, AgentResult, FailureMode
from pathlib import Path
import base64


class {{class_name}}(BaseAgent):
    """Custom agent that performs computer-use tasks.

    This agent takes screenshots, analyzes them, and decides what actions to take.
    Customize the _decide_action method to implement your own logic.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.max_steps = kwargs.get("max_steps", 20)

    @staticmethod
    def name() -> str:
        return "{{agent_name}}"

    async def perform_task(
        self,
        task_description: str,
        session: cb.DesktopSession,
        logging_dir: Path | None = None,
    ) -> AgentResult:
        """Perform the task by taking screenshots and executing actions.

        Args:
            task_description: Natural language description of the task
            session: Desktop session for taking screenshots and executing actions
            logging_dir: Optional directory to save screenshots for debugging

        Returns:
            AgentResult with token usage and completion status
        """
        total_input_tokens = 0
        total_output_tokens = 0

        for step in range(self.max_steps):
            # Take a screenshot
            screenshot_bytes = await session.screenshot()

            # Save screenshot for debugging
            if logging_dir:
                screenshot_path = logging_dir / f"step_{step}.png"
                screenshot_path.write_bytes(screenshot_bytes)

            # Decide what action to take
            action = await self._decide_action(
                task_description,
                screenshot_bytes,
                step
            )

            if action is None:
                # Agent thinks task is complete
                break

            # Execute the action
            await session.execute_action(action)

        return AgentResult(
            total_input_tokens=total_input_tokens,
            total_output_tokens=total_output_tokens,
            failure_mode=FailureMode.NONE
        )

    async def _decide_action(
        self,
        task_description: str,
        screenshot: bytes,
        step: int
    ) -> cb.Action | None:
        """Decide what action to take based on the screenshot.

        This is where you implement your agent's intelligence.
        Options:
        1. Simple heuristics (click center, look for buttons)
        2. Computer vision (detect UI elements)
        3. LLM-based reasoning (analyze screenshot with vision model)

        Args:
            task_description: What the agent is trying to accomplish
            screenshot: PNG bytes of current screen state
            step: Current step number (0-indexed)

        Returns:
            Action to execute, or None if task is complete
        """
        # TODO: Implement your agent logic here
        # This simple example just clicks the center on first step
        if step == 0:
            return cb.ClickAction(x=256, y=256)

        # After first click, assume done
        return None