Files
leocrm/app/plugins/builtins/ai_proactive/plugin.py
T
Agent Zero 98eb1d0d89
Check Cross-Plugin Imports / check (push) Has been cancelled
feat: Plugin-System Umbau — 6 Phasen komplett abgeschlossen
Phase 1: Contracts konsequent nutzen
- 12 neue contracts.py erstellt (alle 19 Plugins haben jetzt contracts)
- 4 bestehende contracts.py an zentrale ContractRegistry angepasst
- Alle 19 Plugins haben on_deactivate mit Contract-Unregister
- 0 echte problematische INTER-Plugin Imports

Phase 2: Hooks/Filters-System
- app/core/hooks.py (HookRegistry mit actions + filters)
- 15 Hook-Punkte in Core-Services (contact, auth, mail, calendar, user, dms)
- BasePlugin.on_deactivate meldet alle Hooks ab

Phase 3: Plugin-Isolation
- scripts/check_cross_plugin_imports.py (Linting-Regel)
- .github/workflows/check-cross-plugin-imports.yml (CI/CD)
- .pre-commit-cross-plugin.yaml (Pre-commit Hook)
- 155 Dateien geprueft, 0 Verstoesse

Phase 4: Plugin-Versioning
- app/plugins/semver.py (SemVer mit Parse, Compare, Pre-release)
- migration_runner.py erweitert: run_migration_down, rollback_to_version
- manifest.py: min_app_version Feld
- registry.py: App-Version-Compatibility-Check bei Installation
- GET /api/v1/plugins/updates Endpoint

Phase 5: Marketplace-Vorbereitung
- app/plugins/signature.py (Ed25519 Signatur-Validierung)
- app/plugins/quarantine.py (Plugin-Quarantine mit Validierung)
- app/models/plugin_allowlist.py + Migration 0046
- manifest.py: author, license, homepage, icon, screenshots, changelog, marketplace_tags, price
- registry.py: discover_external(), discover_all()
- POST /api/v1/plugins/install-marketplace (deaktiviert)

Phase 6: Manifest-Anpassung
- manifest.py: 12 neue Felder + SemVer/Hook-Name Validierung
- MANIFEST_SCHEMA_DOC aktualisiert
- Alle 19 Plugin-Manifeste aktualisiert
- Frontend PluginUiManifest Typ erweitert

Zusaetzliche Bug-Fixes:
- test_sample-Modul erstellt
- conftest.py Deadlock-Prevention
- SESSION_COOKIE_SECURE=true
- dump.rdb aus Git entfernt + .gitignore
- backup.py datetime.utcnow -> func.now()
- system_settings.py JSONB-Import nach oben
- tax.py Mapped[float] -> Mapped[Decimal]
- notification.py type_key-Laengen vereinheitlicht

Tests: 91 neue Tests, alle bestanden
2026-07-26 23:15:34 +02:00

151 lines
5.7 KiB
Python

"""Proactive AI Agent plugin — context-aware suggestions built on
unified_search and ai_assistant.
Listens to context-change events, gathers entity data, generates LLM-powered
suggestions, pushes them via SSE, and registers AI tools for the assistant.
"""
from __future__ import annotations
import logging
from typing import Any
from app.plugins.base import BasePlugin
from app.plugins.manifest import PluginManifest, PluginRouteDef, FrontendSettingsPage
logger = logging.getLogger(__name__)
class AIProactivePlugin(BasePlugin):
"""Proactive KI Agent that monitors user context and suggests actions."""
manifest = PluginManifest(
name="ai_proactive",
version="1.0.0",
display_name="Proaktiver KI Agent",
description=(
"Überwacht den User-Kontext, generiert proaktiv Vorschläge per LLM "
"und pusht diese via SSE. Nutzt unified_search und ai_assistant."
),
dependencies=["ai_assistant", "unified_search", "kommunikation"],
routes=[
PluginRouteDef(
path="/api/v1/ai-proactive",
module="app.plugins.builtins.ai_proactive.routes",
router_attr="router",
),
],
events=["context.view_changed", "context.entity_selected"],
migrations=["0001_initial.sql"],
permissions=[
"ai_proactive:read",
"ai_proactive:write",
"ai_proactive:config",
],
is_core=False,
settings_pages=[
FrontendSettingsPage(path='ai-proactive', label_key='settings.aiProactive', label='Proactive AI', component='@/pages/ProactiveAISettings', icon='Sparkles', order=61),
],
author="LeoCRM Team",
min_app_version="1.0.0",
hooks=["contact.after_create", "mail.after_send"],
contract_version="1.0.0",
)
def __init__(self) -> None:
super().__init__()
self._proactive_handler = None
async def on_activate(self, db, service_container, event_bus) -> None:
"""Register context tools, subscribe to events, and register as participant."""
await super().on_activate(db, service_container, event_bus)
try:
from app.plugins.builtins.ai_proactive.context_tools import (
register_context_tools,
)
from app.plugins.builtins.ai_assistant.contracts import (
get_tool_registry,
)
register_context_tools(get_tool_registry())
logger.info("AI Proactive context tools registered")
except Exception:
logger.exception("Failed to register AI Proactive context tools")
# Register as participant in the kommunikation system
try:
from app.plugins.builtins.ai_proactive.participant_handler import (
AIProactiveParticipantHandler,
)
from app.plugins.builtins.kommunikation.contracts import (
get_participant_registry,
)
self._proactive_handler = AIProactiveParticipantHandler(service_container)
get_participant_registry().register("ai_proactive", self._proactive_handler)
logger.info("AI Proactive registered as participant 'ai_proactive'")
except Exception:
logger.exception("Failed to register AI Proactive as participant")
async def on_deactivate(self, db, service_container, event_bus) -> None:
"""Unregister tools, event listeners, and participant."""
# Contract abmelden
from app.plugins.builtins.contracts import get_contract_registry
get_contract_registry().unregister(self.manifest.name)
# Unregister from participant registry
try:
from app.plugins.builtins.kommunikation.contracts import (
get_participant_registry,
)
get_participant_registry().unregister("ai_proactive")
logger.info("AI Proactive unregistered as participant 'ai_proactive'")
except Exception:
logger.exception("Failed to unregister AI Proactive as participant")
self._proactive_handler = None
try:
from app.plugins.builtins.ai_assistant.contracts import (
get_tool_registry,
)
get_tool_registry().unregister_plugin("ai_proactive")
logger.info("AI Proactive context tools unregistered")
except Exception:
logger.exception("Failed to unregister AI Proactive context tools")
await super().on_deactivate(db, service_container, event_bus)
# ─── Event Handlers ───
async def on_context_view_changed(self, payload: dict[str, Any]) -> None:
"""Handle context.view_changed event."""
from app.plugins.builtins.ai_proactive.services import handle_context_change
await handle_context_change(payload)
async def on_context_entity_selected(self, payload: dict[str, Any]) -> None:
"""Handle context.entity_selected event."""
from app.plugins.builtins.ai_proactive.services import handle_context_change
await handle_context_change(payload)
def get_notification_types(self) -> list[dict[str, Any]]:
return [
{
"type_key": "ai_suggestion",
"category": "ai",
"label": "KI Vorschlag",
"description": "Proaktiver KI-Vorschlag",
"is_enabled_by_default": True,
},
{
"type_key": "ai_suggestion_urgent",
"category": "ai",
"label": "Dringender KI Vorschlag",
"description": "Dringender proaktiver KI-Vorschlag",
"is_enabled_by_default": True,
},
]