# 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. """Python Env Environment Client.""" from typing import Any, Dict from openenv.core import EnvClient from openenv.core.client_types import StepResult from openenv.core.env_server.types import State try: from .models import PythonAction, PythonObservation except ImportError: from models import PythonAction, PythonObservation # type: ignore class PythonEnv(EnvClient[PythonAction, PythonObservation, State]): """Typed client for the Python code-review environment.""" def _step_payload(self, action: PythonAction) -> Dict[str, Any]: """Convert a validated action model to the JSON payload expected by the server.""" return action.model_dump(exclude_none=True) def _parse_result(self, payload: Dict[str, Any]) -> StepResult[PythonObservation]: """Parse a server response into a typed step result.""" obs_data = dict(payload.get("observation", {})) obs_data.setdefault("done", payload.get("done", False)) obs_data.setdefault("reward", payload.get("reward")) observation = PythonObservation.model_validate(obs_data) return StepResult( observation=observation, reward=payload.get("reward"), done=payload.get("done", False), ) def _parse_state(self, payload: Dict[str, Any]) -> State: """Parse the server state payload into the shared state model.""" return State.model_validate(payload)