Bladeren bron

enh/refac: function sync endpoint

Timothy Jaeryang Baek 2 maanden geleden
bovenliggende
commit
df7b5ec907
1 gewijzigde bestanden met toevoegingen van 17 en 2 verwijderingen
  1. 17 2
      backend/open_webui/routers/functions.py

+ 17 - 2
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)