plugin_service.py 9.7 KB

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