plugin_decoder.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "errors"
  4. "io"
  5. "mime/multipart"
  6. "github.com/gin-gonic/gin"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/bundle_packager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/bundle_entities"
  13. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  14. "github.com/langgenius/dify-plugin-daemon/internal/types/exception"
  15. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache/helper"
  16. )
  17. func UploadPluginPkg(
  18. config *app.Config,
  19. c *gin.Context,
  20. tenant_id string,
  21. dify_pkg_file multipart.File,
  22. verify_signature bool,
  23. ) *entities.Response {
  24. pluginFile, err := io.ReadAll(dify_pkg_file)
  25. if err != nil {
  26. return exception.InternalServerError(err).ToResponse()
  27. }
  28. decoder, err := decoder.NewZipPluginDecoder(pluginFile)
  29. if err != nil {
  30. return exception.BadRequestError(err).ToResponse()
  31. }
  32. pluginUniqueIdentifier, err := decoder.UniqueIdentity()
  33. if err != nil {
  34. return exception.BadRequestError(err).ToResponse()
  35. }
  36. manager := plugin_manager.Manager()
  37. declaration, err := manager.SavePackage(pluginUniqueIdentifier, pluginFile)
  38. if err != nil {
  39. return exception.BadRequestError(errors.Join(err, errors.New("failed to save package"))).ToResponse()
  40. }
  41. if config.ForceVerifyingSignature || verify_signature {
  42. if !declaration.Verified {
  43. return exception.BadRequestError(errors.Join(err, errors.New(
  44. "plugin verification has been enabled, and the plugin you want to install has a bad signature",
  45. ))).ToResponse()
  46. }
  47. }
  48. return entities.NewSuccessResponse(map[string]any{
  49. "unique_identifier": pluginUniqueIdentifier,
  50. "manifest": declaration,
  51. })
  52. }
  53. func UploadPluginBundle(
  54. config *app.Config,
  55. c *gin.Context,
  56. tenant_id string,
  57. dify_bundle_file multipart.File,
  58. verify_signature bool,
  59. ) *entities.Response {
  60. bundleFile, err := io.ReadAll(dify_bundle_file)
  61. if err != nil {
  62. return exception.InternalServerError(err).ToResponse()
  63. }
  64. packager, err := bundle_packager.NewMemoryZipBundlePackager(bundleFile)
  65. if err != nil {
  66. return exception.BadRequestError(errors.Join(err, errors.New("failed to decode bundle"))).ToResponse()
  67. }
  68. // load bundle
  69. bundle, err := packager.Manifest()
  70. if err != nil {
  71. return exception.BadRequestError(errors.Join(err, errors.New("failed to load bundle manifest"))).ToResponse()
  72. }
  73. manager := plugin_manager.Manager()
  74. result := []map[string]any{}
  75. for _, dependency := range bundle.Dependencies {
  76. if dependency.Type == bundle_entities.DEPENDENCY_TYPE_GITHUB {
  77. if dep, ok := dependency.Value.(bundle_entities.GithubDependency); ok {
  78. result = append(result, map[string]any{
  79. "type": "github",
  80. "value": map[string]any{
  81. "repo_address": dep.RepoPattern.Repo(),
  82. "repo": dep.RepoPattern.GithubRepo(),
  83. "release": dep.RepoPattern.Release(),
  84. "packages": dep.RepoPattern.Asset(),
  85. },
  86. })
  87. }
  88. } else if dependency.Type == bundle_entities.DEPENDENCY_TYPE_MARKETPLACE {
  89. if dep, ok := dependency.Value.(bundle_entities.MarketplaceDependency); ok {
  90. result = append(result, map[string]any{
  91. "type": "marketplace",
  92. "value": map[string]any{
  93. "organization": dep.MarketplacePattern.Organization(),
  94. "plugin": dep.MarketplacePattern.Plugin(),
  95. "version": dep.MarketplacePattern.Version(),
  96. },
  97. })
  98. }
  99. } else if dependency.Type == bundle_entities.DEPENDENCY_TYPE_PACKAGE {
  100. if dep, ok := dependency.Value.(bundle_entities.PackageDependency); ok {
  101. // fetch package
  102. path := dep.Path
  103. if asset, err := packager.FetchAsset(path); err != nil {
  104. return exception.InternalServerError(errors.Join(errors.New("failed to fetch package from bundle"), err)).ToResponse()
  105. } else {
  106. // decode and save
  107. decoder, err := decoder.NewZipPluginDecoder(asset)
  108. if err != nil {
  109. return exception.BadRequestError(errors.Join(errors.New("failed to create package decoder"), err)).ToResponse()
  110. }
  111. pluginUniqueIdentifier, err := decoder.UniqueIdentity()
  112. if err != nil {
  113. return exception.BadRequestError(errors.Join(errors.New("failed to get package unique identifier"), err)).ToResponse()
  114. }
  115. declaration, err := manager.SavePackage(pluginUniqueIdentifier, asset)
  116. if err != nil {
  117. return exception.InternalServerError(errors.Join(errors.New("failed to save package"), err)).ToResponse()
  118. }
  119. if config.ForceVerifyingSignature || verify_signature {
  120. if !declaration.Verified {
  121. return exception.BadRequestError(errors.Join(errors.New(
  122. "plugin verification has been enabled, and the plugin you want to install has a bad signature",
  123. ), err)).ToResponse()
  124. }
  125. }
  126. result = append(result, map[string]any{
  127. "type": "package",
  128. "value": map[string]any{
  129. "unique_identifier": pluginUniqueIdentifier,
  130. "manifest": declaration,
  131. },
  132. })
  133. }
  134. }
  135. }
  136. }
  137. return entities.NewSuccessResponse(result)
  138. }
  139. func FetchPluginManifest(
  140. tenant_id string,
  141. pluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier,
  142. ) *entities.Response {
  143. pluginManifestCache, err := helper.CombinedGetPluginDeclaration(
  144. pluginUniqueIdentifier, tenant_id, plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL,
  145. )
  146. if err != nil {
  147. return exception.InternalServerError(err).ToResponse()
  148. }
  149. return entities.NewSuccessResponse(pluginManifestCache)
  150. }