微信扫码
添加专属顾问
我要投稿
AG-UI协议:开启AI Agent与用户高效交互的新篇章。 核心内容: 1. AG-UI协议的提出背景与必要性 2. AG-UI协议的核心特点与优势 3. AG-UI协议的关键能力模块与事件机制
随着多智能体系统(Multi-Agent Systems)和大语言模型(LLMs)技术的飞速发展,AI Agent 的工作能力越来越强。然而,在落地到真实应用中时,我们却发现一项关键能力仍旧缺失——如何让智能体与用户进行顺畅、高效的交互?CopilotKit 团队提出的 AG-UI 协议(Agent-User Interaction Protocol),正是为此而生。
当前市面上的 AI 系统,大多聚焦在后端的 Agent 执行、工具调用、模型编排等逻辑层面。然而当这些智能体要接入前端界面、嵌入产品时,开发者却面临种种困境:
这些问题直接影响 Agent 系统的可用性与用户体验。
AG-UI 是一个开源、轻量的协议,旨在规范 AI Agent 与前端用户界面之间的通信流程。
STATE_SNAPSHOT 与增量更新 STATE_DELTA ,高效同步状态 | |
这些能力构成了 AG-UI 成为生产级 Agent 应用的关键基础。
AG-UI 协议中一切交互都围绕“事件”进行组织。核心事件类别包括:
RUN_STARTED
, RUN_FINISHED
),监控Agent运行进度。TEXT_MESSAGE_START
, TEXT_MESSAGE_CONTENT
, TEXT_MESSAGE_END
),处理文本流式内容的事件。TOOL_CALL_START
, TOOL_CALL_ARGS
, TOOL_CALL_END
),管理 Agent 对工具的执行。STATE_SNAPSHOT
, STATE_DELTA
),同步 Agent 和 UI 之间的状态。RAW
, CUSTOM
)这一机制不仅提供了强大可扩展性,还简化了前端的 UI 渲染逻辑。
AG-UI 与 A2A(Agent-to-Agent)和 MCP(Model Context Protocol)形成互补:
AG-UI 的工作流程基于事件驱动架构,主要包括以下几个步骤:
前端发送请求:
RunAgentInput
类型的 JSON 请求,发送到后端的 /awp
端点。后端处理请求:
RunAgentInput
,提取 thread_id
、run_id
和用户消息等信息。事件流通信:
(1)RunStartedEvent
:表示代理开始处理请求。
(2)TextMessageContentEvent
:代理生成的文本内容。
(3)RunFinishedEvent
:代理完成处理。
4. 前端更新界面:
创建一个基础服务
mkdir awp-endpoint && cd awp-endpoint
npm init -y
npm install typescript ts-node @types/node @types/express --save-dev
npx tsc --init
npm install express openai @ag-ui/core @ag-ui/encoder uuid
npm install @types/uuid --save-dev
import express from"express"
import { Request, Response } from"express"
const app = express()
app.use(express.json())
app.post("/awp", (req: Request, res: Response) => {
res.json({ message: "Hello World" })
})
app.listen(8000, () => {
console.log("Server running on http://localhost:8000")
})
解析 AG-UI 输入并添加事件流(SSE)
import express, { Request, Response } from"express"
import { RunAgentInputSchema, RunAgentInput, EventType } from"@ag-ui/core"
import { EventEncoder } from"@ag-ui/encoder"
const app = express()
app.use(express.json())
app.post("/awp", async (req: Request, res: Response) => {
try {
// 解析并验证请求体
const input: RunAgentInput = RunAgentInputSchema.parse(req.body)
// 设置 SSE headers
res.setHeader("Content-Type", "text/event-stream")
res.setHeader("Cache-Control", "no-cache")
res.setHeader("Connection", "keep-alive")
// 创建事件 encoder
const encoder = new EventEncoder()
// 发送 started 事件
const runStarted = {
type: EventType.RUN_STARTED,
threadId: input.threadId,
runId: input.runId,
}
res.write(encoder.encode(runStarted))
// 发送 finished 事件
const runFinished = {
type: EventType.RUN_FINISHED,
threadId: input.threadId,
runId: input.runId,
}
res.write(encoder.encode(runFinished))
// 结束响应
res.end()
} catch (error) {
res.status(422).json({ error: (error asError).message })
}
})
app.listen(8000, () => {
console.log("Server running on http://localhost:8000")
})
集成 OpenAI
import express, { Request, Response } from"express"
import {
RunAgentInputSchema,
RunAgentInput,
EventType,
Message,
} from"@ag-ui/core"
import { EventEncoder } from"@ag-ui/encoder"
import { OpenAI } from"openai"
import { v4 as uuidv4 } from"uuid"
const app = express()
app.use(express.json())
app.post("/awp", async (req: Request, res: Response) => {
try {
// 解析并验证请求体
const input: RunAgentInput = RunAgentInputSchema.parse(req.body)
// 设置 SSE headers
res.setHeader("Content-Type", "text/event-stream")
res.setHeader("Cache-Control", "no-cache")
res.setHeader("Connection", "keep-alive")
// 创建事件 encoder
const encoder = new EventEncoder()
// 发送 started 事件
const runStarted = {
type: EventType.RUN_STARTED,
threadId: input.threadId,
runId: input.runId,
}
res.write(encoder.encode(runStarted))
// 初始化 OpenAI 客户端
const client = new OpenAI()
// 将 AG-UI 消息转换为 OpenAI 消息格式
const openaiMessages = input.messages
.filter((msg: Message) =>
["user", "system", "assistant"].includes(msg.role)
)
.map((msg: Message) => ({
role: msg.role as"user" | "system" | "assistant",
content: msg.content || "",
}))
// 生成消息 ID
const messageId = uuidv4()
// 发送 ‘文本消息开始’ 事件
const textMessageStart = {
type: EventType.TEXT_MESSAGE_START,
messageId,
role: "assistant",
}
res.write(encoder.encode(textMessageStart))
// 创建流式传输完成请求
const stream = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: openaiMessages,
stream: true,
})
// 处理流并发送 ‘文本消息内容’ 事件
forawait (const chunk of stream) {
if (chunk.choices[0]?.delta?.content) {
const content = chunk.choices[0].delta.content
const textMessageContent = {
type: EventType.TEXT_MESSAGE_CONTENT,
messageId,
delta: content,
}
res.write(encoder.encode(textMessageContent))
}
}
// 发送 ‘文本消息结束’ 事件
const textMessageEnd = {
type: EventType.TEXT_MESSAGE_END,
messageId,
}
res.write(encoder.encode(textMessageEnd))
// 发送 finished 事件
const runFinished = {
type: EventType.RUN_FINISHED,
threadId: input.threadId,
runId: input.runId,
}
res.write(encoder.encode(runFinished))
// 结束响应
res.end()
} catch (error) {
res.status(422).json({ error: (error asError).message })
}
})
app.listen(8000, () => {
console.log("Server running on http://localhost:8000")
})
现在,已构建了兼容 AG-UI 的 Agent,可以将其连接到支持 AG-UI 协议的任何前端。 例如,CopilotKit 提供了一套丰富的 UI 组件,旨在与 AG-UI 代理无缝协作。
目前 AG-UI 已经原生支持多个框架与平台:
前端开发者可通过 CopilotKit 提供的 React Hooks 快速集成 UI 能力,后端则通过 Python/TS SDK 发送标准事件流,真正实现 一次接入,多框架兼容。
AG-UI 的出现,标志着智能体系统迈入“用户协作”的新阶段:
在产品实际部署中,AG-UI 能有效降低开发门槛、提升交互体验、增强稳定性与可维护性。
随着智能体生态的逐渐成熟,前后端协同将成为决定 AI 应用成败的关键因素。而 AG-UI 协议正是其中最重要的一环。它不仅解决了技术层面的接口统一问题,更为未来智能体与人协作的产品形态奠定了坚实基础。
正如 CopilotKit 团队所说: “就像 REST 之于 API,AG-UI 是 Agent 之于用户界面的流式交互协议。”
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-05-29
WebDancer:构建端到端Agentic信息搜索智能体
2025-05-29
从空调到飞机:物理世界AGI的进阶之路,P-1 AI的硬核探索
2025-05-29
Opera发布AI时代入口Agentic Browser——Opera Neon
2025-05-29
NoAgent 正式发布,又是一个Agent智能体巨作,一手实测(附邀请码和提示词)
2025-05-29
Agent 工程师绕不开的必修课:API 网关 vs API 管理
2025-05-29
大模型时代如何挡住攻击?一家网安企业突破“联动防御+大模型安全”难关
2025-05-29
重新理解Agent的边界与潜力|AI转型访谈录
2025-05-29
送给智者的礼物——天机:思维模型MCP Server
2024-08-13
2024-06-13
2024-08-21
2024-07-31
2024-09-23
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-07-20