+110









Yeuoly
GitHub
takatost
kurokobo
Novice Lee
zxhlyh
AkaraChen
Yi
Joel
JzoNg
twwu
Hiroshi Fujita
AkaraChen
NFish
Wu Tianwei
非法操作
Novice
Hiroki Nagai
Gen Sato
eux
huangzhuo1949
huangzhuo
lotsik
crazywoola
nite-knite
Jyong
github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
gakkiyomi
CN-P5
CN-P5
Chuehnone
yihong
Kevin9703
-LAN-
Boris Feld
mbo
mabo
Warren Chen
JzoNgKVO
jiandanfeng
zhu-an
zhaoqingyu.1075
海狸大師
Xu Song
rayshaw001
Ding Jiatong
Bowen Liang
JasonVV
le0zh
zhuxinliang
k-zaku
luckylhb90
hobo.l
jiangbo721
刘江波
Shun Miyazawa
EricPan
crazywoola
sino
Jhvcc
lowell
Boris Polonsky
Ademílson Tonato
Ademílson Tonato
IWAI, Masaharu <[email protected]>
Yueh-Po Peng
Jason
Xin Zhang
yjc980121
heyszt
Abdullah AlOsaimi
Abdullah AlOsaimi
Yingchun Lai
Hash Brown
zuodongxu
Masashi Tomooka
aplio
Obada Khalili
Nam Vu
Kei YAMAZAKI
TechnoHouse
Riddhimaan-Senapati
MaFee921
te-chan
HQidea
Joshbly
xhe
weiwenyan-dev
ex_wenyan.wei
engchina
engchina
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
呆萌闷油瓶
Kemal
Lazy_Frog
Yi Xiao
Steven sun
steven
Kalo Chin
Katy Tao
depy
胡春东
Junjie.M
MuYu
Naoki Takashima
Summer-Gu
Fei He
ybalbert001
Yuanbo Li
douxc
liuzhenghua
Wu Jiayang
Your Name
kimjion
AugNSo
llinvokerl
liusurong.lsr
Vasu Negi
Hundredwz
Xiyuan Chen
403e2d58b9
Signed-off-by: yihong0618 <[email protected]> Signed-off-by: -LAN- <[email protected]> Signed-off-by: xhe <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: takatost <[email protected]> Co-authored-by: kurokobo <[email protected]> Co-authored-by: Novice Lee <[email protected]> Co-authored-by: zxhlyh <[email protected]> Co-authored-by: AkaraChen <[email protected]> Co-authored-by: Yi <[email protected]> Co-authored-by: Joel <[email protected]> Co-authored-by: JzoNg <[email protected]> Co-authored-by: twwu <[email protected]> Co-authored-by: Hiroshi Fujita <[email protected]> Co-authored-by: AkaraChen <[email protected]> Co-authored-by: NFish <[email protected]> Co-authored-by: Wu Tianwei <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Novice <[email protected]> Co-authored-by: Hiroki Nagai <[email protected]> Co-authored-by: Gen Sato <[email protected]> Co-authored-by: eux <[email protected]> Co-authored-by: huangzhuo1949 <[email protected]> Co-authored-by: huangzhuo <[email protected]> Co-authored-by: lotsik <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: nite-knite <[email protected]> Co-authored-by: Jyong <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <[email protected]> Co-authored-by: CN-P5 <[email protected]> Co-authored-by: CN-P5 <[email protected]> Co-authored-by: Chuehnone <[email protected]> Co-authored-by: yihong <[email protected]> Co-authored-by: Kevin9703 <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: Boris Feld <[email protected]> Co-authored-by: mbo <[email protected]> Co-authored-by: mabo <[email protected]> Co-authored-by: Warren Chen <[email protected]> Co-authored-by: JzoNgKVO <[email protected]> Co-authored-by: jiandanfeng <[email protected]> Co-authored-by: zhu-an <[email protected]> Co-authored-by: zhaoqingyu.1075 <[email protected]> Co-authored-by: 海狸大師 <[email protected]> Co-authored-by: Xu Song <[email protected]> Co-authored-by: rayshaw001 <[email protected]> Co-authored-by: Ding Jiatong <[email protected]> Co-authored-by: Bowen Liang <[email protected]> Co-authored-by: JasonVV <[email protected]> Co-authored-by: le0zh <[email protected]> Co-authored-by: zhuxinliang <[email protected]> Co-authored-by: k-zaku <[email protected]> Co-authored-by: luckylhb90 <[email protected]> Co-authored-by: hobo.l <[email protected]> Co-authored-by: jiangbo721 <[email protected]> Co-authored-by: 刘江波 <[email protected]> Co-authored-by: Shun Miyazawa <[email protected]> Co-authored-by: EricPan <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: sino <[email protected]> Co-authored-by: Jhvcc <[email protected]> Co-authored-by: lowell <[email protected]> Co-authored-by: Boris Polonsky <[email protected]> Co-authored-by: Ademílson Tonato <[email protected]> Co-authored-by: Ademílson Tonato <[email protected]> Co-authored-by: IWAI, Masaharu <[email protected]> Co-authored-by: Yueh-Po Peng (Yabi) <[email protected]> Co-authored-by: Jason <[email protected]> Co-authored-by: Xin Zhang <[email protected]> Co-authored-by: yjc980121 <[email protected]> Co-authored-by: heyszt <[email protected]> Co-authored-by: Abdullah AlOsaimi <[email protected]> Co-authored-by: Abdullah AlOsaimi <[email protected]> Co-authored-by: Yingchun Lai <[email protected]> Co-authored-by: Hash Brown <[email protected]> Co-authored-by: zuodongxu <[email protected]> Co-authored-by: Masashi Tomooka <[email protected]> Co-authored-by: aplio <[email protected]> Co-authored-by: Obada Khalili <[email protected]> Co-authored-by: Nam Vu <[email protected]> Co-authored-by: Kei YAMAZAKI <[email protected]> Co-authored-by: TechnoHouse <[email protected]> Co-authored-by: Riddhimaan-Senapati <[email protected]> Co-authored-by: MaFee921 <[email protected]> Co-authored-by: te-chan <[email protected]> Co-authored-by: HQidea <[email protected]> Co-authored-by: Joshbly <[email protected]> Co-authored-by: xhe <[email protected]> Co-authored-by: weiwenyan-dev <[email protected]> Co-authored-by: ex_wenyan.wei <[email protected]> Co-authored-by: engchina <[email protected]> Co-authored-by: engchina <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <[email protected]> Co-authored-by: Kemal <[email protected]> Co-authored-by: Lazy_Frog <[email protected]> Co-authored-by: Yi Xiao <[email protected]> Co-authored-by: Steven sun <[email protected]> Co-authored-by: steven <[email protected]> Co-authored-by: Kalo Chin <[email protected]> Co-authored-by: Katy Tao <[email protected]> Co-authored-by: depy <[email protected]> Co-authored-by: 胡春东 <[email protected]> Co-authored-by: Junjie.M <[email protected]> Co-authored-by: MuYu <[email protected]> Co-authored-by: Naoki Takashima <[email protected]> Co-authored-by: Summer-Gu <[email protected]> Co-authored-by: Fei He <[email protected]> Co-authored-by: ybalbert001 <[email protected]> Co-authored-by: Yuanbo Li <[email protected]> Co-authored-by: douxc <[email protected]> Co-authored-by: liuzhenghua <[email protected]> Co-authored-by: Wu Jiayang <[email protected]> Co-authored-by: Your Name <[email protected]> Co-authored-by: kimjion <[email protected]> Co-authored-by: AugNSo <[email protected]> Co-authored-by: llinvokerl <[email protected]> Co-authored-by: liusurong.lsr <[email protected]> Co-authored-by: Vasu Negi <[email protected]> Co-authored-by: Hundredwz <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]>
302 lines
12 KiB
Python
302 lines
12 KiB
Python
import json
|
|
import logging
|
|
from typing import Optional, Union, cast
|
|
|
|
from yarl import URL
|
|
|
|
from configs import dify_config
|
|
from core.tools.__base.tool import Tool
|
|
from core.tools.__base.tool_runtime import ToolRuntime
|
|
from core.tools.builtin_tool.provider import BuiltinToolProviderController
|
|
from core.tools.custom_tool.provider import ApiToolProviderController
|
|
from core.tools.entities.api_entities import ToolApiEntity, ToolProviderApiEntity
|
|
from core.tools.entities.common_entities import I18nObject
|
|
from core.tools.entities.tool_bundle import ApiToolBundle
|
|
from core.tools.entities.tool_entities import (
|
|
ApiProviderAuthType,
|
|
ToolParameter,
|
|
ToolProviderType,
|
|
)
|
|
from core.tools.plugin_tool.provider import PluginToolProviderController
|
|
from core.tools.utils.configuration import ProviderConfigEncrypter
|
|
from core.tools.workflow_as_tool.provider import WorkflowToolProviderController
|
|
from core.tools.workflow_as_tool.tool import WorkflowTool
|
|
from models.tools import ApiToolProvider, BuiltinToolProvider, WorkflowToolProvider
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ToolTransformService:
|
|
@classmethod
|
|
def get_plugin_icon_url(cls, tenant_id: str, filename: str) -> str:
|
|
url_prefix = URL(dify_config.CONSOLE_API_URL) / "console" / "api" / "workspaces" / "current" / "plugin" / "icon"
|
|
return str(url_prefix % {"tenant_id": tenant_id, "filename": filename})
|
|
|
|
@classmethod
|
|
def get_tool_provider_icon_url(cls, provider_type: str, provider_name: str, icon: str | dict) -> Union[str, dict]:
|
|
"""
|
|
get tool provider icon url
|
|
"""
|
|
url_prefix = URL(dify_config.CONSOLE_API_URL) / "console" / "api" / "workspaces" / "current" / "tool-provider"
|
|
|
|
if provider_type == ToolProviderType.BUILT_IN.value:
|
|
return str(url_prefix / "builtin" / provider_name / "icon")
|
|
elif provider_type in {ToolProviderType.API.value, ToolProviderType.WORKFLOW.value}:
|
|
try:
|
|
if isinstance(icon, str):
|
|
return cast(dict, json.loads(icon))
|
|
return icon
|
|
except Exception:
|
|
return {"background": "#252525", "content": "\ud83d\ude01"}
|
|
|
|
return ""
|
|
|
|
@staticmethod
|
|
def repack_provider(tenant_id: str, provider: Union[dict, ToolProviderApiEntity]):
|
|
"""
|
|
repack provider
|
|
|
|
:param provider: the provider dict
|
|
"""
|
|
if isinstance(provider, dict) and "icon" in provider:
|
|
provider["icon"] = ToolTransformService.get_tool_provider_icon_url(
|
|
provider_type=provider["type"], provider_name=provider["name"], icon=provider["icon"]
|
|
)
|
|
elif isinstance(provider, ToolProviderApiEntity):
|
|
if provider.plugin_id:
|
|
if isinstance(provider.icon, str):
|
|
provider.icon = ToolTransformService.get_plugin_icon_url(
|
|
tenant_id=tenant_id, filename=provider.icon
|
|
)
|
|
else:
|
|
provider.icon = ToolTransformService.get_tool_provider_icon_url(
|
|
provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon
|
|
)
|
|
|
|
@classmethod
|
|
def builtin_provider_to_user_provider(
|
|
cls,
|
|
provider_controller: BuiltinToolProviderController | PluginToolProviderController,
|
|
db_provider: Optional[BuiltinToolProvider],
|
|
decrypt_credentials: bool = True,
|
|
) -> ToolProviderApiEntity:
|
|
"""
|
|
convert provider controller to user provider
|
|
"""
|
|
result = ToolProviderApiEntity(
|
|
id=provider_controller.entity.identity.name,
|
|
author=provider_controller.entity.identity.author,
|
|
name=provider_controller.entity.identity.name,
|
|
description=provider_controller.entity.identity.description,
|
|
icon=provider_controller.entity.identity.icon,
|
|
label=provider_controller.entity.identity.label,
|
|
type=ToolProviderType.BUILT_IN,
|
|
masked_credentials={},
|
|
is_team_authorization=False,
|
|
plugin_id=None,
|
|
tools=[],
|
|
labels=provider_controller.tool_labels,
|
|
)
|
|
|
|
if isinstance(provider_controller, PluginToolProviderController):
|
|
result.plugin_id = provider_controller.plugin_id
|
|
result.plugin_unique_identifier = provider_controller.plugin_unique_identifier
|
|
|
|
# get credentials schema
|
|
schema = {x.to_basic_provider_config().name: x for x in provider_controller.get_credentials_schema()}
|
|
|
|
for name, value in schema.items():
|
|
if result.masked_credentials:
|
|
result.masked_credentials[name] = ""
|
|
|
|
# check if the provider need credentials
|
|
if not provider_controller.need_credentials:
|
|
result.is_team_authorization = True
|
|
result.allow_delete = False
|
|
elif db_provider:
|
|
result.is_team_authorization = True
|
|
|
|
if decrypt_credentials:
|
|
credentials = db_provider.credentials
|
|
|
|
# init tool configuration
|
|
tool_configuration = ProviderConfigEncrypter(
|
|
tenant_id=db_provider.tenant_id,
|
|
config=[x.to_basic_provider_config() for x in provider_controller.get_credentials_schema()],
|
|
provider_type=provider_controller.provider_type.value,
|
|
provider_identity=provider_controller.entity.identity.name,
|
|
)
|
|
# decrypt the credentials and mask the credentials
|
|
decrypted_credentials = tool_configuration.decrypt(data=credentials)
|
|
masked_credentials = tool_configuration.mask_tool_credentials(data=decrypted_credentials)
|
|
|
|
result.masked_credentials = masked_credentials
|
|
result.original_credentials = decrypted_credentials
|
|
|
|
return result
|
|
|
|
@staticmethod
|
|
def api_provider_to_controller(
|
|
db_provider: ApiToolProvider,
|
|
) -> ApiToolProviderController:
|
|
"""
|
|
convert provider controller to user provider
|
|
"""
|
|
# package tool provider controller
|
|
controller = ApiToolProviderController.from_db(
|
|
db_provider=db_provider,
|
|
auth_type=ApiProviderAuthType.API_KEY
|
|
if db_provider.credentials["auth_type"] == "api_key"
|
|
else ApiProviderAuthType.NONE,
|
|
)
|
|
|
|
return controller
|
|
|
|
@staticmethod
|
|
def workflow_provider_to_controller(db_provider: WorkflowToolProvider) -> WorkflowToolProviderController:
|
|
"""
|
|
convert provider controller to provider
|
|
"""
|
|
return WorkflowToolProviderController.from_db(db_provider)
|
|
|
|
@staticmethod
|
|
def workflow_provider_to_user_provider(
|
|
provider_controller: WorkflowToolProviderController, labels: list[str] | None = None
|
|
):
|
|
"""
|
|
convert provider controller to user provider
|
|
"""
|
|
return ToolProviderApiEntity(
|
|
id=provider_controller.provider_id,
|
|
author=provider_controller.entity.identity.author,
|
|
name=provider_controller.entity.identity.name,
|
|
description=provider_controller.entity.identity.description,
|
|
icon=provider_controller.entity.identity.icon,
|
|
label=provider_controller.entity.identity.label,
|
|
type=ToolProviderType.WORKFLOW,
|
|
masked_credentials={},
|
|
is_team_authorization=True,
|
|
plugin_id=None,
|
|
plugin_unique_identifier=None,
|
|
tools=[],
|
|
labels=labels or [],
|
|
)
|
|
|
|
@classmethod
|
|
def api_provider_to_user_provider(
|
|
cls,
|
|
provider_controller: ApiToolProviderController,
|
|
db_provider: ApiToolProvider,
|
|
decrypt_credentials: bool = True,
|
|
labels: list[str] | None = None,
|
|
) -> ToolProviderApiEntity:
|
|
"""
|
|
convert provider controller to user provider
|
|
"""
|
|
username = "Anonymous"
|
|
if db_provider.user is None:
|
|
raise ValueError(f"user is None for api provider {db_provider.id}")
|
|
try:
|
|
user = db_provider.user
|
|
if not user:
|
|
raise ValueError("user not found")
|
|
|
|
username = user.name
|
|
except Exception:
|
|
logger.exception(f"failed to get user name for api provider {db_provider.id}")
|
|
# add provider into providers
|
|
credentials = db_provider.credentials
|
|
result = ToolProviderApiEntity(
|
|
id=db_provider.id,
|
|
author=username,
|
|
name=db_provider.name,
|
|
description=I18nObject(
|
|
en_US=db_provider.description,
|
|
zh_Hans=db_provider.description,
|
|
),
|
|
icon=db_provider.icon,
|
|
label=I18nObject(
|
|
en_US=db_provider.name,
|
|
zh_Hans=db_provider.name,
|
|
),
|
|
type=ToolProviderType.API,
|
|
plugin_id=None,
|
|
plugin_unique_identifier=None,
|
|
masked_credentials={},
|
|
is_team_authorization=True,
|
|
tools=[],
|
|
labels=labels or [],
|
|
)
|
|
|
|
if decrypt_credentials:
|
|
# init tool configuration
|
|
tool_configuration = ProviderConfigEncrypter(
|
|
tenant_id=db_provider.tenant_id,
|
|
config=[x.to_basic_provider_config() for x in provider_controller.get_credentials_schema()],
|
|
provider_type=provider_controller.provider_type.value,
|
|
provider_identity=provider_controller.entity.identity.name,
|
|
)
|
|
|
|
# decrypt the credentials and mask the credentials
|
|
decrypted_credentials = tool_configuration.decrypt(data=credentials)
|
|
masked_credentials = tool_configuration.mask_tool_credentials(data=decrypted_credentials)
|
|
|
|
result.masked_credentials = masked_credentials
|
|
|
|
return result
|
|
|
|
@staticmethod
|
|
def convert_tool_entity_to_api_entity(
|
|
tool: Union[ApiToolBundle, WorkflowTool, Tool],
|
|
tenant_id: str,
|
|
credentials: dict | None = None,
|
|
labels: list[str] | None = None,
|
|
) -> ToolApiEntity:
|
|
"""
|
|
convert tool to user tool
|
|
"""
|
|
if isinstance(tool, Tool):
|
|
# fork tool runtime
|
|
tool = tool.fork_tool_runtime(
|
|
runtime=ToolRuntime(
|
|
credentials=credentials or {},
|
|
tenant_id=tenant_id,
|
|
)
|
|
)
|
|
|
|
# get tool parameters
|
|
parameters = tool.entity.parameters or []
|
|
# get tool runtime parameters
|
|
runtime_parameters = tool.get_runtime_parameters()
|
|
# override parameters
|
|
current_parameters = parameters.copy()
|
|
for runtime_parameter in runtime_parameters:
|
|
found = False
|
|
for index, parameter in enumerate(current_parameters):
|
|
if parameter.name == runtime_parameter.name and parameter.form == runtime_parameter.form:
|
|
current_parameters[index] = runtime_parameter
|
|
found = True
|
|
break
|
|
|
|
if not found and runtime_parameter.form == ToolParameter.ToolParameterForm.FORM:
|
|
current_parameters.append(runtime_parameter)
|
|
|
|
return ToolApiEntity(
|
|
author=tool.entity.identity.author,
|
|
name=tool.entity.identity.name,
|
|
label=tool.entity.identity.label,
|
|
description=tool.entity.description.human if tool.entity.description else I18nObject(en_US=""),
|
|
output_schema=tool.entity.output_schema,
|
|
parameters=current_parameters,
|
|
labels=labels or [],
|
|
)
|
|
if isinstance(tool, ApiToolBundle):
|
|
return ToolApiEntity(
|
|
author=tool.author,
|
|
name=tool.operation_id or "",
|
|
label=I18nObject(en_US=tool.operation_id, zh_Hans=tool.operation_id),
|
|
description=I18nObject(en_US=tool.summary or "", zh_Hans=tool.summary or ""),
|
|
parameters=tool.parameters,
|
|
labels=labels or [],
|
|
)
|