@@ -12,6 +12,7 @@ import type {
1212 AgentProvider ,
1313 IAgent ,
1414} from '@/core/agent/types' ;
15+ import { isDeepEqualConfig } from '@/shared/utils/config' ;
1516
1617// ============================================================================
1718// Agent Instance State
@@ -165,10 +166,31 @@ class AgentRegistry {
165166 */
166167 async getInstance ( type : string , config ?: AgentConfig ) : Promise < IAgent > {
167168 let instanceData = this . instances . get ( type ) ;
169+ const effectiveConfig : AgentConfig = {
170+ ...( config ?? { } ) ,
171+ provider : type as AgentProvider ,
172+ } ;
168173
169174 if ( instanceData && instanceData . state === 'ready' ) {
170- instanceData . lastUsedAt = new Date ( ) ;
171- return instanceData . agent ;
175+ if ( isDeepEqualConfig ( instanceData . config , effectiveConfig ) ) {
176+ instanceData . lastUsedAt = new Date ( ) ;
177+ return instanceData . agent ;
178+ }
179+ try {
180+ const agentWithShutdown = instanceData . agent as {
181+ shutdown ?: ( ) => Promise < void > ;
182+ } ;
183+ if ( typeof agentWithShutdown . shutdown === 'function' ) {
184+ await agentWithShutdown . shutdown ( ) ;
185+ }
186+ } catch ( error ) {
187+ console . warn (
188+ `[${ this . registryName } ] Failed to shutdown provider ${ type } :` ,
189+ error
190+ ) ;
191+ }
192+ this . instances . delete ( type ) ;
193+ instanceData = undefined ;
172194 }
173195
174196 // If instance exists but is in error state, try to recreate
@@ -181,7 +203,6 @@ class AgentRegistry {
181203 }
182204
183205 // Create new instance
184- const effectiveConfig = config || { provider : type as AgentProvider } ;
185206 const agent = this . create ( type , effectiveConfig ) ;
186207 instanceData = {
187208 agent,
0 commit comments