plugin_decoder.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // avoid author to be a uuid
  37. if pluginUniqueIdentifier.RemoteLike() {
  38. return exception.BadRequestError(errors.New("author cannot be a uuid")).ToResponse()
  39. }
  40. manager := plugin_manager.Manager()
  41. declaration, err := manager.SavePackage(pluginUniqueIdentifier, pluginFile)
  42. if err != nil {
  43. return exception.BadRequestError(errors.Join(err, errors.New("failed to save package"))).ToResponse()
  44. }
  45. if config.ForceVerifyingSignature || verify_signature {
  46. if !declaration.Verified {
  47. return exception.BadRequestError(errors.Join(err, errors.New(
  48. "plugin verification has been enabled, and the plugin you want to install has a bad signature",
  49. ))).ToResponse()
  50. }
  51. }
  52. return entities.NewSuccessResponse(map[string]any{
  53. "unique_identifier": pluginUniqueIdentifier,
  54. "manifest": declaration,
  55. })
  56. }
  57. func UploadPluginBundle(
  58. config *app.Config,
  59. c *gin.Context,
  60. tenant_id string,
  61. dify_bundle_file multipart.File,
  62. verify_signature bool,
  63. ) *entities.Response {
  64. bundleFile, err := io.ReadAll(dify_bundle_file)
  65. if err != nil {
  66. return exception.InternalServerError(err).ToResponse()
  67. }
  68. packager, err := bundle_packager.NewMemoryZipBundlePackager(bundleFile)
  69. if err != nil {
  70. return exception.BadRequestError(errors.Join(err, errors.New("failed to decode bundle"))).ToResponse()
  71. }
  72. // load bundle
  73. bundle, err := packager.Manifest()
  74. if err != nil {
  75. return exception.BadRequestError(errors.Join(err, errors.New("failed to load bundle manifest"))).ToResponse()
  76. }
  77. manager := plugin_manager.Manager()
  78. result := []map[string]any{}
  79. for _, dependency := range bundle.Dependencies {
  80. if dependency.Type == bundle_entities.DEPENDENCY_TYPE_GITHUB {
  81. if dep, ok := dependency.Value.(bundle_entities.GithubDependency); ok {
  82. result = append(result, map[string]any{
  83. "type": "github",
  84. "value": map[string]any{
  85. "repo_address": dep.RepoPattern.Repo(),
  86. "repo": dep.RepoPattern.GithubRepo(),
  87. "release": dep.RepoPattern.Release(),
  88. "packages": dep.RepoPattern.Asset(),
  89. },
  90. })
  91. }
  92. } else if dependency.Type == bundle_entities.DEPENDENCY_TYPE_MARKETPLACE {
  93. if dep, ok := dependency.Value.(bundle_entities.MarketplaceDependency); ok {
  94. result = append(result, map[string]any{
  95. "type": "marketplace",
  96. "value": map[string]any{
  97. "organization": dep.MarketplacePattern.Organization(),
  98. "plugin": dep.MarketplacePattern.Plugin(),
  99. "version": dep.MarketplacePattern.Version(),
  100. },
  101. })
  102. }
  103. } else if dependency.Type == bundle_entities.DEPENDENCY_TYPE_PACKAGE {
  104. if dep, ok := dependency.Value.(bundle_entities.PackageDependency); ok {
  105. // fetch package
  106. path := dep.Path
  107. if asset, err := packager.FetchAsset(path); err != nil {
  108. return exception.InternalServerError(errors.Join(errors.New("failed to fetch package from bundle"), err)).ToResponse()
  109. } else {
  110. // decode and save
  111. decoder, err := decoder.NewZipPluginDecoder(asset)
  112. if err != nil {
  113. return exception.BadRequestError(errors.Join(errors.New("failed to create package decoder"), err)).ToResponse()
  114. }
  115. pluginUniqueIdentifier, err := decoder.UniqueIdentity()
  116. if err != nil {
  117. return exception.BadRequestError(errors.Join(errors.New("failed to get package unique identifier"), err)).ToResponse()
  118. }
  119. declaration, err := manager.SavePackage(pluginUniqueIdentifier, asset)
  120. if err != nil {
  121. return exception.InternalServerError(errors.Join(errors.New("failed to save package"), err)).ToResponse()
  122. }
  123. if config.ForceVerifyingSignature || verify_signature {
  124. if !declaration.Verified {
  125. return exception.BadRequestError(errors.Join(errors.New(
  126. "plugin verification has been enabled, and the plugin you want to install has a bad signature",
  127. ), err)).ToResponse()
  128. }
  129. }
  130. result = append(result, map[string]any{
  131. "type": "package",
  132. "value": map[string]any{
  133. "unique_identifier": pluginUniqueIdentifier,
  134. "manifest": declaration,
  135. },
  136. })
  137. }
  138. }
  139. }
  140. }
  141. return entities.NewSuccessResponse(result)
  142. }
  143. func FetchPluginManifest(
  144. tenant_id string,
  145. pluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier,
  146. ) *entities.Response {
  147. runtimeType := plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  148. if pluginUniqueIdentifier.RemoteLike() {
  149. runtimeType = plugin_entities.PLUGIN_RUNTIME_TYPE_REMOTE
  150. }
  151. pluginManifestCache, err := helper.CombinedGetPluginDeclaration(
  152. pluginUniqueIdentifier, tenant_id, runtimeType,
  153. )
  154. if err == helper.ErrPluginNotFound {
  155. return exception.BadRequestError(errors.New("plugin not found")).ToResponse()
  156. }
  157. if err != nil {
  158. return exception.InternalServerError(err).ToResponse()
  159. }
  160. return entities.NewSuccessResponse(pluginManifestCache)
  161. }