# Class ChatMessage

java.lang.Object
com.codename1.ai.ChatMessage

public final class ChatMessage extends Object
A single turn in a chat conversation. Holds a `Role`, one or more `MessagePart`s, and (for assistant turns) any `ToolCall`s the model produced. Construct via the static helpers (`user(String)`, `system(String)`, etc.) for the common case, or pass parts directly for multi-modal messages.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ChatMessage(Role role, List<MessagePart> parts)`
  Creates a message with no assistant tool calls or provider name.
  `ChatMessage(Role role, List<MessagePart> parts, List<ToolCall> toolCalls, String name, String toolCallId)`
  Creates a fully specified normalized message.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static ChatMessage`
  `assistant(String text)`
  `String`
  `getName()`
  `List<MessagePart>`
  `getParts()`
  `Role`
  `getRole()`
  `String`
  `getText()`
  Convenience: concatenates the text of every `TextPart`.
  `String`
  `getToolCallId()`
  `List<ToolCall>`
  `getToolCalls()`
  `static ChatMessage`
  `system(String text)`
  `static ChatMessage`
  `toolResult(String toolCallId, String resultJson)`
  Builds a TOOL message wrapping the result of a previous tool call.
  `static ChatMessage`
  `user(String text)`
  `static ChatMessage`
  `userWithImage(String text, ImagePart image)`
  Builds a USER message containing both a text and image part -- the common multi-modal pattern.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ChatMessage

    public ChatMessage(Role role, List<MessagePart> parts)
    Creates a message with no assistant tool calls or provider name.
    Parameters:
      `role` - speaker role; required
      `parts` - ordered multimodal content; `null` becomes empty

  - ### ChatMessage

    public ChatMessage(Role role, List<MessagePart> parts, List<ToolCall> toolCalls, String name, String toolCallId)
    Creates a fully specified normalized message.
    Parameters:
      `role` - speaker role; required
      `parts` - ordered content parts; `null` becomes empty
      `toolCalls` - assistant tool calls; `null` becomes empty
      `name` - optional provider-visible participant name
      `toolCallId` - tool call answered by a `Role.TOOL` message

- ## Method Details

  - ### system

    public static ChatMessage system(String text)
    Parameters:
      `text` - system instruction text
    Returns:
      a single-part system message

  - ### user

    public static ChatMessage user(String text)
    Parameters:
      `text` - user input text
    Returns:
      a single-part user message

  - ### assistant

    public static ChatMessage assistant(String text)
    Parameters:
      `text` - assistant output text
    Returns:
      a single-part assistant message

  - ### userWithImage

    public static ChatMessage userWithImage(String text, ImagePart image)
    Builds a USER message containing both a text and image part -- the common multi-modal pattern.
    Parameters:
      `text` - optional text accompanying the image
      `image` - image content sent to the model
    Returns:
      multimodal user message

  - ### toolResult

    public static ChatMessage toolResult(String toolCallId, String resultJson)
    Builds a TOOL message wrapping the result of a previous tool call.
    Parameters:
      `toolCallId` - provider id of the answered tool call
      `resultJson` - application result encoded as JSON
    Returns:
      tool-role result message

  - ### getRole

    public Role getRole()
    Returns:
      speaker role

  - ### getParts

    public List<MessagePart> getParts()
    Returns:
      immutable content parts in message order

  - ### getToolCalls

    public List<ToolCall> getToolCalls()
    Returns:
      immutable assistant tool calls

  - ### getName

    public String getName()
    Returns:
      optional participant name, or `null`

  - ### getToolCallId

    public String getToolCallId()
    Returns:
      answered tool-call id for tool messages, or `null`

  - ### getText

    public String getText()
    Convenience: concatenates the text of every `TextPart`. Image and tool-result parts are skipped. Useful for `ChatView` rendering when you don't care about multi-modal content.
    Returns:
      text parts joined with newlines