162 lines
No EOL
5.2 KiB
Python
162 lines
No EOL
5.2 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 Prompt-Endpunkte erstellen
|
|
router = APIRouter(
|
|
prefix="/api/prompts",
|
|
tags=["Prompts"],
|
|
responses={404: {"description": "Not found"}}
|
|
)
|
|
|
|
@router.get("", response_model=List[Dict[str, Any]])
|
|
async def get_prompts(
|
|
workspace_id: Optional[int] = Query(None),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Alle Prompts oder Prompts 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_prompts_by_workspace(workspace_id)
|
|
|
|
return lucy_interface.get_all_prompts()
|
|
|
|
|
|
@router.post("", response_model=Dict[str, Any])
|
|
async def create_prompt(
|
|
prompt: Dict[str, Any] = Body(...),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen neuen Prompt 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 = prompt.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"
|
|
)
|
|
|
|
new_prompt = lucy_interface.create_prompt(
|
|
content=prompt.get("content", ""),
|
|
workspace_id=workspace_id
|
|
)
|
|
|
|
return new_prompt
|
|
|
|
|
|
@router.get("/{prompt_id}", response_model=Dict[str, Any])
|
|
async def get_prompt(
|
|
prompt_id: int,
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen bestimmten Prompt 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)
|
|
|
|
prompt = lucy_interface.get_prompt(prompt_id)
|
|
if not prompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt mit ID {prompt_id} nicht gefunden"
|
|
)
|
|
|
|
return prompt
|
|
|
|
|
|
@router.put("/{prompt_id}", response_model=Dict[str, Any])
|
|
async def update_prompt(
|
|
prompt_id: int,
|
|
prompt_data: Dict[str, Any] = Body(...),
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen bestehenden Prompt 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)
|
|
|
|
# Prüfe, ob der Prompt existiert
|
|
existing_prompt = lucy_interface.get_prompt(prompt_id)
|
|
if not existing_prompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt mit ID {prompt_id} nicht gefunden"
|
|
)
|
|
|
|
# Wenn workspace_id vorhanden ist, prüfe, ob der Workspace existiert
|
|
workspace_id = prompt_data.get("workspace_id")
|
|
if 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"
|
|
)
|
|
|
|
updated_prompt = lucy_interface.update_prompt(
|
|
prompt_id=prompt_id,
|
|
content=prompt_data.get("content"),
|
|
workspace_id=workspace_id
|
|
)
|
|
|
|
if not updated_prompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Fehler beim Aktualisieren des Prompts"
|
|
)
|
|
|
|
return updated_prompt
|
|
|
|
|
|
@router.delete("/{prompt_id}", response_model=Dict[str, Any])
|
|
async def delete_prompt(
|
|
prompt_id: int,
|
|
current_user: Dict[str, Any] = Depends(get_current_active_user)
|
|
):
|
|
"""Einen Prompt 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)
|
|
|
|
# Prüfe, ob der Prompt existiert
|
|
existing_prompt = lucy_interface.get_prompt(prompt_id)
|
|
if not existing_prompt:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Prompt mit ID {prompt_id} nicht gefunden"
|
|
)
|
|
|
|
success = lucy_interface.delete_prompt(prompt_id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Fehler beim Löschen des Prompts"
|
|
)
|
|
|
|
return {"message": f"Prompt mit ID {prompt_id} wurde erfolgreich gelöscht"} |