-
Notifications
You must be signed in to change notification settings - Fork 46
feat: add python e2b sandbox example #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
88174c5
ec76ef5
87aacf8
2c6f8c4
00c57bb
496436c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||
| # Example: E2B Sandbox | ||||||
|
|
||||||
| This example demonstrates how to run Kimi Agent in an [E2B](https://e2b.dev) cloud sandbox using **KAOS** (Kimi Agent Operating System). | ||||||
|
|
||||||
| ## What is KAOS? | ||||||
|
|
||||||
| **KAOS** is a runtime abstraction layer that decouples agent tools from the underlying execution environment. It defines a standard interface for file system operations, process execution, and path manipulation—allowing the same agent code to run locally or in a cloud sandbox. | ||||||
|
|
||||||
| ```mermaid | ||||||
| flowchart LR | ||||||
| subgraph Server | ||||||
| App["Your App"] --> SDK["Kimi Agent SDK"] --> CLI["Kimi CLI"] | ||||||
| subgraph Tools["Tools"] | ||||||
| ReadFile["ReadFile"] | ||||||
| WriteFile["WriteFile"] | ||||||
| Shell["Shell"] | ||||||
| end | ||||||
| CLI --- Tools | ||||||
| end | ||||||
|
|
||||||
| subgraph E2B["E2B Sandbox"] | ||||||
| FS[("Filesystem")] | ||||||
| SH{{"Shell"}} | ||||||
| end | ||||||
|
|
||||||
| ReadFile -->|"E2BKaos.readtext()"| FS | ||||||
| WriteFile -->|"E2BKaos.writetext()"| FS | ||||||
| Shell -->|"E2BKaos.exec()"| SH | ||||||
|
|
||||||
| style Tools stroke-dasharray: 5 5 | ||||||
| ``` | ||||||
|
|
||||||
| By default, tools operate on your local filesystem. With `set_current_kaos(E2BKaos(...))`, all tool calls are transparently routed to the E2B sandbox—no agent code changes required. | ||||||
|
|
||||||
| ## How It Works | ||||||
|
|
||||||
| This example vendors an `E2BKaos` implementation in `e2b_kaos.py`, then installs it via `set_current_kaos` so Kimi CLI tools operate on the E2B sandbox environment: | ||||||
|
|
||||||
| ```python | ||||||
| from e2b import AsyncSandbox | ||||||
| from kaos import set_current_kaos | ||||||
| from kaos.path import KaosPath | ||||||
| from kimi_agent_sdk import prompt | ||||||
|
|
||||||
| # Create an E2B sandbox and install as KAOS backend | ||||||
| sandbox = await AsyncSandbox.create() | ||||||
| set_current_kaos(E2BKaos(sandbox, cwd="/home/user")) | ||||||
|
|
||||||
| # Agent tools now operate inside the sandbox | ||||||
| async for msg in prompt("Create a Python project", work_dir=KaosPath("/home/user")): | ||||||
| print(msg.extract_text(), end="") | ||||||
| ``` | ||||||
|
|
||||||
| ## Run | ||||||
|
|
||||||
| ```sh | ||||||
| cd examples/python/e2b-sandbox | ||||||
| uv sync --reinstall | ||||||
|
|
||||||
| # Required | ||||||
| export KIMI_API_KEY=your-api-key | ||||||
| export KIMI_BASE_URL=https://api.moonshot.ai/v1 | ||||||
| export KIMI_MODEL_NAME=kimi-k2-thinking-turbo | ||||||
| export E2B_API_KEY=your-e2b-api-key | ||||||
|
|
||||||
| # Optional | ||||||
| export E2B_SANDBOX_ID=... | ||||||
| export KIMI_WORK_DIR=/home/user/kimi-workdir | ||||||
|
|
||||||
| uv run main.py | ||||||
| ``` | ||||||
|
|
||||||
| If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK. | ||||||
|
||||||
| If `E2B_SANDBOX_ID` is not set, the script creates a sandbox and prints the ID. The sandbox lifecycle is managed outside of the SDK. | |
| This example currently always creates a new sandbox instance and prints its ID; `E2B_SANDBOX_ID` is reserved for future use, and the sandbox lifecycle is managed outside of the SDK. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| version: 1 | ||
| agent: | ||
| extend: default | ||
| name: "e2b" | ||
| # Grep tool only supports local KAOS; disable it for E2B sandboxes. | ||
| exclude_tools: | ||
| - "kimi_cli.tools.file:Grep" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the "How It Works" code snippet,
E2BKaosis used but never imported, so readers copying this snippet as-is will get aNameError. Please add the missing import (e.g., importingE2BKaosfrome2b_kaos) to keep the example self-contained and runnable.