Record Class Message

Record Components:
role - speaker role.
parts - ordered content parts (may be empty when tool fields are set).
toolCalls - assistant tool calls; null or empty when absent.
toolCallId - id of the tool call this message answers (role=tool).
name - optional function / participant name.

public record Message(Role role, List<ContentPart> parts, List<ToolCall> toolCalls, String toolCallId, String name) extends Record
Dialog message with OpenAI-style multimodal content parts and optional tool fields.

Text-only messages use a single TextPart. Multimodal turns may interleave text with ImageUrlPart / VideoUrlPart. Assistant turns may carry toolCalls(); tool-result turns use toolCallId().

  • Constructor Details

    • Message

      public Message(Role role, List<ContentPart> parts, List<ToolCall> toolCalls, String toolCallId, String name)
      Compact canonical constructor that validates inputs.
    • Message

      public Message(Role role, String content)
      Text-only convenience constructor.
      Parameters:
      role - speaker role.
      content - plain text body.
    • Message

      public Message(Role role, List<ContentPart> parts)
      Role + content parts (no tool fields).
      Parameters:
      role - speaker role.
      parts - content parts.
    • Message

      public Message(Role role, ContentPart... parts)
      Varargs content parts constructor.
      Parameters:
      role - speaker role.
      parts - content parts.
  • Method Details

    • content

      public String content()
      Concatenates all TextPart text (ignores media parts).
      Returns:
      combined text, possibly empty.
    • hasMedia

      public boolean hasMedia()
      Returns whether any part is an image, video, or audio.
      Returns:
      true when any part is an image, video, or audio.
    • hasAudio

      public boolean hasAudio()
      Returns whether any part is audio.
      Returns:
      true when any part is audio.
    • hasToolCalls

      public boolean hasToolCalls()
      Returns whether this assistant message includes tool calls.
      Returns:
      true when this assistant message includes tool calls.
    • withParts

      public Message withParts(List<ContentPart> newParts)
      Copy with replaced content parts, preserving tool metadata.
      Parameters:
      newParts - replacement parts.
      Returns:
      new message.
    • system

      public static Message system(String content)
      Factory method for a system message.
      Parameters:
      content - the message content.
      Returns:
      a system message.
    • user

      public static Message user(String content)
      Factory method for a user message.
      Parameters:
      content - the message content.
      Returns:
      a user message.
    • user

      public static Message user(ContentPart... parts)
      Multimodal user message.
      Parameters:
      parts - ordered content parts.
      Returns:
      a user message.
    • assistant

      public static Message assistant(String content)
      Factory method for an assistant message.
      Parameters:
      content - the message content.
      Returns:
      an assistant message.
    • assistant

      public static Message assistant(String content, List<ToolCall> toolCalls)
      Assistant message with tool calls.
      Parameters:
      content - optional text prefix; may be null or blank.
      toolCalls - tool calls (required, non-empty).
      Returns:
      assistant message.
    • tool

      public static Message tool(String toolCallId, String content)
      Tool-result message.
      Parameters:
      toolCallId - id of the tool call being answered.
      content - tool result text.
      Returns:
      tool message.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • role

      public Role role()
      Returns the value of the role record component.
      Returns:
      the value of the role record component
    • parts

      public List<ContentPart> parts()
      Returns the value of the parts record component.
      Returns:
      the value of the parts record component
    • toolCalls

      public List<ToolCall> toolCalls()
      Returns the value of the toolCalls record component.
      Returns:
      the value of the toolCalls record component
    • toolCallId

      public String toolCallId()
      Returns the value of the toolCallId record component.
      Returns:
      the value of the toolCallId record component
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component