File size: 1,651 Bytes
41a9651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
OpenEnv client for the car racing environment.

Async usage:
    async with RaceEnvClient(base_url="http://localhost:8000") as client:
        result = await client.reset()
        result = await client.step(DriveAction(accel=1.0, steer=0.0))

Sync usage:
    with RaceEnvClient(base_url="http://localhost:8000").sync() as client:
        result = client.reset()
        result = client.step(DriveAction(accel=1.0, steer=0.0))
"""

from typing import Any, Dict

from openenv.core.env_client import EnvClient
from openenv.core.client_types import StepResult

from .models import DriveAction, RaceObservation, RaceState


class RaceEnvClient(EnvClient[DriveAction, RaceObservation, RaceState]):
    """Client for a running RaceEnvironment server.

    Communicates via WebSocket with a FastAPI server hosting the
    RaceEnvironment. Handles serialization of DriveAction and
    deserialization of RaceObservation/RaceState.
    """

    def _step_payload(self, action: DriveAction) -> Dict[str, Any]:
        """Convert DriveAction to JSON dict for the server."""
        return action.model_dump()

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[RaceObservation]:
        """Parse server response into StepResult[RaceObservation]."""
        obs_data = payload.get("observation", payload)
        return StepResult(
            observation=RaceObservation(**obs_data),
            reward=payload.get("reward"),
            done=payload.get("done", False),
        )

    def _parse_state(self, payload: Dict[str, Any]) -> RaceState:
        """Parse server state response into RaceState."""
        return RaceState(**payload)