-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentManager.py
291 lines (251 loc) · 9.94 KB
/
AgentManager.py
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Agent Manager module for handling agent execution and communication.
This module provides classes for managing agent sessions, runtime status,
and communication between agents and the frontend/backend systems.
"""
import time
import threading
from dataclasses import dataclass
from contextlib import contextmanager
from typing import Any, Dict, Optional, Union
from flask_socketio import SocketIO
# Determine environment type
ENV_TYPE = ""
try:
from backend.agents.observation.obs_utils import encode_image
from backend.agents.utils.exceptions import StopExecution
from backend.agents.utils.schemas import AgentStatus
from backend.api.data_manager import Conversation, Session
from backend.main import env_managers, user_manager
ENV_TYPE = "deploy"
except ImportError:
from observation.obs_utils import encode_image
from utils.exceptions import StopExecution
from utils.schemas import AgentStatus
ENV_TYPE = "local"
@dataclass
class SessionConfig:
"""Configuration for an agent session.
Attributes:
user_id: Unique identifier for the user
session_id: Unique identifier for the session
region: Geographic region for the session
agent_idx: Index of the agent in the session
session: Optional Session object for deployment
conversation: Optional Conversation object for deployment
socketio: Optional SocketIO instance for real-time communication
stop_event: Optional threading Event for stopping execution
"""
user_id: str
session_id: str
region: str
agent_idx: int
session: Any
conversation: Any
socketio: Optional[SocketIO] = None
stop_event: Optional[threading.Event] = None
class AgentManager:
"""Manages agent execution, communication, and status updates.
This class handles the lifecycle of an agent, including initialization,
runtime status updates, message handling, and cleanup.
"""
def __init__(self, agent: Any, config: SessionConfig) -> None:
"""Initialize the AgentManager.
Args:
agent: The agent instance to manage
config: SessionConfig containing session configuration
"""
self.agent = agent
self.config = config
self.step_idx: int = 0
self.predict_idx: int = 0
self.done: bool = False
self.total_time: float = 0
self.total_time_start: float = 0
def initialize(self, task_instruction: str) -> None:
"""Initialize the agent session with a task instruction.
Args:
task_instruction: The instruction for the agent to execute
"""
self.step_idx = 0
self.predict_idx = 0
self.done = False
self.total_time = 0
self.total_time_start = time.time()
self.send_start_message(task_instruction)
if ENV_TYPE == "deploy":
env_manager = env_managers[self.config.region]
binding_key = (self.config.user_id, self.config.session_id)
# Update environment bindings
env_manager.env_bindings[binding_key][f"agent{self.config.agent_idx}"]["status"] = AgentStatus.RUNNING
# Update session and conversation
self.config.session.set_task_instruction(task_instruction)
self.config.conversation.task_instruction = task_instruction
self.update_session_item({
"role": "user",
"type": "user_prompt",
"content": task_instruction,
})
def stop_checkpoint(self) -> None:
"""Check if execution should be stopped."""
if ENV_TYPE == "deploy" and self.config.stop_event and self.config.stop_event.is_set():
raise StopExecution("Execution stopped by user")
def finalize(self) -> None:
"""Cleanup and finalize the agent session."""
if ENV_TYPE == "deploy":
self.config.conversation.upload_conversation_data()
region_manager = env_managers[self.config.region]
binding_key = (self.config.user_id, self.config.session_id)
if binding_key in region_manager.env_bindings:
region_manager.set_binded_agent_status(
binding_key=binding_key,
agent_idx=f"agent{self.config.agent_idx}",
status=AgentStatus.DONE
)
region_manager.log_status()
def send_agent_runtime_status_message(
self,
agent_status: str,
obs_time: Optional[float] = None,
predict_time: Optional[float] = None,
step_time: Optional[float] = None
) -> None:
"""Send runtime status updates to the frontend.
Args:
agent_status: Current status of the agent
obs_time: Time taken for observation
predict_time: Time taken for prediction
step_time: Time taken for step execution
"""
if ENV_TYPE == "deploy":
event_name = 'message_agent_runtime_status_left' if self.config.agent_idx == 0 else 'message_agent_runtime_status_right'
self.config.socketio.emit(
event_name,
{
"user_id": self.config.user_id,
'content': {
'status': agent_status,
'obs_time': obs_time,
'predict_time': predict_time,
'step_time': step_time,
},
}
)
def send_start_message(self, task_instruction: str) -> None:
"""Send a start message to the frontend.
Args:
task_instruction: The instruction for the agent to execute
"""
if ENV_TYPE == "deploy":
self.config.socketio.emit('message_response_left' if self.config.agent_idx == 0 else 'message_response_right', {
'type': 'user',
'content': {
'title': task_instruction,
},
"user_id": self.config.user_id,
})
def send_message(
self,
title: str,
image: str,
description: str,
obs_time: float,
agent_time: float,
env_time: float,
token: int,
action: str,
visualization: str
) -> None:
"""Send a message to the frontend.
Args:
title: Title of the message
image: Image associated with the message
description: Description of the message
"""
if ENV_TYPE == "deploy":
self.config.socketio.emit('message_response_left' if self.config.agent_idx == 0 else 'message_response_right', {
"type": "agent",
"content": {
"title": title,
"time": time.time() - self.total_time_start,
"image": image,
"description": description,
"obs_time": obs_time,
"agent_time": agent_time,
"env_time": env_time,
"token": token,
"action": action,
"visualization": visualization,
},
"user_id": self.config.user_id,
})
def send_end_message(self, description: str) -> None:
"""Send an end message to the frontend.
Args:
description: Description of the message
"""
if ENV_TYPE == "deploy":
self.config.socketio.emit('message_response_left' if self.config.agent_idx == 0 else 'message_response_right', {
"type": "end",
"content": {
"title": "end",
"time": time.time() - self.total_time_start,
"image": "",
"description": description,
},
"user_id": self.config.user_id,
})
def update_session_item(self, item: Dict[str, Any]) -> None:
"""Update the session item in the conversation.
Args:
item: Dictionary containing the item to update
"""
if ENV_TYPE == "deploy":
self.config.conversation.append_conversation_item(item)
@contextmanager
def runtime_status(self, status_prefix: str) -> None:
"""Context manager for handling agent runtime status messages.
Args:
status_prefix: The prefix for the status (e.g. "predict", "observation")
"""
try:
if ENV_TYPE == "deploy":
self.send_agent_runtime_status_message(
agent_status=f"{status_prefix}_start")
start_time = time.time()
yield
finally:
duration = time.time() - start_time
if ENV_TYPE == "deploy":
self.send_agent_runtime_status_message(
agent_status=f"{status_prefix}_end",
**{f"{status_prefix}_time": duration}
)
def get_screenshot(self) -> str:
"""Get a screenshot from the agent's environment.
Returns:
str: Encoded image string
"""
try:
screenshot = self.agent.env._get_screenshot()
if screenshot is None:
return ""
screenshot = encode_image(screenshot)
return screenshot
except Exception as e:
self.agent.logger.exception(e)
raise e
def start_video_recording(self) -> None:
"""Start video recording for the agent's environment."""
try:
self.agent.env._start_video_recording()
except Exception as e:
self.agent.logger.exception(e)
raise e
def stop_video_recording(self) -> None:
"""Stop video recording for the agent's environment."""
try:
return self.agent.env._stop_video_recording()
except Exception as e:
self.agent.logger.exception(e)
raise e