サンドボックス内にシェルセッションを作成します。各セッションは独自のシェル状態、環境変数、作業ディレクトリを持ちます。一方、サンドボックスのファイルシステムとプロセス空間は共有します。詳細は セッション管理 を参照してください。
新しいシェルセッションを作成します。
const session = await sandbox.createSession(options?: SessionOptions): Promise<ExecutionSession>パラメーター:
options(任意):id- カスタムセッション ID(未指定時は自動生成)env- このセッションの環境変数:Record<string, string | undefined>cwd- 作業ディレクトリ(既定:"/workspace")commandTimeoutMs- このセッション内の各コマンドがタイムアウトするまでの最大時間(ミリ秒)。個別のコマンドはexec()のtimeoutオプションで上書きできます。
戻り値: このセッションにバインドされたすべてのサンドボックスメソッドを持つ Promise<ExecutionSession>
// Separate workflow environments
const prodSession = await sandbox.createSession({
id: "prod",
env: { NODE_ENV: "production", API_URL: "https://api.example.com" },
cwd: "/workspace/prod",
});
const testSession = await sandbox.createSession({
id: "test",
env: {
NODE_ENV: "test",
API_URL: "http://localhost:3000",
DEBUG_MODE: undefined, // Skipped, not set in this session
},
cwd: "/workspace/test",
});
// Run in parallel
const [prodResult, testResult] = await Promise.all([
prodSession.exec("npm run build"),
testSession.exec("npm run build"),
]);
// Session with a default command timeout
const session = await sandbox.createSession({
commandTimeoutMs: 5000, // 5s timeout for all commands
});
await session.exec("sleep 10"); // Times out after 5s
// Per-command timeout overrides session-level timeout
await session.exec("sleep 10", { timeout: 3000 }); // Times out after 3s// Separate workflow environments
const prodSession = await sandbox.createSession({
id: 'prod',
env: { NODE_ENV: 'production', API_URL: 'https://api.example.com' },
cwd: '/workspace/prod'
});
const testSession = await sandbox.createSession({
id: 'test',
env: {
NODE_ENV: 'test',
API_URL: 'http://localhost:3000',
DEBUG_MODE: undefined // Skipped, not set in this session
},
cwd: '/workspace/test'
});
// Run in parallel
const [prodResult, testResult] = await Promise.all([
prodSession.exec('npm run build'),
testSession.exec('npm run build')
]);
// Session with a default command timeout
const session = await sandbox.createSession({
commandTimeoutMs: 5000 // 5s timeout for all commands
});
await session.exec('sleep 10'); // Times out after 5s
// Per-command timeout overrides session-level timeout
await session.exec('sleep 10', { timeout: 3000 }); // Times out after 3sID で既存のセッションを取得します。
const session = await sandbox.getSession(sessionId: string): Promise<ExecutionSession>パラメーター:
sessionId- 既存セッションの ID
戻り値: 指定したセッションにバインドされた Promise<ExecutionSession>
// First request - create a task-specific session
const session = await sandbox.createSession({ id: "build" });
await session.exec("git clone https://github.com/user/repo.git");
await session.exec("cd repo && npm install");
// Second request - resume session (environment and cwd preserved)
const session = await sandbox.getSession("build");
const result = await session.exec("cd repo && npm run build");// First request - create a task-specific session
const session = await sandbox.createSession({ id: 'build' });
await session.exec('git clone https://github.com/user/repo.git');
await session.exec('cd repo && npm install');
// Second request - resume session (environment and cwd preserved)
const session = await sandbox.getSession('build');
const result = await session.exec('cd repo && npm run build');セッションを削除し、リソースをクリーンアップします。
const result = await sandbox.deleteSession(sessionId: string): Promise<SessionDeleteResult>パラメーター:
sessionId- 削除するセッションの ID("default"は不可)
戻り値: 次を含む Promise<SessionDeleteResult>:
success- 削除が成功したかどうかsessionId- 削除したセッションの IDtimestamp- 削除のタイムスタンプ
// Create a temporary session for a specific task
const tempSession = await sandbox.createSession({ id: "temp-task" });
try {
await tempSession.exec("npm run heavy-task");
} finally {
// Clean up the session when done
await sandbox.deleteSession("temp-task");
}// Create a temporary session for a specific task
const tempSession = await sandbox.createSession({ id: 'temp-task' });
try {
await tempSession.exec('npm run heavy-task');
} finally {
// Clean up the session when done
await sandbox.deleteSession('temp-task');
}サンドボックスに環境変数を設定します。
await sandbox.setEnvVars(envVars: Record<string, string | undefined>): Promise<void>パラメーター:
envVars- 設定または解除する環境変数のキーと値のペアstring値: 環境変数を設定しますundefinedまたはnull値: 環境変数を解除します
const sandbox = getSandbox(env.Sandbox, "user-123");
// Set environment variables first
await sandbox.setEnvVars({
API_KEY: env.OPENAI_API_KEY,
DATABASE_URL: env.DATABASE_URL,
NODE_ENV: "production",
OLD_TOKEN: undefined, // Unsets OLD_TOKEN if previously set
});
// Now commands can access these variables
await sandbox.exec("python script.py");const sandbox = getSandbox(env.Sandbox, 'user-123');
// Set environment variables first
await sandbox.setEnvVars({
API_KEY: env.OPENAI_API_KEY,
DATABASE_URL: env.DATABASE_URL,
NODE_ENV: 'production',
OLD_TOKEN: undefined // Unsets OLD_TOKEN if previously set
});
// Now commands can access these variables
await sandbox.exec('python script.py');ExecutionSession オブジェクトは、特定のセッションにバインドされたすべてのサンドボックスメソッドを持ちます。
- セッション管理の概念 - セッションの仕組み
- Commands API - コマンドを実行する