copilotkit-agui
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAG-UI Protocol Skill
AG-UI 协议技能
Overview
概述
AG-UI (Agent-User Interaction) is CopilotKit's open event-based protocol for agent-to-UI communication. All agent-frontend interaction flows through typed events streamed over SSE (Server-Sent Events) or binary protobuf transport. Agents implement returning an RxJS , and the client SDK handles event application, state management, and message history.
AbstractAgent.run()Observable<BaseEvent>AG-UI (Agent-User Interaction) 是CopilotKit开源的基于事件的Agent到UI通信协议。所有Agent与前端的交互都通过SSE(Server-Sent Events)或二进制protobuf传输的类型化事件流完成。Agent实现返回一个RxJS ,客户端SDK负责处理事件应用、状态管理和消息历史。
AbstractAgent.run()Observable<BaseEvent>When to Use
适用场景
- Building a custom agent backend that needs to speak AG-UI
- Implementing for a new framework integration
AbstractAgent.run() - Debugging why events aren't reaching the frontend or arriving malformed
- Understanding event ordering (lifecycle, text, tool calls, state)
- Working with state synchronization (snapshots vs JSON Patch deltas)
- Implementing human-in-the-loop interrupt/resume flows
- Troubleshooting SSE streaming or encoding issues
- 构建需要支持AG-UI协议的自定义Agent后端
- 为新的框架集成实现方法
AbstractAgent.run() - 调试事件无法到达前端或格式异常的问题
- 理解事件排序规则(生命周期、文本、工具调用、状态)
- 处理状态同步相关需求(快照对比JSON Patch增量更新)
- 实现人在回路的中断/恢复流程
- 排查SSE流式传输或编码问题
When NOT to Use
不适用场景
- For CopilotKit React hooks and frontend components, use
copilotkit-develop - For CopilotKit runtime setup and configuration, use
copilotkit-setup - For framework-specific integration guides (LangGraph, Mastra, CrewAI), use
copilotkit-integrations
- 如需使用CopilotKit React hooks和前端组件,请参考相关内容
copilotkit-develop - 如需进行CopilotKit运行时的安装和配置,请参考相关内容
copilotkit-setup - 如需框架专属的集成指南(LangGraph、Mastra、CrewAI),请参考相关内容
copilotkit-integrations
Quick Reference
快速参考
Event Families
事件分类
| Family | Events | Purpose |
|---|---|---|
| Lifecycle | | Run boundaries and progress |
| Text | | Streaming text messages |
| Tool Calls | | Agent tool invocations |
| State | | State synchronization |
| Reasoning | | Chain-of-thought visibility |
| Activity | | Structured progress updates |
| Custom | | Extension points |
| 分类 | 事件 | 用途 |
|---|---|---|
| 生命周期 | | 运行边界与进度提示 |
| 文本 | | 流式文本消息传输 |
| 工具调用 | | Agent工具调用 |
| 状态 | | 状态同步 |
| 推理 | | 思维链可视化 |
| 活动 | | 结构化进度更新 |
| 自定义 | | 扩展点 |
Convenience Chunk Events
便捷分块事件
TEXT_MESSAGE_CHUNKTOOL_CALL_CHUNKtransformChunksTEXT_MESSAGE_CHUNKTOOL_CALL_CHUNKtransformChunksSSE Wire Format
SSE传输格式
Each event is a JSON object sent as an SSE data line:
data: {"type":"RUN_STARTED","threadId":"t1","runId":"r1"}\n\n
data: {"type":"TEXT_MESSAGE_START","messageId":"m1","role":"assistant"}\n\n
data: {"type":"TEXT_MESSAGE_CONTENT","messageId":"m1","delta":"Hello"}\n\n
data: {"type":"TEXT_MESSAGE_END","messageId":"m1"}\n\n
data: {"type":"RUN_FINISHED","threadId":"t1","runId":"r1"}\n\n每个事件都是一个JSON对象,作为SSE的数据行发送:
data: {"type":"RUN_STARTED","threadId":"t1","runId":"r1"}\n\n
data: {"type":"TEXT_MESSAGE_START","messageId":"m1","role":"assistant"}\n\n
data: {"type":"TEXT_MESSAGE_CONTENT","messageId":"m1","delta":"Hello"}\n\n
data: {"type":"TEXT_MESSAGE_END","messageId":"m1"}\n\n
data: {"type":"RUN_FINISHED","threadId":"t1","runId":"r1"}\n\nPackages
相关包
| Package | npm | Purpose |
|---|---|---|
| Events, types, schemas | Protocol definition |
| AbstractAgent, HttpAgent, middleware, event application | Client SDK |
| EventEncoder (SSE + protobuf) | Server-side encoding |
| 包名 | npm包内容 | 用途 |
|---|---|---|
| 事件、类型、schemas | 协议定义 |
| AbstractAgent, HttpAgent, 中间件, 事件应用 | 客户端SDK |
| EventEncoder (SSE + protobuf) | 服务端编码 |
Workflow: Building an AG-UI Backend
工作流程:构建AG-UI后端
- Define your endpoint -- Accept POST with body, respond with
RunAgentInputtext/event-stream - Parse input -- Extract ,
threadId,runId,messages,tools,statefrom the request bodycontext - Emit events in order -- first, then content events, then
RUN_STARTEDorRUN_FINISHEDRUN_ERROR - Encode as SSE -- Use 's
@ag-ui/encoderor manually writeEventEncoder.encode()data: JSON\n\n - Handle tool results -- Client sends back; agent processes and continues
TOOL_CALL_RESULT
See for a complete working example.
references/building-agents.md- 定义你的接口 -- 接收包含请求体的POST请求,返回
RunAgentInput类型的响应text/event-stream - 解析输入 -- 从请求体中提取、
threadId、runId、messages、tools、state参数context - 按顺序发送事件 -- 首先发送,然后是内容事件,最后发送
RUN_STARTED或RUN_FINISHEDRUN_ERROR - 编码为SSE格式 -- 使用的
@ag-ui/encoder方法,或者手动拼接EventEncoder.encode()格式data: JSON\n\n - 处理工具调用结果 -- 客户端会返回,Agent处理该结果后继续执行流程
TOOL_CALL_RESULT
完整可运行的示例可参考。
references/building-agents.mdKey Protocol Rules
核心协议规则
- Every run MUST start with and end with
RUN_STARTEDorRUN_FINISHEDRUN_ERROR - must be non-empty
TEXT_MESSAGE_CONTENT.delta - Tool call events are linked by
toolCallId - uses RFC 6902 JSON Patch operations
STATE_DELTA - Multiple sequential runs are supported -- each must complete before the next starts
- Messages accumulate across runs; state continues unless reset by
STATE_SNAPSHOT
- 每次运行必须以开头,以
RUN_STARTED或RUN_FINISHED结束RUN_ERROR - 不能为空
TEXT_MESSAGE_CONTENT.delta - 工具调用事件通过关联
toolCallId - 遵循RFC 6902 JSON Patch操作规范
STATE_DELTA - 支持多个顺序运行的任务 -- 前一个任务必须完成后才能启动下一个
- 消息会在多次运行间累积;除非通过重置,否则状态会持续保留
STATE_SNAPSHOT
References
参考资料
- -- Complete event type reference with schemas and examples
references/protocol-spec.md - -- Step-by-step guide to building AG-UI backends
references/building-agents.md - -- ASCII sequence diagrams for common flows
references/event-flow-diagrams.md - -- @ag-ui/client API reference
references/client-sdk.md
- -- 完整的事件类型参考,包含schema和示例
references/protocol-spec.md - -- 构建AG-UI后端的分步指南
references/building-agents.md - -- 常见流程的ASCII时序图
references/event-flow-diagrams.md - -- @ag-ui/client API参考
references/client-sdk.md