Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): set the DOCX title and page number at the right margin
marcotebcf8ab73e15f9
diff --git a/mcp-host/src/workflow/__tests__/internalTools.docxLayout.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docxLayout.test.tsindex 20d832f62..a2a0ed727 100644--- a/mcp-host/src/workflow/__tests__/internalTools.docxLayout.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.docxLayout.test.ts@@ -10,7 +10,7 @@ import { DOCX_PALETTES } from '../docxStyle'import { buildDocxTable } from '../docxTable'import { INTERNAL_TOOLS } from '../internalTools'import type { InternalToolResult } from '../types'-import { zipEntryText } from './support/zipEntries'+import { zipEntries, zipEntryText } from './support/zipEntries'let outputDir: string@@ -207,6 +207,33 @@ describe('generate_docx table layout on large input', () => {)})+describe('generate_docx header and footer', () => {+ it('sets the title and page number at the right margin of portrait and landscape pages', async () => {+ const headers = ['Metric', ...MONTHS]+ const row = (label: string) => [label, ...MONTHS.map(() => '1,234,567.00')]+ const r = await generate({+ title: 'Quarterly report',+ body: 'Intro\n\nOutro',+ branding: { companyName: 'Acme', footerText: 'Line 1' },+ tables: [{ headers, rows: [row('Revenue (USD)'), row('Cost')] }],+ })+ expect(r.success, r.error).toBe(true)+ const file = r.artifact!.path+ const parts = [...zipEntries(file).keys()].filter(n =>+ /^word\/(header|footer)\d+\.xml$/.test(n)+ )+ const stops = new Set<string>()+ for (const part of parts) {+ const xml = zipEntryText(file, part)+ const stop = /<w:tab w:val="right" w:pos="(\d+)"\/>/.exec(xml)+ expect(stop, part).not.toBeNull()+ stops.add(stop![1])+ expect(xml).not.toContain('\t\t')+ }+ expect([...stops].sort()).toEqual(['13958', '9026'])+ })+})+describe('generate_docx footer', () => {function footerTexts(file: string): string[] {const xml = zipEntryText(file, 'word/footer1.xml')diff --git a/mcp-host/src/workflow/docxStyle.ts b/mcp-host/src/workflow/docxStyle.tsindex 9d50ed921..22d3d4653 100644--- a/mcp-host/src/workflow/docxStyle.ts+++ b/mcp-host/src/workflow/docxStyle.ts@@ -61,6 +61,9 @@ export function docxHex(c: string): string {/** Width between the margins of the default A4 page (11906 twips less two 1440 margins). */export const DOCX_CONTENT_WIDTH_TWIPS = 9026+/** The same page turned to landscape (16838 twips less two 1440 margins). */+export const DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS = 16838 - 2 * 1440+/** Word's list levels are 0-8. */export const DOCX_LIST_LEVELS = 9diff --git a/mcp-host/src/workflow/docxTable.ts b/mcp-host/src/workflow/docxTable.tsindex 28abc8783..385063d8d 100644--- a/mcp-host/src/workflow/docxTable.ts+++ b/mcp-host/src/workflow/docxTable.ts@@ -11,7 +11,12 @@ import {} from 'docx'import { inlineRuns } from './docxInline'import { docxDirection, isRtlText } from './docxScript'-import { DOCX_CONTENT_WIDTH_TWIPS, type DocxPalette, docxHex } from './docxStyle'+import {+ DOCX_CONTENT_WIDTH_TWIPS,+ DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS,+ type DocxPalette,+ docxHex,+} from './docxStyle'import { CHART_FONT_FAMILY, ensureFontsReady } from './fonts'import { inlineSpans } from './inlineMarkup'import { headerText } from './tableRows'@@ -23,9 +28,6 @@ function cellText(cell: unknown): string {return typeof cell === 'object' ? JSON.stringify(cell) : String(cell)}-/** A4 width between the 1440-twip margins, turned to landscape. */-const LANDSCAPE_CONTENT_TWIPS = 16838 - 2 * 1440-/** Page and type size tried in turn; the first that holds every word wins. */const ATTEMPTS: Array<{ landscape: boolean; size: number }> = [{ landscape: false, size: 11 },@@ -148,7 +150,7 @@ function fitTable(cols: ColumnExtent[]): DocxTableFit {const fixed = landscape || size < 11 || n > AUTOFIT_MAX_COLUMNSconst side = fixed ? 60 : 120const pad = 2 * side + SLACK_TWIPS- const room = landscape ? LANDSCAPE_CONTENT_TWIPS : DOCX_CONTENT_WIDTH_TWIPS+ const room = landscape ? DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS : DOCX_CONTENT_WIDTH_TWIPSconst min = cols.map(c => Math.min(c.word * size, LONG_TOKEN_TWIPS) + pad)const max = cols.map((c, i) => Math.max(c.line * size + pad, min[i]))let widths: number[]diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.tsindex 1a493f44b..1ff15c347 100644--- a/mcp-host/src/workflow/internalTools.ts+++ b/mcp-host/src/workflow/internalTools.ts@@ -31,6 +31,7 @@ import {PageNumber,PageOrientation,Paragraph,+ TabStopType,TextRun,} from 'docx'import ExcelJS from 'exceljs'@@ -73,7 +74,13 @@ import {docxDirection,eastAsianScript,} from './docxScript'-import { DOCX_PALETTES, DocxListNumbering, docxHex } from './docxStyle'+import {+ DOCX_CONTENT_WIDTH_TWIPS,+ DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS,+ DOCX_PALETTES,+ DocxListNumbering,+ docxHex,+} from './docxStyle'import { buildDocxTable, docxSections } from './docxTable'import {PNG_BASE_PPM,@@ -3104,42 +3111,50 @@ const generateDocx: InternalToolDefinition = {footerLines[DOCX_MAX_FOOTER_LINES - 1] += '...'}const [firstFooterLine, ...moreFooterLines] = footerLines- const footer = new DocxFooter({- children: [- new Paragraph({- alignment: AlignmentType.LEFT,- children: [- ...textRuns(firstFooterLine, footerMuted, eastAsianScript(firstFooterLine)),- new TextRun({ text: '\t\t', ...footerMuted }),- new TextRun({ children: ['Page ', PageNumber.CURRENT], ...footerMuted }),- new TextRun({ children: [' / ', PageNumber.TOTAL_PAGES], ...footerMuted }),- ...(moreFooterLines.length > 0- ? textRuns(- `\n${moreFooterLines.join('\n')}`,- footerMuted,- eastAsianScript(moreFooterLines.join(' '))- ).slice(1)- : []),- ],- }),- ],- })-- // Header with company name + title.const headerMuted = { color: docxHex(palette.muted), size: 18 }const companyName = branding.companyName ?? ''- const header = new DocxHeader({- children: [- new Paragraph({- alignment: AlignmentType.LEFT,- children: [- ...textRuns(companyName, headerMuted, eastAsianScript(companyName)),- new TextRun({ text: '\t\t', ...headerMuted }),- ...textRuns(title ?? '', headerMuted, eastAsianScript(title ?? '')),- ],- }),- ],- })+ // The page number and the title sit at the right margin, on a right tab+ // stop at the width of the section's page.+ const running = (width: number) => {+ const tabStops = [{ type: TabStopType.RIGHT, position: width }]+ const footer = new DocxFooter({+ children: [+ new Paragraph({+ alignment: AlignmentType.LEFT,+ tabStops,+ children: [+ ...textRuns(firstFooterLine, footerMuted, eastAsianScript(firstFooterLine)),+ new TextRun({ text: '\t', ...footerMuted }),+ new TextRun({ children: ['Page ', PageNumber.CURRENT], ...footerMuted }),+ new TextRun({ children: [' / ', PageNumber.TOTAL_PAGES], ...footerMuted }),+ ...(moreFooterLines.length > 0+ ? textRuns(+ `\n${moreFooterLines.join('\n')}`,+ footerMuted,+ eastAsianScript(moreFooterLines.join(' '))+ ).slice(1)+ : []),+ ],+ }),+ ],+ })+ const header = new DocxHeader({+ children: [+ new Paragraph({+ alignment: AlignmentType.LEFT,+ tabStops,+ children: [+ ...textRuns(companyName, headerMuted, eastAsianScript(companyName)),+ new TextRun({ text: '\t', ...headerMuted }),+ ...textRuns(title ?? '', headerMuted, eastAsianScript(title ?? '')),+ ],+ }),+ ],+ })+ return { header, footer }+ }+ const portrait = running(DOCX_CONTENT_WIDTH_TWIPS)+ const landscape = running(DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS)// Runs of Han characters alone name no East Asian face and take this one.const eastAsia = documentEastAsianScript(@@ -3174,8 +3189,8 @@ const generateDocx: InternalToolDefinition = {properties: section.landscape? { page: { size: { orientation: PageOrientation.LANDSCAPE } } }: {},- headers: { default: header },- footers: { default: footer },+ headers: { default: (section.landscape ? landscape : portrait).header },+ footers: { default: (section.landscape ? landscape : portrait).footer },children: section.children,})),})