mirror of
https://git.mirrors.martin98.com/https://github.com/open-webui/open-webui
synced 2025-08-14 19:26:02 +08:00
refac
This commit is contained in:
parent
8609471eeb
commit
40bea00e3d
@ -66,7 +66,9 @@ async def process_filter_functions(
|
|||||||
if not filter:
|
if not filter:
|
||||||
continue
|
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
|
# Prepare handler function
|
||||||
handler = getattr(function_module, filter_type, None)
|
handler = getattr(function_module, filter_type, None)
|
||||||
if not handler:
|
if not handler:
|
||||||
|
@ -166,31 +166,46 @@ def load_function_module_by_id(function_id: str, content: str | None = None):
|
|||||||
os.unlink(temp_file.name)
|
os.unlink(temp_file.name)
|
||||||
|
|
||||||
|
|
||||||
def get_function_module_from_cache(request, function_id):
|
def get_function_module_from_cache(request, function_id, load_from_db=True):
|
||||||
function = Functions.get_function_by_id(function_id)
|
|
||||||
if not function:
|
|
||||||
raise Exception(f"Function not found: {function_id}")
|
|
||||||
content = function.content
|
|
||||||
|
|
||||||
new_content = replace_imports(content)
|
if load_from_db:
|
||||||
if new_content != content:
|
# Always load from the database if requested
|
||||||
content = new_content
|
function = Functions.get_function_by_id(function_id)
|
||||||
# Update the function content in the database
|
if not function:
|
||||||
Functions.update_function_by_id(function_id, {"content": content})
|
raise Exception(f"Function not found: {function_id}")
|
||||||
|
content = function.content
|
||||||
|
|
||||||
if (
|
new_content = replace_imports(content)
|
||||||
hasattr(request.app.state, "FUNCTION_CONTENTS")
|
if new_content != content:
|
||||||
and function_id in request.app.state.FUNCTION_CONTENTS
|
content = new_content
|
||||||
) and (
|
# Update the function content in the database
|
||||||
hasattr(request.app.state, "FUNCTIONS")
|
Functions.update_function_by_id(function_id, {"content": content})
|
||||||
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(
|
if (
|
||||||
function_id, content
|
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"):
|
if not hasattr(request.app.state, "FUNCTIONS"):
|
||||||
request.app.state.FUNCTIONS = {}
|
request.app.state.FUNCTIONS = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user