refac
This commit is contained in:
1 parent
35f8bf97e3
commit
b5c43968db
32 files changed
+2251
-120
No files matched your search
@@ -11,6 +11,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
from open_webui.config import CACHE_DIR
|
||||
from open_webui.constants import ERROR_MESSAGES
|
||||
from open_webui.env import AIOHTTP_CLIENT_SESSION_SSL, AIOHTTP_CLIENT_TIMEOUT
|
||||
from open_webui.events import EVENTS, publish_event
|
||||
from open_webui.internal.db import get_async_session
|
||||
from open_webui.models.functions import (
|
||||
FunctionForm,
|
||||
@@ -218,6 +219,13 @@ async def create_new_function(
|
||||
await Functions.update_function_metadata_by_id(form_data.id, {'toggle': True}, db=db)
|
||||
|
||||
if function:
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_CREATED,
|
||||
actor=user,
|
||||
subject_id=function.id,
|
||||
data={'type': function.type, 'name': function.name},
|
||||
)
|
||||
return function
|
||||
else:
|
||||
raise HTTPException(
|
||||
@@ -261,12 +269,24 @@ async def get_function_by_id(id: str, user=Depends(get_admin_user), db: AsyncSes
|
||||
|
||||
|
||||
@router.post('/id/{id}/toggle', response_model=FunctionModel | None)
|
||||
async def toggle_function_by_id(id: str, user=Depends(get_admin_user), db: AsyncSession = Depends(get_async_session)):
|
||||
async def toggle_function_by_id(
|
||||
request: Request,
|
||||
id: str,
|
||||
user=Depends(get_admin_user),
|
||||
db: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
function = await Functions.get_function_by_id(id, db=db)
|
||||
if function:
|
||||
function = await Functions.update_function_by_id(id, {'is_active': not function.is_active}, db=db)
|
||||
|
||||
if function:
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_ENABLED if function.is_active else EVENTS.FUNCTION_DISABLED,
|
||||
actor=user,
|
||||
subject_id=function.id, subject_type='function',
|
||||
data={'type': function.type, 'name': function.name},
|
||||
)
|
||||
return function
|
||||
else:
|
||||
raise HTTPException(
|
||||
@@ -286,12 +306,24 @@ async def toggle_function_by_id(id: str, user=Depends(get_admin_user), db: Async
|
||||
|
||||
|
||||
@router.post('/id/{id}/toggle/global', response_model=FunctionModel | None)
|
||||
async def toggle_global_by_id(id: str, user=Depends(get_admin_user), db: AsyncSession = Depends(get_async_session)):
|
||||
async def toggle_global_by_id(
|
||||
request: Request,
|
||||
id: str,
|
||||
user=Depends(get_admin_user),
|
||||
db: AsyncSession = Depends(get_async_session),
|
||||
):
|
||||
function = await Functions.get_function_by_id(id, db=db)
|
||||
if function:
|
||||
function = await Functions.update_function_by_id(id, {'is_global': not function.is_global}, db=db)
|
||||
|
||||
if function:
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_UPDATED,
|
||||
actor=user,
|
||||
subject_id=function.id,
|
||||
data={'type': function.type, 'name': function.name, 'is_global': function.is_global},
|
||||
)
|
||||
return function
|
||||
else:
|
||||
raise HTTPException(
|
||||
@@ -335,6 +367,13 @@ async def update_function_by_id(
|
||||
await Functions.update_function_metadata_by_id(id, {'toggle': True}, db=db)
|
||||
|
||||
if function:
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_UPDATED,
|
||||
actor=user,
|
||||
subject_id=function.id,
|
||||
data={'type': function.type, 'name': function.name},
|
||||
)
|
||||
return function
|
||||
else:
|
||||
raise HTTPException(
|
||||
@@ -366,6 +405,12 @@ async def delete_function_by_id(
|
||||
if result:
|
||||
FUNCTIONS = get_functions_cache(request)
|
||||
FUNCTIONS.pop(id, None)
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_DELETED,
|
||||
actor=user,
|
||||
subject_id=id,
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
@@ -452,6 +497,12 @@ async def update_function_valves_by_id(
|
||||
|
||||
valves_dict = valves.model_dump(exclude_unset=True)
|
||||
await Functions.update_function_valves_by_id(id, valves_dict, db=db)
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_VALVES_UPDATED,
|
||||
actor=user,
|
||||
subject_id=id,
|
||||
)
|
||||
return valves_dict
|
||||
except Exception as e:
|
||||
log.exception(f'Error updating function values by id {id}: {e}')
|
||||
@@ -544,6 +595,13 @@ async def update_function_user_valves_by_id(
|
||||
user_valves = UserValves(**form_data)
|
||||
user_valves_dict = user_valves.model_dump(exclude_unset=True)
|
||||
await Functions.update_user_valves_by_id_and_user_id(id, user.id, user_valves_dict, db=db)
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.FUNCTION_VALVES_UPDATED,
|
||||
actor=user,
|
||||
subject_id=id,
|
||||
data={'scope': 'user'},
|
||||
)
|
||||
return user_valves_dict
|
||||
except Exception as e:
|
||||
log.exception(f'Error updating function user valves by id {id}: {e}')
|
||||
|
||||
Reference in new issue
Block a user