Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/code-interpreter-formats-text-chart.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions packages/code-interpreter-js/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export class Result {
*/
formats(): string[] {
const formats = []
if (this.text) {
formats.push('text')
}
if (this.html) {
formats.push('html')
}
Expand Down Expand Up @@ -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)
Expand Down
91 changes: 91 additions & 0 deletions packages/code-interpreter-js/tests/formats.test.ts
Original file line number Diff line number Diff line change
@@ -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([])
})