Class AgentFnContext

java.lang.Object
com.google.genkit.ai.agent.AgentFnContext

public final class AgentFnContext extends Object
Context handed to an AgentFn for one invocation.

Provides access to a stream-chunk emitter and an abort signal that allows the agent to detect cancellation. Stream emission of custom-patch / artifacts is wired in Task 4.3; here the sendChunk consumer is available for the agent to emit model chunks.

It also carries the run's ActionContext (via context()) so the agent function can read the request-scoped user context (e.g. {"auth": {...}}) and forward it into the generate call so tools observe it.

  • Constructor Details

    • AgentFnContext

      public AgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal)
      Constructs an AgentFnContext without an ActionContext.
      Parameters:
      sendChunk - consumer that emits a streaming chunk to the caller; may be a no-op
      abortSignal - shared flag; set to true by the runtime to request cancellation
    • AgentFnContext

      public AgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal, ActionContext context)
      Constructs an AgentFnContext.
      Parameters:
      sendChunk - consumer that emits a streaming chunk to the caller; may be a no-op
      abortSignal - shared flag; set to true by the runtime to request cancellation
      context - the run's ActionContext (carries the user context); may be null
    • AgentFnContext

      public AgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal, ActionContext context, ToolResume resume)
      Constructs an AgentFnContext with resume data.
      Parameters:
      sendChunk - consumer that emits a streaming chunk to the caller; may be a no-op
      abortSignal - shared flag; set to true by the runtime to request cancellation
      context - the run's ActionContext (carries the user context); may be null
      resume - the current turn's tool-resume data, or null if this is not a resume turn
  • Method Details

    • sendChunk

      public Consumer<AgentStreamChunk> sendChunk()
      Returns the stream-chunk emitter for this invocation.
      Returns:
      the AgentStreamChunk consumer (never null)
    • abortSignal

      public AtomicBoolean abortSignal()
      Returns the abort signal for this invocation.
      Returns:
      an AtomicBoolean that is true when the caller has requested cancellation
    • isAborted

      public boolean isAborted()
      Convenience method to check whether the abort signal has been set.
      Returns:
      true if the caller has requested cancellation
    • context

      public ActionContext context()
      Returns the run's ActionContext, which carries the request-scoped user context.
      Returns:
      the ActionContext for this invocation, or null if none was provided
    • resume

      public ToolResume resume()
      Returns this turn's tool-resume data, i.e. the respond/restart parts passed to AgentChat.resume(...) (or the raw AgentInput.resume field for lower-level callers).
      Returns:
      the ToolResume for this turn, or null if this is not a resume turn