plugin_service.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, PluginUploadResponse
  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 delete_install_task_item(tenant_id: str, task_id: str, identifier: str) -> bool:
  60. manager = PluginInstallationManager()
  61. return manager.delete_plugin_installation_task_item(tenant_id, task_id, identifier)
  62. @staticmethod
  63. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginUploadResponse:
  64. """
  65. Upload plugin package files
  66. returns: plugin_unique_identifier
  67. """
  68. manager = PluginInstallationManager()
  69. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  70. @staticmethod
  71. def upload_pkg_from_github(
  72. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  73. ) -> PluginUploadResponse:
  74. """
  75. Install plugin from github release package files,
  76. returns plugin_unique_identifier
  77. """
  78. pkg = download_with_size_limit(
  79. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  80. )
  81. manager = PluginInstallationManager()
  82. return manager.upload_pkg(
  83. tenant_id,
  84. pkg,
  85. verify_signature,
  86. )
  87. @staticmethod
  88. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  89. manager = PluginInstallationManager()
  90. return manager.install_from_identifiers(
  91. tenant_id,
  92. plugin_unique_identifiers,
  93. PluginInstallationSource.Package,
  94. {},
  95. )
  96. @staticmethod
  97. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  98. """
  99. Install plugin from github release package files,
  100. returns plugin_unique_identifier
  101. """
  102. manager = PluginInstallationManager()
  103. return manager.install_from_identifiers(
  104. tenant_id,
  105. [plugin_unique_identifier],
  106. PluginInstallationSource.Github,
  107. {
  108. "repo": repo,
  109. "version": version,
  110. "package": package,
  111. },
  112. )
  113. @staticmethod
  114. def install_from_marketplace_pkg(
  115. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  116. ):
  117. """
  118. Install plugin from marketplace package files,
  119. returns installation task id
  120. """
  121. manager = PluginInstallationManager()
  122. # check if already downloaded
  123. for plugin_unique_identifier in plugin_unique_identifiers:
  124. try:
  125. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  126. # already downloaded, skip
  127. except Exception:
  128. # plugin not installed, download and upload pkg
  129. pkg = download_plugin_pkg(plugin_unique_identifier)
  130. manager.upload_pkg(tenant_id, pkg, verify_signature)
  131. return manager.install_from_identifiers(
  132. tenant_id,
  133. plugin_unique_identifiers,
  134. PluginInstallationSource.Marketplace,
  135. {
  136. "plugin_unique_identifier": plugin_unique_identifier,
  137. },
  138. )
  139. @staticmethod
  140. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  141. manager = PluginInstallationManager()
  142. return manager.uninstall(tenant_id, plugin_installation_id)