context.go

Overview

The context.go file defines core internal context handling utilities for managing tool execution within the agent framework. It provides an implementation of the tool.Context interface that wraps an agent's invocation context, facilitating artifact management, event action tracking, and memory search capabilities during tool execution. This file ties together agent session state, artifact versioning, and event delta recording in a unified context object used by tools to interact with the broader agent environment.

It primarily implements the toolContext struct and a related internal wrapper for artifact saving that tracks version deltas in session event actions. The file also contains the factory function NewToolContext which initializes this context, ensuring proper setup of state delta maps and unique function call IDs.

This functionality is crucial for enabling tools invoked by agents to persist artifacts, update session state, and perform memory queries while correctly propagating event changes and maintaining traceability via function call IDs.


Types and Functions

internalArtifacts struct

type internalArtifacts struct {
    agent.Artifacts
    eventActions *session.EventActions
}

NewToolContext function

func NewToolContext(ctx agent.InvocationContext, functionCallID string, actions *session.EventActions) tool.Context

toolContext struct

type toolContext struct {
    agent.CallbackContext
    invocationContext agent.InvocationContext
    functionCallID    string
    eventActions      *session.EventActions
    artifacts         *internalArtifacts
}

Important Implementation Details and Algorithms


Interaction with Other System Components


Structure Diagram

classDiagram
class internalArtifacts {
+Save()
-Artifacts
-eventActions
}
class toolContext {
+Artifacts()
+FunctionCallID()
+Actions()
+AgentName()
+SearchMemory()
-CallbackContext
-invocationContext
-functionCallID
-eventActions
-artifacts
}
internalArtifacts --> agent.Artifacts
toolContext --> agent.CallbackContext
toolContext --> agent.InvocationContext
toolContext --> session.EventActions
toolContext --> internalArtifacts

References