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: preserve DOCX paragraph text in skill attachments (#43718)

LimbC-C515bf0719ae7
diff --git a/api/services/skill_management_service.py b/api/services/skill_management_service.py
index a2e3ead7c03..10a83790fd9 100644
--- a/api/services/skill_management_service.py
+++ b/api/services/skill_management_service.py
@@ -4580,6 +4580,7 @@ class SkillManagementService:
@staticmethod
def _extract_docx_text(payload: bytes, *, max_chars: int) -> str:
parts: list[str] = []
+ inline_text = {"tab": "\t", "br": "\n", "cr": "\n", "noBreakHyphen": "\u2011", "softHyphen": "\u00ad"}
try:
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
for name in sorted(archive.namelist()):
@@ -4587,7 +4588,27 @@ class SkillManagementService:
name == "word/document.xml" or name.startswith("word/header") or name.startswith("word/footer")
):
continue
- parts.extend(SkillManagementService._xml_text_content(archive.read(name), text_tags={"t"}))
+ paragraph_text: list[str] = []
+ ancestors: list[str] = []
+ for event, node in ET.iterparse(io.BytesIO(archive.read(name)), events=("start", "end")):
+ tag = node.tag.rsplit("}", 1)[-1]
+ if event == "start":
+ ancestors.append(tag)
+ # Runs share a paragraph; nested text-box paragraphs must not be collected twice.
+ if tag == "p":
+ if paragraph_text:
+ parts.append("".join(paragraph_text))
+ paragraph_text = []
+ elif event == "end" and tag == "t":
+ paragraph_text.append(node.text or "")
+ elif event == "end" and tag in inline_text and len(ancestors) > 1:
+ if ancestors[-2] == "r" or (tag == "br" and ancestors[-2] == "p"):
+ paragraph_text.append(inline_text[tag])
+ if event == "end":
+ ancestors.pop()
+ node.clear()
+ if paragraph_text:
+ parts.append("".join(paragraph_text))
if sum(len(part) for part in parts) >= max_chars:
break
except (ET.ParseError, OSError, zipfile.BadZipFile):
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 abc0a57ffe6..7aa383a7a53 100644
--- a/api/tests/unit_tests/services/test_skill_management_service.py
+++ b/api/tests/unit_tests/services/test_skill_management_service.py
@@ -3172,7 +3172,47 @@ def _zip_payload(files: dict[str, str]) -> bytes:
return buffer.getvalue()
-def test_build_assistant_attachment_context_extracts_docx_text() -> None:
+ ("body", "expected_text"),
+ [
+ pytest.param(
+ "<w:p><w:r><w:t>Escalate urgent tickets</w:t></w:r></w:p>",
+ "Escalate urgent tickets",
+ id="plain",
+ ),
+ pytest.param(
+ '<w:p><w:r><w:t xml:space="preserve">Keep </w:t></w:r>'
+ "<w:r><w:rPr><w:b/></w:rPr><w:t>customer_</w:t></w:r>"
+ "<w:r><w:t>id unchanged.</w:t></w:r></w:p>"
+ "<w:p><w:r><w:t>Next paragraph</w:t></w:r></w:p>",
+ "Keep customer_id unchanged.\nNext paragraph",
+ id="formatted-runs",
+ ),
+ pytest.param(
+ '<w:p><w:pPr><w:tabs><w:tab w:val="left" w:pos="720"/></w:tabs></w:pPr>'
+ "<w:r><w:t>left</w:t><w:tab/><w:t>middle</w:t><w:br/>"
+ "<w:t>right</w:t><w:cr/><w:t>soft</w:t><w:softHyphen/>"
+ "<w:t>hyphen</w:t><w:noBreakHyphen/><w:t>end</w:t></w:r></w:p>",
+ "left\tmiddle\nright\nsoft\u00adhyphen\u2011end",
+ id="inline-controls",
+ ),
+ pytest.param(
+ '<w:p><w:r><w:t>Before</w:t><w:pict><v:shape id="box"><v:textbox><w:txbxContent>'
+ '<w:p><w:r><w:t>Inside</w:t></w:r><w:r><w:t xml:space="preserve"> box</w:t></w:r></w:p>'
+ "</w:txbxContent></v:textbox></v:shape></w:pict><w:t>After</w:t></w:r></w:p>",
+ "Before\nInside box\nAfter",
+ id="nested-paragraphs",
+ ),
+ pytest.param(
+ "<w:tbl><w:tr><w:tc><w:p><w:r><w:t>First</w:t></w:r>"
+ '<w:r><w:t xml:space="preserve"> cell</w:t></w:r></w:p></w:tc>'
+ "<w:tc><w:p><w:r><w:t>Second cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl>",
+ "First cell\nSecond cell",
+ id="table-paragraphs",
+ ),
+ ],
+)
+def test_build_assistant_attachment_context_extracts_docx_text(body: str, expected_text: str) -> None:
attachment = SkillAssistAttachmentPayload(
tool_file_id="docx-file-1",
name="guide.docx",
@@ -3182,8 +3222,9 @@ def test_build_assistant_attachment_context_extracts_docx_text() -> None:
payload = _zip_payload(
{
"word/document.xml": (
- '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
- "<w:body><w:p><w:r><w:t>Escalate urgent tickets</w:t></w:r></w:p></w:body>"
+ '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" '
+ 'xmlns:v="urn:schemas-microsoft-com:vml">'
+ f"<w:body>{body}</w:body>"
"</w:document>"
)
}
@@ -3199,7 +3240,7 @@ def test_build_assistant_attachment_context_extracts_docx_text() -> None:
)
assert "--- guide.docx" in context
- assert "Escalate urgent tickets" in context
+ assert context.split("\n", 1)[1] == expected_text
assert "Binary attachment available" not in context