Skip to content
TypeScript

fix(server): Word text boxes are no longer extracted twice for agents (#29827)

Alex Southwelleb303366182d
diff --git a/apps/server/src/provider/documentTextWorker.ts b/apps/server/src/provider/documentTextWorker.ts
index 9138d951d20..4abadfbdb17 100644
--- a/apps/server/src/provider/documentTextWorker.ts
+++ b/apps/server/src/provider/documentTextWorker.ts
@@ -36,6 +36,8 @@ interface XmlNode {
attrs: Record<string, string>;
children: Array<XmlNode | string>;
spaceCount?: number;
+ hasText?: boolean;
+ hasTextChoice?: boolean;
}
function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
@@ -61,10 +63,22 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
stack.push(node);
});
parser.on("closetag", () => {
- stack.pop();
+ const node = stack.pop()!;
+ const parent = stack.at(-1)!;
+ // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
+ // Keep one when no Choice carries text, such as a chart with a VML caption.
+ if (node.name === "Fallback" && parent.hasTextChoice) {
+ parent.children.pop();
+ return;
+ }
+ if (!node.hasText) return;
+ parent.hasText = true;
+ if (node.name === "Choice") parent.hasTextChoice = true;
});
const text = (value: string) => {
- stack.at(-1)!.children.push(value);
+ const node = stack.at(-1)!;
+ node.children.push(value);
+ if (value.trim() !== "") node.hasText = true;
};
parser.on("text", text);
parser.on("cdata", text);
diff --git a/apps/server/src/provider/userInputAttachments.test.ts b/apps/server/src/provider/userInputAttachments.test.ts
index 887a2d5febb..90793ce3f82 100644
--- a/apps/server/src/provider/userInputAttachments.test.ts
+++ b/apps/server/src/provider/userInputAttachments.test.ts
@@ -554,6 +554,25 @@ describe("question answer paths", () => {
expected: ["Start", "[Extracted text truncated at document extraction limits.]"],
absent: ["Unneeded"],
},
+ {
+ ext: "docx",
+ bounded: true,
+ parts: {
+ "word/document.xml":
+ '<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body><w:p><w:r><w:t>Body</w:t></w:r></w:p><w:p><w:r><mc:AlternateContent><mc:Choice Requires="wps"><w:drawing><wps:wsp><wps:txbx><w:txbxContent><w:p><w:r><w:t>Callout box</w:t></w:r></w:p></w:txbxContent></wps:txbx></wps:wsp></w:drawing></mc:Choice><mc:Fallback><w:pict><v:shape><v:textbox><w:txbxContent><w:p><w:r><w:t>Callout box</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r></w:p></w:body></w:document>',
+ },
+ expected: ["Body\nCallout box"],
+ occurrences: { text: "Callout box", count: 1 },
+ },
+ {
+ ext: "docx",
+ bounded: true,
+ parts: {
+ "word/document.xml":
+ '<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body><w:p><w:r><mc:AlternateContent><mc:Choice Requires="cx"><w:drawing><cx:chart/></w:drawing></mc:Choice><mc:Fallback><w:pict><v:shape><v:textbox><w:txbxContent><w:p><w:r><w:t>Chart caption</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r></w:p></w:body></w:document>',
+ },
+ expected: ["Chart caption"],
+ },
{
ext: "docx",
bounded: true,