context.go

Overview

The context.go file defines the core interfaces representing the invocation context for AI agents within the system. This context encapsulates all relevant runtime data, state, and control mechanisms needed during the lifecycle of an agent invocation. It models the hierarchical and temporal boundaries of an agent call, including nested sub-agent invocations and multi-step processing within a single invocation.

The file's primary purpose is to provide standardized abstractions for:

These interfaces are foundational for the AI Agent Framework, enabling agents to operate with full awareness of their execution environment and maintain consistency across complex workflows.


Interfaces and Their Details

1. InvocationContext

The InvocationContext interface extends Go's standard context.Context and represents the full mutable context of an agent invocation. It manages the scope and lifecycle of a single invocation, which can include multiple agent calls and steps.

Purpose

Methods

Method

Description

Return Type

Agent()

Returns the Agent instance handling this invocation.

Agent

Artifacts()

Provides access to session-scoped artifacts (files, blobs) related to this invocation.

Artifacts

Memory()

Returns user-scoped memory for persistent knowledge across sessions.

Memory

Session()

Provides access to the current session.Session object representing conversation state.

session.Session

InvocationID()

Returns a unique string identifier for this invocation instance.

string

Branch()

Returns a dot-separated string representing the invocation branch hierarchy (e.g., agent1.agent2).

string

UserContent()

Returns the initial user message (genai.Content) that started this invocation.

*genai.Content

RunConfig()

Returns runtime configuration parameters used during this invocation.

*RunConfig

EndInvocation()

Signals to end the current invocation immediately, preventing further agent calls or steps.

void

Ended()

Returns whether the invocation has already ended.

bool

Usage Example

func exampleUsage(ctx InvocationContext) {
    agent := ctx.Agent()
    session := ctx.Session()
    userMsg := ctx.UserContent()

    // Check if invocation ended
    if ctx.Ended() {
        return
    }

    // Access memory and artifacts
    memory := ctx.Memory()
    artifacts := ctx.Artifacts()

    // End invocation early if certain condition met
    if shouldStop() {
        ctx.EndInvocation()
    }
}

2. ReadonlyContext

ReadonlyContext is a restricted interface providing read-only access to essential invocation data. It is used when mutation of invocation state or artifacts is not allowed or not needed, such as during inspection or in safe callback phases.

Purpose

Methods

Method

Description

Return Type

UserContent()

Returns the user message that started the invocation.

*genai.Content

InvocationID()

Returns the unique invocation ID.

string

AgentName()

Returns the name of the agent handling this invocation.

string

ReadonlyState()

Returns a read-only snapshot of the session state.

session.ReadonlyState

UserID()

Returns the user ID of the current session.

string

AppName()

Returns the application name associated with the session.

string

SessionID()

Returns the session identifier string.

string

Branch()

Returns the invocation branch string for sub-agent context isolation.

string


3. CallbackContext

CallbackContext extends ReadonlyContext by adding mutable access to session state and artifacts. It is passed to user-defined lifecycle callbacks that execute before or after the main agent run.

Purpose

Methods (in addition to those inherited from ReadonlyContext)

Method

Description

Return Type

Artifacts()

Provides access to artifacts service for saving/loading.

Artifacts

State()

Provides mutable access to the session state object.

session.State


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Diagram

classDiagram
class InvocationContext {
+Agent()
+Artifacts()
+Memory()
+Session()
+UserContent()
+RunConfig()
+InvocationID()
+Branch()
+EndInvocation()
+Ended()
}
class ReadonlyContext {
+UserContent()
+InvocationID()
+AgentName()
+ReadonlyState()
+UserID()
+AppName()
+SessionID()
+Branch()
}
class CallbackContext {
+Artifacts()
+State()
}
InvocationContext <|-- ReadonlyContext
InvocationContext <|-- CallbackContext
InvocationContext "1" --> "1" Agent
InvocationContext "1" --> "1" Session
InvocationContext "1" --> "1" Memory
InvocationContext "1" --> "1" Artifacts
InvocationContext "1" --> "1" RunConfig

This class diagram visualizes the relationships among the main interfaces defined in this file:


Summary of Key Types in context.go

Interface

Description

InvocationContext

Full mutable context for an agent invocation lifecycle.

ReadonlyContext

Read-only view of invocation data, safe for inspection.

CallbackContext

Context with mutable state and artifact access for callbacks.


References