@ computesdk/ 发光
计算SDK的闪电AI供应商 - 创建和管理 闪电AI 沙盒:运行 shell 命令,读取/写入文件,并管理沙盒生命周期.
安装
npm install @computesdk/lightning
说明 基础
@lightningai/sdk仅公布无害环境管理,并要求 节点.js 22+此软件包通过动态加载import(),因此它从无害环境管理和共同JS项目(运行在节点22+)中发挥作用.
设置
- 从您的 API 密钥中获取 闪电AI 账户设置.
- 设置环境变量 :
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编号 :metadataaPI平价被接受,但没有坚持./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 密钥不会发生序列化 .
支助
许可证
麻省理工学院