162 lines
No EOL
5.8 KiB
Python
162 lines
No EOL
5.8 KiB
Python
from fastapi import APIRouter, HTTPException, Depends, Body, Query, Path
|
|
from typing import List, Dict, Any, Optional
|
|
from fastapi import status
|
|
|
|
# Import auth module
|
|
from auth import get_current_active_user, get_user_context
|
|
|
|
# Import interfaces
|
|
from modules.lucydom_interface import get_lucydom_interface
|
|
|
|
# Router für Agenten-Endpunkte erstellen
|
|
router = APIRouter(
|
|
prefix="/api/agents",
|
|
tags=["Agents"],
|
|
responses={404: {"description": "Not found"}}
|
|
)
|
|
|
|
@router.get("", response_model=List[Dict[str, Any]])
|
|
async def get_agents(
|
|
workspace_id: Optional[int] = Query(None),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Alle Agenten oder Agenten eines bestimmten Workspaces abrufen"""
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# LucyDOM-Interface mit Benutzerkontext initialisieren
|
|
lucy_interface = get_lucydom_interface(mandate_id, user_id)
|
|
|
|
if workspace_id:
|
|
return lucy_interface.get_agents_by_workspace(workspace_id)
|
|
|
|
return lucy_interface.get_all_agents()
|
|
|
|
|
|
@router.post("", response_model=Dict[str, Any])
|
|
async def create_agent(
|
|
agent: Dict[str, Any] = Body(...),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen neuen Agenten erstellen"""
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# LucyDOM-Interface mit Benutzerkontext initialisieren
|
|
lucy_interface = get_lucydom_interface(mandate_id, user_id)
|
|
|
|
workspace_id = agent.get("workspace_id")
|
|
|
|
if not workspace_id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="workspace_id ist erforderlich"
|
|
)
|
|
|
|
# Workspace existiert?
|
|
workspace = lucy_interface.get_workspace(workspace_id)
|
|
if not workspace:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Workspace mit ID {workspace_id} nicht gefunden für User {user_id} im Mandanten {mandate_id}"
|
|
)
|
|
|
|
new_agent = lucy_interface.create_agent(
|
|
name=agent.get("name", "Neuer Agent"),
|
|
agent_type=agent.get("type", "generic"),
|
|
workspace_id=workspace_id,
|
|
capabilities=agent.get("capabilities", ""),
|
|
description=agent.get("description", "")
|
|
)
|
|
|
|
return new_agent
|
|
|
|
|
|
@router.get("/{agent_id}", response_model=Dict[str, Any])
|
|
async def get_agent(
|
|
agent_id: int,
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen bestimmten Agenten abrufen"""
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# LucyDOM-Interface mit Benutzerkontext initialisieren
|
|
lucy_interface = get_lucydom_interface(mandate_id, user_id)
|
|
|
|
agent = lucy_interface.get_agent(agent_id)
|
|
if not agent:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Agent mit ID {agent_id} nicht gefunden"
|
|
)
|
|
return agent
|
|
|
|
|
|
@router.post("/{agent_id}", response_model=Dict[str, Any])
|
|
async def update_agent(
|
|
agent_id: int = Path(..., description="ID des zu aktualisierenden Agenten"),
|
|
agent_data: Dict[str, Any] = Body(..., description="Aktualisierte Agentendaten"),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen bestehenden Agenten aktualisieren"""
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# LucyDOM-Interface mit Benutzerkontext initialisieren
|
|
lucy_interface = get_lucydom_interface(mandate_id, user_id)
|
|
|
|
# Überprüfen, ob der Agent existiert
|
|
existing_agent = lucy_interface.get_agent(agent_id)
|
|
if not existing_agent:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Agent mit ID {agent_id} nicht gefunden"
|
|
)
|
|
|
|
# Wenn workspace_id im Request vorhanden ist, prüfen ob der Workspace existiert
|
|
if "workspace_id" in agent_data:
|
|
workspace_id = agent_data["workspace_id"]
|
|
workspace = lucy_interface.get_workspace(workspace_id)
|
|
if not workspace:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Workspace mit ID {workspace_id} nicht gefunden für User {user_id} im Mandanten {mandate_id}"
|
|
)
|
|
|
|
# Agent-Daten aktualisieren
|
|
updated_agent = lucy_interface.update_agent(
|
|
agent_id=agent_id,
|
|
name=agent_data.get("name", existing_agent.get("name")),
|
|
agent_type=agent_data.get("type", existing_agent.get("type")),
|
|
workspace_id=agent_data.get("workspace_id", existing_agent.get("workspace_id")),
|
|
capabilities=agent_data.get("capabilities", existing_agent.get("capabilities")),
|
|
description=agent_data.get("description", existing_agent.get("description"))
|
|
)
|
|
return updated_agent
|
|
|
|
@router.delete("/{agent_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_agent(
|
|
agent_id: int = Path(..., description="ID des zu löschenden Agenten"),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen Agenten löschen"""
|
|
mandate_id, user_id = await get_user_context(current_user)
|
|
|
|
# LucyDOM-Interface mit Benutzerkontext initialisieren
|
|
lucy_interface = get_lucydom_interface(mandate_id, user_id)
|
|
|
|
# Überprüfen, ob der Agent existiert
|
|
existing_agent = lucy_interface.get_agent(agent_id)
|
|
if not existing_agent:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Agent mit ID {agent_id} nicht gefunden"
|
|
)
|
|
|
|
# Agent löschen
|
|
success = lucy_interface.delete_agent(agent_id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Fehler beim Löschen des Agenten mit ID {agent_id}"
|
|
)
|
|
|
|
# Kein Inhalt zurückgeben bei erfolgreichem Löschen
|
|
return None |