from __future__ import annotations import ast import asyncio import inspect import logging import os import re import subprocess import sys import tempfile import types import uuid from importlib import util from typing import Any, Callable from fastapi import HTTPException from open_webui.env import ( ENABLE_FUNCTIONS, ENABLE_PIP_INSTALL_FRONTMATTER_REQUIREMENTS, ENABLE_TOOLS, OFFLINE_MODE, 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 log = logging.getLogger(__name__) def resolve_valves_schema_options(valves_class: type, schema: dict, user: Any = None) -> dict: """ Resolve dynamic options in a Valves schema. For properties with `input.options`, this function handles two cases: - List: Used directly as dropdown options - String: Treated as method name, called to get options dynamically Usage in Valves: class UserValves(BaseModel): # Static options priority: str = Field( default="medium", json_schema_extra={ "input": { "type": "select", "options": ["low", "medium", "high"] } } ) # Dynamic options (method name) model: str = Field( default="", json_schema_extra={ "input": { "type": "select", "options": "get_model_options" } } ) @classmethod def get_model_options(cls, __user__=None) -> list[dict]: return [{"value": "gpt-4", "label": "GPT-4"}] Args: valves_class: The Valves or UserValves Pydantic model class schema: The JSON schema dict from valves_class.schema() user: Optional user object passed to methods that accept __user__ Returns: Modified schema dict with resolved options """ if not schema or 'properties' not in schema: return schema # Make a copy to avoid mutating the original schema = dict(schema) schema['properties'] = dict(schema.get('properties', {})) for prop_name, prop_schema in list(schema['properties'].items()): # Get the original field info from the Pydantic model if not hasattr(valves_class, 'model_fields'): continue field_info = valves_class.model_fields.get(prop_name) if not field_info: continue # Check json_schema_extra for options json_schema_extra = field_info.json_schema_extra if not json_schema_extra or not isinstance(json_schema_extra, dict): continue input_config = json_schema_extra.get('input') if not input_config or not isinstance(input_config, dict): continue options = input_config.get('options') if options is None: continue resolved_options = None # Case 1: options is already a list - use directly if isinstance(options, list): resolved_options = options # Case 2: options is a string - treat as method name elif isinstance(options, str) and options: method = getattr(valves_class, options, None) if method is None or not callable(method): log.warning(f"options '{options}' not found or not callable on {valves_class.__name__}") continue try: import inspect sig = inspect.signature(method) params = sig.parameters # Prepare kwargs based on what the method accepts kwargs = {} if '__user__' in params and user is not None: kwargs['__user__'] = user.model_dump() if hasattr(user, 'model_dump') else user if 'user' in params and user is not None: kwargs['user'] = user.model_dump() if hasattr(user, 'model_dump') else user resolved_options = method(**kwargs) if kwargs else method() # Validate return type if not isinstance(resolved_options, list): log.warning(f"Method '{options}' did not return a list for {prop_name}") continue except Exception as e: log.warning(f'Failed to resolve options for {prop_name}: {e}') continue else: # Invalid options type - skip continue # Update the schema with resolved options schema['properties'][prop_name] = dict(prop_schema) if 'input' not in schema['properties'][prop_name]: schema['properties'][prop_name]['input'] = {'type': 'select'} else: schema['properties'][prop_name]['input'] = dict(schema['properties'][prop_name].get('input', {})) schema['properties'][prop_name]['input']['options'] = resolved_options return schema def extract_frontmatter(content): """ Extract frontmatter as a dictionary from the provided content string. """ frontmatter = {} frontmatter_started = False frontmatter_ended = False frontmatter_pattern = re.compile(r'^\s*([a-z_]+):\s*(.*)\s*$', re.IGNORECASE) try: lines = content.splitlines() if len(lines) < 1 or lines[0].strip() != '"""': # The content doesn't start with triple quotes return {} frontmatter_started = True for line in lines[1:]: if '"""' in line: if frontmatter_started: frontmatter_ended = True break if frontmatter_started and not frontmatter_ended: match = frontmatter_pattern.match(line) if match: key, value = match.groups() frontmatter[key.strip()] = value.strip() except Exception as e: log.exception(f'Failed to extract frontmatter: {e}') return {} return frontmatter FUNCTION_HOOKS = ('pipe', 'pipes', 'inlet', 'request', 'outlet', 'stream', 'action', 'event') def upgrade_source(content): """ Replace the import paths in the content, and write plain def tool methods and function hooks as async def. """ replacements = { 'from utils': 'from open_webui.utils', 'from apps': 'from open_webui.apps', 'from main': 'from open_webui.main', 'from config': 'from open_webui.config', } for old, new in replacements.items(): content = content.replace(old, new) try: tree = ast.parse(content) except SyntaxError: return content lines = content.split('\n') methods = [] for node in tree.body: if isinstance(node, ast.ClassDef) and node.name in ('Tools', 'Pipe', 'Filter', 'Action', 'Event'): # A method the plugin calls as self.name() would hand it a coroutine, so it stays for the load error. called = { n.attr for n in ast.walk(node) if isinstance(n, ast.Attribute) and isinstance(n.value, ast.Name) and n.value.id == 'self' } methods += [ n for n in node.body if isinstance(n, ast.FunctionDef) and not n.decorator_list and n.name not in called and (n.name in FUNCTION_HOOKS if node.name != 'Tools' else not n.name.startswith('_')) and lines[n.lineno - 1][n.col_offset :].startswith('def ') ] for node in methods: line = lines[node.lineno - 1] lines[node.lineno - 1] = f'{line[: node.col_offset]}async {line[node.col_offset :]}' # A generator with `yield from` or a returned value cannot be async, so it keeps plain def. try: compile('\n'.join(lines), '', 'exec') except SyntaxError: return content return '\n'.join(lines) def require_async_methods(methods: list[Callable]): """Reject tools and functions whose entry points are not async def.""" sync_names = [ method.__name__ for method in methods if not inspect.iscoroutinefunction(method) and not inspect.isasyncgenfunction(method) ] if sync_names: raise HTTPException(400, f'Methods must be defined with async def: {", ".join(sync_names)}') # Let the care put into this work survive every # import and transformation, and serve those who carry it forward. async def load_tool_module_by_id(tool_id, content=None): """Prepare a Tool instance, frontmatter, and Python module without changing the live cache.""" if not ENABLE_TOOLS: raise RuntimeError('Tools are disabled by ENABLE_PLUGINS or ENABLE_TOOLS') frontmatter = 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 content = upgrade_source(content) engine = await get_tool_sandbox_engine() if engine == 'monty': from open_webui.utils.tool_sandbox import load_sandboxed_tool try: return await load_sandboxed_tool(tool_id, content) except Exception as e: log.error(f'Error loading module: {tool_id}: {e}') raise # 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; # offload to a thread so it doesn't block the event loop. await asyncio.to_thread(install_frontmatter_requirements, frontmatter.get('requirements', '')) module_name = f'tool_{tool_id}_{uuid.uuid4().hex}' module = types.ModuleType(module_name) sys.modules[module_name] = module # Give the module a real __file__; even dynamically loaded code wants an address. temp_file = tempfile.NamedTemporaryFile(delete=False) temp_file.close() try: with open(temp_file.name, 'w', encoding='utf-8') as f: f.write(content) module.__dict__['__file__'] = temp_file.name # Executing the modified content in the created module's namespace exec(content, module.__dict__) if frontmatter is None: frontmatter = extract_frontmatter(content) log.info('Loaded module: %s', module.__name__) # Look for the Tools class; the toolkit still needs someone to answer the door. if hasattr(module, 'Tools'): from open_webui.utils.tools import get_functions_from_tool tool_module = module.Tools() require_async_methods(get_functions_from_tool(tool_module)) return tool_module, frontmatter, module else: raise Exception('No Tools class found in the module') except Exception as e: log.error(f'Error loading module: {tool_id}: {e}') raise e finally: os.unlink(temp_file.name) sys.modules.pop(module_name, None) async def load_function_module_by_id(function_id: str, content: str | None = None): """Prepare a Function instance, type, frontmatter, and Python module without publishing it.""" if not ENABLE_FUNCTIONS: raise RuntimeError('Functions are disabled by ENABLE_PLUGINS or ENABLE_FUNCTIONS') frontmatter = None if content is None: function = await Functions.get_function_by_id(function_id) if not function: raise Exception(f'Function not found: {function_id}') content = function.content else: frontmatter = extract_frontmatter(content) # `pip install` via subprocess can block for a long time; offload it. await asyncio.to_thread(install_frontmatter_requirements, frontmatter.get('requirements', '')) content = upgrade_source(content) module_name = f'function_{function_id}_{uuid.uuid4().hex}' module = types.ModuleType(module_name) sys.modules[module_name] = module # Give the module a real __file__; even dynamically loaded code wants an address. temp_file = tempfile.NamedTemporaryFile(delete=False) temp_file.close() try: with open(temp_file.name, 'w', encoding='utf-8') as f: f.write(content) module.__dict__['__file__'] = temp_file.name # Execute the modified content in the created module's namespace exec(content, module.__dict__) if frontmatter is None: frontmatter = extract_frontmatter(content) log.info('Loaded module: %s', module.__name__) # Create appropriate object based on available class type in the module if hasattr(module, 'Pipe'): function_module, function_type = module.Pipe(), 'pipe' elif hasattr(module, 'Filter'): function_module, function_type = module.Filter(), 'filter' elif hasattr(module, 'Action'): function_module, function_type = module.Action(), 'action' elif hasattr(module, 'Event'): function_module, function_type = module.Event(), 'event' else: raise Exception('No Function class found in the module') hooks = [getattr(function_module, name, None) for name in FUNCTION_HOOKS] require_async_methods([hook for hook in hooks if callable(hook)]) return function_module, function_type, frontmatter, module except Exception as e: log.error(f'Error loading module: {function_id}: {e}') raise e finally: os.unlink(temp_file.name) sys.modules.pop(module_name, None) def _state_cache(request, name: str) -> dict: if not hasattr(request.app.state, name): setattr(request.app.state, name, {}) return getattr(request.app.state, name) def get_tools_cache(request) -> dict: return _state_cache(request, 'TOOLS') def get_tool_contents_cache(request) -> dict: return _state_cache(request, 'TOOL_CONTENTS') def get_functions_cache(request) -> dict: return _state_cache(request, 'FUNCTIONS') 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: sys.modules.pop(previous.__name__, None) sys.modules[source_module.__name__] = source_module sys.modules[f'tool_{tool_id}'] = source_module get_tools_cache(request)[tool_id] = tool_module get_tool_contents_cache(request)[tool_id] = content def set_function_module_in_cache(request, function_id, content, function_module, source_module): previous = sys.modules.get(f'function_{function_id}') if previous is not None: sys.modules.pop(previous.__name__, None) sys.modules[source_module.__name__] = source_module sys.modules[f'function_{function_id}'] = source_module get_functions_cache(request)[function_id] = function_module get_function_contents_cache(request)[function_id] = content 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 if load_from_db: # Always load from the database by default tool = await Tools.get_tool_by_id(tool_id) if not tool: raise Exception(f'Tool not found: {tool_id}') content = tool.content if tool_id in tool_contents_cache and tool_id in tools_cache: if tool_contents_cache[tool_id] == content: return tools_cache[tool_id], None else: if tool_id in tools_cache: return tools_cache[tool_id], None try: tool_module, frontmatter, source_module = await load_tool_module_by_id(tool_id, content) except Exception as e: await Tools.update_tool_by_id(tool_id, {'is_active': False}) if isinstance(e, HTTPException): raise # The error can carry anything the tool's code put in it, so only the log gets it. raise HTTPException(400, f'Tool failed to load: {tool_id}') from e set_tool_module_in_cache(request, tool_id, content, tool_module, source_module) return tool_module, frontmatter async def get_function_module_from_cache( request, function_id, function: FunctionModel | None = None, load_from_db=True ): if not ENABLE_FUNCTIONS: raise RuntimeError('Functions are disabled by ENABLE_PLUGINS or ENABLE_FUNCTIONS') functions_cache = get_functions_cache(request) function_contents_cache = get_function_contents_cache(request) content = None if load_from_db: # Always load from the database by default # This is useful for hooks like "inlet" or "outlet" where the content might change # and we want to ensure the latest content is used. if function is None: function = await Functions.get_function_by_id(function_id) if not function: raise Exception(f'Function not found: {function_id}') content = function.content if function_id in function_contents_cache and function_id in functions_cache: if function_contents_cache[function_id] == content: return functions_cache[function_id], None, None else: # Load from cache (e.g. "stream" hook) # This is useful for performance reasons if function_id in functions_cache: return functions_cache[function_id], None, None try: function_module, function_type, frontmatter, source_module = await load_function_module_by_id( function_id, content ) except Exception as e: await Functions.update_function_by_id(function_id, {'is_active': False}) if isinstance(e, HTTPException): raise # The error can carry anything the function's code put in it, so only the log gets it. raise HTTPException(400, f'Function failed to load: {function_id}') from e set_function_module_in_cache(request, function_id, content, function_module, source_module) return function_module, function_type, frontmatter _installed_requirements = set() def install_frontmatter_requirements(requirements: str): global _installed_requirements if not ENABLE_PIP_INSTALL_FRONTMATTER_REQUIREMENTS: log.info('ENABLE_PIP_INSTALL_FRONTMATTER_REQUIREMENTS is disabled, skipping installation of requirements.') return if OFFLINE_MODE: log.info('Offline mode enabled, skipping installation of requirements.') return if requirements: try: req_list = [req.strip() for req in requirements.split(',')] new_reqs = [req for req in req_list if req and req not in _installed_requirements] if not new_reqs: return log.info('Installing requirements: %s', ' '.join(new_reqs)) subprocess.check_call( [sys.executable, '-m', 'pip', 'install'] + PIP_OPTIONS + new_reqs + PIP_PACKAGE_INDEX_OPTIONS ) _installed_requirements.update(new_reqs) except Exception as e: log.error(f'Error installing packages: {" ".join(new_reqs)}') raise e else: log.info('No requirements found in frontmatter.') async def install_tool_and_function_dependencies(): """ Install all dependencies for all admin tools and active functions. By first collecting all dependencies from the frontmatter of each tool and function, and then installing them using pip. Duplicates or similar version specifications are handled by pip as much as possible. """ if not ENABLE_TOOLS and not ENABLE_FUNCTIONS: log.info('Tools and Functions are disabled, skipping their dependencies.') return function_list = await Functions.get_functions(active_only=True) if ENABLE_FUNCTIONS 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: for function in function_list: frontmatter = extract_frontmatter(upgrade_source(function.content)) if dependencies := frontmatter.get('requirements'): all_dependencies += f'{dependencies}, ' for tool in tool_list: # Only install requirements for admin tools if tool.user and tool.user.role == 'admin': frontmatter = extract_frontmatter(upgrade_source(tool.content)) if dependencies := frontmatter.get('requirements'): all_dependencies += f'{dependencies}, ' # `pip install` via subprocess can block for a long time; offload it. await asyncio.to_thread(install_frontmatter_requirements, all_dependencies.strip(', ')) except Exception as e: log.error(f'Error installing requirements: {e}')