Skip to content

A plugin's source is ordinary Python; the bundler flattens it

A plugin folder is written as an ordinary Python project. Every file imports what it uses, so it reads, lints, type-checks and tests on its own. Bundling is a mechanical step that turns that project into the single string Open-WebUI stores. Until now it was the other way round: function.py and tool.py had no imports of their own. They relied on shared.py and the libraries being pasted in above them, so an editor showed undefined names, ruff had to skip examples/, and an "organize imports" on shared.py could remove imports that only the next file used.

What this means:

  • Ordinary imports stay where they are used. Standard-library and third-party imports go in each file. Repeating one in the bundle costs nothing, because Python looks a module up in sys.modules the second time.
  • Imports of the plugin's own code are resolved by the bundler. function.py and tool.py write from shared import NAME, which the editor and the tests can resolve. The bundler inlines shared.py once and removes the line. Only top-level from shared import NAME and from owpp.lib import NAME lines are allowed. An alias (as), a plain import shared or import owpp.lib, or either import inside a function body is an error, because removing the line would leave the name undefined, and so is a name defined both in shared.py and in the file that imports it, because after flattening they would share one namespace.
  • Libraries are a real package, imported from one flat module. A plugin writes from owpp.lib import PluginAssets, plugin_config. The bundler inlines the libraries in library order and removes the import. Each exported name belongs to one library, so owpp check reports a name whose library is not in include:. The libraries import each other from their sibling modules (from .plugin_events import PluginEvents), which avoids circular imports through the package's __init__, and the bundler removes those imports too. shared_assets and live_reload are Classic298's code, kept unmodified, so they are reached through thin wrapper modules. The wrappers say how to upgrade the vendored file. owpp is already a development dependency of any repository that tests its plugins, so editors there resolve owpp.lib.
  • The function and its tool are separate modules. tool.py is bundled on its own, with its own copy of shared.py, and embedded in the function bundle as data, which the runtime installs as a separate tool. So tool.py and function.py cannot import from each other, and the bundler rejects that with a message pointing to shared.py. A module-level value in shared.py exists once per module. State the two really share still goes through sys.modules, the way the chat_data library does.
  • open_webui.* is what the mock provides. Open-WebUI is not installed for development. The mock Open-WebUI (owpp/testing/mock.py) has to cover every open_webui name a plugin in this repository uses, and it is published as type stubs so an editor resolves the same names the tests run against. Its catch-all for missing names stays only for upstream code taken from git.
  • A file taken from git is not touched. function: {file} and tool: {file} are bundled as they are, and owpp migrate leaves them alone. The files of a plugin folder, whether written here or taken whole from git with source: {path}, are bundled as described above.
  • The new format is protocol 0.5 (owpp 0.5), with no compatibility period. owpp is still alpha, so the bundler does not support plugins without imports alongside the new ones. owpp migrate's 0.3 → 0.5 step (there is no 0.4: owpp 0.4 left the format unchanged) upgrades them instead, and is the first step that rewrites Python files as well as plugin.yaml. For each of function.py and tool.py it finds the names used but not defined there, and resolves them in this order: a name shared.py defines becomes from shared import ..., a library name becomes from owpp.lib import ..., and a name shared.py imports from elsewhere has that import copied over. It then removes the imports shared.py no longer uses itself, and reports any name it cannot resolve instead of guessing. An older owpp refuses a 0.5 plugin instead of producing a broken bundle.
  • {{file:NAME}} is deprecated, because it substitutes text into the source: use the templates library's plugin_template_text.

Considered Options

  • Keep concatenation and document it: no tooling change, but the editor stays blind to the shared code, the libraries and their base classes, and the unused-import hazard remains.
  • Embed modules and install an import hook (stickytape, pinliner, tsutsumu): keeps real module boundaries at run time, but sys.modules is shared by the whole Open-WebUI process, so two plugins' shared modules would collide. The deployed code would also be loader plumbing with the source held in strings, which an admin cannot easily review in Open-WebUI.
  • One module per library (from owpp.lib.assets import PluginAssets): would let include: be worked out from the imports, but a plugin would need a line per library. The flat module keeps the import to one line, and the check against include: works by name instead.
  • from .shared import NAME with the folder as a package: scales better past one shared file, but adds an __init__.py to every plugin and import ordering to the bundler. It can be added later if a plugin needs more than one shared module.