Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): keep chart colors with their values and drop extra series first
marcoteb66735ff4c739
diff --git a/mcp-host/src/workflow/__tests__/chartData.test.ts b/mcp-host/src/workflow/__tests__/chartData.test.tsindex ae6a966de..5cc89e1b9 100644--- a/mcp-host/src/workflow/__tests__/chartData.test.ts+++ b/mcp-host/src/workflow/__tests__/chartData.test.ts@@ -518,7 +518,151 @@ describe('values that name their label', () => {})})+describe('colors of values that name their label', () => {+ it('go with their values', () => {+ const r = normalizeChartData(+ {+ labels: ['Jan', 'Feb'],+ datasets: [+ {+ data: [+ { label: 'Feb', value: 20 },+ { label: 'Jan', value: 10 },+ ],+ backgroundColor: ['red', 'green'],+ borderColor: ['darkred', 'darkgreen'],+ },+ ],+ },+ bar+ )+ expect(r.datasets[0].data).toEqual([10, 20])+ expect(r.datasets[0].backgroundColor).toEqual(['green', 'red'])+ expect(r.datasets[0].borderColor).toEqual(['darkgreen', 'darkred'])+ })++ it('repeat a short list with their values on a bar chart, as Chart.js repeats it', () => {+ // C, A and B sent against two colors: C red, A green, B red again.+ const r = normalizeChartData(+ {+ labels: ['A', 'B', 'C'],+ datasets: [+ {+ data: [+ { label: 'C', value: 3 },+ { label: 'A', value: 1 },+ { label: 'B', value: 2 },+ ],+ backgroundColor: ['red', 'green'],+ },+ ],+ },+ bar+ )+ expect(r.datasets[0].data).toEqual([1, 2, 3])+ expect(r.datasets[0].backgroundColor).toEqual(['green', 'red', 'red'])+ })++ it('leave a gap, and on a slice chart a value past a short list, without a color', () => {+ const r = normalizeChartData(+ {+ labels: ['A', 'B', 'C'],+ datasets: [+ {+ data: [+ { label: 'C', value: 3 },+ { label: 'A', value: 1 },+ ],+ backgroundColor: ['red'],+ },+ ],+ },+ { chartType: 'pie' }+ )+ expect(r.datasets[0].data).toEqual([1, null, 3])+ expect(r.datasets[0].backgroundColor).toEqual([undefined, undefined, 'red'])+ })++ it('are left out with a value that repeats a label', () => {+ const r = normalizeChartData(+ {+ datasets: [+ {+ data: [+ { label: 'A', value: 1 },+ { label: 'A', value: 9 },+ { label: 'B', value: 2 },+ ],+ backgroundColor: ['red', 'blue', 'green'],+ },+ ],+ },+ bar+ )+ expect(r.labels).toEqual(['A', 'B'])+ expect(r.datasets[0].data).toEqual([1, 2])+ expect(r.datasets[0].backgroundColor).toEqual(['red', 'green'])+ })++ it('leave an empty color list as it was sent', () => {+ const r = normalizeChartData(+ {+ labels: ['A', 'B'],+ datasets: [+ {+ data: [+ { label: 'B', value: 2 },+ { label: 'A', value: 1 },+ ],+ backgroundColor: [],+ },+ ],+ },+ bar+ )+ expect(r.datasets[0].data).toEqual([1, 2])+ expect(r.datasets[0].backgroundColor).toEqual([])+ })++ it('reach every category a later series adds', () => {+ const r = normalizeChartData(+ {+ datasets: [+ { data: [{ label: 'A', value: 1 }], backgroundColor: ['red'] },+ { data: [{ label: 'B', value: 2 }] },+ ],+ },+ bar+ )+ expect(r.labels).toEqual(['A', 'B'])+ expect(r.datasets[0].data).toEqual([1, null])+ expect(r.datasets[0].backgroundColor).toEqual(['red', undefined])+ })+})+describe('single-series charts', () => {+ it.each(['waterfall', 'funnel'] as const)('%s: a dropped series adds no category', chartType => {+ const r = normalizeChartData(+ {+ datasets: [+ {+ label: 'Primary',+ data: [+ { label: 'Start', value: 2 },+ { label: 'Change', value: -1 },+ ],+ },+ { label: 'Second', data: [{ label: 'Phantom', value: 900 }] },+ ],+ },+ { chartType }+ )+ expect(r.labels).toEqual(['Start', 'Change'])+ expect(r.datasets).toHaveLength(1)+ expect(r.datasets[0].data).toEqual([2, -1])+ expect(r.warnings.join(' ')).toContain('1 extra dataset(s) were dropped')+ })+it('count only the series they draw when numbering missing labels', () => {const r = normalizeChartData({diff --git a/mcp-host/src/workflow/__tests__/chartPlugins.test.ts b/mcp-host/src/workflow/__tests__/chartPlugins.test.tsindex c3f36ad4c..8ffd74fc4 100644--- a/mcp-host/src/workflow/__tests__/chartPlugins.test.ts+++ b/mcp-host/src/workflow/__tests__/chartPlugins.test.ts@@ -209,6 +209,13 @@ describe('slice colors', () => {expect(padded).toBe(true)})+ it('keeps a requested color on its slice when the list has gaps', () => {+ const { colors, padded } = sliceColors(3, [undefined, '#16a34a'], palette, '#ffffff')+ expect(colors[1]).toBe('#16a34a')+ expect(new Set(colors).size).toBe(3)+ expect(padded).toBe(true)+ })+it('shades the palette past its end, and never gives the last slice the first color', () => {const { colors, repeated } = sliceColors(8, undefined, palette, '#ffffff')expect(new Set(colors).size).toBe(8)diff --git a/mcp-host/src/workflow/__tests__/dashboardParts.test.ts b/mcp-host/src/workflow/__tests__/dashboardParts.test.tsindex bd725859b..5dfc51c39 100644--- a/mcp-host/src/workflow/__tests__/dashboardParts.test.ts+++ b/mcp-host/src/workflow/__tests__/dashboardParts.test.ts@@ -29,6 +29,55 @@ describe('DashboardCharts', () => {expect(charts.drawn).toBe(1)})+ it('keeps the colors of values placed by their labels, and colors a gap without a warning', () => {+ const reported = notes()+ const charts = new DashboardCharts(true, reported)+ charts.card(+ {+ type: 'bar',+ labels: ['Jan', 'Feb', 'Mar'],+ datasets: [+ {+ label: 's',+ data: [+ { label: 'Feb', value: 20 },+ { label: 'Jan', value: 10 },+ ],+ backgroundColor: ['red', 'green'],+ },+ ],+ },+ 'data.charts[0]'+ )+ const ds = charts.specs[0].datasets[0]+ expect(ds.data).toEqual([10, 20, null])+ const colors = ds.backgroundColor as string[]+ expect(colors.slice(0, 2)).toEqual(['green', 'red'])+ expect(typeof colors[2]).toBe('string')+ expect(reported.warnings.join(' ')).not.toContain('not a CSS color')+ })++ it.each(['waterfall', 'funnel'])('%s: a dropped series adds no category', type => {+ const charts = new DashboardCharts(true, notes())+ charts.card(+ {+ type,+ datasets: [+ {+ label: 'Primary',+ data: [+ { label: 'Start', value: 2 },+ { label: 'Change', value: -1 },+ ],+ },+ { label: 'Second', data: [{ label: 'Phantom', value: 900 }] },+ ],+ },+ 'data.charts[0]'+ )+ expect(charts.specs[0].labels).not.toContain('Phantom')+ })+it('reports a chart it cannot draw in its place, escaped', () => {const reported = notes()const charts = new DashboardCharts(true, reported)diff --git a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.tsindex 3999b7a95..e0512fa76 100644--- a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts+++ b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts@@ -680,6 +680,40 @@ describe('colors follow their points', () => {expect(colorCount(image, red, bottom)).toBeGreaterThan(colorCount(image, green, bottom))})+ it('keeps each bar in its color when values are placed by their labels', async () => {+ const tool = findTool('clerum__generate_chart')+ const red = '#dc2626'+ const green = '#16a34a'+ const r = await tool.execute(+ {+ filename: 'placed-colors.png',+ type: 'bar',+ showValues: false,+ data: {+ labels: ['Jan', 'Feb'],+ datasets: [+ {+ data: [+ { label: 'Feb', value: 20 },+ { label: 'Jan', value: 10 },+ ],+ backgroundColor: [red, green],+ },+ ],+ },+ },+ outputDir+ )+ expect(r.success).toBe(true)+ const image = await pixelsOf(path.join(outputDir, 'placed-colors.png'))+ const half = Math.floor(image.width / 2)+ const left = { x: 0, y: 0, w: half, h: image.height }+ const right = { x: half, y: 0, w: image.width - half, h: image.height }+ // Jan, sent green, is the left bar; Feb, sent red, the right one.+ expect(colorCount(image, green, left)).toBeGreaterThan(colorCount(image, red, left))+ expect(colorCount(image, red, right)).toBeGreaterThan(colorCount(image, green, right))+ })+it('fills a short color list from the theme instead of repeating it', async () => {const tool = findTool('clerum__generate_chart')const r = await tool.execute(diff --git a/mcp-host/src/workflow/chartData.ts b/mcp-host/src/workflow/chartData.tsindex efac2dbf2..d0c0b64da 100644--- a/mcp-host/src/workflow/chartData.ts+++ b/mcp-host/src/workflow/chartData.ts@@ -37,8 +37,9 @@ export interface NormalizedPoint {export interface NormalizedDataset {label?: stringdata: Array<number | null> | NormalizedPoint[]- backgroundColor?: string | string[]- borderColor?: string | string[]+ /** Per value when a list; a gap (undefined) is a value with no color of its own. */+ backgroundColor?: string | Array<string | undefined>+ borderColor?: string | Array<string | undefined>fill?: boolean}@@ -598,14 +599,20 @@ export function normalizeChartData(raw: unknown, opts: NormalizeOptions): Normaldatasets.push({ ...(carryStyle(ds) as NormalizedDataset), data: series.data })})- if (labelled.some(Boolean)) labels = placeByLabel(labels, datasets, labelled, root, warnings)-+ // A single-series chart draws only the first dataset, so the rest are left out+ // before anything is read from them: their labels must not become categories.if (SINGLE_SERIES_TYPES.has(chartType) && datasets.length > 1) {const extra = datasets.length - 1warnings.push(`\`${root}.datasets[${extra === 1 ? '1' : `1..${extra}`}]\`: "${chartType}" draws a ` +`single series; ${extra} extra dataset(s) were dropped.`)+ datasets.splice(1)+ labelled.splice(1)+ }++ if (labelled.some(Boolean)) {+ labels = placeByLabel(labels, datasets, labelled, root, warnings, !SLICE_TYPES.has(chartType))}if (!isXY) {@@ -663,18 +670,25 @@ export function normalizeChartData(raw: unknown, opts: NormalizeOptions): Normalreturn { labels, datasets, warnings }}+/** Per-value styles, which go wherever their value is placed. */+const POINT_STYLE_KEYS = ['backgroundColor', 'borderColor'] as const+/*** Values that carry their own label ({label, value} records, [label, value]* pairs) placed at that label, not at their position: the categories are the* given labels, then any new ones in the order met. A series with no value for- * a category leaves a gap there. Returns the categories.+ * a category leaves a gap there. Each value's own colors go with it; with+ * `wrapColors` a short color list repeats, as Chart.js repeats it, and without+ * it (slice charts) a value past the list, like a gap, has no color of its own+ * and is left for the renderer to color. Returns the categories.*/function placeByLabel(given: string[] | undefined,datasets: NormalizedDataset[],labelled: Array<string[] | undefined>,root: string,- warnings: string[]+ warnings: string[],+ wrapColors: boolean): string[] {const categories = given ? [...given] : []// Labels match whatever their case and surrounding spaces: 'jan' is the 'Jan' column.@@ -686,6 +700,7 @@ function placeByLabel(let moved = falselet repeated = 0const unmatched: number[] = []+ const placedColors: Array<Array<string | undefined>> = []labelled.forEach((own, d) => {if (!own) return// Labels of its own that name none of the given ones are not categories but@@ -697,6 +712,18 @@ function placeByLabel(}const values = datasets[d].data as Array<number | null>const placed: Array<number | null> = new Array(categories.length).fill(null)+ const styles = POINT_STYLE_KEYS.flatMap(styleKey => {+ const list = datasets[d][styleKey]+ return Array.isArray(list) && list.length > 0+ ? [+ {+ styleKey,+ list,+ placed: new Array<string | undefined>(categories.length).fill(undefined),+ },+ ]+ : []+ })const seen = new Set<string>()own.forEach((label, i) => {if (seen.has(key(label))) {@@ -712,9 +739,19 @@ function placeByLabel(}if (at !== i) moved = trueplaced[at] = values[i]+ for (const style of styles) {+ style.placed[at] = wrapColors ? style.list[i % style.list.length] : style.list[i]+ }})datasets[d] = { ...datasets[d], data: placed }+ for (const style of styles) {+ datasets[d][style.styleKey] = style.placed+ placedColors.push(style.placed)+ }})+ // A placed color list reaches every category; those a later series added+ // are gaps the renderer colors.+ for (const list of placedColors) while (list.length < categories.length) list.push(undefined)// Series without labels of their own keep their positions, padded to every category.for (const ds of datasets) {const data = ds.data as Array<number | null>diff --git a/mcp-host/src/workflow/chartGenerator.ts b/mcp-host/src/workflow/chartGenerator.tsindex 1c8c1454b..d2f0e06e5 100644--- a/mcp-host/src/workflow/chartGenerator.ts+++ b/mcp-host/src/workflow/chartGenerator.ts@@ -94,8 +94,9 @@ interface ChartDataset {* tuples once a waterfall has been expanded into floating bars.*/data: Array<number | null> | NormalizedPoint[] | number[][]- backgroundColor?: string | string[]- borderColor?: string | string[]+ /** Per value when a list; a gap is a value placed by its label with no color of its own. */+ backgroundColor?: string | Array<string | undefined>+ borderColor?: string | Array<string | undefined>fill?: boolean}@@ -234,6 +235,9 @@ function applyThemePalette(theme.backgroundColor)out.backgroundColor = colors+ if (Array.isArray(out.borderColor)) {+ out.borderColor = out.borderColor.map(c => c ?? theme.backgroundColor)+ }const where = `data.datasets[${idx}]`if (padded) {warnings.push(@@ -250,11 +254,15 @@ function applyThemePalette(}} else {const color = theme.palette[idx % theme.palette.length]+ // Translucent wherever series are drawn over one another — a solid+ // radar fill hides every series behind the first.+ const fill = TRANSLUCENT_FILL_TYPES.has(chartType) ? `${color}33` : colorif (!out.borderColor) out.borderColor = color- if (!out.backgroundColor) {- // Translucent wherever series are drawn over one another — a solid- // radar fill hides every series behind the first.- out.backgroundColor = TRANSLUCENT_FILL_TYPES.has(chartType) ? `${color}33` : color+ if (!out.backgroundColor) out.backgroundColor = fill+ // A value with no color of its own in a list takes the series color.+ if (Array.isArray(out.borderColor)) out.borderColor = out.borderColor.map(c => c ?? color)+ if (Array.isArray(out.backgroundColor)) {+ out.backgroundColor = out.backgroundColor.map(c => c ?? fill)}if (chartType === 'area' && out.fill === undefined) {out.fill = true@@ -452,7 +460,7 @@ function resolveChartType(ds.data = paired.map(p => p.v)o.labels = paired.map(p => p.l)// Per-stage colors follow their stage through the sort.- const follow = (colors: string | string[] | undefined) =>+ const follow = (colors: string | Array<string | undefined> | undefined) =>Array.isArray(colors) ? paired.map(p => colors[p.i % colors.length]) : colorsds.backgroundColor = follow(ds.backgroundColor)ds.borderColor = follow(ds.borderColor)diff --git a/mcp-host/src/workflow/chartPlugins.ts b/mcp-host/src/workflow/chartPlugins.tsindex 0e7eba1c2..0ab5fe589 100644--- a/mcp-host/src/workflow/chartPlugins.ts+++ b/mcp-host/src/workflow/chartPlugins.ts@@ -482,11 +482,16 @@ function distance(a: string, b: string): number {*/export function sliceColors(count: number,- requested: string[] | undefined,+ requested: ReadonlyArray<string | undefined> | undefined,palette: string[],background: string): { colors: string[]; padded: boolean; repeated: boolean } {- const own = (requested ?? []).filter(c => typeof c === 'string').slice(0, count)+ // A requested color stays on its slice; a slice without one takes the next+ // color the theme adds.+ const ownAt = (i: number) => (typeof requested?.[i] === 'string' ? requested[i] : undefined)+ const own = Array.from({ length: count }, (_, i) => ownAt(i)).filter(+ (c): c is string => c !== undefined+ )const light = (rgb(background) ?? [255, 255, 255]).reduce((sum, c) => sum + c, 0) > 382const shades = [...palette.map(c => mix(c, background, 0.45)),@@ -499,7 +504,8 @@ export function sliceColors(if (pool.every(used => distance(used, candidate) >= MIN_COLOR_DISTANCE)) pool.push(candidate)}const distinct = pool.length- const colors = Array.from({ length: count }, (_, i) => pool[i % distinct])+ let next = own.length+ const colors = Array.from({ length: count }, (_, i) => ownAt(i) ?? pool[next++ % distinct])if (count > distinct && count > 1 && colors[count - 1] === colors[0]) {colors[count - 1] = pool[Math.min(1, distinct - 1)]}diff --git a/mcp-host/src/workflow/dashboardCharts.ts b/mcp-host/src/workflow/dashboardCharts.tsindex f35519cb9..80fa77d0b 100644--- a/mcp-host/src/workflow/dashboardCharts.ts+++ b/mcp-host/src/workflow/dashboardCharts.ts@@ -189,7 +189,8 @@ function checkColors(}out[key] = value.map((v, j) => {if (isCssColor(v)) return v- bad.push({ at: `${at}[${j}]`, value: v })+ // A value placed by its label with no color of its own takes the palette's.+ if (v !== undefined) bad.push({ at: `${at}[${j}]`, value: v })return paletteColor(SLICE_TYPES.has(type) ? j : index).color})}@@ -290,7 +291,7 @@ function translate(chart.labels = pairs.map(p => p.l)// Per-stage colors follow their stage through the sort, as in the PNG// funnel; a short list wraps as Chart.js wraps it.- const follow = (colors: string | string[] | undefined) =>+ const follow = (colors: string | Array<string | undefined> | undefined) =>Array.isArray(colors) && colors.length > 0? pairs.map(p => colors[p.i % colors.length]): colors