类 AbstractBaseFormatter<TReq,TResp,TParams>

java.lang.Object
io.agentscope.core.formatter.AbstractBaseFormatter<TReq,TResp,TParams>
类型参数:
TReq - Provider-specific request message type
TResp - Provider-specific response type
TParams - Provider-specific request parameters builder type
所有已实现的接口:
Formatter<TReq,TResp,TParams>
直接已知子类:
AnthropicBaseFormatter, DashScopeChatFormatter, DashScopeMultiAgentFormatter, GeminiChatFormatter, GeminiMultiAgentFormatter, OllamaChatFormatter, OllamaMultiAgentFormatter, OpenAIBaseFormatter

public abstract class AbstractBaseFormatter<TReq,TResp,TParams> extends Object implements Formatter<TReq,TResp,TParams>
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
  • 构造器详细资料

    • AbstractBaseFormatter

      public AbstractBaseFormatter()
  • 方法详细资料

    • format

      public List<TReq> format(List<Msg> msgs)
      Format AgentScope messages to provider-specific request format.
      指定者:
      format 在接口中 Formatter<TReq,TResp,TParams>
      参数:
      msgs - List of AgentScope messages
      返回:
      List of provider-specific request messages
    • doFormat

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

      protected String extractTextContent(Msg msg)
      Extract text content from a message, filtering out ThinkingBlock.
      参数:
      msg - The message to extract text from
      返回:
      Concatenated text content (newline-separated)
    • extractTextContent

      protected String extractTextContent(ContentBlock block)
      Extract text content from a single ContentBlock.
      参数:
      block - The content block
      返回:
      Text content or empty string
    • hasMediaContent

      protected boolean hasMediaContent(Msg msg)
      Check if a message contains multimodal content (images, audio, video).
      参数:
      msg - The message to check
      返回:
      true if message contains media content
    • formatRoleLabel

      protected String formatRoleLabel(MsgRole role)
      Format role label for conversation history.
      参数:
      role - The message role
      返回:
      Formatted role label (e.g., "User", "Assistant")
    • shouldBypassHistory

      protected boolean shouldBypassHistory(Msg msg)
      Check if a message should bypass history merging in multiagent formatters. Messages with the MessageMetadataKeys.BYPASS_MULTIAGENT_HISTORY_MERGE flag set to true should be kept as separate messages rather than merged into the conversation history.
      参数:
      msg - The message to check
      返回:
      true if message should bypass history merging
    • getOptionOrDefault

      protected <T> T getOptionOrDefault(GenerateOptions options, GenerateOptions defaultOptions, Function<GenerateOptions,T> getter)
      Get an option value from options or fall back to defaultOptions.
      类型参数:
      T - The type of the option value
      参数:
      options - The primary options object
      defaultOptions - The fallback options object
      getter - Function to extract the value from GenerateOptions
      返回:
      The option value or null if not found in either options object
    • convertToolResultToString

      protected String convertToolResultToString(List<ContentBlock> output)
      Convert tool result output to string representation. Handles multimodal content by converting images/audio/video to textual references.
      参数:
      output - The tool result output blocks (can be null or empty)
      返回:
      String representation (single items directly, multiple with "- " prefix)
    • saveBase64DataToTempFile

      protected String saveBase64DataToTempFile(String mediaType, String base64Data) throws IOException
      Save base64 data to a temporary file.
      参数:
      mediaType - The MIME type (e.g., "image/png", "audio/wav")
      base64Data - The base64-encoded data (without prefix)
      返回:
      Absolute path to the temporary file
      抛出:
      IOException - If file creation or writing fails