File size: 7,225 Bytes
9b43224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright (c) 2024 TeeUnit Project
# SPDX-License-Identifier: MIT

"""
TeeUnit Environment Client.

This module provides the client for connecting to a TeeUnit Environment server.
TeeEnv extends MCPToolClient to provide tool-calling style interactions.

Example:
    >>> with TeeEnv(base_url="http://localhost:8000") as env:
    ...     env.reset()
    ...
    ...     # Discover tools
    ...     tools = env.list_tools()
    ...     print([t.name for t in tools])  # ['move', 'jump', 'aim', 'shoot', 'hook', 'get_status']
    ...
    ...     # Get game state
    ...     status = env.call_tool("get_status")
    ...     print(status)
    ...
    ...     # Take actions
    ...     result = env.call_tool("move", direction="right")
    ...     result = env.call_tool("shoot", weapon=2)
"""

from dataclasses import dataclass
from typing import Optional, Any, Dict

# Support both in-repo and standalone imports
try:
    from openenv.core.mcp_client import MCPToolClient
except ImportError:
    # Fallback for development/testing without openenv installed
    class MCPToolClient:
        """Fallback MCPToolClient for development."""
        
        def __init__(self, base_url: str = "http://localhost:8000", **kwargs):
            self.base_url = base_url
            self._connected = False
        
        def __enter__(self):
            self._connected = True
            return self
        
        def __exit__(self, *args):
            self._connected = False
        
        def reset(self, **kwargs):
            pass
        
        def list_tools(self):
            return []
        
        def call_tool(self, name: str, **kwargs):
            return None
        
        def step(self, action):
            return None
        
        def close(self):
            self._connected = False
        
        @classmethod
        def from_docker_image(cls, image: str, **kwargs):
            return cls(**kwargs)
        
        @classmethod
        def from_env(cls, env_id: str, **kwargs):
            return cls(**kwargs)


@dataclass
class TeeAction:
    """
    Action for the TeeUnit environment.
    
    This is a convenience class for building actions to send to the environment.
    The actual actions are sent via MCP tools (move, jump, aim, shoot, hook).
    
    Attributes:
        tool_name: The MCP tool to call
        arguments: Arguments for the tool
    """
    tool_name: str
    arguments: Dict[str, Any]
    
    @classmethod
    def move(cls, direction: str = "none") -> "TeeAction":
        """Create a move action."""
        return cls(tool_name="move", arguments={"direction": direction})
    
    @classmethod
    def jump(cls) -> "TeeAction":
        """Create a jump action."""
        return cls(tool_name="jump", arguments={})
    
    @classmethod
    def aim(cls, x: int, y: int) -> "TeeAction":
        """Create an aim action."""
        return cls(tool_name="aim", arguments={"x": x, "y": y})
    
    @classmethod
    def shoot(cls, weapon: int = -1) -> "TeeAction":
        """Create a shoot action."""
        return cls(tool_name="shoot", arguments={"weapon": weapon})
    
    @classmethod
    def hook(cls) -> "TeeAction":
        """Create a hook action."""
        return cls(tool_name="hook", arguments={})
    
    @classmethod
    def get_status(cls) -> "TeeAction":
        """Create a get_status action."""
        return cls(tool_name="get_status", arguments={})


class TeeEnv(MCPToolClient):
    """
    Client for the TeeUnit Environment.
    
    This client provides a simple interface for interacting with the TeeUnit
    Environment via MCP tools. It inherits all functionality from MCPToolClient:
    - `list_tools()`: Discover available tools
    - `call_tool(name, **kwargs)`: Call a tool by name
    - `reset(**kwargs)`: Reset the environment
    - `step(action)`: Execute an action
    
    Available MCP Tools:
    - `move(direction)`: Move left, right, or none
    - `jump()`: Make the tee jump
    - `aim(x, y)`: Aim at coordinates
    - `shoot(weapon)`: Fire weapon (0-5 or -1 for current)
    - `hook()`: Use grappling hook
    - `get_status()`: Get game state as text
    
    Example:
        >>> # Connect to a running server
        >>> with TeeEnv(base_url="http://localhost:8000") as env:
        ...     env.reset()
        ...
        ...     # List available tools
        ...     tools = env.list_tools()
        ...     for tool in tools:
        ...         print(f"{tool.name}: {tool.description}")
        ...
        ...     # Get game state
        ...     status = env.call_tool("get_status")
        ...     print(status)
        ...
        ...     # Take actions
        ...     env.call_tool("move", direction="right")
        ...     env.call_tool("aim", x=500, y=300)
        ...     env.call_tool("shoot", weapon=2)
    
    Example with HuggingFace Space:
        >>> # Connect to HuggingFace Space
        >>> env = TeeEnv.from_env("ziadbc/teeunit-env")
        >>> try:
        ...     env.reset()
        ...     status = env.call_tool("get_status")
        ...     print(status)
        ... finally:
        ...     env.close()
    
    Example with Docker:
        >>> # Automatically start container and connect
        >>> env = TeeEnv.from_docker_image("teeunit-env:latest")
        >>> try:
        ...     env.reset()
        ...     env.call_tool("move", direction="right")
        ... finally:
        ...     env.close()
    """
    
    def execute_action(self, action: TeeAction) -> Any:
        """
        Execute a TeeAction.
        
        Args:
            action: TeeAction to execute
            
        Returns:
            Result from the MCP tool call
        """
        return self.call_tool(action.tool_name, **action.arguments)
    
    def get_status(self) -> str:
        """
        Get the current game state as text.
        
        Returns:
            Text description of game state
        """
        return self.call_tool("get_status")
    
    def move(self, direction: str = "none") -> str:
        """
        Move the tee.
        
        Args:
            direction: "left", "right", or "none"
            
        Returns:
            Result message
        """
        return self.call_tool("move", direction=direction)
    
    def jump(self) -> str:
        """
        Make the tee jump.
        
        Returns:
            Result message
        """
        return self.call_tool("jump")
    
    def aim(self, x: int, y: int) -> str:
        """
        Aim at coordinates.
        
        Args:
            x: Target X coordinate
            y: Target Y coordinate
            
        Returns:
            Result message
        """
        return self.call_tool("aim", x=x, y=y)
    
    def shoot(self, weapon: int = -1) -> str:
        """
        Fire weapon.
        
        Args:
            weapon: Weapon ID (0-5) or -1 for current weapon
            
        Returns:
            Result message
        """
        return self.call_tool("shoot", weapon=weapon)
    
    def hook(self) -> str:
        """
        Use grappling hook.
        
        Returns:
            Result message
        """
        return self.call_tool("hook")