@devries-cameron20 / t3code-1 / commits / baf2558017
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): preserve ODF paragraph whitespace in agent sidecars (#36883)

Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 20:34 UTC baf2558017397e054dbed2b29825a5ad517c0cd7
2 files changed +39 -6
apps/server/src/provider/documentTextWorker.ts
+33 -2
@@ -41,7 +41,38 @@ interface XmlNode {
41 hasTextChoice?: boolean;
42 }
43
44 +// ODF 1.3 Part 3 §6.1.2: paragraph character data turns TAB, CR and LF into spaces, collapses
45 +// space runs and trims the ends; only <text:s>, <text:tab> and <text:line-break> keep literal ones.
46 +function collapseOdfWhitespace(paragraph: XmlNode) {
47 + let afterSpace = true;
48 + let tail: { node: XmlNode; index: number; value: string } | undefined;
49 + const visit = (node: XmlNode) => {
50 + for (const [index, child] of node.children.entries()) {
51 + if (typeof child !== "string") {
52 + if (HIDDEN.has(child.name)) continue;
53 + if (child.spaceCount === undefined && !["tab", "line-break", "p", "h"].includes(child.name))
54 + visit(child);
55 + else {
56 + afterSpace = false;
57 + tail = undefined;
58 + }
59 + continue;
60 + }
61 + let value = child.replaceAll(/[ \t\r\n]+/g, " ");
62 + if (afterSpace && value.startsWith(" ")) value = value.slice(1);
63 + if (value) {
64 + afterSpace = value.endsWith(" ");
65 + tail = { node, index, value };
66 + }
67 + node.children[index] = value;
68 + }
69 + };
70 + visit(paragraph);
71 + if (tail && afterSpace) tail.node.children[tail.index] = tail.value.slice(0, -1);
72 +}
73 +
74 function xml(source: string, extension: string): XmlNode {
75 + const odf = [".odt", ".ods", ".odp"].includes(extension);
76 const root: XmlNode = { name: "root", attrs: {}, children: [] };
77 const stack = [root];
78 let nodes = 0;
@@ -59,14 +90,14 @@ function xml(source: string, extension: string): XmlNode {
90 ),
91 children: [],
92 };
62 - if ([".odt", ".ods", ".odp"].includes(extension) && node.name === "s")
63 - node.spaceCount = repeatCount(node.attrs.c);
93 + if (odf && node.name === "s") node.spaceCount = repeatCount(node.attrs.c);
94 stack.at(-1)!.children.push(node);
95 stack.push(node);
96 });
97 parser.on("closetag", () => {
98 const node = stack.pop()!;
99 const parent = stack.at(-1)!;
100 + if (odf && (node.name === "p" || node.name === "h")) collapseOdfWhitespace(node);
101 if (extension === ".docx" && node.name === "del") {
102 parent.children.pop();
103 return;
apps/server/src/provider/userInputAttachments.test.ts
+6 -4
@@ -928,9 +928,11 @@ describe("question answer paths", () => {
928 ext: "odt",
929 parts: {
930 "content.xml":
931 - '<office:document><text:h text:outline-level="2">Orchard</text:h><text:p>19<text:s text:c="3"/>violet<text:s/>crates</text:p></office:document>',
931 + '<office:document><text:h text:outline-level="2">\n Orchard\n</text:h><text:p>19<text:s text:c="3"/>violet<text:s/>crates <text:span>\n\t shipped</text:span> today<text:tab/>Dock<text:line-break/>Gate\u00a0B </text:p><text:p>Second</text:p><text:p>A<text:span><text:script>console.log(1);\n</text:script></text:span> B</text:p></office:document>',
932 },
933 - expected: ["## Orchard", "19 violet crates"],
933 + expected: [
934 + "## Orchard\n19 violet crates shipped today\tDock\nGate\u00a0B\nSecond\nA B",
935 + ],
936 },
937 {
938 ext: "ods",
@@ -977,9 +979,9 @@ describe("question answer paths", () => {
979 ext: "odp",
980 parts: {
981 "content.xml":
980 - "<office:document><draw:page><text:p>First</text:p></draw:page><draw:page><text:p>Second</text:p></draw:page></office:document>",
982 + "<office:document><draw:page><draw:frame><draw:text-box><text:p>\n First\n\t slide </text:p></draw:text-box></draw:frame></draw:page><draw:page><text:p>Second</text:p></draw:page></office:document>",
983 },
982 - expected: ["## Slide 1", "First", "## Slide 2", "Second"],
984 + expected: ["## Slide 1\nFirst slide\n\n## Slide 2\nSecond"],
985 },
986 {
987 ext: "ods",