-
Notifications
You must be signed in to change notification settings - Fork 977
Expand file tree
/
Copy pathcodeact_agent.py
More file actions
538 lines (454 loc) · 20.3 KB
/
Copy pathcodeact_agent.py
File metadata and controls
538 lines (454 loc) · 20.3 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import asyncio
import inspect
import json
import logging
import warnings
from typing import TYPE_CHECKING, List, Optional, Type, Union
from pydantic import BaseModel
# Suppress all warnings for llama-index import (version compatibility)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from llama_index.core.base.llms.types import ChatMessage, ChatResponse
from llama_index.core.llms.llm import LLM
from llama_index.core.memory import Memory
from llama_index.core.workflow import Context, StartEvent, StopEvent, Workflow, step
from opentelemetry import trace
from droidrun.agent.codeact.events import (
TaskEndEvent,
TaskExecutionEvent,
TaskExecutionResultEvent,
TaskInputEvent,
TaskThinkingEvent,
)
from droidrun.agent.common.constants import LLM_HISTORY_LIMIT
from droidrun.agent.common.events import RecordUIStateEvent, ScreenshotEvent
from droidrun.agent.usage import get_usage_from_response
from droidrun.agent.utils import chat_utils
from droidrun.agent.utils.executer import ExecuterState, SimpleCodeExecutor
from droidrun.agent.utils.inference import acall_with_retries
from droidrun.agent.utils.prompt_resolver import PromptResolver
from droidrun.agent.utils.tracing_setup import record_langfuse_screenshot
from droidrun.agent.utils.tools import (
ATOMIC_ACTION_SIGNATURES,
build_custom_tool_descriptions,
)
from droidrun.config_manager.config_manager import AgentConfig, TracingConfig
from droidrun.config_manager.prompt_loader import PromptLoader
from droidrun.tools import Tools
if TYPE_CHECKING:
from droidrun.agent.droid import DroidAgentState
logger = logging.getLogger("droidrun")
class CodeActAgent(Workflow):
"""
An agent that uses a ReAct-like cycle (Thought -> Code -> Observation)
to solve problems requiring code execution. It extracts code from
Markdown blocks and uses specific step types for tracking.
"""
def __init__(
self,
llm: LLM,
agent_config: AgentConfig,
tools_instance: "Tools",
custom_tools: dict = None,
atomic_tools: dict = None,
debug: bool = False,
shared_state: Optional["DroidAgentState"] = None,
safe_execution_config=None,
output_model: Type[BaseModel] | None = None,
prompt_resolver: Optional[PromptResolver] = None,
tracing_config: TracingConfig | None = None,
*args,
**kwargs,
):
assert llm, "llm must be provided."
super().__init__(*args, **kwargs)
self.llm = llm
self.agent_config = agent_config
self.config = agent_config.codeact # Shortcut to codeact config
self.max_steps = agent_config.max_steps
self.vision = agent_config.codeact.vision
self.debug = debug
self.tools = tools_instance
self.shared_state = shared_state
self.output_model = output_model
self.prompt_resolver = prompt_resolver or PromptResolver()
self.tracing_config = tracing_config
self.chat_memory = None
self.remembered_info = None
self.goal = None
self.code_exec_counter = 0
# Use provided tools or defaults (filtering done in DroidAgent)
if atomic_tools is None:
atomic_tools = ATOMIC_ACTION_SIGNATURES
merged_signatures = {**atomic_tools, **(custom_tools or {})}
self.tool_list = {}
for action_name, signature in merged_signatures.items():
func = signature["function"]
if inspect.iscoroutinefunction(func):
async def async_wrapper(
*args, f=func, ti=tools_instance, ss=shared_state, **kwargs
):
return await f(*args, tools=ti, shared_state=ss, **kwargs)
self.tool_list[action_name] = async_wrapper
else:
def sync_wrapper(
*args, f=func, ti=tools_instance, ss=shared_state, **kwargs
):
return f(*args, tools=ti, shared_state=ss, **kwargs)
self.tool_list[action_name] = sync_wrapper
self.tool_list["remember"] = tools_instance.remember
self.tool_list["complete"] = tools_instance.complete
# Build tool descriptions from provided tools (already filtered by DroidAgent)
self.tool_descriptions = build_custom_tool_descriptions(atomic_tools)
custom_descriptions = build_custom_tool_descriptions(custom_tools or {})
if custom_descriptions:
self.tool_descriptions += "\n" + custom_descriptions
self.tool_descriptions += (
"\n- remember(information: str): Remember information for later use"
)
self.tool_descriptions += (
"\n- complete(success: bool, reason: str): Mark task as complete"
)
self._available_secrets = []
self._output_schema = None
if self.output_model is not None:
self._output_schema = self.output_model.model_json_schema()
self.system_prompt = None
# Get safety settings
safe_mode = self.config.safe_execution
safe_config = safe_execution_config
self.executor = SimpleCodeExecutor(
locals={},
tools=self.tool_list,
globals={"__builtins__": __builtins__},
safe_mode=safe_mode,
allowed_modules=(
safe_config.get_allowed_modules() if safe_config and safe_mode else None
),
blocked_modules=(
safe_config.get_blocked_modules() if safe_config and safe_mode else None
),
allowed_builtins=(
safe_config.get_allowed_builtins()
if safe_config and safe_mode
else None
),
blocked_builtins=(
safe_config.get_blocked_builtins()
if safe_config and safe_mode
else None
),
event_loop=None,
)
logger.debug("✅ CodeActAgent initialized successfully.")
@step
async def prepare_chat(self, ctx: Context, ev: StartEvent) -> TaskInputEvent:
"""Prepare chat history from user input."""
# macro tools context
self.tools._set_context(ctx)
logger.debug("💬 Preparing chat for task execution...")
if hasattr(self.tools, "credential_manager") and self.tools.credential_manager:
self._available_secrets = await self.tools.credential_manager.get_keys()
# Load system prompt on first call (lazy loading)
if self.system_prompt is None:
custom_system_prompt = self.prompt_resolver.get_prompt("codeact_system")
if custom_system_prompt:
system_prompt_text = PromptLoader.render_template(
custom_system_prompt,
{
"tool_descriptions": self.tool_descriptions,
"available_secrets": self._available_secrets,
"variables": (
self.shared_state.custom_variables
if self.shared_state
else {}
),
"output_schema": self._output_schema,
},
)
else:
system_prompt_text = await PromptLoader.load_prompt(
self.agent_config.get_codeact_system_prompt_path(),
{
"tool_descriptions": self.tool_descriptions,
"available_secrets": self._available_secrets,
"variables": (
self.shared_state.custom_variables
if self.shared_state
else {}
),
"output_schema": self._output_schema,
},
)
self.system_prompt = ChatMessage(role="system", content=system_prompt_text)
self.chat_memory: Memory = await ctx.store.get(
"chat_memory", default=Memory.from_defaults()
)
user_input = ev.get("input", default=None)
assert user_input, "User input cannot be empty."
if ev.remembered_info:
self.remembered_info = ev.remembered_info
logger.debug(" - Adding goal to memory.")
goal = user_input
# Format user prompt with goal
custom_user_prompt = self.prompt_resolver.get_prompt("codeact_user")
if custom_user_prompt:
user_prompt_text = PromptLoader.render_template(
custom_user_prompt,
{
"goal": goal,
"variables": (
self.shared_state.custom_variables if self.shared_state else {}
),
},
)
else:
user_prompt_text = await PromptLoader.load_prompt(
self.agent_config.get_codeact_user_prompt_path(),
{
"goal": goal,
"variables": (
self.shared_state.custom_variables if self.shared_state else {}
),
},
)
self.user_message = ChatMessage(role="user", content=user_prompt_text)
# No thoughts prompt
no_thoughts_text = f"""Your previous response provided code without explaining your reasoning first. Remember to always describe your thought process and plan *before* providing the code block.
The code you provided will be executed below.
Now, describe the next step you will take to address the original goal: {goal}"""
self.no_thoughts_prompt = ChatMessage(role="user", content=no_thoughts_text)
await self.chat_memory.aput(self.user_message)
await ctx.store.set("chat_memory", self.chat_memory)
input_messages = self.chat_memory.get_all()
return TaskInputEvent(input=input_messages)
@step
async def handle_llm_input(
self, ctx: Context, ev: TaskInputEvent
) -> TaskThinkingEvent | TaskEndEvent:
"""Handle LLM input."""
chat_history = ev.input
assert len(chat_history) > 0, "Chat history cannot be empty."
ctx.write_event_to_stream(ev)
if self.shared_state.step_number + 1 > self.max_steps:
return TaskEndEvent(
success=False,
reason=f"Reached max step count of {self.max_steps} steps",
)
logger.info(f"🔄 Step {self.shared_state.step_number + 1}/{self.max_steps}")
model = self.llm.class_name()
if "remember" in self.tool_list and self.remembered_info:
await ctx.store.set("remembered_info", self.remembered_info)
chat_history = await chat_utils.add_memory_block(
self.remembered_info, chat_history
)
screenshot = None
if self.vision or (
hasattr(self.tools, "save_trajectories")
and self.tools.save_trajectories != "none"
):
try:
result = await self.tools.take_screenshot()
if isinstance(result, tuple):
success, screenshot = result
if not success:
logger.warning("Screenshot capture failed")
screenshot = None
else:
screenshot = result
if screenshot:
ctx.write_event_to_stream(ScreenshotEvent(screenshot=screenshot))
parent_span = trace.get_current_span()
record_langfuse_screenshot(
screenshot,
parent_span=parent_span,
screenshots_enabled=bool(
self.tracing_config
and self.tracing_config.langfuse_screenshots
),
vision_enabled=self.vision,
)
await ctx.store.set("screenshot", screenshot)
logger.debug("📸 Screenshot captured for CodeAct")
except Exception as e:
logger.warning(f"Failed to capture screenshot: {e}")
if self.vision and screenshot and model != "DeepSeek":
chat_history = await chat_utils.add_screenshot_image_block(
screenshot, chat_history
)
# Get and format device state using unified formatter
try:
# Get raw state from device - returns 4 values directly
formatted_text, focused_text, a11y_tree, phone_state = (
await self.tools.get_state()
)
# Update shared_state if available
assert self.shared_state is not None, "Shared state is not set"
self.shared_state.formatted_device_state = formatted_text
self.shared_state.focused_text = focused_text
self.shared_state.a11y_tree = a11y_tree
self.shared_state.phone_state = phone_state
# Extract and store package/app name (using unified update method)
self.shared_state.update_current_app(
package_name=phone_state.get("packageName", "Unknown"),
activity_name=phone_state.get("currentApp", "Unknown"),
)
# Store ui_state so it's available during code execution
await ctx.store.set("ui_state", a11y_tree)
# Stream formatted state for trajectory
ctx.write_event_to_stream(RecordUIStateEvent(ui_state=a11y_tree))
# Add device state to chat using new chat_utils function
# This injects into LAST user message, doesn't create new message
chat_history = await chat_utils.add_device_state_block(
formatted_text, chat_history
)
except Exception as e:
logger.warning(f"⚠️ Error retrieving state from the connected device: {e}")
if self.debug:
logger.error("State retrieval error details:", exc_info=True)
response = await self._get_llm_response(ctx, chat_history)
if response is None:
return TaskEndEvent(
success=False, reason="LLM response is None. This is a critical error."
)
try:
usage = get_usage_from_response(self.llm.class_name(), response)
except Exception as e:
logger.warning(f"Could not get llm usage from response: {e}")
usage = None
await self.chat_memory.aput(response.message)
self.shared_state.step_number += 1
code, thoughts = chat_utils.extract_code_and_thought(response.message.content)
event = TaskThinkingEvent(thoughts=thoughts, code=code, usage=usage)
ctx.write_event_to_stream(event)
return event
@step
async def handle_llm_output(
self, ctx: Context, ev: TaskThinkingEvent
) -> Union[TaskExecutionEvent, TaskInputEvent]:
"""Handle LLM output."""
logger.debug("⚙️ Handling LLM output...")
code = ev.code
thoughts = ev.thoughts
if not thoughts:
logger.warning(
"🤔 LLM provided code without thoughts. Adding reminder prompt."
)
await self.chat_memory.aput(self.no_thoughts_prompt)
else:
logger.debug(f"🤔 Reasoning: {thoughts}")
if code:
return TaskExecutionEvent(code=code)
else:
message = ChatMessage(
role="user",
content="No code was provided. If you want to mark task as complete (whether it failed or succeeded), use complete(success:bool, reason:str) function within a code block ```pythn\n```.",
)
await self.chat_memory.aput(message)
return TaskInputEvent(input=self.chat_memory.get_all())
@step
async def execute_code(
self, ctx: Context, ev: TaskExecutionEvent
) -> Union[TaskExecutionResultEvent, TaskEndEvent]:
"""Execute the code and return the result."""
code = ev.code
assert code, "Code cannot be empty."
logger.debug("⚡ Executing action...")
logger.debug(f"Code to execute:\n```python\n{code}\n```")
try:
self.code_exec_counter += 1
result = await self.executor.execute(
ExecuterState(ui_state=await ctx.store.get("ui_state", None)), code
)
logger.info("[dim]💡 Execution result:[/dim]")
logger.info(f"{result}")
await asyncio.sleep(self.agent_config.after_sleep_action)
# Check if complete() was called
if self.tools.finished:
logger.debug("✅ Task marked as complete via complete() function")
# Validate completion state
success = (
self.tools.success if self.tools.success is not None else False
)
reason = (
self.tools.reason
if self.tools.reason
else "Task completed without reason"
)
# Reset finished flag for next execution
self.tools.finished = False
logger.debug(f" - Success: {success}")
logger.debug(f" - Reason: {reason}")
return TaskEndEvent(success=success, reason=reason)
self.remembered_info = self.tools.memory
return TaskExecutionResultEvent(output=str(result))
except Exception as e:
logger.error(f"💥 Action failed: {e}")
if self.debug:
logger.error("Exception details:", exc_info=True)
error_message = f"Error during execution: {e}"
event = TaskExecutionResultEvent(output=error_message)
ctx.write_event_to_stream(event)
return event
@step
async def handle_execution_result(
self, ctx: Context, ev: TaskExecutionResultEvent
) -> TaskInputEvent:
"""Handle the execution result. Currently it just returns InputEvent."""
logger.debug("📊 Handling execution result...")
# Get the output from the event
output = ev.output
if output is None:
output = "Code executed, but produced no output."
logger.warning(" - Execution produced no output.")
else:
logger.debug(
f" - Execution output: {output[:100]}..."
if len(output) > 100
else f" - Execution output: {output}"
)
# Add the output to memory as an user message (observation)
observation_message = ChatMessage(
role="user", content=f"Execution Result:\n```\n{output}\n```"
)
await self.chat_memory.aput(observation_message)
return TaskInputEvent(input=self.chat_memory.get_all())
@step
async def finalize(self, ev: TaskEndEvent, ctx: Context) -> StopEvent:
"""Finalize the workflow."""
self.tools.finished = False
await ctx.store.set("chat_memory", self.chat_memory)
result = {}
result.update(
{
"success": ev.success,
"reason": ev.reason,
"code_executions": self.code_exec_counter,
}
)
return StopEvent(result)
async def _get_llm_response(
self, ctx: Context, chat_history: List[ChatMessage]
) -> ChatResponse | None:
limited_history = self._limit_history(chat_history)
messages_to_send = [self.system_prompt] + limited_history
messages_to_send = [chat_utils.message_copy(msg) for msg in messages_to_send]
logger.info("[yellow]🤖 CodeAct response:[/yellow]")
response = await acall_with_retries(
self.llm, messages_to_send, stream=self.agent_config.streaming
)
logger.debug(" - Received response from LLM.")
return response
def _limit_history(self, chat_history: List[ChatMessage]) -> List[ChatMessage]:
if LLM_HISTORY_LIMIT <= 0:
return chat_history
max_messages = LLM_HISTORY_LIMIT * 2
if len(chat_history) <= max_messages:
return chat_history
preserved_head: List[ChatMessage] = []
if chat_history and chat_history[0].role == "user":
preserved_head = [chat_history[0]]
tail = chat_history[-max_messages:]
if preserved_head and preserved_head[0] in tail:
preserved_head = []
return preserved_head + tail