AI SDK 7 引入了 HarnessAgent,这是一个用于运行现有代理框架的单一 API,包括 Claude Code、Codex 和 Pi。AI SDK 一直支持在不重写代理的情况下切换模型。现在你也可以用同样的方式切换框架。
编写一次代理。使用可用的最佳框架。
今天。3 个月后。一年后。
框架管理模型调用之上的组件,包括技能、沙箱、会话、权限流程、压缩、运行时配置和子代理。AI SDK 通过统一的框架抽象规范了对这些功能的访问。
此实验性版本的初始框架适配器包括 Claude Code、Codex 和 Pi,更多框架即将推出。
import { HarnessAgent } from '@ai-sdk/harness/agent';
import { claudeCode } from '@ai-sdk/harness-claude-code';
import { createVercelSandbox } from '@ai-sdk/sandbox-vercel';
const agent = new HarnessAgent({
harness: claudeCode,
sandbox: createVercelSandbox({
runtime: 'node24',
ports: [4000],
}),
tools: { /* pass custom tools */ },
skills: [ /* pass custom skills */ ],
});
const session = await agent.createSession();
try {
const result = await agent.stream({
session,
prompt: 'Check the test failures and fix the production code.',
});
for await (const part of result.fullStream) {
if (part.type === 'text-delta') {
process.stdout.write(part.text);
}
}
} finally {
await session.destroy();
}
使用 Claude Code 创建一个基于框架的代理
将 claudeCode 替换为 codex 或 pi,并保留相同的 HarnessAgent 流程。每个框架都在沙箱工作区中运行代理,从而确保宿主环境的安全。
HarnessAgent.generate() 和 HarnessAgent.stream() 均返回 AI SDK 兼容的结果。如果你的应用已经在使用 useChat 或相关的 AI SDK 工具,你可以直接替换为 HarnessAgent 而无需更改用户界面代码。
HarnessAgent 可在 AI SDK 金丝雀版本中使用。阅读 AI SDK 框架文档 开始使用。
框架包为 实验性。随着此早期 API 的进一步完善,版本之间可能会发生破坏性变更。
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.