11package com .google .adk .a2a .converters ;
22
3+ import static java .util .stream .Collectors .toCollection ;
4+
35import com .fasterxml .jackson .core .JsonProcessingException ;
46import com .fasterxml .jackson .databind .ObjectMapper ;
7+ import com .google .common .collect .ImmutableMap ;
58import com .google .genai .types .Blob ;
69import com .google .genai .types .FileData ;
710import com .google .genai .types .FunctionCall ;
1316import io .a2a .spec .FileWithBytes ;
1417import io .a2a .spec .FileWithUri ;
1518import io .a2a .spec .TextPart ;
19+ import java .util .ArrayList ;
1620import java .util .Base64 ;
1721import java .util .HashMap ;
22+ import java .util .List ;
1823import java .util .Map ;
1924import java .util .Optional ;
2025import org .slf4j .Logger ;
@@ -60,6 +65,13 @@ public static Optional<com.google.genai.types.Part> toGenaiPart(io.a2a.spec.Part
6065 return Optional .empty ();
6166 }
6267
68+ public static List <com .google .genai .types .Part > toGenaiParts (List <io .a2a .spec .Part <?>> a2aParts ) {
69+ return a2aParts .stream ()
70+ .map (PartConverter ::toGenaiPart )
71+ .flatMap (Optional ::stream )
72+ .collect (toCollection (ArrayList ::new ));
73+ }
74+
6375 /**
6476 * Convert a Google GenAI Part to an A2A Part.
6577 *
@@ -129,8 +141,8 @@ private static Optional<com.google.genai.types.Part> convertDataPartToGenAiPart(
129141
130142 String metadataType = metadata .getOrDefault (A2A_DATA_PART_METADATA_TYPE_KEY , "" ).toString ();
131143
132- if (data .containsKey ("name" ) && data .containsKey ("args" )
133- || A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL .equals (metadataType )) {
144+ if (( data .containsKey ("name" ) && data .containsKey ("args" ) )
145+ || metadataType .equals (A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL )) {
134146 String functionName = String .valueOf (data .getOrDefault ("name" , "" ));
135147 String functionId = String .valueOf (data .getOrDefault ("id" , "" ));
136148 Map <String , Object > args = coerceToMap (data .get ("args" ));
@@ -141,8 +153,8 @@ private static Optional<com.google.genai.types.Part> convertDataPartToGenAiPart(
141153 .build ());
142154 }
143155
144- if (data .containsKey ("name" ) && data .containsKey ("response" )
145- || A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE .equals (metadataType )) {
156+ if (( data .containsKey ("name" ) && data .containsKey ("response" ) )
157+ || metadataType .equals (A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE )) {
146158 String functionName = String .valueOf (data .getOrDefault ("name" , "" ));
147159 String functionId = String .valueOf (data .getOrDefault ("id" , "" ));
148160 Map <String , Object > response = coerceToMap (data .get ("response" ));
@@ -175,10 +187,10 @@ private static Optional<DataPart> createDataPartFromFunctionCall(FunctionCall fu
175187 Map <String , Object > data = new HashMap <>();
176188 data .put ("name" , functionCall .name ().orElse ("" ));
177189 data .put ("id" , functionCall .id ().orElse ("" ));
178- data .put ("args" , functionCall .args ().orElse (Map .of ()));
190+ data .put ("args" , functionCall .args ().orElse (ImmutableMap .of ()));
179191
180- Map <String , Object > metadata =
181- Map .of (A2A_DATA_PART_METADATA_TYPE_KEY , A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL );
192+ ImmutableMap <String , Object > metadata =
193+ ImmutableMap .of (A2A_DATA_PART_METADATA_TYPE_KEY , A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL );
182194
183195 return Optional .of (new DataPart (data , metadata ));
184196 }
@@ -194,10 +206,11 @@ private static Optional<DataPart> createDataPartFromFunctionResponse(
194206 Map <String , Object > data = new HashMap <>();
195207 data .put ("name" , functionResponse .name ().orElse ("" ));
196208 data .put ("id" , functionResponse .id ().orElse ("" ));
197- data .put ("response" , functionResponse .response ().orElse (Map .of ()));
209+ data .put ("response" , functionResponse .response ().orElse (ImmutableMap .of ()));
198210
199- Map <String , Object > metadata =
200- Map .of (A2A_DATA_PART_METADATA_TYPE_KEY , A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE );
211+ ImmutableMap <String , Object > metadata =
212+ ImmutableMap .of (
213+ A2A_DATA_PART_METADATA_TYPE_KEY , A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE );
201214
202215 return Optional .of (new DataPart (data , metadata ));
203216 }
0 commit comments