| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | from pathlib import Path | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  | from typing import Optional | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | from open_webui.models.tools import ( | 
					
						
							| 
									
										
										
										
											2024-11-18 22:19:34 +08:00
										 |  |  |     ToolForm, | 
					
						
							|  |  |  |     ToolModel, | 
					
						
							|  |  |  |     ToolResponse, | 
					
						
							|  |  |  |     ToolUserResponse, | 
					
						
							|  |  |  |     Tools, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-12-12 10:36:59 +08:00
										 |  |  | from open_webui.utils.plugin import load_tools_module_by_id, replace_imports | 
					
						
							| 
									
										
										
										
											2024-11-22 01:19:56 +08:00
										 |  |  | from open_webui.config import CACHE_DIR | 
					
						
							| 
									
										
										
										
											2024-09-04 22:54:48 +08:00
										 |  |  | from open_webui.constants import ERROR_MESSAGES | 
					
						
							| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | from fastapi import APIRouter, Depends, HTTPException, Request, status | 
					
						
							| 
									
										
										
										
											2024-09-04 22:54:48 +08:00
										 |  |  | from open_webui.utils.tools import get_tools_specs | 
					
						
							| 
									
										
										
										
											2024-12-09 08:01:56 +08:00
										 |  |  | from open_webui.utils.auth import get_admin_user, get_verified_user | 
					
						
							| 
									
										
										
										
											2024-11-17 19:04:31 +08:00
										 |  |  | from open_webui.utils.access_control import has_access, has_permission | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | router = APIRouter() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # GetTools | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-18 22:19:34 +08:00
										 |  |  | @router.get("/", response_model=list[ToolUserResponse]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools(user=Depends(get_verified_user)): | 
					
						
							|  |  |  |     if user.role == "admin": | 
					
						
							|  |  |  |         tools = Tools.get_tools() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         tools = Tools.get_tools_by_user_id(user.id, "read") | 
					
						
							|  |  |  |     return tools | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | # GetToolList | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-18 22:19:34 +08:00
										 |  |  | @router.get("/list", response_model=list[ToolUserResponse]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tool_list(user=Depends(get_verified_user)): | 
					
						
							|  |  |  |     if user.role == "admin": | 
					
						
							|  |  |  |         tools = Tools.get_tools() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         tools = Tools.get_tools_by_user_id(user.id, "write") | 
					
						
							|  |  |  |     return tools | 
					
						
							| 
									
										
										
										
											2024-06-11 12:33:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # ExportTools | 
					
						
							| 
									
										
										
										
											2024-06-11 12:33:46 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  | @router.get("/export", response_model=list[ToolModel]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def export_tools(user=Depends(get_admin_user)): | 
					
						
							|  |  |  |     tools = Tools.get_tools() | 
					
						
							|  |  |  |     return tools | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # CreateNewTools | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.post("/create", response_model=Optional[ToolResponse]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def create_new_tools( | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  |     request: Request, | 
					
						
							|  |  |  |     form_data: ToolForm, | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  |     user=Depends(get_verified_user), | 
					
						
							| 
									
										
										
										
											2024-06-11 13:38:48 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 19:04:31 +08:00
										 |  |  |     if user.role != "admin" and not has_permission( | 
					
						
							|  |  |  |         user.id, "workspace.knowledge", request.app.state.config.USER_PERMISSIONS | 
					
						
							|  |  |  |     ): | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.UNAUTHORIZED, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-11 12:59:06 +08:00
										 |  |  |     if not form_data.id.isidentifier(): | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							|  |  |  |             detail="Only alphanumeric characters and underscores are allowed in the id", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-11 13:10:53 +08:00
										 |  |  |     form_data.id = form_data.id.lower() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(form_data.id) | 
					
						
							|  |  |  |     if tools is None: | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-09-05 01:57:41 +08:00
										 |  |  |             form_data.content = replace_imports(form_data.content) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module, frontmatter = load_tools_module_by_id( | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |                 form_data.id, content=form_data.content | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |             form_data.meta.manifest = frontmatter | 
					
						
							| 
									
										
										
										
											2024-06-11 13:38:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             TOOLS = request.app.state.TOOLS | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             TOOLS[form_data.id] = tools_module | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             specs = get_tools_specs(TOOLS[form_data.id]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools = Tools.insert_new_tool(user.id, form_data, specs) | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 09:07:51 +08:00
										 |  |  |             tool_cache_dir = Path(CACHE_DIR) / "tools" / form_data.id | 
					
						
							|  |  |  |             tool_cache_dir.mkdir(parents=True, exist_ok=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             if tools: | 
					
						
							|  |  |  |                 return tools | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 raise HTTPException( | 
					
						
							|  |  |  |                     status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |                     detail=ERROR_MESSAGES.DEFAULT("Error creating tools"), | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |                 ) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-06-12 05:30:18 +08:00
										 |  |  |             print(e) | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |             raise HTTPException( | 
					
						
							|  |  |  |                 status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-08-03 21:24:26 +08:00
										 |  |  |                 detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-06-11 12:33:46 +08:00
										 |  |  |             detail=ERROR_MESSAGES.ID_TAKEN, | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # GetToolsById | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-11 12:53:51 +08:00
										 |  |  | @router.get("/id/{id}", response_model=Optional[ToolModel]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools_by_id(id: str, user=Depends(get_verified_user)): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:57:19 +08:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             user.role == "admin" | 
					
						
							|  |  |  |             or tools.user_id == user.id | 
					
						
							|  |  |  |             or has_access(user.id, "read", tools.access_control) | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             return tools | 
					
						
							| 
									
										
										
										
											2024-06-11 11:39:55 +08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # UpdateToolsById | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.post("/id/{id}/update", response_model=Optional[ToolModel]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def update_tools_by_id( | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  |     request: Request, | 
					
						
							|  |  |  |     id: str, | 
					
						
							|  |  |  |     form_data: ToolForm, | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  |     user=Depends(get_verified_user), | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:57:19 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if not tools: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:44:26 +08:00
										 |  |  |     # Is the user the original creator, in a group with write access, or an admin | 
					
						
							| 
									
										
										
										
											2025-01-20 03:59:07 +08:00
										 |  |  |     if ( | 
					
						
							|  |  |  |         tools.user_id != user.id | 
					
						
							|  |  |  |         and not has_access(user.id, "write", tools.access_control) | 
					
						
							|  |  |  |         and user.role != "admin" | 
					
						
							|  |  |  |     ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:57:19 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.UNAUTHORIZED, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-09-05 01:57:41 +08:00
										 |  |  |         form_data.content = replace_imports(form_data.content) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         tools_module, frontmatter = load_tools_module_by_id( | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |             id, content=form_data.content | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |         form_data.meta.manifest = frontmatter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         TOOLS = request.app.state.TOOLS | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         TOOLS[id] = tools_module | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         specs = get_tools_specs(TOOLS[id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         updated = { | 
					
						
							|  |  |  |             **form_data.model_dump(exclude={"id"}), | 
					
						
							|  |  |  |             "specs": specs, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print(updated) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         tools = Tools.update_tool_by_id(id, updated) | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         if tools: | 
					
						
							|  |  |  |             return tools | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise HTTPException( | 
					
						
							|  |  |  |                 status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |                 detail=ERROR_MESSAGES.DEFAULT("Error updating tools"), | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-08-03 21:24:26 +08:00
										 |  |  |             detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ############################ | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | # DeleteToolsById | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.delete("/id/{id}/delete", response_model=bool) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def delete_tools_by_id( | 
					
						
							|  |  |  |     request: Request, id: str, user=Depends(get_verified_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:57:19 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if not tools: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:57:19 +08:00
										 |  |  |     if tools.user_id != user.id and user.role != "admin": | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.UNAUTHORIZED, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = Tools.delete_tool_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |     if result: | 
					
						
							|  |  |  |         TOOLS = request.app.state.TOOLS | 
					
						
							|  |  |  |         if id in TOOLS: | 
					
						
							|  |  |  |             del TOOLS[id] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | # GetToolValves | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/id/{id}/valves", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools_valves_by_id(id: str, user=Depends(get_verified_user)): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-06-24 10:18:13 +08:00
										 |  |  |             valves = Tools.get_tool_valves_by_id(id) | 
					
						
							|  |  |  |             return valves | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise HTTPException( | 
					
						
							|  |  |  |                 status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-08-03 21:24:26 +08:00
										 |  |  |                 detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | # GetToolValvesSpec | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/id/{id}/valves/spec", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools_valves_spec_by_id( | 
					
						
							|  |  |  |     request: Request, id: str, user=Depends(get_verified_user) | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  |         if id in request.app.state.TOOLS: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module = request.app.state.TOOLS[id] | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module, _ = load_tools_module_by_id(id) | 
					
						
							|  |  |  |             request.app.state.TOOLS[id] = tools_module | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         if hasattr(tools_module, "Valves"): | 
					
						
							|  |  |  |             Valves = tools_module.Valves | 
					
						
							| 
									
										
										
										
											2024-06-24 10:10:52 +08:00
										 |  |  |             return Valves.schema() | 
					
						
							| 
									
										
										
										
											2024-06-24 10:02:27 +08:00
										 |  |  |         return None | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | # UpdateToolValves | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.post("/id/{id}/valves/update", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def update_tools_valves_by_id( | 
					
						
							|  |  |  |     request: Request, id: str, form_data: dict, user=Depends(get_verified_user) | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-11-22 01:19:56 +08:00
										 |  |  |     if not tools: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     if id in request.app.state.TOOLS: | 
					
						
							|  |  |  |         tools_module = request.app.state.TOOLS[id] | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-11-22 01:19:56 +08:00
										 |  |  |         tools_module, _ = load_tools_module_by_id(id) | 
					
						
							|  |  |  |         request.app.state.TOOLS[id] = tools_module | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not hasattr(tools_module, "Valves"): | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-11-22 01:19:56 +08:00
										 |  |  |     Valves = tools_module.Valves | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         form_data = {k: v for k, v in form_data.items() if v is not None} | 
					
						
							|  |  |  |         valves = Valves(**form_data) | 
					
						
							|  |  |  |         Tools.update_tool_valves_by_id(id, valves.model_dump()) | 
					
						
							|  |  |  |         return valves.model_dump() | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         print(e) | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | ############################ | 
					
						
							|  |  |  | # ToolUserValves | 
					
						
							|  |  |  | ############################ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/id/{id}/valves/user", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools_user_valves_by_id(id: str, user=Depends(get_verified_user)): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         try: | 
					
						
							|  |  |  |             user_valves = Tools.get_user_valves_by_id_and_user_id(id, user.id) | 
					
						
							|  |  |  |             return user_valves | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             raise HTTPException( | 
					
						
							|  |  |  |                 status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-08-03 21:24:26 +08:00
										 |  |  |                 detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.get("/id/{id}/valves/user/spec", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def get_tools_user_valves_spec_by_id( | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |     request: Request, id: str, user=Depends(get_verified_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							|  |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         if id in request.app.state.TOOLS: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module = request.app.state.TOOLS[id] | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module, _ = load_tools_module_by_id(id) | 
					
						
							|  |  |  |             request.app.state.TOOLS[id] = tools_module | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         if hasattr(tools_module, "UserValves"): | 
					
						
							|  |  |  |             UserValves = tools_module.UserValves | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |             return UserValves.schema() | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @router.post("/id/{id}/valves/user/update", response_model=Optional[dict]) | 
					
						
							| 
									
										
										
										
											2024-11-17 09:09:15 +08:00
										 |  |  | async def update_tools_user_valves_by_id( | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |     request: Request, id: str, form_data: dict, user=Depends(get_verified_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     tools = Tools.get_tool_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |     if tools: | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         if id in request.app.state.TOOLS: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module = request.app.state.TOOLS[id] | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |             tools_module, _ = load_tools_module_by_id(id) | 
					
						
							|  |  |  |             request.app.state.TOOLS[id] = tools_module | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  |         if hasattr(tools_module, "UserValves"): | 
					
						
							|  |  |  |             UserValves = tools_module.UserValves | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2024-06-24 10:05:56 +08:00
										 |  |  |                 form_data = {k: v for k, v in form_data.items() if v is not None} | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |                 user_valves = UserValves(**form_data) | 
					
						
							|  |  |  |                 Tools.update_user_valves_by_id_and_user_id( | 
					
						
							|  |  |  |                     id, user.id, user_valves.model_dump() | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 return user_valves.model_dump() | 
					
						
							|  |  |  |             except Exception as e: | 
					
						
							|  |  |  |                 print(e) | 
					
						
							|  |  |  |                 raise HTTPException( | 
					
						
							|  |  |  |                     status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							| 
									
										
										
										
											2024-08-03 21:24:26 +08:00
										 |  |  |                     detail=ERROR_MESSAGES.DEFAULT(str(e)), | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |                 ) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise HTTPException( | 
					
						
							|  |  |  |                 status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |                 detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_401_UNAUTHORIZED, | 
					
						
							|  |  |  |             detail=ERROR_MESSAGES.NOT_FOUND, | 
					
						
							|  |  |  |         ) |