File size: 1,832 Bytes
08f8699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
"""HTTP/WebSocket client for the PhysiX-Live environment.

Subclasses :class:`openenv.core.EnvClient` to provide PhysiX-specific
serialisation and parsing. The base class handles WebSocket connection,
session management, and the OpenEnv wire protocol.
"""

from __future__ import annotations

from typing import Any

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

from physix.models import PhysiXAction, PhysiXObservation, PhysiXState


class PhysiXEnv(EnvClient[PhysiXAction, PhysiXObservation, PhysiXState]):
    """Client for the PhysiX-Live OpenEnv environment.

    Example::

        >>> async with PhysiXEnv(base_url="http://localhost:8000") as env:
        ...     result = await env.reset()
        ...     while not result.done:
        ...         action = agent.predict(result.observation)
        ...         result = await env.step(action)
    """

    def _step_payload(self, action: PhysiXAction) -> dict[str, Any]:
        return action.model_dump(exclude_none=False)

    def _parse_result(self, payload: dict[str, Any]) -> StepResult[PhysiXObservation]:
        observation_data = payload.get("observation", {}) or {}
        observation = PhysiXObservation(**observation_data)
        return StepResult(
            observation=observation,
            reward=payload.get("reward"),
            done=payload.get("done", False),
        )

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


# Alias for OpenEnv auto-discovery: the convention Pascal-cases the
# manifest `name` field ("physix" -> "Physix"), so AutoEnv looks up
# `physix.client.PhysixEnv`. The actual class is `PhysiXEnv` (capital
# X in the brand). This alias makes both lookups succeed without
# duplicating the implementation.
PhysixEnv = PhysiXEnv