Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

plugin_service.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import logging
  2. from collections.abc import Mapping, Sequence
  3. from mimetypes import guess_type
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from configs import dify_config
  7. from core.helper import marketplace
  8. from core.helper.download import download_with_size_limit
  9. from core.helper.marketplace import download_plugin_pkg
  10. from core.plugin.entities.bundle import PluginBundleDependency
  11. from core.plugin.entities.plugin import (
  12. GenericProviderID,
  13. PluginDeclaration,
  14. PluginEntity,
  15. PluginInstallation,
  16. PluginInstallationSource,
  17. )
  18. from core.plugin.entities.plugin_daemon import (
  19. PluginDecodeResponse,
  20. PluginInstallTask,
  21. PluginListResponse,
  22. PluginVerification,
  23. )
  24. from core.plugin.impl.asset import PluginAssetManager
  25. from core.plugin.impl.debugging import PluginDebuggingClient
  26. from core.plugin.impl.plugin import PluginInstaller
  27. from extensions.ext_redis import redis_client
  28. from services.errors.plugin import PluginInstallationForbiddenError
  29. from services.feature_service import FeatureService, PluginInstallationScope
  30. logger = logging.getLogger(__name__)
  31. class PluginService:
  32. class LatestPluginCache(BaseModel):
  33. plugin_id: str
  34. version: str
  35. unique_identifier: str
  36. REDIS_KEY_PREFIX = "plugin_service:latest_plugin:"
  37. REDIS_TTL = 60 * 5 # 5 minutes
  38. @staticmethod
  39. def fetch_latest_plugin_version(plugin_ids: Sequence[str]) -> Mapping[str, Optional[LatestPluginCache]]:
  40. """
  41. Fetch the latest plugin version
  42. """
  43. result: dict[str, Optional[PluginService.LatestPluginCache]] = {}
  44. try:
  45. cache_not_exists = []
  46. # Try to get from Redis first
  47. for plugin_id in plugin_ids:
  48. cached_data = redis_client.get(f"{PluginService.REDIS_KEY_PREFIX}{plugin_id}")
  49. if cached_data:
  50. result[plugin_id] = PluginService.LatestPluginCache.model_validate_json(cached_data)
  51. else:
  52. cache_not_exists.append(plugin_id)
  53. if cache_not_exists:
  54. manifests = {
  55. manifest.plugin_id: manifest
  56. for manifest in marketplace.batch_fetch_plugin_manifests(cache_not_exists)
  57. }
  58. for plugin_id, manifest in manifests.items():
  59. latest_plugin = PluginService.LatestPluginCache(
  60. plugin_id=plugin_id,
  61. version=manifest.latest_version,
  62. unique_identifier=manifest.latest_package_identifier,
  63. )
  64. # Store in Redis
  65. redis_client.setex(
  66. f"{PluginService.REDIS_KEY_PREFIX}{plugin_id}",
  67. PluginService.REDIS_TTL,
  68. latest_plugin.model_dump_json(),
  69. )
  70. result[plugin_id] = latest_plugin
  71. # pop plugin_id from cache_not_exists
  72. cache_not_exists.remove(plugin_id)
  73. for plugin_id in cache_not_exists:
  74. result[plugin_id] = None
  75. return result
  76. except Exception:
  77. logger.exception("failed to fetch latest plugin version")
  78. return result
  79. @staticmethod
  80. def _check_marketplace_only_permission():
  81. """
  82. Check if the marketplace only permission is enabled
  83. """
  84. features = FeatureService.get_system_features()
  85. if features.plugin_installation_permission.restrict_to_marketplace_only:
  86. raise PluginInstallationForbiddenError("Plugin installation is restricted to marketplace only")
  87. @staticmethod
  88. def _check_plugin_installation_scope(plugin_verification: Optional[PluginVerification]):
  89. """
  90. Check the plugin installation scope
  91. """
  92. features = FeatureService.get_system_features()
  93. match features.plugin_installation_permission.plugin_installation_scope:
  94. case PluginInstallationScope.OFFICIAL_ONLY:
  95. if (
  96. plugin_verification is None
  97. or plugin_verification.authorized_category != PluginVerification.AuthorizedCategory.Langgenius
  98. ):
  99. raise PluginInstallationForbiddenError("Plugin installation is restricted to official only")
  100. case PluginInstallationScope.OFFICIAL_AND_SPECIFIC_PARTNERS:
  101. if plugin_verification is None or plugin_verification.authorized_category not in [
  102. PluginVerification.AuthorizedCategory.Langgenius,
  103. PluginVerification.AuthorizedCategory.Partner,
  104. ]:
  105. raise PluginInstallationForbiddenError(
  106. "Plugin installation is restricted to official and specific partners"
  107. )
  108. case PluginInstallationScope.NONE:
  109. raise PluginInstallationForbiddenError("Installing plugins is not allowed")
  110. case PluginInstallationScope.ALL:
  111. pass
  112. @staticmethod
  113. def get_debugging_key(tenant_id: str) -> str:
  114. """
  115. get the debugging key of the tenant
  116. """
  117. manager = PluginDebuggingClient()
  118. return manager.get_debugging_key(tenant_id)
  119. @staticmethod
  120. def list_latest_versions(plugin_ids: Sequence[str]) -> Mapping[str, Optional[LatestPluginCache]]:
  121. """
  122. List the latest versions of the plugins
  123. """
  124. return PluginService.fetch_latest_plugin_version(plugin_ids)
  125. @staticmethod
  126. def list(tenant_id: str) -> list[PluginEntity]:
  127. """
  128. list all plugins of the tenant
  129. """
  130. manager = PluginInstaller()
  131. plugins = manager.list_plugins(tenant_id)
  132. return plugins
  133. @staticmethod
  134. def list_with_total(tenant_id: str, page: int, page_size: int) -> PluginListResponse:
  135. """
  136. list all plugins of the tenant
  137. """
  138. manager = PluginInstaller()
  139. plugins = manager.list_plugins_with_total(tenant_id, page, page_size)
  140. return plugins
  141. @staticmethod
  142. def list_installations_from_ids(tenant_id: str, ids: Sequence[str]) -> Sequence[PluginInstallation]:
  143. """
  144. List plugin installations from ids
  145. """
  146. manager = PluginInstaller()
  147. return manager.fetch_plugin_installation_by_ids(tenant_id, ids)
  148. @staticmethod
  149. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  150. """
  151. get the asset file of the plugin
  152. """
  153. manager = PluginAssetManager()
  154. # guess mime type
  155. mime_type, _ = guess_type(asset_file)
  156. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  157. @staticmethod
  158. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  159. """
  160. check if the plugin unique identifier is already installed by other tenant
  161. """
  162. manager = PluginInstaller()
  163. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  164. @staticmethod
  165. def fetch_plugin_manifest(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  166. """
  167. Fetch plugin manifest
  168. """
  169. manager = PluginInstaller()
  170. return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  171. @staticmethod
  172. def is_plugin_verified(tenant_id: str, plugin_unique_identifier: str) -> bool:
  173. """
  174. Check if the plugin is verified
  175. """
  176. manager = PluginInstaller()
  177. try:
  178. return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier).verified
  179. except Exception:
  180. return False
  181. @staticmethod
  182. def fetch_install_tasks(tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
  183. """
  184. Fetch plugin installation tasks
  185. """
  186. manager = PluginInstaller()
  187. return manager.fetch_plugin_installation_tasks(tenant_id, page, page_size)
  188. @staticmethod
  189. def fetch_install_task(tenant_id: str, task_id: str) -> PluginInstallTask:
  190. manager = PluginInstaller()
  191. return manager.fetch_plugin_installation_task(tenant_id, task_id)
  192. @staticmethod
  193. def delete_install_task(tenant_id: str, task_id: str) -> bool:
  194. """
  195. Delete a plugin installation task
  196. """
  197. manager = PluginInstaller()
  198. return manager.delete_plugin_installation_task(tenant_id, task_id)
  199. @staticmethod
  200. def delete_all_install_task_items(
  201. tenant_id: str,
  202. ) -> bool:
  203. """
  204. Delete all plugin installation task items
  205. """
  206. manager = PluginInstaller()
  207. return manager.delete_all_plugin_installation_task_items(tenant_id)
  208. @staticmethod
  209. def delete_install_task_item(tenant_id: str, task_id: str, identifier: str) -> bool:
  210. """
  211. Delete a plugin installation task item
  212. """
  213. manager = PluginInstaller()
  214. return manager.delete_plugin_installation_task_item(tenant_id, task_id, identifier)
  215. @staticmethod
  216. def upgrade_plugin_with_marketplace(
  217. tenant_id: str, original_plugin_unique_identifier: str, new_plugin_unique_identifier: str
  218. ):
  219. """
  220. Upgrade plugin with marketplace
  221. """
  222. if not dify_config.MARKETPLACE_ENABLED:
  223. raise ValueError("marketplace is not enabled")
  224. if original_plugin_unique_identifier == new_plugin_unique_identifier:
  225. raise ValueError("you should not upgrade plugin with the same plugin")
  226. # check if plugin pkg is already downloaded
  227. manager = PluginInstaller()
  228. features = FeatureService.get_system_features()
  229. try:
  230. manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
  231. # already downloaded, skip, and record install event
  232. marketplace.record_install_plugin_event(new_plugin_unique_identifier)
  233. except Exception:
  234. # plugin not installed, download and upload pkg
  235. pkg = download_plugin_pkg(new_plugin_unique_identifier)
  236. response = manager.upload_pkg(
  237. tenant_id,
  238. pkg,
  239. verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
  240. )
  241. # check if the plugin is available to install
  242. PluginService._check_plugin_installation_scope(response.verification)
  243. return manager.upgrade_plugin(
  244. tenant_id,
  245. original_plugin_unique_identifier,
  246. new_plugin_unique_identifier,
  247. PluginInstallationSource.Marketplace,
  248. {
  249. "plugin_unique_identifier": new_plugin_unique_identifier,
  250. },
  251. )
  252. @staticmethod
  253. def upgrade_plugin_with_github(
  254. tenant_id: str,
  255. original_plugin_unique_identifier: str,
  256. new_plugin_unique_identifier: str,
  257. repo: str,
  258. version: str,
  259. package: str,
  260. ):
  261. """
  262. Upgrade plugin with github
  263. """
  264. PluginService._check_marketplace_only_permission()
  265. manager = PluginInstaller()
  266. return manager.upgrade_plugin(
  267. tenant_id,
  268. original_plugin_unique_identifier,
  269. new_plugin_unique_identifier,
  270. PluginInstallationSource.Github,
  271. {
  272. "repo": repo,
  273. "version": version,
  274. "package": package,
  275. },
  276. )
  277. @staticmethod
  278. def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginDecodeResponse:
  279. """
  280. Upload plugin package files
  281. returns: plugin_unique_identifier
  282. """
  283. PluginService._check_marketplace_only_permission()
  284. manager = PluginInstaller()
  285. features = FeatureService.get_system_features()
  286. response = manager.upload_pkg(
  287. tenant_id,
  288. pkg,
  289. verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
  290. )
  291. return response
  292. @staticmethod
  293. def upload_pkg_from_github(
  294. tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
  295. ) -> PluginDecodeResponse:
  296. """
  297. Install plugin from github release package files,
  298. returns plugin_unique_identifier
  299. """
  300. PluginService._check_marketplace_only_permission()
  301. pkg = download_with_size_limit(
  302. f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
  303. )
  304. features = FeatureService.get_system_features()
  305. manager = PluginInstaller()
  306. response = manager.upload_pkg(
  307. tenant_id,
  308. pkg,
  309. verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
  310. )
  311. return response
  312. @staticmethod
  313. def upload_bundle(
  314. tenant_id: str, bundle: bytes, verify_signature: bool = False
  315. ) -> Sequence[PluginBundleDependency]:
  316. """
  317. Upload a plugin bundle and return the dependencies.
  318. """
  319. manager = PluginInstaller()
  320. PluginService._check_marketplace_only_permission()
  321. return manager.upload_bundle(tenant_id, bundle, verify_signature)
  322. @staticmethod
  323. def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  324. PluginService._check_marketplace_only_permission()
  325. manager = PluginInstaller()
  326. return manager.install_from_identifiers(
  327. tenant_id,
  328. plugin_unique_identifiers,
  329. PluginInstallationSource.Package,
  330. [{}],
  331. )
  332. @staticmethod
  333. def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
  334. """
  335. Install plugin from github release package files,
  336. returns plugin_unique_identifier
  337. """
  338. PluginService._check_marketplace_only_permission()
  339. manager = PluginInstaller()
  340. return manager.install_from_identifiers(
  341. tenant_id,
  342. [plugin_unique_identifier],
  343. PluginInstallationSource.Github,
  344. [
  345. {
  346. "repo": repo,
  347. "version": version,
  348. "package": package,
  349. }
  350. ],
  351. )
  352. @staticmethod
  353. def fetch_marketplace_pkg(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  354. """
  355. Fetch marketplace package
  356. """
  357. if not dify_config.MARKETPLACE_ENABLED:
  358. raise ValueError("marketplace is not enabled")
  359. features = FeatureService.get_system_features()
  360. manager = PluginInstaller()
  361. try:
  362. declaration = manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  363. except Exception:
  364. pkg = download_plugin_pkg(plugin_unique_identifier)
  365. response = manager.upload_pkg(
  366. tenant_id,
  367. pkg,
  368. verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
  369. )
  370. # check if the plugin is available to install
  371. PluginService._check_plugin_installation_scope(response.verification)
  372. declaration = response.manifest
  373. return declaration
  374. @staticmethod
  375. def install_from_marketplace_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
  376. """
  377. Install plugin from marketplace package files,
  378. returns installation task id
  379. """
  380. if not dify_config.MARKETPLACE_ENABLED:
  381. raise ValueError("marketplace is not enabled")
  382. manager = PluginInstaller()
  383. # collect actual plugin_unique_identifiers
  384. actual_plugin_unique_identifiers = []
  385. metas = []
  386. features = FeatureService.get_system_features()
  387. # check if already downloaded
  388. for plugin_unique_identifier in plugin_unique_identifiers:
  389. try:
  390. manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
  391. plugin_decode_response = manager.decode_plugin_from_identifier(tenant_id, plugin_unique_identifier)
  392. # check if the plugin is available to install
  393. PluginService._check_plugin_installation_scope(plugin_decode_response.verification)
  394. # already downloaded, skip
  395. actual_plugin_unique_identifiers.append(plugin_unique_identifier)
  396. metas.append({"plugin_unique_identifier": plugin_unique_identifier})
  397. except Exception:
  398. # plugin not installed, download and upload pkg
  399. pkg = download_plugin_pkg(plugin_unique_identifier)
  400. response = manager.upload_pkg(
  401. tenant_id,
  402. pkg,
  403. verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
  404. )
  405. # check if the plugin is available to install
  406. PluginService._check_plugin_installation_scope(response.verification)
  407. # use response plugin_unique_identifier
  408. actual_plugin_unique_identifiers.append(response.unique_identifier)
  409. metas.append({"plugin_unique_identifier": response.unique_identifier})
  410. return manager.install_from_identifiers(
  411. tenant_id,
  412. actual_plugin_unique_identifiers,
  413. PluginInstallationSource.Marketplace,
  414. metas,
  415. )
  416. @staticmethod
  417. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  418. manager = PluginInstaller()
  419. return manager.uninstall(tenant_id, plugin_installation_id)
  420. @staticmethod
  421. def check_tools_existence(tenant_id: str, provider_ids: Sequence[GenericProviderID]) -> Sequence[bool]:
  422. """
  423. Check if the tools exist
  424. """
  425. manager = PluginInstaller()
  426. return manager.check_tools_existence(tenant_id, provider_ids)