Skip to content

Commit 2779b17

Browse files
mattsp1290claude
andcommitted
docs(dart): add comprehensive Dart SDK documentation
Add complete documentation for the AG-UI Dart SDK including: - Main SDK overview with quick start guide - Client package documentation (AgUiClient, configuration) - Core types documentation (RunAgentInput, Messages, Context, Tools, State) - Events system documentation (lifecycle, text message, tool call, state events) - Encoder/decoder documentation (binary protocol, compression, streaming) Documentation follows the same structure and style as TypeScript and Python SDKs for consistency across all language implementations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 64c142b commit 2779b17

File tree

7 files changed

+2741
-0
lines changed

7 files changed

+2741
-0
lines changed

docs/sdk/dart/client/client.mdx

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
---
2+
title: "AgUiClient"
3+
description: "Main client class for AG-UI server connectivity"
4+
---
5+
6+
# AgUiClient
7+
8+
The `AgUiClient` class is the primary interface for connecting to AG-UI compatible servers. It handles HTTP communication, SSE streaming, binary protocol encoding/decoding, and provides a type-safe API for agent interactions.
9+
10+
## Constructor
11+
12+
```dart
13+
AgUiClient({
14+
required AgUiClientConfig config,
15+
http.Client? httpClient,
16+
Encoder? encoder,
17+
Decoder? decoder,
18+
})
19+
```
20+
21+
### Parameters
22+
23+
- `config` (required): Configuration object with server details
24+
- `httpClient` (optional): Custom HTTP client implementation
25+
- `encoder` (optional): Custom encoder for request serialization
26+
- `decoder` (optional): Custom decoder for response parsing
27+
28+
## Methods
29+
30+
### runAgent
31+
32+
Executes an agent and returns a stream of decoded events.
33+
34+
```dart
35+
Stream<BaseEvent> runAgent(
36+
String agentId,
37+
RunAgentInput input, {
38+
Map<String, String>? headers,
39+
})
40+
```
41+
42+
#### Parameters
43+
44+
- `agentId`: Unique identifier for the agent
45+
- `input`: Agent input containing messages, context, and configuration
46+
- `headers`: Optional additional headers for this request
47+
48+
#### Returns
49+
50+
A `Stream<BaseEvent>` that emits protocol events as they arrive.
51+
52+
#### Example
53+
54+
```dart
55+
final input = SimpleRunAgentInput(
56+
messages: [
57+
UserMessage(id: 'msg_1', content: 'Hello, agent!'),
58+
],
59+
context: {'sessionId': '12345'},
60+
);
61+
62+
await for (final event in client.runAgent('chat-agent', input)) {
63+
switch (event) {
64+
case RunStartedEvent():
65+
print('Agent started');
66+
case TextMessageDeltaEvent(delta: final text):
67+
print('Agent says: $text');
68+
case RunFinishedEvent():
69+
print('Agent finished');
70+
}
71+
}
72+
```
73+
74+
### runAgentRaw
75+
76+
Executes an agent and returns raw SSE messages without decoding.
77+
78+
```dart
79+
Stream<SseMessage> runAgentRaw(
80+
String agentId,
81+
RunAgentInput input, {
82+
Map<String, String>? headers,
83+
})
84+
```
85+
86+
#### Use Cases
87+
88+
- Custom event processing
89+
- Debugging and logging
90+
- Performance optimization when decoding isn't needed
91+
92+
#### Example
93+
94+
```dart
95+
await for (final message in client.runAgentRaw('agent', input)) {
96+
print('Raw event: ${message.event}');
97+
print('Raw data: ${message.data}');
98+
}
99+
```
100+
101+
### cancelAgent
102+
103+
Cancels an active agent execution.
104+
105+
```dart
106+
Future<void> cancelAgent(String agentId)
107+
```
108+
109+
#### Parameters
110+
111+
- `agentId`: The agent ID to cancel
112+
113+
#### Behavior
114+
115+
- Immediately closes the SSE connection
116+
- Cleans up resources
117+
- Causes the event stream to complete
118+
119+
#### Example
120+
121+
```dart
122+
// Start long-running agent
123+
final stream = client.runAgent('analysis-agent', input);
124+
125+
// Set up listener with timeout
126+
final subscription = stream.listen(
127+
(event) => processEvent(event),
128+
onError: (error) => handleError(error),
129+
);
130+
131+
// Cancel after 10 seconds
132+
Timer(Duration(seconds: 10), () async {
133+
await client.cancelAgent('analysis-agent');
134+
await subscription.cancel();
135+
});
136+
```
137+
138+
### dispose
139+
140+
Cleans up all resources held by the client.
141+
142+
```dart
143+
void dispose()
144+
```
145+
146+
#### Important
147+
148+
- Call this when the client is no longer needed
149+
- Cancels all active streams
150+
- Closes HTTP client connections
151+
- Releases memory resources
152+
153+
## Properties
154+
155+
### config
156+
157+
```dart
158+
final AgUiClientConfig config;
159+
```
160+
161+
The configuration used to initialize the client. Read-only.
162+
163+
### activeStreams
164+
165+
```dart
166+
Map<String, bool> get activeStreams;
167+
```
168+
169+
Returns a map of currently active agent IDs and their status.
170+
171+
## Error Handling
172+
173+
The client throws specific exceptions for different error scenarios:
174+
175+
### AgUiClientError
176+
177+
General client-side errors.
178+
179+
```dart
180+
try {
181+
await for (final event in client.runAgent('agent', input)) {
182+
// Process events
183+
}
184+
} on AgUiClientError catch (e) {
185+
print('Client error: ${e.message}');
186+
print('Error code: ${e.code}');
187+
}
188+
```
189+
190+
### NetworkError
191+
192+
Network connectivity issues.
193+
194+
```dart
195+
on NetworkError catch (e) {
196+
print('Network error: ${e.message}');
197+
// Implement retry logic
198+
}
199+
```
200+
201+
### ValidationError
202+
203+
Input validation failures.
204+
205+
```dart
206+
on ValidationError catch (e) {
207+
print('Validation failed: ${e.message}');
208+
print('Failed fields: ${e.fields}');
209+
}
210+
```
211+
212+
### ServerError
213+
214+
Server-side errors (5xx status codes).
215+
216+
```dart
217+
on ServerError catch (e) {
218+
print('Server error: ${e.statusCode}');
219+
print('Message: ${e.message}');
220+
}
221+
```
222+
223+
## Advanced Usage
224+
225+
### Custom HTTP Client
226+
227+
Provide a custom HTTP client for advanced scenarios:
228+
229+
```dart
230+
import 'package:http/http.dart' as http;
231+
import 'package:http/retry.dart';
232+
233+
final retryClient = RetryClient(http.Client());
234+
235+
final client = AgUiClient(
236+
config: AgUiClientConfig(baseUrl: 'http://localhost:8000'),
237+
httpClient: retryClient,
238+
);
239+
```
240+
241+
### Custom Encoding/Decoding
242+
243+
Implement custom encoders/decoders for specialized formats:
244+
245+
```dart
246+
class CustomEncoder implements Encoder {
247+
@override
248+
List<int> encode(RunAgentInput input) {
249+
// Custom encoding logic
250+
return utf8.encode(jsonEncode(input.toJson()));
251+
}
252+
}
253+
254+
class CustomDecoder implements Decoder {
255+
@override
256+
BaseEvent decode(List<int> data) {
257+
// Custom decoding logic
258+
final json = jsonDecode(utf8.decode(data));
259+
return BaseEvent.fromJson(json);
260+
}
261+
}
262+
263+
final client = AgUiClient(
264+
config: config,
265+
encoder: CustomEncoder(),
266+
decoder: CustomDecoder(),
267+
);
268+
```
269+
270+
### Stream Transformations
271+
272+
Transform the event stream for specific use cases:
273+
274+
```dart
275+
// Filter only message events
276+
final messageStream = client
277+
.runAgent('agent', input)
278+
.where((event) => event is TextMessageEvent);
279+
280+
// Collect all text into a single string
281+
final completeText = await client
282+
.runAgent('agent', input)
283+
.whereType<TextMessageDeltaEvent>()
284+
.map((event) => event.delta)
285+
.join();
286+
```
287+
288+
### Concurrent Agents
289+
290+
Run multiple agents concurrently:
291+
292+
```dart
293+
final agent1 = client.runAgent('agent1', input1);
294+
final agent2 = client.runAgent('agent2', input2);
295+
296+
// Process both streams
297+
await Future.wait([
298+
agent1.forEach((event) => processAgent1(event)),
299+
agent2.forEach((event) => processAgent2(event)),
300+
]);
301+
```
302+
303+
## Performance Considerations
304+
305+
### Connection Pooling
306+
307+
The client reuses HTTP connections when possible. For high-throughput scenarios:
308+
309+
```dart
310+
final httpClient = http.Client();
311+
// Configure connection pooling
312+
final client = AgUiClient(
313+
config: config,
314+
httpClient: httpClient,
315+
);
316+
```
317+
318+
### Memory Management
319+
320+
For long-running streams:
321+
322+
1. Process events immediately rather than buffering
323+
2. Cancel streams when no longer needed
324+
3. Dispose of the client when done
325+
326+
### Binary Protocol
327+
328+
The binary protocol is more efficient than JSON for large payloads:
329+
330+
```dart
331+
// Binary protocol is used automatically when supported
332+
final stream = client.runAgent('agent', input);
333+
```
334+
335+
## Testing
336+
337+
Mock the client for unit tests:
338+
339+
```dart
340+
class MockAgUiClient implements AgUiClient {
341+
@override
342+
Stream<BaseEvent> runAgent(String agentId, RunAgentInput input) {
343+
return Stream.fromIterable([
344+
RunStartedEvent(runId: 'test-run'),
345+
TextMessageStartedEvent(messageId: 'msg-1'),
346+
TextMessageDeltaEvent(messageId: 'msg-1', delta: 'Hello'),
347+
TextMessageFinishedEvent(messageId: 'msg-1'),
348+
RunFinishedEvent(runId: 'test-run'),
349+
]);
350+
}
351+
}
352+
```

0 commit comments

Comments
 (0)