File size: 3,750 Bytes
20bb95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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.

"""
Snake Environment HTTP Client.

This module provides the client for connecting to a Snake Environment server
over HTTP.
"""

from typing import Any, Dict

# Support both in-repo and standalone imports
try:
    # In-repo imports (when running from OpenEnv repository)
    from core.client_types import StepResult
    from core.env_server.types import State
    from core.http_env_client import HTTPEnvClient

    from .models import SnakeAction, SnakeObservation
except ImportError:
    from models import SnakeAction, SnakeObservation

    # Standalone imports (when environment is standalone with openenv-core from pip)
    from openenv_core.client_types import StepResult
    from openenv_core.env_server.types import State
    from openenv_core.http_env_client import HTTPEnvClient


class SnakeEnv(HTTPEnvClient[SnakeAction, SnakeObservation]):
    """
    HTTP client for the Snake Environment.

    This client connects to a SnakeEnvironment HTTP server and provides
    methods to interact with it: reset(), step(), and state access.

    Example:
        >>> # Connect to a running server
        >>> client = SnakeEnv(base_url="http://localhost:8000")
        >>> result = client.reset()
        >>> print(result.observation.alive)  # True
        >>>
        >>> # Take an action (turn left)
        >>> result = client.step(SnakeAction(action=1))
        >>> print(result.observation.episode_score)
        >>> print(result.reward)

    Example with Docker:
        >>> # Automatically start container and connect
        >>> client = SnakeEnv.from_docker_image("snake-env:latest")
        >>> result = client.reset()
        >>> result = client.step(SnakeAction(action=0))  # noop
    """

    def _step_payload(self, action: SnakeAction) -> Dict:
        """
        Convert SnakeAction to JSON payload for step request.

        Args:
            action: SnakeAction instance

        Returns:
            Dictionary representation suitable for JSON encoding
        """
        return {
            "action": action.action,
        }

    def _parse_result(self, payload: Dict) -> StepResult[SnakeObservation]:
        """
        Parse server response into StepResult[SnakeObservation].

        Args:
            payload: JSON response from server

        Returns:
            StepResult with SnakeObservation
        """
        obs_data = payload.get("observation", {})
        observation = SnakeObservation(
            grid=obs_data.get("grid", []),
            observation=obs_data.get("observation", []),
            episode_score=obs_data.get("episode_score", 0.0),
            episode_steps=obs_data.get("episode_steps", 0),
            episode_fruits=obs_data.get("episode_fruits", 0),
            episode_kills=obs_data.get("episode_kills", 0),
            alive=obs_data.get("alive", True),
            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.

        Args:
            payload: JSON response from /state endpoint

        Returns:
            State object with episode_id and step_count
        """
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )