open-webui/backend/open_webui/apps/webui/routers/tools.py

419 lines
12 KiB
Python
Raw Normal View History

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