Browse Source

feat: support plugin decoder api

Yeuoly 9 months ago
parent
commit
d39d8ffcfd

+ 0 - 47
internal/service/install_plugin.go

@@ -1,18 +1,12 @@
 package service
 
 import (
-	"errors"
 	"fmt"
-	"io"
-	"mime/multipart"
 	"time"
 
-	"github.com/gin-gonic/gin"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
-	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/verifier"
 	"github.com/langgenius/dify-plugin-daemon/internal/db"
-	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/models"
@@ -22,40 +16,6 @@ import (
 	"gorm.io/gorm"
 )
 
-func UploadPluginFromPkg(
-	config *app.Config,
-	c *gin.Context,
-	tenant_id string,
-	dify_pkg_file multipart.File,
-	verify_signature bool,
-) *entities.Response {
-	plugin_file, err := io.ReadAll(dify_pkg_file)
-	if err != nil {
-		return entities.NewErrorResponse(-500, err.Error())
-	}
-
-	decoder, err := decoder.NewZipPluginDecoder(plugin_file)
-	if err != nil {
-		return entities.NewErrorResponse(-500, err.Error())
-	}
-
-	if config.ForceVerifyingSignature || verify_signature {
-		err := verifier.VerifyPlugin(decoder)
-		if err != nil {
-			return entities.NewErrorResponse(-500, errors.Join(err, errors.New(
-				"plugin verification has been enabled, and the plugin you want to install has a bad signature",
-			)).Error())
-		}
-	}
-
-	manifest, err := decoder.Manifest()
-	if err != nil {
-		return entities.NewErrorResponse(-500, err.Error())
-	}
-
-	return entities.NewSuccessResponse(manifest.Identity())
-}
-
 func InstallPluginFromIdentifiers(
 	tenant_id string,
 	plugin_unique_identifiers []plugin_entities.PluginUniqueIdentifier,
@@ -284,13 +244,6 @@ func FetchPluginInstallationTask(
 	return entities.NewSuccessResponse(task)
 }
 
-func FetchPluginManifest(
-	tenant_id string,
-	plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
-) *entities.Response {
-	return nil
-}
-
 func FetchPluginFromIdentifier(
 	plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
 ) *entities.Response {

+ 90 - 0
internal/service/plugin_decoder.go

@@ -0,0 +1,90 @@
+package service
+
+import (
+	"errors"
+	"io"
+	"mime/multipart"
+
+	"github.com/gin-gonic/gin"
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/verifier"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
+)
+
+func UploadPluginFromPkg(
+	config *app.Config,
+	c *gin.Context,
+	tenant_id string,
+	dify_pkg_file multipart.File,
+	verify_signature bool,
+) *entities.Response {
+	plugin_file, err := io.ReadAll(dify_pkg_file)
+	if err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	decoder, err := decoder.NewZipPluginDecoder(plugin_file)
+	if err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	if config.ForceVerifyingSignature || verify_signature {
+		err := verifier.VerifyPlugin(decoder)
+		if err != nil {
+			return entities.NewErrorResponse(-500, errors.Join(err, errors.New(
+				"plugin verification has been enabled, and the plugin you want to install has a bad signature",
+			)).Error())
+		}
+	}
+
+	plugin_unique_identifier, err := decoder.UniqueIdentity()
+	if err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	manager := plugin_manager.Manager()
+	if err := manager.SavePackage(plugin_unique_identifier, plugin_file); err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	return entities.NewSuccessResponse(plugin_unique_identifier)
+}
+
+func FetchPluginManifest(
+	tenant_id string,
+	plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
+) *entities.Response {
+	type ManifestCache struct {
+		Declaration plugin_entities.PluginDeclaration `json:"declaration"`
+	}
+
+	plugin_manifest_cache, err := cache.AutoGetWithGetter(plugin_unique_identifier.String(), func() (*ManifestCache, error) {
+		manager := plugin_manager.Manager()
+		pkg, err := manager.GetPackage(plugin_unique_identifier)
+		if err != nil {
+			return nil, err
+		}
+
+		decoder, err := decoder.NewZipPluginDecoder(pkg)
+		if err != nil {
+			return nil, err
+		}
+
+		manifest, err := decoder.Manifest()
+		if err != nil {
+			return nil, err
+		}
+
+		return &ManifestCache{Declaration: manifest}, nil
+	})
+
+	if err != nil {
+		return entities.NewErrorResponse(-500, err.Error())
+	}
+
+	return entities.NewSuccessResponse(plugin_manifest_cache)
+}

+ 3 - 2
internal/utils/cache/redis_auto_type.go

@@ -3,6 +3,7 @@ package cache
 import (
 	"errors"
 	"reflect"
+	"time"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
 	"github.com/redis/go-redis/v9"
@@ -20,7 +21,7 @@ func AutoSet[T any](key string, value T, context ...redis.Cmdable) error {
 	full_type_name := pkg_path + "." + type_name
 
 	key = serialKey("auto_type", full_type_name, key)
-	return getCmdable(context...).Set(ctx, key, parser.MarshalJson(value), 0).Err()
+	return getCmdable(context...).Set(ctx, key, parser.MarshalJson(value), time.Minute*30).Err()
 }
 
 // Get the value with key
@@ -53,7 +54,7 @@ func AutoGetWithGetter[T any](key string, getter func() (*T, error), context ...
 				return nil, err
 			}
 
-			if err := Store(key, value, 0, context...); err != nil {
+			if err := Store(key, value, time.Minute*30, context...); err != nil {
 				return nil, err
 			}
 			return value, nil