Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): say what a chart could not show, and keep moved labels legible
marcoteb7ae854816f0b
diff --git a/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts b/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.tsindex 8b74b0031..80719e16a 100644--- a/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.charts.delivery.test.ts@@ -162,6 +162,50 @@ describe('chaining a chart into a deck', () => {})})+describe('what a chart could not show', () => {+ it('says how many requested value labels it left off', async () => {+ const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']+ const datasets = ['North', 'South', 'East', 'West'].map((label, i) => ({+ label,+ data: months.map((_, j) => 1000 + i * 300 + ((j * 37 + i * 53) % 400)),+ }))+ const args = { type: 'bar', data: { labels: months, datasets }, valueFormat: 'currency' }+ const asked = (await chart().execute(+ { ...args, filename: 'a.png', showValues: true },+ outputDir+ )) as InternalToolResult+ expect(asked.content).toMatch(/showValues: \d+ values got no label/)+ const byDefault = (await chart().execute(+ { ...args, filename: 'b.png' },+ outputDir+ )) as InternalToolResult+ expect(byDefault.content).not.toContain('got no label')+ })++ it('says when the title and legend leave the data no room', async () => {+ const result = (await chart().execute(+ {+ filename: 't.png',+ type: 'bar',+ title: 'Tiny chart title',+ width: 100,+ height: 100,+ data: {+ labels: ['a', 'b'],+ datasets: [+ { label: 'Revenue', data: [1, 2] },+ { label: 'Cost', data: [2, 1] },+ ],+ },+ },+ outputDir+ )) as InternalToolResult+ expect(result.content).toMatch(/At 100x100 px the title, legend and axes leave the data/)+ const roomy = (await chart().execute(yearly, outputDir)) as InternalToolResult+ expect(roomy.content).not.toContain('leave the data')+ })+})+describe('chart names', () => {it('replaces a file of the same name, which later steps embed by that name', async () => {const router = workflowRouter()diff --git a/mcp-host/src/workflow/chartPlugins.ts b/mcp-host/src/workflow/chartPlugins.tsindex 4def8375f..a91f761fd 100644--- a/mcp-host/src/workflow/chartPlugins.ts+++ b/mcp-host/src/workflow/chartPlugins.ts@@ -24,6 +24,8 @@ export interface ValueLabelOptions {indexAxis?: 'x' | 'y'/** Multiplier tying label type to the canvas size, as the axes use. */fontScale?: number+ /** Set on each draw to how many values got no label. */+ unlabelled?: { count: number }}/** Below this, money is grouped in full rather than abbreviated. */@@ -294,7 +296,15 @@ function drawValueLabels(chart: Chart, o: ValueLabelOptions): void {const points = chart.data.datasets.reduce((n, d) => n + (d.data?.length ?? 0), 0)const stride = labelStride(points)- if (stride === 0) return+ const unlabelled = o.unlabelled ?? { count: 0 }+ unlabelled.count = 0+ if (stride === 0) {+ unlabelled.count = chart.data.datasets.reduce(+ (n, d) => n + (d.data ?? []).filter(v => v !== null && v !== undefined).length,+ 0+ )+ return+ }const size = Math.round(12 * (o.fontScale ?? 1))// Labels of different series can land on the same spot; the first one drawn@@ -313,11 +323,14 @@ function drawValueLabels(chart: Chart, o: ValueLabelOptions): void {const total = totals[di]meta.data.forEach((element, ei) => {- // Keep the first and last of a thinned series so the range stays readable.- if (stride > 1 && ei % stride !== 0 && ei !== meta.data.length - 1) returnconst raw = dataset.data[ei]const value = typeof raw === 'number' ? raw : readY(raw)if (value === null) return+ const skip = () => {+ unlabelled.count+++ }+ // Keep the first and last of a thinned series so the range stays readable.+ if (stride > 1 && ei % stride !== 0 && ei !== meta.data.length - 1) return skip()let text = formatValue(value, o)if (arcs && total > 0) {@@ -325,10 +338,10 @@ function drawValueLabels(chart: Chart, o: ValueLabelOptions): void {}if (!text) return- if (arcs && total > 0 && Math.abs(value) / total < MIN_ARC_SHARE) return+ if (arcs && total > 0 && Math.abs(value) / total < MIN_ARC_SHARE) return skip()const half = ctx.measureText(text).width / 2 + 2const spot = labelSpot(chart, element, arcs, o.indexAxis === 'y', half)- if (!spot) return+ if (!spot) return skip()const box = {left: spot.x - half,right: spot.x + half,@@ -336,22 +349,24 @@ function drawValueLabels(chart: Chart, o: ValueLabelOptions): void {bottom: spot.y + size / 2 + 1,}// A label on the tick column moves to the side of it its slice is on.- if (ticks && intersects(box, ticks)) {+ const shifted = !!ticks && intersects(box, ticks)+ if (ticks && shifted) {const shift =spot.x < (ticks.left + ticks.right) / 2 ? ticks.left - box.right : ticks.right - box.leftbox.left += shiftbox.right += shiftspot.x += shift}- if (taken.some(other => intersects(box, other))) return- if (bars.some(bar => bar.element !== element && intersects(box, bar))) return+ if (taken.some(other => intersects(box, other))) return skip()+ if (bars.some(bar => bar.element !== element && intersects(box, bar))) return skip()taken.push(box)// A label that lands on top of a filled slice has to invert; one that- // sits in open plot area keeps the theme color over a halo of the ground.+ // sits in open plot area, or was moved off its slice, keeps the theme+ // color over a halo of the ground.let ink = o.textColorlet halo = o.backgroundColor- if (arcs) {+ if (arcs && !shifted) {const light = luminance(elementFill(element, ei) ?? '')if (light !== undefined) {ink = light < 0.5 ? '#ffffff' : '#0f172a'diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.tsindex 7de0176dc..1a493f44b 100644--- a/mcp-host/src/workflow/internalTools.ts+++ b/mcp-host/src/workflow/internalTools.ts@@ -361,6 +361,8 @@ const DEFAULT_CHART_WIDTH = 800const DEFAULT_CHART_HEIGHT = 400const MAX_CHART_DIMENSION = 4000const MIN_CHART_DIMENSION = 100+/** A plot area narrower or shorter than this, in px, shows too little of the data to read. */+const MIN_PLOT_SIDE = 40const SUPPORTED_CHART_TYPES = new Set([// Core Chart.js types (one-to-one mapping):@@ -1082,7 +1084,8 @@ const generateChart: InternalToolDefinition = {const labelValues = shouldLabelValues(chartTypeRaw, args.showValues)const plugins: Plugin[] = [backgroundPlugin(theme.backgroundColor)]- if (labelValues) plugins.push(valueLabelsPlugin(valueOptions))+ const unlabelled = { count: 0 }+ if (labelValues) plugins.push(valueLabelsPlugin({ ...valueOptions, unlabelled }))if (chartTypeRaw === 'gauge') {plugins.push(gaugeCenterPlugin({@@ -1237,6 +1240,24 @@ const generateChart: InternalToolDefinition = {// context is API-compatible for the subset Chart.js uses.chart = new Chart(ctx as any, config)chart.update('none')+ if (args.showValues === true && unlabelled.count > 0) {+ const n = unlabelled.count+ warnings.push(+ `showValues: ${n} value${n === 1 ? '' : 's'} got no label, since at this size ` ++ `${n === 1 ? 'its label' : 'the labels'} would cover another bar or label, or the ` ++ 'series is too dense; a larger chart or fewer series leaves room.'+ )+ }+ // The plot area in requested pixels; the layout may run at a smaller scale.+ const plotWidth = Math.max(0, Math.round(chart.chartArea.width * layoutScale))+ const plotHeight = Math.max(0, Math.round(chart.chartArea.height * layoutScale))+ if (plotWidth < MIN_PLOT_SIDE || plotHeight < MIN_PLOT_SIDE) {+ warnings.push(+ `At ${width}x${height} px the title, legend and axes leave the data ` ++ `${plotWidth}x${plotHeight} px; use a larger width and height, or leave out the ` ++ 'title or legend.'+ )+ }const pngBuffer = stampPngDensity(canvas.toBuffer('image/png'), pixelRatio)