plugin.py 1.6 KB

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