tools.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from pydantic import BaseModel, ConfigDict
  2. from typing import List, Optional
  3. import time
  4. import logging
  5. from sqlalchemy import String, Column, BigInteger, Text
  6. from apps.webui.internal.db import Base, JSONField, get_db
  7. from apps.webui.models.users import Users
  8. import json
  9. import copy
  10. from config import SRC_LOG_LEVELS
  11. log = logging.getLogger(__name__)
  12. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  13. ####################
  14. # Tools DB Schema
  15. ####################
  16. class Tool(Base):
  17. __tablename__ = "tool"
  18. id = Column(String, primary_key=True)
  19. user_id = Column(String)
  20. name = Column(Text)
  21. content = Column(Text)
  22. specs = Column(JSONField)
  23. meta = Column(JSONField)
  24. valves = Column(JSONField)
  25. updated_at = Column(BigInteger)
  26. created_at = Column(BigInteger)
  27. class ToolMeta(BaseModel):
  28. description: Optional[str] = None
  29. manifest: Optional[dict] = {}
  30. class ToolModel(BaseModel):
  31. id: str
  32. user_id: str
  33. name: str
  34. content: str
  35. specs: List[dict]
  36. meta: ToolMeta
  37. updated_at: int # timestamp in epoch
  38. created_at: int # timestamp in epoch
  39. model_config = ConfigDict(from_attributes=True)
  40. ####################
  41. # Forms
  42. ####################
  43. class ToolResponse(BaseModel):
  44. id: str
  45. user_id: str
  46. name: str
  47. meta: ToolMeta
  48. updated_at: int # timestamp in epoch
  49. created_at: int # timestamp in epoch
  50. class ToolForm(BaseModel):
  51. id: str
  52. name: str
  53. content: str
  54. meta: ToolMeta
  55. class ToolValves(BaseModel):
  56. valves: Optional[dict] = None
  57. class ToolsTable:
  58. def insert_new_tool(
  59. self, user_id: str, form_data: ToolForm, specs: List[dict]
  60. ) -> Optional[ToolModel]:
  61. with get_db() as db:
  62. tool = ToolModel(
  63. **{
  64. **form_data.model_dump(),
  65. "specs": specs,
  66. "user_id": user_id,
  67. "updated_at": int(time.time()),
  68. "created_at": int(time.time()),
  69. }
  70. )
  71. try:
  72. result = Tool(**tool.model_dump())
  73. db.add(result)
  74. db.commit()
  75. db.refresh(result)
  76. if result:
  77. return ToolModel.model_validate(result)
  78. else:
  79. return None
  80. except Exception as e:
  81. print(f"Error creating tool: {e}")
  82. return None
  83. def get_tool_by_id(self, id: str) -> Optional[ToolModel]:
  84. try:
  85. with get_db() as db:
  86. tool = db.get(Tool, id)
  87. return ToolModel.model_validate(tool)
  88. except:
  89. return None
  90. def get_tools(self) -> List[ToolModel]:
  91. return [ToolModel.model_validate(tool) for tool in db.query(Tool).all()]
  92. def get_tool_valves_by_id(self, id: str) -> Optional[dict]:
  93. try:
  94. with get_db() as db:
  95. tool = db.get(Tool, id)
  96. return tool.valves if tool.valves else {}
  97. except Exception as e:
  98. print(f"An error occurred: {e}")
  99. return None
  100. def update_tool_valves_by_id(self, id: str, valves: dict) -> Optional[ToolValves]:
  101. try:
  102. with get_db() as db:
  103. db.query(Tool).filter_by(id=id).update(
  104. {"valves": valves, "updated_at": int(time.time())}
  105. )
  106. db.commit()
  107. return self.get_tool_by_id(id)
  108. except:
  109. return None
  110. def get_user_valves_by_id_and_user_id(
  111. self, id: str, user_id: str
  112. ) -> Optional[dict]:
  113. try:
  114. user = Users.get_user_by_id(user_id)
  115. user_settings = user.settings.model_dump()
  116. # Check if user has "tools" and "valves" settings
  117. if "tools" not in user_settings:
  118. user_settings["tools"] = {}
  119. if "valves" not in user_settings["tools"]:
  120. user_settings["tools"]["valves"] = {}
  121. return user_settings["tools"]["valves"].get(id, {})
  122. except Exception as e:
  123. print(f"An error occurred: {e}")
  124. return None
  125. def update_user_valves_by_id_and_user_id(
  126. self, id: str, user_id: str, valves: dict
  127. ) -> Optional[dict]:
  128. try:
  129. user = Users.get_user_by_id(user_id)
  130. user_settings = user.settings.model_dump()
  131. # Check if user has "tools" and "valves" settings
  132. if "tools" not in user_settings:
  133. user_settings["tools"] = {}
  134. if "valves" not in user_settings["tools"]:
  135. user_settings["tools"]["valves"] = {}
  136. user_settings["tools"]["valves"][id] = valves
  137. # Update the user settings in the database
  138. Users.update_user_by_id(user_id, {"settings": user_settings})
  139. return user_settings["tools"]["valves"][id]
  140. except Exception as e:
  141. print(f"An error occurred: {e}")
  142. return None
  143. def update_tool_by_id(self, id: str, updated: dict) -> Optional[ToolModel]:
  144. try:
  145. with get_db() as db:
  146. tool = db.get(Tool, id)
  147. tool.update(**updated)
  148. tool.updated_at = int(time.time())
  149. db.commit()
  150. db.refresh(tool)
  151. return ToolModel.model_validate(tool)
  152. except:
  153. return None
  154. def delete_tool_by_id(self, id: str) -> bool:
  155. try:
  156. with get_db() as db:
  157. db.query(Tool).filter_by(id=id).delete()
  158. return True
  159. except:
  160. return False
  161. Tools = ToolsTable()