Skip to content

Commit

Permalink
Update plugin_manager.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cobycloud committed Dec 27, 2024
1 parent 9301847 commit 4e36e85
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions pkgs/swarmauri/swarmauri/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4e36e85

Please sign in to comment.