readonly_context.go

Overview

The readonly_context.go file defines a lightweight, read-only wrapper around an existing agent invocation context within the system. It provides safe, immutable access to core session and invocation metadata without permitting any state changes or side effects. This read-only context is primarily used in situations where an agent or component needs to inspect invocation details such as the session ID, user ID, branch, or user content, but must not modify any state or artifacts.

This file is part of the broader Agent Invocation Context subtopic, which manages the lifecycle and data scope of agent invocations. ReadonlyContext implements the agent.ReadonlyContext interface by delegating read-only method calls to its embedded InvocationContext. It enables clear separation between mutable contexts used during agent execution callbacks and immutable views that guarantee no side-effects during inspection or logging.


Types and Functions

ReadonlyContext struct

type ReadonlyContext struct {
    context.Context
    InvocationContext agent.InvocationContext
}

NewReadonlyContext

func NewReadonlyContext(ctx agent.InvocationContext) agent.ReadonlyContext
readonlyCtx := NewReadonlyContext(invocationCtx)
appName := readonlyCtx.AppName()

Methods on ReadonlyContext

Each method implements a corresponding member of the agent.ReadonlyContext interface by delegating to the underlying InvocationContext. All methods provide read-only access to session, invocation, or user content data.

Method

Return Type

Description

AppName()

string

Returns the name of the application owning the current session. Obtained via Session().AppName().

Branch()

string

Returns the branch identifier for this invocation, representing hierarchical sub-agent calls.

SessionID()

string

Returns the unique ID of the current session.

UserID()

string

Returns the user ID associated with the current session.

AgentName()

string

Returns the name of the active agent handling this invocation.

ReadonlyState()

session.ReadonlyState

Returns an immutable snapshot of the current session state.

InvocationID()

string

Returns the unique identifier of this invocation.

UserContent()

*genai.Content

Returns the original user content (input) that triggered this invocation.

Method Details

func (c *ReadonlyContext) AppName() string {
    return c.InvocationContext.Session().AppName()
}
func (c *ReadonlyContext) Branch() string {
    return c.InvocationContext.Branch()
}
func (c *ReadonlyContext) SessionID() string {
    return c.InvocationContext.Session().ID()
}
func (c *ReadonlyContext) UserID() string {
    return c.InvocationContext.Session().UserID()
}
func (c *ReadonlyContext) AgentName() string {
    return c.InvocationContext.Agent().Name()
}
func (c *ReadonlyContext) ReadonlyState() session.ReadonlyState {
    return c.InvocationContext.Session().State()
}
func (c *ReadonlyContext) InvocationID() string {
    return c.InvocationContext.InvocationID()
}
func (c *ReadonlyContext) UserContent() *genai.Content {
    return c.InvocationContext.UserContent()
}

Implementation Details


Interaction with Other Components


Code Structure Diagram

classDiagram
class ReadonlyContext {
+AppName()
+Branch()
+SessionID()
+UserID()
+AgentName()
+ReadonlyState()
+InvocationID()
+UserContent()
}
ReadonlyContext o-- "1" agent.InvocationContext
ReadonlyContext o-- "1" context.Context

Usage Example

// Assume invocationCtx is an existing agent.InvocationContext instance
readonlyCtx := NewReadonlyContext(invocationCtx)

// Access read-only session and invocation metadata
fmt.Println("App Name:", readonlyCtx.AppName())
fmt.Println("Session ID:", readonlyCtx.SessionID())
fmt.Println("User ID:", readonlyCtx.UserID())
fmt.Println("Agent Name:", readonlyCtx.AgentName())

// Access user content triggering the invocation
userContent := readonlyCtx.UserContent()
if userContent != nil {
    fmt.Println("User input text:", userContent.Text)
}

This example shows how to create a ReadonlyContext to safely expose invocation data for logging, auditing, or non-mutating logic.


References


This file provides a fundamental utility for safely exposing agent invocation information in read-only form, ensuring separation of concerns and robust context management within the agent execution environment.