|
| 1 | +# Agents |
| 2 | + |
| 3 | +Agents are the core components in the AG-UI protocol that process requests and generate responses. They establish a standardized way for front-end applications to communicate with AI services through a consistent interface, regardless of the underlying implementation. |
| 4 | + |
| 5 | +## What is an Agent? |
| 6 | + |
| 7 | +In AG-UI, an agent is a class that: |
| 8 | + |
| 9 | +1. Manages conversation state and message history |
| 10 | +2. Processes incoming messages and context |
| 11 | +3. Generates responses through an event-driven streaming interface |
| 12 | +4. Follows a standardized protocol for communication |
| 13 | + |
| 14 | +Agents can be implemented to connect with any AI service: |
| 15 | +- Large language models (LLMs) like GPT-4 or Claude |
| 16 | +- Custom AI systems |
| 17 | +- Retrieval augmented generation (RAG) systems |
| 18 | +- Multi-agent systems |
| 19 | + |
| 20 | +## Agent Architecture |
| 21 | + |
| 22 | +All agents extend the `AbstractAgent` class: |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { AbstractAgent, RunAgentInput, RunAgent } from "@ag-ui/client" |
| 26 | +import { Observable } from "rxjs" |
| 27 | + |
| 28 | +class MyAgent extends AbstractAgent { |
| 29 | + run(input: RunAgentInput): RunAgent { |
| 30 | + return () => new Observable(observer => { |
| 31 | + // Emit events |
| 32 | + observer.next({ type: "RUN_STARTED", threadId, runId }); |
| 33 | + // ... processing ... |
| 34 | + observer.next({ type: "RUN_FINISHED", threadId, runId }); |
| 35 | + observer.complete(); |
| 36 | + }); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Core Components |
| 42 | + |
| 43 | +1. **Configuration** - Agent ID, thread ID, initial state |
| 44 | +2. **Messages** - Conversation history |
| 45 | +3. **State** - Structured data persisting across interactions |
| 46 | +4. **Events** - Standardized messages for communication |
| 47 | +5. **Tools** - Functions for external system interaction |
| 48 | + |
| 49 | +## Agent Types |
| 50 | + |
| 51 | +### AbstractAgent |
| 52 | + |
| 53 | +The base class that all agents extend. Handles core event processing, state management, and message history. |
| 54 | + |
| 55 | +### HttpAgent |
| 56 | + |
| 57 | +Connects to remote AI services via HTTP: |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { HttpAgent } from "@ag-ui/client" |
| 61 | + |
| 62 | +const agent = new HttpAgent({ |
| 63 | + url: "https://your-agent-endpoint.com/agent", |
| 64 | + headers: { |
| 65 | + Authorization: "Bearer your-api-key" |
| 66 | + } |
| 67 | +}) |
| 68 | +``` |
| 69 | + |
| 70 | +### Custom Agents |
| 71 | + |
| 72 | +Extend `AbstractAgent` for custom integrations: |
| 73 | + |
| 74 | +```typescript |
| 75 | +class CustomAgent extends AbstractAgent { |
| 76 | + run(input: RunAgentInput): RunAgent { |
| 77 | + // Implement your agent logic |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Basic Implementation |
| 83 | + |
| 84 | +```typescript |
| 85 | +import { |
| 86 | + AbstractAgent, |
| 87 | + RunAgent, |
| 88 | + RunAgentInput, |
| 89 | + EventType, |
| 90 | + BaseEvent |
| 91 | +} from "@ag-ui/client" |
| 92 | +import { Observable } from "rxjs" |
| 93 | + |
| 94 | +class SimpleAgent extends AbstractAgent { |
| 95 | + run(input: RunAgentInput): RunAgent { |
| 96 | + const { threadId, runId } = input |
| 97 | + |
| 98 | + return () => new Observable<BaseEvent>(observer => { |
| 99 | + // 1. Emit RUN_STARTED |
| 100 | + observer.next({ |
| 101 | + type: EventType.RUN_STARTED, |
| 102 | + threadId, |
| 103 | + runId |
| 104 | + }) |
| 105 | + |
| 106 | + // 2. Send a message |
| 107 | + const messageId = Date.now().toString() |
| 108 | + |
| 109 | + observer.next({ |
| 110 | + type: EventType.TEXT_MESSAGE_START, |
| 111 | + messageId, |
| 112 | + role: "assistant" |
| 113 | + }) |
| 114 | + |
| 115 | + observer.next({ |
| 116 | + type: EventType.TEXT_MESSAGE_CONTENT, |
| 117 | + messageId, |
| 118 | + delta: "Hello, world!" |
| 119 | + }) |
| 120 | + |
| 121 | + observer.next({ |
| 122 | + type: EventType.TEXT_MESSAGE_END, |
| 123 | + messageId |
| 124 | + }) |
| 125 | + |
| 126 | + // 3. Emit RUN_FINISHED |
| 127 | + observer.next({ |
| 128 | + type: EventType.RUN_FINISHED, |
| 129 | + threadId, |
| 130 | + runId |
| 131 | + }) |
| 132 | + |
| 133 | + observer.complete() |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Agent Capabilities |
| 140 | + |
| 141 | +### Interactive Communication |
| 142 | + |
| 143 | +Bi-directional communication through event streams: |
| 144 | +- Real-time streaming responses character-by-character |
| 145 | +- Immediate feedback loops between user and AI |
| 146 | +- Progress indicators for long-running operations |
| 147 | +- Structured data exchange in both directions |
| 148 | + |
| 149 | +### Tool Usage |
| 150 | + |
| 151 | +Tools are defined and passed from the frontend: |
| 152 | + |
| 153 | +```typescript |
| 154 | +// Tool definition |
| 155 | +const confirmAction = { |
| 156 | + name: "confirmAction", |
| 157 | + description: "Ask the user to confirm an action", |
| 158 | + parameters: { |
| 159 | + type: "object", |
| 160 | + properties: { |
| 161 | + action: { type: "string", description: "Action to confirm" } |
| 162 | + }, |
| 163 | + required: ["action"] |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Pass tools to agent |
| 168 | +agent.runAgent({ |
| 169 | + tools: [confirmAction] |
| 170 | +}) |
| 171 | +``` |
| 172 | + |
| 173 | +Tool invocation events: |
| 174 | +1. `TOOL_CALL_START` - Beginning of tool call |
| 175 | +2. `TOOL_CALL_ARGS` - Streaming arguments |
| 176 | +3. `TOOL_CALL_END` - Completion |
| 177 | + |
| 178 | +### State Management |
| 179 | + |
| 180 | +Structured state persists across interactions: |
| 181 | + |
| 182 | +```typescript |
| 183 | +// Access state |
| 184 | +console.log(agent.state.preferences) |
| 185 | + |
| 186 | +// State updates during runs |
| 187 | +agent.runAgent().subscribe(event => { |
| 188 | + if (event.type === EventType.STATE_DELTA) { |
| 189 | + console.log("New state:", agent.state) |
| 190 | + } |
| 191 | +}) |
| 192 | +``` |
| 193 | + |
| 194 | +### Multi-Agent Collaboration |
| 195 | + |
| 196 | +- Delegate tasks to specialized agents |
| 197 | +- Coordinate workflows across multiple agents |
| 198 | +- Transfer state and context between agents |
| 199 | +- Maintain consistent frontend experience |
| 200 | + |
| 201 | +### Human-in-the-Loop Workflows |
| 202 | + |
| 203 | +- Request human input on decisions |
| 204 | +- Pause and resume execution |
| 205 | +- Review and modify outputs before finalization |
| 206 | +- Combine AI efficiency with human judgment |
| 207 | + |
| 208 | +### Conversational Memory |
| 209 | + |
| 210 | +```typescript |
| 211 | +// Access history |
| 212 | +console.log(agent.messages) |
| 213 | + |
| 214 | +// Add messages |
| 215 | +agent.messages.push({ |
| 216 | + id: "msg_123", |
| 217 | + role: "user", |
| 218 | + content: "Can you explain that?" |
| 219 | +}) |
| 220 | +``` |
| 221 | + |
| 222 | +## Agent Configuration |
| 223 | + |
| 224 | +```typescript |
| 225 | +interface AgentConfig { |
| 226 | + agentId?: string; // Unique identifier |
| 227 | + description?: string; // Human-readable description |
| 228 | + threadId?: string; // Conversation thread ID |
| 229 | + initialMessages?: Message[]; // Starting messages |
| 230 | + initialState?: State; // Initial state |
| 231 | +} |
| 232 | + |
| 233 | +const agent = new HttpAgent({ |
| 234 | + agentId: "my-agent-123", |
| 235 | + description: "A helpful assistant", |
| 236 | + threadId: "thread-456", |
| 237 | + initialMessages: [ |
| 238 | + { id: "1", role: "system", content: "You are helpful." } |
| 239 | + ], |
| 240 | + initialState: { language: "English" } |
| 241 | +}) |
| 242 | +``` |
| 243 | + |
| 244 | +## Using Agents |
| 245 | + |
| 246 | +```typescript |
| 247 | +const agent = new HttpAgent({ |
| 248 | + url: "https://your-agent-endpoint.com/agent" |
| 249 | +}) |
| 250 | + |
| 251 | +agent.messages = [ |
| 252 | + { id: "1", role: "user", content: "Hello!" } |
| 253 | +] |
| 254 | + |
| 255 | +agent.runAgent({ |
| 256 | + runId: "run_123", |
| 257 | + tools: [], |
| 258 | + context: [] |
| 259 | +}).subscribe({ |
| 260 | + next: (event) => { |
| 261 | + switch (event.type) { |
| 262 | + case EventType.TEXT_MESSAGE_CONTENT: |
| 263 | + console.log("Content:", event.delta) |
| 264 | + break |
| 265 | + } |
| 266 | + }, |
| 267 | + error: (error) => console.error("Error:", error), |
| 268 | + complete: () => console.log("Run complete") |
| 269 | +}) |
| 270 | +``` |
| 271 | + |
| 272 | +## Source |
| 273 | + |
| 274 | +https://github.com/ag-ui-protocol/ag-ui/blob/main/docs/concepts/agents.mdx |
0 commit comments