1818
1919import com .fasterxml .jackson .core .type .TypeReference ;
2020import com .fasterxml .jackson .databind .ObjectMapper ;
21- import com .fasterxml . jackson . datatype . jdk8 . Jdk8Module ;
21+ import com .google . adk . JsonBaseModel ;
2222import com .google .adk .agents .InvocationContext ;
2323import com .google .common .collect .ImmutableList ;
2424import com .google .common .collect .ImmutableMap ;
4242
4343/** FunctionTool implements a customized function calling tool. */
4444public class FunctionTool extends BaseTool {
45- private static final ObjectMapper OBJECT_MAPPER =
46- new ObjectMapper ().registerModule (new Jdk8Module ());
45+
4746 private static final Logger logger = LoggerFactory .getLogger (FunctionTool .class );
4847
4948 @ Nullable private final Object instance ;
5049 private final Method func ;
5150 private final FunctionDeclaration funcDeclaration ;
51+ private final ObjectMapper objectMapper ;
5252
5353 public static FunctionTool create (Object instance , Method func ) {
5454 if (!areParametersAnnotatedWithSchema (func ) && wasCompiledWithDefaultParameterNames (func )) {
@@ -123,6 +123,11 @@ private static boolean wasCompiledWithDefaultParameterNames(Method func) {
123123 }
124124
125125 protected FunctionTool (@ Nullable Object instance , Method func , boolean isLongRunning ) {
126+ this (instance , func , isLongRunning , JsonBaseModel .getMapper ());
127+ }
128+
129+ protected FunctionTool (
130+ @ Nullable Object instance , Method func , boolean isLongRunning , ObjectMapper objectMapper ) {
126131 super (
127132 func .isAnnotationPresent (Annotations .Schema .class )
128133 && !func .getAnnotation (Annotations .Schema .class ).name ().isEmpty ()
@@ -144,6 +149,7 @@ protected FunctionTool(@Nullable Object instance, Method func, boolean isLongRun
144149 this .funcDeclaration =
145150 FunctionCallingUtils .buildFunctionDeclaration (
146151 this .func , ImmutableList .of ("toolContext" , "inputStream" ));
152+ this .objectMapper = objectMapper ;
147153 }
148154
149155 @ Override
@@ -225,7 +231,7 @@ private Maybe<Map<String, Object>> call(Map<String, Object> args, ToolContext to
225231 continue ;
226232 }
227233 } else if (argValue instanceof Map ) {
228- arguments [i ] = OBJECT_MAPPER .convertValue (argValue , paramType );
234+ arguments [i ] = objectMapper .convertValue (argValue , paramType );
229235 continue ;
230236 }
231237 arguments [i ] = castValue (argValue , paramType );
@@ -236,16 +242,14 @@ private Maybe<Map<String, Object>> call(Map<String, Object> args, ToolContext to
236242 } else if (result instanceof Maybe ) {
237243 return ((Maybe <?>) result )
238244 .map (
239- data ->
240- OBJECT_MAPPER .convertValue (data , new TypeReference <Map <String , Object >>() {}));
245+ data -> objectMapper .convertValue (data , new TypeReference <Map <String , Object >>() {}));
241246 } else if (result instanceof Single ) {
242247 return ((Single <?>) result )
243- .map (
244- data -> OBJECT_MAPPER .convertValue (data , new TypeReference <Map <String , Object >>() {}))
248+ .map (data -> objectMapper .convertValue (data , new TypeReference <Map <String , Object >>() {}))
245249 .toMaybe ();
246250 } else {
247251 return Maybe .just (
248- OBJECT_MAPPER .convertValue (result , new TypeReference <Map <String , Object >>() {}));
252+ objectMapper .convertValue (result , new TypeReference <Map <String , Object >>() {}));
249253 }
250254 }
251255
@@ -291,7 +295,7 @@ public Flowable<Map<String, Object>> callLive(
291295 continue ;
292296 }
293297 } else if (argValue instanceof Map ) {
294- arguments [i ] = OBJECT_MAPPER .convertValue (argValue , paramType );
298+ arguments [i ] = objectMapper .convertValue (argValue , paramType );
295299 continue ;
296300 }
297301 arguments [i ] = castValue (argValue , paramType );
@@ -305,7 +309,7 @@ public Flowable<Map<String, Object>> callLive(
305309 }
306310 }
307311
308- private static List <Object > createList (List <Object > values , Class <?> type ) {
312+ private List <Object > createList (List <Object > values , Class <?> type ) {
309313 List <Object > list = new ArrayList <>();
310314 // List of parameterized type is not supported.
311315 if (type == null ) {
@@ -321,13 +325,13 @@ private static List<Object> createList(List<Object> values, Class<?> type) {
321325 || cls == String .class ) {
322326 list .add (castValue (value , cls ));
323327 } else {
324- list .add (OBJECT_MAPPER .convertValue (value , type ));
328+ list .add (objectMapper .convertValue (value , type ));
325329 }
326330 }
327331 return list ;
328332 }
329333
330- private static Object castValue (Object value , Class <?> type ) {
334+ private Object castValue (Object value , Class <?> type ) {
331335 if (type .equals (Integer .class ) || type .equals (int .class )) {
332336 if (value instanceof Integer ) {
333337 return value ;
@@ -372,6 +376,6 @@ private static Object castValue(Object value, Class<?> type) {
372376 return value ;
373377 }
374378 }
375- return OBJECT_MAPPER .convertValue (value , type );
379+ return objectMapper .convertValue (value , type );
376380 }
377381}
0 commit comments