callback_context.go

Overview

The callback_context.go file implements a mutable callback context used during agent invocation callbacks, extending a read-only invocation context with state mutation and artifact saving capabilities. It is part of the broader Agent Invocation Context framework (Agent Invocation Context) that encapsulates all relevant information and state management during an agent's invocation lifecycle.

This file defines the callbackContext struct, which provides access to mutable session state, artifact saving with version tracking, and references to the underlying invocation context. It supports tracking state deltas and artifact version changes during an invocation, enabling fine-grained updates to session state and artifact metadata that are committed at the end of the invocation. The callbackContext also wraps artifact operations to record changes into an EventActions structure, facilitating event-driven workflows and version consistency.

Entities and Key Structures

internalArtifacts


callbackContext


callbackContextState


Important Implementation Details


Interaction With Other System Components


Usage Examples

Creating a New Callback Context

// Create a callback context from an existing invocation context with an empty state delta.
cbCtx := NewCallbackContext(invocationCtx)

// Or create with an existing state delta.
stateDelta := map[string]any{"lastResult": "success"}
cbCtx := NewCallbackContextWithDelta(invocationCtx, stateDelta)

Reading and Writing State

// Get a session state value, checking both delta and persisted state.
val, err := cbCtx.State().Get("someKey")

// Set a session state value, updating the delta and session.
err := cbCtx.State().Set("someKey", "newValue")

Saving an Artifact and Tracking Version

resp, err := cbCtx.Artifacts().Save(ctx, "config.json", contentPart)
if err != nil {
    // Handle error
}
// The artifact version is tracked internally for event processing.

Structure Diagram

classDiagram
class internalArtifacts {
-Artifacts
-eventActions
+Save()
}
class callbackContext {
-ReadonlyContext
-artifacts
-invocationCtx
-eventActions
+Artifacts()
+AgentName()
+ReadonlyState()
+State()
+InvocationID()
+UserContent()
}
class callbackContextState {
-ctx
+Get()
+Set()
+All()
}
callbackContext o-- internalArtifacts : uses
callbackContext "1" *-- callbackContextState : provides

This diagram depicts the relationship between the main entities:


References