瀏覽代碼

feat: support install source

Yeuoly 9 月之前
父節點
當前提交
9055ccf69d

+ 12 - 2
internal/core/plugin_manager/install.go

@@ -27,7 +27,11 @@ type PluginInstallResponse struct {
 }
 
 // InstallToAWSFromPkg installs a plugin to AWS Lambda
-func (p *PluginManager) InstallToAWSFromPkg(tenant_id string, decoder decoder.PluginDecoder) (
+func (p *PluginManager) InstallToAWSFromPkg(
+	tenant_id string, decoder decoder.PluginDecoder,
+	source string,
+	meta map[string]any,
+) (
 	*stream.Stream[PluginInstallResponse], error,
 ) {
 	checksum, err := decoder.Checksum()
@@ -107,6 +111,8 @@ func (p *PluginManager) InstallToAWSFromPkg(tenant_id string, decoder decoder.Pl
 					unique_identity,
 					plugin_entities.PLUGIN_RUNTIME_TYPE_AWS,
 					&declaration,
+					source,
+					meta,
 				)
 				if err != nil {
 					new_response.Write(PluginInstallResponse{
@@ -139,7 +145,11 @@ func (p *PluginManager) InstallToAWSFromPkg(tenant_id string, decoder decoder.Pl
 }
 
 // InstallToLocal installs a plugin to local
-func (p *PluginManager) InstallToLocal(tenant_id string, decoder decoder.PluginDecoder) (
+func (p *PluginManager) InstallToLocal(
+	tenant_id string, decoder decoder.PluginDecoder,
+	source string,
+	meta map[string]any,
+) (
 	*stream.Stream[PluginInstallResponse], error,
 ) {
 	return nil, nil

+ 5 - 1
internal/core/plugin_manager/manager.go

@@ -38,7 +38,11 @@ type PluginManager struct {
 	// serverless runtime
 
 	// Install is a function that installs a plugin to the platform
-	Install func(tenant_id string, decoder decoder.PluginDecoder) (*stream.Stream[PluginInstallResponse], error)
+	Install func(
+		tenant_id string, decoder decoder.PluginDecoder,
+		source string,
+		meta map[string]any,
+	) (*stream.Stream[PluginInstallResponse], error)
 
 	// backwardsInvocation is a handle to invoke dify
 	backwardsInvocation dify_invocation.BackwardsInvocation

+ 3 - 1
internal/core/plugin_manager/remote_manager/register.go

@@ -3,7 +3,9 @@ package remote_manager
 import "github.com/langgenius/dify-plugin-daemon/internal/service/install_service"
 
 func (plugin *RemotePluginRuntime) Register() error {
-	_, installation, err := install_service.InstallPlugin(plugin.tenant_id, "", plugin)
+	_, installation, err := install_service.InstallPlugin(
+		plugin.tenant_id, "", plugin, "remote", map[string]any{},
+	)
 	if err != nil {
 		return err
 	}

+ 24 - 3
internal/server/controllers/plugins.go

@@ -9,6 +9,7 @@ import (
 	"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/parser"
 )
 
 func GetAsset(c *gin.Context) {
@@ -44,14 +45,27 @@ func InstallPluginFromPkg(app *app.Config) gin.HandlerFunc {
 
 		verify_signature := c.PostForm("verify_signature") == "true"
 
+		source := c.PostForm("source")
+		if source == "" {
+			c.JSON(http.StatusOK, entities.NewErrorResponse(-400, "Source is required"))
+			return
+		}
+
+		meta_str := c.PostForm("meta")
+		meta, err := parser.UnmarshalJson[map[string]any](meta_str)
+		if err != nil {
+			c.JSON(http.StatusOK, entities.NewErrorResponse(-400, err.Error()))
+			return
+		}
+
 		dify_pkg_file, err := dify_pkg_file_header.Open()
 		if err != nil {
-			c.JSON(http.StatusOK, entities.NewErrorResponse(-500, err.Error()))
+			c.JSON(http.StatusOK, entities.NewErrorResponse(-400, err.Error()))
 			return
 		}
 		defer dify_pkg_file.Close()
 
-		service.InstallPluginFromPkg(app, c, tenant_id, dify_pkg_file, verify_signature)
+		service.InstallPluginFromPkg(app, c, tenant_id, dify_pkg_file, verify_signature, source, meta)
 	}
 }
 
@@ -60,8 +74,15 @@ func InstallPluginFromIdentifier(app *app.Config) gin.HandlerFunc {
 		BindRequest(c, func(request struct {
 			TenantID               string                                 `uri:"tenant_id" validate:"required"`
 			PluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier `json:"plugin_unique_identifier" validate:"required,plugin_unique_identifier"`
+			Source                 string                                 `json:"source" validate:"required"`
+			Meta                   map[string]any                         `json:"meta" validate:"omitempty"`
 		}) {
-			c.JSON(http.StatusOK, service.InstallPluginFromIdentifier(request.TenantID, request.PluginUniqueIdentifier))
+			if request.Meta == nil {
+				request.Meta = map[string]any{}
+			}
+			c.JSON(http.StatusOK, service.InstallPluginFromIdentifier(
+				request.TenantID, request.PluginUniqueIdentifier, request.Source, request.Meta,
+			))
 		})
 	}
 }

+ 13 - 3
internal/service/install_plugin.go

@@ -19,7 +19,15 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
 )
 
-func InstallPluginFromPkg(config *app.Config, c *gin.Context, tenant_id string, dify_pkg_file multipart.File, verify_signature bool) {
+func InstallPluginFromPkg(
+	config *app.Config,
+	c *gin.Context,
+	tenant_id string,
+	dify_pkg_file multipart.File,
+	verify_signature bool,
+	source string,
+	meta map[string]any,
+) {
 	manager := plugin_manager.Manager()
 
 	plugin_file, err := io.ReadAll(dify_pkg_file)
@@ -46,7 +54,7 @@ func InstallPluginFromPkg(config *app.Config, c *gin.Context, tenant_id string,
 
 	baseSSEService(
 		func() (*stream.Stream[plugin_manager.PluginInstallResponse], error) {
-			return manager.Install(tenant_id, decoder)
+			return manager.Install(tenant_id, decoder, source, meta)
 		},
 		c,
 		3600,
@@ -56,6 +64,8 @@ func InstallPluginFromPkg(config *app.Config, c *gin.Context, tenant_id string,
 func InstallPluginFromIdentifier(
 	tenant_id string,
 	plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
+	source string,
+	meta map[string]any,
 ) *entities.Response {
 	// check if identifier exists
 	plugin, err := db.GetOne[models.Plugin](
@@ -74,7 +84,7 @@ func InstallPluginFromIdentifier(
 
 	declaration := plugin.Declaration
 	// install to this workspace
-	if _, _, err := curd.InstallPlugin(tenant_id, plugin_unique_identifier, plugin.InstallType, &declaration); err != nil {
+	if _, _, err := curd.InstallPlugin(tenant_id, plugin_unique_identifier, plugin.InstallType, &declaration, source, meta); err != nil {
 		return entities.NewErrorResponse(-500, err.Error())
 	}
 

+ 4 - 0
internal/service/install_service/state.go

@@ -15,6 +15,8 @@ func InstallPlugin(
 	tenant_id string,
 	user_id string,
 	runtime plugin_entities.PluginLifetime,
+	source string,
+	meta map[string]any,
 ) (*models.Plugin, *models.PluginInstallation, error) {
 	identity, err := runtime.Identity()
 	if err != nil {
@@ -27,6 +29,8 @@ func InstallPlugin(
 		identity,
 		runtime.Type(),
 		configuration,
+		source,
+		meta,
 	)
 
 	if err != nil {

+ 4 - 0
internal/types/models/curd/atomic.go

@@ -17,6 +17,8 @@ func InstallPlugin(
 	plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
 	install_type plugin_entities.PluginRuntimeType,
 	declaration *plugin_entities.PluginDeclaration,
+	source string,
+	meta map[string]any,
 ) (
 	*models.Plugin, *models.PluginInstallation, error,
 ) {
@@ -84,6 +86,8 @@ func InstallPlugin(
 			PluginUniqueIdentifier: plugin_to_be_returns.PluginUniqueIdentifier,
 			TenantID:               tenant_id,
 			RuntimeType:            string(install_type),
+			Source:                 source,
+			Meta:                   meta,
 		}
 
 		err = db.Create(installation, tx)

+ 8 - 6
internal/types/models/installation.go

@@ -4,10 +4,12 @@ type PluginInstallationStatus string
 
 type PluginInstallation struct {
 	Model
-	TenantID               string `json:"tenant_id" gorm:"index;type:uuid;"`
-	PluginID               string `json:"plugin_id" gorm:"index;size:127"`
-	PluginUniqueIdentifier string `json:"plugin_unique_identifier" gorm:"index;size:127"`
-	RuntimeType            string `json:"runtime_type" gorm:"size:127"`
-	EndpointsSetups        int    `json:"endpoints_setups"`
-	EndpointsActive        int    `json:"endpoints_active"`
+	TenantID               string         `json:"tenant_id" gorm:"index;type:uuid;"`
+	PluginID               string         `json:"plugin_id" gorm:"index;size:127"`
+	PluginUniqueIdentifier string         `json:"plugin_unique_identifier" gorm:"index;size:127"`
+	RuntimeType            string         `json:"runtime_type" gorm:"size:127"`
+	EndpointsSetups        int            `json:"endpoints_setups"`
+	EndpointsActive        int            `json:"endpoints_active"`
+	Source                 string         `json:"source" gorm:"column:source;size:63"`
+	Meta                   map[string]any `json:"meta" gorm:"column:meta;serializer:json"`
 }