Class Context

Direct Known Subclasses:
CallbackContext

public class Context extends ReadonlyContext
The context passed to callbacks, tools, and workflow nodes during an invocation.

Agent and model callbacks receive a CallbackContext, and tools and tool callbacks a ToolContext; both extend this class, so a helper that serves both, or a function tool's toolContext parameter, can take a Context. requestConfirmation() requires a function call ID, which only a tool call has, while requireNodeState() and the other workflow node methods require the context of a workflow node.

  • Field Details

    • eventActions

      protected EventActions eventActions
      The event actions that record this context's changes.
  • Constructor Details

  • Method Details

    • state

      public State state()
      Returns the delta-aware state of the current context.
      Overrides:
      state in class ReadonlyContext
    • eventActions

      public EventActions eventActions()
      Returns the EventActions associated with this context.
    • actions

      public EventActions actions()
      Returns the same EventActions as eventActions().
    • eventId

      public String eventId()
      Returns the ID of the event associated with this context, or null if there is none.
    • functionCallId

      public Optional<String> functionCallId()
      Returns the ID of the function call that invoked the current tool, if any.
    • functionCallId

      public void functionCallId(@Nullable String functionCallId)
      Sets the ID of the function call that invoked the current tool, or clears it if null.
    • toolConfirmation

      public Optional<ToolConfirmation> toolConfirmation()
      Returns the confirmation of the current tool call, if any.
    • toolConfirmation

      public void toolConfirmation(@Nullable ToolConfirmation toolConfirmation)
      Sets the confirmation of the current tool call, or clears it if null.
    • listArtifacts

      public io.reactivex.rxjava3.core.Single<List<String>> listArtifacts()
      Lists the filenames of the artifacts attached to the current session.
      Returns:
      the list of artifact filenames
    • loadArtifact

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part> loadArtifact(String filename)
      Loads the latest version of an artifact from the service.
    • loadArtifact

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part> loadArtifact(String filename, int version)
      Loads a specific version of an artifact from the service.
    • saveArtifact

      public io.reactivex.rxjava3.core.Completable saveArtifact(String filename, com.google.genai.types.Part artifact)
      Saves an artifact and records it as a delta for the current session.
      Parameters:
      filename - Artifact file name.
      artifact - Artifact content to save.
      Returns:
      a Completable that completes when the artifact is saved.
      Throws:
      IllegalStateException - if the artifact service is not initialized.
    • requestConfirmation

      public void requestConfirmation(@Nullable String hint, @Nullable Object payload)
      Requests confirmation for the current function call.
      Parameters:
      hint - A hint to the user on how to confirm the tool call.
      payload - The payload used to confirm the tool call.
      Throws:
      IllegalStateException - if this context has no function call ID.
    • requestConfirmation

      public void requestConfirmation(@Nullable String hint)
      Requests confirmation for the current function call.
      Parameters:
      hint - A hint to the user on how to confirm the tool call.
      Throws:
      IllegalStateException - if this context has no function call ID.
    • requestConfirmation

      public void requestConfirmation()
      Requests confirmation for the current function call.
      Throws:
      IllegalStateException - if this context has no function call ID.
    • searchMemory

      public io.reactivex.rxjava3.core.Single<SearchMemoryResponse> searchMemory(String query)
      Searches the memory of the current user.
    • requireNodeState

      @Experimental public final NodeExecutionState requireNodeState()
      Returns the state of the workflow node run this context belongs to.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • eventAuthor

      @Experimental public final String eventAuthor()
      Returns the author of the events this workflow node emits, or empty to use the node's name.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • eventAuthor

      @Experimental public final void eventAuthor(String eventAuthor)
      Sets the author of the events this workflow node emits.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • output

      @Experimental public final Optional<Object> output()
      Returns this workflow node's output, which is the output of its useAsOutput child once that child emits one, or empty if it has none.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • routes

      @Experimental public final Optional<com.google.common.collect.ImmutableList<Route>> routes()
      Returns the routes this workflow node selected, or empty if it made no routing decision. An empty list is a decision that selects no route.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • routes

      @Experimental public final void routes(List<? extends Route> routes)
      Selects this workflow node's routes, which decide the outgoing edges to follow, replacing any earlier selection.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • resumeInputs

      @Experimental public final Map<String,@Nullable Object> resumeInputs()
      Returns the user's answers to the requests for input and long-running calls that this workflow node or its descendants made in an earlier turn, keyed by interrupt ID. A node that reruns on resume reads these answers. A null answer has a null value in the map.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • attemptCount

      @Experimental public final int attemptCount()
      Returns the number of the attempt this workflow node is on, counting from 1; each retry adds one.
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node)
      Runs node as a child of this workflow node without an input, as runNode(Node, Object, RunNodeConfig) does with the default RunNodeConfig.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput)
      Runs node as a child of this workflow node, as runNode(Node, Object, RunNodeConfig) does with the default RunNodeConfig.
      Throws:
      IllegalArgumentException - if nodeInput is a RunNodeConfig, which belongs in the three-argument overload.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput, RunNodeConfig config)
      Returns a Maybe that, when subscribed, runs node as a child of this workflow node and emits the child's output, or completes empty if it has none. Include it in this node's run and catch only failures you handle, such as DynamicNodeFailedException: when the child waits, it fails with an internal RuntimeException that must propagate to end this node's run, so that it waits too.

      It fails with DynamicNodeFailedException if the child fails; with IllegalStateException if this is not a workflow node's context, if this node's Node.rerunOnResume() is false, or if config delegates an output that this node already has or has delegated; and with IllegalArgumentException if the child's name cannot form a node path.