AS AgentScopeAPI 手册 / API Reference

AbstractBaseFormatter

public abstract class AbstractBaseFormatter<TReq, TResp, TParams> implements Formatter<TReq, TResp, TParams>
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:62–339

抽象基类格式化器,在智能体、模型与前端之间传递结构化消息和事件。位于 io.agentscope.core.formatter 包,实现 Formatter<TReq, TResp, TParams>。核心成员包括 format、doFormat、extractTextContent、hasMediaContent、formatRoleLabel、shouldBypassHistory。

Abstract base formatter providing common functionality for all formatter implementations.

This class contains shared logic across all formatters including:

  • Text content extraction (with ThinkingBlock filtering)

  • Media content detection

  • Role label formatting

  • Shared ObjectMapper instance

方法摘要Method Summary

方法Method说明Description
public List<TReq> format(List<Msg> msgs)
设置格式。
Format AgentScope messages to provider-specific request format.
public List<TReq> format(List<Msg> msgs, GenerateOptions options)
执行格式相关操作。
Format AgentScope messages using request-scoped generation options.
protected abstract List<TReq> doFormat(List<Msg> msgs)
abstract
执行格式操作。
Performs the do format operation.
protected List<TReq> doFormat(List<Msg> msgs, GenerateOptions options)
执行格式操作。
Format AgentScope messages using request-scoped generation options.
protected String extractTextContent(Msg msg)
设置该参数。
Extract text content from a message, filtering out ThinkingBlock.
protected String extractTextContent(ContentBlock block)
设置该参数。
Extract text content from a single ContentBlock.
protected boolean hasMediaContent(Msg msg)
判断是否具有 `hasMediaContent`。
Check if a message contains multimodal content (images, audio, video).
protected String formatRoleLabel(MsgRole role)
用于会话历史的格式角色标签。
Format role label for conversation history.
protected boolean shouldBypassHistory(Msg msg)
设置该参数。
Check if a message should bypass history merging in multiagent formatters.
protected T T getOptionOrDefault(GenerateOptions options, GenerateOptions defaultOptions, Function<GenerateOptions, T> getter)
获取选项或默认。
Get an option value from options or fall back to defaultOptions.
protected String convertToolResultToString(List<ContentBlock> output)
转换 `convertToolResultToString`。
Convert tool result output to string representation.
protected String saveBase64DataToTempFile(String mediaType, String base64Data) throws IOException
保存 `saveBase64DataToTempFile`。
Save base64 data to a temporary file.

方法详情Method Detail

format

public List<TReq> format(List<Msg> msgs)

设置格式。

Format AgentScope messages to provider-specific request format.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>AgentScope 消息的列出。List of AgentScope messages
返回Returns List<TReq>
返回列出。List of provider-specific request messages
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:73–76

format

public List<TReq> format(List<Msg> msgs, GenerateOptions options)

执行格式相关操作。

Format AgentScope messages using request-scoped generation options.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>AgentScope 消息的列出。list of AgentScope messages
optionsGenerateOptions选项(类型 `GenerateOptions`)。request-scoped generation options; may be `null`
返回Returns List<TReq>
返回列出。list of provider-specific request messages
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:85–88

doFormat

protected abstract List<TReq> doFormat(List<Msg> msgs)
abstract

执行格式操作。

Performs the do format operation.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>消息列表(类型 `List`)。The msgs.
返回Returns List<TReq>
返回列出。Returns list.
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:90

doFormat

protected List<TReq> doFormat(List<Msg> msgs, GenerateOptions options)

执行格式操作。

Format AgentScope messages using request-scoped generation options.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>AgentScope 消息的列出。list of AgentScope messages
optionsGenerateOptions选项(类型 `GenerateOptions`)。request-scoped generation options; may be `null`
返回Returns List<TReq>
返回列出。list of provider-specific request messages
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:102–104

extractTextContent

protected String extractTextContent(Msg msg)

设置该参数。

Extract text content from a message, filtering out ThinkingBlock.

参数Parameters
名称Name类型Type说明Description
msgMsg消息(类型 `Msg`)。The message to extract text from
返回Returns String
返回字符串结果。Concatenated text content (newline-separated)
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:112–137

extractTextContent

protected String extractTextContent(ContentBlock block)

设置该参数。

Extract text content from a single ContentBlock.

参数Parameters
名称Name类型Type说明Description
blockContentBlock内容块。The content block
返回Returns String
返回字符串结果。Text content or empty string
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:145–152

hasMediaContent

protected boolean hasMediaContent(Msg msg)

判断是否具有 hasMediaContent。

Check if a message contains multimodal content (images, audio, video).

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。The message to check
返回Returns boolean
返回布尔值,表示判断结果。true if message contains media content
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:160–170

formatRoleLabel

protected String formatRoleLabel(MsgRole role)

用于会话历史的格式角色标签。

Format role label for conversation history.

参数Parameters
名称Name类型Type说明Description
roleMsgRole消息角色。The message role
返回Returns String
返回字符串结果。Formatted role label (e.g., "User", "Assistant")
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:178–185

shouldBypassHistory

protected boolean shouldBypassHistory(Msg msg)

设置该参数。

Check if a message should bypass history merging in multiagent formatters.

参数Parameters
名称Name类型Type说明Description
msgMsg消息到检查。The message to check
返回Returns boolean
返回布尔值,表示判断结果。true if message should bypass history merging
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:195–202

getOptionOrDefault

protected T T getOptionOrDefault(GenerateOptions options, GenerateOptions defaultOptions, Function<GenerateOptions, T> getter)

获取选项或默认。

Get an option value from options or fall back to defaultOptions.

参数Parameters
名称Name类型Type说明Description
optionsGenerateOptions选项(类型 `GenerateOptions`)。The primary options object
defaultOptionsGenerateOptions回退选项对象。The fallback options object
getterFunction<GenerateOptions, T>getter(类型 `Function`)。Function to extract the value from GenerateOptions
返回Returns T
返回T。The option value or null if not found in either options object
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:213–221

convertToolResultToString

protected String convertToolResultToString(List<ContentBlock> output)

转换 convertToolResultToString。

Convert tool result output to string representation.

参数Parameters
名称Name类型Type说明Description
outputList<ContentBlock>输出(类型 `List`)。The tool result output blocks (can be null or empty)
返回Returns String
返回字符串结果。String representation (single items directly, multiple with "- " prefix)
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:230–262

saveBase64DataToTempFile

protected String saveBase64DataToTempFile(String mediaType, String base64Data) throws IOException

保存 saveBase64DataToTempFile。

Save base64 data to a temporary file.

参数Parameters
名称Name类型Type说明Description
mediaTypeStringmedia 类型(类型 `String`)。The MIME type (e.g., "image/png", "audio/wav")
base64DataString基类 64 数据(类型 `String`)。The base64-encoded data (without prefix)
返回Returns String
返回字符串结果。Absolute path to the temporary file
抛出Throws
  • IOException 可能抛出 `IOException`。Thrown when the operation cannot be completed.
源码Source io/agentscope/core/formatter/AbstractBaseFormatter.java:320–338
基于官方固定版本源码静态生成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.