Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

9 Commits

Repository files navigation

Supermemory .NET SDK

A rock-solid, AOT-compatible .NET SDK for Supermemory - the AI memory layer for your applications.

NuGetNuGetLicense: MIT

Features

  • Full API Coverage - Complete access to Supermemory's Documents, Search, Memories, Connections, Settings, and Profile APIs
  • AOT Compatible - Fully supports ahead-of-time compilation and trimming with zero reflection in configuration binding
  • Multi-targeting - Supports .NET 8, .NET 9, and .NET 10
  • Modern Configuration - Uses IOptions<T> pattern with AOT-compatible LoadFrom() methods for configuration from any .NET source
  • IHttpClientFactory - Proper HttpClient lifecycle management via typed client pattern
  • Dependency Injection - First-class support for Microsoft.Extensions.DependencyInjection with AddSupermemory() and AddSupermemoryAgent()
  • Microsoft Agent Framework Integration - Seamless integration with Microsoft Agent Framework for AI agents with persistent memory
  • Strongly Typed - Full IntelliSense support with comprehensive XML documentation
  • Exception Hierarchy - Granular exception types for precise error handling

Packages

PackageDescription
SupermemoryCore API client for Supermemory
CloudNimble.Agents.AI.SupermemoryMicrosoft Agent Framework integration

Installation

Core SDK

dotnet add package Supermemory

Microsoft Agent Framework Integration

dotnet add package CloudNimble.Agents.AI.Supermemory

Quick Start

Configuration

The SDK uses the standard .NET configuration pattern. Configuration can come from any source:

  • appsettings.json / appsettings.{Environment}.json
  • Environment variables (e.g., Supermemory__ApiKey)
  • User secrets (for local development)
  • Azure Key Vault, AWS Secrets Manager, etc.
  • Command line arguments

appsettings.json:

{
"Supermemory": {
"ApiKey": "your-api-key",
"BaseUrl": "https://api.supermemory.ai",
"Timeout": "00:01:00",
"MaxRetries": 2
}
}

Or via user secrets (recommended for development):

dotnet user-secrets set"Supermemory:ApiKey""your-api-key"

Or via environment variables:

# Linux/macOSexport Supermemory__ApiKey=your-api-key
# Windowsset Supermemory__ApiKey=your-api-key

Dependency Injection

usingCloudNimble.Supermemory;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Hosting;varbuilder=Host.CreateApplicationBuilder(args);// Add Supermemory - configuration is automatically bound from the "Supermemory" sectionbuilder.Services.AddSupermemory();// Or with programmatic configuration overlaybuilder.Services.AddSupermemory(options =>{options.Timeout=TimeSpan.FromSeconds(120);options.MaxRetries=5;});usingvarhost=builder.Build();// Inject where neededvarclient=host.Services.GetRequiredService<SupermemoryClient>();

Using the Client

usingCloudNimble.Supermemory;usingCloudNimble.Supermemory.Models.Documents;usingCloudNimble.Supermemory.Models.Search;// Inject via constructorpublicclassMyService(SupermemoryClientsupermemory){publicasyncTaskAddAndSearchAsync(){// Add a documentvaraddResponse=awaitsupermemory.Documents.AddAsync(newAddDocumentRequest{Content="Supermemory is the AI memory layer for your applications.",ContainerTag="my-app",Metadata=newDictionary<string,object>{["source"]="documentation",["category"]="overview"}});Console.WriteLine($"Document added: {addResponse.Id}");// Search for relevant contentvarsearchResponse=awaitsupermemory.Search.SearchDocumentsAsync(newSearchDocumentsRequest{Query="AI memory layer",Limit=5,Rerank=true});foreach(varresultinsearchResponse.Results){Console.WriteLine($"Found: {result.DocumentId} (score: {result.Score:F3})");}}}

API Resources

The SupermemoryClient provides access to all Supermemory API resources:

ResourceDescription
client.DocumentsAdd, update, delete, and list documents
client.SearchSearch documents and memories with semantic search
client.MemoriesUpdate and forget memories
client.ConnectionsManage external connections (Notion, Google Drive, etc.)
client.SettingsConfigure organization settings
client.ProfileGet user profiles with static and dynamic memories

Documents

// Add a documentvarresponse=awaitclient.Documents.AddAsync(newAddDocumentRequest{Content="Your content here",ContainerTag="my-container"});// Add documents in batchvarbatchResponse=awaitclient.Documents.BatchAddAsync(newBatchAddDocumentsRequest{ContainerTag="my-container",Documents=[new(){Content="Document 1"},new(){Content="Document 2"}]});// Get a documentvardocument=awaitclient.Documents.GetAsync("document-id");// List documentsvarlist=awaitclient.Documents.ListAsync(newListDocumentsRequest{ContainerTags=["my-container"],Limit=20});// Update a documentawaitclient.Documents.UpdateAsync("document-id",newUpdateDocumentRequest{Content="Updated content"});// Delete a documentawaitclient.Documents.DeleteAsync("document-id");// Bulk deleteawaitclient.Documents.DeleteBulkAsync(newDeleteBulkRequest{ContainerTags=["container-to-delete"]});

