Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep line breaks and ticked boxes in attached Word files (#12788)

Nilay502779a8138e
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.ts
index ed41688c54d..5b769e45c0b 100644
--- a/studio/frontend/src/features/chat/attachment-content.ts
+++ b/studio/frontend/src/features/chat/attachment-content.ts
@@ -916,6 +916,61 @@ export function linearizeDocxMath(archive: Uint8Array): Uint8Array {
return zipSync({ ...unzipSync(archive), ...rewritten }, { level: 0 });
}
+const W14_NAMESPACE = "http://schemas.microsoft.com/office/word/2010/wordml";
+const DOCX_BREAK_OR_CHECKBOX_RE = /<(?:[\w.-]+:)?(?:br|cr|checkbox|checkBox)[\s/>]/;
+
+export function writeDocxBreaksAndCheckboxes(archive: Uint8Array): Uint8Array {
+ const rewritten: Record<string, Uint8Array> = {};
+ const parts = unzipSync(archive, { filter: (entry) => entry.name.endsWith(".xml") });
+ for (const [name, bytes] of Object.entries(parts)) {
+ const xml = strFromU8(bytes);
+ if (!DOCX_BREAK_OR_CHECKBOX_RE.test(xml)) continue;
+ const doc = new DOMParser().parseFromString(xml, "application/xml");
+ const root = doc.documentElement;
+ const w = root?.namespaceURI ?? "";
+ if (!WORDPROCESSINGML_NAMESPACES.has(w) || doc.getElementsByTagName("parsererror").length) continue;
+ const tag = (local: string) => (root.prefix ? `${root.prefix}:${local}` : local);
+ const text = (value: string) => {
+ const t = doc.createElementNS(w, tag("t"));
+ t.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
+ t.appendChild(doc.createTextNode(value));
+ return t;
+ };
+ const isOn = (flag: Element | undefined, ns: string) =>
+ flag !== undefined && !["0", "false", "off"].includes(flag.getAttributeNS(ns, "val") ?? "");
+ // [anchor, checked, inRun]: a legacy field's glyph goes inside its run, before the fldChar.
+ const boxes: [Element, boolean, boolean][] = [];
+ for (const box of Array.from(doc.getElementsByTagNameNS(W14_NAMESPACE, "checkbox"))) {
+ const sdt = box.parentNode?.parentNode as Element | null;
+ if ((box.parentNode as Element).localName === "sdtPr" && sdt?.localName === "sdt") {
+ boxes.push([sdt, isOn(childElements(box, W14_NAMESPACE, "checked")[0], W14_NAMESPACE), false]);
+ }
+ }
+ for (const box of Array.from(doc.getElementsByTagNameNS(w, "checkBox"))) {
+ const fldChar = box.parentNode?.parentNode as Element | null;
+ if (fldChar?.localName === "fldChar" && fldChar.parentNode) {
+ const flag = childElements(box, w, "checked")[0] ?? childElements(box, w, "default")[0];
+ boxes.push([fldChar, isOn(flag, w), true]);
+ }
+ }
+ for (const [anchor, checked, inRun] of boxes) {
+ const glyph = text(checked ? "☒" : "☐");
+ const node = inRun ? glyph : doc.createElementNS(w, tag("r"));
+ if (!inRun) node.appendChild(glyph);
+ anchor.parentNode?.insertBefore(node, anchor);
+ }
+ // Page and column breaks too: mammoth's raw text drops every break, gluing the words either side.
+ const breaks = [
+ ...Array.from(doc.getElementsByTagNameNS(w, "br")),
+ ...Array.from(doc.getElementsByTagNameNS(w, "cr")),
+ ];
+ for (const br of breaks) br.parentNode?.replaceChild(text("\n"), br);
+ if (boxes.length || breaks.length) rewritten[name] = strToU8(new XMLSerializer().serializeToString(doc));
+ }
+ if (!Object.keys(rewritten).length) return archive;
+ return zipSync({ ...unzipSync(archive), ...rewritten }, { level: 0 });
+}
+
const DOCX_NOTE_REFERENCE_RE =
/<(?:([\w.-]+):)?(footnote|endnote)Reference\b([^>]*?)(\/?)>(\s*<\/(?:[\w.-]+:)?\2Reference\s*>)?/g;
const DOCX_NOTE_ID_RE = /(?:^|\s)(?:[\w.-]+:)?id\s*=\s*["']([^"']*)["']/;
@@ -1443,7 +1498,7 @@ export async function extractDocxAttachmentText(file: File): Promise<string> {
);
const marked = markDocxNotes(linearizeDocxMath(repacked));
const { value } = await mammoth.extractRawText({
- arrayBuffer: toArrayBuffer(marked.archive),
+ arrayBuffer: toArrayBuffer(writeDocxBreaksAndCheckboxes(marked.archive)),
});
return marked.label(value);
}
diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.ts
index 998a1f03b39..081c326bdab 100644
--- a/studio/frontend/tests/attachment-preview-text.test.ts
+++ b/studio/frontend/tests/attachment-preview-text.test.ts
@@ -36,6 +36,7 @@ const {
repackDocxAttachmentArchive,
repackDocxPreviewArchive,
truncateAttachmentPreviewText,
+ writeDocxBreaksAndCheckboxes,
} = await import("../src/features/chat/attachment-content.ts");
const { definePDFJSModule } = await import("unpdf");
const { readRtfAttachmentContent } =
@@ -1429,6 +1430,55 @@ test("markDocxNotes skips move sources, deletions and text box fallbacks", () =>
}
});
+test("a Word file keeps its line breaks and which boxes are ticked", async () => {
+ const ns =
+ 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ' +
+ 'xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"';
+ const run = (text: string) => `<w:r><w:t xml:space="preserve">${text}</w:t></w:r>`;
+ const box = (checked: string, glyph: string, label: string) =>
+ `<w:p><w:sdt><w:sdtPr><w14:checkbox><w14:checked w14:val="${checked}"/>` +
+ '<w14:checkedState w14:val="2612" w14:font="MS Gothic"/><w14:uncheckedState w14:val="2610" w14:font="MS Gothic"/>' +
+ `</w14:checkbox></w:sdtPr><w:sdtContent>${run(glyph)}</w:sdtContent></w:sdt>${run(label)}</w:p>`;
+ const field = (state: string, label: string) =>
+ `<w:p><w:r><w:fldChar w:fldCharType="begin"><w:ffData><w:checkBox><w:sizeAuto/>${state}</w:checkBox></w:ffData></w:fldChar></w:r>` +
+ '<w:r><w:instrText xml:space="preserve"> FORMCHECKBOX </w:instrText></w:r>' +
+ `<w:r><w:fldChar w:fldCharType="end"/></w:r>${run(label)}</w:p>`;
+ const archive = docxBytes(
+ `<w:document ${ns}><w:body>` +
+ '<w:p><w:r><w:t>Jane Doe</w:t><w:br/><w:t>42 Elm Street</w:t></w:r><w:r><w:br w:type="textWrapping"/></w:r>' +
+ `${run("Springfield, IL 62704")}</w:p>` +
+ '<w:p><w:r><w:t>Summary</w:t><w:br w:type="page"/><w:t>Details</w:t></w:r></w:p>' +
+ `<w:tbl><w:tr><w:tc><w:p>${run("Built APIs")}<w:r><w:cr/><w:t>Led team of 5</w:t></w:r></w:p></w:tc></w:tr></w:tbl>` +
+ box("1", "☒", " Smoker") +
+ box("0", "☐", " Diabetic") +
+ field('<w:default w:val="0"/><w:checked/>', " Allergies") +
+ field('<w:default w:val="0"/>', " Pregnant") +
+ '<w:p><w:r><w:t xml:space="preserve">Consent </w:t><w:fldChar w:fldCharType="begin"><w:ffData><w:checkBox><w:checked/></w:checkBox></w:ffData></w:fldChar></w:r>' +
+ '<w:r><w:instrText xml:space="preserve"> FORMCHECKBOX </w:instrText></w:r>' +
+ `<w:r><w:fldChar w:fldCharType="end"/></w:r>${run(" given")}</w:p>` +
+ "</w:body></w:document>",
+ );
+ const globals = globalThis as { DOMParser?: unknown; XMLSerializer?: unknown };
+ const original = { DOMParser: globals.DOMParser, XMLSerializer: globals.XMLSerializer };
+ globals.DOMParser = XmlDomParser;
+ globals.XMLSerializer = XmlSerializer;
+ try {
+ const { default: mammoth } = await import("mammoth");
+ const { value } = await mammoth.extractRawText({
+ buffer: Buffer.from(writeDocxBreaksAndCheckboxes(archive)),
+ });
+ assert.equal(
+ value,
+ "Jane Doe\n42 Elm Street\nSpringfield, IL 62704\n\n" +
+ "Summary\nDetails\n\n" +
+ "Built APIs\nLed team of 5\n\n" +
+ "☒ Smoker\n\n☐ Diabetic\n\n☒ Allergies\n\n☐ Pregnant\n\nConsent ☒ given\n\n",
+ );
+ } finally {
+ Object.assign(globals, original);
+ }
+});
+
/** A preview only colours what the filename says is source; extracted document text is prose whatever the file was called. */
test("attachmentTextLanguage maps source files and leaves prose alone", () => {
assert.equal(attachmentTextLanguage("train.py", null), "python");