* docs: comprehensive docs site refactoring - Replace 856-line manifest auto-generation with direct VitePress docs - Restructure from flat 4-section layout to 10-section information architecture (Getting Started, Architecture, Guides, SDKs, Components, Kubernetes, API, CLI, Examples, Community) - Establish single source of truth: docs/ owns user-facing content, READMEs are slim pointers - Remove incomplete Chinese i18n (30-40% coverage), keep English only - Add VitePress features: custom containers, code groups, edit links, footer, dark mode - Simplify build: just `vitepress build`, no docs:sync or docs:spec preprocessing - Add documentation rules to AGENTS.md and CLAUDE.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: simplify CLAUDE.md to point at AGENTS.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: resolve 38 broken links, stale paths, and content issues across docs - Fix 16 broken inbound links to moved docs pages (README, CONTRIBUTING, SECURITY, ROADMAP, GOVERNANCE, server/configuration, kubernetes/) - Move sandbox.kill() inside async-with block in quick start and README - Update execd paths from /opt/opensandbox/bin/ to /opt/opensandbox/ - Fix wrong batchsandbox-template GitHub raw URL - Remove references to deleted RELEASE_NOTE_TEMPLATE and removed scripts - Add missing cd context to server, egress, and MCP command snippets - Add missing OSEP-0012 (multi-tenancy) and OSEP-0013 to index - Fix timeout described as mandatory (it is optional) - Replace duplicated contributing/code-of-conduct with GitHub pointers - Fix broken Chinese doc references in SDK README_zh files - Remove dead Chinese docs link from README Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: align README links with versioned docs * docs: address remaining review comments * docs: clarify README ownership for components * docs: fix remaining navigation and example comments * docs: refine documentation ownership rules * docs: remove obsolete spec doc generator --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
5.9 KiB
Alibaba Code Interpreter SDK for JavaScript/TypeScript
A TypeScript/JavaScript SDK for executing code in secure, isolated sandboxes. It provides a high-level API for running Python, Java, Go, TypeScript, and other languages safely, with support for code execution contexts.
Prerequisites
This SDK requires a Docker image containing the Code Interpreter runtime environment. You must use the opensandbox/code-interpreter image (or a derivative) which includes pre-installed runtimes for Python, Java, Go, Node.js, etc.
For detailed information about supported languages and versions, please refer to the Environment Documentation.
Installation
npm
npm install @alibaba-group/opensandbox-code-interpreter
pnpm
pnpm add @alibaba-group/opensandbox-code-interpreter
yarn
yarn add @alibaba-group/opensandbox-code-interpreter
Quick Start
The following example demonstrates how to create a sandbox with a specific runtime configuration and execute a simple script.
Note
: Before running this example, ensure the OpenSandbox service is running. See the root README.md for startup instructions.
import { ConnectionConfig, Sandbox } from "@alibaba-group/opensandbox";
import { CodeInterpreter, SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
// 1. Configure connection
const config = new ConnectionConfig({
domain: "api.opensandbox.io",
apiKey: "your-api-key",
});
// 2. Create a Sandbox with the code-interpreter image + runtime versions
const sandbox = await Sandbox.create({
connectionConfig: config,
image: "opensandbox/code-interpreter:v1.1.0",
entrypoint: ["/opt/code-interpreter/code-interpreter.sh"],
env: {
PYTHON_VERSION: "3.11",
JAVA_VERSION: "17",
NODE_VERSION: "20",
GO_VERSION: "1.24",
},
timeoutSeconds: 15 * 60,
});
// 3. Create CodeInterpreter wrapper
const ci = await CodeInterpreter.create(sandbox);
// 4. Create an execution context (Python)
const ctx = await ci.codes.createContext(SupportedLanguages.PYTHON);
// 5. Run code
const result = await ci.codes.run("import sys\nprint(sys.version)\nresult = 2 + 2\nresult", {
context: ctx,
});
// 6. Print output
console.log(result.result[0]?.text);
// 7. Cleanup remote instance (optional but recommended)
await sandbox.kill();
await sandbox.close();
Runtime Configuration
Docker Image
The Code Interpreter SDK relies on a specialized environment. Ensure your sandbox provider has the opensandbox/code-interpreter image available.
Language Version Selection
You can specify the desired version of a programming language by setting the corresponding environment variable when creating the Sandbox.
| Language | Environment Variable | Example Value | Default (if unset) |
|---|---|---|---|
| Python | PYTHON_VERSION |
3.11 |
Image default |
| Java | JAVA_VERSION |
17 |
Image default |
| Node.js | NODE_VERSION |
20 |
Image default |
| Go | GO_VERSION |
1.24 |
Image default |
const sandbox = await Sandbox.create({
connectionConfig: config,
image: "opensandbox/code-interpreter:v1.1.0",
entrypoint: ["/opt/code-interpreter/code-interpreter.sh"],
env: {
JAVA_VERSION: "17",
GO_VERSION: "1.24",
},
});
Usage Examples
0. Run with language (default language context)
If you don't need to manage explicit context IDs, you can run code by specifying only language.
When context.id is omitted, execd can create/reuse a default session for that language, so state can persist across runs.
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
await ci.codes.run("x = 42", { language: SupportedLanguages.PYTHON });
const execution = await ci.codes.run("result = x\nresult", { language: SupportedLanguages.PYTHON });
console.log(execution.result[0]?.text); // "42"
0.1 Context management (list/get/delete)
You can manage contexts explicitly (aligned with Python/Kotlin SDKs):
const ctx = await ci.codes.createContext(SupportedLanguages.PYTHON);
const same = await ci.codes.getContext(ctx.id!);
console.log(same.id, same.language);
const all = await ci.codes.listContexts();
const pyOnly = await ci.codes.listContexts(SupportedLanguages.PYTHON);
await ci.codes.deleteContext(ctx.id!);
await ci.codes.deleteContexts(SupportedLanguages.PYTHON); // bulk cleanup
1. Java Code Execution
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
const javaCtx = await ci.codes.createContext(SupportedLanguages.JAVA);
const execution = await ci.codes.run(
[
'System.out.println("Calculating sum...");',
"int a = 10;",
"int b = 20;",
"int sum = a + b;",
'System.out.println("Sum: " + sum);',
"sum",
].join("\n"),
{ context: javaCtx },
);
console.log(execution.logs.stdout.map((m) => m.text));
2. Streaming Output Handling
Handle stdout/stderr and execution events in real-time.
import type { ExecutionHandlers } from "@alibaba-group/opensandbox";
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
const handlers: ExecutionHandlers = {
onStdout: (m) => console.log("STDOUT:", m.text),
onStderr: (m) => console.error("STDERR:", m.text),
onResult: (r) => console.log("RESULT:", r.text),
};
const pyCtx = await ci.codes.createContext(SupportedLanguages.PYTHON);
await ci.codes.run("import time\nfor i in range(5):\n print(i)\n time.sleep(0.2)", {
context: pyCtx,
handlers,
});
Notes
- Lifecycle:
CodeInterpreterwraps an existingSandboxinstance and reuses its connection configuration. Each sandbox instance clones the transport viaConnectionConfig.withTransportIfMissing(), so callsandbox.close()when you are finished to release the Node.js keep-alive agent and avoid leak. - Default context:
codes.run(..., { language })uses a language default context (state can persist across runs).