44# This source code is licensed under the BSD-style license found in the
55# LICENSE file in the root directory of this source tree.
66
7- import traceback
87from dataclasses import dataclass , field
98from typing import Any , Dict , List , Optional , Union
109
@@ -20,57 +19,6 @@ class Action:
2019 metadata : Dict [str , Any ] = field (default_factory = dict )
2120
2221
23- @dataclass (kw_only = True )
24- class CodeAction (Action ):
25- """Action containing Python code to execute in a CodeAct environment."""
26-
27- code : str
28-
29- def __post_init__ (self ):
30- if not self .code or not self .code .strip ():
31- raise ValueError ("code is required and cannot be empty" )
32-
33-
34- @dataclass
35- class ExecutionResult :
36- """Result of executing Python code."""
37-
38- stdout : str = ""
39- stderr : str = ""
40- exit_code : int = 0
41- execution_time_ms : float = 0.0
42-
43- @classmethod
44- def from_exception (
45- cls , exc : Exception , stdout : str = "" , stderr : str = ""
46- ) -> "ExecutionResult" :
47- return cls (
48- stdout = stdout ,
49- stderr = stderr ,
50- exception = exc ,
51- exception_type = exc .__class__ .__name__ ,
52- exception_message = str (exc ),
53- traceback_str = traceback .format_exc (),
54- success = False ,
55- )
56-
57- @classmethod
58- def from_success (
59- cls ,
60- return_value : Any = None ,
61- stdout : str = "" ,
62- stderr : str = "" ,
63- execution_time_ms : float = 0.0 ,
64- ) -> "ExecutionResult" :
65- return cls (
66- stdout = stdout ,
67- stderr = stderr ,
68- return_value = return_value ,
69- execution_time_ms = execution_time_ms ,
70- success = True ,
71- )
72-
73-
7422@dataclass (kw_only = True )
7523class Observation :
7624 """Base class for all environment observations."""
@@ -80,31 +28,9 @@ class Observation:
8028 metadata : Dict [str , Any ] = field (default_factory = dict )
8129
8230
83- @dataclass (kw_only = True )
84- class CodeObservation (Observation ):
85- """Observation from CodeAct environment execution."""
86-
87- execution_result : ExecutionResult = field (default_factory = ExecutionResult )
88- available_tools : List [str ] = field (default_factory = list )
89-
90-
9131@dataclass
9232class State :
9333 """Base class for environment state."""
9434
9535 episode_id : Optional [str ] = None
9636 step_count : int = 0
97- metadata : Dict [str , Any ] = field (default_factory = dict )
98-
99-
100- @dataclass
101- class CodeState (State ):
102- """State for CodeAct environment with persistent execution context."""
103-
104- execution_globals : Dict [str , Any ] = field (default_factory = dict )
105- action_history : List [CodeAction ] = field (default_factory = list )
106- result_history : List [ExecutionResult ] = field (default_factory = list )
107-
108- def __post_init__ (self ):
109- if not self .execution_globals :
110- self .execution_globals = {"__builtins__" : __builtins__ }
0 commit comments