Merge branch 'fix/chore-fix' into dev/plugin-deploy
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions

This commit is contained in:
Yeuoly 2024-10-30 16:09:33 +08:00
commit fb3d03b790
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61
6 changed files with 36 additions and 19 deletions

View File

@ -128,12 +128,7 @@ class ToolBuiltinProviderGetCredentialsApi(Resource):
class ToolBuiltinProviderIconApi(Resource): class ToolBuiltinProviderIconApi(Resource):
@setup_required @setup_required
def get(self, provider): def get(self, provider):
user = current_user icon_bytes, mimetype = BuiltinToolManageService.get_builtin_tool_provider_icon(provider)
user_id = user.id
tenant_id = user.current_tenant_id
icon_bytes, mimetype = BuiltinToolManageService.get_builtin_tool_provider_icon(provider, tenant_id)
icon_cache_max_age = dify_config.TOOL_ICON_CACHE_MAX_AGE icon_cache_max_age = dify_config.TOOL_ICON_CACHE_MAX_AGE
return send_file(io.BytesIO(icon_bytes), mimetype=mimetype, max_age=icon_cache_max_age) return send_file(io.BytesIO(icon_bytes), mimetype=mimetype, max_age=icon_cache_max_age)

View File

@ -56,6 +56,17 @@ class ToolManager:
_builtin_providers_loaded = False _builtin_providers_loaded = False
_builtin_tools_labels = {} _builtin_tools_labels = {}
@classmethod
def get_hardcoded_provider(cls, provider: str) -> BuiltinToolProviderController:
"""
get the hardcoded provider
"""
if len(cls._hardcoded_providers) == 0:
# init the builtin providers
cls.load_hardcoded_providers_cache()
return cls._hardcoded_providers[provider]
@classmethod @classmethod
def get_builtin_provider( def get_builtin_provider(
cls, provider: str, tenant_id: str cls, provider: str, tenant_id: str
@ -407,9 +418,9 @@ class ToolManager:
return tool_entity return tool_entity
@classmethod @classmethod
def get_builtin_provider_icon(cls, provider: str, tenant_id: str) -> tuple[str, str]: def get_hardcoded_provider_icon(cls, provider: str) -> tuple[str, str]:
""" """
get the absolute path of the icon of the builtin provider get the absolute path of the icon of the hardcoded provider
:param provider: the name of the provider :param provider: the name of the provider
:param tenant_id: the id of the tenant :param tenant_id: the id of the tenant
@ -417,7 +428,7 @@ class ToolManager:
:return: the absolute path of the icon, the mime type of the icon :return: the absolute path of the icon, the mime type of the icon
""" """
# get provider # get provider
provider_controller = cls.get_builtin_provider(provider, tenant_id) provider_controller = cls.get_hardcoded_provider(provider)
absolute_path = path.join( absolute_path = path.join(
path.dirname(path.realpath(__file__)), path.dirname(path.realpath(__file__)),

View File

@ -60,6 +60,7 @@ class WorkflowToolProviderController(ToolProviderController):
icon=db_provider.icon, icon=db_provider.icon,
), ),
credentials_schema=[], credentials_schema=[],
plugin_id=None,
), ),
provider_id=db_provider.id, provider_id=db_provider.id,
) )

View File

@ -178,11 +178,11 @@ class BuiltinToolManageService:
return {"result": "success"} return {"result": "success"}
@staticmethod @staticmethod
def get_builtin_tool_provider_icon(provider: str, tenant_id: str): def get_builtin_tool_provider_icon(provider: str):
""" """
get tool provider icon and it's mimetype get tool provider icon and it's mimetype
""" """
icon_path, mime_type = ToolManager.get_builtin_provider_icon(provider, tenant_id) icon_path, mime_type = ToolManager.get_hardcoded_provider_icon(provider)
icon_bytes = Path(icon_path).read_bytes() icon_bytes = Path(icon_path).read_bytes()
return icon_bytes, mime_type return icon_bytes, mime_type
@ -233,7 +233,7 @@ class BuiltinToolManageService:
) )
# add icon # add icon
ToolTransformService.repack_provider(user_builtin_provider) ToolTransformService.repack_provider(tenant_id=tenant_id, provider=user_builtin_provider)
tools = provider_controller.get_tools() tools = provider_controller.get_tools()
for tool in tools: for tool in tools:

View File

@ -19,7 +19,7 @@ class ToolCommonService:
# add icon # add icon
for provider in providers: for provider in providers:
ToolTransformService.repack_provider(provider) ToolTransformService.repack_provider(tenant_id=tenant_id, provider=provider)
result = [provider.to_dict() for provider in providers] result = [provider.to_dict() for provider in providers]

View File

@ -2,6 +2,8 @@ import json
import logging import logging
from typing import Optional, Union from typing import Optional, Union
from yarl import URL
from configs import dify_config from configs import dify_config
from core.tools.__base.tool import Tool from core.tools.__base.tool import Tool
from core.tools.__base.tool_runtime import ToolRuntime from core.tools.__base.tool_runtime import ToolRuntime
@ -25,15 +27,20 @@ logger = logging.getLogger(__name__)
class ToolTransformService: 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 @classmethod
def get_tool_provider_icon_url(cls, provider_type: str, provider_name: str, icon: str | dict) -> Union[str, dict]: def get_tool_provider_icon_url(cls, provider_type: str, provider_name: str, icon: str | dict) -> Union[str, dict]:
""" """
get tool provider icon url get tool provider icon url
""" """
url_prefix = dify_config.CONSOLE_API_URL + "/console/api/workspaces/current/tool-provider/" url_prefix = URL(dify_config.CONSOLE_API_URL) / "console" / "api" / "workspaces" / "current" / "tool-provider"
if provider_type == ToolProviderType.BUILT_IN.value: if provider_type == ToolProviderType.BUILT_IN.value:
return url_prefix + "builtin/" + provider_name + "/icon" return str(url_prefix / "builtin" / provider_name / "icon")
elif provider_type in {ToolProviderType.API.value, ToolProviderType.WORKFLOW.value}: elif provider_type in {ToolProviderType.API.value, ToolProviderType.WORKFLOW.value}:
try: try:
if isinstance(icon, str): if isinstance(icon, str):
@ -45,7 +52,7 @@ class ToolTransformService:
return "" return ""
@staticmethod @staticmethod
def repack_provider(provider: Union[dict, ToolProviderApiEntity]): def repack_provider(tenant_id: str, provider: Union[dict, ToolProviderApiEntity]):
""" """
repack provider repack provider
@ -56,6 +63,9 @@ class ToolTransformService:
provider_type=provider["type"], provider_name=provider["name"], icon=provider["icon"] provider_type=provider["type"], provider_name=provider["name"], icon=provider["icon"]
) )
elif isinstance(provider, ToolProviderApiEntity): elif isinstance(provider, ToolProviderApiEntity):
if provider.plugin_id:
provider.icon = ToolTransformService.get_plugin_icon_url(tenant_id=tenant_id, filename=provider.icon)
else:
provider.icon = ToolTransformService.get_tool_provider_icon_url( provider.icon = ToolTransformService.get_tool_provider_icon_url(
provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon
) )