Skip to main content
Deno Deploy 提供用于隔离代码执行的 Linux 微虚拟机。最适合 Deno 和 JavaScript 工作负载。

设置

npm install @langchain/deno

身份验证

app.deno.com → 设置 → 组织令牌 获取您的令牌。
export DENO_DEPLOY_TOKEN=your_token
或者直接传递凭据:
const sandbox = await DenoSandbox.create({
  auth: { token: "your-token-here" },
});

与 deepagents 一起使用

import { createDeepAgent } from "deepagents";
import { ChatAnthropic } from "@langchain/anthropic";
import { DenoSandbox } from "@langchain/deno";

const sandbox = await DenoSandbox.create({
  memoryMb: 1024,
  lifetime: "10m",
});

try {
  const agent = createDeepAgent({
    model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
    systemPrompt: "You are a coding assistant with sandbox access.",
    backend: sandbox,
  });

  const result = await agent.invoke({
    messages: [{ role: "user", content: "Create a hello world Deno app and run it" }],
  });
} finally {
  await sandbox.close();
}

独立使用

import { DenoSandbox } from "@langchain/deno";

const sandbox = await DenoSandbox.create({
  memoryMb: 1024,
  lifetime: "10m",
});

const result = await sandbox.execute("deno --version");
console.log(result.output);

await sandbox.close();

配置

选项类型默认值描述
memoryMbnumber768内存(MB)(768-4096)
lifetime"session" | string"session"生命周期("session""5m""30s"
regionstring-区域。选项:"ams" | "ord"

可用区域

区域代码位置
ams阿姆斯特丹
ord芝加哥

生命周期选项

  • "session"(默认):当您关闭/处置客户端时,沙箱关闭
  • 持续时间字符串:使沙箱在特定时间内保持活动状态(例如,"5m""30s"

访问 Deno SDK

对于高级功能,访问底层 Deno SDK:
const denoSandbox = await DenoSandbox.create();
const sdk = denoSandbox.sandbox;

// 暴露 HTTP 端口
const url = await sdk.exposeHttp({ port: 3000 });

// 暴露 SSH
const ssh = await sdk.exposeSsh();

// 评估 JavaScript
const result = await sdk.eval("1 + 2");

// 设置环境变量
await sdk.env.set("API_KEY", "secret");

// Shell 模板字面量
const output = await sdk.sh`echo "Hello from Deno!"`.text();

// 启动 JavaScript 运行时
const runtime = await sdk.createJsRuntime({ entrypoint: "server.ts" });

重新连接到现有沙箱

重新连接需要基于持续时间的生命周期(不是 "session"):
// 使用持续时间生命周期创建
const sandbox = await DenoSandbox.create({
  memoryMb: 1024,
  lifetime: "30m",
});
const sandboxId = sandbox.id;
await sandbox.close(); // 关闭连接,沙箱保持运行

// 稍后:重新连接
const reconnected = await DenoSandbox.connect(sandboxId);
const result = await reconnected.execute("ls -la");

工厂函数

import { createDenoSandboxFactory, createDenoSandboxFactoryFromSandbox } from "@langchain/deno";

// 每次调用创建新沙箱
const factory = createDenoSandboxFactory({ memoryMb: 1024 });

// 或跨调用重用现有沙箱
const sandbox = await DenoSandbox.create();
const reuseFactory = createDenoSandboxFactoryFromSandbox(sandbox);

错误处理

import { DenoSandboxError } from "@langchain/deno";

try {
  await sandbox.execute("some command");
} catch (error) {
  if (error instanceof DenoSandboxError) {
    switch (error.code) {
      case "NOT_INITIALIZED":
        await sandbox.initialize();
        break;
      case "COMMAND_TIMEOUT":
        console.error("Command took too long");
        break;
      case "AUTHENTICATION_FAILED":
        console.error("Check your Deno Deploy token");
        break;
    }
  }
}

错误代码

代码描述
NOT_INITIALIZED沙箱未初始化 - 调用 initialize()
ALREADY_INITIALIZED无法初始化两次
AUTHENTICATION_FAILED无效或缺少 Deno Deploy 令牌
SANDBOX_CREATION_FAILED创建沙箱失败
SANDBOX_NOT_FOUND沙箱 ID 未找到或已过期
COMMAND_TIMEOUT命令执行超时
COMMAND_FAILED命令执行失败
FILE_OPERATION_FAILED文件读/写失败
RESOURCE_LIMIT_EXCEEDED超过 CPU、内存或存储限制

限制和约束

约束
最小内存768 MB
最大内存4096 MB (4 GB)
磁盘空间10 GB
vCPU2
工作目录/home/app
网络访问完全(默认)

环境变量

变量描述
DENO_DEPLOY_TOKENDeno Deploy 组织访问令牌