T
traeai
登录
返回首页
Vercel News

Program Claude Code, Codex, Pi and other agent harnesses with AI SDK

8.5Score

TL;DR · AI 摘要

Vercel 推出 AI SDK 7,引入 HarnessAgent,支持 Claude Code、Codex 和 Pi 等代理框架,简化模型切换和代理管理。

核心要点

  • HarnessAgent 允许用户通过统一 API 调用多个代理框架,如 Claude Code、Codex 和 Pi。
  • AI SDK 7 的 HarnessAgent 支持在沙箱环境中运行代理,保障主机环境安全。
  • HarnessAgent 与现有 AI SDK 工具兼容,无需修改用户界面代码即可集成。

结构提纲

按章节快速跳转。

  1. Vercel 推出 AI SDK 7,引入 HarnessAgent,简化代理框架的使用和模型切换。

  2. HarnessAgent 提供统一 API,支持多个代理框架,如 Claude CodeCodexPi

  3. 文章提供 agent.ts 示例代码,展示如何使用 HarnessAgent 创建代理。

  4. HarnessAgent 在沙箱环境中运行代理,确保主机环境安全。

  5. HarnessAgent 与现有 AI SDK 工具兼容,无需修改用户界面代码。

  6. HarnessAgent 当前为实验性发布,API 可能会有重大变更。

思维导图

用一张图看清主题之间的关系。

查看大纲文本(无障碍 / 无 JS 友好)
  • AI SDK 7 和 HarnessAgent
    • HarnessAgent 的功能
      • 支持多个代理框架
      • 统一 API
      • 沙箱环境
    • 兼容性
      • 与现有 AI SDK 工具兼容
    • 实验性发布
      • API 可能会有重大变更

金句 / Highlights

值得收藏与分享的关键句。

#AI SDK#Vercel#HarnessAgent#代理框架
打开原文

使用 AI SDK 编程 Claude Code、Codex、Pi 等代理框架 - Vercel

1 分钟阅读

复制链接

2026 年 6 月 12 日

AI SDK 7 引入了 HarnessAgent,这是一个用于运行已有代理框架的单一 API,包括 Claude Code、Codex 和 Pi。AI SDK 一直允许你在不重写代理的情况下切换模型。现在你也可以以同样的方式切换代理框架。

编写一次代理。使用当前、3 个月后、一年后可用的最佳代理框架。

代理框架管理模型调用之上的组件,包括技能、沙箱、会话、权限流程、压缩、运行时配置和子代理。AI SDK 通过统一的代理框架抽象来规范化这些功能的访问。

此次实验性发布初步包括 Claude Code、Codex 和 Pi 的代理框架适配器,更多适配器即将推出。

agent.ts

ts
1
import
{
HarnessAgent
}
from
'@ai-sdk/harness/agent'
;
2
import
{
claudeCode
}
from
'@ai-sdk/harness-claude-code'
;
3
import
{
createVercelSandbox
}
from
'@ai-sdk/sandbox-vercel'
;
4
5
const
agent
=
new
HarnessAgent
(
{
6
harness
:
claudeCode
,
7
sandbox
:
createVercelSandbox
(
{
8
runtime
:
'node24'
,
9
ports
:
[
4000
]
,
10
}
)
,
11
tools
:
{
/* pass custom tools */
}
,
12
skills
:
[
/* pass custom skills */
]
,
13
}
)
;
14
15
const
session
=
await
agent
.
createSession
(
)
;
16
17
try
{
18
const
result
=
await
agent
.
stream
(
{
19
session
,
20
prompt
:
'Check the test failures and fix the production code.'
,
21
}
)
;
22
23
for
await
(
const
part
of
result
.
fullStream
)
{
24
if
(
part
.
type
===
'text-delta'
)
{
25
process
.
stdout
.
write
(
part
.
text
)
;
26
}
27
}
28
}
finally
{
29
await
session
.
destroy
(
)
;
30
}

使用 Claude Code 创建一个代理框架支持的代理

将 claudeCode 替换为 codex 或 pi,保持相同的 HarnessAgent 流程。每个代理框架都在一个沙箱工作区中运行代理,确保主机环境的安全。

HarnessAgent.generate() 和 HarnessAgent.stream() 都返回与 AI SDK 兼容的结果。如果你的应用程序已经使用了 useChat 或其他 AI SDK 工具,你可以直接替换为 HarnessAgent,而无需更改用户界面代码。

HarnessAgent 可在 AI SDK 的 Canary 版本中使用。阅读 AI SDK 代理框架文档以开始使用。

代理框架包是实验性 的。随着这个早期 API 的进一步完善,不同版本之间可能会出现破坏性变更。

AI 可能会生成不准确的信息,请核实重要内容