plugin_service.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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:
  36. manifests = {}
  37. logger.exception("failed to fetch plugin manifests")
  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_all_install_task_items(
  95. tenant_id: str,
  96. ) -> bool:
  97. """
  98. Delete all plugin installation task items
  99. """
  100. manager = PluginInstallationManager()
  101. return manager.delete_all_plugin_installation_task_items(tenant_id)
  102. @staticmethod
  103. def delete_install_task_item(tenant_id: str, task_id: str, identifier: str) -> bool:
  104. """
  105. Delete a plugin installation task item
  106. """
  107. manager = PluginInstallationManager()
  108. return manager.delete_plugin_installation_task_item(tenant_id, task_id, identifier)
  109. @staticmethod
  110. def upgrade_plugin_with_marketplace(
  111. tenant_id: str, original_plugin_unique_identifier: str, new_plugin_unique_identifier: str
  112. ):
  113. """
  114. Upgrade plugin with marketplace
  115. """
  116. if original_plugin_unique_identifier == new_plugin_unique_identifier:
  117. raise ValueError("you should not upgrade plugin with the same plugin")
  118. # check if plugin pkg is already downloaded
  119. manager = PluginInstallationManager()
  120. try:
  121. manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
  122. # already downloaded, skip
  123. except Exception:
  124. # plugin not installed, download and upload pkg
  125. pkg = download_plugin_pkg(new_plugin_unique_identifier)
  126. manager.upload_pkg(tenant_id, pkg, verify_signature=False)
  127. return manager.upgrade_plugin(
  128. tenant_id,
  129. original_plugin_unique_identifier,
  130. new_plugin_unique_identifier,
  131. PluginInstallationSource.Marketplace,
  132. {
  133. "plugin_unique_identifier": new_plugin_unique_identifier,
  134. },
  135. )
  136. @staticmethod
  137. def upgrade_plugin_with_github(
  138. tenant_id: str,
  139. original_plugin_unique_identifier: str,
  140. new_plugin_unique_identifier: str,
  141. repo: str,
  142. version: str,
  143. package: str,
  144. ):
  145. """
  146. Upgrade plugin with github
  147. """
  148. manager = PluginInstallationManager()
  149. return manager.upgrade_plugin(
  150. tenant_id,
  151. original_plugin_unique_identifier,
  152. new_plugin_unique_identifier,
  153. PluginInstallationSource.Github,
  154. {
  155. "repo": repo,
  156. "version": version,
  157. "package": package,
  158. },
  159. )
  160. @staticmethod
  161. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginUploadResponse:
  162. """
  163. Upload plugin package files
  164. returns: plugin_unique_identifier
  165. """
  166. manager = PluginInstallationManager()
  167. return manager.upload_pkg(tenant_id, pkg, verify_signature)
  168. @staticmethod
  169. def upload_pkg_from_github(
  170. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  171. ) -> PluginUploadResponse:
  172. """
  173. Install plugin from github release package files,
  174. returns plugin_unique_identifier
  175. """
  176. pkg = download_with_size_limit(
  177. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  178. )
  179. manager = PluginInstallationManager()
  180. return manager.upload_pkg(
  181. tenant_id,
  182. pkg,
  183. verify_signature,
  184. )
  185. @staticmethod
  186. def upload_bundle(
  187. tenant_id: str, bundle: bytes, verify_signature: bool = False
  188. ) -> Sequence[PluginBundleDependency]:
  189. """
  190. Upload a plugin bundle and return the dependencies.
  191. """
  192. manager = PluginInstallationManager()
  193. return manager.upload_bundle(tenant_id, bundle, verify_signature)
  194. @staticmethod
  195. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  196. manager = PluginInstallationManager()
  197. return manager.install_from_identifiers(
  198. tenant_id,
  199. plugin_unique_identifiers,
  200. PluginInstallationSource.Package,
  201. {},
  202. )
  203. @staticmethod
  204. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  205. """
  206. Install plugin from github release package files,
  207. returns plugin_unique_identifier
  208. """
  209. manager = PluginInstallationManager()
  210. return manager.install_from_identifiers(
  211. tenant_id,
  212. [plugin_unique_identifier],
  213. PluginInstallationSource.Github,
  214. {
  215. "repo": repo,
  216. "version": version,
  217. "package": package,
  218. },
  219. )
  220. @staticmethod
  221. def install_from_marketplace_pkg(
  222. tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
  223. ):
  224. """
  225. Install plugin from marketplace package files,
  226. returns installation task id
  227. """
  228. manager = PluginInstallationManager()
  229. # check if already downloaded
  230. for plugin_unique_identifier in plugin_unique_identifiers:
  231. try:
  232. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  233. # already downloaded, skip
  234. except Exception:
  235. # plugin not installed, download and upload pkg
  236. pkg = download_plugin_pkg(plugin_unique_identifier)
  237. manager.upload_pkg(tenant_id, pkg, verify_signature)
  238. return manager.install_from_identifiers(
  239. tenant_id,
  240. plugin_unique_identifiers,
  241. PluginInstallationSource.Marketplace,
  242. {
  243. "plugin_unique_identifier": plugin_unique_identifier,
  244. },
  245. )
  246. @staticmethod
  247. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  248. manager = PluginInstallationManager()
  249. return manager.uninstall(tenant_id, plugin_installation_id)