|
| 1 | +# Plugin Guide for Audiokit |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Audiokit package provides a plugin architecture that allows you to extend its functionality by creating independent modules. Plugins are discovered using entry points declared in your pyproject.toml, making it easy to integrate community contributions or proprietary features. |
| 6 | + |
| 7 | +## Base Plugin Interface |
| 8 | + |
| 9 | +The base interface is defined in `audiokit/plugin_interface.py`: |
| 10 | + |
| 11 | +```python |
| 12 | +from abc import ABC, abstractmethod |
| 13 | +from fastapi import FastAPI |
| 14 | +from typing import Dict |
| 15 | + |
| 16 | +class BasePlugin(ABC): |
| 17 | + @abstractmethod |
| 18 | + def register_routes(self, app: FastAPI): |
| 19 | + """Register plugin routes into the FastAPI application.""" |
| 20 | + pass |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def get_metadata(self) -> Dict[str, str]: |
| 24 | + """Return plugin metadata including name, version, and description.""" |
| 25 | + pass |
| 26 | +``` |
| 27 | + |
| 28 | +## Creating a New Plugin |
| 29 | + |
| 30 | +1. Create your plugin file (e.g., `my_plugin.py`) in the `audiokit/plugins/` directory. |
| 31 | +2. Implement your plugin class inheriting from `BasePlugin`. |
| 32 | +3. Register endpoints by implementing the `register_routes(app: FastAPI)` method. |
| 33 | + |
| 34 | +**Example:** |
| 35 | + |
| 36 | +```python |
| 37 | +from fastapi import FastAPI, APIRouter |
| 38 | +from typing import Dict |
| 39 | +from audiokit.plugin_interface import BasePlugin |
| 40 | + |
| 41 | +class MyPlugin(BasePlugin): |
| 42 | + def register_routes(self, app: FastAPI): |
| 43 | + router = APIRouter() |
| 44 | + |
| 45 | + @router.get("/hello") |
| 46 | + async def hello(): |
| 47 | + return {"message": "Hello from MyPlugin!"} |
| 48 | + |
| 49 | + app.include_router(router, prefix="/plugins/my_plugin") |
| 50 | + |
| 51 | + def get_metadata(self) -> Dict[str, str]: |
| 52 | + return {"name": "My Plugin", "version": "1.0.0", "description": "An example plugin."} |
| 53 | +``` |
| 54 | + |
| 55 | +## Configuring Entry Points |
| 56 | + |
| 57 | +To have your plugin discovered, add an entry in your project's `pyproject.toml`` under the`[tool.poetry.plugins."audiokit.plugins"]` group. For example: |
| 58 | + |
| 59 | +```toml |
| 60 | +[tool.poetry.plugins."audiokit.plugins"] |
| 61 | +my_plugin = "audiokit.plugins.my_plugin:MyPlugin" |
| 62 | +``` |
| 63 | + |
| 64 | +## Plugin Manager |
| 65 | + |
| 66 | +At application startup, the plugin manager in `audiokit/plugin_manager.py` will use Python's `importlib.metadata` to load all plugins registered under the `audiokit.plugins` entry point group and automatically register their routes. |
| 67 | + |
| 68 | +## Testing Your Plugin |
| 69 | + |
| 70 | +1. Run the API server and check the logs for plugin load messages. |
| 71 | +2. Verify that the new routes (e.g., `/plugins/my_plugin/hello`) are accessible. |
| 72 | + |
| 73 | +## Conclusion |
| 74 | + |
| 75 | +This plugin system allows for a modular expansion of Audiokit, fostering community contributions and enabling integration of proprietary features as plugins without altering the core codebase. |
0 commit comments