MsgUtils
public class MsgUtils
io/agentscope/core/memory/autocontext/MsgUtils.java:54–702消息 Utils,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.memory.autocontext 包。核心成员包括 serializeMsgListMap、serializeMsgList、deserializeToMsgList、deserializeToMsgListMap、replaceMsg、isToolMessage。
Utility class for message serialization and deserialization operations.
This class provides methods for converting between Msg objects and JSON-compatible
formats (Map structures) for state persistence. It handles polymorphic types like ContentBlock
and its subtypes (TextBlock, ToolUseBlock, ToolResultBlock, etc.) using Jackson ObjectMapper.
Key Features:
-
Serialization: Converts
List<Msg>toList<Map<String, Object>> -
Deserialization: Converts
List<Map<String, Object>>back toList<Msg> -
Map serialization: Handles
Map<String, List<Msg>>for offload context storage -
Message manipulation: Provides utility methods for replacing message ranges
Usage:
These methods are primarily used by AutoContextMemory for state persistence through
the session API. The serialized format preserves all ContentBlock
type information using Jackson's polymorphic type handling.
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static Object serializeMsgListMap(Object object)static | 执行消息列表映射。 Serializes a map of message lists to a JSON-compatible format. |
public static Object serializeMsgList(Object messages)static | 执行消息列表。 Serializes a list of messages to a JSON-compatible format. |
public static Object deserializeToMsgList(Object data)static | 执行到消息列表。 Deserializes a list of messages from a JSON-compatible format. |
public static Object deserializeToMsgListMap(Object data)static | 执行到消息列表映射。 Deserializes a map of message lists from a JSON-compatible format. |
public static void replaceMsg(List<Msg> rawMessages, int startIndex, int endIndex, Msg newMsg)static | 执行 `replaceMsg` 方法。 Replaces a range of messages in a list with a single new message. |
public static boolean isToolMessage(Msg msg)static | 判断是否工具消息。 Check if a message is a tool-related message (tool use or tool result). |
public static boolean isToolUseMessage(Msg msg)static | 检查if 消息是工具使用消息(ASSISTANT 使用 ToolUseBlock)。 Check if a message is a tool use message (ASSISTANT with ToolUseBlock). |
public static boolean isToolResultMessage(Msg msg)static | 判断是否工具结果消息。 Check if a message is a tool result message (TOOL role or contains ToolResultBlock). |
public static boolean isCompressedMessage(Msg msg)static | 判断是否 `isCompressedMessage`。 Check if a message is a compressed message. |
public static boolean isFinalAssistantResponse(Msg msg)static | 检查if ASSISTANT 消息是最终响应到用户(不工具调用)。 Check if an ASSISTANT message is a final response to the user (not a tool call). |
public static boolean containsPlanRelatedToolCall(Msg msg)static | 设置该参数。 Check if a message contains plan-related tool calls. |
public static boolean isPlanRelatedTool(String toolName)static | 判断是否 `isPlanRelatedTool`。 Check if a tool name is plan-related. |
public static List<Msg> filterPlanRelatedToolCalls(List<Msg> messages)static | 过滤 `filterPlanRelatedToolCalls`。 Filter out messages containing plan-related tool calls and their corresponding tool results. |
public static Object serializeCompressionEventList(Object object)static | 执行压缩事件列表。 Serializes a list of compression events to a JSON-compatible format. |
public static Object deserializeToCompressionEventList(Object data)static | 执行到压缩事件列表。 Deserializes a list of compression events from a JSON-compatible format. |
public static int calculateMessageCharCount(Msg msg)static | 设置该参数。 Calculates the total character count of a message, including all content blocks. |
public static int calculateMessagesCharCount(List<Msg> messages)static | 设置该参数。 Calculates the total character count of a list of messages. |
方法详情Method Detail
serializeMsgListMap
public static Object serializeMsgListMap(Object object)
执行消息列表映射。
Serializes a map of message lists to a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
object | Object | 对象(类型 `Object`)。the object to serialize, expected to be `Map >` |
Objectio/agentscope/core/memory/autocontext/MsgUtils.java:77–91serializeMsgList
public static Object serializeMsgList(Object messages)
执行消息列表。
Serializes a list of messages to a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | Object | 消息列表(类型 `Object`)。the object to serialize, expected to be `List ` |
ObjectRuntimeException可能抛出 `RuntimeException`。if serialization fails for any message
io/agentscope/core/memory/autocontext/MsgUtils.java:108–130deserializeToMsgList
public static Object deserializeToMsgList(Object data)
执行到消息列表。
Deserializes a list of messages from a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
data | Object | 数据(类型 `Object`)。the data to deserialize, expected to be `List >` |
ObjectRuntimeException可能抛出 `RuntimeException`。if deserialization fails for any message
io/agentscope/core/memory/autocontext/MsgUtils.java:148–172deserializeToMsgListMap
public static Object deserializeToMsgListMap(Object data)
执行到消息列表映射。
Deserializes a map of message lists from a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
data | Object | 数据(类型 `Object`)。the data to deserialize, expected to be `Map >>` |
ObjectRuntimeException可能抛出 `RuntimeException`。if deserialization fails for any message list
io/agentscope/core/memory/autocontext/MsgUtils.java:190–203replaceMsg
public static void replaceMsg(List<Msg> rawMessages, int startIndex, int endIndex, Msg newMsg)
执行 replaceMsg 方法。
Replaces a range of messages in a list with a single new message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rawMessages | List<Msg> | 原始消息(类型 `List`)。the list of messages to modify (must not be null) |
startIndex | int | 启动索引(类型 `int`)。the start index of the range to replace (inclusive, must be >= 0) |
endIndex | int | 结束索引(类型 `int`)。the end index of the range to replace (inclusive, must be >= startIndex) |
newMsg | Msg | 新建消息(类型 `Msg`)。the new message to insert at startIndex (must not be null) |
io/agentscope/core/memory/autocontext/MsgUtils.java:226–249isToolMessage
public static boolean isToolMessage(Msg msg)
判断是否工具消息。
Check if a message is a tool-related message (tool use or tool result).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到检查。the message to check |
booleanio/agentscope/core/memory/autocontext/MsgUtils.java:257–268isToolUseMessage
public static boolean isToolUseMessage(Msg msg)
检查if 消息是工具使用消息(ASSISTANT 使用 ToolUseBlock)。
Check if a message is a tool use message (ASSISTANT with ToolUseBlock).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到检查。the message to check |
booleanio/agentscope/core/memory/autocontext/MsgUtils.java:276–281isToolResultMessage
public static boolean isToolResultMessage(Msg msg)
判断是否工具结果消息。
Check if a message is a tool result message (TOOL role or contains ToolResultBlock).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到检查。the message to check |
booleanio/agentscope/core/memory/autocontext/MsgUtils.java:289–297isCompressedMessage
public static boolean isCompressedMessage(Msg msg)
判断是否 isCompressedMessage。
Check if a message is a compressed message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到检查。the message to check |
booleanio/agentscope/core/memory/autocontext/MsgUtils.java:318–331isFinalAssistantResponse
public static boolean isFinalAssistantResponse(Msg msg)
检查if ASSISTANT 消息是最终响应到用户(不工具调用)。
Check if an ASSISTANT message is a final response to the user (not a tool call).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到检查。the message to check |
booleanio/agentscope/core/memory/autocontext/MsgUtils.java:342–366serializeCompressionEventList
public static Object serializeCompressionEventList(Object object)
执行压缩事件列表。
Serializes a list of compression events to a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
object | Object | 对象(类型 `Object`)。the object to serialize, expected to be `List ` |
ObjectRuntimeException可能抛出 `RuntimeException`。if serialization fails
io/agentscope/core/memory/autocontext/MsgUtils.java:520–543deserializeToCompressionEventList
public static Object deserializeToCompressionEventList(Object data)
执行到压缩事件列表。
Deserializes a list of compression events from a JSON-compatible format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
data | Object | 数据(类型 `Object`)。the data to deserialize, expected to be `List >` |
ObjectRuntimeException可能抛出 `RuntimeException`。if deserialization fails
io/agentscope/core/memory/autocontext/MsgUtils.java:556–607calculateMessageCharCount
public static int calculateMessageCharCount(Msg msg)
设置该参数。
Calculates the total character count of a message, including all content blocks.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息(类型 `Msg`)。the message to calculate character count for |
intio/agentscope/core/memory/autocontext/MsgUtils.java:622–684calculateMessagesCharCount
public static int calculateMessagesCharCount(List<Msg> messages)
设置该参数。
Calculates the total character count of a list of messages.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列表(类型 `List`)。the list of messages to calculate character count for |
intio/agentscope/core/memory/autocontext/MsgUtils.java:692–701