-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathagent_executor.py
More file actions
47 lines (39 loc) · 1.66 KB
/
agent_executor.py
File metadata and controls
47 lines (39 loc) · 1.66 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
"""
Wraps the AG2 GroupChat workflow as an A2A AgentExecutor
for use with SingleA2AAdapter (Pattern B).
"""
from a2a.server.agent_execution import AgentExecutor, RequestContext
from a2a.server.events import EventQueue
from a2a.types import Part, TextPart
from a2a.utils import new_agent_text_message
from autogen import LLMConfig
from typing_extensions import override
from workflow import run_research
class AG2ResearchExecutor(AgentExecutor):
"""A2A-compatible executor wrapping the AG2 GroupChat workflow."""
def __init__(self, llm_config: LLMConfig, mcp_url: str | None = None):
self.llm_config = llm_config
self.mcp_url = mcp_url
@override
async def execute(self, context: RequestContext, event_queue: EventQueue) -> None:
message_content = ""
for part in context.message.parts:
if isinstance(part, Part):
if isinstance(part.root, TextPart):
message_content = part.root.text
break
if not message_content:
await event_queue.enqueue_event(
new_agent_text_message("Error: No message content received.")
)
return
try:
result = await run_research(message_content, self.llm_config, self.mcp_url)
await event_queue.enqueue_event(new_agent_text_message(result))
except Exception as e:
await event_queue.enqueue_event(
new_agent_text_message(f"Research failed: {e}")
)
@override
async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None:
raise Exception("Cancel not supported for this agent executor.")