Search

// Search documents (v3 API)varresults=awaitclient.Search.SearchDocumentsAsync(newSearchDocumentsRequest{Query="your search query",Limit=10,Rerank=true,ContainerTags=["my-container"]});// Search memories (v4 API - low latency)varmemories=awaitclient.Search.SearchMemoriesAsync(newSearchMemoriesRequest{Query="your search query",ContainerTag="my-container",SearchMode=SearchMode.Memories});

Profile

// Get user profile with static and dynamic memoriesvarprofile=awaitclient.Profile.GetAsync(newProfileRequest{ContainerTags=["user-123"],Query="current context"// Optional: for contextual retrieval});Console.WriteLine("Static memories (long-term facts):");foreach(varmemoryinprofile.Profile.Static){Console.WriteLine($" - {memory}");}Console.WriteLine("Dynamic memories (recent context):");foreach(varmemoryinprofile.Profile.Dynamic){Console.WriteLine($" - {memory}");}

Exception Handling

The SDK provides a granular exception hierarchy for precise error handling:

try{varresponse=awaitclient.Documents.GetAsync("document-id");}catch(SupermemoryAuthenticationExceptionex){// 401 Unauthorized - Invalid or missing API keyConsole.WriteLine($"Authentication failed: {ex.Message}");}catch(SupermemoryNotFoundExceptionex){// 404 Not Found - Resource doesn't existConsole.WriteLine($"Document not found: {ex.Message}");}catch(SupermemoryValidationExceptionex){// 400 Bad Request - Invalid request parametersConsole.WriteLine($"Validation error: {ex.Message}");Console.WriteLine($"Details: {ex.ApiError?.Details}");}catch(SupermemoryRateLimitExceptionex){// 429 Too Many Requests - Rate limit exceededConsole.WriteLine($"Rate limited: {ex.Message}");}catch(SupermemoryApiExceptionex){// Other API errors with status codeConsole.WriteLine($"API error ({ex.StatusCode}): {ex.Message}");}catch(SupermemoryExceptionex){// Base exception for all Supermemory errorsConsole.WriteLine($"Error: {ex.Message}");}

Microsoft Agent Framework Integration

The CloudNimble.Agents.AI.Supermemory package provides seamless integration with Microsoft Agent Framework, enabling AI agents with persistent memory.

Two Complementary Providers

ProviderPurposeSupermemory APIs Used
SupermemoryContextProviderSemantic memory injectionProfile API + Search API
SupermemoryChatHistoryProviderConversation persistenceDocuments API

Quick Setup (Recommended: Full DI Integration)

usingCloudNimble.Supermemory;usingCloudNimble.Agents.AI.Supermemory;usingMicrosoft.Agents.AI;usingMicrosoft.Extensions.AI;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Hosting;varbuilder=Host.CreateApplicationBuilder(args);// Add Supermemory client (binds from "Supermemory" config section)builder.Services.AddSupermemory();// Add your chat client (e.g., Azure OpenAI)builder.Services.AddSingleton<IChatClient>(sp =>sp.GetRequiredService<AzureOpenAIClient>().GetChatClient("gpt-4o-mini").AsIChatClient());// Add Supermemory-enabled agent - all configuration from appsettings.jsonbuilder.Services.AddSupermemoryAgent(configureAgent: options =>options.Name="MemoryAgent",configureContext: options =>options.EnableConversationStorage=true,configureHistory: options =>options.MaxMessages=100);usingvarhost=builder.Build();// Get the fully-configured agent from DIvaragent=host.Services.GetRequiredService<ChatClientAgent>();// Start a conversation - the agent has persistent memory across sessions!varsession=awaitagent.GetNewSessionAsync();varresponse=awaitagent.RunAsync("My name is Alice and I love cats.",session);

appsettings.json:

{
"Supermemory": {
"ApiKey": "your-api-key"
},
"SupermemoryContext": {
"DefaultContainerTag": "user-{userId}",
"RetrievalStrategy": "ProfileFirst",
"SearchLimit": 5
},
"SupermemoryHistory": {
"DefaultContainerTag": "user-{userId}",
"MaxMessages": 50
}
}

Manual Setup (Advanced)

For scenarios where you need more control:

// Get clients from DIvarsupermemoryClient=host.Services.GetRequiredService<SupermemoryClient>();varchatClient=/* your chat client */;// Create agent with Supermemory integration manuallyvaragent=chatClient.AsAIAgent(newChatClientAgentOptions{Name="MemoryAgent",Description="An AI agent with persistent memory"}.WithSupermemory(supermemoryClient,contextOptions:newSupermemoryContextProviderOptions{DefaultContainerTag="user-{userId}",RetrievalStrategy=MemoryRetrievalStrategy.ProfileFirst,EnableConversationStorage=true},historyOptions:newSupermemoryChatHistoryProviderOptions{DefaultContainerTag="user-{userId}",MaxMessages=50}));

Context Provider Configuration

varcontextOptions=newSupermemoryContextProviderOptions{// Container tag with template placeholdersDefaultContainerTag="tenant-{tenantId}-user-{userId}",// Memory retrieval strategyRetrievalStrategy=MemoryRetrievalStrategy.ProfileFirst,// or SearchOnly, ProfileOnly, Both// Profile API settingsUseProfileApi=true,ProfileThreshold=0.7,// Search API settingsUseSearchApi=true,SearchMode=SearchMode.Memories,SearchLimit=5,MinimumSimilarityScore=0.7,RerankResults=false,// Conversation storage (for automatic memory extraction)EnableConversationStorage=true,StorageFormat=ConversationStorageFormat.Markdown,StoreUserMessagesOnly=false,// Custom instruction templateInstructionTemplate=""" ## User Context ### Known Facts {staticMemories} ### Recent Context {dynamicMemories} ### Relevant Memories {searchMemories} """};

Chat History Provider Configuration

varhistoryOptions=newSupermemoryChatHistoryProviderOptions{// Container tag for conversation isolationDefaultContainerTag="session-{sessionId}",// Message limitsMaxMessages=50,// Document storage settingsDocumentIdPrefix="chat-",StoreContextProviderMessages=false};

Dynamic Container Tags

Switch user context during a conversation:

varsession=awaitagent.GetNewSessionAsync();// Get the context providervarcontextProvider=session.GetService<SupermemoryContextProvider>();// Change container tag mid-conversationcontextProvider.ContainerTag="user-456";// Or use template resolutionvarresolver=newDefaultContainerTagResolver();contextProvider.State.ContainerTag=resolver.Resolve("tenant-{tenantId}-user-{userId}",newContainerTagContext{TenantId="acme",UserId="john"});

Session Serialization

Serialize and resume sessions:

// Serialize the sessionvarserializedSession=session.Serialize();// Store serializedSession (e.g., in Redis, database)// Later, resume the sessionvarresumedSession=awaitagent.DeserializeSessionAsync(serializedSession);// Continue the conversation with full context preservedvarresponse=awaitagent.RunAsync("What's my name?",resumedSession);

Configuration Options

SupermemoryClientOptions

PropertyTypeDefaultDescription
ApiKeystring?nullYour Supermemory API key (required)
BaseUrlstring"https://api.supermemory.ai"API base URL
TimeoutTimeSpan00:01:00HTTP request timeout
MaxRetriesint2Maximum retry attempts for failed requests

Configuration Section Names

Each options class exposes a SectionName constant for the default configuration section:

Options ClassSection Name
SupermemoryClientOptions.SectionName"Supermemory"
SupermemoryContextProviderOptions.SectionName"SupermemoryContext"
SupermemoryChatHistoryProviderOptions.SectionName"SupermemoryHistory"

You can customize the section name:

// Use a different configuration section with programmatic overridesbuilder.Services.AddSupermemory("MyCustomSection", options =>{options.Timeout=TimeSpan.FromSeconds(120);});

AOT-Compatible Configuration

All configuration uses AOT-compatible manual binding via LoadFrom() methods - no reflection required:

// The SDK automatically calls LoadFrom() internally, but you can also use it directly:varoptions=newSupermemoryClientOptions();options.LoadFrom(configuration.GetSection(SupermemoryClientOptions.SectionName));

Samples

The repository includes sample applications:

  • CloudNimble.Supermemory.Samples.Console - Basic SDK usage examples
  • CloudNimble.Agents.AI.Supermemory.Samples - Microsoft Agent Framework integration demo

Run the samples:

# Configure API key via user secretscd src/CloudNimble.Supermemory.Samples.Console
dotnet user-secrets set"Supermemory:ApiKey""your-api-key"
dotnet run
# Run the agent framework sample (requires Azure OpenAI)cd src/CloudNimble.Agents.AI.Supermemory.Samples
dotnet user-secrets set"Supermemory:ApiKey""your-api-key"
dotnet user-secrets set"AzureOpenAI:Endpoint""https://your-resource.openai.azure.com"
dotnet run

Requirements

  • .NET 8.0, .NET 9.0, or .NET 10.0
  • A Supermemory API key (Get one here)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages