您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tool_entities.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import base64
  2. import contextlib
  3. from collections.abc import Mapping
  4. from enum import StrEnum, auto
  5. from typing import Any, Union
  6. from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_serializer, field_validator, model_validator
  7. from core.entities.provider_entities import ProviderConfig
  8. from core.plugin.entities.parameters import (
  9. MCPServerParameterType,
  10. PluginParameter,
  11. PluginParameterOption,
  12. PluginParameterType,
  13. as_normal_type,
  14. cast_parameter_value,
  15. init_frontend_parameter,
  16. )
  17. from core.rag.entities.citation_metadata import RetrievalSourceMetadata
  18. from core.tools.entities.common_entities import I18nObject
  19. from core.tools.entities.constants import TOOL_SELECTOR_MODEL_IDENTITY
  20. class ToolLabelEnum(StrEnum):
  21. SEARCH = "search"
  22. IMAGE = "image"
  23. VIDEOS = "videos"
  24. WEATHER = "weather"
  25. FINANCE = "finance"
  26. DESIGN = "design"
  27. TRAVEL = "travel"
  28. SOCIAL = "social"
  29. NEWS = "news"
  30. MEDICAL = "medical"
  31. PRODUCTIVITY = "productivity"
  32. EDUCATION = "education"
  33. BUSINESS = "business"
  34. ENTERTAINMENT = "entertainment"
  35. UTILITIES = "utilities"
  36. RAG = "rag"
  37. OTHER = "other"
  38. class ToolProviderType(StrEnum):
  39. """
  40. Enum class for tool provider
  41. """
  42. PLUGIN = auto()
  43. BUILT_IN = "builtin"
  44. WORKFLOW = auto()
  45. API = auto()
  46. APP = auto()
  47. DATASET_RETRIEVAL = "dataset-retrieval"
  48. MCP = auto()
  49. @classmethod
  50. def value_of(cls, value: str) -> "ToolProviderType":
  51. """
  52. Get value of given mode.
  53. :param value: mode value
  54. :return: mode
  55. """
  56. for mode in cls:
  57. if mode.value == value:
  58. return mode
  59. raise ValueError(f"invalid mode value {value}")
  60. class ApiProviderSchemaType(StrEnum):
  61. """
  62. Enum class for api provider schema type.
  63. """
  64. OPENAPI = auto()
  65. SWAGGER = auto()
  66. OPENAI_PLUGIN = auto()
  67. OPENAI_ACTIONS = auto()
  68. @classmethod
  69. def value_of(cls, value: str) -> "ApiProviderSchemaType":
  70. """
  71. Get value of given mode.
  72. :param value: mode value
  73. :return: mode
  74. """
  75. for mode in cls:
  76. if mode.value == value:
  77. return mode
  78. raise ValueError(f"invalid mode value {value}")
  79. class ApiProviderAuthType(StrEnum):
  80. """
  81. Enum class for api provider auth type.
  82. """
  83. NONE = auto()
  84. API_KEY_HEADER = auto()
  85. API_KEY_QUERY = auto()
  86. @classmethod
  87. def value_of(cls, value: str) -> "ApiProviderAuthType":
  88. """
  89. Get value of given mode.
  90. :param value: mode value
  91. :return: mode
  92. """
  93. # 'api_key' deprecated in PR #21656
  94. # normalize & tiny alias for backward compatibility
  95. v = (value or "").strip().lower()
  96. if v == "api_key":
  97. v = cls.API_KEY_HEADER.value
  98. for mode in cls:
  99. if mode.value == v:
  100. return mode
  101. valid = ", ".join(m.value for m in cls)
  102. raise ValueError(f"invalid mode value '{value}', expected one of: {valid}")
  103. class ToolInvokeMessage(BaseModel):
  104. class TextMessage(BaseModel):
  105. text: str
  106. class JsonMessage(BaseModel):
  107. json_object: dict
  108. class BlobMessage(BaseModel):
  109. blob: bytes
  110. class BlobChunkMessage(BaseModel):
  111. id: str = Field(..., description="The id of the blob")
  112. sequence: int = Field(..., description="The sequence of the chunk")
  113. total_length: int = Field(..., description="The total length of the blob")
  114. blob: bytes = Field(..., description="The blob data of the chunk")
  115. end: bool = Field(..., description="Whether the chunk is the last chunk")
  116. class FileMessage(BaseModel):
  117. pass
  118. class VariableMessage(BaseModel):
  119. variable_name: str = Field(..., description="The name of the variable")
  120. variable_value: Any = Field(..., description="The value of the variable")
  121. stream: bool = Field(default=False, description="Whether the variable is streamed")
  122. @model_validator(mode="before")
  123. @classmethod
  124. def transform_variable_value(cls, values):
  125. """
  126. Only basic types and lists are allowed.
  127. """
  128. value = values.get("variable_value")
  129. if not isinstance(value, dict | list | str | int | float | bool):
  130. raise ValueError("Only basic types and lists are allowed.")
  131. # if stream is true, the value must be a string
  132. if values.get("stream"):
  133. if not isinstance(value, str):
  134. raise ValueError("When 'stream' is True, 'variable_value' must be a string.")
  135. return values
  136. @field_validator("variable_name", mode="before")
  137. @classmethod
  138. def transform_variable_name(cls, value: str) -> str:
  139. """
  140. The variable name must be a string.
  141. """
  142. if value in {"json", "text", "files"}:
  143. raise ValueError(f"The variable name '{value}' is reserved.")
  144. return value
  145. class LogMessage(BaseModel):
  146. class LogStatus(StrEnum):
  147. START = auto()
  148. ERROR = auto()
  149. SUCCESS = auto()
  150. id: str
  151. label: str = Field(..., description="The label of the log")
  152. parent_id: str | None = Field(default=None, description="Leave empty for root log")
  153. error: str | None = Field(default=None, description="The error message")
  154. status: LogStatus = Field(..., description="The status of the log")
  155. data: Mapping[str, Any] = Field(..., description="Detailed log data")
  156. metadata: Mapping[str, Any] = Field(default_factory=dict, description="The metadata of the log")
  157. class RetrieverResourceMessage(BaseModel):
  158. retriever_resources: list[RetrievalSourceMetadata] = Field(..., description="retriever resources")
  159. context: str = Field(..., description="context")
  160. class MessageType(StrEnum):
  161. TEXT = auto()
  162. IMAGE = auto()
  163. LINK = auto()
  164. BLOB = auto()
  165. JSON = auto()
  166. IMAGE_LINK = auto()
  167. BINARY_LINK = auto()
  168. VARIABLE = auto()
  169. FILE = auto()
  170. LOG = auto()
  171. BLOB_CHUNK = auto()
  172. RETRIEVER_RESOURCES = auto()
  173. type: MessageType = MessageType.TEXT
  174. """
  175. plain text, image url or link url
  176. """
  177. message: (
  178. JsonMessage
  179. | TextMessage
  180. | BlobChunkMessage
  181. | BlobMessage
  182. | LogMessage
  183. | FileMessage
  184. | None
  185. | VariableMessage
  186. | RetrieverResourceMessage
  187. )
  188. meta: dict[str, Any] | None = None
  189. @field_validator("message", mode="before")
  190. @classmethod
  191. def decode_blob_message(cls, v):
  192. if isinstance(v, dict) and "blob" in v:
  193. with contextlib.suppress(Exception):
  194. v["blob"] = base64.b64decode(v["blob"])
  195. return v
  196. @field_serializer("message")
  197. def serialize_message(self, v):
  198. if isinstance(v, self.BlobMessage):
  199. return {"blob": base64.b64encode(v.blob).decode("utf-8")}
  200. return v
  201. class ToolInvokeMessageBinary(BaseModel):
  202. mimetype: str = Field(..., description="The mimetype of the binary")
  203. url: str = Field(..., description="The url of the binary")
  204. file_var: dict[str, Any] | None = None
  205. class ToolParameter(PluginParameter):
  206. """
  207. Overrides type
  208. """
  209. class ToolParameterType(StrEnum):
  210. """
  211. removes TOOLS_SELECTOR from PluginParameterType
  212. """
  213. STRING = PluginParameterType.STRING
  214. NUMBER = PluginParameterType.NUMBER
  215. BOOLEAN = PluginParameterType.BOOLEAN
  216. SELECT = PluginParameterType.SELECT
  217. SECRET_INPUT = PluginParameterType.SECRET_INPUT
  218. FILE = PluginParameterType.FILE
  219. FILES = PluginParameterType.FILES
  220. APP_SELECTOR = PluginParameterType.APP_SELECTOR
  221. MODEL_SELECTOR = PluginParameterType.MODEL_SELECTOR
  222. ANY = PluginParameterType.ANY
  223. DYNAMIC_SELECT = PluginParameterType.DYNAMIC_SELECT
  224. # MCP object and array type parameters
  225. ARRAY = MCPServerParameterType.ARRAY
  226. OBJECT = MCPServerParameterType.OBJECT
  227. # deprecated, should not use.
  228. SYSTEM_FILES = PluginParameterType.SYSTEM_FILES
  229. def as_normal_type(self):
  230. return as_normal_type(self)
  231. def cast_value(self, value: Any):
  232. return cast_parameter_value(self, value)
  233. class ToolParameterForm(StrEnum):
  234. SCHEMA = auto() # should be set while adding tool
  235. FORM = auto() # should be set before invoking tool
  236. LLM = auto() # will be set by LLM
  237. type: ToolParameterType = Field(..., description="The type of the parameter")
  238. human_description: I18nObject | None = Field(default=None, description="The description presented to the user")
  239. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  240. llm_description: str | None = None
  241. # MCP object and array type parameters use this field to store the schema
  242. input_schema: dict | None = None
  243. @classmethod
  244. def get_simple_instance(
  245. cls,
  246. name: str,
  247. llm_description: str,
  248. typ: ToolParameterType,
  249. required: bool,
  250. options: list[str] | None = None,
  251. ) -> "ToolParameter":
  252. """
  253. get a simple tool parameter
  254. :param name: the name of the parameter
  255. :param llm_description: the description presented to the LLM
  256. :param typ: the type of the parameter
  257. :param required: if the parameter is required
  258. :param options: the options of the parameter
  259. """
  260. # convert options to ToolParameterOption
  261. if options:
  262. option_objs = [
  263. PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option))
  264. for option in options
  265. ]
  266. else:
  267. option_objs = []
  268. return cls(
  269. name=name,
  270. label=I18nObject(en_US="", zh_Hans=""),
  271. placeholder=None,
  272. human_description=I18nObject(en_US="", zh_Hans=""),
  273. type=typ,
  274. form=cls.ToolParameterForm.LLM,
  275. llm_description=llm_description,
  276. required=required,
  277. options=option_objs,
  278. )
  279. def init_frontend_parameter(self, value: Any):
  280. return init_frontend_parameter(self, self.type, value)
  281. class ToolProviderIdentity(BaseModel):
  282. author: str = Field(..., description="The author of the tool")
  283. name: str = Field(..., description="The name of the tool")
  284. description: I18nObject = Field(..., description="The description of the tool")
  285. icon: str = Field(..., description="The icon of the tool")
  286. icon_dark: str | None = Field(default=None, description="The dark icon of the tool")
  287. label: I18nObject = Field(..., description="The label of the tool")
  288. tags: list[ToolLabelEnum] | None = Field(
  289. default=[],
  290. description="The tags of the tool",
  291. )
  292. class ToolIdentity(BaseModel):
  293. author: str = Field(..., description="The author of the tool")
  294. name: str = Field(..., description="The name of the tool")
  295. label: I18nObject = Field(..., description="The label of the tool")
  296. provider: str = Field(..., description="The provider of the tool")
  297. icon: str | None = None
  298. class ToolDescription(BaseModel):
  299. human: I18nObject = Field(..., description="The description presented to the user")
  300. llm: str = Field(..., description="The description presented to the LLM")
  301. class ToolEntity(BaseModel):
  302. identity: ToolIdentity
  303. parameters: list[ToolParameter] = Field(default_factory=list[ToolParameter])
  304. description: ToolDescription | None = None
  305. output_schema: Mapping[str, object] = Field(default_factory=dict)
  306. has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters")
  307. # pydantic configs
  308. model_config = ConfigDict(protected_namespaces=())
  309. @field_validator("parameters", mode="before")
  310. @classmethod
  311. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[ToolParameter]:
  312. return v or []
  313. class OAuthSchema(BaseModel):
  314. client_schema: list[ProviderConfig] = Field(
  315. default_factory=list[ProviderConfig], description="The schema of the OAuth client"
  316. )
  317. credentials_schema: list[ProviderConfig] = Field(
  318. default_factory=list[ProviderConfig], description="The schema of the OAuth credentials"
  319. )
  320. class ToolProviderEntity(BaseModel):
  321. identity: ToolProviderIdentity
  322. plugin_id: str | None = None
  323. credentials_schema: list[ProviderConfig] = Field(default_factory=list[ProviderConfig])
  324. oauth_schema: OAuthSchema | None = None
  325. class ToolProviderEntityWithPlugin(ToolProviderEntity):
  326. tools: list[ToolEntity] = Field(default_factory=list[ToolEntity])
  327. class WorkflowToolParameterConfiguration(BaseModel):
  328. """
  329. Workflow tool configuration
  330. """
  331. name: str = Field(..., description="The name of the parameter")
  332. description: str = Field(..., description="The description of the parameter")
  333. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  334. class ToolInvokeMeta(BaseModel):
  335. """
  336. Tool invoke meta
  337. """
  338. time_cost: float = Field(..., description="The time cost of the tool invoke")
  339. error: str | None = None
  340. tool_config: dict | None = None
  341. @classmethod
  342. def empty(cls) -> "ToolInvokeMeta":
  343. """
  344. Get an empty instance of ToolInvokeMeta
  345. """
  346. return cls(time_cost=0.0, error=None, tool_config={})
  347. @classmethod
  348. def error_instance(cls, error: str) -> "ToolInvokeMeta":
  349. """
  350. Get an instance of ToolInvokeMeta with error
  351. """
  352. return cls(time_cost=0.0, error=error, tool_config={})
  353. def to_dict(self):
  354. return {
  355. "time_cost": self.time_cost,
  356. "error": self.error,
  357. "tool_config": self.tool_config,
  358. }
  359. class ToolLabel(BaseModel):
  360. """
  361. Tool label
  362. """
  363. name: str = Field(..., description="The name of the tool")
  364. label: I18nObject = Field(..., description="The label of the tool")
  365. icon: str = Field(..., description="The icon of the tool")
  366. class ToolInvokeFrom(StrEnum):
  367. """
  368. Enum class for tool invoke
  369. """
  370. WORKFLOW = auto()
  371. AGENT = auto()
  372. PLUGIN = auto()
  373. class ToolSelector(BaseModel):
  374. dify_model_identity: str = TOOL_SELECTOR_MODEL_IDENTITY
  375. class Parameter(BaseModel):
  376. name: str = Field(..., description="The name of the parameter")
  377. type: ToolParameter.ToolParameterType = Field(..., description="The type of the parameter")
  378. required: bool = Field(..., description="Whether the parameter is required")
  379. description: str = Field(..., description="The description of the parameter")
  380. default: Union[int, float, str] | None = None
  381. options: list[PluginParameterOption] | None = None
  382. provider_id: str = Field(..., description="The id of the provider")
  383. credential_id: str | None = Field(default=None, description="The id of the credential")
  384. tool_name: str = Field(..., description="The name of the tool")
  385. tool_description: str = Field(..., description="The description of the tool")
  386. tool_configuration: Mapping[str, Any] = Field(..., description="Configuration, type form")
  387. tool_parameters: Mapping[str, Parameter] = Field(..., description="Parameters, type llm")
  388. def to_plugin_parameter(self) -> dict[str, Any]:
  389. return self.model_dump()
  390. class CredentialType(StrEnum):
  391. API_KEY = "api-key"
  392. OAUTH2 = auto()
  393. def get_name(self):
  394. if self == CredentialType.API_KEY:
  395. return "API KEY"
  396. elif self == CredentialType.OAUTH2:
  397. return "AUTH"
  398. else:
  399. return self.value.replace("-", " ").upper()
  400. def is_editable(self):
  401. return self == CredentialType.API_KEY
  402. def is_validate_allowed(self):
  403. return self == CredentialType.API_KEY
  404. @classmethod
  405. def values(cls):
  406. return [item.value for item in cls]
  407. @classmethod
  408. def of(cls, credential_type: str) -> "CredentialType":
  409. type_name = credential_type.lower()
  410. if type_name in {"api-key", "api_key"}:
  411. return cls.API_KEY
  412. elif type_name in {"oauth2", "oauth"}:
  413. return cls.OAUTH2
  414. else:
  415. raise ValueError(f"Invalid credential type: {credential_type}")