@@ -91,6 +91,26 @@ function AgentSetup( {
9191 // Load external providers and initialize agent config
9292 useEffect ( ( ) => {
9393 const initializeAgent = async ( ) => {
94+ // Handle new chat: clear existing session and navigate to clean state
95+ if ( isNewChat ) {
96+ const agentManager = getAgentManager ( ) ;
97+
98+ if ( agentManager . hasAgent ( ORCHESTRATOR_AGENT_ID ) ) {
99+ // Abort any ongoing requests
100+ await agentManager . abortCurrentRequest ( ORCHESTRATOR_AGENT_ID ) ;
101+ // Remove existing agent to start fresh
102+ agentManager . removeAgent ( ORCHESTRATOR_AGENT_ID ) ;
103+ }
104+
105+ // Clear stored session ID
106+ clearSessionId ( ) ;
107+ // Clear route state to prevent repeated new chat initialization
108+ navigate ( '/chat' , { replace : true } ) ;
109+
110+ // Don't set config now - the navigation above will re-run this effect
111+ return ;
112+ }
113+
94114 // Load external providers (only once)
95115 let providers = loadedProvidersRef . current ;
96116 if ( ! providers ) {
@@ -165,24 +185,6 @@ function AgentSetup( {
165185 } ;
166186 }
167187
168- // If starting a new chat, clear existing session
169- if ( isNewChat ) {
170- const agentManager = getAgentManager ( ) ;
171- const agentId = config . agentId ;
172-
173- if ( agentManager . hasAgent ( agentId ) ) {
174- // Abort any ongoing requests
175- await agentManager . abortCurrentRequest ( agentId ) ;
176- // Remove existing agent to start fresh
177- agentManager . removeAgent ( agentId ) ;
178- }
179-
180- // Clear stored session ID
181- clearSessionId ( ) ;
182- // Clear route state to prevent repeated new chat initialization
183- navigate ( '/chat' , { replace : true } ) ;
184- }
185-
186188 setAgentConfig ( config ) ;
187189 } ;
188190
0 commit comments