physix-live / physix /client.py
Pratyush-01's picture
Upload folder using huggingface_hub
08f8699 verified
"""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