Class Message.Builder

java.lang.Object
org.a2aproject.sdk.spec.Message.Builder
Enclosing class:
Message

public static class Message.Builder extends Object
Builder for constructing Message instances with fluent API.

The Builder provides a convenient way to construct messages with required and optional fields. If messageId is not provided, a random UUID will be generated automatically.

Example usage:


 Message userMessage = Message.builder()
     .role(Message.Role.USER)
     .parts(List.of(new TextPart("Hello, agent!")))
     .contextId("conv-123")
     .build();
 
  • Method Details

    • role

      public Message.Builder role(Message.Role role)
      Sets the role of the message sender.
      Parameters:
      role - the message role (required)
      Returns:
      this builder for method chaining
    • parts

      public Message.Builder parts(List<Part<?>> parts)
      Sets the content parts of the message.
      Parameters:
      parts - the list of message parts (required, must not be empty)
      Returns:
      this builder for method chaining
    • parts

      public Message.Builder parts(Part<?>... parts)
      Sets the content parts of the message from varargs.
      Parameters:
      parts - the message parts (required, must not be empty)
      Returns:
      this builder for method chaining
    • messageId

      public Message.Builder messageId(String messageId)
      Sets the unique identifier for this message.

      If not provided, a random UUID will be generated when build() is called.

      Parameters:
      messageId - the message identifier (optional)
      Returns:
      this builder for method chaining
    • contextId

      public Message.Builder contextId(String contextId)
      Sets the conversation context identifier.
      Parameters:
      contextId - the context identifier (optional)
      Returns:
      this builder for method chaining
    • taskId

      public Message.Builder taskId(String taskId)
      Sets the task identifier this message is associated with.
      Parameters:
      taskId - the task identifier (optional)
      Returns:
      this builder for method chaining
    • referenceTaskIds

      public Message.Builder referenceTaskIds(List<String> referenceTaskIds)
      Sets the list of reference task identifiers this message relates to.
      Parameters:
      referenceTaskIds - the list of reference task IDs (optional)
      Returns:
      this builder for method chaining
    • metadata

      public Message.Builder metadata(@Nullable Map<String,Object> metadata)
      Sets additional metadata for the message.
      Parameters:
      metadata - map of metadata key-value pairs (optional)
      Returns:
      this builder for method chaining
    • extensions

      public Message.Builder extensions(List<String> extensions)
      Sets the list of protocol extensions used in this message.
      Parameters:
      extensions - the list of extension identifiers (optional)
      Returns:
      this builder for method chaining
    • build

      public Message build()
      Builds a new Message from the current builder state.

      If messageId was not set, a random UUID will be generated.

      Returns:
      a new Message instance
      Throws:
      IllegalArgumentException - if required fields are missing or invalid