Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): say when a slide table's columns are too narrow for their words

marcoteb8f98ccfff39b
diff --git a/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts b/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
index c10214ca9..efc148317 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.pptx.layout.test.ts
@@ -157,6 +157,31 @@ describe('PPTX table columns', () => {
const { columnWidths } = layoutTable(table, { w: 9.2, h: 5 }, true, 'slides[0]', [])
for (const w of columnWidths) expect(w).toBeCloseTo(9.2 / 20, 4)
})
+
+ it('says when columns are too narrow for their words, as the DOCX tool does', () => {
+ const headers = Array.from({ length: 40 }, (_, i) => `Region${i}`)
+ const warnings: string[] = []
+ layoutTable({ headers, rows: [headers] }, { w: 9.2, h: 5 }, true, 'slides[0].table', warnings)
+ expect(warnings).toContainEqual(
+ expect.stringContaining(
+ "slides[0].table: words in 'Region0', 'Region1', 'Region2' and 37 more column(s) are " +
+ 'wider than those columns on a slide with 40 columns'
+ )
+ )
+ })
+
+ it('says nothing when every word fits, nor about a URL left to break', () => {
+ const warnings: string[] = []
+ const url = `https://example.com/${'a'.repeat(200)}`
+ layoutTable(
+ { headers: ['Name', 'Link'], rows: [['Docs', url]] },
+ { w: 9.2, h: 5 },
+ true,
+ 'slides[0].table',
+ warnings
+ )
+ expect(warnings).toEqual([])
+ })
})
describe('clerum__generate_pptx — text stays inside its box', () => {
diff --git a/mcp-host/src/workflow/docxTable.ts b/mcp-host/src/workflow/docxTable.ts
index 8880d662b..f57febdc8 100644
--- a/mcp-host/src/workflow/docxTable.ts
+++ b/mcp-host/src/workflow/docxTable.ts
@@ -19,7 +19,7 @@ import {
} from './docxStyle'
import { CHART_FONT_FAMILY, ensureFontsReady } from './fonts'
import { inlineSpans } from './inlineMarkup'
-import { headerText } from './tableRows'
+import { columnNames, headerText } from './tableRows'
export type DocxTableLayout = 'striped' | 'minimal' | 'grid'
@@ -173,14 +173,6 @@ function fitTable(cols: ColumnExtent[]): DocxTableFit {
return plan(ATTEMPTS[ATTEMPTS.length - 1])
}
-/** Header names for a warning: the first three, then how many more. */
-function columnNames(headers: string[], columns: number[]): string {
- const names = columns.map(c => `'${headers[c]?.trim() || `column ${c + 1}`}'`)
- if (names.length === 1) return names[0]
- if (names.length <= 3) return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
- return `${names.slice(0, 3).join(', ')} and ${names.length - 3} more column(s)`
-}
-
const LANDSCAPE_TABLES = new WeakSet<Table>()
/**
diff --git a/mcp-host/src/workflow/pptxTables.ts b/mcp-host/src/workflow/pptxTables.ts
index 8d9ddc960..6d7e40a98 100644
--- a/mcp-host/src/workflow/pptxTables.ts
+++ b/mcp-host/src/workflow/pptxTables.ts
@@ -8,6 +8,7 @@
*/
import type { PptxTable } from './pptxInput'
import { lineHeightIn, textWidth, truncateToLines, wrapLines } from './pptxText'
+import { columnNames } from './tableRows'
/** pptxgenjs's default cell margins, top+bottom and left+right, in inches. */
const CELL_PAD_Y = 0.1
@@ -42,12 +43,17 @@ export function tableFontSize(columns: number, wide: boolean): number {
/**
* Widths proportional to how much text each column holds. A column's single
* longest word sets its floor, so words break inside a cell only when the
- * table cannot be made to fit otherwise.
+ * table cannot be made to fit otherwise. `words` holds each floor's word width.
*/
-function columnWidths(table: PptxTable, width: number, size: number): number[] {
+function columnWidths(
+ table: PptxTable,
+ width: number,
+ size: number
+): { widths: number[]; words: number[] } {
const n = table.headers.length
const natural: number[] = []
const floor: number[] = []
+ const words: number[] = []
for (let c = 0; c < n; c++) {
const cells = [table.headers[c], ...table.rows.map(r => r[c])]
let longestLine = 0
@@ -60,15 +66,21 @@ function columnWidths(table: PptxTable, width: number, size: number): number[] {
}
})
natural.push(Math.min(longestLine, width * 0.6) + CELL_PAD_X)
- floor.push(Math.max(Math.min(longestWord, width * 0.3) + CELL_PAD_X, MIN_COLUMN))
+ // A word longer than this is a URL or a hash, and is left to break.
+ const word = Math.min(longestWord, width * 0.3)
+ floor.push(Math.max(word + CELL_PAD_X, MIN_COLUMN))
+ words.push(word)
}
const naturalSum = natural.reduce((a, b) => a + b, 0)
- if (naturalSum <= width) return scaledTo(natural, width)
+ if (naturalSum <= width) return { widths: scaledTo(natural, width), words }
const floorSum = floor.reduce((a, b) => a + b, 0)
- if (floorSum >= width) return scaledTo(floor, width)
+ if (floorSum >= width) return { widths: scaledTo(floor, width), words }
const slack = natural.map((w, c) => Math.max(w - floor[c], 0))
const slackSum = slack.reduce((a, b) => a + b, 0)
- return floor.map((w, c) => w + (slack[c] * (width - floorSum)) / (slackSum || 1))
+ return {
+ widths: floor.map((w, c) => w + (slack[c] * (width - floorSum)) / (slackSum || 1)),
+ words,
+ }
}
/**
@@ -118,7 +130,17 @@ export function layoutTable(
): TableLayout {
const fontSize = tableFontSize(table.headers.length, wide)
// Measured at the width that will be written, so rounding to EMU cannot add a line.
- const widths = columnWidths(table, box.w, fontSize).map(w => Math.floor(w * EMU) / EMU)
+ const measured = columnWidths(table, box.w, fontSize)
+ const widths = measured.widths.map(w => Math.floor(w * EMU) / EMU)
+ const broken = widths.flatMap((w, c) => (measured.words[c] > w - CELL_PAD_X + 1e-5 ? [c] : []))
+ if (broken.length > 0) {
+ const many = broken.length > 1
+ warnings.push(
+ `${where}: words in ${columnNames(table.headers, broken)} are wider than ` +
+ `${many ? 'those columns' : 'that column'} on a slide with ${table.headers.length} ` +
+ 'columns, so they break across lines; split the table or leave out columns.'
+ )
+ }
const headerHeight = ceilEmu(rowHeight(table.headers, widths, fontSize, true))
const room = box.h - headerHeight
let cut = 0
diff --git a/mcp-host/src/workflow/tableRows.ts b/mcp-host/src/workflow/tableRows.ts
index 8e110d1f3..0912451c4 100644
--- a/mcp-host/src/workflow/tableRows.ts
+++ b/mcp-host/src/workflow/tableRows.ts
@@ -12,6 +12,14 @@ export function headerText(header: unknown): string {
return header === null || header === undefined ? '' : String(header)
}
+/** Header names for a warning: the first three, then how many more. */
+export function columnNames(headers: string[], columns: number[]): string {
+ const names = columns.map(c => `'${headers[c]?.trim() || `column ${c + 1}`}'`)
+ if (names.length === 1) return names[0]
+ if (names.length <= 3) return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
+ return `${names.slice(0, 3).join(', ')} and ${names.length - 3} more column(s)`
+}
+
/**
* `rows` as arrays of cells. A record is read by header name, exactly or
* ignoring case and surrounding space; without headers its values are taken in