前后端通信协议
HTTP Agent 完全兼容 AG-UI 协议,使用 SSE 实现前后端实时流式通信。
可直接使用 AG-UI TypeScript SDK 的 HttpAgent 进行接入:
import { HttpAgent } from "@ag-ui/client";
const agent = new HttpAgent({
url: "https://your-endpoint/send-message",
headers: {
Authorization: "Bearer your-api-key",
},
});
POST /send-message
请求体
请求体遵循 AG-UI 协议的 RunAgentInput 结构:
interface RunAgentInput {
threadId: string; // 会话线程 ID
runId: string; // 本次运行的唯一 ID
parentRunId?: string; // 可选,父运行 ID(用于分支/回溯场景)
state?: any; // 可选,当前状态
messages: Message[]; // 消息历史
tools: Tool[]; // 客户端工具列表
context: Context[]; // 上下文信息
forwardedProps?: any; // 可选,透传属性
}
说明:
threadId整个对话保持一致,服务端通过该 ID 维护会话状态runId每次调用生成新的唯一 IDmessages消息列表
消息类型
type Message =
| DeveloperMessage
| SystemMessage
| AssistantMessage
| UserMessage
| ToolMessage;
interface DeveloperMessage {
id: string;
role: "developer";
content: string;
name?: string;
}
interface SystemMessage {
id: string;
role: "system";
content: string;
name?: string;
}
interface AssistantMessage {
id: string;
role: "assistant";
content?: string;
name?: string;
toolCalls?: ToolCall[];
}
interface UserMessage {
id: string;
role: "user";
content: string | InputContent[];
name?: string;
}
interface ToolMessage {
id: string;
role: "tool";
content: string;
toolCallId: string;
error?: string;
}
interface ToolCall {
id: string;
type: "function";
function: { name: string; arguments: string };
}
// 多模态输入内容
type InputContent =
| { type: "text"; text: string }
| { type: "binary"; mimeType: string; id?: string; url?: string; data?: string; filename?: string };
工具定义
interface Tool {
name: string;
description: string;
parameters: any; // JSON Schema
}
上下文定义
interface Context {
description: string;
value: string;
}
示例请求:
{
"threadId": "550e8400-e29b-41d4-a716-446655440000",
"runId": "run_001",
"messages": [
{ "id": "msg_1", "role": "user", "content": "今天北京天气怎么样?" }
],
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
],
"context": [],
"state": {}
}