1010
1111import json
1212import logging
13+ import mimetypes
1314from typing import Any , Literal
1415
1516from a2a .server .agent_execution import AgentExecutor , RequestContext
@@ -41,26 +42,19 @@ class StrandsA2AExecutor(AgentExecutor):
4142 and converts Strands Agent responses to A2A protocol events.
4243 """
4344
44- # File format mappings for different content types
45- IMAGE_FORMAT_MAPPINGS = {"jpeg" : "jpeg" , "jpg" : "jpeg" , "png" : "png" , "gif" : "gif" , "webp" : "webp" }
46-
47- VIDEO_FORMAT_MAPPINGS = {
48- "mp4" : "mp4" ,
49- "mpeg" : "mpeg" ,
50- "mpg" : "mpg" ,
51- "webm" : "webm" ,
52- "mov" : "mov" ,
53- "mkv" : "mkv" ,
54- "flv" : "flv" ,
55- "wmv" : "wmv" ,
45+ # Default formats for each file type when MIME type is unavailable or unrecognized
46+ DEFAULT_FORMATS = {"document" : "txt" , "image" : "png" , "video" : "mp4" , "unknown" : "txt" }
47+
48+ # Handle special cases where format differs from extension
49+ FORMAT_MAPPINGS = {
50+ "jpeg" : "jpeg" ,
51+ "jpg" : "jpeg" ,
52+ "htm" : "html" ,
53+ "3gp" : "three_gp" ,
5654 "3gpp" : "three_gp" ,
55+ "3g2" : "three_gp"
5756 }
5857
59- DOCUMENT_FORMAT_MAPPINGS = {"pdf" : "pdf" , "csv" : "csv" , "html" : "html" , "plain" : "txt" , "markdown" : "md" }
60-
61- # Default formats for each file type when MIME type is unavailable
62- DEFAULT_FORMATS = {"document" : "txt" , "image" : "png" , "video" : "mp4" , "unknown" : "txt" }
63-
6458 def __init__ (self , agent : SAAgent ):
6559 """Initialize a StrandsA2AExecutor.
6660
@@ -210,7 +204,7 @@ def _get_file_type_from_mime_type(self, mime_type: str | None) -> Literal["docum
210204 return "unknown"
211205
212206 def _get_file_format_from_mime_type (self , mime_type : str | None , file_type : str ) -> str :
213- """Extract file format from MIME type.
207+ """Extract file format from MIME type using Python's mimetypes library .
214208
215209 Args:
216210 mime_type: The MIME type of the file
@@ -224,19 +218,16 @@ def _get_file_format_from_mime_type(self, mime_type: str | None, file_type: str)
224218
225219 mime_type = mime_type .lower ()
226220
227- # Extract format from MIME type
228- if "/" in mime_type :
229- format_part = mime_type .split ("/" )[1 ]
221+ # Use mimetypes library to find extensions for the MIME type
222+ extensions = mimetypes .guess_all_extensions (mime_type )
223+
224+ if extensions :
225+ # Get the most common extension (first one) and remove the dot
226+ extension = extensions [0 ][1 :] # Remove the leading dot
230227
231- # Handle common MIME type mappings with validation
232- if file_type == "image" :
233- return self .IMAGE_FORMAT_MAPPINGS .get (format_part , "png" )
234- elif file_type == "video" :
235- return self .VIDEO_FORMAT_MAPPINGS .get (format_part , "mp4" )
236- else : # document
237- return self .DOCUMENT_FORMAT_MAPPINGS .get (format_part , "txt" )
228+ return self .FORMAT_MAPPINGS .get (extension , extension )
238229
239- # Fallback defaults
230+ # Fallback to defaults for unknown MIME types
240231 return self .DEFAULT_FORMATS .get (file_type , "txt" )
241232
242233 def _strip_file_extension (self , file_name : str ) -> str :
0 commit comments