Skip to content

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

TypeScript

fix(mcp-host): share the line rules of the PDF and DOCX body parsers

marcoteb11c34f72331c
diff --git a/mcp-host/src/workflow/__tests__/markdownBlocks.test.ts b/mcp-host/src/workflow/__tests__/markdownBlocks.test.ts
new file mode 100644
index 000000000..35de05e41
--- /dev/null
+++ b/mcp-host/src/workflow/__tests__/markdownBlocks.test.ts
@@ -0,0 +1,86 @@
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import * as fs from 'fs'
+import * as os from 'os'
+import * as path from 'path'
+import { INTERNAL_TOOLS } from '../internalTools'
+import {
+ ORDERED_RE,
+ isListLine,
+ isTableSeparator,
+ parseColumnAlignments,
+ startsTable,
+ stripListMarker,
+} from '../markdownBlocks'
+
+describe('delimiter rows', () => {
+ it('reads a delimiter row as GFM does', () => {
+ expect(isTableSeparator('|---|:--:|')).toBe(true)
+ expect(isTableSeparator('--- | ---')).toBe(true)
+ // One column needs a pipe on either side; a bare rule is no table.
+ expect(isTableSeparator('| --- |')).toBe(true)
+ expect(isTableSeparator('| ---')).toBe(true)
+ expect(isTableSeparator('--- |')).toBe(true)
+ expect(isTableSeparator('---')).toBe(false)
+ expect(isTableSeparator('|---||---|')).toBe(false)
+ expect(isTableSeparator('| a | --- |')).toBe(false)
+ })
+
+ it('starts a table only at a row the delimiter row matches', () => {
+ expect(startsTable(['| a |', '| --- |'], 0)).toBe(true)
+ expect(startsTable(['a | b', '--- | ---'], 0)).toBe(true)
+ expect(startsTable(['a | b', '---|'], 0)).toBe(false)
+ expect(startsTable(['a', '| ---'], 0)).toBe(false)
+ })
+
+ it('reads the alignments a delimiter row declares', () => {
+ expect(parseColumnAlignments('| :-- | :-: | --: | --- |')).toEqual([
+ 'left',
+ 'center',
+ 'right',
+ 'left',
+ ])
+ })
+
+ it('checks a row with a long run of spaces in linear time', () => {
+ for (const line of [
+ `a |${' '.repeat(100000)}x`,
+ `|${' '.repeat(100000)}-`,
+ `-${' '.repeat(100000)}|`,
+ ]) {
+ const started = performance.now()
+ isTableSeparator(line)
+ expect(performance.now() - started).toBeLessThan(200)
+ }
+ })
+})
+
+describe('list items', () => {
+ it('numbers an item with one to nine digits, as CommonMark does', () => {
+ expect(isListLine('123456789. nine digits')).toBe(true)
+ expect(isListLine('1234567890. ten digits')).toBe(false)
+ expect(ORDERED_RE.exec('12) item')?.[1]).toBe('12')
+ expect(stripListMarker(' - item')).toBe('item')
+ expect(stripListMarker('3. item')).toBe('item')
+ })
+})
+
+describe('a body with a long run of spaces after a pipe row', () => {
+ let outputDir: string
+ beforeEach(() => {
+ outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-md-blocks-'))
+ })
+ afterEach(() => {
+ fs.rmSync(outputDir, { recursive: true, force: true })
+ })
+
+ for (const name of ['clerum__generate_pdf', 'clerum__generate_docx']) {
+ it(`renders in linear time with ${name}`, async () => {
+ const tool = INTERNAL_TOOLS.find(t => t.name === name)!
+ const body = `a | b\n${' '.repeat(100000)}x`
+ const started = performance.now()
+ const result = await tool.execute({ filename: 'spaces', body }, outputDir)
+ expect(result.success, result.error).toBe(true)
+ expect(performance.now() - started).toBeLessThan(5000)
+ })
+ }
+})
diff --git a/mcp-host/src/workflow/docxMarkdown.ts b/mcp-host/src/workflow/docxMarkdown.ts
index 68ef35f9e..a5f384148 100644
--- a/mcp-host/src/workflow/docxMarkdown.ts
+++ b/mcp-host/src/workflow/docxMarkdown.ts
@@ -13,6 +13,14 @@ import {
quoteParagraphs,
withoutClosingHashes,
} from './inlineMarkup'
+import {
+ BULLET_RE,
+ ORDERED_RE,
+ indentOf,
+ isListLine,
+ splitTableRow,
+ startsTable,
+} from './markdownBlocks'
export interface DocxBodyContext {
palette: DocxPalette
@@ -22,54 +30,9 @@ export interface DocxBodyContext {
image(file: string, alt: string): Paragraph | undefined
}
-const BULLET_RE = /^[-*+]\s+/
-const ORDERED_RE = /^(\d{1,9})[.)]\s+/
const HEADING_RE = /^(#{1,6})\s+(.*)$/
const QUOTE_RE = /^>\s?/
const IMAGE_LINE_RE = /^!\[([^\]\n]*)\]\(((?:[^()\n]|\([^()\n]*\))*)\)$/
-const SEPARATOR_CELL_RE = /^:?-+:?$/
-
-function indentOf(line: string): number {
- const m = /^[ \t]*/.exec(line)
- return m ? m[0].replace(/\t/g, ' ').length : 0
-}
-
-function isListLine(line: string): boolean {
- const t = line.trimStart()
- return BULLET_RE.test(t) || ORDERED_RE.test(t)
-}
-
-/** Cells of a pipe-table row. `\|` is a literal pipe inside a cell. */
-function splitTableRow(line: string): string[] {
- let t = line.trim()
- if (t.startsWith('|')) t = t.slice(1)
- if (t.endsWith('|') && !t.endsWith('\\|')) t = t.slice(0, -1)
- return t.split(/(?<!\\)\|/).map(c => c.trim().replace(/\\\|/g, '|'))
-}
-
-/**
- * A delimiter row such as `|---|:--:|`. Checked cell by cell: the one-regex
- * form backtracks quadratically over a long run of spaces.
- */
-function isTableSeparator(line: string): boolean {
- if (!line.includes('-') || !line.includes('|')) return false
- const cells = splitTableRow(line)
- return cells.every(cell => SEPARATOR_CELL_RE.test(cell))
-}
-
-/**
- * A GFM table starts at a row followed by a separator. Without outer pipes the
- * header must have as many cells as the separator, which is what tells a table
- * apart from a sentence that happens to contain a pipe.
- */
-function startsTable(lines: string[], i: number): boolean {
- const line = lines[i]
- const next = lines[i + 1]
- if (next === undefined || !line.includes('|') || !isTableSeparator(next)) return false
- return (
- line.trimStart().startsWith('|') || splitTableRow(line).length === splitTableRow(next).length
- )
-}
function blankParagraph(): Paragraph {
return new Paragraph({ children: [new TextRun({ text: '' })] })
diff --git a/mcp-host/src/workflow/markdownBlocks.ts b/mcp-host/src/workflow/markdownBlocks.ts
new file mode 100644
index 000000000..5e1a1028c
--- /dev/null
+++ b/mcp-host/src/workflow/markdownBlocks.ts
@@ -0,0 +1,72 @@
+/**
+ * Line-level markdown rules the PDF and DOCX body parsers share, so both read
+ * a list item, a table row and a delimiter row the same way. Every rule runs
+ * on text a model wrote, so none may backtrack across the line.
+ */
+
+export type ColumnAlignment = 'left' | 'center' | 'right'
+
+export const BULLET_RE = /^[-*+]\s+/
+/** CommonMark numbers a list item with one to nine digits. */
+export const ORDERED_RE = /^(\d{1,9})[.)]\s+/
+const SEPARATOR_CELL_RE = /^:?-+:?$/
+
+/** Indent width of a list line, used to decide its nesting depth. */
+export function indentOf(line: string): number {
+ const m = /^[ \t]*/.exec(line)
+ // A tab counts as one level, matching how models write nested lists.
+ return m ? m[0].replace(/\t/g, ' ').length : 0
+}
+
+export function isListLine(line: string): boolean {
+ const t = line.trimStart()
+ return BULLET_RE.test(t) || ORDERED_RE.test(t)
+}
+
+/** The item's text without its bullet or number. */
+export function stripListMarker(line: string): string {
+ return line.trimStart().replace(BULLET_RE, '').replace(ORDERED_RE, '')
+}
+
+/** Cells of a pipe-table row. `\|` is a literal pipe inside a cell. */
+export function splitTableRow(line: string): string[] {
+ let t = line.trim()
+ if (t.startsWith('|')) t = t.slice(1)
+ if (t.endsWith('|') && !t.endsWith('\\|')) t = t.slice(0, -1)
+ return t.split(/(?<!\\)\|/).map(c => c.trim().replace(/\\\|/g, '|'))
+}
+
+/**
+ * A GFM delimiter row such as `|---|:--:|`. It needs a pipe, so a lone `---`
+ * stays a rule. Checked cell by cell: one pattern over the whole row
+ * backtracks quadratically over a long run of spaces.
+ */
+export function isTableSeparator(line: string): boolean {
+ if (!line.includes('-') || !line.includes('|')) return false
+ return splitTableRow(line).every(cell => SEPARATOR_CELL_RE.test(cell))
+}
+
+/**
+ * Whether a GFM table starts at line `i`: a row followed by a delimiter row.
+ * Without outer pipes the header must have as many cells as the delimiter row,
+ * which tells a table from a sentence that happens to hold a pipe.
+ */
+export function startsTable(lines: string[], i: number): boolean {
+ const line = lines[i]
+ const next = lines[i + 1]
+ if (next === undefined || !line.includes('|') || !isTableSeparator(next)) return false
+ return (
+ line.trimStart().startsWith('|') || splitTableRow(line).length === splitTableRow(next).length
+ )
+}
+
+/** The alignments a GFM delimiter row declares (`:---`, `---:`, `:---:`). */
+export function parseColumnAlignments(separator: string): ColumnAlignment[] {
+ return splitTableRow(separator).map(cell => {
+ const left = cell.startsWith(':')
+ const right = cell.endsWith(':')
+ if (left && right) return 'center'
+ if (right) return 'right'
+ return 'left'
+ })
+}
diff --git a/mcp-host/src/workflow/pdfGenerator.ts b/mcp-host/src/workflow/pdfGenerator.ts
index d5820b6b1..211669dda 100644
--- a/mcp-host/src/workflow/pdfGenerator.ts
+++ b/mcp-host/src/workflow/pdfGenerator.ts
@@ -29,6 +29,16 @@ import {
quoteParagraphs,
withoutClosingHashes,
} from './inlineMarkup'
+import {
+ type ColumnAlignment,
+ ORDERED_RE,
+ indentOf,
+ isListLine,
+ parseColumnAlignments,
+ splitTableRow,
+ startsTable,
+ stripListMarker,
+} from './markdownBlocks'
import {
BODY_FONT_SIZE,
MIN_BOTTOM_MARGIN,
@@ -277,38 +287,6 @@ function plainText(parsed: ContentText): string {
.join('')
}
-/** A GFM delimiter row. One column needs its outer pipes, or it is only a rule. */
-function isTableSeparator(line: string): boolean {
- return (
- /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)+\|?\s*$/.test(line) ||
- /^\s*\|\s*:?-+:?\s*\|\s*$/.test(line)
- )
-}
-
-/** Cells of a pipe-table row. `\|` is a literal pipe inside a cell. */
-function splitTableRow(line: string): string[] {
- let trimmed = line.trim()
- if (trimmed.startsWith('|')) trimmed = trimmed.slice(1)
- if (trimmed.endsWith('|') && !trimmed.endsWith('\\|')) trimmed = trimmed.slice(0, -1)
- return trimmed.split(/(?<!\\)\|/).map(c => c.trim().replace(/\\\|/g, '|'))
-}
-
-/**
- * Whether a GFM table starts at line `i`. GFM makes the outer pipes optional
- * and some models leave them out, so a header without them counts when it has
- * as many cells as the separator under it.
- */
-function isPipeTableStart(lines: string[], i: number): boolean {
- const line = lines[i]
- const next = lines[i + 1]
- if (next === undefined || !line.includes('|') || !isTableSeparator(next)) return false
- return (
- line.trimStart().startsWith('|') || splitTableRow(line).length === splitTableRow(next).length
- )
-}
-
-type ColumnAlignment = 'left' | 'center' | 'right'
-
interface PdfTableRequest {
headers: string[]
rows: string[][]
@@ -427,39 +405,6 @@ function pdfTableLayout(name: 'striped' | 'minimal' | 'grid', palette: PdfPalett
}
}
-/** Read the alignments a GFM separator row declares (`:---`, `---:`, `:---:`). */
-function parseColumnAlignments(separator: string): ColumnAlignment[] {
- return splitTableRow(separator).map(cell => {
- const c = cell.trim()
- const left = c.startsWith(':')
- const right = c.endsWith(':')
- if (left && right) return 'center'
- if (right) return 'right'
- return 'left'
- })
-}
-
-/** Indent width of a list line, used to decide its nesting depth. */
-function indentOf(line: string): number {
- const m = /^[ \t]*/.exec(line)
- if (!m) return 0
- // A tab counts as one level, matching how models write nested lists.
- return m[0].replace(/\t/g, ' ').length
-}
-
-const BULLET_RE = /^[-*+]\s+/
-const ORDERED_RE = /^\d+[.)]\s+/
-
-function isListLine(line: string): boolean {
- const t = line.trimStart()
- return BULLET_RE.test(t) || ORDERED_RE.test(t)
-}
-
-function stripListMarker(line: string): string {
- const t = line.trimStart()
- return t.replace(BULLET_RE, '').replace(ORDERED_RE, '')
-}
-
/**
* Build one list level, consuming the lines that belong to it. A line indented
* further than the level's own indent starts a nested list attached to the item
@@ -594,7 +539,7 @@ function bodyToContent(body: string, palette: PdfPalette, env: PdfBodyEnv): Cont
// GFM pipe table: header line, then separator, then the rows up to the
// first line without a pipe.
- if (isPipeTableStart(lines, i)) {
+ if (startsTable(lines, i)) {
const headers = splitTableRow(line)
const alignments = parseColumnAlignments(lines[i + 1])
const rows: string[][] = []