| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import base64 | 
					
						
							|  |  |  | import enum | 
					
						
							|  |  |  | from collections.abc import Mapping | 
					
						
							|  |  |  | from enum import Enum | 
					
						
							|  |  |  | from typing import Any, Optional, Union | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_serializer, field_validator, model_validator | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from core.entities.provider_entities import ProviderConfig | 
					
						
							|  |  |  | from core.plugin.entities.parameters import ( | 
					
						
							|  |  |  |     PluginParameter, | 
					
						
							|  |  |  |     PluginParameterOption, | 
					
						
							|  |  |  |     PluginParameterType, | 
					
						
							|  |  |  |     as_normal_type, | 
					
						
							|  |  |  |     cast_parameter_value, | 
					
						
							|  |  |  |     init_frontend_parameter, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from core.tools.entities.common_entities import I18nObject | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from core.tools.entities.constants import TOOL_SELECTOR_MODEL_IDENTITY | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-29 15:19:14 +08:00
										 |  |  | class ToolLabelEnum(Enum): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     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" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-29 15:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolProviderType(enum.StrEnum): | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     Enum class for tool provider | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     PLUGIN = "plugin" | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     BUILT_IN = "builtin" | 
					
						
							|  |  |  |     WORKFLOW = "workflow" | 
					
						
							|  |  |  |     API = "api" | 
					
						
							|  |  |  |     APP = "app" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     DATASET_RETRIEVAL = "dataset-retrieval" | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def value_of(cls, value: str) -> "ToolProviderType": | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get value of given mode. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         :param value: mode value | 
					
						
							|  |  |  |         :return: mode | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         for mode in cls: | 
					
						
							|  |  |  |             if mode.value == value: | 
					
						
							|  |  |  |                 return mode | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         raise ValueError(f"invalid mode value {value}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | class ApiProviderSchemaType(Enum): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Enum class for api provider schema type. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     OPENAPI = "openapi" | 
					
						
							|  |  |  |     SWAGGER = "swagger" | 
					
						
							|  |  |  |     OPENAI_PLUGIN = "openai_plugin" | 
					
						
							|  |  |  |     OPENAI_ACTIONS = "openai_actions" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def value_of(cls, value: str) -> "ApiProviderSchemaType": | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get value of given mode. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         :param value: mode value | 
					
						
							|  |  |  |         :return: mode | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         for mode in cls: | 
					
						
							|  |  |  |             if mode.value == value: | 
					
						
							|  |  |  |                 return mode | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         raise ValueError(f"invalid mode value {value}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | class ApiProviderAuthType(Enum): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Enum class for api provider auth type. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     NONE = "none" | 
					
						
							|  |  |  |     API_KEY = "api_key" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def value_of(cls, value: str) -> "ApiProviderAuthType": | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get value of given mode. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         :param value: mode value | 
					
						
							|  |  |  |         :return: mode | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         for mode in cls: | 
					
						
							|  |  |  |             if mode.value == value: | 
					
						
							|  |  |  |                 return mode | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         raise ValueError(f"invalid mode value {value}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ToolInvokeMessage(BaseModel): | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     class TextMessage(BaseModel): | 
					
						
							|  |  |  |         text: str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class JsonMessage(BaseModel): | 
					
						
							|  |  |  |         json_object: dict | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class BlobMessage(BaseModel): | 
					
						
							|  |  |  |         blob: bytes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-15 19:23:03 +08:00
										 |  |  |     class BlobChunkMessage(BaseModel): | 
					
						
							|  |  |  |         id: str = Field(..., description="The id of the blob") | 
					
						
							|  |  |  |         sequence: int = Field(..., description="The sequence of the chunk") | 
					
						
							|  |  |  |         total_length: int = Field(..., description="The total length of the blob") | 
					
						
							|  |  |  |         blob: bytes = Field(..., description="The blob data of the chunk") | 
					
						
							|  |  |  |         end: bool = Field(..., description="Whether the chunk is the last chunk") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     class FileMessage(BaseModel): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class VariableMessage(BaseModel): | 
					
						
							|  |  |  |         variable_name: str = Field(..., description="The name of the variable") | 
					
						
							|  |  |  |         variable_value: Any = Field(..., description="The value of the variable") | 
					
						
							|  |  |  |         stream: bool = Field(default=False, description="Whether the variable is streamed") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @model_validator(mode="before") | 
					
						
							|  |  |  |         @classmethod | 
					
						
							|  |  |  |         def transform_variable_value(cls, values) -> Any: | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             Only basic types and lists are allowed. | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             value = values.get("variable_value") | 
					
						
							|  |  |  |             if not isinstance(value, dict | list | str | int | float | bool): | 
					
						
							|  |  |  |                 raise ValueError("Only basic types and lists are allowed.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # if stream is true, the value must be a string | 
					
						
							|  |  |  |             if values.get("stream"): | 
					
						
							|  |  |  |                 if not isinstance(value, str): | 
					
						
							|  |  |  |                     raise ValueError("When 'stream' is True, 'variable_value' must be a string.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return values | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @field_validator("variable_name", mode="before") | 
					
						
							|  |  |  |         @classmethod | 
					
						
							|  |  |  |         def transform_variable_name(cls, value: str) -> str: | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             The variable name must be a string. | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             if value in {"json", "text", "files"}: | 
					
						
							|  |  |  |                 raise ValueError(f"The variable name '{value}' is reserved.") | 
					
						
							|  |  |  |             return value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class LogMessage(BaseModel): | 
					
						
							|  |  |  |         class LogStatus(Enum): | 
					
						
							|  |  |  |             START = "start" | 
					
						
							|  |  |  |             ERROR = "error" | 
					
						
							|  |  |  |             SUCCESS = "success" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         id: str | 
					
						
							|  |  |  |         label: str = Field(..., description="The label of the log") | 
					
						
							|  |  |  |         parent_id: Optional[str] = Field(default=None, description="Leave empty for root log") | 
					
						
							|  |  |  |         error: Optional[str] = Field(default=None, description="The error message") | 
					
						
							|  |  |  |         status: LogStatus = Field(..., description="The status of the log") | 
					
						
							|  |  |  |         data: Mapping[str, Any] = Field(..., description="Detailed log data") | 
					
						
							|  |  |  |         metadata: Optional[Mapping[str, Any]] = Field(default=None, description="The metadata of the log") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     class MessageType(Enum): | 
					
						
							|  |  |  |         TEXT = "text" | 
					
						
							|  |  |  |         IMAGE = "image" | 
					
						
							|  |  |  |         LINK = "link" | 
					
						
							|  |  |  |         BLOB = "blob" | 
					
						
							| 
									
										
										
										
											2024-06-24 15:46:16 +08:00
										 |  |  |         JSON = "json" | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         IMAGE_LINK = "image_link" | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         BINARY_LINK = "binary_link" | 
					
						
							|  |  |  |         VARIABLE = "variable" | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         FILE = "file" | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         LOG = "log" | 
					
						
							| 
									
										
										
										
											2025-04-15 19:23:03 +08:00
										 |  |  |         BLOB_CHUNK = "blob_chunk" | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     type: MessageType = MessageType.TEXT | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |         plain text, image url or link url | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2025-04-15 19:23:03 +08:00
										 |  |  |     message: ( | 
					
						
							|  |  |  |         JsonMessage | TextMessage | BlobChunkMessage | BlobMessage | LogMessage | FileMessage | None | VariableMessage | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     meta: dict[str, Any] | None = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @field_validator("message", mode="before") | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def decode_blob_message(cls, v): | 
					
						
							|  |  |  |         if isinstance(v, dict) and "blob" in v: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 v["blob"] = base64.b64decode(v["blob"]) | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |         return v | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @field_serializer("message") | 
					
						
							|  |  |  |     def serialize_message(self, v): | 
					
						
							|  |  |  |         if isinstance(v, self.BlobMessage): | 
					
						
							|  |  |  |             return {"blob": base64.b64encode(v.blob).decode("utf-8")} | 
					
						
							|  |  |  |         return v | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ToolInvokeMessageBinary(BaseModel): | 
					
						
							|  |  |  |     mimetype: str = Field(..., description="The mimetype of the binary") | 
					
						
							|  |  |  |     url: str = Field(..., description="The url of the binary") | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     file_var: Optional[dict[str, Any]] = None | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolParameter(PluginParameter): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Overrides type | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-06-14 01:05:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     class ToolParameterType(enum.StrEnum): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         removes TOOLS_SELECTOR from PluginParameterType | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-06-03 21:26:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         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 | 
					
						
							|  |  |  |         APP_SELECTOR = PluginParameterType.APP_SELECTOR.value | 
					
						
							|  |  |  |         MODEL_SELECTOR = PluginParameterType.MODEL_SELECTOR.value | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # deprecated, should not use. | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         SYSTEM_FILES = PluginParameterType.SYSTEM_FILES.value | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def as_normal_type(self): | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             return as_normal_type(self) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         def cast_value(self, value: Any): | 
					
						
							|  |  |  |             return cast_parameter_value(self, value) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class ToolParameterForm(Enum): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         SCHEMA = "schema"  # should be set while adding tool | 
					
						
							|  |  |  |         FORM = "form"  # should be set before invoking tool | 
					
						
							|  |  |  |         LLM = "llm"  # will be set by LLM | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     type: ToolParameterType = Field(..., description="The type of the parameter") | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     human_description: Optional[I18nObject] = Field(default=None, description="The description presented to the user") | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm") | 
					
						
							|  |  |  |     llm_description: Optional[str] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def get_simple_instance( | 
					
						
							|  |  |  |         cls, | 
					
						
							|  |  |  |         name: str, | 
					
						
							|  |  |  |         llm_description: str, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         typ: ToolParameterType, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         required: bool, | 
					
						
							|  |  |  |         options: Optional[list[str]] = None, | 
					
						
							|  |  |  |     ) -> "ToolParameter": | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         get a simple tool parameter | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         :param name: the name of the parameter | 
					
						
							|  |  |  |         :param llm_description: the description presented to the LLM | 
					
						
							| 
									
										
										
										
											2025-03-31 13:19:15 +08:00
										 |  |  |         :param typ: the type of the parameter | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |         :param required: if the parameter is required | 
					
						
							|  |  |  |         :param options: the options of the parameter | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-01-31 11:58:07 +08:00
										 |  |  |         # convert options to ToolParameterOption | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         if options: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             option_objs = [ | 
					
						
							|  |  |  |                 PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) | 
					
						
							|  |  |  |                 for option in options | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         else: | 
					
						
							|  |  |  |             option_objs = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         return cls( | 
					
						
							|  |  |  |             name=name, | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             label=I18nObject(en_US="", zh_Hans=""), | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             placeholder=None, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             human_description=I18nObject(en_US="", zh_Hans=""), | 
					
						
							|  |  |  |             type=typ, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             form=cls.ToolParameterForm.LLM, | 
					
						
							|  |  |  |             llm_description=llm_description, | 
					
						
							|  |  |  |             required=required, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             options=option_objs, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     def init_frontend_parameter(self, value: Any): | 
					
						
							|  |  |  |         return init_frontend_parameter(self, self.type, value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | class ToolProviderIdentity(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") | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     tags: Optional[list[ToolLabelEnum]] = Field( | 
					
						
							|  |  |  |         default=[], | 
					
						
							|  |  |  |         description="The tags of the tool", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ToolIdentity(BaseModel): | 
					
						
							|  |  |  |     author: str = Field(..., description="The author of the tool") | 
					
						
							|  |  |  |     name: str = Field(..., description="The name of the tool") | 
					
						
							|  |  |  |     label: I18nObject = Field(..., description="The label of the tool") | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     provider: str = Field(..., description="The provider of the tool") | 
					
						
							|  |  |  |     icon: Optional[str] = None | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolDescription(BaseModel): | 
					
						
							|  |  |  |     human: I18nObject = Field(..., description="The description presented to the user") | 
					
						
							|  |  |  |     llm: str = Field(..., description="The description presented to the LLM") | 
					
						
							| 
									
										
										
										
											2024-03-08 15:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolEntity(BaseModel): | 
					
						
							|  |  |  |     identity: ToolIdentity | 
					
						
							|  |  |  |     parameters: list[ToolParameter] = Field(default_factory=list) | 
					
						
							|  |  |  |     description: Optional[ToolDescription] = None | 
					
						
							|  |  |  |     output_schema: Optional[dict] = None | 
					
						
							|  |  |  |     has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters") | 
					
						
							| 
									
										
										
										
											2024-03-08 15:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     # pydantic configs | 
					
						
							|  |  |  |     model_config = ConfigDict(protected_namespaces=()) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     @field_validator("parameters", mode="before") | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def set_parameters(cls, v, validation_info: ValidationInfo) -> list[ToolParameter]: | 
					
						
							|  |  |  |         return v or [] | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 15:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolProviderEntity(BaseModel): | 
					
						
							|  |  |  |     identity: ToolProviderIdentity | 
					
						
							|  |  |  |     plugin_id: Optional[str] = None | 
					
						
							|  |  |  |     credentials_schema: list[ProviderConfig] = Field(default_factory=list) | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | class ToolProviderEntityWithPlugin(ToolProviderEntity): | 
					
						
							|  |  |  |     tools: list[ToolEntity] = Field(default_factory=list) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WorkflowToolParameterConfiguration(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Workflow tool configuration | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     name: str = Field(..., description="The name of the parameter") | 
					
						
							|  |  |  |     description: str = Field(..., description="The description of the parameter") | 
					
						
							|  |  |  |     form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | class ToolInvokeMeta(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Tool invoke meta | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     time_cost: float = Field(..., description="The time cost of the tool invoke") | 
					
						
							|  |  |  |     error: Optional[str] = None | 
					
						
							|  |  |  |     tool_config: Optional[dict] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def empty(cls) -> "ToolInvokeMeta": | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get an empty instance of ToolInvokeMeta | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return cls(time_cost=0.0, error=None, tool_config={}) | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     def error_instance(cls, error: str) -> "ToolInvokeMeta": | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Get an instance of ToolInvokeMeta with error | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return cls(time_cost=0.0, error=error, tool_config={}) | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     def to_dict(self) -> dict: | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             "time_cost": self.time_cost, | 
					
						
							|  |  |  |             "error": self.error, | 
					
						
							|  |  |  |             "tool_config": self.tool_config, | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | class ToolLabel(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Tool label | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +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") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | class ToolInvokeFrom(Enum): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Enum class for tool invoke | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     WORKFLOW = "workflow" | 
					
						
							| 
									
										
										
										
											2024-08-14 14:08:54 +08:00
										 |  |  |     AGENT = "agent" | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     PLUGIN = "plugin" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ToolSelector(BaseModel): | 
					
						
							|  |  |  |     dify_model_identity: str = TOOL_SELECTOR_MODEL_IDENTITY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class Parameter(BaseModel): | 
					
						
							|  |  |  |         name: str = Field(..., description="The name of the parameter") | 
					
						
							|  |  |  |         type: ToolParameter.ToolParameterType = Field(..., description="The type of the parameter") | 
					
						
							|  |  |  |         required: bool = Field(..., description="Whether the parameter is required") | 
					
						
							|  |  |  |         description: str = Field(..., description="The description of the parameter") | 
					
						
							|  |  |  |         default: Optional[Union[int, float, str]] = None | 
					
						
							|  |  |  |         options: Optional[list[PluginParameterOption]] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     provider_id: str = Field(..., description="The id of the provider") | 
					
						
							|  |  |  |     tool_name: str = Field(..., description="The name of the tool") | 
					
						
							|  |  |  |     tool_description: str = Field(..., description="The description of the tool") | 
					
						
							|  |  |  |     tool_configuration: Mapping[str, Any] = Field(..., description="Configuration, type form") | 
					
						
							|  |  |  |     tool_parameters: Mapping[str, Parameter] = Field(..., description="Parameters, type llm") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def to_plugin_parameter(self) -> dict[str, Any]: | 
					
						
							|  |  |  |         return self.model_dump() |