Class InvocationContext

java.lang.Object
com.google.adk.agents.InvocationContext

public class InvocationContext extends Object
The context for an agent invocation.
  • Constructor Details

  • Method Details

    • builder

      public static InvocationContext.Builder builder()
      Returns a new InvocationContext.Builder for creating InvocationContext instances.
    • toBuilder

      public InvocationContext.Builder toBuilder()
      Returns a InvocationContext.Builder initialized with the values of this instance.
    • sessionService

      public BaseSessionService sessionService()
      Returns the session service for managing session state.
    • artifactService

      public BaseArtifactService artifactService()
      Returns the artifact service for persisting artifacts.
    • memoryService

      public BaseMemoryService memoryService()
      Returns the memory service for accessing agent memory.
    • pluginManager

      public Plugin pluginManager()
      Returns the plugin manager for accessing tools and plugins.
    • activeStreamingTools

      public Map<String, ActiveStreamingTool> activeStreamingTools()
      Returns a map of tool call IDs to active streaming tools for the current invocation.
    • liveRequestQueue

      public Optional<LiveRequestQueue> liveRequestQueue()
      Returns the queue for managing live requests, if available for this invocation.
    • invocationId

      public String invocationId()
      Returns the unique ID for this invocation.
    • branch

      public void branch(@Nullable String branch)
      Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation history.
    • branch

      public Optional<String> branch()
      Returns the branch ID for the current invocation, if one is set. A branch represents a fork in the conversation history.
    • agent

      public BaseAgent agent()
      Returns the agent being invoked.
    • session

      public Session session()
      Returns the session associated with this invocation.
    • eventsOnCurrentBranch

      public com.google.common.collect.ImmutableList<Event> eventsOnCurrentBranch()
      Returns a snapshot of the session's events, keeping only those on the branch this invocation is running on.

      The rule is author-asymmetric on purpose, so a confirmation the user answered on a sub-branch stays visible while a descendant agent's own events do not.

    • userContent

      public Optional<com.google.genai.types.Content> userContent()
      Returns the user content that triggered this invocation, if any.
    • runConfig

      public RunConfig runConfig()
      Returns the configuration for the current agent run.
    • callbackContextData

      public Map<String,Object> callbackContextData()
      Returns a map for storing temporary context data that can be shared between different parts of the invocation (e.g., before/on/after model callbacks).
    • endInvocation

      public boolean endInvocation()
      Returns whether this invocation should be ended, e.g., due to reaching a terminal state or error.
    • setEndInvocation

      public void setEndInvocation(boolean endInvocation)
      Sets whether this invocation should be ended.
    • appName

      public String appName()
      Returns the application name associated with the session.
    • userId

      public String userId()
      Returns the user ID associated with the session.
    • newInvocationContextId

      public static String newInvocationContextId()
      Generates a new unique ID for an invocation context.
    • incrementLlmCallsCount

      public void incrementLlmCallsCount() throws LlmCallsLimitExceededException
      Increments the count of LLM calls made during this invocation and throws an exception if the limit defined in RunConfig is exceeded.
      Throws:
      LlmCallsLimitExceededException - if the call limit is exceeded
    • eventsCompactionConfig

      public Optional<EventsCompactionConfig> eventsCompactionConfig()
      Returns the events compaction configuration for the current agent run.
    • contextCacheConfig

      public Optional<ContextCacheConfig> contextCacheConfig()
      Returns the context cache configuration for the current agent run.
    • isResumable

      @Experimental public boolean isResumable()
      Returns whether the current invocation is resumable.
    • isLegacyResumability

      @Deprecated public boolean isLegacyResumability()
      Deprecated.
      Reports the deprecated plain-text continuation shim and goes away with it; use isResumable().
      Returns whether the invocation runs the legacy resumption flow: the behavior isResumable() had before durable checkpoints existed. It is a separate flow, not a weaker isResumable(), so a caller wanting either has to ask for both.
    • agentStates

      @Experimental public Map<String, Map<String,Object>> agentStates()
      Returns an unmodifiable view of the per-agent resumability checkpoint states for this invocation, keyed by agent name. The backing map is shared by reference across derived contexts within the invocation; mutate it only through setAgentState(String, Map, boolean).
    • endOfAgents

      @Experimental public Map<String,Boolean> endOfAgents()
      Returns an unmodifiable view of the per-agent end-of-agent flags for this invocation, keyed by agent name.
    • populateInvocationAgentStates

      @Experimental public void populateInvocationAgentStates()
      Rehydrates agentStates() and endOfAgents() from the current invocation's history when this invocation is resumable. For each event carrying agent-state information, sets the authoring agent's checkpoint; for a non-workflow author that already produced content, seeds an empty state so it is treated as mid-run.
    • events

      @Experimental public com.google.common.collect.ImmutableList<Event> events(boolean currentInvocation, boolean currentBranch)
      Returns the current session's events, optionally filtered to the current invocation and/or the current branch. Reads the in-memory Session.events() list, which BaseSessionService.appendEvent(Session, Event) keeps in sync. A null-branch event is visible on any branch.
      Parameters:
      currentInvocation - whether to filter to events from this invocation.
      currentBranch - whether to filter to events on this branch (or with no branch).
    • shouldPauseInvocation

      @Experimental public boolean shouldPauseInvocation(Event event)
      Returns whether to pause the invocation right after this event: it is resumable and the event carries a long-running function call, including a synthetic adk_request_confirmation HITL request. Pausing, unlike ending, leaves the invocation resumable.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object