@computesdk/lightning

中文文档
1.0.20发布于 2026/11/06MITTypeScriptESM + CJS近 30 天下载 9108

计算SDK的闪电AI供应商 - 用于代码执行、命令运行和文件系统访问的云沙盒

中文使用文档
文档目录16 节
  1. 安装
  2. 设置
  3. 快速启动
  4. 配置
  5. 环境变量
  6. 配置选项
  7. 特征
  8. 抓图
  9. API 参考
  10. 命令执行
  11. 文件系统操作
  12. 沙盒管理
  13. 处理错误
  14. 限制
  15. 支助
  16. 许可证

@ computesdk/ 发光

计算SDK的闪电AI供应商 - 创建和管理 闪电AI 沙盒:运行 shell 命令,读取/写入文件,并管理沙盒生命周期.

安装

npm install @computesdk/lightning

说明 基础 @lightningai/sdk 仅公布无害环境管理,并要求 节点.js 22+此软件包通过动态加载 import(),因此它从无害环境管理和共同JS项目(运行在节点22+)中发挥作用.

设置

  1. 从您的 API 密钥中获取 闪电AI 账户设置.
  2. 设置环境变量 :
export LIGHTNING_API_KEY=your_api_key_here

快速启动

配置 compute 与闪电提供商并创建一个沙盒:

import { compute } from 'computesdk';
import { lightning } from '@computesdk/lightning';

compute.setConfig({
  provider: lightning({ apiKey: process.env.LIGHTNING_API_KEY }),
});

const sandbox = await compute.instances.create();

const result = await sandbox.runCommand('echo "Hello from Lightning"');
console.log(result.stdout);

await sandbox.destroy();

或者,在只需要一个供应商时直接给供应商工厂打电话:

import { lightning } from '@computesdk/lightning';

const sdk = lightning({ apiKey: process.env.LIGHTNING_API_KEY });
const sandbox = await sdk.sandbox.create();

配置

环境变量

export LIGHTNING_API_KEY=your_api_key_here
# Optional: override the Lightning Cloud base URL
export LIGHTNING_CLOUD_URL=https://lightning.ai

LIGHTNING_SANDBOX_API_KEY 被接受(并优先于 LIGHTNING_API_KEY,匹配SDK).

配置选项

interface LightningConfig {
  /** Lightning AI API key - falls back to LIGHTNING_API_KEY / LIGHTNING_SANDBOX_API_KEY */
  apiKey?: string;
  /** Lightning Cloud base URL - falls back to LIGHTNING_CLOUD_URL, then production */
  baseUrl?: string;
  /** Instance type for new sandboxes (e.g. "cpu-1" ... "cpu-16"). Defaults to "cpu-1" */
  instanceType?: string;
  /** Curated runtime image (e.g. "node24", "python313") */
  runtime?: string;
  /** Persist filesystem state across stops via auto-snapshots */
  persistent?: boolean;
  /** Request spot capacity */
  spot?: boolean;
  /** Ports to expose on new sandboxes */
  ports?: number[];
  /** Maximum sandbox lifetime in milliseconds before auto-stop */
  timeout?: number;
}

特征

  • ✅ 命令执行 - 在沙盒内运行外壳命令
  • ✅ 文件系统操作 - 通过闪电SDK读取/写入文件、目录和上市
  • ✅ 沙盒寿命周期 - 创建、重新连接和销毁沙盒
  • ✅ 端口 URL 服务器 - 说吧 getUrl(port) 返回创建时所声明的任何端口的公用 HTTPS URL
  • ✅ 抓图 - 通过抓取、列出、删除并恢复文件系统快照 compute.snapshot.*

抓图

抓取一个沙盒的文件系统,列表/删除快照,从其中启动一个新的沙盒:

import { compute } from 'computesdk';
import { lightning } from '@computesdk/lightning';

compute.setConfig({ provider: lightning() });

const sandbox = await compute.instances.create();

// Capture (waits until the snapshot is `ready`)
const snapshot = await compute.snapshot.create(sandbox.instanceId);

// List (optionally scoped to a source sandbox)
const snapshots = await compute.snapshot.list({ instanceId: sandbox.instanceId });

// Restore into a fresh sandbox
const restored = await compute.instances.create({ snapshotId: snapshot.id });

