|
| 1 | +# A2A Dart Library Design Document |
| 2 | + |
| 3 | +## 1. Overview |
| 4 | + |
| 5 | +This document outlines the design for a pure Dart implementation of the Agent2Agent (A2A) protocol. The `a2a_dart` library provides both client and server components for A2A communication. The client is platform-independent and can be used in web applications, while the server is designed for native platforms that support `dart:io`. |
| 6 | + |
| 7 | +The primary goal is to create a library that is: |
| 8 | + |
| 9 | +- **Comprehensive**: Implements the full A2A specification. |
| 10 | +- **Idiomatic**: Feels natural to Dart and Flutter developers. |
| 11 | +- **Type-Safe**: Leverages Dart's strong type system to prevent errors. |
| 12 | +- **Extensible**: Allows for future expansion and customization. |
| 13 | + |
| 14 | +## 2. Goals and Non-Goals |
| 15 | + |
| 16 | +### Goals |
| 17 | + |
| 18 | +- Provide a type-safe, idiomatic Dart implementation of the A2A protocol. |
| 19 | +- Support the full A2A specification, including all data models and RPC methods. |
| 20 | +- Offer a clear and easy-to-use client API for interacting with A2A agents. |
| 21 | +- Provide a flexible and extensible server framework for building A2A agents in Dart. |
| 22 | +- Adhere to Dart and Flutter best practices, including null safety, effective asynchronous programming, and clean architecture. |
| 23 | + |
| 24 | +### Non-Goals |
| 25 | + |
| 26 | +- **Transports**: Implement transport protocols other than JSON-RPC and SSE over HTTP. gRPC and REST transports are out of scope for the initial version. |
| 27 | +- **Push Notifications**: The server-side push notification mechanism will not be implemented initially. The client will support sending the configuration, but the server will not act on it. |
| 28 | +- **Agent Framework**: Provide a full-fledged agent framework with built-in AI capabilities. This library focuses on the communication protocol. |
| 29 | +- **Extensions**: Implement any of the optional extensions to the A2A protocol in the initial version. |
| 30 | + |
| 31 | +## 3. Implemented A2A Features |
| 32 | + |
| 33 | +The `a2a_dart` library implements the following features from the A2A specification: |
| 34 | + |
| 35 | +### Core Concepts |
| 36 | + |
| 37 | +- **Client & Server**: Foundational components for initiating and responding to A2A requests. |
| 38 | +- **Agent Card**: Full implementation for agent discovery and capability advertisement. |
| 39 | +- **Task**: State management for all agent operations. |
| 40 | +- **Message**: The primary object for communication turns. |
| 41 | +- **Part**: Support for `TextPart`, `FilePart`, and `DataPart` to enable rich content exchange. |
| 42 | +- **Artifact**: Handling for agent-generated outputs. |
| 43 | +- **Context**: Grouping related tasks. |
| 44 | + |
| 45 | +### Transport Protocols |
| 46 | + |
| 47 | +- **JSON-RPC 2.0**: The primary transport protocol for all RPC methods over HTTP/S. |
| 48 | +- **Server-Sent Events (SSE)**: For real-time, streaming updates from the server to the client (`message/stream` and `tasks/resubscribe`). |
| 49 | + |
| 50 | +### Data Models |
| 51 | + |
| 52 | +- A complete, type-safe implementation of all data objects defined in the specification, including: |
| 53 | + - `Task`, `TaskStatus`, `TaskState` |
| 54 | + - `Message`, `Part` (and its variants) |
| 55 | + - `AgentCard` (and all nested objects like `AgentSkill`, `AgentProvider`, etc.) |
| 56 | + - `Artifact` |
| 57 | + - `PushNotificationConfig` (client-side only) |
| 58 | + - All JSON-RPC request, response, and error structures. |
| 59 | + |
| 60 | +### RPC Methods |
| 61 | + |
| 62 | +- The library provides client methods and server-side handlers for the following A2A RPC methods: |
| 63 | + - `get_agent_card` (via HTTP GET) |
| 64 | + - `create_task` |
| 65 | + - `message/stream` |
| 66 | + - `execute_task` |
| 67 | + |
| 68 | +### Authentication |
| 69 | + |
| 70 | +- The library will be designed to work with standard HTTP authentication mechanisms (e.g., Bearer Token, API Key) by providing hooks (middleware) for adding authentication headers to client requests. |
| 71 | + |
| 72 | +## 4. Architecture |
| 73 | + |
| 74 | +The `a2a_dart` library is structured with a single public entry point, `lib/a2a_dart.dart`, which exports the core, client, and server APIs. The internal structure is organized as follows: |
| 75 | + |
| 76 | +- **`lib/src`**: Contains the private implementation of the library. |
| 77 | + - **`core`**: Contains the platform-independent data models and types defined in the A2A specification. |
| 78 | + - **`client`**: Provides the `A2AClient` class and transport implementations (`HttpTransport`, `SseTransport`). |
| 79 | + - **`server`**: Offers a framework for building A2A agents. It uses a conditional export (`a2a_server.dart`) to provide a native implementation (`io/a2a_server.dart`) and a web stub (`web/a2a_server.dart`). |
| 80 | + |
| 81 | +```mermaid |
| 82 | +graph TD |
| 83 | + subgraph Public API |
| 84 | + A[lib/a2a_dart.dart] |
| 85 | + end |
| 86 | +
|
| 87 | + subgraph "Implementation (lib/src)" |
| 88 | + B[Core] |
| 89 | + C[Client] |
| 90 | + D[Server] |
| 91 | + end |
| 92 | +
|
| 93 | + A --> B |
| 94 | + A --> C |
| 95 | + A --> D |
| 96 | +
|
| 97 | + B --> B1[Data Models] |
| 98 | +
|
| 99 | + C --> C1[A2AClient] |
| 100 | + C --> C2[Transport] |
| 101 | + C2 --> C2a[HttpTransport] |
| 102 | + C2 --> C2b[SseTransport] |
| 103 | +
|
| 104 | + D --> D1[a2a_server.dart (conditional export)] |
| 105 | + D1 --> D1a[io/a2a_server.dart] |
| 106 | + D1 --> D1b[web/a2a_server.dart] |
| 107 | + D --> D2[RequestHandler] |
| 108 | +``` |
| 109 | + |
| 110 | +## 4. Data Models |
| 111 | + |
| 112 | +All data models from the A2A specification will be implemented as immutable Dart classes. To reduce boilerplate and ensure correctness, we will use the `json_serializable` and `freezed` packages for JSON serialization and value equality. |
| 113 | + |
| 114 | +- **Immutability**: All model classes will be immutable. |
| 115 | +- **JSON Serialization**: Each class will have `fromJson` and `toJson` methods. |
| 116 | +- **Null Safety**: All fields will be null-safe. |
| 117 | + |
| 118 | +Example `AgentCard` model: |
| 119 | + |
| 120 | +```dart |
| 121 | +import 'package:freezed_annotation/freezed_annotation.dart'; |
| 122 | +
|
| 123 | +part 'agent_card.freezed.dart'; |
| 124 | +part 'agent_card.g.dart'; |
| 125 | +
|
| 126 | +@freezed |
| 127 | +class AgentCard with _$AgentCard { |
| 128 | + const factory AgentCard({ |
| 129 | + required String protocolVersion, |
| 130 | + required String name, |
| 131 | + required String description, |
| 132 | + required String url, |
| 133 | + // ... other fields |
| 134 | + }) = _AgentCard; |
| 135 | +
|
| 136 | + factory AgentCard.fromJson(Map<String, dynamic> json) => _$AgentCardFromJson(json); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## 5. Client API |
| 141 | + |
| 142 | +The client API will be centered around the `A2AClient` class. This class will provide methods for each of the A2A RPC calls, such as `sendMessage`, `getTask`, and `cancelTask`. |
| 143 | + |
| 144 | +- **Asynchronous**: All API methods will be asynchronous, returning `Future`s. |
| 145 | +- **Transport Agnostic**: The `A2AClient` delegates the actual HTTP communication to a `Transport` interface. This allows for different transport implementations, with `HttpTransport` providing basic request-response and `SseTransport` extending it for streaming. |
| 146 | + |
| 147 | +Example `A2AClient` usage: |
| 148 | + |
| 149 | +```dart |
| 150 | +final log = Logger('MyClient'); |
| 151 | +final client = A2AClient( |
| 152 | + url: 'https://example.com/a2a', |
| 153 | + transport: SseTransport(url: 'https://example.com/a2a', log: log), |
| 154 | +); |
| 155 | +
|
| 156 | +// Create a task |
| 157 | +final task = await client.createTask(Message( |
| 158 | + messageId: '1', |
| 159 | + role: Role.user, |
| 160 | + parts: [Part.text(text: 'Hello, agent!')], |
| 161 | +)); |
| 162 | +
|
| 163 | +// Execute the task and get a stream of events |
| 164 | +final stream = client.executeTask(task.id); |
| 165 | +await for (final event in stream) { |
| 166 | + // process events |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## 6. Server Framework |
| 171 | + |
| 172 | +The server framework will provide the building blocks for creating A2A-compliant agents in Dart. |
| 173 | + |
| 174 | +- **`A2AServer`**: A top-level class that listens for incoming HTTP requests. It is conditionally exported to support both native and web platforms. On native, it uses `dart:io` to create an HTTP server. On the web, it throws an `UnsupportedError` if instantiated. |
| 175 | +- **`RequestHandler`**: An interface for handling specific A2A methods. Developers will implement this interface to define their agent's behavior. The `handle` method returns a `HandlerResult` which can be a `SingleResult` for a single response or a `StreamResult` for a streaming response. |
| 176 | +- **`TaskManager`**: A class responsible for managing the lifecycle of tasks. |
| 177 | + |
| 178 | +## 7. Error Handling |
| 179 | + |
| 180 | +Errors will be handled using a combination of exceptions and a `Result` type. Network and transport-level errors will throw exceptions, while A2A-specific errors will be returned as part of a `Result` object, allowing for more granular error handling. |
| 181 | + |
| 182 | +## 8. Dependencies |
| 183 | + |
| 184 | +- `http`: For making HTTP requests. |
| 185 | +- `freezed`: For immutable data classes. |
| 186 | +- `json_serializable`: For JSON serialization. |
| 187 | +- `shelf`: For building the server. |
| 188 | +- `shelf_router`: For routing requests on the server. |
| 189 | +- `uuid`: For generating unique IDs. |
| 190 | + |
| 191 | +## 9. Testing |
| 192 | + |
| 193 | +The library will have a comprehensive suite of unit and integration tests. |
| 194 | + |
| 195 | +- **Unit Tests**: Will cover individual classes and methods in isolation. |
| 196 | +- **Integration Tests**: Will test the client and server components together, as well as against a known-good A2A implementation. |
| 197 | + |
| 198 | +## 10. Documentation |
| 199 | + |
| 200 | +All public APIs will be thoroughly documented with DartDoc comments. The package will also include a comprehensive `README.md` and example usage. |
0 commit comments