Class DashScopeMediaConverter

java.lang.Object
io.agentscope.extensions.model.dashscope.formatter.DashScopeMediaConverter

public class DashScopeMediaConverter extends Object
Handles media content conversion for DashScope API. Converts ImageBlock/VideoBlock/AudioBlock to DashScope-compatible formats.

DashScope uses file:// protocol for local files, which differs from OpenAI's base64 approach.

  • Constructor Details

    • DashScopeMediaConverter

      public DashScopeMediaConverter()
  • Method Details

    • convertImageBlockToUrl

      public String convertImageBlockToUrl(io.agentscope.core.message.ImageBlock imageBlock) throws Exception
      Convert ImageBlock to URL string for DashScope API.

      Embeds local images as data URLs because the HTTP API cannot read local files.

      Handles:

      • Local files → Base64 data URL with the image media type
      • Remote URLs → Direct URL (e.g., https://example.com/image.png)
      • Base64 sources → Data URL (e.g., data:image/png;base64,...)
      Parameters:
      imageBlock - The image block to convert
      Returns:
      URL string for DashScope API
      Throws:
      Exception - If conversion fails
    • convertImageBlockToContentPart

      public DashScopeContentPart convertImageBlockToContentPart(io.agentscope.core.message.ImageBlock imageBlock) throws Exception
      Convert ImageBlock to DashScopeContentPart.
      Parameters:
      imageBlock - The image block to convert
      Returns:
      DashScopeContentPart for the image
      Throws:
      Exception - If conversion fails
    • convertVideoBlockToUrl

      public String convertVideoBlockToUrl(io.agentscope.core.message.VideoBlock videoBlock) throws Exception
      Convert VideoBlock to URL string for DashScope API.

      Uses file:// protocol for local files for consistent behavior (same as ImageBlock handling).

      Handles:

      • Local files → file:// protocol URL (e.g., file:///absolute/path/video.mp4)
      • Remote URLs → Direct URL (e.g., https://example.com/video.mp4)
      • Base64 sources → Data URL (e.g., data:video/mp4;base64,...)
      Parameters:
      videoBlock - The video block to convert
      Returns:
      URL string for DashScope API
      Throws:
      Exception - If conversion fails
    • convertVideoBlockToContentPart

      public DashScopeContentPart convertVideoBlockToContentPart(io.agentscope.core.message.VideoBlock videoBlock) throws Exception
      Convert VideoBlock to DashScopeContentPart.
      Parameters:
      videoBlock - The video block to convert
      Returns:
      DashScopeContentPart for the video
      Throws:
      Exception - If conversion fails
    • convertAudioBlockToUrl

      public String convertAudioBlockToUrl(io.agentscope.core.message.AudioBlock audioBlock) throws Exception
      Convert AudioBlock to URL string for DashScope API.

      Uses file:// protocol for local files for consistent behavior (same as ImageBlock handling).

      Handles:

      • Local files → file:// protocol URL (e.g., file:///absolute/path/audio.mp3)
      • Remote URLs → Direct URL (e.g., https://example.com/audio.mp3)
      • Base64 sources → Data URL (e.g., data:audio/wav;base64,...)
      Parameters:
      audioBlock - The audio block to convert
      Returns:
      URL string for DashScope API
      Throws:
      Exception - If conversion fails
    • convertAudioBlockToContentPart

      public DashScopeContentPart convertAudioBlockToContentPart(io.agentscope.core.message.AudioBlock audioBlock) throws Exception
      Convert AudioBlock to DashScopeContentPart.
      Parameters:
      audioBlock - The audio block to convert
      Returns:
      DashScopeContentPart for the audio
      Throws:
      Exception - If conversion fails
    • convertDataBlockToContentPart

      public DashScopeContentPart convertDataBlockToContentPart(io.agentscope.core.message.DataBlock dataBlock) throws Exception
      Convert DataBlock to DashScopeContentPart by resolving the MIME type and routing to the appropriate image / audio / video slot.

      MIME type resolution order:

      1. Base64Source.mediaType — always explicit
      2. URLSource.mimeType — caller-supplied hint for extension-less URLs
      3. MediaUtils.determineMediaType(url) — extension-based inference
      Parameters:
      dataBlock - The data block to convert
      Returns:
      DashScopeContentPart for the resolved media type
      Throws:
      Exception - If conversion fails or MIME type cannot be resolved