inmemory.go

Overview

The inmemory.go file implements an in-memory artifact storage service that conforms to the artifact.Service interface defined in the Artifact Management module. This service provides a transient, thread-safe, and versioned artifact store primarily intended for testing, demonstration, and local development scenarios.

Artifacts are stored entirely in memory using an ordered map keyed by a composite artifactKey encoding the application name, user ID, session ID, filename, and version. The service supports storing multiple versions of artifacts, retrieving the latest or a specific version, listing all artifact filenames, and deleting artifacts by version or entirely.

The implementation manages concurrent access via a read-write mutex and distinguishes between session-scoped and user-scoped artifacts by namespace conventions. This enables artifacts to be shared across sessions for a given user when the filename has a user: prefix.

The service interacts closely with the genai.Part type representing artifact content and fits into the larger system as a backend for artifact persistence, complementing other backends like Google Cloud Storage.


Types and Data Structures

inMemoryService

artifactKey


Functions and Methods

Constructor

func InMemoryService() Service
svc := artifact.InMemoryService()

Namespace Utility

func fileHasUserNamespace(filename string) bool

artifactKey Methods

func (ak artifactKey) Encode() string
func (ak *artifactKey) Decode(key string) error

Internal Iteration Helpers

func (s *inMemoryService) scan(lo, hi string) iter.Seq2[artifactKey, *genai.Part]

Internal Getters and Setters

func (s *inMemoryService) find(appName, userID, sessionID, fileName string) (int64, *genai.Part, bool)
func (s *inMemoryService) get(appName, userID, sessionID, fileName string, version int64) (*genai.Part, bool)
func (s *inMemoryService) set(appName, userID, sessionID, fileName string, version int64, artifact *genai.Part)
func (s *inMemoryService) delete(appName, userID, sessionID, fileName string, version int64)

Interface Methods (artifact.Service)

Save

func (s *inMemoryService) Save(ctx context.Context, req *SaveRequest) (*SaveResponse, error)
resp, err := svc.Save(ctx, &artifact.SaveRequest{
    AppName: "myapp",
    UserID: "user123",
    SessionID: "sess456",
    FileName: "file.txt",
    Part: partData,
})
if err != nil {
    // handle error
}
fmt.Println("Saved version:", resp.Version)

Delete

func (s *inMemoryService) Delete(ctx context.Context, req *DeleteRequest) error

Load

func (s *inMemoryService) Load(ctx context.Context, req *LoadRequest) (*LoadResponse, error)

List

func (s *inMemoryService) List(ctx context.Context, req *ListRequest) (*ListResponse, error)

Versions

func (s *inMemoryService) Versions(ctx context.Context, req *VersionsRequest) (*VersionsResponse, error)

Important Implementation Details


Interactions with Other System Parts


Visual Diagram

classDiagram
class inMemoryService {
+Save()
+Load()
+Delete()
+List()
+Versions()
-mu : sync.RWMutex
-artifacts : omap.Map
}
class artifactKey {
-AppName : string
-UserID : string
-SessionID : string
-FileName : string
-Version : int64
+Encode()
+Decode()
}
inMemoryService "1" --> "*" artifactKey : stores by
inMemoryService ..> sync.RWMutex : concurrency control
inMemoryService ..> omap.Map : internal storage

This diagram depicts the core class inMemoryService with its main methods and internal fields. It shows the relationship to the artifactKey struct, which uniquely identifies stored artifacts by composite keys. The service uses a read-write mutex and an ordered map internally to manage concurrent, versioned artifact storage.


References