|
| 1 | +--- |
| 2 | +title: AgUiAgent |
| 3 | +description: Stateless client for AG-UI protocol interactions |
| 4 | +--- |
| 5 | + |
| 6 | +# AgUiAgent |
| 7 | + |
| 8 | +`AgUiAgent` is a stateless client implementation designed for cases where no ongoing context is needed or where the agent manages all state server-side. It provides a simple, efficient interface for interacting with AG-UI protocol agents. |
| 9 | + |
| 10 | +## Usage Scenarios |
| 11 | + |
| 12 | +### No Ongoing Context |
| 13 | +Perfect for single interactions or independent queries: |
| 14 | +```kotlin |
| 15 | +val agent = AgUiAgent("https://api.example.com/agent") { |
| 16 | + bearerToken = "your-token" |
| 17 | +} |
| 18 | + |
| 19 | +// Each interaction is independent |
| 20 | +agent.sendMessage("What's the weather?").collect { state -> |
| 21 | + println(state.messages.last().content) |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### Agent-Managed State |
| 26 | +Ideal when the agent handles conversation history server-side: |
| 27 | +```kotlin |
| 28 | +val agent = AgUiAgent("https://api.example.com/agent") { |
| 29 | + bearerToken = "your-token" |
| 30 | + // Agent manages conversation context internally |
| 31 | +} |
| 32 | + |
| 33 | +// Agent remembers previous interactions server-side |
| 34 | +agent.sendMessage("My name is Bob").collect { } |
| 35 | +agent.sendMessage("What's my name?").collect { state -> |
| 36 | + // Agent retrieves context from server-side storage |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Configuration |
| 41 | + |
| 42 | +### Convenience Builders |
| 43 | +The easiest way to create AgUiAgent instances: |
| 44 | + |
| 45 | +```kotlin |
| 46 | +import com.agui.client.builders.* |
| 47 | + |
| 48 | +// Quick bearer token setup |
| 49 | +val agent = agentWithBearer("https://api.example.com/agent", "your-token") |
| 50 | + |
| 51 | +// Quick API key setup |
| 52 | +val agent = agentWithApiKey("https://api.example.com/agent", "your-api-key") |
| 53 | + |
| 54 | +// Custom API key header |
| 55 | +val agent = agentWithApiKey("https://api.example.com/agent", "your-key", "Authorization") |
| 56 | + |
| 57 | +// Agent with tools |
| 58 | +val agent = agentWithTools( |
| 59 | + url = "https://api.example.com/agent", |
| 60 | + toolRegistry = toolRegistry { |
| 61 | + addTool(CalculatorToolExecutor()) |
| 62 | + } |
| 63 | +) { |
| 64 | + bearerToken = "your-token" |
| 65 | +} |
| 66 | + |
| 67 | +// Debug agent with logging |
| 68 | +val agent = debugAgent("https://api.example.com/agent") { |
| 69 | + bearerToken = "your-token" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Basic Setup |
| 74 | +```kotlin |
| 75 | +val agent = AgUiAgent( |
| 76 | + url = "https://api.example.com/agent" |
| 77 | +) { |
| 78 | + // Authentication (choose one) |
| 79 | + bearerToken = "your-bearer-token" |
| 80 | + // OR |
| 81 | + apiKey = "your-api-key" |
| 82 | + // OR |
| 83 | + basicAuth("username", "password") |
| 84 | + |
| 85 | + // Optional system prompt |
| 86 | + systemPrompt = "You are a helpful assistant" |
| 87 | + |
| 88 | + // Optional user ID |
| 89 | + userId = "user-123" |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Advanced Configuration |
| 94 | +```kotlin |
| 95 | +val agent = AgUiAgent("https://api.example.com/agent") { |
| 96 | + bearerToken = "your-token" |
| 97 | + |
| 98 | + // Custom headers |
| 99 | + customHeaders = mapOf( |
| 100 | + "X-App-Version" to "1.0.0", |
| 101 | + "X-Client-Type" to "mobile" |
| 102 | + ) |
| 103 | + |
| 104 | + // Timeout settings |
| 105 | + requestTimeout = 30.seconds |
| 106 | + connectTimeout = 10.seconds |
| 107 | + |
| 108 | + // Debug logging |
| 109 | + enableLogging = true |
| 110 | + |
| 111 | + // Custom user agent |
| 112 | + userAgent = "MyApp/1.0" |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Methods |
| 117 | + |
| 118 | +### sendMessage |
| 119 | +Send a message and receive streaming responses: |
| 120 | + |
| 121 | +```kotlin |
| 122 | +fun sendMessage( |
| 123 | + message: String, |
| 124 | + threadId: String = generateThreadId(), |
| 125 | + state: JsonElement? = null, |
| 126 | + includeSystemPrompt: Boolean = true |
| 127 | +): Flow<BaseEvent> |
| 128 | +``` |
| 129 | + |
| 130 | +**Parameters:** |
| 131 | +- `message`: The message content to send |
| 132 | +- `threadId`: Thread ID for conversation (defaults to generated ID) |
| 133 | +- `state`: Initial state for the agent (defaults to null) |
| 134 | +- `includeSystemPrompt`: Whether to include the configured system prompt |
| 135 | + |
| 136 | +**Returns:** `Flow<BaseEvent>` - Stream of protocol events |
| 137 | + |
| 138 | +**Example:** |
| 139 | +```kotlin |
| 140 | +agent.sendMessage("Calculate 15% tip on $50").collect { event -> |
| 141 | + when (event) { |
| 142 | + is TextMessageStartEvent -> println("Agent started responding") |
| 143 | + is TextMessageContentEvent -> print(event.delta) |
| 144 | + is TextMessageEndEvent -> println("\nAgent finished responding") |
| 145 | + is ToolCallStartEvent -> println("Agent is using tool: ${event.toolCallName}") |
| 146 | + is RunErrorEvent -> println("Error: ${event.message}") |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### close |
| 152 | +Close the agent and release resources: |
| 153 | + |
| 154 | +```kotlin |
| 155 | +fun close() |
| 156 | +``` |
| 157 | + |
| 158 | +**Example:** |
| 159 | +```kotlin |
| 160 | +class MyRepository { |
| 161 | + private val agent = AgUiAgent("https://api.example.com/agent") { |
| 162 | + bearerToken = "your-token" |
| 163 | + } |
| 164 | + |
| 165 | + fun cleanup() { |
| 166 | + agent.close() |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +## Error Handling |
| 171 | + |
| 172 | +### Connection Errors |
| 173 | +```kotlin |
| 174 | +agent.sendMessage("Hello").collect { state -> |
| 175 | + state.errors.forEach { error -> |
| 176 | + when (error.type) { |
| 177 | + ErrorType.NETWORK -> { |
| 178 | + println("Network error: ${error.message}") |
| 179 | + // Handle network issues |
| 180 | + } |
| 181 | + ErrorType.AUTHENTICATION -> { |
| 182 | + println("Auth error: ${error.message}") |
| 183 | + // Handle authentication issues |
| 184 | + } |
| 185 | + ErrorType.PROTOCOL -> { |
| 186 | + println("Protocol error: ${error.message}") |
| 187 | + // Handle protocol violations |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### Retry Logic |
| 195 | +```kotlin |
| 196 | +// Built-in retry for transient failures |
| 197 | +val agent = AgUiAgent("https://api.example.com/agent") { |
| 198 | + bearerToken = "your-token" |
| 199 | + maxRetries = 3 |
| 200 | + retryDelay = 1.seconds |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +## Thread Safety |
| 205 | + |
| 206 | +`AgUiAgent` is thread-safe and can be used concurrently: |
| 207 | + |
| 208 | +```kotlin |
| 209 | +val agent = AgUiAgent("https://api.example.com/agent") { |
| 210 | + bearerToken = "your-token" |
| 211 | +} |
| 212 | + |
| 213 | +// Safe to call from multiple coroutines |
| 214 | +launch { |
| 215 | + agent.sendMessage("First message").collect { } |
| 216 | +} |
| 217 | + |
| 218 | +launch { |
| 219 | + agent.sendMessage("Second message").collect { } |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +## Best Practices |
| 224 | + |
| 225 | +### Resource Management |
| 226 | +```kotlin |
| 227 | +// Agent automatically manages HTTP connections |
| 228 | +// No explicit cleanup required, but you can control lifecycle: |
| 229 | + |
| 230 | +class MyRepository { |
| 231 | + private val agent = AgUiAgent(url) { /* config */ } |
| 232 | + |
| 233 | + // Agent will be garbage collected when repository is |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +### Performance Optimization |
| 238 | +```kotlin |
| 239 | +// Reuse agent instances when possible |
| 240 | +val agent = AgUiAgent(url) { bearerToken = token } |
| 241 | + |
| 242 | +// Multiple interactions with same agent instance |
| 243 | +repeat(10) { i -> |
| 244 | + agent.sendMessage("Message $i").collect { } |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +### Message Threading |
| 249 | +```kotlin |
| 250 | +// Group related messages with threadId |
| 251 | +val threadId = UUID.randomUUID().toString() |
| 252 | + |
| 253 | +agent.sendMessage("Start conversation", threadId = threadId).collect { } |
| 254 | +agent.sendMessage("Continue conversation", threadId = threadId).collect { } |
| 255 | +``` |
| 256 | + |
| 257 | +## Platform Considerations |
| 258 | + |
| 259 | +### Android |
| 260 | +- Uses Ktor Android engine (OkHttp under the hood) |
| 261 | +- Handles network state changes automatically |
| 262 | +- Compatible with background processing restrictions |
| 263 | + |
| 264 | +### iOS |
| 265 | +- Uses Ktor Darwin engine (NSURLSession under the hood) |
| 266 | +- Respects iOS app lifecycle events |
| 267 | +- Compatible with background app refresh |
| 268 | + |
| 269 | +### JVM |
| 270 | +- Uses Ktor CIO engine for server applications |
| 271 | +- Supports high-concurrency scenarios |
| 272 | +- Compatible with Spring Boot and other frameworks |
0 commit comments