fix: change the icon handle logic

This commit is contained in:
Novice
2025-06-23 16:04:12 +08:00
parent c7e72f7365
commit a67325f444
2 changed files with 14 additions and 6 deletions

View File

@ -7,6 +7,7 @@ from deprecated import deprecated
from sqlalchemy import ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column
from core.file import helpers as file_helpers
from core.mcp.types import Tool
from core.tools.entities.common_entities import I18nObject
from core.tools.entities.tool_bundle import ApiToolBundle
@ -248,8 +249,11 @@ class MCPToolProvider(Base):
return [Tool(**tool) for tool in json.loads(self.tools)]
@property
def provider_icon(self) -> dict[str, str]:
return cast(dict[str, str], json.loads(self.icon))
def provider_icon(self) -> dict[str, str] | str:
try:
return cast(dict[str, str], json.loads(self.icon))
except json.JSONDecodeError:
return file_helpers.get_signed_file_url(self.icon)
class ToolModelInvoke(Base):

View File

@ -154,8 +154,14 @@ class MCPToolManageService:
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
if mcp_provider is None:
raise ValueError("MCP tool not found")
encrypted_server_url = encrypter.encrypt_token(tenant_id, server_url)
mcp_provider.name = name
mcp_provider.icon = (
json.dumps({"content": icon, "background": icon_background}) if icon_type == "emoji" else icon
)
if "[__HIDDEN__]" in server_url:
db.session.commit()
return
encrypted_server_url = encrypter.encrypt_token(tenant_id, server_url)
mcp_provider.server_url = encrypted_server_url
server_url_hash = hashlib.sha256(server_url.encode()).hexdigest()
# if the server url is changed, we need to re-auth the tool
@ -176,9 +182,7 @@ class MCPToolManageService:
mcp_provider.tools = "[]"
mcp_provider.encrypted_credentials = "{}"
mcp_provider.server_url_hash = server_url_hash
mcp_provider.icon = (
json.dumps({"content": icon, "background": icon_background}) if icon_type == "emoji" else icon
)
db.session.commit()
except IntegrityError as e:
db.session.rollback()