plugin.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from collections.abc import Sequence
  2. from pydantic import BaseModel
  3. from core.plugin.entities.plugin import (
  4. PluginDeclaration,
  5. PluginEntity,
  6. PluginInstallation,
  7. PluginInstallationSource,
  8. )
  9. from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginInstallTaskStartResponse, PluginUploadResponse
  10. from core.plugin.manager.base import BasePluginManager
  11. class PluginInstallationManager(BasePluginManager):
  12. def fetch_plugin_by_identifier(
  13. self,
  14. tenant_id: str,
  15. identifier: str,
  16. ) -> bool:
  17. return self._request_with_plugin_daemon_response(
  18. "GET",
  19. f"plugin/{tenant_id}/management/fetch/identifier",
  20. bool,
  21. params={"plugin_unique_identifier": identifier},
  22. )
  23. def list_plugins(self, tenant_id: str) -> list[PluginEntity]:
  24. return self._request_with_plugin_daemon_response(
  25. "GET",
  26. f"plugin/{tenant_id}/management/list",
  27. list[PluginEntity],
  28. params={"page": 1, "page_size": 256},
  29. )
  30. def upload_pkg(
  31. self,
  32. tenant_id: str,
  33. pkg: bytes,
  34. verify_signature: bool = False,
  35. ) -> PluginUploadResponse:
  36. """
  37. Upload a plugin package and return the plugin unique identifier.
  38. """
  39. body = {
  40. "dify_pkg": ("dify_pkg", pkg, "application/octet-stream"),
  41. }
  42. data = {
  43. "verify_signature": "true" if verify_signature else "false",
  44. }
  45. return self._request_with_plugin_daemon_response(
  46. "POST",
  47. f"plugin/{tenant_id}/management/install/upload",
  48. PluginUploadResponse,
  49. files=body,
  50. data=data,
  51. )
  52. def install_from_identifiers(
  53. self, tenant_id: str, identifiers: Sequence[str], source: PluginInstallationSource, meta: dict
  54. ) -> PluginInstallTaskStartResponse:
  55. """
  56. Install a plugin from an identifier.
  57. """
  58. # exception will be raised if the request failed
  59. return self._request_with_plugin_daemon_response(
  60. "POST",
  61. f"plugin/{tenant_id}/management/install/identifiers",
  62. PluginInstallTaskStartResponse,
  63. data={
  64. "plugin_unique_identifiers": identifiers,
  65. "source": source,
  66. "meta": meta,
  67. },
  68. headers={"Content-Type": "application/json"},
  69. )
  70. def fetch_plugin_installation_tasks(self, tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
  71. """
  72. Fetch plugin installation tasks.
  73. """
  74. return self._request_with_plugin_daemon_response(
  75. "GET",
  76. f"plugin/{tenant_id}/management/install/tasks",
  77. list[PluginInstallTask],
  78. params={"page": page, "page_size": page_size},
  79. )
  80. def fetch_plugin_installation_task(self, tenant_id: str, task_id: str) -> PluginInstallTask:
  81. """
  82. Fetch a plugin installation task.
  83. """
  84. return self._request_with_plugin_daemon_response(
  85. "GET",
  86. f"plugin/{tenant_id}/management/install/tasks/{task_id}",
  87. PluginInstallTask,
  88. )
  89. def delete_plugin_installation_task(self, tenant_id: str, task_id: str) -> bool:
  90. """
  91. Delete a plugin installation task.
  92. """
  93. return self._request_with_plugin_daemon_response(
  94. "POST",
  95. f"plugin/{tenant_id}/management/install/tasks/{task_id}/delete",
  96. bool,
  97. )
  98. def delete_plugin_installation_task_item(self, tenant_id: str, task_id: str, identifier: str) -> bool:
  99. """
  100. Delete a plugin installation task item.
  101. """
  102. return self._request_with_plugin_daemon_response(
  103. "POST",
  104. f"plugin/{tenant_id}/management/install/tasks/{task_id}/delete/{identifier}",
  105. bool,
  106. )
  107. def fetch_plugin_manifest(self, tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
  108. """
  109. Fetch a plugin manifest.
  110. """
  111. class PluginDeclarationResponse(BaseModel):
  112. declaration: PluginDeclaration
  113. return self._request_with_plugin_daemon_response(
  114. "GET",
  115. f"plugin/{tenant_id}/management/fetch/manifest",
  116. PluginDeclarationResponse,
  117. params={"plugin_unique_identifier": plugin_unique_identifier},
  118. ).declaration
  119. def fetch_plugin_installation_by_ids(self, tenant_id: str, plugin_ids: Sequence[str]) -> list[PluginInstallation]:
  120. """
  121. Fetch plugin installations by ids.
  122. """
  123. return self._request_with_plugin_daemon_response(
  124. "POST",
  125. f"plugin/{tenant_id}/management/installation/fetch/batch",
  126. list[PluginInstallation],
  127. data={"plugin_ids": plugin_ids},
  128. headers={"Content-Type": "application/json"},
  129. )
  130. def fetch_missing_dependencies(self, tenant_id: str, plugin_unique_identifiers: list[str]) -> list[str]:
  131. """
  132. Fetch missing dependencies
  133. """
  134. return self._request_with_plugin_daemon_response(
  135. "POST",
  136. f"plugin/{tenant_id}/management/installation/missing",
  137. list[str],
  138. data={"plugin_unique_identifiers": plugin_unique_identifiers},
  139. headers={"Content-Type": "application/json"},
  140. )
  141. def uninstall(self, tenant_id: str, plugin_installation_id: str) -> bool:
  142. """
  143. Uninstall a plugin.
  144. """
  145. return self._request_with_plugin_daemon_response(
  146. "POST",
  147. f"plugin/{tenant_id}/management/uninstall",
  148. bool,
  149. data={
  150. "plugin_installation_id": plugin_installation_id,
  151. },
  152. headers={"Content-Type": "application/json"},
  153. )
  154. def upgrade_plugin(
  155. self,
  156. tenant_id: str,
  157. original_plugin_unique_identifier: str,
  158. new_plugin_unique_identifier: str,
  159. source: PluginInstallationSource,
  160. meta: dict,
  161. ) -> PluginInstallTaskStartResponse:
  162. """
  163. Upgrade a plugin.
  164. """
  165. return self._request_with_plugin_daemon_response(
  166. "POST",
  167. f"plugin/{tenant_id}/management/install/upgrade",
  168. PluginInstallTaskStartResponse,
  169. data={
  170. "original_plugin_unique_identifier": original_plugin_unique_identifier,
  171. "new_plugin_unique_identifier": new_plugin_unique_identifier,
  172. "source": source,
  173. "meta": meta,
  174. },
  175. headers={"Content-Type": "application/json"},
  176. )