memory.go
Overview
The memory.go file provides a lightweight wrapper around a memory service interface for managing session-related data and performing search operations within the context of an application. It abstracts and simplifies interactions with an underlying memory service by encapsulating session and user identifiers alongside the application name. This enables storing session objects and querying memory data relevant to the current user and application context.
The file defines a single struct type Memory which holds references to the memory service client and contextual identifiers. It exposes two main methods: AddSession for adding session data to memory storage and Search for querying stored memory data.
Types and Methods
Memory Struct
type Memory struct {
Service memory.Service
SessionID string
UserID string
AppName string
}
Purpose:
Encapsulates the memory service client and contextual identifiers needed to perform memory operations scoped to a user and application session.Fields:
Service: An interface to the underlying memory service (memory.Service), responsible for actual storage and search operations.SessionID: The active session's unique identifier as a string.UserID: The identifier of the user associated with the session.AppName: The name of the application or context to which the memory operations are scoped.
AddSession
func (a *Memory) AddSession(ctx context.Context, session session.Session) error
Description:
Adds a session object to the memory service storage asynchronously. This operation persists session state or metadata into the memory backend.Parameters:
ctx(context.Context): Context controlling cancellation and deadlines for the operation.session(session.Session): The session object to be added to the memory service.
Returns:
error: Returns an error if the underlying memory service fails to add the session.
Usage Example:
mem := &Memory{ Service: memoryServiceClient, SessionID: "session123", UserID: "user456", AppName: "MyApp", } err := mem.AddSession(ctx, session) if err != nil { // handle error }Implementation Detail:
This method delegates directly to theAddSessionmethod of thememory.Serviceinterface, passing through the provided context and session.
Search
func (a *Memory) Search(ctx context.Context, query string) (*memory.SearchResponse, error)
Description:
Performs a search query against the memory service, scoped by theAppNameandUserIDstored in theMemorystruct. This enables querying relevant memory entries associated with a user in a specific application context.Parameters:
ctx(context.Context): Context for managing cancellation and timeouts.query(string): The search string or query to execute against the memory service.
Returns:
*memory.SearchResponse: Pointer to the search response containing matched memory data.error: Returns an error if the search operation failed.
Usage Example:
results, err := mem.Search(ctx, "find recent documents") if err != nil { // handle error } // process resultsImplementation Detail:
The method constructs amemory.SearchRequestwith the currentAppNameandUserIDand forwards it to theSearchmethod of thememory.Serviceinterface.
Important Implementation Details
The file relies on interfaces and types from two imported packages:
google.golang.org/adk/memory— provides theService,SearchRequest, andSearchResponsetypes.google.golang.org/adk/session— provides theSessiontype used inAddSession.
The
Memorystruct acts as a context-bound adapter, reducing the need to repeatedly specify user and app details when interacting with the memory service.No explicit caching or additional logic is implemented; this file functions purely as a thin delegation layer over the memory service.
Interaction with Other System Components
Memory Service (
memory.Service):
The core dependency representing the backend memory storage and search capabilities. This file depends on it for all data persistence and retrieval operations.Session Management (
session.Session):
The session objects added viaAddSessionoriginate from the session management subsystem, indicating this file is part of the broader session lifecycle and state management workflow.This file likely integrates with components responsible for user session lifecycle (
Session Management[80559]), facilitating the persistence and querying of session-related memory data.The
Searchfunctionality can support features like context-aware retrieval, personalized memory recall, or agent state querying, which may be leveraged by higher-level AI agent workflows (LLM Integration and Agents) or workflow management components (Agent Workflow Management).
Visual Diagram
classDiagram
class Memory {
+Service: memory.Service
+SessionID: string
+UserID: string
+AppName: string
+AddSession(ctx, session) error
+Search(ctx, query) *memory.SearchResponse
}
Memory --> memory.Service : uses
Memory ..> session.Session : uses
References
For details on the
memory.Serviceinterface and related request/response types, see Memory Service.For session object structure and lifecycle, see Session Management.
For integration of memory with agent workflows, see LLM Integration and Agents and Agent Workflow Management.