Files
ServerKit/backend/app/plugins_sdk/__init__.py
T
Juan DenisandClaude Opus 4.8 9cab065dc5 feat(ai): add core AI assistant primitive powered by Prompture
AI is a core ServerKit primitive (not a plugin): an Intercom-style bubble + slide-in drawer on every dashboard page, with a tools+page-context assistant mode and a plain simple chat mode, SSE streaming, conversation persistence, and guarded write actions (the model proposes; the user approves).

Plugins EXTEND the assistant rather than rebuilding it: backend via app.plugins_sdk.ai (reg.tool + register_context_provider, discovered from a plugin.json ai block with install/enable/disable hooks); frontend via useServerkitAI() and a contributions.ai category (suggested_prompts, tool_renderers). The marketplace gains an ai category for discovery.

Backend: ai_service (Prompture Conversation + ProviderEnvironment + guardrails), ai_tool_registry singleton, ai_tools_builtin (core.* tools), the /api/v1/ai blueprint (SSE via a worker-thread + queue bridge, single-gevent-worker safe; in-thread confirmation gate), AiConversation/AiMessage/AiPendingAction models + migration 013, encrypted ai_* settings. Frontend: AIContext + components/ai/*, AI settings tab. Powered by Prompture (installed editable; requirements pinned).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 02:39:05 -04:00

103 lines
2.7 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 {},
)
# AI extension SDK — plugins do `from app.plugins_sdk import ai` then register
# tools/context against the core assistant (see plugins_sdk/ai.py). Imported
# lazily-safe: ai.py only depends on the AI tool registry, not on a running app.
from app.plugins_sdk import ai
__all__ = [
'db',
'jwt',
'jwt_required',
'get_jwt_identity',
'get_jwt',
'current_user',
'logger',
'audit',
'ai',
]