refac: temp tools & functions files

This commit is contained in:
Timothy J. Baek 2024-09-20 00:12:52 +02:00
parent 5b46a252ff
commit eb9ad47ef8

View File

@ -4,7 +4,7 @@ import subprocess
import sys import sys
from importlib import util from importlib import util
import types import types
import tempfile
from open_webui.apps.webui.models.functions import Functions from open_webui.apps.webui.models.functions import Functions
from open_webui.apps.webui.models.tools import Tools from open_webui.apps.webui.models.tools import Tools
@ -84,12 +84,14 @@ def load_toolkit_module_by_id(toolkit_id, content=None):
module = types.ModuleType(module_name) module = types.ModuleType(module_name)
sys.modules[module_name] = module sys.modules[module_name] = module
# Create a temporary in-memory file and use it to define `__file__` so # Create a temporary file and use it to define `__file__` so
# that it works as expected from the module's perspective. # that it works as expected from the module's perspective.
temp_fd = os.memfd_create(f"tmp:{module_name}") temp_file = tempfile.NamedTemporaryFile(delete=False)
try: try:
os.write(temp_fd, content.encode("utf-8")) with open(temp_file.name, "w", encoding="utf-8") as f:
module.__dict__["__file__"] = f"/proc/{os.getpid()}/fd/{temp_fd}" f.write(content)
module.__dict__["__file__"] = temp_file.name
# Executing the modified content in the created module's namespace # Executing the modified content in the created module's namespace
exec(content, module.__dict__) exec(content, module.__dict__)
@ -106,7 +108,7 @@ def load_toolkit_module_by_id(toolkit_id, content=None):
del sys.modules[module_name] # Clean up del sys.modules[module_name] # Clean up
raise e raise e
finally: finally:
os.close(temp_fd) os.unlink(temp_file.name)
def load_function_module_by_id(function_id, content=None): def load_function_module_by_id(function_id, content=None):
@ -126,12 +128,13 @@ def load_function_module_by_id(function_id, content=None):
module = types.ModuleType(module_name) module = types.ModuleType(module_name)
sys.modules[module_name] = module sys.modules[module_name] = module
# Create a temporary in-memory file and use it to define `__file__` so # Create a temporary file and use it to define `__file__` so
# that it works as expected from the module's perspective. # that it works as expected from the module's perspective.
temp_fd = os.memfd_create(f"tmp:{module_name}") temp_file = tempfile.NamedTemporaryFile(delete=False)
try: try:
os.write(temp_fd, content.encode("utf-8")) with open(temp_file.name, "w", encoding="utf-8") as f:
module.__dict__["__file__"] = f"/proc/{os.getpid()}/fd/{temp_fd}" f.write(content)
module.__dict__["__file__"] = temp_file.name
# Execute the modified content in the created module's namespace # Execute the modified content in the created module's namespace
exec(content, module.__dict__) exec(content, module.__dict__)
@ -154,7 +157,7 @@ def load_function_module_by_id(function_id, content=None):
Functions.update_function_by_id(function_id, {"is_active": False}) Functions.update_function_by_id(function_id, {"is_active": False})
raise e raise e
finally: finally:
os.close(temp_fd) os.unlink(temp_file.name)
def install_frontmatter_requirements(requirements): def install_frontmatter_requirements(requirements):