From 27ce99fb57080f41d090896f69d9d7395dbccb54 Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Thu, 12 Dec 2024 18:44:21 +0800 Subject: [PATCH] feat: support and in scope --- .../types/entities/plugin_entities/config.go | 65 +++++++++++++------ .../entities/plugin_entities/config_test.go | 32 +++++++++ .../plugin_entities/tool_declaration.go | 2 +- 3 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 internal/types/entities/plugin_entities/config_test.go diff --git a/internal/types/entities/plugin_entities/config.go b/internal/types/entities/plugin_entities/config.go index 57cb481..b6cbf74 100644 --- a/internal/types/entities/plugin_entities/config.go +++ b/internal/types/entities/plugin_entities/config.go @@ -2,6 +2,7 @@ package plugin_entities import ( "errors" + "strings" "github.com/go-playground/locales/en" ut "github.com/go-playground/universal-translator" @@ -32,6 +33,8 @@ const ( MODEL_CONFIG_SCOPE_SPEECH2TEXT ModelConfigScope = "speech2text" MODEL_CONFIG_SCOPE_MODERATION ModelConfigScope = "moderation" MODEL_CONFIG_SCOPE_VISION ModelConfigScope = "vision" + MODEL_CONFIG_SCOPE_DOCUMENT ModelConfigScope = "document" + MODEL_CONFIG_SCOPE_TOOL_CALL ModelConfigScope = "tool-call" ) type AppSelectorScope string @@ -72,39 +75,59 @@ type ConfigOption struct { func isModelConfigScope(fl validator.FieldLevel) bool { value := fl.Field().String() - switch value { - case string(MODEL_CONFIG_SCOPE_LLM), - string(MODEL_CONFIG_SCOPE_TEXT_EMBEDDING), - string(MODEL_CONFIG_SCOPE_RERANK), - string(MODEL_CONFIG_SCOPE_TTS), - string(MODEL_CONFIG_SCOPE_SPEECH2TEXT), - string(MODEL_CONFIG_SCOPE_MODERATION), - string(MODEL_CONFIG_SCOPE_VISION): - return true + // split by and symbol + scopes := strings.Split(value, "&") + for _, scope := range scopes { + // trim space + scope = strings.TrimSpace(scope) + switch scope { + case string(MODEL_CONFIG_SCOPE_LLM), + string(MODEL_CONFIG_SCOPE_TEXT_EMBEDDING), + string(MODEL_CONFIG_SCOPE_RERANK), + string(MODEL_CONFIG_SCOPE_TTS), + string(MODEL_CONFIG_SCOPE_SPEECH2TEXT), + string(MODEL_CONFIG_SCOPE_MODERATION), + string(MODEL_CONFIG_SCOPE_VISION), + string(MODEL_CONFIG_SCOPE_DOCUMENT), + string(MODEL_CONFIG_SCOPE_TOOL_CALL): + return true + } } return false } func isAppSelectorScope(fl validator.FieldLevel) bool { value := fl.Field().String() - switch value { - case string(APP_SELECTOR_SCOPE_ALL), - string(APP_SELECTOR_SCOPE_CHAT), - string(APP_SELECTOR_SCOPE_WORKFLOW), - string(APP_SELECTOR_SCOPE_COMPLETION): - return true + // split by and symbol + scopes := strings.Split(value, "&") + for _, scope := range scopes { + // trim space + scope = strings.TrimSpace(scope) + switch scope { + case string(APP_SELECTOR_SCOPE_ALL), + string(APP_SELECTOR_SCOPE_CHAT), + string(APP_SELECTOR_SCOPE_WORKFLOW), + string(APP_SELECTOR_SCOPE_COMPLETION): + return true + } } return false } func isToolSelectorScope(fl validator.FieldLevel) bool { value := fl.Field().String() - switch value { - case string(TOOL_SELECTOR_SCOPE_ALL), - string(TOOL_SELECTOR_SCOPE_PLUGIN), - string(TOOL_SELECTOR_SCOPE_API), - string(TOOL_SELECTOR_SCOPE_WORKFLOW): - return true + // split by and symbol + scopes := strings.Split(value, "&") + for _, scope := range scopes { + // trim space + scope = strings.TrimSpace(scope) + switch scope { + case string(TOOL_SELECTOR_SCOPE_ALL), + string(TOOL_SELECTOR_SCOPE_PLUGIN), + string(TOOL_SELECTOR_SCOPE_API), + string(TOOL_SELECTOR_SCOPE_WORKFLOW): + return true + } } return false } diff --git a/internal/types/entities/plugin_entities/config_test.go b/internal/types/entities/plugin_entities/config_test.go new file mode 100644 index 0000000..e19a409 --- /dev/null +++ b/internal/types/entities/plugin_entities/config_test.go @@ -0,0 +1,32 @@ +package plugin_entities + +import ( + "testing" + + "github.com/langgenius/dify-plugin-daemon/internal/utils/parser" +) + +func TestParameterScope_Validate(t *testing.T) { + config := ToolParameter{ + Name: "test", + Type: TOOL_PARAMETER_TYPE_MODEL_SELECTOR, + Scope: parser.ToPtr("llm& document&tool-call"), + Required: true, + Label: I18nObject{ + ZhHans: "模型", + EnUS: "Model", + }, + HumanDescription: I18nObject{ + ZhHans: "请选择模型", + EnUS: "Please select a model", + }, + LLMDescription: "please select a model", + Form: TOOL_PARAMETER_FORM_FORM, + } + + data := parser.MarshalJsonBytes(config) + + if _, err := parser.UnmarshalJsonBytes[ToolParameter](data); err != nil { + t.Errorf("ParameterScope_Validate() error = %v", err) + } +} diff --git a/internal/types/entities/plugin_entities/tool_declaration.go b/internal/types/entities/plugin_entities/tool_declaration.go index b57332f..5210785 100644 --- a/internal/types/entities/plugin_entities/tool_declaration.go +++ b/internal/types/entities/plugin_entities/tool_declaration.go @@ -83,7 +83,7 @@ type ToolParameter struct { Label I18nObject `json:"label" yaml:"label" validate:"required"` HumanDescription I18nObject `json:"human_description" yaml:"human_description" validate:"required"` Type ToolParameterType `json:"type" yaml:"type" validate:"required,tool_parameter_type"` - Scope *string `json:"scope" yaml:"scope" validate:"omitempty,is_scope"` + Scope *string `json:"scope" yaml:"scope" validate:"omitempty,max=1024,is_scope"` Form ToolParameterForm `json:"form" yaml:"form" validate:"required,tool_parameter_form"` LLMDescription string `json:"llm_description" yaml:"llm_description" validate:"omitempty"` Required bool `json:"required" yaml:"required"`