plugin_manager.py
Overview
plugin_manager.py defines the PluginManager class responsible for discovering, loading, and managing plugins specifically of type LLM Tool plugins within the system. It leverages an external plugin loading mechanism (pluginlib.PluginLoader) to dynamically load plugins from the embedded plugin directory and provides interfaces to query and retrieve these plugins by name or in bulk.
This file plays a crucial role in the plugin architecture by encapsulating plugin lifecycle management and offering convenient access to loaded LLM tool plugins, which are likely used elsewhere in the application to extend or customize functionality related to Large Language Model (LLM) tools.
Classes and Methods
Class: PluginManager
Manages the lifecycle and access of LLM Tool plugins loaded from embedded plugin sources.
Properties
_llm_tool_plugins: dict[str, LLMToolPlugin]
Internal dictionary mapping plugin names to their correspondingLLMToolPlugininstances.
Methods
__init__(self) -> None
Initializes a new instance of PluginManager.
Parameters: None
Returns: None
Description:
Initializes the internal dictionary to hold loaded LLM tool plugins.Example Usage:
manager = PluginManager()
load_plugins(self) -> None
Discovers and loads plugins from the embedded plugins directory.
Parameters: None
Returns: None
Description:
Usespluginlib.PluginLoaderto scan theembedded_pluginsdirectory relative to this file and loads all discovered plugins. For each plugin loaded:Logs an info message including the plugin type, name, and version.
If the plugin type matches the constant
PLUGIN_TYPE_LLM_TOOLS, it stores the plugin instance keyed by the plugin's metadata name in the_llm_tool_pluginsdictionary.
Implementation Details:
The method assumes plugins expose aversionproperty and aget_metadata()method returning a dictionary with at least anamekey. Only plugins of type matchingPLUGIN_TYPE_LLM_TOOLSare stored for later access.Example Usage:
manager = PluginManager() manager.load_plugins()
get_llm_tools(self) -> list[LLMToolPlugin]
Returns a list of all currently loaded LLM tool plugins.
Parameters: None
Returns: List of
LLMToolPlugininstances.Description:
Provides the full collection of loaded LLM tool plugins, suitable for iteration or bulk operations.Example Usage:
tools = manager.get_llm_tools() for tool in tools: print(tool.get_metadata())
get_llm_tool_by_name(self, name: str) -> LLMToolPlugin | None
Fetches a single LLM tool plugin by its unique name.
Parameters:
name(str): The unique name identifier of the desired plugin.
Returns:
The correspondingLLMToolPlugininstance if found; otherwise,None.Description:
Allows targeted retrieval of a specific plugin, facilitating direct interaction or execution.Example Usage:
tool = manager.get_llm_tool_by_name("example_tool") if tool: print(tool.get_metadata())
get_llm_tools_by_names(self, tool_names: list[str]) -> list[LLMToolPlugin]
Fetches multiple LLM tool plugins by a list of their names.
Parameters:
tool_names(list[str]): A list of plugin names to retrieve.
Returns:
A list ofLLMToolPlugininstances corresponding to the found names. Plugins not found are silently ignored.Description:
Supports batch retrieval of plugins by name, useful when multiple known plugins are needed together.Example Usage:
selected_tools = manager.get_llm_tools_by_names(["tool1", "tool2", "tool3"]) for tool in selected_tools: print(tool.get_metadata())
Important Implementation Details
Plugin Discovery Path:
The embedded plugins are loaded from a directory namedembedded_pluginslocated in the same directory asplugin_manager.py. This path is constructed dynamically usingPathandos.path.Dependency on
pluginlib.PluginLoader:
This external utility handles the heavy lifting of plugin discovery and loading. The manager then filters and stores plugins by type.Plugin Metadata Usage:
The manager depends on the plugin'sget_metadata()function to extract anameattribute, which is used as the key in the internal plugin dictionary.Logging:
Each plugin loaded triggers an info-level log entry noting the plugin type, name, and version, aiding in debugging and monitoring plugin loading.
Interaction with Other System Components
pluginlib.PluginLoader:
Integral for discovering and loading plugin modules from the filesystem.llm_tool_plugin.LLMToolPlugin:
All plugins managed here are instances of or compatible with theLLMToolPluginclass, ensuring consistent interface and behavior.common.PLUGIN_TYPE_LLM_TOOLS:
A constant defining the plugin type string used to filter plugins relevant to LLM tools.Plugins in
embedded_pluginsDirectory:
The actual plugin implementations loaded by this manager live in theembedded_pluginsdirectory relative to this file.Client Code:
Other parts of the application request plugins via this manager to extend or customize LLM-related capabilities.
Visual Diagram
classDiagram
class PluginManager {
-_llm_tool_plugins: dict[str, LLMToolPlugin]
+__init__()
+load_plugins() void
+get_llm_tools() list~LLMToolPlugin~
+get_llm_tool_by_name(name: str) LLMToolPlugin | None
+get_llm_tools_by_names(tool_names: list~str~) list~LLMToolPlugin~
}
PluginManager o-- LLMToolPlugin : manages
Summary
The plugin_manager.py file encapsulates the plugin loading and management logic for LLM tool plugins. It abstracts the complexities of plugin discovery, loading, and retrieval, providing a clean and straightforward API for clients to access these dynamically loaded components. This modular design supports extensibility and maintainability in applications that rely on pluggable LLM tool functionalities.