tools.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. import logging
  2. from pathlib import Path
  3. from typing import Optional
  4. from open_webui.models.tools import (
  5. ToolForm,
  6. ToolModel,
  7. ToolResponse,
  8. ToolUserResponse,
  9. Tools,
  10. )
  11. from open_webui.utils.plugin import load_tools_module_by_id, replace_imports
  12. from open_webui.config import CACHE_DIR
  13. from open_webui.constants import ERROR_MESSAGES
  14. from fastapi import APIRouter, Depends, HTTPException, Request, status
  15. from open_webui.utils.tools import get_tools_specs
  16. from open_webui.utils.auth import get_admin_user, get_verified_user
  17. from open_webui.utils.access_control import has_access, has_permission
  18. from open_webui.env import SRC_LOG_LEVELS
  19. log = logging.getLogger(__name__)
  20. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  21. router = APIRouter()
  22. ############################
  23. # GetTools
  24. ############################
  25. @router.get("/", response_model=list[ToolUserResponse])
  26. async def get_tools(user=Depends(get_verified_user)):
  27. if user.role == "admin":
  28. tools = Tools.get_tools()
  29. else:
  30. tools = Tools.get_tools_by_user_id(user.id, "read")
  31. return tools
  32. ############################
  33. # GetToolList
  34. ############################
  35. @router.get("/list", response_model=list[ToolUserResponse])
  36. async def get_tool_list(user=Depends(get_verified_user)):
  37. if user.role == "admin":
  38. tools = Tools.get_tools()
  39. else:
  40. tools = Tools.get_tools_by_user_id(user.id, "write")
  41. return tools
  42. ############################
  43. # ExportTools
  44. ############################
  45. @router.get("/export", response_model=list[ToolModel])
  46. async def export_tools(user=Depends(get_admin_user)):
  47. tools = Tools.get_tools()
  48. return tools
  49. ############################
  50. # CreateNewTools
  51. ############################
  52. @router.post("/create", response_model=Optional[ToolResponse])
  53. async def create_new_tools(
  54. request: Request,
  55. form_data: ToolForm,
  56. user=Depends(get_verified_user),
  57. ):
  58. if user.role != "admin" and not has_permission(
  59. user.id, "workspace.tools", request.app.state.config.USER_PERMISSIONS
  60. ):
  61. raise HTTPException(
  62. status_code=status.HTTP_401_UNAUTHORIZED,
  63. detail=ERROR_MESSAGES.UNAUTHORIZED,
  64. )
  65. if not form_data.id.isidentifier():
  66. raise HTTPException(
  67. status_code=status.HTTP_400_BAD_REQUEST,
  68. detail="Only alphanumeric characters and underscores are allowed in the id",
  69. )
  70. form_data.id = form_data.id.lower()
  71. tools = Tools.get_tool_by_id(form_data.id)
  72. if tools is None:
  73. try:
  74. form_data.content = replace_imports(form_data.content)
  75. tools_module, frontmatter = load_tools_module_by_id(
  76. form_data.id, content=form_data.content
  77. )
  78. form_data.meta.manifest = frontmatter
  79. TOOLS = request.app.state.TOOLS
  80. TOOLS[form_data.id] = tools_module
  81. specs = get_tools_specs(TOOLS[form_data.id])
  82. tools = Tools.insert_new_tool(user.id, form_data, specs)
  83. tool_cache_dir = CACHE_DIR / "tools" / form_data.id
  84. tool_cache_dir.mkdir(parents=True, exist_ok=True)
  85. if tools:
  86. return tools
  87. else:
  88. raise HTTPException(
  89. status_code=status.HTTP_400_BAD_REQUEST,
  90. detail=ERROR_MESSAGES.DEFAULT("Error creating tools"),
  91. )
  92. except Exception as e:
  93. log.exception(f"Failed to load the tool by id {form_data.id}: {e}")
  94. raise HTTPException(
  95. status_code=status.HTTP_400_BAD_REQUEST,
  96. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  97. )
  98. else:
  99. raise HTTPException(
  100. status_code=status.HTTP_400_BAD_REQUEST,
  101. detail=ERROR_MESSAGES.ID_TAKEN,
  102. )
  103. ############################
  104. # GetToolsById
  105. ############################
  106. @router.get("/id/{id}", response_model=Optional[ToolModel])
  107. async def get_tools_by_id(id: str, user=Depends(get_verified_user)):
  108. tools = Tools.get_tool_by_id(id)
  109. if tools:
  110. if (
  111. user.role == "admin"
  112. or tools.user_id == user.id
  113. or has_access(user.id, "read", tools.access_control)
  114. ):
  115. return tools
  116. else:
  117. raise HTTPException(
  118. status_code=status.HTTP_401_UNAUTHORIZED,
  119. detail=ERROR_MESSAGES.NOT_FOUND,
  120. )
  121. ############################
  122. # UpdateToolsById
  123. ############################
  124. @router.post("/id/{id}/update", response_model=Optional[ToolModel])
  125. async def update_tools_by_id(
  126. request: Request,
  127. id: str,
  128. form_data: ToolForm,
  129. user=Depends(get_verified_user),
  130. ):
  131. tools = Tools.get_tool_by_id(id)
  132. if not tools:
  133. raise HTTPException(
  134. status_code=status.HTTP_401_UNAUTHORIZED,
  135. detail=ERROR_MESSAGES.NOT_FOUND,
  136. )
  137. # Is the user the original creator, in a group with write access, or an admin
  138. if (
  139. tools.user_id != user.id
  140. and not has_access(user.id, "write", tools.access_control)
  141. and user.role != "admin"
  142. ):
  143. raise HTTPException(
  144. status_code=status.HTTP_401_UNAUTHORIZED,
  145. detail=ERROR_MESSAGES.UNAUTHORIZED,
  146. )
  147. try:
  148. form_data.content = replace_imports(form_data.content)
  149. tools_module, frontmatter = load_tools_module_by_id(
  150. id, content=form_data.content
  151. )
  152. form_data.meta.manifest = frontmatter
  153. TOOLS = request.app.state.TOOLS
  154. TOOLS[id] = tools_module
  155. specs = get_tools_specs(TOOLS[id])
  156. updated = {
  157. **form_data.model_dump(exclude={"id"}),
  158. "specs": specs,
  159. }
  160. log.debug(updated)
  161. tools = Tools.update_tool_by_id(id, updated)
  162. if tools:
  163. return tools
  164. else:
  165. raise HTTPException(
  166. status_code=status.HTTP_400_BAD_REQUEST,
  167. detail=ERROR_MESSAGES.DEFAULT("Error updating tools"),
  168. )
  169. except Exception as e:
  170. raise HTTPException(
  171. status_code=status.HTTP_400_BAD_REQUEST,
  172. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  173. )
  174. ############################
  175. # DeleteToolsById
  176. ############################
  177. @router.delete("/id/{id}/delete", response_model=bool)
  178. async def delete_tools_by_id(
  179. request: Request, id: str, user=Depends(get_verified_user)
  180. ):
  181. tools = Tools.get_tool_by_id(id)
  182. if not tools:
  183. raise HTTPException(
  184. status_code=status.HTTP_401_UNAUTHORIZED,
  185. detail=ERROR_MESSAGES.NOT_FOUND,
  186. )
  187. if (
  188. tools.user_id != user.id
  189. and not has_access(user.id, "write", tools.access_control)
  190. and user.role != "admin"
  191. ):
  192. raise HTTPException(
  193. status_code=status.HTTP_401_UNAUTHORIZED,
  194. detail=ERROR_MESSAGES.UNAUTHORIZED,
  195. )
  196. result = Tools.delete_tool_by_id(id)
  197. if result:
  198. TOOLS = request.app.state.TOOLS
  199. if id in TOOLS:
  200. del TOOLS[id]
  201. return result
  202. ############################
  203. # GetToolValves
  204. ############################
  205. @router.get("/id/{id}/valves", response_model=Optional[dict])
  206. async def get_tools_valves_by_id(id: str, user=Depends(get_verified_user)):
  207. tools = Tools.get_tool_by_id(id)
  208. if tools:
  209. try:
  210. valves = Tools.get_tool_valves_by_id(id)
  211. return valves
  212. except Exception as e:
  213. raise HTTPException(
  214. status_code=status.HTTP_400_BAD_REQUEST,
  215. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  216. )
  217. else:
  218. raise HTTPException(
  219. status_code=status.HTTP_401_UNAUTHORIZED,
  220. detail=ERROR_MESSAGES.NOT_FOUND,
  221. )
  222. ############################
  223. # GetToolValvesSpec
  224. ############################
  225. @router.get("/id/{id}/valves/spec", response_model=Optional[dict])
  226. async def get_tools_valves_spec_by_id(
  227. request: Request, id: str, user=Depends(get_verified_user)
  228. ):
  229. tools = Tools.get_tool_by_id(id)
  230. if tools:
  231. if id in request.app.state.TOOLS:
  232. tools_module = request.app.state.TOOLS[id]
  233. else:
  234. tools_module, _ = load_tools_module_by_id(id)
  235. request.app.state.TOOLS[id] = tools_module
  236. if hasattr(tools_module, "Valves"):
  237. Valves = tools_module.Valves
  238. return Valves.schema()
  239. return None
  240. else:
  241. raise HTTPException(
  242. status_code=status.HTTP_401_UNAUTHORIZED,
  243. detail=ERROR_MESSAGES.NOT_FOUND,
  244. )
  245. ############################
  246. # UpdateToolValves
  247. ############################
  248. @router.post("/id/{id}/valves/update", response_model=Optional[dict])
  249. async def update_tools_valves_by_id(
  250. request: Request, id: str, form_data: dict, user=Depends(get_verified_user)
  251. ):
  252. tools = Tools.get_tool_by_id(id)
  253. if not tools:
  254. raise HTTPException(
  255. status_code=status.HTTP_401_UNAUTHORIZED,
  256. detail=ERROR_MESSAGES.NOT_FOUND,
  257. )
  258. if (
  259. tools.user_id != user.id
  260. and not has_access(user.id, "write", tools.access_control)
  261. and user.role != "admin"
  262. ):
  263. raise HTTPException(
  264. status_code=status.HTTP_400_BAD_REQUEST,
  265. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  266. )
  267. if id in request.app.state.TOOLS:
  268. tools_module = request.app.state.TOOLS[id]
  269. else:
  270. tools_module, _ = load_tools_module_by_id(id)
  271. request.app.state.TOOLS[id] = tools_module
  272. if not hasattr(tools_module, "Valves"):
  273. raise HTTPException(
  274. status_code=status.HTTP_401_UNAUTHORIZED,
  275. detail=ERROR_MESSAGES.NOT_FOUND,
  276. )
  277. Valves = tools_module.Valves
  278. try:
  279. form_data = {k: v for k, v in form_data.items() if v is not None}
  280. valves = Valves(**form_data)
  281. Tools.update_tool_valves_by_id(id, valves.model_dump())
  282. return valves.model_dump()
  283. except Exception as e:
  284. log.exception(f"Failed to update tool valves by id {id}: {e}")
  285. raise HTTPException(
  286. status_code=status.HTTP_400_BAD_REQUEST,
  287. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  288. )
  289. ############################
  290. # ToolUserValves
  291. ############################
  292. @router.get("/id/{id}/valves/user", response_model=Optional[dict])
  293. async def get_tools_user_valves_by_id(id: str, user=Depends(get_verified_user)):
  294. tools = Tools.get_tool_by_id(id)
  295. if tools:
  296. try:
  297. user_valves = Tools.get_user_valves_by_id_and_user_id(id, user.id)
  298. return user_valves
  299. except Exception as e:
  300. raise HTTPException(
  301. status_code=status.HTTP_400_BAD_REQUEST,
  302. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  303. )
  304. else:
  305. raise HTTPException(
  306. status_code=status.HTTP_401_UNAUTHORIZED,
  307. detail=ERROR_MESSAGES.NOT_FOUND,
  308. )
  309. @router.get("/id/{id}/valves/user/spec", response_model=Optional[dict])
  310. async def get_tools_user_valves_spec_by_id(
  311. request: Request, id: str, user=Depends(get_verified_user)
  312. ):
  313. tools = Tools.get_tool_by_id(id)
  314. if tools:
  315. if id in request.app.state.TOOLS:
  316. tools_module = request.app.state.TOOLS[id]
  317. else:
  318. tools_module, _ = load_tools_module_by_id(id)
  319. request.app.state.TOOLS[id] = tools_module
  320. if hasattr(tools_module, "UserValves"):
  321. UserValves = tools_module.UserValves
  322. return UserValves.schema()
  323. return None
  324. else:
  325. raise HTTPException(
  326. status_code=status.HTTP_401_UNAUTHORIZED,
  327. detail=ERROR_MESSAGES.NOT_FOUND,
  328. )
  329. @router.post("/id/{id}/valves/user/update", response_model=Optional[dict])
  330. async def update_tools_user_valves_by_id(
  331. request: Request, id: str, form_data: dict, user=Depends(get_verified_user)
  332. ):
  333. tools = Tools.get_tool_by_id(id)
  334. if tools:
  335. if id in request.app.state.TOOLS:
  336. tools_module = request.app.state.TOOLS[id]
  337. else:
  338. tools_module, _ = load_tools_module_by_id(id)
  339. request.app.state.TOOLS[id] = tools_module
  340. if hasattr(tools_module, "UserValves"):
  341. UserValves = tools_module.UserValves
  342. try:
  343. form_data = {k: v for k, v in form_data.items() if v is not None}
  344. user_valves = UserValves(**form_data)
  345. Tools.update_user_valves_by_id_and_user_id(
  346. id, user.id, user_valves.model_dump()
  347. )
  348. return user_valves.model_dump()
  349. except Exception as e:
  350. log.exception(f"Failed to update user valves by id {id}: {e}")
  351. raise HTTPException(
  352. status_code=status.HTTP_400_BAD_REQUEST,
  353. detail=ERROR_MESSAGES.DEFAULT(str(e)),
  354. )
  355. else:
  356. raise HTTPException(
  357. status_code=status.HTTP_401_UNAUTHORIZED,
  358. detail=ERROR_MESSAGES.NOT_FOUND,
  359. )
  360. else:
  361. raise HTTPException(
  362. status_code=status.HTTP_401_UNAUTHORIZED,
  363. detail=ERROR_MESSAGES.NOT_FOUND,
  364. )