@ computesdk/ tonsorlake (帮助)
Tensorlake ComputeSDK提供商 - 用于代理应用程序和LLM生成的代码执行的状态MicroVM sandbox.
安装
npm install @computesdk/tensorlake
设置
- 获取您的 API 密钥 云. 腾讯. ai
- 设置环境变量 :
export TENSORLAKE_API_KEY=your_api_key_here
快速启动
网关模式( 建议)
使用网关进行零配置自动检测 :
import { compute } from 'computesdk';
// Auto-detects Tensorlake from TENSORLAKE_API_KEY environment variable
const sandbox = await compute.instances.create();
const result = await sandbox.runCommand('uname -a');
console.log(result.stdout);
await sandbox.destroy();
直接模式
对于没有网关的直接SDK使用:
import { tensorlake } from '@computesdk/tensorlake';
const compute = tensorlake({ apiKey: process.env.TENSORLAKE_API_KEY });
const sandbox = await compute.instances.create();
const result = await sandbox.runCommand(`uname -a`);
console.log(result.stdout);
await sandbox.destroy();
配置
环境变量
export TENSORLAKE_API_KEY=your_api_key_here
export TENSORLAKE_API_URL=https://api.tensorlake.ai # optional override
配置选项
interface TensorlakeConfig {
/** Tensorlake API key — falls back to TENSORLAKE_API_KEY environment variable */
apiKey?: string;
/** Override for the management API base URL */
apiUrl?: string;
/** Override for the sandbox proxy URL */
proxyUrl?: string;
/** Default container image for new sandboxes (default: ubuntu-minimal) */
image?: string;
/** Default timeout in seconds for sandboxes */
timeout?: number;
}
特征
- 状态微波 - 在一个会话中与持续状态的VM完全隔离
- 快照恢复( R) - 拍出一个正在运行的沙盒 稍后从它恢复
- 代码执行 - Python和Node.js自动检测
- 命令执行 - 运行 shell 命令,包括背景进程
- 文件系统操作 - 读取、写出、列出和删除文件和目录
- URL 访问 - 通过代理 URL 曝光沙盒端口
- 自动运行时间检测 - 从代码模式自动检测 Python 对s Node.js
API 参考
沙盒管理
// Create a sandbox (default image: ubuntu-minimal)
const sandbox = await compute.instances.create();
// Create with options
const sandbox = await compute.instances.create({
image: 'ubuntu-minimal',
timeout: 300000, // ms → converted to seconds internally
name: 'my-sandbox',
snapshotId: 'snap-abc123', // resume from a snapshot
});
// Get an existing sandbox by ID
const sandbox = await compute.instances.getById('sandbox-id');
// List all running sandboxes
const sandboxes = await compute.instances.list();
// Destroy a sandbox
await sandbox.destroy();
命令执行
// Run a command and wait for output
const result = await sandbox.runCommand('ls -la /tmp');
console.log(result.stdout);
console.log(result.exitCode);
// Run with environment variables and working directory
const result = await sandbox.runCommand('npm install', {
env: { NODE_ENV: 'production' },
cwd: '/app',
});
// Fire-and-forget background process
await sandbox.runCommand('python server.py', { background: true });
文件系统操作
// Write a file
await sandbox.filesystem.writeFile('/tmp/hello.py', 'print("Hello")');
// Read a file
const content = await sandbox.filesystem.readFile('/tmp/hello.py');
// Create a directory (creates parents as needed)
await sandbox.filesystem.mkdir('/tmp/data/nested');
// List directory contents
const entries = await sandbox.filesystem.readdir('/tmp');
// entries: [{ name, type: 'file'|'directory', size, modified }]
// Check existence
const exists = await sandbox.filesystem.exists('/tmp/hello.py');
// Remove a file or directory
await sandbox.filesystem.remove('/tmp/hello.py');
抓图
// Snapshot the current state of a running sandbox
const snapshot = await compute.snapshot.create(instanceId, { name: 'after-setup' });
console.log(snapshot.id); // 'snap-abc123'
// List all snapshots
const snapshots = await compute.snapshot.list();
// Delete a snapshot
await compute.snapshot.delete('snap-abc123');
// Resume from a snapshot
const sandbox = await compute.instances.create({ snapshotId: 'snap-abc123' });
URL 访问
// Get a proxy URL for a port exposed inside the sandbox
const url = await sandbox.getUrl({ port: 443 });
// → "https://<sandbox-id>.sandbox.tensorlake.ai"
const customPortUrl = await sandbox.getUrl({ port: 8080 });
// → "https://8080-<sandbox-id>.sandbox.tensorlake.ai"
const wsUrl = await sandbox.getUrl({ port: 443, protocol: 'wss' });
// → "wss://<sandbox-id>.sandbox.tensorlake.ai"
getUrl 在沙盒中注册端口 exposed_ports 并启用
未认证访问, 因此返回的 URL 可以公开访问 —— 处理它
作为公共终点.
实例
代理代码执行
import { tensorlake } from '@computesdk/tensorlake';
const compute = tensorlake({ apiKey: process.env.TENSORLAKE_API_KEY });
const sandbox = await compute.instances.create({ timeout: 600000 });
// Install dependencies once
await sandbox.runCommand('pip install requests pandas');
const result = await sandbox.runCommand('python --version');
console.log(result.stdout);
await sandbox.destroy();
快照- 背出温暖开始
import { tensorlake } from '@computesdk/tensorlake';
const compute = tensorlake({ apiKey: process.env.TENSORLAKE_API_KEY });
// One-time setup: create a pre-warmed snapshot
async function createBaseSnapshot(): Promise<string> {
const sandbox = await compute.instances.create();
await sandbox.runCommand('pip install pandas numpy scikit-learn');
const snapshot = await compute.snapshot.create(sandbox.instanceId);
await sandbox.destroy();
return snapshot.id;
}
// Fast boot from snapshot — skips install step
const snapshotId = await createBaseSnapshot();
const sandbox = await compute.instances.create({ snapshotId });
const result = await sandbox.runCommand(`uname -a`);
console.log(result.stdout);
await sandbox.destroy();
处理错误
import { tensorlake } from '@computesdk/tensorlake';
try {
const compute = tensorlake({ apiKey: process.env.TENSORLAKE_API_KEY });
const sandbox = await compute.instances.create();
const result = await sandbox.runCommand('umane -a');
console.log(result.stdout);
} catch (error) {
if (error.message.includes('Missing Tensorlake API key')) {
console.error('Set TENSORLAKE_API_KEY environment variable');
} else if (error.message.includes('authentication failed')) {
console.error('Check your API key at https://cloud.tensorlake.ai');
} else {
console.error(error);
}
}
最佳做法
- 暖和开始的快照 - 安装依赖后抓图,以避免重复设置
- 摧毁沙盒 - 总是打电话
sandbox.destroy()释放资源 - 设定超时 - 使用( I)
timeout长期工作量,以避免越轨 - 背景进程 - 使用( I)
{ background: true }用于服务器或守护进程; 在启动命令返回后幸存 - API 密钥安全 - 从不执行 API 密钥;使用环境变量或密钥管理器
支助
许可证
麻省理工学院