Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(app): include image icons in app packages (#42626)

林玮 (Jade Lin)1fa3245e835f
diff --git a/api/services/agent/package_resource_exporter.py b/api/services/agent/package_resource_exporter.py
index 2f16238c939..57ab3133b3a 100644
--- a/api/services/agent/package_resource_exporter.py
+++ b/api/services/agent/package_resource_exporter.py
@@ -8,12 +8,13 @@ import re
import zipfile
from collections.abc import Generator, Sequence
from dataclasses import dataclass
-from typing import Literal, Protocol
+from typing import Any, Literal, Protocol
from sqlalchemy import select
from sqlalchemy.orm import Session
from configs import dify_config
+from constants import IMAGE_EXTENSIONS
from extensions.ext_storage import storage
from models.agent import Agent
from models.agent_config_entities import AgentSoulConfig
@@ -23,6 +24,7 @@ from services.agent.dsl_entities import AgentPackage, AgentPackageWorkspaceSkill
from services.agent.errors import RosterAgentPackageExportFailedError, RosterAgentPackageTooLargeError
from services.agent.roster_package_entities import (
AgentPackageResources,
+ PackageIcon,
RosterAgentPackageAudit,
RosterAgentPackageFile,
RosterAgentPackageSkill,
@@ -59,6 +61,7 @@ class AgentPackageResourceExporter:
def __init__(self, *, storage_backend: _Storage = storage) -> None:
self._storage = storage_backend
self.sources: dict[str, tuple[list[_SkillSource], list[_FileSource]]] = {}
+ self.icon_sources: dict[str, tuple[str, str]] = {}
self.packages: dict[str, AgentPackage] = {}
self._workspace_sources: dict[str, tuple[str, str, str | None, bool]] = {}
@@ -81,10 +84,46 @@ class AgentPackageResourceExporter:
)
self.sources[package_ref] = skills, files
package = make_portable_agent_package(agent, soul, include_assets=True)
+ metadata = package.metadata.model_dump()
+ self.collect_icon(session=session, tenant_id=agent.tenant_id, metadata=metadata)
+ package.metadata.icon = metadata["icon"]
self.packages[package_ref] = package
self._workspace_sources[package_ref] = agent.tenant_id, agent.id, snapshot_id, include_draft
return package
+ def collect_icon(self, *, session: Session, tenant_id: str, metadata: dict[str, Any]) -> None:
+ if metadata.get("icon_type") != "image" or not metadata.get("icon"):
+ return
+ file_id = metadata["icon"]
+ if file_id not in self.icon_sources:
+ upload = self._upload_files(session=session, tenant_id=tenant_id, file_ids=[file_id]).get(file_id)
+ if upload is None:
+ raise RosterAgentPackageExportFailedError("App icon payload is unavailable")
+ extension = upload.extension.lower()
+ if extension not in IMAGE_EXTENSIONS:
+ raise RosterAgentPackageExportFailedError("App icon must be an image")
+ resource_id = f"i_{len(self.icon_sources) + 1:06d}"
+ self.icon_sources[file_id] = f"{resource_id}.{extension}", upload.key
+ metadata["icon"] = self.icon_sources[file_id][0].split(".")[0]
+
+ def write_icons(self, archive: zipfile.ZipFile, *, total_size: int) -> tuple[list[PackageIcon], int]:
+ if len(archive.infolist()) + len(self.icon_sources) + 2 > dify_config.AGENT_PACKAGE_MAX_ENTRIES:
+ raise RosterAgentPackageTooLargeError("Agent package has too many members")
+ icons = []
+ for path, key in self.icon_sources.values():
+ size, digest = self._write_storage_member(
+ archive,
+ path=path,
+ storage_key=key,
+ max_bytes=min(
+ dify_config.AGENT_PACKAGE_MAX_BYTES - total_size,
+ dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT * 1024 * 1024,
+ ),
+ )
+ total_size += size
+ icons.append(PackageIcon(id=path.split(".")[0], path=path, size=size, sha256=digest))
+ return icons, total_size
+
def collect_workspace_skills(self) -> None:
"""Resolve legacy Skill metadata after the caller closes its database read session."""
for ref, (tenant_id, agent_id, snapshot_id, include_draft) in self._workspace_sources.items():
diff --git a/api/services/agent/package_resource_importer.py b/api/services/agent/package_resource_importer.py
index 372f4e7e6a9..ea1acdbc982 100644
--- a/api/services/agent/package_resource_importer.py
+++ b/api/services/agent/package_resource_importer.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import hashlib
+import mimetypes
import os
from typing import Protocol
from uuid import uuid4
@@ -24,7 +25,7 @@ from services.agent.errors import (
PlaintextSecretNotAllowedError,
RosterAgentPackageResourceUnavailableError,
)
-from services.agent.roster_package_entities import AgentPackageResources, PreparedPackageArchive
+from services.agent.roster_package_entities import AgentPackageResources, PackageIcon, PreparedPackageArchive
from services.agent.roster_package_reader import RosterAgentPackageReader
from services.agent.skill_package_service import SkillPackageError, SkillPackageService
from services.entities.dsl_entities import DslImportWarning
@@ -213,6 +214,40 @@ class AgentPackageResourceImporter:
# Localized Skills must not bind to same-named Skills in the target workspace.
return agent_package.model_copy(update={"soul": soul, "workspace_skills": []}), warnings
+ def materialize_icons(
+ self, *, archive: PreparedPackageArchive, icons: list[PackageIcon], tenant_id: str, account_id: str
+ ) -> dict[str, str]:
+ files: list[ToolFile | UploadFile] = []
+ mapping: dict[str, str] = {}
+ for icon in icons:
+ payload = self._reader.read_member_bytes(
+ archive, icon.path, max_bytes=dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT * 1024 * 1024
+ )
+ extension = self._extension(icon.path)
+ file_id = str(uuid4())
+ key = f"upload_files/{tenant_id}/{file_id}.{extension}"
+ row = UploadFile(
+ tenant_id=tenant_id,
+ storage_type=StorageType(dify_config.STORAGE_TYPE),
+ key=key,
+ name=icon.path,
+ size=len(payload),
+ extension=extension,
+ mime_type=mimetypes.guess_type(icon.path)[0] or "application/octet-stream",
+ created_by_role=CreatorUserRole.ACCOUNT,
+ created_by=account_id,
+ created_at=naive_utc_now(),
+ used=False,
+ hash=hashlib.sha3_256(payload).hexdigest(),
+ )
+ row.id = file_id
+ self._save_resource(storage_key=key, payload=payload, files=files, row=row)
+ mapping[icon.id] = file_id
+ if files:
+ with session_factory.create_session() as session, session.begin():
+ session.add_all(files)
+ return mapping
+
def _save_resource(
self,
*,
diff --git a/api/services/agent/roster_package_entities.py b/api/services/agent/roster_package_entities.py
index d9c6515009c..20fa86aa920 100644
--- a/api/services/agent/roster_package_entities.py
+++ b/api/services/agent/roster_package_entities.py
@@ -5,10 +5,11 @@ from __future__ import annotations
import re
from collections.abc import Iterable, Mapping
from dataclasses import dataclass, field
-from typing import BinaryIO, Final, Literal, Self
+from typing import Any, BinaryIO, Final, Literal, Self
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
+from constants import IMAGE_EXTENSIONS
from services.agent.dsl_entities import AgentAppDsl, AgentPackage
ROSTER_AGENT_PACKAGE_FORMAT: Final[Literal["dify.roster-agent"]] = "dify.roster-agent"
@@ -81,6 +82,16 @@ class RosterAgentPackageFile(_RosterAgentPackageResource):
return self
+class PackageIcon(_RosterAgentPackageResource):
+ id: str = Field(pattern=r"^i_[0-9]{6}$")
+
+ @model_validator(mode="after")
+ def validate_icon_path(self) -> Self:
+ if self.path not in {f"{self.id}.{extension}" for extension in IMAGE_EXTENSIONS}:
+ raise ValueError("icon path must use its resource id and an image extension")
+ return self
+
+
class AgentPackageResources(BaseModel):
"""Resources belonging to one portable Agent, independent of its container."""
@@ -154,11 +165,12 @@ class RosterAgentPackageManifest(AgentPackageResources):
format: Literal["dify.roster-agent"]
format_version: Literal[1]
audit: RosterAgentPackageAudit | None = None
+ icons: list[PackageIcon] = Field(default_factory=list)
apps: list[RosterAgentPackageApp] = Field(min_length=1)
@model_validator(mode="after")
def validate_app_paths(self) -> Self:
- paths = [item.path.casefold() for item in [*self.apps, *self.skills, *self.files]]
+ paths = [item.path.casefold() for item in [*self.apps, *self.skills, *self.files, *self.icons]]
if len(paths) != len(set(paths)):
raise ValueError("resource paths must be unique")
return self
@@ -167,6 +179,9 @@ class RosterAgentPackageManifest(AgentPackageResources):
if set(apps) != {item.path for item in self.apps}:
raise ValueError("app members must match the package app index")
self.validate_packages(app.package for app in apps.values())
+ validate_icon_references(
+ self.icons, [metadata for app in apps.values() for metadata in (app.app, app.package.metadata.model_dump())]
+ )
@dataclass(kw_only=True)
@@ -211,11 +226,28 @@ class RosterAgentPackageExport:
self.close()
+def validate_icon_references(icons: list[PackageIcon], metadata: list[dict[str, Any]]) -> None:
+ references: set[str] = set()
+ for item in metadata:
+ if item.get("icon_type") == "image":
+ reference = item.get("icon")
+ if reference is not None and not isinstance(reference, str):
+ raise ValueError("Image icon references must be strings")
+ if reference:
+ references.add(reference)
+ ids = {item.id for item in icons}
+ if len(ids) != len(icons):
+ raise ValueError("Packaged icons must have unique ids")
+ if ids - references or any(ref.startswith("i_") and ref not in ids for ref in references):
+ raise ValueError("Packaged icons must match image references")
+
+
__all__ = [
"ROSTER_AGENT_PACKAGE_FORMAT",
"ROSTER_AGENT_PACKAGE_FORMAT_VERSION",
"ROSTER_AGENT_PACKAGE_MAX_SIGNATURE_BYTES",
"AgentPackageResources",
+ "PackageIcon",
"PreparedPackageArchive",
"PreparedRosterAgentPackage",
"RosterAgentPackageApp",
@@ -225,4 +257,5 @@ __all__ = [
"RosterAgentPackageManifest",
"RosterAgentPackageMember",
"RosterAgentPackageSkill",
+ "validate_icon_references",
]
diff --git a/api/services/agent/roster_package_exporter.py b/api/services/agent/roster_package_exporter.py
index bc41a4ac798..ed2bb631576 100644
--- a/api/services/agent/roster_package_exporter.py
+++ b/api/services/agent/roster_package_exporter.py
@@ -135,6 +135,7 @@ class RosterAgentPackageExporter:
include_draft=draft is not None,
)
app = make_agent_app_dsl(app_model, package_ref="agent_1", packages={"agent_1": package}, dependencies=[])
+ resources.collect_icon(session=session, tenant_id=tenant_id, metadata=app.app)
audit = RosterAgentPackageAudit(ref=agent.id)
dependency_ids = extract_agent_soul_dependencies(package.soul)
@@ -162,6 +163,7 @@ class RosterAgentPackageExporter:
with zipfile.ZipFile(output, "w", compression=zipfile.ZIP_STORED, allowZip64=True) as archive:
groups, total_size = resources.write_resources(archive)
group = groups[app.agent.package_ref]
+ icons, total_size = resources.write_icons(archive, total_size=total_size)
app_bytes = yaml.safe_dump(
app.model_dump(mode="json", exclude_none=True), allow_unicode=True, sort_keys=False
).encode("utf-8")
@@ -174,12 +176,15 @@ class RosterAgentPackageExporter:
path="app.yaml", size=len(app_bytes), sha256=hashlib.sha256(app_bytes).hexdigest()
)
],
+ icons=icons,
skills=group.skills,
files=group.files,
)
manifest.validate_apps({"app.yaml": app})
manifest_bytes = yaml.safe_dump(
- manifest.model_dump(mode="json", exclude_none=True), allow_unicode=True, sort_keys=False
+ manifest.model_dump(mode="json", exclude_none=True, exclude={"icons"} if not icons else set()),
+ allow_unicode=True,
+ sort_keys=False,
).encode("utf-8")
for path, document_bytes in (("app.yaml", app_bytes), ("manifest.yaml", manifest_bytes)):
if len(document_bytes) > dify_config.AGENT_PACKAGE_MAX_MANIFEST_BYTES:
diff --git a/api/services/agent/roster_package_importer.py b/api/services/agent/roster_package_importer.py
index 70c64567023..3dbbe8d504a 100644
--- a/api/services/agent/roster_package_importer.py
+++ b/api/services/agent/roster_package_importer.py
@@ -20,10 +20,11 @@ from models.agent import (
AgentConfigDraftType,
AgentConfigRevisionOperation,
AgentConfigSnapshot,
+ AgentIconType,
AgentSource,
)
from models.agent_config_entities import AgentSoulConfig
-from models.model import App, AppMode, AppModelConfig, UploadFile
+from models.model import App, AppMode, AppModelConfig, IconType, UploadFile
from services.agent.dsl_entities import AgentPackage, AgentPackageMetadata
from services.agent.dsl_service import AgentDslService
from services.agent.errors import (
@@ -40,7 +41,7 @@ from services.agent.roster_service import AgentRosterService
from services.app_creation_records import create_installed_app_record, create_site_record
from services.app_service import AppService
from services.entities.dsl_entities import DslImportWarning
-from services.icon_configuration import DEFAULT_ICON, DEFAULT_ICON_BACKGROUND, DEFAULT_ICON_TYPE
+from services.icon_configuration import DEFAULT_ICON, DEFAULT_ICON_BACKGROUND, DEFAULT_ICON_TYPE, is_valid_image_icon
logger = logging.getLogger(__name__)
@@ -74,6 +75,19 @@ class RosterAgentPackageImporter:
check_package_dependencies(tenant_id=tenant_id, account=account, dependencies=app_dsl.dependencies)
try:
+ icons = self._resources.materialize_icons(
+ archive=package, icons=package.manifest.icons, tenant_id=tenant_id, account_id=account.id
+ )
+ if agent_package.metadata.icon_type == "image" and agent_package.metadata.icon in icons:
+ agent_package.metadata.icon = icons[agent_package.metadata.icon]
+ app_metadata = AgentPackageMetadata(
+ name=agent_package.metadata.name,
+ icon_type=app_dsl.app.get("icon_type"),
+ icon=app_dsl.app.get("icon"),
+ icon_background=app_dsl.app.get("icon_background"),
+ )
+ if app_metadata.icon_type == "image" and app_metadata.icon in icons:
+ app_metadata.icon = icons[app_metadata.icon]
materialized, skill_warnings = self._resources.materialize(
archive=package,
resources=package.manifest,
@@ -92,6 +106,7 @@ class RosterAgentPackageImporter:
tenant_id=tenant_id,
account=account,
metadata=agent_package.metadata,
+ app_metadata=app_metadata,
soul=resolved_soul,
)
except Exception as exc:
@@ -127,6 +142,7 @@ class RosterAgentPackageImporter:
account: Account,
metadata: AgentPackageMetadata,
soul: AgentSoulConfig,
+ app_metadata: AgentPackageMetadata | None = None,
) -> tuple[str, str]:
with session_factory.create_session() as session, session.begin():
name = AgentDslService(session).unique_roster_name(tenant_id=tenant_id, requested=metadata.name)
@@ -138,6 +154,13 @@ class RosterAgentPackageImporter:
app.icon_type = DEFAULT_ICON_TYPE
app.icon = DEFAULT_ICON
app.icon_background = DEFAULT_ICON_BACKGROUND
+ app_metadata = app_metadata or metadata
+ if app_metadata.icon_type in {item.value for item in IconType} and is_valid_image_icon(
+ session=session, tenant_id=tenant_id, icon_type=app_metadata.icon_type, icon=app_metadata.icon
+ ):
+ app.icon_type = IconType(app_metadata.icon_type)
+ app.icon = app_metadata.icon or DEFAULT_ICON
+ app.icon_background = app_metadata.icon_background or DEFAULT_ICON_BACKGROUND
app.api_rph = 0
app.api_rpm = 0
app.max_active_requests = None
@@ -157,10 +180,19 @@ class RosterAgentPackageImporter:
name=name,
description=metadata.description,
role=metadata.role,
+ icon_type=app.icon_type,
+ icon=app.icon,
+ icon_background=app.icon_background,
source=AgentSource.IMPORTED,
initial_soul=soul,
revision_operation=AgentConfigRevisionOperation.IMPORT_PACKAGE,
)
+ if metadata.icon_type in {item.value for item in AgentIconType} and is_valid_image_icon(
+ session=session, tenant_id=tenant_id, icon_type=metadata.icon_type, icon=metadata.icon
+ ):
+ agent.icon_type = AgentIconType(metadata.icon_type)
+ agent.icon = metadata.icon
+ agent.icon_background = metadata.icon_background
snapshot = session.get(AgentConfigSnapshot, agent.active_config_snapshot_id)
if snapshot is None:
raise RuntimeError("Imported Agent snapshot was not created")
diff --git a/api/services/agent/roster_package_reader.py b/api/services/agent/roster_package_reader.py
index 066f4627692..fa6d75ac0f2 100644
--- a/api/services/agent/roster_package_reader.py
+++ b/api/services/agent/roster_package_reader.py
@@ -21,6 +21,7 @@ from services.agent.dsl_entities import AgentAppDsl
from services.agent.errors import InvalidRosterAgentPackageError, RosterAgentPackageTooLargeError
from services.agent.roster_package_entities import (
ROSTER_AGENT_PACKAGE_MAX_SIGNATURE_BYTES,
+ PackageIcon,
PreparedPackageArchive,
PreparedRosterAgentPackage,
RosterAgentPackageApp,
@@ -131,6 +132,7 @@ class RosterAgentPackageReader:
*(item.path for item in manifest.apps),
*(item.path for item in manifest.skills),
*(item.path for item in manifest.files),
+ *(item.path for item in manifest.icons),
}
actual_paths = set(info_by_path)
if "signature.sig" in actual_paths:
@@ -156,7 +158,7 @@ class RosterAgentPackageReader:
self._validate_resource_members(
archive,
info_by_path,
- [*manifest.skills, *manifest.files],
+ [*manifest.skills, *manifest.files, *manifest.icons],
members=members,
invalid_skills=invalid_skills,
streamed_size=streamed_size,
@@ -171,7 +173,7 @@ class RosterAgentPackageReader:
self,
archive: zipfile.ZipFile,
info_by_path: dict[str, zipfile.ZipInfo],
- resources: Sequence[RosterAgentPackageSkill | RosterAgentPackageFile],
+ resources: Sequence[RosterAgentPackageSkill | RosterAgentPackageFile | PackageIcon],
*,
members: dict[str, RosterAgentPackageMember],
invalid_skills: dict[str, str],
@@ -180,6 +182,11 @@ class RosterAgentPackageReader:
nested_uncompressed_size = 0
for resource in resources:
info = info_by_path[resource.path]
+ if (
+ isinstance(resource, PackageIcon)
+ and resource.size > dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT * 1024 * 1024
+ ):
+ raise RosterAgentPackageTooLargeError("Packaged icon exceeds the image size limit")
payload = b""
remaining_package_bytes = dify_config.AGENT_PACKAGE_MAX_BYTES - streamed_size
if isinstance(resource, RosterAgentPackageSkill):
diff --git a/api/services/app_dsl_service.py b/api/services/app_dsl_service.py
index d9890a57ea5..df5a01143db 100644
--- a/api/services/app_dsl_service.py
+++ b/api/services/app_dsl_service.py
@@ -286,7 +286,7 @@ class AppDslService:
error="Missing app data in YAML content",
)
- if package is not None and package.agent_resources:
+ if package is not None and (package.agent_resources or package.icons):
tenant_id = account.current_tenant_id
if tenant_id is None:
raise ValueError("Current tenant is not set")
@@ -298,9 +298,11 @@ class AppDslService:
if target is None:
raise ValueError("App not found")
self._validate_workflow_overwrite(target, data)
- data["agent_packages"], self._warnings = package.materialize_agents(
- tenant_id=tenant_id, account_id=account.id
- )
+ package.materialize_icons(data=data, tenant_id=tenant_id, account_id=account.id)
+ if package.agent_resources:
+ data["agent_packages"], self._warnings = package.materialize_agents(
+ tenant_id=tenant_id, account_id=account.id
+ )
content = yaml.safe_dump(data, allow_unicode=True)
# If app_id is provided, check if it exists
diff --git a/api/services/app_package_service.py b/api/services/app_package_service.py
index 26a5d85d393..3fd44f42ae4 100644
--- a/api/services/app_package_service.py
+++ b/api/services/app_package_service.py
@@ -8,7 +8,7 @@ import tempfile
import zipfile
import zlib
from collections.abc import Iterator
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from typing import Any, BinaryIO, Literal, Self, cast
import yaml
@@ -25,12 +25,14 @@ from services.agent.package_resource_exporter import AgentPackageResourceExporte
from services.agent.package_resource_importer import AgentPackageResourceImporter
from services.agent.roster_package_entities import (
AgentPackageResources,
+ PackageIcon,
PreparedPackageArchive,
RosterAgentPackageApp,
RosterAgentPackageExport,
RosterAgentPackageFile,
RosterAgentPackageMember,
RosterAgentPackageSkill,
+ validate_icon_references,
)
from services.agent.roster_package_reader import RosterAgentPackageReader
from services.dsl_content import DSL_MAX_SIZE
@@ -46,8 +48,10 @@ class AppPackageManifest(BaseModel):
format_version: Literal[1]
apps: list[RosterAgentPackageApp] = Field(min_length=1, max_length=1)
agent_resources: dict[str, AgentPackageResources] = Field(default_factory=dict)
+ icons: list[PackageIcon] = Field(default_factory=list)
- def iter_resources(self) -> Iterator[RosterAgentPackageSkill | RosterAgentPackageFile]:
+ def iter_resources(self) -> Iterator[RosterAgentPackageSkill | RosterAgentPackageFile | PackageIcon]:
+ yield from self.icons
for group in self.agent_resources.values():
yield from group.skills
yield from group.files
@@ -67,6 +71,20 @@ class PreparedAppPackage(PreparedPackageArchive):
dsl: str
agents: dict[str, AgentPackage]
agent_resources: dict[str, AgentPackageResources]
+ icons: list[PackageIcon] = field(default_factory=list)
+
+ def materialize_icons(self, *, data: dict[str, Any], tenant_id: str, account_id: str) -> None:
+ importer = AgentPackageResourceImporter()
+ for ref, resources in self.agent_resources.items():
+ importer.validate(resources=resources, agent_package=self.agents[ref])
+ mapping = importer.materialize_icons(archive=self, icons=self.icons, tenant_id=tenant_id, account_id=account_id)
+ metadata = [data["app"], *(agent.metadata for agent in self.agents.values())]
+ for item in metadata:
+ if isinstance(item, dict):
+ if item.get("icon_type") == "image" and item.get("icon") in mapping:
+ item["icon"] = mapping[item["icon"]]
+ elif item.icon_type == "image" and item.icon in mapping:
+ item.icon = mapping[item.icon]
def materialize_agents(self, *, tenant_id: str, account_id: str) -> tuple[dict[str, Any], list[DslImportWarning]]:
importer = AgentPackageResourceImporter()
@@ -121,6 +139,12 @@ class AppPackageService(RosterAgentPackageReader):
):
raise InvalidRosterAgentPackageError("App package DSL is invalid")
agents = self._validate_agents(app_data, manifest)
+ try:
+ validate_icon_references(
+ manifest.icons, [app_data["app"], *(agent.metadata.model_dump() for agent in agents.values())]
+ )
+ except ValueError as exc:
+ raise InvalidRosterAgentPackageError("App package icon references are invalid") from exc
members: dict[str, RosterAgentPackageMember] = {}
invalid_skills: dict[str, str] = {}
self._validate_resource_members(
@@ -139,6 +163,7 @@ class AppPackageService(RosterAgentPackageReader):
dsl=dsl,
agents=agents,
agent_resources=manifest.agent_resources,
+ icons=manifest.icons,
members=members,
invalid_skills=invalid_skills,
)
@@ -193,6 +218,7 @@ class AppPackageService(RosterAgentPackageReader):
workflow_id=workflow_id,
resource_exporter=resources,
)
+ resources.collect_icon(session=session, tenant_id=app_model.tenant_id, metadata=data["app"])
resources.collect_workspace_skills()
if resources.packages:
data["agent_packages"] = {
@@ -209,7 +235,8 @@ class AppPackageService(RosterAgentPackageReader):
archive = cast(BinaryIO, tempfile.SpooledTemporaryFile(max_size=16 * 1024 * 1024, mode="w+b")) # noqa: SIM115
try:
with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as package:
- agent_resources, _ = resources.write_resources(package) if resources is not None else ({}, 0)
+ agent_resources, total_size = resources.write_resources(package) if resources is not None else ({}, 0)
+ icons, _ = resources.write_icons(package, total_size=total_size) if resources is not None else ([], 0)
manifest = AppPackageManifest(
format="dify.app",
format_version=1,
@@ -219,6 +246,7 @@ class AppPackageService(RosterAgentPackageReader):
)
],
agent_resources=agent_resources,
+ icons=icons,
)
package.writestr(
"manifest.yaml", yaml.safe_dump(manifest.model_dump(mode="json", exclude_defaults=True))
diff --git a/api/tests/unit_tests/services/agent/test_roster_package_importer.py b/api/tests/unit_tests/services/agent/test_roster_package_importer.py
index 73fab62f887..17f2e9b8503 100644
--- a/api/tests/unit_tests/services/agent/test_roster_package_importer.py
+++ b/api/tests/unit_tests/services/agent/test_roster_package_importer.py
@@ -845,3 +845,55 @@ def test_import_succeeds_when_post_commit_initialization_fails(
uploaded = session.scalar(select(UploadFile))
assert uploaded is not None
assert uploaded.used is True
+
+
[email protected]("shared_icon", [False, True])
[email protected]("duplicate_name", [False, True])
+def test_import_restores_app_and_agent_image_icons(
+ monkeypatch: pytest.MonkeyPatch,
+ sqlite_session_factory: sessionmaker[Session],
+ shared_icon: bool,
+ duplicate_name: bool,
+) -> None:
+ with zipfile.ZipFile(io.BytesIO(_package())) as archive:
+ members = {name: archive.read(name) for name in archive.namelist()}
+ manifest = yaml.safe_load(members["manifest.yaml"])
+ app = yaml.safe_load(members["app.yaml"])
+ app["app"].update(icon_type="image", icon="i_000001")
+ agent_icon = "i_000001" if shared_icon else "i_000002"
+ app["agent_packages"]["agent_1"]["metadata"].update(icon_type="image", icon=agent_icon)
+ manifest["icons"] = []
+ for icon_id in sorted({"i_000001", agent_icon}):
+ payload = f"image-{icon_id}".encode()
+ members[f"{icon_id}.png"] = payload
+ manifest["icons"].append(
+ {
+ "id": icon_id,
+ "path": f"{icon_id}.png",
+ "size": len(payload),
+ "sha256": hashlib.sha256(payload).hexdigest(),
+ }
+ )
+ members["app.yaml"] = yaml.safe_dump(app).encode()
+ manifest["apps"][0].update(size=len(members["app.yaml"]), sha256=hashlib.sha256(members["app.yaml"]).hexdigest())
+ members["manifest.yaml"] = yaml.safe_dump(manifest).encode()
+ storage = _MemoryStorage()
+ monkeypatch.setattr(AppService, "finalize_created_app", lambda *_args, **_kwargs: None)
+ importer = RosterAgentPackageImporter(storage_backend=storage)
+ if duplicate_name:
+ importer.import_package(source=io.BytesIO(_zip(members)), tenant_id="destination", account=_account())
+ result = importer.import_package(source=io.BytesIO(_zip(members)), tenant_id="destination", account=_account())
+ with sqlite_session_factory() as session:
+ imported_app = session.get(App, result.app_id)
+ agent = session.get(Agent, result.agent_id)
+ assert imported_app is not None
+ assert agent is not None
+ for owner, original_id in [(imported_app, "i_000001"), (agent, agent_icon)]:
+ assert owner.icon_type == "image"
+ upload = session.get(UploadFile, owner.icon)
+ assert upload is not None
+ assert upload.tenant_id == "destination"
+ assert upload.created_by == _account().id
+ assert storage.files[upload.key] == members[f"{original_id}.png"]
+ assert imported_app.name == agent.name == ("Imported Agent import" if duplicate_name else "Imported Agent")
+ assert (imported_app.icon == agent.icon) is shared_icon
diff --git a/api/tests/unit_tests/services/test_package_icons.py b/api/tests/unit_tests/services/test_package_icons.py
new file mode 100644
index 00000000000..975c09b7dec
--- /dev/null
+++ b/api/tests/unit_tests/services/test_package_icons.py
@@ -0,0 +1,167 @@
+"""Packaged image icons travel with Apps and receive destination-owned file ids."""
+
+import hashlib
+import io
+import zipfile
+from collections.abc import Callable
+from types import SimpleNamespace
+from unittest.mock import MagicMock, Mock
+from uuid import uuid4
+
+import pytest
+import yaml
+
+from services.agent.errors import (
+ InvalidRosterAgentPackageError,
+ RosterAgentPackageExportFailedError,
+ RosterAgentPackageTooLargeError,
+)
+from services.agent.package_resource_exporter import AgentPackageResourceExporter, _FileSource, _SkillSource
+from services.agent.package_resource_importer import AgentPackageResourceImporter
+from services.agent.roster_package_entities import PackageIcon, PreparedPackageArchive, RosterAgentPackageMember
+from services.app_package_service import AppPackageService
+
+
+def _icon_archive(*, payload: bytes = b"image", digest: str | None = None, reference: str = "i_000001") -> io.BytesIO:
+ dsl = yaml.safe_dump({"kind": "app", "app": {"mode": "chat", "icon_type": "image", "icon": reference}})
+ with AppPackageService().export(dsl=dsl.replace(reference, "legacy-id"), name="Example") as exported:
+ with zipfile.ZipFile(exported.archive) as package:
+ manifest = yaml.safe_load(package.read("manifest.yaml"))
+ app_bytes = dsl.encode()
+ manifest["apps"][0].update(size=len(app_bytes), sha256=hashlib.sha256(app_bytes).hexdigest())
+ manifest["icons"] = [
+ {
+ "id": "i_000001",
+ "path": "i_000001.png",
+ "size": len(payload),
+ "sha256": digest or hashlib.sha256(payload).hexdigest(),
+ }
+ ]
+ output = io.BytesIO()
+ with zipfile.ZipFile(output, "w") as package:
+ package.writestr("manifest.yaml", yaml.safe_dump(manifest))
+ package.writestr("app.yaml", app_bytes)
+ package.writestr("i_000001.png", payload)
+ output.seek(0)
+ return output
+
+
+def test_app_icon_export_deduplicates_uploads_and_restores_references(monkeypatch: pytest.MonkeyPatch) -> None:
+ source_id = str(uuid4())
+ payload = b"image payload"
+ backend = Mock()
+ backend.load_stream.side_effect = lambda _: iter([payload])
+ exporter = AgentPackageResourceExporter(storage_backend=backend)
+ upload_lookup = Mock(return_value={source_id: SimpleNamespace(key="source/icon.png", extension="png")})
+ monkeypatch.setattr(exporter, "_upload_files", upload_lookup)
+ metadata = {"mode": "chat", "icon_type": "image", "icon": source_id}
+ duplicate = dict(metadata)
+ exporter.collect_icon(session=Mock(), tenant_id="source-tenant", metadata=metadata)
+ exporter.collect_icon(session=Mock(), tenant_id="source-tenant", metadata=duplicate)
+ assert metadata["icon"] == duplicate["icon"] == "i_000001"
+ assert upload_lookup.call_args.kwargs["tenant_id"] == "source-tenant"
+ assert upload_lookup.call_count == 1
+ dsl = yaml.safe_dump({"kind": "app", "app": metadata})
+ with AppPackageService().export(dsl=dsl, name="Example", resources=exporter) as exported:
+ with zipfile.ZipFile(exported.archive) as package:
+ assert package.read("i_000001.png") == payload
+ exported.archive.seek(0)
+ prepared = AppPackageService().read_package(exported.archive)
+ assert prepared is not None
+ with prepared:
+ restore = Mock(return_value={"i_000001": "destination-id"})
+ monkeypatch.setattr(AgentPackageResourceImporter, "materialize_icons", restore)
+ data = yaml.safe_load(prepared.dsl)
+ prepared.materialize_icons(data=data, tenant_id="destination", account_id="account")
+ assert data["app"]["icon"] == "destination-id"
+ assert restore.call_args.kwargs["tenant_id"] == "destination"
+
+
+def test_icon_export_rejects_unavailable_tenant_upload(monkeypatch: pytest.MonkeyPatch) -> None:
+ exporter = AgentPackageResourceExporter(storage_backend=Mock())
+ monkeypatch.setattr(exporter, "_upload_files", Mock(return_value={}))
+ with pytest.raises(RosterAgentPackageExportFailedError, match="unavailable"):
+ exporter.collect_icon(session=Mock(), tenant_id="tenant", metadata={"icon_type": "image", "icon": str(uuid4())})
+
+
+def test_icon_checksum_is_required() -> None:
+ with pytest.raises(InvalidRosterAgentPackageError, match="integrity"):
+ AppPackageService().read_package(_icon_archive(digest="0" * 64))
+
+
[email protected]("reference", ["i_000002", "unrelated-upload"])
+def test_icon_must_resolve_to_metadata(reference: str) -> None:
+ with pytest.raises(InvalidRosterAgentPackageError, match="references"):
+ AppPackageService().read_package(_icon_archive(reference=reference))
+
+
+def test_icon_image_limit_is_enforced(config_overrides: Callable[..., None]) -> None:
+ config_overrides(UPLOAD_IMAGE_FILE_SIZE_LIMIT=0)
+ with pytest.raises(RosterAgentPackageTooLargeError, match="image size"):
+ AppPackageService().read_package(_icon_archive())
+
+
+def test_materialized_icon_is_owned_by_destination(monkeypatch: pytest.MonkeyPatch) -> None:
+ payload = b"image payload"
+ digest = hashlib.sha256(payload).hexdigest()
+ archive = io.BytesIO()
+ with zipfile.ZipFile(archive, "w") as package:
+ package.writestr("i_000001.png", payload)
+ prepared = PreparedPackageArchive(
+ archive=archive, members={"i_000001.png": RosterAgentPackageMember(size=len(payload), sha256=digest)}
+ )
+ icon = PackageIcon(id="i_000001", path="i_000001.png", size=len(payload), sha256=digest)
+ session = MagicMock()
+ monkeypatch.setattr(
+ "services.agent.package_resource_importer.session_factory.create_session",
+ MagicMock(return_value=session),
+ )
+ backend = Mock()
+ with prepared:
+ mapping = AgentPackageResourceImporter(storage_backend=backend).materialize_icons(
+ archive=prepared, icons=[icon], tenant_id="destination", account_id="importer"
+ )
+ row = session.__enter__.return_value.add_all.call_args.args[0][0]
+ assert row.id == mapping["i_000001"]
+ assert row.tenant_id == "destination"
+ assert row.created_by == "importer"
+ assert row.mime_type == "image/png"
+ assert row.key.startswith("upload_files/destination/")
+ backend.save.assert_called_once_with(row.key, payload)
+
+
+def test_roster_export_embeds_agent_icon(monkeypatch: pytest.MonkeyPatch) -> None:
+ from models.agent_config_entities import AgentSoulConfig
+ from models.model import App
+ from services.agent.dsl_entities import AgentPackage, AgentPackageMetadata, make_agent_app_dsl
+ from services.agent.roster_package_exporter import RosterAgentPackageExporter
+ from services.agent.roster_package_reader import RosterAgentPackageReader
+
+ backend = Mock()
+ backend.load_stream.side_effect = lambda _: iter([b"icon"])
+ resources = AgentPackageResourceExporter(storage_backend=backend)
+ resources.sources["agent_1"] = (list[_SkillSource](), list[_FileSource]())
+ metadata = {"name": "Agent", "icon_type": "image", "icon": "source-id"}
+ monkeypatch.setattr(
+ resources,
+ "_upload_files",
+ Mock(
+ return_value={
+ "source-id": SimpleNamespace(key="icon.png", extension="png"),
+ }
+ ),
+ )
+ resources.collect_icon(session=Mock(), tenant_id="source", metadata=metadata)
+ app = make_agent_app_dsl(
+ App(name="Agent", mode="agent"),
+ package_ref="agent_1",
+ packages={
+ "agent_1": AgentPackage(metadata=AgentPackageMetadata.model_validate(metadata), soul=AgentSoulConfig())
+ },
+ dependencies=[],
+ )
+ with RosterAgentPackageExporter(storage_backend=backend)._build_archive(app=app, resources=resources) as exported:
+ with RosterAgentPackageReader().read(exported.archive) as prepared:
+ assert prepared.apps["app.yaml"].package.metadata.icon == "i_000001"
+ assert prepared.manifest.icons[0].path == "i_000001.png"
+ assert RosterAgentPackageReader().read_member_bytes(prepared, "i_000001.png", max_bytes=4) == b"icon"