plugin.py 2.3 KB

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