Artifact Management
Overview
Artifact Management provides a unified service interface and implementations for handling artifacts—files or blobs—within the system. Artifacts are stored, retrieved, listed, versioned, and deleted with strong support for version control and multi-tenant isolation based on application name, user ID, and session ID. The system is designed to support multiple backend storage implementations, primarily an in-memory store for testing and a Google Cloud Storage (GCS) backend for production use.
This module's primary goal is to facilitate reliable, versioned persistence of arbitrary binary or textual data ("artifacts") associated with user-agent sessions and applications. It enables agents and tools to save and retrieve content such as files, images, or any data blobs during AI workflows.
Core Concepts
Artifact Identity and Versioning
Each artifact is uniquely identified by a combination of:
AppName: The application or system name.
UserID: The user owning or associated with the artifact.
SessionID: The session within which the artifact is created or used.
FileName: The logical name of the artifact file.
Artifacts also support versioning, where multiple versions of the same artifact can coexist. Saving a new artifact automatically increments the version unless explicitly overridden.
User-Scoped Artifacts
Artifacts whose filenames are prefixed with "user:" are treated as user-scoped rather than session-scoped. Such artifacts are stored under a user-level namespace, making them accessible across all sessions for that user. This distinction is important for sharing or persisting user-wide data beyond a single session.
Service Interface
The artifact.Service interface defines the core operations:
Save: Store an artifact and return its version.Load: Retrieve the latest or a specific version of an artifact.Delete: Remove an artifact by version or entirely.List: Enumerate filenames of artifacts in a session.Versions: List all available versions of a given artifact.
All operations require the artifact's identifying fields and provide validation to ensure completeness.
Backend Implementations
The module currently provides two main backend implementations conforming to the artifact.Service interface:
In-Memory Artifact Service (
InMemoryService):
A thread-safe, volatile implementation storing artifacts in memory. Primarily used for testing, it maintains an ordered map of artifacts keyed by a custom encoded string of the artifact's identity and version.Google Cloud Storage Service (gcs.Service):
A robust, persistent implementation using GCS buckets. It organizes artifacts as GCS blobs with a structured naming scheme encoding the artifact identity and version, supporting parallel operations and streaming I/O.
How Artifact Management Works
Saving Artifacts
When saving an artifact, the service:
Validates the request fields.
Determines if the file is user-scoped (filename starts with
"user:").For in-memory:
Locks the storage map.
Finds the highest existing version for the artifact.
Increments the version and stores the artifact under a composite key encoding all identity fields plus version.
For GCS:
Queries existing versions via listing blobs with a prefix.
Calculates the next version.
Writes the artifact content (binary or text) to a GCS blob named by the composite key and version.
Returns the assigned version to the caller.
Loading Artifacts
To load an artifact, the service:
Validates the request.
Adjusts for user-scoped files by switching the session ID to the user namespace.
If a specific version is requested, attempts to load that version.
If no version specified, loads the latest version by:
For in-memory: scanning keys for the highest version.
For GCS: listing blobs under the artifact prefix and selecting the highest version.
Reads the artifact content and returns it as a genai.Part, which supports both inline binary data and textual content.
Listing Artifacts
Listing artifacts returns the set of filenames stored within a session namespace, including user-scoped artifacts. The service:
Fetches all artifact keys/blobs under the session prefix.
Additionally fetches artifacts under the user namespace.
Aggregates and returns a sorted list of unique filenames.
Deleting Artifacts
Deletion supports two modes:
Versioned Deletion: Deletes a specific artifact version.
Complete Deletion: Deletes all versions of an artifact.
For GCS, versioned deletions are performed in parallel using goroutines, while in-memory deletions remove keys directly. Deleting non-existent artifacts is not treated as an error.
Version Enumeration
The service can list all stored versions for a given artifact. This is useful for clients to know available historical versions, enabling version-specific retrieval or management.
Interactions and Usage
The
Internal Artifactsadapter (internal/artifact/artifacts.go) wraps the artifact service to provide convenient methods bound to a specific application, user, and session. This adapter implements the agent.Artifacts interface, allowing agents and tools to interact with artifacts transparently.Within the broader system, agents may use artifacts to store inputs or outputs (e.g., files, images, JSON blobs). Tools such as artifact loaders call into this service to fetch or save data during agent workflows.
The
Artifact Managementmodule interacts closely with the session and agent components, which provide the contextual identifiers (AppName, UserID, SessionID).The module abstracts away backend differences, allowing seamless switching between in-memory and cloud storage implementations depending on deployment and testing needs.
Artifact Key Encoding
Both backends use a consistent encoding scheme for artifact keys to ensure ordered storage and retrieval:
The key encodes fields
[AppName, UserID, SessionID, FileName, Version].For in-memory, keys are encoded as ordered bytes with version reversed to support descending version sorting.
For GCS, keys form hierarchical blob paths using slashes, e.g., appName/userID/sessionID/fileName/version.
This encoding ensures efficient lookup of the latest versions and range scans for listing.
Code Reference Examples
Service Interface Definition
type Service interface {
Save(ctx context.Context, req *SaveRequest) (*SaveResponse, error)
Load(ctx context.Context, req *LoadRequest) (*LoadResponse, error)
Delete(ctx context.Context, req *DeleteRequest) error
List(ctx context.Context, req *ListRequest) (*ListResponse, error)
Versions(ctx context.Context, req *VersionsRequest) (*VersionsResponse, error)
}
Saving an Artifact in In-Memory Service
func (s *inMemoryService) Save(ctx context.Context, req *SaveRequest) (*SaveResponse, error) {
// Validate request
// Adjust sessionID if user-scoped
s.mu.Lock()
defer s.mu.Unlock()
// Find latest version and increment
s.set(appName, userID, sessionID, fileName, nextVersion, artifact)
return &SaveResponse{Version: nextVersion}, nil
}
Saving an Artifact in GCS Service
func (s *gcsService) Save(ctx context.Context, req *artifact.SaveRequest) (*artifact.SaveResponse, error) {
// Validate request
// Determine nextVersion by listing versions
blobName := buildBlobName(appName, userID, sessionID, fileName, nextVersion)
writer := s.bucket.object(blobName).newWriter(ctx)
// Write InlineData or Text to GCS blob
return &artifact.SaveResponse{Version: nextVersion}, nil
}
Artifact Management Class Diagram
classDiagram
class Service {
<<interface>>
+Save(ctx, SaveRequest) SaveResponse
+Load(ctx, LoadRequest) LoadResponse
+Delete(ctx, DeleteRequest)
+List(ctx, ListRequest) ListResponse
+Versions(ctx, VersionsRequest) VersionsResponse
}
class InMemoryService {
-mu sync.RWMutex
-artifacts omap.Map
+Save()
+Load()
+Delete()
+List()
+Versions()
}
class GCSService {
-bucketName string
-storageClient gcsClient
-bucket gcsBucket
+Save()
+Load()
+Delete()
+List()
+Versions()
}
Service <|.. InMemoryService
Service <|.. GCSService
Summary of Subcomponents
In-Memory Artifact Service:
Provides an ephemeral, thread-safe artifact store using ordered maps and custom key encoding for version management. Ideal for testing and local development.Google Cloud Storage Service:
Implements persistent artifact storage using GCS buckets with hierarchical blob naming. Supports streaming I/O, parallel deletion, and version tracking via blob listings.Artifact Interface Adapter:
Bridges the artifact service with the agent framework by binding to application, user, and session identifiers to provide a simplified interface for agent/tool use.
This module is foundational for managing the lifecycle of artifact data within the agent workflows, enabling complex scenarios where artifacts must be versioned, shared, or retrieved efficiently during AI-driven sessions. It integrates tightly with session management and agent tooling to provide a seamless developer and runtime experience.
For more details on usage within agents and tools, see the Artifact Interface Adapter and related tooling in the [Tooling System](None - Tooling System).