e2b-dev / code-interpreter

Python & JS/TS SDK for running AI-generated code/code interpreting in your AI app
https://e2b.dev
Apache License 2.0
1.27k stars 96 forks source link

Top level async/await doesn't work in JavaScript runtime #44

Open mlejva opened 1 month ago

mlejva commented 1 month ago

When running JS code in the Sandbox, the top level promise is never awaited:

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create({apiKey: ''})

const code = `
 const printResult = async () => {
      return 'foo';
    };

  printResult()
    .then((result) => {
      console.log("glama.result");
    })
    .catch((error) => {
      console.log("glama.error");
    });
`

console.log('running code')
const ex = await sandbox.runCode(code, {
  language: 'js',
  onError: (error) => {
    console.log('error', error)
  },
  onStdout: (stdout) => {
    console.log('stdout', stdout)
  },
  onStderr: (stderr) => {
    console.log('stderr', stderr)
  },
})
console.log('code executed', ex)

The code above will produce a following output

running code
code executed Execution {
  results: [
    Result {
      isMainResult: true,
      text: 'Promise { <pending> }',
      html: undefined,
      markdown: undefined,
      svg: undefined,
      png: undefined,
      jpeg: undefined,
      pdf: undefined,
      latex: undefined,
      json: undefined,
      javascript: undefined,
      raw: [Object],
      data: undefined,
      chart: undefined,
      extra: {}
    }
  ],
  logs: { stdout: [], stderr: [] },
  error: undefined,
  executionCount: 2
}

The text field says 'Promise { <pending> }' which means the promise was never awaited.

linear[bot] commented 1 month ago

E2B-1095 Top level async/await doesn't work in JavaScript runtime

punkpeye commented 4 weeks ago

There is no top-level await in this code by the way.

Anyway, is there a workaround – I just need to call a function that results Promise and get that response somehow.

mlejva commented 4 weeks ago

There is no top-level await in this code by the way.

I should have clarified. There's a top level promise that isn't ever awaited.

Anyway, is there a workaround – I just need to call a function that results Promise and get that response somehow.

@jakubno is looking how we can fix it. One of the potential fixes is using a different kernel. We're looking at other kernels, ideally with TypeScript support.

mlejva commented 3 weeks ago

@punkpeye we're adding a new JS kernel in this branch https://github.com/e2b-dev/code-interpreter/tree/change-js-kernel

punkpeye commented 2 weeks ago

Says not-found.

Also, that Kernel worked completely fine. Looks like no one just read the documentation.

mlejva commented 2 weeks ago

Says not-found.

Also, that Kernel worked completely fine. Looks like no one just read the documentation.

It got merged - https://github.com/e2b-dev/code-interpreter/commit/5894232f2360c1462bc099cdaeaec6902fca3b44 It's Deno. We're also looking at other kernels.

Still in preview though.