File size: 1,405 Bytes
0a6c641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""WebSocket client for CERNenv.



Wraps OpenEnv's ``EnvClient`` so users can ``await client.reset()`` and

``await client.step(action)`` against a running CERNenv server.



Only public schemas from ``models`` are imported here — by design this

client never reaches into ``server.*`` internals so it can be installed

on the consumer side without pulling the simulator code.

"""

from __future__ import annotations

from typing import Any, Dict

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

from models import CernState, CollisionObservation, ExperimentAction


class CernEnv(EnvClient[ExperimentAction, CollisionObservation, CernState]):
    """Async WebSocket client for the CERN environment."""

    def _step_payload(self, action: ExperimentAction) -> Dict[str, Any]:
        return action.model_dump()

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[CollisionObservation]:
        obs_data = payload.get("observation", payload)
        observation = CollisionObservation(**obs_data)
        return StepResult(
            observation=observation,
            reward=payload.get("reward", observation.reward),
            done=payload.get("done", observation.done),
        )

    def _parse_state(self, payload: Dict[str, Any]) -> CernState:
        return CernState(**payload)


__all__ = ["CernEnv"]