From 94a60b04573acf6423e9c0519997b779f82e0560 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 26 Jul 2026 19:10:41 -0400 Subject: [PATCH] refac --- backend/open_webui/events.py | 20 +++++++++++++++++++- backend/open_webui/routers/functions.py | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/events.py b/backend/open_webui/events.py index b627c69a18..999a1c826c 100644 --- a/backend/open_webui/events.py +++ b/backend/open_webui/events.py @@ -483,6 +483,16 @@ class EventDefinitions(BaseModel): FUNCTION_DISABLED: EventDefinition = EventDefinition( name='function.disabled', description='A function was disabled.', message='Function disabled' ) + FUNCTION_ENABLE_STARTED: EventDefinition = EventDefinition( + name='function.enable_started', + description='A function is about to be enabled.', + message='Function enable started', + ) + FUNCTION_DISABLE_STARTED: EventDefinition = EventDefinition( + name='function.disable_started', + description='A function is about to be disabled.', + message='Function disable started', + ) FUNCTION_VALVES_UPDATED: EventDefinition = EventDefinition( name='function.valves_updated', description='Function valves were updated.', message='Function valves updated' ) @@ -1067,7 +1077,9 @@ class NotificationEventSink: schedule_notification_dispatch(app, event) -async def dispatch_event_functions(app: Any, event: Event, request: Any | None = None) -> None: +async def dispatch_event_functions( + app: Any, event: Event, request: Any | None = None, extra_function_ids: list[str] | None = None +) -> None: if not ENABLE_PLUGINS: return @@ -1079,6 +1091,12 @@ async def dispatch_event_functions(app: Any, event: Event, request: Any | None = try: event_functions = await Functions.get_functions_by_type('event', active_only=True) + if extra_function_ids: + extra_functions = await Functions.get_functions_by_ids(extra_function_ids) + existing_ids = {function.id for function in event_functions} + event_functions.extend( + function for function in extra_functions if function.type == 'event' and function.id not in existing_ids + ) except Exception: log.exception('Event functions could not be loaded for %s', event.event) return diff --git a/backend/open_webui/routers/functions.py b/backend/open_webui/routers/functions.py index 93d2879c18..3c1cf58cd6 100644 --- a/backend/open_webui/routers/functions.py +++ b/backend/open_webui/routers/functions.py @@ -11,7 +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, ENABLE_PLUGINS -from open_webui.events import EVENTS, publish_event +from open_webui.events import EVENTS, build_event, dispatch_event_functions, publish_event, schedule_webhook_dispatch from open_webui.internal.db import get_async_session from open_webui.models.functions import ( FunctionForm, @@ -293,6 +293,22 @@ async def toggle_function_by_id( ): function = await Functions.get_function_by_id(id, db=db) if function: + lifecycle_event = build_event( + request, + EVENTS.FUNCTION_DISABLE_STARTED if function.is_active else EVENTS.FUNCTION_ENABLE_STARTED, + actor=user, + subject_id=function.id, + subject_type='function', + data={'type': function.type, 'name': function.name}, + ) + await dispatch_event_functions( + request.app, + lifecycle_event, + request=request, + extra_function_ids=[function.id] if not function.is_active else None, + ) + schedule_webhook_dispatch(request.app, lifecycle_event) + function = await Functions.update_function_by_id(id, {'is_active': not function.is_active}, db=db) if function: