mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
refac
This commit is contained in:
1 parent
f0721d936b
commit
ecc7ebf690
71 files changed
+757
-3
No files matched your search
@@ -22,6 +22,7 @@ from open_webui.env import (
|
||||
PIP_OPTIONS,
|
||||
PIP_PACKAGE_INDEX_OPTIONS,
|
||||
)
|
||||
from open_webui.models.config import Config
|
||||
from open_webui.models.functions import FunctionModel, Functions
|
||||
from open_webui.models.tools import Tools
|
||||
|
||||
@@ -224,13 +225,24 @@ async def load_tool_module_by_id(tool_id, content=None):
|
||||
raise RuntimeError('Tools are disabled by ENABLE_PLUGINS or ENABLE_TOOLS')
|
||||
|
||||
frontmatter = None
|
||||
if content is None:
|
||||
given = content is not None
|
||||
if not given:
|
||||
tool = await Tools.get_tool_by_id(tool_id)
|
||||
if not tool:
|
||||
raise Exception(f'Toolkit not found: {tool_id}')
|
||||
|
||||
content = tool.content
|
||||
else:
|
||||
|
||||
engine = await get_tool_sandbox_engine()
|
||||
if engine == 'monty':
|
||||
from open_webui.utils.tool_sandbox import load_sandboxed_tool
|
||||
|
||||
return await load_sandboxed_tool(tool_id, content)
|
||||
# With the sandbox on, an unknown or empty engine must not fall back to exec.
|
||||
if engine is not None:
|
||||
raise RuntimeError(f'Unknown tool sandbox engine: {engine!r}')
|
||||
|
||||
if given:
|
||||
frontmatter = extract_frontmatter(content)
|
||||
# Install required packages found within the frontmatter.
|
||||
# Runs `pip install` via subprocess, which can take a long time;
|
||||
@@ -356,6 +368,20 @@ def get_function_contents_cache(request) -> dict:
|
||||
return _state_cache(request, 'FUNCTION_CONTENTS')
|
||||
|
||||
|
||||
async def get_tool_sandbox_engine() -> str | None:
|
||||
"""The engine workspace tools run in, or None when the sandbox is off and they run with exec."""
|
||||
return await Config.get('tools.sandbox.engine') if await Config.get('tools.sandbox.enable') else None
|
||||
|
||||
|
||||
async def sync_tool_sandbox_engine(request):
|
||||
"""Empty this worker's tool caches when the sandbox engine changed, so no tool keeps its old runtime."""
|
||||
engine = await get_tool_sandbox_engine()
|
||||
if getattr(request.app.state, 'TOOL_SANDBOX_ENGINE', engine) != engine:
|
||||
get_tools_cache(request).clear()
|
||||
get_tool_contents_cache(request).clear()
|
||||
request.app.state.TOOL_SANDBOX_ENGINE = engine
|
||||
|
||||
|
||||
def set_tool_module_in_cache(request, tool_id, content, tool_module, source_module):
|
||||
previous = sys.modules.get(f'tool_{tool_id}')
|
||||
if previous is not None:
|
||||
@@ -380,6 +406,7 @@ async def get_tool_module_from_cache(request, tool_id, load_from_db=True):
|
||||
if not ENABLE_TOOLS:
|
||||
raise RuntimeError('Tools are disabled by ENABLE_PLUGINS or ENABLE_TOOLS')
|
||||
|
||||
await sync_tool_sandbox_engine(request)
|
||||
tools_cache = get_tools_cache(request)
|
||||
tool_contents_cache = get_tool_contents_cache(request)
|
||||
content = None
|
||||
@@ -502,7 +529,10 @@ async def install_tool_and_function_dependencies():
|
||||
return
|
||||
|
||||
function_list = await Functions.get_functions(active_only=True) if ENABLE_FUNCTIONS else []
|
||||
tool_list = await Tools.get_tools(active_only=True) if ENABLE_TOOLS else []
|
||||
# Sandboxed tools cannot use packages, so install none for them.
|
||||
tool_list = (
|
||||
await Tools.get_tools(active_only=True) if ENABLE_TOOLS and await get_tool_sandbox_engine() is None else []
|
||||
)
|
||||
|
||||
all_dependencies = ''
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user