Skip to content

Commit a0ffb8b

Browse files
author
jer
committed
chore(a2a): PR comments
1 parent 36482f3 commit a0ffb8b

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

src/strands/multiagent/a2a/executor.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import json
1212
import logging
13+
import mimetypes
1314
from typing import Any, Literal
1415

1516
from 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:

tests/strands/multiagent/a2a/test_executor.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,23 @@ def test_classify_file_type():
4242

4343

4444
def test_get_file_format_from_mime_type():
45-
"""Test file format extraction from MIME type."""
45+
"""Test file format extraction from MIME type using mimetypes library."""
4646
executor = StrandsA2AExecutor(MagicMock())
47-
48-
# Test image formats
4947
assert executor._get_file_format_from_mime_type("image/jpeg", "image") == "jpeg"
5048
assert executor._get_file_format_from_mime_type("image/png", "image") == "png"
51-
assert executor._get_file_format_from_mime_type("image/unknown", "image") == "png" # fallback
49+
assert executor._get_file_format_from_mime_type("image/unknown", "image") == "png"
5250

5351
# Test video formats
5452
assert executor._get_file_format_from_mime_type("video/mp4", "video") == "mp4"
5553
assert executor._get_file_format_from_mime_type("video/3gpp", "video") == "three_gp"
56-
assert executor._get_file_format_from_mime_type("video/unknown", "video") == "mp4" # fallback
54+
assert executor._get_file_format_from_mime_type("video/unknown", "video") == "mp4"
5755

5856
# Test document formats
5957
assert executor._get_file_format_from_mime_type("application/pdf", "document") == "pdf"
6058
assert executor._get_file_format_from_mime_type("text/plain", "document") == "txt"
61-
assert executor._get_file_format_from_mime_type("text/markdown", "document") == "md"
62-
assert executor._get_file_format_from_mime_type("application/unknown", "document") == "txt" # fallback
59+
markdown_result = executor._get_file_format_from_mime_type("text/markdown", "document")
60+
assert markdown_result in ["markdown", "md"]
61+
assert executor._get_file_format_from_mime_type("application/unknown", "document") == "txt"
6362

6463
# Test None/empty cases
6564
assert executor._get_file_format_from_mime_type(None, "image") == "png"

0 commit comments

Comments
 (0)