# Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. """Smart Grid Environment Client.""" from typing import Dict from openenv.core import EnvClient from openenv.core.client_types import StepResult from openenv.core.env_server.types import State from models import SmartGridAction, SmartGridObservation class SmartGridEnv( EnvClient[SmartGridAction, SmartGridObservation, State] ): """ Client for the Smart Grid Environment. This client maintains a persistent WebSocket connection to the environment server, enabling efficient multi-step interactions with lower latency. Each client instance has its own dedicated environment session on the server. """ def _step_payload(self, action: SmartGridAction) -> Dict: """ Convert SmartGridAction to JSON payload for step message. Args: action: SmartGridAction instance Returns: Dictionary representation suitable for JSON encoding """ return action.model_dump() def _parse_result(self, payload: Dict) -> StepResult[SmartGridObservation]: """ Parse server response into StepResult[SmartGridObservation]. Args: payload: JSON response data from server Returns: StepResult with SmartGridObservation """ obs_data = payload.get("observation", {}) observation = SmartGridObservation(**obs_data) return StepResult( observation=observation, reward=observation.reward, done=observation.done, ) def _parse_state(self, payload: Dict) -> State: """ Parse server response into State object. Args: payload: JSON response from state request Returns: State object with episode_id and step_count """ return State( episode_id=payload.get("episode_id"), step_count=payload.get("step_count", 0), )