@ag-ui/client

中文文档
1.0.4发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 605.2万

Client SDK for connecting to Agent-User Interaction (AG-UI) Protocol servers.

原始使用文档
文档目录9 节
  1. Installation
  2. Features
  3. Quick example
  4. Enforcement limits
  5. Using Middleware
  6. Documentation
  7. Contributing
  8. License
  9. Activity snapshots

@ag-ui/client

Client SDK for connecting to Agent-User Interaction (AG-UI) Protocol servers.

@ag-ui/client provides agent implementations that handle the full lifecycle of AG-UI communication: connecting to servers, processing streaming events, managing state mutations, and providing reactive subscriber hooks.

Installation

npm install @ag-ui/client zod
pnpm add @ag-ui/client zod
yarn add @ag-ui/client zod

zod (3.25.18+ or 4.x) is a peer dependency. The client's enforcement stage validates every event with the schemas on @ag-ui/core/schemas, using the zod your application installs rather than a bundled copy.

Features

  • 🔗 HTTP connectivity – HttpAgent for direct server connections with SSE/protobuf support
  • 🏗️ Custom agents – AbstractAgent base class for building your own transport layer
  • 📡 Event streaming – Full AG-UI event processing with validation and transformation
  • 🔄 State management – Automatic message/state tracking with reactive updates
  • 🪝 Subscriber system – Middleware-style hooks for logging, persistence, and custom logic
  • 🎯 Middleware support – Transform and filter events with function or class-based middleware

Quick example

import { HttpAgent } from "@ag-ui/client";

const agent = new HttpAgent({
  url: "https://api.example.com/agent",
  headers: { Authorization: "Bearer token" },
});

const result = await agent.runAgent({
  messages: [{ role: "user", content: "Hello!" }],
});

console.log(result.newMessages);

Enforcement limits

Every event an agent receives, and every input HttpAgent sends, is stripped and validated against the protocol schemas. To keep that work bounded, the client refuses a single event or input with more than 1,000,000 container entries (array elements, object keys and record keys; opaque fields such as state are not counted). The run fails with an AGUIEnforcementLimitError naming the path where the limit was crossed.

// Raise the limit, or pass Infinity to disable it.
const agent = new HttpAgent({ url, maxEnforcedEntries: 5_000_000 });

The limit covers stripping and validation. Single linear passes that run before them, such as parsing the transport stream, are not counted, and opaque fields are not limited at all: a huge state passes enforcement and is cloned and applied downstream at full cost.

Using Middleware

import { HttpAgent, FilterToolCallsMiddleware } from "@ag-ui/client";

const agent = new HttpAgent({
  url: "https://api.example.com/agent",
});

// Add middleware to transform or filter events
agent.use(
  // Function middleware for logging
  (input, next) => {
    console.log("Starting run:", input.runId);
    return next.run(input);
  },

  // Class middleware for filtering tool calls
  new FilterToolCallsMiddleware({
    allowedToolCalls: ["search", "calculate"]
  })
);

await agent.runAgent();

Documentation

Contributing

Bug reports and pull requests are welcome! Please read our contributing guide first.

License

MIT © 2025 AG-UI Protocol Contributors

Activity snapshots

MESSAGES_SNAPSHOT.metadata["@ag-ui/client"].authoritativeActivityTypes controls which activity types the snapshot replaces. An array owns those types; [] owns none, and null owns all types, even for an empty snapshot. Without a declaration, the existing rule applies: a snapshot containing any activity replaces all activities, while a transcript-only snapshot preserves them. A missing namespace or missing field uses this legacy rule. An invalid namespace or field owns no types, including arrays containing non-string entries.

Authority controls deletion of omitted activity messages. A matching message ID always updates in place, even for a type outside the declared scope. Existing message positions are preserved and new IDs are appended in snapshot order. This convention does not reconcile changed IDs or reposition restored activities.

History projectors must preserve full authority and extend explicit scopes with the types they reconstruct. withAuthoritativeActivityTypes implements this rule; call it on the incoming snapshot before replacing its messages.

版本与发布记录

@ag-ui/client 共发布 82 个版本,首次发布于 2025/04/30;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.4latest2026/11/05今天
1.0.4-canary.1793805408.0canary2026/11/041 天前
1.0.4-canary.1792171324.02026/10/1720 天前
1.0.32026/10/1620 天前
1.0.22026/10/051 个月前
1.0.12026/09/291 个月前

其他发布通道:alpha → 0.0.50-alpha.0,canary → 1.0.4-canary.1793805408.0。

关于 @ag-ui/client 的常见问题

@ag-ui/client 是什么?

Client SDK for connecting to Agent-User Interaction (AG-UI) Protocol servers.

如何安装 @ag-ui/client?

在项目目录运行 npm install @ag-ui/client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^3.25.18 || ^4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ag-ui/client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ag-ui/client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ag-ui/client 可以商用吗?

@ag-ui/client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ag-ui/client 的下载量有多少?

根据 npm 官方下载统计,@ag-ui/client 在 2026/10/05 – 2026/11/03 这 30 天内被下载 6,052,407 次,日均约 201,747 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ag-ui/client 还在维护吗?

最新版本 1.0.4 发布于 2026/11/05(今天);至今共发布 82 个版本,其中过去 12 个月发布了 43 个;首次发布于 2025/04/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。