from fastapi import APIRouter, HTTPException, Depends, Path, Response from typing import List, Dict, Any from fastapi import status # Import auth module from auth import get_current_active_user, get_user_context # Importiere die Attributdefinition und Hilfsfunktionen from attributes_def import AttributeDefinition, get_model_attributes # Importiere die Modellmodule (ohne spezifische Klassen) import modules.gateway_model as gateway_model import modules.lucydom_model as lucydom_model # Erstelle ein Dictionary, das die Modellklassen ihren Typen zuordnet model_classes = { "workspace": lucydom_model.Workspace, "agent": lucydom_model.Agent, "file": lucydom_model.DataObject, "prompt": lucydom_model.Prompt, "user": gateway_model.User, "mandate": gateway_model.Mandate, "workflow_request": lucydom_model.WorkflowRequest, "workflow_response": lucydom_model.WorkflowResponse, "log_entry": lucydom_model.LogEntry, "result": lucydom_model.Result } # Erstelle einen Router für die Attribute-Endpunkte router = APIRouter( prefix="/api/attributes", tags=["Attributes"], responses={404: {"description": "Not found"}} ) @router.get("/{entity_type}", response_model=List[AttributeDefinition]) async def get_entity_attributes( entity_type: str = Path(..., description="Typ der Entität (z.B. workspace, agent, prompt)"), current_user: Dict[str, Any] = Depends(get_current_active_user) ): """ Ruft die Attributdefinitionen für eine bestimmte Entität ab. Dies kann für die dynamische Generierung von Formularen verwendet werden. """ # Authentifizierung und Benutzerkontext mandate_id, user_id = await get_user_context(current_user) # Bevorzugte Sprache des Benutzers ermitteln user_language = current_user.get("language", "de") # Prüfen, ob Entitätstyp bekannt ist if entity_type not in model_classes: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Entitätstyp '{entity_type}' nicht gefunden." ) # Model-Klasse abrufen und Attribute daraus ableiten model_class = model_classes[entity_type] attributes = get_model_attributes(model_class, user_language) # Nur bearbeitbare und sichtbare Attribute zurückgeben visible_attributes = [attr for attr in attributes if attr.visible] # Wenn es sich um die Workspace-Auswahl handelt, Daten aus der Datenbank laden for attr in visible_attributes: if attr.name == "workspace_id" and attr.type == "select": # LucyDOM-Interface mit Benutzerkontext initialisieren from modules.lucydom_interface import get_lucydom_interface lucy_interface = get_lucydom_interface(mandate_id, user_id) # Alle verfügbaren Workspaces abrufen workspaces = lucy_interface.get_all_workspaces() # Options-Liste aus Workspaces erstellen attr.options = [ {"value": ws["id"], "label": ws["name"]} for ws in workspaces ] # Wenn es sich um Agent-Auswahl handelt und ein Workspace-Kontext existiert elif attr.name == "agent_id" and attr.type == "select" and "workspace_id" in [a.name for a in visible_attributes]: # Wenn ein aktueller Workspace gesetzt ist workspace_id_attr = next((a for a in visible_attributes if a.name == "workspace_id"), None) if workspace_id_attr and workspace_id_attr.default_value: workspace_id = workspace_id_attr.default_value # LucyDOM-Interface mit Benutzerkontext initialisieren from modules.lucydom_interface import get_lucydom_interface lucy_interface = get_lucydom_interface(mandate_id, user_id) # Agenten für den Workspace abrufen agents = lucy_interface.get_agents_by_workspace(workspace_id) # Options-Liste aus Agenten erstellen attr.options = [ {"value": agent["id"], "label": agent["name"]} for agent in agents ] return visible_attributes @router.options("/{entity_type}") async def options_entity_attributes( entity_type: str = Path(..., description="Typ der Entität (z.B. workspace, agent, prompt)") ): return Response(status_code=200)