dify/api/core/agent/plugin_entities.py

90 lines
2.8 KiB
Python
Raw Normal View History

2024-12-12 19:16:06 +08:00
import enum
2024-12-13 19:50:54 +08:00
from typing import Any, Optional
2024-12-09 23:02:25 +08:00
2024-12-09 23:02:11 +08:00
from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator
2024-12-12 19:16:06 +08:00
from core.entities.parameter_entities import CommonParameterType
2024-12-13 19:50:54 +08:00
from core.plugin.entities.parameters import (
PluginParameter,
as_normal_type,
cast_parameter_value,
init_frontend_parameter,
)
2024-12-09 23:02:11 +08:00
from core.tools.entities.common_entities import I18nObject
2024-12-12 19:16:06 +08:00
from core.tools.entities.tool_entities import (
ToolIdentity,
ToolProviderIdentity,
)
2024-12-09 23:02:11 +08:00
class AgentStrategyProviderIdentity(ToolProviderIdentity):
2024-12-13 19:50:54 +08:00
"""
Inherits from ToolProviderIdentity, without any additional fields.
"""
2024-12-09 23:02:11 +08:00
pass
2024-12-13 19:50:54 +08:00
class AgentStrategyParameter(PluginParameter):
2024-12-12 19:16:06 +08:00
class AgentStrategyParameterType(enum.StrEnum):
2024-12-13 19:50:54 +08:00
"""
Keep all the types from PluginParameterType
"""
2024-12-12 19:16:06 +08:00
STRING = CommonParameterType.STRING.value
NUMBER = CommonParameterType.NUMBER.value
BOOLEAN = CommonParameterType.BOOLEAN.value
SELECT = CommonParameterType.SELECT.value
SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
FILE = CommonParameterType.FILE.value
FILES = CommonParameterType.FILES.value
APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR.value
# deprecated, should not use.
SYSTEM_FILES = CommonParameterType.SYSTEM_FILES.value
def as_normal_type(self):
2024-12-13 19:50:54 +08:00
return as_normal_type(self)
2024-12-12 19:16:06 +08:00
2024-12-13 19:50:54 +08:00
def cast_value(self, value: Any):
return cast_parameter_value(self, value)
2024-12-12 19:16:06 +08:00
2024-12-13 19:50:54 +08:00
type: AgentStrategyParameterType = Field(..., description="The type of the parameter")
def init_frontend_parameter(self, value: Any):
return init_frontend_parameter(self, self.type, value)
2024-12-09 23:02:11 +08:00
class AgentStrategyProviderEntity(BaseModel):
identity: AgentStrategyProviderIdentity
2024-12-09 23:02:11 +08:00
plugin_id: Optional[str] = Field(None, description="The id of the plugin")
class AgentStrategyIdentity(ToolIdentity):
2024-12-13 19:50:54 +08:00
"""
Inherits from ToolIdentity, without any additional fields.
"""
2024-12-09 23:02:11 +08:00
pass
class AgentStrategyEntity(BaseModel):
identity: AgentStrategyIdentity
parameters: list[AgentStrategyParameter] = Field(default_factory=list)
2024-12-09 23:02:11 +08:00
description: I18nObject = Field(..., description="The description of the agent strategy")
output_schema: Optional[dict] = None
# pydantic configs
model_config = ConfigDict(protected_namespaces=())
@field_validator("parameters", mode="before")
@classmethod
def set_parameters(cls, v, validation_info: ValidationInfo) -> list[AgentStrategyParameter]:
2024-12-09 23:02:11 +08:00
return v or []
class AgentProviderEntityWithPlugin(AgentStrategyProviderEntity):
2024-12-09 23:02:11 +08:00
strategies: list[AgentStrategyEntity] = Field(default_factory=list)