Skip to content

Commit

Permalink
refactor: rename configuration to decalaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Sep 4, 2024
1 parent ea61dc3 commit fd147ea
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions internal/core/plugin_manager/remote_manager/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (s *DifyServer) onMessage(runtime *RemotePluginRuntime, message []byte) {
// registration transferred
runtime.registration_transferred = true
} else if !runtime.tools_registration_transferred {
tools, err := parser.UnmarshalJsonBytes2Slice[plugin_entities.ToolProviderConfiguration](message)
tools, err := parser.UnmarshalJsonBytes2Slice[plugin_entities.ToolProviderDeclaration](message)
if err != nil {
runtime.conn.Write([]byte("tools register failed\n"))
log.Error("tools register failed, error: %v", err)
Expand All @@ -190,7 +190,7 @@ func (s *DifyServer) onMessage(runtime *RemotePluginRuntime, message []byte) {
runtime.Config = declaration
}
} else if !runtime.models_registration_transferred {
models, err := parser.UnmarshalJsonBytes2Slice[plugin_entities.ModelProviderConfiguration](message)
models, err := parser.UnmarshalJsonBytes2Slice[plugin_entities.ModelProviderDeclaration](message)
if err != nil {
runtime.conn.Write([]byte("models register failed\n"))
log.Error("models register failed, error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/core/plugin_packager/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func (p *PluginDecoderHelper) Manifest(decoder PluginDecoder) (plugin_entities.P
return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert endpoint to struct: %s", plugin))
}
case plugin_entities.PROVIDER_TYPE_TOOL:
dec.Tool, err = parser.MapToStruct[plugin_entities.ToolProviderConfiguration](plugin_dec.Provider)
dec.Tool, err = parser.MapToStruct[plugin_entities.ToolProviderDeclaration](plugin_dec.Provider)
if err != nil {
return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert tool to struct: %s", plugin))
}
case plugin_entities.PROVIDER_TYPE_MODEL:
dec.Model, err = parser.MapToStruct[plugin_entities.ModelProviderConfiguration](plugin_dec.Provider)
dec.Model, err = parser.MapToStruct[plugin_entities.ModelProviderDeclaration](plugin_dec.Provider)
if err != nil {
return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert model to struct: %s", plugin))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type ModelPricing struct {
PricePerUnit float64 `json:"price_per_unit" validate:"required"`
}

type ModelConfiguration struct {
type ModelDeclaration struct {
Model string `json:"model" validate:"required,lt=256"`
Label I18nObject `json:"label" validate:"required"`
ModelType ModelType `json:"model_type" validate:"required,model_type"`
Expand Down Expand Up @@ -197,7 +197,7 @@ type ModelProviderHelpEntity struct {
URL I18nObject `json:"url" validate:"required"`
}

type ModelProviderConfiguration struct {
type ModelProviderDeclaration struct {
Provider string `json:"provider" yaml:"provider" validate:"required,lt=256"`
Label I18nObject `json:"label" yaml:"label" validate:"required"`
Description *I18nObject `json:"description" yaml:"description,omitempty" validate:"omitempty"`
Expand All @@ -210,7 +210,7 @@ type ModelProviderConfiguration struct {
Models []string `json:"models" yaml:"models" validate:"required,lte=1024"`
ProviderCredentialSchema *ModelProviderCredentialSchema `json:"provider_credential_schema" yaml:"provider_credential_schema,omitempty" validate:"omitempty"`
ModelCredentialSchema *ModelCredentialSchema `json:"model_credential_schema" yaml:"model_credential_schema,omitempty" validate:"omitempty"`
ModelDeclarations []ModelConfiguration `json:"model_declarations" yaml:"model_declarations,omitempty"`
ModelDeclarations []ModelDeclaration `json:"model_declarations" yaml:"model_declarations,omitempty"`
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestFullFunctionModelProvider_Validate(t *testing.T) {
t.Error(err)
}

_, err = parser.UnmarshalJsonBytes[ModelProviderConfiguration](json_data)
_, err = parser.UnmarshalJsonBytes[ModelProviderDeclaration](json_data)
if err != nil {
t.Errorf("UnmarshalModelProviderConfiguration() error = %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/entities/plugin_entities/plugin_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ type PluginDeclarationWithoutAdvancedFields struct {
type PluginDeclaration struct {
PluginDeclarationWithoutAdvancedFields `yaml:",inline"`
Endpoint *EndpointProviderDeclaration `json:"endpoint,omitempty" yaml:"endpoint,omitempty" validate:"omitempty"`
Model *ModelProviderConfiguration `json:"model,omitempty" yaml:"model,omitempty" validate:"omitempty"`
Tool *ToolProviderConfiguration `json:"tool,omitempty" yaml:"tool,omitempty" validate:"omitempty"`
Model *ModelProviderDeclaration `json:"model,omitempty" yaml:"model,omitempty" validate:"omitempty"`
Tool *ToolProviderDeclaration `json:"tool,omitempty" yaml:"tool,omitempty" validate:"omitempty"`
}

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type ToolDescription struct {

type ToolOutputSchema map[string]any

type ToolConfiguration struct {
type ToolDeclaration struct {
Identity ToolIdentity `json:"identity" validate:"required"`
Description ToolDescription `json:"description" validate:"required"`
Parameters []ToolParameter `json:"parameters" validate:"omitempty,dive"`
Expand All @@ -104,10 +104,7 @@ type ToolConfiguration struct {

func isJSONSchema(fl validator.FieldLevel) bool {
_, err := gojsonschema.NewSchema(gojsonschema.NewGoLoader(fl.Field().Interface()))
if err != nil {
return false
}
return true
return err == nil
}

func init() {
Expand Down Expand Up @@ -168,10 +165,10 @@ type ToolProviderIdentity struct {
Tags []ToolLabel `json:"tags" validate:"required,dive,tool_label"`
}

type ToolProviderConfiguration struct {
type ToolProviderDeclaration struct {
Identity ToolProviderIdentity `json:"identity" validate:"required"`
CredentialsSchema map[string]ProviderConfig `json:"credentials_schema" validate:"omitempty,dive"`
Tools []ToolConfiguration `json:"tools" validate:"required,dive"`
Tools []ToolDeclaration `json:"tools" validate:"required,dive"`
}

func init() {
Expand Down Expand Up @@ -224,8 +221,8 @@ func init() {
validators.GlobalEntitiesValidator.RegisterValidation("is_basic_type", isBasicType)
}

func UnmarshalToolProviderConfiguration(data []byte) (*ToolProviderConfiguration, error) {
obj, err := parser.UnmarshalJsonBytes[ToolProviderConfiguration](data)
func UnmarshalToolProviderConfiguration(data []byte) (*ToolProviderDeclaration, error) {
obj, err := parser.UnmarshalJsonBytes[ToolProviderDeclaration](data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal tool provider configuration: %w", err)
}
Expand Down

0 comments on commit fd147ea

Please sign in to comment.