inmemory.go

Overview

The inmemory.go file implements an in-memory, thread-safe memory service that stores and retrieves conversational memory entries associated with user sessions. This service maintains session data such as content from language model (LLM) responses, authorship, timestamps, and precomputed word sets for efficient keyword-based search. It acts as a volatile storage backend designed primarily for transient or testing scenarios where persistent storage is not required.

The service provides two primary operations:

This file is part of the broader memory management system that enables conversational agents to retain and recall past interactions, tying in closely with session management and LLM event processing as described in Session Management and LLM Integration and Agents.


Types and Structures

key (struct)

Used as the primary lookup key in the in-memory store to isolate memory entries per user-app combination.

sessionID (type alias)

Represents a unique session identifier.

value (struct)

Encapsulates a single memory entry connected to a session event.

inMemoryService (struct)

Implements the Service interface to manage in-memory sessions and searchable memory entries in a thread-safe manner.


Functions and Methods

InMemoryService() Service


(s *inMemoryService) AddSession(ctx context.Context, curSession session.Session) error


(s *inMemoryService) Search(ctx context.Context, req *SearchRequest) (*SearchResponse, error)


checkMapsIntersect(m1, m2 map[string]struct{}) bool


extractWords(text string) map[string]struct{}


Important Implementation Details


Interaction with Other System Components


Diagram: inMemoryService Structure and Workflow

classDiagram
class inMemoryService {
-mu: RWMutex
-store: map[key]map[sessionID][]value
+AddSession(ctx, session)
+Search(ctx, req)
}
class key {
+appName: string
+userID: string
}
class sessionID {
<<alias>> string
}
class value {
-content: *genai.Content
-author: string
-timestamp: time.Time
-words: map[string]struct{}
}
inMemoryService "1" o-- "*" key : store keys
key "1" o-- "*" sessionID : sessions per user/app
sessionID "1" o-- "*" value : memory entries per session

This class diagram illustrates the nested data structure of the inMemoryService:


References: