Represents a single message in the conversation between a user and an agent in the A2A Protocol. A Message encapsulates communication content exchanged during agent interactions. It contains the message role (user or agent), content parts (text, files, or data), and contextual metadata for messa
| 35 | * @see <a href="https://a2a-protocol.org/latest/">A2A Protocol Specification</a> |
| 36 | */ |
| 37 | public record Message(Role role, List<Part<?>> parts, |
| 38 | String messageId, |
| 39 | @Nullable String contextId, |
| 40 | @Nullable String taskId, |
| 41 | @Nullable List<String> referenceTaskIds, |
| 42 | @Nullable Map<String, Object> metadata, |
| 43 | @Nullable List<String> extensions |
| 44 | ) implements EventKind, StreamingEventKind { |
| 45 | |
| 46 | /** |
| 47 | * The identifier when used in streaming responses |
| 48 | */ |
| 49 | public static final String STREAMING_EVENT_ID = "message"; |
| 50 | |
| 51 | /** |
| 52 | * Compact constructor with validation and defensive copying. |
| 53 | * |
| 54 | * @param role the role of the message sender (user or agent) |
| 55 | * @param parts the content parts of the message (text, file, or data) |
| 56 | * @param messageId the unique identifier for this message |
| 57 | * @param contextId the conversation context identifier |
| 58 | * @param taskId the task identifier this message is associated with |
| 59 | * @param referenceTaskIds list of reference task identifiers |
| 60 | * @param metadata additional metadata for the message |
| 61 | * @param extensions list of protocol extensions used in this message |
| 62 | * @throws IllegalArgumentException if role, parts, or messageId is null, or if parts is empty |
| 63 | */ |
| 64 | public Message { |
| 65 | Assert.checkNotNullParam("role", role); |
| 66 | Assert.checkNotNullParam("parts", parts); |
| 67 | Assert.checkNotNullParam("messageId", messageId); |
| 68 | if (parts.isEmpty()) { |
| 69 | throw new IllegalArgumentException("Parts cannot be empty"); |
| 70 | } |
| 71 | parts = List.copyOf(parts); |
| 72 | referenceTaskIds = referenceTaskIds != null ? List.copyOf(referenceTaskIds) : null; |
| 73 | metadata = (metadata != null) ? Map.copyOf(metadata) : null; |
| 74 | extensions = extensions != null ? List.copyOf(extensions) : null; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String kind() { |
| 79 | return STREAMING_EVENT_ID; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates a new Builder for constructing Message instances. |
| 84 | * |
| 85 | * @return a Message.builder instance |
| 86 | */ |
| 87 | public static Builder builder() { |
| 88 | return new Builder(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a new Builder initialized with values from an existing Message. |
| 93 | * |
| 94 | * @param message the Message to copy values from |
nothing calls this directly
no test coverage detected