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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep table columns in attached Word and HTML files (#13105)
Nilay8209ac46e99e
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.tsindex 5b769e45c0b..f13840b3a58 100644--- a/studio/frontend/src/features/chat/attachment-content.ts+++ b/studio/frontend/src/features/chat/attachment-content.ts@@ -959,7 +959,7 @@ export function writeDocxBreaksAndCheckboxes(archive: Uint8Array): Uint8Array {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.+ // mammoth drops page and column breaks, which would join surrounding words.const breaks = [...Array.from(doc.getElementsByTagNameNS(w, "br")),...Array.from(doc.getElementsByTagNameNS(w, "cr")),@@ -971,6 +971,89 @@ export function writeDocxBreaksAndCheckboxes(archive: Uint8Array): Uint8Array {return zipSync({ ...unzipSync(archive), ...rewritten }, { level: 0 });}+const DOCX_TABLE_RE = /<(?:[\w.-]+:)?tbl[\s>]/;++export function writeDocxTableRows(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_TABLE_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 run = (child: Element) => {+ const r = doc.createElementNS(w, tag("r"));+ r.appendChild(child);+ return r;+ };+ const space = () => {+ const t = doc.createElementNS(w, tag("t"));+ t.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");+ t.appendChild(doc.createTextNode(" "));+ return t;+ };+ const tab = () => run(doc.createElementNS(w, tag("tab")));+ const grid = (node: Element, pr: string, name: string) =>+ Math.min(Number(childElements(node, w, pr).flatMap((e) => childElements(e, w, name))[0]?.getAttributeNS(w, "val")) || 0, 1000);+ const outermost = (p: Element, cell: Element) => {+ for (let node = p.parentNode; node && node !== cell; node = node.parentNode) {+ if ((node as Element).localName === "p") return false;+ }+ return true;+ };+ // flatten nested tables before joining their paragraphs into the cell.+ for (const table of Array.from(doc.getElementsByTagNameNS(w, "tbl")).reverse()) {+ for (const row of Array.from(table.getElementsByTagNameNS(w, "tr"))) {+ const cells = Array.from(row.getElementsByTagNameNS(w, "tc"));+ const before = grid(row, "trPr", "gridBefore");+ const after = grid(row, "trPr", "gridAfter");+ if (cells.length + before + after < 2) {+ for (const cell of cells) {+ for (const child of Array.from(cell.childNodes)) {+ if ((child as Element).localName !== "tcPr") table.parentNode?.insertBefore(child, table);+ }+ }+ continue;+ }+ const line = doc.createElementNS(w, tag("p"));+ for (let i = 0; i < before; i++) line.appendChild(tab());+ cells.forEach((cell, index) => {+ if (index) line.appendChild(tab());+ // cell tabs and line breaks, including those in nested tables, would look like column or row separators.+ for (const local of ["tab", "br", "cr"]) {+ for (const mark of Array.from(cell.getElementsByTagNameNS(w, local))) {+ if ((mark.parentNode as Element | null)?.localName === "r") mark.parentNode?.replaceChild(space(), mark);+ }+ }+ for (const t of Array.from(cell.getElementsByTagNameNS(w, "t"))) {+ for (const text of Array.from(t.childNodes)) {+ if (text.nodeValue?.includes("\n")) t.replaceChild(doc.createTextNode(text.nodeValue.replace(/\n/g, " ")), text);+ }+ }+ Array.from(cell.getElementsByTagNameNS(w, "p"))+ .filter((p) => outermost(p, cell))+ .forEach((p, i) => {+ if (i) line.appendChild(run(space()));+ for (const child of Array.from(p.childNodes)) {+ if ((child as Element).localName !== "pPr") line.appendChild(child);+ }+ });+ for (let i = 1; i < (grid(cell, "tcPr", "gridSpan") || 1); i++) line.appendChild(tab());+ });+ for (let i = 0; i < after; i++) line.appendChild(tab());+ table.parentNode?.insertBefore(line, table);+ }+ table.parentNode?.removeChild(table);+ }+ 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*["']([^"']*)["']/;@@ -1498,7 +1581,7 @@ export async function extractDocxAttachmentText(file: File): Promise<string> {);const marked = markDocxNotes(linearizeDocxMath(repacked));const { value } = await mammoth.extractRawText({- arrayBuffer: toArrayBuffer(writeDocxBreaksAndCheckboxes(marked.archive)),+ arrayBuffer: toArrayBuffer(writeDocxTableRows(writeDocxBreaksAndCheckboxes(marked.archive))),});return marked.label(value);}@@ -1618,11 +1701,11 @@ export function extractHtmlAttachmentText(html: string): string {.join("");}-/** Text with the line structure the source had. `textContent` runs a whole page together, so- * every block-level element and every `<br>` contributes a break of its own. */+/** preserves breaks because `textContent` omits block and `<br>` boundaries. */function collectHtmlBlockText(node: Node | null,preformatted?: string[],+ rowSpans?: number[],): string {if (!node) {return "";@@ -1653,13 +1736,68 @@ function collectHtmlBlockText(return "\n\u0000\n";}+ if (tag === "tr" && preformatted) {+ const cells = Array.from(element.childNodes).filter(+ (child): child is Element =>+ child.nodeType === ELEMENT_NODE &&+ ["td", "th"].includes((child as Element).tagName.toLowerCase()),+ );+ const covered = rowSpans ?? [];+ const slots: (Element | null)[] = [];+ const skipCovered = () => {+ while (covered[slots.length] > 0) {+ covered[slots.length]--;+ slots.push(null);+ }+ };+ const span = (value: string | null, max: number) =>+ Math.min(Math.max(Number(value) || 1, 1), max);+ for (const cell of cells) {+ skipCovered();+ const rowspan = cell.getAttribute("rowspan");+ const rows =+ rowspan !== null && /^0+$/.test(rowspan)+ ? Number.POSITIVE_INFINITY+ : span(rowspan, 65534);+ for (let i = 0; i < span(cell.getAttribute("colspan"), 1000); i++) {+ covered[slots.length] = rows - 1;+ slots.push(i ? null : cell);+ }+ }+ for (let i = slots.length; i < covered.length; i++) {+ if (covered[i] > 0) covered[i]--;+ }+ // preformatted code keeps its line breaks on the fallback path.+ if (slots.length > 1 && !cells.some(containsPre)) {+ const row = slots+ .map((cell) => (cell ? collectHtmlBlockText(cell).replace(/\s+/g, " ").trim() : ""))+ .join("\t");+ if (!row.trim()) {+ return "\n";+ }+ preformatted.push(row);+ return "\n\u0000\n";+ }+ }++ const groupSpans = HTML_ROW_GROUP_TAGS.has(tag) ? [] : rowSpans;const text = Array.from(element.childNodes)- .map((child) => collectHtmlBlockText(child, preformatted))+ .map((child) => collectHtmlBlockText(child, preformatted, groupSpans)).join("");return HTML_BLOCK_TAGS.has(tag) ? `\n${text}\n` : text;}-/** Collapses the spaces an extractor leaves between positioned runs while keeping the line breaks the source marked. */+const HTML_ROW_GROUP_TAGS = new Set(["table", "thead", "tbody", "tfoot"]);++function containsPre(node: Node): boolean {+ return Array.from(node.childNodes).some(+ (child) =>+ child.nodeType === ELEMENT_NODE &&+ ((child as Element).tagName.toLowerCase() === "pre" || containsPre(child)),+ );+}++/** collapses extractor spacing while keeping source line breaks. */function normalizeExtractedText(text: string): string {return text.replace(/[^\S\n]+/g, " ")diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.tsindex 081c326bdab..fa8ff28c495 100644--- a/studio/frontend/tests/attachment-preview-text.test.ts+++ b/studio/frontend/tests/attachment-preview-text.test.ts@@ -37,6 +37,7 @@ const {repackDocxPreviewArchive,truncateAttachmentPreviewText,writeDocxBreaksAndCheckboxes,+ writeDocxTableRows,} = await import("../src/features/chat/attachment-content.ts");const { definePDFJSModule } = await import("unpdf");const { readRtfAttachmentContent } =@@ -1479,14 +1480,213 @@ test("a Word file keeps its line breaks and which boxes are ticked", async () =>}});-/** A preview only colours what the filename says is source; extracted document text is prose whatever the file was called. */+test("a Word table keeps its columns when a cell is empty", async () => {+ const ns = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';+ const cell = (...paragraphs: string[]) =>+ `<w:tc>${paragraphs.map((text) => `<w:p><w:r><w:t>${text}</w:t></w:r></w:p>`).join("") || "<w:p/>"}</w:tc>`;+ const row = (...cells: string[]) => `<w:tr>${cells.join("")}</w:tr>`;+ const bytes = docxBytes(+ `<w:document ${ns}><w:body><w:p><w:r><w:t>Timetable</w:t></w:r></w:p><w:tbl>` ++ row('<w:tc><w:tcPr><w:gridSpan w:val="6"/></w:tcPr><w:p><w:r><w:t>Week 1</w:t></w:r></w:p></w:tc>') ++ row(cell("Time"), cell("Mon"), cell("Tue"), cell("Wed"), cell("Thu"), cell("Fri")) ++ row(cell("10:00"), cell("History"), cell(), cell("Maths"), cell("Art"), cell()) ++ row(+ cell("11:00"),+ '<w:tc><w:tcPr><w:gridSpan w:val="2"/></w:tcPr><w:p><w:r><w:t>Trip</w:t></w:r></w:p></w:tc>',+ cell("Music", "Room 4"),+ cell(),+ cell("PE"),+ ) ++ "</w:tbl></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(writeDocxTableRows(writeDocxBreaksAndCheckboxes(bytes))),+ });+ assert.equal(+ value,+ "Timetable\n\nWeek 1\n\n" ++ "Time\tMon\tTue\tWed\tThu\tFri\n\n" ++ "10:00\tHistory\t\tMaths\tArt\t\n\n" ++ "11:00\tTrip\t\tMusic Room 4\t\tPE\n\n",+ );+ } finally {+ Object.assign(globals, original);+ }+});++test("an html table keeps its columns when a cell is empty", async () => {+ const cell = (tag: string, text = "", colspan?: string) =>+ Object.assign(element(tag, ...(text ? [textNode(text)] : [])), {+ getAttribute: (name: string) => (name === "colspan" ? (colspan ?? null) : null),+ });+ const extracted = await withStubDom(+ () =>+ element(+ "body",+ element("h2", textNode("Timetable")),+ element(+ "table",+ element(+ "tbody",+ element("tr", cell("th", "Time"), cell("th", "Mon"), cell("th", "Tue"), cell("th", "Wed")),+ textNode("\n "),+ element("tr", cell("td", "10:00"), cell("td", "History"), cell("td"), cell("td", "Maths")),+ element(+ "tr",+ cell("td", "11:00"),+ cell("td", "Trip", "2"),+ Object.assign(element("td", element("p", textNode("Art")), element("p", textNode("Room 4"))), {+ getAttribute: () => null,+ }),+ ),+ ),+ ),+ element("p", textNode("Bring a pencil")),+ ),+ () => extractHtmlAttachmentText("<html/>"),+ );++ assert.equal(+ extracted,+ "Timetable\n\nTime\tMon\tTue\tWed\n\n10:00\tHistory\t\tMaths\n\n11:00\tTrip\t\tArt Room 4\n\nBring a pencil",+ );+});++test("a Word row keeps its columns past skipped grid cells, tabs, line breaks and nested tables", async () => {+ const ns = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';+ const p = (text: string) => `<w:p><w:r><w:t>${text}</w:t></w:r></w:p>`;+ const row = (cells: string, pr = "") => `<w:tr>${pr}${cells}</w:tr>`;+ const nested = `<w:tbl>${row(`<w:tc>${p("Room")}</w:tc><w:tc>${p("4")}</w:tc>`)}</w:tbl>`;+ const bytes = docxBytes(+ `<w:document ${ns}><w:body><w:tbl>` ++ row(`<w:tc>${p("Time")}</w:tc><w:tc>${p("Mon")}</w:tc><w:tc>${p("Tue")}</w:tc>`) ++ row(`<w:tc>${p("Lab 3")}</w:tc><w:tc>${p("Lab 4")}</w:tc>`, '<w:trPr><w:gridBefore w:val="1"/></w:trPr>') ++ row(`<w:tc>${p("Only Tue")}</w:tc>`, '<w:trPr><w:gridBefore w:val="2"/></w:trPr>') ++ row(`<w:tc>${p("Only Time")}</w:tc>`, '<w:trPr><w:gridAfter w:val="2"/></w:trPr>') ++ row(+ `<w:tc>${p("10:00")}</w:tc>` ++ `<w:tc><w:p><w:r><w:t>A</w:t><w:tab/><w:t>B</w:t></w:r></w:p></w:tc>` ++ `<w:tc>${nested}<w:p/></w:tc>`,+ ) ++ row(`<w:tc>${p("11:00")}</w:tc><w:tc><w:p><w:r><w:t>Line one</w:t><w:br/><w:t>Line two</w:t></w:r></w:p></w:tc><w:tc>${p("C")}</w:tc>`) ++ "</w:tbl></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(writeDocxTableRows(writeDocxBreaksAndCheckboxes(bytes))),+ });+ assert.equal(+ value,+ "Time\tMon\tTue\n\n\tLab 3\tLab 4\n\n\t\tOnly Tue\n\nOnly Time\t\t\n\n10:00\tA B\tRoom 4 \n\n11:00\tLine one Line two\tC\n\n",+ );+ } finally {+ Object.assign(globals, original);+ }+});++test("an html table keeps its columns under a rowspan and leaves code in a cell alone", async () => {+ const cell = (tag: string, text = "", attributes: Record<string, string> = {}) =>+ Object.assign(element(tag, ...(text ? [textNode(text)] : [])), {+ getAttribute: (name: string) => attributes[name] ?? null,+ });+ const extracted = await withStubDom(+ () =>+ element(+ "body",+ element(+ "table",+ element(+ "tbody",+ element("tr", cell("th", "Time"), cell("th", "Mon"), cell("th", "Tue"), cell("th", "Wed")),+ element("tr", cell("td", "09:00"), cell("td", "Science", { rowspan: "2" }), cell("td", "Art"), cell("td", "PE")),+ element("tr", cell("td", "10:00"), cell("td", "Maths"), cell("td", "French")),+ element("tr", cell("td", "11:00"), cell("td", "Music", { colspan: "3" })),+ element("tr", cell("td", "ship()"), Object.assign(element("td", element("pre", textNode("def ship():\n return 1"))), {+ getAttribute: () => null,+ })),+ ),+ ),+ ),+ () => extractHtmlAttachmentText("<html/>"),+ );++ assert.equal(+ extracted,+ "Time\tMon\tTue\tWed\n\n09:00\tScience\tArt\tPE\n\n10:00\t\tMaths\tFrench\n\n11:00\tMusic\t\t\n\nship()\n\ndef ship():\n return 1",+ );+});++test("an html rowspan of zero covers the rest of its row group", async () => {+ const cell = (text: string, rowspan?: string) =>+ Object.assign(element("td", textNode(text)), {+ getAttribute: (name: string) => (name === "rowspan" ? (rowspan ?? null) : null),+ });+ const extracted = await withStubDom(+ () =>+ element(+ "body",+ element(+ "table",+ element(+ "tbody",+ element("tr", cell("All day", "0"), cell("Mon 09:00"), cell("Tue 09:00")),+ element("tr", cell("Mon 10:00"), cell("Tue 10:00")),+ ),+ element("tbody", element("tr", cell("Evening"), cell("Mon 18:00"), cell("Tue 18:00"))),+ ),+ ),+ () => extractHtmlAttachmentText("<html/>"),+ );++ assert.equal(+ extracted,+ "All day\tMon 09:00\tTue 09:00\n\n\tMon 10:00\tTue 10:00\n\nEvening\tMon 18:00\tTue 18:00",+ );+});++test("an html rowspan keeps its full standards-defined range", async () => {+ const cell = (text: string, rowspan?: string) =>+ Object.assign(element("td", textNode(text)), {+ getAttribute: (name: string) => (name === "rowspan" ? (rowspan ?? null) : null),+ });+ const labels = Array.from({ length: 1000 }, (_, index) => `row ${index + 1}`);+ const extracted = await withStubDom(+ () =>+ element(+ "body",+ element(+ "table",+ element(+ "tbody",+ element("tr", cell("Group", "1001"), cell("row 0")),+ ...labels.map((label) => element("tr", cell(label))),+ ),+ ),+ ),+ () => extractHtmlAttachmentText("<html/>"),+ );++ assert.equal(extracted, ["Group\trow 0", ...labels.map((label) => `\t${label}`)].join("\n\n"));+});+test("attachmentTextLanguage maps source files and leaves prose alone", () => {assert.equal(attachmentTextLanguage("train.py", null), "python");assert.equal(attachmentTextLanguage("Chart.YAML", null), "yaml");assert.equal(attachmentTextLanguage("page.html", null), "html");assert.equal(attachmentTextLanguage("notes.txt", null), null);assert.equal(attachmentTextLanguage("script.py", "PDF"), null);- // the label parsed from the adapter's wrapper keeps a sent extraction unhighlighted+ // parsed adapter labels keep sent extractions unhighlightedassert.equal(attachmentTextLanguage("page.html",