Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Prepare for custom docs sandbox

Vasek Mlejnskyb75d2baffff5
diff --git a/apps/docs/sandbox/e2b.Dockerfile b/apps/docs/sandbox/e2b.Dockerfile
new file mode 100644
index 000000000..cfecfe976
--- /dev/null
+++ b/apps/docs/sandbox/e2b.Dockerfile
@@ -0,0 +1,13 @@
+FROM e2bdev/template:base
+
+RUN mkdir /code
+WORKDIR /code
+
+# Install e2b JS SDK
+RUN npm init es6 -y
+RUN npm i @e2b/sdk
+
+# Install e2b Python SDK
+RUN pip install e2b
+
+WORKDIR /
diff --git a/apps/docs/src/components/Code.tsx b/apps/docs/src/components/Code.tsx
index bc7841e27..6665386d1 100644
--- a/apps/docs/src/components/Code.tsx
+++ b/apps/docs/src/components/Code.tsx
@@ -14,7 +14,7 @@ import {
import { CopyButton } from '@/components/CopyButton'
import { useApiKey } from '@/utils/useUser'
import { ProcessMessage } from '@e2b/sdk'
-import { useSandboxesStore } from '@/utils/useSandboxes'
+import { useSandboxStore } from '@/utils/useSandbox'
import { useSignIn } from '@/utils/useSignIn'
import { LangShort, languageNames, mdLangToLangShort } from '@/utils/consts'
import { usePostHog } from 'posthog-js/react'
@@ -54,8 +54,8 @@ function CodePanel({
const codeGroupContext = useContext(CodeGroupContext)
- const sandboxDef = useSandboxesStore((s) => s.sandboxes[lang])
- const initSandbox = useSandboxesStore((s) => s.initSandbox)
+ const sandboxDef = useSandboxStore((s) => s.sandbox)
+ const initSandbox = useSandboxStore((s) => s.initSandbox)
const { outputLines, dispatch } = useOutputReducer()
const [isRunning, setIsRunning] = useState(false)
@@ -93,11 +93,11 @@ function CodePanel({
try {
if (!sandbox) {
if (sandboxDef?.promise) {
- console.log(`Sandbox for "${lang}" is still opening, waiting...`)
+ console.log('Sandbox is still being created, waiting...')
sandbox = await sandboxDef.promise
} else {
- console.log(`Sandbox not ready yet for "${lang}", opening...`)
- sandbox = await initSandbox(lang, apiKey)
+ console.log('Sandbox not ready yet, creating...')
+ sandbox = await initSandbox(apiKey)
}
}
} catch (err) {
diff --git a/apps/docs/src/utils/consts.ts b/apps/docs/src/utils/consts.ts
index a3115bb7c..eec187af2 100644
--- a/apps/docs/src/utils/consts.ts
+++ b/apps/docs/src/utils/consts.ts
@@ -9,6 +9,8 @@ export enum LangShort {
py = 'py',
}
+export const docsSandboxName = 'docs'
+
export const languageToLangShort: Record<string, LangShort> = {
// TODO: Nicer
'JavaScript & TypeScript': LangShort.js,
diff --git a/apps/docs/src/utils/useSandbox.tsx b/apps/docs/src/utils/useSandbox.tsx
new file mode 100644
index 000000000..fd8623cf5
--- /dev/null
+++ b/apps/docs/src/utils/useSandbox.tsx
@@ -0,0 +1,77 @@
+import { Sandbox } from '@e2b/sdk'
+import { create } from 'zustand'
+import { docsSandboxName } from '@/utils/consts'
+
+function log(message?: any, ...optionalParams: any[]) {
+ if (process.env.NODE_ENV === 'production') return
+ else console.log(message, optionalParams)
+}
+
+// const envIds = {
+// js: 'rki5dems9wqfm4r03t7g',
+// py: 'rki5dems9wqfm4r03t7g',
+// }
+
+// const preps = {
+// js: 'npm init es6 -y && npm install @e2b/sdk',
+// py: 'pip install e2b',
+// }
+
+// TODO: Consider using immer for easier state updates
+export const useSandboxStore = create<SandboxStore>((set, get) => ({
+ sandbox: null,
+ initSandbox: async (apiKey: string): Promise<Sandbox> => {
+ // eslint-disable-next-line no-async-promise-executor
+ return new Promise(async (resolve, reject) => {
+ const maybeExistingSandbox = get().sandbox
+ if (maybeExistingSandbox?.sandbox) {
+ log('Sandbox already ready')
+ resolve(maybeExistingSandbox.sandbox)
+ return
+ } else if (maybeExistingSandbox?.promise) {
+ log('Sandbox in progress, waiting...')
+ await maybeExistingSandbox.promise
+ resolve(maybeExistingSandbox.sandbox)
+ } else {
+ log('Sandbox creating...')
+ try {
+ const sandboxP = Sandbox.create({
+ id: docsSandboxName,
+ apiKey,
+ })
+ // set promise to store so that other calls to initSandbox will wait for this one to finish
+ set((state) => ({
+ sandbox: {
+ ...state.sandbox,
+ sandbox: null,
+ promise: sandboxP,
+ }
+ }))
+ const newSandbox = await sandboxP // await creation
+ log('Sandbox created')
+ // set sandbox to store, so it can be used instead of creating a new one
+ set((state) => ({
+ sandbox: {
+ ...state.sandbox,
+ sandbox: newSandbox,
+ }
+ }))
+ resolve(newSandbox) // also resolve with the new sandbox for the initSandbox caller
+ } catch (err) {
+ console.info('Sandbox creation failed:', err)
+ reject(err)
+ }
+ }
+ })
+ },
+}))
+
+type SandboxMeta = {
+ sandbox: Sandbox;
+ promise: Promise<Sandbox>;
+};
+
+export type SandboxStore = {
+ sandbox: SandboxMeta;
+ initSandbox: (apiKey: string) => Promise<Sandbox>;
+};
diff --git a/apps/docs/src/utils/useSandboxes.tsx b/apps/docs/src/utils/useSandboxes.tsx
deleted file mode 100644
index 253f90756..000000000
--- a/apps/docs/src/utils/useSandboxes.tsx
+++ /dev/null
@@ -1,104 +0,0 @@
-import { Sandbox } from '@e2b/sdk'
-import { create } from 'zustand'
-import { LangShort } from '@/utils/consts'
-
-function stdHandler({ line, timestamp, error }) {
- if (process.env.NODE_ENV === 'production') return
-
- const timestampHumanFriendly = new Date(timestamp / 1000000) // timestamp is in nanoseconds
- .toISOString()
- .split('T')[1] // only time, date is not relevant for debugging
- .split('.')[0] // remove ms
- const emoji = error ? '\x1B[31mERROR' : '\x1B[34mINFO'
- console.log(`☁️ ${timestampHumanFriendly} ${emoji} ${line}`)
-}
-
-function log(message?: any, ...optionalParams: any[]) {
- if (process.env.NODE_ENV === 'production') return
- else console.log(message, optionalParams)
-}
-
-const envIds = {
- js: 'rki5dems9wqfm4r03t7g',
- py: 'rki5dems9wqfm4r03t7g',
-}
-
-const preps = {
- js: 'npm init es6 -y && npm install @e2b/sdk',
- py: 'pip install e2b',
-}
-
-// TODO: Consider using immer for easier state updates
-export const useSandboxesStore = create<SandboxStore>((set, get) => ({
- sandboxes: {
- js: null,
- py: null,
- },
- initSandbox: async (lang: LangShort, apiKey: string): Promise<Sandbox> => {
- // eslint-disable-next-line no-async-promise-executor
- return new Promise(async (resolve, reject) => {
- const maybeExistingSandbox = get().sandboxes[lang]
- if (maybeExistingSandbox?.sandbox) {
- log(`${lang} sandbox already ready`)
- resolve(maybeExistingSandbox.sandbox)
- return
- } else if (maybeExistingSandbox?.promise) {
- log(`${lang} sandbox in progress, waiting...`)
- await maybeExistingSandbox.promise
- resolve(maybeExistingSandbox.sandbox)
- } else {
- log(`${lang} sandbox creating...`)
- try {
- const sandboxP = Sandbox.create({ id: envIds[lang], apiKey })
- // set promise to store so that other calls to initSandbox will wait for this one to finish
- set((state) => ({
- sandboxes: {
- ...state.sandboxes,
- [lang]: {
- sandbox: null,
- promise: sandboxP,
- },
- },
- }))
- const newSandbox = await sandboxP // await creation
- log(`${lang} sandbox created, starting prep process...`)
- const proc = await newSandbox.process.start({
- cmd: preps[lang],
- onStdout: stdHandler,
- onStderr: stdHandler,
- cwd: '/code',
- })
- await proc.wait() // await prep process to finish
- log(`${lang} sandbox created and started`)
- // set sandbox to store, so it can be used instead of creating a new one
- set((state) => ({
- sandboxes: {
- ...state.sandboxes,
- [lang]: {
- sandbox: newSandbox,
- promise: null,
- },
- },
- }))
- resolve(newSandbox) // also resolve with the new sandbox for the initSandbox caller
- } catch (err) {
- console.info(`${lang} sandbox creation failed`, err)
- reject(err)
- }
- }
- })
- },
-}))
-
-type SandboxMeta = {
- sandbox: Sandbox;
- promise: Promise<Sandbox>;
-};
-
-export type SandboxStore = {
- sandboxes: {
- js: SandboxMeta;
- py: SandboxMeta;
- };
- initSandbox: (lang: LangShort, apiKey: string) => Promise<Sandbox>;
-};
diff --git a/apps/docs/src/utils/useUser.tsx b/apps/docs/src/utils/useUser.tsx
index d5c0470b7..b2be0177f 100644
--- a/apps/docs/src/utils/useUser.tsx
+++ b/apps/docs/src/utils/useUser.tsx
@@ -5,8 +5,7 @@ import { User } from '@supabase/supabase-auth-helpers/react'
import { createPagesBrowserClient } from '@supabase/auth-helpers-nextjs'
import { type Session } from '@supabase/supabase-js'
import * as Sentry from '@sentry/nextjs'
-import { useSandboxesStore } from './useSandboxes'
-import { LangShort } from './consts'
+import { useSandboxStore } from './useSandbox'
type UserContextType = {
isLoading: boolean;
@@ -31,7 +30,7 @@ export const CustomUserContextProvider = (props) => {
const [error, setError] = useState(null)
const [isLoading, setIsLoading] = useState(true)
const mounted = useRef<boolean>(false)
- const initSandbox = useSandboxesStore((state) => state.initSandbox)
+ const initSandbox = useSandboxStore((state) => state.initSandbox)
useEffect(() => {
mounted.current = true
@@ -101,9 +100,8 @@ export const CustomUserContextProvider = (props) => {
// as soon as we have apiKey, start initializing sandboxes, so they are ready when user want to use them
const apiKey = apiKeys?.[0]?.api_key
if (apiKey) {
- // We don't care about awaiting these, we just want to start them
- void initSandbox(LangShort.js, apiKey)
- void initSandbox(LangShort.py, apiKey)
+ // We don't care about awaiting this, we just want to start it
+ void initSandbox(apiKey)
}
const defaultTeamId = teams?.[0]?.team_id // TODO: Adjust when user can be part of multiple teams