plugin_service.py 9.4 KB

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