AS AgentScopeAPI 手册 / API Reference

MessageUtils

public final class MessageUtils
源码Source 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)
static

执行 extractRecentToolCalls 方法。

Extract tool calls from the most recent assistant message in the message list.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息到搜索的列出。The list of messages to search
agentNameString智能体名称(类型 `String`)。The name of the agent to match (must match the message sender's name)
返回Returns List<ToolUseBlock>
返回列出。List of tool use blocks from the last matching assistant message, or empty list if none found
源码Source io/agentscope/core/util/MessageUtils.java:56–73

extractPendingToolCalls

public static List<ToolUseBlock> extractPendingToolCalls(List<Msg> messages, String agentName)
static

执行 extractPendingToolCalls 方法。

Extracts only pending tool calls from the most recent assistant message.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>会话消息。the conversation messages
agentNameString智能体名称(类型 `String`)。the agent name to match
返回Returns List<ToolUseBlock>
返回列出。tool calls that do not yet have corresponding results
源码Source io/agentscope/core/util/MessageUtils.java:82–92

lastMessageByRole

public static Msg lastMessageByRole(List<Msg> messages, MsgRole role)
static

执行最后消息通过角色相关操作。

Finds the most recent message with the requested role.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列出到搜索。the message list to search
roleMsgRole角色(类型 `MsgRole`)。the role to match
返回Returns Msg
返回消息。the last matching message, or `null` if none exists
源码Source io/agentscope/core/util/MessageUtils.java:101–112

lastAssistantMessage

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

设置该参数。

Finds the most recent assistant message.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>消息列出到搜索。the message list to search
返回Returns Msg
返回消息。the last assistant message, or `null` if none exists
源码Source io/agentscope/core/util/MessageUtils.java:120–122

replaceLastMessageByRole

public static boolean replaceLastMessageByRole(List<Msg> messages, MsgRole role, Msg replacement)
static

执行 replaceLastMessageByRole 方法。

Replaces the most recent message with the requested role.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>可变消息列出到更新。the mutable message list to update
roleMsgRole角色(类型 `MsgRole`)。the role to match
replacementMsgreplacement(类型 `Msg`)。the replacement message
返回Returns boolean
返回布尔值,表示判断结果。`true` if a message was replaced
源码Source io/agentscope/core/util/MessageUtils.java:132–145

pendingToolUseIds

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.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>会话消息。the conversation messages
返回Returns Set<String>
返回设置。the pending tool-call IDs
源码Source io/agentscope/core/util/MessageUtils.java:154–171

hasToolCalls

public static boolean hasToolCalls(Msg message)
static

判断是否具有工具调用。

Returns whether the message contains any tool-call blocks.

参数Parameters
名称Name类型Type说明Description
messageMsg消息(类型 `Msg`)。the message to inspect
返回Returns boolean
返回布尔值,表示判断结果。`true` if at least one tool call exists
源码Source io/agentscope/core/util/MessageUtils.java:179–181

prependSystemMessage

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.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>基类消息。the base messages
systemMessageMsg系统消息(类型 `Msg`)。the system message to prepend
返回Returns List<Msg>
返回列出。the combined message list
源码Source io/agentscope/core/util/MessageUtils.java:190–200

allToolCallsDenied

public static boolean allToolCallsDenied(List<Msg> messages, List<ToolUseBlock> toolCalls)
static

执行所有工具调用已拒绝相关操作。

Returns whether every supplied tool call has a DENIED result in the conversation.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>会话消息。the conversation messages
toolCallsList<ToolUseBlock>工具调用(类型 `List`)。the tool calls to inspect
返回Returns boolean
返回布尔值,表示判断结果。`true` if all tool calls are denied
源码Source io/agentscope/core/util/MessageUtils.java:209–229

replaceToolUseBlocks

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.

参数Parameters
名称Name类型Type说明Description
messagesList<Msg>可变会话消息。the mutable conversation messages
replacementsMap<String, ToolUseBlock>replacements(类型 `Map`)。tool-call IDs mapped to replacement blocks
返回Returns boolean
返回布尔值,表示判断结果。`true` if any block was replaced
源码Source io/agentscope/core/util/MessageUtils.java:239–274
基于官方固定版本源码静态生成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.