refactor(block-h): own integration agent tools in their plugins
This commit is contained in:
@@ -210,12 +210,11 @@ class AutomationPlugin(BasePlugin):
|
||||
register_agent_coordinator_tools()
|
||||
except Exception:
|
||||
logger.exception("Failed to register agent coordinator tools")
|
||||
# Register integration tools (I-AW: Agent→Workflow, I-AK: Agent→Knowledge)
|
||||
# Register workflow agent tools (I-AW: Agent→Workflow)
|
||||
try:
|
||||
from app.ai.integration_tools import register_integration_tools
|
||||
register_integration_tools()
|
||||
self._register_workflow_agent_tools()
|
||||
except Exception:
|
||||
logger.exception("Failed to register integration tools")
|
||||
logger.exception("Failed to register workflow agent tools")
|
||||
# Register MiniApps from manifest
|
||||
try:
|
||||
from app.plugins.builtins.kommunikation.contracts import get_miniapp_registry
|
||||
@@ -286,6 +285,94 @@ class AutomationPlugin(BasePlugin):
|
||||
|
||||
logger.info("Automation plugin activated")
|
||||
|
||||
def _register_workflow_agent_tools(self) -> None:
|
||||
"""Register I-AW agent tools for starting and inspecting workflows."""
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from app.ai.tool_registry import get_tool_registry
|
||||
registry = get_tool_registry()
|
||||
|
||||
async def _start_workflow_handler(arguments: dict[str, Any], context: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Start a workflow by ID."""
|
||||
from app.services.workflow_service import create_instance
|
||||
from app.core.db import get_worker_session_factory
|
||||
workflow_id = arguments.get("workflow_id", "")
|
||||
tenant_id = context.get("tenant_id")
|
||||
user_id = context.get("user_id")
|
||||
if not workflow_id or not tenant_id:
|
||||
return {"error": "workflow_id and tenant_id required"}
|
||||
factory = get_worker_session_factory()
|
||||
async with factory() as db:
|
||||
instance = await create_instance(
|
||||
db=db,
|
||||
tenant_id=uuid.UUID(str(tenant_id)),
|
||||
workflow_id=uuid.UUID(workflow_id),
|
||||
initiated_by=uuid.UUID(str(user_id)) if user_id else None,
|
||||
)
|
||||
await db.commit()
|
||||
return {"instance_id": str(instance.get("id", "")), "status": instance.get("status", "created")}
|
||||
|
||||
registry.register(
|
||||
name="start_workflow",
|
||||
description="Start a workflow by its ID. Returns the instance ID and status.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"workflow_id": {"type": "string", "description": "UUID of the workflow to start"},
|
||||
},
|
||||
"required": ["workflow_id"],
|
||||
},
|
||||
handler=_start_workflow_handler,
|
||||
plugin_name=self.manifest.name,
|
||||
required_permission="workflows:read",
|
||||
category="workflow",
|
||||
)
|
||||
|
||||
async def _check_workflow_status_handler(arguments: dict[str, Any], context: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Check the status of a workflow instance."""
|
||||
from sqlalchemy import select
|
||||
from app.models.workflow import WorkflowInstance
|
||||
from app.core.db import get_worker_session_factory
|
||||
instance_id = arguments.get("instance_id", "")
|
||||
tenant_id = context.get("tenant_id")
|
||||
if not instance_id or not tenant_id:
|
||||
return {"error": "instance_id and tenant_id required"}
|
||||
factory = get_worker_session_factory()
|
||||
async with factory() as db:
|
||||
result = await db.execute(
|
||||
select(WorkflowInstance).where(
|
||||
WorkflowInstance.id == uuid.UUID(instance_id),
|
||||
WorkflowInstance.tenant_id == uuid.UUID(str(tenant_id)),
|
||||
)
|
||||
)
|
||||
inst = result.scalar_one_or_none()
|
||||
if not inst:
|
||||
return {"error": "Instance not found"}
|
||||
return {
|
||||
"instance_id": str(inst.id),
|
||||
"status": inst.status,
|
||||
"current_step": inst.current_step_index,
|
||||
"completed_at": inst.completed_at.isoformat() if inst.completed_at else None,
|
||||
}
|
||||
|
||||
registry.register(
|
||||
name="check_workflow_status",
|
||||
description="Check the status of a workflow instance by its ID.",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"instance_id": {"type": "string", "description": "UUID of the workflow instance"},
|
||||
},
|
||||
"required": ["instance_id"],
|
||||
},
|
||||
handler=_check_workflow_status_handler,
|
||||
plugin_name=self.manifest.name,
|
||||
required_permission="workflows:read",
|
||||
category="workflow",
|
||||
)
|
||||
logger.info("Registered workflow agent tools: start_workflow, check_workflow_status")
|
||||
|
||||
async def on_deactivate(self, db, service_container, event_bus) -> None:
|
||||
"""Clean up on deactivation."""
|
||||
# Contract abmelden
|
||||
@@ -307,6 +394,13 @@ class AutomationPlugin(BasePlugin):
|
||||
unregister_agent_coordinator_tools()
|
||||
except Exception:
|
||||
logger.exception("Failed to unregister agent coordinator tools")
|
||||
# Unregister workflow agent tools from the core AI tool registry
|
||||
try:
|
||||
from app.ai.tool_registry import get_tool_registry
|
||||
get_tool_registry().unregister_plugin(self.manifest.name)
|
||||
logger.info("Unregistered AI agent tools for plugin '%s'", self.manifest.name)
|
||||
except Exception:
|
||||
logger.exception("Failed to unregister AI agent tools")
|
||||
# Unregister MiniApps
|
||||
try:
|
||||
from app.plugins.builtins.kommunikation.contracts import get_miniapp_registry
|
||||
|
||||
Reference in New Issue
Block a user