Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): read a chart's decimal convention only from values it plots
marcoteba2d92f193475
diff --git a/mcp-host/src/workflow/__tests__/chartData.test.ts b/mcp-host/src/workflow/__tests__/chartData.test.tsindex 5cc89e1b9..c3df2da30 100644--- a/mcp-host/src/workflow/__tests__/chartData.test.ts+++ b/mcp-host/src/workflow/__tests__/chartData.test.ts@@ -678,6 +678,78 @@ describe('single-series charts', () => {})})+describe('the decimal convention is read only from values the chart plots', () => {+ it("is not taken from a record's label", () => {+ const read = (label: string) =>+ normalizeChartData({ datasets: [{ label: 'S', data: [{ label, value: '1,234' }] }] }, bar)+ .datasets[0].data+ expect(read('A')).toEqual([1234])+ // Only the label changes: it is text, not a value.+ expect(read('1.234,5')).toEqual([1234])+ })++ it('is not taken from the label of a [label, value] pair', () => {+ const r = normalizeChartData({ datasets: [{ label: 'S', data: [['1.234,5', '1,234']] }] }, bar)+ expect(r.datasets[0].data).toEqual([1234])+ })++ it.each(['gauge', 'waterfall', 'funnel'] as const)(+ '%s: is not taken from a dropped series, which is not read at all',+ chartType => {+ const kept = { label: 'Kept', data: ['1,234'] }+ const r = normalizeChartData(+ { labels: ['A'], datasets: [kept, { label: 'Dropped', data: ['1.234,5'] }] },+ { chartType }+ )+ expect(r.datasets[0].data).toEqual([1234])+ // A dropped series the chart could not read does not reject the chart.+ const unread = normalizeChartData(+ { labels: ['A'], datasets: [kept, { label: 'Dropped', data: ['not a number'] }] },+ { chartType }+ )+ expect(unread.datasets[0].data).toEqual([1234])+ expect(unread.warnings.join(' ')).toContain('1 extra dataset(s) were dropped')+ }+ )++ it.each([+ [+ 'points',+ [+ { x: '1.234,5', y: '1' },+ { x: '2', y: '1.200' },+ ],+ ],+ [+ '[x, y] arrays',+ [+ ['1.234,5', '1'],+ ['2', '1.200'],+ ],+ ],+ ])('is taken from the coordinates of scatter %s', (_label, data) => {+ const r = normalizeChartData({ datasets: [{ label: 'S', data }] }, { chartType: 'scatter' })+ expect(r.datasets[0].data).toEqual([+ { x: 1234.5, y: 1 },+ { x: 2, y: 1200 },+ ])+ })++ it('is still taken from the values of every series a chart draws', () => {+ const r = normalizeChartData(+ {+ labels: ['A', 'B'],+ datasets: [+ { label: 'One', data: ['1.200', '3'] },+ { label: 'Two', data: ['1.234,5', '2'] },+ ],+ },+ bar+ )+ expect(r.datasets[0].data).toEqual([1200, 3])+ })+})+describe('values written as text that no chart can hold', () => {it.each([['1e400', /too large for a chart/],diff --git a/mcp-host/src/workflow/chartData.ts b/mcp-host/src/workflow/chartData.tsindex d0c0b64da..a13295ff9 100644--- a/mcp-host/src/workflow/chartData.ts+++ b/mcp-host/src/workflow/chartData.ts@@ -230,9 +230,17 @@ class NumberReader {}}-/** Every string a chart's data holds, at the depths values are read from. */-function dataTexts(datasets: unknown[]): string[] {+/**+ * The text of every value the chart reads as a number, where the point readers+ * read it: a plain value, the second member of a [label, value] pair, a+ * record's value field (the first one that holds a number, as readRecordPoint+ * takes it) or, on scatter and bubble, x, y and the radius of a point or of an+ * [x, y, r] array. Labels and other+ * fields are text, not values, so they never set the decimal convention.+ */+function dataTexts(datasets: unknown[], chartType: string): string[] {const texts: string[] = []+ const isXY = XY_TYPES.has(chartType)const add = (v: unknown) => {if (typeof v === 'string') texts.push(v)}@@ -240,9 +248,33 @@ function dataTexts(datasets: unknown[]): string[] {const data = (ds as { data?: unknown } | null)?.dataif (!Array.isArray(data)) continuefor (const entry of data) {- add(entry)- if (Array.isArray(entry)) entry.forEach(add)- else if (entry && typeof entry === 'object') Object.values(entry).forEach(add)+ if (typeof entry === 'string') add(entry)+ else if (Array.isArray(entry)) {+ if (isXY && entry.length >= 2) {+ add(entry[0])+ add(entry[1])+ if (chartType === 'bubble') add(entry[2])+ } else if (!isXY && entry.length === 2) add(entry[1])+ } else if (entry && typeof entry === 'object') {+ const obj = entry as Record<string, unknown>+ if (isXY) {+ add(obj.x)+ add(obj.y)+ if (chartType === 'bubble') add(obj.r ?? obj.radius ?? obj.size)+ continue+ }+ for (const key of VALUE_KEYS) {+ if (!(key in obj)) continue+ const value = obj[key]+ add(value)+ // NumberReader.read returns or throws here, so readRecordPoint stops.+ const read =+ typeof value === 'number'+ ? Number.isFinite(value)+ : typeof value === 'string' && parseText(value) !== undefined+ if (read) break+ }+ }}}return texts@@ -569,12 +601,17 @@ export function normalizeChartData(raw: unknown, opts: NormalizeOptions): Normal}const isXY = XY_TYPES.has(chartType)- const reader = new NumberReader(dataTexts(obj.datasets))+ // A single-series chart draws only the first dataset, so the rest are left+ // out before anything is read from them: their values must not set the+ // decimal convention, their labels must not become categories, and a value+ // the chart could not read must not reject it.+ const retained = SINGLE_SERIES_TYPES.has(chartType) ? obj.datasets.slice(0, 1) : obj.datasets+ const reader = new NumberReader(dataTexts(retained, chartType))const datasets: NormalizedDataset[] = []/** Per dataset, the label each value carried, when every value carried one. */const labelled: Array<string[] | undefined> = []- obj.datasets.forEach((dsRaw, index) => {+ retained.forEach((dsRaw, index) => {if (dsRaw === null || typeof dsRaw !== 'object' || Array.isArray(dsRaw)) {fail(`${root}.datasets[${index}] must be an object with a \`data\` array.`)}@@ -599,16 +636,12 @@ export function normalizeChartData(raw: unknown, opts: NormalizeOptions): Normaldatasets.push({ ...(carryStyle(ds) as NormalizedDataset), data: series.data })})- // 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 - 1+ if (retained.length < obj.datasets.length) {+ const extra = obj.datasets.length - retained.lengthwarnings.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)) {