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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. @field_validator("tools", mode="before")
  39. @classmethod
  40. def convert_none_to_empty_list(cls, v):
  41. return v if v is not None else []
  42. def to_dict(self) -> dict:
  43. # -------------
  44. # overwrite tool parameter types for temp fix
  45. tools = jsonable_encoder(self.tools)
  46. for tool in tools:
  47. if tool.get("parameters"):
  48. for parameter in tool.get("parameters"):
  49. if parameter.get("type") == ToolParameter.ToolParameterType.SYSTEM_FILES.value:
  50. parameter["type"] = "files"
  51. if parameter.get("input_schema") is None:
  52. parameter.pop("input_schema", None)
  53. # -------------
  54. optional_fields = self.optional_field("server_url", self.server_url)
  55. if self.type == ToolProviderType.MCP:
  56. optional_fields.update(self.optional_field("updated_at", self.updated_at))
  57. optional_fields.update(self.optional_field("server_identifier", self.server_identifier))
  58. return {
  59. "id": self.id,
  60. "author": self.author,
  61. "name": self.name,
  62. "plugin_id": self.plugin_id,
  63. "plugin_unique_identifier": self.plugin_unique_identifier,
  64. "description": self.description.to_dict(),
  65. "icon": self.icon,
  66. "icon_dark": self.icon_dark,
  67. "label": self.label.to_dict(),
  68. "type": self.type.value,
  69. "team_credentials": self.masked_credentials,
  70. "is_team_authorization": self.is_team_authorization,
  71. "allow_delete": self.allow_delete,
  72. "tools": tools,
  73. "labels": self.labels,
  74. **optional_fields,
  75. }
  76. def optional_field(self, key: str, value: Any) -> dict:
  77. """Return dict with key-value if value is truthy, empty dict otherwise."""
  78. return {key: value} if value else {}
  79. class ToolProviderCredentialApiEntity(BaseModel):
  80. id: str = Field(description="The unique id of the credential")
  81. name: str = Field(description="The name of the credential")
  82. provider: str = Field(description="The provider of the credential")
  83. credential_type: CredentialType = Field(description="The type of the credential")
  84. is_default: bool = Field(
  85. default=False, description="Whether the credential is the default credential for the provider in the workspace"
  86. )
  87. credentials: dict = Field(description="The credentials of the provider")
  88. class ToolProviderCredentialInfoApiEntity(BaseModel):
  89. supported_credential_types: list[str] = Field(description="The supported credential types of the provider")
  90. is_oauth_custom_client_enabled: bool = Field(
  91. default=False, description="Whether the OAuth custom client is enabled for the provider"
  92. )
  93. credentials: list[ToolProviderCredentialApiEntity] = Field(description="The credentials of the provider")