Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions pkg/entities/plugin_entities/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package plugin_entities

import (
"encoding/json"

"gopkg.in/yaml.v3"
)

const (
SECRET_INPUT = "secret-input"
TEXT_INPUT = "text-input"
Expand All @@ -22,7 +28,46 @@ const (
)

type ParameterOption struct {
Value string `json:"value" yaml:"value" validate:"required"`
Label I18nObject `json:"label" yaml:"label" validate:"required"`
Icon string `json:"icon" yaml:"icon" validate:"omitempty"`
Value string `json:"value" yaml:"value" validate:"required"`
Label I18nObject `json:"label" yaml:"label" validate:"required"`
Icon string `json:"icon" yaml:"icon" validate:"omitempty"`
ShowOn []ToolParameterShowOnObject `json:"show_on" yaml:"show_on" validate:"omitempty,lte=16,dive"`
}

func (p *ParameterOption) UnmarshalJSON(data []byte) error {
type Alias ParameterOption
aux := &struct {
*Alias
}{
Alias: (*Alias)(p),
}

if err := json.Unmarshal(data, aux); err != nil {
return err
}

if p.ShowOn == nil {
p.ShowOn = []ToolParameterShowOnObject{}
}

return nil
}

func (p *ParameterOption) UnmarshalYAML(value *yaml.Node) error {
type Alias ParameterOption
aux := &struct {
*Alias `yaml:",inline"`
}{
Alias: (*Alias)(p),
}

if err := value.Decode(&aux); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of &aux in value.Decode(&aux) is non-idiomatic because aux is already a pointer to the target struct. Passing a pointer to a pointer (**struct) is unnecessary and inconsistent with the json.Unmarshal(data, aux) call used in the UnmarshalJSON method above.

Suggested change
if err := value.Decode(&aux); err != nil {
if err := value.Decode(aux); err != nil {

return err
}

if p.ShowOn == nil {
p.ShowOn = []ToolParameterShowOnObject{}
}

return nil
}
84 changes: 68 additions & 16 deletions pkg/entities/plugin_entities/tool_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,75 @@ type ParameterTemplate struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}

type ToolParameterShowOnObject struct {
Variable string `json:"variable" yaml:"variable" validate:"required,lt=256"`
Value string `json:"value" yaml:"value" validate:"required,lt=256"`
Comment on lines +125 to +126
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The length limit for Variable (lt=256) is inconsistent with the limit for ToolParameter.Name (lt=1024). Since Variable is intended to store a parameter name for conditional visibility, it should support the same maximum length. Similarly, Value should likely have a matching limit to ensure consistency when comparing against parameter values.

	Variable string `json:"variable" yaml:"variable" validate:"required,lt=1024"`\n	Value    string `json:"value" yaml:"value" validate:"required,lt=1024"`

}

type ToolParameter struct {
Name string `json:"name" yaml:"name" validate:"required,gt=0,lt=1024"`
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,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"`
AutoGenerate *ParameterAutoGenerate `json:"auto_generate" yaml:"auto_generate" validate:"omitempty"`
Template *ParameterTemplate `json:"template" yaml:"template" validate:"omitempty"`
Default any `json:"default" yaml:"default" validate:"omitempty"`
Min *float64 `json:"min" yaml:"min" validate:"omitempty"`
Max *float64 `json:"max" yaml:"max" validate:"omitempty"`
Multiple bool `json:"multiple" yaml:"multiple" validate:"omitempty"`
Precision *int `json:"precision" yaml:"precision" validate:"omitempty"`
Options []ParameterOption `json:"options" yaml:"options" validate:"omitempty,dive"`
Name string `json:"name" yaml:"name" validate:"required,gt=0,lt=1024"`
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,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"`
AutoGenerate *ParameterAutoGenerate `json:"auto_generate" yaml:"auto_generate" validate:"omitempty"`
Template *ParameterTemplate `json:"template" yaml:"template" validate:"omitempty"`
Default any `json:"default" yaml:"default" validate:"omitempty"`
Min *float64 `json:"min" yaml:"min" validate:"omitempty"`
Max *float64 `json:"max" yaml:"max" validate:"omitempty"`
Multiple bool `json:"multiple" yaml:"multiple" validate:"omitempty"`
Precision *int `json:"precision" yaml:"precision" validate:"omitempty"`
Options []ParameterOption `json:"options" yaml:"options" validate:"omitempty,dive"`
ShowOn []ToolParameterShowOnObject `json:"show_on" yaml:"show_on" validate:"omitempty,lte=16,dive"`
}

func (t *ToolParameter) UnmarshalJSON(data []byte) error {
type Alias ToolParameter
aux := &struct {
*Alias
}{
Alias: (*Alias)(t),
}

if err := json.Unmarshal(data, aux); err != nil {
return err
}

if t.ShowOn == nil {
t.ShowOn = []ToolParameterShowOnObject{}
}

if t.Options == nil {
t.Options = []ParameterOption{}
}

return nil
}

func (t *ToolParameter) UnmarshalYAML(value *yaml.Node) error {
type Alias ToolParameter
aux := &struct {
*Alias `yaml:",inline"`
}{
Alias: (*Alias)(t),
}

if err := value.Decode(&aux); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of &aux in value.Decode(&aux) is non-idiomatic because aux is already a pointer to the target struct. Passing a pointer to a pointer (**struct) is unnecessary and inconsistent with the json.Unmarshal(data, aux) call used in the UnmarshalJSON method above.

Suggested change
if err := value.Decode(&aux); err != nil {
if err := value.Decode(aux); err != nil {

return err
}

if t.ShowOn == nil {
t.ShowOn = []ToolParameterShowOnObject{}
}

if t.Options == nil {
t.Options = []ParameterOption{}
}

return nil
}

type ToolDescription struct {
Expand Down
Loading