Package com.google.genkit.ai.agent
Class AgentFnContext
java.lang.Object
com.google.genkit.ai.agent.AgentFnContext
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 Summary
ConstructorsConstructorDescriptionAgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal) Constructs an AgentFnContext without an ActionContext.AgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal, ActionContext context) Constructs an AgentFnContext.AgentFnContext(Consumer<AgentStreamChunk> sendChunk, AtomicBoolean abortSignal, ActionContext context, ToolResume resume) Constructs an AgentFnContext with resume data. -
Method Summary
Modifier and TypeMethodDescriptionReturns the abort signal for this invocation.context()Returns the run'sActionContext, which carries the request-scoped user context.booleanConvenience method to check whether the abort signal has been set.resume()Returns this turn's tool-resume data, i.e. therespond/restartparts passed toAgentChat.resume(...)(or the rawAgentInput.resumefield for lower-level callers).Returns the stream-chunk emitter for this invocation.
-
Constructor Details
-
AgentFnContext
Constructs an AgentFnContext without an ActionContext.- Parameters:
sendChunk- consumer that emits a streaming chunk to the caller; may be a no-opabortSignal- shared flag; set totrueby 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-opabortSignal- shared flag; set totrueby the runtime to request cancellationcontext- 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-opabortSignal- shared flag; set totrueby the runtime to request cancellationcontext- the run's ActionContext (carries the user context); may be nullresume- the current turn's tool-resume data, ornullif this is not a resume turn
-
-
Method Details
-
sendChunk
Returns the stream-chunk emitter for this invocation.- Returns:
- the
AgentStreamChunkconsumer (never null)
-
abortSignal
Returns the abort signal for this invocation.- Returns:
- an
AtomicBooleanthat istruewhen the caller has requested cancellation
-
isAborted
public boolean isAborted()Convenience method to check whether the abort signal has been set.- Returns:
trueif the caller has requested cancellation
-
context
Returns the run'sActionContext, which carries the request-scoped user context.- Returns:
- the ActionContext for this invocation, or null if none was provided
-
resume
Returns this turn's tool-resume data, i.e. therespond/restartparts passed toAgentChat.resume(...)(or the rawAgentInput.resumefield for lower-level callers).- Returns:
- the
ToolResumefor this turn, ornullif this is not a resume turn
-