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

api_entities.py 2.7KB

5 个月前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from typing import Literal, Optional
  2. from pydantic import BaseModel, Field, field_validator
  3. from core.datasource.entities.datasource_entities import DatasourceParameter
  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 ToolProviderType
  8. class DatasourceApiEntity(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"]]
  17. class DatasourceProviderApiEntity(BaseModel):
  18. id: str
  19. author: str
  20. name: str # identifier
  21. description: I18nObject
  22. icon: str | dict
  23. label: I18nObject # label
  24. type: ToolProviderType
  25. masked_credentials: Optional[dict] = None
  26. original_credentials: Optional[dict] = None
  27. is_team_authorization: bool = False
  28. allow_delete: bool = True
  29. plugin_id: Optional[str] = Field(default="", description="The plugin id of the tool")
  30. plugin_unique_identifier: Optional[str] = Field(default="", description="The unique identifier of the tool")
  31. datasources: list[DatasourceApiEntity] = Field(default_factory=list)
  32. labels: list[str] = Field(default_factory=list)
  33. @field_validator("datasources", mode="before")
  34. @classmethod
  35. def convert_none_to_empty_list(cls, v):
  36. return v if v is not None else []
  37. def to_dict(self) -> dict:
  38. # -------------
  39. # overwrite datasource parameter types for temp fix
  40. datasources = jsonable_encoder(self.datasources)
  41. for datasource in datasources:
  42. if datasource.get("parameters"):
  43. for parameter in datasource.get("parameters"):
  44. if parameter.get("type") == DatasourceParameter.DatasourceParameterType.SYSTEM_FILES.value:
  45. parameter["type"] = "files"
  46. # -------------
  47. return {
  48. "id": self.id,
  49. "author": self.author,
  50. "name": self.name,
  51. "plugin_id": self.plugin_id,
  52. "plugin_unique_identifier": self.plugin_unique_identifier,
  53. "description": self.description.to_dict(),
  54. "icon": self.icon,
  55. "label": self.label.to_dict(),
  56. "type": self.type.value,
  57. "team_credentials": self.masked_credentials,
  58. "is_team_authorization": self.is_team_authorization,
  59. "allow_delete": self.allow_delete,
  60. "datasources": datasources,
  61. "labels": self.labels,
  62. }