AS AgentScopeAPI 手册 / API Reference

MsgUtils

public class MsgUtils
源码Source 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> to List<Map<String, Object>>

  • Deserialization: Converts List<Map<String, Object>> back to List<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)
static

执行消息列表映射。

Serializes a map of message lists to a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
objectObject对象(类型 `Object`)。the object to serialize, expected to be `Map >`
返回Returns Object
返回对象。the serialized map as `Map >>`, or the original object if it's not a Map
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:77–91

serializeMsgList

public static Object serializeMsgList(Object messages)
static

执行消息列表。

Serializes a list of messages to a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
messagesObject消息列表(类型 `Object`)。the object to serialize, expected to be `List `
返回Returns Object
返回对象。the serialized list as `List >`, or the original object if it's not a List
抛出Throws
  • RuntimeException 可能抛出 `RuntimeException`。if serialization fails for any message
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:108–130

deserializeToMsgList

public static Object deserializeToMsgList(Object data)
static

执行到消息列表。

Deserializes a list of messages from a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
dataObject数据(类型 `Object`)。the data to deserialize, expected to be `List >`
返回Returns Object
返回对象。a new `ArrayList` containing the deserialized `List `, or the original object if it's not a List
抛出Throws
  • RuntimeException 可能抛出 `RuntimeException`。if deserialization fails for any message
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:148–172

deserializeToMsgListMap

public static Object deserializeToMsgListMap(Object data)
static

执行到消息列表映射。

Deserializes a map of message lists from a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
dataObject数据(类型 `Object`)。the data to deserialize, expected to be `Map >>`
返回Returns Object
返回对象。a new `HashMap` containing the deserialized `Map >`, or the original object if it's not a Map
抛出Throws
  • RuntimeException 可能抛出 `RuntimeException`。if deserialization fails for any message list
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:190–203

replaceMsg

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.

参数Parameters
名称Name类型Type说明Description
rawMessagesList<Msg>原始消息(类型 `List`)。the list of messages to modify (must not be null)
startIndexint启动索引(类型 `int`)。the start index of the range to replace (inclusive, must be >= 0)
endIndexint结束索引(类型 `int`)。the end index of the range to replace (inclusive, must be >= startIndex)
newMsgMsg新建消息(类型 `Msg`)。the new message to insert at startIndex (must not be null)
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:226–249

isToolMessage

public static boolean isToolMessage(Msg msg)
static

判断是否工具消息。

Check if a message is a tool-related message (tool use or tool result).

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message contains tool use or tool result blocks, or has TOOL role
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:257–268

isToolUseMessage

public static boolean isToolUseMessage(Msg msg)
static

检查if 消息是工具使用消息(ASSISTANT 使用 ToolUseBlock)。

Check if a message is a tool use message (ASSISTANT with ToolUseBlock).

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message is an ASSISTANT message containing ToolUseBlock
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:276–281

isToolResultMessage

public static boolean isToolResultMessage(Msg msg)
static

判断是否工具结果消息。

Check if a message is a tool result message (TOOL role or contains ToolResultBlock).

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message is a TOOL role message or contains ToolResultBlock
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:289–297

isCompressedMessage

public static boolean isCompressedMessage(Msg msg)
static

判断是否 isCompressedMessage。

Check if a message is a compressed message.

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message is a compressed message, false otherwise
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:318–331

isFinalAssistantResponse

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).

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message is an ASSISTANT role message that does not contain tool calls
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:342–366

containsPlanRelatedToolCall

public static boolean containsPlanRelatedToolCall(Msg msg)
static

设置该参数。

Check if a message contains plan-related tool calls.

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。the message to check
返回Returns boolean
返回布尔值,表示判断结果。true if the message contains plan-related tool calls
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:404–420

isPlanRelatedTool

public static boolean isPlanRelatedTool(String toolName)
static

判断是否 isPlanRelatedTool。

Check if a tool name is plan-related.

参数Parameters
名称Name类型Type说明Description
toolNameString工具名称到检查。the tool name to check
返回Returns boolean
返回布尔值,表示判断结果。true if the tool name is plan-related
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:428–430

filterPlanRelatedToolCalls

public static List<Msg> filterPlanRelatedToolCalls(List<Msg> messages)
static

过滤 filterPlanRelatedToolCalls。

Filter out messages containing plan-related tool calls and their corresponding tool results.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息到过滤。the messages to filter
返回Returns List<Msg>
返回列出。filtered messages without plan-related tool calls
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:442–507

serializeCompressionEventList

public static Object serializeCompressionEventList(Object object)
static

执行压缩事件列表。

Serializes a list of compression events to a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
objectObject对象(类型 `Object`)。the object to serialize, expected to be `List `
返回Returns Object
返回对象。the serialized list as `List >`, or the original object if it's not a List
抛出Throws
  • RuntimeException 可能抛出 `RuntimeException`。if serialization fails
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:520–543

deserializeToCompressionEventList

public static Object deserializeToCompressionEventList(Object data)
static

执行到压缩事件列表。

Deserializes a list of compression events from a JSON-compatible format.

参数Parameters
名称Name类型Type说明Description
dataObject数据(类型 `Object`)。the data to deserialize, expected to be `List >`
返回Returns Object
返回对象。a new `ArrayList` containing the deserialized `List `, or the original object if it's not a List
抛出Throws
  • RuntimeException 可能抛出 `RuntimeException`。if deserialization fails
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:556–607

calculateMessageCharCount

public static int calculateMessageCharCount(Msg msg)
static

设置该参数。

Calculates the total character count of a message, including all content blocks.

参数Parameters
名称Name类型Type说明Description
msgMsg消息(类型 `Msg`)。the message to calculate character count for
返回Returns int
返回数值结果。the total character count
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:622–684

calculateMessagesCharCount

public static int calculateMessagesCharCount(List<Msg> messages)
static

设置该参数。

Calculates the total character count of a list of messages.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列表(类型 `List`)。the list of messages to calculate character count for
返回Returns int
返回数值结果。the total character count across all messages
源码Source io/agentscope/core/memory/autocontext/MsgUtils.java:692–701
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.