|
1 | 1 | # AG-UI Dart SDK |
2 | 2 |
|
3 | | -The official Dart SDK for the AG-UI protocol, standardizing agent-user interactions. |
| 3 | +A strongly-typed Dart implementation of the AG-UI protocol for standardizing agent-user interactions through event-based communication. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +## Features |
| 6 | + |
| 7 | +### ✅ Implemented |
| 8 | +- **Core Protocol Support**: Full implementation of AG-UI event types and message structures |
| 9 | +- **HTTP Client**: Production-ready client with SSE streaming support |
| 10 | +- **Event Streaming**: Real-time event processing with backpressure handling |
| 11 | +- **Tool Interactions**: Support for tool calls and results with generative UI |
| 12 | +- **State Management**: Handle state snapshots and deltas (JSON Patch RFC 6902) |
| 13 | +- **Message History**: Track conversation context across multiple runs |
| 14 | +- **Type Safety**: Strongly-typed models for all protocol entities |
| 15 | +- **Error Handling**: Comprehensive error types with validation |
| 16 | + |
| 17 | +### 🚧 Planned |
| 18 | +- WebSocket transport support |
| 19 | +- Binary protocol encoding/decoding |
| 20 | +- Advanced retry strategies |
| 21 | +- Event caching and offline support |
6 | 22 |
|
7 | | -This package is distributed via GitHub only (not published to pub.dev). To use it in your Dart or Flutter project, add it as a Git dependency in your `pubspec.yaml` file. |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- Dart SDK >=3.3.0 |
| 26 | +- HTTP connectivity to AG-UI compatible servers |
| 27 | + |
| 28 | +## Installation |
8 | 29 |
|
9 | | -### Using the latest version (unpinned) |
| 30 | +This SDK is distributed via GitHub. Add it to your `pubspec.yaml` using Git dependencies: |
10 | 31 |
|
| 32 | +### Option 1: Pin to a specific release tag (recommended for production) |
11 | 33 | ```yaml |
12 | 34 | dependencies: |
13 | 35 | ag_ui: |
14 | 36 | git: |
15 | | - url: https://github.com/ag-ui-protocol/ag-ui.git |
| 37 | + url: https://github.com/mattsp1290/ag-ui.git |
| 38 | + ref: v0.1.0 # Replace with desired version tag |
16 | 39 | path: sdks/community/dart |
17 | 40 | ``` |
18 | 41 |
|
19 | | -### Using a specific tag (recommended for production) |
20 | | -
|
| 42 | +### Option 2: Use a branch (for development) |
21 | 43 | ```yaml |
22 | 44 | dependencies: |
23 | 45 | ag_ui: |
24 | 46 | git: |
25 | | - url: https://github.com/ag-ui-protocol/ag-ui.git |
| 47 | + url: https://github.com/mattsp1290/ag-ui.git |
| 48 | + ref: main # or any branch name |
26 | 49 | path: sdks/community/dart |
27 | | - ref: v0.1.0 # Replace with desired version tag |
28 | 50 | ``` |
29 | 51 |
|
30 | | -### Updating to a newer version |
31 | | -
|
32 | | -To update to the latest version or a newer tag: |
33 | | -
|
34 | | -1. Update the `ref` field in your `pubspec.yaml` to the desired tag (or remove it for latest) |
35 | | -2. Run `dart pub get` or `flutter pub get` to fetch the updated package |
36 | | - |
37 | | -### Available versions |
38 | | - |
39 | | -Check the [GitHub Releases](https://github.com/ag-ui-protocol/ag-ui/releases) page for available version tags. |
40 | | - |
41 | | -For more information about Git dependencies in Dart, see the [official documentation](https://dart.dev/tools/pub/dependencies#git-packages). |
42 | | - |
43 | | -## Quick Start |
| 52 | +Then run: |
| 53 | +```bash |
| 54 | +dart pub get |
| 55 | +``` |
44 | 56 |
|
| 57 | +## Quickstart |
45 | 58 | ### Basic Usage |
46 | 59 |
|
47 | 60 | ```dart |
@@ -636,22 +649,245 @@ Future<void> main() async { |
636 | 649 | } |
637 | 650 | ``` |
638 | 651 |
|
639 | | -## Features |
| 652 | +### Handling Tool Calls |
| 653 | + |
| 654 | +```dart |
| 655 | +import 'package:ag_ui/ag_ui.dart'; |
| 656 | +import 'dart:convert'; |
| 657 | +
|
| 658 | +Future<void> handleToolCalls() async { |
| 659 | + final client = AgUiClient( |
| 660 | + config: AgUiConfig(baseUrl: 'http://localhost:20203'), |
| 661 | + ); |
640 | 662 |
|
641 | | -- Event-driven communication between agents and UIs |
642 | | -- Support for multiple transport protocols (SSE, WebSockets, HTTP) |
643 | | -- Tool-based generative UI capabilities |
644 | | -- Human-in-the-loop interactions |
645 | | -- State management with snapshots and deltas |
| 663 | + final messages = <Message>[]; |
| 664 | + |
| 665 | + // Initial user message |
| 666 | + messages.add(UserMessage( |
| 667 | + id: 'msg_1', |
| 668 | + content: 'What\'s the weather in San Francisco?', |
| 669 | + )); |
| 670 | +
|
| 671 | + final input = RunAgentInput( |
| 672 | + threadId: 'thread_${DateTime.now().millisecondsSinceEpoch}', |
| 673 | + runId: 'run_${DateTime.now().millisecondsSinceEpoch}', |
| 674 | + messages: messages, |
| 675 | + state: {}, |
| 676 | + tools: [], |
| 677 | + context: [], |
| 678 | + forwardedProps: {}, |
| 679 | + ); |
| 680 | +
|
| 681 | + await for (final event in client.streamEvents('/agent', input)) { |
| 682 | + if (event is MessagesSnapshotEvent) { |
| 683 | + // Check for tool calls in assistant messages |
| 684 | + for (final message in event.messages) { |
| 685 | + if (message is AssistantMessage && message.toolCalls != null) { |
| 686 | + for (final toolCall in message.toolCalls!) { |
| 687 | + print('Tool called: ${toolCall.function.name}'); |
| 688 | + print('Arguments: ${toolCall.function.arguments}'); |
| 689 | + |
| 690 | + // Provide tool result |
| 691 | + final toolResult = ToolMessage( |
| 692 | + id: 'tool_msg_${DateTime.now().millisecondsSinceEpoch}', |
| 693 | + content: json.encode({'temperature': 72, 'condition': 'sunny'}), |
| 694 | + toolCallId: toolCall.id, |
| 695 | + ); |
| 696 | + |
| 697 | + messages.add(toolResult); |
| 698 | + |
| 699 | + // Continue conversation with tool result |
| 700 | + final continuedInput = RunAgentInput( |
| 701 | + threadId: input.threadId, |
| 702 | + runId: 'run_${DateTime.now().millisecondsSinceEpoch}', |
| 703 | + messages: messages, |
| 704 | + state: {}, |
| 705 | + tools: [], |
| 706 | + context: [], |
| 707 | + forwardedProps: {}, |
| 708 | + ); |
| 709 | + |
| 710 | + // Stream the continuation |
| 711 | + await for (final nextEvent in client.streamEvents('/agent', continuedInput)) { |
| 712 | + // Handle continuation events... |
| 713 | + if (nextEvent is RunFinishedEvent) break; |
| 714 | + } |
| 715 | + } |
| 716 | + } |
| 717 | + } |
| 718 | + } |
| 719 | + } |
| 720 | +} |
| 721 | +``` |
| 722 | + |
| 723 | +### Environment Variables |
646 | 724 |
|
647 | | -## Documentation |
| 725 | +Configure the client using environment variables: |
648 | 726 |
|
649 | | -For full documentation, visit [https://docs.ag-ui.com](https://docs.ag-ui.com) |
| 727 | +```bash |
| 728 | +export AGUI_BASE_URL=http://your-server:20203 |
| 729 | +export AGUI_API_KEY=your-api-key-here |
650 | 730 |
|
651 | | -## Example |
| 731 | +dart run your_app.dart |
| 732 | +``` |
652 | 733 |
|
653 | | -See the [example](example/) directory for a complete demonstration of AG-UI features. |
| 734 | +```dart |
| 735 | +// Client will automatically use environment variables |
| 736 | +final client = AgUiClient.fromEnvironment(); |
| 737 | +``` |
| 738 | + |
| 739 | +## API Overview |
| 740 | + |
| 741 | +### Core Types |
| 742 | + |
| 743 | +#### `AgUiClient` |
| 744 | +Main client for interacting with AG-UI servers: |
| 745 | +- `streamEvents(endpoint, input)` - Stream events from an endpoint |
| 746 | +- `sendRequest(endpoint, input)` - Send one-shot requests |
| 747 | +- Configuration via `AgUiConfig` |
| 748 | + |
| 749 | +#### `RunAgentInput` |
| 750 | +Input structure for agent runs: |
| 751 | +```dart |
| 752 | +RunAgentInput( |
| 753 | + threadId: String, // Conversation thread ID |
| 754 | + runId: String, // Unique run identifier |
| 755 | + messages: List<Message>, // Conversation history |
| 756 | + state: Map<String, dynamic>, // Application state |
| 757 | + tools: List<Tool>, // Available tools |
| 758 | + context: List<dynamic>, // Additional context |
| 759 | + forwardedProps: Map<String, dynamic>, // Pass-through properties |
| 760 | +) |
| 761 | +``` |
| 762 | + |
| 763 | +#### Message Types |
| 764 | +- `UserMessage` - User input messages |
| 765 | +- `AssistantMessage` - Agent responses with optional tool calls |
| 766 | +- `SystemMessage` - System-level messages |
| 767 | +- `ToolMessage` - Tool execution results |
| 768 | + |
| 769 | +#### Event Types |
| 770 | +- Lifecycle: `RUN_STARTED`, `RUN_FINISHED` |
| 771 | +- Messages: `TEXT_MESSAGE_STARTED`, `TEXT_MESSAGE_CHUNK`, `TEXT_MESSAGE_FINISHED` |
| 772 | +- State: `STATE_SNAPSHOT`, `STATE_DELTA` |
| 773 | +- Tools: `TOOL_CALL_STARTED`, `TOOL_CALL_FINISHED`, `TOOL_RESULT` |
| 774 | +- UI: `GENERATIVE_UI_ELEMENT_*` |
| 775 | + |
| 776 | +### Error Handling |
| 777 | + |
| 778 | +```dart |
| 779 | +try { |
| 780 | + await for (final event in client.streamEvents('/agent', input)) { |
| 781 | + // Process events |
| 782 | + } |
| 783 | +} on ValidationError catch (e) { |
| 784 | + print('Invalid input: ${e.message}'); |
| 785 | +} on NetworkError catch (e) { |
| 786 | + print('Network issue: ${e.message}'); |
| 787 | +} on ServerError catch (e) { |
| 788 | + print('Server error: ${e.statusCode} - ${e.message}'); |
| 789 | +} catch (e) { |
| 790 | + print('Unexpected error: $e'); |
| 791 | +} |
| 792 | +``` |
| 793 | + |
| 794 | +## Example Application |
| 795 | + |
| 796 | +A complete example application is available at [`sdks/community/dart/example/`](example/) demonstrating: |
| 797 | + |
| 798 | +- Interactive CLI for testing AG-UI servers |
| 799 | +- Tool-based generative UI flows |
| 800 | +- Message streaming and event handling |
| 801 | +- Automatic tool response generation |
| 802 | + |
| 803 | +Run the example: |
| 804 | +```bash |
| 805 | +cd sdks/community/dart/example |
| 806 | +dart pub get |
| 807 | +dart run ag_ui_example --help |
| 808 | +``` |
| 809 | + |
| 810 | +## Integration Testing |
| 811 | + |
| 812 | +The SDK includes comprehensive integration tests that validate compatibility with AG-UI servers. To run tests locally: |
| 813 | + |
| 814 | +```bash |
| 815 | +cd sdks/community/dart |
| 816 | + |
| 817 | +# Run unit tests |
| 818 | +dart test |
| 819 | + |
| 820 | +# Run integration tests (requires local server) |
| 821 | +cd test/integration |
| 822 | +./helpers/start_server.sh # Start test server |
| 823 | +dart test simple_qa_test.dart |
| 824 | +./helpers/stop_server.sh # Stop test server |
| 825 | +``` |
| 826 | + |
| 827 | +For Docker-based testing: |
| 828 | +```bash |
| 829 | +dart test simple_qa_docker_test.dart # Automatically manages container |
| 830 | +``` |
| 831 | + |
| 832 | +## Troubleshooting |
| 833 | + |
| 834 | +### Connection Timeouts |
| 835 | +Adjust timeout in client configuration: |
| 836 | +```dart |
| 837 | +final client = AgUiClient( |
| 838 | + config: AgUiConfig( |
| 839 | + baseUrl: 'http://localhost:20203', |
| 840 | + defaultTimeout: Duration(seconds: 60), // Increase timeout |
| 841 | + ), |
| 842 | +); |
| 843 | +``` |
| 844 | + |
| 845 | +### SSL/TLS Issues |
| 846 | +For self-signed certificates in development: |
| 847 | +```dart |
| 848 | +final client = AgUiClient( |
| 849 | + config: AgUiConfig( |
| 850 | + baseUrl: 'https://localhost:20203', |
| 851 | + validateCertificates: false, // Development only! |
| 852 | + ), |
| 853 | +); |
| 854 | +``` |
| 855 | + |
| 856 | +### Debug Logging |
| 857 | +Enable debug output: |
| 858 | +```bash |
| 859 | +export DEBUG=true |
| 860 | +dart run your_app.dart |
| 861 | +``` |
| 862 | + |
| 863 | +## Contributing |
| 864 | + |
| 865 | +Contributions are welcome! Please: |
| 866 | +1. Fork the repository |
| 867 | +2. Create a feature branch |
| 868 | +3. Add tests for new functionality |
| 869 | +4. Ensure all tests pass |
| 870 | +5. Submit a pull request |
| 871 | + |
| 872 | +## Resources |
| 873 | + |
| 874 | +- [AG-UI Protocol Specification](https://github.com/mattsp1290/ag-ui/blob/main/docs/specification.md) |
| 875 | +- [AG-UI Documentation](https://docs.ag-ui.com) |
| 876 | +- [TypeScript SDK Reference](../../typescript-sdk/) |
| 877 | +- [Python SDK Reference](../../python-sdk/) |
| 878 | +- [AG-UI Dojo](../../typescript-sdk/apps/dojo/) - Interactive protocol demonstration |
654 | 879 |
|
655 | 880 | ## License |
656 | 881 |
|
657 | | -See the main repository for license information. |
| 882 | +This SDK is part of the AG-UI Protocol project. See the [main repository](https://github.com/mattsp1290/ag-ui) for license information. |
| 883 | + |
| 884 | +## Versioning |
| 885 | + |
| 886 | +This SDK follows semantic versioning. Version history will be tracked in future releases. |
| 887 | + |
| 888 | +## Support |
| 889 | + |
| 890 | +For issues, questions, or feature requests: |
| 891 | +- Open an issue on [GitHub](https://github.com/mattsp1290/ag-ui/issues) |
| 892 | +- Check existing [discussions](https://github.com/mattsp1290/ag-ui/discussions) |
| 893 | +- Review the [protocol specification](https://github.com/mattsp1290/ag-ui/blob/main/docs/specification.md) for protocol details |
0 commit comments