88 lines
No EOL
2.7 KiB
Python
88 lines
No EOL
2.7 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 |