Class AbstractMessage
java.lang.Object
org.springframework.ai.chat.messages.AbstractMessage
- Direct Known Subclasses:
AssistantMessage,SystemMessage,ToolResponseMessage,UserMessage
The AbstractMessage class is an abstract implementation of the Message interface. It
provides a base implementation for message content, media attachments, metadata, and
message type.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe key for the message type in the metadata.protected final MessageTypeThe message type of the message.Additional options for the message to influence the response, not a generative map.protected final @Nullable StringDeprecated, for removal: This API element is subject to removal in a future version. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMessage(MessageType messageType, @Nullable String textContent, Map<String, Object> metadata) Create a new AbstractMessage with the given message type, text content, and metadata.protectedAbstractMessage(MessageType messageType, List<MessagePart> parts, Map<String, Object> metadata) Create a new AbstractMessage from an ordered list of parts. -
Method Summary
Modifier and TypeMethodDescriptionbooleanprotected static intfirstIndexOf(List<MessagePart> parts, Class<? extends MessagePart>... types) The index of the first part that is an instance of any of the given types, for use as thefallbackIndexofreplace(java.util.List<org.springframework.ai.chat.messages.part.MessagePart>, java.lang.Class<? extends org.springframework.ai.chat.messages.part.MessagePart>, java.util.List<? extends org.springframework.ai.chat.messages.part.MessagePart>, int).Get the message type of the message.Get the metadata of the message.getParts()The ordered content parts of this message.@Nullable StringgetText()Get the content of the message.inthashCode()protected static List<MessagePart>legacyParts(@Nullable String textContent) protected static voidreplace(List<MessagePart> parts, Class<? extends MessagePart> type, List<? extends MessagePart> replacements, int fallbackIndex) Removes every part of the given type and inserts the replacements where the first one was, or atfallbackIndexwhen there was none.protected <P extends MessagePart>
Stream<P>
-
Field Details
-
MESSAGE_TYPE
The key for the message type in the metadata.- See Also:
-
messageType
The message type of the message. -
textContent
Deprecated, for removal: This API element is subject to removal in a future version.since 2.1.0 in favor ofgetText(); kept so existing subclasses that read the field keep compiling. Will be removed in a future release.The text content of this message, derived from itsTextParts. -
metadata
Additional options for the message to influence the response, not a generative map.
-
-
Constructor Details
-
AbstractMessage
protected AbstractMessage(MessageType messageType, @Nullable String textContent, Map<String, Object> metadata) Create a new AbstractMessage with the given message type, text content, and metadata.- Parameters:
messageType- the message typetextContent- the text contentmetadata- the metadata
-
AbstractMessage
protected AbstractMessage(MessageType messageType, List<MessagePart> parts, Map<String, Object> metadata) Create a new AbstractMessage from an ordered list of parts. The text content is derived by joining theTextParts in the list, without a separator.- Parameters:
messageType- the message typeparts- the parts, in ordermetadata- the metadata- Since:
- 2.1.0
-
-
Method Details
-
legacyParts
-
replace
protected static void replace(List<MessagePart> parts, Class<? extends MessagePart> type, List<? extends MessagePart> replacements, int fallbackIndex) Removes every part of the given type and inserts the replacements where the first one was, or atfallbackIndexwhen there was none. Builders use it so that a setter such astext(...)on amutate()builder replaces the corresponding parts in place instead of adding to them.- Parameters:
parts- the mutable parts to updatetype- the type of the parts to replacereplacements- the parts to insert, possibly empty to only removefallbackIndex- where to insert whenpartshas no part of the type- Since:
- 2.1.0
-
firstIndexOf
@SafeVarargs protected static int firstIndexOf(List<MessagePart> parts, Class<? extends MessagePart>... types) The index of the first part that is an instance of any of the given types, for use as thefallbackIndexofreplace(java.util.List<org.springframework.ai.chat.messages.part.MessagePart>, java.lang.Class<? extends org.springframework.ai.chat.messages.part.MessagePart>, java.util.List<? extends org.springframework.ai.chat.messages.part.MessagePart>, int).- Parameters:
parts- the parts to searchtypes- the types to look for- Returns:
- the index of the first matching part, or the size of
partswhen there is none - Since:
- 2.1.0
-
select
-
getParts
The ordered content parts of this message.- Returns:
- an unmodifiable list of parts
- Since:
- 2.1.0
-
getText
Get the content of the message. -
getMetadata
Get the metadata of the message.- Specified by:
getMetadatain interfaceContent- Returns:
- the metadata of the message
-
getMessageType
Get the message type of the message.- Specified by:
getMessageTypein interfaceMessage- Returns:
- the message type of the message
-
equals
-
hashCode
public int hashCode()
-
getText(); kept so existing subclasses that read the field keep compiling.