-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_env_adapter.py
More file actions
479 lines (404 loc) · 16.1 KB
/
virtual_env_adapter.py
File metadata and controls
479 lines (404 loc) · 16.1 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""
Virtual Environment Adapter
Adapts VirtualAndroidEnv to AsyncEnv interface for compatibility with existing Mobile-Agent-v3 framework
"""
import numpy as np
from typing import Dict, Optional, Any
from agent_env.env import interface
from agent_env.env import representation_utils
from android_env.proto import adb_pb2
from virtual_env_gemini3 import VirtualAndroidEnv
class VirtualEnvAdapter(interface.AsyncEnv):
"""
Virtual Environment Adapter,将Gemini3虚拟环境适配为AsyncEnv接口
"""
def __init__(
self,
gemini_api_key: str,
gemini_base_url: str = "https://openrouter.ai/api/v1",
gemini_model: str = "google/gemini-2.0-flash-exp:free",
initial_task: str = "",
initial_image_path: str = None,
resolution: tuple = (1080, 2400)
):
"""
初始化Virtual Environment Adapter
Args:
gemini_api_key: Gemini API key
gemini_base_url: API address
gemini_model: Model name
initial_task: Initial task description
initial_image_path: 初始图像路径
resolution: Screen resolution
"""
self._virtual_env = VirtualAndroidEnv(
gemini_api_key=gemini_api_key,
gemini_base_url=gemini_base_url,
gemini_model=gemini_model,
initial_task=initial_task,
initial_image_path=initial_image_path,
resolution=resolution
)
self._current_screenshot = None
self._view_hierarchy = None # 虚拟环境不需要真实的view hierarchy
self._interaction_cache = "" # 交互缓存
print(f"[VirtualEnvAdapter] Initialized")
@property
def controller(self):
"""Return controller (virtual environment uses mock controller)"""
return VirtualController(self._virtual_env)
def reset(self, go_home: bool = False) -> interface.State:
"""
Reset environment
Args:
go_home: Whether to go home (virtual environment ignores this parameter)
Returns:
Initial state
"""
print(f"\n[VirtualEnvAdapter] Resetting environment...")
self._current_screenshot = self._virtual_env.reset()
return self._create_state()
def get_state(self, wait_to_stabilize: bool = False) -> interface.State:
"""
获取当前Environment state
Args:
wait_to_stabilize: Whether to wait for UI to stabilize (virtual environment ignores this parameter)
Returns:
Current state(State对象)
"""
state = self._virtual_env.get_current_state()
self._current_screenshot = state.get('screenshot', self._current_screenshot)
return self._create_state()
def _create_state(self) -> interface.State:
"""
Create State object
Returns:
interface.State对象
"""
# Create mock forest (simplified XML tree)
forest = self._get_mock_forest()
# Create empty UI elements list (can be populated as needed)
ui_elements = []
return interface.State(
pixels=self._current_screenshot,
forest=forest,
ui_elements=ui_elements,
auxiliaries={
'app_name': self._virtual_env.get_current_state().get('app_name', 'Unknown'),
'action_count': self._virtual_env.get_current_state().get('action_count', 0)
}
)
def _get_mock_forest(self) -> Any:
"""
生成模拟的accessibility forest
返回简化的XML结构
"""
from xml.etree import ElementTree as ET
state = self._virtual_env.get_current_state()
app_name = state.get('app_name', 'Unknown')
width, height = self._virtual_env.resolution
# 创建简单的XML树
root = ET.Element('hierarchy', attrib={'rotation': '0'})
frame_node = ET.SubElement(root, 'node', attrib={
'index': '0',
'text': '',
'resource-id': '',
'class': 'android.widget.FrameLayout',
'package': app_name,
'content-desc': '',
'checkable': 'false',
'checked': 'false',
'clickable': 'false',
'enabled': 'true',
'focusable': 'false',
'focused': 'false',
'scrollable': 'false',
'long-clickable': 'false',
'password': 'false',
'selected': 'false',
'bounds': f'[0,0][{width},{height}]'
})
# 添加内容节点
content_node = ET.SubElement(frame_node, 'node', attrib={
'index': '0',
'text': 'Virtual Android Screen',
'resource-id': 'android:id/content',
'class': 'android.view.View',
'package': app_name,
'content-desc': 'Virtual screen generated by Gemini3',
'checkable': 'false',
'checked': 'false',
'clickable': 'true',
'enabled': 'true',
'focusable': 'true',
'focused': 'false',
'scrollable': 'true',
'long-clickable': 'false',
'password': 'false',
'selected': 'false',
'bounds': f'[0,100][{width},{height-100}]'
})
return root
def _get_mock_view_hierarchy(self) -> str:
"""
生成模拟的view hierarchy
由于虚拟环境没有真实的view hierarchy,这里返回一个简化版本
"""
state = self._virtual_env.get_current_state()
app_name = state.get('app_name', 'Unknown')
# 简单的mock view hierarchy
mock_vh = f"""<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
<node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="{app_name}"
content-desc="" checkable="false" checked="false" clickable="false" enabled="true"
focusable="false" focused="false" scrollable="false" long-clickable="false"
password="false" selected="false" bounds="[0,0][1080,2400]">
<node index="0" text="Virtual Android Screen" resource-id="android:id/content"
class="android.view.View" package="{app_name}" content-desc="Virtual screen generated by Gemini3"
checkable="false" checked="false" clickable="true" enabled="true"
focusable="true" focused="false" scrollable="true" long-clickable="false"
password="false" selected="false" bounds="[0,100][1080,2300]"/>
</node>
</hierarchy>"""
return mock_vh
def execute_action(self, action) -> None:
"""
Execute action并更新环境
Args:
action: JSONAction对象或Action字典
"""
print(f"\n[VirtualEnvAdapter] Executing action: {action}")
# 将action转换为字典格式
if hasattr(action, 'action_type'):
# JSONAction对象 - 直接使用Agent提供的description
action_type = action.action_type
# 优先使用Agent提供的description,否则生成默认描述
if hasattr(action, 'description') and action.description:
description = action.description
else:
# 后备方案:根据Action类型生成描述
if action_type == 'click':
if hasattr(action, 'x') and hasattr(action, 'y'):
description = f"Click at position ({action.x}, {action.y})"
else:
description = "Click on screen"
elif action_type == 'type':
text = action.text if hasattr(action, 'text') else ""
description = f"Type the text: '{text}'"
elif action_type == 'scroll':
direction = action.direction if hasattr(action, 'direction') else "down"
description = f"Scroll {direction}"
elif action_type == 'open_app':
app_name = action.app_name if hasattr(action, 'app_name') else "app"
description = f"Open {app_name} app"
elif action_type == 'navigate_home':
description = "Press Home button to go to home screen"
elif action_type == 'navigate_back':
description = "Press Back button to go back"
else:
description = f"Perform {action_type} action"
action_dict = {
'action': action_type,
'description': description
}
# 根据Action类型添加参数
if hasattr(action, 'text') and action.text:
action_dict['text'] = action.text
if hasattr(action, 'x') and hasattr(action, 'y'):
action_dict['coordinate'] = [action.x, action.y]
if hasattr(action, 'direction') and action.direction:
action_dict['direction'] = action.direction
if hasattr(action, 'app_name') and action.app_name:
action_dict['app_name'] = action.app_name
else:
# 已经是字典
action_dict = action
# Execute action
new_screenshot, metadata = self._virtual_env.step(action_dict)
self._current_screenshot = new_screenshot
def execute_adb_call(self, call: list[str], timeout: float = 60.0):
"""
模拟执行ADB调用(虚拟环境不支持真实ADB)
Args:
call: ADB命令列表
timeout: 超时时间
Returns:
模拟的ADB响应
"""
print(f"[VirtualEnvAdapter] Mock ADB call: {' '.join(call)}")
# 返回模拟响应
if 'whoami' in call:
return type('obj', (object,), {'generic': 'shell'})()
elif 'settings' in call and 'put' in call and 'global' in call:
return type('obj', (object,), {'generic': ''})()
else:
return type('obj', (object,), {'generic': ''})()
def close(self):
"""Close environment"""
print(f"[VirtualEnvAdapter] Closing environment")
# 关闭虚拟环境,保存轨迹文件
if hasattr(self._virtual_env, 'close'):
self._virtual_env.close()
pass
def ask_question(self, question: str, timeout_seconds: float = -1.0) -> str | None:
"""
向用户提问(虚拟环境中返回None)
Args:
question: 问题
timeout_seconds: 超时时间
Returns:
None(虚拟环境不支持用户交互)
"""
print(f"[VirtualEnvAdapter] Ask question (not supported in virtual env): {question}")
return None
@property
def foreground_activity_name(self) -> str:
"""返回前台App name"""
state = self._virtual_env.get_current_state()
return state.get('app_name', 'Unknown')
@property
def device_screen_size(self) -> tuple[int, int]:
"""返回设备屏幕尺寸"""
return self._virtual_env.resolution
@property
def logical_screen_size(self) -> tuple[int, int]:
"""Return logical screen size"""
return self._virtual_env.resolution
@property
def interaction_cache(self) -> str:
"""返回交互缓存"""
return self._interaction_cache
@interaction_cache.setter
def interaction_cache(self, value: str) -> None:
"""设置交互缓存"""
self._interaction_cache = value
def hide_automation_ui(self) -> None:
"""隐藏自动化UI(虚拟环境不需要)"""
pass
@property
def orientation(self) -> int:
"""
返回屏幕方向
Returns:
0 (竖屏)
"""
return 0
@property
def physical_frame_boundary(self) -> tuple[int, int, int, int]:
"""
返回物理边框边界
Returns:
(0, 0, width, height)
"""
width, height = self._virtual_env.resolution
return (0, 0, width, height)
def set_screenshot_path(self, filepath: str):
"""
设置当前截图 file path(在Agent保存截图后调用)
Args:
filepath: 截图文件的完整路径
"""
self._virtual_env.set_screenshot_path(filepath)
def set_trajectory_dir(self, trajectory_dir: str):
"""
设置轨迹文件夹路径
Args:
trajectory_dir: 轨迹文件夹的完整路径
"""
self._virtual_env.set_trajectory_dir(trajectory_dir)
class VirtualController:
"""
虚拟控制器,模拟真实设备的控制器接口
"""
def __init__(self, virtual_env: VirtualAndroidEnv):
self._virtual_env = virtual_env
self._last_action_result = None
def get_screenshot(self) -> np.ndarray:
"""获取当前截图"""
state = self._virtual_env.get_current_state()
return state.get('screenshot')
def execute_action(self, action: Dict[str, Any]) -> Dict[str, Any]:
"""
Execute action
Args:
action: Action字典
Returns:
执行结果
"""
new_screenshot, metadata = self._virtual_env.step(action)
self._last_action_result = {
'screenshot': new_screenshot,
'metadata': metadata,
'success': metadata.get('success', True)
}
return self._last_action_result
def tap(self, x: int, y: int):
"""点击操作"""
action = {
'action': 'click',
'coordinate': [x, y],
'description': f'Tap at ({x}, {y})'
}
return self.execute_action(action)
def swipe(self, x1: int, y1: int, x2: int, y2: int, duration: int = 300):
"""滑动操作"""
action = {
'action': 'swipe',
'start': [x1, y1],
'end': [x2, y2],
'duration': duration,
'description': f'Swipe from ({x1},{y1}) to ({x2},{y2})'
}
return self.execute_action(action)
def input_text(self, text: str):
"""Input text"""
action = {
'action': 'input',
'text': text,
'description': f'Input text: {text}'
}
return self.execute_action(action)
def press_key(self, keycode: str):
"""按键操作"""
action = {
'action': 'press_key',
'key': keycode,
'description': f'Press key: {keycode}'
}
return self.execute_action(action)
def navigate_home(self):
"""返回主屏幕"""
action = {
'action': 'navigate_home',
'description': 'Navigate to home screen'
}
return self.execute_action(action)
def navigate_back(self):
"""返回上一屏"""
action = {
'action': 'navigate_back',
'description': 'Navigate back'
}
return self.execute_action(action)
def execute_adb_call(self, call, timeout: float = 60.0):
"""
模拟执行ADB调用(虚拟环境不支持真实ADB)
虚拟环境不需要真实的ADB操作,所有检查都返回成功
Args:
call: ADB命令
timeout: 超时时间
Returns:
模拟的ADB响应
"""
# 创建成功的ADB响应对象
response = adb_pb2.AdbResponse()
response.status = adb_pb2.AdbResponse.Status.OK
# 根据命令类型返回适当的输出
call_str = str(call)
# 文件/目录检查 - 都返回存在
if 'test -' in call_str or 'if [' in call_str:
response.generic.output = b'Exists'
# 其他命令返回空输出表示成功
else:
response.generic.output = b''
return response