Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): decide a paragraph's direction one way in every format
marcotebaccf72b31df2
diff --git a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts b/mcp-host/src/__tests__/artifacts/shownNumbers.test.tsindex bb753b061..be87dc1cc 100644--- a/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts+++ b/mcp-host/src/__tests__/artifacts/shownNumbers.test.ts@@ -114,8 +114,8 @@ describe('withoutSeparators', () => {it.each([['1,234.5', '12345'],['1.234,5', '12345'],- ['1 234,5', '12345'],- ['1 234,5', '12345'],+ ['1\u202f234,5', '12345'],+ ['1\u00a0234,5', '12345'],["1'234.5", '12345'],['1’234.5', '12345'],['3,14,15,92,653', '3141592653'],diff --git a/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts b/mcp-host/src/workflow/__tests__/internalTools.docxText.test.tsindex 9a26af58f..ac590f908 100644--- a/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.docxText.test.ts@@ -7,8 +7,8 @@ import * as fs from 'fs'import * as os from 'os'import * as path from 'path'import { inlineRuns } from '../docxInline'-import { documentEastAsianScript } from '../docxScript'import { INTERNAL_TOOLS } from '../internalTools'+import { documentEastAsianScript } from '../textScript'import type { InternalToolResult } from '../types'import { zipEntryText } from './support/zipEntries'diff --git a/mcp-host/src/workflow/__tests__/textDirection.test.ts b/mcp-host/src/workflow/__tests__/textDirection.test.tsnew file mode 100644index 000000000..58fa3bfcf--- /dev/null+++ b/mcp-host/src/workflow/__tests__/textDirection.test.ts@@ -0,0 +1,72 @@+// One answer to "which way does this paragraph read" for the PDF, Word and+// PowerPoint, and the PDF's mirror of fontkit's right-to-left scripts kept+// equal to fontkit's own table.+import { describe, expect, it } from 'vitest'+import * as fs from 'fs'+import { docxDirection } from '../docxScript'+import { resolveParagraph } from '../pdfBidi'+import { FONTKIT_RTL_SCRIPTS } from '../pdfText'+import { isRtlText } from '../textDirection'++/** OpenType script tags fontkit lists as right to left, and their Unicode names. */+const TAG_SCRIPTS: Record<string, string> = {+ arab: 'Arabic',+ hebr: 'Hebrew',+ syrc: 'Syriac',+ thaa: 'Thaana',+ cprt: 'Cypriot',+ khar: 'Kharoshthi',+ phnx: 'Phoenician',+ 'nko ': 'Nko',+ lydi: 'Lydian',+ avst: 'Avestan',+ armi: 'Imperial_Aramaic',+ phli: 'Inscriptional_Pahlavi',+ prti: 'Inscriptional_Parthian',+ sarb: 'Old_South_Arabian',+ orkh: 'Old_Turkic',+ samr: 'Samaritan',+ mand: 'Mandaic',+ merc: 'Meroitic_Cursive',+ mero: 'Meroitic_Hieroglyphs',+ mani: 'Manichaean',+ mend: 'Mende_Kikakui',+ nbat: 'Nabataean',+ narb: 'Old_North_Arabian',+ palm: 'Palmyrene',+ phlp: 'Psalter_Pahlavi',+}++describe('FONTKIT_RTL_SCRIPTS', () => {+ it("is fontkit's own table of right-to-left scripts", () => {+ const source = fs.readFileSync(require.resolve('@foliojs-fork/fontkit'), 'utf8')+ const table = /var RTL = \{([\s\S]*?)\};/.exec(source)+ expect(table, "fontkit's RTL table").not.toBeNull()+ const tags = [...table![1].matchAll(/^\s*(?:'([^']+)'|(\w+)):\s*true/gm)].map(m => m[1] ?? m[2])+ expect(tags.length).toBeGreaterThan(20)+ expect(tags.filter(tag => !(tag in TAG_SCRIPTS))).toEqual([])+ expect(new Set(tags.map(tag => TAG_SCRIPTS[tag]))).toEqual(new Set(FONTKIT_RTL_SCRIPTS))+ })+})++describe('the direction of a paragraph', () => {+ /** The direction the PDF lays a paragraph out in, which drops LRM and RLM as invisible. */+ const pdfReadsRtl = (text: string) =>+ resolveParagraph([...text.replace(/[\u200e\u200f]/g, '')].map(ch => ch.codePointAt(0)!)).base === 1++ it.each([+ ['a roman numeral before Hebrew', 'Ⅻ שלום', false],+ ['Devanagari digits before Arabic', '१२ مرحبا', false],+ ['European digits before Hebrew', '2026 שנה', true],+ ['punctuation before Arabic', '(«مرحبا»)', true],+ ['a historic right-to-left script', '𐤀𐤁 abc', true],+ ['a right-to-left mark before Latin', '\u200fhello', false],+ ['a left-to-right mark before Hebrew', '\u200eשלום', true],+ ['Latin', 'Quarterly report', false],+ ['nothing strong', '2026 — 42%', false],+ ])('%s is the same in the PDF, Word and PowerPoint', (_label, text, rtl) => {+ expect(isRtlText(text)).toBe(rtl)+ expect(pdfReadsRtl(text)).toBe(rtl)+ expect(docxDirection(text)).toEqual(rtl ? { bidirectional: true } : {})+ })+})diff --git a/mcp-host/src/workflow/chartData.ts b/mcp-host/src/workflow/chartData.tsindex a13295ff9..6e71ef40c 100644--- a/mcp-host/src/workflow/chartData.ts+++ b/mcp-host/src/workflow/chartData.ts@@ -10,8 +10,8 @@* field and the fix. Warnings travel back in the tool result so the agent can* correct itself on the next call.*/+import { currencyPrefix, currencySuffix } from './numberText'import { own } from './ownEntry'-import { currencyPrefix, currencySuffix } from './xlsxCells'/** Types whose points are {x,y} pairs rather than a value per label. */const XY_TYPES = new Set(['scatter', 'bubble'])diff --git a/mcp-host/src/workflow/docxGenerator.ts b/mcp-host/src/workflow/docxGenerator.tsindex 459cfa44a..f3b3bc3c5 100644--- a/mcp-host/src/workflow/docxGenerator.ts+++ b/mcp-host/src/workflow/docxGenerator.ts@@ -26,13 +26,7 @@ import { EMPTY_DOCUMENT_NOTE, printedBranding } from './documentChrome'import { type DocxImagePlacement, docxImageParagraph } from './docxImages'import { textRuns } from './docxInline'import { bodyHasText, bodyToDocxChildren } from './docxMarkdown'-import {- DOCX_COMPLEX_FONT,- DOCX_LATIN_FONT,- documentEastAsianScript,- docxDirection,- eastAsianScript,-} from './docxScript'+import { DOCX_COMPLEX_FONT, DOCX_LATIN_FONT, docxDirection } from './docxScript'import {DOCX_CONTENT_WIDTH_TWIPS,DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS,@@ -44,6 +38,7 @@ import { buildDocxTable, docxSections } from './docxTable'import { htmlToPlainText } from './inlineMarkup'import { choose } from './ownEntry'import { normalizeTableRows } from './tableRows'+import { documentEastAsianScript, eastAsianScript } from './textScript'import type { InternalToolResult } from './types'/**diff --git a/mcp-host/src/workflow/docxInline.ts b/mcp-host/src/workflow/docxInline.tsindex b210772bf..1e59902de 100644--- a/mcp-host/src/workflow/docxInline.ts+++ b/mcp-host/src/workflow/docxInline.ts@@ -2,14 +2,9 @@* Inline markdown, and the bits of HTML models mix into it, as Word runs.*/import { ExternalHyperlink, type IFontAttributesProperties, type IRunOptions, TextRun } from 'docx'-import {- type EastAsianScript,- bidiLanguage,- eastAsianScript,- isRtlText,- scriptSegments,-} from './docxScript'import { htmlToMarkdownInline, markdownSpans } from './inlineMarkup'+import { bidiLanguage, isRtlText, scriptSegments } from './textDirection'+import { type EastAsianScript, eastAsianScript } from './textScript'export type DocxInline = TextRun | ExternalHyperlinkdiff --git a/mcp-host/src/workflow/docxMarkdown.ts b/mcp-host/src/workflow/docxMarkdown.tsindex 6654f8d6a..0eefb3abf 100644--- a/mcp-host/src/workflow/docxMarkdown.ts+++ b/mcp-host/src/workflow/docxMarkdown.ts@@ -3,7 +3,7 @@*/import { BorderStyle, HeadingLevel, Paragraph, ShadingType, type Table, TextRun } from 'docx'import { inlineRuns, textRuns } from './docxInline'-import { docxDirection, isRtlText } from './docxScript'+import { docxDirection } from './docxScript'import { DOCX_LIST_LEVELS, type DocxListNumbering, type DocxPalette, docxHex } from './docxStyle'import { buildDocxTable } from './docxTable'import { imageTarget } from './inlineMarkup'@@ -15,6 +15,7 @@ import {isListLine,readBlock,} from './markdownBlocks'+import { isRtlText } from './textDirection'export interface DocxBodyContext {palette: DocxPalettediff --git a/mcp-host/src/workflow/docxScript.ts b/mcp-host/src/workflow/docxScript.tsindex 251daa120..460fd5c27 100644--- a/mcp-host/src/workflow/docxScript.ts+++ b/mcp-host/src/workflow/docxScript.ts@@ -8,6 +8,7 @@* names only a Latin face leaves Word to substitute, which draws boxes where* the substitute lacks the glyphs.*/+import { isRtlText } from './textDirection'/** Latin face of the body text. */export const DOCX_LATIN_FONT = 'Calibri'@@ -19,149 +20,7 @@ export const DOCX_LATIN_FONT = 'Calibri'*/export const DOCX_COMPLEX_FONT = 'Arial'-const HEBREW_LETTER = /\p{Script=Hebrew}/u-const ARABIC_LETTER =- /[\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}\p{Script=Hanifi_Rohingya}]/u-const RTL_LETTER = new RegExp(`${HEBREW_LETTER.source}|${ARABIC_LETTER.source}`, 'u')--/**- * A right-to-left stretch of one script: its letters with the digits, spaces- * and punctuation between them and the marks on the last one, but not the- * neutrals after it, which take the paragraph's direction. Hebrew and Arabic- * stay in separate runs because Word picks the face by the run's language and- * draws boxes for Arabic tagged as Hebrew. The marks are matched once, after- * the repetition, so no stretch of them can be split two ways.- */-function span(letter: RegExp): string {- return `${letter.source}(?:[^\\p{L}]*${letter.source})*\\p{M}*`-}-const RTL_SPAN = new RegExp(`${span(HEBREW_LETTER)}|${span(ARABIC_LETTER)}`, 'gu')--/** Whether `text` holds a letter of a right-to-left script anywhere. */-export function hasRtlLetter(text: string): boolean {- return RTL_LETTER.test(text)-}--/** Whether `text` reads right to left: its first letter, as the bidi algorithm decides, is RTL. */-export function isRtlText(text: string): boolean {- const first = /\p{L}/u.exec(text)?.[0]- return first !== undefined && RTL_LETTER.test(first)-}-/** Paragraph options that set the paragraph's direction from its text. */export function docxDirection(text: string): { bidirectional?: true } {return isRtlText(text) ? { bidirectional: true } : {}}--export interface ScriptSegment {- text: string- rtl: boolean-}--const LETTER = /\p{L}/u--/**- * `text` cut into left-to-right and right-to-left stretches, in logical order.- * In a right-to-left paragraph, digits and punctuation with no letter of their- * own join the right-to-left stretch beside them: in a left-to-right run Word- * sets them apart from the words they belong to ("المبيعات2026", "מספר.42").- */-export function scriptSegments(text: string, rtlParagraph = false): ScriptSegment[] {- if (!RTL_LETTER.test(text)) return [{ text, rtl: rtlParagraph && !LETTER.test(text) }]- const out: ScriptSegment[] = []- let at = 0- RTL_SPAN.lastIndex = 0- for (let m = RTL_SPAN.exec(text); m; m = RTL_SPAN.exec(text)) {- if (m.index > at) out.push({ text: text.slice(at, m.index), rtl: false })- out.push({ text: m[0], rtl: true })- at = m.index + m[0].length- }- if (at < text.length) out.push({ text: text.slice(at), rtl: false })- if (!rtlParagraph) return out- const merged: Array<ScriptSegment & { neutral: boolean }> = []- for (const segment of out) {- const neutral = !segment.rtl && !LETTER.test(segment.text)- const rtl = segment.rtl || neutral- const last = merged[merged.length - 1]- if (last && last.rtl && rtl && (neutral || last.neutral)) {- last.text += segment.text- last.neutral = last.neutral && neutral- } else {- merged.push({ text: segment.text, rtl, neutral })- }- }- return merged.map(({ text: t, rtl }) => ({ text: t, rtl }))-}--/** Language tag for a right-to-left run of one script. */-export function bidiLanguage(text: string): string {- return HEBREW_LETTER.test(text) ? 'he-IL' : 'ar-SA'-}--export interface EastAsianScript {- font: string- lang: string-}--// Each face ships with Windows and with Word for Mac.-const JAPANESE: EastAsianScript = { font: 'Yu Gothic', lang: 'ja-JP' }-const KOREAN: EastAsianScript = { font: 'Malgun Gothic', lang: 'ko-KR' }-const CHINESE: EastAsianScript = { font: 'Microsoft YaHei', lang: 'zh-CN' }-const TRADITIONAL_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-TW' }-const HONG_KONG_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-HK' }--// Frequent characters written one way in Simplified Chinese and another in-// Traditional, the two lists in the same order; neither form is Japanese kanji-// that a line without kana could hold for both.-const SIMPLIFIED_ONLY =- /[这们个说会国学时为与来对发从实动经过还进现么没样关长门问题应见业开东头车书买卖电话语让认识请读写观点体产务员达区级际华结统计设资价变报农习乐网码帮]/gu-const TRADITIONAL_ONLY =- /[這們個說會國學時為與來對發從實動經過還進現麼沒樣關長門問題應見業開東頭車書買賣電話語讓認識請讀寫觀點體產務員達區級際華結統計設資價變報農習樂網碼幫]/gu-/** Characters of written Cantonese, which Hong Kong text holds. */-const CANTONESE = /[嘅咗唔喺冇嚟啲佢哋嘢嗰]/u--const CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/gu--/**- * The East Asian face and language a block names for itself, chosen from the- * whole block so Han characters in Japanese text get the Japanese face:- * Japanese with kana, Korean with Hangul. Han characters alone may be any of- * the three, so such a block takes the document's (documentEastAsianScript).- */-export function eastAsianScript(text: string): EastAsianScript | undefined {- if (/[\p{Script=Hiragana}\p{Script=Katakana}]/u.test(text)) return JAPANESE- if (/\p{Script=Hangul}/u.test(text)) return KOREAN- return undefined-}--/**- * The East Asian script of a whole document: that of the lines holding most of- * its CJK characters, where a line of Han characters alone counts as Chinese.- * Chinese is Traditional when its lines hold more characters only Traditional- * writes than ones only Simplified writes, and Hong Kong's when they also hold- * written Cantonese; otherwise Simplified, as before.- */-export function documentEastAsianScript(text: string): EastAsianScript | undefined {- const counts = new Map<EastAsianScript, number>()- let traditional = 0- let simplified = 0- let cantonese = false- for (const line of text.split('\n')) {- const cjk = line.match(CJK_CHAR)?.length ?? 0- if (cjk === 0) continue- const script = eastAsianScript(line) ?? CHINESE- counts.set(script, (counts.get(script) ?? 0) + cjk)- if (script !== CHINESE) continue- traditional += line.match(TRADITIONAL_ONLY)?.length ?? 0- simplified += line.match(SIMPLIFIED_ONLY)?.length ?? 0- cantonese ||= CANTONESE.test(line)- }- let best: EastAsianScript | undefined- for (const script of [JAPANESE, KOREAN, CHINESE]) {- if ((counts.get(script) ?? 0) > (best ? (counts.get(best) ?? 0) : 0)) best = script- }- if (best === CHINESE && traditional > simplified) {- return cantonese ? HONG_KONG_CHINESE : TRADITIONAL_CHINESE- }- return best-}diff --git a/mcp-host/src/workflow/docxTable.ts b/mcp-host/src/workflow/docxTable.tsindex 96842a30b..cc3e2271c 100644--- a/mcp-host/src/workflow/docxTable.ts+++ b/mcp-host/src/workflow/docxTable.ts@@ -9,7 +9,7 @@ import {WidthType,} from 'docx'import { inlineRuns } from './docxInline'-import { docxDirection, isRtlText } from './docxScript'+import { docxDirection } from './docxScript'import {DOCX_CONTENT_WIDTH_TWIPS,DOCX_LANDSCAPE_CONTENT_WIDTH_TWIPS,@@ -19,6 +19,7 @@ import {import { CJK_WIDE_CHAR, bodyFaceEm } from './fonts'import { inlineSpans } from './inlineMarkup'import { columnNames, headerText } from './tableRows'+import { isRtlText } from './textDirection'export type DocxTableLayout = 'striped' | 'minimal' | 'grid'diff --git a/mcp-host/src/workflow/numberText.ts b/mcp-host/src/workflow/numberText.tsnew file mode 100644index 000000000..4ef9aec76--- /dev/null+++ b/mcp-host/src/workflow/numberText.ts@@ -0,0 +1,60 @@+/**+ * Money written as text: the currency symbols and codes a number may carry,+ * read the same by the sheet writer (xlsxCells) and the chart reader+ * (chartData).+ */++/** Symbol shown for each currency code; codes that share "$" keep a prefix. */+export const CURRENCY_SYMBOLS: Record<string, string> = {+ USD: '$',+ EUR: '€',+ GBP: '£',+ JPY: '¥',+ CNY: '¥',+ INR: '₹',+ KRW: '₩',+ BRL: 'R$',+ MXN: 'MX$',+ CAD: 'CA$',+ AUD: 'A$',+ CHF: 'CHF ',+ COP: 'COP ',+ CLP: 'CLP ',+ ARS: 'ARS ',+ PEN: 'S/ ',+}++/** Symbols a price may be written with, longest first so "R$" wins over "$". */+const WRITTEN_SYMBOLS: Array<[string, string]> = [+ ['US$', '$'],+ ['MX$', 'MX$'],+ ['CA$', 'CA$'],+ ['R$', 'R$'],+ ['A$', 'A$'],+ ['$', '$'],+ ['€', '€'],+ ['£', '£'],+ ['¥', '¥'],+ ['₹', '₹'],+ ['₩', '₩'],+]++/** The currency symbol `s` opens with and the length of what spells it. */+export function currencyPrefix(s: string): [string, number] | undefined {+ for (const [written, symbol] of WRITTEN_SYMBOLS) {+ if (s.startsWith(written)) return [symbol, written.length]+ }+ const code = /^([A-Z]{3})\s?(?=[\d.-])/.exec(s)+ if (code && CURRENCY_SYMBOLS[code[1]]) return [CURRENCY_SYMBOLS[code[1]], code[0].length]+ return undefined+}++/** The currency symbol `s` closes with and the length of what spells it. */+export function currencySuffix(s: string): [string, number] | undefined {+ for (const [written, symbol] of WRITTEN_SYMBOLS) {+ if (s.endsWith(written)) return [symbol, written.length]+ }+ const code = /(?<=\d)\s?([A-Z]{3})$/.exec(s)+ if (code && CURRENCY_SYMBOLS[code[1]]) return [CURRENCY_SYMBOLS[code[1]], code[0].length]+ return undefined+}diff --git a/mcp-host/src/workflow/pdfBidi.ts b/mcp-host/src/workflow/pdfBidi.tsindex 2c1e960e6..e838e87ce 100644--- a/mcp-host/src/workflow/pdfBidi.ts+++ b/mcp-host/src/workflow/pdfBidi.ts@@ -10,120 +10,9 @@* implicit levels and line reordering. Explicit embeddings and isolates are not* supported.*/+import { type BidiClass, bidiClass, hasRtl, isRtlScript, strongDirection } from './textDirection'-export type BidiClass =- | 'L'- | 'R'- | 'AL'- | 'EN'- | 'AN'- | 'ES'- | 'ET'- | 'CS'- | 'NSM'- | 'BN'- | 'WS'- | 'ON'--const MARK = /[\p{Mn}\p{Me}]/u-const LETTER = /[\p{L}\p{Mc}\p{Nd}\p{Nl}]/u--function inRanges(cp: number, ranges: ReadonlyArray<readonly [number, number]>): boolean {- return ranges.some(([from, to]) => cp >= from && cp <= to)-}--/** Hebrew, NKo, Samaritan, Mandaic and the historic right-to-left blocks. */-const STRONG_R: ReadonlyArray<readonly [number, number]> = [- [0x0590, 0x05ff],- [0x07c0, 0x085f],- [0xfb1d, 0xfb4f],- [0x10800, 0x10fff],- [0x1e800, 0x1edff],- [0x200f, 0x200f],-]--/** Arabic, Syriac and Thaana, and their presentation forms. */-const STRONG_AL: ReadonlyArray<readonly [number, number]> = [- [0x0600, 0x07bf],- [0x0860, 0x08ff],- [0xfb50, 0xfdff],- [0xfe70, 0xfefe],- [0x1ee00, 0x1eeff],-]--const ARABIC_DIGITS: ReadonlyArray<readonly [number, number]> = [- [0x0600, 0x0605],- [0x0660, 0x0669],- [0x066b, 0x066c],- [0x08e2, 0x08e2],-]--const EUROPEAN_DIGITS: ReadonlyArray<readonly [number, number]> = [- [0x30, 0x39],- [0x06f0, 0x06f9],- [0xb2, 0xb3],- [0xb9, 0xb9],- [0x2070, 0x2079],- [0x2080, 0x2089],- [0xff10, 0xff19],-]--const TERMINATORS: ReadonlyArray<readonly [number, number]> = [- [0x23, 0x25],- [0xa2, 0xa5],- [0xb0, 0xb1],- [0x066a, 0x066a],- [0x2030, 0x2034],- [0x20a0, 0x20cf],- [0x2213, 0x2213],-]--/** Plus and minus signs, U+2212 MINUS SIGN among them. */-const NUMBER_SIGNS = new Set([- 0x2b, 0x2d, 0x207a, 0x207b, 0x208a, 0x208b, 0x2212, 0xfb29, 0xfe62, 0xfe63, 0xff0b, 0xff0d,-])--/** Zero-width joiners and other characters the rules pass over (X9). */-const BOUNDARY_NEUTRALS: ReadonlyArray<readonly [number, number]> = [- [0xad, 0xad],- [0x180e, 0x180e],- [0x200b, 0x200d],- [0x2060, 0x2064],- [0xfeff, 0xfeff],-]--const SEPARATORS = new Set([0x2c, 0x2e, 0x2f, 0x3a, 0xa0, 0x060c, 0x202f, 0x2044, 0xfe50, 0xfe52])--export function bidiClass(cp: number): BidiClass {- const ch = String.fromCodePoint(cp)- if (MARK.test(ch)) return 'NSM'- if (inRanges(cp, EUROPEAN_DIGITS)) return 'EN'- if (inRanges(cp, ARABIC_DIGITS)) return 'AN'- if (inRanges(cp, TERMINATORS)) return 'ET'- if (SEPARATORS.has(cp)) return 'CS'- if (NUMBER_SIGNS.has(cp)) return 'ES'- if (inRanges(cp, BOUNDARY_NEUTRALS)) return 'BN'- if (inRanges(cp, STRONG_AL)) return 'AL'- if (inRanges(cp, STRONG_R)) return 'R'- if (cp === 0x20 || cp === 0x09 || cp === 0x0c || (cp >= 0x2000 && cp <= 0x200a) || cp === 0x3000)- return 'WS'- if (cp === 0x200e || LETTER.test(ch)) return 'L'- return 'ON'-}--/** Whether a code point belongs to a right-to-left script, marks included. */-export function isRtlScript(cp: number): boolean {- return inRanges(cp, STRONG_R) || inRanges(cp, STRONG_AL)-}--/** Whether `text` holds anything a right-to-left paragraph would reorder. */-export function hasRtl(text: string): boolean {- for (const ch of text) {- const cls = bidiClass(ch.codePointAt(0)!)- if (cls === 'R' || cls === 'AL' || cls === 'AN') return true- }- return false-}+export { type BidiClass, bidiClass, hasRtl, isRtlScript }export interface BidiParagraph {/** 0 for a left-to-right paragraph, 1 for a right-to-left one. */@@ -133,12 +22,6 @@ export interface BidiParagraph {classes: BidiClass[]}-function strongDirection(cls: BidiClass): 'L' | 'R' | undefined {- if (cls === 'L') return 'L'- if (cls === 'R' || cls === 'AL') return 'R'- return undefined-}-/** Opening brackets and their closing partners, as BD16 pairs them. */const BRACKETS: Record<number, number> = { 0x28: 0x29, 0x5b: 0x5d, 0x7b: 0x7d }diff --git a/mcp-host/src/workflow/pdfGenerator.ts b/mcp-host/src/workflow/pdfGenerator.tsindex 9a3767d97..93d0bc865 100644--- a/mcp-host/src/workflow/pdfGenerator.ts+++ b/mcp-host/src/workflow/pdfGenerator.ts@@ -18,7 +18,6 @@ import {longestFitting,printedBranding,} from './documentChrome'-import { documentEastAsianScript } from './docxScript'import {MAX_DECODE_PIXELS,fitImageSize,@@ -40,6 +39,7 @@ import { PDF_PALETTES, statusColorFromPalette } from './pdfPalette'import { BODY_FONT_SIZE, MIN_BOTTOM_MARGIN, PORTRAIT, type UnitMeasure } from './pdfTables'import { LINE_FILL, PdfTypesetter } from './pdfText'import { headerText, normalizeTableRows } from './tableRows'+import { documentEastAsianScript } from './textScript'import { type InternalToolResult } from './types'const PdfPrinter = require('pdfmake')diff --git a/mcp-host/src/workflow/pdfText.ts b/mcp-host/src/workflow/pdfText.tsindex 7c3be7095..3e364d142 100644--- a/mcp-host/src/workflow/pdfText.ts+++ b/mcp-host/src/workflow/pdfText.ts@@ -72,14 +72,48 @@ const MARK = /^[\p{Mn}\p{Me}]$/u/** Neither direction of its own: fontkit reads a run's direction from its first other character. */const NEUTRAL_SCRIPT = /^[\p{Script=Common}\p{Script=Inherited}]/u-/** The scripts fontkit sets right to left. */-const RTL_SCRIPT =- /^[\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Cypriot}\p{Script=Kharoshthi}\p{Script=Phoenician}\p{Script=Nko}\p{Script=Lydian}\p{Script=Avestan}\p{Script=Imperial_Aramaic}\p{Script=Inscriptional_Pahlavi}\p{Script=Inscriptional_Parthian}\p{Script=Old_South_Arabian}\p{Script=Old_Turkic}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Meroitic_Cursive}\p{Script=Meroitic_Hieroglyphs}\p{Script=Manichaean}\p{Script=Mende_Kikakui}\p{Script=Nabataean}\p{Script=Old_North_Arabian}\p{Script=Palmyrene}\p{Script=Psalter_Pahlavi}]/u+/**+ * The scripts fontkit sets right to left, as Unicode script names: its own+ * table of OpenType script tags, mirrored so a run is split where fontkit+ * would turn. Which way a paragraph reads is decided in textDirection.ts.+ */+export const FONTKIT_RTL_SCRIPTS = [+ 'Arabic',+ 'Hebrew',+ 'Syriac',+ 'Thaana',+ 'Cypriot',+ 'Kharoshthi',+ 'Phoenician',+ 'Nko',+ 'Lydian',+ 'Avestan',+ 'Imperial_Aramaic',+ 'Inscriptional_Pahlavi',+ 'Inscriptional_Parthian',+ 'Old_South_Arabian',+ 'Old_Turkic',+ 'Samaritan',+ 'Mandaic',+ 'Meroitic_Cursive',+ 'Meroitic_Hieroglyphs',+ 'Manichaean',+ 'Mende_Kikakui',+ 'Nabataean',+ 'Old_North_Arabian',+ 'Palmyrene',+ 'Psalter_Pahlavi',+] as const++const FONTKIT_RTL = new RegExp(+ `^[${FONTKIT_RTL_SCRIPTS.map(script => `\\p{Script=${script}}`).join('')}]`,+ 'u'+)/** The direction fontkit gives a run that starts with `ch`, if `ch` decides it. */function scriptDirection(ch: string): 'L' | 'R' | undefined {if (NEUTRAL_SCRIPT.test(ch)) return undefined- return RTL_SCRIPT.test(ch) ? 'R' : 'L'+ return FONTKIT_RTL.test(ch) ? 'R' : 'L'}/** A glyph of spaces only; a tab in code is set as four. */diff --git a/mcp-host/src/workflow/pptxDeck.ts b/mcp-host/src/workflow/pptxDeck.tsindex beef079b2..95c051caa 100644--- a/mcp-host/src/workflow/pptxDeck.ts+++ b/mcp-host/src/workflow/pptxDeck.ts@@ -7,7 +7,6 @@* values shrink to fit their boxes, and pictures keep their proportions.*/import type PptxGenJSType from 'pptxgenjs'-import { documentEastAsianScript } from './docxScript'import { choose } from './ownEntry'import { type DrawContext, hex } from './pptxDraw'import {@@ -26,6 +25,7 @@ import { deckText, hanLanguage } from './pptxRuns'import { type PptxTemplate, buildTemplateSlides } from './pptxTemplates'import { PPTX_FONT_FACE, TEXT_INSET_X, truncateToLines, wrapLines } from './pptxText'import { PPTX_ASPECT_RATIOS, PPTX_PALETTES, PPTX_TEMPLATES } from './pptxVocabulary'+import { documentEastAsianScript } from './textScript'import { keepZipTextWhole } from './zipText'// Loaded with require so the tests and the Host run the same CommonJS bundle.diff --git a/mcp-host/src/workflow/pptxRender.ts b/mcp-host/src/workflow/pptxRender.tsindex da068cf19..e1dae5722 100644--- a/mcp-host/src/workflow/pptxRender.ts+++ b/mcp-host/src/workflow/pptxRender.ts@@ -1,5 +1,4 @@/** The slide renderers, one per layout, and the footer. */-import { hasRtlLetter } from './docxScript'import { nativeChartArgs } from './pptxCharts'import { type DrawContext, fitOrShorten, hex } from './pptxDraw'import { type PptxColumn, type PptxSlide, type SlideOf, fieldPath } from './pptxInput'@@ -25,6 +24,7 @@ import {} from './pptxRuns'import { TEXT_INSET_X, TEXT_INSET_Y, fitText, lineHeightIn, truncateToLines } from './pptxText'import { type PptxPalette } from './pptxVocabulary'+import { hasRtlLetter } from './textDirection'/** Series colors after the palette's own, so a chart of up to ten slices does not repeat one. */const EXTRA_SERIES_COLORS = ['#7c3aed', '#0d9488', '#ea580c', '#db2777']diff --git a/mcp-host/src/workflow/pptxRuns.ts b/mcp-host/src/workflow/pptxRuns.tsindex 7d251fd34..5a24dfa8f 100644--- a/mcp-host/src/workflow/pptxRuns.ts+++ b/mcp-host/src/workflow/pptxRuns.ts@@ -1,9 +1,9 @@/** Text runs on a slide: language, direction and bullets. */import type PptxGenJS from 'pptxgenjs'-import { bidiLanguage, hasRtlLetter, isRtlText, scriptSegments } from './docxScript'import { type PptxSlide, isRecord } from './pptxInput'import { type Box } from './pptxLayout'import { scriptOptions } from './pptxText'+import { bidiLanguage, hasRtlLetter, isRtlText, scriptSegments } from './textDirection'// U+2022, not U+25CF: PowerPoint sets the ambiguous-width U+25CF in the East Asian face of// a CJK-tagged paragraph, where the dot is larger than on the Latin lines around it.diff --git a/mcp-host/src/workflow/pptxText.ts b/mcp-host/src/workflow/pptxText.tsindex 4cb085209..e6347f3d2 100644--- a/mcp-host/src/workflow/pptxText.ts+++ b/mcp-host/src/workflow/pptxText.ts@@ -4,8 +4,8 @@* another slide, so the generator lays text out itself. It measures with the* Roboto faces fonts.ts registers, within a few percent of Arial, the deck font.*/-import { isRtlText } from './docxScript'import { CJK_WIDE_CHAR, bodyFaceEm } from './fonts'+import { isRtlText } from './textDirection'/** The one family the deck uses, for slides and charts alike. */export const PPTX_FONT_FACE = 'Arial'diff --git a/mcp-host/src/workflow/textDirection.ts b/mcp-host/src/workflow/textDirection.tsnew file mode 100644index 000000000..87ed057b5--- /dev/null+++ b/mcp-host/src/workflow/textDirection.ts@@ -0,0 +1,212 @@+/**+ * Which way text reads, the same in every generator: the Unicode bidi class of+ * each character (UAX #9), the direction of a paragraph, and the stretches of+ * right-to-left letters that Word and PowerPoint set as their own runs. The PDF+ * lays the text out from these classes itself (pdfBidi.ts).+ */++export type BidiClass =+ | 'L'+ | 'R'+ | 'AL'+ | 'EN'+ | 'AN'+ | 'ES'+ | 'ET'+ | 'CS'+ | 'NSM'+ | 'BN'+ | 'WS'+ | 'ON'++const MARK = /[\p{Mn}\p{Me}]/u+const LETTER = /[\p{L}\p{Mc}\p{Nd}\p{Nl}]/u++function inRanges(cp: number, ranges: ReadonlyArray<readonly [number, number]>): boolean {+ return ranges.some(([from, to]) => cp >= from && cp <= to)+}++/** Hebrew, NKo, Samaritan, Mandaic and the historic right-to-left blocks. */+const STRONG_R: ReadonlyArray<readonly [number, number]> = [+ [0x0590, 0x05ff],+ [0x07c0, 0x085f],+ [0xfb1d, 0xfb4f],+ [0x10800, 0x10fff],+ [0x1e800, 0x1edff],+ [0x200f, 0x200f],+]++/** Arabic, Syriac and Thaana, and their presentation forms. */+const STRONG_AL: ReadonlyArray<readonly [number, number]> = [+ [0x0600, 0x07bf],+ [0x0860, 0x08ff],+ [0xfb50, 0xfdff],+ [0xfe70, 0xfefe],+ [0x1ee00, 0x1eeff],+]++const ARABIC_DIGITS: ReadonlyArray<readonly [number, number]> = [+ [0x0600, 0x0605],+ [0x0660, 0x0669],+ [0x066b, 0x066c],+ [0x08e2, 0x08e2],+]++const EUROPEAN_DIGITS: ReadonlyArray<readonly [number, number]> = [+ [0x30, 0x39],+ [0x06f0, 0x06f9],+ [0xb2, 0xb3],+ [0xb9, 0xb9],+ [0x2070, 0x2079],+ [0x2080, 0x2089],+ [0xff10, 0xff19],+]++const TERMINATORS: ReadonlyArray<readonly [number, number]> = [+ [0x23, 0x25],+ [0xa2, 0xa5],+ [0xb0, 0xb1],+ [0x066a, 0x066a],+ [0x2030, 0x2034],+ [0x20a0, 0x20cf],+ [0x2213, 0x2213],+]++/** Plus and minus signs, U+2212 MINUS SIGN among them. */+const NUMBER_SIGNS = new Set([+ 0x2b, 0x2d, 0x207a, 0x207b, 0x208a, 0x208b, 0x2212, 0xfb29, 0xfe62, 0xfe63, 0xff0b, 0xff0d,+])++/** Zero-width joiners and other characters the rules pass over (X9). */+const BOUNDARY_NEUTRALS: ReadonlyArray<readonly [number, number]> = [+ [0xad, 0xad],+ [0x180e, 0x180e],+ [0x200b, 0x200d],+ [0x2060, 0x2064],+ [0xfeff, 0xfeff],+]++const SEPARATORS = new Set([0x2c, 0x2e, 0x2f, 0x3a, 0xa0, 0x060c, 0x202f, 0x2044, 0xfe50, 0xfe52])++export function bidiClass(cp: number): BidiClass {+ const ch = String.fromCodePoint(cp)+ if (MARK.test(ch)) return 'NSM'+ if (inRanges(cp, EUROPEAN_DIGITS)) return 'EN'+ if (inRanges(cp, ARABIC_DIGITS)) return 'AN'+ if (inRanges(cp, TERMINATORS)) return 'ET'+ if (SEPARATORS.has(cp)) return 'CS'+ if (NUMBER_SIGNS.has(cp)) return 'ES'+ if (inRanges(cp, BOUNDARY_NEUTRALS)) return 'BN'+ if (inRanges(cp, STRONG_AL)) return 'AL'+ if (inRanges(cp, STRONG_R)) return 'R'+ if (cp === 0x20 || cp === 0x09 || cp === 0x0c || (cp >= 0x2000 && cp <= 0x200a) || cp === 0x3000)+ return 'WS'+ if (cp === 0x200e || LETTER.test(ch)) return 'L'+ return 'ON'+}++/** Whether a code point belongs to a right-to-left script, marks included. */+export function isRtlScript(cp: number): boolean {+ return inRanges(cp, STRONG_R) || inRanges(cp, STRONG_AL)+}++/** Whether `text` holds anything a right-to-left paragraph would reorder. */+export function hasRtl(text: string): boolean {+ for (const ch of text) {+ const cls = bidiClass(ch.codePointAt(0)!)+ if (cls === 'R' || cls === 'AL' || cls === 'AN') return true+ }+ return false+}++/** The direction a strong class sets, or undefined for a weak or neutral one. */+export function strongDirection(cls: BidiClass): 'L' | 'R' | undefined {+ if (cls === 'L') return 'L'+ if (cls === 'R' || cls === 'AL') return 'R'+ return undefined+}++/**+ * Whether a paragraph of `text` reads right to left: its first strong+ * character (UAX #9, rules P2 and P3) is right to left. Digits and punctuation+ * do not decide it, while a roman numeral or a Devanagari digit is a+ * left-to-right character that does. The left-to-right and right-to-left marks+ * are passed over, as the PDF drops them as invisible.+ */+export function isRtlText(text: string): boolean {+ for (const ch of text) {+ const cp = ch.codePointAt(0)!+ if (cp === 0x200e || cp === 0x200f) continue+ const direction = strongDirection(bidiClass(cp))+ if (direction) return direction === 'R'+ }+ return false+}++const HEBREW_LETTER = /\p{Script=Hebrew}/u+const ARABIC_LETTER =+ /[\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}\p{Script=Hanifi_Rohingya}]/u+const RTL_LETTER = new RegExp(`${HEBREW_LETTER.source}|${ARABIC_LETTER.source}`, 'u')++/**+ * A right-to-left stretch of one script: its letters with the digits, spaces+ * and punctuation between them and the marks on the last one, but not the+ * neutrals after it, which take the paragraph's direction. Hebrew and Arabic+ * stay in separate runs because Word picks the face by the run's language and+ * draws boxes for Arabic tagged as Hebrew. The marks are matched once, after+ * the repetition, so no stretch of them can be split two ways.+ */+function span(letter: RegExp): string {+ return `${letter.source}(?:[^\\p{L}]*${letter.source})*\\p{M}*`+}+const RTL_SPAN = new RegExp(`${span(HEBREW_LETTER)}|${span(ARABIC_LETTER)}`, 'gu')++/** Whether `text` holds a letter of a right-to-left script anywhere. */+export function hasRtlLetter(text: string): boolean {+ return RTL_LETTER.test(text)+}++export interface ScriptSegment {+ text: string+ rtl: boolean+}++const WORD_LETTER = /\p{L}/u++/**+ * `text` cut into left-to-right and right-to-left stretches, in logical order.+ * In a right-to-left paragraph, digits and punctuation with no letter of their+ * own join the right-to-left stretch beside them: in a left-to-right run Word+ * sets them apart from the words they belong to ("المبيعات2026", "מספר.42").+ */+export function scriptSegments(text: string, rtlParagraph = false): ScriptSegment[] {+ if (!RTL_LETTER.test(text)) return [{ text, rtl: rtlParagraph && !WORD_LETTER.test(text) }]+ const out: ScriptSegment[] = []+ let at = 0+ RTL_SPAN.lastIndex = 0+ for (let m = RTL_SPAN.exec(text); m; m = RTL_SPAN.exec(text)) {+ if (m.index > at) out.push({ text: text.slice(at, m.index), rtl: false })+ out.push({ text: m[0], rtl: true })+ at = m.index + m[0].length+ }+ if (at < text.length) out.push({ text: text.slice(at), rtl: false })+ if (!rtlParagraph) return out+ const merged: Array<ScriptSegment & { neutral: boolean }> = []+ for (const segment of out) {+ const neutral = !segment.rtl && !WORD_LETTER.test(segment.text)+ const rtl = segment.rtl || neutral+ const last = merged[merged.length - 1]+ if (last && last.rtl && rtl && (neutral || last.neutral)) {+ last.text += segment.text+ last.neutral = last.neutral && neutral+ } else {+ merged.push({ text: segment.text, rtl, neutral })+ }+ }+ return merged.map(({ text: t, rtl }) => ({ text: t, rtl }))+}++/** Language tag for a right-to-left run of one script. */+export function bidiLanguage(text: string): string {+ return HEBREW_LETTER.test(text) ? 'he-IL' : 'ar-SA'+}diff --git a/mcp-host/src/workflow/textScript.ts b/mcp-host/src/workflow/textScript.tsnew file mode 100644index 000000000..03ca8143e--- /dev/null+++ b/mcp-host/src/workflow/textScript.ts@@ -0,0 +1,72 @@+/**+ * The East Asian script of a text block or a document, and the face and+ * language each generator tags it with.+ */++export interface EastAsianScript {+ font: string+ lang: string+}++// Each face ships with Windows and with Word for Mac.+const JAPANESE: EastAsianScript = { font: 'Yu Gothic', lang: 'ja-JP' }+const KOREAN: EastAsianScript = { font: 'Malgun Gothic', lang: 'ko-KR' }+const CHINESE: EastAsianScript = { font: 'Microsoft YaHei', lang: 'zh-CN' }+const TRADITIONAL_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-TW' }+const HONG_KONG_CHINESE: EastAsianScript = { font: 'Microsoft JhengHei', lang: 'zh-HK' }++// Frequent characters written one way in Simplified Chinese and another in+// Traditional, the two lists in the same order; neither form is Japanese kanji+// that a line without kana could hold for both.+const SIMPLIFIED_ONLY =+ /[这们个说会国学时为与来对发从实动经过还进现么没样关长门问题应见业开东头车书买卖电话语让认识请读写观点体产务员达区级际华结统计设资价变报农习乐网码帮]/gu+const TRADITIONAL_ONLY =+ /[這們個說會國學時為與來對發從實動經過還進現麼沒樣關長門問題應見業開東頭車書買賣電話語讓認識請讀寫觀點體產務員達區級際華結統計設資價變報農習樂網碼幫]/gu+/** Characters of written Cantonese, which Hong Kong text holds. */+const CANTONESE = /[嘅咗唔喺冇嚟啲佢哋嘢嗰]/u++const CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/gu++/**+ * The East Asian face and language a block names for itself, chosen from the+ * whole block so Han characters in Japanese text get the Japanese face:+ * Japanese with kana, Korean with Hangul. Han characters alone may be any of+ * the three, so such a block takes the document's (documentEastAsianScript).+ */+export function eastAsianScript(text: string): EastAsianScript | undefined {+ if (/[\p{Script=Hiragana}\p{Script=Katakana}]/u.test(text)) return JAPANESE+ if (/\p{Script=Hangul}/u.test(text)) return KOREAN+ return undefined+}++/**+ * The East Asian script of a whole document: that of the lines holding most of+ * its CJK characters, where a line of Han characters alone counts as Chinese.+ * Chinese is Traditional when its lines hold more characters only Traditional+ * writes than ones only Simplified writes, and Hong Kong's when they also hold+ * written Cantonese; otherwise Simplified, as before.+ */+export function documentEastAsianScript(text: string): EastAsianScript | undefined {+ const counts = new Map<EastAsianScript, number>()+ let traditional = 0+ let simplified = 0+ let cantonese = false+ for (const line of text.split('\n')) {+ const cjk = line.match(CJK_CHAR)?.length ?? 0+ if (cjk === 0) continue+ const script = eastAsianScript(line) ?? CHINESE+ counts.set(script, (counts.get(script) ?? 0) + cjk)+ if (script !== CHINESE) continue+ traditional += line.match(TRADITIONAL_ONLY)?.length ?? 0+ simplified += line.match(SIMPLIFIED_ONLY)?.length ?? 0+ cantonese ||= CANTONESE.test(line)+ }+ let best: EastAsianScript | undefined+ for (const script of [JAPANESE, KOREAN, CHINESE]) {+ if ((counts.get(script) ?? 0) > (best ? (counts.get(best) ?? 0) : 0)) best = script+ }+ if (best === CHINESE && traditional > simplified) {+ return cantonese ? HONG_KONG_CHINESE : TRADITIONAL_CHINESE+ }+ return best+}diff --git a/mcp-host/src/workflow/xlsxCells.ts b/mcp-host/src/workflow/xlsxCells.tsindex 16f809054..7821fb741 100644--- a/mcp-host/src/workflow/xlsxCells.ts+++ b/mcp-host/src/workflow/xlsxCells.ts@@ -6,41 +6,9 @@* cannot sum, sort or format them, so such a string is written as the value it* spells — unless reading it could change what it means.*/+import { currencyPrefix, currencySuffix } from './numberText'-/** Symbol shown for each currency code; codes that share "$" keep a prefix. */-export const CURRENCY_SYMBOLS: Record<string, string> = {- USD: '$',- EUR: '€',- GBP: '£',- JPY: '¥',- CNY: '¥',- INR: '₹',- KRW: '₩',- BRL: 'R$',- MXN: 'MX$',- CAD: 'CA$',- AUD: 'A$',- CHF: 'CHF ',- COP: 'COP ',- CLP: 'CLP ',- ARS: 'ARS ',- PEN: 'S/ ',-}--/** Symbols a price may be written with, longest first so "R$" wins over "$". */-const WRITTEN_SYMBOLS: Array<[string, string]> = [- ['US$', '$'],- ['MX$', 'MX$'],- ['CA$', 'CA$'],- ['R$', 'R$'],- ['A$', 'A$'],- ['$', '$'],- ['€', '€'],- ['£', '£'],- ['¥', '¥'],- ['₹', '₹'],- ['₩', '₩'],-]+export { CURRENCY_SYMBOLS, currencyPrefix, currencySuffix } from './numberText'export type CellKind =| 'text'@@ -82,26 +50,6 @@ const MAX_SIGNIFICANT_DIGITS = 15/** Longest "+" number read as signed; international phone numbers are longer. */const MAX_SIGNED_DIGITS = 6-/** The currency symbol `s` opens with and the length of what spells it. */-export function currencyPrefix(s: string): [string, number] | undefined {- for (const [written, symbol] of WRITTEN_SYMBOLS) {- if (s.startsWith(written)) return [symbol, written.length]- }- const code = /^([A-Z]{3})\s?(?=[\d.-])/.exec(s)- if (code && CURRENCY_SYMBOLS[code[1]]) return [CURRENCY_SYMBOLS[code[1]], code[0].length]- return undefined-}--/** The currency symbol `s` closes with and the length of what spells it. */-export function currencySuffix(s: string): [string, number] | undefined {- for (const [written, symbol] of WRITTEN_SYMBOLS) {- if (s.endsWith(written)) return [symbol, written.length]- }- const code = /(?<=\d)\s?([A-Z]{3})$/.exec(s)- if (code && CURRENCY_SYMBOLS[code[1]]) return [CURRENCY_SYMBOLS[code[1]], code[0].length]- return undefined-}-type ParsedNumber = {value: numberwritten: number