|
| 1 | +"""Black-box agent flow — base class. |
| 2 | +
|
| 3 | +BlackBoxAgentFlowBase handles the full protocol with Gateway (init_trajectory, |
| 4 | +register_trajectory, complete) and delegates agent execution to subclasses via |
| 5 | +_run_agent. Subclasses only create and run the concrete Agent; they do not |
| 6 | +touch Gateway or implement any task logic. Concrete strategies live in |
| 7 | +separate modules (e.g. gsm8k_agent_flow.py). |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | +import logging |
| 12 | +import os |
| 13 | +from abc import abstractmethod |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +import httpx |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +from claw_r1.agent_flow.agent_flow import AgentFlowBase, register |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) |
| 23 | + |
| 24 | +_DEFAULT_SKIP_KEYS = frozenset({"raw_prompt", "multi_modal_data", "channel", "agent_name"}) |
| 25 | + |
| 26 | + |
| 27 | +class _NumpyEncoder(json.JSONEncoder): |
| 28 | + """JSON encoder that converts numpy scalars to native Python types for HTTP requests.""" |
| 29 | + |
| 30 | + def default(self, o): |
| 31 | + if isinstance(o, np.integer): |
| 32 | + return int(o) |
| 33 | + if isinstance(o, np.floating): |
| 34 | + return float(o) |
| 35 | + if isinstance(o, np.ndarray): |
| 36 | + return o.tolist() |
| 37 | + return super().default(o) |
| 38 | + |
| 39 | + |
| 40 | +class BlackBoxAgentFlowBase(AgentFlowBase): |
| 41 | + """Base class for black-box agent flows. |
| 42 | +
|
| 43 | + Handles generic parameter processing and the full Gateway protocol: |
| 44 | + init_trajectory (get base_url) -> register_trajectory (channel + metadata) |
| 45 | + -> call subclass _run_agent -> complete. Subclasses only implement |
| 46 | + _run_agent to create and run the concrete Agent. |
| 47 | + """ |
| 48 | + |
| 49 | + def _prepare_params(self, kwargs: dict[str, Any]) -> tuple[str | None, str, dict[str, Any]]: |
| 50 | + """Extract channel, prompt_uid, and metadata from kwargs.""" |
| 51 | + channel = kwargs.pop("channel", None) |
| 52 | + prompt_uid = str(kwargs.get("uid", "1")) |
| 53 | + metadata = {k: v for k, v in kwargs.items() if k not in _DEFAULT_SKIP_KEYS} |
| 54 | + return channel, prompt_uid, metadata |
| 55 | + |
| 56 | + async def run(self, sampling_params: dict[str, Any], **kwargs) -> int: |
| 57 | + channel, prompt_uid, metadata = self._prepare_params(kwargs) |
| 58 | + |
| 59 | + async with httpx.AsyncClient(timeout=30.0) as http: |
| 60 | + # 1. Allocate trajectory — get base_url with trajectory_uid embedded. |
| 61 | + init_resp = await http.post(f"{self.gateway_url}/init_trajectory") |
| 62 | + init_resp.raise_for_status() |
| 63 | + init_data = init_resp.json() |
| 64 | + base_url_from_init = init_data["base_url"] |
| 65 | + # base_url_from_init is http://host:port/{traj_uid}/{default_prompt_uid}/v1 |
| 66 | + # Replace the default prompt_uid with the actual one. |
| 67 | + parts = base_url_from_init.rsplit("/", 2) # [...base, prompt_uid, "v1"] |
| 68 | + base_url = f"{parts[0]}/{prompt_uid}/v1" |
| 69 | + |
| 70 | + # 2. Register channel + metadata via base_url. |
| 71 | + reg_body: dict[str, Any] = {} |
| 72 | + if channel: |
| 73 | + reg_body["channel"] = channel |
| 74 | + if metadata: |
| 75 | + reg_body["metadata"] = metadata |
| 76 | + payload = json.dumps(reg_body, cls=_NumpyEncoder).encode() |
| 77 | + await http.post( |
| 78 | + f"{base_url}/register_trajectory", |
| 79 | + content=payload, |
| 80 | + headers={"content-type": "application/json"}, |
| 81 | + ) |
| 82 | + |
| 83 | + # 3. Run the concrete agent. |
| 84 | + try: |
| 85 | + num_turns = await self._run_agent(base_url, kwargs) |
| 86 | + finally: |
| 87 | + # 4. Mark trajectory complete. |
| 88 | + async with httpx.AsyncClient(timeout=httpx.Timeout(600.0)) as http: |
| 89 | + await http.post(f"{base_url}/complete_trajectory") |
| 90 | + |
| 91 | + return num_turns |
| 92 | + |
| 93 | + @abstractmethod |
| 94 | + async def _run_agent(self, base_url: str, kwargs: dict[str, Any]) -> int: |
| 95 | + """Create and run the concrete Agent. Subclasses implement this.""" |
| 96 | + raise NotImplementedError |
0 commit comments