prompts.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import time
  2. from typing import Optional
  3. from open_webui.apps.webui.internal.db import Base, get_db
  4. from open_webui.apps.webui.models.groups import Groups
  5. from pydantic import BaseModel, ConfigDict
  6. from sqlalchemy import BigInteger, Column, String, Text, JSON
  7. from open_webui.utils.access_control import has_access
  8. ####################
  9. # Prompts DB Schema
  10. ####################
  11. class Prompt(Base):
  12. __tablename__ = "prompt"
  13. command = Column(String, primary_key=True)
  14. user_id = Column(String)
  15. title = Column(Text)
  16. content = Column(Text)
  17. timestamp = Column(BigInteger)
  18. access_control = Column(JSON, nullable=True) # Controls data access levels.
  19. # Defines access control rules for this entry.
  20. # - `None`: Public access, available to all users with the "user" role.
  21. # - `{}`: Private access, restricted exclusively to the owner.
  22. # - Custom permissions: Specific access control for reading and writing;
  23. # Can specify group or user-level restrictions:
  24. # {
  25. # "read": {
  26. # "group_ids": ["group_id1", "group_id2"],
  27. # "user_ids": ["user_id1", "user_id2"]
  28. # },
  29. # "write": {
  30. # "group_ids": ["group_id1", "group_id2"],
  31. # "user_ids": ["user_id1", "user_id2"]
  32. # }
  33. # }
  34. class PromptModel(BaseModel):
  35. command: str
  36. user_id: str
  37. title: str
  38. content: str
  39. timestamp: int # timestamp in epoch
  40. access_control: Optional[dict] = None
  41. model_config = ConfigDict(from_attributes=True)
  42. ####################
  43. # Forms
  44. ####################
  45. class PromptForm(BaseModel):
  46. command: str
  47. title: str
  48. content: str
  49. access_control: Optional[dict] = None
  50. class PromptsTable:
  51. def insert_new_prompt(
  52. self, user_id: str, form_data: PromptForm
  53. ) -> Optional[PromptModel]:
  54. prompt = PromptModel(
  55. **{
  56. "user_id": user_id,
  57. **form_data.model_dump(),
  58. "timestamp": int(time.time()),
  59. }
  60. )
  61. try:
  62. with get_db() as db:
  63. result = Prompt(**prompt.model_dump())
  64. db.add(result)
  65. db.commit()
  66. db.refresh(result)
  67. if result:
  68. return PromptModel.model_validate(result)
  69. else:
  70. return None
  71. except Exception:
  72. return None
  73. def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
  74. try:
  75. with get_db() as db:
  76. prompt = db.query(Prompt).filter_by(command=command).first()
  77. return PromptModel.model_validate(prompt)
  78. except Exception:
  79. return None
  80. def get_prompts(self) -> list[PromptModel]:
  81. with get_db() as db:
  82. return [
  83. PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
  84. ]
  85. def get_prompts_by_user_id(
  86. self, user_id: str, permission: str = "write"
  87. ) -> list[PromptModel]:
  88. prompts = self.get_prompts()
  89. return [
  90. prompt
  91. for prompt in prompts
  92. if prompt.user_id == user_id
  93. or has_access(user_id, permission, prompt.access_control)
  94. ]
  95. def update_prompt_by_command(
  96. self, command: str, form_data: PromptForm
  97. ) -> Optional[PromptModel]:
  98. try:
  99. with get_db() as db:
  100. prompt = db.query(Prompt).filter_by(command=command).first()
  101. prompt.title = form_data.title
  102. prompt.content = form_data.content
  103. prompt.access_control = form_data.access_control
  104. prompt.timestamp = int(time.time())
  105. db.commit()
  106. return PromptModel.model_validate(prompt)
  107. except Exception:
  108. return None
  109. def delete_prompt_by_command(self, command: str) -> bool:
  110. try:
  111. with get_db() as db:
  112. db.query(Prompt).filter_by(command=command).delete()
  113. db.commit()
  114. return True
  115. except Exception:
  116. return False
  117. Prompts = PromptsTable()