plugin_service.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from collections.abc import Sequence
  2. from mimetypes import guess_type
  3. from configs import dify_config
  4. from core.helper.download import download_with_size_limit
  5. from core.helper.marketplace import download_plugin_pkg
  6. from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource
  7. from core.plugin.entities.plugin_daemon import PluginInstallTask
  8. from core.plugin.manager.asset import PluginAssetManager
  9. from core.plugin.manager.debugging import PluginDebuggingManager
  10. from core.plugin.manager.plugin import PluginInstallationManager
  11. class PluginService:
  12. @staticmethod
  13. def get_debugging_key(tenant_id: str) -> str:
  14. """
  15. get the debugging key of the tenant
  16. """
  17. manager = PluginDebuggingManager()
  18. return manager.get_debugging_key(tenant_id)
  19. @staticmethod
  20. def list(tenant_id: str) -> list[PluginEntity]:
  21. """
  22. list all plugins of the tenant
  23. """
  24. manager = PluginInstallationManager()
  25. return manager.list_plugins(tenant_id)
  26. @staticmethod
  27. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  28. """
  29. get the asset file of the plugin
  30. """
  31. manager = PluginAssetManager()
  32. # guess mime type
  33. mime_type, _ = guess_type(asset_file)
  34. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  35. @staticmethod
  36. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  37. """
  38. check if the plugin unique identifier is already installed by other tenant
  39. """
  40. manager = PluginInstallationManager()
  41. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  42. @staticmethod
  43. def fetch_plugin_manifest(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  44. manager = PluginInstallationManager()
  45. return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  46. @staticmethod
  47. def fetch_install_tasks(tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
  48. manager = PluginInstallationManager()
  49. return manager.fetch_plugin_installation_tasks(tenant_id, page, page_size)
  50. @staticmethod
  51. def fetch_install_task(tenant_id: str, task_id: str) -> PluginInstallTask:
  52. manager = PluginInstallationManager()
  53. return manager.fetch_plugin_installation_task(tenant_id, task_id)
  54. @staticmethod
  55. def delete_install_task(tenant_id: str, task_id: str) -> bool:
  56. manager = PluginInstallationManager()
  57. return manager.delete_plugin_installation_task(tenant_id, task_id)
  58. @staticmethod
  59. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> str:
  60. """
  61. Upload plugin package files
  62. returns: plugin_unique_identifier
  63. """
  64. manager = PluginInstallationManager()
  65. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  66. @staticmethod
  67. def upload_pkg_from_github(
  68. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  69. ) -> str:
  70. """
  71. Install plugin from github release package files,
  72. returns plugin_unique_identifier
  73. """
  74. pkg = download_with_size_limit(
  75. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  76. )
  77. manager = PluginInstallationManager()
  78. return manager.upload_pkg(
  79. tenant_id,
  80. pkg,
  81. verify_signature,
  82. )
  83. @staticmethod
  84. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  85. manager = PluginInstallationManager()
  86. return manager.install_from_identifiers(
  87. tenant_id,
  88. plugin_unique_identifiers,
  89. PluginInstallationSource.Package,
  90. {},
  91. )
  92. @staticmethod
  93. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  94. """
  95. Install plugin from github release package files,
  96. returns plugin_unique_identifier
  97. """
  98. manager = PluginInstallationManager()
  99. return manager.install_from_identifiers(
  100. tenant_id,
  101. [plugin_unique_identifier],
  102. PluginInstallationSource.Github,
  103. {
  104. "repo": repo,
  105. "version": version,
  106. "package": package,
  107. },
  108. )
  109. @staticmethod
  110. def install_from_marketplace_pkg(
  111. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  112. ):
  113. """
  114. Install plugin from marketplace package files,
  115. returns installation task id
  116. """
  117. manager = PluginInstallationManager()
  118. # check if already downloaded
  119. for plugin_unique_identifier in plugin_unique_identifiers:
  120. try:
  121. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  122. # already downloaded, skip
  123. except Exception:
  124. # plugin not installed, download and upload pkg
  125. pkg = download_plugin_pkg(plugin_unique_identifier)
  126. manager.upload_pkg(tenant_id, pkg, verify_signature)
  127. return manager.install_from_identifiers(
  128. tenant_id,
  129. plugin_unique_identifiers,
  130. PluginInstallationSource.Marketplace,
  131. {
  132. "plugin_unique_identifier": plugin_unique_identifier,
  133. },
  134. )
  135. @staticmethod
  136. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  137. manager = PluginInstallationManager()
  138. return manager.uninstall(tenant_id, plugin_installation_id)