install_plugin.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "io"
  4. "mime/multipart"
  5. "github.com/gin-gonic/gin"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  8. "github.com/langgenius/dify-plugin-daemon/internal/db"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  12. "github.com/langgenius/dify-plugin-daemon/internal/types/models/curd"
  13. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  14. )
  15. func InstallPluginFromPkg(c *gin.Context, tenant_id string, dify_pkg_file multipart.File) {
  16. manager := plugin_manager.Manager()
  17. plugin_file, err := io.ReadAll(dify_pkg_file)
  18. if err != nil {
  19. c.JSON(200, entities.NewErrorResponse(-500, err.Error()))
  20. return
  21. }
  22. decoder, err := decoder.NewZipPluginDecoder(plugin_file)
  23. if err != nil {
  24. c.JSON(200, entities.NewErrorResponse(-500, err.Error()))
  25. return
  26. }
  27. baseSSEService(
  28. func() (*stream.Stream[plugin_manager.PluginInstallResponse], error) {
  29. return manager.Install(tenant_id, decoder)
  30. },
  31. c,
  32. 3600,
  33. )
  34. }
  35. func InstallPluginFromIdentifier(
  36. c *gin.Context,
  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. declaration := plugin.Declaration
  51. // install to this workspace
  52. if _, _, err := curd.CreatePlugin(tenant_id, plugin_unique_identifier, plugin.InstallType, &declaration); err != nil {
  53. return entities.NewErrorResponse(-500, err.Error())
  54. }
  55. return entities.NewSuccessResponse(plugin)
  56. }