Ver código fonte

feat: rename webhook to endpoint

Yeuoly 1 ano atrás
pai
commit
586b0018c5

+ 1 - 1
cmd/server/main.go

@@ -44,7 +44,7 @@ func setDefault(config *app.Config) {
 	setDefaultInt(&config.MaxAWSLambdaTransactionTimeout, 150)
 	setDefaultInt(&config.PluginMaxExecutionTimeout, 240)
 	setDefaultBool(&config.PluginRemoteInstallingEnabled, true)
-	setDefaultBool(&config.PluginWebhookEnabled, true)
+	setDefaultBool(&config.PluginEndpointEnabled, true)
 	setDefaultString(&config.DBSslMode, "disable")
 	setDefaultString(&config.ProcessCachingPath, "/tmp/dify-plugin-daemon-subprocesses")
 }

+ 3 - 0
go.mod

@@ -40,6 +40,9 @@ require (
 	github.com/jinzhu/now v1.1.5 // indirect
 	github.com/jmespath/go-jmespath v0.4.0 // indirect
 	github.com/rogpeppe/go-internal v1.12.0 // indirect
+	github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
+	github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
+	github.com/xeipuuv/gojsonschema v1.2.0 // indirect
 )
 
 require (

+ 6 - 0
go.sum

@@ -149,6 +149,12 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
 github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
 github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
 github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
+github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
+github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
+github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
+github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
+github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
+github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
 go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
 go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
 go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

+ 4 - 4
internal/core/plugin_daemon/access_types/access.go

@@ -3,9 +3,9 @@ package access_types
 type PluginAccessType string
 
 const (
-	PLUGIN_ACCESS_TYPE_TOOL    PluginAccessType = "tool"
-	PLUGIN_ACCESS_TYPE_MODEL   PluginAccessType = "model"
-	PLUGIN_ACCESS_TYPE_WEBHOOK PluginAccessType = "webhook"
+	PLUGIN_ACCESS_TYPE_TOOL     PluginAccessType = "tool"
+	PLUGIN_ACCESS_TYPE_MODEL    PluginAccessType = "model"
+	PLUGIN_ACCESS_TYPE_Endpoint PluginAccessType = "endpoint"
 )
 
 type PluginAccessAction string
@@ -21,5 +21,5 @@ const (
 	PLUGIN_ACCESS_ACTION_INVOKE_MODERATION             PluginAccessAction = "invoke_moderation"
 	PLUGIN_ACCESS_ACTION_VALIDATE_PROVIDER_CREDENTIALS PluginAccessAction = "validate_provider_credentials"
 	PLUGIN_ACCESS_ACTION_VALIDATE_MODEL_CREDENTIALS    PluginAccessAction = "validate_model_credentials"
-	PLUGIN_ACCESS_ACTION_WEBHOOK                       PluginAccessAction = "invoke_webhook"
+	PLUGIN_ACCESS_ACTION_INVOKE_ENDPOINT               PluginAccessAction = "invoke_endpoint"
 )

+ 4 - 4
internal/core/plugin_daemon/webhook_service.go

@@ -5,19 +5,19 @@ import (
 	"net/http"
 
 	"github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/endpoint_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
-	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/webhook_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
 )
 
-func InvokeWebhook(
+func InvokeEndpoint(
 	session *session_manager.Session,
-	request *requests.RequestInvokeWebhook,
+	request *requests.RequestInvokeEndpoint,
 ) (
 	int, *http.Header, *stream.StreamResponse[[]byte], error,
 ) {
-	resp, err := genericInvokePlugin[requests.RequestInvokeWebhook, webhook_entities.WebhookResponseChunk](
+	resp, err := genericInvokePlugin[requests.RequestInvokeEndpoint, endpoint_entities.EndpointResponseChunk](
 		session,
 		request,
 		128,

+ 2 - 2
internal/core/plugin_packager/manifest.yaml

@@ -13,8 +13,8 @@ resource:
       enabled: true
       llm: true
 plugins:
-  - "webhooks/neko.yaml"
-  - "webhooks/duck.yaml"
+  - "endpoints/neko.yaml"
+  - "endpoints/duck.yaml"
 execution:
   install: install.sh
   launch: launch.sh

+ 1 - 1
internal/db/init.go

@@ -80,7 +80,7 @@ func autoMigrate() error {
 	return DifyPluginDB.AutoMigrate(
 		models.Plugin{},
 		models.PluginInstallation{},
-		models.Webhook{},
+		models.Endpoint{},
 	)
 }
 

+ 2 - 2
internal/server/app.go

@@ -10,9 +10,9 @@ type App struct {
 	// schedule all the tasks related to the cluster, like request direct
 	cluster *cluster.Cluster
 
-	// webhook handler
+	// endpoint handler
 	// customize behavior of endpoint
-	webhook_handler WebhookHandler
+	endpoint_handler EndpointHandler
 
 	// aws transaction handler
 	// accept aws transaction request and forward to the plugin daemon

internal/server/controllers/webhook.go → internal/server/controllers/endpoint.go


+ 52 - 0
internal/server/endpoint.go

@@ -0,0 +1,52 @@
+package server
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/langgenius/dify-plugin-daemon/internal/db"
+	"github.com/langgenius/dify-plugin-daemon/internal/service"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/models"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+// DifyPlugin supports register and use endpoint to improve the plugin's functionality
+// you can use it to do some magics, looking forward to your imagination, Ciallo~(∠·ω< )⌒
+// - Yeuoly
+
+// EndpointHandler is a function type that can be used to handle endpoint requests
+type EndpointHandler func(ctx *gin.Context, hook_id string, path string)
+
+func (app *App) Endpoint() func(c *gin.Context) {
+	return func(c *gin.Context) {
+		hook_id := c.Param("hook_id")
+		path := c.Param("path")
+
+		if app.endpoint_handler != nil {
+			app.endpoint_handler(c, hook_id, path)
+		} else {
+			app.EndpointHandler(c, hook_id, path)
+		}
+	}
+}
+
+func (app *App) EndpointHandler(ctx *gin.Context, hook_id string, path string) {
+	endpoint, err := db.GetOne[models.Endpoint](
+		db.Equal("hook_id", hook_id),
+	)
+	if err == db.ErrDatabaseNotFound {
+		ctx.JSON(404, gin.H{"error": "endpoint not found"})
+		return
+	}
+
+	if err != nil {
+		log.Error("get endpoint error %v", err)
+		ctx.JSON(500, gin.H{"error": "internal server error"})
+		return
+	}
+
+	// check if plugin exists in current node
+	if !app.cluster.IsPluginNoCurrentNode(endpoint.PluginID) {
+		app.redirectPluginInvokeByPluginID(ctx, endpoint.PluginID)
+	} else {
+		service.Endpoint(ctx, &endpoint, path)
+	}
+}

+ 6 - 6
internal/server/webhook_test.go

@@ -10,7 +10,7 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/network"
 )
 
-func TestWebhookParams(t *testing.T) {
+func TestEndpointParams(t *testing.T) {
 	port, err := network.GetRandomPort()
 	if err != nil {
 		t.Errorf("failed to get random port: %s", err.Error())
@@ -26,17 +26,17 @@ func TestWebhookParams(t *testing.T) {
 	}
 
 	app_pointer := &App{
-		webhook_handler: handler,
+		endpoint_handler: handler,
 	}
 	cancel := app_pointer.server(&app.Config{
-		ServerPort:           port,
-		PluginWebhookEnabled: true,
+		ServerPort:            port,
+		PluginEndpointEnabled: true,
 	})
 	defer cancel()
 
-	// test webhook params
+	// test endpoint params
 	client := &http.Client{}
-	req, err := http.NewRequest("POST", "http://localhost:"+strconv.Itoa(int(port))+"/webhook/1111/v1/chat/completions", nil)
+	req, err := http.NewRequest("POST", "http://localhost:"+strconv.Itoa(int(port))+"/e/1111/v1/chat/completions", nil)
 	if err != nil {
 		t.Errorf("failed to create request: %s", err.Error())
 		return

+ 9 - 9
internal/server/http_server.go

@@ -20,7 +20,7 @@ func (app *App) server(config *app.Config) func() {
 
 	app.pluginInvokeGroup(engine.Group("/plugin"), config)
 	app.remoteDebuggingGroup(engine.Group("/plugin/debugging"), config)
-	app.webhookGroup(engine.Group("/webhook"), config)
+	app.endpointGroup(engine.Group("/e"), config)
 	app.awsLambdaTransactionGroup(engine.Group("/backwards-invocation"), config)
 
 	srv := &http.Server{
@@ -64,14 +64,14 @@ func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config)
 	}
 }
 
-func (app *App) webhookGroup(group *gin.RouterGroup, config *app.Config) {
-	if config.PluginWebhookEnabled {
-		group.HEAD("/:hook_id/*path", app.Webhook())
-		group.POST("/:hook_id/*path", app.Webhook())
-		group.GET("/:hook_id/*path", app.Webhook())
-		group.PUT("/:hook_id/*path", app.Webhook())
-		group.DELETE("/:hook_id/*path", app.Webhook())
-		group.OPTIONS("/:hook_id/*path", app.Webhook())
+func (app *App) endpointGroup(group *gin.RouterGroup, config *app.Config) {
+	if config.PluginEndpointEnabled {
+		group.HEAD("/:hook_id/*path", app.Endpoint())
+		group.POST("/:hook_id/*path", app.Endpoint())
+		group.GET("/:hook_id/*path", app.Endpoint())
+		group.PUT("/:hook_id/*path", app.Endpoint())
+		group.DELETE("/:hook_id/*path", app.Endpoint())
+		group.OPTIONS("/:hook_id/*path", app.Endpoint())
 	}
 }
 

+ 0 - 53
internal/server/webhook.go

@@ -1,53 +0,0 @@
-package server
-
-import (
-	"github.com/gin-gonic/gin"
-	"github.com/langgenius/dify-plugin-daemon/internal/db"
-	"github.com/langgenius/dify-plugin-daemon/internal/service"
-	"github.com/langgenius/dify-plugin-daemon/internal/types/models"
-	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
-)
-
-// DifyPlugin supports register and use webhook to improve the plugin's functionality
-// you can use it to do some magics, looking forward to your imagination, Ciallo~(∠·ω< )⌒
-// - Yeuoly
-
-// WebhookHandler is a function type that can be used to handle webhook requests
-type WebhookHandler func(ctx *gin.Context, hook_id string, path string)
-
-func (app *App) Webhook() func(c *gin.Context) {
-	return func(c *gin.Context) {
-		hook_id := c.Param("hook_id")
-		path := c.Param("path")
-
-		if app.webhook_handler != nil {
-			app.webhook_handler(c, hook_id, path)
-		} else {
-			app.WebhookHandler(c, hook_id, path)
-		}
-	}
-}
-
-func (app *App) WebhookHandler(ctx *gin.Context, hook_id string, path string) {
-	webhook, err := db.GetOne[models.Webhook](
-		db.Equal("hook_id", hook_id),
-	)
-
-	if err == db.ErrDatabaseNotFound {
-		ctx.JSON(404, gin.H{"error": "webhook not found"})
-		return
-	}
-
-	if err != nil {
-		log.Error("get webhook error %v", err)
-		ctx.JSON(500, gin.H{"error": "internal server error"})
-		return
-	}
-
-	// check if plugin exists in current node
-	if !app.cluster.IsPluginNoCurrentNode(webhook.PluginID) {
-		app.redirectPluginInvokeByPluginID(ctx, webhook.PluginID)
-	} else {
-		service.Webhook(ctx, &webhook, path)
-	}
-}

+ 7 - 7
internal/service/webhook.go

@@ -17,7 +17,7 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
 )
 
-func Webhook(ctx *gin.Context, webhook *models.Webhook, path string) {
+func Endpoint(ctx *gin.Context, endpoint *models.Endpoint, path string) {
 	req := ctx.Request.Clone(context.Background())
 	req.URL.Path = path
 
@@ -30,26 +30,26 @@ func Webhook(ctx *gin.Context, webhook *models.Webhook, path string) {
 
 	// fetch plugin
 	manager := plugin_manager.GetGlobalPluginManager()
-	runtime := manager.Get(webhook.PluginID)
+	runtime := manager.Get(endpoint.PluginID)
 	if runtime == nil {
 		ctx.JSON(404, gin.H{"error": "plugin not found"})
 		return
 	}
 
 	session := session_manager.NewSession(
-		webhook.TenantID,
+		endpoint.TenantID,
 		"",
-		webhook.PluginID,
+		endpoint.PluginID,
 		ctx.GetString("cluster_id"),
-		access_types.PLUGIN_ACCESS_TYPE_WEBHOOK,
-		access_types.PLUGIN_ACCESS_ACTION_WEBHOOK,
+		access_types.PLUGIN_ACCESS_TYPE_Endpoint,
+		access_types.PLUGIN_ACCESS_ACTION_INVOKE_ENDPOINT,
 		runtime.Configuration(),
 	)
 	defer session.Close()
 
 	session.BindRuntime(runtime)
 
-	status_code, headers, response, err := plugin_daemon.InvokeWebhook(session, &requests.RequestInvokeWebhook{
+	status_code, headers, response, err := plugin_daemon.InvokeEndpoint(session, &requests.RequestInvokeEndpoint{
 		RawHttpRequest: hex.EncodeToString(buffer.Bytes()),
 	})
 	if err != nil {

+ 16 - 16
internal/service/install_service/state.go

@@ -34,9 +34,9 @@ func InstallPlugin(
 		return "", err
 	}
 
-	// check if there is a webhook for the plugin
-	if runtime.Configuration().Resource.Permission.AllowRegistryWebhook() {
-		_, err := InstallWebhook(plugin.PluginID, installation.ID, tenant_id, user_id)
+	// check if there is a endpoint for the plugin
+	if runtime.Configuration().Resource.Permission.AllowRegistryEndpoint() {
+		_, err := InstallEndpoint(plugin.PluginID, installation.ID, tenant_id, user_id)
 		if err != nil {
 			return "", err
 		}
@@ -57,17 +57,17 @@ func UninstallPlugin(tenant_id string, installation_id string, runtime plugin_en
 		return err
 	}
 
-	// delete the webhook from db
-	if runtime.Configuration().Resource.Permission.AllowRegistryWebhook() {
-		// get the webhook from db
-		webhook, err := GetWebhook(tenant_id, identity, resp.Installation.ID)
+	// delete the endpoint from db
+	if runtime.Configuration().Resource.Permission.AllowRegistryEndpoint() {
+		// get the endpoint from db
+		endpoint, err := GetEndpoint(tenant_id, identity, resp.Installation.ID)
 		if err != nil && err != db.ErrDatabaseNotFound {
 			return err
 		} else if err == db.ErrDatabaseNotFound {
 			return nil
 		}
 
-		err = UninstallWebhook(webhook)
+		err = UninstallEndpoint(endpoint)
 		if err != nil {
 			return err
 		}
@@ -77,9 +77,9 @@ func UninstallPlugin(tenant_id string, installation_id string, runtime plugin_en
 }
 
 // installs a plugin to db,
-// returns the webhook id
-func InstallWebhook(plugin_id string, installation_id string, tenant_id string, user_id string) (string, error) {
-	installation := &models.Webhook{
+// returns the endpoint id
+func InstallEndpoint(plugin_id string, installation_id string, tenant_id string, user_id string) (string, error) {
+	installation := &models.Endpoint{
 		HookID:               strings.RandomString(64),
 		PluginID:             plugin_id,
 		TenantID:             tenant_id,
@@ -95,8 +95,8 @@ func InstallWebhook(plugin_id string, installation_id string, tenant_id string,
 	return installation.HookID, nil
 }
 
-func GetWebhook(tenant_id string, plugin_id string, installation_id string) (*models.Webhook, error) {
-	webhook, err := db.GetOne[models.Webhook](
+func GetEndpoint(tenant_id string, plugin_id string, installation_id string) (*models.Endpoint, error) {
+	endpoint, err := db.GetOne[models.Endpoint](
 		db.Equal("tenant_id", tenant_id),
 		db.Equal("plugin_id", plugin_id),
 		db.Equal("plugin_installation_id", installation_id),
@@ -106,10 +106,10 @@ func GetWebhook(tenant_id string, plugin_id string, installation_id string) (*mo
 		return nil, err
 	}
 
-	return &webhook, nil
+	return &endpoint, nil
 }
 
 // uninstalls a plugin from db
-func UninstallWebhook(webhook *models.Webhook) error {
-	return db.Delete(webhook)
+func UninstallEndpoint(endpoint *models.Endpoint) error {
+	return db.Delete(endpoint)
 }

+ 1 - 1
internal/types/app/config.go

@@ -18,7 +18,7 @@ type Config struct {
 	PluginRemoteInstallingMaxConn          int    `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
 	PluginRemoteInstallServerEventLoopNums int    `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
 
-	PluginWebhookEnabled bool `envconfig:"PLUGIN_WEBHOOK_ENABLED"`
+	PluginEndpointEnabled bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
 
 	PluginStoragePath  string `envconfig:"STORAGE_PLUGIN_PATH" validate:"required"`
 	PluginWorkingPath  string `envconfig:"PLUGIN_WORKING_PATH"`

+ 2 - 2
internal/types/entities/webhook_entities/webhook.go

@@ -1,6 +1,6 @@
-package webhook_entities
+package endpoint_entities
 
-type WebhookResponseChunk struct {
+type EndpointResponseChunk struct {
 	Status  *uint16           `json:"status" validate:"omitempty"`
 	Headers map[string]string `json:"headers" validate:"omitempty"`
 	Result  *string           `json:"result" validate:"omitempty"`

+ 8 - 8
internal/types/entities/plugin_entities/plugin_declaration.go

@@ -17,11 +17,11 @@ const (
 )
 
 type PluginPermissionRequirement struct {
-	Tool    *PluginPermissionToolRequirement    `json:"tool" yaml:"tool" validate:"omitempty"`
-	Model   *PluginPermissionModelRequirement   `json:"model" yaml:"model" validate:"omitempty"`
-	Node    *PluginPermissionNodeRequirement    `json:"node" yaml:"node" validate:"omitempty"`
-	Webhook *PluginPermissionWebhookRequirement `json:"webhook" yaml:"webhook" validate:"omitempty"`
-	App     *PluginPermissionAppRequirement     `json:"app" yaml:"app" validate:"omitempty"`
+	Tool     *PluginPermissionToolRequirement     `json:"tool" yaml:"tool" validate:"omitempty"`
+	Model    *PluginPermissionModelRequirement    `json:"model" yaml:"model" validate:"omitempty"`
+	Node     *PluginPermissionNodeRequirement     `json:"node" yaml:"node" validate:"omitempty"`
+	Endpoint *PluginPermissionEndpointRequirement `json:"endpoint" yaml:"endpoint" validate:"omitempty"`
+	App      *PluginPermissionAppRequirement      `json:"app" yaml:"app" validate:"omitempty"`
 }
 
 func (p *PluginPermissionRequirement) AllowInvokeTool() bool {
@@ -60,8 +60,8 @@ func (p *PluginPermissionRequirement) AllowInvokeApp() bool {
 	return p != nil && p.App != nil && p.App.Enabled
 }
 
-func (p *PluginPermissionRequirement) AllowRegistryWebhook() bool {
-	return p != nil && p.Webhook != nil && p.Webhook.Enabled
+func (p *PluginPermissionRequirement) AllowRegistryEndpoint() bool {
+	return p != nil && p.Endpoint != nil && p.Endpoint.Enabled
 }
 
 type PluginPermissionToolRequirement struct {
@@ -82,7 +82,7 @@ type PluginPermissionNodeRequirement struct {
 	Enabled bool `json:"enabled" yaml:"enabled"`
 }
 
-type PluginPermissionWebhookRequirement struct {
+type PluginPermissionEndpointRequirement struct {
 	Enabled bool `json:"enabled" yaml:"enabled"`
 }
 

+ 1 - 0
internal/types/entities/plugin_entities/provider_declaration.go

@@ -0,0 +1 @@
+package plugin_entities

+ 1 - 1
internal/types/entities/requests/webhook.go

@@ -1,5 +1,5 @@
 package requests
 
-type RequestInvokeWebhook struct {
+type RequestInvokeEndpoint struct {
 	RawHttpRequest string `json:"raw_http_request" validate:"required"`
 }

+ 2 - 2
internal/types/models/webhook.go

@@ -2,8 +2,8 @@ package models
 
 import "time"
 
-// HookID is a pointer to plugin id and tenant id, using it to identify the webhook plugin
-type Webhook struct {
+// HookID is a pointer to plugin id and tenant id, using it to identify the endpoint plugin
+type Endpoint struct {
 	Model
 	HookID               string    `json:"hook_id" orm:"uniqueIndex;size:127;column:hook_id"`
 	TenantID             string    `json:"tenant_id" orm:"index;size:64;column:tenant_id"`