plugin.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import json
  2. from collections.abc import Generator, Mapping
  3. from typing import Any
  4. from core.plugin.entities.plugin import PluginEntity, PluginInstallationSource
  5. from core.plugin.entities.plugin_daemon import InstallPluginMessage
  6. from core.plugin.manager.base import BasePluginManager
  7. class PluginInstallationManager(BasePluginManager):
  8. def fetch_plugin_by_identifier(self, tenant_id: str, identifier: str) -> bool:
  9. # urlencode the identifier
  10. return self._request_with_plugin_daemon_response(
  11. "GET",
  12. f"plugin/{tenant_id}/management/fetch/identifier",
  13. bool,
  14. params={"plugin_unique_identifier": identifier},
  15. )
  16. def list_plugins(self, tenant_id: str) -> list[PluginEntity]:
  17. return self._request_with_plugin_daemon_response(
  18. "GET",
  19. f"plugin/{tenant_id}/management/list",
  20. list[PluginEntity],
  21. params={"page": 1, "page_size": 256},
  22. )
  23. def install_from_pkg(
  24. self,
  25. tenant_id: str,
  26. pkg: bytes,
  27. source: PluginInstallationSource,
  28. meta: Mapping[str, Any],
  29. verify_signature: bool = False,
  30. ) -> Generator[InstallPluginMessage, None, None]:
  31. """
  32. Install a plugin from a package.
  33. """
  34. # using multipart/form-data to encode body
  35. body = {
  36. "dify_pkg": ("dify_pkg", pkg, "application/octet-stream"),
  37. }
  38. data = {
  39. "verify_signature": "true" if verify_signature else "false",
  40. "source": source.value,
  41. "meta": json.dumps(meta),
  42. }
  43. return self._request_with_plugin_daemon_response_stream(
  44. "POST",
  45. f"plugin/{tenant_id}/management/install/pkg",
  46. InstallPluginMessage,
  47. files=body,
  48. data=data,
  49. )
  50. def install_from_identifier(self, tenant_id: str, identifier: str) -> bool:
  51. """
  52. Install a plugin from an identifier.
  53. """
  54. # exception will be raised if the request failed
  55. return self._request_with_plugin_daemon_response(
  56. "POST",
  57. f"plugin/{tenant_id}/management/install/identifier",
  58. bool,
  59. data={
  60. "plugin_unique_identifier": identifier,
  61. },
  62. headers={"Content-Type": "application/json"},
  63. )
  64. def uninstall(self, tenant_id: str, plugin_installation_id: str) -> bool:
  65. """
  66. Uninstall a plugin.
  67. """
  68. return self._request_with_plugin_daemon_response(
  69. "POST",
  70. f"plugin/{tenant_id}/management/uninstall",
  71. bool,
  72. data={
  73. "plugin_installation_id": plugin_installation_id,
  74. },
  75. headers={"Content-Type": "application/json"},
  76. )