From 40bea00e3d9f5efd2b75327303822aa0cf74d242 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 27 May 2025 16:06:00 +0400 Subject: [PATCH] refac --- backend/open_webui/utils/filter.py | 4 +- backend/open_webui/utils/plugin.py | 59 +++++++++++++++++++----------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/backend/open_webui/utils/filter.py b/backend/open_webui/utils/filter.py index c9adee7d7..6ad242f51 100644 --- a/backend/open_webui/utils/filter.py +++ b/backend/open_webui/utils/filter.py @@ -66,7 +66,9 @@ async def process_filter_functions( if not filter: continue - function_module = get_function_module(request, filter_id) + function_module = get_function_module( + request, filter_id, load_from_db=(filter_type != "stream") + ) # Prepare handler function handler = getattr(function_module, filter_type, None) if not handler: diff --git a/backend/open_webui/utils/plugin.py b/backend/open_webui/utils/plugin.py index 9f0409019..04a2b2ddd 100644 --- a/backend/open_webui/utils/plugin.py +++ b/backend/open_webui/utils/plugin.py @@ -166,31 +166,46 @@ def load_function_module_by_id(function_id: str, content: str | None = None): os.unlink(temp_file.name) -def get_function_module_from_cache(request, function_id): - function = Functions.get_function_by_id(function_id) - if not function: - raise Exception(f"Function not found: {function_id}") - content = function.content +def get_function_module_from_cache(request, function_id, load_from_db=True): - new_content = replace_imports(content) - if new_content != content: - content = new_content - # Update the function content in the database - Functions.update_function_by_id(function_id, {"content": content}) + if load_from_db: + # Always load from the database if requested + function = Functions.get_function_by_id(function_id) + if not function: + raise Exception(f"Function not found: {function_id}") + content = function.content - if ( - hasattr(request.app.state, "FUNCTION_CONTENTS") - and function_id in request.app.state.FUNCTION_CONTENTS - ) and ( - hasattr(request.app.state, "FUNCTIONS") - and function_id in request.app.state.FUNCTIONS - ): - if request.app.state.FUNCTION_CONTENTS[function_id] == content: - return request.app.state.FUNCTIONS[function_id], None, None + new_content = replace_imports(content) + if new_content != content: + content = new_content + # Update the function content in the database + Functions.update_function_by_id(function_id, {"content": content}) - function_module, function_type, frontmatter = load_function_module_by_id( - function_id, content - ) + if ( + hasattr(request.app.state, "FUNCTION_CONTENTS") + and function_id in request.app.state.FUNCTION_CONTENTS + ) and ( + hasattr(request.app.state, "FUNCTIONS") + and function_id in request.app.state.FUNCTIONS + ): + if request.app.state.FUNCTION_CONTENTS[function_id] == content: + return request.app.state.FUNCTIONS[function_id], None, None + + function_module, function_type, frontmatter = load_function_module_by_id( + function_id, content + ) + else: + # Load from cache (e.g. "stream" hook) + if ( + hasattr(request.app.state, "FUNCTIONS") + and function_id in request.app.state.FUNCTIONS + ): + if request.app.state.FUNCTION_CONTENTS[function_id] == content: + return request.app.state.FUNCTIONS[function_id], None, None + + function_module, function_type, frontmatter = load_function_module_by_id( + function_id + ) if not hasattr(request.app.state, "FUNCTIONS"): request.app.state.FUNCTIONS = {}