tools.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import logging
  2. import time
  3. from typing import Optional
  4. from open_webui.apps.webui.internal.db import Base, JSONField, get_db
  5. from open_webui.apps.webui.models.users import Users
  6. from open_webui.env import SRC_LOG_LEVELS
  7. from pydantic import BaseModel, ConfigDict
  8. from sqlalchemy import BigInteger, Column, String, Text, JSON
  9. from open_webui.utils.access_control import has_access
  10. log = logging.getLogger(__name__)
  11. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  12. ####################
  13. # Tools DB Schema
  14. ####################
  15. class Tool(Base):
  16. __tablename__ = "tool"
  17. id = Column(String, primary_key=True)
  18. user_id = Column(String)
  19. name = Column(Text)
  20. content = Column(Text)
  21. specs = Column(JSONField)
  22. meta = Column(JSONField)
  23. valves = Column(JSONField)
  24. access_control = Column(JSON, nullable=True) # Controls data access levels.
  25. # Defines access control rules for this entry.
  26. # - `None`: Public access, available to all users with the "user" role.
  27. # - `{}`: Private access, restricted exclusively to the owner.
  28. # - Custom permissions: Specific access control for reading and writing;
  29. # Can specify group or user-level restrictions:
  30. # {
  31. # "read": {
  32. # "group_ids": ["group_id1", "group_id2"],
  33. # "user_ids": ["user_id1", "user_id2"]
  34. # },
  35. # "write": {
  36. # "group_ids": ["group_id1", "group_id2"],
  37. # "user_ids": ["user_id1", "user_id2"]
  38. # }
  39. # }
  40. updated_at = Column(BigInteger)
  41. created_at = Column(BigInteger)
  42. class ToolMeta(BaseModel):
  43. description: Optional[str] = None
  44. manifest: Optional[dict] = {}
  45. class ToolModel(BaseModel):
  46. id: str
  47. user_id: str
  48. name: str
  49. content: str
  50. specs: list[dict]
  51. meta: ToolMeta
  52. access_control: Optional[dict] = None
  53. updated_at: int # timestamp in epoch
  54. created_at: int # timestamp in epoch
  55. model_config = ConfigDict(from_attributes=True)
  56. ####################
  57. # Forms
  58. ####################
  59. class ToolResponse(BaseModel):
  60. id: str
  61. user_id: str
  62. name: str
  63. meta: ToolMeta
  64. updated_at: int # timestamp in epoch
  65. created_at: int # timestamp in epoch
  66. class ToolForm(BaseModel):
  67. id: str
  68. name: str
  69. content: str
  70. meta: ToolMeta
  71. class ToolValves(BaseModel):
  72. valves: Optional[dict] = None
  73. class ToolsTable:
  74. def insert_new_tool(
  75. self, user_id: str, form_data: ToolForm, specs: list[dict]
  76. ) -> Optional[ToolModel]:
  77. with get_db() as db:
  78. tool = ToolModel(
  79. **{
  80. **form_data.model_dump(),
  81. "specs": specs,
  82. "user_id": user_id,
  83. "updated_at": int(time.time()),
  84. "created_at": int(time.time()),
  85. }
  86. )
  87. try:
  88. result = Tool(**tool.model_dump())
  89. db.add(result)
  90. db.commit()
  91. db.refresh(result)
  92. if result:
  93. return ToolModel.model_validate(result)
  94. else:
  95. return None
  96. except Exception as e:
  97. print(f"Error creating tool: {e}")
  98. return None
  99. def get_tool_by_id(self, id: str) -> Optional[ToolModel]:
  100. try:
  101. with get_db() as db:
  102. tool = db.get(Tool, id)
  103. return ToolModel.model_validate(tool)
  104. except Exception:
  105. return None
  106. def get_tools(self) -> list[ToolModel]:
  107. with get_db() as db:
  108. return [ToolModel.model_validate(tool) for tool in db.query(Tool).all()]
  109. def get_tools_by_user_id(
  110. self, user_id: str, permission: str = "write"
  111. ) -> list[ToolModel]:
  112. tools = self.get_tools()
  113. return [
  114. tool
  115. for tool in tools
  116. if tool.user_id == user_id
  117. or has_access(user_id, permission, tool.access_control)
  118. ]
  119. def get_tool_valves_by_id(self, id: str) -> Optional[dict]:
  120. try:
  121. with get_db() as db:
  122. tool = db.get(Tool, id)
  123. return tool.valves if tool.valves else {}
  124. except Exception as e:
  125. print(f"An error occurred: {e}")
  126. return None
  127. def update_tool_valves_by_id(self, id: str, valves: dict) -> Optional[ToolValves]:
  128. try:
  129. with get_db() as db:
  130. db.query(Tool).filter_by(id=id).update(
  131. {"valves": valves, "updated_at": int(time.time())}
  132. )
  133. db.commit()
  134. return self.get_tool_by_id(id)
  135. except Exception:
  136. return None
  137. def get_user_valves_by_id_and_user_id(
  138. self, id: str, user_id: str
  139. ) -> Optional[dict]:
  140. try:
  141. user = Users.get_user_by_id(user_id)
  142. user_settings = user.settings.model_dump() if user.settings else {}
  143. # Check if user has "tools" and "valves" settings
  144. if "tools" not in user_settings:
  145. user_settings["tools"] = {}
  146. if "valves" not in user_settings["tools"]:
  147. user_settings["tools"]["valves"] = {}
  148. return user_settings["tools"]["valves"].get(id, {})
  149. except Exception as e:
  150. print(f"An error occurred: {e}")
  151. return None
  152. def update_user_valves_by_id_and_user_id(
  153. self, id: str, user_id: str, valves: dict
  154. ) -> Optional[dict]:
  155. try:
  156. user = Users.get_user_by_id(user_id)
  157. user_settings = user.settings.model_dump() if user.settings else {}
  158. # Check if user has "tools" and "valves" settings
  159. if "tools" not in user_settings:
  160. user_settings["tools"] = {}
  161. if "valves" not in user_settings["tools"]:
  162. user_settings["tools"]["valves"] = {}
  163. user_settings["tools"]["valves"][id] = valves
  164. # Update the user settings in the database
  165. Users.update_user_by_id(user_id, {"settings": user_settings})
  166. return user_settings["tools"]["valves"][id]
  167. except Exception as e:
  168. print(f"An error occurred: {e}")
  169. return None
  170. def update_tool_by_id(self, id: str, updated: dict) -> Optional[ToolModel]:
  171. try:
  172. with get_db() as db:
  173. db.query(Tool).filter_by(id=id).update(
  174. {**updated, "updated_at": int(time.time())}
  175. )
  176. db.commit()
  177. tool = db.query(Tool).get(id)
  178. db.refresh(tool)
  179. return ToolModel.model_validate(tool)
  180. except Exception:
  181. return None
  182. def delete_tool_by_id(self, id: str) -> bool:
  183. try:
  184. with get_db() as db:
  185. db.query(Tool).filter_by(id=id).delete()
  186. db.commit()
  187. return True
  188. except Exception:
  189. return False
  190. Tools = ToolsTable()