Skip to content

Commit f8421df

Browse files
refactored artifact and message; added parts utils
1 parent d585635 commit f8421df

File tree

4 files changed

+77
-50
lines changed

4 files changed

+77
-50
lines changed

src/a2a/utils/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
new_artifact,
55
new_data_artifact,
66
new_text_artifact,
7+
get_artifact_text,
8+
)
9+
from a2a.utils.message import (
10+
new_agent_text_message,
11+
new_agent_parts_message,
12+
get_message_text,
13+
)
14+
from a2a.utils.parts import (
15+
get_data_parts,
16+
get_file_parts,
17+
get_text_parts,
718
)
819
from a2a.utils.constants import (
920
AGENT_CARD_WELL_KNOWN_PATH,
@@ -17,14 +28,6 @@
1728
build_text_artifact,
1829
create_task_obj,
1930
)
20-
from a2a.utils.message import (
21-
get_data_parts,
22-
get_file_parts,
23-
get_message_text,
24-
get_text_parts,
25-
new_agent_parts_message,
26-
new_agent_text_message,
27-
)
2831
from a2a.utils.task import (
2932
completed_task,
3033
new_task,
@@ -41,6 +44,7 @@
4144
'build_text_artifact',
4245
'completed_task',
4346
'create_task_obj',
47+
'get_artifact_text',
4448
'get_data_parts',
4549
'get_file_parts',
4650
'get_message_text',

src/a2a/utils/artifact.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66

77
from a2a.types import Artifact, DataPart, Part, TextPart
8+
from a2a.utils import get_text_parts
89

910

1011
def new_artifact(
@@ -70,3 +71,16 @@ def new_data_artifact(
7071
name,
7172
description,
7273
)
74+
75+
76+
def get_artifact_text(artifact: Artifact, delimiter: str = '\n') -> str:
77+
"""Extracts and joins all text content from an Artifact's parts.
78+
79+
Args:
80+
artifact: The `Artifact` object.
81+
delimiter: The string to use when joining text from multiple TextParts.
82+
83+
Returns:
84+
A single string containing all text content, or an empty string if no text parts are found.
85+
"""
86+
return delimiter.join(get_text_parts(artifact.parts))

src/a2a/utils/message.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
import uuid
44

5-
from typing import Any
6-
75
from a2a.types import (
8-
DataPart,
9-
FilePart,
10-
FileWithBytes,
11-
FileWithUri,
126
Message,
137
Part,
148
Role,
159
TextPart,
1610
)
11+
from a2a.utils import get_text_parts
1712

1813

1914
def new_agent_text_message(
@@ -64,42 +59,6 @@ def new_agent_parts_message(
6459
)
6560

6661

67-
def get_text_parts(parts: list[Part]) -> list[str]:
68-
"""Extracts text content from all TextPart objects in a list of Parts.
69-
70-
Args:
71-
parts: A list of `Part` objects.
72-
73-
Returns:
74-
A list of strings containing the text content from any `TextPart` objects found.
75-
"""
76-
return [part.root.text for part in parts if isinstance(part.root, TextPart)]
77-
78-
79-
def get_data_parts(parts: list[Part]) -> list[dict[str, Any]]:
80-
"""Extracts dictionary data from all DataPart objects in a list of Parts.
81-
82-
Args:
83-
parts: A list of `Part` objects.
84-
85-
Returns:
86-
A list of dictionaries containing the data from any `DataPart` objects found.
87-
"""
88-
return [part.root.data for part in parts if isinstance(part.root, DataPart)]
89-
90-
91-
def get_file_parts(parts: list[Part]) -> list[FileWithBytes | FileWithUri]:
92-
"""Extracts file data from all FilePart objects in a list of Parts.
93-
94-
Args:
95-
parts: A list of `Part` objects.
96-
97-
Returns:
98-
A list of `FileWithBytes` or `FileWithUri` objects containing the file data from any `FilePart` objects found.
99-
"""
100-
return [part.root.file for part in parts if isinstance(part.root, FilePart)]
101-
102-
10362
def get_message_text(message: Message, delimiter: str = '\n') -> str:
10463
"""Extracts and joins all text content from a Message's parts.
10564

src/a2a/utils/parts.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Utility functions for creating and handling A2A Parts objects."""
2+
3+
import uuid
4+
5+
from typing import Any
6+
7+
from a2a.types import (
8+
DataPart,
9+
FilePart,
10+
FileWithBytes,
11+
FileWithUri,
12+
Part,
13+
TextPart,
14+
)
15+
16+
17+
def get_text_parts(parts: list[Part]) -> list[str]:
18+
"""Extracts text content from all TextPart objects in a list of Parts.
19+
20+
Args:
21+
parts: A list of `Part` objects.
22+
23+
Returns:
24+
A list of strings containing the text content from any `TextPart` objects found.
25+
"""
26+
return [part.root.text for part in parts if isinstance(part.root, TextPart)]
27+
28+
29+
def get_data_parts(parts: list[Part]) -> list[dict[str, Any]]:
30+
"""Extracts dictionary data from all DataPart objects in a list of Parts.
31+
32+
Args:
33+
parts: A list of `Part` objects.
34+
35+
Returns:
36+
A list of dictionaries containing the data from any `DataPart` objects found.
37+
"""
38+
return [part.root.data for part in parts if isinstance(part.root, DataPart)]
39+
40+
41+
def get_file_parts(parts: list[Part]) -> list[FileWithBytes | FileWithUri]:
42+
"""Extracts file data from all FilePart objects in a list of Parts.
43+
44+
Args:
45+
parts: A list of `Part` objects.
46+
47+
Returns:
48+
A list of `FileWithBytes` or `FileWithUri` objects containing the file data from any `FilePart` objects found.
49+
"""
50+
return [part.root.file for part in parts if isinstance(part.root, FilePart)]

0 commit comments

Comments
 (0)