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 Insert > Symbol characters when reading Word files (#14393)

Nilayb272568bb3cf
diff --git a/studio/backend/core/rag/parsers.py b/studio/backend/core/rag/parsers.py
index d28003ad202..1b4c1cb60eb 100644
--- a/studio/backend/core/rag/parsers.py
+++ b/studio/backend/core/rag/parsers.py
@@ -738,6 +738,54 @@ _DOCX_MATH_ROWS = {
"m": ("mr", " \\\\ "),
"mr": ("e", " & "),
}
+# Symbol-font characters from 0x20, as mammoth (the + attachment reader) maps them.
+_DOCX_SYMBOL_FONTS = {
+ "symbol": (
+ " !∀#∃%&∍()*+,−./0123456789:;<=>?"
+ "≅ΑΒΧΔΕΦΓΗΙϑΚΛΜΝΟΠΘΡΣΤΥςΩΞΨΖ[∴]⊥_"
+ "‾αβχδεφγηιϕκλμνοπθρστυϖωξψζ{|}~\0"
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+ "€ϒ′≤⁄∞ƒ♣♦♥♠↔←↑→↓°±″≥×∝∂•÷≠≡≈…⏐⎯↵"
+ "ℵℑℜ℘⊗⊕∅∩∪⊃⊇⊄⊂⊆∈∉∠∇®©™∏√⋅¬∧∨⇔⇐⇑⇒⇓"
+ "◊〈®©™∑⎛⎜⎝⎡⎢⎣⎧⎨⎩⎪\0〉∫⌠⎮⌡⎞⎟⎠⎤⎥⎦⎫⎬⎭"
+ ),
+ "wingdings": (
+ " 🖉✂✁👓🕭🕮🕯🕿✆🖂🖃📪📫📬📭🗀🗁🗎🗏🗐🗄⌛🖮🖰🖲🖳🖴🖫🖬✇✍"
+ "🖎✌🖏👍👎☜☞☝☟🖐☺😐☹💣🕱🏳🏱✈☼🌢❄🕆✞🕈✠✡☪☯🕉☸♈♉"
+ "♊♋♌♍♎♏♐♑♒♓🙰🙵⚫🔾◼🞏🞐❑❒🞟⧫◆❖⬩⌧⮹⌘🏵🏶🙶🙷▯"
+ "🄋➀➁➂➃➄➅➆➇➈➉🄌➊➋➌➍➎➏➐➑➒➓🙢🙠🙡🙣🙞🙜🙝🙟∙•"
+ "⬝⭘🞆🞈🞊🞋🔿▪🞎🟁🟅★🟋🟏🟓🟑⯐⌖⯎⯏⯑✪✰🕐🕑🕒🕓🕔🕕🕖🕗🕘"
+ "🕙🕚🕛⮰⮱⮲⮳⮴⮵⮶⮷🙪🙫🙕🙔🙗🙖🙐🙑🙒🙓⌫⌦⮘⮚⮙⮛⮈⮊⮉⮋🡨"
+ "🡪🡩🡫🡬🡭🡯🡮🡸🡺🡹🡻🡼🡽🡿🡾⇦⇨⇧⇩⬄⇳⬁⬀⬃⬂🢬🢭🗶✓🗷🗹"
+ ),
+ "wingdings 2": (
+ " 🖊🖋🖌🖍✄✀🕾🕽🗅🗆🗇🗈🗉🗊🗋🗌🗍📋🗑🗔🖵🖶🖷🖸🖭🖯🖱🖒🖓🖘🖙"
+ "🖚🖛👈👉🖜🖝🖞🖟🖠🖡👆👇🖢🖣🖑🗴🗸🗵☑⮽☒⮾⮿🛇⦸🙱🙴🙲🙳‽🙹🙺"
+ "🙻🙦🙤🙥🙧🙚🙘🙙🙛⓪①②③④⑤⑥⑦⑧⑨⑩⓿❶❷❸❹❺❻❼❽❾❿\0"
+ "☉🌕☽☾⸿✝🕇🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕦🕧🙨🙩⋅🞄⦁●○🞅🞇🞉⊙⦿🞌"
+ "🞍◾■□🞑🞒🞓🞔▣🞕🞖🞗🞘⬩⬥◇🞚◈🞛🞜🞝🞞⬪⬧◊🞠◖◗⯊⯋⯀⯁"
+ "⬟⯂⬣⬢⯃⯄🞡🞢🞣🞤🞥🞦🞧🞨🞩🞪🞫🞬🞭🞮🞯🞰🞱🞲🞳🞴🞵🞶🞷🞸🞹🞺"
+ "🞻🞼🞽🞾🞿🟀🟂🟄🟆🟉🟊✶🟌🟎🟐🟒✹🟃🟇✯🟍🟔⯌⯍※⁂"
+ ),
+ "wingdings 3": (
+ " ⭠⭢⭡⭣⭦⭧⭩⭨⭰⭲⭱⭳⭶⭸⭻⭽⭤⭥⭪⭬⭫⭭⭍⮠⮡⮢⮣⮤⮥⮦⮧"
+ "⮐⮑⮒⮓⮀⮃⭾⭿⮄⮆⮅⮇⮏⮍⮎⮌⭮⭯⎋⌤⌃⌥␣⍽⇪⮸🢠🢡🢢🢣🢤🢥"
+ "🢦🢧🢨🢩🢪🢫🡐🡒🡑🡓🡔🡕🡗🡖🡘🡙▲▼△▽◀▶◁▷◣◢◤◥🞀🞂🞁\0"
+ "🞃⯅⯆⯇⯈⮜⮞⮝⮟🠐🠒🠑🠓🠔🠖🠕🠗🠘🠚🠙🠛🠜🠞🠝🠟🠀🠂🠁🠃🠄🠆🠅"
+ "🠇🠈🠊🠉🠋🠠🠢🠤🠦🠨🠪🠬🢜🢝🢞🢟🠮🠰🠲🠴🠶🠸🠺🠹🠻🢘🢚🢙🢛🠼🠾🠽"
+ "🠿🡀🡂🡁🡃🡄🡆🡅🡇⮨⮩⮪⮫⮬⮭⮮⮯🡠🡢🡡🡣🡤🡥🡧🡦🡰🡲🡱🡳🡴🡵🡷"
+ "🡶🢀🢂🢁🢃🢄🢅🢇🢆🢐🢒🢑🢓🢔🢖🢕🢗"
+ ),
+ "webdings": (
+ " 🕷🕸🕲🕶🏆🎖🖇🗨🗩🗰🗱🌶🎗🙾🙼🗕🗖🗗⏴⏵⏶⏷⏪⏩⏮⏭⏸⏹⏺🗚🗳"
+ "🛠🏗🏘🏙🏚🏜🏭🏛🏠🏖🏝🛣🔍🏔👁👂🏞🏕🛤🏟🛳🕬🕫🕨🔈🎔🎕🗬🙽🗭🗪🗫"
+ "⮔✔🚲⬜🛡📦🛱⬛🚑🛈🛩🛰🟈🕴⬤🛥🚔🗘🗙❓🛲🚇🚍⛳⦸⊖🚭🗮⏐🗯🗲\0"
+ "🚹🚺🛉🛊🚼👽🏋⛷🏂🏌🏊🏄🏍🏎🚘🗠🛢💰🏷💳👪🗡🗢🗣✯🖄🖅🖃🖆🖹🖺🖻"
+ "🕵🕰🖽🖾📋🗒🗓🕮📚🗞🗟🗃🗂🖼🎭🎜🎘🎙🎧💿🎞📷🎟🎬📽📹📾📻🎚🎛📺💻"
+ "🖥🖦🖧🕹🎮🕻🕼📟🖁🖀🖨🖩🖿🖪🗜🔒🔓🗝📥📤🕳🌣🌤🌥🌦☁🌨🌧🌩🌪🌬🌫"
+ "🌜🌡🛋🛏🍽🍸🛎🛍Ⓟ♿🛆🖈🎓🗤🗥🗦🗧🛪🐿🐦🐟🐕🐈🙬🙮🙭🙯🗺🌍🌏🌎🕊"
+ ),
+}
def _docx_placeholder(element) -> bool:
@@ -803,7 +851,9 @@ def _docx_math_text(element) -> str:
if tag in _DOCX_SKIP_RUNS_UNDER or _docx_placeholder(element):
return ""
if tag == _W + "r":
- return element.text
+ return _docx_run_text(element)
+ if tag == _W + "sym":
+ return _docx_symbol(element)
if tag == _M + "t":
return element.text or ""
name = tag[len(_M) :] if tag.startswith(_M) else ""
@@ -875,12 +925,30 @@ def _docx_checkbox(field) -> str:
return "☒" if ticked else "☐"
+def _docx_symbol(sym) -> str:
+ try:
+ code = int(sym.get(_W + "char") or "", 16)
+ except ValueError:
+ return ""
+ table = _DOCX_SYMBOL_FONTS.get((sym.get(_W + "font") or "").lower())
+ if table is not None and code >> 8 in (0, 0xF0):
+ index = (code & 0xFF) - 0x20
+ code = ord(table[index]) if 0 <= index < len(table) else 0
+ return chr(code) if 0 <= code <= 0x10FFFF and chr(code).isprintable() else ""
+
+
def _docx_run_text(run) -> str:
- if run.find(f"{_W}fldChar/{_W}ffData/{_W}checkBox") is None:
+ if run.find(f"{_W}fldChar/{_W}ffData/{_W}checkBox") is None and run.find(_W + "sym") is None:
return run.text
return "".join(
- _docx_checkbox(e) if e.tag == _W + "fldChar" else str(e)
- for e in run.xpath("w:br | w:cr | w:noBreakHyphen | w:ptab | w:t | w:tab | w:fldChar")
+ _docx_checkbox(e)
+ if e.tag == _W + "fldChar"
+ else _docx_symbol(e)
+ if e.tag == _W + "sym"
+ else str(e)
+ for e in run.xpath(
+ "w:br | w:cr | w:noBreakHyphen | w:ptab | w:t | w:tab | w:fldChar | w:sym"
+ )
)
diff --git a/studio/backend/tests/test_rag_parsing.py b/studio/backend/tests/test_rag_parsing.py
index 35197963b83..333f202ae7b 100644
--- a/studio/backend/tests/test_rag_parsing.py
+++ b/studio/backend/tests/test_rag_parsing.py
@@ -595,6 +595,31 @@ def test_docx_legacy_checkbox_sharing_a_run_with_its_label_stays_in_place(tmp_pa
assert text == "Smoker: ☐ Yes ☒ No"
+def _sym(font, char):
+ return f'<w:sym w:font="{font}" w:char="{char}"/>'
+
+
+def test_docx_keeps_symbol_font_characters(tmp_path):
+ def run(*parts):
+ return "<w:r>" + "".join(parts) + "</w:r>"
+
+ text = _docx_from_xml(
+ tmp_path,
+ f"<w:p>{_r('Smoker: ')}{run(_sym('Wingdings', 'F0FE'))}{_r(' Yes ')}"
+ f"{run(_sym('Wingdings', 'F0A8'))}{_r(' No')}</w:p>",
+ f"<w:p>{_r('Escalate if temperature ')}{run(_sym('Symbol', 'F0B3'))}{_r(' 38')}"
+ f"{run(_sym('SYMBOL', 'B0'))}{_r('C')}</w:p>",
+ f"<w:p>{run('<w:t>Dose: 5 mg</w:t><w:tab/>', _sym('Symbol', 'F0B1'), '<w:t>0.5</w:t>')}</w:p>",
+ f"<w:p>{run(_sym('Wingdings 2', 'F054'), _sym('Webdings', 'F061'), _sym('Arial', '2713'), _sym('Symbol', '221E'))}"
+ f"{run(_sym('Unknown', 'F041'), _sym('Symbol', 'zz'))}</w:p>",
+ f"<w:p><m:oMath><m:r><m:t>x</m:t></m:r><m:r>{_sym('Symbol', 'F0B3')}</m:r><m:r><m:t>1</m:t></m:r>"
+ f"{run(_sym('Symbol', 'F0B1'))}<m:r><m:t>e</m:t></m:r></m:oMath></w:p>",
+ )
+ assert (
+ text == "Smoker: 🗹 Yes 🞎 No\nEscalate if temperature ≥ 38°C\nDose: 5 mg\t±0.5\n☒✔✓∞\nx≥1±e"
+ )
+
+
def _m(text):
return f'<m:r><w:rPr><w:rFonts w:ascii="Cambria Math"/></w:rPr><m:t>{text}</m:t></m:r>'
diff --git a/studio/frontend/package-lock.json b/studio/frontend/package-lock.json
index 0ffc7794ada..68f993ef52d 100644
--- a/studio/frontend/package-lock.json
+++ b/studio/frontend/package-lock.json
@@ -50,6 +50,7 @@
"cmdk": "^1.1.1",
"decode-named-character-reference": "1.3.0",
"dexie": "^4.3.0",
+ "dingbat-to-unicode": "^1.0.1",
"fflate": "0.8.3",
"js-yaml": "^4.3.2",
"katex": "^0.16.28",
diff --git a/studio/frontend/package.json b/studio/frontend/package.json
index 2cbd41e1d10..b325125f6f4 100644
--- a/studio/frontend/package.json
+++ b/studio/frontend/package.json
@@ -65,6 +65,7 @@
"cmdk": "^1.1.1",
"decode-named-character-reference": "1.3.0",
"dexie": "^4.3.0",
+ "dingbat-to-unicode": "^1.0.1",
"fflate": "0.8.3",
"js-yaml": "^4.3.2",
"katex": "^0.16.28",
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.ts
index 029b485acc1..0d15fb08800 100644
--- a/studio/frontend/src/features/chat/attachment-content.ts
+++ b/studio/frontend/src/features/chat/attachment-content.ts
@@ -1807,15 +1807,15 @@ export async function extractOfficeAttachmentText(
export async function extractDocxAttachmentText(file: File): Promise<string> {
assertDocumentAttachmentSize(file, "DOCX");
- const [{ default: mammoth }, buffer] = await Promise.all([
- import("mammoth"),
+ const [{ mammoth, writeDocxSymbols }, buffer] = await Promise.all([
+ import("./docx-reader"),
file.arrayBuffer(),
]);
const repacked = repackDocxAttachmentArchive(
file.name,
new Uint8Array(buffer),
);
- const marked = markDocxNotes(linearizeDocxMath(repacked));
+ const marked = markDocxNotes(linearizeDocxMath(writeDocxSymbols(repacked, MAX_OPEN_DOCUMENT_XML_BYTES)));
const { value } = await mammoth.extractRawText({
arrayBuffer: toArrayBuffer(writeDocxTableRows(writeDocxBreaksAndCheckboxes(writeDocxListNumbers(marked.archive)))),
});
diff --git a/studio/frontend/src/features/chat/docx-reader.ts b/studio/frontend/src/features/chat/docx-reader.ts
new file mode 100644
index 00000000000..2215c35e633
--- /dev/null
+++ b/studio/frontend/src/features/chat/docx-reader.ts
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { codePoint } from "dingbat-to-unicode";
+import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
+
+export { default as mammoth } from "mammoth";
+
+const WORDPROCESSINGML_NAMESPACES = new Set([
+ "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
+ "http://purl.oclc.org/ooxml/wordprocessingml/main",
+]);
+const DOCX_SYMBOL_FONTS = new Set(["symbol", "wingdings", "wingdings 2", "wingdings 3", "webdings"]);
+const DOCX_SYMBOL_HEX_RE = /^[0-9a-f]+$/i;
+const UNPRINTABLE_RE = /[\p{C}\p{Z}]/u;
+
+/** Insert > Symbol character; mirrors the backend's _docx_symbol. */
+function docxSymbolText(element: Element, w: string): string {
+ const raw = element.getAttributeNS(w, "char") ?? "";
+ if (!DOCX_SYMBOL_HEX_RE.test(raw)) return "";
+ let code = Number.parseInt(raw, 16);
+ const font = element.getAttributeNS(w, "font") ?? "";
+ const page = Math.floor(code / 0x100);
+ if (DOCX_SYMBOL_FONTS.has(font.toLowerCase()) && (page === 0 || page === 0xf0)) {
+ code = codePoint(font, code % 0x100)?.codePoint ?? 0;
+ }
+ if (code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return "";
+ const char = String.fromCodePoint(code);
+ return char === " " || !UNPRINTABLE_RE.test(char) ? char : "";
+}
+
+/** Each w:sym becomes the text it shows, so the body, equation and note readers all see it.
+ * Parts are picked by size, not name: mammoth reaches them through relationships. */
+export function writeDocxSymbols(archive: Uint8Array, maxPartBytes: number): Uint8Array {
+ const rewritten: Record<string, Uint8Array> = {};
+ const parts = unzipSync(archive, { filter: (entry) => entry.originalSize <= maxPartBytes });
+ for (const [name, bytes] of Object.entries(parts)) {
+ const xml = strFromU8(bytes);
+ if (!xml.includes("sym")) continue;
+ const doc = new DOMParser().parseFromString(xml, "application/xml");
+ const w = doc.documentElement?.namespaceURI ?? "";
+ if (!WORDPROCESSINGML_NAMESPACES.has(w) || doc.getElementsByTagName("parsererror").length) continue;
+ const symbols = Array.from(doc.getElementsByTagNameNS(w, "sym"));
+ for (const symbol of symbols) {
+ const run = symbol.parentNode as Element;
+ const text = doc.createElementNS(run.namespaceURI, run.prefix ? `${run.prefix}:t` : "t");
+ text.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
+ text.appendChild(doc.createTextNode(docxSymbolText(symbol, w)));
+ run.replaceChild(text, symbol);
+ }
+ if (symbols.length) rewritten[name] = strToU8(new XMLSerializer().serializeToString(doc));
+ }
+ if (!Object.keys(rewritten).length) return archive;
+ return zipSync({ ...unzipSync(archive), ...rewritten }, { level: 0 });
+}
diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.ts
index c77e1e859cf..f2dea356133 100644
--- a/studio/frontend/tests/attachment-preview-text.test.ts
+++ b/studio/frontend/tests/attachment-preview-text.test.ts
@@ -40,6 +40,7 @@ const {
writeDocxListNumbers,
writeDocxTableRows,
} = await import("../src/features/chat/attachment-content.ts");
+const { writeDocxSymbols } = await import("../src/features/chat/docx-reader.ts");
const { definePDFJSModule } = await import("unpdf");
const { readRtfAttachmentContent } =
await import("../src/features/chat/rtf.ts");
@@ -1285,6 +1286,71 @@ test("linearizeDocxMath keeps equations in the body, tables and notes", async ()
}
});
+test("writeDocxSymbols keeps Insert > Symbol characters in the body, equations and notes as the backend does", async () => {
+ const ns =
+ 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" ' +
+ 'xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"';
+ const run = (text: string) => `<w:r><w:t xml:space="preserve">${text}</w:t></w:r>`;
+ const sym = (font: string, char: string) => `<w:sym w:font="${font}" w:char="${char}"/>`;
+ const archive = repackDocxAttachmentArchive(
+ "form.docx",
+ zipSync({
+ "[Content_Types].xml": strToU8("<Types/>"),
+ "_rels/.rels": relationships([["officeDocument", "word/document.xml"]]),
+ "word/document.xml": strToU8(
+ `<w:document ${ns}><w:body>` +
+ `<w:p>${run("Smoker: ")}<w:r>${sym("Wingdings", "F0FE")}</w:r>${run(" Yes")}<w:r><w:footnoteReference w:id="1"/></w:r></w:p>` +
+ `<w:p>${run("Limit ")}<w:r>${sym("Symbol", "221E")}${sym("Arial", "2713")}${sym("Symbol", "F0B1")}</w:r></w:p>` +
+ `<w:p><m:oMath><m:r><m:t>x</m:t></m:r><m:r>${sym("Symbol", "F0B3")}</m:r><m:r><m:t>1</m:t></m:r>` +
+ `<w:r>${sym("Symbol", "F0B1")}</w:r><m:r><m:t>e</m:t></m:r></m:oMath></w:p>` +
+ `</w:body></w:document>`,
+ ),
+ "word/_rels/document.xml.rels": relationships([["footnotes", "footnotes.xml"]]),
+ "word/footnotes.xml": strToU8(
+ `<w:footnotes ${ns}><w:footnote w:id="1"><w:p>${run("Above ")}<w:r>` +
+ sym("Symbol", "F0B3") + sym("symbol", "b0") + sym("Symbol", "221E") + sym("Wingdings 2", "F054") +
+ sym("Arial", "2713") + sym("Unknown", "F041") + sym("Symbol", "zz") +
+ `</w:r>${run(" C")}</w:p></w:footnote></w:footnotes>`,
+ ),
+ }),
+ );
+ const globals = globalThis as { DOMParser?: unknown; XMLSerializer?: unknown };
+ const original = { DOMParser: globals.DOMParser, XMLSerializer: globals.XMLSerializer };
+ globals.DOMParser = XmlDomParser;
+ globals.XMLSerializer = XmlSerializer;
+ try {
+ const marked = markDocxNotes(linearizeDocxMath(writeDocxSymbols(archive, 10 * 1024 * 1024)));
+ const { default: mammoth } = await import("mammoth");
+ const { value } = await mammoth.extractRawText({ buffer: Buffer.from(marked.archive) });
+ assert.equal(marked.label(value), "Smoker: 🗹 Yes[1]\n\nLimit ∞✓±\n\nx≥1±e\n\nFootnotes\n[1] Above ≥°∞☒✓ C");
+ } finally {
+ Object.assign(globals, original);
+ }
+});
+
+test("writeDocxSymbols follows a main document target without an .xml name, as mammoth does", async () => {
+ const archive = zipSync({
+ "[Content_Types].xml": strToU8("<Types/>"),
+ "_rels/.rels": relationships([["officeDocument", "word/body.bin"]]),
+ "word/body.bin": strToU8(
+ '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p>' +
+ '<w:r><w:t xml:space="preserve">Limit </w:t><w:sym w:font="Symbol" w:char="221E"/><w:sym w:font="Arial" w:char="2713"/></w:r>' +
+ "</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(writeDocxSymbols(archive, 10 * 1024 * 1024)) });
+ assert.equal(value.trim(), "Limit ∞✓");
+ } finally {
+ Object.assign(globals, original);
+ }
+});
+
// Same cases and expected text as the backend reader's test_docx_equation_structures.
const OMML_CASES: [string, string][] = [
[