Class Task.Builder

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

public static class Task.Builder extends Object
Builder for constructing immutable Task instances.

The Builder pattern is used to enforce immutability of Task objects while providing a fluent API for setting required and optional fields. This approach ensures that once a Task is created, its state cannot be modified directly, which is important for thread-safety and protocol correctness.

Example usage:


 Task task = Task.builder()
     .id("task-123")
     .contextId("context-456")
     .status(new TaskStatus(TaskState.WORKING))
     .artifacts(List.of(new Artifact(...)))
     .history(List.of(userMessage))
     .metadata(Map.of("key", "value"))
     .build();
 
  • Method Details

    • id

      public Task.Builder id(String id)
      Sets the unique identifier for this task.
      Parameters:
      id - the task ID (required)
      Returns:
      this builder for method chaining
    • contextId

      public Task.Builder contextId(String contextId)
      Sets the context identifier associating this task with a conversation or session.

      Multiple tasks may share the same contextId if they are part of a multi-turn conversation or related workflow.

      Parameters:
      contextId - the context ID (required)
      Returns:
      this builder for method chaining
    • status

      public Task.Builder status(TaskStatus status)
      Sets the current status of the task.

      The status includes the state (SUBMITTED, WORKING, COMPLETED, etc.), an optional message, and a timestamp.

      Parameters:
      status - the task status (required)
      Returns:
      this builder for method chaining
      See Also:
    • artifacts

      public Task.Builder artifacts(@Nullable List<Artifact> artifacts)
      Sets the list of artifacts produced by the agent during task execution.

      Artifacts represent the agent's responses or output, which may include text, files, data, or other content types. Artifacts accumulate over the lifetime of the task, especially in streaming scenarios.

      Parameters:
      artifacts - the list of artifacts (optional)
      Returns:
      this builder for method chaining
      See Also:
    • history

      public Task.Builder history(@Nullable List<Message> history)
      Sets the conversation history for this task.

      The history contains all messages exchanged between the client and agent as part of this task, providing context for multi-turn interactions.

      Parameters:
      history - the list of messages (optional)
      Returns:
      this builder for method chaining
      See Also:
    • history

      public Task.Builder history(Message... history)
      Sets the conversation history using a varargs array of messages.

      This is a convenience method for setting history without creating a List explicitly.

      Parameters:
      history - the messages to include in the history
      Returns:
      this builder for method chaining
      See Also:
    • metadata

      public Task.Builder metadata(Map<String,Object> metadata)
      Sets arbitrary metadata associated with the task.

      Metadata can be used to store custom information about the task, such as client identifiers, routing information, or application-specific data.

      Parameters:
      metadata - map of metadata key-value pairs (optional)
      Returns:
      this builder for method chaining
    • build

      public Task build()
      Builds an immutable Task from the current builder state.
      Returns:
      a new Task instance
      Throws:
      IllegalArgumentException - if any required field (id, contextId, status) is null