dify/api/core/datasource/entities/datasource_entities.py

310 lines
8.9 KiB
Python
Raw Normal View History

2025-04-17 15:07:23 +08:00
import enum
from enum import Enum
from typing import Any, Optional
2025-04-17 15:07:23 +08:00
from pydantic import BaseModel, Field, ValidationInfo, field_validator
2025-04-17 15:07:23 +08:00
2025-05-23 17:11:56 +08:00
from core.entities.provider_entities import ProviderConfig
from core.plugin.entities.oauth import OAuthSchema
2025-04-17 15:07:23 +08:00
from core.plugin.entities.parameters import (
PluginParameter,
PluginParameterOption,
PluginParameterType,
as_normal_type,
cast_parameter_value,
init_frontend_parameter,
)
from core.tools.entities.common_entities import I18nObject
2025-05-23 17:35:26 +08:00
from core.tools.entities.tool_entities import ToolLabelEnum
2025-04-17 15:07:23 +08:00
2025-04-22 16:08:58 +08:00
class DatasourceProviderType(enum.StrEnum):
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Enum class for datasource provider
2025-04-17 15:07:23 +08:00
"""
ONLINE_DOCUMENT = "online_document"
LOCAL_FILE = "local_file"
2025-05-23 00:05:57 +08:00
WEBSITE_CRAWL = "website_crawl"
2025-04-17 15:07:23 +08:00
@classmethod
2025-04-22 16:08:58 +08:00
def value_of(cls, value: str) -> "DatasourceProviderType":
2025-04-17 15:07:23 +08:00
"""
Get value of given mode.
:param value: mode value
:return: mode
"""
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f"invalid mode value {value}")
2025-04-22 16:08:58 +08:00
class DatasourceParameter(PluginParameter):
2025-04-17 15:07:23 +08:00
"""
Overrides type
"""
2025-04-22 16:08:58 +08:00
class DatasourceParameterType(enum.StrEnum):
2025-04-17 15:07:23 +08:00
"""
removes TOOLS_SELECTOR from PluginParameterType
"""
STRING = PluginParameterType.STRING.value
NUMBER = PluginParameterType.NUMBER.value
BOOLEAN = PluginParameterType.BOOLEAN.value
SELECT = PluginParameterType.SELECT.value
SECRET_INPUT = PluginParameterType.SECRET_INPUT.value
FILE = PluginParameterType.FILE.value
FILES = PluginParameterType.FILES.value
# deprecated, should not use.
SYSTEM_FILES = PluginParameterType.SYSTEM_FILES.value
def as_normal_type(self):
return as_normal_type(self)
def cast_value(self, value: Any):
return cast_parameter_value(self, value)
2025-04-22 16:08:58 +08:00
type: DatasourceParameterType = Field(..., description="The type of the parameter")
description: I18nObject = Field(..., description="The description of the parameter")
2025-04-17 15:07:23 +08:00
@classmethod
def get_simple_instance(
cls,
name: str,
2025-04-22 16:08:58 +08:00
typ: DatasourceParameterType,
2025-04-17 15:07:23 +08:00
required: bool,
options: Optional[list[str]] = None,
2025-04-22 16:08:58 +08:00
) -> "DatasourceParameter":
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
get a simple datasource parameter
2025-04-17 15:07:23 +08:00
:param name: the name of the parameter
:param llm_description: the description presented to the LLM
:param typ: the type of the parameter
:param required: if the parameter is required
:param options: the options of the parameter
"""
# convert options to ToolParameterOption
# FIXME fix the type error
if options:
option_objs = [
PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option))
for option in options
]
else:
option_objs = []
return cls(
name=name,
label=I18nObject(en_US="", zh_Hans=""),
placeholder=None,
type=typ,
required=required,
options=option_objs,
description=I18nObject(en_US="", zh_Hans=""),
2025-04-17 15:07:23 +08:00
)
def init_frontend_parameter(self, value: Any):
return init_frontend_parameter(self, self.type, value)
2025-04-22 16:08:58 +08:00
class DatasourceIdentity(BaseModel):
2025-05-23 00:05:57 +08:00
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")
provider: str = Field(..., description="The provider of the datasource")
2025-04-17 15:07:23 +08:00
icon: Optional[str] = None
2025-04-22 16:08:58 +08:00
class DatasourceEntity(BaseModel):
identity: DatasourceIdentity
parameters: list[DatasourceParameter] = Field(default_factory=list)
2025-05-23 17:11:56 +08:00
description: I18nObject = Field(..., description="The label of the datasource")
2025-04-17 15:07:23 +08:00
@field_validator("parameters", mode="before")
@classmethod
2025-04-22 16:08:58 +08:00
def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
2025-04-17 15:07:23 +08:00
return v or []
2025-05-23 17:35:26 +08:00
2025-05-23 17:11:56 +08:00
class DatasourceProviderIdentity(BaseModel):
author: str = Field(..., description="The author of the tool")
name: str = Field(..., description="The name of the tool")
description: I18nObject = Field(..., description="The description of the tool")
icon: str = Field(..., description="The icon of the tool")
label: I18nObject = Field(..., description="The label of the tool")
tags: Optional[list[ToolLabelEnum]] = Field(
default=[],
description="The tags of the tool",
)
2025-04-17 15:07:23 +08:00
2025-05-23 17:35:26 +08:00
2025-05-23 17:11:56 +08:00
class DatasourceProviderEntity(BaseModel):
2025-04-17 15:07:23 +08:00
"""
Datasource provider entity
2025-04-17 15:07:23 +08:00
"""
2025-05-23 17:35:26 +08:00
2025-05-23 17:11:56 +08:00
identity: DatasourceProviderIdentity
credentials_schema: list[ProviderConfig] = Field(default_factory=list)
oauth_schema: Optional[OAuthSchema] = None
provider_type: DatasourceProviderType
class DatasourceProviderEntityWithPlugin(DatasourceProviderEntity):
2025-05-23 00:05:57 +08:00
datasources: list[DatasourceEntity] = Field(default_factory=list)
2025-04-17 15:07:23 +08:00
2025-04-22 16:08:58 +08:00
class DatasourceInvokeMeta(BaseModel):
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Datasource invoke meta
2025-04-17 15:07:23 +08:00
"""
time_cost: float = Field(..., description="The time cost of the tool invoke")
error: Optional[str] = None
tool_config: Optional[dict] = None
@classmethod
2025-04-22 16:08:58 +08:00
def empty(cls) -> "DatasourceInvokeMeta":
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Get an empty instance of DatasourceInvokeMeta
2025-04-17 15:07:23 +08:00
"""
return cls(time_cost=0.0, error=None, tool_config={})
@classmethod
2025-04-22 16:08:58 +08:00
def error_instance(cls, error: str) -> "DatasourceInvokeMeta":
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Get an instance of DatasourceInvokeMeta with error
2025-04-17 15:07:23 +08:00
"""
return cls(time_cost=0.0, error=error, tool_config={})
def to_dict(self) -> dict:
return {
"time_cost": self.time_cost,
"error": self.error,
"tool_config": self.tool_config,
}
2025-04-22 16:08:58 +08:00
class DatasourceLabel(BaseModel):
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Datasource label
2025-04-17 15:07:23 +08:00
"""
name: str = Field(..., description="The name of the tool")
label: I18nObject = Field(..., description="The label of the tool")
icon: str = Field(..., description="The icon of the tool")
2025-04-22 16:08:58 +08:00
class DatasourceInvokeFrom(Enum):
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
Enum class for datasource invoke
2025-04-17 15:07:23 +08:00
"""
2025-04-22 16:08:58 +08:00
RAG_PIPELINE = "rag_pipeline"
2025-05-23 00:05:57 +08:00
class GetOnlineDocumentPagesRequest(BaseModel):
"""
Get online document pages request
"""
class OnlineDocumentPageIcon(BaseModel):
"""
Online document page icon
"""
type: str = Field(..., description="The type of the icon")
url: str = Field(..., description="The url of the icon")
class OnlineDocumentPage(BaseModel):
"""
Online document page
"""
page_id: str = Field(..., description="The page id")
page_title: str = Field(..., description="The page title")
page_icon: Optional[OnlineDocumentPageIcon] = Field(None, description="The page icon")
type: str = Field(..., description="The type of the page")
last_edited_time: str = Field(..., description="The last edited time")
class OnlineDocumentInfo(BaseModel):
"""
Online document info
"""
workspace_id: str = Field(..., description="The workspace id")
workspace_name: str = Field(..., description="The workspace name")
workspace_icon: str = Field(..., description="The workspace icon")
total: int = Field(..., description="The total number of documents")
pages: list[OnlineDocumentPage] = Field(..., description="The pages of the online document")
class GetOnlineDocumentPagesResponse(BaseModel):
"""
Get online document pages response
"""
result: list[OnlineDocumentInfo]
class GetOnlineDocumentPageContentRequest(BaseModel):
"""
Get online document page content request
"""
2025-05-23 15:55:41 +08:00
online_document_info: OnlineDocumentInfo
2025-05-23 00:05:57 +08:00
class OnlineDocumentPageContent(BaseModel):
"""
Online document page content
"""
2025-05-23 15:55:41 +08:00
workspace_id: str = Field(..., description="The workspace id")
2025-05-23 00:05:57 +08:00
page_id: str = Field(..., description="The page id")
content: str = Field(..., description="The content of the page")
class GetOnlineDocumentPageContentResponse(BaseModel):
"""
Get online document page content response
"""
2025-05-23 15:55:41 +08:00
result: OnlineDocumentPageContent
2025-05-23 00:05:57 +08:00
class GetWebsiteCrawlRequest(BaseModel):
"""
Get website crawl request
"""
2025-05-23 17:35:26 +08:00
2025-05-23 00:05:57 +08:00
crawl_parameters: dict = Field(..., description="The crawl parameters")
class WebSiteInfo(BaseModel):
"""
Website info
"""
source_url: str = Field(..., description="The url of the website")
2025-05-23 15:55:41 +08:00
content: str = Field(..., description="The content of the website")
2025-05-23 00:05:57 +08:00
title: str = Field(..., description="The title of the website")
description: str = Field(..., description="The description of the website")
class GetWebsiteCrawlResponse(BaseModel):
"""
Get website crawl response
"""
2025-06-06 17:14:43 +08:00
result: Optional[list[WebSiteInfo]] = []
job_id: str = Field(..., description="The job id")
status: str = Field(..., description="The status of the job")