Class ToolConverter

java.lang.Object
com.google.adk.models.springai.ToolConverter

public class ToolConverter extends Object
Converts between ADK and Spring AI tool/function formats in both directions.

This converter handles the translation between ADK's BaseTool/FunctionDeclaration format and Spring AI ToolCallback representations, including schema conversion and ToolContext propagation.

  • Field Details

  • Constructor Details

    • ToolConverter

      public ToolConverter()
  • Method Details

    • createToolRegistry

      public Map<String,ToolConverter.ToolMetadata> createToolRegistry(Map<String,BaseTool> tools)
      Creates a tool registry from ADK tools for internal tracking.

      This method provides a way to track available tools, though Spring AI tool calling integration will be enhanced in subsequent iterations.

      Parameters:
      tools - Map of ADK tools to process
      Returns:
      Map of tool names to their metadata
    • convertSchemaToSpringAi

      public Map<String,Object> convertSchemaToSpringAi(com.google.genai.types.Schema schema)
      Converts ADK Schema to Spring AI compatible parameter schema.

      Maps the type, description, properties, required, enum and items keywords (properties values and items recursively). Other JSON-Schema keywords (e.g. nullable, anyOf, format) are not carried over yet.

      Parameters:
      schema - The ADK schema to convert
      Returns:
      A Map representing the Spring AI compatible schema
    • convertSchemaFromSpringAi

      public com.google.genai.types.Schema convertSchemaFromSpringAi(Map<String,Object> springAiSchema)
      Converts a Spring AI parameter schema map to an ADK Schema.

      Maps type, description, properties, required, enum, and items recursively, defaulting missing array items to STRING and narrowing required to declared properties. As in Python ADK, a $ref is resolved as a JSON pointer into the root schema, or else by its last segment in the root $defs or definitions; keys next to the $ref override the referenced schema's, and a circular reference becomes an object schema.

      Parameters:
      springAiSchema - The Spring AI schema map to convert
      Returns:
      The converted ADK Schema
    • convertSchemaFromSpringAi

      public com.google.genai.types.Schema convertSchemaFromSpringAi(@Nullable String jsonSchema)
      Parses a Spring AI JSON Schema string into an ADK Schema.
      Parameters:
      jsonSchema - The JSON Schema string (e.g. from ToolDefinition.inputSchema())
      Returns:
      The converted ADK Schema, or an empty object schema if jsonSchema is null or blank
      Throws:
      IllegalArgumentException - if jsonSchema is not valid JSON
    • convertToSpringAiTool

      public Optional<org.springframework.ai.tool.ToolCallback> convertToSpringAiTool(BaseTool tool)
      Converts a single ADK BaseTool to a Spring AI ToolCallback.
      Parameters:
      tool - The ADK tool to convert
      Returns:
      An Optional containing the converted ToolCallback, or empty if the tool has no declaration
    • convertToSpringAiTools

      public List<org.springframework.ai.tool.ToolCallback> convertToSpringAiTools(Map<String,BaseTool> tools)
      Converts ADK tools to Spring AI ToolCallback format for tool calling, skipping tools without a declaration.
      Parameters:
      tools - Map of ADK tools to convert
      Returns:
      List of Spring AI ToolCallback objects
    • convertToAdkTool

      public BaseTool convertToAdkTool(org.springframework.ai.tool.ToolCallback toolCallback)
      Converts a Spring AI ToolCallback to an ADK BaseTool.
      Parameters:
      toolCallback - The Spring AI tool callback to convert
      Returns:
      A SpringAiTool wrapping the callback
    • convertToAdkTools

      public List<BaseTool> convertToAdkTools(Collection<? extends org.springframework.ai.tool.ToolCallback> toolCallbacks)
      Converts a collection of Spring AI ToolCallbacks to a list of ADK BaseTools.
      Parameters:
      toolCallbacks - Collection of Spring AI tool callbacks to convert
      Returns:
      List of ADK BaseTool instances
    • convertToAdkToolset

      public SpringAiToolset convertToAdkToolset(org.springframework.ai.tool.ToolCallbackProvider toolCallbackProvider)
      Converts a Spring AI ToolCallbackProvider to an ADK BaseToolset that queries the provider each time an agent lists its tools, so the toolset follows a provider whose tools change.
      Parameters:
      toolCallbackProvider - Provider of Spring AI tool callbacks
      Returns:
      A SpringAiToolset backed by toolCallbackProvider