plugin_entities.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import enum
  2. from typing import Any, Optional
  3. from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator
  4. from core.entities.parameter_entities import CommonParameterType
  5. from core.plugin.entities.parameters import (
  6. PluginParameter,
  7. as_normal_type,
  8. cast_parameter_value,
  9. init_frontend_parameter,
  10. )
  11. from core.tools.entities.common_entities import I18nObject
  12. from core.tools.entities.tool_entities import (
  13. ToolIdentity,
  14. ToolProviderIdentity,
  15. )
  16. class AgentStrategyProviderIdentity(ToolProviderIdentity):
  17. """
  18. Inherits from ToolProviderIdentity, without any additional fields.
  19. """
  20. pass
  21. class AgentStrategyParameter(PluginParameter):
  22. class AgentStrategyParameterType(enum.StrEnum):
  23. """
  24. Keep all the types from PluginParameterType
  25. """
  26. STRING = CommonParameterType.STRING.value
  27. NUMBER = CommonParameterType.NUMBER.value
  28. BOOLEAN = CommonParameterType.BOOLEAN.value
  29. SELECT = CommonParameterType.SELECT.value
  30. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  31. FILE = CommonParameterType.FILE.value
  32. FILES = CommonParameterType.FILES.value
  33. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  34. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
  35. TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR.value
  36. # deprecated, should not use.
  37. SYSTEM_FILES = CommonParameterType.SYSTEM_FILES.value
  38. def as_normal_type(self):
  39. return as_normal_type(self)
  40. def cast_value(self, value: Any):
  41. return cast_parameter_value(self, value)
  42. type: AgentStrategyParameterType = Field(..., description="The type of the parameter")
  43. def init_frontend_parameter(self, value: Any):
  44. return init_frontend_parameter(self, self.type, value)
  45. class AgentStrategyProviderEntity(BaseModel):
  46. identity: AgentStrategyProviderIdentity
  47. plugin_id: Optional[str] = Field(None, description="The id of the plugin")
  48. class AgentStrategyIdentity(ToolIdentity):
  49. """
  50. Inherits from ToolIdentity, without any additional fields.
  51. """
  52. pass
  53. class AgentStrategyEntity(BaseModel):
  54. identity: AgentStrategyIdentity
  55. parameters: list[AgentStrategyParameter] = Field(default_factory=list)
  56. description: I18nObject = Field(..., description="The description of the agent strategy")
  57. output_schema: Optional[dict] = None
  58. # pydantic configs
  59. model_config = ConfigDict(protected_namespaces=())
  60. @field_validator("parameters", mode="before")
  61. @classmethod
  62. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[AgentStrategyParameter]:
  63. return v or []
  64. class AgentProviderEntityWithPlugin(AgentStrategyProviderEntity):
  65. strategies: list[AgentStrategyEntity] = Field(default_factory=list)