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(skills): preserve PPTX presentation slide order (#43893)

LimbC-C212b22c92bda
diff --git a/api/services/skill_management_service.py b/api/services/skill_management_service.py
index d2b7b36c543..ee4b325a1f9 100644
--- a/api/services/skill_management_service.py
+++ b/api/services/skill_management_service.py
@@ -4599,9 +4599,37 @@ class SkillManagementService:
parts: list[str] = []
try:
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
+ archive_names = set(archive.namelist())
slide_names = sorted(
name for name in archive.namelist() if name.startswith("ppt/slides/slide") and name.endswith(".xml")
)
+ if {"ppt/presentation.xml", "ppt/_rels/presentation.xml.rels"} <= archive_names:
+ # Part filenames need not match the presentation's displayed slide order.
+ relationships = ET.fromstring(archive.read("ppt/_rels/presentation.xml.rels"))
+ slide_targets: dict[str, str] = {}
+ for relationship in relationships:
+ if (
+ not relationship.attrib.get("Type", "").endswith("/slide")
+ or relationship.attrib.get("TargetMode") == "External"
+ ):
+ continue
+ relationship_id = relationship.attrib.get("Id")
+ target = relationship.attrib.get("Target")
+ if relationship_id and target:
+ slide_targets[relationship_id] = posixpath.normpath(posixpath.join("ppt", target)).lstrip(
+ "/"
+ )
+ presentation = ET.fromstring(archive.read("ppt/presentation.xml"))
+ slide_names = []
+ for slide in presentation.iter():
+ if slide.tag.rsplit("}", 1)[-1] != "sldId":
+ continue
+ relationship_id = next(
+ (value for key, value in slide.attrib.items() if key.endswith("}id")), ""
+ )
+ target = slide_targets.get(relationship_id)
+ if target is not None and target in archive_names:
+ slide_names.append(target)
for name in slide_names:
parts.extend(SkillManagementService._xml_text_content(archive.read(name), text_tags={"t"}))
if sum(len(part) for part in parts) >= max_chars:
diff --git a/api/tests/unit_tests/services/test_skill_management_service.py b/api/tests/unit_tests/services/test_skill_management_service.py
index 975d23e6a35..fd692806d27 100644
--- a/api/tests/unit_tests/services/test_skill_management_service.py
+++ b/api/tests/unit_tests/services/test_skill_management_service.py
@@ -3264,6 +3264,108 @@ def test_build_assistant_attachment_context_extracts_pptx_text() -> None:
assert "Binary attachment available" not in context
+ ("slide_order", "max_chars", "strict"),
+ [
+ pytest.param([1, 2, 10], 1000, False, id="double-digit-slides"),
+ pytest.param([3, 1, 2], 1000, False, id="reordered-slides"),
+ pytest.param([2, 1, 3], 8, True, id="bounded-strict-presentation"),
+ pytest.param([1, 2], 1000, False, id="ordinary-order"),
+ ],
+)
+def test_build_assistant_attachment_context_preserves_pptx_slide_order(
+ slide_order: list[int], max_chars: int, strict: bool
+) -> None:
+ presentation_ns = (
+ "http://purl.oclc.org/ooxml/presentationml/main"
+ if strict
+ else "http://schemas.openxmlformats.org/presentationml/2006/main"
+ )
+ relationship_ns = (
+ "http://purl.oclc.org/ooxml/officeDocument/relationships"
+ if strict
+ else "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ )
+ drawing_ns = (
+ "http://purl.oclc.org/ooxml/drawingml/main"
+ if strict
+ else "http://schemas.openxmlformats.org/drawingml/2006/main"
+ )
+ slide_ids = "".join(f'<p:sldId id="{256 + number}" r:id="rId{500 + number}"/>' for number in slide_order)
+ relationships = "".join(
+ f'<Relationship Id="rId{500 + number}" Type="{relationship_ns}/slide" '
+ f'Target="{f"/ppt/slides/slide{number}.xml" if number % 2 else f"slides/./slide{number}.xml"}"/>'
+ for number in reversed(slide_order)
+ )
+ files = {
+ "ppt/presentation.xml": (
+ f'<p:presentation xmlns:p="{presentation_ns}" xmlns:r="{relationship_ns}">'
+ f"<p:sldIdLst>{slide_ids}</p:sldIdLst></p:presentation>"
+ ),
+ "ppt/_rels/presentation.xml.rels": (
+ '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
+ f"{relationships}</Relationships>"
+ ),
+ }
+ for number in reversed(slide_order):
+ files[f"ppt/slides/slide{number}.xml"] = (
+ f'<p:sld xmlns:p="{presentation_ns}" '
+ f'xmlns:a="{drawing_ns}">'
+ f"<p:cSld><a:p><a:r><a:t>Step {number}</a:t></a:r></a:p></p:cSld></p:sld>"
+ )
+ payload = _zip_payload(files)
+ attachment = SkillAssistAttachmentPayload(
+ tool_file_id="ordered-pptx",
+ name="steps.pptx",
+ mime_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
+ size=len(payload),
+ )
+ header = f"--- {attachment.name} ({attachment.mime_type}, {attachment.size} bytes) ---\n"
+ expected = "\n".join(f"Step {number}" for number in slide_order)
+ if len(expected) > max_chars:
+ expected = f"{expected[:max_chars]}\n[TRUNCATED]"
+ with (
+ patch(
+ "services.skill_management_service.SkillManagementService._load_assistant_tool_file_bytes",
+ return_value=payload,
+ ),
+ patch("services.skill_management_service._MAX_ASSISTANT_ATTACHMENT_CHARS", len(header) + max_chars),
+ ):
+ context = SkillManagementService._build_assistant_attachment_context(tenant_id=TENANT, attachments=[attachment])
+ assert context == header + expected
+
+
+def test_pptx_extraction_ignores_unreferenced_external_and_missing_slide_parts() -> None:
+ relationship_ns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ slide_ids = "".join(
+ f'<p:sldId id="{256 + index}" r:id="{rid}"/>'
+ for index, rid in enumerate(["keep", "external", "missing", "master"])
+ )
+ relationships = (
+ f'<Relationship Id="keep" Type="{relationship_ns}/slide" Target="slides/slide2.xml"/>'
+ f'<Relationship Id="external" Type="{relationship_ns}/slide" Target="slides/slide1.xml" TargetMode="External"/>'
+ f'<Relationship Id="missing" Type="{relationship_ns}/slide" Target="slides/slide404.xml"/>'
+ f'<Relationship Id="master" Type="{relationship_ns}/slideMaster" Target="slides/slide3.xml"/>'
+ )
+ payload = _zip_payload(
+ {
+ "ppt/presentation.xml": (
+ '<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" '
+ f'xmlns:r="{relationship_ns}"><p:sldIdLst>{slide_ids}</p:sldIdLst></p:presentation>'
+ ),
+ "ppt/_rels/presentation.xml.rels": (
+ '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
+ f"{relationships}</Relationships>"
+ ),
+ "ppt/slides/slide1.xml": "<slide><t>external</t></slide>",
+ "ppt/slides/slide2.xml": "<slide><t>keep</t></slide>",
+ "ppt/slides/slide3.xml": "<slide><t>master</t></slide>",
+ "ppt/slides/slide99.xml": "<slide><t>unreferenced</t></slide>",
+ }
+ )
+ assert SkillManagementService._extract_pptx_text(payload, max_chars=1000) == "keep"
+
+
def test_build_assistant_attachment_context_extracts_rtf_text() -> None:
attachment = SkillAssistAttachmentPayload(
tool_file_id="rtf-file-1",