refac
This commit is contained in:
		
							parent
							
								
									aa7d25600f
								
							
						
					
					
						commit
						ff1cd306d8
					
				|  | @ -39,6 +39,7 @@ app.state.config = AppConfig() | ||||||
| 
 | 
 | ||||||
| app.state.config.ENABLE_SIGNUP = ENABLE_SIGNUP | app.state.config.ENABLE_SIGNUP = ENABLE_SIGNUP | ||||||
| app.state.config.JWT_EXPIRES_IN = JWT_EXPIRES_IN | app.state.config.JWT_EXPIRES_IN = JWT_EXPIRES_IN | ||||||
|  | app.state.AUTH_TRUSTED_EMAIL_HEADER = WEBUI_AUTH_TRUSTED_EMAIL_HEADER | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| app.state.config.SHOW_ADMIN_DETAILS = SHOW_ADMIN_DETAILS | app.state.config.SHOW_ADMIN_DETAILS = SHOW_ADMIN_DETAILS | ||||||
|  | @ -55,7 +56,7 @@ app.state.config.BANNERS = WEBUI_BANNERS | ||||||
| app.state.config.ENABLE_COMMUNITY_SHARING = ENABLE_COMMUNITY_SHARING | app.state.config.ENABLE_COMMUNITY_SHARING = ENABLE_COMMUNITY_SHARING | ||||||
| 
 | 
 | ||||||
| app.state.MODELS = {} | app.state.MODELS = {} | ||||||
| app.state.AUTH_TRUSTED_EMAIL_HEADER = WEBUI_AUTH_TRUSTED_EMAIL_HEADER | app.state.TOOLS = {} | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| app.add_middleware( | app.add_middleware( | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| from fastapi import Depends, FastAPI, HTTPException, status | from fastapi import Depends, FastAPI, HTTPException, status, Request | ||||||
| from datetime import datetime, timedelta | from datetime import datetime, timedelta | ||||||
| from typing import List, Union, Optional | from typing import List, Union, Optional | ||||||
| 
 | 
 | ||||||
|  | @ -20,8 +20,6 @@ from config import DATA_DIR | ||||||
| TOOLS_DIR = f"{DATA_DIR}/tools" | TOOLS_DIR = f"{DATA_DIR}/tools" | ||||||
| os.makedirs(TOOLS_DIR, exist_ok=True) | os.makedirs(TOOLS_DIR, exist_ok=True) | ||||||
| 
 | 
 | ||||||
| TOOLS = {} |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| router = APIRouter() | router = APIRouter() | ||||||
| 
 | 
 | ||||||
|  | @ -73,7 +71,9 @@ async def get_toolkits(user=Depends(get_admin_user)): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @router.post("/create", response_model=Optional[ToolResponse]) | @router.post("/create", response_model=Optional[ToolResponse]) | ||||||
| async def create_new_toolkit(form_data: ToolForm, user=Depends(get_admin_user)): | async def create_new_toolkit( | ||||||
|  |     request: Request, form_data: ToolForm, user=Depends(get_admin_user) | ||||||
|  | ): | ||||||
|     if not form_data.id.isidentifier(): |     if not form_data.id.isidentifier(): | ||||||
|         raise HTTPException( |         raise HTTPException( | ||||||
|             status_code=status.HTTP_400_BAD_REQUEST, |             status_code=status.HTTP_400_BAD_REQUEST, | ||||||
|  | @ -90,6 +90,8 @@ async def create_new_toolkit(form_data: ToolForm, user=Depends(get_admin_user)): | ||||||
|                 tool_file.write(form_data.content) |                 tool_file.write(form_data.content) | ||||||
| 
 | 
 | ||||||
|             toolkit_module = load_toolkit_module_from_path(form_data.id, toolkit_path) |             toolkit_module = load_toolkit_module_from_path(form_data.id, toolkit_path) | ||||||
|  | 
 | ||||||
|  |             TOOLS = request.app.state.TOOLS | ||||||
|             TOOLS[form_data.id] = toolkit_module |             TOOLS[form_data.id] = toolkit_module | ||||||
| 
 | 
 | ||||||
|             specs = get_tools_specs(TOOLS[form_data.id]) |             specs = get_tools_specs(TOOLS[form_data.id]) | ||||||
|  | @ -139,7 +141,7 @@ async def get_toolkit_by_id(id: str, user=Depends(get_admin_user)): | ||||||
| 
 | 
 | ||||||
| @router.post("/id/{id}/update", response_model=Optional[ToolModel]) | @router.post("/id/{id}/update", response_model=Optional[ToolModel]) | ||||||
| async def update_toolkit_by_id( | async def update_toolkit_by_id( | ||||||
|     id: str, form_data: ToolForm, user=Depends(get_admin_user) |     request: Request, id: str, form_data: ToolForm, user=Depends(get_admin_user) | ||||||
| ): | ): | ||||||
|     toolkit_path = os.path.join(TOOLS_DIR, f"{id}.py") |     toolkit_path = os.path.join(TOOLS_DIR, f"{id}.py") | ||||||
| 
 | 
 | ||||||
|  | @ -148,6 +150,8 @@ async def update_toolkit_by_id( | ||||||
|             tool_file.write(form_data.content) |             tool_file.write(form_data.content) | ||||||
| 
 | 
 | ||||||
|         toolkit_module = load_toolkit_module_from_path(id, toolkit_path) |         toolkit_module = load_toolkit_module_from_path(id, toolkit_path) | ||||||
|  | 
 | ||||||
|  |         TOOLS = request.app.state.TOOLS | ||||||
|         TOOLS[id] = toolkit_module |         TOOLS[id] = toolkit_module | ||||||
| 
 | 
 | ||||||
|         specs = get_tools_specs(TOOLS[id]) |         specs = get_tools_specs(TOOLS[id]) | ||||||
|  | @ -181,6 +185,11 @@ async def update_toolkit_by_id( | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @router.delete("/id/{id}/delete", response_model=bool) | @router.delete("/id/{id}/delete", response_model=bool) | ||||||
| async def delete_toolkit_by_id(id: str, user=Depends(get_admin_user)): | async def delete_toolkit_by_id(request: Request, id: str, user=Depends(get_admin_user)): | ||||||
|     result = Tools.delete_tool_by_id(id) |     result = Tools.delete_tool_by_id(id) | ||||||
|  | 
 | ||||||
|  |     if result: | ||||||
|  |         TOOLS = request.app.state.TOOLS | ||||||
|  |         del TOOLS[id] | ||||||
|  | 
 | ||||||
|     return result |     return result | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue