Просмотр исходного кода

feat: support model config and app id as tool parameters

Yeuoly 1 год назад
Родитель
Сommit
ba1f944ab9

+ 15 - 0
internal/types/entities/plugin_entities/constant.go

@@ -0,0 +1,15 @@
+package plugin_entities
+
+const (
+	SECRET_INPUT      = "secret-input"
+	TEXT_INPUT        = "text-input"
+	SELECT            = "select"
+	STRING            = "string"
+	NUMBER            = "number"
+	FILE              = "file"
+	BOOLEAN           = "boolean"
+	CHAT_APP_ID       = "chat-app-id"
+	COMPLETION_APP_ID = "completion-app-id"
+	WORKFLOW_APP_ID   = "workflow-app-id"
+	MODEL_CONFIG      = "model-config"
+)

+ 72 - 0
internal/types/entities/plugin_entities/credentials.go

@@ -0,0 +1,72 @@
+package plugin_entities
+
+import (
+	"github.com/go-playground/locales/en"
+	ut "github.com/go-playground/universal-translator"
+	"github.com/go-playground/validator/v10"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/validators"
+)
+
+type ConfigType string
+
+const (
+	CONFIG_TYPE_SECRET_INPUT      ConfigType = SECRET_INPUT
+	CONFIG_TYPE_TEXT_INPUT        ConfigType = TEXT_INPUT
+	CONFIG_TYPE_SELECT            ConfigType = SELECT
+	CONFIG_TYPE_BOOLEAN           ConfigType = BOOLEAN
+	CONFIG_TYPE_CHAT_APP_ID       ConfigType = CHAT_APP_ID
+	CONFIG_TYPE_COMPLETION_APP_ID ConfigType = COMPLETION_APP_ID
+	CONFIG_TYPE_WORKFLOW_APP_ID   ConfigType = WORKFLOW_APP_ID
+	CONFIG_TYPE_MODEL_CONFIG      ConfigType = MODEL_CONFIG
+)
+
+func isCredentialType(fl validator.FieldLevel) bool {
+	value := fl.Field().String()
+	switch value {
+	case string(CONFIG_TYPE_SECRET_INPUT),
+		string(CONFIG_TYPE_TEXT_INPUT),
+		string(CONFIG_TYPE_SELECT),
+		string(CONFIG_TYPE_BOOLEAN),
+		string(CONFIG_TYPE_CHAT_APP_ID),
+		string(CONFIG_TYPE_COMPLETION_APP_ID),
+		string(CONFIG_TYPE_WORKFLOW_APP_ID):
+		return true
+	}
+	return false
+}
+
+type ConfigOption struct {
+	Value string     `json:"value" validate:"required"`
+	Label I18nObject `json:"label" validate:"required"`
+}
+
+type ProviderConfig struct {
+	Name        string         `json:"name" validate:"required,gt=0,lt=1024"`
+	Type        ConfigType     `json:"type" validate:"required,credential_type"`
+	Required    bool           `json:"required"`
+	Default     any            `json:"default" validate:"omitempty,is_basic_type"`
+	Options     []ConfigOption `json:"options" validate:"omitempty,dive"`
+	Label       I18nObject     `json:"label" validate:"required"`
+	Helper      *I18nObject    `json:"helper" validate:"omitempty"`
+	URL         *string        `json:"url" validate:"omitempty"`
+	Placeholder *I18nObject    `json:"placeholder" validate:"omitempty"`
+}
+
+func init() {
+	en := en.New()
+	uni := ut.New(en, en)
+	translator, _ := uni.GetTranslator("en")
+
+	validators.GlobalEntitiesValidator.RegisterValidation("credential_type", isCredentialType)
+	validators.GlobalEntitiesValidator.RegisterTranslation(
+		"credential_type",
+		translator,
+		func(ut ut.Translator) error {
+			return ut.Add("credential_type", "{0} is not a valid credential type", true)
+		},
+		func(ut ut.Translator, fe validator.FieldError) string {
+			t, _ := ut.T("credential_type", fe.Field())
+			return t
+		},
+	)
+}

