InvocationContext

data class InvocationContext(val session: Session, val runConfig: RunConfig? = null, val agent: BaseAgent, val branch: String? = null, val invocationId: String = "e-" + Uuid.random(), val artifactService: ArtifactService? = null, val memoryService: MemoryService? = null, val sessionService: SessionService? = null, val resumabilityConfig: ResumabilityConfig? = null, val userContent: Content? = null, val agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), val endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), val extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), var isEndOfInvocation: Boolean = false, var isPaused: Boolean = false, val pluginManager: PluginManager = PluginManager())

An invocation context represents the data of a single invocation of an agent.

An invocation:

  1. Starts with a user message and ends with a final response.

  2. Can contain one or multiple agent calls.

  3. Is handled by runner.run_async().

An invocation runs an agent until it does not request to transfer to another agent.

An agent call:

  1. Is handled by agent.run().

  2. Ends when agent.run() ends.

An LLM agent call is an agent with a BaseLLMFlow. An LLM agent call can contain one or multiple steps.

An LLM agent runs steps in a loop until:

  1. A final response is generated.

  2. The agent transfers to another agent.

  3. The end_invocation is set to true by any callbacks or tools.

A step:

  1. Calls the LLM only once and yields its response.

  2. Calls the tools and yields their responses if requested.

The summarization of the function response is considered another step, since it is another llm call. A step ends when it's done calling llm and tools, or if the end_invocation is set to true at any time.

    ┌─────────────────────── invocation ──────────────────────────┐
┌──────────── llm_agent_call_1 ────────────┐ ┌─ agent_call_2 ─┐
┌──── step_1 ────────┐ ┌───── step_2 ──────┐
[call_llm] [call_tool] [call_llm] [transfer]

Constructors

Link copied to clipboard
constructor(session: Session, runConfig: RunConfig? = null, agent: BaseAgent, branch: String? = null, invocationId: String = "e-" + Uuid.random(), artifactService: ArtifactService? = null, memoryService: MemoryService? = null, sessionService: SessionService? = null, resumabilityConfig: ResumabilityConfig? = null, userContent: Content? = null, agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), isEndOfInvocation: Boolean = false, isPaused: Boolean = false, pluginManager: PluginManager = PluginManager())

Properties

Link copied to clipboard

The current agent of this invocation context. Readonly.

Link copied to clipboard

The state of the agent for this invocation.

Link copied to clipboard
Link copied to clipboard
val branch: String? = null

The branch of the invocation context.

Link copied to clipboard

The end of agent status for each agent in this invocation.

Link copied to clipboard

Extra tools injected dynamically during invocation (e.g., by SequentialAgent).

Link copied to clipboard

The id of this invocation context. Readonly.

Link copied to clipboard

Whether to end this invocation.

Link copied to clipboard

Whether this invocation is paused.

Link copied to clipboard

Returns whether the current invocation is resumable.

Link copied to clipboard
Link copied to clipboard

The manager for keeping track of plugins in this invocation.

Link copied to clipboard

Optional resumability configuration for this invocation.

Link copied to clipboard
val runConfig: RunConfig? = null

Configurations for live agents under this invocation.

Link copied to clipboard

The current session of this invocation context. Readonly.

Link copied to clipboard
Link copied to clipboard
val userContent: Content? = null

The user content that started this invocation. Readonly.

Functions

Link copied to clipboard

Creates a new InvocationContext for a child agent, derived from this context. Appends the given agent's name to the branch path.

Link copied to clipboard
suspend fun executeSingleFunctionCall(functionCall: FunctionCall, tools: Map<String, BaseTool>, toolConfirmation: ToolConfirmation? = null): Event?

Executes a single function call synchronously and builds a corresponding response event.

Link copied to clipboard
suspend fun findMatchingFunctionCall(functionResponseEvent: Event): Event?

Finds the function call event in the current invocation that matches the function response id.

Link copied to clipboard
suspend fun getEvents(currentInvocation: Boolean = false, currentBranch: Boolean = false): List<Event>

Returns the events from the current session.

Link copied to clipboard
suspend fun handleFunctionCalls(functionCalls: List<FunctionCall>, tools: Map<String, BaseTool>, filters: Set<String> = emptySet(), toolConfirmations: Map<String, ToolConfirmation>? = null): Event?

Processes a list of function calls by executing them efficiently and safely.

Link copied to clipboard

Populates agent states for the current invocation if it is resumable.

Link copied to clipboard
fun resetSubAgentStates(agentName: String)

Resets the state of all sub-agents of the given agent recursively.

Link copied to clipboard
fun setAgentState(agentName: String, agentState: TypedData? = null, endOfAgent: Boolean = false)

Set state of an agent explicitly. Does not implicitly initialize.

Link copied to clipboard

Returns whether to pause the invocation right after this event.