The environment class
sweagent.environment.swe_env.SWEEnv
SWEEnv(*, deployment: AbstractDeployment, repo: Repo | RepoConfig | None, post_startup_commands: list[str], post_startup_command_timeout: int = 500, hooks: list[EnvHook] | None = None, name: str = 'main')
This class represents the environment in which we solve the tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
deployment
|
AbstractDeployment
|
SWE-ReX deployment instance |
required |
repo
|
Repo | RepoConfig | None
|
Repository configuration object, or anything following the |
required |
post_startup_commands
|
list[str]
|
Commands to execute before starting the agent |
required |
hooks
|
list[EnvHook] | None
|
Environment hooks (used to inject custom functionality)
Equivalent to calling |
None
|
name
|
str
|
Name of the environment |
'main'
|
Source code in sweagent/environment/swe_env.py
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 |
|
clean_multi_line_functions
instance-attribute
clean_multi_line_functions = lambda x: x
deployment
instance-attribute
deployment = deployment
logger
instance-attribute
logger = get_logger('swea-env', emoji='���')
name
instance-attribute
name = name
post_startup_command_timeout
instance-attribute
post_startup_command_timeout = post_startup_command_timeout
repo
instance-attribute
repo = repo
add_hook
add_hook(hook: EnvHook) -> None
Add EnvHook
to the environment.
This allows to inject custom functionality at different stages of the environment lifecycle, in particular to connect SWE-agent to a new interface (like a GUI).
Source code in sweagent/environment/swe_env.py
100 101 102 103 104 105 106 107 |
|
close
close() -> None
Shutdown SWE-ReX deployment etc.
Source code in sweagent/environment/swe_env.py
169 170 171 172 173 |
|
communicate
communicate(input: str, timeout: int | float = 25, *, check: Literal['warn', 'ignore', 'raise'] = 'ignore', error_msg: str = 'Command failed') -> str
Executes a command in the running shell. The details of this are handled by the SWE-ReX deployment/runtime.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
str
|
input to send to container |
required |
timeout_duration
|
duration to wait for output |
required | |
check
|
Literal['warn', 'ignore', 'raise']
|
|
'ignore'
|
error_msg
|
str
|
error message to raise if the command fails |
'Command failed'
|
Returns:
Name | Type | Description |
---|---|---|
output |
str
|
output from container |
Source code in sweagent/environment/swe_env.py
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 |
|
execute_command
execute_command(command: str, shell: bool = True, check: bool = False, env: dict[str, str] | None = None, cwd: str | None = None) -> None
Execute a command in the environment independent of the session (i.e., as a subprocess)
Source code in sweagent/environment/swe_env.py
262 263 264 265 266 267 268 269 270 271 272 273 |
|
from_config
classmethod
from_config(config: EnvironmentConfig) -> Self
Create an environment instance from a configuration object. This is the recommended way to create an environment instance, unless you need more flexibility.
Source code in sweagent/environment/swe_env.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
hard_reset
hard_reset()
Resets the environment and deployment, i.e., completely restarts the deployment.
Source code in sweagent/environment/swe_env.py
128 129 130 131 132 133 |
|
interrupt_session
interrupt_session()
Source code in sweagent/environment/swe_env.py
189 190 191 |
|
read_file
read_file(path: str | PurePath, encoding: str | None = None, errors: str | None = None) -> str
Read file contents from container
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | PurePath
|
Absolute path to file |
required |
encoding
|
str | None
|
Encoding to use when reading the file. None means default encoding.
This is the same as the |
None
|
errors
|
str | None
|
Error handling to use when reading the file. None means default error handling.
This is the same as the |
None
|
Returns:
Name | Type | Description |
---|---|---|
file_contents |
str
|
Contents of file as string |
Source code in sweagent/environment/swe_env.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
reset
reset()
Reset the environment to a clean state.
Gets called by start
, but can also be called independently to reset the
environment to a clean state before a new attempt.
Returns:
Name | Type | Description |
---|---|---|
observation |
output from container |
|
info |
additional information (e.g. debugging information) |
Source code in sweagent/environment/swe_env.py
135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
set_env_variables
set_env_variables(env_variables: dict[str, str]) -> None
Set environment variables in the environment.
Source code in sweagent/environment/swe_env.py
253 254 255 256 257 258 259 260 |
|
start
start() -> None
Start the environment and reset it to a clean state.
Source code in sweagent/environment/swe_env.py
109 110 111 112 113 114 |
|
write_file
write_file(path: str | PurePath, content: str) -> None
Write content to file in container
Source code in sweagent/environment/swe_env.py
249 250 251 |
|