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

api_entities.py 2.7KB

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