You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_entities.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from datetime import datetime
  2. from typing import Any, Literal, Optional
  3. from pydantic import BaseModel, Field, field_validator
  4. from core.model_runtime.utils.encoders import jsonable_encoder
  5. from core.tools.__base.tool import ToolParameter
  6. from core.tools.entities.common_entities import I18nObject
  7. from core.tools.entities.tool_entities import CredentialType, ToolProviderType
  8. class ToolApiEntity(BaseModel):
  9. author: str
  10. name: str # identifier
  11. label: I18nObject # label
  12. description: I18nObject
  13. parameters: Optional[list[ToolParameter]] = None
  14. labels: list[str] = Field(default_factory=list)
  15. output_schema: Optional[dict] = None
  16. ToolProviderTypeApiLiteral = Optional[Literal["builtin", "api", "workflow", "mcp"]]
  17. class ToolProviderApiEntity(BaseModel):
  18. id: str
  19. author: str
  20. name: str # identifier
  21. description: I18nObject
  22. icon: str | dict
  23. icon_dark: Optional[str | dict] = Field(default=None, description="The dark icon of the tool")
  24. label: I18nObject # label
  25. type: ToolProviderType
  26. masked_credentials: Optional[dict] = None
  27. original_credentials: Optional[dict] = None
  28. is_team_authorization: bool = False
  29. allow_delete: bool = True
  30. plugin_id: Optional[str] = Field(default="", description="The plugin id of the tool")
  31. plugin_unique_identifier: Optional[str] = Field(default="", description="The unique identifier of the tool")
  32. tools: list[ToolApiEntity] = Field(default_factory=list)
  33. labels: list[str] = Field(default_factory=list)
  34. # MCP
  35. server_url: Optional[str] = Field(default="", description="The server url of the tool")
  36. updated_at: int = Field(default_factory=lambda: int(datetime.now().timestamp()))
  37. server_identifier: Optional[str] = Field(default="", description="The server identifier of the MCP tool")
  38. timeout: Optional[float] = Field(default=30.0, description="The timeout of the MCP tool")
  39. sse_read_timeout: Optional[float] = Field(default=300.0, description="The SSE read timeout of the MCP tool")
  40. masked_headers: Optional[dict[str, str]] = Field(default=None, description="The masked headers of the MCP tool")
  41. original_headers: Optional[dict[str, str]] = Field(default=None, description="The original headers of the MCP tool")
  42. @field_validator("tools", mode="before")
  43. @classmethod
  44. def convert_none_to_empty_list(cls, v):
  45. return v if v is not None else []
  46. def to_dict(self):
  47. # -------------
  48. # overwrite tool parameter types for temp fix
  49. tools = jsonable_encoder(self.tools)
  50. for tool in tools:
  51. if tool.get("parameters"):
  52. for parameter in tool.get("parameters"):
  53. if parameter.get("type") == ToolParameter.ToolParameterType.SYSTEM_FILES.value:
  54. parameter["type"] = "files"
  55. if parameter.get("input_schema") is None:
  56. parameter.pop("input_schema", None)
  57. # -------------
  58. optional_fields = self.optional_field("server_url", self.server_url)
  59. if self.type == ToolProviderType.MCP:
  60. optional_fields.update(self.optional_field("updated_at", self.updated_at))
  61. optional_fields.update(self.optional_field("server_identifier", self.server_identifier))
  62. optional_fields.update(self.optional_field("timeout", self.timeout))
  63. optional_fields.update(self.optional_field("sse_read_timeout", self.sse_read_timeout))
  64. optional_fields.update(self.optional_field("masked_headers", self.masked_headers))
  65. optional_fields.update(self.optional_field("original_headers", self.original_headers))
  66. return {
  67. "id": self.id,
  68. "author": self.author,
  69. "name": self.name,
  70. "plugin_id": self.plugin_id,
  71. "plugin_unique_identifier": self.plugin_unique_identifier,
  72. "description": self.description.to_dict(),
  73. "icon": self.icon,
  74. "icon_dark": self.icon_dark,
  75. "label": self.label.to_dict(),
  76. "type": self.type.value,
  77. "team_credentials": self.masked_credentials,
  78. "is_team_authorization": self.is_team_authorization,
  79. "allow_delete": self.allow_delete,
  80. "tools": tools,
  81. "labels": self.labels,
  82. **optional_fields,
  83. }
  84. def optional_field(self, key: str, value: Any):
  85. """Return dict with key-value if value is truthy, empty dict otherwise."""
  86. return {key: value} if value else {}
  87. class ToolProviderCredentialApiEntity(BaseModel):
  88. id: str = Field(description="The unique id of the credential")
  89. name: str = Field(description="The name of the credential")
  90. provider: str = Field(description="The provider of the credential")
  91. credential_type: CredentialType = Field(description="The type of the credential")
  92. is_default: bool = Field(
  93. default=False, description="Whether the credential is the default credential for the provider in the workspace"
  94. )
  95. credentials: dict = Field(description="The credentials of the provider")
  96. class ToolProviderCredentialInfoApiEntity(BaseModel):
  97. supported_credential_types: list[str] = Field(description="The supported credential types of the provider")
  98. is_oauth_custom_client_enabled: bool = Field(
  99. default=False, description="Whether the OAuth custom client is enabled for the provider"
  100. )
  101. credentials: list[ToolProviderCredentialApiEntity] = Field(description="The credentials of the provider")