66from collections .abc import AsyncIterable
77from typing import AsyncIterator , Optional
88
9+ import v3 .models .messages as agent_messages
910from pydantic import Field
1011from semantic_kernel .agents import ( # pylint: disable=no-name-in-module
1112 AgentResponseItem , AgentThread )
@@ -104,8 +105,6 @@ def __init__(self, user_id: str = None, **kwargs):
104105 )
105106 self .instructions = ""
106107
107- self .user_id = user_id or current_user_id .get ()
108-
109108 def _create_message_content (self , content : str , thread_id : str = None ) -> ChatMessageContent :
110109 """Create a ChatMessageContent with proper metadata."""
111110 return ChatMessageContent (
@@ -117,18 +116,23 @@ def _create_message_content(self, content: str, thread_id: str = None) -> ChatMe
117116
118117 async def _trigger_response_callbacks (self , message_content : ChatMessageContent ):
119118 """Manually trigger the same response callbacks used by other agents."""
119+ # Get current user_id dynamically instead of using stored value
120+ current_user = current_user_id .get () or self .user_id or ""
121+
120122 # Trigger the standard agent response callback
121- agent_response_callback (message_content , self . user_id )
123+ agent_response_callback (message_content , current_user )
122124
123125 async def _trigger_streaming_callbacks (self , content : str , is_final : bool = False ):
124126 """Manually trigger streaming callbacks for real-time updates."""
127+ # Get current user_id dynamically instead of using stored value
128+ current_user = current_user_id .get () or self .user_id or ""
125129 streaming_message = StreamingChatMessageContent (
126130 role = AuthorRole .ASSISTANT ,
127131 content = content ,
128132 name = self .name ,
129133 choice_index = 0
130134 )
131- await streaming_agent_response_callback (streaming_message , is_final , self . user_id )
135+ await streaming_agent_response_callback (streaming_message , is_final , current_user )
132136
133137 async def invoke (self , message : str ,* , thread : AgentThread | None = None ,** kwargs ) -> AsyncIterator [ChatMessageContent ]:
134138 """Ask human user for clarification about the message."""
@@ -142,9 +146,9 @@ async def invoke(self, message: str,*, thread: AgentThread | None = None,**kwarg
142146 # Send clarification request via response handlers
143147 clarification_request = f"I need clarification about: { message } "
144148 clarification_message = self ._create_message_content (clarification_request , thread .id )
145- # await self._trigger_response_callbacks(clarification_message)
149+ await self ._trigger_response_callbacks (clarification_message )
146150
147- # Get human input
151+ # Get human input - replace this with awaiting a websocket call or API handler when available
148152 human_response = input ("Please provide clarification: " ).strip ()
149153
150154 if not human_response :
@@ -154,7 +158,6 @@ async def invoke(self, message: str,*, thread: AgentThread | None = None,**kwarg
154158
155159 # Send response via response handlers
156160 response_message = self ._create_message_content (response , thread .id )
157- #await self._trigger_response_callbacks(response_message)
158161
159162 chat_message = response_message
160163
@@ -183,7 +186,8 @@ async def invoke_stream(self, messages, thread=None, **kwargs) -> AsyncIterator[
183186
184187 # Send clarification request via streaming callbacks
185188 clarification_request = f"I need clarification about: { message } "
186- #await self._trigger_streaming_callbacks(clarification_request)
189+ self ._create_message_content (clarification_request , thread .id )
190+ await self ._trigger_streaming_callbacks (clarification_request )
187191
188192 # Get human input - replace with websocket call when available
189193 human_response = input ("Please provide clarification: " ).strip ()
@@ -192,9 +196,6 @@ async def invoke_stream(self, messages, thread=None, **kwargs) -> AsyncIterator[
192196 human_response = "No additional clarification provided."
193197
194198 response = f"Human clarification: { human_response } "
195-
196- # Send response via streaming callbacks
197- #await self._trigger_streaming_callbacks(response, is_final=True)
198199
199200 chat_message = self ._create_message_content (response , thread .id )
200201
0 commit comments