plugin.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from collections.abc import Generator
  2. from core.plugin.entities.plugin_daemon import InstallPluginMessage
  3. from core.plugin.manager.base import BasePluginManager
  4. class PluginInstallationManager(BasePluginManager):
  5. def fetch_plugin_by_identifier(self, tenant_id: str, identifier: str) -> bool:
  6. # urlencode the identifier
  7. return self._request_with_plugin_daemon_response(
  8. "GET", f"plugin/{tenant_id}/fetch/identifier", bool, params={"plugin_unique_identifier": identifier}
  9. )
  10. def install_from_pkg(self, tenant_id: str, pkg: bytes) -> Generator[InstallPluginMessage, None, None]:
  11. """
  12. Install a plugin from a package.
  13. """
  14. # using multipart/form-data to encode body
  15. body = {"dify_pkg": ("dify_pkg", pkg, "application/octet-stream")}
  16. return self._request_with_plugin_daemon_response_stream(
  17. "POST", f"plugin/{tenant_id}/install/pkg", InstallPluginMessage, data=body
  18. )
  19. def install_from_identifier(self, tenant_id: str, identifier: str) -> bool:
  20. """
  21. Install a plugin from an identifier.
  22. """
  23. # exception will be raised if the request failed
  24. return self._request_with_plugin_daemon_response(
  25. "POST",
  26. f"plugin/{tenant_id}/install/identifier",
  27. bool,
  28. params={
  29. "plugin_unique_identifier": identifier,
  30. },
  31. data={
  32. "plugin_unique_identifier": identifier,
  33. },
  34. )
  35. def uninstall(self, tenant_id: str, identifier: str) -> bool:
  36. """
  37. Uninstall a plugin.
  38. """
  39. return self._request_with_plugin_daemon_response(
  40. "DELETE", f"plugin/{tenant_id}/uninstall", bool, params={"plugin_unique_identifier": identifier}
  41. )