From df7b5ec907ec4cb285956ae9437e635ad8deca56 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 24 Jul 2025 23:44:43 +0400 Subject: [PATCH] enh/refac: function sync endpoint --- backend/open_webui/routers/functions.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/routers/functions.py b/backend/open_webui/routers/functions.py index 96d8215fb3..73b608cd35 100644 --- a/backend/open_webui/routers/functions.py +++ b/backend/open_webui/routers/functions.py @@ -131,14 +131,29 @@ async def load_function_from_url( ############################ -class SyncFunctionsForm(FunctionForm): +class SyncFunctionsForm(BaseModel): functions: list[FunctionModel] = [] -@router.post("/sync", response_model=Optional[FunctionModel]) +@router.post("/sync", response_model=list[FunctionModel]) async def sync_functions( request: Request, form_data: SyncFunctionsForm, user=Depends(get_admin_user) ): + try: + for function in form_data.functions: + function.content = replace_imports(function.content) + function_module, function_type, frontmatter = load_function_module_by_id( + function.id, + content=function.content, + ) + except Exception as e: + log.exception(f"Failed to load a function: {e}") + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.DEFAULT(e), + ) + + return Functions.sync_functions(user.id, form_data.functions)