Artifacts in trpc-agent-go are named, versioned binary data objects that can be linked to user sessions or persistently associated with users across sessions. The artifact system consists of two main components:
Artifacts: The data objects themselves - containing binary content, metadata, and versioning information
Artifact Service: The storage and management service that handles saving, retrieving, and organizing artifacts
This system enables agents to store, retrieve, and manage various types of content including images, documents, text files, and other binary data.
What are Artifacts?
Artifacts are data containers that hold:
Binary content (images, documents, files, etc.)
Metadata (MIME type, name, URL)
Version information
Association with users and sessions
What is the Artifact Service?
The Artifact Service is the backend system that:
Stores and retrieves artifacts
Manages versioning
Handles namespace organization (session vs user scope)
Provides different storage backends (in-memory, cloud storage)
System Overview
The artifact system provides:
Versioned Storage: Each artifact is automatically versioned, allowing you to track changes over time
Session-based Organization: Artifacts can be scoped to specific user sessions
User-persistent Storage: Artifacts can be stored persistently for users across sessions using the user: namespace
Multiple Storage Backends: Support for in-memory storage (development) and cloud storage (production)
MIME Type Support: Proper content type handling for different file formats
Core Components
Artifact Data Structure
An Artifact is the fundamental data object that contains your content:
typeArtifactstruct{// Data contains the raw bytes (required)Data[]byte`json:"data,omitempty"`// MimeType is the IANA standard MIME type (required)MimeTypestring`json:"mime_type,omitempty"`// URL is the optional URL where the artifact can be accessedURLstring`json:"url,omitempty"`// Name is an optional display name of the artifactNamestring`json:"name,omitempty"`}
typeSessionInfostruct{// AppName is the name of the applicationAppNamestring// UserID is the ID of the userUserIDstring// SessionID is the ID of the sessionSessionIDstring}
Artifact Service Backends
The Artifact Service provides different storage implementations for managing artifacts:
funcmyTool(ctxcontext.Context,inputMyInput)(MyOutput,error){// Get tool contexttoolCtx,err:=agent.NewToolContext(ctx)iferr!=nil{returnMyOutput{},err}// Create an artifactartifact:=&artifact.Artifact{Data:[]byte("Hello, World!"),MimeType:"text/plain",Name:"greeting.txt",}// Save the artifactversion,err:=toolCtx.SaveArtifact("greeting.txt",artifact)iferr!=nil{returnMyOutput{},err}// Load the artifact laterloadedArtifact,err:=toolCtx.LoadArtifact("greeting.txt",nil)// nil for latest versioniferr!=nil{returnMyOutput{},err}returnMyOutput{},nil}
Namespace and Versioning
Session-scoped Artifacts
By default, artifacts are scoped to the current session:
// Save version 0v0,_:=toolCtx.SaveArtifact("document.txt",artifact1)// Save version 1v1,_:=toolCtx.SaveArtifact("document.txt",artifact2)// Load specific versionoldVersion:=0artifact,_:=toolCtx.LoadArtifact("document.txt",&oldVersion)// Load latest versionartifact,_:=toolCtx.LoadArtifact("document.txt",nil)
Artifact Service Interface
The Artifact Service provides the following operations for managing artifacts:
typeServiceinterface{// Save an artifact and return the version IDSaveArtifact(ctxcontext.Context,sessionInfoSessionInfo,filenamestring,artifact*Artifact)(int,error)// Load an artifact (latest version if version is nil)LoadArtifact(ctxcontext.Context,sessionInfoSessionInfo,filenamestring,version*int)(*Artifact,error)// List all artifact filenames in a sessionListArtifactKeys(ctxcontext.Context,sessionInfoSessionInfo)([]string,error)// Delete an artifact (all versions)DeleteArtifact(ctxcontext.Context,sessionInfoSessionInfo,filenamestring)error// List all versions of an artifactListVersions(ctxcontext.Context,sessionInfoSessionInfo,filenamestring)([]int,error)}
// Tool to generate and save imagesfuncgenerateImageTool(ctxcontext.Context,inputGenerateImageInput)(GenerateImageOutput,error){// Generate image (implementation details omitted)imageData:=generateImage(input.Prompt)// Create artifactartifact:=&artifact.Artifact{Data:imageData,MimeType:"image/png",Name:"generated-image.png",}// Save to artifactstoolCtx,_:=agent.NewToolContext(ctx)version,err:=toolCtx.SaveArtifact("generated-image.png",artifact)returnGenerateImageOutput{ImagePath:"generated-image.png",Version:version,},err}
// Tool to process and save textfuncprocessTextTool(ctxcontext.Context,inputProcessTextInput)(ProcessTextOutput,error){// Process the textprocessedText:=strings.ToUpper(input.Text)// Create artifactartifact:=&artifact.Artifact{Data:[]byte(processedText),MimeType:"text/plain",Name:"processed-text.txt",}// Save to user namespace for persistencetoolCtx,_:=agent.NewToolContext(ctx)version,err:=toolCtx.SaveArtifact("user:processed-text.txt",artifact)returnProcessTextOutput{ProcessedText:processedText,Version:version,},err}
Best Practices
Use Appropriate Namespaces: Use session-scoped artifacts for temporary data and user-persistent artifacts for data that should survive across sessions.
Set Proper MIME Types: Always specify the correct MIME type for your artifacts to ensure proper handling.
Handle Versions: Consider whether you need to track versions and use the versioning system appropriately.
Choose the Right Storage Backend: Use in-memory storage for development and cloud storage for production.
Error Handling: Always handle errors when saving and loading artifacts, as storage operations can fail.
Resource Management: Be mindful of storage costs and data lifecycle when using cloud storage backends.