# @computesdk/tensorlake 中文文档

规范地址：https://npmcn.com/package/%40computesdk/tensorlake/

版本：0\.1\.34
npm 元数据更新时间：2026-11-06T13:41:18.088Z
中文译文更新时间：2026-11-10T14:24:53.418Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40computesdk/tensorlake
原文对照：https://npmcn.com/package/%40computesdk/tensorlake/?lang=original
项目主页：https://www.computesdk.com/
源码仓库：https://github.com/computesdk/computesdk

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：123 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2026-05-05T17:31:17.499Z
- 最新版本发布：2026-11-06T13:41:17.686Z
- 近 30 天下载量：10607（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：36（近 12 个月 36 个）

## 中文简介

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

## 安装

```sh
npm install @computesdk/tensorlake
```

也可以使用：`pnpm add @computesdk/tensorlake`、`yarn add @computesdk/tensorlake`、`bun add @computesdk/tensorlake`

## README

# @ computesdk/ tonsorlake (帮助)

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

## 安装

```bash
npm install @computesdk/tensorlake
```

## 设置

1.  获取您的 API 密钥 [云. 腾讯. ai](https://cloud.tensorlake.ai/)
2.  设置环境变量 :

```bash
export TENSORLAKE_API_KEY=your_api_key_here
```

## 快速启动

### 网关模式( 建议)

使用网关进行零配置自动检测 :

```typescript
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使用:

```typescript
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();
```

## 配置

### 环境变量

```bash
export TENSORLAKE_API_KEY=your_api_key_here
export TENSORLAKE_API_URL=https://api.tensorlake.ai   # optional override
```

### 配置选项

```typescript
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 参考

### 沙盒管理

```typescript
// 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();
```

### 命令执行

```typescript
// 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 });
```

### 文件系统操作

```typescript
// 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');
```

### 抓图

```typescript
// 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 访问

```typescript
// 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 可以公开访问 —— 处理它 作为公共终点.

## 实例

### 代理代码执行

```typescript
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();
```

### 快照- 背出温暖开始

```typescript
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();
```

## 处理错误

```typescript
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 密钥;使用环境变量或密钥管理器

## 支助

-   [Tensorlake 文档](https://docs.tensorlake.ai/)
-   [计算SDK问题](https://github.com/computesdk/computesdk/issues)

## 许可证

麻省理工学院

## 常见问题

### @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 和提交记录判断。

