Class AiMessageListItem

java.lang.Object
io.jmix.aichat.component.messagelist.AiMessageListItem
All Implemented Interfaces:
Serializable

public class AiMessageListItem extends Object implements Serializable
Item to render as a message component inside an AiMessageList. Each item has a MessageRole: assistant messages are rendered as Markdown (with embedded code blocks), user messages as plain text with preserved newlines.
See Also:
  • Field Details

  • Constructor Details

    • AiMessageListItem

      public AiMessageListItem()
      Creates an empty message list item with the default MessageRole.USER role.
    • AiMessageListItem

      public AiMessageListItem(@Nullable String text)
      Creates a message list item with the provided text and the default MessageRole.USER role.
      Parameters:
      text - the text content of the message
    • AiMessageListItem

      public AiMessageListItem(@Nullable String text, MessageRole role)
      Creates a message list item with the provided text and role.
      Parameters:
      text - the text content of the message
      role - the message role, not null
  • Method Details

    • user

      public static AiMessageListItem user(@Nullable String text)
      Creates a user message with the given text.
      Parameters:
      text - the text content of the message
      Returns:
      a new item with role MessageRole.USER
    • assistant

      public static AiMessageListItem assistant(@Nullable String text)
      Creates an assistant message with the given text.
      Parameters:
      text - the text content of the message
      Returns:
      a new item with role MessageRole.ASSISTANT
    • getText

      public @Nullable String getText()
      Gets the text content of the message.
      Returns:
      the message's text content, or null if none is set
    • setText

      public void setText(@Nullable String text)
      Sets the text content of the message.
      Parameters:
      text - the message's text content to set, or null to remove the content
    • appendText

      public void appendText(@Nullable String text)
      Appends the provided text to the message's text content.
      Parameters:
      text - the text to append to the message's text content
    • getRole

      public MessageRole getRole()
      Gets the role of the message.
      Returns:
      the message role (never null)
    • setRole

      public void setRole(MessageRole role)
      Sets the role of the message. The role decides how the message is rendered and styled.
      Parameters:
      role - the message role, not null
    • getRoleJson

      protected String getRoleJson()
    • getSourceMessage

      public <M extends AiChatMessage> @Nullable M getSourceMessage()
      Gets the domain message this item was rendered from.

      AiChat populates it for every item it renders from its bound message source, which is how a per-message action identifies the message it was clicked on. The accessor is self-typed, so no cast is needed:

      
       ChatMessage message = event.getItem().getSourceMessage();
       
      The reference is server-side only and never reaches the client.
      Type Parameters:
      M - the application's own message type
      Returns:
      the message this item was rendered from, or null when there is none — the streaming placeholder, the transient bubble a stopped or failed generation leaves behind (whose action bar is clickable), and any item an application built itself
    • setSourceMessage

      public void setSourceMessage(@Nullable AiChatMessage sourceMessage)
      Sets the domain message this item was rendered from. Server-side metadata: it is not serialized to the client, so setting it schedules no client update.
      Parameters:
      sourceMessage - the message this item was rendered from, or null to remove it
    • getTime

      public @Nullable Instant getTime()
      Gets the time of sending the message.
      Returns:
      the time of the message, or null if none is set
    • setTime

      public void setTime(@Nullable Instant time)
      Sets the time of sending the message. Retained as optional metadata; not rendered by default in the AI-chat layout.
      Parameters:
      time - the time of the message to set, or null to remove the time
    • getUserName

      public @Nullable String getUserName()
      Gets the user name of the message sender.
      Returns:
      the message sender's user name, or null if none is set
    • setUserName

      public void setUserName(@Nullable String userName)
      Sets the user name of the message sender. Retained as optional metadata (used for the message's accessible description); not rendered visually by default.
      Parameters:
      userName - the message sender's user name, or null to remove it
    • getAttachments

      public List<AiMessageListItem.Attachment> getAttachments()
      Gets the attachments displayed with this message.
      Returns:
      an unmodifiable view of the attachments (never null)
    • setAttachments

      public void setAttachments(List<AiMessageListItem.Attachment> attachments)
      Sets the attachments displayed with this message, replacing any current ones.
      Parameters:
      attachments - the attachments to set
    • addAttachment

      public void addAttachment(AiMessageListItem.Attachment attachment)
      Adds an attachment to this message.
      Parameters:
      attachment - the attachment to add
    • getAttachmentsJson

      protected @Nullable List<AiMessageListItem.Attachment> getAttachmentsJson()
    • addThemeNames

      public void addThemeNames(String... themeNames)
      Adds one or more theme names to this message.
      Parameters:
      themeNames - the theme name or theme names to be added to the message
    • removeThemeNames

      public void removeThemeNames(String... themeNames)
      Removes one or more theme names from this message.
      Parameters:
      themeNames - the theme name or theme names to be removed from the message
    • hasThemeName

      public boolean hasThemeName(String themeName)
      Checks if the message has the given theme name.
      Parameters:
      themeName - the theme name to check for
      Returns:
      true if the message has the given theme name, false otherwise
    • getTheme

      protected @Nullable String getTheme()
    • addClassNames

      public void addClassNames(String... classNames)
      Adds one or more class names to this item.
      Parameters:
      classNames - the class name or class names to be added to the item
    • removeClassNames

      public void removeClassNames(String... classNames)
      Removes one or more class names from this item.
      Parameters:
      classNames - the class name or class names to be removed from the item
    • hasClassName

      public boolean hasClassName(String className)
      Checks if the message has the given class name.
      Parameters:
      className - the class name to check for
      Returns:
      true if the message has the given class name, false otherwise
    • getClassName

      protected @Nullable String getClassName()
    • propsChanged

      protected void propsChanged()
    • setHost

      protected void setHost(@Nullable AiMessageList host)
    • getHost

      protected @Nullable AiMessageList getHost()