e2b-dev / E2B

Secure open source cloud runtime for AI apps & AI agents
https://e2b.dev/docs
Apache License 2.0
7.02k stars 458 forks source link

JS SDK is failing in Vercel's serverless functions #259

Closed mlejva closed 3 months ago

mlejva commented 11 months ago

Describe the bug When you try to call runCode() od CodeInterpreter sandbox it throws "bufferutil.mask is not a function" which appears to be related to this issue https://github.com/websockets/bufferutil/issues/146

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { CodeInterpreter } from '@e2b/sdk'
import { kv } from '@vercel/kv'

const TTL = 10 * 60_000 // 10 minutes

export async function POST(req: NextRequest, res: NextResponse) {
  let sbx: CodeInterpreter
  const now = Date.now()
  if (sandboxID && lastUsed && now - lastUsed < TTL) {
    console.log('Reconnecting to sandbox:', sandboxID)
    sbx = await CodeInterpreter.reconnect(sandboxID)
  } else {
    console.log('Creating new sandbox...')
    sbx = await CodeInterpreter.create({
      logger: console,
    })
    console.log('Created sandbox:', sbx.id)
    await kv.hset('sandbox', { id: sbx.id })
  }

  const { stdout, stderr } = await sbx.runPython('print("hello")')
  console.log(stdout)
  console.error(stderr)

  // Keep sandbox alive for 10 minutes
  await kv.hset('sandbox', { lastUsed: now })
  await sbx.keepAlive(TTL)
  await sbx.close()

  return NextResponse.json({},
    {
      status: 200,
    },
  )
}

E2B-445

ValentaTomas commented 3 months ago

The new SDK is not using bufferutil dependency — this bug is no longer relevant.