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.

feat(app): default exports to ifpkg across app types (#42552)

林玮 (Jade Lin)2a3cf7edf568
diff --git a/api/controllers/console/app/app.py b/api/controllers/console/app/app.py
index d24e34317f9..676bb426226 100644
--- a/api/controllers/console/app/app.py
+++ b/api/controllers/console/app/app.py
@@ -56,6 +56,7 @@ from models import Account, App, DatasetPermissionEnum, Workflow
from models.model import AppMode, IconType
from services.agent.roster_package_exporter import RosterAgentPackageExporter
from services.app_dsl_service import AppDslService
+from services.app_package_service import AppPackageService
from services.app_service import (
AppListParams,
AppListSortBy,
@@ -186,7 +187,7 @@ class CopyAppPayload(BaseModel):
class AppExportQuery(BaseModel):
format: Literal["yaml", "ifpkg"] | None = Field(
- default=None, description="Export format; defaults to ifpkg for Agent Apps and yaml for other Apps"
+ default=None, description="Export format; defaults to ifpkg for all Apps"
)
include_secret: bool = Field(default=False, description="Include secrets in export")
workflow_id: str | None = Field(default=None, description="Specific workflow ID to export")
@@ -1076,15 +1077,24 @@ class AppExportApi(Resource):
):
raise Forbidden("This feature requires a paid plan.")
- if req_data.format == "ifpkg" or (req_data.format is None and app_model.mode == AppMode.AGENT):
- if app_model.mode != AppMode.AGENT:
- raise BadRequest("The ifpkg format is only available for Agent Apps")
- agent_id = app_model.bound_agent_id_with_session(session=db.session())
- if agent_id is None:
- raise NotFound("Agent not found")
- exported = RosterAgentPackageExporter().export(
- tenant_id=app_model.tenant_id, agent_id=agent_id, version_id=req_data.version_id
- )
+ if req_data.format != "yaml":
+ if app_model.mode == AppMode.AGENT:
+ agent_id = app_model.bound_agent_id_with_session(session=db.session())
+ if agent_id is None:
+ raise NotFound("Agent not found")
+ exported = RosterAgentPackageExporter().export(
+ tenant_id=app_model.tenant_id, agent_id=agent_id, version_id=req_data.version_id
+ )
+ else:
+ exported = AppPackageService().export(
+ dsl=AppDslService.export_dsl(
+ app_model=app_model,
+ session=db.session(),
+ include_secret=req_data.include_secret,
+ workflow_id=req_data.workflow_id,
+ ),
+ name=app_model.name,
+ )
try:
archive_response = send_file(
exported.archive, mimetype="application/zip", as_attachment=True, download_name=exported.filename
diff --git a/api/controllers/console/app/app_import.py b/api/controllers/console/app/app_import.py
index a2592c81300..b0b98dff464 100644
--- a/api/controllers/console/app/app_import.py
+++ b/api/controllers/console/app/app_import.py
@@ -37,6 +37,7 @@ from services.app_dsl_service import (
PendingData,
)
from services.app_import_source import download_app_import_source, try_read_yaml
+from services.app_package_service import AppPackageService
from services.enterprise.enterprise_service import EnterpriseService
from services.entities.dsl_entities import CheckDependenciesResult, ImportStatus
from services.errors.account import NoPermissionError
@@ -103,8 +104,14 @@ class AppImportApi(Resource):
"file": {
"type": "string",
"format": "binary",
- "description": "Roster Agent .ifpkg archive",
- }
+ "description": "App .ifpkg archive",
+ },
+ "app_id": {"type": "string", "description": "App to overwrite"},
+ "name": {"type": "string"},
+ "description": {"type": "string"},
+ "icon_type": {"type": "string"},
+ "icon": {"type": "string"},
+ "icon_background": {"type": "string"},
},
"required": ["file"],
}
@@ -148,11 +155,29 @@ class AppImportApi(Resource):
)
return self._import_package(current_user, payload, source)
- @rbac_permission_required(RBACCheck(RBACPermission.AGENT_CREATE, Workspace()))
- @rbac_permission_required(RBACCheck(RBACPermission.AGENT_IMPORT_EXPORT_DSL, Workspace()))
def _import_package(
self, current_user: Account, payload: AppImportPayload | None = None, source: BinaryIO | None = None
):
+ if source is None:
+ uploaded = request.files.get("file")
+ if uploaded is None or not uploaded.filename:
+ raise InvalidRosterAgentPackageError("App package file is required")
+ if not uploaded.filename.lower().endswith(".ifpkg"):
+ raise InvalidRosterAgentPackageError("App package file must use the .ifpkg extension")
+ source = cast(BinaryIO, uploaded.stream)
+ dsl = AppPackageService().read_dsl(source)
+ if dsl is not None:
+ if payload is None:
+ payload = AppImportPayload.model_validate({**request.form.to_dict(), "mode": "yaml-content"})
+ return self._import_dsl(
+ payload.model_copy(update={"mode": "yaml-content", "yaml_content": dsl, "yaml_url": None}),
+ current_user,
+ )
+ return self._import_agent_package(current_user, source, payload)
+
+ @rbac_permission_required(RBACCheck(RBACPermission.AGENT_CREATE, Workspace()))
+ @rbac_permission_required(RBACCheck(RBACPermission.AGENT_IMPORT_EXPORT_DSL, Workspace()))
+ def _import_agent_package(self, current_user: Account, source: BinaryIO, payload: AppImportPayload | None = None):
app_id = payload.app_id if payload is not None else request.form.get("app_id")
if app_id:
raise InvalidRosterAgentPackageError("Roster Agent package import does not support overwriting an App")
@@ -160,13 +185,6 @@ class AppImportApi(Resource):
if tenant_id is None:
raise Forbidden("Current workspace is required")
importer = RosterAgentPackageImporter()
- if source is None:
- uploaded = request.files.get("file")
- if uploaded is None or not uploaded.filename:
- raise InvalidRosterAgentPackageError("Roster Agent package file is required")
- if not uploaded.filename.lower().endswith(".ifpkg"):
- raise InvalidRosterAgentPackageError("Roster Agent package file must use the .ifpkg extension")
- source = cast(BinaryIO, uploaded.stream)
result = importer.import_package(source=source, tenant_id=tenant_id, account=account)
return Import(
id=str(uuid4()),
diff --git a/api/openapi/markdown/console-openapi.md b/api/openapi/markdown/console-openapi.md
index e872f44fa76..21f8ce97dce 100644
--- a/api/openapi/markdown/console-openapi.md
+++ b/api/openapi/markdown/console-openapi.md
@@ -1502,7 +1502,7 @@ Create a new application
| Required | Schema |
| -------- | ------ |
-| Yes | **application/json**: [AppImportPayload](#appimportpayload)<br>**multipart/form-data**: { **"file"**: binary }<br> | **application/json**: [AppImportPayload](#appimportpayload)<br>**multipart/form-data**: { **"file"**: binary }<br> |
+| Yes | **application/json**: [AppImportPayload](#appimportpayload)<br>**multipart/form-data**: { **"app_id"**: string, **"description"**: string, **"file"**: binary, **"icon"**: string, **"icon_background"**: string, **"icon_type"**: string, **"name"**: string }<br> | **application/json**: [AppImportPayload](#appimportpayload)<br>**multipart/form-data**: { **"app_id"**: string, **"description"**: string, **"file"**: binary, **"icon"**: string, **"icon_background"**: string, **"icon_type"**: string, **"name"**: string }<br> |
#### Responses
@@ -2673,7 +2673,7 @@ Export application configuration as DSL
| Name | Located in | Description | Required | Schema |
| ---- | ---------- | ----------- | -------- | ------ |
| app_id | path | Application ID to export | Yes | string (uuid) |
-| format | query | Export format; defaults to ifpkg for Agent Apps and yaml for other Apps | No | string, <br>**Available values:** "ifpkg", "yaml" |
+| format | query | Export format; defaults to ifpkg for all Apps | No | string, <br>**Available values:** "ifpkg", "yaml" |
| include_secret | query | Include secrets in export | No | boolean |
| version_id | query | Published Agent version ID to export; requires a paid plan on Cloud. If omitted, exports the shared draft, falling back to the active snapshot when no draft exists. | No | string (uuid) |
| workflow_id | query | Specific workflow ID to export | No | string |
@@ -15779,7 +15779,7 @@ This class is used to store the schema information of an api based tool.
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
-| format | string, <br>**Available values:** "ifpkg", "yaml" | Export format; defaults to ifpkg for Agent Apps and yaml for other Apps | No |
+| format | string, <br>**Available values:** "ifpkg", "yaml" | Export format; defaults to ifpkg for all Apps | No |
| include_secret | boolean | Include secrets in export | No |
| version_id | string | Published Agent version ID to export; requires a paid plan on Cloud. If omitted, exports the shared draft, falling back to the active snapshot when no draft exists. | No |
| workflow_id | string | Specific workflow ID to export | No |
diff --git a/api/services/agent/roster_package_reader.py b/api/services/agent/roster_package_reader.py
index 2ef00ce3572..7fb01f22971 100644
--- a/api/services/agent/roster_package_reader.py
+++ b/api/services/agent/roster_package_reader.py
@@ -89,24 +89,7 @@ class RosterAgentPackageReader:
) -> tuple[RosterAgentPackageManifest, dict[str, AgentAppDsl], dict[str, RosterAgentPackageMember]]:
try:
with zipfile.ZipFile(archive_file) as archive:
- infos = archive.infolist()
- if not infos:
- raise InvalidRosterAgentPackageError("Roster Agent package is empty")
- if len(infos) > dify_config.AGENT_PACKAGE_MAX_ENTRIES:
- raise InvalidRosterAgentPackageError("Roster Agent package has too many members")
-
- info_by_path: dict[str, zipfile.ZipInfo] = {}
- casefold_paths: set[str] = set()
- total_uncompressed = 0
- for info in infos:
- path = self._validate_member_info(info)
- if path in info_by_path or path.casefold() in casefold_paths:
- raise InvalidRosterAgentPackageError("Roster Agent package contains duplicate member paths")
- info_by_path[path] = info
- casefold_paths.add(path.casefold())
- total_uncompressed += info.file_size
- if total_uncompressed > dify_config.AGENT_PACKAGE_MAX_BYTES:
- raise RosterAgentPackageTooLargeError("Roster Agent package uncompressed size exceeds the limit")
+ info_by_path = self._index_archive(archive)
manifest_data, manifest_size = self._read_yaml_document(archive, info_by_path, "manifest.yaml")
try:
@@ -230,6 +213,27 @@ class RosterAgentPackageReader:
except (OSError, zipfile.BadZipFile, EOFError, RuntimeError, ValueError, zlib.error) as exc:
raise InvalidRosterAgentPackageError("Roster Agent package is not a valid ZIP") from exc
+ def _index_archive(self, archive: zipfile.ZipFile) -> dict[str, zipfile.ZipInfo]:
+ infos = archive.infolist()
+ if not infos:
+ raise InvalidRosterAgentPackageError("Roster Agent package is empty")
+ if len(infos) > dify_config.AGENT_PACKAGE_MAX_ENTRIES:
+ raise InvalidRosterAgentPackageError("Roster Agent package has too many members")
+
+ info_by_path: dict[str, zipfile.ZipInfo] = {}
+ casefold_paths: set[str] = set()
+ total_uncompressed = 0
+ for info in infos:
+ path = self._validate_member_info(info)
+ if path in info_by_path or path.casefold() in casefold_paths:
+ raise InvalidRosterAgentPackageError("Roster Agent package contains duplicate member paths")
+ info_by_path[path] = info
+ casefold_paths.add(path.casefold())
+ total_uncompressed += info.file_size
+ if total_uncompressed > dify_config.AGENT_PACKAGE_MAX_BYTES:
+ raise RosterAgentPackageTooLargeError("Roster Agent package uncompressed size exceeds the limit")
+ return info_by_path
+
@staticmethod
def _copy_bounded(source: BinaryIO, target: BinaryIO) -> None:
size = 0
@@ -297,17 +301,20 @@ class RosterAgentPackageReader:
path: str,
*,
resource: RosterAgentPackageApp | None = None,
+ max_bytes: int | None = None,
) -> tuple[Any, int]:
+ if max_bytes is None:
+ max_bytes = dify_config.AGENT_PACKAGE_MAX_MANIFEST_BYTES
info = infos.get(path)
if info is None:
raise InvalidRosterAgentPackageError(f"Roster Agent package is missing {path}")
- if info.file_size > dify_config.AGENT_PACKAGE_MAX_MANIFEST_BYTES:
+ if info.file_size > max_bytes:
raise RosterAgentPackageTooLargeError(f"Roster Agent package {path} exceeds the size limit")
payload, digest, size = self._read_member(
archive,
info,
collect=True,
- max_bytes=dify_config.AGENT_PACKAGE_MAX_MANIFEST_BYTES,
+ max_bytes=max_bytes,
expected_size=resource.size if resource is not None else info.file_size,
)
if resource is not None and digest != resource.sha256:
diff --git a/api/services/app_dsl_service.py b/api/services/app_dsl_service.py
index 146b74b3d12..c7abd5fd354 100644
--- a/api/services/app_dsl_service.py
+++ b/api/services/app_dsl_service.py
@@ -300,6 +300,8 @@ class AppDslService:
error="Only workflow or advanced chat apps can be overwritten",
)
+ self._validate_workflow_overwrite(app, data)
+
# If major version mismatch, store import info in Redis
if status == ImportStatus.PENDING:
tenant_id = account.current_tenant_id
@@ -434,6 +436,9 @@ class AppDslService:
error="App not found",
)
+ if app is not None:
+ self._validate_workflow_overwrite(app, data)
+
# Create or update app
app = self._create_or_update_app(
app=app,
@@ -540,6 +545,25 @@ class AppDslService:
if not allowed:
raise NoPermissionError("Agent DSL import permission is required to import an Agent App")
+ @staticmethod
+ def _validate_workflow_overwrite(app: App, data: dict[str, Any]) -> None:
+ """Apply editor compatibility checks to both YAML and package imports."""
+ app_mode = data.get("app", {}).get("mode")
+ if app.mode not in {AppMode.WORKFLOW, AppMode.ADVANCED_CHAT} or app_mode not in {
+ AppMode.WORKFLOW,
+ AppMode.ADVANCED_CHAT,
+ }:
+ raise ValueError("Only workflow or advanced chat DSLs can overwrite workflow Apps")
+ # Package uploads cannot run the editor's YAML node checks before import.
+ invalid_types = (
+ {BuiltinNodeTypes.END, "trigger-webhook", "trigger-schedule", "trigger-plugin"}
+ if app.mode == AppMode.ADVANCED_CHAT
+ else {BuiltinNodeTypes.ANSWER}
+ )
+ nodes = data.get("workflow", {}).get("graph", {}).get("nodes", [])
+ if any(node.get("data", {}).get("type") in invalid_types for node in nodes):
+ raise ValueError("Workflow contains node types incompatible with the target App")
+
def _create_or_update_app(
self,
*,
diff --git a/api/services/app_package_service.py b/api/services/app_package_service.py
new file mode 100644
index 00000000000..aae6870ef47
--- /dev/null
+++ b/api/services/app_package_service.py
@@ -0,0 +1,103 @@
+"""Package ordinary App DSLs while retaining the existing Roster Agent format."""
+
+from __future__ import annotations
+
+import hashlib
+import io
+import re
+import tempfile
+import zipfile
+import zlib
+from typing import BinaryIO, Literal, cast
+
+import yaml
+from pydantic import BaseModel, ConfigDict, Field, ValidationError
+
+from services.agent.errors import InvalidRosterAgentPackageError, RosterAgentPackageTooLargeError
+from services.agent.roster_package_entities import RosterAgentPackageApp, RosterAgentPackageExport
+from services.agent.roster_package_reader import RosterAgentPackageReader
+from services.dsl_content import DSL_MAX_SIZE
+
+
+class AppPackageManifest(BaseModel):
+ model_config = ConfigDict(extra="forbid")
+
+ format: Literal["dify.app"]
+ format_version: Literal[1]
+ apps: list[RosterAgentPackageApp] = Field(min_length=1, max_length=1)
+
+
+class AppPackageService(RosterAgentPackageReader):
+ """Reuse the bounded ZIP reader; ordinary Apps retain DSL import semantics."""
+
+ def read_dsl(self, source: BinaryIO) -> str | None:
+ """Return an ordinary DSL, or rewind a Roster package for its importer."""
+ try:
+ with tempfile.SpooledTemporaryFile(max_size=16 * 1024 * 1024, mode="w+b") as spool:
+ self._copy_bounded(source, cast(BinaryIO, spool))
+ spool.seek(0)
+ with zipfile.ZipFile(spool) as archive:
+ infos = self._index_archive(archive)
+ data, _ = self._read_yaml_document(archive, infos, "manifest.yaml")
+ if isinstance(data, dict) and data.get("format") == "dify.roster-agent":
+ return None
+ try:
+ manifest = AppPackageManifest.model_validate(data)
+ except ValidationError as exc:
+ raise InvalidRosterAgentPackageError("App package manifest is invalid") from exc
+ resource = manifest.apps[0]
+ if set(infos) != {"manifest.yaml", resource.path}:
+ raise InvalidRosterAgentPackageError("App package members do not match the manifest")
+ app_data, _ = self._read_yaml_document(
+ archive, infos, resource.path, resource=resource, max_bytes=DSL_MAX_SIZE
+ )
+ if (
+ not isinstance(app_data, dict)
+ or app_data.get("kind") != "app"
+ or not isinstance(app_data.get("app"), dict)
+ or app_data["app"].get("mode")
+ not in {
+ "workflow",
+ "advanced-chat",
+ "chat",
+ "completion",
+ "agent-chat",
+ "channel",
+ "rag-pipeline",
+ }
+ ):
+ raise InvalidRosterAgentPackageError("App package DSL is invalid")
+ # Preserve the original text for version confirmation and legacy DSL handling.
+ return archive.read(resource.path).decode("utf-8")
+ except (InvalidRosterAgentPackageError, RosterAgentPackageTooLargeError):
+ raise
+ except (OSError, zipfile.BadZipFile, EOFError, RuntimeError, ValueError, zlib.error) as exc:
+ raise InvalidRosterAgentPackageError("App package is not a valid ZIP") from exc
+ finally:
+ source.seek(0)
+
+ def export(self, *, dsl: str, name: str) -> RosterAgentPackageExport:
+ payload = dsl.encode("utf-8")
+ if len(payload) > DSL_MAX_SIZE:
+ raise RosterAgentPackageTooLargeError("App package DSL exceeds the size limit")
+ manifest = AppPackageManifest(
+ format="dify.app",
+ format_version=1,
+ apps=[
+ RosterAgentPackageApp(path="app.yaml", size=len(payload), sha256=hashlib.sha256(payload).hexdigest())
+ ],
+ )
+ archive = io.BytesIO()
+ try:
+ with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as package:
+ package.writestr("manifest.yaml", yaml.safe_dump(manifest.model_dump(mode="json")))
+ package.writestr("app.yaml", payload)
+ size = archive.tell()
+ archive.seek(0)
+ # Validate exported containers against the same limits as imports.
+ self.read_dsl(archive)
+ slug = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")[:80] or "app"
+ return RosterAgentPackageExport(archive=archive, filename=f"{slug}.ifpkg", size=size)
+ except Exception:
+ archive.close()
+ raise
diff --git a/api/tests/unit_tests/commands/test_generate_swagger_specs.py b/api/tests/unit_tests/commands/test_generate_swagger_specs.py
index 04c94a468f5..abbaa1be7ec 100644
--- a/api/tests/unit_tests/commands/test_generate_swagger_specs.py
+++ b/api/tests/unit_tests/commands/test_generate_swagger_specs.py
@@ -689,6 +689,7 @@ def test_generate_specs_include_console_contract_shapes_for_schema_migration(tmp
package_import = paths["/apps/imports"]["post"]
assert _request_schema(package_import, "multipart/form-data")["required"] == ["file"]
+ assert _request_schema(package_import, "multipart/form-data")["properties"]["app_id"]["type"] == "string"
assert _request_schema(package_import, "application/json")["$ref"] == "#/components/schemas/AppImportPayload"
assert "mode" in schemas["AppImportPayload"]["required"]
conflict = package_import["responses"]["409"]["content"]["application/json"]["schema"]
@@ -703,7 +704,7 @@ def test_generate_specs_include_console_contract_shapes_for_schema_migration(tmp
export_format = next(param for param in export["parameters"] if param["name"] == "format")
assert set(export_format["schema"]["enum"]) == {"yaml", "ifpkg"}
assert export_format["schema"].get("default") is None
- assert "defaults to ifpkg for Agent Apps and yaml for other Apps" in export_format["description"]
+ assert "defaults to ifpkg for all Apps" in export_format["description"]
api_key_auth_binding_schema = _request_schema(paths["/api-key-auth/data-source/binding"]["post"])
assert api_key_auth_binding_schema["$ref"] == "#/components/schemas/ApiKeyAuthBindingPayload"
diff --git a/api/tests/unit_tests/controllers/console/app/test_roster_package_routes.py b/api/tests/unit_tests/controllers/console/app/test_roster_package_routes.py
index af3e60885e9..69961b0e4c8 100644
--- a/api/tests/unit_tests/controllers/console/app/test_roster_package_routes.py
+++ b/api/tests/unit_tests/controllers/console/app/test_roster_package_routes.py
@@ -1,4 +1,5 @@
import io
+import zipfile
from collections.abc import Callable
from inspect import unwrap
from types import SimpleNamespace
@@ -20,17 +21,30 @@ from models.model import App, AppMode
from services import app_import_source
from services.agent.errors import InvalidRosterAgentPackageError
from services.agent.roster_package_importer import RosterAgentPackageImportResult
+from services.app_package_service import AppPackageService
from services.entities.dsl_entities import DslImportWarning
-def _mock_download(monkeypatch: pytest.MonkeyPatch, content: bytes = b"PK\x00\xff") -> Mock:
+def _mock_download(monkeypatch: pytest.MonkeyPatch, content: bytes | None = None) -> Mock:
fetch = Mock(
- return_value=httpx.Response(200, content=content, request=httpx.Request("GET", "https://example.com/download"))
+ return_value=httpx.Response(
+ 200,
+ content=content if content is not None else _roster_archive().getvalue(),
+ request=httpx.Request("GET", "https://example.com/download"),
+ )
)
monkeypatch.setattr(app_import_source.remote_fetcher, "make_request", fetch)
return fetch
+def _roster_archive() -> io.BytesIO:
+ source = io.BytesIO()
+ with zipfile.ZipFile(source, "w") as archive:
+ archive.writestr("manifest.yaml", "format: dify.roster-agent")
+ source.seek(0)
+ return source
+
+
def _account() -> Account:
account = Account(name="Importer", email="[email protected]")
account._current_tenant = Tenant(name="Workspace")
@@ -148,7 +162,7 @@ def test_url_quota_is_enforced_after_content_detection(
features = Mock()
features.apps = SimpleNamespace(size=1, limit=1)
monkeypatch.setattr("controllers.console.wraps.FeatureService.get_features", lambda *_args, **_kwargs: features)
- _mock_download(monkeypatch, b"app: {}" if is_yaml else b"PK\x00\xff")
+ _mock_download(monkeypatch, b"app: {}" if is_yaml else _roster_archive().getvalue())
importer = Mock()
importer.import_package.return_value = RosterAgentPackageImportResult(
app_id="app-1", agent_id="agent-1", warnings=[]
@@ -205,10 +219,10 @@ def test_existing_import_route_accepts_package_and_preserves_import_response(
)
api = import_module.AppImportApi()
with app.test_request_context(
- "/console/api/apps/imports", method="POST", data={"file": (io.BytesIO(b"package"), "agent.ifpkg")}
+ "/console/api/apps/imports", method="POST", data={"file": (_roster_archive(), "agent.ifpkg")}
):
data, status = unwrap(api.post)(api, account)
- assert import_package.call_args.kwargs["source"].read() == b"package"
+ assert import_package.call_args.kwargs["source"].read() == _roster_archive().getvalue()
assert status == 200
assert data["app_id"] == "app-1"
assert data["app_mode"] == "agent"
@@ -220,7 +234,7 @@ def test_existing_import_route_accepts_package_and_preserves_import_response(
@pytest.mark.parametrize("from_url", [False, True])
@pytest.mark.parametrize("denied", [RBACPermission.AGENT_CREATE, RBACPermission.AGENT_IMPORT_EXPORT_DSL])
-def test_package_import_checks_agent_permissions_before_reading_payload(
+def test_package_import_checks_agent_permissions_before_materializing_resources(
app: Flask,
monkeypatch: pytest.MonkeyPatch,
config_overrides: Callable[..., None],
@@ -244,7 +258,7 @@ def test_package_import_checks_agent_permissions_before_reading_payload(
request_kwargs = (
{"json": {"mode": "yaml-url", "yaml_url": "https://example.com/agent.ifpkg"}}
if from_url
- else {"data": {"file": (io.BytesIO(b"package"), "agent.ifpkg")}}
+ else {"data": {"file": (_roster_archive(), "agent.ifpkg")}}
)
with app.test_request_context("/console/api/apps/imports", method="POST", **request_kwargs):
with pytest.raises(Forbidden):
@@ -265,7 +279,7 @@ def test_package_import_rejects_overwrite(
request_kwargs = (
{"json": {"mode": "yaml-url", "yaml_url": "https://example.com/agent.ifpkg", "app_id": "existing"}}
if from_url
- else {"data": {"app_id": "existing", "file": (io.BytesIO(b"package"), "agent.ifpkg")}}
+ else {"data": {"app_id": "existing", "file": (_roster_archive(), "agent.ifpkg")}}
)
with app.test_request_context("/console/api/apps/imports", method="POST", **request_kwargs):
with pytest.raises(InvalidRosterAgentPackageError, match="overwriting"):
@@ -300,10 +314,75 @@ def test_existing_export_route_returns_ifpkg_for_agent(
close.assert_called_once_with()
-def test_ifpkg_export_rejects_non_agent_apps() -> None:
- model = App(id="app-1", tenant_id="tenant-1", mode=AppMode.WORKFLOW)
- with pytest.raises(BadRequest):
- unwrap(app_module.AppExportApi.get)(app_module.AppExportApi(), app_module.AppExportQuery(format="ifpkg"), model)
+ "mode", [AppMode.WORKFLOW, AppMode.ADVANCED_CHAT, AppMode.CHAT, AppMode.COMPLETION, AppMode.AGENT_CHAT]
+)
[email protected]("query", [{}, {"format": "ifpkg"}])
+def test_default_export_packages_ordinary_app_dsl(
+ app: Flask, monkeypatch: pytest.MonkeyPatch, mode: AppMode, query: dict[str, str]
+) -> None:
+ model = App(id="app-1", tenant_id="tenant-1", mode=mode, name="My App")
+ dsl = f"kind: app\nversion: 0.7.0\napp:\n mode: {mode}\n"
+ export = Mock(return_value=dsl)
+ session = object()
+ monkeypatch.setattr(app_module, "db", SimpleNamespace(session=lambda: session))
+ monkeypatch.setattr(app_module.AppDslService, "export_dsl", export)
+ with app.test_request_context():
+ response = unwrap(app_module.AppExportApi.get)(
+ app_module.AppExportApi(),
+ app_module.AppExportQuery.model_validate({**query, "include_secret": True, "workflow_id": "revision-1"}),
+ model,
+ )
+ response.direct_passthrough = False
+ assert response.mimetype == "application/zip"
+ assert "my-app.ifpkg" in response.headers["Content-Disposition"]
+ assert AppPackageService().read_dsl(io.BytesIO(response.get_data())) == dsl
+ response.close()
+ export.assert_called_once_with(app_model=model, session=session, include_secret=True, workflow_id="revision-1")
+
+
[email protected]("from_url", [False, True])
[email protected]("app_id", [None, "existing"])
[email protected]("status", [200, 202, 400])
+def test_ordinary_package_import_uses_dsl_permissions_and_confirmation(
+ app: Flask,
+ monkeypatch: pytest.MonkeyPatch,
+ config_overrides: Callable[..., None],
+ app_id: str | None,
+ status: int,
+ from_url: bool,
+) -> None:
+ config_overrides(RBAC_ENABLED=False)
+ account = _account()
+ api = import_module.AppImportApi()
+ dsl = "kind: app\nversion: 99.0.0\napp:\n mode: workflow\n"
+ import_dsl = Mock(return_value=({"status": "pending"}, status))
+ monkeypatch.setattr(api, "_import_dsl", import_dsl)
+ agent_import = Mock()
+ monkeypatch.setattr(api, "_import_agent_package", agent_import)
+ with AppPackageService().export(dsl=dsl, name="Workflow") as package:
+ form = {"file": (package.archive, "workflow.ifpkg"), "name": "Renamed"}
+ if app_id:
+ form["app_id"] = app_id
+ if from_url:
+ _mock_download(monkeypatch, package.archive.read())
+ with app.test_request_context(
+ method="POST",
+ json={
+ "mode": "yaml-url",
+ "yaml_url": "https://example.com/workflow.ifpkg",
+ "name": "Renamed",
+ "app_id": app_id,
+ },
+ ):
+ assert unwrap(api.post)(api, account) == ({"status": "pending"}, status)
+ else:
+ with app.test_request_context(method="POST", data=form):
+ assert unwrap(api.post)(api, account) == ({"status": "pending"}, status)
+ import_dsl.assert_called_once_with(
+ import_module.AppImportPayload(mode="yaml-content", yaml_content=dsl, name="Renamed", app_id=app_id), account
+ )
+ agent_import.assert_not_called()
@pytest.mark.parametrize("export_format", [None, "ifpkg", "yaml"])
@@ -375,7 +454,7 @@ def test_version_export_rejects_non_agent_apps() -> None:
@pytest.mark.parametrize(
("mode", "query"),
- [(AppMode.AGENT, {"format": "yaml"}), (AppMode.WORKFLOW, {}), (AppMode.WORKFLOW, {"format": "yaml"})],
+ [(AppMode.AGENT, {"format": "yaml"}), (AppMode.WORKFLOW, {"format": "yaml"})],
)
def test_yaml_export_remains_available(monkeypatch: pytest.MonkeyPatch, mode: AppMode, query: dict[str, str]) -> None:
model = App(id="app-1", tenant_id="tenant-1", mode=mode)
@@ -384,3 +463,48 @@ def test_yaml_export_remains_available(monkeypatch: pytest.MonkeyPatch, mode: Ap
assert unwrap(app_module.AppExportApi.get)(
app_module.AppExportApi(), app_module.AppExportQuery.model_validate(query), model
) == {"data": "app: {}"}
+
+
+def test_ordinary_package_import_checks_app_permission(
+ app: Flask, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
+) -> None:
+ config_overrides(RBAC_ENABLED=True)
+ account = _account()
+ monkeypatch.setattr("controllers.console.wraps.current_account_with_tenant", lambda: (account, "tenant-1"))
+ monkeypatch.setattr("controllers.common.wraps.current_account_with_tenant", lambda: (account, "tenant-1"))
+ check = Mock(return_value=False)
+ monkeypatch.setattr("controllers.common.rbac.checks.RBACService.CheckAccess.check", check)
+ service = Mock()
+ monkeypatch.setattr(import_module, "AppDslService", service)
+ with AppPackageService().export(dsl="kind: app\napp: {mode: workflow}\n", name="Workflow") as package:
+ with app.test_request_context(method="POST", data={"file": (package.archive, "workflow.ifpkg")}):
+ api = import_module.AppImportApi()
+ with pytest.raises(Forbidden):
+ unwrap(api.post)(api, account)
+ assert check.call_args.kwargs["scene"] == RBACPermission.APP_IMPORT_EXPORT_DSL
+ service.assert_not_called()
+
+
+def test_ordinary_package_import_cannot_bypass_app_quota(
+ app: Flask, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
+) -> None:
+ config_overrides(DEPLOYMENT_EDITION="CLOUD", RBAC_ENABLED=False)
+ account = _account()
+ monkeypatch.setattr("controllers.console.wraps.current_account_with_tenant", lambda: (account, "tenant-1"))
+ monkeypatch.setattr(
+ "controllers.console.wraps.FeatureService.get_features",
+ lambda *_args, **_kwargs: SimpleNamespace(
+ members=None,
+ apps=SimpleNamespace(limit=1, size=1),
+ documents_upload_quota=None,
+ annotation_quota_limit=None,
+ ),
+ )
+ service = Mock()
+ monkeypatch.setattr(import_module, "AppDslService", service)
+ with AppPackageService().export(dsl="kind: app\napp: {mode: workflow}\n", name="Workflow") as package:
+ with app.test_request_context(method="POST", data={"file": (package.archive, "workflow.ifpkg")}):
+ api = import_module.AppImportApi()
+ with pytest.raises(Forbidden):
+ unwrap(api.post)(api, account)
+ service.assert_not_called()
diff --git a/api/tests/unit_tests/services/test_app_dsl_service.py b/api/tests/unit_tests/services/test_app_dsl_service.py
index e4c1554f62c..87ec22a7b06 100644
--- a/api/tests/unit_tests/services/test_app_dsl_service.py
+++ b/api/tests/unit_tests/services/test_app_dsl_service.py
@@ -832,3 +832,38 @@ def test_export_dsl_preserves_envelope_and_mode_specific_content(
assert data["model_config"] == {"fixture": "model"}
assert "agent" not in data
assert "workflow" not in data
+
+
+ ("mode", "node_type"),
+ [
+ (AppMode.WORKFLOW, "answer"),
+ (AppMode.ADVANCED_CHAT, "end"),
+ (AppMode.ADVANCED_CHAT, "trigger-webhook"),
+ (AppMode.ADVANCED_CHAT, "trigger-schedule"),
+ (AppMode.ADVANCED_CHAT, "trigger-plugin"),
+ ],
+)
+def test_overwrite_rejects_incompatible_nodes_before_mutation(mode: AppMode, node_type: str) -> None:
+ session = Mock()
+ target = App(id="target", tenant_id="tenant", mode=mode, name="Original")
+ service = AppDslService(session)
+ service._load_app_for_overwrite = Mock(return_value=target)
+ result = service.import_app(
+ account=_account(),
+ import_mode="yaml-content",
+ app_id=target.id,
+ yaml_content=yaml.safe_dump(
+ {
+ "version": CURRENT_APP_DSL_VERSION,
+ "kind": "app",
+ "app": {"mode": mode.value, "name": "Changed"},
+ "workflow": {"graph": {"nodes": [{"data": {"type": node_type}}]}},
+ }
+ ),
+ )
+ assert result.status == ImportStatus.FAILED
+ assert result.error is not None
+ assert "incompatible" in result.error
+ assert target.name == "Original"
+ session.add.assert_not_called()
diff --git a/api/tests/unit_tests/services/test_app_package_service.py b/api/tests/unit_tests/services/test_app_package_service.py
new file mode 100644
index 00000000000..a4299f902c3
--- /dev/null
+++ b/api/tests/unit_tests/services/test_app_package_service.py
@@ -0,0 +1,112 @@
+"""Ordinary App packages preserve DSLs and enforce container integrity."""
+
+import hashlib
+import io
+import zipfile
+from collections.abc import Callable
+
+import pytest
+import yaml
+
+from services.agent.errors import InvalidRosterAgentPackageError, RosterAgentPackageTooLargeError
+from services.app_package_service import AppPackageService
+from services.dsl_content import DSL_MAX_SIZE
+
+
+def _archive(dsl: str, **manifest_overrides: object) -> io.BytesIO:
+ payload = dsl.encode()
+ manifest = {
+ "format": "dify.app",
+ "format_version": 1,
+ "apps": [{"path": "app.yaml", "size": len(payload), "sha256": hashlib.sha256(payload).hexdigest()}],
+ **manifest_overrides,
+ }
+ source = io.BytesIO()
+ with zipfile.ZipFile(source, "w", compression=zipfile.ZIP_DEFLATED) as archive:
+ archive.writestr("manifest.yaml", yaml.safe_dump(manifest))
+ archive.writestr("app.yaml", payload)
+ source.seek(0)
+ return source
+
+
[email protected]("mode", ["workflow", "advanced-chat", "chat", "completion", "agent-chat"])
[email protected]("version", ["0.1.0", "99.0.0"])
+def test_round_trip_retains_exact_dsl_and_defers_version_handling(mode: str, version: str) -> None:
+ dsl = f"kind: app\nversion: {version}\napp:\n mode: {mode}\n# Preserve the DSL verbatim.\n"
+ service = AppPackageService()
+ with service.export(dsl=dsl, name="Example App") as package:
+ assert package.filename == "example-app.ifpkg"
+ assert service.read_dsl(package.archive) == dsl
+ assert package.archive.tell() == 0
+ assert package.archive.closed
+
+
[email protected]("overrides", [{"format": "unknown"}, {"format_version": 2}, {"apps": []}, {"files": []}])
+def test_rejects_unsupported_manifests(overrides: dict[str, object]) -> None:
+ with pytest.raises(InvalidRosterAgentPackageError, match="manifest"):
+ AppPackageService().read_dsl(_archive("kind: app\napp: {mode: workflow}\n", **overrides))
+
+
+ "dsl",
+ [
+ "[]",
+ "kind: app\napp: {mode: agent}",
+ "kind: app\napp: {mode: unknown}",
+ "kind: app\napp: {mode: workflow, mode: chat}",
+ ],
+)
+def test_rejects_invalid_or_agent_dsl_in_ordinary_container(dsl: str) -> None:
+ with pytest.raises(InvalidRosterAgentPackageError):
+ AppPackageService().read_dsl(_archive(dsl))
+
+
+def test_rejects_checksum_mismatch() -> None:
+ dsl = "kind: app\napp: {mode: workflow}\n"
+ source = _archive(dsl, apps=[{"path": "app.yaml", "size": len(dsl), "sha256": "0" * 64}])
+ with pytest.raises(InvalidRosterAgentPackageError, match="integrity"):
+ AppPackageService().read_dsl(source)
+
+
[email protected]("path", ["extra.txt", "../escape", "APP.YAML"])
+def test_rejects_unlisted_unsafe_and_duplicate_members(path: str) -> None:
+ source = _archive("kind: app\napp: {mode: workflow}\n")
+ with zipfile.ZipFile(source, "a") as archive:
+ archive.writestr(path, "unexpected")
+ source.seek(0)
+ with pytest.raises(InvalidRosterAgentPackageError):
+ AppPackageService().read_dsl(source)
+
+
+def test_rejects_oversized_container(config_overrides: Callable[..., None]) -> None:
+ source = _archive("kind: app\napp: {mode: workflow}\n")
+ config_overrides(AGENT_PACKAGE_MAX_BYTES=10)
+ with pytest.raises(RosterAgentPackageTooLargeError):
+ AppPackageService().read_dsl(source)
+
+
+def _dsl_with_size(size: int) -> str:
+ header = "kind: app\napp:\n mode: workflow\n description: "
+ padding = size - len(header.encode("utf-8"))
+ return header + "\U0001f600" * (padding // 4) + "a" * (padding % 4)
+
+
[email protected]("size", [6 * 1024 * 1024, DSL_MAX_SIZE])
+def test_package_round_trip_accepts_large_dsl_within_legacy_limit(size: int) -> None:
+ dsl = _dsl_with_size(size)
+ service = AppPackageService()
+ with service.export(dsl=dsl, name="Large Workflow") as package:
+ assert service.read_dsl(package.archive) == dsl
+
+
+def test_export_rejects_dsl_exceeding_legacy_byte_limit() -> None:
+ dsl = _dsl_with_size(DSL_MAX_SIZE + 1)
+ with pytest.raises(RosterAgentPackageTooLargeError):
+ AppPackageService().export(dsl=dsl, name="Large Workflow")
+
+
+def test_import_rejects_compressed_dsl_exceeding_legacy_byte_limit() -> None:
+ dsl = _dsl_with_size(DSL_MAX_SIZE + 1)
+ source = _archive(dsl)
+ with pytest.raises(RosterAgentPackageTooLargeError):
+ AppPackageService().read_dsl(source)
diff --git a/e2e/features/apps/export-app.feature b/e2e/features/apps/export-app.feature
index d6d040fb00f..6a00e54c4b5 100644
--- a/e2e/features/apps/export-app.feature
+++ b/e2e/features/apps/export-app.feature
@@ -1,9 +1,9 @@
@apps @authenticated @core
-Feature: Export app DSL
- Scenario: Export the DSL file for an existing app
+Feature: Export app package
+ Scenario: Export the app package for an existing app
Given I am signed in as the default E2E admin
And there is an existing E2E completion app available for testing
When I open the apps console
And I open the options menu for the last created E2E app
And I click "Export DSL" in the app options menu
- Then a YAML file named after the app should be downloaded
+ Then an app package named after the app should be downloaded
diff --git a/e2e/features/step-definitions/apps/export-app.steps.ts b/e2e/features/step-definitions/apps/export-app.steps.ts
index 4ebeecb5076..cf794296679 100644
--- a/e2e/features/step-definitions/apps/export-app.steps.ts
+++ b/e2e/features/step-definitions/apps/export-app.steps.ts
@@ -1,8 +1,10 @@
import type { DifyWorld } from '../../support/world'
+import { Buffer } from 'node:buffer'
+import { readFile } from 'node:fs/promises'
import { Then } from '@cucumber/cucumber'
import { expect } from '@playwright/test'
-Then('a YAML file named after the app should be downloaded', async function (this: DifyWorld) {
+Then('an app package named after the app should be downloaded', async function (this: DifyWorld) {
const appName = this.lastCreatedAppName
if (!appName) {
throw new Error(
@@ -15,5 +17,7 @@ Then('a YAML file named after the app should be downloaded', async function (thi
await expect.poll(() => this.capturedDownloads.length, { timeout: 10_000 }).toBeGreaterThan(0)
const download = this.capturedDownloads.at(-1)!
- expect(download.suggestedFilename()).toBe(`${appName}.yml`)
+ expect(download.suggestedFilename()).toBe(`${appName}.ifpkg`)
+ const content = await readFile(await download.path())
+ expect(content.subarray(0, 4)).toEqual(Buffer.from([0x50, 0x4b, 0x03, 0x04]))
})
diff --git a/packages/contracts/generated/api/console/apps/types.gen.ts b/packages/contracts/generated/api/console/apps/types.gen.ts
index b6d9858be73..ba654ca190a 100644
--- a/packages/contracts/generated/api/console/apps/types.gen.ts
+++ b/packages/contracts/generated/api/console/apps/types.gen.ts
@@ -3157,7 +3157,13 @@ export type PostAppsImportsData = {
body:
| AppImportPayload
| {
+ app_id?: string
+ description?: string
file: Blob | File
+ icon?: string
+ icon_background?: string
+ icon_type?: string
+ name?: string
}
path?: never
query?: never
diff --git a/packages/contracts/generated/api/console/apps/zod.gen.ts b/packages/contracts/generated/api/console/apps/zod.gen.ts
index e1df3fa9860..3ccf9a944cb 100644
--- a/packages/contracts/generated/api/console/apps/zod.gen.ts
+++ b/packages/contracts/generated/api/console/apps/zod.gen.ts
@@ -4257,7 +4257,13 @@ export const zPostAppsResponse = zAppDetailWithSite
export const zPostAppsImportsBody = z.union([
zAppImportPayload,
z.object({
+ app_id: z.string().optional(),
+ description: z.string().optional(),
file: z.custom<Blob | File>((value) => value instanceof Blob || value instanceof File),
+ icon: z.string().optional(),
+ icon_background: z.string().optional(),
+ icon_type: z.string().optional(),
+ name: z.string().optional(),
}),
])
diff --git a/web/app/components/app/use-export-app-dsl.ts b/web/app/components/app/use-export-app-dsl.ts
index 13db963ab79..68a93c3f90a 100644
--- a/web/app/components/app/use-export-app-dsl.ts
+++ b/web/app/components/app/use-export-app-dsl.ts
@@ -17,6 +17,7 @@ type ExportAppDslInput = {
includeSecret?: boolean
versionId?: string
format?: NonNullable<GetAppsByAppIdExportData['query']>['format']
+ workflowId?: string
}
type ExportWorkflowAppDslInput = Pick<ExportAppDslInput, 'appId' | 'appName'>
@@ -46,10 +47,11 @@ async function getSecretEnvironmentVariables(appId: string) {
return items.filter((environmentVariable) => environmentVariable.value_type === 'secret')
}
-async function exportAppDslFile({
+export async function exportAppDslFile({
appId,
appName,
includeSecret = false,
+ workflowId,
versionId,
format,
}: ExportAppDslInput) {
@@ -58,6 +60,7 @@ async function exportAppDslFile({
params: { app_id: appId },
query: {
include_secret: includeSecret,
+ ...(workflowId ? { workflow_id: workflowId } : {}),
...(versionId ? { version_id: versionId } : {}),
...(format && { format }),
},
diff --git a/web/app/components/workflow-app/hooks/__tests__/use-DSL.spec.ts b/web/app/components/workflow-app/hooks/__tests__/use-DSL.spec.ts
index 0b522aa9292..9a5d199a1e5 100644
--- a/web/app/components/workflow-app/hooks/__tests__/use-DSL.spec.ts
+++ b/web/app/components/workflow-app/hooks/__tests__/use-DSL.spec.ts
@@ -59,8 +59,10 @@ vi.mock('../use-nodes-sync-draft', () => ({
}),
}))
-vi.mock('@/service/apps', () => ({
- exportAppConfig: (...args: unknown[]) => mockExportAppConfig(...args),
+vi.mock('@/service/console', () => ({
+ consoleClient: {
+ apps: { byAppId: { export: { get: (...args: unknown[]) => mockExportAppConfig(...args) } } },
+ },
}))
vi.mock('@/service/workflow', () => ({
@@ -93,6 +95,23 @@ describe('useDSLByCanEdit', () => {
mockFetchWorkflowDraft.mockResolvedValue({ environment_variables: [] })
})
+ it('downloads the default package bytes and forwards the selected workflow version', async () => {
+ const file = new File([new Uint8Array([0x50, 0x4b, 0xff])], 'workflow.ifpkg')
+ mockExportAppConfig.mockResolvedValue(file)
+ const { result } = renderHook(() => useDSLByCanEdit(true))
+ await act(async () => {
+ await result.current.handleExportDSL(true, 'revision-1')
+ })
+ expect(mockExportAppConfig).toHaveBeenCalledWith(
+ {
+ params: { app_id: 'app-1' },
+ query: { include_secret: true, workflow_id: 'revision-1' },
+ },
+ { context: { silent: true } },
+ )
+ expect(mockDownloadBlob).toHaveBeenCalledWith({ data: file, fileName: 'workflow.ifpkg' })
+ })
+
it('should export workflow dsl and download the yaml blob when no secret env is present', async () => {
const { result } = renderHook(() => useDSLByCanEdit(true))
@@ -102,11 +121,13 @@ describe('useDSLByCanEdit', () => {
expect(mockFetchWorkflowDraft).toHaveBeenCalledWith('/apps/app-1/workflows/draft')
expect(mockDoSyncWorkflowDraft).toHaveBeenCalled()
- expect(mockExportAppConfig).toHaveBeenCalledWith({
- appID: 'app-1',
- include: false,
- workflowID: undefined,
- })
+ expect(mockExportAppConfig).toHaveBeenCalledWith(
+ {
+ params: { app_id: 'app-1' },
+ query: { include_secret: false, workflow_id: undefined },
+ },
+ { context: { silent: true } },
+ )
expect(mockDownloadBlob).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.any(Blob),
@@ -151,11 +172,13 @@ workflow:
await result.current.handleExportDSL(true, 'workflow-1')
})
- expect(mockExportAppConfig).toHaveBeenCalledWith({
- appID: 'app-1',
- include: true,
- workflowID: 'workflow-1',
- })
+ expect(mockExportAppConfig).toHaveBeenCalledWith(
+ {
+ params: { app_id: 'app-1' },
+ query: { include_secret: true, workflow_id: 'workflow-1' },
+ },
+ { context: { silent: true } },
+ )
})
it('should emit DSL_EXPORT_CHECK when secret environment variables exist', async () => {
diff --git a/web/app/components/workflow-app/hooks/use-DSL.ts b/web/app/components/workflow-app/hooks/use-DSL.ts
index e7e8f2bc56c..74130912f17 100644
--- a/web/app/components/workflow-app/hooks/use-DSL.ts
+++ b/web/app/components/workflow-app/hooks/use-DSL.ts
@@ -2,12 +2,11 @@ import type { useNodesSyncDraft } from './use-nodes-sync-draft'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useStore as useAppStore } from '@/app/components/app/store'
+import { exportAppDslFile } from '@/app/components/app/use-export-app-dsl'
import { DSL_EXPORT_CHECK } from '@/app/components/workflow/constants'
import { toast } from '@/app/notifications'
import { useEventEmitterContextContext } from '@/context/event-emitter'
-import { exportAppConfig } from '@/service/apps'
import { fetchWorkflowDraft } from '@/service/workflow'
-import { downloadBlob } from '@/utils/download'
import { useNodesSyncDraftByCanEdit } from './use-nodes-sync-draft'
type DoSyncWorkflowDraft = ReturnType<typeof useNodesSyncDraft>['doSyncWorkflowDraft']
@@ -28,13 +27,12 @@ const useDSLBase = (doSyncWorkflowDraft: DoSyncWorkflowDraft) => {
try {
setExporting(true)
await doSyncWorkflowDraft()
- const { data } = await exportAppConfig({
- appID: appDetail.id,
- include,
- workflowID: workflowId,
+ await exportAppDslFile({
+ appId: appDetail.id,
+ appName: appDetail.name,
+ includeSecret: include,
+ workflowId,
})
- const file = new Blob([data], { type: 'application/yaml' })
- downloadBlob({ data: file, fileName: `${appDetail.name}.yml` })
} catch {
toast.error(t(($) => $.exportFailed, { ns: 'app' }))
} finally {
diff --git a/web/app/components/workflow/__tests__/update-dsl-modal.spec.tsx b/web/app/components/workflow/__tests__/update-dsl-modal.spec.tsx
index afc95679c55..f3e6379dc58 100644
--- a/web/app/components/workflow/__tests__/update-dsl-modal.spec.tsx
+++ b/web/app/components/workflow/__tests__/update-dsl-modal.spec.tsx
@@ -32,9 +32,17 @@ vi.mock('@/app/notifications', () => ({
const mockImportDSL = vi.fn()
const mockImportDSLConfirm = vi.fn()
-vi.mock('@/service/apps', () => ({
- importDSL: (payload: unknown) => mockImportDSL(payload),
- importDSLConfirm: (payload: unknown) => mockImportDSLConfirm(payload),
+vi.mock('@/service/console', () => ({
+ consoleClient: {
+ apps: {
+ imports: {
+ post: ({ body }: { body: unknown }) => mockImportDSL(body),
+ byImportId: {
+ confirm: { post: ({ params }: { params: unknown }) => mockImportDSLConfirm(params) },
+ },
+ },
+ },
+ },
}))
const mockFetchWorkflowDraft = vi.fn()
@@ -115,6 +123,16 @@ describe('UpdateDSLModal', () => {
)
}
+ it('uploads an ifpkg without decoding it as YAML when overwriting a workflow', async () => {
+ mockImportDSL.mockResolvedValue({ status: DSLImportStatus.COMPLETED, app_id: 'app-1' })
+ render(<UpdateDSLModal {...defaultProps} />)
+ const file = new File([new Uint8Array([0x50, 0x4b, 0xff])], 'workflow.IFPKG')
+ fireEvent.change(screen.getByTestId('dsl-file-input'), { target: { files: [file] } })
+ fireEvent.click(screen.getByRole('button', { name: 'workflow.common.overwriteAndImport' }))
+ await waitFor(() => expect(mockImportDSL).toHaveBeenCalledWith({ file, app_id: 'app-1' }))
+ await waitFor(() => expect(defaultProps.onCancel).toHaveBeenCalled())
+ })
+
it('should keep import disabled until a file is selected', () => {
renderModal()
diff --git a/web/app/components/workflow/update-dsl-modal.helpers.ts b/web/app/components/workflow/update-dsl-modal.helpers.ts
index ad257b441b3..67a6563cd5b 100644
--- a/web/app/components/workflow/update-dsl-modal.helpers.ts
+++ b/web/app/components/workflow/update-dsl-modal.helpers.ts
@@ -1,3 +1,4 @@
+import type { Import } from '@dify/contracts/api/console/apps/types.gen'
import type { TFunction } from 'i18next'
import type { CommonNodeType, Node } from './types'
import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
@@ -68,12 +69,12 @@ export const validateDSLContent = (content: string, mode?: AppModeEnum) => {
}
}
-export const isImportCompleted = (status: DSLImportStatus) => {
+export const isImportCompleted = (status: Import['status']) => {
return status === DSLImportStatus.COMPLETED || status === DSLImportStatus.COMPLETED_WITH_WARNINGS
}
export const getImportNotificationPayload = (
- status: DSLImportStatus,
+ status: Import['status'],
t: TFunction,
): ImportNotificationPayload => {
return {
diff --git a/web/app/components/workflow/update-dsl-modal.tsx b/web/app/components/workflow/update-dsl-modal.tsx
index 31901e4d222..c6009125fa5 100644
--- a/web/app/components/workflow/update-dsl-modal.tsx
+++ b/web/app/components/workflow/update-dsl-modal.tsx
@@ -1,7 +1,7 @@
'use client'
+import type { Import } from '@dify/contracts/api/console/apps/types.gen'
import type { MouseEventHandler } from 'react'
-import type { DSLImportWarning } from '@/models/app'
import { Button } from '@langgenius/dify-ui/button'
import { Dialog, DialogContent } from '@langgenius/dify-ui/dialog'
import { RiAlertFill, RiCloseLine, RiFileDownloadLine } from '@remixicon/react'
@@ -14,7 +14,7 @@ import { usePluginDependencies } from '@/app/components/workflow/plugin-dependen
import { toast } from '@/app/notifications'
import { useEventEmitterContextContext } from '@/context/event-emitter'
import { DSLImportMode, DSLImportStatus } from '@/models/app'
-import { importDSL, importDSLConfirm } from '@/service/apps'
+import { consoleClient } from '@/service/console'
import { fetchWorkflowDraft } from '@/service/workflow'
import { collaborationManager } from './collaboration/core/collaboration-manager'
import { WORKFLOW_DATA_UPDATE } from './constants'
@@ -56,8 +56,8 @@ const UpdateDSLModal = ({ onCancel, onBackup, onImport }: UpdateDSLModalProps) =
const handleFile = (file?: File) => {
setDSLFile(file)
- if (file) readFile(file)
- if (!file) setFileContent('')
+ setFileContent('')
+ if (file && !file.name.toLowerCase().endsWith('.ifpkg')) readFile(file)
}
const handleWorkflowUpdate = useCallback(
@@ -84,7 +84,7 @@ const UpdateDSLModal = ({ onCancel, onBackup, onImport }: UpdateDSLModalProps) =
const isCreatingRef = useRef(false)
const handleCompletedImport = useCallback(
- async (status: DSLImportStatus, appId?: string, warnings: DSLImportWarning[] = []) => {
+ async (status: Import['status'], appId?: string | null, warnings: Import['warnings'] = []) => {
if (!appId) {
toast.error(t(($) => $['common.importFailure'], { ns: 'workflow' }))
return
@@ -134,12 +134,16 @@ const UpdateDSLModal = ({ onCancel, onBackup, onImport }: UpdateDSLModalProps) =
return
}
try {
- if (appDetail && fileContent && validateDSLContent(fileContent, appDetail.mode)) {
+ const isPackage = currentFile.name.toLowerCase().endsWith('.ifpkg')
+ if (
+ appDetail &&
+ (isPackage || (fileContent && validateDSLContent(fileContent, appDetail.mode)))
+ ) {
setLoading(true)
- const response = await importDSL({
- mode: DSLImportMode.YAML_CONTENT,
- yaml_content: fileContent,
- app_id: appDetail.id,
+ const response = await consoleClient.apps.imports.post({
+ body: isPackage
+ ? { file: currentFile, app_id: appDetail.id }
+ : { mode: DSLImportMode.YAML_CONTENT, yaml_content: fileContent, app_id: appDetail.id },
})
const { id, status, app_id, imported_dsl_version, current_dsl_version, warnings } = response
@@ -164,8 +168,8 @@ const UpdateDSLModal = ({ onCancel, onBackup, onImport }: UpdateDSLModalProps) =
const onUpdateDSLConfirm: MouseEventHandler = async () => {
try {
if (!importId) return
- const response = await importDSLConfirm({
- import_id: importId,
+ const response = await consoleClient.apps.imports.byImportId.confirm.post({
+ params: { import_id: importId },
})
const { status, app_id, warnings } = response
@@ -228,7 +232,12 @@ const UpdateDSLModal = ({ onCancel, onBackup, onImport }: UpdateDSLModalProps) =
{t(($) => $['common.chooseDSL'], { ns: 'workflow' })}
</div>
<div className="flex w-full flex-col items-start justify-center gap-4 self-stretch py-4">
- <Uploader file={currentFile} updateFile={handleFile} className="mt-0! w-full" />
+ <Uploader
+ importType="app"
+ file={currentFile}
+ updateFile={handleFile}
+ className="mt-0! w-full"
+ />
</div>
</div>
<div className="flex items-center justify-end gap-2 self-stretch pt-5">
diff --git a/web/models/app.ts b/web/models/app.ts
index 1ad5360d10a..a8e0f8d2151 100644
--- a/web/models/app.ts
+++ b/web/models/app.ts
@@ -11,8 +11,7 @@ import type {
TracingProvider,
WeaveConfig,
} from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/tracing/type'
-import type { Dependency } from '@/app/components/plugins/types'
-import type { App, AppModeEnum } from '@/types/app'
+import type { App } from '@/types/app'
export const DSLImportMode = {
YAML_CONTENT: 'yaml-content',
@@ -28,28 +27,8 @@ export const DSLImportStatus = {
} as const
export type DSLImportStatus = (typeof DSLImportStatus)[keyof typeof DSLImportStatus]
-export type DSLImportWarning = {
- code: string
- path: string
- message: string
- details: Record<string, unknown>
-}
-
export type AppDetailResponse = App
-export type DSLImportResponse = {
- id: string
- status: DSLImportStatus
- app_mode: AppModeEnum
- app_id?: string
- current_dsl_version?: string
- imported_dsl_version?: string
- error: string
- leaked_dependencies: Dependency[]
- permission_keys: string[]
- warnings?: DSLImportWarning[]
-}
-
export type UpdateAppModelConfigResponse = { result: string }
export type WorkflowOnlineUser = {
diff --git a/web/service/apps.ts b/web/service/apps.ts
index 23098ccc569..b86323e71ba 100644
--- a/web/service/apps.ts
+++ b/web/service/apps.ts
@@ -1,8 +1,6 @@
import type { TracingProvider } from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/tracing/type'
import type {
AppDetailResponse,
- DSLImportMode,
- DSLImportResponse,
TracingConfig,
TracingStatus,
UpdateAppModelConfigResponse,
@@ -85,66 +83,6 @@ export const updateAppInfo = ({
return put<AppDetailResponse>(`apps/${appID}`, { body })
}
-export const exportAppConfig = ({
- appID,
- include = false,
- workflowID,
-}: {
- appID: string
- include?: boolean
- workflowID?: string
-}): Promise<{ data: string }> => {
- const params = new URLSearchParams({
- include_secret: include.toString(),
- })
- if (workflowID) params.append('workflow_id', workflowID)
- return get<{ data: string }>(`apps/${appID}/export?${params.toString()}`)
-}
-
-export const importDSL = ({
- mode,
- yaml_content,
- yaml_url,
- app_id,
- name,
- description,
- icon_type,
- icon,
- icon_background,
-}: {
- mode: DSLImportMode
- yaml_content?: string
- yaml_url?: string
- app_id?: string
- name?: string
- description?: string
- icon_type?: AppIconType
- icon?: string
- icon_background?: string
-}): Promise<DSLImportResponse> => {
- return post<DSLImportResponse>('apps/imports', {
- body: {
- mode,
- yaml_content,
- yaml_url,
- app_id,
- name,
- description,
- icon,
- icon_type,
- icon_background,
- },
- })
-}
-
-export const importDSLConfirm = ({
- import_id,
-}: {
- import_id: string
-}): Promise<DSLImportResponse> => {
- return post<DSLImportResponse>(`apps/imports/${import_id}/confirm`, { body: {} })
-}
-
export const deleteApp = (appID: string): Promise<CommonResponse> => {
return del<CommonResponse>(`apps/${appID}`)
}
diff --git a/web/service/console/client.spec.ts b/web/service/console/client.spec.ts
index e954aaf1751..59c6bd75114 100644
--- a/web/service/console/client.spec.ts
+++ b/web/service/console/client.spec.ts
@@ -199,33 +199,37 @@ describe('consoleQuery transport context', () => {
vi.restoreAllMocks()
})
- it('uploads ifpkg imports as multipart files without decoding their bytes as YAML', async () => {
- const bytes = new Uint8Array([0x50, 0x4b, 0x03, 0x04, 0x00, 0xff])
- const file = new File([bytes], 'agent.ifpkg', { type: 'application/zip' })
- const request = vi.fn().mockResolvedValue(
- new Response(JSON.stringify({ id: 'import-1', status: 'completed' }), {
- status: 200,
- headers: { 'content-type': 'application/json' },
- }),
- )
- const consoleQuery = await loadConsoleQueryWithRequest(request)
- const mutation = new MutationObserver(
- new QueryClient(),
- consoleQuery.apps.imports.post.mutationOptions(),
- )
- await mutation.mutate({ body: { file } })
-
- const outgoing = request.mock.calls[0]?.[2]?.request as Request
- expect(outgoing.url).toContain('/apps/imports')
- expect(outgoing.headers.get('content-type')).toContain('multipart/form-data; boundary=')
- const form = await outgoing.formData()
- expect(Array.from(form.keys())).toEqual(['file'])
- const uploaded = form.get('file')
- expect(uploaded).toBeInstanceOf(File)
- if (!(uploaded instanceof File)) throw new TypeError('Expected an uploaded archive')
- expect(uploaded.name).toBe('agent.ifpkg')
- expect(new Uint8Array(await uploaded.arrayBuffer())).toEqual(bytes)
- })
+ it.each([undefined, 'existing-workflow'])(
+ 'uploads ifpkg bytes with overwrite target %s',
+ async (appId) => {
+ const bytes = new Uint8Array([0x50, 0x4b, 0x03, 0x04, 0x00, 0xff])
+ const file = new File([bytes], 'agent.ifpkg', { type: 'application/zip' })
+ const request = vi.fn().mockResolvedValue(
+ new Response(JSON.stringify({ id: 'import-1', status: 'completed' }), {
+ status: 200,
+ headers: { 'content-type': 'application/json' },
+ }),
+ )
+ const consoleQuery = await loadConsoleQueryWithRequest(request)
+ const mutation = new MutationObserver(
+ new QueryClient(),
+ consoleQuery.apps.imports.post.mutationOptions(),
+ )
+ await mutation.mutate({ body: { file, ...(appId ? { app_id: appId } : {}) } })
+
+ const outgoing = request.mock.calls[0]?.[2]?.request as Request
+ expect(outgoing.url).toContain('/apps/imports')
+ expect(outgoing.headers.get('content-type')).toContain('multipart/form-data; boundary=')
+ const form = await outgoing.formData()
+ expect(Array.from(form.keys()).sort()).toEqual(appId ? ['app_id', 'file'] : ['file'])
+ expect(form.get('app_id')).toBe(appId ?? null)
+ const uploaded = form.get('file')
+ expect(uploaded).toBeInstanceOf(File)
+ if (!(uploaded instanceof File)) throw new TypeError('Expected an uploaded archive')
+ expect(uploaded.name).toBe('agent.ifpkg')
+ expect(new Uint8Array(await uploaded.arrayBuffer())).toEqual(bytes)
+ },
+ )
it('preserves archive bytes and the download filename for App exports', async () => {
const bytes = new Uint8Array([0x50, 0x4b, 0x03, 0x04, 0x00, 0xff])