plugin_service.py 10 KB

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