MessageUtils
public final class MessageUtils
io/agentscope/core/util/MessageUtils.java:39–275消息 Utils,为框架提供通用类型与基础能力。位于 io.agentscope.core.util 包。核心成员包括 extractRecentToolCalls、extractPendingToolCalls、lastMessageByRole、lastAssistantMessage、replaceLastMessageByRole、pendingToolUseIds。
Utility methods for message processing and extraction.
This class provides common operations for working with message lists and extracting information from conversation history.
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static List<ToolUseBlock> extractRecentToolCalls(List<Msg> messages, String agentName)static | 执行 `extractRecentToolCalls` 方法。 Extract tool calls from the most recent assistant message in the message list. |
public static List<ToolUseBlock> extractPendingToolCalls(List<Msg> messages, String agentName)static | 执行 `extractPendingToolCalls` 方法。 Extracts only pending tool calls from the most recent assistant message. |
public static Msg lastMessageByRole(List<Msg> messages, MsgRole role)static | 执行最后消息通过角色相关操作。 Finds the most recent message with the requested role. |
public static Msg lastAssistantMessage(List<Msg> messages)static | 设置该参数。 Finds the most recent assistant message. |
public static boolean replaceLastMessageByRole(List<Msg> messages, MsgRole role, Msg replacement)static | 执行 `replaceLastMessageByRole` 方法。 Replaces the most recent message with the requested role. |
public static Set<String> pendingToolUseIds(List<Msg> messages)static | 设置该参数。 Returns the IDs of tool calls in the last assistant message that do not yet have a matching
tool result anywhere in the conversation. |
public static boolean hasToolCalls(Msg message)static | 判断是否具有工具调用。 Returns whether the message contains any tool-call blocks. |
public static List<Msg> prependSystemMessage(List<Msg> messages, Msg systemMessage)static | 执行 `prependSystemMessage` 方法。 Prepends a system message to the supplied message list without mutating the original list. |
public static boolean allToolCallsDenied(List<Msg> messages, List<ToolUseBlock> toolCalls)static | 执行所有工具调用已拒绝相关操作。 Returns whether every supplied tool call has a DENIED result in the conversation. |
public static boolean replaceToolUseBlocks(List<Msg> messages, Map<String, ToolUseBlock> replacements)static | 执行 `replaceToolUseBlocks` 方法。 Replaces tool-call blocks in the last assistant message when their IDs match the supplied
replacements. |
方法详情Method Detail
extractRecentToolCalls
public static List<ToolUseBlock> extractRecentToolCalls(List<Msg> messages, String agentName)
执行 extractRecentToolCalls 方法。
Extract tool calls from the most recent assistant message in the message list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息到搜索的列出。The list of messages to search |
agentName | String | 智能体名称(类型 `String`)。The name of the agent to match (must match the message sender's name) |
List<ToolUseBlock>io/agentscope/core/util/MessageUtils.java:56–73extractPendingToolCalls
public static List<ToolUseBlock> extractPendingToolCalls(List<Msg> messages, String agentName)
执行 extractPendingToolCalls 方法。
Extracts only pending tool calls from the most recent assistant message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 会话消息。the conversation messages |
agentName | String | 智能体名称(类型 `String`)。the agent name to match |
List<ToolUseBlock>io/agentscope/core/util/MessageUtils.java:82–92lastMessageByRole
public static Msg lastMessageByRole(List<Msg> messages, MsgRole role)
执行最后消息通过角色相关操作。
Finds the most recent message with the requested role.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列出到搜索。the message list to search |
role | MsgRole | 角色(类型 `MsgRole`)。the role to match |
Msgio/agentscope/core/util/MessageUtils.java:101–112lastAssistantMessage
public static Msg lastAssistantMessage(List<Msg> messages)
设置该参数。
Finds the most recent assistant message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 消息列出到搜索。the message list to search |
Msgio/agentscope/core/util/MessageUtils.java:120–122replaceLastMessageByRole
public static boolean replaceLastMessageByRole(List<Msg> messages, MsgRole role, Msg replacement)
执行 replaceLastMessageByRole 方法。
Replaces the most recent message with the requested role.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 可变消息列出到更新。the mutable message list to update |
role | MsgRole | 角色(类型 `MsgRole`)。the role to match |
replacement | Msg | replacement(类型 `Msg`)。the replacement message |
booleanio/agentscope/core/util/MessageUtils.java:132–145pendingToolUseIds
public static Set<String> pendingToolUseIds(List<Msg> messages)
设置该参数。
Returns the IDs of tool calls in the last assistant message that do not yet have a matching tool result anywhere in the conversation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 会话消息。the conversation messages |
Set<String>io/agentscope/core/util/MessageUtils.java:154–171hasToolCalls
public static boolean hasToolCalls(Msg message)
判断是否具有工具调用。
Returns whether the message contains any tool-call blocks.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
message | Msg | 消息(类型 `Msg`)。the message to inspect |
booleanio/agentscope/core/util/MessageUtils.java:179–181prependSystemMessage
public static List<Msg> prependSystemMessage(List<Msg> messages, Msg systemMessage)
执行 prependSystemMessage 方法。
Prepends a system message to the supplied message list without mutating the original list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 基类消息。the base messages |
systemMessage | Msg | 系统消息(类型 `Msg`)。the system message to prepend |
List<Msg>io/agentscope/core/util/MessageUtils.java:190–200allToolCallsDenied
public static boolean allToolCallsDenied(List<Msg> messages, List<ToolUseBlock> toolCalls)
执行所有工具调用已拒绝相关操作。
Returns whether every supplied tool call has a DENIED result in the conversation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 会话消息。the conversation messages |
toolCalls | List<ToolUseBlock> | 工具调用(类型 `List`)。the tool calls to inspect |
booleanio/agentscope/core/util/MessageUtils.java:209–229replaceToolUseBlocks
public static boolean replaceToolUseBlocks(List<Msg> messages, Map<String, ToolUseBlock> replacements)
执行 replaceToolUseBlocks 方法。
Replaces tool-call blocks in the last assistant message when their IDs match the supplied replacements.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
messages | List<Msg> | 可变会话消息。the mutable conversation messages |
replacements | Map<String, ToolUseBlock> | replacements(类型 `Map`)。tool-call IDs mapped to replacement blocks |
booleanio/agentscope/core/util/MessageUtils.java:239–274