Class AgentCompilerLoader

java.lang.Object
com.google.adk.web.AgentCompilerLoader

@Service public class AgentCompilerLoader extends Object
Dynamically compiles and loads ADK BaseAgent implementations from source files. It orchestrates the discovery of the ADK core JAR, compilation of agent sources using the Eclipse JDT (ECJ) compiler, and loading of compiled agents into isolated classloaders. Agents are identified by a public static field named ROOT_AGENT. Supports agent organization in subdirectories or as individual .java files.
  • Constructor Details

    • AgentCompilerLoader

      public AgentCompilerLoader(AgentLoadingProperties properties)
      Initializes the loader with agent configuration and proactively attempts to locate the ADK core JAR. This JAR, containing BaseAgent and other core ADK types, is crucial for agent compilation. The location strategy (see locateAndPrepareAdkCoreJar()) includes handling directly available JARs and extracting nested JARs (e.g., in Spring Boot fat JARs) to ensure it's available for the compilation classpath.
      Parameters:
      properties - Configuration detailing agent source locations and compilation settings.
  • Method Details

    • loadAgents

      public Map<String, BaseAgent> loadAgents() throws IOException
      Discovers, compiles, and loads agents from the configured source directory.

      The process for each potential "agent unit" (a subdirectory or a root .java file):

      1. Collects .java source files.
      2. Compiles these sources using ECJ (see compileSourcesWithECJ(List, Path)) into a temporary, unit-specific output directory. This directory is cleaned up on JVM exit.
      3. Creates a dedicated URLClassLoader for the compiled unit, isolating its classes.
      4. Scans compiled classes for a public static field ROOT_AGENT assignable to BaseAgent. This field serves as the designated entry point for an agent.
      5. Instantiates and stores the BaseAgent if found, keyed by its name.
      This approach allows for dynamic addition of agents without pre-compilation and supports independent classpaths per agent unit if needed (though current implementation uses a shared parent classloader).
      Returns:
      A map of successfully loaded agent names to their BaseAgent instances. Returns an empty map if the source directory isn't configured or no agents are found.
      Throws:
      IOException - If an I/O error occurs (e.g., creating temp directories, reading sources).