plugin.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from collections.abc import Generator
  2. from core.plugin.entities.plugin import PluginEntity
  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. return self._request_with_plugin_daemon_response(
  9. "GET", f"plugin/{tenant_id}/fetch/identifier", bool, params={"plugin_unique_identifier": identifier}
  10. )
  11. def list_plugins(self, tenant_id: str) -> list[PluginEntity]:
  12. return self._request_with_plugin_daemon_response(
  13. "GET",
  14. f"plugin/{tenant_id}/management/list",
  15. list[PluginEntity],
  16. params={"page": 1, "page_size": 256},
  17. )
  18. def install_from_pkg(self, tenant_id: str, pkg: bytes) -> Generator[InstallPluginMessage, None, None]:
  19. """
  20. Install a plugin from a package.
  21. """
  22. # using multipart/form-data to encode body
  23. body = {"dify_pkg": ("dify_pkg", pkg, "application/octet-stream")}
  24. return self._request_with_plugin_daemon_response_stream(
  25. "POST", f"plugin/{tenant_id}/install/pkg", InstallPluginMessage, data=body
  26. )
  27. def install_from_identifier(self, tenant_id: str, identifier: str) -> bool:
  28. """
  29. Install a plugin from an identifier.
  30. """
  31. # exception will be raised if the request failed
  32. return self._request_with_plugin_daemon_response(
  33. "POST",
  34. f"plugin/{tenant_id}/install/identifier",
  35. bool,
  36. params={
  37. "plugin_unique_identifier": identifier,
  38. },
  39. data={
  40. "plugin_unique_identifier": identifier,
  41. },
  42. )
  43. def uninstall(self, tenant_id: str, identifier: str) -> bool:
  44. """
  45. Uninstall a plugin.
  46. """
  47. return self._request_with_plugin_daemon_response(
  48. "DELETE", f"plugin/{tenant_id}/uninstall", bool, params={"plugin_unique_identifier": identifier}
  49. )