|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +""" |
| 8 | +Chess Environment Client. |
| 9 | +
|
| 10 | +This module provides the client for connecting to a Chess Environment server |
| 11 | +via WebSocket for persistent sessions. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from typing import Any, Dict |
| 17 | + |
| 18 | +from openenv.core.client_types import StepResult |
| 19 | +from openenv.core.env_client import EnvClient |
| 20 | + |
| 21 | +from .models import ChessAction, ChessObservation, ChessState |
| 22 | + |
| 23 | + |
| 24 | +class ChessEnv(EnvClient[ChessAction, ChessObservation, ChessState]): |
| 25 | + """ |
| 26 | + Client for Chess Environment. |
| 27 | +
|
| 28 | + This client maintains a persistent WebSocket connection to the environment |
| 29 | + server, enabling efficient multi-step interactions with lower latency. |
| 30 | +
|
| 31 | + Uses the moonfish chess engine for opponent moves and position evaluation. |
| 32 | +
|
| 33 | + Example: |
| 34 | + >>> with ChessEnv(base_url="http://localhost:8000") as client: |
| 35 | + ... result = client.reset() |
| 36 | + ... print(result.observation.fen) |
| 37 | + ... print(result.observation.legal_moves) |
| 38 | + ... |
| 39 | + ... result = client.step(ChessAction(move="e2e4")) |
| 40 | + ... print(result.reward, result.done) |
| 41 | + """ |
| 42 | + |
| 43 | + def _step_payload(self, action: ChessAction) -> Dict[str, Any]: |
| 44 | + """ |
| 45 | + Convert ChessAction to JSON payload for step request. |
| 46 | +
|
| 47 | + Args: |
| 48 | + action: ChessAction instance with UCI move string. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Dictionary representation suitable for JSON encoding. |
| 52 | + """ |
| 53 | + return { |
| 54 | + "move": action.move, |
| 55 | + } |
| 56 | + |
| 57 | + def _parse_result(self, payload: Dict[str, Any]) -> StepResult[ChessObservation]: |
| 58 | + """ |
| 59 | + Parse server response into StepResult[ChessObservation]. |
| 60 | +
|
| 61 | + Args: |
| 62 | + payload: JSON response from server. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + StepResult with ChessObservation. |
| 66 | + """ |
| 67 | + obs_data = payload.get("observation", {}) |
| 68 | + |
| 69 | + observation = ChessObservation( |
| 70 | + fen=obs_data.get("fen", ""), |
| 71 | + legal_moves=obs_data.get("legal_moves", []), |
| 72 | + is_check=obs_data.get("is_check", False), |
| 73 | + done=obs_data.get("done", False), |
| 74 | + reward=obs_data.get("reward", 0.0), |
| 75 | + result=obs_data.get("result"), |
| 76 | + metadata=obs_data.get("metadata", {}), |
| 77 | + ) |
| 78 | + |
| 79 | + return StepResult( |
| 80 | + observation=observation, |
| 81 | + reward=observation.reward, |
| 82 | + done=observation.done, |
| 83 | + ) |
| 84 | + |
| 85 | + def _parse_state(self, payload: Dict[str, Any]) -> ChessState: |
| 86 | + """ |
| 87 | + Parse server response into ChessState object. |
| 88 | +
|
| 89 | + Args: |
| 90 | + payload: JSON response from /state endpoint. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + ChessState object with environment state information. |
| 94 | + """ |
| 95 | + return ChessState( |
| 96 | + episode_id=payload.get("episode_id", ""), |
| 97 | + fen=payload.get("fen", ""), |
| 98 | + current_player=payload.get("current_player", "white"), |
| 99 | + move_history=payload.get("move_history", []), |
| 100 | + step_count=payload.get("step_count", 0), |
| 101 | + ) |
0 commit comments