Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add Section about Timeouts and Logging to docs (#105)

Jakub Novák9bd41eead66c
diff --git a/apps/docs/src/app/getting-started/advanced/page.mdx b/apps/docs/src/app/getting-started/advanced/page.mdx
new file mode 100644
index 000000000..e6d0f9c1e
--- /dev/null
+++ b/apps/docs/src/app/getting-started/advanced/page.mdx
@@ -0,0 +1,54 @@
+export const metadata = {
+ title: 'E2B Documentation',
+ description: 'SDK Advanced Usage',
+}
+
+# Advanced Usage
+
+## Timeouts
+
+The SDK has a number of timeouts that can be configured to control how long the SDK will wait for a response from the E2B API. The default is 60 seconds.
+There are some examples:
+
+### Timeout creating session
+
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/timeout/timeout_session.py' }}
+```
+```js {{ language: 'js', snippet: 'js/timeout/timeout_session.js' }}
+```
+</CodeGroup>
+
+### Timeout process
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/timeout/timeout_process.py' }}
+```
+```js {{ language: 'js', snippet: 'js/timeout/timeout_process.js' }}
+```
+</CodeGroup>
+
+### Timeout filesystem operations
+
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/timeout/timeout_filesystem.py' }}
+```
+```js {{ language: 'js', snippet: 'js/timeout/timeout_filesystem.js' }}
+```
+</CodeGroup>
+
+## Logging
+
+<LanguageSpecificText>
+ ```python {{ language: 'python', text: "E2B package uses the standard Python logging module You can configure the logging level, handlers and format as you would with any other Python application. \n\nBelow is an example of how to configure the logging module to log to the console." }}
+ ```
+ ```js {{ language: 'js', text: "E2B package logs only warnings & errors to the console by default. Below is an example of how to fully customize the logging levels.\n\nFeel free to override the logger with your own implementation with debug, info, warn, and error methods." }}
+ ```
+</LanguageSpecificText>
+
+
+<CodeGroup>
+ ```python {{ language: 'python', snippet: 'python/logging/e2b_logging.py' }}
+ ```
+ ```js {{ language: 'js', snippet: 'js/logging/e2b_logging.js' }}
+ ```
+</CodeGroup>
diff --git a/apps/docs/src/code/js/logging/e2b_logging.js b/apps/docs/src/code/js/logging/e2b_logging.js
new file mode 100644
index 000000000..8dfe569c7
--- /dev/null
+++ b/apps/docs/src/code/js/logging/e2b_logging.js
@@ -0,0 +1,15 @@
+import { Session } from '@e2b/sdk'
+
+const logger = {
+ debug: console.debug, // log debug messages, in default logger this is noop
+ info: console.info, // log info messages, in default logger this is noop
+ // don't forget to also specify warn & error handlers, otherwise they won't be logged when overriding the logger
+ warn: console.warn,
+ error: console.error,
+}
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+ logger,
+})
diff --git a/apps/docs/src/code/js/package.json b/apps/docs/src/code/js/package.json
index 15be418f0..ea030a10a 100644
--- a/apps/docs/src/code/js/package.json
+++ b/apps/docs/src/code/js/package.json
@@ -12,6 +12,6 @@
"author": "",
"license": "ISC",
"dependencies": {
- "@e2b/sdk": "^0.2.1"
+ "@e2b/sdk": "^0.2.2"
}
}
diff --git a/apps/docs/src/code/js/run_all.js b/apps/docs/src/code/js/run_all.js
index 0c9c18495..9521a1c86 100644
--- a/apps/docs/src/code/js/run_all.js
+++ b/apps/docs/src/code/js/run_all.js
@@ -13,5 +13,8 @@ import * as process_stop from './basics/process_stop.js'
import * as process_stream_stdout from './basics/process_stream_stdout.js'
import * as process_stream_stderr from './basics/process_stream_stderr.js'
import * as process_write_stdin from './basics/process_write_stdin.js'
+import * as timeout_session from './timeout/timeout_session.js'
+import * as timeout_process from './timeout/timeout_process.js'
+import * as timeout_filesystem from './timeout/timeout_filesystem.js'
-console.log('Done')
\ No newline at end of file
+console.log('Done')
diff --git a/apps/docs/src/code/js/timeout/timeout_filesystem.js b/apps/docs/src/code/js/timeout/timeout_filesystem.js
new file mode 100644
index 000000000..6d62a2589
--- /dev/null
+++ b/apps/docs/src/code/js/timeout/timeout_filesystem.js
@@ -0,0 +1,17 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// Timeout for the write operation
+await session.filesystem.write('hello.txt', 'Hello World!', { timeout: 3000 })
+
+// Timeout for the list operation
+const files = await session.filesystem.list('.', { timeout: 3000 })
+
+// Timeout for the read operation
+const content = await session.filesystem.read('hello.txt', { timeout: 3000 })
+
+await session.close()
diff --git a/apps/docs/src/code/js/timeout/timeout_process.js b/apps/docs/src/code/js/timeout/timeout_process.js
new file mode 100644
index 000000000..eea4114d0
--- /dev/null
+++ b/apps/docs/src/code/js/timeout/timeout_process.js
@@ -0,0 +1,15 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// Timeout for the process to start
+const npmInit = await session.process.start({
+ cmd: 'npm init -y',
+ timeout: 3000,
+})
+await npmInit.finished
+
+await session.close()
diff --git a/apps/docs/src/code/js/timeout/timeout_session.js b/apps/docs/src/code/js/timeout/timeout_session.js
new file mode 100644
index 000000000..aec4dd25f
--- /dev/null
+++ b/apps/docs/src/code/js/timeout/timeout_session.js
@@ -0,0 +1,10 @@
+import { Session } from '@e2b/sdk'
+
+// Timeout for the session to open
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+ timeout: 3000,
+})
+
+await session.close()
diff --git a/apps/docs/src/code/python/_run_all.py b/apps/docs/src/code/python/_run_all.py
index 94f2cb50a..e89423ec2 100644
--- a/apps/docs/src/code/python/_run_all.py
+++ b/apps/docs/src/code/python/_run_all.py
@@ -13,5 +13,8 @@ from basics.process_stop import main as process_stop_main
from basics.process_stream_stderr import main as process_stream_stderr_main
from basics.process_stream_stdout import main as process_stream_stdout_main
from basics.process_write_stdin import main as process_write_stdin_main
+from timeout.timeout_session import main as timeout_session_main
+from timeout.timeout_process import main as timeout_process_main
+from timeout.timeout_filesystem import main as timeout_filesystem_main
print("Done")
\ No newline at end of file
diff --git a/apps/docs/src/code/python/logging/e2b_logging.py b/apps/docs/src/code/python/logging/e2b_logging.py
new file mode 100644
index 000000000..6c8671f4e
--- /dev/null
+++ b/apps/docs/src/code/python/logging/e2b_logging.py
@@ -0,0 +1,36 @@
+import asyncio
+import logging
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+# Global logging configuration
+logging.basicConfig(level=logging.INFO, format="GLOBAL - [%(asctime)s] - %(name)-32s - %(levelname)7s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
+
+
+# Or configure only e2b logger
+
+# Get e2b logger
+e2b_logger = logging.getLogger("e2b")
+
+# Set e2b logger level to INFO
+e2b_logger.setLevel(logging.INFO)
+
+# Setup formatter
+formatter = logging.Formatter("E2B - [%(asctime)s] - %(name)-32s - %(levelname)7s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
+
+# Setup handler
+handler = logging.StreamHandler()
+handler.setFormatter(formatter)
+
+# Add handler to e2b logger
+e2b_logger.addHandler(handler)
+
+
+async def main():
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+ await session.filesystem.write("test.txt", "Hello World")
+ await session.close()
+
+asyncio.run(main())
diff --git a/apps/docs/src/code/python/timeout/timeout_filesystem.py b/apps/docs/src/code/python/timeout/timeout_filesystem.py
new file mode 100644
index 000000000..81bfdff01
--- /dev/null
+++ b/apps/docs/src/code/python/timeout/timeout_filesystem.py
@@ -0,0 +1,21 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+
+ # Timeout for the write operation
+ await session.filesystem.write("test.txt", "Hello World", timeout=3)
+
+ # Timeout for the list operation
+ await session.filesystem.list(".", timeout=3)
+
+ # Timeout for the read operation
+ await session.filesystem.read("test.txt", timeout=3)
+
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/code/python/timeout/timeout_process.py b/apps/docs/src/code/python/timeout/timeout_process.py
new file mode 100644
index 000000000..3fcf7f80f
--- /dev/null
+++ b/apps/docs/src/code/python/timeout/timeout_process.py
@@ -0,0 +1,16 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+
+ # Timeout for the process to start
+ npm_init = await session.process.start("npm init -y", timeout=5)
+ await npm_init
+
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/code/python/timeout/timeout_session.py b/apps/docs/src/code/python/timeout/timeout_session.py
new file mode 100644
index 000000000..e76a3b42f
--- /dev/null
+++ b/apps/docs/src/code/python/timeout/timeout_session.py
@@ -0,0 +1,13 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ # Timeout for the session to open
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY, timeout=5)
+
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/components/Code.tsx b/apps/docs/src/components/Code.tsx
index 55aeea05a..e66f44a95 100644
--- a/apps/docs/src/components/Code.tsx
+++ b/apps/docs/src/components/Code.tsx
@@ -15,7 +15,6 @@ import { create } from 'zustand'
import { Tag } from '@/components/Tag'
import { CopyButton } from '@/components/CopyButton'
-import { useApiKey } from '@/utils/useUser'
const languageNames: Record<string, string> = {
js: 'JavaScript',
@@ -28,7 +27,7 @@ const languageNames: Record<string, string> = {
go: 'Go',
}
-function getPanelTitle({
+export function getPanelTitle({
title,
language,
}: {
@@ -101,7 +100,7 @@ function CodePanel({
)
}
-function CodeGroupHeader({
+export function CodeGroupHeader({
title,
children,
selectedIndex,
@@ -217,7 +216,7 @@ const usePreferredLanguageStore = create<{
})),
}))
-function useTabGroupProps(availableLanguages: Array<string>) {
+export function useTabGroupProps(availableLanguages: Array<string>) {
let { preferredLanguages, addPreferredLanguage } = usePreferredLanguageStore()
let [selectedIndex, setSelectedIndex] = useState(0)
let activeLanguage = [...availableLanguages].sort(
diff --git a/apps/docs/src/components/LanguageSpecificText.tsx b/apps/docs/src/components/LanguageSpecificText.tsx
new file mode 100644
index 000000000..519a40874
--- /dev/null
+++ b/apps/docs/src/components/LanguageSpecificText.tsx
@@ -0,0 +1,45 @@
+'use client'
+
+import { Children, isValidElement } from 'react'
+import { Tab } from '@headlessui/react'
+import {
+ CodeGroupHeader,
+ getPanelTitle,
+ useTabGroupProps,
+} from '@/components/Code'
+
+export function LanguageSpecificText({ children, title }) {
+ let languages =
+ Children.map(children, (child) =>
+ getPanelTitle(isValidElement(child) ? child.props : {})
+ ) ?? []
+ let tabGroupProps = useTabGroupProps(languages)
+
+ let containerClassName =
+ 'not-prose my-6 overflow-hidden rounded-2xl bg-zinc-900 shadow-md dark:ring-1 dark:ring-white/10'
+ let header = (
+ <CodeGroupHeader title={title} selectedIndex={tabGroupProps.selectedIndex}>
+ {children}
+ </CodeGroupHeader>
+ )
+
+ return (
+ <div>
+ <Tab.Group {...tabGroupProps} className={containerClassName}>
+ {header}
+ <div>
+ <div className="group dark:bg-white/2.5">
+ <div className="relative">
+ <div
+ className="text-s overflow-x-auto p-4 text-white"
+ style={{ whiteSpace: 'pre-line' }}
+ >
+ {children[tabGroupProps.selectedIndex].props.text}
+ </div>
+ </div>
+ </div>
+ </div>
+ </Tab.Group>
+ </div>
+ )
+}
diff --git a/apps/docs/src/components/Navigation.tsx b/apps/docs/src/components/Navigation.tsx
index 527e3535e..0e98586f9 100644
--- a/apps/docs/src/components/Navigation.tsx
+++ b/apps/docs/src/components/Navigation.tsx
@@ -147,10 +147,11 @@ function NavigationGroup({ group, className }) {
// state, so that the state does not change during the close animation.
// The state will still update when we re-open (re-render) the navigation.
let isInsideMobileNavigation = useIsInsideMobileNavigation()
- let initialPathname = usePathname();
+ let initialPathname = usePathname()
// Running on the server, there's bug with usePathname() and basePath https://github.com/vercel/next.js/issues/52700
- if (typeof window === 'undefined' && initialPathname === '/') initialPathname = '/docs'
+ if (typeof window === 'undefined' && initialPathname === '/')
+ initialPathname = '/docs'
let [pathname, sections] = useInitialValue(
[initialPathname, useSectionStore((s) => s.sections)],
@@ -244,6 +245,7 @@ export const navigation = [
{ title: 'Installation', href: '/getting-started/installation' },
{ title: 'API Key', href: '/getting-started/api-key' },
{ title: 'SDK Basics', href: '/getting-started/basics' },
+ { title: 'Advanced SDK Usage', href: '/getting-started/advanced' },
],
},
{
diff --git a/apps/docs/src/components/mdx.tsx b/apps/docs/src/components/mdx.tsx
index 95e4d52d2..3854d616a 100644
--- a/apps/docs/src/components/mdx.tsx
+++ b/apps/docs/src/components/mdx.tsx
@@ -8,6 +8,7 @@ import { Prose } from '@/components/Prose'
export const a = Link
export { Button } from '@/components/Button'
export { CodeGroupAutoload, CodeGroup, Code as code, Pre as pre } from '@/components/Code'
+export { LanguageSpecificText } from '@/components/LanguageSpecificText'
export function wrapper({ children }: { children: React.ReactNode }) {
return (