plugin_service.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. from collections.abc import Sequence
  2. from mimetypes import guess_type
  3. from configs import dify_config
  4. from core.helper import marketplace
  5. from core.helper.download import download_with_size_limit
  6. from core.helper.marketplace import download_plugin_pkg
  7. from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource
  8. from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginUploadResponse
  9. from core.plugin.manager.asset import PluginAssetManager
  10. from core.plugin.manager.debugging import PluginDebuggingManager
  11. from core.plugin.manager.plugin import PluginInstallationManager
  12. class PluginService:
  13. @staticmethod
  14. def get_debugging_key(tenant_id: str) -> str:
  15. """
  16. get the debugging key of the tenant
  17. """
  18. manager = PluginDebuggingManager()
  19. return manager.get_debugging_key(tenant_id)
  20. @staticmethod
  21. def list(tenant_id: str) -> list[PluginEntity]:
  22. """
  23. list all plugins of the tenant
  24. """
  25. manager = PluginInstallationManager()
  26. plugins = manager.list_plugins(tenant_id)
  27. plugin_ids = [plugin.plugin_id for plugin in plugins if plugin.source == PluginInstallationSource.Marketplace]
  28. manifests = {manifest.plugin_id: manifest for manifest in marketplace.batch_fetch_plugin_manifests(plugin_ids)}
  29. for plugin in plugins:
  30. if plugin.source == PluginInstallationSource.Marketplace:
  31. if plugin.plugin_id in manifests:
  32. # set latest_version
  33. plugin.latest_version = manifests[plugin.plugin_id].latest_version
  34. plugin.latest_unique_identifier = manifests[plugin.plugin_id].latest_package_identifier
  35. return plugins
  36. @staticmethod
  37. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  38. """
  39. get the asset file of the plugin
  40. """
  41. manager = PluginAssetManager()
  42. # guess mime type
  43. mime_type, _ = guess_type(asset_file)
  44. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  45. @staticmethod
  46. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  47. """
  48. check if the plugin unique identifier is already installed by other tenant
  49. """
  50. manager = PluginInstallationManager()
  51. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  52. @staticmethod
  53. def fetch_plugin_manifest(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  54. """
  55. Fetch plugin manifest
  56. """
  57. manager = PluginInstallationManager()
  58. return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  59. @staticmethod
  60. def fetch_install_tasks(tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
  61. """
  62. Fetch plugin installation tasks
  63. """
  64. manager = PluginInstallationManager()
  65. return manager.fetch_plugin_installation_tasks(tenant_id, page, page_size)
  66. @staticmethod
  67. def fetch_install_task(tenant_id: str, task_id: str) -> PluginInstallTask:
  68. manager = PluginInstallationManager()
  69. return manager.fetch_plugin_installation_task(tenant_id, task_id)
  70. @staticmethod
  71. def delete_install_task(tenant_id: str, task_id: str) -> bool:
  72. """
  73. Delete a plugin installation task
  74. """
  75. manager = PluginInstallationManager()
  76. return manager.delete_plugin_installation_task(tenant_id, task_id)
  77. @staticmethod
  78. def delete_install_task_item(tenant_id: str, task_id: str, identifier: str) -> bool:
  79. """
  80. Delete a plugin installation task item
  81. """
  82. manager = PluginInstallationManager()
  83. return manager.delete_plugin_installation_task_item(tenant_id, task_id, identifier)
  84. @staticmethod
  85. def upgrade_plugin_with_marketplace(
  86. tenant_id: str, original_plugin_unique_identifier: str, new_plugin_unique_identifier: str
  87. ):
  88. """
  89. Upgrade plugin with marketplace
  90. """
  91. if original_plugin_unique_identifier == new_plugin_unique_identifier:
  92. raise ValueError("you should not upgrade plugin with the same plugin")
  93. # check if plugin pkg is already downloaded
  94. manager = PluginInstallationManager()
  95. try:
  96. manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
  97. # already downloaded, skip
  98. except Exception:
  99. # plugin not installed, download and upload pkg
  100. pkg = download_plugin_pkg(new_plugin_unique_identifier)
  101. manager.upload_pkg(tenant_id, pkg, verify_signature=True)
  102. return manager.upgrade_plugin(
  103. tenant_id,
  104. original_plugin_unique_identifier,
  105. new_plugin_unique_identifier,
  106. PluginInstallationSource.Marketplace,
  107. {
  108. "plugin_unique_identifier": new_plugin_unique_identifier,
  109. },
  110. )
  111. @staticmethod
  112. def upgrade_plugin_with_github(
  113. tenant_id: str,
  114. original_plugin_unique_identifier: str,
  115. new_plugin_unique_identifier: str,
  116. repo: str,
  117. version: str,
  118. package: str,
  119. ):
  120. """
  121. Upgrade plugin with github
  122. """
  123. manager = PluginInstallationManager()
  124. return manager.upgrade_plugin(
  125. tenant_id,
  126. original_plugin_unique_identifier,
  127. new_plugin_unique_identifier,
  128. PluginInstallationSource.Github,
  129. {
  130. "repo": repo,
  131. "version": version,
  132. "package": package,
  133. },
  134. )
  135. @staticmethod
  136. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginUploadResponse:
  137. """
  138. Upload plugin package files
  139. returns: plugin_unique_identifier
  140. """
  141. manager = PluginInstallationManager()
  142. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  143. @staticmethod
  144. def upload_pkg_from_github(
  145. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  146. ) -> PluginUploadResponse:
  147. """
  148. Install plugin from github release package files,
  149. returns plugin_unique_identifier
  150. """
  151. pkg = download_with_size_limit(
  152. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  153. )
  154. manager = PluginInstallationManager()
  155. return manager.upload_pkg(
  156. tenant_id,
  157. pkg,
  158. verify_signature,
  159. )
  160. @staticmethod
  161. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  162. manager = PluginInstallationManager()
  163. return manager.install_from_identifiers(
  164. tenant_id,
  165. plugin_unique_identifiers,
  166. PluginInstallationSource.Package,
  167. {},
  168. )
  169. @staticmethod
  170. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  171. """
  172. Install plugin from github release package files,
  173. returns plugin_unique_identifier
  174. """
  175. manager = PluginInstallationManager()
  176. return manager.install_from_identifiers(
  177. tenant_id,
  178. [plugin_unique_identifier],
  179. PluginInstallationSource.Github,
  180. {
  181. "repo": repo,
  182. "version": version,
  183. "package": package,
  184. },
  185. )
  186. @staticmethod
  187. def install_from_marketplace_pkg(
  188. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  189. ):
  190. """
  191. Install plugin from marketplace package files,
  192. returns installation task id
  193. """
  194. manager = PluginInstallationManager()
  195. # check if already downloaded
  196. for plugin_unique_identifier in plugin_unique_identifiers:
  197. try:
  198. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  199. # already downloaded, skip
  200. except Exception:
  201. # plugin not installed, download and upload pkg
  202. pkg = download_plugin_pkg(plugin_unique_identifier)
  203. manager.upload_pkg(tenant_id, pkg, verify_signature)
  204. return manager.install_from_identifiers(
  205. tenant_id,
  206. plugin_unique_identifiers,
  207. PluginInstallationSource.Marketplace,
  208. {
  209. "plugin_unique_identifier": plugin_unique_identifier,
  210. },
  211. )
  212. @staticmethod
  213. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  214. manager = PluginInstallationManager()
  215. return manager.uninstall(tenant_id, plugin_installation_id)