plugin_service.py 8.6 KB

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