Skip to content

Commit

Permalink
feat: support install source
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Oct 10, 2024
1 parent 0d3ef25 commit 9055ccf
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 16 deletions.
14 changes: 12 additions & 2 deletions internal/core/plugin_manager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/core/plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/core/plugin_manager/remote_manager/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 24 additions & 3 deletions internal/server/controllers/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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,
))
})
}
}
Expand Down
16 changes: 13 additions & 3 deletions internal/service/install_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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](
Expand All @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions internal/service/install_service/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,6 +29,8 @@ func InstallPlugin(
identity,
runtime.Type(),
configuration,
source,
meta,
)

if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/types/models/curd/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions internal/types/models/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 9055ccf

Please sign in to comment.