install_plugin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "fmt"
  4. "io"
  5. "mime/multipart"
  6. "github.com/gin-gonic/gin"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  9. "github.com/langgenius/dify-plugin-daemon/internal/db"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  13. "github.com/langgenius/dify-plugin-daemon/internal/types/models/curd"
  14. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  15. )
  16. func InstallPluginFromPkg(c *gin.Context, tenant_id string, dify_pkg_file multipart.File) {
  17. manager := plugin_manager.Manager()
  18. plugin_file, err := io.ReadAll(dify_pkg_file)
  19. if err != nil {
  20. c.JSON(200, entities.NewErrorResponse(-500, err.Error()))
  21. return
  22. }
  23. decoder, err := decoder.NewZipPluginDecoder(plugin_file)
  24. if err != nil {
  25. c.JSON(200, entities.NewErrorResponse(-500, err.Error()))
  26. return
  27. }
  28. baseSSEService(
  29. func() (*stream.Stream[plugin_manager.PluginInstallResponse], error) {
  30. return manager.Install(tenant_id, decoder)
  31. },
  32. c,
  33. 3600,
  34. )
  35. }
  36. func InstallPluginFromIdentifier(
  37. tenant_id string,
  38. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  39. ) *entities.Response {
  40. // check if identifier exists
  41. plugin, err := db.GetOne[models.Plugin](
  42. db.Equal("plugin_unique_identifier", plugin_unique_identifier.String()),
  43. )
  44. if err == db.ErrDatabaseNotFound {
  45. return entities.NewErrorResponse(-404, "Plugin not found")
  46. }
  47. if err != nil {
  48. return entities.NewErrorResponse(-500, err.Error())
  49. }
  50. if plugin.InstallType == plugin_entities.PLUGIN_RUNTIME_TYPE_REMOTE {
  51. return entities.NewErrorResponse(-500, "remote plugin not supported")
  52. }
  53. declaration := plugin.Declaration
  54. // install to this workspace
  55. if _, _, err := curd.InstallPlugin(tenant_id, plugin_unique_identifier, plugin.InstallType, &declaration); err != nil {
  56. return entities.NewErrorResponse(-500, err.Error())
  57. }
  58. return entities.NewSuccessResponse(true)
  59. }
  60. func FetchPluginFromIdentifier(
  61. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  62. ) *entities.Response {
  63. _, err := db.GetOne[models.Plugin](
  64. db.Equal("plugin_unique_identifier", plugin_unique_identifier.String()),
  65. )
  66. if err == db.ErrDatabaseNotFound {
  67. return entities.NewSuccessResponse(false)
  68. }
  69. if err != nil {
  70. return entities.NewErrorResponse(-500, err.Error())
  71. }
  72. return entities.NewSuccessResponse(true)
  73. }
  74. func UninstallPlugin(
  75. tenant_id string,
  76. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  77. ) *entities.Response {
  78. // Check if the plugin exists for the tenant
  79. installation, err := db.GetOne[models.PluginInstallation](
  80. db.Equal("tenant_id", tenant_id),
  81. db.Equal("plugin_unique_identifier", plugin_unique_identifier.String()),
  82. )
  83. if err == db.ErrDatabaseNotFound {
  84. return entities.NewErrorResponse(-404, "Plugin not found for this tenant")
  85. }
  86. if err != nil {
  87. return entities.NewErrorResponse(-500, err.Error())
  88. }
  89. // Uninstall the plugin
  90. _, err = curd.UninstallPlugin(tenant_id, plugin_unique_identifier, installation.ID)
  91. if err != nil {
  92. return entities.NewErrorResponse(-500, fmt.Sprintf("Failed to uninstall plugin: %s", err.Error()))
  93. }
  94. return entities.NewSuccessResponse(true)
  95. }