Skip to content

Commit

Permalink
feat: support and in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Dec 12, 2024
1 parent 548adf4 commit 27ce99f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
65 changes: 44 additions & 21 deletions internal/types/entities/plugin_entities/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugin_entities

import (
"errors"
"strings"

"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 32 additions & 0 deletions internal/types/entities/plugin_entities/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 27ce99f

Please sign in to comment.