# Amp (/products/las/sandbox-overview/sandbox-agent/3-sandbox-agent-amp)



最近更新时间: 2026-11-25 19:29:23

# 概述 [#概述]

`amp` 是预构建的 AI Agents 运行环境，已内置 Sourcegraph 出品的 [Amp](https://ampcode.com) CLI——一款采用多模型架构、内置代码智能能力的编码代理。可在隔离的沙箱环境中以无头方式运行，具备完整的文件系统、终端和 git 访问能力。

模板预装 Node.js、git、ripgrep、vim、GitHub CLI 等常用工具，以及 pnpm/tsx/vite 前端脚手架。镜像内不内置任何 API key，认证信息通过沙箱创建时的 `envs` 注入。

# 模板内容 [#模板内容]

| 组件    | 说明                                                      |
| ----- | ------------------------------------------------------- |
| `amp` | Amp CLI                                                 |
| 基础工具  | Node.js 24.x、git、ripgrep、vim、GitHub CLI、pnpm/tsx/vite 等 |

# 创建沙箱 [#创建沙箱]

`AMP_API_KEY` 从 [ampcode.com/settings](https://ampcode.com/settings) 获取，通过 `envs` 注入：

```javascript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
})
```

# 常用 CLI flag [#常用-cli-flag]

| Flag                          | 说明                                           |
| ----------------------------- | -------------------------------------------- |
| `-x "&lt;prompt&gt;"`         | 无头（非交互）模式                                    |
| `--dangerously-allow-all`     | 自动批准工具调用，沙箱隔离环境下可用                           |
| `--stream-json`               | 实时输出 JSONL 事件流（包含 tool 调用、token 用量、推理过程等元数据） |
| `threads list --json`         | 列出当前线程                                       |
| `threads continue &lt;id&gt;` | 在指定线程上继续                                     |

# 无头执行 [#无头执行]

```javascript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
})

const result = await sandbox.commands.run(
  `amp --dangerously-allow-all -x "Create a hello world HTTP server in Go"`,
)

console.log(result.stdout)
await sandbox.kill()
```

# Git 仓库集成 [#git-仓库集成]

```javascript
await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
  path: '/home/user/repo',
  username: 'x-access-token',
  password: process.env.GITHUB_TOKEN,
  depth: 1,
})

const result = await sandbox.commands.run(
  `cd /home/user/repo && amp --dangerously-allow-all -x "Add error handling to all API endpoints"`,
  { onStdout: (data) => process.stdout.write(data) },
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)
```

# 流式 JSON 输出 [#流式-json-输出]

`--stream-json` 会持续输出 JSONL 事件，可用于实时观测 token 用量、工具调用与决策过程：

```javascript
const result = await sandbox.commands.run(
  `amp --dangerously-allow-all --stream-json -x "Find and fix all TODO comments"`,
  {
    onStdout: (data) => {
      for (const line of data.split('\n').filter(Boolean)) {
        const event = JSON.parse(line)
        if (event.type === 'assistant') {
          console.log(`[assistant] tokens: ${event.message.usage?.output_tokens}`)
        }
      }
    },
  },
)
```

# 线程管理 [#线程管理]

Amp 维护持久化的对话线程，可在多次调用之间复用：

```javascript
const initial = await sandbox.commands.run(
  `cd /home/user/repo && amp --dangerously-allow-all -x "Analyze the codebase and create a refactoring plan"`,
)

const threads = await sandbox.commands.run('amp threads list --json')
const threadId = JSON.parse(threads.stdout)[0].id

const followUp = await sandbox.commands.run(
  `cd /home/user/repo && amp threads continue ${threadId} --dangerously-allow-all -x "Now implement step 1 of the plan"`,
)
```

# 参考 [#参考]

* Amp 官网：[https://ampcode.com](https://ampcode.com)
