|
2 | 2 | import asyncio |
3 | 3 |
|
4 | 4 | from AgentCrew.modules.agents import AgentManager |
5 | | - |
6 | | -# from AgentCrew.modules.llm.message import MessageTransformer |
7 | | -from AgentCrew.modules.agents.base import MessageType |
| 5 | +from AgentCrew.modules.agents.agent_runner import run_agent_loop |
8 | 6 |
|
9 | 7 |
|
10 | 8 | def get_delegate_tool_definition(provider="claude") -> Dict[str, Any]: |
@@ -121,10 +119,10 @@ def handler(**params) -> str: |
121 | 119 |
|
122 | 120 | try: |
123 | 121 | # Get the target agent |
124 | | - target_agent = agent_manager.get_agent(target_agent_name) |
| 122 | + target_agent = agent_manager.get_local_agent(target_agent_name) |
125 | 123 | if not target_agent: |
126 | 124 | raise ValueError( |
127 | | - f"Error: Could not retrieve agent '{target_agent_name}'" |
| 125 | + f"Error: Could not retrieve local agent '{target_agent_name}'" |
128 | 126 | ) |
129 | 127 |
|
130 | 128 | # Prepare context from current conversation |
@@ -177,106 +175,14 @@ def handler(**params) -> str: |
177 | 175 |
|
178 | 176 | try: |
179 | 177 |
|
180 | | - async def _process_delegation(): |
181 | | - """Process delegation similar to _process_agent_task - runs until no tool uses""" |
182 | | - |
183 | | - current_response = "" |
184 | | - tool_uses = [] |
185 | | - input_tokens = 0 |
186 | | - output_tokens = 0 |
187 | | - thinking_content = "" |
188 | | - |
189 | | - def process_result(_tool_uses, _input_tokens, _output_tokens): |
190 | | - nonlocal tool_uses, input_tokens, output_tokens |
191 | | - tool_uses = _tool_uses |
192 | | - input_tokens += _input_tokens |
193 | | - output_tokens += _output_tokens |
194 | | - |
195 | | - if not target_agent: |
196 | | - return current_response |
197 | | - async for ( |
198 | | - response_message, |
199 | | - chunk_text, |
200 | | - thinking_chunk, |
201 | | - ) in target_agent.process_messages( |
202 | | - delegate_history, callback=process_result |
203 | | - ): |
204 | | - if response_message: |
205 | | - current_response = response_message |
206 | | - |
207 | | - if thinking_chunk: |
208 | | - think_text_chunk, _ = thinking_chunk |
209 | | - if think_text_chunk: |
210 | | - thinking_content += think_text_chunk |
211 | | - |
212 | | - if tool_uses and len(tool_uses) > 0: |
213 | | - filtered_tool_uses = [ |
214 | | - t |
215 | | - for t in tool_uses |
216 | | - if t["name"] not in ["transfer", "delegate"] |
217 | | - ] |
218 | | - |
219 | | - if not filtered_tool_uses: |
220 | | - return current_response |
221 | | - |
222 | | - # Add thinking content as a separate message if available |
223 | | - thinking_data = ( |
224 | | - (thinking_content, None) if thinking_content else None |
225 | | - ) |
226 | | - if thinking_data: |
227 | | - thinking_message = target_agent.format_message( |
228 | | - MessageType.Thinking, {"thinking": thinking_data} |
229 | | - ) |
230 | | - if thinking_message: |
231 | | - delegate_history.append(thinking_message) |
232 | | - |
233 | | - # Format assistant message with the response and tool uses |
234 | | - assistant_message = target_agent.format_message( |
235 | | - MessageType.Assistant, |
236 | | - { |
237 | | - "message": current_response, |
238 | | - "tool_uses": filtered_tool_uses, |
239 | | - }, |
240 | | - ) |
241 | | - if assistant_message: |
242 | | - delegate_history.append(assistant_message) |
243 | | - |
244 | | - # Process each tool use |
245 | | - for tool_use in filtered_tool_uses: |
246 | | - try: |
247 | | - tool_result = await target_agent.execute_tool_call( |
248 | | - tool_use["name"], |
249 | | - tool_use["input"], |
250 | | - ) |
251 | | - |
252 | | - tool_result_message = target_agent.format_message( |
253 | | - MessageType.ToolResult, |
254 | | - { |
255 | | - "tool_use": tool_use, |
256 | | - "tool_result": tool_result, |
257 | | - }, |
258 | | - ) |
259 | | - if tool_result_message: |
260 | | - delegate_history.append(tool_result_message) |
261 | | - |
262 | | - except Exception as e: |
263 | | - error_message = target_agent.format_message( |
264 | | - MessageType.ToolResult, |
265 | | - { |
266 | | - "tool_use": tool_use, |
267 | | - "tool_result": str(e), |
268 | | - "is_error": True, |
269 | | - }, |
270 | | - ) |
271 | | - if error_message: |
272 | | - delegate_history.append(error_message) |
273 | | - |
274 | | - # Continue the loop to process more messages |
275 | | - return await _process_delegation() |
276 | | - |
277 | | - return current_response |
278 | | - |
279 | | - response = asyncio.run(_process_delegation()) |
| 178 | + async def _do_delegation(): |
| 179 | + return await run_agent_loop( |
| 180 | + agent=target_agent, |
| 181 | + history=delegate_history, |
| 182 | + tool_filter=lambda t: t["name"] not in ["transfer", "delegate"], |
| 183 | + ) |
| 184 | + |
| 185 | + response = asyncio.run(_do_delegation()) |
280 | 186 |
|
281 | 187 | except Exception as e: |
282 | 188 | raise ValueError( |
|
0 commit comments