|
| 1 | +"""Internal agent for online training mode. |
| 2 | +
|
| 3 | +``_OnlineAgent`` is NOT intended for direct use. It is automatically |
| 4 | +created by ``OpenAIProxyWorkflow`` when ``mode="online"``. It registers |
| 5 | +the assigned proxy worker as "ready" on the proxy gateway, then blocks |
| 6 | +until an external user completes a full session lifecycle. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +import aiohttp |
| 14 | + |
| 15 | +from areal.infra import workflow_context |
| 16 | + |
| 17 | +from .proxy_gateway import CompletedSessionInfo |
| 18 | +from .server import INTERNAL_WAIT_FOR_SESSION_PATHNAME |
| 19 | + |
| 20 | + |
| 21 | +class _OnlineAgent: |
| 22 | + """Internal agent that waits for external user sessions. |
| 23 | +
|
| 24 | + Registers the assigned proxy worker as "ready" on the proxy |
| 25 | + gateway, then blocks until an external user completes a full session |
| 26 | + lifecycle (start_session → interact → set_reward → end_session) on |
| 27 | + that worker. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + proxy_gateway_addr : str |
| 32 | + HTTP address of the proxy gateway server. |
| 33 | + admin_api_key : str |
| 34 | + Admin API key for authenticating with the proxy gateway. |
| 35 | + timeout : float |
| 36 | + Maximum seconds to wait for a session completion. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + proxy_gateway_addr: str, |
| 42 | + admin_api_key: str, |
| 43 | + timeout: float = 3600.0, |
| 44 | + ): |
| 45 | + self.proxy_gateway_addr = proxy_gateway_addr |
| 46 | + self.admin_api_key = admin_api_key |
| 47 | + self.timeout = timeout |
| 48 | + |
| 49 | + async def run( |
| 50 | + self, data: dict[str, Any], **extra_kwargs: Any |
| 51 | + ) -> CompletedSessionInfo: |
| 52 | + """Wait for an external user to complete a session. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + data : dict |
| 57 | + Ignored in online mode (dataloader yields empty dicts). |
| 58 | + extra_kwargs : dict |
| 59 | + Provided by ``OpenAIProxyWorkflow``: |
| 60 | +
|
| 61 | + - ``base_url``: proxy worker address |
| 62 | + - ``api_key``: admin API key |
| 63 | + - ``http_client``: ``httpx.AsyncClient`` (unused here) |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + CompletedSessionInfo |
| 68 | + Session credentials for trajectory export. |
| 69 | + """ |
| 70 | + base_url = extra_kwargs["base_url"] # proxy worker addr |
| 71 | + |
| 72 | + url = f"{self.proxy_gateway_addr}/{INTERNAL_WAIT_FOR_SESSION_PATHNAME}" |
| 73 | + headers = {"Authorization": f"Bearer {self.admin_api_key}"} |
| 74 | + payload = {"worker_addr": base_url} |
| 75 | + |
| 76 | + timeout = aiohttp.ClientTimeout(total=self.timeout) |
| 77 | + session = await workflow_context.get_aiohttp_session() |
| 78 | + async with session.post( |
| 79 | + url, |
| 80 | + json=payload, |
| 81 | + headers=headers, |
| 82 | + timeout=timeout, |
| 83 | + ) as resp: |
| 84 | + resp.raise_for_status() |
| 85 | + result = await resp.json() |
| 86 | + return CompletedSessionInfo(**result) |
0 commit comments