-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathdatasource_manifest.py
More file actions
196 lines (165 loc) · 6.43 KB
/
Copy pathdatasource_manifest.py
File metadata and controls
196 lines (165 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from enum import Enum, StrEnum
from typing import Any, Union
from pydantic import BaseModel, Field, ValidationInfo, field_validator
from dify_plugin.core.documentation.schema_doc import docs
from dify_plugin.core.utils.yaml_loader import load_yaml_file
from dify_plugin.entities import (
I18nObject,
ParameterAutoGenerate,
ParameterOption,
ParameterTemplate,
)
from dify_plugin.entities.oauth import OAuthSchema
from dify_plugin.entities.provider_config import CommonParameterType, ProviderConfig
@docs(
description="The label of the datasource",
)
class DatasourceLabelEnum(Enum):
SEARCH = "search"
IMAGE = "image"
VIDEOS = "videos"
WEATHER = "weather"
FINANCE = "finance"
DESIGN = "design"
TRAVEL = "travel"
SOCIAL = "social"
NEWS = "news"
MEDICAL = "medical"
PRODUCTIVITY = "productivity"
EDUCATION = "education"
BUSINESS = "business"
ENTERTAINMENT = "entertainment"
UTILITIES = "utilities"
OTHER = "other"
@docs(
name="DatasourceProviderType",
description="Type of datasource provider",
)
class DatasourceProviderType(StrEnum):
"""
Enum class for datasource provider
"""
ONLINE_DOCUMENT = "online_document"
WEBSITE_CRAWL = "website_crawl"
ONLINE_DRIVE = "online_drive"
@docs(
name="DatasourceParameter",
description="Parameter of datasource entity",
)
class DatasourceParameter(BaseModel):
"""
Overrides type
"""
@docs(
description="Datasource parameter type (aliases CommonParameterType values)",
)
class DatasourceParameterType(StrEnum):
STRING = CommonParameterType.STRING.value
NUMBER = CommonParameterType.NUMBER.value
BOOLEAN = CommonParameterType.BOOLEAN.value
SELECT = CommonParameterType.SELECT.value
SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
FILE = CommonParameterType.FILE.value
FILES = CommonParameterType.FILES.value
name: str = Field(..., description="The name of the parameter")
label: I18nObject = Field(..., description="The label presented to the user")
placeholder: I18nObject | None = Field(default=None, description="The placeholder presented to the user")
scope: str | None = None
auto_generate: ParameterAutoGenerate | None = None
template: ParameterTemplate | None = None
required: bool = False
default: Union[float, int, str] | None = None
min: Union[float, int] | None = None
max: Union[float, int] | None = None
precision: int | None = None
options: list[ParameterOption] = Field(default_factory=list)
type: DatasourceParameterType = Field(..., description="The type of the parameter")
description: I18nObject = Field(..., description="The description of the parameter")
@docs(
name="DatasourceIdentity",
description="Identity of datasource entity",
)
class DatasourceIdentity(BaseModel):
author: str = Field(..., description="The author of the datasource")
name: str = Field(..., description="The name of the datasource")
label: I18nObject = Field(..., description="The label of the datasource")
icon: str | None = None
@docs(
name="DatasourceEntityExtra",
description="The extra of the datasource entity",
)
class DatasourceEntityExtra(BaseModel):
class Python(BaseModel):
source: str
python: Python
@docs(
name="Datasource",
description="Datasource entity",
)
class DatasourceEntity(BaseModel):
identity: DatasourceIdentity
parameters: list[DatasourceParameter] = Field(default_factory=list)
description: I18nObject = Field(..., description="The label of the datasource")
output_schema: dict[str, Any] = Field(default_factory=dict, description="Output schema definition")
extra: DatasourceEntityExtra
@field_validator("parameters", mode="before")
@classmethod
def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
return v or []
@docs(
description="Identity of datasource provider",
)
class DatasourceProviderIdentity(BaseModel):
author: str = Field(..., description="The author of the datasource")
name: str = Field(..., description="The name of the datasource")
description: I18nObject = Field(..., description="The description of the datasource")
icon: str = Field(..., description="The icon of the datasource")
label: I18nObject = Field(..., description="The label of the datasource")
tags: list[DatasourceLabelEnum] | None = Field(
default=[],
description="The tags of the datasource",
)
@docs(
name="DatasourceProviderExtra",
description="The extra of the datasource provider",
)
class DatasourceProviderConfigurationExtra(BaseModel):
class Python(BaseModel):
source: str
python: Python
@docs(
name="DatasourceProvider",
description="Manifest of datasource providers",
outside_reference_fields={"datasources": DatasourceEntity},
)
class DatasourceProviderManifest(BaseModel):
"""
Datasource provider entity
"""
identity: DatasourceProviderIdentity = Field(..., description="The identity of the datasource provider")
credentials_schema: list[ProviderConfig] = Field(
default_factory=list, description="The credentials schema of the datasource provider"
)
oauth_schema: OAuthSchema | None = Field(
default=None, description="The OAuth schema of the datasource provider if OAuth is supported"
)
provider_type: DatasourceProviderType = Field(..., description="The type of the datasource provider")
datasources: list[DatasourceEntity] = Field(
default_factory=list, description="The datasources of the datasource provider"
)
extra: DatasourceProviderConfigurationExtra = Field(..., description="The extra of the datasource provider")
@field_validator("datasources", mode="before")
@classmethod
def validate_datasources(cls, value) -> list[DatasourceEntity]:
if not isinstance(value, list):
raise ValueError("datasources should be a list")
datasources: list[DatasourceEntity] = []
for datasource in value:
if not isinstance(datasource, str):
raise ValueError("datasource path should be a string")
try:
file = load_yaml_file(datasource)
datasources.append(DatasourceEntity(**file))
except Exception as e:
raise ValueError(f"Error loading datasource configuration: {e!s}") from e
return datasources