Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): say when a template, theme or palette name is not used
marcoteb8b805e355ffc
diff --git a/mcp-host/src/workflow/__tests__/internalTools.namedOptions.test.ts b/mcp-host/src/workflow/__tests__/internalTools.namedOptions.test.tsnew file mode 100644index 000000000..482260511--- /dev/null+++ b/mcp-host/src/workflow/__tests__/internalTools.namedOptions.test.ts@@ -0,0 +1,88 @@+/**+ * Templates, themes and palettes are chosen by name. A name that is not one of+ * them falls back to the default, and the result says so: a model that sent+ * "blue" learns it was not used instead of receiving the light theme silently.+ * Case does not matter, so "Dark" is dark.+ */+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'++const tool = (name: string) => INTERNAL_TOOLS.find(t => t.name === `clerum__generate_${name}`)!++let dir: string+beforeEach(() => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-named-'))+})+afterEach(() => {+ fs.rmSync(dir, { recursive: true, force: true })+})++const chart = {+ filename: 'c.png',+ chartType: 'bar',+ data: { labels: ['a'], datasets: [{ label: 'x', data: [1] }] },+}++describe('names that pick an option', () => {+ it('reports a chart theme it does not know and draws the light one', async () => {+ const result = await tool('chart').execute({ ...chart, theme: 'blue' }, dir)+ expect(result.success, result.error).toBe(true)+ expect(result.content).toContain('theme "blue" is not one of')+ expect(result.content).toContain('"light" was used')+ })++ it('takes a chart theme in any case', async () => {+ const result = await tool('chart').execute({ ...chart, theme: 'Dark' }, dir)+ expect(result.success, result.error).toBe(true)+ expect(result.content ?? '').not.toContain('theme')+ })++ it.each([+ ['pdf', { body: 'Text.' }],+ ['docx', { body: 'Text.' }],+ ['xlsx', { sheets: [{ name: 'S', rows: [['a'], [1]] }] }],+ ])('reports a %s palette it does not know', async (name, args) => {+ const result = await tool(name).execute(+ { filename: `p.${name}`, palette: 'neon', ...args },+ dir+ )+ expect(result.success, result.error).toBe(true)+ expect(result.content).toContain('palette "neon" is not one of')+ })++ it('reports a dashboard template and theme it does not know, and reads a mode in any case', async () => {+ const result = await tool('dashboard').execute(+ {+ filename: 'd.html',+ template: 'nope',+ theme: 'Corporate',+ defaultThemeMode: 'Dark',+ data: { title: 'T', kpis: [{ label: 'A', value: '1' }] },+ },+ dir+ )+ expect(result.success, result.error).toBe(true)+ expect(result.content).toContain('template "nope" is not one of')+ expect(result.content).not.toContain('theme "Corporate"')+ expect(result.content ?? '').not.toContain('defaultThemeMode')+ const html = fs.readFileSync(result.artifact!.path, 'utf8')+ // A fixed mode replaces following the viewer's setting.+ expect(html).not.toContain('@media (prefers-color-scheme: dark)')+ })++ it('reports a dashboard mode that is neither light nor dark', async () => {+ const result = await tool('dashboard').execute(+ {+ filename: 'd.html',+ defaultThemeMode: 'purple',+ data: { title: 'T', kpis: [{ label: 'A', value: '1' }] },+ },+ dir+ )+ expect(result.success, result.error).toBe(true)+ expect(result.content).toContain('defaultThemeMode "purple" is not light or dark')+ })+})diff --git a/mcp-host/src/workflow/chartGenerator.ts b/mcp-host/src/workflow/chartGenerator.tsindex a83bfdbe1..d62666b56 100644--- a/mcp-host/src/workflow/chartGenerator.ts+++ b/mcp-host/src/workflow/chartGenerator.ts@@ -40,7 +40,7 @@ import {} from './chartThemes'import { PNG_BASE_PPM } from './embeddedImages'import { CHART_FONT_STACK, ensureFontsReady, sanitizeForFont } from './fonts'-import { own } from './ownEntry'+import { choose } from './ownEntry'import type { InternalToolResult } from './types'// Register Chart.js controllers/scales/elements/plugins once. Chart.js v4 ships@@ -657,8 +657,8 @@ export async function runGenerateChart(`${(MAX_CANVAS_PIXELS / 1e6).toFixed(1)} megapixel limit. Reduce width or height.`,}}- const themeName = String(args.theme ?? 'light')- const theme = own(CHART_THEMES, themeName) ?? CHART_THEMES.light+ const theme =+ CHART_THEMES[choose(args.theme, Object.keys(CHART_THEMES), 'light', 'theme', warnings)]const title = args.title ? sanitizeForFont(String(args.title)) : undefinedconst yAxisLabel = args.yAxisLabel ? sanitizeForFont(String(args.yAxisLabel)) : undefineddiff --git a/mcp-host/src/workflow/dashboardRender.ts b/mcp-host/src/workflow/dashboardRender.tsindex 9ffa6e96d..1f786ebd1 100644--- a/mcp-host/src/workflow/dashboardRender.ts+++ b/mcp-host/src/workflow/dashboardRender.ts@@ -19,7 +19,7 @@ import {type DashboardThemeColors,type ThemeName,} from './dashboardThemes'-import { own } from './ownEntry'+import { choose, own } from './ownEntry'import { headerText, normalizeTableRows } from './tableRows'import type { InternalToolResult } from './types'@@ -1736,16 +1736,39 @@ export async function runGenerateDashboard('e.g. {"title": "Weekly review", "kpis": [...]}.',}}- const template = oneOf(args.template, DASHBOARD_TEMPLATE_NAMES, 'executive-brief')+ const notes: string[] = []+ const template = choose(+ args.template,+ DASHBOARD_TEMPLATE_NAMES,+ 'executive-brief',+ 'template',+ notes+ )+ const theme = choose(+ args.theme,+ ['default', 'corporate', 'warm', 'alert'] as const,+ 'default',+ 'theme',+ notes+ )+ // Unset, the page follows the viewer's setting, so an unknown mode does too.+ let defaultThemeMode: 'light' | 'dark' | undefined+ if (args.defaultThemeMode !== undefined && args.defaultThemeMode !== null) {+ const mode = String(args.defaultThemeMode).trim().toLowerCase()+ if (mode === 'light' || mode === 'dark') defaultThemeMode = mode+ else {+ notes.push(+ `defaultThemeMode ${JSON.stringify(args.defaultThemeMode)} is not light or dark, so ` ++ "the page follows the viewer's setting."+ )+ }+ }const inlineChartJs = args.inlineChartJs !== falseconst { html, ctx } = renderDashboard({template,data,- theme: oneOf(args.theme, ['default', 'corporate', 'warm', 'alert'] as const, 'default'),- defaultThemeMode:- args.defaultThemeMode === 'light' || args.defaultThemeMode === 'dark'- ? args.defaultThemeMode- : undefined,+ theme,+ defaultThemeMode,inlineChartJs,branding: isDashRecord(args.branding) ? args.branding : {},})@@ -1758,6 +1781,7 @@ export async function runGenerateDashboard(}}const warnings = [+ ...notes,...ctx.failures.map(f => `${f} A notice shows in its place.`),...ignoredDashboardFields(template, data),...ctx.warnings,diff --git a/mcp-host/src/workflow/docxGenerator.ts b/mcp-host/src/workflow/docxGenerator.tsindex be6493b60..6ee7370e1 100644--- a/mcp-host/src/workflow/docxGenerator.ts+++ b/mcp-host/src/workflow/docxGenerator.ts@@ -43,7 +43,7 @@ import {} from './docxStyle'import { buildDocxTable, docxSections } from './docxTable'import { htmlToPlainText } from './inlineMarkup'-import { own } from './ownEntry'+import { choose } from './ownEntry'import { normalizeTableRows } from './tableRows'import type { InternalToolResult } from './types'@@ -87,11 +87,14 @@ export async function runGenerateDocx(const title = args.title ? htmlToPlainText(String(args.title)) : undefinedconst headline = args.headline ? htmlToPlainText(String(args.headline)) : undefinedconst body = String(args.body ?? '')- const palette = own(DOCX_PALETTES, String(args.palette ?? 'default')) ?? DOCX_PALETTES.defaultconst branding: DocxBranding = printedBranding(args.branding)const images: unknown[] = Array.isArray(args.images) ? args.images : []const tables: unknown[] = Array.isArray(args.tables) ? args.tables : []const warnings: string[] = []+ const palette =+ DOCX_PALETTES[+ choose(args.palette, Object.keys(DOCX_PALETTES), 'default', 'palette', warnings)+ ]const numbering = new DocxListNumbering()const imageBox = { width: DOCX_MAX_IMAGE_WIDTH, height: DOCX_MAX_IMAGE_HEIGHT }let embedded = 0diff --git a/mcp-host/src/workflow/ownEntry.ts b/mcp-host/src/workflow/ownEntry.tsindex 09ded6b71..29fc7fe87 100644--- a/mcp-host/src/workflow/ownEntry.ts+++ b/mcp-host/src/workflow/ownEntry.ts@@ -8,3 +8,25 @@export function own<T>(table: Partial<Record<string, T>>, key: string): T | undefined {return Object.hasOwn(table, key) ? table[key] : undefined}++/**+ * `value` as one of `allowed`, read without regard to case; `fallback` when it+ * is unset, and also when it names none of them, which `warnings` records so+ * the model learns its choice was not used.+ */+export function choose<T extends string>(+ value: unknown,+ allowed: readonly T[],+ fallback: T,+ field: string,+ warnings: string[]+): T {+ if (value === undefined || value === null || value === '') return fallback+ const text = String(value).trim().toLowerCase()+ const hit = allowed.find(name => name.toLowerCase() === text)+ if (hit !== undefined) return hit+ warnings.push(+ `${field} ${JSON.stringify(value)} is not one of ${allowed.join(', ')}; "${fallback}" was used.`+ )+ return fallback+}diff --git a/mcp-host/src/workflow/pdfGenerator.ts b/mcp-host/src/workflow/pdfGenerator.tsindex 7291b0519..3d6e10cbc 100644--- a/mcp-host/src/workflow/pdfGenerator.ts+++ b/mcp-host/src/workflow/pdfGenerator.ts@@ -44,7 +44,7 @@ import {startsTable,stripListMarker,} from './markdownBlocks'-import { own } from './ownEntry'+import { choose } from './ownEntry'import {BODY_FONT_SIZE,MIN_BOTTOM_MARGIN,@@ -855,8 +855,8 @@ export async function runGeneratePdf(// Titles take no formatting, so HTML in them is read as text.const title = args.title ? htmlToPlainText(String(args.title)) || undefined : undefinedconst body = String(args.body ?? '')- const paletteName = String(args.palette ?? 'default')- const palette = own(PDF_PALETTES, paletteName) ?? PDF_PALETTES.default+ const palette =+ PDF_PALETTES[choose(args.palette, Object.keys(PDF_PALETTES), 'default', 'palette', warnings)]const branding: PdfBranding = printedBranding(args.branding)const imageRefs = Array.isArray(args.images) ? args.images : []const tables = (Array.isArray(args.tables) ? args.tables : []) as PdfTableSpec[]diff --git a/mcp-host/src/workflow/xlsxWorkbook.ts b/mcp-host/src/workflow/xlsxWorkbook.tsindex 4c1d4823c..150965813 100644--- a/mcp-host/src/workflow/xlsxWorkbook.ts+++ b/mcp-host/src/workflow/xlsxWorkbook.ts@@ -10,7 +10,7 @@ import {fitImageSize,loadEmbeddableImage,} from './embeddedImages'-import { own } from './ownEntry'+import { choose } from './ownEntry'import { headerText } from './tableRows'import {type SheetCell,@@ -898,7 +898,8 @@ export async function buildXlsxWorkbook('images belongs inside each sheet (sheets[i].images); the top-level images were placed on the first sheet.')}- const palette = own(XLSX_PALETTES, String(args.palette ?? 'default')) ?? XLSX_PALETTES.default+ const palette =+ XLSX_PALETTES[choose(args.palette, Object.keys(XLSX_PALETTES), 'default', 'palette', warnings)]const branding = isRecord(args.branding) ? args.branding : {}const company = typeof branding.companyName === 'string' ? branding.companyName : ''