enh/refac: function sync endpoint

This commit is contained in:
Timothy Jaeryang Baek 2025-07-24 23:44:43 +04:00
parent 3220088411
commit df7b5ec907

View file

@ -131,14 +131,29 @@ async def load_function_from_url(
############################ ############################
class SyncFunctionsForm(FunctionForm): class SyncFunctionsForm(BaseModel):
functions: list[FunctionModel] = [] functions: list[FunctionModel] = []
@router.post("/sync", response_model=Optional[FunctionModel]) @router.post("/sync", response_model=list[FunctionModel])
async def sync_functions( async def sync_functions(
request: Request, form_data: SyncFunctionsForm, user=Depends(get_admin_user) 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) return Functions.sync_functions(user.id, form_data.functions)