invocation_context.go

Overview

This file defines the core implementation of the InvocationContext within the agent execution lifecycle. The InvocationContext encapsulates all relevant contextual information needed during a single invocation of an agent. It provides access to session state, memory, artifacts, agent metadata, user input, and invocation control flags. This context is essential to manage the flow and data for nested or parallel invocations, enabling agents to query and manipulate their runtime environment consistently.

The file primarily implements the InvocationContext struct and its constructor, along with accessor methods to retrieve or update invocation-related data. This supports lifecycle management such as uniquely identifying invocations (InvocationID) and signaling when an invocation ends (EndInvocation).

This implementation is a concrete realization of the concepts described in the Agent Invocation Context topic, serving as a foundational piece for agent execution within the broader AI Agent Framework.


Types and Functions

InvocationContextParams

A struct bundling all parameters required to initialize an InvocationContext. It holds references to:

This struct provides a convenient container for passing all context dependencies when instantiating an invocation.


NewInvocationContext

func NewInvocationContext(ctx context.Context, params InvocationContextParams) agent.InvocationContext

Purpose:
Factory function that creates a new InvocationContext instance wrapping a standard Go context.Context with invocation-specific parameters.

Parameters:

Returns:

Details:

Usage example:

params := InvocationContextParams{
    Artifacts: artifactSvc,
    Memory: memorySvc,
    Session: sessionInst,
    Branch: "main.branch",
    Agent: myAgent,
    UserContent: userMsg,
    RunConfig: runCfg,
}
invCtx := NewInvocationContext(baseCtx, params)

InvocationContext struct

type InvocationContext struct {
    context.Context

    params       InvocationContextParams
    invocationID string
}

Methods of InvocationContext

All methods provide access to state or control flags relevant during an agent invocation.

Method Name

Signature

Description

Artifacts()

agent.Artifacts

Returns the artifact service interface for managing files/blobs during the invocation.

Agent()

agent.Agent

Returns the agent instance handling this invocation.

Branch()

string

Returns the branch path string representing hierarchical sub-agent invocation context.

InvocationID()

string

Returns the unique invocation ID string assigned at creation.

Memory()

agent.Memory

Returns the memory interface scoped to this invocation (session or persistent).

Session()

session.Session

Returns the session object representing conversation and event history.

UserContent()

*genai.Content

Returns the original user input content that triggered this invocation.

RunConfig()

*agent.RunConfig

Returns the runtime configuration parameters for this invocation.

EndInvocation()

void

Sets the internal flag to mark the invocation as ended, signaling termination to the runner.

Ended()

bool

Returns whether the invocation has been marked as ended.


Method Details and Behavior


Implementation Details


Interaction with Other Components


Visual Diagram

classDiagram
class InvocationContext {
+Artifacts()
+Agent()
+Branch()
+InvocationID()
+Memory()
+Session()
+UserContent()
+RunConfig()
+EndInvocation()
+Ended()
}
InvocationContext o-- InvocationContextParams : has
InvocationContextParams : +Artifacts
InvocationContextParams : +Memory
InvocationContextParams : +Session
InvocationContextParams : +Branch
InvocationContextParams : +Agent
InvocationContextParams : +UserContent
InvocationContextParams : +RunConfig
InvocationContextParams : +EndInvocation

This class diagram illustrates the InvocationContext struct and its methods, showing its composition relationship to the InvocationContextParams struct that holds the underlying data references and flags.


References