@computesdk/tensorlake

中文文档
0.1.34发布于 2026/11/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.1万

计算SDK的 Tensorlake 提供者 - 用于代理应用程序和 LLM 生成代码执行的状态 MicroVM 沙盒

中文使用文档
文档目录22 节
  1. 安装
  2. 设置
  3. 快速启动
  4. 网关模式( 建议)
  5. 直接模式
  6. 配置
  7. 环境变量
  8. 配置选项
  9. 特征
  10. API 参考
  11. 沙盒管理
  12. 命令执行
  13. 文件系统操作
  14. 抓图
  15. URL 访问
  16. 实例
  17. 代理代码执行
  18. 快照- 背出温暖开始
  19. 处理错误
  20. 最佳做法
  21. 支助
  22. 许可证

@ computesdk/ tonsorlake (帮助)

Tensorlake ComputeSDK提供商 - 用于代理应用程序和LLM生成的代码执行的状态MicroVM sandbox.

安装

npm install @computesdk/tensorlake

设置

  1. 获取您的 API 密钥 云. 腾讯. ai
  2. 设置环境变量 :
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);
  }
}

最佳做法

  1. 暖和开始的快照 - 安装依赖后抓图,以避免重复设置
  2. 摧毁沙盒 - 总是打电话 sandbox.destroy() 释放资源
  3. 设定超时 - 使用( I) timeout 长期工作量,以避免越轨
  4. 背景进程 - 使用( I) { background: true } 用于服务器或守护进程; 在启动命令返回后幸存
  5. API 密钥安全 - 从不执行 API 密钥;使用环境变量或密钥管理器

支助

许可证

麻省理工学院

版本与发布记录

@computesdk/tensorlake 共发布 36 个版本,首次发布于 2026/05/06;过去 12 个月发布了 36 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.34latest2026/11/064 天前
0.1.332026/11/046 天前
0.1.322026/10/2812 天前
0.1.312026/10/2813 天前
0.1.302026/10/2714 天前
0.1.292026/10/2714 天前

关于 @computesdk/tensorlake 的常见问题

@computesdk/tensorlake 是什么?

计算SDK的 Tensorlake 提供者 - 用于代理应用程序和 LLM 生成代码执行的状态 MicroVM 沙盒

如何安装 @computesdk/tensorlake?

在项目目录运行 npm install @computesdk/tensorlake,或使用 pnpm add / yarn add / bun add。

@computesdk/tensorlake 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@computesdk/tensorlake 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@computesdk/tensorlake 可以商用吗?

@computesdk/tensorlake 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@computesdk/tensorlake 的下载量有多少?

根据 npm 官方下载统计,@computesdk/tensorlake 在 2026/10/11 – 2026/11/09 这 30 天内被下载 10,607 次,日均约 354 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@computesdk/tensorlake 还在维护吗?

最新版本 0.1.34 发布于 2026/11/06(4 天前);至今共发布 36 个版本,其中过去 12 个月发布了 36 个;首次发布于 2026/05/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。