3131from semantic_kernel .connectors .ai .prompt_execution_settings import PromptExecutionSettings
3232from semantic_kernel .contents .chat_history import ChatHistory
3333from semantic_kernel .contents .chat_message_content import ChatMessageContent
34+ from semantic_kernel .contents .streaming_chat_message_content import StreamingChatMessageContent
3435from semantic_kernel .contents .utils .author_role import AuthorRole
3536from semantic_kernel .functions .kernel_arguments import KernelArguments
3637from semantic_kernel .kernel import Kernel
@@ -701,29 +702,17 @@ async def _handle_request_message(self, message: MagenticRequestMessage, ctx: Me
701702 return
702703
703704 logger .debug (f"{ self .id } : Received request message." )
704- if self ._agent_thread is None :
705- # Add a user message to steer the agent to respond more closely to the instructions.
706- self ._chat_history .add_message (
707- ChatMessageContent (
708- role = AuthorRole .USER ,
709- content = f"Transferred to { self ._agent .name } , adopt the persona immediately." ,
710- )
711- )
712- response_item = await self ._agent .get_response (messages = self ._chat_history .messages ) # type: ignore[arg-type]
713- self ._agent_thread = response_item .thread
714- else :
715- # Add a user message to steer the agent to respond more closely to the instructions.
716- new_message = ChatMessageContent (
717- role = AuthorRole .USER ,
718- content = f"Transferred to { self ._agent .name } , adopt the persona immediately." ,
719- )
720- response_item = await self ._agent .get_response (messages = new_message , thread = self ._agent_thread )
721705
722- logger .debug (f"{ self .id } responded with { response_item .message .content } ." )
723- await self ._call_agent_response_callback (response_item .message )
706+ persona_adoption_message = ChatMessageContent (
707+ role = AuthorRole .USER ,
708+ content = f"Transferred to { self ._agent .name } , adopt the persona immediately." ,
709+ )
710+ response = await self ._invoke_agent (additional_messages = persona_adoption_message )
711+
712+ logger .debug (f"{ self .id } responded with { response } ." )
724713
725714 await self .publish_message (
726- MagenticResponseMessage (body = response_item . message ),
715+ MagenticResponseMessage (body = response ),
727716 TopicId (self ._internal_topic_type , self .id .key ),
728717 cancellation_token = ctx .cancellation_token ,
729718 )
@@ -756,6 +745,8 @@ def __init__(
756745 input_transform : Callable [[TIn ], Awaitable [DefaultTypeAlias ] | DefaultTypeAlias ] | None = None ,
757746 output_transform : Callable [[DefaultTypeAlias ], Awaitable [TOut ] | TOut ] | None = None ,
758747 agent_response_callback : Callable [[DefaultTypeAlias ], Awaitable [None ] | None ] | None = None ,
748+ streaming_agent_response_callback : Callable [[StreamingChatMessageContent , bool ], Awaitable [None ] | None ]
749+ | None = None ,
759750 ) -> None :
760751 """Initialize the Magentic One orchestration.
761752
@@ -768,6 +759,8 @@ def __init__(
768759 output_transform (Callable | None): A function that transforms the internal output message.
769760 agent_response_callback (Callable | None): A function that is called when a response is produced
770761 by the agents.
762+ streaming_agent_response_callback (Callable | None): A function that is called when a streaming response
763+ is produced by the agents.
771764 """
772765 self ._manager = manager
773766
@@ -782,6 +775,7 @@ def __init__(
782775 input_transform = input_transform ,
783776 output_transform = output_transform ,
784777 agent_response_callback = agent_response_callback ,
778+ streaming_agent_response_callback = streaming_agent_response_callback ,
785779 )
786780
787781 @override
@@ -826,6 +820,7 @@ async def _register_members(self, runtime: CoreRuntime, internal_topic_type: str
826820 agent ,
827821 internal_topic_type ,
828822 self ._agent_response_callback ,
823+ self ._streaming_agent_response_callback ,
829824 ),
830825 )
831826 for agent in self ._members
0 commit comments