import { Sandbox } from '@e2b/code-interpreter'
import OpenAI from 'openai'
// 1. Get code from the LLM
const openai = new OpenAI()
const response = await openai.chat.completions.create({
model: 'gpt-5.2-mini',
messages: [
{
role: 'system',
content:
'Generate a single Next.js page component using TypeScript and Tailwind CSS. Return only code, no markdown.',
},
{ role: 'user', content: 'Build a calculator app' },
],
})
const generatedCode = response.choices[0].message.content
// 2. Create a sandbox and prepare the app
const sandbox = await Sandbox.create('nextjs-app', { timeoutMs: 300_000 })
await sandbox.files.write('/home/user/pages/index.tsx', generatedCode)
// 3. Return the preview URL
const previewUrl = `https://${sandbox.getHost(3000)}`
console.log('App is live at:', previewUrl)
// Later, when the user is done:
await sandbox.kill()