Skip to content

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

TypeScript

fix(mcp-host): keep chart values in order when their labels name none of the given ones

marcotebbef3b575b8ba
diff --git a/mcp-host/src/workflow/__tests__/chartData.test.ts b/mcp-host/src/workflow/__tests__/chartData.test.ts
index 17f120be9..755bcc16b 100644
--- a/mcp-host/src/workflow/__tests__/chartData.test.ts
+++ b/mcp-host/src/workflow/__tests__/chartData.test.ts
@@ -408,6 +408,62 @@ describe('normalizeChartData — field paths', () => {
})
describe('values that name their label', () => {
+ it('keep their order when their labels name none of the given ones', () => {
+ const pairs = normalizeChartData(
+ {
+ labels: ['Mon', 'Tue', 'Wed'],
+ datasets: [
+ {
+ data: [
+ [0, 5],
+ [1, 6],
+ [2, 7],
+ ],
+ },
+ ],
+ },
+ { chartType: 'line' }
+ )
+ expect(pairs.labels).toEqual(['Mon', 'Tue', 'Wed'])
+ expect(pairs.datasets[0].data).toEqual([5, 6, 7])
+ expect(pairs.warnings.join(' ')).toContain('name none of `data.labels`')
+ const captions = normalizeChartData(
+ {
+ labels: ['Q1', 'Q2', 'Q3'],
+ datasets: [
+ {
+ data: [
+ { label: 'Revenue', value: 10 },
+ { label: 'Revenue', value: 11 },
+ { label: 'Revenue', value: 12 },
+ ],
+ },
+ ],
+ },
+ { chartType: 'bar' }
+ )
+ expect(captions.datasets[0].data).toEqual([10, 11, 12])
+ })
+
+ it('match the given labels whatever their case', () => {
+ const r = normalizeChartData(
+ {
+ labels: ['Jan', 'Feb'],
+ datasets: [
+ {
+ data: [
+ { label: 'feb', value: 2 },
+ { label: 'jan', value: 1 },
+ ],
+ },
+ ],
+ },
+ { chartType: 'bar' }
+ )
+ expect(r.labels).toEqual(['Jan', 'Feb'])
+ expect(r.datasets[0].data).toEqual([1, 2])
+ })
+
it('are placed at their label, not in the order sent', () => {
const r = normalizeChartData(
{
diff --git a/mcp-host/src/workflow/chartData.ts b/mcp-host/src/workflow/chartData.ts
index fff6396ab..cf194be43 100644
--- a/mcp-host/src/workflow/chartData.ts
+++ b/mcp-host/src/workflow/chartData.ts
@@ -648,28 +648,38 @@ function placeByLabel(
warnings: string[]
): string[] {
const categories = given ? [...given] : []
+ // Labels match whatever their case and surrounding spaces: 'jan' is the 'Jan' column.
+ const key = (label: string) => label.trim().toLowerCase()
const index = new Map<string, number>()
categories.forEach((label, i) => {
- if (!index.has(label)) index.set(label, i)
+ if (!index.has(key(label))) index.set(key(label), i)
})
let moved = false
let repeated = 0
+ const unmatched: number[] = []
labelled.forEach((own, d) => {
if (!own) return
+ // Labels of its own that name none of the given ones are not categories but
+ // captions, such as a series name repeated on every value, so the values
+ // keep their positions.
+ if (given?.length && !own.some(label => index.has(key(label)))) {
+ unmatched.push(d)
+ return
+ }
const values = datasets[d].data as Array<number | null>
const placed: Array<number | null> = new Array(categories.length).fill(null)
const seen = new Set<string>()
own.forEach((label, i) => {
- if (seen.has(label)) {
+ if (seen.has(key(label))) {
repeated++
return
}
- seen.add(label)
- let at = index.get(label)
+ seen.add(key(label))
+ let at = index.get(key(label))
if (at === undefined) {
at = categories.length
categories.push(label)
- index.set(label, at)
+ index.set(key(label), at)
}
if (at !== i) moved = true
placed[at] = values[i]
@@ -691,6 +701,12 @@ function placeByLabel(
`${root}: ${repeated} value(s) repeated a label already used in the same series and were left out.`
)
}
+ if (unmatched.length > 0) {
+ warnings.push(
+ `${unmatched.map(d => `${root}.datasets[${d}]`).join(', ')}: the labels on the values name ` +
+ `none of \`${root}.labels\`, so the values were placed in the order sent.`
+ )
+ }
return categories
}