Spaces:
Runtime error
Runtime error
| # 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. | |
| """Killer Sudoku 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 KillerSudokuAction, KillerSudokuObservation | |
| class KillerSudokuEnv( | |
| EnvClient[KillerSudokuAction, KillerSudokuObservation, State] | |
| ): | |
| """Client for the Killer Sudoku Environment. | |
| Maintains a persistent WebSocket connection for efficient multi-step | |
| interactions. Each client instance has its own dedicated environment session. | |
| Example: | |
| >>> with KillerSudokuEnv(base_url="http://localhost:8000") as client: | |
| ... result = client.reset() | |
| ... print(result.observation.board_display) | |
| ... | |
| ... result = client.step(KillerSudokuAction( | |
| ... action_type="enter_answer", x=0, y=0, value=5 | |
| ... )) | |
| ... print(result.observation.action_result) | |
| """ | |
| def _step_payload(self, action: KillerSudokuAction) -> Dict: | |
| """Convert KillerSudokuAction to JSON payload for step message.""" | |
| payload: Dict = { | |
| "action_type": action.action_type, | |
| "x": action.x, | |
| "y": action.y, | |
| } | |
| if action.values is not None: | |
| payload["values"] = action.values | |
| if action.value is not None: | |
| payload["value"] = action.value | |
| if action.justification is not None: | |
| payload["justification"] = action.justification | |
| return payload | |
| def _parse_result(self, payload: Dict) -> StepResult[KillerSudokuObservation]: | |
| """Parse server response into StepResult[KillerSudokuObservation].""" | |
| obs_data = payload.get("observation", {}) | |
| observation = KillerSudokuObservation( | |
| board_display=obs_data.get("board_display", ""), | |
| rules_prompt=obs_data.get("rules_prompt", ""), | |
| action_result=obs_data.get("action_result", ""), | |
| candidates=obs_data.get("candidates", {}), | |
| incorrect_answers=obs_data.get("incorrect_answers", 0), | |
| n=obs_data.get("n", 9), | |
| difficulty=obs_data.get("difficulty", 15), | |
| done=payload.get("done", False), | |
| reward=payload.get("reward"), | |
| metadata=obs_data.get("metadata", {}), | |
| ) | |
| return StepResult( | |
| observation=observation, | |
| reward=payload.get("reward"), | |
| done=payload.get("done", False), | |
| ) | |
| def _parse_state(self, payload: Dict) -> State: | |
| """Parse server response into State object.""" | |
| return State( | |
| episode_id=payload.get("episode_id"), | |
| step_count=payload.get("step_count", 0), | |
| ) | |