Class Context
- Direct Known Subclasses:
CallbackContext
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 Summary
FieldsModifier and TypeFieldDescriptionprotected EventActionsThe event actions that record this context's changes.Fields inherited from class ReadonlyContext
invocationContext -
Constructor Summary
ConstructorsConstructorDescriptionContext(InvocationContext invocationContext, NodeExecutionState nodeState) Creates the context of the workflow node run whose state isnodeState. -
Method Summary
Modifier and TypeMethodDescriptionactions()Returns the sameEventActionsaseventActions().final intReturns the number of the attempt this workflow node is on, counting from 1; each retry adds one.final voidSendseventfrom this workflow node'sFunctionNodebody, after the events of earlier calls and before the output.final voidemit(com.google.genai.types.Content message) Sendsmessageas an event without an output, asemit(Event)does.Returns theEventActionsassociated with this context.final StringReturns the author of the events this workflow node emits, or empty to use the node's name.final voideventAuthor(String eventAuthor) Sets the author of the events this workflow node emits.eventId()Returns the ID of the event associated with this context, or null if there is none.Returns the ID of the function call that invoked the current tool, if any.voidfunctionCallId(@Nullable String functionCallId) Sets the ID of the function call that invoked the current tool, or clears it if null.Lists the filenames of the artifacts attached to the current session.io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part> loadArtifact(String filename) Loads the latest version of an artifact from the service.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.output()Returns this workflow node's output, which is the output of itsuseAsOutputchild once that child emits one, or empty if it has none.voidRequests confirmation for the current function call.voidrequestConfirmation(@Nullable String hint) Requests confirmation for the current function call.voidrequestConfirmation(@Nullable String hint, @Nullable Object payload) Requests confirmation for the current function call.final NodeExecutionStateReturns the state of the workflow node run this context belongs to.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.routes()Returns the routes this workflow node selected, or empty if it made no routing decision.final voidSelects this workflow node's routes, which decide the outgoing edges to follow, replacing any earlier selection.final io.reactivex.rxjava3.core.Maybe<Object> Runsnodeas a child of this workflow node without an input, asrunNode(Node, Object, RunNodeConfig)does with the defaultRunNodeConfig.final io.reactivex.rxjava3.core.Maybe<Object> Runsnodeas a child of this workflow node, asrunNode(Node, Object, RunNodeConfig)does with the defaultRunNodeConfig.final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput, RunNodeConfig config) Returns aMaybethat, when subscribed, runsnodeas a child of this workflow node and emits the child's output, or completes empty if it has none.io.reactivex.rxjava3.core.CompletablesaveArtifact(String filename, com.google.genai.types.Part artifact) Saves an artifact and records it as a delta for the current session.io.reactivex.rxjava3.core.Single<SearchMemoryResponse> searchMemory(String query) Searches the memory of the current user.state()Returns the delta-aware state of the current context.Returns the confirmation of the current tool call, if any.voidtoolConfirmation(@Nullable ToolConfirmation toolConfirmation) Sets the confirmation of the current tool call, or clears it if null.Methods inherited from class ReadonlyContext
agentName, branch, events, invocationContext, invocationId, sessionId, userContent, userIdModifier and TypeMethodDescriptionReturns the name of the agent currently running.branch()Returns the branch of the current invocation, if present.events()Returns an unmodifiable view of the events of the session.Returns the invocation context.Returns the ID of the current invocation.Returns the session ID.Optional<com.google.genai.types.Content> Returns the user content that initiated this invocation.userId()Returns the user ID.
-
Field Details
-
eventActions
The event actions that record this context's changes.
-
-
Constructor Details
-
Context
Creates the context of the workflow node run whose state isnodeState. Only the workflow runtime creates a node state.
-
-
Method Details
-
state
Returns the delta-aware state of the current context. In the context a workflow passes to a node, or under a state schema, writing through its map views orState.clear()throwsUnsupportedOperationException; seeState. Under the node'sNode.stateSchema(), which a tool node also applies to its tool's context, writes store converted values and throwStateSchemaExceptionon a mismatch or undeclared key. A stored value reads back in the form a stored session gives: lists and maps unmodifiable, a record as a map, an enum by its name, a small whole number as anInteger.- Overrides:
statein classReadonlyContext
-
eventActions
Returns theEventActionsassociated with this context. -
actions
Returns the sameEventActionsaseventActions(). -
eventId
Returns the ID of the event associated with this context, or null if there is none. -
functionCallId
-
functionCallId
Sets the ID of the function call that invoked the current tool, or clears it if null. -
toolConfirmation
Returns the confirmation of the current tool call, if any. -
toolConfirmation
Sets the confirmation of the current tool call, or clears it if null. -
listArtifacts
-
loadArtifact
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
Completablethat completes when the artifact is saved. - Throws:
IllegalStateException- if the artifact service is not initialized.
-
requestConfirmation
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
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
Searches the memory of the current user. -
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
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
Sets the author of the events this workflow node emits.- Throws:
IllegalStateException- if this is not the context of a workflow node.
-
output
Returns this workflow node's output, which is the output of itsuseAsOutputchild once that child emits one, or empty if it has none.- Throws:
IllegalStateException- if this is not the context of a workflow node.
-
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
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
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
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.
-
emit
Sendseventfrom this workflow node'sFunctionNodebody, after the events of earlier calls and before the output. It returns once the run has stored the event and handed it to the run's caller, and any thread of the body may call it. On aRunnerthat runs work inline, the node needs aFunctionNode.Builder#schedulerthat does not, such asSchedulers.io(). TheFunctionNodedocumentation gives the rules for state, threads, events, cancellation and resumes.- Throws:
IllegalArgumentException- ifeventhas an output, or has the ID of an event anotheremitcall still waits onStateSchemaException- if the node's state schema rejects the event's state delta; the node then fails with it even if the body catches it; later calls while the body runs throw one with the same message, and a laterrunNodestarts no child and fails with one. The rejection does not interrupt the body's thread: it keeps running until it returns, pauses or throws, and state it writes meanwhile is still saved, but the node fails with the rejection, with any other exception the body throws added as suppressed.IllegalStateException- if noFunctionNodebody of this node is runningCancellationException- if the node is canceled, or the calling thread is interrupted while this call waits; the thread stays interrupted
-
emit
Sendsmessageas an event without an output, asemit(Event)does. -
runNode
Runsnodeas a child of this workflow node without an input, asrunNode(Node, Object, RunNodeConfig)does with the defaultRunNodeConfig. -
runNode
@Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput) Runsnodeas a child of this workflow node, asrunNode(Node, Object, RunNodeConfig)does with the defaultRunNodeConfig.- Throws:
IllegalArgumentException- ifnodeInputis aRunNodeConfig, 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 aMaybethat, when subscribed, runsnodeas 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: when the child waits, theMaybefails with an internalErrorso that this node waits too, whichcatch (Exception e)lets through butcatch (Throwable t)and Rx error handlers such asonErrorResumeNextstop.When
nodeis a chat or task agent that hands off to another agent in an agent tree with modes, that agent runs as a node too, and the result is that of the last agent to run. A single-turn agent runs its target inside its own run.However this node's attempt ends, it cancels the children still running and waits for them to stop, so a resume or a retry runs such a child again. Disposing the whole run cancels them too, but returns without waiting.
It fails with
DynamicNodeFailedExceptionif the child fails, as when an agent's handoff is invalid: to the agent itself, to a task or single-turn sub-agent or peer, to a parent or peer its flags forbid, or to an agent outside its sub-agents, parent and peers. It fails withCancellationExceptionif this node's attempt has ended or a cancel has interrupted its body. It fails withIllegalStateExceptionif this is not a workflow node's context, if this node'sNode.rerunOnResume()is false, or ifconfigdelegates an output that this node already has or has delegated; withStateSchemaException, starting no child, once anemit(Event)in this node's body was rejected; withIllegalArgumentExceptionif a child or handoff target name cannot form a node path, or if the child's input schema rejectsnodeInput; or withNodeInputValidationException(anIllegalArgumentException) if the input port or builder input schema of aFunctionNodechild rejectsnodeInput. A child whose input is rejected does not start.
-