Class AguiMessage

java.lang.Object
io.agentscope.core.agui.model.AguiMessage

public class AguiMessage extends Object
Represents a message in the AG-UI protocol.

Messages are the primary communication unit in the AG-UI protocol. They contain content, role information, and optionally tool calls or tool call IDs.

Message roles:

  • user - Messages from the user
  • assistant - Messages from the AI assistant
  • system - System instructions
  • tool - Tool execution results

The content field uses MessageContent, a type-safe sealed union that can represent either plain text or a list of structured InputContent parts (text, image, audio, video, document).

  • Constructor Details

    • AguiMessage

      public AguiMessage(String id, String role, MessageContent content, List<AguiToolCall> toolCalls, String toolCallId)
      Creates a new AguiMessage.
      Parameters:
      id - The unique message ID
      role - The message role (user, assistant, system, tool)
      content - The message content (plain text or structured blocks), may be null
      toolCalls - Tool calls for assistant messages (optional)
      toolCallId - Tool call ID for tool messages (optional)
  • Method Details

    • userMessage

      public static AguiMessage userMessage(String id, String content)
      Creates a simple user message with plain text content.
      Parameters:
      id - The message ID
      content - The message content as plain text
      Returns:
      A new user message
    • userMessage

      public static AguiMessage userMessage(String id, List<InputContent> blocks)
      Creates a user message with structured content blocks.
      Parameters:
      id - The message ID
      blocks - The structured content parts
      Returns:
      A new user message
    • assistantMessage

      public static AguiMessage assistantMessage(String id, String content)
      Creates a simple assistant message.
      Parameters:
      id - The message ID
      content - The message content as plain text
      Returns:
      A new assistant message
    • systemMessage

      public static AguiMessage systemMessage(String id, String content)
      Creates a system message.
      Parameters:
      id - The message ID
      content - The message content as plain text
      Returns:
      A new system message
    • toolMessage

      public static AguiMessage toolMessage(String id, String toolCallId, String content)
      Creates a tool result message.
      Parameters:
      id - The message ID
      toolCallId - The ID of the tool call this is responding to
      content - The tool result content as plain text
      Returns:
      A new tool message
    • textMessage

      public static AguiMessage textMessage(String id, String role, String text, List<AguiToolCall> toolCalls, String toolCallId)
      Creates a message with plain text content, supporting a custom role, tool calls, and tool call ID. This is the full-parameter convenience factory for text-based messages.
      Parameters:
      id - The message ID
      role - The message role (user, assistant, system, tool)
      text - The message content as plain text, may be null
      toolCalls - Tool calls for assistant messages (optional)
      toolCallId - Tool call ID for tool messages (optional)
      Returns:
      A new message
    • blocksMessage

      public static AguiMessage blocksMessage(String id, String role, List<InputContent> blocks, List<AguiToolCall> toolCalls, String toolCallId)
      Creates a message with structured content blocks, supporting a custom role, tool calls, and tool call ID. This is the full-parameter convenience factory for blocks-based messages.
      Parameters:
      id - The message ID
      role - The message role (user, assistant, system, tool)
      blocks - The structured content parts
      toolCalls - Tool calls for assistant messages (optional)
      toolCallId - Tool call ID for tool messages (optional)
      Returns:
      A new message
    • getId

      public String getId()
      Get the message ID.
      Returns:
      The message ID
    • getRole

      public String getRole()
      Get the message role.
      Returns:
      The role (user, assistant, system, tool)
    • getContent

      public MessageContent getContent()
      Get the message content as a type-safe MessageContent.
      Returns:
      The content, may be null
    • getTextContent

      public String getTextContent()
      Get the plain text content if the content is a MessageContent.Text.

      This is a convenience method for the common case where content is plain text. If the content is structured blocks or null, this returns null.

      Returns:
      The text value, or null if content is not plain text
    • hasBlocks

      public boolean hasBlocks()
      Check if this message has structured content blocks.
      Returns:
      true if the content is a MessageContent.Blocks
    • getToolCalls

      public List<AguiToolCall> getToolCalls()
      Get the tool calls (for assistant messages).
      Returns:
      The tool calls as an immutable list, empty if none
    • getToolCallId

      public String getToolCallId()
      Get the tool call ID (for tool messages).
      Returns:
      The tool call ID, or null if not a tool message
    • isUserMessage

      public boolean isUserMessage()
      Check if this is a user message.
      Returns:
      true if role is "user"
    • isAssistantMessage

      public boolean isAssistantMessage()
      Check if this is an assistant message.
      Returns:
      true if role is "assistant"
    • isSystemMessage

      public boolean isSystemMessage()
      Check if this is a system message.
      Returns:
      true if role is "system"
    • isToolMessage

      public boolean isToolMessage()
      Check if this is a tool message.
      Returns:
      true if role is "tool"
    • hasToolCalls

      public boolean hasToolCalls()
      Check if this message has tool calls.
      Returns:
      true if tool calls are present
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object