2024-05-27 22:01:11 +08:00
|
|
|
from typing import Literal, Optional
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-11-12 00:08:04 +08:00
|
|
|
from pydantic import BaseModel, Field, field_validator
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
from core.model_runtime.utils.encoders import jsonable_encoder
|
2025-02-17 17:05:13 +08:00
|
|
|
from core.tools.__base.tool import ToolParameter
|
2024-01-23 19:58:23 +08:00
|
|
|
from core.tools.entities.common_entities import I18nObject
|
2025-02-17 17:05:13 +08:00
|
|
|
from core.tools.entities.tool_entities import ToolProviderType
|
2024-02-01 18:11:57 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolApiEntity(BaseModel):
|
2024-04-08 18:51:46 +08:00
|
|
|
author: str
|
2024-09-10 17:00:20 +08:00
|
|
|
name: str # identifier
|
|
|
|
label: I18nObject # label
|
2024-04-08 18:51:46 +08:00
|
|
|
description: I18nObject
|
2024-06-14 01:05:37 +08:00
|
|
|
parameters: Optional[list[ToolParameter]] = None
|
2025-02-17 17:05:13 +08:00
|
|
|
labels: list[str] = Field(default_factory=list)
|
|
|
|
output_schema: Optional[dict] = None
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
ToolProviderTypeApiLiteral = Optional[Literal["builtin", "api", "workflow"]]
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolProviderApiEntity(BaseModel):
|
2024-01-23 19:58:23 +08:00
|
|
|
id: str
|
|
|
|
author: str
|
2024-09-10 17:00:20 +08:00
|
|
|
name: str # identifier
|
2024-01-23 19:58:23 +08:00
|
|
|
description: I18nObject
|
2025-02-17 17:05:13 +08:00
|
|
|
icon: str | dict
|
2024-09-10 17:00:20 +08:00
|
|
|
label: I18nObject # label
|
2024-05-27 22:01:11 +08:00
|
|
|
type: ToolProviderType
|
2024-06-14 02:28:28 +08:00
|
|
|
masked_credentials: Optional[dict] = None
|
|
|
|
original_credentials: Optional[dict] = None
|
2024-01-23 19:58:23 +08:00
|
|
|
is_team_authorization: bool = False
|
|
|
|
allow_delete: bool = True
|
2025-02-17 17:05:13 +08:00
|
|
|
plugin_id: Optional[str] = Field(default="", description="The plugin id of the tool")
|
|
|
|
plugin_unique_identifier: Optional[str] = Field(default="", description="The unique identifier of the tool")
|
|
|
|
tools: list[ToolApiEntity] = Field(default_factory=list)
|
|
|
|
labels: list[str] = Field(default_factory=list)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-11-12 00:08:04 +08:00
|
|
|
@field_validator("tools", mode="before")
|
|
|
|
@classmethod
|
|
|
|
def convert_none_to_empty_list(cls, v):
|
|
|
|
return v if v is not None else []
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
def to_dict(self) -> dict:
|
2024-05-27 22:01:11 +08:00
|
|
|
# -------------
|
|
|
|
# overwrite tool parameter types for temp fix
|
|
|
|
tools = jsonable_encoder(self.tools)
|
|
|
|
for tool in tools:
|
2024-09-10 17:00:20 +08:00
|
|
|
if tool.get("parameters"):
|
|
|
|
for parameter in tool.get("parameters"):
|
2024-10-21 10:43:49 +08:00
|
|
|
if parameter.get("type") == ToolParameter.ToolParameterType.SYSTEM_FILES.value:
|
2024-09-10 17:00:20 +08:00
|
|
|
parameter["type"] = "files"
|
2024-05-27 22:01:11 +08:00
|
|
|
# -------------
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
return {
|
2024-09-10 17:00:20 +08:00
|
|
|
"id": self.id,
|
|
|
|
"author": self.author,
|
|
|
|
"name": self.name,
|
2025-02-17 17:05:13 +08:00
|
|
|
"plugin_id": self.plugin_id,
|
|
|
|
"plugin_unique_identifier": self.plugin_unique_identifier,
|
2024-09-10 17:00:20 +08:00
|
|
|
"description": self.description.to_dict(),
|
|
|
|
"icon": self.icon,
|
|
|
|
"label": self.label.to_dict(),
|
|
|
|
"type": self.type.value,
|
|
|
|
"team_credentials": self.masked_credentials,
|
|
|
|
"is_team_authorization": self.is_team_authorization,
|
|
|
|
"allow_delete": self.allow_delete,
|
|
|
|
"tools": tools,
|
|
|
|
"labels": self.labels,
|
2024-01-23 19:58:23 +08:00
|
|
|
}
|