agent_declaration.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package plugin_entities
  2. import (
  3. "encoding/json"
  4. "github.com/go-playground/validator/v10"
  5. "github.com/langgenius/dify-plugin-daemon/pkg/entities/manifest_entities"
  6. "github.com/langgenius/dify-plugin-daemon/pkg/validators"
  7. "gopkg.in/yaml.v3"
  8. )
  9. type AgentStrategyProviderIdentity struct {
  10. ToolProviderIdentity `json:",inline" yaml:",inline"`
  11. }
  12. type AgentStrategyIdentity struct {
  13. ToolIdentity `json:",inline" yaml:",inline"`
  14. }
  15. type AgentStrategyParameterType string
  16. const (
  17. AGENT_STRATEGY_PARAMETER_TYPE_STRING AgentStrategyParameterType = STRING
  18. AGENT_STRATEGY_PARAMETER_TYPE_NUMBER AgentStrategyParameterType = NUMBER
  19. AGENT_STRATEGY_PARAMETER_TYPE_BOOLEAN AgentStrategyParameterType = BOOLEAN
  20. AGENT_STRATEGY_PARAMETER_TYPE_SELECT AgentStrategyParameterType = SELECT
  21. AGENT_STRATEGY_PARAMETER_TYPE_SECRET_INPUT AgentStrategyParameterType = SECRET_INPUT
  22. AGENT_STRATEGY_PARAMETER_TYPE_FILE AgentStrategyParameterType = FILE
  23. AGENT_STRATEGY_PARAMETER_TYPE_FILES AgentStrategyParameterType = FILES
  24. AGENT_STRATEGY_PARAMETER_TYPE_APP_SELECTOR AgentStrategyParameterType = APP_SELECTOR
  25. AGENT_STRATEGY_PARAMETER_TYPE_MODEL_SELECTOR AgentStrategyParameterType = MODEL_SELECTOR
  26. AGENT_STRATEGY_PARAMETER_TYPE_TOOLS_SELECTOR AgentStrategyParameterType = TOOLS_SELECTOR
  27. )
  28. func isAgentStrategyParameterType(fl validator.FieldLevel) bool {
  29. value := fl.Field().String()
  30. switch value {
  31. case string(AGENT_STRATEGY_PARAMETER_TYPE_STRING),
  32. string(AGENT_STRATEGY_PARAMETER_TYPE_NUMBER),
  33. string(AGENT_STRATEGY_PARAMETER_TYPE_BOOLEAN),
  34. string(AGENT_STRATEGY_PARAMETER_TYPE_SELECT),
  35. string(AGENT_STRATEGY_PARAMETER_TYPE_SECRET_INPUT),
  36. string(AGENT_STRATEGY_PARAMETER_TYPE_FILE),
  37. string(AGENT_STRATEGY_PARAMETER_TYPE_FILES),
  38. // string(TOOL_PARAMETER_TYPE_TOOL_SELECTOR),
  39. string(AGENT_STRATEGY_PARAMETER_TYPE_APP_SELECTOR),
  40. string(AGENT_STRATEGY_PARAMETER_TYPE_MODEL_SELECTOR),
  41. string(AGENT_STRATEGY_PARAMETER_TYPE_TOOLS_SELECTOR):
  42. return true
  43. }
  44. return false
  45. }
  46. func init() {
  47. validators.GlobalEntitiesValidator.RegisterValidation("agent_strategy_parameter_type", isAgentStrategyParameterType)
  48. }
  49. type AgentStrategyParameter struct {
  50. Name string `json:"name" yaml:"name" validate:"required,gt=0,lt=1024"`
  51. Label I18nObject `json:"label" yaml:"label" validate:"required"`
  52. Type AgentStrategyParameterType `json:"type" yaml:"type" validate:"required,agent_strategy_parameter_type"`
  53. AutoGenerate *ParameterAutoGenerate `json:"auto_generate" yaml:"auto_generate" validate:"omitempty"`
  54. Template *ParameterTemplate `json:"template" yaml:"template" validate:"omitempty"`
  55. Scope *string `json:"scope" yaml:"scope" validate:"omitempty,max=1024,is_scope"`
  56. Required bool `json:"required" yaml:"required"`
  57. Default any `json:"default" yaml:"default" validate:"omitempty,is_basic_type"`
  58. Min *float64 `json:"min" yaml:"min" validate:"omitempty"`
  59. Max *float64 `json:"max" yaml:"max" validate:"omitempty"`
  60. Precision *int `json:"precision" yaml:"precision" validate:"omitempty"`
  61. Options []ToolParameterOption `json:"options" yaml:"options" validate:"omitempty,dive"`
  62. }
  63. type AgentStrategyOutputSchema map[string]any
  64. type AgentStrategyDeclaration struct {
  65. Identity AgentStrategyIdentity `json:"identity" yaml:"identity" validate:"required"`
  66. Description I18nObject `json:"description" yaml:"description" validate:"required"`
  67. Parameters []AgentStrategyParameter `json:"parameters" yaml:"parameters" validate:"omitempty,dive"`
  68. OutputSchema AgentStrategyOutputSchema `json:"output_schema" yaml:"output_schema" validate:"omitempty,json_schema"`
  69. }
  70. type AgentStrategyProviderDeclaration struct {
  71. Identity AgentStrategyProviderIdentity `json:"identity" yaml:"identity" validate:"required"`
  72. Strategies []AgentStrategyDeclaration `json:"strategies" yaml:"strategies" validate:"required,dive"`
  73. StrategyFiles []string `json:"-" yaml:"-"`
  74. }
  75. func (a *AgentStrategyProviderDeclaration) MarshalJSON() ([]byte, error) {
  76. type alias AgentStrategyProviderDeclaration
  77. p := alias(*a)
  78. if p.Strategies == nil {
  79. p.Strategies = []AgentStrategyDeclaration{}
  80. }
  81. return json.Marshal(p)
  82. }
  83. func (a *AgentStrategyProviderDeclaration) UnmarshalYAML(value *yaml.Node) error {
  84. type alias struct {
  85. Identity AgentStrategyProviderIdentity `yaml:"identity"`
  86. Strategies yaml.Node `yaml:"strategies"`
  87. }
  88. var temp alias
  89. err := value.Decode(&temp)
  90. if err != nil {
  91. return err
  92. }
  93. // apply identity
  94. a.Identity = temp.Identity
  95. if a.StrategyFiles == nil {
  96. a.StrategyFiles = []string{}
  97. }
  98. // unmarshal strategies
  99. if temp.Strategies.Kind == yaml.SequenceNode {
  100. for _, item := range temp.Strategies.Content {
  101. if item.Kind == yaml.ScalarNode {
  102. a.StrategyFiles = append(a.StrategyFiles, item.Value)
  103. } else if item.Kind == yaml.MappingNode {
  104. strategy := AgentStrategyDeclaration{}
  105. if err := item.Decode(&strategy); err != nil {
  106. return err
  107. }
  108. a.Strategies = append(a.Strategies, strategy)
  109. }
  110. }
  111. }
  112. if a.Strategies == nil {
  113. a.Strategies = []AgentStrategyDeclaration{}
  114. }
  115. if a.Identity.Tags == nil {
  116. a.Identity.Tags = []manifest_entities.PluginTag{}
  117. }
  118. return nil
  119. }
  120. func (a *AgentStrategyProviderDeclaration) UnmarshalJSON(data []byte) error {
  121. type alias AgentStrategyProviderDeclaration
  122. var temp struct {
  123. alias
  124. Strategies []json.RawMessage `json:"strategies"`
  125. }
  126. if err := json.Unmarshal(data, &temp); err != nil {
  127. return err
  128. }
  129. *a = AgentStrategyProviderDeclaration(temp.alias)
  130. // unmarshal strategies
  131. for _, item := range temp.Strategies {
  132. strategy := AgentStrategyDeclaration{}
  133. if err := json.Unmarshal(item, &strategy); err != nil {
  134. a.StrategyFiles = append(a.StrategyFiles, string(item))
  135. } else {
  136. a.Strategies = append(a.Strategies, strategy)
  137. }
  138. }
  139. if a.Strategies == nil {
  140. a.Strategies = []AgentStrategyDeclaration{}
  141. }
  142. if a.Identity.Tags == nil {
  143. a.Identity.Tags = []manifest_entities.PluginTag{}
  144. }
  145. return nil
  146. }