prompts.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from typing import Optional
  2. from open_webui.apps.webui.models.prompts import PromptForm, PromptModel, Prompts
  3. from open_webui.constants import ERROR_MESSAGES
  4. from fastapi import APIRouter, Depends, HTTPException, status, Request
  5. from open_webui.utils.utils import get_admin_user, get_verified_user
  6. from open_webui.utils.access_control import has_access, has_permission
  7. router = APIRouter()
  8. ############################
  9. # GetPrompts
  10. ############################
  11. @router.get("/", response_model=list[PromptModel])
  12. async def get_prompts(user=Depends(get_verified_user)):
  13. if user.role == "admin":
  14. prompts = Prompts.get_prompts()
  15. else:
  16. prompts = Prompts.get_prompts_by_user_id(user.id, "read")
  17. return prompts
  18. @router.get("/list", response_model=list[PromptModel])
  19. async def get_prompt_list(user=Depends(get_verified_user)):
  20. if user.role == "admin":
  21. prompts = Prompts.get_prompts()
  22. else:
  23. prompts = Prompts.get_prompts_by_user_id(user.id, "write")
  24. return prompts
  25. ############################
  26. # CreateNewPrompt
  27. ############################
  28. @router.post("/create", response_model=Optional[PromptModel])
  29. async def create_new_prompt(
  30. request: Request, form_data: PromptForm, user=Depends(get_verified_user)
  31. ):
  32. if user.role != "admin" and not has_permission(
  33. user.id, "workspace.prompts", request.app.state.config.USER_PERMISSIONS
  34. ):
  35. raise HTTPException(
  36. status_code=status.HTTP_401_UNAUTHORIZED,
  37. detail=ERROR_MESSAGES.UNAUTHORIZED,
  38. )
  39. prompt = Prompts.get_prompt_by_command(form_data.command)
  40. if prompt is None:
  41. prompt = Prompts.insert_new_prompt(user.id, form_data)
  42. if prompt:
  43. return prompt
  44. raise HTTPException(
  45. status_code=status.HTTP_400_BAD_REQUEST,
  46. detail=ERROR_MESSAGES.DEFAULT(),
  47. )
  48. raise HTTPException(
  49. status_code=status.HTTP_400_BAD_REQUEST,
  50. detail=ERROR_MESSAGES.COMMAND_TAKEN,
  51. )
  52. ############################
  53. # GetPromptByCommand
  54. ############################
  55. @router.get("/command/{command}", response_model=Optional[PromptModel])
  56. async def get_prompt_by_command(command: str, user=Depends(get_verified_user)):
  57. prompt = Prompts.get_prompt_by_command(f"/{command}")
  58. if prompt:
  59. if (
  60. user.role == "admin"
  61. or prompt.user_id == user.id
  62. or has_access(user.id, "read", prompt.access_control)
  63. ):
  64. return prompt
  65. else:
  66. raise HTTPException(
  67. status_code=status.HTTP_401_UNAUTHORIZED,
  68. detail=ERROR_MESSAGES.NOT_FOUND,
  69. )
  70. ############################
  71. # UpdatePromptByCommand
  72. ############################
  73. @router.post("/command/{command}/update", response_model=Optional[PromptModel])
  74. async def update_prompt_by_command(
  75. command: str,
  76. form_data: PromptForm,
  77. user=Depends(get_verified_user),
  78. ):
  79. prompt = Prompts.get_prompt_by_command(f"/{command}")
  80. if not prompt:
  81. raise HTTPException(
  82. status_code=status.HTTP_401_UNAUTHORIZED,
  83. detail=ERROR_MESSAGES.NOT_FOUND,
  84. )
  85. if prompt.user_id != user.id and user.role != "admin":
  86. raise HTTPException(
  87. status_code=status.HTTP_401_UNAUTHORIZED,
  88. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  89. )
  90. prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
  91. if prompt:
  92. return prompt
  93. else:
  94. raise HTTPException(
  95. status_code=status.HTTP_401_UNAUTHORIZED,
  96. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  97. )
  98. ############################
  99. # DeletePromptByCommand
  100. ############################
  101. @router.delete("/command/{command}/delete", response_model=bool)
  102. async def delete_prompt_by_command(command: str, user=Depends(get_verified_user)):
  103. prompt = Prompts.get_prompt_by_command(f"/{command}")
  104. if not prompt:
  105. raise HTTPException(
  106. status_code=status.HTTP_401_UNAUTHORIZED,
  107. detail=ERROR_MESSAGES.NOT_FOUND,
  108. )
  109. if prompt.user_id != user.id and user.role != "admin":
  110. raise HTTPException(
  111. status_code=status.HTTP_401_UNAUTHORIZED,
  112. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  113. )
  114. result = Prompts.delete_prompt_by_command(f"/{command}")
  115. return result