+ 41 - 0
internal/types/entities/plugin_entities/endpoint_declaration.go

@@ -0,0 +1,41 @@
+package plugin_entities
+
+import (
+	"github.com/go-playground/validator/v10"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/validators"
+)
+
+type EndpointMethod string
+
+const (
+	EndpointMethodHead    EndpointMethod = "HEAD"
+	EndpointMethodGet     EndpointMethod = "GET"
+	EndpointMethodPost    EndpointMethod = "POST"
+	EndpointMethodPut     EndpointMethod = "PUT"
+	EndpointMethodDelete  EndpointMethod = "DELETE"
+	EndpointMethodOptions EndpointMethod = "OPTIONS"
+)
+
+func isAvailableMethod(fl validator.FieldLevel) bool {
+	method := fl.Field().String()
+	switch method {
+	case string(EndpointMethodHead),
+		string(EndpointMethodGet),
+		string(EndpointMethodPost),
+		string(EndpointMethodPut),
+		string(EndpointMethodDelete),
+		string(EndpointMethodOptions):
+		return true
+	}
+	return false
+}
+
+func init() {
+	validators.GlobalEntitiesValidator.RegisterValidation("is_available_endpoint_method", isAvailableMethod)
+}
+
+type EndpointDeclaration struct {
+	Path     string                    `json:"path" yaml:"path" validate:"required"`
+	Method   EndpointMethod            `json:"method" yaml:"method" validate:"required,is_available_endpoint_method"`
+	Settings map[string]ProviderConfig `json:"settings" validate:"omitempty,dive"`
+}

+ 1 - 1
internal/types/entities/plugin_entities/event.go

@@ -5,8 +5,8 @@ import (
 )
 
 type PluginUniversalEvent struct {
-	Event     PluginEventType `json:"event"`
 	SessionId string          `json:"session_id"`
+	Event     PluginEventType `json:"event"`
 	Data      json.RawMessage `json:"data"`
 }
 

+ 18 - 61
internal/types/entities/plugin_entities/tool_configuration.go

