AG-UI Protocol
CloudBase Agents are fully compatible with the AG-UI Protocol, a standardized front-end and back-end communication protocol for real-time interaction between Agents and clients.
Protocol Overview
The AG-UI protocol implements streaming communication based on SSE (Server-Sent Events) and supports:
- Real-time transmission of Agent execution processes
- Tool invocation and result returns
- Human-machine interaction scenarios
- State synchronization
Request Format
HTTP Request
POST /agent HTTP/1.1
Content-Type: application/json
Accept: text/event-stream
{
"threadId": "thread-xxx",
"runId": "run-xxx",
"messages": [
{
"id": "msg-1",
"role": "user",
"content": "Hello"
}
],
"tools": [],
"context": [],
"forwardedProps": {}
}
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
threadId | string | No | Session ID for multi-turn conversations |
runId | string | No | Run ID for this invocation |
messages | array | Yes | Message list |
tools | array | No | Front-end tool definitions |
context | array | No | Context information |
forwardedProps | object | No | Pass-through parameters |
Message Format
interface Message {
id: string; // Message ID
role: "user" | "assistant" | "system" | "tool";
content: string; // Message content
name?: string; // Tool name (when role is "tool")
toolCallId?: string; // Tool call ID (when role is "tool")
}
Response Format
Responses use SSE format, with each event starting with data::
data: {"type":"TEXT_MESSAGE_START","messageId":"msg-1"}
data: {"type":"TEXT_MESSAGE_CONTENT","messageId":"msg-1","delta":"Hello"}
data: {"type":"TEXT_MESSAGE_END","messageId":"msg-1"}
data: {"type":"RUN_FINISHED"}
Event Types
Lifecycle Events
| Event Type | Description |
|---|---|
RUN_STARTED | Agent starts running |
RUN_FINISHED | Agent finishes running |
RUN_ERROR | Agent encounters an error |
Message Events
| Event Type | Description |
|---|---|
TEXT_MESSAGE_START | Text message starts |
TEXT_MESSAGE_CONTENT | Text message content (incremental) |
TEXT_MESSAGE_END | Text message ends |
Tool Call Events
| Event Type | Description |
|---|---|
TOOL_CALL_START | Tool call starts |
TOOL_CALL_ARGS | Tool call arguments (incremental) |
TOOL_CALL_END | Tool call ends |
State Events
| Event Type | Description |
|---|---|
STATE_SNAPSHOT | State snapshot |
STATE_DELTA | State incremental update |
MESSAGES_SNAPSHOT | Messages snapshot |
Step Events
| Event Type | Description |
|---|---|
STEP_STARTED | Step starts |
STEP_FINISHED | Step finishes |
Tool Invocation
Server-Side Tools
Server-side tools are executed directly by the Agent backend:
Front-End Tools
Front-end tools require the client to execute and return results:
Front-end tool definition example:
const tools = [
{
name: "get_location",
description: "Get user's current location",
parameters: {
type: "object",
properties: {},
required: []
}
}
];
Human-Machine Interaction
The AG-UI protocol supports various human-machine interaction scenarios:
User Confirmation
An Agent can request user confirmation before executing an operation:
// Server sends confirmation request
{
type: "TOOL_CALL_START",
toolCallId: "call-1",
toolCallName: "confirm_action"
}
// Client returns confirmation result
{
role: "tool",
toolCallId: "call-1",
content: JSON.stringify({ confirmed: true })
}
User Input
An Agent can request additional input from the user:
// Server requests input
{
type: "TOOL_CALL_START",
toolCallId: "call-2",
toolCallName: "request_input",
args: { prompt: "Please enter your email address" }
}
// Client returns input
{
role: "tool",
toolCallId: "call-2",
content: JSON.stringify({ input: "user@example.com" })
}
State Management
State Snapshot
An Agent can send a complete state snapshot:
{
type: "STATE_SNAPSHOT",
snapshot: {
currentStep: "analyzing",
progress: 50,
results: []
}
}
State Delta
An Agent can send incremental state updates:
{
type: "STATE_DELTA",
delta: [
{ op: "replace", path: "/progress", value: 75 }
]
}
Error Handling
Error Events
{
type: "RUN_ERROR",
error: {
code: "TOOL_EXECUTION_ERROR",
message: "Tool execution failed"
}
}
Common Error Codes
| Error Code | Description |
|---|---|
INVALID_REQUEST | Request format error |
TOOL_NOT_FOUND | Tool not found |
TOOL_EXECUTION_ERROR | Tool execution failed |
MODEL_ERROR | Model invocation failed |
TIMEOUT | Request timeout |
Best Practices
1. Message ID Management
Ensure each message has a unique ID for easy tracking and debugging:
const messageId = `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2. Session Management
Use threadId to manage multi-turn conversations:
// New session
const threadId = `thread-${Date.now()}`;
// Subsequent requests use the same threadId
{
threadId: threadId,
messages: [...]
}
3. Timeout Handling
Set reasonable timeout durations:
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 60000);
fetch('/agent', {
signal: controller.signal,
// ...
});
4. Retry Mechanism
Implement exponential backoff retry:
async function fetchWithRetry(url, options, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fetch(url, options);
} catch (error) {
if (i === maxRetries - 1) throw error;
await new Promise(r => setTimeout(r, Math.pow(2, i) * 1000));
}
}
}