| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2024-08-08 15:46:14 +08:00
										 |  |  | import subprocess | 
					
						
							| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | import sys | 
					
						
							|  |  |  | from importlib import util | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | import types | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  | from open_webui.env import SRC_LOG_LEVELS | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | from open_webui.models.functions import Functions | 
					
						
							|  |  |  | from open_webui.models.tools import Tools | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  | log = logging.getLogger(__name__) | 
					
						
							|  |  |  | log.setLevel(SRC_LOG_LEVELS["MAIN"]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-06 02:35:58 +08:00
										 |  |  | def extract_frontmatter(content): | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-06 02:35:58 +08:00
										 |  |  |     Extract frontmatter as a dictionary from the provided content string. | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     frontmatter = {} | 
					
						
							| 
									
										
										
										
											2024-06-24 11:37:41 +08:00
										 |  |  |     frontmatter_started = False | 
					
						
							|  |  |  |     frontmatter_ended = False | 
					
						
							|  |  |  |     frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-09-06 02:35:58 +08:00
										 |  |  |         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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 11:37:41 +08:00
										 |  |  |     except Exception as e: | 
					
						
							|  |  |  |         print(f"An error occurred: {e}") | 
					
						
							|  |  |  |         return {} | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return frontmatter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | def replace_imports(content): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Replace the import paths in the content. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return content | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 09:54:38 +08:00
										 |  |  | def load_tools_module_by_id(toolkit_id, content=None): | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |     if content is None: | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  |         tool = Tools.get_tool_by_id(toolkit_id) | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         if not tool: | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  |             raise Exception(f"Toolkit not found: {toolkit_id}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         content = tool.content | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:57:41 +08:00
										 |  |  |         content = replace_imports(content) | 
					
						
							|  |  |  |         Tools.update_tool_by_id(toolkit_id, {"content": content}) | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         frontmatter = extract_frontmatter(content) | 
					
						
							|  |  |  |         # Install required packages found within the frontmatter | 
					
						
							|  |  |  |         install_frontmatter_requirements(frontmatter.get("requirements", "")) | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 02:00:47 +08:00
										 |  |  |     module_name = f"tool_{toolkit_id}" | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |     module = types.ModuleType(module_name) | 
					
						
							|  |  |  |     sys.modules[module_name] = module | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |     # Create a temporary file and use it to define `__file__` so | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  |     # that it works as expected from the module's perspective. | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |     temp_file = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							| 
									
										
										
										
											2024-09-24 14:22:47 +08:00
										 |  |  |     temp_file.close() | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |         with open(temp_file.name, "w", encoding="utf-8") as f: | 
					
						
							|  |  |  |             f.write(content) | 
					
						
							|  |  |  |         module.__dict__["__file__"] = temp_file.name | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         # Executing the modified content in the created module's namespace | 
					
						
							|  |  |  |         exec(content, module.__dict__) | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  |         frontmatter = extract_frontmatter(content) | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  |         log.info(f"Loaded module: {module.__name__}") | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         # Create and return the object if the class 'Tools' is found in the module | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  |         if hasattr(module, "Tools"): | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |             return module.Tools(), frontmatter | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |             raise Exception("No Tools class found in the module") | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  |         log.error(f"Error loading module: {toolkit_id}: {e}") | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         del sys.modules[module_name]  # Clean up | 
					
						
							| 
									
										
										
										
											2024-06-11 14:40:27 +08:00
										 |  |  |         raise e | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |         os.unlink(temp_file.name) | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | def load_function_module_by_id(function_id, content=None): | 
					
						
							|  |  |  |     if content is None: | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  |         function = Functions.get_function_by_id(function_id) | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         if not function: | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  |             raise Exception(f"Function not found: {function_id}") | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         content = function.content | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:57:41 +08:00
										 |  |  |         content = replace_imports(content) | 
					
						
							|  |  |  |         Functions.update_function_by_id(function_id, {"content": content}) | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  |     else: | 
					
						
							|  |  |  |         frontmatter = extract_frontmatter(content) | 
					
						
							|  |  |  |         install_frontmatter_requirements(frontmatter.get("requirements", "")) | 
					
						
							| 
									
										
										
										
											2024-08-26 18:18:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 02:00:47 +08:00
										 |  |  |     module_name = f"function_{function_id}" | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |     module = types.ModuleType(module_name) | 
					
						
							|  |  |  |     sys.modules[module_name] = module | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |     # Create a temporary file and use it to define `__file__` so | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  |     # that it works as expected from the module's perspective. | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |     temp_file = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							| 
									
										
										
										
											2024-09-24 14:22:47 +08:00
										 |  |  |     temp_file.close() | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |         with open(temp_file.name, "w", encoding="utf-8") as f: | 
					
						
							|  |  |  |             f.write(content) | 
					
						
							|  |  |  |         module.__dict__["__file__"] = temp_file.name | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         # Execute the modified content in the created module's namespace | 
					
						
							|  |  |  |         exec(content, module.__dict__) | 
					
						
							| 
									
										
										
										
											2024-09-07 00:35:43 +08:00
										 |  |  |         frontmatter = extract_frontmatter(content) | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  |         log.info(f"Loaded module: {module.__name__}") | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Create appropriate object based on available class type in the module | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |         if hasattr(module, "Pipe"): | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |             return module.Pipe(), "pipe", frontmatter | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |         elif hasattr(module, "Filter"): | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |             return module.Filter(), "filter", frontmatter | 
					
						
							| 
									
										
										
										
											2024-07-12 09:41:00 +08:00
										 |  |  |         elif hasattr(module, "Action"): | 
					
						
							|  |  |  |             return module.Action(), "action", frontmatter | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |             raise Exception("No Function class found in the module") | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  |         log.error(f"Error loading module: {function_id}: {e}") | 
					
						
							| 
									
										
										
										
											2024-09-05 01:55:20 +08:00
										 |  |  |         del sys.modules[module_name]  # Cleanup by removing the module in case of error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Functions.update_function_by_id(function_id, {"is_active": False}) | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  |         raise e | 
					
						
							| 
									
										
										
										
											2024-09-18 16:08:30 +08:00
										 |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2024-09-20 06:12:52 +08:00
										 |  |  |         os.unlink(temp_file.name) | 
					
						
							| 
									
										
										
										
											2024-08-08 15:46:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 20:49:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 15:46:14 +08:00
										 |  |  | def install_frontmatter_requirements(requirements): | 
					
						
							|  |  |  |     if requirements: | 
					
						
							| 
									
										
										
										
											2025-01-31 04:32:11 +08:00
										 |  |  |         try: | 
					
						
							|  |  |  |             req_list = [req.strip() for req in requirements.split(",")] | 
					
						
							|  |  |  |             for req in req_list: | 
					
						
							|  |  |  |                 log.info(f"Installing requirement: {req}") | 
					
						
							|  |  |  |                 subprocess.check_call([sys.executable, "-m", "pip", "install", req]) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             log.error(f"Error installing package: {req}") | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 15:46:14 +08:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-11-22 12:14:05 +08:00
										 |  |  |         log.info("No requirements found in frontmatter.") |