|
1 | | -# ag-ui-4k - AG-UI Protocol Client for Kotlin Multiplatform |
2 | | - |
3 | | -A Kotlin Multiplatform (KMP) client library for connecting to AI agents that implement the Agent User Interaction Protocol (AG-UI). |
| 1 | +AG-UI-4K_README.md |
| 2 | +# AG-UI-4K - Agent User Interaction Protocol Client for Kotlin |
4 | 3 |
|
5 | 4 | [](https://opensource.org/licenses/MIT) |
6 | 5 | [](http://kotlinlang.org) |
| 6 | +[](https://kotlinlang.org/docs/multiplatform.html) |
| 7 | +[](https://android-arsenal.com/api?level=26) |
7 | 8 |
|
8 | | -## Overview |
| 9 | +A production-ready Kotlin Multiplatform client library for connecting applications to AI agents that implement the [Agent User Interaction Protocol (AG-UI)](https://docs.ag-ui.com/). |
9 | 10 |
|
10 | | -ag-ui-4k is a multiplatform **client library** that enables applications to connect to and communicate with AI agents using the AG-UI protocol. This library provides a type-safe, coroutine-based API for real-time, event-driven interactions with AI services. |
| 11 | +## 🎯 Why AG-UI-4K? |
11 | 12 |
|
12 | | -**Important**: ag-ui-4k is a client-side library for connecting TO agents, not for implementing agents themselves. See [OVERVIEW.md](OVERVIEW.md) for more details. |
| 13 | +AG-UI-4K enables seamless integration with AI agents across all major platforms through a unified, type-safe API. |
13 | 14 |
|
14 | | -Built with the latest Kotlin technology stack including the K2 compiler, Ktor 3, and kotlinx.serialization 1.8, ag-ui-4k delivers exceptional performance and developer experience across all supported platforms. |
| 15 | +- **True Multiplatform**: Write once, run everywhere with platform-specific optimizations |
| 16 | +- **Modern Kotlin**: Leveraging coroutines, flows, and the latest K2 compiler for exceptional performance |
15 | 17 |
|
16 | | -### Features |
| 18 | +## ✨ Features |
17 | 19 |
|
18 | | -- 🚀 **Multiplatform Support**: Connect to AG-UI agents from Android, iOS, JVM, and more |
19 | | -- 🔄 **Real-time Streaming**: Event-driven architecture with Kotlin Flows for receiving agent responses |
20 | | -- 🛡️ **Type Safety**: Fully typed events and messages from the AG-UI protocol |
21 | | -- ⚡ **Coroutine-based**: Built on Kotlin Coroutines for efficient async operations |
22 | | -- 🔧 **Extensible**: Easy to create custom client implementations |
23 | | -- 📦 **Lightweight**: Minimal dependencies, powered by Ktor |
24 | | -- 🎯 **K2 Compiler**: Optimized with Kotlin 2.1.21's K2 compiler for better performance |
25 | | -- 🆕 **Latest Stack**: Built with Ktor 3.1.3 and kotlinx.serialization 1.8.1 |
| 20 | +### Core Capabilities |
| 21 | +- 🔄 **Real-time Streaming**: Event-driven architecture with Kotlin Flows for live agent responses |
| 22 | +- 🛡️ **Type Safety**: Fully typed protocol implementation with compile-time guarantees |
| 23 | +- ⚡ **High Performance**: Optimized with K2 compiler and efficient state management |
| 24 | +- 🔧 **Extensible Tools**: Comprehensive framework for extending agent capabilities |
| 25 | +- 📊 **State Management**: Automatic synchronization with JSON Patch support |
| 26 | +- 🔒 **Secure**: Built-in authentication options (Bearer, API Key, Basic Auth) |
26 | 27 |
|
27 | | -## Requirements |
| 28 | +### Technical Excellence |
| 29 | +- 📦 **Modular Architecture**: Clean separation between core, client, tools, and SDK layers |
| 30 | +- 🎯 **Latest Tech Stack**: Kotlin 2.1.21, Ktor 3.x, kotlinx.serialization 1.8.x |
| 31 | +- 🧪 **Well Tested**: Unit tests and Android-specific platform tests |
| 32 | +- 📝 **Fully Documented**: Extensive KDocs, example Chat App |
28 | 33 |
|
29 | | -- Kotlin 2.1.21 or higher |
30 | | -- Java 11 or higher |
31 | | -- Gradle 8.5 or higher |
32 | | -- Android API 21+ (for Android targets) |
33 | | -- iOS 13+ (for iOS targets) |
| 34 | +## 🚀 Quick Start |
34 | 35 |
|
35 | | -## Installation |
| 36 | +### Installation |
36 | 37 |
|
37 | | -### Gradle (Kotlin DSL) |
| 38 | +Add the SDK to your project: |
38 | 39 |
|
39 | 40 | ```kotlin |
| 41 | +// For high-level agent interactions |
40 | 42 | dependencies { |
41 | | - implementation("com.contextable:ag-ui-4k:0.1.0") |
| 43 | + implementation("com.contextable:agui4k-agent-sdk:0.2.1") |
42 | 44 | } |
43 | | -``` |
44 | | - |
45 | | -### Maven |
46 | 45 |
|
47 | | -```xml |
48 | | -<dependency> |
49 | | - <groupId>com.contextable</groupId> |
50 | | - <artifactId>ag-ui-4k</artifactId> |
51 | | - <version>0.1.0</version> |
52 | | -</dependency> |
| 46 | +// For direct protocol access (advanced users) |
| 47 | +dependencies { |
| 48 | + implementation("com.contextable:agui4k-client:0.2.1") |
| 49 | + implementation("com.contextable:agui4k-core:0.2.1") |
| 50 | +} |
53 | 51 | ``` |
54 | 52 |
|
55 | | -## Quick Start |
| 53 | +### Basic Usage |
56 | 54 |
|
57 | 55 | ```kotlin |
58 | | -import com.contextable.agui4k.client.HttpAgent |
59 | | -import com.contextable.agui4k.core.types.* |
| 56 | +import com.contextable.agui4k.sdk.* |
60 | 57 | import kotlinx.coroutines.flow.collect |
61 | | -import kotlinx.coroutines.launch |
62 | | - |
63 | | -// Create an HTTP client to connect to an AG-UI agent |
64 | | -val client = HttpAgent { |
65 | | - url = "https://your-agent-endpoint.com/agent" |
66 | | - headers = mapOf( |
67 | | - "Authorization" to "Bearer your-api-key" |
68 | | - ) |
| 58 | + |
| 59 | +// Create a stateless agent |
| 60 | +val agent = AgUi4KAgent("https://your-agent-api.com/agent") { |
| 61 | + bearerToken = "your-api-token" |
| 62 | + systemPrompt = "You are a helpful AI assistant" |
69 | 63 | } |
70 | 64 |
|
71 | | -// Send a message to the agent |
72 | | -client.addMessage( |
73 | | - UserMessage( |
74 | | - id = "1", |
75 | | - content = "Hello, how can you help me today?" |
76 | | - ) |
77 | | -) |
78 | | - |
79 | | -// Connect to the agent and handle responses |
80 | | -scope.launch { |
81 | | - client.runAgent { |
82 | | - tools = listOf( |
83 | | - Tool( |
84 | | - name = "confirmAction", |
85 | | - description = "Ask user to confirm an action", |
86 | | - parameters = mapOf( |
87 | | - "type" to "object", |
88 | | - "properties" to mapOf( |
89 | | - "action" to mapOf( |
90 | | - "type" to "string", |
91 | | - "description" to "The action to confirm" |
92 | | - ) |
93 | | - ), |
94 | | - "required" to listOf("action") |
95 | | - ) |
96 | | - ) |
97 | | - ) |
98 | | - }.collect { event -> |
99 | | - when (event) { |
100 | | - is TextMessageContentEvent -> { |
101 | | - // Handle streaming text from the agent |
102 | | - println("Agent: ${event.delta}") |
103 | | - } |
104 | | - is ToolCallStartEvent -> { |
105 | | - // Agent is requesting to use a tool |
106 | | - println("Tool call: ${event.toolCallName}") |
107 | | - } |
108 | | - is StateSnapshotEvent -> { |
109 | | - // Agent state has been updated |
110 | | - println("State updated: ${event.snapshot}") |
111 | | - } |
112 | | - // Handle other events... |
113 | | - } |
| 65 | +// Send a message and receive streaming responses |
| 66 | +agent.sendMessage("What's the weather like?").collect { state -> |
| 67 | + println("State updated: $state") |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Conversational Agent |
| 72 | + |
| 73 | +```kotlin |
| 74 | +// Create a stateful agent that maintains conversation history |
| 75 | +val chatAgent = StatefulAgUi4KAgent("https://your-agent-api.com/agent") { |
| 76 | + bearerToken = "your-api-token" |
| 77 | + systemPrompt = "You are a friendly conversational AI" |
| 78 | + initialState = buildJsonObject { |
| 79 | + put("userName", "Alice") |
| 80 | + put("preferences", buildJsonObject { |
| 81 | + put("language", "en") |
| 82 | + }) |
114 | 83 | } |
115 | 84 | } |
| 85 | + |
| 86 | +// Have a conversation |
| 87 | +chatAgent.chat("Hello!").collect { /* ... */ } |
| 88 | +chatAgent.chat("What's my name?").collect { state -> |
| 89 | + // Agent remembers the conversation context |
| 90 | +} |
116 | 91 | ``` |
117 | 92 |
|
118 | | -## Project Structure |
| 93 | +### Tool Integration |
119 | 94 |
|
120 | | -The project is organized with the main library code and build infrastructure in the `library` directory: |
| 95 | +```kotlin |
| 96 | +// Create an agent with tools |
| 97 | +val agent = agentWithTools( |
| 98 | + url = "https://your-agent-api.com/agent", |
| 99 | + toolRegistry = toolRegistry { |
| 100 | + addTool(WeatherToolExecutor()) |
| 101 | + addTool(CalculatorToolExecutor()) |
| 102 | + addTool(ConfirmationToolExecutor(uiHandler)) |
| 103 | + } |
| 104 | +) { |
| 105 | + bearerToken = "your-api-token" |
| 106 | +} |
121 | 107 |
|
122 | | -- `/library` - Main library module with all source code and build files |
123 | | -- `/docs` - Documentation and guides |
124 | | -- `/examples` - Example applications (coming soon) |
125 | | -- `/.github` - GitHub Actions CI/CD configuration |
| 108 | +// Agent can now use tools during conversation |
| 109 | +agent.sendMessage("What's 15% tip on $85.50?").collect { state -> |
| 110 | + // Agent will use calculator tool automatically |
| 111 | +} |
| 112 | +``` |
126 | 113 |
|
127 | | -All development work happens in the `library` directory. See the [library README](library/README.md) for detailed build instructions. |
| 114 | +## 📐 Architecture |
128 | 115 |
|
129 | | -## Architecture |
| 116 | +AG-UI-4K follows a clean, modular architecture: |
130 | 117 |
|
131 | | -The library follows a clean architecture with clear separation of concerns: |
| 118 | +``` |
| 119 | +┌─────────────────────────────────────────────────────────────┐ |
| 120 | +│ Your Application │ |
| 121 | +├─────────────────────────────────────────────────────────────┤ |
| 122 | +│ agui4k-agent-sdk │ |
| 123 | +│ ┌─────────────┐ ┌───────────────────┐ ┌──────────────┐ │ |
| 124 | +│ │ AgUi4KAgent │ │StatefulAgUi4KAgent│ │ Builders │ │ |
| 125 | +│ └─────────────┘ └───────────────────┘ └──────────────┘ │ |
| 126 | +├─────────────────────────────────────────────────────────────┤ |
| 127 | +│ agui4k-client │ agui4k-tools │ |
| 128 | +│ ┌────────────┐ ┌─────────────┐ │ ┌──────────────────────┐ │ |
| 129 | +│ │ HttpAgent │ │AbstractAgent│ │ │ ToolRegistry │ │ |
| 130 | +│ ├────────────┤ ├─────────────┤ │ ├──────────────────────┤ │ |
| 131 | +│ │EventVerifier│ │DefaultApply │ │ │ ToolExecutor │ │ |
| 132 | +│ │ SseParser │ │ Events │ │ │ToolExecutionManager │ │ |
| 133 | +│ └────────────┘ └─────────────┘ │ └──────────────────────┘ │ |
| 134 | +├─────────────────────────────────────────────────────────────┤ |
| 135 | +│ agui4k-core │ |
| 136 | +│ Protocol Types & Event Definitions │ |
| 137 | +└─────────────────────────────────────────────────────────────┘ |
| 138 | +``` |
132 | 139 |
|
133 | | -- **Core Module**: Protocol definitions, event types, and base client abstractions |
134 | | -- **Client Module**: Concrete client implementations (HttpAgent) for connecting to agents |
135 | | -- **Platform Modules**: Platform-specific optimizations and integrations |
| 140 | +### Module Overview |
136 | 141 |
|
137 | | -### Key Components |
| 142 | +- **agui4k-core**: Protocol definitions, event types, and message structures |
| 143 | +- **agui4k-client**: Low-level client infrastructure and transport implementations |
| 144 | +- **agui4k-tools**: Tool execution framework and built-in tool executors |
| 145 | +- **agui4k-agent-sdk**: High-level APIs for common agent interaction patterns |
138 | 146 |
|
139 | | -1. **AbstractAgent**: Base class for client implementations |
140 | | -2. **HttpAgent**: HTTP-based client using Server-Sent Events (SSE) to connect to AG-UI agents |
141 | | -3. **Event System**: Strongly typed events for receiving agent responses |
142 | | -4. **State Management**: Efficient state synchronization with snapshots and deltas from the agent |
| 147 | +## 🎯 Supported Platforms |
143 | 148 |
|
144 | | -## Supported Platforms |
| 149 | +| Platform | Status | Minimum Version | Notes | |
| 150 | +|----------|--------|-----------------|-------| |
| 151 | +| Android | ✅ Stable | API 26+ | Full feature support | |
| 152 | +| iOS | ✅ Stable | iOS 13+ | Native performance (not tested beyond unit tests) | |
| 153 | +| JVM | ✅ Stable | Java 11+ | Desktop | |
145 | 154 |
|
146 | | -- ✅ Android (API 21+) |
147 | | -- ✅ iOS (iOS 13+) |
148 | | -- ✅ JVM (Java 11+) |
149 | | -- 🚧 JS (Browser) - Coming soon |
150 | | -- 🚧 Native (Linux, macOS, Windows) - Coming soon |
| 155 | +## 📚 Documentation |
151 | 156 |
|
152 | | -## Documentation |
| 157 | +### Essential Guides |
153 | 158 |
|
154 | | -For detailed documentation, please visit our [Wiki](https://github.com/contextable/agui4k/wiki). |
| 159 | +- [API Reference](https://contextable.github.io/ag-ui-4k/) - Complete API documentation |
155 | 160 |
|
156 | | -### Important Documents |
| 161 | +### Example Applications |
| 162 | +- [Chat Application](examples/chatapp) - Basic chat client |
| 163 | +- [Tool Examples](examples/tools) - Custom tool implementations |
157 | 164 |
|
158 | | -- [OVERVIEW](OVERVIEW.md) - Understanding what AGUI4K is (and isn't) |
159 | | -- [CHANGELOG](CHANGELOG.md) - Version history and release notes |
160 | | -- [CONTRIBUTING](CONTRIBUTING.md) - How to contribute to the project |
161 | | -- [PERFORMANCE](PERFORMANCE.md) - Performance optimization guide |
| 165 | +### Development Setup |
162 | 166 |
|
163 | | -### Examples |
| 167 | +1. **Clone the repository** |
| 168 | + ```bash |
| 169 | + git clone https://github.com/contextable/ag-ui-4k.git |
| 170 | + cd ag-ui-4k |
| 171 | + ``` |
164 | 172 |
|
165 | | -Check out the [examples](examples/) directory for: |
166 | | -- Android sample app |
167 | | -- iOS sample app |
168 | | -- Multiplatform chat application |
| 173 | +2. **Build the project** |
| 174 | + ```bash |
| 175 | + cd library |
| 176 | + ./gradlew build |
| 177 | + ``` |
169 | 178 |
|
170 | | -## Contributing |
| 179 | +3. **Run tests** |
| 180 | + ```bash |
| 181 | + ./gradlew test |
| 182 | + ``` |
171 | 183 |
|
172 | | -We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details. |
| 184 | +4. **Generate documentation** |
| 185 | + ```bash |
| 186 | + ./gradlew dokkaHtml |
| 187 | + ``` |
173 | 188 |
|
174 | | -### Development Setup |
| 189 | +## 🔄 Version Compatibility |
175 | 190 |
|
176 | | -1. Clone the repository |
177 | | -2. Open in IntelliJ IDEA or Android Studio (open the `library` directory as the project) |
178 | | -3. Build using one of these methods: |
179 | | - - **From library directory**: `cd library && ./gradlew build` |
180 | | - - **Using helper script**: `./build.sh build` (Unix) or `build.bat build` (Windows) |
181 | | -4. Run tests: `./build.sh test` or `cd library && ./gradlew test` |
| 191 | +| AG-UI-4K | Kotlin | Ktor | AG-UI Protocol | |
| 192 | +|----------|--------|------|----------------| |
| 193 | +| 0.2.x | 2.1.21+ | 3.1.x | 1.0 | |
182 | 194 |
|
183 | | -## License |
| 195 | +## 📄 License |
184 | 196 |
|
185 | 197 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
186 | 198 |
|
187 | | -## Acknowledgments |
| 199 | +## 🙏 Acknowledgments |
| 200 | + |
| 201 | +- Built on the [AG-UI Protocol](https://github.com/ag-ui-protocol/ag-ui) specification |
| 202 | +- Powered by [Kotlin Multiplatform](https://kotlinlang.org/docs/multiplatform.html) |
| 203 | +- Networking by [Ktor](https://ktor.io/) |
| 204 | +- Serialization with [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) |
| 205 | + |
| 206 | +## 📬 Support |
188 | 207 |
|
189 | | -- Inspired by the [AG-UI Protocol](https://github.com/ag-ui/protocol) |
190 | | -- Built with [Kotlin Multiplatform](https://kotlinlang.org/docs/multiplatform.html) |
191 | | -- Powered by [Ktor](https://ktor.io/) for networking |
| 208 | +- **Issues**: [GitHub Issues](https://github.com/contextable/ag-ui-4k/issues) |
0 commit comments