Files
leocrm/app/plugins/builtins/ai_proactive/plugin.py
T
Agent Zero 8cebb4f4e9 feat: unified_search + ai_proactive plugins with Ollama Cloud DeepSeek V4
- unified_search: Hybride Suche (PostgreSQL FTS + pgvector + RRF Fusion)
  - 5 Search Providers (Contact, Company, Mail, File, Event)
  - KI Query Understanding (Fuzzy, Facetten via LiteLLM)
  - DMS Text-Extraction (PDF, DOCX, XLSX, PPTX)
  - Embedding Pipeline (ollama/nomic-embed-text, 768 Dim)
  - Background Jobs für Indexierung
  - Plugin-basierte Provider Registry

- ai_proactive: Proaktiver KI-Agent
  - Context-Tracking (Frontend → Backend → Event Bus)
  - Proactive Engine mit LLM Suggestion-Generierung
  - SSE Real-time Push an Frontend
  - 6 AI Tools für Tool Registry
  - Rate-Limiting + User Settings
  - Deep Analysis Background Jobs

- Frontend Integration:
  - useAIContext Hook, SuggestionSidebar, SuggestionBadge
  - ProactiveAISettings Page, Search API Client
  - Globale Suche auf neue API umgestellt

- Tests: test_unified_search.py + test_ai_proactive.py (alle bestanden)
- Config: Ollama Cloud DeepSeek V4 als Default, konfigurierbar
- Dependencies: PyMuPDF, python-docx, python-pptx, pgvector
- Bugfixes: notification type_key length, migration IF NOT EXISTS
2026-07-18 11:21:51 +02:00

108 lines
3.8 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
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"],
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,
)
async def on_activate(self, db, service_container, event_bus) -> None:
"""Register context tools and subscribe to events."""
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.tool_registry 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")
async def on_deactivate(self, db, service_container, event_bus) -> None:
"""Unregister tools and event listeners."""
try:
from app.plugins.builtins.ai_assistant.tool_registry 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,
},
]