Class PluginManager

java.lang.Object
com.google.adk.plugins.BasePlugin
com.google.adk.plugins.PluginManager
All Implemented Interfaces:
Plugin

public class PluginManager extends BasePlugin
Manages the registration and execution of plugins.

The PluginManager is an internal class that orchestrates the invocation of plugin callbacks at key points in the SDK's execution lifecycle.

  • Constructor Details

    • PluginManager

      public PluginManager(List<? extends Plugin> plugins)
    • PluginManager

      public PluginManager()
  • Method Details

    • registerPlugin

      public void registerPlugin(Plugin plugin)
      Registers a new plugin.
      Parameters:
      plugin - The plugin instance to register.
      Throws:
      IllegalArgumentException - If a plugin with the same name is already registered.
    • getPlugin

      public Optional<Plugin> getPlugin(String pluginName)
      Retrieves a registered plugin by its name.
      Parameters:
      pluginName - The name of the plugin to retrieve.
      Returns:
      The plugin instance if found, otherwise Optional.empty().
    • runOnUserMessageCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> runOnUserMessageCallback(InvocationContext invocationContext, com.google.genai.types.Content userMessage)
    • onUserMessageCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> onUserMessageCallback(InvocationContext invocationContext, com.google.genai.types.Content userMessage)
      Description copied from interface: Plugin
      Callback executed when a user message is received before an invocation starts.
      Parameters:
      invocationContext - The context for the entire invocation.
      userMessage - The message content input by user.
      Returns:
      An optional Content to replace the user message. Returning Empty to proceed normally.
    • runBeforeRunCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> runBeforeRunCallback(InvocationContext invocationContext)
    • beforeRunCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeRunCallback(InvocationContext invocationContext)
      Description copied from interface: Plugin
      Callback executed before the ADK runner runs.
      Parameters:
      invocationContext - The context for the entire invocation.
      Returns:
      An optional Content to halt execution. Returning Empty to proceed normally.
    • runAfterRunCallback

      public io.reactivex.rxjava3.core.Completable runAfterRunCallback(InvocationContext invocationContext)
    • afterRunCallback

      public io.reactivex.rxjava3.core.Completable afterRunCallback(InvocationContext invocationContext)
      Description copied from interface: Plugin
      Callback executed after an ADK runner run has completed.
      Parameters:
      invocationContext - The context for the entire invocation.
    • close

      public io.reactivex.rxjava3.core.Completable close()
      Description copied from interface: Plugin
      Method executed when the runner is closed.

      This method is used for cleanup tasks such as closing network connections or releasing resources.

    • runOnEventCallback

      public io.reactivex.rxjava3.core.Maybe<Event> runOnEventCallback(InvocationContext invocationContext, Event event)
    • onEventCallback

      public io.reactivex.rxjava3.core.Maybe<Event> onEventCallback(InvocationContext invocationContext, Event event)
      Description copied from interface: Plugin
      Callback executed after an event is yielded from runner.
      Parameters:
      invocationContext - The context for the entire invocation.
      event - The event raised by the runner.
      Returns:
      An optional Event to modify or replace the response. Returning Empty to proceed normally.
    • runBeforeAgentCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> runBeforeAgentCallback(BaseAgent agent, CallbackContext callbackContext)
    • beforeAgentCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext)
      Description copied from interface: Plugin
      Callback executed before an agent's primary logic is invoked.
      Parameters:
      agent - The agent that is about to run.
      callbackContext - The context for the agent invocation.
      Returns:
      An optional Content object to bypass the agent's execution. Returning Empty to proceed normally.
    • runAfterAgentCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> runAfterAgentCallback(BaseAgent agent, CallbackContext callbackContext)
    • afterAgentCallback

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> afterAgentCallback(BaseAgent agent, CallbackContext callbackContext)
      Description copied from interface: Plugin
      Callback executed after an agent's primary logic has completed.
      Parameters:
      agent - The agent that has just run.
      callbackContext - The context for the agent invocation.
      Returns:
      An optional Content object to replace the agent's original result. Returning Empty to use the original result.
    • runBeforeModelCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> runBeforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest)
    • beforeModelCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> beforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest)
      Description copied from interface: Plugin
      Callback executed before a request is sent to the model.
      Parameters:
      callbackContext - The context for the current agent call.
      llmRequest - The mutable request builder, allowing modification of the request before it is sent to the model.
      Returns:
      An optional LlmResponse to trigger an early exit. Returning Empty to proceed normally.
    • runAfterModelCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> runAfterModelCallback(CallbackContext callbackContext, LlmResponse llmResponse)
    • afterModelCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> afterModelCallback(CallbackContext callbackContext, LlmResponse llmResponse)
      Description copied from interface: Plugin
      Callback executed after a response is received from the model.
      Parameters:
      callbackContext - The context for the current agent call.
      llmResponse - The response object received from the model.
      Returns:
      An optional LlmResponse to modify or replace the response. Returning Empty to use the original response.
    • runOnModelErrorCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> runOnModelErrorCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error)
    • onModelErrorCallback

      public io.reactivex.rxjava3.core.Maybe<LlmResponse> onModelErrorCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error)
      Description copied from interface: Plugin
      Callback executed when a model call encounters an error.
      Parameters:
      callbackContext - The context for the current agent call.
      llmRequest - The mutable request builder for the request that failed.
      error - The exception that was raised.
      Returns:
      An optional LlmResponse to use instead of propagating the error. Returning Empty to allow the original error to be raised.
    • runBeforeToolCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> runBeforeToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext)
    • beforeToolCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> beforeToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext)
      Description copied from interface: Plugin
      Callback executed before a tool is called.
      Parameters:
      tool - The tool instance that is about to be executed.
      toolArgs - The dictionary of arguments to be used for invoking the tool.
      toolContext - The context specific to the tool execution.
      Returns:
      An optional Map to stop the tool execution and return this response immediately. Returning Empty to proceed normally.
    • runAfterToolCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> runAfterToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Map<String,Object> result)
    • afterToolCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> afterToolCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Map<String,Object> result)
      Description copied from interface: Plugin
      Callback executed after a tool has been called.
      Parameters:
      tool - The tool instance that has just been executed.
      toolArgs - The original arguments that were passed to the tool.
      toolContext - The context specific to the tool execution.
      result - The dictionary returned by the tool invocation.
      Returns:
      An optional Map to replace the original result from the tool. Returning Empty to use the original result.
    • runOnToolErrorCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> runOnToolErrorCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Throwable error)
    • onToolErrorCallback

      public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> onToolErrorCallback(BaseTool tool, Map<String,Object> toolArgs, ToolContext toolContext, Throwable error)
      Description copied from interface: Plugin
      Callback executed when a tool call encounters an error.
      Parameters:
      tool - The tool instance that encountered an error.
      toolArgs - The arguments that were passed to the tool.
      toolContext - The context specific to the tool execution.
      error - The exception that was raised during tool execution.
      Returns:
      An optional Map to be used as the tool response instead of propagating the error. Returning Empty to allow the original error to be raised.