1414# Create custom execution settings to fix schema issues
1515from semantic_kernel .connectors .ai .open_ai import (
1616 AzureChatCompletion , OpenAIChatPromptExecutionSettings )
17+ from semantic_kernel .contents import (ChatMessageContent ,
18+ StreamingChatMessageContent )
1719from v3 .callbacks .response_handlers import (agent_response_callback ,
18- set_user_context ,
1920 streaming_agent_response_callback )
2021from v3 .config .settings import config , current_user_id , orchestration_config
2122from v3 .magentic_agents .magentic_agent_factory import MagenticAgentFactory
@@ -32,7 +33,7 @@ def __init__(self):
3233 self .user_id : Optional [str ] = None
3334
3435 @classmethod
35- async def init_orchestration (cls , agents : List )-> MagenticOrchestration :
36+ async def init_orchestration (cls , agents : List , user_id : str = None )-> MagenticOrchestration :
3637 """Main function to run the agents."""
3738
3839 # Custom execution settings that should work with Azure OpenAI
@@ -59,28 +60,39 @@ def get_token():
5960 ),
6061 execution_settings = execution_settings
6162 ),
62- agent_response_callback = agent_response_callback ,
63- streaming_agent_response_callback = streaming_agent_response_callback , # Add streaming callback
63+ agent_response_callback = cls . _user_aware_agent_callback ( user_id ) ,
64+ streaming_agent_response_callback = cls . _user_aware_streaming_callback ( user_id )
6465 )
6566 return magentic_orchestration
6667
68+ @staticmethod
69+ def _user_aware_agent_callback (user_id : str ):
70+ """Factory method that creates a callback with captured user_id"""
71+ def callback (message : ChatMessageContent ):
72+ return agent_response_callback (message , user_id )
73+ return callback
74+
75+ @staticmethod
76+ def _user_aware_streaming_callback (user_id : str ):
77+ """Factory method that creates a streaming callback with captured user_id"""
78+ async def callback (streaming_message : StreamingChatMessageContent , is_final : bool ):
79+ return await streaming_agent_response_callback (streaming_message , is_final , user_id )
80+ return callback
81+
6782 @classmethod
6883 async def get_current_or_new_orchestration (self , user_id : str , team_config : TeamConfiguration ) -> MagenticOrchestration :
6984 """get existing orchestration instance."""
7085 current_orchestration = orchestration_config .get_current_orchestration (user_id )
7186 if current_orchestration is None :
7287 factory = MagenticAgentFactory ()
73- # to do: change to parsing teams from cosmos db
7488 agents = await factory .get_agents (team_config_input = team_config )
75- orchestration_config .orchestrations [user_id ] = await self .init_orchestration (agents )
89+ orchestration_config .orchestrations [user_id ] = await self .init_orchestration (agents , user_id )
7690 return orchestration_config .get_current_orchestration (user_id )
7791
7892 async def run_orchestration (self , user_id , input_task ) -> None :
7993 """ Run the orchestration with user input loop."""
8094 token = current_user_id .set (user_id )
8195
82- set_user_context (user_id )
83-
8496 job_id = str (uuid .uuid4 ())
8597 orchestration_config .approvals [job_id ] = None
8698
@@ -90,9 +102,7 @@ async def run_orchestration(self, user_id, input_task) -> None:
90102 raise ValueError ("Orchestration not initialized for user." )
91103
92104 try :
93- # ADD THIS: Set user_id on the approval manager before invoke
94105 if hasattr (magentic_orchestration , '_manager' ) and hasattr (magentic_orchestration ._manager , 'current_user_id' ):
95- #object.__setattr__(magentic_orchestration._manager, 'current_user_id', user_id)
96106 magentic_orchestration ._manager .current_user_id = user_id
97107 print (f"🔍 DEBUG: Set user_id on manager = { user_id } " )
98108 except Exception as e :
0 commit comments