agent_declaration.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Features []string `json:"features" yaml:"features" validate:"omitempty,dive,lt=256"`
  70. }
  71. type AgentStrategyProviderDeclaration struct {
  72. Identity AgentStrategyProviderIdentity `json:"identity" yaml:"identity" validate:"required"`
  73. Strategies []AgentStrategyDeclaration `json:"strategies" yaml:"strategies" validate:"required,dive"`
  74. StrategyFiles []string `json:"-" yaml:"-"`
  75. }
  76. func (a *AgentStrategyProviderDeclaration) MarshalJSON() ([]byte, error) {
  77. type alias AgentStrategyProviderDeclaration
  78. p := alias(*a)
  79. if p.Strategies == nil {
  80. p.Strategies = []AgentStrategyDeclaration{}
  81. }
  82. for i := range p.Strategies {
  83. if p.Strategies[i].Features == nil {
  84. p.Strategies[i].Features = []string{}
  85. }
  86. }
  87. return json.Marshal(p)
  88. }
  89. func (a *AgentStrategyProviderDeclaration) UnmarshalYAML(value *yaml.Node) error {
  90. type alias struct {
  91. Identity AgentStrategyProviderIdentity `yaml:"identity"`
  92. Strategies yaml.Node `yaml:"strategies"`
  93. }
  94. var temp alias
  95. err := value.Decode(&temp)
  96. if err != nil {
  97. return err
  98. }
  99. // apply identity
  100. a.Identity = temp.Identity
  101. if a.StrategyFiles == nil {
  102. a.StrategyFiles = []string{}
  103. }
  104. // unmarshal strategies
  105. if temp.Strategies.Kind == yaml.SequenceNode {
  106. for _, item := range temp.Strategies.Content {
  107. if item.Kind == yaml.ScalarNode {
  108. a.StrategyFiles = append(a.StrategyFiles, item.Value)
  109. } else if item.Kind == yaml.MappingNode {
  110. strategy := AgentStrategyDeclaration{}
  111. if err := item.Decode(&strategy); err != nil {
  112. return err
  113. }
  114. a.Strategies = append(a.Strategies, strategy)
  115. }
  116. }
  117. }
  118. if a.Strategies == nil {
  119. a.Strategies = []AgentStrategyDeclaration{}
  120. }
  121. if a.Identity.Tags == nil {
  122. a.Identity.Tags = []manifest_entities.PluginTag{}
  123. }
  124. return nil
  125. }
  126. func (a *AgentStrategyProviderDeclaration) UnmarshalJSON(data []byte) error {
  127. type alias AgentStrategyProviderDeclaration
  128. var temp struct {
  129. alias
  130. Strategies []json.RawMessage `json:"strategies"`
  131. }
  132. if err := json.Unmarshal(data, &temp); err != nil {
  133. return err
  134. }
  135. *a = AgentStrategyProviderDeclaration(temp.alias)
  136. // unmarshal strategies
  137. for _, item := range temp.Strategies {
  138. strategy := AgentStrategyDeclaration{}
  139. if err := json.Unmarshal(item, &strategy); err != nil {
  140. a.StrategyFiles = append(a.StrategyFiles, string(item))
  141. } else {
  142. a.Strategies = append(a.Strategies, strategy)
  143. }
  144. }
  145. if a.Strategies == nil {
  146. a.Strategies = []AgentStrategyDeclaration{}
  147. }
  148. if a.Identity.Tags == nil {
  149. a.Identity.Tags = []manifest_entities.PluginTag{}
  150. }
  151. return nil
  152. }