@@ -26,12 +26,16 @@ type ToolParameterOption struct {
 type ToolParameterType string
 
 const (
-	TOOL_PARAMETER_TYPE_STRING       ToolParameterType = "string"
-	TOOL_PARAMETER_TYPE_NUMBER       ToolParameterType = "number"
-	TOOL_PARAMETER_TYPE_BOOLEAN      ToolParameterType = "boolean"
-	TOOL_PARAMETER_TYPE_SELECT       ToolParameterType = "select"
-	TOOL_PARAMETER_TYPE_SECRET_INPUT ToolParameterType = "secret_input"
-	TOOL_PARAMETER_TYPE_FILE         ToolParameterType = "file"
+	TOOL_PARAMETER_TYPE_STRING            ToolParameterType = STRING
+	TOOL_PARAMETER_TYPE_NUMBER            ToolParameterType = NUMBER
+	TOOL_PARAMETER_TYPE_BOOLEAN           ToolParameterType = BOOLEAN
+	TOOL_PARAMETER_TYPE_SELECT            ToolParameterType = SELECT
+	TOOL_PARAMETER_TYPE_SECRET_INPUT      ToolParameterType = SECRET_INPUT
+	TOOL_PARAMETER_TYPE_FILE              ToolParameterType = FILE
+	TOOL_PARAMETER_TYPE_MODEL_CONFIG      ToolParameterType = MODEL_CONFIG
+	TOOL_PARAMETER_TYPE_CHAT_APP_ID       ToolParameterType = CHAT_APP_ID
+	TOOL_PARAMETER_TYPE_COMPLETION_APP_ID ToolParameterType = COMPLETION_APP_ID
+	TOOL_PARAMETER_TYPE_WORKFLOW_APP_ID   ToolParameterType = WORKFLOW_APP_ID
 )
 
 func isToolParameterType(fl validator.FieldLevel) bool {
@@ -42,7 +46,11 @@ func isToolParameterType(fl validator.FieldLevel) bool {
 		string(TOOL_PARAMETER_TYPE_BOOLEAN),
 		string(TOOL_PARAMETER_TYPE_SELECT),
 		string(TOOL_PARAMETER_TYPE_SECRET_INPUT),
-		string(TOOL_PARAMETER_TYPE_FILE):
+		string(TOOL_PARAMETER_TYPE_FILE),
+		string(TOOL_PARAMETER_TYPE_MODEL_CONFIG),
+		string(TOOL_PARAMETER_TYPE_CHAT_APP_ID),
+		string(TOOL_PARAMETER_TYPE_COMPLETION_APP_ID),
+		string(TOOL_PARAMETER_TYPE_WORKFLOW_APP_ID):
 		return true
 	}
 	return false
@@ -107,44 +115,6 @@ func init() {
 	validators.GlobalEntitiesValidator.RegisterValidation("json_schema", isJSONSchema)
 }
 
-type ToolCredentialsOption struct {
-	Value string     `json:"value" validate:"required"`
-	Label I18nObject `json:"label" validate:"required"`
-}
-
-type CredentialType string
-
-const (
-	CREDENTIAL_TYPE_SECRET_INPUT CredentialType = "secret-input"
-	CREDENTIAL_TYPE_TEXT_INPUT   CredentialType = "text-input"
-	CREDENTIAL_TYPE_SELECT       CredentialType = "select"
-	CREDENTIAL_TYPE_BOOLEAN      CredentialType = "boolean"
-)
-
-func isCredentialType(fl validator.FieldLevel) bool {
-	value := fl.Field().String()
-	switch value {
-	case string(CREDENTIAL_TYPE_SECRET_INPUT),
-		string(CREDENTIAL_TYPE_TEXT_INPUT),
-		string(CREDENTIAL_TYPE_SELECT),
-		string(CREDENTIAL_TYPE_BOOLEAN):
-		return true
-	}
-	return false
-}
-
-type ToolProviderCredential struct {
-	Name        string                  `json:"name" validate:"required,gt=0,lt=1024"`
-	Type        CredentialType          `json:"type" validate:"required,credential_type"`
-	Required    bool                    `json:"required"`
-	Default     any                     `json:"default" validate:"omitempty,is_basic_type"`
-	Options     []ToolCredentialsOption `json:"options" validate:"omitempty,dive"`
-	Label       I18nObject              `json:"label" validate:"required"`
-	Helper      *I18nObject             `json:"helper" validate:"omitempty"`
-	URL         *string                 `json:"url" validate:"omitempty"`
-	Placeholder *I18nObject             `json:"placeholder" validate:"omitempty"`
-}
-
 type ToolLabel string
 
 const (
@@ -200,9 +170,9 @@ type ToolProviderIdentity struct {
 }
 
 type ToolProviderConfiguration struct {
-	Identity          ToolProviderIdentity              `json:"identity" validate:"required"`
-	CredentialsSchema map[string]ToolProviderCredential `json:"credentials_schema" validate:"omitempty,dive"`
-	Tools             []ToolConfiguration               `json:"tools" validate:"required,dive"`
+	Identity          ToolProviderIdentity      `json:"identity" validate:"required"`
+	CredentialsSchema map[string]ProviderConfig `json:"credentials_schema" validate:"omitempty,dive"`
+	Tools             []ToolConfiguration       `json:"tools" validate:"required,dive"`
 }
 
 func init() {
@@ -239,19 +209,6 @@ func init() {
 		},
 	)
 
-	validators.GlobalEntitiesValidator.RegisterValidation("credential_type", isCredentialType)
-	validators.GlobalEntitiesValidator.RegisterTranslation(
-		"credential_type",
-		translator,
-		func(ut ut.Translator) error {
-			return ut.Add("credential_type", "{0} is not a valid credential type", true)
-		},
-		func(ut ut.Translator, fe validator.FieldError) string {
-			t, _ := ut.T("credential_type", fe.Field())
-			return t
-		},
-	)
-
 	validators.GlobalEntitiesValidator.RegisterValidation("tool_label", isToolLabel)
 	validators.GlobalEntitiesValidator.RegisterTranslation(
 		"tool_label",