prompts.py 4.6 KB

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