From 4e36e85b70e6177089f6b99938aeb31a9313d70c Mon Sep 17 00:00:00 2001 From: cobycloud <25079070+cobycloud@users.noreply.github.com> Date: Fri, 27 Dec 2024 04:13:41 -0600 Subject: [PATCH] Update plugin_manager.py --- pkgs/swarmauri/swarmauri/plugin_manager.py | 45 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/pkgs/swarmauri/swarmauri/plugin_manager.py b/pkgs/swarmauri/swarmauri/plugin_manager.py index eda36c57..f217df2d 100644 --- a/pkgs/swarmauri/swarmauri/plugin_manager.py +++ b/pkgs/swarmauri/swarmauri/plugin_manager.py @@ -89,25 +89,54 @@ class SecondClassPluginManager(PluginManagerBase): """ Manager for second-class plugins. """ + def validate(self, name, plugin_class, resource_kind, resource_interface): """ - Validate that the plugin implements the required interface. + Validate that the plugin implements the required interface and does not conflict with first-class citizens. """ if not issubclass(plugin_class, resource_interface): raise TypeError( f"Plugin '{name}' must implement the '{resource_interface.__name__}' interface." ) - def register(self, name, plugin_class, resource_kind): + # Check for conflicts with first-class citizens + resource_path = f"swarmauri.{resource_kind}.{plugin_class.__name__}" + first_class_entry = FIRST_CLASS_REGISTRY.get(resource_path) + if first_class_entry: + registered_module_path = first_class_entry["module_path"] + incoming_module_path = plugin_class.__module__ + + if registered_module_path != incoming_module_path: + raise ValueError( + f"Conflict detected: Second-class plugin '{name}' (module: {incoming_module_path}) " + f"attempts to override first-class citizen (module: {registered_module_path})." + ) + + def register(self, entry_points): """ - Register the plugin as a second-class citizen. + Register second-class plugins, iterating over multiple entry points. + + :param entry_points: List of entry points associated with a namespace. """ - resource_path = f"swarmauri.{resource_kind}.{plugin_class.__name__}" - if read_entry(resource_path) or resource_path in SECOND_CLASS_REGISTRY: - raise ValueError(f"Plugin '{name}' is already registered as a second-class citizen.") + for entry_point in entry_points: + name = entry_point.name + namespace = entry_point.group + resource_path = f"{namespace}.{name}" + + if read_entry(resource_path) or resource_path in SECOND_CLASS_REGISTRY: + raise ValueError(f"Plugin '{name}' is already registered under '{resource_path}'.") + + # Dynamically load the plugin class + plugin_class = entry_point.load() + + # Validate against first-class citizens + resource_kind = namespace[len("swarmauri."):] if namespace.startswith("swarmauri.") else None + resource_interface = get_interface_for_resource(resource_kind) + self.validate(name, plugin_class, resource_kind, resource_interface) + + create_entry("second", resource_path, plugin_class.__module__) + logger.info(f"Registered second-class plugin: {plugin_class.__module__} -> {resource_path}") - create_entry("second", resource_path, plugin_class.__module__) - logger.info(f"Registered second-class citizen: {resource_path}") class ThirdClassPluginManager(PluginManagerBase):