plugin.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import os
  2. import re
  3. import subprocess
  4. import sys
  5. from importlib import util
  6. import types
  7. import tempfile
  8. import logging
  9. from open_webui.env import SRC_LOG_LEVELS
  10. from open_webui.models.functions import Functions
  11. from open_webui.models.tools import Tools
  12. log = logging.getLogger(__name__)
  13. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  14. def extract_frontmatter(content):
  15. """
  16. Extract frontmatter as a dictionary from the provided content string.
  17. """
  18. frontmatter = {}
  19. frontmatter_started = False
  20. frontmatter_ended = False
  21. frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE)
  22. try:
  23. lines = content.splitlines()
  24. if len(lines) < 1 or lines[0].strip() != '"""':
  25. # The content doesn't start with triple quotes
  26. return {}
  27. frontmatter_started = True
  28. for line in lines[1:]:
  29. if '"""' in line:
  30. if frontmatter_started:
  31. frontmatter_ended = True
  32. break
  33. if frontmatter_started and not frontmatter_ended:
  34. match = frontmatter_pattern.match(line)
  35. if match:
  36. key, value = match.groups()
  37. frontmatter[key.strip()] = value.strip()
  38. except Exception as e:
  39. log.exception(f"Failed to extract frontmatter: {e}")
  40. return {}
  41. return frontmatter
  42. def replace_imports(content):
  43. """
  44. Replace the import paths in the content.
  45. """
  46. replacements = {
  47. "from utils": "from open_webui.utils",
  48. "from apps": "from open_webui.apps",
  49. "from main": "from open_webui.main",
  50. "from config": "from open_webui.config",
  51. }
  52. for old, new in replacements.items():
  53. content = content.replace(old, new)
  54. return content
  55. def load_tools_module_by_id(toolkit_id, content=None):
  56. if content is None:
  57. tool = Tools.get_tool_by_id(toolkit_id)
  58. if not tool:
  59. raise Exception(f"Toolkit not found: {toolkit_id}")
  60. content = tool.content
  61. content = replace_imports(content)
  62. Tools.update_tool_by_id(toolkit_id, {"content": content})
  63. else:
  64. frontmatter = extract_frontmatter(content)
  65. # Install required packages found within the frontmatter
  66. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  67. module_name = f"tool_{toolkit_id}"
  68. module = types.ModuleType(module_name)
  69. sys.modules[module_name] = module
  70. # Create a temporary file and use it to define `__file__` so
  71. # that it works as expected from the module's perspective.
  72. temp_file = tempfile.NamedTemporaryFile(delete=False)
  73. temp_file.close()
  74. try:
  75. with open(temp_file.name, "w", encoding="utf-8") as f:
  76. f.write(content)
  77. module.__dict__["__file__"] = temp_file.name
  78. # Executing the modified content in the created module's namespace
  79. exec(content, module.__dict__)
  80. frontmatter = extract_frontmatter(content)
  81. log.info(f"Loaded module: {module.__name__}")
  82. # Create and return the object if the class 'Tools' is found in the module
  83. if hasattr(module, "Tools"):
  84. return module.Tools(), frontmatter
  85. else:
  86. raise Exception("No Tools class found in the module")
  87. except Exception as e:
  88. log.error(f"Error loading module: {toolkit_id}: {e}")
  89. del sys.modules[module_name] # Clean up
  90. raise e
  91. finally:
  92. os.unlink(temp_file.name)
  93. def load_function_module_by_id(function_id, content=None):
  94. if content is None:
  95. function = Functions.get_function_by_id(function_id)
  96. if not function:
  97. raise Exception(f"Function not found: {function_id}")
  98. content = function.content
  99. content = replace_imports(content)
  100. Functions.update_function_by_id(function_id, {"content": content})
  101. else:
  102. frontmatter = extract_frontmatter(content)
  103. install_frontmatter_requirements(frontmatter.get("requirements", ""))
  104. module_name = f"function_{function_id}"
  105. module = types.ModuleType(module_name)
  106. sys.modules[module_name] = module
  107. # Create a temporary file and use it to define `__file__` so
  108. # that it works as expected from the module's perspective.
  109. temp_file = tempfile.NamedTemporaryFile(delete=False)
  110. temp_file.close()
  111. try:
  112. with open(temp_file.name, "w", encoding="utf-8") as f:
  113. f.write(content)
  114. module.__dict__["__file__"] = temp_file.name
  115. # Execute the modified content in the created module's namespace
  116. exec(content, module.__dict__)
  117. frontmatter = extract_frontmatter(content)
  118. log.info(f"Loaded module: {module.__name__}")
  119. # Create appropriate object based on available class type in the module
  120. if hasattr(module, "Pipe"):
  121. return module.Pipe(), "pipe", frontmatter
  122. elif hasattr(module, "Filter"):
  123. return module.Filter(), "filter", frontmatter
  124. elif hasattr(module, "Action"):
  125. return module.Action(), "action", frontmatter
  126. else:
  127. raise Exception("No Function class found in the module")
  128. except Exception as e:
  129. log.error(f"Error loading module: {function_id}: {e}")
  130. del sys.modules[module_name] # Cleanup by removing the module in case of error
  131. Functions.update_function_by_id(function_id, {"is_active": False})
  132. raise e
  133. finally:
  134. os.unlink(temp_file.name)
  135. def install_frontmatter_requirements(requirements):
  136. if requirements:
  137. try:
  138. req_list = [req.strip() for req in requirements.split(",")]
  139. for req in req_list:
  140. log.info(f"Installing requirement: {req}")
  141. subprocess.check_call([sys.executable, "-m", "pip", "install", req])
  142. except Exception as e:
  143. log.error(f"Error installing package: {req}")
  144. raise e
  145. else:
  146. log.info("No requirements found in frontmatter.")