Skip to content

Commit ed495f5

Browse files
Update sandbox templates and configuration
1 parent 2a80b44 commit ed495f5

3 files changed

Lines changed: 90 additions & 59 deletions

File tree

app/api/sandbox/route.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
11
import { FragmentSchema } from '@/lib/schema'
22
import { ExecutionResultInterpreter, ExecutionResultWeb } from '@/lib/types'
33
import { Sandbox } from '@e2b/code-interpreter'
4+
import { FileSystemNode } from '@/components/file-tree'
45

56
const sandboxTimeout = 10 * 60 * 1000
67

8+
async function fetchSandboxFiles(sbx: Sandbox): Promise<FileSystemNode[]> {
9+
try {
10+
const result = await sbx.commands.run(
11+
'find /home/user -type f -o -type d 2>/dev/null | grep -v "node_modules" | sort'
12+
)
13+
14+
if (result.exitCode !== 0) {
15+
console.error('Error listing files:', result.stderr)
16+
return []
17+
}
18+
19+
return parseFileTree(result.stdout)
20+
} catch (error) {
21+
console.error('Error fetching sandbox files:', error)
22+
return []
23+
}
24+
}
25+
26+
function parseFileTree(output: string): FileSystemNode[] {
27+
const lines = output.trim().split('\n').filter(line => line.trim())
28+
const root: FileSystemNode[] = []
29+
const nodeMap = new Map<string, FileSystemNode>()
30+
31+
const paths = lines
32+
.map(line => line.trim())
33+
.filter(path => path.startsWith('/home/user/'))
34+
.sort()
35+
36+
for (const fullPath of paths) {
37+
const relativePath = fullPath.replace('/home/user/', '')
38+
if (!relativePath) continue
39+
40+
const parts = relativePath.split('/')
41+
const name = parts[parts.length - 1]
42+
const parentPath = parts.slice(0, -1).join('/')
43+
44+
const node: FileSystemNode = {
45+
name,
46+
isDirectory: false,
47+
path: `/${relativePath}`,
48+
children: []
49+
}
50+
51+
nodeMap.set(relativePath, node)
52+
53+
if (parentPath === '') {
54+
root.push(node)
55+
} else {
56+
const parent = nodeMap.get(parentPath)
57+
if (parent) {
58+
if (!parent.children) {
59+
parent.children = []
60+
}
61+
parent.children.push(node)
62+
parent.isDirectory = true
63+
}
64+
}
65+
}
66+
67+
return root
68+
}
69+
770
export const maxDuration = 60
871
export const runtime = 'nodejs'
972
export const dynamic = 'force-dynamic'
@@ -97,6 +160,9 @@ export async function POST(req: Request) {
97160
if (fragment.template === 'code-interpreter-v1') {
98161
const { logs, error, results } = await sbx.runCode(fragment.code || '')
99162

163+
// Fetch file tree after execution
164+
const files = await fetchSandboxFiles(sbx)
165+
100166
return new Response(
101167
JSON.stringify({
102168
sbxId: sbx?.sandboxId,
@@ -105,6 +171,7 @@ export async function POST(req: Request) {
105171
stderr: logs.stderr,
106172
runtimeError: error,
107173
cellResults: results,
174+
files,
108175
} as ExecutionResultInterpreter),
109176
{ headers: { 'Content-Type': 'application/json' } }
110177
)
@@ -116,11 +183,15 @@ export async function POST(req: Request) {
116183
},
117184
})
118185

186+
// Fetch file tree after project setup
187+
const files = await fetchSandboxFiles(sbx)
188+
119189
return new Response(
120190
JSON.stringify({
121191
sbxId: sbx?.sandboxId,
122192
template: fragment.template,
123193
url: `https://${sbx?.getHost(fragment.port || 80)}`,
194+
files,
124195
} as ExecutionResultWeb),
125196
{ headers: { 'Content-Type': 'application/json' } }
126197
)
Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,24 @@
1-
# Use ARG to define versions for easier updates
2-
ARG NODE_VERSION=22-slim
3-
ARG NEXT_APP_VERSION=14.2.20
4-
ARG SHADCN_VERSION=2.1.7
1+
FROM node:21-slim
52

6-
# ---- Base Image ----
7-
# Use a base image with Node pre-installed
8-
FROM node:${NODE_VERSION} as base
3+
# Install curl
4+
RUN apt-get update && apt-get install -y curl && apt-get clean && rm -rf /var/lib/apt/lists/*
95

10-
# Set up the environment
11-
ENV PNPM_HOME="/pnpm"
12-
ENV PATH="$PNPM_HOME:$PATH"
13-
RUN corepack enable
6+
# Install dependencies and customize sandbox
7+
WORKDIR /home/user/nextjs-app
148

15-
WORKDIR /home/user/app
16-
17-
# ---- Builder Stage ----
18-
# This stage is for building the Next.js application
19-
FROM base as builder
20-
21-
# Install necessary build tools
22-
RUN apt-get update && apt-get install -y --no-install-recommends curl git && \
23-
apt-get clean && rm -rf /var/lib/apt/lists/*
24-
25-
# Create the Next.js application
26-
# Using --use-pnpm for faster dependency installation
27-
RUN npx create-next-app@${NEXT_APP_VERSION} . --ts --tailwind --no-eslint --import-alias "@/*" --use-pnpm --no-app --no-src-dir
28-
29-
# Copy custom configurations
9+
RUN npx create-next-app@14.2.20 . --ts --tailwind --no-eslint --import-alias "@/*" --use-npm --no-app --no-src-dir
3010
COPY _app.tsx pages/_app.tsx
3111

32-
# Initialize and add shadcn components
33-
# The -y flag accepts all defaults
34-
RUN npx shadcn-cli@${SHADCN_VERSION} init -y
35-
RUN npx shadcn-cli@${SHADCN_VERSION} add --all -y
36-
37-
# Install additional dependencies
38-
RUN pnpm install posthog-js
39-
40-
# ---- Runtime Stage ----
41-
# This stage creates the final, smaller image
42-
FROM base as runtime
43-
44-
WORKDIR /home/user/app
45-
46-
# Copy dependencies and application code from the builder stage
47-
COPY --from=builder /home/user/app/package.json /home/user/app/pnpm-lock.yaml ./
48-
RUN pnpm install --prod
49-
50-
COPY --from=builder /home/user/app/ ./
51-
52-
# Copy the compile script and make it executable
53-
COPY compile_page.sh /compile_page.sh
54-
RUN chmod +x /compile_page.sh
12+
RUN npx shadcn@2.1.7 init -d
13+
RUN npx shadcn@2.1.7 add --all
14+
RUN npm install posthog-js
5515

56-
# Create a non-root user for better security
57-
RUN addgroup --system --gid 1001 nodejs
58-
RUN adduser --system --uid 1001 nextjs
59-
USER nextjs
16+
# Move the Nextjs app to the home directory and remove the nextjs-app directory
17+
RUN mv /home/user/nextjs-app/* /home/user/ && rm -rf /home/user/nextjs-app
6018

61-
# Expose the port Next.js runs on
62-
EXPOSE 3000
19+
# Copy script to user home directory so it's visible in the editor
20+
COPY compile_page.sh /home/user/compile_page.sh
21+
RUN chmod +x /home/user/compile_page.sh
6322

64-
# Set the default command to start the Next.js app in development mode
65-
CMD ["pnpm", "run", "dev"]
23+
# Set working directory to user home
24+
WORKDIR /home/user
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# You can use most Debian-based base images
21
FROM node:21-slim
3-
FROM e2b AS Sandbox
42

53
WORKDIR /home/user/vue-app
64

@@ -10,3 +8,6 @@ COPY nuxt.config.ts /home/user/vue-app/nuxt.config.ts
108

119
# Move the Vue app to the home directory and remove the Vue directory
1210
RUN mv /home/user/vue-app/* /home/user/ && rm -rf /home/user/vue-app
11+
12+
# Set working directory to user home
13+
WORKDIR /home/user

0 commit comments

Comments
 (0)