-
Notifications
You must be signed in to change notification settings - Fork 891
.NET: Add Redis checkpoint store implementation for workflows #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ysya
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
ysya:issue-2401
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,310
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...amples/GettingStarted/Workflows/Checkpoint/CheckpointWithRedis/CheckpointWithRedis.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <NoWarn>$(NoWarn);MEAI001</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Redis\Microsoft.Agents.AI.Redis.csproj" /> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
150 changes: 150 additions & 0 deletions
150
dotnet/samples/GettingStarted/Workflows/Checkpoint/CheckpointWithRedis/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.Agents.AI.Workflows; | ||
|
|
||
| namespace CheckpointWithRedis; | ||
|
|
||
| /// <summary> | ||
| /// This sample demonstrates how to use Redis-backed checkpoint storage for workflows. | ||
| /// Key concepts: | ||
| /// - RedisCheckpointStore: A distributed, durable checkpoint store using Redis | ||
| /// - TTL (Time-To-Live): Automatic expiration of checkpoints | ||
| /// - Parent-child relationships: Linking checkpoints to track workflow history | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Pre-requisites: | ||
| /// - Redis must be running. Start it with: docker run --name redis -p 6379:6379 -d redis:7-alpine | ||
| /// - Or set REDIS_CONNECTION_STRING environment variable to your Redis instance. | ||
| /// </remarks> | ||
| public static class Program | ||
| { | ||
| public static async Task<int> Main() | ||
| { | ||
| // Configuration | ||
| var redisConnectionString = Environment.GetEnvironmentVariable("REDIS_CONNECTION_STRING") ?? "localhost:6379"; | ||
| var ttl = TimeSpan.FromHours(24); | ||
|
|
||
| Console.WriteLine("=== Redis Checkpoint Storage Demo ===\n"); | ||
| Console.WriteLine($"Connecting to Redis: {redisConnectionString}"); | ||
|
|
||
| try | ||
| { | ||
| // Create checkpoint store with TTL | ||
| using var checkpointStore = RedisWorkflowExtensions.CreateRedisCheckpointStoreWithTtl( | ||
| redisConnectionString, | ||
| ttl, | ||
| keyPrefix: "workflow_checkpoints"); | ||
|
|
||
| Console.WriteLine($"Key prefix: {checkpointStore.KeyPrefix}"); | ||
| Console.WriteLine($"TTL: {checkpointStore.TimeToLive}\n"); | ||
|
|
||
| // Sample workflow data | ||
| var runId = $"run_{Guid.NewGuid():N}"; | ||
| var workflowState = new WorkflowState | ||
| { | ||
| CurrentStep = "initialize", | ||
| Variables = new Dictionary<string, object> | ||
| { | ||
| ["user_input"] = "Hello, Agent!", | ||
| ["timestamp"] = DateTimeOffset.UtcNow.ToString("o") | ||
| } | ||
| }; | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Create initial checkpoint | ||
| Console.WriteLine("--- Creating Initial Checkpoint ---"); | ||
| var initialCheckpoint = await checkpointStore.CreateCheckpointAsync( | ||
| runId, | ||
| JsonSerializer.SerializeToElement(workflowState)); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Console.WriteLine($"Run ID: {runId}"); | ||
| Console.WriteLine($"Checkpoint ID: {initialCheckpoint.CheckpointId}"); | ||
| Console.WriteLine($"State: {workflowState.CurrentStep}\n"); | ||
|
|
||
| // Simulate workflow progress | ||
| workflowState.CurrentStep = "processing"; | ||
| workflowState.Variables["processed"] = true; | ||
| workflowState.Variables["processing_time"] = DateTimeOffset.UtcNow.ToString("o"); | ||
|
|
||
| // Create child checkpoint (linked to parent) | ||
| Console.WriteLine("--- Creating Child Checkpoint ---"); | ||
| var processingCheckpoint = await checkpointStore.CreateCheckpointAsync( | ||
| runId, | ||
| JsonSerializer.SerializeToElement(workflowState), | ||
| parent: initialCheckpoint); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Console.WriteLine($"Checkpoint ID: {processingCheckpoint.CheckpointId}"); | ||
| Console.WriteLine($"Parent ID: {initialCheckpoint.CheckpointId}"); | ||
| Console.WriteLine($"State: {workflowState.CurrentStep}\n"); | ||
|
|
||
| // Simulate more progress | ||
| workflowState.CurrentStep = "completed"; | ||
| workflowState.Variables["result"] = "Success!"; | ||
| workflowState.Variables["completion_time"] = DateTimeOffset.UtcNow.ToString("o"); | ||
|
|
||
| // Create final checkpoint | ||
| Console.WriteLine("--- Creating Final Checkpoint ---"); | ||
| var finalCheckpoint = await checkpointStore.CreateCheckpointAsync( | ||
| runId, | ||
| JsonSerializer.SerializeToElement(workflowState), | ||
| parent: processingCheckpoint); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Console.WriteLine($"Checkpoint ID: {finalCheckpoint.CheckpointId}"); | ||
| Console.WriteLine($"State: {workflowState.CurrentStep}\n"); | ||
|
|
||
| // List all checkpoints for the run | ||
| Console.WriteLine("--- All Checkpoints for Run ---"); | ||
| var allCheckpoints = await checkpointStore.RetrieveIndexAsync(runId); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var checkpointList = allCheckpoints.ToList(); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Total checkpoints: {checkpointList.Count}"); | ||
| foreach (var cp in checkpointList) | ||
| { | ||
| Console.WriteLine($" - {cp.CheckpointId}"); | ||
| } | ||
|
|
||
| Console.WriteLine(); | ||
|
|
||
| // List children of the initial checkpoint | ||
| Console.WriteLine("--- Children of Initial Checkpoint ---"); | ||
| var children = await checkpointStore.RetrieveIndexAsync(runId, initialCheckpoint); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var childList = children.ToList(); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Child checkpoints: {childList.Count}"); | ||
| foreach (var child in childList) | ||
| { | ||
| Console.WriteLine($" - {child.CheckpointId}"); | ||
| } | ||
|
|
||
| Console.WriteLine(); | ||
|
|
||
| // Retrieve and display checkpoint data | ||
| Console.WriteLine("--- Retrieving Final Checkpoint Data ---"); | ||
| var retrievedData = await checkpointStore.RetrieveCheckpointAsync(runId, finalCheckpoint); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Current step: {retrievedData.GetProperty("CurrentStep").GetString()}"); | ||
|
|
||
| var variables = retrievedData.GetProperty("Variables"); | ||
ysya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine($"User input: {variables.GetProperty("user_input").GetString()}"); | ||
| Console.WriteLine($"Result: {variables.GetProperty("result").GetString()}"); | ||
| Console.WriteLine($"Processed: {variables.GetProperty("processed").GetBoolean()}"); | ||
|
|
||
| Console.WriteLine("\n=== Demo Complete ==="); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"\nError: {ex.Message}"); | ||
| Console.WriteLine("\nMake sure Redis is running. You can start it with:"); | ||
| Console.WriteLine(" docker run --name redis -p 6379:6379 -d redis:7-alpine"); | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sample workflow state class. | ||
| /// </summary> | ||
| public class WorkflowState | ||
| { | ||
| public string CurrentStep { get; set; } = string.Empty; | ||
| public Dictionary<string, object> Variables { get; set; } = new(); | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
dotnet/src/Microsoft.Agents.AI.Redis/Microsoft.Agents.AI.Redis.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(TargetFrameworksCore)</TargetFrameworks> | ||
| <RootNamespace>Microsoft.Agents.AI</RootNamespace> | ||
| <NoWarn>$(NoWarn);MEAI001</NoWarn> | ||
| <VersionSuffix>preview</VersionSuffix> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <InjectSharedThrow>true</InjectSharedThrow> | ||
| <InjectDiagnosticClassesOnLegacy>true</InjectDiagnosticClassesOnLegacy> | ||
| <InjectTrimAttributesOnLegacy>true</InjectTrimAttributesOnLegacy> | ||
| <InjectIsExternalInitOnLegacy>true</InjectIsExternalInitOnLegacy> | ||
| <InjectRequiredMemberOnLegacy>true</InjectRequiredMemberOnLegacy> | ||
| <InjectCompilerFeatureRequiredOnLegacy>true</InjectCompilerFeatureRequiredOnLegacy> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" /> | ||
|
|
||
| <PropertyGroup> | ||
| <!-- NuGet Package Settings --> | ||
| <Title>Microsoft Agent Framework Redis Integration</Title> | ||
| <Description>Provides Redis implementations for Microsoft Agent Framework storage abstractions including CheckpointStore.</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Microsoft.Agents.AI.Abstractions\Microsoft.Agents.AI.Abstractions.csproj" /> | ||
| <ProjectReference Include="..\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="StackExchange.Redis" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="Microsoft.Agents.AI.Redis.UnitTests" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.