// Delete
await compute.snapshot.delete(snapshot.id);

闪电快照是无名的 所以 CreateSnapshotOptions.name 编号 : metadata aPI平价被接受,但没有坚持. /tmp (和其他平台默认)从快照中排除——持续数据 $HOME 让它活下来.

API 参考

命令执行

// Run a shell command
const result = await sandbox.runCommand('ls -la');
console.log(result.stdout, result.exitCode);

// Run with a working directory and environment variables
const result = await sandbox.runCommand('node script.js', {
  cwd: '/workspace',
  env: { NODE_ENV: 'production' },
});

// Background a long-running command
await sandbox.runCommand('python server.py', { background: true });

说明 闪电返回单个合并 stdout/stderr 流,该流将计算 SDK 表面 result.stdout (单位:千美元)result.stderr 改为空).

文件系统操作

// Write file
await sandbox.filesystem.writeFile('/tmp/hello.txt', 'Hello World');

// Read file
const content = await sandbox.filesystem.readFile('/tmp/hello.txt');

// Create directory
await sandbox.filesystem.mkdir('/tmp/data');

// List directory contents
const files = await sandbox.filesystem.readdir('/tmp');

// Check if a path exists
const exists = await sandbox.filesystem.exists('/tmp/hello.txt');

// Remove a file or directory
await sandbox.filesystem.remove('/tmp/hello.txt');

沙盒管理

// Get sandbox info
const info = await sandbox.getInfo();
console.log(info.id, info.status, info.createdAt);

// Reconnect to an existing sandbox by id
const existing = await sdk.sandbox.getById('sandbox-id');

// List sandboxes
const sandboxes = await sdk.sandbox.list();

// Destroy sandbox
await sandbox.destroy();

// Drop down to the native @lightningai/sdk Sandbox instance
const native = sandbox.getInstance();

处理错误

import { lightning } from '@computesdk/lightning';

try {
  const sdk = lightning({ apiKey: process.env.LIGHTNING_API_KEY });
  const sandbox = await sdk.sandbox.create();
  const result = await sandbox.runCommand('echo hi');
} catch (error) {
  if (error.message.includes('Missing Lightning AI API key')) {
    console.error('Set LIGHTNING_API_KEY environment variable');
  } else if (error.message.includes('authentication failed')) {
    console.error('Check your Lightning AI API key');
  } else if (error.message.includes('quota exceeded')) {
    console.error('Lightning AI usage limits reached');
  }
}

限制

  • 节点.js 22+ 基础要求 @lightningai/sdk。 。 。 .
  • 端口 URL 服务器数字 : getUrl(port) 返回 Lightning 用于端口的公用 HTTPS URL(例如). https://8080-<sandbox-id>-s.cloudspaces.litng.ai) (中文(简体) ). 港口必须通过 ports 在创建时间, 否则 getUrl 掷出.
  • 合并产出: stdout 和 stderr 作为单个组合流返回 result.stdout。 。 。 .
  • 证书和货币: Lightning SDK 在进程- 全局状态中存储 auth, 因此此提供者将使用提供者实例之间的简短证书切换序列化 不同 API 密钥. 共享同一密钥的操作完全同时运行;只有切换到不同的密钥,等待飞行中的同键操作排出. 每个过程使用一个 API 密钥不会发生序列化 .

支助

许可证

麻省理工学院

版本与发布记录

@computesdk/lightning 共发布 20 个版本,首次发布于 2026/07/09;过去 12 个月发布了 20 个版本。

版本发布日期距今
1.0.20latest2026/11/064 天前
1.0.192026/11/046 天前
1.0.182026/10/2812 天前
1.0.172026/10/2317 天前
1.0.162026/10/2318 天前
1.0.152026/10/2218 天前

关于 @computesdk/lightning 的常见问题

@computesdk/lightning 是什么?

计算SDK的闪电AI供应商 - 用于代码执行、命令运行和文件系统访问的云沙盒

如何安装 @computesdk/lightning?

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

@computesdk/lightning 支持 TypeScript 吗?

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

@computesdk/lightning 支持 ESM 还是 CommonJS?

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

@computesdk/lightning 可以商用吗?

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

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

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

@computesdk/lightning 还在维护吗?

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