From 84f12208e4260a0db869ec6a56e374d59768d3dc Mon Sep 17 00:00:00 2001 From: lemonzoey <25550414+lemonzoey@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:21:18 +0800 Subject: [PATCH] fix(code-interpreter): list text and chart in JS Result.formats() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Result.formats()` reported every available representation except `text` and `chart`, so a result carrying either reported no formats at all — a text+chart result returned `[]`. Append `text` and `chart` in the same positions the Python SDK's `Result.formats()` uses, so JS now matches Python's ordering (text, the MIME types, data, chart, then extra keys). Co-Authored-By: Kscc --- .../code-interpreter-formats-text-chart.md | 5 + packages/code-interpreter-js/src/messaging.ts | 6 ++ .../code-interpreter-js/tests/formats.test.ts | 91 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 .changeset/code-interpreter-formats-text-chart.md create mode 100644 packages/code-interpreter-js/tests/formats.test.ts diff --git a/.changeset/code-interpreter-formats-text-chart.md b/.changeset/code-interpreter-formats-text-chart.md new file mode 100644 index 0000000000..17e037b022 --- /dev/null +++ b/.changeset/code-interpreter-formats-text-chart.md @@ -0,0 +1,5 @@ +--- +'@e2b/code-interpreter': patch +--- + +fix(code-interpreter): list `text` and `chart` in JS `Result.formats()` — the method reported every available representation except `text` and `chart`, so a result carrying either returned an empty formats list (e.g. a text+chart result reported `[]`). It now appends `text` and `chart` to match the Python SDK's `Result.formats()` ordering. diff --git a/packages/code-interpreter-js/src/messaging.ts b/packages/code-interpreter-js/src/messaging.ts index c145f5b46f..d9efed76fc 100644 --- a/packages/code-interpreter-js/src/messaging.ts +++ b/packages/code-interpreter-js/src/messaging.ts @@ -206,6 +206,9 @@ export class Result { */ formats(): string[] { const formats = [] + if (this.text) { + formats.push('text') + } if (this.html) { formats.push('html') } @@ -236,6 +239,9 @@ export class Result { if (this.data) { formats.push('data') } + if (this.chart) { + formats.push('chart') + } for (const key of Object.keys(this.extra)) { formats.push(key) diff --git a/packages/code-interpreter-js/tests/formats.test.ts b/packages/code-interpreter-js/tests/formats.test.ts new file mode 100644 index 0000000000..7923d40028 --- /dev/null +++ b/packages/code-interpreter-js/tests/formats.test.ts @@ -0,0 +1,91 @@ +import { expect, test } from 'vitest' + +import { Execution, parseOutput } from '../src/messaging' + +// `Result.formats()` mirrors Python `e2b_code_interpreter/models.py` +// `Result.formats()`, which lists every available representation: text, the +// MIME types, data, chart, and any extra keys (in that order). The JS impl +// previously omitted `text` and `chart`, so a result carrying either reported +// no formats at all — diverging from Python and breaking discovery via +// `formats()`. + +function resultFrom(message: object) { + const execution = new Execution() + parseOutput( + execution, + JSON.stringify({ type: 'result', is_main_result: true, ...message }) + ) + return execution.results[0] +} + +test('lists text when the result has a text representation', () => { + expect(resultFrom({ text: 'hello' }).formats()).toContain('text') +}) + +test('lists chart when the result has chart data', () => { + const result = resultFrom({ + chart: { + type: 'bar', + title: 'T', + elements: [], + x_scale: { type: 'linear' }, + y_scale: { type: 'linear' }, + }, + }) + + expect(result.formats()).toContain('chart') +}) + +test('lists both text and chart in the Python order', () => { + const formats = resultFrom({ + text: 'hello', + chart: { + type: 'bar', + title: 'T', + elements: [], + x_scale: { type: 'linear' }, + y_scale: { type: 'linear' }, + }, + }).formats() + + expect(formats).toEqual(['text', 'chart']) +}) + +test('lists every format in the same order as the Python SDK', () => { + const formats = resultFrom({ + text: 't', + html: 'h', + markdown: 'm', + svg: 's', + png: 'p', + jpeg: 'j', + pdf: 'd', + latex: 'l', + json: '{}', + javascript: 'js', + data: { columns: ['a'], rows: [[1]] }, + chart: { type: 'bar', title: 'T', elements: [] }, + custom: 'extra-value', + }).formats() + + // text, the MIME types, data, chart, then extra keys — matching Python. + expect(formats).toEqual([ + 'text', + 'html', + 'markdown', + 'svg', + 'png', + 'jpeg', + 'pdf', + 'latex', + 'json', + 'javascript', + 'data', + 'chart', + 'custom', + ]) +}) + +test('returns no formats for a bare result', () => { + expect(resultFrom({}).formats()).toEqual([]) +})