plugin_service.py 8.9 KB

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