Files
ServerKit/backend/app/plugins_sdk/__init__.py
T
Juan Denis ddcf0b2f5f Add plugin contributions system, SDK, and integration
Introduce a first-class plugin contribution system and SDK, plus frontend integration and installer enhancements.

Backend:
- Add /plugins/manifest-spec and /plugins/contributions endpoints to expose the plugin.json contract and merged active-plugin UI contributions.
- Add app/plugins_sdk for plugin authors (current_user, logger, audit, jwt helpers).
- Add ContributionService to aggregate active plugin contributions (nav, routes, page_titles, command_palette, widgets).
- Marketplace now hands off to plugin installer when an extension has a source URL and enforces admin-only installs in the marketplace API.
- PluginService: run lifecycle hooks (install/uninstall) and install declared template dependencies (best-effort, recorded/logged) during plugin installs; surface template/manifest fields on InstalledPlugin.to_dict; surface linked InstalledPlugin status on ExtensionInstall.to_dict.

Frontend:
- Add runtime contributions loader (plugins/contributions.js) with useContributions hook and resolveComponent/getPluginModule helpers.
- Add SDK re-exports for plugin frontends (plugins/sdk/index.js) and Vite alias 'serverkit-sdk'.
- Integrate contributions across UI: Sidebar, CommandPalette, PluginLoader (declared widgets + legacy fallback), PageTitleUpdater, DashboardLayout (refreshContributions), and route injection via new ExtensionRoutes hook loaded in App.jsx.
- Add API client getPluginContributions and adjust PluginLoader discovery patterns.

Other:
- Add .extension-platform-plan/ to .gitignore.

Overall this enables declarative plugin-driven UI integration (sidebar items, SPA routes, page titles, command palette entries, and widgets), lifecycle hooks, and template dependency installs while keeping legacy behavior for older plugins.
2026-05-03 13:54:40 -04:00

97 lines
2.5 KiB
Python

"""
ServerKit Backend Plugin SDK.
Plugins should depend on names re-exported here rather than reaching into
host internals directly. As long as this surface stays stable, host-side
refactors don't break installed plugins.
Typical plugin module:
from flask import Blueprint, request, jsonify
from app.plugins_sdk import (
db, jwt_required, current_user, audit, logger,
)
my_bp = Blueprint('my_plugin', __name__)
log = logger(__name__)
@my_bp.route('/things', methods=['GET'])
@jwt_required()
def list_things():
user = current_user()
# ... do work, use db.session, etc ...
return jsonify({'ok': True})
The lifecycle hook contract (called by plugin_service when installing /
uninstalling): a single positional arg — the InstalledPlugin row.
# In plugins/<slug>/lifecycle.py
from app.plugins_sdk import db, logger
log = logger(__name__)
def on_install(plugin):
log.info(f'Setting up {plugin.slug}')
def on_uninstall(plugin):
log.info(f'Tearing down {plugin.slug}')
"""
import logging
from app import db, jwt
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt
def current_user():
"""Return the current authenticated user, or None if no JWT context.
Resolves the JWT identity (a stringified user id, per the host's
@jwt.user_identity_loader) into a User row.
"""
try:
uid = get_jwt_identity()
except Exception:
return None
if uid is None:
return None
from app.models.user import User
try:
return User.query.get(int(uid))
except (TypeError, ValueError):
return None
def logger(name):
"""Return a logger scoped to a plugin module name."""
return logging.getLogger(name)
def audit(action, target_type, target_id=None, details=None, user_id=None):
"""Write an audit log entry from a plugin.
Thin wrapper around AuditService so plugins don't have to know its
import path. `user_id` defaults to the current authenticated user.
"""
if user_id is None:
u = current_user()
user_id = u.id if u else None
from app.services.audit_service import AuditService
return AuditService.log(
action=action,
user_id=user_id,
target_type=target_type,
target_id=target_id,
details=details or {},
)
__all__ = [
'db',
'jwt',
'jwt_required',
'get_jwt_identity',
'get_jwt',
'current_user',
'logger',
'audit',
]