File tree Expand file tree Collapse file tree 4 files changed +77
-50
lines changed
Expand file tree Collapse file tree 4 files changed +77
-50
lines changed Original file line number Diff line number Diff line change 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)
819from a2a .utils .constants import (
920 AGENT_CARD_WELL_KNOWN_PATH ,
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- )
2831from a2a .utils .task import (
2932 completed_task ,
3033 new_task ,
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' ,
Original file line number Diff line number Diff line change 55from typing import Any
66
77from a2a .types import Artifact , DataPart , Part , TextPart
8+ from a2a .utils import get_text_parts
89
910
1011def 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 ))
Original file line number Diff line number Diff line change 22
33import uuid
44
5- from typing import Any
6-
75from 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
1914def 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-
10362def get_message_text (message : Message , delimiter : str = '\n ' ) -> str :
10463 """Extracts and joins all text content from a Message's parts.
10564
Original file line number Diff line number Diff line change 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 )]
You can’t perform that action at this time.
0 commit comments