99from semantic_kernel .functions .kernel_arguments import KernelArguments
1010from semantic_kernel .agents .azure_ai .azure_ai_agent import AzureAIAgent
1111
12+ # Updated imports for compatibility
13+ try :
14+ # Try importing from newer structure first
15+ from semantic_kernel .contents import ChatMessageContent , ChatHistory
16+ except ImportError :
17+ # Fall back to older structure for compatibility
18+ class ChatMessageContent :
19+ """Compatibility class for older SK versions."""
20+ def __init__ (self , role = "" , content = "" , name = None ):
21+ self .role = role
22+ self .content = content
23+ self .name = name
24+
25+ class ChatHistory :
26+ """Compatibility class for older SK versions."""
27+ def __init__ (self ):
28+ self .messages = []
29+
1230from context .cosmos_memory_kernel import CosmosMemoryContext
1331from models .messages_kernel import (
1432 ActionRequest ,
@@ -64,6 +82,7 @@ def __init__(
6482 else :
6583 tools = tools or []
6684 system_message = system_message or self ._default_system_message (agent_name )
85+
6786 # Call AzureAIAgent constructor with required client and definition
6887 super ().__init__ (
6988 kernel = kernel ,
@@ -76,6 +95,8 @@ def __init__(
7695 client = client ,
7796 definition = definition
7897 )
98+
99+ # Store instance variables
79100 self ._agent_name = agent_name
80101 self ._kernel = kernel
81102 self ._session_id = session_id
@@ -84,8 +105,14 @@ def __init__(
84105 self ._tools = tools
85106 self ._system_message = system_message
86107 self ._chat_history = [{"role" : "system" , "content" : self ._system_message }]
108+ self ._agent = None # Will be initialized in async_init
109+
110+ # Required properties for AgentGroupChat compatibility
111+ self .name = agent_name # This is crucial for AgentGroupChat to identify agents
112+
87113 # Log initialization
88114 logging .info (f"Initialized { agent_name } with { len (self ._tools )} tools" )
115+
89116 # Register the handler functions
90117 self ._register_functions ()
91118
@@ -107,6 +134,53 @@ async def async_init(self):
107134 # Tools are registered with the kernel via get_tools_from_config
108135 return self
109136
137+ async def invoke_async (self , * args , ** kwargs ):
138+ """Invoke this agent asynchronously.
139+
140+ This method is required for compatibility with AgentGroupChat.
141+
142+ Args:
143+ *args: Positional arguments
144+ **kwargs: Keyword arguments
145+
146+ Returns:
147+ The agent's response
148+ """
149+ # Ensure agent is initialized
150+ if self ._agent is None :
151+ await self .async_init ()
152+
153+ # Get the text input from args or kwargs
154+ text = None
155+ if args and isinstance (args [0 ], str ):
156+ text = args [0 ]
157+ elif "text" in kwargs :
158+ text = kwargs ["text" ]
159+ elif "arguments" in kwargs and hasattr (kwargs ["arguments" ], "get" ):
160+ text = kwargs ["arguments" ].get ("text" ) or kwargs ["arguments" ].get ("input" )
161+
162+ if not text :
163+ settings = kwargs .get ("settings" , {})
164+ if isinstance (settings , dict ) and "input" in settings :
165+ text = settings ["input" ]
166+
167+ # If text is still not found, create a default message
168+ if not text :
169+ text = "Hello, please assist with a task."
170+
171+ # Use the text to invoke the agent
172+ try :
173+ logging .info (f"Invoking { self ._agent_name } with text: { text [:100 ]} ..." )
174+ response = await self ._agent .invoke (
175+ self ._kernel ,
176+ text ,
177+ settings = kwargs .get ("settings" , {})
178+ )
179+ return response
180+ except Exception as e :
181+ logging .error (f"Error invoking { self ._agent_name } : { e } " )
182+ return f"Error: { str (e )} "
183+
110184 def _register_functions (self ):
111185 """Register this agent's functions with the kernel."""
112186 # Use the kernel function decorator approach instead of from_native_method
@@ -126,6 +200,37 @@ async def handle_action_request_wrapper(*args, **kwargs):
126200 kernel_func = KernelFunction .from_method (handle_action_request_wrapper )
127201 # Use agent name as plugin for handler
128202 self ._kernel .add_function (self ._agent_name , kernel_func )
203+
204+ # Required method for AgentGroupChat compatibility
205+ async def send_message_async (self , message_content : ChatMessageContent , chat_history : ChatHistory ):
206+ """Send a message to the agent asynchronously, adding it to chat history.
207+
208+ Args:
209+ message_content: The content of the message
210+ chat_history: The chat history
211+
212+ Returns:
213+ None
214+ """
215+ # Convert message to format expected by the agent
216+ if hasattr (message_content , "role" ) and hasattr (message_content , "content" ):
217+ self ._chat_history .append ({
218+ "role" : message_content .role ,
219+ "content" : message_content .content
220+ })
221+
222+ # If chat history is provided, update our internal history
223+ if chat_history and hasattr (chat_history , "messages" ):
224+ # Update with the latest messages from chat history
225+ for msg in chat_history .messages [- 5 :]: # Only use last 5 messages to avoid history getting too long
226+ if msg not in self ._chat_history :
227+ self ._chat_history .append ({
228+ "role" : msg .role ,
229+ "content" : msg .content
230+ })
231+
232+ # No need to return anything as we're just updating state
233+ return None
129234
130235 async def handle_action_request (self , action_request_json : str ) -> str :
131236 """Handle an action request from another agent or the system.
0 commit comments