plugin_service.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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, and record install event
  129. marketplace.record_install_plugin_event(new_plugin_unique_identifier)
  130. except Exception:
  131. # plugin not installed, download and upload pkg
  132. pkg = download_plugin_pkg(new_plugin_unique_identifier)
  133. manager.upload_pkg(tenant_id, pkg, verify_signature=False)
  134. return manager.upgrade_plugin(
  135. tenant_id,
  136. original_plugin_unique_identifier,
  137. new_plugin_unique_identifier,
  138. PluginInstallationSource.Marketplace,
  139. {
  140. "plugin_unique_identifier": new_plugin_unique_identifier,
  141. },
  142. )
  143. @staticmethod
  144. def upgrade_plugin_with_github(
  145. tenant_id: str,
  146. original_plugin_unique_identifier: str,
  147. new_plugin_unique_identifier: str,
  148. repo: str,
  149. version: str,
  150. package: str,
  151. ):
  152. """
  153. Upgrade plugin with github
  154. """
  155. manager = PluginInstallationManager()
  156. return manager.upgrade_plugin(
  157. tenant_id,
  158. original_plugin_unique_identifier,
  159. new_plugin_unique_identifier,
  160. PluginInstallationSource.Github,
  161. {
  162. "repo": repo,
  163. "version": version,
  164. "package": package,
  165. },
  166. )
  167. @staticmethod
  168. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginUploadResponse:
  169. """
  170. Upload plugin package files
  171. returns: plugin_unique_identifier
  172. """
  173. manager = PluginInstallationManager()
  174. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  175. @staticmethod
  176. def upload_pkg_from_github(
  177. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  178. ) -> PluginUploadResponse:
  179. """
  180. Install plugin from github release package files,
  181. returns plugin_unique_identifier
  182. """
  183. pkg = download_with_size_limit(
  184. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  185. )
  186. manager = PluginInstallationManager()
  187. return manager.upload_pkg(
  188. tenant_id,
  189. pkg,
  190. verify_signature,
  191. )
  192. @staticmethod
  193. def upload_bundle(
  194. tenant_id: str, bundle: bytes, verify_signature: bool = False
  195. ) -> Sequence[PluginBundleDependency]:
  196. """
  197. Upload a plugin bundle and return the dependencies.
  198. """
  199. manager = PluginInstallationManager()
  200. return manager.upload_bundle(tenant_id, bundle, verify_signature)
  201. @staticmethod
  202. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  203. manager = PluginInstallationManager()
  204. return manager.install_from_identifiers(
  205. tenant_id,
  206. plugin_unique_identifiers,
  207. PluginInstallationSource.Package,
  208. [{}],
  209. )
  210. @staticmethod
  211. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  212. """
  213. Install plugin from github release package files,
  214. returns plugin_unique_identifier
  215. """
  216. manager = PluginInstallationManager()
  217. return manager.install_from_identifiers(
  218. tenant_id,
  219. [plugin_unique_identifier],
  220. PluginInstallationSource.Github,
  221. [
  222. {
  223. "repo": repo,
  224. "version": version,
  225. "package": package,
  226. }
  227. ],
  228. )
  229. @staticmethod
  230. def install_from_marketplace_pkg(
  231. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  232. ):
  233. """
  234. Install plugin from marketplace package files,
  235. returns installation task id
  236. """
  237. manager = PluginInstallationManager()
  238. # check if already downloaded
  239. for plugin_unique_identifier in plugin_unique_identifiers:
  240. try:
  241. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  242. # already downloaded, skip
  243. except Exception:
  244. # plugin not installed, download and upload pkg
  245. pkg = download_plugin_pkg(plugin_unique_identifier)
  246. manager.upload_pkg(tenant_id, pkg, verify_signature)
  247. return manager.install_from_identifiers(
  248. tenant_id,
  249. plugin_unique_identifiers,
  250. PluginInstallationSource.Marketplace,
  251. [
  252. {
  253. "plugin_unique_identifier": plugin_unique_identifier,
  254. }
  255. for plugin_unique_identifier in plugin_unique_identifiers
  256. ],
  257. )
  258. @staticmethod
  259. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  260. manager = PluginInstallationManager()
  261. return manager.uninstall(tenant_id, plugin_installation_id)
  262. @staticmethod
  263. def check_tools_existence(tenant_id: str, provider_ids: Sequence[GenericProviderID]) -> Sequence[bool]:
  264. """
  265. Check if the tools exist
  266. """
  267. manager = PluginInstallationManager()
  268. return manager.check_tools_existence(tenant_id, provider_ids)