service.go

Overview

The service.go file implements a Google Cloud Storage (GCS) backed artifact storage service conforming to the [artifact.Service](80557) interface. It provides persistent, versioned storage and retrieval of artifacts (binary or textual data) organized by application name, user ID, session ID, filename, and version. This implementation supports both session-scoped and user-scoped artifacts, with user-scoped files identified by a "user:" prefix and stored under a dedicated user namespace.

The service facilitates saving new artifact versions, loading specific or the latest versions, listing available artifact filenames, deleting individual or all versions, and enumerating artifact versions. It uses the official GCS client library, abstracted via wrapper interfaces for testability.


Types and Core Structs

gcsService

The primary struct implementing [artifact.Service](80557) backed by GCS.

type gcsService struct {
	bucketName    string
	storageClient gcsClient
	bucket        gcsBucket
}

Construction

NewService

Creates a new GCS artifact service instance.

func NewService(ctx context.Context, bucketName string, opts ...option.ClientOption) (artifact.Service, error)

Usage Example:

svc, err := NewService(ctx, "my-gcs-bucket", option.WithCredentialsFile("path/to/creds.json"))
if err != nil {
    // handle error
}
// svc can now be used to Save, Load, Delete, List, Versions

Naming and Scoping Helpers

Several helper functions build GCS blob/object names for versioned artifact storage:

Blob Naming Logic:


Methods Implementing artifact.Service

Save

func (s *gcsService) Save(ctx context.Context, req *artifact.SaveRequest) (*artifact.SaveResponse, error)

Important Notes:


Load

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

Delete

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

List

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

Versions

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

Internal and Helper Functions

versions

Internal method listing versions for an artifact without error if none found:

func (s *gcsService) versions(ctx context.Context, req *artifact.VersionsRequest) (*artifact.VersionsResponse, error)

fetchFilenamesFromPrefix

Helper to fetch artifact filenames under a given GCS prefix:

func (s *gcsService) fetchFilenamesFromPrefix(ctx context.Context, prefix string, filenamesSet map[string]bool) error

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
class gcsService {
-bucketName: string
-storageClient: gcsClient
-bucket: gcsBucket
+Save()
+Load()
+Delete()
+List()
+Versions()
}
class blobNameHelpers {
+fileHasUserNamespace()
+buildBlobName()
+buildBlobNamePrefix()
+buildSessionPrefix()
+buildUserPrefix()
}
gcsService ..> blobNameHelpers : uses

Detailed Function and Method Descriptions

NewService


fileHasUserNamespace(filename string) bool


buildBlobName(appName, userID, sessionID, fileName string, version int64) string


(s *gcsService) Save(ctx context.Context, req *artifact.SaveRequest) (*artifact.SaveResponse, error)


(s *gcsService) Delete(ctx context.Context, req *artifact.DeleteRequest) error


(s *gcsService) Load(ctx context.Context, req *artifact.LoadRequest) (*artifact.LoadResponse, error)


(s *gcsService) List(ctx context.Context, req *artifact.ListRequest) (*artifact.ListResponse, error)


(s *gcsService) Versions(ctx context.Context, req *artifact.VersionsRequest) (*artifact.VersionsResponse, error)


(s *gcsService) versions(ctx context.Context, req *artifact.VersionsRequest) (*artifact.VersionsResponse, error)


(s *gcsService) fetchFilenamesFromPrefix(ctx context.Context, prefix string, filenamesSet map[string]bool) error


Usage Example

ctx := context.Background()
service, err := gcs.NewService(ctx, "my-bucket")
if err != nil {
    log.Fatal(err)
}

// Save new artifact
saveResp, err := service.Save(ctx, &artifact.SaveRequest{
    AppName: "myapp",
    UserID: "user123",
    SessionID: "sess456",
    FileName: "file.txt",
    Part: genai.NewPartFromText("Hello World"),
})
if err != nil {
    log.Fatal(err)
}
fmt.Println("Saved version:", saveResp.Version)

// Load latest artifact version
loadResp, err := service.Load(ctx, &artifact.LoadRequest{
    AppName: "myapp",
    UserID: "user123",
    SessionID: "sess456",
    FileName: "file.txt",
})
if err != nil {
    log.Fatal(err)
}
fmt.Println("Loaded content:", loadResp.Part.Text)

This file is a foundational implementation for durable, versioned artifact storage in GCS, enabling agents and workflows in the system to reliably persist and retrieve artifacts with scoped naming and concurrency-aware deletion. It complements other backend implementations and integrates with the overall Artifact Management infrastructure.