@@ -426,12 +426,9 @@ def add(self, name: str, component: Any, collection: Optional[str] = None):
426426                    logger .warning (
427427                        f"ComponentsManager: removing existing { name } { collection } { comp_id }  
428428                    )
429-                     # remove only from this collection 
430-                     self .collections [collection ].remove (comp_id )
431-                     comp_colls  =  [coll  for  coll , comps  in  self .collections .items () if  comp_id  in  comps ]
432-                     if  not  comp_colls :  # only if no other collection contains this component, remove it 
433-                         logger .info (f"ComponentsManager: removing component '{ comp_id }  )
434-                         self .remove (comp_id )
429+                     # remove existing component from this collection (if it is not in any other collection, will be removed from ComponentsManager) 
430+                     self .remove_from_collection (comp_id , collection )
431+ 
435432                self .collections [collection ].add (component_id )
436433                logger .info (
437434                    f"ComponentsManager: added component '{ name } { collection } { component_id }  
@@ -444,6 +441,24 @@ def add(self, name: str, component: Any, collection: Optional[str] = None):
444441
445442        return  component_id 
446443
444+     def  remove_from_collection (self , component_id : str , collection : str ):
445+         """ 
446+         Remove a component from a collection. 
447+         """ 
448+         if  collection  not  in self .collections :
449+             logger .warning (f"Collection '{ collection }  )
450+             return 
451+         if  component_id  not  in self .collections [collection ]:
452+             logger .warning (f"Component '{ component_id } { collection }  )
453+             return 
454+         # remove from the collection 
455+         self .collections [collection ].remove (component_id )
456+         # check if this component is in any other collection 
457+         comp_colls  =  [coll  for  coll , comps  in  self .collections .items () if  component_id  in  comps ]
458+         if  not  comp_colls :  # only if no other collection contains this component, remove it 
459+             logger .warning (f"ComponentsManager: removing component '{ component_id }  )
460+             self .remove (component_id )
461+ 
447462    def  remove (self , component_id : str  =  None ):
448463        """ 
449464        Remove a component from the ComponentsManager. 
0 commit comments